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