xref: /freebsd/sys/dev/firewire/if_fwe.c (revision 3642298923e528d795e3a30ec165d2b469e28b40)
1 /*-
2  * Copyright (c) 2002-2003
3  * 	Hidetoshi Shimokawa. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *
16  *	This product includes software developed by Hidetoshi Shimokawa.
17  *
18  * 4. Neither the name of the author nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $FreeBSD$
35  */
36 
37 #include "opt_inet.h"
38 
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47 #include <sys/module.h>
48 #include <sys/bus.h>
49 #include <machine/bus.h>
50 
51 #include <net/bpf.h>
52 #include <net/ethernet.h>
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/if_types.h>
56 #ifdef __DragonFly__
57 #include <net/vlan/if_vlan_var.h>
58 #include <bus/firewire/firewire.h>
59 #include <bus/firewire/firewirereg.h>
60 #include "if_fwevar.h"
61 #else
62 #include <net/if_vlan_var.h>
63 
64 #include <dev/firewire/firewire.h>
65 #include <dev/firewire/firewirereg.h>
66 #include <dev/firewire/if_fwevar.h>
67 #endif
68 
69 #define FWEDEBUG	if (fwedebug) if_printf
70 #define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
71 
72 /* network interface */
73 static void fwe_start (struct ifnet *);
74 static int fwe_ioctl (struct ifnet *, u_long, caddr_t);
75 static void fwe_init (void *);
76 
77 static void fwe_output_callback (struct fw_xfer *);
78 static void fwe_as_output (struct fwe_softc *, struct ifnet *);
79 static void fwe_as_input (struct fw_xferq *);
80 
81 static int fwedebug = 0;
82 static int stream_ch = 1;
83 static int tx_speed = 2;
84 static int rx_queue_len = FWMAXQUEUE;
85 
86 MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
87 SYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
88 SYSCTL_DECL(_hw_firewire);
89 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
90 	"Ethernet emulation subsystem");
91 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
92 	"Stream channel to use");
93 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RW, &tx_speed, 0,
94 	"Transmission speed");
95 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
96 	0, "Length of the receive queue");
97 
98 TUNABLE_INT("hw.firewire.fwe.stream_ch", &stream_ch);
99 TUNABLE_INT("hw.firewire.fwe.tx_speed", &tx_speed);
100 TUNABLE_INT("hw.firewire.fwe.rx_queue_len", &rx_queue_len);
101 
102 #ifdef DEVICE_POLLING
103 #define FWE_POLL_REGISTER(func, fwe, ifp)			\
104 	if (ether_poll_register(func, ifp)) {			\
105 		struct firewire_comm *fc = (fwe)->fd.fc;	\
106 		fc->set_intr(fc, 0);				\
107 	}
108 
109 #define FWE_POLL_DEREGISTER(fwe, ifp)				\
110 	do {							\
111 		struct firewire_comm *fc = (fwe)->fd.fc;	\
112 		ether_poll_deregister(ifp);			\
113 		fc->set_intr(fc, 1);				\
114 	} while(0)						\
115 
116 static poll_handler_t fwe_poll;
117 
118 static void
119 fwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
120 {
121 	struct fwe_softc *fwe;
122 	struct firewire_comm *fc;
123 
124 	fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
125 	fc = fwe->fd.fc;
126 	if (cmd == POLL_DEREGISTER) {
127 		/* enable interrupts */
128 		fc->set_intr(fc, 1);
129 		return;
130 	}
131 	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
132 }
133 #else
134 #define FWE_POLL_REGISTER(func, fwe, ifp)
135 #define FWE_POLL_DEREGISTER(fwe, ifp)
136 #endif
137 static void
138 fwe_identify(driver_t *driver, device_t parent)
139 {
140 	BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent));
141 }
142 
143 static int
144 fwe_probe(device_t dev)
145 {
146 	device_t pa;
147 
148 	pa = device_get_parent(dev);
149 	if(device_get_unit(dev) != device_get_unit(pa)){
150 		return(ENXIO);
151 	}
152 
153 	device_set_desc(dev, "Ethernet over FireWire");
154 	return (0);
155 }
156 
157 static int
158 fwe_attach(device_t dev)
159 {
160 	struct fwe_softc *fwe;
161 	struct ifnet *ifp;
162 	int unit, s;
163 #if defined(__DragonFly__) || __FreeBSD_version < 500000
164 	u_char *eaddr;
165 #else
166 	u_char eaddr[6];
167 #endif
168 	struct fw_eui64 *eui;
169 
170 	fwe = ((struct fwe_softc *)device_get_softc(dev));
171 	unit = device_get_unit(dev);
172 
173 	bzero(fwe, sizeof(struct fwe_softc));
174 	/* XXX */
175 	fwe->stream_ch = stream_ch;
176 	fwe->dma_ch = -1;
177 
178 	fwe->fd.fc = device_get_ivars(dev);
179 	if (tx_speed < 0)
180 		tx_speed = fwe->fd.fc->speed;
181 
182 	fwe->fd.dev = dev;
183 	fwe->fd.post_explore = NULL;
184 	fwe->eth_softc.fwe = fwe;
185 
186 	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
187 	fwe->pkt_hdr.mode.stream.sy = 0;
188 	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
189 
190 	/* generate fake MAC address: first and last 3bytes from eui64 */
191 #define LOCAL (0x02)
192 #define GROUP (0x01)
193 #if defined(__DragonFly__) || __FreeBSD_version < 500000
194 	eaddr = &IFP2ENADDR(fwe->eth_softc.ifp)[0];
195 #endif
196 
197 
198 	eui = &fwe->fd.fc->eui;
199 	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
200 	eaddr[1] = FW_EUI64_BYTE(eui, 1);
201 	eaddr[2] = FW_EUI64_BYTE(eui, 2);
202 	eaddr[3] = FW_EUI64_BYTE(eui, 5);
203 	eaddr[4] = FW_EUI64_BYTE(eui, 6);
204 	eaddr[5] = FW_EUI64_BYTE(eui, 7);
205 	printf("if_fwe%d: Fake Ethernet address: "
206 		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
207 		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
208 
209 	/* fill the rest and attach interface */
210 	ifp = fwe->eth_softc.ifp = if_alloc(IFT_ETHER);
211 	if (ifp == NULL) {
212 		device_printf(dev, "can not if_alloc()\n");
213 		return (ENOSPC);
214 	}
215 	ifp->if_softc = &fwe->eth_softc;
216 
217 #if __FreeBSD_version >= 501113 || defined(__DragonFly__)
218 	if_initname(ifp, device_get_name(dev), unit);
219 #else
220 	ifp->if_unit = unit;
221 	ifp->if_name = "fwe";
222 #endif
223 	ifp->if_init = fwe_init;
224 #if defined(__DragonFly__) || __FreeBSD_version < 500000
225 	ifp->if_output = ether_output;
226 #endif
227 	ifp->if_start = fwe_start;
228 	ifp->if_ioctl = fwe_ioctl;
229 	ifp->if_mtu = ETHERMTU;
230 	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|
231 	    IFF_NEEDSGIANT);
232 	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
233 
234 	s = splimp();
235 #if defined(__DragonFly__) || __FreeBSD_version < 500000
236 	ether_ifattach(ifp, 1);
237 #else
238 	ether_ifattach(ifp, eaddr);
239 #endif
240 	splx(s);
241 
242         /* Tell the upper layer(s) we support long frames. */
243 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
244 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
245 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
246 	ifp->if_capenable |= IFCAP_VLAN_MTU;
247 #endif
248 
249 
250 	FWEDEBUG(ifp, "interface created\n");
251 	return 0;
252 }
253 
254 static void
255 fwe_stop(struct fwe_softc *fwe)
256 {
257 	struct firewire_comm *fc;
258 	struct fw_xferq *xferq;
259 	struct ifnet *ifp = fwe->eth_softc.ifp;
260 	struct fw_xfer *xfer, *next;
261 	int i;
262 
263 	fc = fwe->fd.fc;
264 
265 	FWE_POLL_DEREGISTER(fwe, ifp);
266 
267 	if (fwe->dma_ch >= 0) {
268 		xferq = fc->ir[fwe->dma_ch];
269 
270 		if (xferq->flag & FWXFERQ_RUNNING)
271 			fc->irx_disable(fc, fwe->dma_ch);
272 		xferq->flag &=
273 			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
274 			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
275 		xferq->hand =  NULL;
276 
277 		for (i = 0; i < xferq->bnchunk; i ++)
278 			m_freem(xferq->bulkxfer[i].mbuf);
279 		free(xferq->bulkxfer, M_FWE);
280 
281 		for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
282 					xfer = next) {
283 			next = STAILQ_NEXT(xfer, link);
284 			fw_xfer_free(xfer);
285 		}
286 		STAILQ_INIT(&fwe->xferlist);
287 
288 		xferq->bulkxfer =  NULL;
289 		fwe->dma_ch = -1;
290 	}
291 
292 #if defined(__FreeBSD__)
293 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
294 #else
295 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
296 #endif
297 }
298 
299 static int
300 fwe_detach(device_t dev)
301 {
302 	struct fwe_softc *fwe;
303 	struct ifnet *ifp;
304 	int s;
305 
306 	fwe = device_get_softc(dev);
307 	ifp = fwe->eth_softc.ifp;
308 	s = splimp();
309 
310 	fwe_stop(fwe);
311 #if defined(__DragonFly__) || __FreeBSD_version < 500000
312 	ether_ifdetach(ifp, 1);
313 #else
314 	ether_ifdetach(ifp);
315 	if_free(ifp);
316 #endif
317 
318 	splx(s);
319 	return 0;
320 }
321 
322 static void
323 fwe_init(void *arg)
324 {
325 	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
326 	struct firewire_comm *fc;
327 	struct ifnet *ifp = fwe->eth_softc.ifp;
328 	struct fw_xferq *xferq;
329 	struct fw_xfer *xfer;
330 	struct mbuf *m;
331 	int i;
332 
333 	FWEDEBUG(ifp, "initializing\n");
334 
335 	/* XXX keep promiscoud mode */
336 	ifp->if_flags |= IFF_PROMISC;
337 
338 	fc = fwe->fd.fc;
339 #define START 0
340 	if (fwe->dma_ch < 0) {
341 		for (i = START; i < fc->nisodma; i ++) {
342 			xferq = fc->ir[i];
343 			if ((xferq->flag & FWXFERQ_OPEN) == 0)
344 				goto found;
345 		}
346 		printf("no free dma channel\n");
347 		return;
348 found:
349 		fwe->dma_ch = i;
350 		fwe->stream_ch = stream_ch;
351 		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
352 		/* allocate DMA channel and init packet mode */
353 		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
354 				FWXFERQ_HANDLER | FWXFERQ_STREAM;
355 		xferq->flag &= ~0xff;
356 		xferq->flag |= fwe->stream_ch & 0xff;
357 		/* register fwe_input handler */
358 		xferq->sc = (caddr_t) fwe;
359 		xferq->hand = fwe_as_input;
360 		xferq->bnchunk = rx_queue_len;
361 		xferq->bnpacket = 1;
362 		xferq->psize = MCLBYTES;
363 		xferq->queued = 0;
364 		xferq->buf = NULL;
365 		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
366 			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
367 							M_FWE, M_WAITOK);
368 		if (xferq->bulkxfer == NULL) {
369 			printf("if_fwe: malloc failed\n");
370 			return;
371 		}
372 		STAILQ_INIT(&xferq->stvalid);
373 		STAILQ_INIT(&xferq->stfree);
374 		STAILQ_INIT(&xferq->stdma);
375 		xferq->stproc = NULL;
376 		for (i = 0; i < xferq->bnchunk; i ++) {
377 			m =
378 #if defined(__DragonFly__) || __FreeBSD_version < 500000
379 				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
380 #else
381 				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
382 #endif
383 			xferq->bulkxfer[i].mbuf = m;
384 			if (m != NULL) {
385 				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
386 				STAILQ_INSERT_TAIL(&xferq->stfree,
387 						&xferq->bulkxfer[i], link);
388 			} else
389 				printf("fwe_as_input: m_getcl failed\n");
390 		}
391 		STAILQ_INIT(&fwe->xferlist);
392 		for (i = 0; i < TX_MAX_QUEUE; i++) {
393 			xfer = fw_xfer_alloc(M_FWE);
394 			if (xfer == NULL)
395 				break;
396 			xfer->send.spd = tx_speed;
397 			xfer->fc = fwe->fd.fc;
398 			xfer->retry_req = fw_asybusy;
399 			xfer->sc = (caddr_t)fwe;
400 			xfer->act.hand = fwe_output_callback;
401 			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
402 		}
403 	} else
404 		xferq = fc->ir[fwe->dma_ch];
405 
406 
407 	/* start dma */
408 	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
409 		fc->irx_enable(fc, fwe->dma_ch);
410 
411 #if defined(__FreeBSD__)
412 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
413 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
414 #else
415 	ifp->if_flags |= IFF_RUNNING;
416 	ifp->if_flags &= ~IFF_OACTIVE;
417 #endif
418 
419 	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
420 #if 0
421 	/* attempt to start output */
422 	fwe_start(ifp);
423 #endif
424 }
425 
426 
427 static int
428 fwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
429 {
430 	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
431 	struct ifstat *ifs = NULL;
432 	int s, error, len;
433 
434 	switch (cmd) {
435 		case SIOCSIFFLAGS:
436 			s = splimp();
437 			if (ifp->if_flags & IFF_UP) {
438 #if defined(__FreeBSD__)
439 				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
440 #else
441 				if (!(ifp->if_flags & IFF_RUNNING))
442 #endif
443 					fwe_init(&fwe->eth_softc);
444 			} else {
445 #if defined(__FreeBSD__)
446 				if (ifp->if_drv_flags & IFF_DRV_RUNNING)
447 #else
448 				if (ifp->if_flags & IFF_RUNNING)
449 #endif
450 					fwe_stop(fwe);
451 			}
452 			/* XXX keep promiscoud mode */
453 			ifp->if_flags |= IFF_PROMISC;
454 			splx(s);
455 			break;
456 		case SIOCADDMULTI:
457 		case SIOCDELMULTI:
458 			break;
459 
460 		case SIOCGIFSTATUS:
461 			s = splimp();
462 			ifs = (struct ifstat *)data;
463 			len = strlen(ifs->ascii);
464 			if (len < sizeof(ifs->ascii))
465 				snprintf(ifs->ascii + len,
466 					sizeof(ifs->ascii) - len,
467 					"\tch %d dma %d\n",
468 						fwe->stream_ch, fwe->dma_ch);
469 			splx(s);
470 			break;
471 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
472 		default:
473 #else
474 		case SIOCSIFADDR:
475 		case SIOCGIFADDR:
476 		case SIOCSIFMTU:
477 #endif
478 			s = splimp();
479 			error = ether_ioctl(ifp, cmd, data);
480 			splx(s);
481 			return (error);
482 #if defined(__DragonFly__) || __FreeBSD_version < 500000
483 		default:
484 			return (EINVAL);
485 #endif
486 	}
487 
488 	return (0);
489 }
490 
491 static void
492 fwe_output_callback(struct fw_xfer *xfer)
493 {
494 	struct fwe_softc *fwe;
495 	struct ifnet *ifp;
496 	int s;
497 
498 	fwe = (struct fwe_softc *)xfer->sc;
499 	ifp = fwe->eth_softc.ifp;
500 	/* XXX error check */
501 	FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
502 	if (xfer->resp != 0)
503 		ifp->if_oerrors ++;
504 
505 	m_freem(xfer->mbuf);
506 	fw_xfer_unload(xfer);
507 
508 	s = splimp();
509 	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
510 	splx(s);
511 
512 	/* for queue full */
513 	if (ifp->if_snd.ifq_head != NULL)
514 		fwe_start(ifp);
515 }
516 
517 static void
518 fwe_start(struct ifnet *ifp)
519 {
520 	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
521 	int s;
522 
523 	GIANT_REQUIRED;
524 
525 	FWEDEBUG(ifp, "starting\n");
526 
527 	if (fwe->dma_ch < 0) {
528 		struct mbuf	*m = NULL;
529 
530 		FWEDEBUG(ifp, "not ready\n");
531 
532 		s = splimp();
533 		do {
534 			IF_DEQUEUE(&ifp->if_snd, m);
535 			if (m != NULL)
536 				m_freem(m);
537 			ifp->if_oerrors ++;
538 		} while (m != NULL);
539 		splx(s);
540 
541 		return;
542 	}
543 
544 	s = splimp();
545 #if defined(__FreeBSD__)
546 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
547 #else
548 	ifp->if_flags |= IFF_OACTIVE;
549 #endif
550 
551 	if (ifp->if_snd.ifq_len != 0)
552 		fwe_as_output(fwe, ifp);
553 
554 #if defined(__FreeBSD__)
555 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
556 #else
557 	ifp->if_flags &= ~IFF_OACTIVE;
558 #endif
559 	splx(s);
560 }
561 
562 #define HDR_LEN 4
563 #ifndef ETHER_ALIGN
564 #define ETHER_ALIGN 2
565 #endif
566 /* Async. stream output */
567 static void
568 fwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
569 {
570 	struct mbuf *m;
571 	struct fw_xfer *xfer;
572 	struct fw_xferq *xferq;
573 	struct fw_pkt *fp;
574 	int i = 0;
575 
576 	xfer = NULL;
577 	xferq = fwe->fd.fc->atq;
578 	while (xferq->queued < xferq->maxq - 1) {
579 		xfer = STAILQ_FIRST(&fwe->xferlist);
580 		if (xfer == NULL) {
581 			printf("if_fwe: lack of xfer\n");
582 			return;
583 		}
584 		IF_DEQUEUE(&ifp->if_snd, m);
585 		if (m == NULL)
586 			break;
587 		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
588 #if defined(__DragonFly__) || __FreeBSD_version < 500000
589 		if (ifp->if_bpf != NULL)
590 			bpf_mtap(ifp, m);
591 #else
592 		BPF_MTAP(ifp, m);
593 #endif
594 
595 		/* keep ip packet alignment for alpha */
596 		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
597 		fp = &xfer->send.hdr;
598 		*(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
599 		fp->mode.stream.len = m->m_pkthdr.len;
600 		xfer->mbuf = m;
601 		xfer->send.pay_len = m->m_pkthdr.len;
602 
603 		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
604 			/* error */
605 			ifp->if_oerrors ++;
606 			/* XXX set error code */
607 			fwe_output_callback(xfer);
608 		} else {
609 			ifp->if_opackets ++;
610 			i++;
611 		}
612 	}
613 #if 0
614 	if (i > 1)
615 		printf("%d queued\n", i);
616 #endif
617 	if (i > 0)
618 		xferq->start(fwe->fd.fc);
619 }
620 
621 /* Async. stream output */
622 static void
623 fwe_as_input(struct fw_xferq *xferq)
624 {
625 	struct mbuf *m, *m0;
626 	struct ifnet *ifp;
627 	struct fwe_softc *fwe;
628 	struct fw_bulkxfer *sxfer;
629 	struct fw_pkt *fp;
630 	u_char *c;
631 #if defined(__DragonFly__) || __FreeBSD_version < 500000
632 	struct ether_header *eh;
633 #endif
634 
635 	fwe = (struct fwe_softc *)xferq->sc;
636 	ifp = fwe->eth_softc.ifp;
637 #if 0
638 	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
639 #endif
640 	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
641 		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
642 		fp = mtod(sxfer->mbuf, struct fw_pkt *);
643 		if (fwe->fd.fc->irx_post != NULL)
644 			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
645 		m = sxfer->mbuf;
646 
647 		/* insert new rbuf */
648 		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
649 		if (m0 != NULL) {
650 			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
651 			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
652 		} else
653 			printf("fwe_as_input: m_getcl failed\n");
654 
655 		if (sxfer->resp != 0 || fp->mode.stream.len <
656 		    ETHER_ALIGN + sizeof(struct ether_header)) {
657 			m_freem(m);
658 			ifp->if_ierrors ++;
659 			continue;
660 		}
661 
662 		m->m_data += HDR_LEN + ETHER_ALIGN;
663 		c = mtod(m, char *);
664 #if defined(__DragonFly__) || __FreeBSD_version < 500000
665 		eh = (struct ether_header *)c;
666 		m->m_data += sizeof(struct ether_header);
667 		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN
668 		    - sizeof(struct ether_header);
669 #else
670 		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN;
671 #endif
672 		m->m_pkthdr.rcvif = ifp;
673 #if 0
674 		FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n"
675 			 "%02x %02x %02x %02x %02x %02x\n"
676 			 "%02x %02x %02x %02x\n"
677 			 "%02x %02x %02x %02x\n"
678 			 "%02x %02x %02x %02x\n"
679 			 "%02x %02x %02x %02x\n",
680 			 c[0], c[1], c[2], c[3], c[4], c[5],
681 			 c[6], c[7], c[8], c[9], c[10], c[11],
682 			 c[12], c[13], c[14], c[15],
683 			 c[16], c[17], c[18], c[19],
684 			 c[20], c[21], c[22], c[23],
685 			 c[20], c[21], c[22], c[23]
686 		 );
687 #endif
688 #if defined(__DragonFly__) || __FreeBSD_version < 500000
689 		ether_input(ifp, eh, m);
690 #else
691 		(*ifp->if_input)(ifp, m);
692 #endif
693 		ifp->if_ipackets ++;
694 	}
695 	if (STAILQ_FIRST(&xferq->stfree) != NULL)
696 		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
697 }
698 
699 
700 static devclass_t fwe_devclass;
701 
702 static device_method_t fwe_methods[] = {
703 	/* device interface */
704 	DEVMETHOD(device_identify,	fwe_identify),
705 	DEVMETHOD(device_probe,		fwe_probe),
706 	DEVMETHOD(device_attach,	fwe_attach),
707 	DEVMETHOD(device_detach,	fwe_detach),
708 	{ 0, 0 }
709 };
710 
711 static driver_t fwe_driver = {
712         "fwe",
713 	fwe_methods,
714 	sizeof(struct fwe_softc),
715 };
716 
717 
718 #ifdef __DragonFly__
719 DECLARE_DUMMY_MODULE(fwe);
720 #endif
721 DRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
722 MODULE_VERSION(fwe, 1);
723 MODULE_DEPEND(fwe, firewire, 1, 1, 1);
724