NetBSD/sys/dev/ieee1394/if_fwip.c

898 lines
23 KiB
C
Raw Normal View History

2020-01-29 09:17:07 +03:00
/* $NetBSD: if_fwip.c,v 1.30 2020/01/29 06:19:39 thorpej Exp $ */
2005-07-11 19:29:05 +04:00
/*-
* Copyright (c) 2004
* Doug Rabson
* Copyright (c) 2002-2003
* Hidetoshi Shimokawa. All rights reserved.
2010-03-29 07:05:27 +04:00
*
2005-07-11 19:29:05 +04:00
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
*
* This product includes software developed by Hidetoshi Shimokawa.
*
* 4. Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
2010-03-29 07:05:27 +04:00
*
2005-07-11 19:29:05 +04:00
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
2010-03-29 07:05:27 +04:00
*
* $FreeBSD: src/sys/dev/firewire/if_fwip.c,v 1.18 2009/02/09 16:58:18 fjoe Exp $
2005-07-11 19:29:05 +04:00
*/
2007-12-11 14:25:46 +03:00
#include <sys/cdefs.h>
2020-01-29 09:17:07 +03:00
__KERNEL_RCSID(0, "$NetBSD: if_fwip.c,v 1.30 2020/01/29 06:19:39 thorpej Exp $");
2005-07-11 19:29:05 +04:00
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/malloc.h>
2005-07-11 19:29:05 +04:00
#include <sys/mbuf.h>
2010-03-29 07:05:27 +04:00
#include <sys/mutex.h>
2005-07-11 19:29:05 +04:00
#include <sys/sysctl.h>
2010-03-29 07:05:27 +04:00
#include <net/bpf.h>
2005-07-11 19:29:05 +04:00
#include <net/if.h>
#include <net/if_ieee1394.h>
#include <net/if_types.h>
2005-07-11 19:29:05 +04:00
#include <dev/ieee1394/firewire.h>
#include <dev/ieee1394/firewirereg.h>
#include <dev/ieee1394/iec13213.h>
#include <dev/ieee1394/if_fwipvar.h>
/*
* We really need a mechanism for allocating regions in the FIFO
* address space. We pick a address in the OHCI controller's 'middle'
* address space. This means that the controller will automatically
* send responses for us, which is fine since we don't have any
* important information to put in the response anyway.
*/
#define INET_FIFO 0xfffe00000000LL
#define FWIPDEBUG if (fwipdebug) aprint_debug_ifnet
2005-07-11 19:29:05 +04:00
#define TX_MAX_QUEUE (FWMAXQUEUE - 1)
2010-03-29 07:05:27 +04:00
struct fw_hwaddr {
uint32_t sender_unique_ID_hi;
uint32_t sender_unique_ID_lo;
uint8_t sender_max_rec;
uint8_t sspd;
uint16_t sender_unicast_FIFO_hi;
uint32_t sender_unicast_FIFO_lo;
};
static int fwipmatch(device_t, cfdata_t, void *);
static void fwipattach(device_t, device_t, void *);
static int fwipdetach(device_t, int);
static int fwipactivate(device_t, enum devact);
2005-07-11 19:29:05 +04:00
/* network interface */
2010-03-29 07:05:27 +04:00
static void fwip_start(struct ifnet *);
static int fwip_ioctl(struct ifnet *, u_long, void *);
static int fwip_init(struct ifnet *);
static void fwip_stop(struct ifnet *, int);
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
static void fwip_post_busreset(void *);
static void fwip_output_callback(struct fw_xfer *);
static void fwip_async_output(struct fwip_softc *, struct ifnet *);
static void fwip_stream_input(struct fw_xferq *);
2005-07-11 19:29:05 +04:00
static void fwip_unicast_input(struct fw_xfer *);
static int fwipdebug = 0;
static int broadcast_channel = 0xc0 | 0x1f; /* tag | channel(XXX) */
static int tx_speed = 2;
static int rx_queue_len = FWMAXQUEUE;
/*
* Setup sysctl(3) MIB, hw.fwip.*
*
* TBD condition CTLFLAG_PERMANENT on being a module or not
2005-07-11 19:29:05 +04:00
*/
SYSCTL_SETUP(sysctl_fwip, "sysctl fwip(4) subtree setup")
{
int rc, fwip_node_num;
const struct sysctlnode *node;
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT, CTLTYPE_NODE, "fwip",
SYSCTL_DESCR("fwip controls"),
NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
goto err;
}
fwip_node_num = node->sysctl_num;
/* fwip RX queue length */
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
"rx_queue_len", SYSCTL_DESCR("Length of the receive queue"),
NULL, 0, &rx_queue_len,
0, CTL_HW, fwip_node_num, CTL_CREATE, CTL_EOL)) != 0) {
goto err;
}
/* fwip RX queue length */
if ((rc = sysctl_createv(clog, 0, NULL, &node,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
"if_fwip_debug", SYSCTL_DESCR("fwip driver debug flag"),
NULL, 0, &fwipdebug,
0, CTL_HW, fwip_node_num, CTL_CREATE, CTL_EOL)) != 0) {
goto err;
}
return;
err:
2010-03-29 07:05:27 +04:00
aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
CFATTACH_DECL_NEW(fwip, sizeof(struct fwip_softc),
fwipmatch, fwipattach, fwipdetach, fwipactivate);
2005-07-11 19:29:05 +04:00
static int
fwipmatch(device_t parent, cfdata_t cf, void *aux)
2005-07-11 19:29:05 +04:00
{
struct fw_attach_args *fwa = aux;
if (strcmp(fwa->name, "fwip") == 0)
2010-03-29 07:05:27 +04:00
return 1;
return 0;
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
static void
fwipattach(device_t parent, device_t self, void *aux)
2005-07-11 19:29:05 +04:00
{
2010-03-29 07:05:27 +04:00
struct fwip_softc *sc = device_private(self);
struct fw_attach_args *fwa = (struct fw_attach_args *)aux;
struct fw_hwaddr *hwaddr;
2005-07-11 19:29:05 +04:00
struct ifnet *ifp;
2010-03-29 07:05:27 +04:00
aprint_naive("\n");
aprint_normal(": IP over IEEE1394\n");
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
sc->sc_fd.dev = self;
sc->sc_eth.fwip_ifp = &sc->sc_eth.fwcom.fc_if;
hwaddr = (struct fw_hwaddr *)&sc->sc_eth.fwcom.ic_hwaddr;
2010-03-29 07:05:27 +04:00
ifp = sc->sc_eth.fwip_ifp;
mutex_init(&sc->sc_fwb.fwb_mtx, MUTEX_DEFAULT, IPL_NET);
mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NET);
2010-03-11 06:54:56 +03:00
2005-07-11 19:29:05 +04:00
/* XXX */
2010-03-29 07:05:27 +04:00
sc->sc_dma_ch = -1;
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
sc->sc_fd.fc = fwa->fc;
2005-07-11 19:29:05 +04:00
if (tx_speed < 0)
2010-03-29 07:05:27 +04:00
tx_speed = sc->sc_fd.fc->speed;
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
sc->sc_fd.post_explore = NULL;
sc->sc_fd.post_busreset = fwip_post_busreset;
sc->sc_eth.fwip = sc;
2005-07-11 19:29:05 +04:00
/*
* Encode our hardware the way that arp likes it.
*/
2010-03-29 07:05:27 +04:00
hwaddr->sender_unique_ID_hi = htonl(sc->sc_fd.fc->eui.hi);
hwaddr->sender_unique_ID_lo = htonl(sc->sc_fd.fc->eui.lo);
hwaddr->sender_max_rec = sc->sc_fd.fc->maxrec;
hwaddr->sspd = sc->sc_fd.fc->speed;
2005-07-11 19:29:05 +04:00
hwaddr->sender_unicast_FIFO_hi = htons((uint16_t)(INET_FIFO >> 32));
hwaddr->sender_unicast_FIFO_lo = htonl((uint32_t)INET_FIFO);
2010-03-29 07:05:27 +04:00
/* fill the rest and attach interface */
ifp->if_softc = &sc->sc_eth;
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
ifp->if_start = fwip_start;
ifp->if_ioctl = fwip_ioctl;
ifp->if_init = fwip_init;
ifp->if_stop = fwip_stop;
ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
2010-03-29 07:05:27 +04:00
IFQ_SET_READY(&ifp->if_snd);
IFQ_SET_MAXLEN(&ifp->if_snd, TX_MAX_QUEUE);
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
if_attach(ifp);
ieee1394_ifattach(ifp, (const struct ieee1394_hwaddr *)hwaddr);
2005-07-11 19:29:05 +04:00
2007-12-09 23:27:42 +03:00
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "couldn't establish power handler\n");
else
pmf_class_network_register(self, ifp);
2005-07-11 19:29:05 +04:00
FWIPDEBUG(ifp, "interface created\n");
2010-03-29 07:05:27 +04:00
return;
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
static int
fwipdetach(device_t self, int flags)
2005-07-11 19:29:05 +04:00
{
2010-03-29 07:05:27 +04:00
struct fwip_softc *sc = device_private(self);
struct ifnet *ifp = sc->sc_eth.fwip_ifp;
fwip_stop(sc->sc_eth.fwip_ifp, 1);
ieee1394_ifdetach(ifp);
if_detach(ifp);
mutex_destroy(&sc->sc_mtx);
mutex_destroy(&sc->sc_fwb.fwb_mtx);
return 0;
}
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
static int
fwipactivate(device_t self, enum devact act)
{
struct fwip_softc *sc = device_private(self);
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
switch (act) {
case DVACT_DEACTIVATE:
if_deactivate(sc->sc_eth.fwip_ifp);
return 0;
default:
return EOPNOTSUPP;
}
}
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
static void
fwip_start(struct ifnet *ifp)
{
struct fwip_softc *sc = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
FWIPDEBUG(ifp, "starting\n");
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
if (sc->sc_dma_ch < 0) {
struct mbuf *m = NULL;
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
FWIPDEBUG(ifp, "not ready\n");
do {
IF_DEQUEUE(&ifp->if_snd, m);
if (m != NULL)
m_freem(m);
2020-01-29 09:17:07 +03:00
if_statinc(ifp, if_oerrors);
2010-03-29 07:05:27 +04:00
} while (m != NULL);
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
return;
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
ifp->if_flags |= IFF_OACTIVE;
if (ifp->if_snd.ifq_len != 0)
fwip_async_output(sc, ifp);
ifp->if_flags &= ~IFF_OACTIVE;
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
static int
fwip_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2005-07-11 19:29:05 +04:00
{
2010-03-29 07:05:27 +04:00
int s, error = 0;
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
s = splnet();
2010-03-29 07:05:27 +04:00
switch (cmd) {
case SIOCSIFFLAGS:
if ((error = ifioctl_common(ifp, cmd, data)) != 0)
break;
switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
case IFF_RUNNING:
fwip_stop(ifp, 0);
break;
case IFF_UP:
fwip_init(ifp);
break;
default:
break;
}
break;
2010-03-29 07:05:27 +04:00
case SIOCADDMULTI:
case SIOCDELMULTI:
break;
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
default:
error = ieee1394_ioctl(ifp, cmd, data);
if (error == ENETRESET)
error = 0;
break;
}
2005-07-11 19:29:05 +04:00
splx(s);
2010-03-29 07:05:27 +04:00
return error;
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
static int
fwip_init(struct ifnet *ifp)
2005-07-11 19:29:05 +04:00
{
2010-03-29 07:05:27 +04:00
struct fwip_softc *sc = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
2005-07-11 19:29:05 +04:00
struct firewire_comm *fc;
struct fw_xferq *xferq;
struct fw_xfer *xfer;
struct mbuf *m;
int i;
FWIPDEBUG(ifp, "initializing\n");
2010-03-29 07:05:27 +04:00
fc = sc->sc_fd.fc;
if (sc->sc_dma_ch < 0) {
sc->sc_dma_ch = fw_open_isodma(fc, /* tx */0);
if (sc->sc_dma_ch < 0)
return ENXIO;
xferq = fc->ir[sc->sc_dma_ch];
xferq->flag |=
FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_STREAM;
2005-07-11 19:29:05 +04:00
xferq->flag &= ~0xff;
xferq->flag |= broadcast_channel & 0xff;
/* register fwip_input handler */
2010-03-29 07:05:27 +04:00
xferq->sc = (void *) sc;
2005-07-11 19:29:05 +04:00
xferq->hand = fwip_stream_input;
xferq->bnchunk = rx_queue_len;
xferq->bnpacket = 1;
xferq->psize = MCLBYTES;
xferq->queued = 0;
xferq->buf = NULL;
xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
sizeof(struct fw_bulkxfer) * xferq->bnchunk,
M_FW, M_WAITOK);
2005-07-11 19:29:05 +04:00
if (xferq->bulkxfer == NULL) {
aprint_error_ifnet(ifp, "if_fwip: malloc failed\n");
2010-03-29 07:05:27 +04:00
return ENOMEM;
2005-07-11 19:29:05 +04:00
}
STAILQ_INIT(&xferq->stvalid);
STAILQ_INIT(&xferq->stfree);
STAILQ_INIT(&xferq->stdma);
xferq->stproc = NULL;
2010-03-29 07:05:27 +04:00
for (i = 0; i < xferq->bnchunk; i++) {
m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
2005-07-11 19:29:05 +04:00
xferq->bulkxfer[i].mbuf = m;
if (m != NULL) {
m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
STAILQ_INSERT_TAIL(&xferq->stfree,
&xferq->bulkxfer[i], link);
} else
2010-03-29 07:05:27 +04:00
aprint_error_ifnet(ifp,
"fwip_as_input: m_getcl failed\n");
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
sc->sc_fwb.start = INET_FIFO;
sc->sc_fwb.end = INET_FIFO + 16384; /* S3200 packet size */
2005-07-11 19:29:05 +04:00
/* pre-allocate xfer */
2010-03-29 07:05:27 +04:00
STAILQ_INIT(&sc->sc_fwb.xferlist);
for (i = 0; i < rx_queue_len; i++) {
xfer = fw_xfer_alloc(M_FW);
2005-07-11 19:29:05 +04:00
if (xfer == NULL)
break;
2010-03-29 07:05:27 +04:00
m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
2005-07-11 19:29:05 +04:00
xfer->recv.payload = mtod(m, uint32_t *);
xfer->recv.pay_len = MCLBYTES;
xfer->hand = fwip_unicast_input;
xfer->fc = fc;
2010-03-29 07:05:27 +04:00
xfer->sc = (void *) sc;
2005-07-11 19:29:05 +04:00
xfer->mbuf = m;
2010-03-29 07:05:27 +04:00
STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link);
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
fw_bindadd(fc, &sc->sc_fwb);
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
STAILQ_INIT(&sc->sc_xferlist);
2005-07-11 19:29:05 +04:00
for (i = 0; i < TX_MAX_QUEUE; i++) {
xfer = fw_xfer_alloc(M_FW);
2005-07-11 19:29:05 +04:00
if (xfer == NULL)
break;
xfer->send.spd = tx_speed;
2010-03-29 07:05:27 +04:00
xfer->fc = sc->sc_fd.fc;
xfer->sc = (void *)sc;
2005-07-11 19:29:05 +04:00
xfer->hand = fwip_output_callback;
2010-03-29 07:05:27 +04:00
STAILQ_INSERT_TAIL(&sc->sc_xferlist, xfer, link);
2005-07-11 19:29:05 +04:00
}
} else
2010-03-29 07:05:27 +04:00
xferq = fc->ir[sc->sc_dma_ch];
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
sc->sc_last_dest.hi = 0;
sc->sc_last_dest.lo = 0;
2005-07-11 19:29:05 +04:00
/* start dma */
if ((xferq->flag & FWXFERQ_RUNNING) == 0)
2010-03-29 07:05:27 +04:00
fc->irx_enable(fc, sc->sc_dma_ch);
2005-07-11 19:29:05 +04:00
ifp->if_flags |= IFF_RUNNING;
ifp->if_flags &= ~IFF_OACTIVE;
#if 0
/* attempt to start output */
fwip_start(ifp);
#endif
2010-03-29 07:05:27 +04:00
return 0;
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
static void
fwip_stop(struct ifnet *ifp, int disable)
2005-07-11 19:29:05 +04:00
{
2010-03-29 07:05:27 +04:00
struct fwip_softc *sc = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
struct firewire_comm *fc = sc->sc_fd.fc;
struct fw_xferq *xferq;
struct fw_xfer *xfer, *next;
int i;
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
if (sc->sc_dma_ch >= 0) {
xferq = fc->ir[sc->sc_dma_ch];
if (xferq->flag & FWXFERQ_RUNNING)
fc->irx_disable(fc, sc->sc_dma_ch);
xferq->flag &=
~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
xferq->hand = NULL;
for (i = 0; i < xferq->bnchunk; i++)
m_freem(xferq->bulkxfer[i].mbuf);
free(xferq->bulkxfer, M_FW);
2010-03-29 07:05:27 +04:00
fw_bindremove(fc, &sc->sc_fwb);
for (xfer = STAILQ_FIRST(&sc->sc_fwb.xferlist); xfer != NULL;
xfer = next) {
next = STAILQ_NEXT(xfer, link);
fw_xfer_free(xfer);
}
2010-03-29 07:05:27 +04:00
for (xfer = STAILQ_FIRST(&sc->sc_xferlist); xfer != NULL;
xfer = next) {
next = STAILQ_NEXT(xfer, link);
fw_xfer_free(xfer);
}
2005-07-11 19:29:05 +04:00
2010-03-29 07:05:27 +04:00
xferq->bulkxfer = NULL;
sc->sc_dma_ch = -1;
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2005-07-11 19:29:05 +04:00
}
static void
fwip_post_busreset(void *arg)
{
2010-03-29 07:05:27 +04:00
struct fwip_softc *sc = arg;
2005-07-11 19:29:05 +04:00
struct crom_src *src;
struct crom_chunk *root;
2010-03-29 07:05:27 +04:00
src = sc->sc_fd.fc->crom_src;
root = sc->sc_fd.fc->crom_root;
2005-07-11 19:29:05 +04:00
/* RFC2734 IPv4 over IEEE1394 */
2010-03-29 07:05:27 +04:00
memset(&sc->sc_unit4, 0, sizeof(struct crom_chunk));
crom_add_chunk(src, root, &sc->sc_unit4, CROM_UDIR);
crom_add_entry(&sc->sc_unit4, CSRKEY_SPEC, CSRVAL_IETF);
crom_add_simple_text(src, &sc->sc_unit4, &sc->sc_spec4, "IANA");
crom_add_entry(&sc->sc_unit4, CSRKEY_VER, 1);
crom_add_simple_text(src, &sc->sc_unit4, &sc->sc_ver4, "IPv4");
2005-07-11 19:29:05 +04:00
/* RFC3146 IPv6 over IEEE1394 */
2010-03-29 07:05:27 +04:00
memset(&sc->sc_unit6, 0, sizeof(struct crom_chunk));
crom_add_chunk(src, root, &sc->sc_unit6, CROM_UDIR);
crom_add_entry(&sc->sc_unit6, CSRKEY_SPEC, CSRVAL_IETF);
crom_add_simple_text(src, &sc->sc_unit6, &sc->sc_spec6, "IANA");
crom_add_entry(&sc->sc_unit6, CSRKEY_VER, 2);
crom_add_simple_text(src, &sc->sc_unit6, &sc->sc_ver6, "IPv6");
sc->sc_last_dest.hi = 0;
sc->sc_last_dest.lo = 0;
ieee1394_drain(sc->sc_eth.fwip_ifp);
2005-07-11 19:29:05 +04:00
}
static void
fwip_output_callback(struct fw_xfer *xfer)
{
2010-03-29 07:05:27 +04:00
struct fwip_softc *sc = (struct fwip_softc *)xfer->sc;
2005-07-11 19:29:05 +04:00
struct ifnet *ifp;
2010-03-29 07:05:27 +04:00
ifp = sc->sc_eth.fwip_ifp;
2005-07-11 19:29:05 +04:00
/* XXX error check */
FWIPDEBUG(ifp, "resp = %d\n", xfer->resp);
if (xfer->resp != 0)
2020-01-29 09:17:07 +03:00
if_statinc(ifp, if_oerrors);
2010-03-29 07:05:27 +04:00
2005-07-11 19:29:05 +04:00
m_freem(xfer->mbuf);
fw_xfer_unload(xfer);
2010-03-29 07:05:27 +04:00
mutex_enter(&sc->sc_mtx);
STAILQ_INSERT_TAIL(&sc->sc_xferlist, xfer, link);
mutex_exit(&sc->sc_mtx);
2005-07-11 19:29:05 +04:00
/* for queue full */
2010-03-29 07:05:27 +04:00
if (ifp->if_snd.ifq_head != NULL)
2005-07-11 19:29:05 +04:00
fwip_start(ifp);
}
/* Async. stream output */
static void
2010-03-29 07:05:27 +04:00
fwip_async_output(struct fwip_softc *sc, struct ifnet *ifp)
2005-07-11 19:29:05 +04:00
{
2010-03-29 07:05:27 +04:00
struct firewire_comm *fc = sc->sc_fd.fc;
2005-07-11 19:29:05 +04:00
struct mbuf *m;
struct m_tag *mtag;
struct fw_hwaddr *destfw;
struct fw_xfer *xfer;
struct fw_xferq *xferq;
struct fw_pkt *fp;
uint16_t nodeid;
int error;
int i = 0;
xfer = NULL;
xferq = fc->atq;
while ((xferq->queued < xferq->maxq - 1) &&
(ifp->if_snd.ifq_head != NULL)) {
2010-03-29 07:05:27 +04:00
mutex_enter(&sc->sc_mtx);
if (STAILQ_EMPTY(&sc->sc_xferlist)) {
mutex_exit(&sc->sc_mtx);
#if 0
2010-03-29 07:05:27 +04:00
aprint_normal("if_fwip: lack of xfer\n");
#endif
break;
2005-07-11 19:29:05 +04:00
}
IF_DEQUEUE(&ifp->if_snd, m);
if (m == NULL) {
2010-03-29 07:05:27 +04:00
mutex_exit(&sc->sc_mtx);
2005-07-11 19:29:05 +04:00
break;
}
2010-03-29 07:05:27 +04:00
xfer = STAILQ_FIRST(&sc->sc_xferlist);
STAILQ_REMOVE_HEAD(&sc->sc_xferlist, link);
mutex_exit(&sc->sc_mtx);
2005-07-11 19:29:05 +04:00
/*
* Dig out the link-level address which
* firewire_output got via arp or neighbour
* discovery. If we don't have a link-level address,
* just stick the thing on the broadcast channel.
*/
mtag = m_tag_find(m, MTAG_FIREWIRE_HWADDR);
2005-07-11 19:29:05 +04:00
if (mtag == NULL)
destfw = 0;
else
destfw = (struct fw_hwaddr *) (mtag + 1);
/*
* Put the mbuf in the xfer early in case we hit an
* error case below - fwip_output_callback will free
* the mbuf.
*/
xfer->mbuf = m;
/*
* We use the arp result (if any) to add a suitable firewire
* packet header before handing off to the bus.
*/
fp = &xfer->send.hdr;
nodeid = FWLOCALBUS | fc->nodeid;
if ((m->m_flags & M_BCAST) || !destfw) {
/*
* Broadcast packets are sent as GASP packets with
* specifier ID 0x00005e, version 1 on the broadcast
* channel. To be conservative, we send at the
* slowest possible speed.
*/
uint32_t *p;
2010-03-29 07:05:27 +04:00
M_PREPEND(m, 2 * sizeof(uint32_t), M_DONTWAIT);
2005-07-11 19:29:05 +04:00
p = mtod(m, uint32_t *);
fp->mode.stream.len = m->m_pkthdr.len;
fp->mode.stream.chtag = broadcast_channel;
fp->mode.stream.tcode = FWTCODE_STREAM;
fp->mode.stream.sy = 0;
xfer->send.spd = 0;
p[0] = htonl(nodeid << 16);
p[1] = htonl((0x5e << 24) | 1);
} else {
/*
* Unicast packets are sent as block writes to the
* target's unicast fifo address. If we can't
* find the node address, we just give up. We
* could broadcast it but that might overflow
* the packet size limitations due to the
* extra GASP header. Note: the hardware
* address is stored in network byte order to
* make life easier for ARP.
*/
struct fw_device *fd;
struct fw_eui64 eui;
eui.hi = ntohl(destfw->sender_unique_ID_hi);
eui.lo = ntohl(destfw->sender_unique_ID_lo);
2010-03-29 07:05:27 +04:00
if (sc->sc_last_dest.hi != eui.hi ||
sc->sc_last_dest.lo != eui.lo) {
2005-07-11 19:29:05 +04:00
fd = fw_noderesolve_eui64(fc, &eui);
if (!fd) {
/* error */
2020-01-29 09:17:07 +03:00
if_statinc(ifp, if_oerrors);
2005-07-11 19:29:05 +04:00
/* XXX set error code */
fwip_output_callback(xfer);
continue;
}
2010-03-29 07:05:27 +04:00
sc->sc_last_hdr.mode.wreqb.dst =
FWLOCALBUS | fd->dst;
sc->sc_last_hdr.mode.wreqb.tlrt = 0;
sc->sc_last_hdr.mode.wreqb.tcode =
FWTCODE_WREQB;
sc->sc_last_hdr.mode.wreqb.pri = 0;
sc->sc_last_hdr.mode.wreqb.src = nodeid;
sc->sc_last_hdr.mode.wreqb.dest_hi =
2005-07-11 19:29:05 +04:00
ntohs(destfw->sender_unicast_FIFO_hi);
2010-03-29 07:05:27 +04:00
sc->sc_last_hdr.mode.wreqb.dest_lo =
2005-07-11 19:29:05 +04:00
ntohl(destfw->sender_unicast_FIFO_lo);
2010-03-29 07:05:27 +04:00
sc->sc_last_hdr.mode.wreqb.extcode = 0;
sc->sc_last_dest = eui;
2005-07-11 19:29:05 +04:00
}
2010-03-29 07:05:27 +04:00
fp->mode.wreqb = sc->sc_last_hdr.mode.wreqb;
2005-07-11 19:29:05 +04:00
fp->mode.wreqb.len = m->m_pkthdr.len;
Rename min/max -> uimin/uimax for better honesty. These functions are defined on unsigned int. The generic name min/max should not silently truncate to 32 bits on 64-bit systems. This is purely a name change -- no functional change intended. HOWEVER! Some subsystems have #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) even though our standard name for that is MIN/MAX. Although these may invite multiple evaluation bugs, these do _not_ cause integer truncation. To avoid `fixing' these cases, I first changed the name in libkern, and then compile-tested every file where min/max occurred in order to confirm that it failed -- and thus confirm that nothing shadowed min/max -- before changing it. I have left a handful of bootloaders that are too annoying to compile-test, and some dead code: cobalt ews4800mips hp300 hppa ia64 luna68k vax acorn32/if_ie.c (not included in any kernels) macppc/if_gm.c (superseded by gem(4)) It should be easy to fix the fallout once identified -- this way of doing things fails safe, and the goal here, after all, is to _avoid_ silent integer truncations, not introduce them. Maybe one day we can reintroduce min/max as type-generic things that never silently truncate. But we should avoid doing that for a while, so that existing code has a chance to be detected by the compiler for conversion to uimin/uimax without changing the semantics until we can properly audit it all. (Who knows, maybe in some cases integer truncation is actually intended!)
2018-09-03 19:29:22 +03:00
xfer->send.spd = uimin(destfw->sspd, fc->speed);
2005-07-11 19:29:05 +04:00
}
xfer->send.pay_len = m->m_pkthdr.len;
error = fw_asyreq(fc, -1, xfer);
if (error == EAGAIN) {
/*
* We ran out of tlabels - requeue the packet
* for later transmission.
*/
xfer->mbuf = 0;
2010-03-29 07:05:27 +04:00
mutex_enter(&sc->sc_mtx);
STAILQ_INSERT_TAIL(&sc->sc_xferlist, xfer, link);
mutex_exit(&sc->sc_mtx);
2005-07-11 19:29:05 +04:00
IF_PREPEND(&ifp->if_snd, m);
break;
}
if (error) {
/* error */
2020-01-29 09:17:07 +03:00
if_statinc(ifp, if_oerrors);
2005-07-11 19:29:05 +04:00
/* XXX set error code */
fwip_output_callback(xfer);
continue;
} else {
2020-01-29 09:17:07 +03:00
if_statinc(ifp, if_opackets);
2005-07-11 19:29:05 +04:00
i++;
}
}
#if 0
if (i > 1)
2010-03-29 07:05:27 +04:00
aprint_normal("%d queued\n", i);
2005-07-11 19:29:05 +04:00
#endif
if (i > 0)
2005-07-11 19:29:05 +04:00
xferq->start(fc);
}
/* Async. stream output */
static void
fwip_stream_input(struct fw_xferq *xferq)
{
struct mbuf *m, *m0;
struct m_tag *mtag;
struct ifnet *ifp;
2010-03-29 07:05:27 +04:00
struct fwip_softc *sc;
2005-07-11 19:29:05 +04:00
struct fw_bulkxfer *sxfer;
struct fw_pkt *fp;
uint16_t src;
uint32_t *p;
2010-03-29 07:05:27 +04:00
sc = (struct fwip_softc *)xferq->sc;
ifp = sc->sc_eth.fwip_ifp;
2005-07-11 19:29:05 +04:00
while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
fp = mtod(sxfer->mbuf, struct fw_pkt *);
2010-03-29 07:05:27 +04:00
if (sc->sc_fd.fc->irx_post != NULL)
sc->sc_fd.fc->irx_post(sc->sc_fd.fc, fp->mode.ld);
2005-07-11 19:29:05 +04:00
m = sxfer->mbuf;
/* insert new rbuf */
sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
if (m0 != NULL) {
m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
} else
2010-03-29 07:05:27 +04:00
aprint_error_ifnet(ifp,
"fwip_as_input: m_getcl failed\n");
2005-07-11 19:29:05 +04:00
/*
* We must have a GASP header - leave the
* encapsulation sanity checks to the generic
* code. Remeber that we also have the firewire async
* stream header even though that isn't accounted for
* in mode.stream.len.
*/
2010-03-29 07:05:27 +04:00
if (sxfer->resp != 0 ||
fp->mode.stream.len < 2 * sizeof(uint32_t)) {
2005-07-11 19:29:05 +04:00
m_freem(m);
2020-01-29 09:17:07 +03:00
if_statinc(ifp, if_ierrors);
2005-07-11 19:29:05 +04:00
continue;
}
m->m_len = m->m_pkthdr.len = fp->mode.stream.len
+ sizeof(fp->mode.stream);
/*
* If we received the packet on the broadcast channel,
* mark it as broadcast, otherwise we assume it must
* be multicast.
*/
if (fp->mode.stream.chtag == broadcast_channel)
m->m_flags |= M_BCAST;
else
m->m_flags |= M_MCAST;
/*
* Make sure we recognise the GASP specifier and
* version.
*/
p = mtod(m, uint32_t *);
2010-03-29 07:05:27 +04:00
if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) !=
0x00005e ||
(ntohl(p[2]) & 0xffffff) != 1) {
2005-07-11 19:29:05 +04:00
FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n",
ntohl(p[1]), ntohl(p[2]));
m_freem(m);
2020-01-29 09:17:07 +03:00
if_statinc(ifp, if_ierrors);
2005-07-11 19:29:05 +04:00
continue;
}
/*
* Record the sender ID for possible BPF usage.
*/
src = ntohl(p[1]) >> 16;
2010-03-29 07:05:27 +04:00
if (ifp->if_bpf) {
mtag = m_tag_get(MTAG_FIREWIRE_SENDER_EUID,
2 * sizeof(uint32_t), M_NOWAIT);
2005-07-11 19:29:05 +04:00
if (mtag) {
/* bpf wants it in network byte order */
struct fw_device *fd;
uint32_t *p2 = (uint32_t *) (mtag + 1);
2010-03-29 07:05:27 +04:00
fd = fw_noderesolve_nodeid(sc->sc_fd.fc,
2005-07-11 19:29:05 +04:00
src & 0x3f);
if (fd) {
p2[0] = htonl(fd->eui.hi);
p2[1] = htonl(fd->eui.lo);
} else {
p2[0] = 0;
p2[1] = 0;
}
m_tag_prepend(m, mtag);
}
}
/*
* Trim off the GASP header
*/
m_adj(m, 3*sizeof(uint32_t));
m_set_rcvif(m, ifp);
2010-03-29 07:05:27 +04:00
ieee1394_input(ifp, m, src);
2020-01-29 09:17:07 +03:00
if_statinc(ifp, if_ipackets);
2005-07-11 19:29:05 +04:00
}
if (STAILQ_FIRST(&xferq->stfree) != NULL)
2010-03-29 07:05:27 +04:00
sc->sc_fd.fc->irx_enable(sc->sc_fd.fc, sc->sc_dma_ch);
2005-07-11 19:29:05 +04:00
}
static inline void
2010-03-29 07:05:27 +04:00
fwip_unicast_input_recycle(struct fwip_softc *sc, struct fw_xfer *xfer)
2005-07-11 19:29:05 +04:00
{
struct mbuf *m;
/*
* We have finished with a unicast xfer. Allocate a new
* cluster and stick it on the back of the input queue.
*/
m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
if (m == NULL)
2010-03-29 07:05:27 +04:00
aprint_error_dev(sc->sc_fd.dev,
"fwip_unicast_input_recycle: m_getcl failed\n");
2005-07-11 19:29:05 +04:00
xfer->recv.payload = mtod(m, uint32_t *);
xfer->recv.pay_len = MCLBYTES;
xfer->mbuf = m;
2010-03-29 07:05:27 +04:00
mutex_enter(&sc->sc_fwb.fwb_mtx);
STAILQ_INSERT_TAIL(&sc->sc_fwb.xferlist, xfer, link);
mutex_exit(&sc->sc_fwb.fwb_mtx);
2005-07-11 19:29:05 +04:00
}
static void
fwip_unicast_input(struct fw_xfer *xfer)
{
uint64_t address;
struct mbuf *m;
struct m_tag *mtag;
struct ifnet *ifp;
2010-03-29 07:05:27 +04:00
struct fwip_softc *sc;
2005-07-11 19:29:05 +04:00
struct fw_pkt *fp;
int rtcode;
2010-03-29 07:05:27 +04:00
sc = (struct fwip_softc *)xfer->sc;
ifp = sc->sc_eth.fwip_ifp;
2005-07-11 19:29:05 +04:00
m = xfer->mbuf;
xfer->mbuf = 0;
fp = &xfer->recv.hdr;
/*
* Check the fifo address - we only accept addresses of
* exactly INET_FIFO.
*/
address = ((uint64_t)fp->mode.wreqb.dest_hi << 32)
| fp->mode.wreqb.dest_lo;
if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
rtcode = FWRCODE_ER_TYPE;
} else if (address != INET_FIFO) {
rtcode = FWRCODE_ER_ADDR;
} else {
rtcode = FWRCODE_COMPLETE;
}
/*
* Pick up a new mbuf and stick it on the back of the receive
* queue.
*/
2010-03-29 07:05:27 +04:00
fwip_unicast_input_recycle(sc, xfer);
2005-07-11 19:29:05 +04:00
/*
* If we've already rejected the packet, give up now.
*/
if (rtcode != FWRCODE_COMPLETE) {
m_freem(m);
2020-01-29 09:17:07 +03:00
if_statinc(ifp, if_ierrors);
2005-07-11 19:29:05 +04:00
return;
}
2010-03-29 07:05:27 +04:00
if (ifp->if_bpf) {
2005-07-11 19:29:05 +04:00
/*
* Record the sender ID for possible BPF usage.
*/
2010-03-29 07:05:27 +04:00
mtag = m_tag_get(MTAG_FIREWIRE_SENDER_EUID,
2 * sizeof(uint32_t), M_NOWAIT);
2005-07-11 19:29:05 +04:00
if (mtag) {
/* bpf wants it in network byte order */
struct fw_device *fd;
uint32_t *p = (uint32_t *) (mtag + 1);
2010-03-29 07:05:27 +04:00
fd = fw_noderesolve_nodeid(sc->sc_fd.fc,
2005-07-11 19:29:05 +04:00
fp->mode.wreqb.src & 0x3f);
if (fd) {
p[0] = htonl(fd->eui.hi);
p[1] = htonl(fd->eui.lo);
} else {
p[0] = 0;
p[1] = 0;
}
m_tag_prepend(m, mtag);
}
}
/*
* Hand off to the generic encapsulation code. We don't use
* ifp->if_input so that we can pass the source nodeid as an
* argument to facilitate link-level fragment reassembly.
*/
m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len;
m_set_rcvif(m, ifp);
2010-03-29 07:05:27 +04:00
ieee1394_input(ifp, m, fp->mode.wreqb.src);
2020-01-29 09:17:07 +03:00
if_statinc(ifp, if_ipackets);
2005-07-11 19:29:05 +04:00
}