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