xref: /freebsd/sys/dev/firewire/if_fwip.c (revision f5dc2263ab1be8a35a7e27e82103f9ccd41ae584)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2004
5  *	Doug Rabson
6  * Copyright (c) 2002-2003
7  * 	Hidetoshi Shimokawa. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *
20  *	This product includes software developed by Hidetoshi Shimokawa.
21  *
22  * 4. Neither the name of the author nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  */
39 
40 #ifdef HAVE_KERNEL_OPTION_HEADERS
41 #include "opt_device_polling.h"
42 #include "opt_inet.h"
43 #endif
44 
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
53 #include <sys/taskqueue.h>
54 #include <sys/module.h>
55 #include <sys/bus.h>
56 #include <machine/bus.h>
57 
58 #include <net/bpf.h>
59 #include <net/if.h>
60 #include <net/if_var.h>
61 #include <net/firewire.h>
62 #include <net/if_arp.h>
63 #include <net/if_types.h>
64 #include <dev/firewire/firewire.h>
65 #include <dev/firewire/firewirereg.h>
66 #include <dev/firewire/iec13213.h>
67 #include <dev/firewire/if_fwipvar.h>
68 #include <dev/firewire/fw_net.h>
69 
70 /*
71  * We really need a mechanism for allocating regions in the FIFO
72  * address space. We pick a address in the OHCI controller's 'middle'
73  * address space. This means that the controller will automatically
74  * send responses for us, which is fine since we don't have any
75  * important information to put in the response anyway.
76  */
77 #define INET_FIFO	0xfffe00000000LL
78 
79 #define FWIPDEBUG	if (fwipdebug) if_printf
80 #define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
81 
82 /* network interface */
83 static void fwip_start (if_t);
84 static int fwip_ioctl (if_t, u_long, caddr_t);
85 static void fwip_init (void *);
86 
87 static void fwip_post_busreset (void *);
88 static void fwip_output_callback (struct fw_xfer *);
89 static void fwip_async_output (struct fwip_softc *, if_t);
90 static void fwip_start_send (void *, int);
91 static void fwip_stream_input (struct fw_xferq *);
92 static void fwip_unicast_input(struct fw_xfer *);
93 
94 /* tag field: bits [7:6] = 0b11 (broadcast), channel field: bits [5:0] = 31 */
95 #define FWXFERQ_TAG_ALL		(3 << 6)
96 #define FW_IP_CHANNEL		31
97 /* GASP header: specifier_hi + specifier_lo/version + payload */
98 #define FW_GASP_HDR_LEN		(3 * sizeof(uint32_t))
99 
100 static int fwipdebug = 0;
101 static int broadcast_channel = FWXFERQ_TAG_ALL | FW_IP_CHANNEL;
102 static int tx_speed = FWSPD_S400;
103 static int rx_queue_len = FWMAXQUEUE;
104 
105 static MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over FireWire interface");
106 SYSCTL_INT(_debug, OID_AUTO, if_fwip_debug, CTLFLAG_RW, &fwipdebug, 0, "");
107 SYSCTL_DECL(_hw_firewire);
108 static SYSCTL_NODE(_hw_firewire, OID_AUTO, fwip, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
109 	"Firewire ip subsystem");
110 SYSCTL_INT(_hw_firewire_fwip, OID_AUTO, rx_queue_len, CTLFLAG_RWTUN, &rx_queue_len,
111 	0, "Length of the receive queue");
112 
113 #ifdef DEVICE_POLLING
114 static poll_handler_t fwip_poll;
115 
116 static int
117 fwip_poll(if_t ifp, enum poll_cmd cmd, int count)
118 {
119 	struct fwip_softc *fwip;
120 	struct firewire_comm *fc;
121 
122 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
123 		return (0);
124 
125 	fwip = ((struct fwip_eth_softc *)if_getsoftc(ifp))->fwip;
126 	fc = fwip->fd.fc;
127 	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
128 	return (0);
129 }
130 #endif /* DEVICE_POLLING */
131 
132 static void
133 fwip_identify(driver_t *driver, device_t parent)
134 {
135 	BUS_ADD_CHILD(parent, 0, "fwip", device_get_unit(parent));
136 }
137 
138 static int
139 fwip_probe(device_t dev)
140 {
141 	device_t pa;
142 
143 	if (fw_get_unit(dev) != NULL)
144 		return (ENXIO);
145 
146 	pa = device_get_parent(dev);
147 	if (device_get_unit(dev) != device_get_unit(pa))
148 		return (ENXIO);
149 
150 	device_set_desc(dev, "IP over FireWire");
151 	return (0);
152 }
153 
154 static int
155 fwip_attach(device_t dev)
156 {
157 	struct fwip_softc *fwip;
158 	if_t ifp;
159 	int unit;
160 	struct fw_hwaddr *hwaddr;
161 
162 	fwip = ((struct fwip_softc *)device_get_softc(dev));
163 	unit = device_get_unit(dev);
164 	ifp = fwip->fw_softc.fwip_ifp = if_alloc(IFT_IEEE1394);
165 
166 	mtx_init(&fwip->mtx, "fwip", NULL, MTX_DEF);
167 	/* XXX */
168 	fwip->dma_ch = -1;
169 
170 	fwip->fd.fc = fw_get_comm(dev);
171 	if (tx_speed < 0)
172 		tx_speed = fwip->fd.fc->speed;
173 
174 	fwip->fd.dev = dev;
175 	fwip->fd.post_explore = NULL;
176 	fwip->fd.post_busreset = fwip_post_busreset;
177 	fwip->fw_softc.fwip = fwip;
178 	TASK_INIT(&fwip->start_send, 0, fwip_start_send, fwip);
179 
180 	/*
181 	 * Encode our hardware the way that arp likes it.
182 	 */
183 	hwaddr = &IFP2FWC(fwip->fw_softc.fwip_ifp)->fc_hwaddr;
184 	hwaddr->sender_unique_ID_hi = htonl(fwip->fd.fc->eui.hi);
185 	hwaddr->sender_unique_ID_lo = htonl(fwip->fd.fc->eui.lo);
186 	hwaddr->sender_max_rec = fwip->fd.fc->maxrec;
187 	hwaddr->sspd = fwip->fd.fc->speed;
188 	hwaddr->sender_unicast_FIFO_hi = htons((uint16_t)(INET_FIFO >> 32));
189 	hwaddr->sender_unicast_FIFO_lo = htonl((uint32_t)INET_FIFO);
190 
191 	/* fill the rest and attach interface */
192 	if_setsoftc(ifp, &fwip->fw_softc);
193 
194 	if_initname(ifp, device_get_name(dev), unit);
195 	if_setinitfn(ifp, fwip_init);
196 	if_setstartfn(ifp, fwip_start);
197 	if_setioctlfn(ifp, fwip_ioctl);
198 	if_setflags(ifp, (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST));
199 	if_setsendqlen(ifp, TX_MAX_QUEUE);
200 #ifdef DEVICE_POLLING
201 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
202 #endif
203 
204 	firewire_ifattach(ifp, hwaddr);
205 
206 	FWIPDEBUG(ifp, "interface created\n");
207 	return (0);
208 }
209 
210 static void
211 fwip_stop(struct fwip_softc *fwip)
212 {
213 	struct firewire_comm *fc;
214 	struct fw_xferq *xferq;
215 	if_t ifp = fwip->fw_softc.fwip_ifp;
216 	int i;
217 
218 	fc = fwip->fd.fc;
219 
220 	if (fwip->dma_ch >= 0) {
221 		xferq = fc->ir[fwip->dma_ch];
222 
223 		if (xferq->flag & FWXFERQ_RUNNING)
224 			fc->irx_disable(fc, fwip->dma_ch);
225 		xferq->flag &=
226 			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
227 			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
228 		xferq->hand =  NULL;
229 
230 		for (i = 0; i < xferq->bnchunk; i++)
231 			m_freem(xferq->bulkxfer[i].mbuf);
232 		free(xferq->bulkxfer, M_FWIP);
233 
234 		fw_bindremove(fc, &fwip->fwb);
235 		FW_NET_FREE_XFERLIST(&fwip->fwb.xferlist);
236 		FW_NET_FREE_XFERLIST(&fwip->xferlist);
237 		STAILQ_INIT(&fwip->xferlist);
238 
239 		xferq->bulkxfer =  NULL;
240 		fwip->dma_ch = -1;
241 	}
242 
243 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
244 }
245 
246 static int
247 fwip_detach(device_t dev)
248 {
249 	struct fwip_softc *fwip;
250 	if_t ifp;
251 
252 	fwip = (struct fwip_softc *)device_get_softc(dev);
253 	ifp = fwip->fw_softc.fwip_ifp;
254 
255 #ifdef DEVICE_POLLING
256 	if (if_getcapenable(ifp) & IFCAP_POLLING)
257 		ether_poll_deregister(ifp);
258 #endif
259 
260 	fwip_stop(fwip);
261 	firewire_ifdetach(ifp);
262 	if_free(ifp);
263 	mtx_destroy(&fwip->mtx);
264 
265 	return 0;
266 }
267 
268 static void
269 fwip_init(void *arg)
270 {
271 	struct fwip_softc *fwip = ((struct fwip_eth_softc *)arg)->fwip;
272 	struct firewire_comm *fc;
273 	if_t ifp = fwip->fw_softc.fwip_ifp;
274 	struct fw_xferq *xferq;
275 	struct fw_xfer *xfer;
276 	struct mbuf *m;
277 	int i;
278 
279 	FWIPDEBUG(ifp, "initializing\n");
280 
281 	fc = fwip->fd.fc;
282 #define START 0
283 	if (fwip->dma_ch < 0) {
284 		fwip->dma_ch = fw_open_isodma(fc, /* tx */0);
285 		if (fwip->dma_ch < 0)
286 			return;
287 		xferq = fc->ir[fwip->dma_ch];
288 		xferq->flag |= FWXFERQ_EXTBUF |
289 				FWXFERQ_HANDLER | FWXFERQ_STREAM;
290 		xferq->flag &= ~0xff;
291 		xferq->flag |= broadcast_channel & 0xff;
292 		/* register fwip_input handler */
293 		xferq->sc = (caddr_t) fwip;
294 		xferq->hand = fwip_stream_input;
295 		xferq->bnchunk = rx_queue_len;
296 		xferq->bnpacket = 1;
297 		xferq->psize = MCLBYTES;
298 		xferq->queued = 0;
299 		xferq->buf = NULL;
300 		xferq->bulkxfer = malloc(
301 			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
302 							M_FWIP, M_WAITOK);
303 		STAILQ_INIT(&xferq->stvalid);
304 		STAILQ_INIT(&xferq->stfree);
305 		STAILQ_INIT(&xferq->stdma);
306 		xferq->stproc = NULL;
307 		fw_net_init_iso_chunks(xferq);
308 
309 		fwip->fwb.start = INET_FIFO;
310 		fwip->fwb.end = INET_FIFO + 16384; /* S3200 packet size */
311 
312 		/* pre-allocate xfer */
313 		STAILQ_INIT(&fwip->fwb.xferlist);
314 		for (i = 0; i < rx_queue_len; i++) {
315 			xfer = fw_xfer_alloc(M_FWIP);
316 			if (xfer == NULL)
317 				break;
318 			m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
319 			xfer->recv.payload = mtod(m, uint32_t *);
320 			xfer->recv.pay_len = MCLBYTES;
321 			xfer->hand = fwip_unicast_input;
322 			xfer->fc = fc;
323 			xfer->sc = (caddr_t)fwip;
324 			xfer->mbuf = m;
325 			STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
326 		}
327 		fw_bindadd(fc, &fwip->fwb);
328 
329 		STAILQ_INIT(&fwip->xferlist);
330 		for (i = 0; i < TX_MAX_QUEUE; i++) {
331 			xfer = fw_net_alloc_txfer(fwip->fd.fc, tx_speed,
332 			    fwip, fwip_output_callback, M_FWIP);
333 			if (xfer == NULL)
334 				break;
335 			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
336 		}
337 	} else
338 		xferq = fc->ir[fwip->dma_ch];
339 
340 	fwip->last_dest.hi = 0;
341 	fwip->last_dest.lo = 0;
342 
343 	/* start dma */
344 	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
345 		fc->irx_enable(fc, fwip->dma_ch);
346 
347 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
348 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
349 
350 }
351 
352 static int
353 fwip_ioctl(if_t ifp, u_long cmd, caddr_t data)
354 {
355 	struct fwip_softc *fwip = ((struct fwip_eth_softc *)if_getsoftc(ifp))->fwip;
356 	int error;
357 
358 	switch (cmd) {
359 	case SIOCSIFFLAGS:
360 		if (if_getflags(ifp) & IFF_UP) {
361 			if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
362 				fwip_init(&fwip->fw_softc);
363 		} else {
364 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
365 				fwip_stop(fwip);
366 		}
367 		break;
368 	case SIOCADDMULTI:
369 	case SIOCDELMULTI:
370 		break;
371 	case SIOCSIFCAP:
372 #ifdef DEVICE_POLLING
373 	    {
374 		struct ifreq *ifr = (struct ifreq *) data;
375 
376 		error = fw_net_poll_ioctl(ifp, ifr,
377 		    fwip->fd.fc, fwip_poll);
378 		if (error >= 0)
379 			return (error);
380 	    }
381 #endif /* DEVICE_POLLING */
382 		break;
383 	default:
384 		error = firewire_ioctl(ifp, cmd, data);
385 		return (error);
386 	}
387 
388 	return (0);
389 }
390 
391 static void
392 fwip_post_busreset(void *arg)
393 {
394 	struct fwip_softc *fwip = arg;
395 	struct crom_src *src;
396 	struct crom_chunk *root;
397 
398 	src = fwip->fd.fc->crom_src;
399 	root = fwip->fd.fc->crom_root;
400 
401 	/* RFC2734 IPv4 over IEEE1394 */
402 	bzero(&fwip->unit4, sizeof(struct crom_chunk));
403 	crom_add_chunk(src, root, &fwip->unit4, CROM_UDIR);
404 	crom_add_entry(&fwip->unit4, CSRKEY_SPEC, CSRVAL_IETF);
405 	crom_add_simple_text(src, &fwip->unit4, &fwip->spec4, "IANA");
406 	crom_add_entry(&fwip->unit4, CSRKEY_VER, 1);
407 	crom_add_simple_text(src, &fwip->unit4, &fwip->ver4, "IPv4");
408 
409 	/* RFC3146 IPv6 over IEEE1394 */
410 	bzero(&fwip->unit6, sizeof(struct crom_chunk));
411 	crom_add_chunk(src, root, &fwip->unit6, CROM_UDIR);
412 	crom_add_entry(&fwip->unit6, CSRKEY_SPEC, CSRVAL_IETF);
413 	crom_add_simple_text(src, &fwip->unit6, &fwip->spec6, "IANA");
414 	crom_add_entry(&fwip->unit6, CSRKEY_VER, 2);
415 	crom_add_simple_text(src, &fwip->unit6, &fwip->ver6, "IPv6");
416 
417 	fwip->last_dest.hi = 0;
418 	fwip->last_dest.lo = 0;
419 	firewire_busreset(fwip->fw_softc.fwip_ifp);
420 }
421 
422 static void
423 fwip_output_callback(struct fw_xfer *xfer)
424 {
425 	struct fwip_softc *fwip;
426 	if_t ifp;
427 
428 	fwip = (struct fwip_softc *)xfer->sc;
429 	ifp = fwip->fw_softc.fwip_ifp;
430 	/* XXX error check */
431 	FWIPDEBUG(ifp, "resp = %d\n", xfer->resp);
432 	if (xfer->resp != 0)
433 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
434 	m_freem(xfer->mbuf);
435 	fw_xfer_unload(xfer);
436 
437 	FWIP_LOCK(fwip);
438 	STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
439 	FWIP_UNLOCK(fwip);
440 
441 	/* for queue full */
442 	if (!if_sendq_empty(ifp)) {
443 		fwip_start(ifp);
444 	}
445 }
446 
447 static void
448 fwip_start(if_t ifp)
449 {
450 	struct fwip_softc *fwip = ((struct fwip_eth_softc *)if_getsoftc(ifp))->fwip;
451 
452 	FWIPDEBUG(ifp, "starting\n");
453 
454 	if (fwip->dma_ch < 0) {
455 		FWIPDEBUG(ifp, "not ready\n");
456 
457 		fw_net_drain_sendq(ifp);
458 
459 		return;
460 	}
461 
462 	if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
463 
464 	if (!if_sendq_empty(ifp))
465 		fwip_async_output(fwip, ifp);
466 
467 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
468 }
469 
470 /* Async. stream output */
471 static void
472 fwip_async_output(struct fwip_softc *fwip, if_t ifp)
473 {
474 	struct firewire_comm *fc = fwip->fd.fc;
475 	struct mbuf *m;
476 	struct m_tag *mtag;
477 	struct fw_hwaddr *destfw;
478 	struct fw_xfer *xfer;
479 	struct fw_xferq *xferq;
480 	struct fw_pkt *fp;
481 	uint16_t nodeid;
482 	int error;
483 	int i = 0;
484 
485 	xfer = NULL;
486 	xferq = fc->atq;
487 	while ((xferq->queued < xferq->maxq - 1) &&
488 			!if_sendq_empty(ifp)) {
489 		FWIP_LOCK(fwip);
490 		xfer = STAILQ_FIRST(&fwip->xferlist);
491 		if (xfer == NULL) {
492 			FWIP_UNLOCK(fwip);
493 			break;
494 		}
495 		STAILQ_REMOVE_HEAD(&fwip->xferlist, link);
496 		FWIP_UNLOCK(fwip);
497 
498 		m = if_dequeue(ifp);
499 		if (m == NULL) {
500 			FWIP_LOCK(fwip);
501 			STAILQ_INSERT_HEAD(&fwip->xferlist, xfer, link);
502 			FWIP_UNLOCK(fwip);
503 			break;
504 		}
505 
506 		/*
507 		 * Dig out the link-level address which
508 		 * firewire_output got via arp or neighbour
509 		 * discovery. If we don't have a link-level address,
510 		 * just stick the thing on the broadcast channel.
511 		 */
512 		mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, 0);
513 		if (mtag == NULL)
514 			destfw = NULL;
515 		else
516 			destfw = (struct fw_hwaddr *) (mtag + 1);
517 
518 
519 		/*
520 		 * We don't do any bpf stuff here - the generic code
521 		 * in firewire_output gives the packet to bpf before
522 		 * it adds the link-level encapsulation.
523 		 */
524 
525 		/*
526 		 * We use the arp result (if any) to add a suitable firewire
527 		 * packet header before handing off to the bus.
528 		 */
529 		fp = &xfer->send.hdr;
530 		nodeid = FWLOCALBUS | fc->nodeid;
531 		if ((m->m_flags & M_BCAST) || !destfw) {
532 			/*
533 			 * Broadcast packets are sent as GASP packets with
534 			 * specifier ID 0x00005e, version 1 on the broadcast
535 			 * channel. To be conservative, we send at the
536 			 * slowest possible speed.
537 			 */
538 			uint32_t *p;
539 
540 			/*
541 			 * M_PREPEND may move M_PKTHDR to a new head mbuf.
542 			 * Keep xfer->mbuf NULL until it succeeds.
543 			 */
544 			xfer->mbuf = NULL;
545 			M_PREPEND(m, 2*sizeof(uint32_t), M_NOWAIT);
546 			if (m == NULL) {
547 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
548 				fwip_output_callback(xfer);
549 				continue;
550 			}
551 			xfer->mbuf = m;
552 			p = mtod(m, uint32_t *);
553 			fp->mode.stream.len = m->m_pkthdr.len;
554 			fp->mode.stream.chtag = broadcast_channel;
555 			fp->mode.stream.tcode = FWTCODE_STREAM;
556 			fp->mode.stream.sy = 0;
557 			xfer->send.spd = FWSPD_S100;
558 			p[0] = htonl(nodeid << 16);
559 			p[1] = htonl((CSRVAL_IETF << 24) | 1);
560 		} else {
561 			/*
562 			 * Unicast packets are sent as block writes to the
563 			 * target's unicast fifo address. If we can't
564 			 * find the node address, we just give up. We
565 			 * could broadcast it but that might overflow
566 			 * the packet size limitations due to the
567 			 * extra GASP header. Note: the hardware
568 			 * address is stored in network byte order to
569 			 * make life easier for ARP.
570 			 */
571 			struct fw_device *fd;
572 			struct fw_eui64 eui;
573 
574 			/*
575 			 * Error paths below let the callback free m.
576 			 */
577 			xfer->mbuf = m;
578 			eui.hi = ntohl(destfw->sender_unique_ID_hi);
579 			eui.lo = ntohl(destfw->sender_unique_ID_lo);
580 			if (fwip->last_dest.hi != eui.hi ||
581 			    fwip->last_dest.lo != eui.lo) {
582 				fd = fw_noderesolve_eui64(fc, &eui);
583 				if (!fd) {
584 					/* error */
585 					if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
586 					/* XXX set error code */
587 					fwip_output_callback(xfer);
588 					continue;
589 
590 				}
591 				fwip->last_hdr.mode.wreqb.dst = FWLOCALBUS | fd->dst;
592 				fwip->last_hdr.mode.wreqb.tlrt = 0;
593 				fwip->last_hdr.mode.wreqb.tcode = FWTCODE_WREQB;
594 				fwip->last_hdr.mode.wreqb.pri = 0;
595 				fwip->last_hdr.mode.wreqb.src = nodeid;
596 				fwip->last_hdr.mode.wreqb.dest_hi =
597 					ntohs(destfw->sender_unicast_FIFO_hi);
598 				fwip->last_hdr.mode.wreqb.dest_lo =
599 					ntohl(destfw->sender_unicast_FIFO_lo);
600 				fwip->last_hdr.mode.wreqb.extcode = 0;
601 				fwip->last_dest = eui;
602 			}
603 
604 			fp->mode.wreqb = fwip->last_hdr.mode.wreqb;
605 			fp->mode.wreqb.len = m->m_pkthdr.len;
606 			xfer->send.spd = min(destfw->sspd, fc->speed);
607 		}
608 
609 		xfer->send.pay_len = m->m_pkthdr.len;
610 
611 		error = fw_asyreq(fc, -1, xfer);
612 		if (error == EAGAIN) {
613 			/*
614 			 * We ran out of tlabels - requeue the packet
615 			 * for later transmission.
616 			 */
617 			xfer->mbuf = 0;
618 			FWIP_LOCK(fwip);
619 			STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
620 			FWIP_UNLOCK(fwip);
621 			if_sendq_prepend(ifp, m);
622 			break;
623 		}
624 		if (error) {
625 			/* error */
626 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
627 			/* XXX set error code */
628 			fwip_output_callback(xfer);
629 			continue;
630 		} else {
631 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
632 			i++;
633 		}
634 	}
635 	if (i > 0)
636 		xferq->start(fc);
637 }
638 
639 static void
640 fwip_start_send (void *arg, int count)
641 {
642 	struct fwip_softc *fwip = arg;
643 
644 	fwip->fd.fc->atq->start(fwip->fd.fc);
645 }
646 
647 /* Async. stream output */
648 static void
649 fwip_stream_input(struct fw_xferq *xferq)
650 {
651 	struct epoch_tracker et;
652 	struct mbuf *m, *m0;
653 	struct m_tag *mtag;
654 	if_t ifp;
655 	struct fwip_softc *fwip;
656 	struct fw_bulkxfer *sxfer;
657 	struct fw_pkt *fp;
658 	uint16_t src;
659 	uint32_t *p;
660 
661 	fwip = (struct fwip_softc *)xferq->sc;
662 	ifp = fwip->fw_softc.fwip_ifp;
663 
664 	NET_EPOCH_ENTER(et);
665 	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
666 		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
667 		fp = mtod(sxfer->mbuf, struct fw_pkt *);
668 		if (fwip->fd.fc->irx_post != NULL)
669 			fwip->fd.fc->irx_post(fwip->fd.fc, fp->mode.ld);
670 		m = sxfer->mbuf;
671 
672 		/* insert new rbuf */
673 		sxfer->mbuf = m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
674 		if (m0 != NULL) {
675 			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
676 			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
677 		} else
678 			printf("fwip_as_input: m_getcl failed\n");
679 
680 		/*
681 		 * We must have a GASP header - leave the
682 		 * encapsulation sanity checks to the generic
683 		 * code. Remember that we also have the firewire async
684 		 * stream header even though that isn't accounted for
685 		 * in mode.stream.len.
686 		 */
687 		if (sxfer->resp != 0 || fp->mode.stream.len <
688 		    2*sizeof(uint32_t)) {
689 			m_freem(m);
690 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
691 			continue;
692 		}
693 		m->m_len = m->m_pkthdr.len = fp->mode.stream.len
694 			+ sizeof(fp->mode.stream);
695 
696 		/*
697 		 * If we received the packet on the broadcast channel,
698 		 * mark it as broadcast, otherwise we assume it must
699 		 * be multicast.
700 		 */
701 		if (fp->mode.stream.chtag == broadcast_channel)
702 			m->m_flags |= M_BCAST;
703 		else
704 			m->m_flags |= M_MCAST;
705 
706 		/*
707 		 * Make sure we recognise the GASP specifier and
708 		 * version.
709 		 */
710 		p = mtod(m, uint32_t *);
711 		if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) != CSRVAL_IETF
712 		    || (ntohl(p[2]) & 0xffffff) != 1) {
713 			FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n",
714 			    ntohl(p[1]), ntohl(p[2]));
715 			m_freem(m);
716 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
717 			continue;
718 		}
719 
720 		/*
721 		 * Record the sender ID for possible BPF usage.
722 		 */
723 		src = ntohl(p[1]) >> 16;
724 		if (bpf_peers_present_if(ifp)) {
725 			mtag = m_tag_alloc(MTAG_FIREWIRE,
726 			    MTAG_FIREWIRE_SENDER_EUID,
727 			    2*sizeof(uint32_t), M_NOWAIT);
728 			if (mtag) {
729 				/* bpf wants it in network byte order */
730 				struct fw_device *fd;
731 				uint32_t *p = (uint32_t *) (mtag + 1);
732 				fd = fw_noderesolve_nodeid(fwip->fd.fc,
733 				    src & FW_NODE_MASK);
734 				if (fd) {
735 					p[0] = htonl(fd->eui.hi);
736 					p[1] = htonl(fd->eui.lo);
737 				} else {
738 					p[0] = 0;
739 					p[1] = 0;
740 				}
741 				m_tag_prepend(m, mtag);
742 			}
743 		}
744 
745 		/*
746 		 * Trim off the GASP header
747 		 */
748 		m_adj(m, FW_GASP_HDR_LEN);
749 		m->m_pkthdr.rcvif = ifp;
750 		firewire_input(ifp, m, src);
751 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
752 	}
753 	NET_EPOCH_EXIT(et);
754 	if (STAILQ_FIRST(&xferq->stfree) != NULL)
755 		fwip->fd.fc->irx_enable(fwip->fd.fc, fwip->dma_ch);
756 }
757 
758 static __inline void
759 fwip_unicast_input_recycle(struct fwip_softc *fwip, struct fw_xfer *xfer)
760 {
761 	struct mbuf *m;
762 
763 	/*
764 	 * We have finished with a unicast xfer. Allocate a new
765 	 * cluster and stick it on the back of the input queue.
766 	 */
767 	m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
768 	xfer->mbuf = m;
769 	xfer->recv.payload = mtod(m, uint32_t *);
770 	xfer->recv.pay_len = MCLBYTES;
771 	xfer->mbuf = m;
772 	STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
773 }
774 
775 static void
776 fwip_unicast_input(struct fw_xfer *xfer)
777 {
778 	uint64_t address;
779 	struct mbuf *m;
780 	struct m_tag *mtag;
781 	struct epoch_tracker et;
782 	if_t ifp;
783 	struct fwip_softc *fwip;
784 	struct fw_pkt *fp;
785 	//struct fw_pkt *sfp;
786 	int rtcode;
787 
788 	fwip = (struct fwip_softc *)xfer->sc;
789 	ifp = fwip->fw_softc.fwip_ifp;
790 	m = xfer->mbuf;
791 	xfer->mbuf = 0;
792 	fp = &xfer->recv.hdr;
793 
794 	/*
795 	 * Check the fifo address - we only accept addresses of
796 	 * exactly INET_FIFO.
797 	 */
798 	address = ((uint64_t)fp->mode.wreqb.dest_hi << 32)
799 		| fp->mode.wreqb.dest_lo;
800 	if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
801 		rtcode = FWRCODE_ER_TYPE;
802 	} else if (address != INET_FIFO) {
803 		rtcode = FWRCODE_ER_ADDR;
804 	} else {
805 		rtcode = FWRCODE_COMPLETE;
806 	}
807 	NET_EPOCH_ENTER(et);
808 
809 	/*
810 	 * Pick up a new mbuf and stick it on the back of the receive
811 	 * queue.
812 	 */
813 	fwip_unicast_input_recycle(fwip, xfer);
814 
815 	/*
816 	 * If we've already rejected the packet, give up now.
817 	 */
818 	if (rtcode != FWRCODE_COMPLETE) {
819 		m_freem(m);
820 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
821 		goto done;
822 	}
823 
824 	if (bpf_peers_present_if(ifp)) {
825 		/*
826 		 * Record the sender ID for possible BPF usage.
827 		 */
828 		mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID,
829 		    2*sizeof(uint32_t), M_NOWAIT);
830 		if (mtag) {
831 			/* bpf wants it in network byte order */
832 			struct fw_device *fd;
833 			uint32_t *p = (uint32_t *) (mtag + 1);
834 			fd = fw_noderesolve_nodeid(fwip->fd.fc,
835 			    fp->mode.wreqb.src & 0x3f);
836 			if (fd) {
837 				p[0] = htonl(fd->eui.hi);
838 				p[1] = htonl(fd->eui.lo);
839 			} else {
840 				p[0] = 0;
841 				p[1] = 0;
842 			}
843 			m_tag_prepend(m, mtag);
844 		}
845 	}
846 
847 	/*
848 	 * Hand off to the generic encapsulation code. We don't use
849 	 * ifp->if_input so that we can pass the source nodeid as an
850 	 * argument to facilitate link-level fragment reassembly.
851 	 */
852 	m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len;
853 	m->m_pkthdr.rcvif = ifp;
854 	firewire_input(ifp, m, fp->mode.wreqb.src);
855 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
856 done:
857 	NET_EPOCH_EXIT(et);
858 }
859 
860 static device_method_t fwip_methods[] = {
861 	/* device interface */
862 	DEVMETHOD(device_identify,	fwip_identify),
863 	DEVMETHOD(device_probe,		fwip_probe),
864 	DEVMETHOD(device_attach,	fwip_attach),
865 	DEVMETHOD(device_detach,	fwip_detach),
866 	DEVMETHOD_END
867 };
868 
869 static driver_t fwip_driver = {
870         "fwip",
871 	fwip_methods,
872 	sizeof(struct fwip_softc),
873 };
874 
875 
876 DRIVER_MODULE(fwip, firewire, fwip_driver, 0, 0);
877 MODULE_VERSION(fwip, 1);
878 MODULE_DEPEND(fwip, firewire, 1, 1, 1);
879