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