]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/firewire/if_fwe.c
Sync to HEAD@r271160.
[FreeBSD/FreeBSD.git] / sys / dev / firewire / if_fwe.c
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 #ifdef HAVE_KERNEL_OPTION_HEADERS
38 #include "opt_device_polling.h"
39 #include "opt_inet.h"
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.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/ethernet.h>
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_arp.h>
59 #include <net/if_types.h>
60 #ifdef __DragonFly__
61 #include <net/vlan/if_vlan_var.h>
62 #include <bus/firewire/firewire.h>
63 #include <bus/firewire/firewirereg.h>
64 #include "if_fwevar.h"
65 #else
66 #include <net/if_vlan_var.h>
67
68 #include <dev/firewire/firewire.h>
69 #include <dev/firewire/firewirereg.h>
70 #include <dev/firewire/if_fwevar.h>
71 #endif
72
73 #define FWEDEBUG        if (fwedebug) if_printf
74 #define TX_MAX_QUEUE    (FWMAXQUEUE - 1)
75
76 /* network interface */
77 static void fwe_start (struct ifnet *);
78 static int fwe_ioctl (struct ifnet *, u_long, caddr_t);
79 static void fwe_init (void *);
80
81 static void fwe_output_callback (struct fw_xfer *);
82 static void fwe_as_output (struct fwe_softc *, struct ifnet *);
83 static void fwe_as_input (struct fw_xferq *);
84
85 static int fwedebug = 0;
86 static int stream_ch = 1;
87 static int tx_speed = 2;
88 static int rx_queue_len = FWMAXQUEUE;
89
90 static MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
91 SYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RWTUN, &fwedebug, 0, "");
92 SYSCTL_DECL(_hw_firewire);
93 static SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
94         "Ethernet emulation subsystem");
95 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RWTUN, &stream_ch, 0,
96         "Stream channel to use");
97 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RWTUN, &tx_speed, 0,
98         "Transmission speed");
99 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RWTUN, &rx_queue_len,
100         0, "Length of the receive queue");
101
102 #ifdef DEVICE_POLLING
103 static poll_handler_t fwe_poll;
104
105 static int
106 fwe_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 (0);
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         return (0);
118 }
119 #endif /* DEVICE_POLLING */
120
121 static void
122 fwe_identify(driver_t *driver, device_t parent)
123 {
124         BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent));
125 }
126
127 static int
128 fwe_probe(device_t dev)
129 {
130         device_t pa;
131
132         pa = device_get_parent(dev);
133         if(device_get_unit(dev) != device_get_unit(pa)){
134                 return(ENXIO);
135         }
136
137         device_set_desc(dev, "Ethernet over FireWire");
138         return (0);
139 }
140
141 static int
142 fwe_attach(device_t dev)
143 {
144         struct fwe_softc *fwe;
145         struct ifnet *ifp;
146         int unit, s;
147 #if defined(__DragonFly__) || __FreeBSD_version < 500000
148         u_char *eaddr;
149 #else
150         u_char eaddr[6];
151 #endif
152         struct fw_eui64 *eui;
153
154         fwe = ((struct fwe_softc *)device_get_softc(dev));
155         unit = device_get_unit(dev);
156
157         bzero(fwe, sizeof(struct fwe_softc));
158         mtx_init(&fwe->mtx, "fwe", NULL, MTX_DEF);
159         /* XXX */
160         fwe->stream_ch = stream_ch;
161         fwe->dma_ch = -1;
162
163         fwe->fd.fc = device_get_ivars(dev);
164         if (tx_speed < 0)
165                 tx_speed = fwe->fd.fc->speed;
166
167         fwe->fd.dev = dev;
168         fwe->fd.post_explore = NULL;
169         fwe->eth_softc.fwe = fwe;
170
171         fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
172         fwe->pkt_hdr.mode.stream.sy = 0;
173         fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
174
175         /* generate fake MAC address: first and last 3bytes from eui64 */
176 #define LOCAL (0x02)
177 #define GROUP (0x01)
178 #if defined(__DragonFly__) || __FreeBSD_version < 500000
179         eaddr = &IFP2ENADDR(fwe->eth_softc.ifp)[0];
180 #endif
181
182
183         eui = &fwe->fd.fc->eui;
184         eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
185         eaddr[1] = FW_EUI64_BYTE(eui, 1);
186         eaddr[2] = FW_EUI64_BYTE(eui, 2);
187         eaddr[3] = FW_EUI64_BYTE(eui, 5);
188         eaddr[4] = FW_EUI64_BYTE(eui, 6);
189         eaddr[5] = FW_EUI64_BYTE(eui, 7);
190         printf("if_fwe%d: Fake Ethernet address: "
191                 "%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
192                 eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
193
194         /* fill the rest and attach interface */        
195         ifp = fwe->eth_softc.ifp = if_alloc(IFT_ETHER);
196         if (ifp == NULL) {
197                 device_printf(dev, "can not if_alloc()\n");
198                 return (ENOSPC);
199         }
200         ifp->if_softc = &fwe->eth_softc;
201
202 #if __FreeBSD_version >= 501113 || defined(__DragonFly__)
203         if_initname(ifp, device_get_name(dev), unit);
204 #else
205         ifp->if_unit = unit;
206         ifp->if_name = "fwe";
207 #endif
208         ifp->if_init = fwe_init;
209 #if defined(__DragonFly__) || __FreeBSD_version < 500000
210         ifp->if_output = ether_output;
211 #endif
212         ifp->if_start = fwe_start;
213         ifp->if_ioctl = fwe_ioctl;
214         ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
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_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
237 static void
238 fwe_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
280 static int
281 fwe_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         mtx_destroy(&fwe->mtx);
306         return 0;
307 }
308
309 static void
310 fwe_init(void *arg)
311 {
312         struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
313         struct firewire_comm *fc;
314         struct ifnet *ifp = fwe->eth_softc.ifp;
315         struct fw_xferq *xferq;
316         struct fw_xfer *xfer;
317         struct mbuf *m;
318         int i;
319
320         FWEDEBUG(ifp, "initializing\n");
321
322         /* XXX keep promiscoud mode */
323         ifp->if_flags |= IFF_PROMISC;
324
325         fc = fwe->fd.fc;
326         if (fwe->dma_ch < 0) {
327                 fwe->dma_ch = fw_open_isodma(fc, /* tx */0);
328                 if (fwe->dma_ch < 0)
329                         return;
330                 xferq = fc->ir[fwe->dma_ch];
331                 xferq->flag |= FWXFERQ_EXTBUF |
332                                 FWXFERQ_HANDLER | FWXFERQ_STREAM;
333                 fwe->stream_ch = stream_ch;
334                 fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
335                 xferq->flag &= ~0xff;
336                 xferq->flag |= fwe->stream_ch & 0xff;
337                 /* register fwe_input handler */
338                 xferq->sc = (caddr_t) fwe;
339                 xferq->hand = fwe_as_input;
340                 xferq->bnchunk = rx_queue_len;
341                 xferq->bnpacket = 1;
342                 xferq->psize = MCLBYTES;
343                 xferq->queued = 0;
344                 xferq->buf = NULL;
345                 xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
346                         sizeof(struct fw_bulkxfer) * xferq->bnchunk,
347                                                         M_FWE, M_WAITOK);
348                 if (xferq->bulkxfer == NULL) {
349                         printf("if_fwe: malloc failed\n");
350                         return;
351                 }
352                 STAILQ_INIT(&xferq->stvalid);
353                 STAILQ_INIT(&xferq->stfree);
354                 STAILQ_INIT(&xferq->stdma);
355                 xferq->stproc = NULL;
356                 for (i = 0; i < xferq->bnchunk; i ++) {
357                         m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
358                         xferq->bulkxfer[i].mbuf = m;
359                         m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
360                         STAILQ_INSERT_TAIL(&xferq->stfree,
361                                         &xferq->bulkxfer[i], link);
362                 }
363                 STAILQ_INIT(&fwe->xferlist);
364                 for (i = 0; i < TX_MAX_QUEUE; i++) {
365                         xfer = fw_xfer_alloc(M_FWE);
366                         if (xfer == NULL)
367                                 break;
368                         xfer->send.spd = tx_speed;
369                         xfer->fc = fwe->fd.fc;
370                         xfer->sc = (caddr_t)fwe;
371                         xfer->hand = fwe_output_callback;
372                         STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
373                 }
374         } else
375                 xferq = fc->ir[fwe->dma_ch];
376
377
378         /* start dma */
379         if ((xferq->flag & FWXFERQ_RUNNING) == 0)
380                 fc->irx_enable(fc, fwe->dma_ch);
381
382 #if defined(__FreeBSD__)
383         ifp->if_drv_flags |= IFF_DRV_RUNNING;
384         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
385 #else
386         ifp->if_flags |= IFF_RUNNING;
387         ifp->if_flags &= ~IFF_OACTIVE;
388 #endif
389
390 #if 0
391         /* attempt to start output */
392         fwe_start(ifp);
393 #endif
394 }
395
396
397 static int
398 fwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
399 {
400         struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
401         struct ifstat *ifs = NULL;
402         int s, error;
403
404         switch (cmd) {
405                 case SIOCSIFFLAGS:
406                         s = splimp();
407                         if (ifp->if_flags & IFF_UP) {
408 #if defined(__FreeBSD__)
409                                 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
410 #else
411                                 if (!(ifp->if_flags & IFF_RUNNING))
412 #endif
413                                         fwe_init(&fwe->eth_softc);
414                         } else {
415 #if defined(__FreeBSD__)
416                                 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
417 #else
418                                 if (ifp->if_flags & IFF_RUNNING)
419 #endif
420                                         fwe_stop(fwe);
421                         }
422                         /* XXX keep promiscoud mode */
423                         ifp->if_flags |= IFF_PROMISC;
424                         splx(s);
425                         break;
426                 case SIOCADDMULTI:
427                 case SIOCDELMULTI:
428                         break;
429
430                 case SIOCGIFSTATUS:
431                         s = splimp();
432                         ifs = (struct ifstat *)data;
433                         snprintf(ifs->ascii, sizeof(ifs->ascii),
434                             "\tch %d dma %d\n", fwe->stream_ch, fwe->dma_ch);
435                         splx(s);
436                         break;
437                 case SIOCSIFCAP:
438 #ifdef DEVICE_POLLING
439                     {
440                         struct ifreq *ifr = (struct ifreq *) data;
441                         struct firewire_comm *fc = fwe->fd.fc;
442
443                         if (ifr->ifr_reqcap & IFCAP_POLLING &&
444                             !(ifp->if_capenable & IFCAP_POLLING)) {
445                                 error = ether_poll_register(fwe_poll, ifp);
446                                 if (error)
447                                         return(error);
448                                 /* Disable interrupts */
449                                 fc->set_intr(fc, 0);
450                                 ifp->if_capenable |= IFCAP_POLLING;
451                                 ifp->if_capenable |= IFCAP_POLLING_NOCOUNT;
452                                 return (error);
453                         }
454                         if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
455                             ifp->if_capenable & IFCAP_POLLING) {
456                                 error = ether_poll_deregister(ifp);
457                                 /* Enable interrupts. */
458                                 fc->set_intr(fc, 1);
459                                 ifp->if_capenable &= ~IFCAP_POLLING;
460                                 ifp->if_capenable &= ~IFCAP_POLLING_NOCOUNT;
461                                 return (error);
462                         }
463                     }
464 #endif /* DEVICE_POLLING */
465                         break;
466 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
467                 default:
468 #else
469                 case SIOCSIFADDR:
470                 case SIOCGIFADDR:
471                 case SIOCSIFMTU:
472 #endif
473                         s = splimp();
474                         error = ether_ioctl(ifp, cmd, data);
475                         splx(s);
476                         return (error);
477 #if defined(__DragonFly__) || __FreeBSD_version < 500000
478                 default:
479                         return (EINVAL);
480 #endif
481         }
482
483         return (0);
484 }
485
486 static void
487 fwe_output_callback(struct fw_xfer *xfer)
488 {
489         struct fwe_softc *fwe;
490         struct ifnet *ifp;
491         int s;
492
493         fwe = (struct fwe_softc *)xfer->sc;
494         ifp = fwe->eth_softc.ifp;
495         /* XXX error check */
496         FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
497         if (xfer->resp != 0)
498                 ifp->if_oerrors ++;
499                 
500         m_freem(xfer->mbuf);
501         fw_xfer_unload(xfer);
502
503         s = splimp();
504         FWE_LOCK(fwe);
505         STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
506         FWE_UNLOCK(fwe);
507         splx(s);
508
509         /* for queue full */
510         if (ifp->if_snd.ifq_head != NULL)
511                 fwe_start(ifp);
512 }
513
514 static void
515 fwe_start(struct ifnet *ifp)
516 {
517         struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
518         int s;
519
520         FWEDEBUG(ifp, "starting\n");
521
522         if (fwe->dma_ch < 0) {
523                 struct mbuf     *m = NULL;
524
525                 FWEDEBUG(ifp, "not ready\n");
526
527                 s = splimp();
528                 do {
529                         IF_DEQUEUE(&ifp->if_snd, m);
530                         if (m != NULL)
531                                 m_freem(m);
532                         ifp->if_oerrors ++;
533                 } while (m != NULL);
534                 splx(s);
535
536                 return;
537         }
538
539         s = splimp();
540 #if defined(__FreeBSD__)
541         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
542 #else
543         ifp->if_flags |= IFF_OACTIVE;
544 #endif
545
546         if (ifp->if_snd.ifq_len != 0)
547                 fwe_as_output(fwe, ifp);
548
549 #if defined(__FreeBSD__)
550         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
551 #else
552         ifp->if_flags &= ~IFF_OACTIVE;
553 #endif
554         splx(s);
555 }
556
557 #define HDR_LEN 4
558 #ifndef ETHER_ALIGN
559 #define ETHER_ALIGN 2
560 #endif
561 /* Async. stream output */
562 static void
563 fwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
564 {
565         struct mbuf *m;
566         struct fw_xfer *xfer;
567         struct fw_xferq *xferq;
568         struct fw_pkt *fp;
569         int i = 0;
570
571         xfer = NULL;
572         xferq = fwe->fd.fc->atq;
573         while ((xferq->queued < xferq->maxq - 1) &&
574                         (ifp->if_snd.ifq_head != NULL)) {
575                 FWE_LOCK(fwe);
576                 xfer = STAILQ_FIRST(&fwe->xferlist);
577                 if (xfer == NULL) {
578 #if 0
579                         printf("if_fwe: lack of xfer\n");
580 #endif
581                         FWE_UNLOCK(fwe);
582                         break;
583                 }
584                 STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
585                 FWE_UNLOCK(fwe);
586
587                 IF_DEQUEUE(&ifp->if_snd, m);
588                 if (m == NULL) {
589                         FWE_LOCK(fwe);
590                         STAILQ_INSERT_HEAD(&fwe->xferlist, xfer, link);
591                         FWE_UNLOCK(fwe);
592                         break;
593                 }
594 #if defined(__DragonFly__) || __FreeBSD_version < 500000
595                 if (ifp->if_bpf != NULL)
596                         bpf_mtap(ifp, m);
597 #else
598                 BPF_MTAP(ifp, m);
599 #endif
600
601                 /* keep ip packet alignment for alpha */
602                 M_PREPEND(m, ETHER_ALIGN, M_NOWAIT);
603                 fp = &xfer->send.hdr;
604                 *(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
605                 fp->mode.stream.len = m->m_pkthdr.len;
606                 xfer->mbuf = m;
607                 xfer->send.pay_len = m->m_pkthdr.len;
608
609                 if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
610                         /* error */
611                         ifp->if_oerrors ++;
612                         /* XXX set error code */
613                         fwe_output_callback(xfer);
614                 } else {
615                         ifp->if_opackets ++;
616                         i++;
617                 }
618         }
619 #if 0
620         if (i > 1)
621                 printf("%d queued\n", i);
622 #endif
623         if (i > 0)
624                 xferq->start(fwe->fd.fc);
625 }
626
627 /* Async. stream output */
628 static void
629 fwe_as_input(struct fw_xferq *xferq)
630 {
631         struct mbuf *m, *m0;
632         struct ifnet *ifp;
633         struct fwe_softc *fwe;
634         struct fw_bulkxfer *sxfer;
635         struct fw_pkt *fp;
636         u_char *c;
637 #if defined(__DragonFly__) || __FreeBSD_version < 500000
638         struct ether_header *eh;
639 #endif
640
641         fwe = (struct fwe_softc *)xferq->sc;
642         ifp = fwe->eth_softc.ifp;
643
644         /* We do not need a lock here because the bottom half is serialized */
645         while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
646                 STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
647                 fp = mtod(sxfer->mbuf, struct fw_pkt *);
648                 if (fwe->fd.fc->irx_post != NULL)
649                         fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
650                 m = sxfer->mbuf;
651
652                 /* insert new rbuf */
653                 sxfer->mbuf = m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
654                 if (m0 != NULL) {
655                         m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
656                         STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
657                 } else
658                         printf("%s: m_getcl failed\n", __FUNCTION__);
659
660                 if (sxfer->resp != 0 || fp->mode.stream.len <
661                     ETHER_ALIGN + sizeof(struct ether_header)) {
662                         m_freem(m);
663                         ifp->if_ierrors ++;
664                         continue;
665                 }
666
667                 m->m_data += HDR_LEN + ETHER_ALIGN;
668                 c = mtod(m, u_char *);
669 #if defined(__DragonFly__) || __FreeBSD_version < 500000
670                 eh = (struct ether_header *)c;
671                 m->m_data += sizeof(struct ether_header);
672                 m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN
673                     - sizeof(struct ether_header);
674 #else
675                 m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN;
676 #endif
677                 m->m_pkthdr.rcvif = ifp;
678 #if 0
679                 FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n"
680                          "%02x %02x %02x %02x %02x %02x\n"
681                          "%02x %02x %02x %02x\n"
682                          "%02x %02x %02x %02x\n"
683                          "%02x %02x %02x %02x\n"
684                          "%02x %02x %02x %02x\n",
685                          c[0], c[1], c[2], c[3], c[4], c[5],
686                          c[6], c[7], c[8], c[9], c[10], c[11],
687                          c[12], c[13], c[14], c[15],
688                          c[16], c[17], c[18], c[19],
689                          c[20], c[21], c[22], c[23],
690                          c[20], c[21], c[22], c[23]
691                  );
692 #endif
693 #if defined(__DragonFly__) || __FreeBSD_version < 500000
694                 ether_input(ifp, eh, m);
695 #else
696                 (*ifp->if_input)(ifp, m);
697 #endif
698                 ifp->if_ipackets ++;
699         }
700         if (STAILQ_FIRST(&xferq->stfree) != NULL)
701                 fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
702 }
703
704
705 static devclass_t fwe_devclass;
706
707 static device_method_t fwe_methods[] = {
708         /* device interface */
709         DEVMETHOD(device_identify,      fwe_identify),
710         DEVMETHOD(device_probe,         fwe_probe),
711         DEVMETHOD(device_attach,        fwe_attach),
712         DEVMETHOD(device_detach,        fwe_detach),
713         { 0, 0 }
714 };
715
716 static driver_t fwe_driver = {
717         "fwe",
718         fwe_methods,
719         sizeof(struct fwe_softc),
720 };
721
722
723 #ifdef __DragonFly__
724 DECLARE_DUMMY_MODULE(fwe);
725 #endif
726 DRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
727 MODULE_VERSION(fwe, 1);
728 MODULE_DEPEND(fwe, firewire, 1, 1, 1);