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