Home | History | Annotate | Line # | Download | only in ieee1394
if_fwip.c revision 1.11
      1 /*	$NetBSD: if_fwip.c,v 1.11 2007/12/09 20:28:01 jmcneill Exp $	*/
      2 /*-
      3  * Copyright (c) 2004
      4  *	Doug Rabson
      5  * Copyright (c) 2002-2003
      6  * 	Hidetoshi Shimokawa. All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *
     19  *	This product includes software developed by Hidetoshi Shimokawa.
     20  *
     21  * 4. Neither the name of the author nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  * $FreeBSD: src/sys/dev/firewire/if_fwip.c,v 1.16 2007/06/06 14:31:36 simokawa Exp $
     38  */
     39 
     40 #ifdef HAVE_KERNEL_OPTION_HEADERS
     41 #include "opt_device_polling.h"
     42 #include "opt_inet.h"
     43 #endif
     44 
     45 #if defined(__FreeBSD__)
     46 #include <sys/param.h>
     47 #include <sys/kernel.h>
     48 #include <sys/malloc.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/socket.h>
     51 #include <sys/sockio.h>
     52 #include <sys/sysctl.h>
     53 #include <sys/systm.h>
     54 #include <sys/taskqueue.h>
     55 #include <sys/module.h>
     56 #include <sys/bus.h>
     57 #include <sys/bus.h>
     58 
     59 #include <net/bpf.h>
     60 #include <net/if.h>
     61 #include <net/firewire.h>
     62 #include <net/if_arp.h>
     63 #include <net/if_types.h>
     64 #ifdef __DragonFly__
     65 #include <bus/firewire/fw_port.h>
     66 #include <bus/firewire/firewire.h>
     67 #include <bus/firewire/firewirereg.h>
     68 #include "if_fwipvar.h"
     69 #else
     70 #include <dev/firewire/fw_port.h>
     71 #include <dev/firewire/firewire.h>
     72 #include <dev/firewire/firewirereg.h>
     73 #include <dev/firewire/iec13213.h>
     74 #include <dev/firewire/if_fwipvar.h>
     75 #endif
     76 #elif defined(__NetBSD__)
     77 #include <sys/param.h>
     78 #include <sys/device.h>
     79 #include <sys/errno.h>
     80 #include <sys/malloc.h>
     81 #include <sys/mbuf.h>
     82 #include <sys/sysctl.h>
     83 
     84 #include <sys/bus.h>
     85 
     86 #include <net/if.h>
     87 #include <net/if_ieee1394.h>
     88 #include <net/if_types.h>
     89 
     90 #include <dev/ieee1394/fw_port.h>
     91 #include <dev/ieee1394/firewire.h>
     92 #include <dev/ieee1394/firewirereg.h>
     93 #include <dev/ieee1394/iec13213.h>
     94 #include <dev/ieee1394/if_fwipvar.h>
     95 #endif
     96 
     97 /*
     98  * We really need a mechanism for allocating regions in the FIFO
     99  * address space. We pick a address in the OHCI controller's 'middle'
    100  * address space. This means that the controller will automatically
    101  * send responses for us, which is fine since we don't have any
    102  * important information to put in the response anyway.
    103  */
    104 #define INET_FIFO	0xfffe00000000LL
    105 
    106 #if defined(__FreeBSD__)
    107 #define FWIPDEBUG	if (fwipdebug) if_printf
    108 #elif defined(__NetBSD__)
    109 #define FWIPDEBUG(ifp, fmt, ...)		\
    110 	if (fwipdebug) {			\
    111 		printf("%s: ", (ifp)->if_xname);\
    112 		printf((fmt) , ##__VA_ARGS__);	\
    113 	}
    114 #endif
    115 #define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
    116 
    117 #if defined(__NetBSD__)
    118 int fwipmatch (struct device *, struct cfdata *, void *);
    119 void fwipattach (struct device *, struct device *, void *);
    120 int fwipdetach (struct device *, int);
    121 int fwipactivate (struct device *, enum devact);
    122 
    123 #endif
    124 /* network interface */
    125 static void fwip_start (struct ifnet *);
    126 static int fwip_ioctl (struct ifnet *, u_long, void *);
    127 #if defined(__FreeBSD__)
    128 static void fwip_init(void *);
    129 static void fwip_stop(struct fwip_softc *);
    130 #elif defined(__NetBSD__)
    131 static int fwip_init(struct ifnet *);
    132 static void fwip_stop(struct ifnet *, int);
    133 #endif
    134 
    135 static void fwip_post_busreset (void *);
    136 static void fwip_output_callback (struct fw_xfer *);
    137 static void fwip_async_output (struct fwip_softc *, struct ifnet *);
    138 static void fwip_start_send (void *, int);
    139 static void fwip_stream_input (struct fw_xferq *);
    140 static void fwip_unicast_input(struct fw_xfer *);
    141 
    142 static int fwipdebug = 0;
    143 static int broadcast_channel = 0xc0 | 0x1f; /*  tag | channel(XXX) */
    144 static int tx_speed = 2;
    145 static int rx_queue_len = FWMAXQUEUE;
    146 
    147 #if defined(__FreeBSD__)
    148 MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over FireWire interface");
    149 SYSCTL_INT(_debug, OID_AUTO, if_fwip_debug, CTLFLAG_RW, &fwipdebug, 0, "");
    150 SYSCTL_DECL(_hw_firewire);
    151 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwip, CTLFLAG_RD, 0,
    152 	"Firewire ip subsystem");
    153 SYSCTL_INT(_hw_firewire_fwip, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
    154 	0, "Length of the receive queue");
    155 
    156 TUNABLE_INT("hw.firewire.fwip.rx_queue_len", &rx_queue_len);
    157 #elif defined(__NetBSD__)
    158 MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over IEEE1394 interface");
    159 /*
    160  * Setup sysctl(3) MIB, hw.fwip.*
    161  *
    162  * TBD condition CTLFLAG_PERMANENT on being an LKM or not
    163  */
    164 SYSCTL_SETUP(sysctl_fwip, "sysctl fwip(4) subtree setup")
    165 {
    166 	int rc, fwip_node_num;
    167 	const struct sysctlnode *node;
    168 
    169 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
    170 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
    171 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
    172 		goto err;
    173 	}
    174 
    175 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    176 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "fwip",
    177 	    SYSCTL_DESCR("fwip controls"),
    178 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
    179 		goto err;
    180 	}
    181 	fwip_node_num = node->sysctl_num;
    182 
    183 	/* fwip RX queue length */
    184 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    185 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
    186 	    "rx_queue_len", SYSCTL_DESCR("Length of the receive queue"),
    187 	    NULL, 0, &rx_queue_len,
    188 	    0, CTL_HW, fwip_node_num, CTL_CREATE, CTL_EOL)) != 0) {
    189 		goto err;
    190 	}
    191 
    192 	/* fwip RX queue length */
    193 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    194 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
    195 	    "if_fwip_debug", SYSCTL_DESCR("fwip driver debug flag"),
    196 	    NULL, 0, &fwipdebug,
    197 	    0, CTL_HW, fwip_node_num, CTL_CREATE, CTL_EOL)) != 0) {
    198 		goto err;
    199 	}
    200 
    201 	return;
    202 
    203 err:
    204 	printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    205 }
    206 #endif
    207 
    208 #ifdef DEVICE_POLLING
    209 static poll_handler_t fwip_poll;
    210 
    211 static void
    212 fwip_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
    213 {
    214 	struct fwip_softc *fwip;
    215 	struct firewire_comm *fc;
    216 
    217 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
    218 		return;
    219 
    220 	fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
    221 	fc = fwip->fd.fc;
    222 	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
    223 }
    224 #endif /* DEVICE_POLLING */
    225 #if defined(__FreeBSD__)
    226 static void
    227 fwip_identify(driver_t *driver, device_t parent)
    228 {
    229 	BUS_ADD_CHILD(parent, 0, "fwip", fw_get_unit(parent));
    230 }
    231 
    232 static int
    233 fwip_probe(device_t dev)
    234 {
    235 	device_t pa;
    236 
    237 	pa = device_get_parent(dev);
    238 	if(fw_get_unit(dev) != fw_get_unit(pa)){
    239 		return(ENXIO);
    240 	}
    241 
    242 	device_set_desc(dev, "IP over FireWire");
    243 	return (0);
    244 }
    245 #elif defined(__NetBSD__)
    246 int
    247 fwipmatch(struct device *parent, struct cfdata *cf, void *aux)
    248 {
    249 	struct fw_attach_args *fwa = aux;
    250 
    251 	if (strcmp(fwa->name, "fwip") == 0)
    252 		return (1);
    253 	return (0);
    254 }
    255 #endif
    256 
    257 FW_ATTACH(fwip)
    258 {
    259 	FW_ATTACH_START(fwip, fwip, fwa);
    260 	FWIP_ATTACH_START;
    261 	struct ifnet *ifp;
    262 	int s;
    263 
    264 	FWIP_ATTACH_SETUP;
    265 
    266 	ifp = fwip->fw_softc.fwip_ifp;
    267 	if (ifp == NULL)
    268 		FW_ATTACH_RETURN(ENOSPC);
    269 
    270 	fw_mtx_init(&fwip->mtx, "fwip", NULL, MTX_DEF);
    271 	/* XXX */
    272 	fwip->dma_ch = -1;
    273 
    274 	fwip->fd.fc = fwa->fc;
    275 	if (tx_speed < 0)
    276 		tx_speed = fwip->fd.fc->speed;
    277 
    278 	fwip->fd.post_explore = NULL;
    279 	fwip->fd.post_busreset = fwip_post_busreset;
    280 	fwip->fw_softc.fwip = fwip;
    281 	FW_TASK_INIT(&fwip->start_send, 0, fwip_start_send, fwip);
    282 
    283 	/*
    284 	 * Encode our hardware the way that arp likes it.
    285 	 */
    286 	hwaddr->sender_unique_ID_hi = htonl(fwip->fd.fc->eui.hi);
    287 	hwaddr->sender_unique_ID_lo = htonl(fwip->fd.fc->eui.lo);
    288 	hwaddr->sender_max_rec = fwip->fd.fc->maxrec;
    289 	hwaddr->sspd = fwip->fd.fc->speed;
    290 	hwaddr->sender_unicast_FIFO_hi = htons((uint16_t)(INET_FIFO >> 32));
    291 	hwaddr->sender_unicast_FIFO_lo = htonl((uint32_t)INET_FIFO);
    292 
    293 	/* fill the rest and attach interface */
    294 	ifp->if_softc = &fwip->fw_softc;
    295 
    296 #if __FreeBSD_version >= 501113 || defined(__DragonFly__) || defined(__NetBSD__)
    297 	IF_INITNAME(ifp, dev, unit);
    298 #else
    299 	ifp->if_unit = unit;
    300 	ifp->if_name = "fwip";
    301 #endif
    302 #if defined(__NetBSD__)
    303 	IFQ_SET_READY(&ifp->if_snd);
    304 #endif
    305 	SET_IFFUNC(ifp, fwip_start, fwip_ioctl, fwip_init, fwip_stop);
    306 	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
    307 	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
    308 #ifdef DEVICE_POLLING
    309 	ifp->if_capabilities |= IFCAP_POLLING;
    310 #endif
    311 
    312 	s = splfwnet();
    313 	FIREWIRE_IFATTACH(ifp, hwaddr);
    314 	splx(s);
    315 
    316 #if defined(__NetBSD__)
    317 	if (!pmf_device_register(self, NULL, NULL))
    318 		aprint_error_dev(self, "couldn't establish power handler\n");
    319 	else
    320 		pmf_class_network_register(self, ifp);
    321 #endif
    322 
    323 	FWIPDEBUG(ifp, "interface created\n");
    324 	FW_ATTACH_RETURN(0);
    325 }
    326 
    327 IF_STOP(fwip)
    328 {
    329 	IF_STOP_START(fwip, ifp, fwip);
    330 	struct firewire_comm *fc;
    331 	struct fw_xferq *xferq;
    332 	struct fw_xfer *xfer, *next;
    333 	int i;
    334 
    335 	fc = fwip->fd.fc;
    336 
    337 	if (fwip->dma_ch >= 0) {
    338 		xferq = fc->ir[fwip->dma_ch];
    339 
    340 		if (xferq->flag & FWXFERQ_RUNNING)
    341 			fc->irx_disable(fc, fwip->dma_ch);
    342 		xferq->flag &=
    343 			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
    344 			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
    345 		xferq->hand =  NULL;
    346 
    347 		for (i = 0; i < xferq->bnchunk; i ++)
    348 			m_freem(xferq->bulkxfer[i].mbuf);
    349 		free(xferq->bulkxfer, M_FWIP);
    350 
    351 		fw_bindremove(fc, &fwip->fwb);
    352 		for (xfer = STAILQ_FIRST(&fwip->fwb.xferlist); xfer != NULL;
    353 					xfer = next) {
    354 			next = STAILQ_NEXT(xfer, link);
    355 			fw_xfer_free(xfer);
    356 		}
    357 
    358 		for (xfer = STAILQ_FIRST(&fwip->xferlist); xfer != NULL;
    359 					xfer = next) {
    360 			next = STAILQ_NEXT(xfer, link);
    361 			fw_xfer_free(xfer);
    362 		}
    363 		STAILQ_INIT(&fwip->xferlist);
    364 
    365 		xferq->bulkxfer =  NULL;
    366 		fwip->dma_ch = -1;
    367 	}
    368 
    369 #if defined(__FreeBSD__)
    370 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
    371 #elif defined(__NetBSD__)
    372 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    373 #endif
    374 }
    375 
    376 FW_DETACH(fwip)
    377 {
    378 	IF_DETACH_START(fwip, fwip);
    379 	struct ifnet *ifp;
    380 	int s;
    381 
    382 	ifp = fwip->fw_softc.fwip_ifp;
    383 
    384 #ifdef DEVICE_POLLING
    385 	if (ifp->if_capenable & IFCAP_POLLING)
    386 		ether_poll_deregister(ifp);
    387 #endif
    388 
    389 	s = splfwnet();
    390 
    391 	FWIP_STOP(fwip);
    392 	FIREWIRE_IFDETACH(ifp);
    393 	fw_mtx_destroy(&fwip->mtx);
    394 
    395 	splx(s);
    396 	return 0;
    397 }
    398 
    399 #if defined(__NetBSD__)
    400 int
    401 fwipactivate(struct device *self, enum devact act)
    402 {
    403 	struct fwip_softc *fwip = (struct fwip_softc *)self;
    404 	int s, error = 0;
    405 
    406 	s = splfwnet();
    407 	switch (act) {
    408 	case DVACT_ACTIVATE:
    409 		error = EOPNOTSUPP;
    410 		break;
    411 
    412 	case DVACT_DEACTIVATE:
    413 		if_deactivate(fwip->fw_softc.fwip_ifp);
    414 			break;
    415 	}
    416 	splx(s);
    417 
    418 	return (error);
    419 }
    420 
    421 #endif
    422 IF_INIT(fwip)
    423 {
    424 	IF_INIT_START(fwip, fwip, ifp);
    425 	struct firewire_comm *fc;
    426 	struct fw_xferq *xferq;
    427 	struct fw_xfer *xfer;
    428 	struct mbuf *m;
    429 	int i;
    430 
    431 	FWIPDEBUG(ifp, "initializing\n");
    432 
    433 	fc = fwip->fd.fc;
    434 #define START 0
    435 	if (fwip->dma_ch < 0) {
    436 		fwip->dma_ch = fw_open_isodma(fc, /* tx */0);
    437 		if (fwip->dma_ch < 0)
    438 			IF_INIT_RETURN(ENXIO);
    439 		xferq = fc->ir[fwip->dma_ch];
    440 		xferq->flag |=
    441 		    FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_STREAM;
    442 		xferq->flag &= ~0xff;
    443 		xferq->flag |= broadcast_channel & 0xff;
    444 		/* register fwip_input handler */
    445 		xferq->sc = (void *) fwip;
    446 		xferq->hand = fwip_stream_input;
    447 		xferq->bnchunk = rx_queue_len;
    448 		xferq->bnpacket = 1;
    449 		xferq->psize = MCLBYTES;
    450 		xferq->queued = 0;
    451 		xferq->buf = NULL;
    452 		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
    453 			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
    454 							M_FWIP, M_WAITOK);
    455 		if (xferq->bulkxfer == NULL) {
    456 			printf("if_fwip: malloc failed\n");
    457 			IF_INIT_RETURN(ENOMEM);
    458 		}
    459 		STAILQ_INIT(&xferq->stvalid);
    460 		STAILQ_INIT(&xferq->stfree);
    461 		STAILQ_INIT(&xferq->stdma);
    462 		xferq->stproc = NULL;
    463 		for (i = 0; i < xferq->bnchunk; i ++) {
    464 			m =
    465 #if defined(__DragonFly__) || __FreeBSD_version < 500000
    466 				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
    467 #else
    468 				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
    469 #endif
    470 			xferq->bulkxfer[i].mbuf = m;
    471 			if (m != NULL) {
    472 				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
    473 				STAILQ_INSERT_TAIL(&xferq->stfree,
    474 						&xferq->bulkxfer[i], link);
    475 			} else
    476 				printf("fwip_as_input: m_getcl failed\n");
    477 		}
    478 
    479 		fwip->fwb.start = INET_FIFO;
    480 		fwip->fwb.end = INET_FIFO + 16384; /* S3200 packet size */
    481 
    482 		/* pre-allocate xfer */
    483 		STAILQ_INIT(&fwip->fwb.xferlist);
    484 		for (i = 0; i < rx_queue_len; i ++) {
    485 			xfer = fw_xfer_alloc(M_FWIP);
    486 			if (xfer == NULL)
    487 				break;
    488 			m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
    489 			xfer->recv.payload = mtod(m, uint32_t *);
    490 			xfer->recv.pay_len = MCLBYTES;
    491 			xfer->hand = fwip_unicast_input;
    492 			xfer->fc = fc;
    493 			xfer->sc = (void *)fwip;
    494 			xfer->mbuf = m;
    495 			STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
    496 		}
    497 		fw_bindadd(fc, &fwip->fwb);
    498 
    499 		STAILQ_INIT(&fwip->xferlist);
    500 		for (i = 0; i < TX_MAX_QUEUE; i++) {
    501 			xfer = fw_xfer_alloc(M_FWIP);
    502 			if (xfer == NULL)
    503 				break;
    504 			xfer->send.spd = tx_speed;
    505 			xfer->fc = fwip->fd.fc;
    506 			xfer->sc = (void *)fwip;
    507 			xfer->hand = fwip_output_callback;
    508 			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
    509 		}
    510 	} else
    511 		xferq = fc->ir[fwip->dma_ch];
    512 
    513 	fwip->last_dest.hi = 0;
    514 	fwip->last_dest.lo = 0;
    515 
    516 	/* start dma */
    517 	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
    518 		fc->irx_enable(fc, fwip->dma_ch);
    519 
    520 #if defined(__FreeBSD__)
    521 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
    522 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
    523 #elif defined(__NetBSD__)
    524 	ifp->if_flags |= IFF_RUNNING;
    525 	ifp->if_flags &= ~IFF_OACTIVE;
    526 #endif
    527 
    528 #if 0
    529 	/* attempt to start output */
    530 	fwip_start(ifp);
    531 #endif
    532 	IF_INIT_RETURN(0);
    533 }
    534 
    535 static int
    536 fwip_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    537 {
    538 	IF_IOCTL_START(fwip, fwip);
    539 	int s, error;
    540 
    541 	switch (cmd) {
    542 	case SIOCSIFFLAGS:
    543 		s = splfwnet();
    544 		if (ifp->if_flags & IFF_UP) {
    545 #if defined(__FreeBSD__)
    546 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
    547 #elif defined(__NetBSD__)
    548 			if (!(ifp->if_flags & IFF_RUNNING))
    549 #endif
    550 				FWIP_INIT(fwip);
    551 		} else {
    552 #if defined(__FreeBSD__)
    553 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
    554 #elif defined(__NetBSD__)
    555 			if (ifp->if_flags & IFF_RUNNING)
    556 #endif
    557 				FWIP_STOP(fwip);
    558 		}
    559 		splx(s);
    560 		break;
    561 	case SIOCADDMULTI:
    562 	case SIOCDELMULTI:
    563 		break;
    564 	case SIOCSIFCAP:
    565 #ifdef DEVICE_POLLING
    566 	    {
    567 		struct ifreq *ifr = (struct ifreq *) data;
    568 		struct firewire_comm *fc = fc = fwip->fd.fc;
    569 
    570 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
    571 		    !(ifp->if_capenable & IFCAP_POLLING)) {
    572 			error = ether_poll_register(fwip_poll, ifp);
    573 			if (error)
    574 				return(error);
    575 			/* Disable interrupts */
    576 			fc->set_intr(fc, 0);
    577 			ifp->if_capenable |= IFCAP_POLLING;
    578 			return (error);
    579 
    580 		}
    581 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
    582 		    ifp->if_capenable & IFCAP_POLLING) {
    583 			error = ether_poll_deregister(ifp);
    584 			/* Enable interrupts. */
    585 			fc->set_intr(fc, 1);
    586 			ifp->if_capenable &= ~IFCAP_POLLING;
    587 			return (error);
    588 		}
    589 	    }
    590 #endif /* DEVICE_POLLING */
    591 		break;
    592 
    593 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__NetBSD__)
    594 	default:
    595 #else
    596 	case SIOCSIFADDR:
    597 	case SIOCGIFADDR:
    598 	case SIOCSIFMTU:
    599 #endif
    600 		s = splfwnet();
    601 		error = FIREWIRE_IOCTL(ifp, cmd, data);
    602 		splx(s);
    603 		return (error);
    604 #if defined(__DragonFly__) || \
    605     (defined(__FreeBSD__) && __FreeBSD_version < 500000)
    606 	default:
    607 		return (EINVAL);
    608 #endif
    609 	}
    610 
    611 	return (0);
    612 }
    613 
    614 static void
    615 fwip_post_busreset(void *arg)
    616 {
    617 	struct fwip_softc *fwip = arg;
    618 	struct crom_src *src;
    619 	struct crom_chunk *root;
    620 
    621 	src = fwip->fd.fc->crom_src;
    622 	root = fwip->fd.fc->crom_root;
    623 
    624 	/* RFC2734 IPv4 over IEEE1394 */
    625 	bzero(&fwip->unit4, sizeof(struct crom_chunk));
    626 	crom_add_chunk(src, root, &fwip->unit4, CROM_UDIR);
    627 	crom_add_entry(&fwip->unit4, CSRKEY_SPEC, CSRVAL_IETF);
    628 	crom_add_simple_text(src, &fwip->unit4, &fwip->spec4, "IANA");
    629 	crom_add_entry(&fwip->unit4, CSRKEY_VER, 1);
    630 	crom_add_simple_text(src, &fwip->unit4, &fwip->ver4, "IPv4");
    631 
    632 	/* RFC3146 IPv6 over IEEE1394 */
    633 	bzero(&fwip->unit6, sizeof(struct crom_chunk));
    634 	crom_add_chunk(src, root, &fwip->unit6, CROM_UDIR);
    635 	crom_add_entry(&fwip->unit6, CSRKEY_SPEC, CSRVAL_IETF);
    636 	crom_add_simple_text(src, &fwip->unit6, &fwip->spec6, "IANA");
    637 	crom_add_entry(&fwip->unit6, CSRKEY_VER, 2);
    638 	crom_add_simple_text(src, &fwip->unit6, &fwip->ver6, "IPv6");
    639 
    640 	fwip->last_dest.hi = 0;
    641 	fwip->last_dest.lo = 0;
    642 	FIREWIRE_BUSRESET(fwip->fw_softc.fwip_ifp);
    643 }
    644 
    645 static void
    646 fwip_output_callback(struct fw_xfer *xfer)
    647 {
    648 	struct fwip_softc *fwip;
    649 	struct ifnet *ifp;
    650 	int s;
    651 
    652 	fwip = (struct fwip_softc *)xfer->sc;
    653 	ifp = fwip->fw_softc.fwip_ifp;
    654 	/* XXX error check */
    655 	FWIPDEBUG(ifp, "resp = %d\n", xfer->resp);
    656 	if (xfer->resp != 0)
    657 		ifp->if_oerrors ++;
    658 
    659 	m_freem(xfer->mbuf);
    660 	fw_xfer_unload(xfer);
    661 
    662 	s = splfwnet();
    663 	FWIP_LOCK(fwip);
    664 	STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
    665 	FWIP_UNLOCK(fwip);
    666 	splx(s);
    667 
    668 	/* for queue full */
    669 	if (ifp->if_snd.ifq_head != NULL) {
    670 		fwip_start(ifp);
    671 	}
    672 }
    673 
    674 static void
    675 fwip_start(struct ifnet *ifp)
    676 {
    677 	struct fwip_softc *fwip =
    678 	    ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
    679 	int s;
    680 
    681 	FWIPDEBUG(ifp, "starting\n");
    682 
    683 	if (fwip->dma_ch < 0) {
    684 		struct mbuf	*m = NULL;
    685 
    686 		FWIPDEBUG(ifp, "not ready\n");
    687 
    688 		s = splfwnet();
    689 		do {
    690 			IF_DEQUEUE(&ifp->if_snd, m);
    691 			if (m != NULL)
    692 				m_freem(m);
    693 			ifp->if_oerrors ++;
    694 		} while (m != NULL);
    695 		splx(s);
    696 
    697 		return;
    698 	}
    699 
    700 	s = splfwnet();
    701 #if defined(__FreeBSD__)
    702 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
    703 #elif defined(__NetBSD__)
    704 	ifp->if_flags |= IFF_OACTIVE;
    705 #endif
    706 
    707 	if (ifp->if_snd.ifq_len != 0)
    708 		fwip_async_output(fwip, ifp);
    709 
    710 #if defined(__FreeBSD__)
    711 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
    712 #elif defined(__NetBSD__)
    713 	ifp->if_flags &= ~IFF_OACTIVE;
    714 #endif
    715 	splx(s);
    716 }
    717 
    718 /* Async. stream output */
    719 static void
    720 fwip_async_output(struct fwip_softc *fwip, struct ifnet *ifp)
    721 {
    722 	struct firewire_comm *fc = fwip->fd.fc;
    723 	struct mbuf *m;
    724 	struct m_tag *mtag;
    725 	struct fw_hwaddr *destfw;
    726 	struct fw_xfer *xfer;
    727 	struct fw_xferq *xferq;
    728 	struct fw_pkt *fp;
    729 	uint16_t nodeid;
    730 	int error;
    731 	int i = 0;
    732 
    733 	xfer = NULL;
    734 	xferq = fc->atq;
    735 	while ((xferq->queued < xferq->maxq - 1) &&
    736 	    (ifp->if_snd.ifq_head != NULL)) {
    737 		FWIP_LOCK(fwip);
    738 		xfer = STAILQ_FIRST(&fwip->xferlist);
    739 		if (xfer == NULL) {
    740 			FWIP_UNLOCK(fwip);
    741 #if 0
    742 			printf("if_fwip: lack of xfer\n");
    743 #endif
    744 			break;
    745 		}
    746 		STAILQ_REMOVE_HEAD(&fwip->xferlist, link);
    747 		FWIP_UNLOCK(fwip);
    748 
    749 		IF_DEQUEUE(&ifp->if_snd, m);
    750 		if (m == NULL) {
    751 			FWIP_LOCK(fwip);
    752 			STAILQ_INSERT_HEAD(&fwip->xferlist, xfer, link);
    753 			FWIP_UNLOCK(fwip);
    754 			break;
    755 		}
    756 
    757 		/*
    758 		 * Dig out the link-level address which
    759 		 * firewire_output got via arp or neighbour
    760 		 * discovery. If we don't have a link-level address,
    761 		 * just stick the thing on the broadcast channel.
    762 		 */
    763 		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, 0);
    764 		if (mtag == NULL)
    765 			destfw = 0;
    766 		else
    767 			destfw = (struct fw_hwaddr *) (mtag + 1);
    768 
    769 		/*
    770 		 * We don't do any bpf stuff here - the generic code
    771 		 * in firewire_output gives the packet to bpf before
    772 		 * it adds the link-level encapsulation.
    773 		 */
    774 
    775 		/*
    776 		 * Put the mbuf in the xfer early in case we hit an
    777 		 * error case below - fwip_output_callback will free
    778 		 * the mbuf.
    779 		 */
    780 		xfer->mbuf = m;
    781 
    782 		/*
    783 		 * We use the arp result (if any) to add a suitable firewire
    784 		 * packet header before handing off to the bus.
    785 		 */
    786 		fp = &xfer->send.hdr;
    787 		nodeid = FWLOCALBUS | fc->nodeid;
    788 		if ((m->m_flags & M_BCAST) || !destfw) {
    789 			/*
    790 			 * Broadcast packets are sent as GASP packets with
    791 			 * specifier ID 0x00005e, version 1 on the broadcast
    792 			 * channel. To be conservative, we send at the
    793 			 * slowest possible speed.
    794 			 */
    795 			uint32_t *p;
    796 
    797 			M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
    798 			p = mtod(m, uint32_t *);
    799 			fp->mode.stream.len = m->m_pkthdr.len;
    800 			fp->mode.stream.chtag = broadcast_channel;
    801 			fp->mode.stream.tcode = FWTCODE_STREAM;
    802 			fp->mode.stream.sy = 0;
    803 			xfer->send.spd = 0;
    804 			p[0] = htonl(nodeid << 16);
    805 			p[1] = htonl((0x5e << 24) | 1);
    806 		} else {
    807 			/*
    808 			 * Unicast packets are sent as block writes to the
    809 			 * target's unicast fifo address. If we can't
    810 			 * find the node address, we just give up. We
    811 			 * could broadcast it but that might overflow
    812 			 * the packet size limitations due to the
    813 			 * extra GASP header. Note: the hardware
    814 			 * address is stored in network byte order to
    815 			 * make life easier for ARP.
    816 			 */
    817 			struct fw_device *fd;
    818 			struct fw_eui64 eui;
    819 
    820 			eui.hi = ntohl(destfw->sender_unique_ID_hi);
    821 			eui.lo = ntohl(destfw->sender_unique_ID_lo);
    822 			if (fwip->last_dest.hi != eui.hi ||
    823 			    fwip->last_dest.lo != eui.lo) {
    824 				fd = fw_noderesolve_eui64(fc, &eui);
    825 				if (!fd) {
    826 					/* error */
    827 					ifp->if_oerrors ++;
    828 					/* XXX set error code */
    829 					fwip_output_callback(xfer);
    830 					continue;
    831 
    832 				}
    833 				fwip->last_hdr.mode.wreqb.dst = FWLOCALBUS | fd->dst;
    834 				fwip->last_hdr.mode.wreqb.tlrt = 0;
    835 				fwip->last_hdr.mode.wreqb.tcode = FWTCODE_WREQB;
    836 				fwip->last_hdr.mode.wreqb.pri = 0;
    837 				fwip->last_hdr.mode.wreqb.src = nodeid;
    838 				fwip->last_hdr.mode.wreqb.dest_hi =
    839 					ntohs(destfw->sender_unicast_FIFO_hi);
    840 				fwip->last_hdr.mode.wreqb.dest_lo =
    841 					ntohl(destfw->sender_unicast_FIFO_lo);
    842 				fwip->last_hdr.mode.wreqb.extcode = 0;
    843 				fwip->last_dest = eui;
    844 			}
    845 
    846 			fp->mode.wreqb = fwip->last_hdr.mode.wreqb;
    847 			fp->mode.wreqb.len = m->m_pkthdr.len;
    848 			xfer->send.spd = min(destfw->sspd, fc->speed);
    849 		}
    850 
    851 		xfer->send.pay_len = m->m_pkthdr.len;
    852 
    853 		error = fw_asyreq(fc, -1, xfer);
    854 		if (error == EAGAIN) {
    855 			/*
    856 			 * We ran out of tlabels - requeue the packet
    857 			 * for later transmission.
    858 			 */
    859 			xfer->mbuf = 0;
    860 			FWIP_LOCK(fwip);
    861 			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
    862 			FWIP_UNLOCK(fwip);
    863 			IF_PREPEND(&ifp->if_snd, m);
    864 			break;
    865 		}
    866 		if (error) {
    867 			/* error */
    868 			ifp->if_oerrors ++;
    869 			/* XXX set error code */
    870 			fwip_output_callback(xfer);
    871 			continue;
    872 		} else {
    873 			ifp->if_opackets ++;
    874 			i++;
    875 		}
    876 	}
    877 #if 0
    878 	if (i > 1)
    879 		printf("%d queued\n", i);
    880 #endif
    881 	if (i > 0)
    882 		xferq->start(fc);
    883 }
    884 
    885 static void
    886 fwip_start_send (void *arg, int count)
    887 {
    888 	struct fwip_softc *fwip = arg;
    889 
    890 	fwip->fd.fc->atq->start(fwip->fd.fc);
    891 }
    892 
    893 /* Async. stream output */
    894 static void
    895 fwip_stream_input(struct fw_xferq *xferq)
    896 {
    897 	struct mbuf *m, *m0;
    898 	struct m_tag *mtag;
    899 	struct ifnet *ifp;
    900 	struct fwip_softc *fwip;
    901 	struct fw_bulkxfer *sxfer;
    902 	struct fw_pkt *fp;
    903 	uint16_t src;
    904 	uint32_t *p;
    905 
    906 	fwip = (struct fwip_softc *)xferq->sc;
    907 	ifp = fwip->fw_softc.fwip_ifp;
    908 	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
    909 		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
    910 		fp = mtod(sxfer->mbuf, struct fw_pkt *);
    911 		if (fwip->fd.fc->irx_post != NULL)
    912 			fwip->fd.fc->irx_post(fwip->fd.fc, fp->mode.ld);
    913 		m = sxfer->mbuf;
    914 
    915 		/* insert new rbuf */
    916 		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
    917 		if (m0 != NULL) {
    918 			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
    919 			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
    920 		} else
    921 			printf("fwip_as_input: m_getcl failed\n");
    922 
    923 		/*
    924 		 * We must have a GASP header - leave the
    925 		 * encapsulation sanity checks to the generic
    926 		 * code. Remeber that we also have the firewire async
    927 		 * stream header even though that isn't accounted for
    928 		 * in mode.stream.len.
    929 		 */
    930 		if (sxfer->resp != 0 || fp->mode.stream.len <
    931 		    2*sizeof(uint32_t)) {
    932 			m_freem(m);
    933 			ifp->if_ierrors ++;
    934 			continue;
    935 		}
    936 		m->m_len = m->m_pkthdr.len = fp->mode.stream.len
    937 			+ sizeof(fp->mode.stream);
    938 
    939 		/*
    940 		 * If we received the packet on the broadcast channel,
    941 		 * mark it as broadcast, otherwise we assume it must
    942 		 * be multicast.
    943 		 */
    944 		if (fp->mode.stream.chtag == broadcast_channel)
    945 			m->m_flags |= M_BCAST;
    946 		else
    947 			m->m_flags |= M_MCAST;
    948 
    949 		/*
    950 		 * Make sure we recognise the GASP specifier and
    951 		 * version.
    952 		 */
    953 		p = mtod(m, uint32_t *);
    954 		if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) != 0x00005e
    955 		    || (ntohl(p[2]) & 0xffffff) != 1) {
    956 			FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n",
    957 			    ntohl(p[1]), ntohl(p[2]));
    958 			m_freem(m);
    959 			ifp->if_ierrors ++;
    960 			continue;
    961 		}
    962 
    963 		/*
    964 		 * Record the sender ID for possible BPF usage.
    965 		 */
    966 		src = ntohl(p[1]) >> 16;
    967 		if (bpf_peers_present(ifp->if_bpf)) {
    968 			mtag = m_tag_alloc(MTAG_FIREWIRE,
    969 			    MTAG_FIREWIRE_SENDER_EUID,
    970 			    2*sizeof(uint32_t), M_NOWAIT);
    971 			if (mtag) {
    972 				/* bpf wants it in network byte order */
    973 				struct fw_device *fd;
    974 				uint32_t *p2 = (uint32_t *) (mtag + 1);
    975 				fd = fw_noderesolve_nodeid(fwip->fd.fc,
    976 				    src & 0x3f);
    977 				if (fd) {
    978 					p2[0] = htonl(fd->eui.hi);
    979 					p2[1] = htonl(fd->eui.lo);
    980 				} else {
    981 					p2[0] = 0;
    982 					p2[1] = 0;
    983 				}
    984 				m_tag_prepend(m, mtag);
    985 			}
    986 		}
    987 
    988 		/*
    989 		 * Trim off the GASP header
    990 		 */
    991 		m_adj(m, 3*sizeof(uint32_t));
    992 		m->m_pkthdr.rcvif = ifp;
    993 		FIREWIRE_INPUT(ifp, m, src);
    994 		ifp->if_ipackets ++;
    995 	}
    996 	if (STAILQ_FIRST(&xferq->stfree) != NULL)
    997 		fwip->fd.fc->irx_enable(fwip->fd.fc, fwip->dma_ch);
    998 }
    999 
   1000 static inline void
   1001 fwip_unicast_input_recycle(struct fwip_softc *fwip, struct fw_xfer *xfer)
   1002 {
   1003 	struct mbuf *m;
   1004 
   1005 	/*
   1006 	 * We have finished with a unicast xfer. Allocate a new
   1007 	 * cluster and stick it on the back of the input queue.
   1008 	 */
   1009 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
   1010 	if (m == NULL)
   1011 		printf("fwip_unicast_input_recycle: m_getcl failed\n");
   1012 	xfer->mbuf = m;
   1013 	xfer->recv.payload = mtod(m, uint32_t *);
   1014 	xfer->recv.pay_len = MCLBYTES;
   1015 	xfer->mbuf = m;
   1016 	STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
   1017 }
   1018 
   1019 static void
   1020 fwip_unicast_input(struct fw_xfer *xfer)
   1021 {
   1022 	uint64_t address;
   1023 	struct mbuf *m;
   1024 	struct m_tag *mtag;
   1025 	struct ifnet *ifp;
   1026 	struct fwip_softc *fwip;
   1027 	struct fw_pkt *fp;
   1028 	//struct fw_pkt *sfp;
   1029 	int rtcode;
   1030 
   1031 	fwip = (struct fwip_softc *)xfer->sc;
   1032 	ifp = fwip->fw_softc.fwip_ifp;
   1033 	m = xfer->mbuf;
   1034 	xfer->mbuf = 0;
   1035 	fp = &xfer->recv.hdr;
   1036 
   1037 	/*
   1038 	 * Check the fifo address - we only accept addresses of
   1039 	 * exactly INET_FIFO.
   1040 	 */
   1041 	address = ((uint64_t)fp->mode.wreqb.dest_hi << 32)
   1042 		| fp->mode.wreqb.dest_lo;
   1043 	if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
   1044 		rtcode = FWRCODE_ER_TYPE;
   1045 	} else if (address != INET_FIFO) {
   1046 		rtcode = FWRCODE_ER_ADDR;
   1047 	} else {
   1048 		rtcode = FWRCODE_COMPLETE;
   1049 	}
   1050 
   1051 	/*
   1052 	 * Pick up a new mbuf and stick it on the back of the receive
   1053 	 * queue.
   1054 	 */
   1055 	fwip_unicast_input_recycle(fwip, xfer);
   1056 
   1057 	/*
   1058 	 * If we've already rejected the packet, give up now.
   1059 	 */
   1060 	if (rtcode != FWRCODE_COMPLETE) {
   1061 		m_freem(m);
   1062 		ifp->if_ierrors ++;
   1063 		return;
   1064 	}
   1065 
   1066 	if (bpf_peers_present(ifp->if_bpf)) {
   1067 		/*
   1068 		 * Record the sender ID for possible BPF usage.
   1069 		 */
   1070 		mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID,
   1071 		    2*sizeof(uint32_t), M_NOWAIT);
   1072 		if (mtag) {
   1073 			/* bpf wants it in network byte order */
   1074 			struct fw_device *fd;
   1075 			uint32_t *p = (uint32_t *) (mtag + 1);
   1076 			fd = fw_noderesolve_nodeid(fwip->fd.fc,
   1077 			    fp->mode.wreqb.src & 0x3f);
   1078 			if (fd) {
   1079 				p[0] = htonl(fd->eui.hi);
   1080 				p[1] = htonl(fd->eui.lo);
   1081 			} else {
   1082 				p[0] = 0;
   1083 				p[1] = 0;
   1084 			}
   1085 			m_tag_prepend(m, mtag);
   1086 		}
   1087 	}
   1088 
   1089 	/*
   1090 	 * Hand off to the generic encapsulation code. We don't use
   1091 	 * ifp->if_input so that we can pass the source nodeid as an
   1092 	 * argument to facilitate link-level fragment reassembly.
   1093 	 */
   1094 	m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len;
   1095 	m->m_pkthdr.rcvif = ifp;
   1096 	FIREWIRE_INPUT(ifp, m, fp->mode.wreqb.src);
   1097 	ifp->if_ipackets ++;
   1098 }
   1099 
   1100 #if defined(__FreeBSD__)
   1101 static devclass_t fwip_devclass;
   1102 
   1103 static device_method_t fwip_methods[] = {
   1104 	/* device interface */
   1105 	DEVMETHOD(device_identify,	fwip_identify),
   1106 	DEVMETHOD(device_probe,		fwip_probe),
   1107 	DEVMETHOD(device_attach,	fwip_attach),
   1108 	DEVMETHOD(device_detach,	fwip_detach),
   1109 	{ 0, 0 }
   1110 };
   1111 
   1112 static driver_t fwip_driver = {
   1113         "fwip",
   1114 	fwip_methods,
   1115 	sizeof(struct fwip_softc),
   1116 };
   1117 
   1118 
   1119 #ifdef __DragonFly__
   1120 DECLARE_DUMMY_MODULE(fwip);
   1121 #endif
   1122 DRIVER_MODULE(fwip, firewire, fwip_driver, fwip_devclass, 0, 0);
   1123 MODULE_VERSION(fwip, 1);
   1124 MODULE_DEPEND(fwip, firewire, 1, 1, 1);
   1125 #elif defined(__NetBSD__)
   1126 CFATTACH_DECL(fwip, sizeof (struct fwip_softc),
   1127     fwipmatch, fwipattach, fwipdetach, NULL);
   1128 #endif
   1129