Home | History | Annotate | Line # | Download | only in ieee1394
if_fwip.c revision 1.10
      1 /*	$NetBSD: if_fwip.c,v 1.10 2007/11/05 19:08:57 kiyohara 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 	FWIPDEBUG(ifp, "interface created\n");
    317 	FW_ATTACH_RETURN(0);
    318 }
    319 
    320 IF_STOP(fwip)
    321 {
    322 	IF_STOP_START(fwip, ifp, fwip);
    323 	struct firewire_comm *fc;
    324 	struct fw_xferq *xferq;
    325 	struct fw_xfer *xfer, *next;
    326 	int i;
    327 
    328 	fc = fwip->fd.fc;
    329 
    330 	if (fwip->dma_ch >= 0) {
    331 		xferq = fc->ir[fwip->dma_ch];
    332 
    333 		if (xferq->flag & FWXFERQ_RUNNING)
    334 			fc->irx_disable(fc, fwip->dma_ch);
    335 		xferq->flag &=
    336 			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
    337 			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
    338 		xferq->hand =  NULL;
    339 
    340 		for (i = 0; i < xferq->bnchunk; i ++)
    341 			m_freem(xferq->bulkxfer[i].mbuf);
    342 		free(xferq->bulkxfer, M_FWIP);
    343 
    344 		fw_bindremove(fc, &fwip->fwb);
    345 		for (xfer = STAILQ_FIRST(&fwip->fwb.xferlist); xfer != NULL;
    346 					xfer = next) {
    347 			next = STAILQ_NEXT(xfer, link);
    348 			fw_xfer_free(xfer);
    349 		}
    350 
    351 		for (xfer = STAILQ_FIRST(&fwip->xferlist); xfer != NULL;
    352 					xfer = next) {
    353 			next = STAILQ_NEXT(xfer, link);
    354 			fw_xfer_free(xfer);
    355 		}
    356 		STAILQ_INIT(&fwip->xferlist);
    357 
    358 		xferq->bulkxfer =  NULL;
    359 		fwip->dma_ch = -1;
    360 	}
    361 
    362 #if defined(__FreeBSD__)
    363 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
    364 #elif defined(__NetBSD__)
    365 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    366 #endif
    367 }
    368 
    369 FW_DETACH(fwip)
    370 {
    371 	IF_DETACH_START(fwip, fwip);
    372 	struct ifnet *ifp;
    373 	int s;
    374 
    375 	ifp = fwip->fw_softc.fwip_ifp;
    376 
    377 #ifdef DEVICE_POLLING
    378 	if (ifp->if_capenable & IFCAP_POLLING)
    379 		ether_poll_deregister(ifp);
    380 #endif
    381 
    382 	s = splfwnet();
    383 
    384 	FWIP_STOP(fwip);
    385 	FIREWIRE_IFDETACH(ifp);
    386 	fw_mtx_destroy(&fwip->mtx);
    387 
    388 	splx(s);
    389 	return 0;
    390 }
    391 
    392 #if defined(__NetBSD__)
    393 int
    394 fwipactivate(struct device *self, enum devact act)
    395 {
    396 	struct fwip_softc *fwip = (struct fwip_softc *)self;
    397 	int s, error = 0;
    398 
    399 	s = splfwnet();
    400 	switch (act) {
    401 	case DVACT_ACTIVATE:
    402 		error = EOPNOTSUPP;
    403 		break;
    404 
    405 	case DVACT_DEACTIVATE:
    406 		if_deactivate(fwip->fw_softc.fwip_ifp);
    407 			break;
    408 	}
    409 	splx(s);
    410 
    411 	return (error);
    412 }
    413 
    414 #endif
    415 IF_INIT(fwip)
    416 {
    417 	IF_INIT_START(fwip, fwip, ifp);
    418 	struct firewire_comm *fc;
    419 	struct fw_xferq *xferq;
    420 	struct fw_xfer *xfer;
    421 	struct mbuf *m;
    422 	int i;
    423 
    424 	FWIPDEBUG(ifp, "initializing\n");
    425 
    426 	fc = fwip->fd.fc;
    427 #define START 0
    428 	if (fwip->dma_ch < 0) {
    429 		fwip->dma_ch = fw_open_isodma(fc, /* tx */0);
    430 		if (fwip->dma_ch < 0)
    431 			IF_INIT_RETURN(ENXIO);
    432 		xferq = fc->ir[fwip->dma_ch];
    433 		xferq->flag |=
    434 		    FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_STREAM;
    435 		xferq->flag &= ~0xff;
    436 		xferq->flag |= broadcast_channel & 0xff;
    437 		/* register fwip_input handler */
    438 		xferq->sc = (void *) fwip;
    439 		xferq->hand = fwip_stream_input;
    440 		xferq->bnchunk = rx_queue_len;
    441 		xferq->bnpacket = 1;
    442 		xferq->psize = MCLBYTES;
    443 		xferq->queued = 0;
    444 		xferq->buf = NULL;
    445 		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
    446 			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
    447 							M_FWIP, M_WAITOK);
    448 		if (xferq->bulkxfer == NULL) {
    449 			printf("if_fwip: malloc failed\n");
    450 			IF_INIT_RETURN(ENOMEM);
    451 		}
    452 		STAILQ_INIT(&xferq->stvalid);
    453 		STAILQ_INIT(&xferq->stfree);
    454 		STAILQ_INIT(&xferq->stdma);
    455 		xferq->stproc = NULL;
    456 		for (i = 0; i < xferq->bnchunk; i ++) {
    457 			m =
    458 #if defined(__DragonFly__) || __FreeBSD_version < 500000
    459 				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
    460 #else
    461 				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
    462 #endif
    463 			xferq->bulkxfer[i].mbuf = m;
    464 			if (m != NULL) {
    465 				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
    466 				STAILQ_INSERT_TAIL(&xferq->stfree,
    467 						&xferq->bulkxfer[i], link);
    468 			} else
    469 				printf("fwip_as_input: m_getcl failed\n");
    470 		}
    471 
    472 		fwip->fwb.start = INET_FIFO;
    473 		fwip->fwb.end = INET_FIFO + 16384; /* S3200 packet size */
    474 
    475 		/* pre-allocate xfer */
    476 		STAILQ_INIT(&fwip->fwb.xferlist);
    477 		for (i = 0; i < rx_queue_len; i ++) {
    478 			xfer = fw_xfer_alloc(M_FWIP);
    479 			if (xfer == NULL)
    480 				break;
    481 			m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
    482 			xfer->recv.payload = mtod(m, uint32_t *);
    483 			xfer->recv.pay_len = MCLBYTES;
    484 			xfer->hand = fwip_unicast_input;
    485 			xfer->fc = fc;
    486 			xfer->sc = (void *)fwip;
    487 			xfer->mbuf = m;
    488 			STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
    489 		}
    490 		fw_bindadd(fc, &fwip->fwb);
    491 
    492 		STAILQ_INIT(&fwip->xferlist);
    493 		for (i = 0; i < TX_MAX_QUEUE; i++) {
    494 			xfer = fw_xfer_alloc(M_FWIP);
    495 			if (xfer == NULL)
    496 				break;
    497 			xfer->send.spd = tx_speed;
    498 			xfer->fc = fwip->fd.fc;
    499 			xfer->sc = (void *)fwip;
    500 			xfer->hand = fwip_output_callback;
    501 			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
    502 		}
    503 	} else
    504 		xferq = fc->ir[fwip->dma_ch];
    505 
    506 	fwip->last_dest.hi = 0;
    507 	fwip->last_dest.lo = 0;
    508 
    509 	/* start dma */
    510 	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
    511 		fc->irx_enable(fc, fwip->dma_ch);
    512 
    513 #if defined(__FreeBSD__)
    514 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
    515 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
    516 #elif defined(__NetBSD__)
    517 	ifp->if_flags |= IFF_RUNNING;
    518 	ifp->if_flags &= ~IFF_OACTIVE;
    519 #endif
    520 
    521 #if 0
    522 	/* attempt to start output */
    523 	fwip_start(ifp);
    524 #endif
    525 	IF_INIT_RETURN(0);
    526 }
    527 
    528 static int
    529 fwip_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    530 {
    531 	IF_IOCTL_START(fwip, fwip);
    532 	int s, error;
    533 
    534 	switch (cmd) {
    535 	case SIOCSIFFLAGS:
    536 		s = splfwnet();
    537 		if (ifp->if_flags & IFF_UP) {
    538 #if defined(__FreeBSD__)
    539 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
    540 #elif defined(__NetBSD__)
    541 			if (!(ifp->if_flags & IFF_RUNNING))
    542 #endif
    543 				FWIP_INIT(fwip);
    544 		} else {
    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_STOP(fwip);
    551 		}
    552 		splx(s);
    553 		break;
    554 	case SIOCADDMULTI:
    555 	case SIOCDELMULTI:
    556 		break;
    557 	case SIOCSIFCAP:
    558 #ifdef DEVICE_POLLING
    559 	    {
    560 		struct ifreq *ifr = (struct ifreq *) data;
    561 		struct firewire_comm *fc = fc = fwip->fd.fc;
    562 
    563 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
    564 		    !(ifp->if_capenable & IFCAP_POLLING)) {
    565 			error = ether_poll_register(fwip_poll, ifp);
    566 			if (error)
    567 				return(error);
    568 			/* Disable interrupts */
    569 			fc->set_intr(fc, 0);
    570 			ifp->if_capenable |= IFCAP_POLLING;
    571 			return (error);
    572 
    573 		}
    574 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
    575 		    ifp->if_capenable & IFCAP_POLLING) {
    576 			error = ether_poll_deregister(ifp);
    577 			/* Enable interrupts. */
    578 			fc->set_intr(fc, 1);
    579 			ifp->if_capenable &= ~IFCAP_POLLING;
    580 			return (error);
    581 		}
    582 	    }
    583 #endif /* DEVICE_POLLING */
    584 		break;
    585 
    586 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__NetBSD__)
    587 	default:
    588 #else
    589 	case SIOCSIFADDR:
    590 	case SIOCGIFADDR:
    591 	case SIOCSIFMTU:
    592 #endif
    593 		s = splfwnet();
    594 		error = FIREWIRE_IOCTL(ifp, cmd, data);
    595 		splx(s);
    596 		return (error);
    597 #if defined(__DragonFly__) || \
    598     (defined(__FreeBSD__) && __FreeBSD_version < 500000)
    599 	default:
    600 		return (EINVAL);
    601 #endif
    602 	}
    603 
    604 	return (0);
    605 }
    606 
    607 static void
    608 fwip_post_busreset(void *arg)
    609 {
    610 	struct fwip_softc *fwip = arg;
    611 	struct crom_src *src;
    612 	struct crom_chunk *root;
    613 
    614 	src = fwip->fd.fc->crom_src;
    615 	root = fwip->fd.fc->crom_root;
    616 
    617 	/* RFC2734 IPv4 over IEEE1394 */
    618 	bzero(&fwip->unit4, sizeof(struct crom_chunk));
    619 	crom_add_chunk(src, root, &fwip->unit4, CROM_UDIR);
    620 	crom_add_entry(&fwip->unit4, CSRKEY_SPEC, CSRVAL_IETF);
    621 	crom_add_simple_text(src, &fwip->unit4, &fwip->spec4, "IANA");
    622 	crom_add_entry(&fwip->unit4, CSRKEY_VER, 1);
    623 	crom_add_simple_text(src, &fwip->unit4, &fwip->ver4, "IPv4");
    624 
    625 	/* RFC3146 IPv6 over IEEE1394 */
    626 	bzero(&fwip->unit6, sizeof(struct crom_chunk));
    627 	crom_add_chunk(src, root, &fwip->unit6, CROM_UDIR);
    628 	crom_add_entry(&fwip->unit6, CSRKEY_SPEC, CSRVAL_IETF);
    629 	crom_add_simple_text(src, &fwip->unit6, &fwip->spec6, "IANA");
    630 	crom_add_entry(&fwip->unit6, CSRKEY_VER, 2);
    631 	crom_add_simple_text(src, &fwip->unit6, &fwip->ver6, "IPv6");
    632 
    633 	fwip->last_dest.hi = 0;
    634 	fwip->last_dest.lo = 0;
    635 	FIREWIRE_BUSRESET(fwip->fw_softc.fwip_ifp);
    636 }
    637 
    638 static void
    639 fwip_output_callback(struct fw_xfer *xfer)
    640 {
    641 	struct fwip_softc *fwip;
    642 	struct ifnet *ifp;
    643 	int s;
    644 
    645 	fwip = (struct fwip_softc *)xfer->sc;
    646 	ifp = fwip->fw_softc.fwip_ifp;
    647 	/* XXX error check */
    648 	FWIPDEBUG(ifp, "resp = %d\n", xfer->resp);
    649 	if (xfer->resp != 0)
    650 		ifp->if_oerrors ++;
    651 
    652 	m_freem(xfer->mbuf);
    653 	fw_xfer_unload(xfer);
    654 
    655 	s = splfwnet();
    656 	FWIP_LOCK(fwip);
    657 	STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
    658 	FWIP_UNLOCK(fwip);
    659 	splx(s);
    660 
    661 	/* for queue full */
    662 	if (ifp->if_snd.ifq_head != NULL) {
    663 		fwip_start(ifp);
    664 	}
    665 }
    666 
    667 static void
    668 fwip_start(struct ifnet *ifp)
    669 {
    670 	struct fwip_softc *fwip =
    671 	    ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
    672 	int s;
    673 
    674 	FWIPDEBUG(ifp, "starting\n");
    675 
    676 	if (fwip->dma_ch < 0) {
    677 		struct mbuf	*m = NULL;
    678 
    679 		FWIPDEBUG(ifp, "not ready\n");
    680 
    681 		s = splfwnet();
    682 		do {
    683 			IF_DEQUEUE(&ifp->if_snd, m);
    684 			if (m != NULL)
    685 				m_freem(m);
    686 			ifp->if_oerrors ++;
    687 		} while (m != NULL);
    688 		splx(s);
    689 
    690 		return;
    691 	}
    692 
    693 	s = splfwnet();
    694 #if defined(__FreeBSD__)
    695 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
    696 #elif defined(__NetBSD__)
    697 	ifp->if_flags |= IFF_OACTIVE;
    698 #endif
    699 
    700 	if (ifp->if_snd.ifq_len != 0)
    701 		fwip_async_output(fwip, ifp);
    702 
    703 #if defined(__FreeBSD__)
    704 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
    705 #elif defined(__NetBSD__)
    706 	ifp->if_flags &= ~IFF_OACTIVE;
    707 #endif
    708 	splx(s);
    709 }
    710 
    711 /* Async. stream output */
    712 static void
    713 fwip_async_output(struct fwip_softc *fwip, struct ifnet *ifp)
    714 {
    715 	struct firewire_comm *fc = fwip->fd.fc;
    716 	struct mbuf *m;
    717 	struct m_tag *mtag;
    718 	struct fw_hwaddr *destfw;
    719 	struct fw_xfer *xfer;
    720 	struct fw_xferq *xferq;
    721 	struct fw_pkt *fp;
    722 	uint16_t nodeid;
    723 	int error;
    724 	int i = 0;
    725 
    726 	xfer = NULL;
    727 	xferq = fc->atq;
    728 	while ((xferq->queued < xferq->maxq - 1) &&
    729 	    (ifp->if_snd.ifq_head != NULL)) {
    730 		FWIP_LOCK(fwip);
    731 		xfer = STAILQ_FIRST(&fwip->xferlist);
    732 		if (xfer == NULL) {
    733 			FWIP_UNLOCK(fwip);
    734 #if 0
    735 			printf("if_fwip: lack of xfer\n");
    736 #endif
    737 			break;
    738 		}
    739 		STAILQ_REMOVE_HEAD(&fwip->xferlist, link);
    740 		FWIP_UNLOCK(fwip);
    741 
    742 		IF_DEQUEUE(&ifp->if_snd, m);
    743 		if (m == NULL) {
    744 			FWIP_LOCK(fwip);
    745 			STAILQ_INSERT_HEAD(&fwip->xferlist, xfer, link);
    746 			FWIP_UNLOCK(fwip);
    747 			break;
    748 		}
    749 
    750 		/*
    751 		 * Dig out the link-level address which
    752 		 * firewire_output got via arp or neighbour
    753 		 * discovery. If we don't have a link-level address,
    754 		 * just stick the thing on the broadcast channel.
    755 		 */
    756 		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, 0);
    757 		if (mtag == NULL)
    758 			destfw = 0;
    759 		else
    760 			destfw = (struct fw_hwaddr *) (mtag + 1);
    761 
    762 		/*
    763 		 * We don't do any bpf stuff here - the generic code
    764 		 * in firewire_output gives the packet to bpf before
    765 		 * it adds the link-level encapsulation.
    766 		 */
    767 
    768 		/*
    769 		 * Put the mbuf in the xfer early in case we hit an
    770 		 * error case below - fwip_output_callback will free
    771 		 * the mbuf.
    772 		 */
    773 		xfer->mbuf = m;
    774 
    775 		/*
    776 		 * We use the arp result (if any) to add a suitable firewire
    777 		 * packet header before handing off to the bus.
    778 		 */
    779 		fp = &xfer->send.hdr;
    780 		nodeid = FWLOCALBUS | fc->nodeid;
    781 		if ((m->m_flags & M_BCAST) || !destfw) {
    782 			/*
    783 			 * Broadcast packets are sent as GASP packets with
    784 			 * specifier ID 0x00005e, version 1 on the broadcast
    785 			 * channel. To be conservative, we send at the
    786 			 * slowest possible speed.
    787 			 */
    788 			uint32_t *p;
    789 
    790 			M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
    791 			p = mtod(m, uint32_t *);
    792 			fp->mode.stream.len = m->m_pkthdr.len;
    793 			fp->mode.stream.chtag = broadcast_channel;
    794 			fp->mode.stream.tcode = FWTCODE_STREAM;
    795 			fp->mode.stream.sy = 0;
    796 			xfer->send.spd = 0;
    797 			p[0] = htonl(nodeid << 16);
    798 			p[1] = htonl((0x5e << 24) | 1);
    799 		} else {
    800 			/*
    801 			 * Unicast packets are sent as block writes to the
    802 			 * target's unicast fifo address. If we can't
    803 			 * find the node address, we just give up. We
    804 			 * could broadcast it but that might overflow
    805 			 * the packet size limitations due to the
    806 			 * extra GASP header. Note: the hardware
    807 			 * address is stored in network byte order to
    808 			 * make life easier for ARP.
    809 			 */
    810 			struct fw_device *fd;
    811 			struct fw_eui64 eui;
    812 
    813 			eui.hi = ntohl(destfw->sender_unique_ID_hi);
    814 			eui.lo = ntohl(destfw->sender_unique_ID_lo);
    815 			if (fwip->last_dest.hi != eui.hi ||
    816 			    fwip->last_dest.lo != eui.lo) {
    817 				fd = fw_noderesolve_eui64(fc, &eui);
    818 				if (!fd) {
    819 					/* error */
    820 					ifp->if_oerrors ++;
    821 					/* XXX set error code */
    822 					fwip_output_callback(xfer);
    823 					continue;
    824 
    825 				}
    826 				fwip->last_hdr.mode.wreqb.dst = FWLOCALBUS | fd->dst;
    827 				fwip->last_hdr.mode.wreqb.tlrt = 0;
    828 				fwip->last_hdr.mode.wreqb.tcode = FWTCODE_WREQB;
    829 				fwip->last_hdr.mode.wreqb.pri = 0;
    830 				fwip->last_hdr.mode.wreqb.src = nodeid;
    831 				fwip->last_hdr.mode.wreqb.dest_hi =
    832 					ntohs(destfw->sender_unicast_FIFO_hi);
    833 				fwip->last_hdr.mode.wreqb.dest_lo =
    834 					ntohl(destfw->sender_unicast_FIFO_lo);
    835 				fwip->last_hdr.mode.wreqb.extcode = 0;
    836 				fwip->last_dest = eui;
    837 			}
    838 
    839 			fp->mode.wreqb = fwip->last_hdr.mode.wreqb;
    840 			fp->mode.wreqb.len = m->m_pkthdr.len;
    841 			xfer->send.spd = min(destfw->sspd, fc->speed);
    842 		}
    843 
    844 		xfer->send.pay_len = m->m_pkthdr.len;
    845 
    846 		error = fw_asyreq(fc, -1, xfer);
    847 		if (error == EAGAIN) {
    848 			/*
    849 			 * We ran out of tlabels - requeue the packet
    850 			 * for later transmission.
    851 			 */
    852 			xfer->mbuf = 0;
    853 			FWIP_LOCK(fwip);
    854 			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
    855 			FWIP_UNLOCK(fwip);
    856 			IF_PREPEND(&ifp->if_snd, m);
    857 			break;
    858 		}
    859 		if (error) {
    860 			/* error */
    861 			ifp->if_oerrors ++;
    862 			/* XXX set error code */
    863 			fwip_output_callback(xfer);
    864 			continue;
    865 		} else {
    866 			ifp->if_opackets ++;
    867 			i++;
    868 		}
    869 	}
    870 #if 0
    871 	if (i > 1)
    872 		printf("%d queued\n", i);
    873 #endif
    874 	if (i > 0)
    875 		xferq->start(fc);
    876 }
    877 
    878 static void
    879 fwip_start_send (void *arg, int count)
    880 {
    881 	struct fwip_softc *fwip = arg;
    882 
    883 	fwip->fd.fc->atq->start(fwip->fd.fc);
    884 }
    885 
    886 /* Async. stream output */
    887 static void
    888 fwip_stream_input(struct fw_xferq *xferq)
    889 {
    890 	struct mbuf *m, *m0;
    891 	struct m_tag *mtag;
    892 	struct ifnet *ifp;
    893 	struct fwip_softc *fwip;
    894 	struct fw_bulkxfer *sxfer;
    895 	struct fw_pkt *fp;
    896 	uint16_t src;
    897 	uint32_t *p;
    898 
    899 	fwip = (struct fwip_softc *)xferq->sc;
    900 	ifp = fwip->fw_softc.fwip_ifp;
    901 	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
    902 		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
    903 		fp = mtod(sxfer->mbuf, struct fw_pkt *);
    904 		if (fwip->fd.fc->irx_post != NULL)
    905 			fwip->fd.fc->irx_post(fwip->fd.fc, fp->mode.ld);
    906 		m = sxfer->mbuf;
    907 
    908 		/* insert new rbuf */
    909 		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
    910 		if (m0 != NULL) {
    911 			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
    912 			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
    913 		} else
    914 			printf("fwip_as_input: m_getcl failed\n");
    915 
    916 		/*
    917 		 * We must have a GASP header - leave the
    918 		 * encapsulation sanity checks to the generic
    919 		 * code. Remeber that we also have the firewire async
    920 		 * stream header even though that isn't accounted for
    921 		 * in mode.stream.len.
    922 		 */
    923 		if (sxfer->resp != 0 || fp->mode.stream.len <
    924 		    2*sizeof(uint32_t)) {
    925 			m_freem(m);
    926 			ifp->if_ierrors ++;
    927 			continue;
    928 		}
    929 		m->m_len = m->m_pkthdr.len = fp->mode.stream.len
    930 			+ sizeof(fp->mode.stream);
    931 
    932 		/*
    933 		 * If we received the packet on the broadcast channel,
    934 		 * mark it as broadcast, otherwise we assume it must
    935 		 * be multicast.
    936 		 */
    937 		if (fp->mode.stream.chtag == broadcast_channel)
    938 			m->m_flags |= M_BCAST;
    939 		else
    940 			m->m_flags |= M_MCAST;
    941 
    942 		/*
    943 		 * Make sure we recognise the GASP specifier and
    944 		 * version.
    945 		 */
    946 		p = mtod(m, uint32_t *);
    947 		if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) != 0x00005e
    948 		    || (ntohl(p[2]) & 0xffffff) != 1) {
    949 			FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n",
    950 			    ntohl(p[1]), ntohl(p[2]));
    951 			m_freem(m);
    952 			ifp->if_ierrors ++;
    953 			continue;
    954 		}
    955 
    956 		/*
    957 		 * Record the sender ID for possible BPF usage.
    958 		 */
    959 		src = ntohl(p[1]) >> 16;
    960 		if (bpf_peers_present(ifp->if_bpf)) {
    961 			mtag = m_tag_alloc(MTAG_FIREWIRE,
    962 			    MTAG_FIREWIRE_SENDER_EUID,
    963 			    2*sizeof(uint32_t), M_NOWAIT);
    964 			if (mtag) {
    965 				/* bpf wants it in network byte order */
    966 				struct fw_device *fd;
    967 				uint32_t *p2 = (uint32_t *) (mtag + 1);
    968 				fd = fw_noderesolve_nodeid(fwip->fd.fc,
    969 				    src & 0x3f);
    970 				if (fd) {
    971 					p2[0] = htonl(fd->eui.hi);
    972 					p2[1] = htonl(fd->eui.lo);
    973 				} else {
    974 					p2[0] = 0;
    975 					p2[1] = 0;
    976 				}
    977 				m_tag_prepend(m, mtag);
    978 			}
    979 		}
    980 
    981 		/*
    982 		 * Trim off the GASP header
    983 		 */
    984 		m_adj(m, 3*sizeof(uint32_t));
    985 		m->m_pkthdr.rcvif = ifp;
    986 		FIREWIRE_INPUT(ifp, m, src);
    987 		ifp->if_ipackets ++;
    988 	}
    989 	if (STAILQ_FIRST(&xferq->stfree) != NULL)
    990 		fwip->fd.fc->irx_enable(fwip->fd.fc, fwip->dma_ch);
    991 }
    992 
    993 static inline void
    994 fwip_unicast_input_recycle(struct fwip_softc *fwip, struct fw_xfer *xfer)
    995 {
    996 	struct mbuf *m;
    997 
    998 	/*
    999 	 * We have finished with a unicast xfer. Allocate a new
   1000 	 * cluster and stick it on the back of the input queue.
   1001 	 */
   1002 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
   1003 	if (m == NULL)
   1004 		printf("fwip_unicast_input_recycle: m_getcl failed\n");
   1005 	xfer->mbuf = m;
   1006 	xfer->recv.payload = mtod(m, uint32_t *);
   1007 	xfer->recv.pay_len = MCLBYTES;
   1008 	xfer->mbuf = m;
   1009 	STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
   1010 }
   1011 
   1012 static void
   1013 fwip_unicast_input(struct fw_xfer *xfer)
   1014 {
   1015 	uint64_t address;
   1016 	struct mbuf *m;
   1017 	struct m_tag *mtag;
   1018 	struct ifnet *ifp;
   1019 	struct fwip_softc *fwip;
   1020 	struct fw_pkt *fp;
   1021 	//struct fw_pkt *sfp;
   1022 	int rtcode;
   1023 
   1024 	fwip = (struct fwip_softc *)xfer->sc;
   1025 	ifp = fwip->fw_softc.fwip_ifp;
   1026 	m = xfer->mbuf;
   1027 	xfer->mbuf = 0;
   1028 	fp = &xfer->recv.hdr;
   1029 
   1030 	/*
   1031 	 * Check the fifo address - we only accept addresses of
   1032 	 * exactly INET_FIFO.
   1033 	 */
   1034 	address = ((uint64_t)fp->mode.wreqb.dest_hi << 32)
   1035 		| fp->mode.wreqb.dest_lo;
   1036 	if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
   1037 		rtcode = FWRCODE_ER_TYPE;
   1038 	} else if (address != INET_FIFO) {
   1039 		rtcode = FWRCODE_ER_ADDR;
   1040 	} else {
   1041 		rtcode = FWRCODE_COMPLETE;
   1042 	}
   1043 
   1044 	/*
   1045 	 * Pick up a new mbuf and stick it on the back of the receive
   1046 	 * queue.
   1047 	 */
   1048 	fwip_unicast_input_recycle(fwip, xfer);
   1049 
   1050 	/*
   1051 	 * If we've already rejected the packet, give up now.
   1052 	 */
   1053 	if (rtcode != FWRCODE_COMPLETE) {
   1054 		m_freem(m);
   1055 		ifp->if_ierrors ++;
   1056 		return;
   1057 	}
   1058 
   1059 	if (bpf_peers_present(ifp->if_bpf)) {
   1060 		/*
   1061 		 * Record the sender ID for possible BPF usage.
   1062 		 */
   1063 		mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID,
   1064 		    2*sizeof(uint32_t), M_NOWAIT);
   1065 		if (mtag) {
   1066 			/* bpf wants it in network byte order */
   1067 			struct fw_device *fd;
   1068 			uint32_t *p = (uint32_t *) (mtag + 1);
   1069 			fd = fw_noderesolve_nodeid(fwip->fd.fc,
   1070 			    fp->mode.wreqb.src & 0x3f);
   1071 			if (fd) {
   1072 				p[0] = htonl(fd->eui.hi);
   1073 				p[1] = htonl(fd->eui.lo);
   1074 			} else {
   1075 				p[0] = 0;
   1076 				p[1] = 0;
   1077 			}
   1078 			m_tag_prepend(m, mtag);
   1079 		}
   1080 	}
   1081 
   1082 	/*
   1083 	 * Hand off to the generic encapsulation code. We don't use
   1084 	 * ifp->if_input so that we can pass the source nodeid as an
   1085 	 * argument to facilitate link-level fragment reassembly.
   1086 	 */
   1087 	m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len;
   1088 	m->m_pkthdr.rcvif = ifp;
   1089 	FIREWIRE_INPUT(ifp, m, fp->mode.wreqb.src);
   1090 	ifp->if_ipackets ++;
   1091 }
   1092 
   1093 #if defined(__FreeBSD__)
   1094 static devclass_t fwip_devclass;
   1095 
   1096 static device_method_t fwip_methods[] = {
   1097 	/* device interface */
   1098 	DEVMETHOD(device_identify,	fwip_identify),
   1099 	DEVMETHOD(device_probe,		fwip_probe),
   1100 	DEVMETHOD(device_attach,	fwip_attach),
   1101 	DEVMETHOD(device_detach,	fwip_detach),
   1102 	{ 0, 0 }
   1103 };
   1104 
   1105 static driver_t fwip_driver = {
   1106         "fwip",
   1107 	fwip_methods,
   1108 	sizeof(struct fwip_softc),
   1109 };
   1110 
   1111 
   1112 #ifdef __DragonFly__
   1113 DECLARE_DUMMY_MODULE(fwip);
   1114 #endif
   1115 DRIVER_MODULE(fwip, firewire, fwip_driver, fwip_devclass, 0, 0);
   1116 MODULE_VERSION(fwip, 1);
   1117 MODULE_DEPEND(fwip, firewire, 1, 1, 1);
   1118 #elif defined(__NetBSD__)
   1119 CFATTACH_DECL(fwip, sizeof (struct fwip_softc),
   1120     fwipmatch, fwipattach, fwipdetach, NULL);
   1121 #endif
   1122