2 * Copyright (c) 2002-2003
3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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:
16 * This product includes software developed by Hidetoshi Shimokawa.
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.
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
37 #ifdef HAVE_KERNEL_OPTION_HEADERS
38 #include "opt_device_polling.h"
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.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>
52 #include <machine/bus.h>
55 #include <net/ethernet.h>
57 #include <net/if_var.h>
58 #include <net/if_arp.h>
59 #include <net/if_types.h>
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"
66 #include <net/if_vlan_var.h>
68 #include <dev/firewire/firewire.h>
69 #include <dev/firewire/firewirereg.h>
70 #include <dev/firewire/if_fwevar.h>
73 #define FWEDEBUG if (fwedebug) if_printf
74 #define TX_MAX_QUEUE (FWMAXQUEUE - 1)
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 *);
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 *);
85 static int fwedebug = 0;
86 static int stream_ch = 1;
87 static int tx_speed = 2;
88 static int rx_queue_len = FWMAXQUEUE;
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");
102 #ifdef DEVICE_POLLING
103 static poll_handler_t fwe_poll;
106 fwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
108 struct fwe_softc *fwe;
109 struct firewire_comm *fc;
111 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
114 fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
116 fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
119 #endif /* DEVICE_POLLING */
122 fwe_identify(driver_t *driver, device_t parent)
124 BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent));
128 fwe_probe(device_t dev)
132 pa = device_get_parent(dev);
133 if(device_get_unit(dev) != device_get_unit(pa)){
137 device_set_desc(dev, "Ethernet over FireWire");
142 fwe_attach(device_t dev)
144 struct fwe_softc *fwe;
147 #if defined(__DragonFly__) || __FreeBSD_version < 500000
152 struct fw_eui64 *eui;
154 fwe = ((struct fwe_softc *)device_get_softc(dev));
155 unit = device_get_unit(dev);
157 bzero(fwe, sizeof(struct fwe_softc));
158 mtx_init(&fwe->mtx, "fwe", NULL, MTX_DEF);
160 fwe->stream_ch = stream_ch;
163 fwe->fd.fc = device_get_ivars(dev);
165 tx_speed = fwe->fd.fc->speed;
168 fwe->fd.post_explore = NULL;
169 fwe->eth_softc.fwe = fwe;
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;
175 /* generate fake MAC address: first and last 3bytes from eui64 */
178 #if defined(__DragonFly__) || __FreeBSD_version < 500000
179 eaddr = &IFP2ENADDR(fwe->eth_softc.ifp)[0];
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]);
194 /* fill the rest and attach interface */
195 ifp = fwe->eth_softc.ifp = if_alloc(IFT_ETHER);
197 device_printf(dev, "can not if_alloc()\n");
200 ifp->if_softc = &fwe->eth_softc;
202 #if __FreeBSD_version >= 501113 || defined(__DragonFly__)
203 if_initname(ifp, device_get_name(dev), unit);
206 ifp->if_name = "fwe";
208 ifp->if_init = fwe_init;
209 #if defined(__DragonFly__) || __FreeBSD_version < 500000
210 ifp->if_output = ether_output;
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;
218 #if defined(__DragonFly__) || __FreeBSD_version < 500000
219 ether_ifattach(ifp, 1);
221 ether_ifattach(ifp, eaddr);
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;
233 FWEDEBUG(ifp, "interface created\n");
238 fwe_stop(struct fwe_softc *fwe)
240 struct firewire_comm *fc;
241 struct fw_xferq *xferq;
242 struct ifnet *ifp = fwe->eth_softc.ifp;
243 struct fw_xfer *xfer, *next;
248 if (fwe->dma_ch >= 0) {
249 xferq = fc->ir[fwe->dma_ch];
251 if (xferq->flag & FWXFERQ_RUNNING)
252 fc->irx_disable(fc, fwe->dma_ch);
254 ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
255 FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
258 for (i = 0; i < xferq->bnchunk; i ++)
259 m_freem(xferq->bulkxfer[i].mbuf);
260 free(xferq->bulkxfer, M_FWE);
262 for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
264 next = STAILQ_NEXT(xfer, link);
267 STAILQ_INIT(&fwe->xferlist);
269 xferq->bulkxfer = NULL;
273 #if defined(__FreeBSD__)
274 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
276 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
281 fwe_detach(device_t dev)
283 struct fwe_softc *fwe;
287 fwe = device_get_softc(dev);
288 ifp = fwe->eth_softc.ifp;
290 #ifdef DEVICE_POLLING
291 if (ifp->if_capenable & IFCAP_POLLING)
292 ether_poll_deregister(ifp);
297 #if defined(__DragonFly__) || __FreeBSD_version < 500000
298 ether_ifdetach(ifp, 1);
305 mtx_destroy(&fwe->mtx);
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;
320 FWEDEBUG(ifp, "initializing\n");
322 /* XXX keep promiscoud mode */
323 ifp->if_flags |= IFF_PROMISC;
326 if (fwe->dma_ch < 0) {
327 fwe->dma_ch = fw_open_isodma(fc, /* tx */0);
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;
342 xferq->psize = MCLBYTES;
345 xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
346 sizeof(struct fw_bulkxfer) * xferq->bnchunk,
348 if (xferq->bulkxfer == NULL) {
349 printf("if_fwe: malloc failed\n");
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);
363 STAILQ_INIT(&fwe->xferlist);
364 for (i = 0; i < TX_MAX_QUEUE; i++) {
365 xfer = fw_xfer_alloc(M_FWE);
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);
375 xferq = fc->ir[fwe->dma_ch];
379 if ((xferq->flag & FWXFERQ_RUNNING) == 0)
380 fc->irx_enable(fc, fwe->dma_ch);
382 #if defined(__FreeBSD__)
383 ifp->if_drv_flags |= IFF_DRV_RUNNING;
384 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
386 ifp->if_flags |= IFF_RUNNING;
387 ifp->if_flags &= ~IFF_OACTIVE;
391 /* attempt to start output */
398 fwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
400 struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
401 struct ifstat *ifs = NULL;
407 if (ifp->if_flags & IFF_UP) {
408 #if defined(__FreeBSD__)
409 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
411 if (!(ifp->if_flags & IFF_RUNNING))
413 fwe_init(&fwe->eth_softc);
415 #if defined(__FreeBSD__)
416 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
418 if (ifp->if_flags & IFF_RUNNING)
422 /* XXX keep promiscoud mode */
423 ifp->if_flags |= IFF_PROMISC;
432 ifs = (struct ifstat *)data;
433 snprintf(ifs->ascii, sizeof(ifs->ascii),
434 "\tch %d dma %d\n", fwe->stream_ch, fwe->dma_ch);
438 #ifdef DEVICE_POLLING
440 struct ifreq *ifr = (struct ifreq *) data;
441 struct firewire_comm *fc = fwe->fd.fc;
443 if (ifr->ifr_reqcap & IFCAP_POLLING &&
444 !(ifp->if_capenable & IFCAP_POLLING)) {
445 error = ether_poll_register(fwe_poll, ifp);
448 /* Disable interrupts */
450 ifp->if_capenable |= IFCAP_POLLING;
451 ifp->if_capenable |= IFCAP_POLLING_NOCOUNT;
454 if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
455 ifp->if_capenable & IFCAP_POLLING) {
456 error = ether_poll_deregister(ifp);
457 /* Enable interrupts. */
459 ifp->if_capenable &= ~IFCAP_POLLING;
460 ifp->if_capenable &= ~IFCAP_POLLING_NOCOUNT;
464 #endif /* DEVICE_POLLING */
466 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
474 error = ether_ioctl(ifp, cmd, data);
477 #if defined(__DragonFly__) || __FreeBSD_version < 500000
487 fwe_output_callback(struct fw_xfer *xfer)
489 struct fwe_softc *fwe;
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);
501 fw_xfer_unload(xfer);
505 STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
510 if (ifp->if_snd.ifq_head != NULL)
515 fwe_start(struct ifnet *ifp)
517 struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
520 FWEDEBUG(ifp, "starting\n");
522 if (fwe->dma_ch < 0) {
523 struct mbuf *m = NULL;
525 FWEDEBUG(ifp, "not ready\n");
529 IF_DEQUEUE(&ifp->if_snd, m);
540 #if defined(__FreeBSD__)
541 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
543 ifp->if_flags |= IFF_OACTIVE;
546 if (ifp->if_snd.ifq_len != 0)
547 fwe_as_output(fwe, ifp);
549 #if defined(__FreeBSD__)
550 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
552 ifp->if_flags &= ~IFF_OACTIVE;
559 #define ETHER_ALIGN 2
561 /* Async. stream output */
563 fwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
566 struct fw_xfer *xfer;
567 struct fw_xferq *xferq;
572 xferq = fwe->fd.fc->atq;
573 while ((xferq->queued < xferq->maxq - 1) &&
574 (ifp->if_snd.ifq_head != NULL)) {
576 xfer = STAILQ_FIRST(&fwe->xferlist);
579 printf("if_fwe: lack of xfer\n");
584 STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
587 IF_DEQUEUE(&ifp->if_snd, m);
590 STAILQ_INSERT_HEAD(&fwe->xferlist, xfer, link);
594 #if defined(__DragonFly__) || __FreeBSD_version < 500000
595 if (ifp->if_bpf != NULL)
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;
607 xfer->send.pay_len = m->m_pkthdr.len;
609 if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
612 /* XXX set error code */
613 fwe_output_callback(xfer);
621 printf("%d queued\n", i);
624 xferq->start(fwe->fd.fc);
627 /* Async. stream output */
629 fwe_as_input(struct fw_xferq *xferq)
633 struct fwe_softc *fwe;
634 struct fw_bulkxfer *sxfer;
637 #if defined(__DragonFly__) || __FreeBSD_version < 500000
638 struct ether_header *eh;
641 fwe = (struct fwe_softc *)xferq->sc;
642 ifp = fwe->eth_softc.ifp;
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);
652 /* insert new rbuf */
653 sxfer->mbuf = m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
655 m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
656 STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
658 printf("%s: m_getcl failed\n", __FUNCTION__);
660 if (sxfer->resp != 0 || fp->mode.stream.len <
661 ETHER_ALIGN + sizeof(struct ether_header)) {
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);
675 m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN;
677 m->m_pkthdr.rcvif = ifp;
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]
693 #if defined(__DragonFly__) || __FreeBSD_version < 500000
694 ether_input(ifp, eh, m);
696 (*ifp->if_input)(ifp, m);
700 if (STAILQ_FIRST(&xferq->stfree) != NULL)
701 fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
705 static devclass_t fwe_devclass;
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),
716 static driver_t fwe_driver = {
719 sizeof(struct fwe_softc),
724 DECLARE_DUMMY_MODULE(fwe);
726 DRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
727 MODULE_VERSION(fwe, 1);
728 MODULE_DEPEND(fwe, firewire, 1, 1, 1);