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