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
34 * $FreeBSD: src/sys/dev/firewire/if_fwe.c,v 1.1.2.11 2003/04/28 03:29:18 simokawa Exp $
35 * $DragonFly: src/sys/dev/netif/fwe/if_fwe.c,v 1.2 2003/06/17 04:28:26 dillon Exp $
40 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <sys/module.h>
51 #include <machine/bus.h>
54 #include <net/ethernet.h>
56 #include <net/if_arp.h>
57 #include <net/if_vlan_var.h>
58 #include <net/route.h>
60 #include <netinet/in.h>
62 #include <dev/firewire/firewire.h>
63 #include <dev/firewire/firewirereg.h>
64 #include <dev/firewire/if_fwevar.h>
66 #define FWEDEBUG if (fwedebug) printf
67 #define TX_MAX_QUEUE (FWMAXQUEUE - 1)
68 #define RX_MAX_QUEUE FWMAXQUEUE
70 /* network interface */
71 static void fwe_start __P((struct ifnet *));
72 static int fwe_ioctl __P((struct ifnet *, u_long, caddr_t));
73 static void fwe_init __P((void *));
75 static void fwe_output_callback __P((struct fw_xfer *));
76 static void fwe_as_output __P((struct fwe_softc *, struct ifnet *));
77 static void fwe_as_input __P((struct fw_xferq *));
79 static int fwedebug = 0;
80 static int stream_ch = 1;
82 MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
83 SYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
84 SYSCTL_DECL(_hw_firewire);
85 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
86 "Ethernet Emulation Subsystem");
87 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
88 "Stream channel to use");
91 #define FWE_POLL_REGISTER(func, fwe, ifp) \
92 if (ether_poll_register(func, ifp)) { \
93 struct firewire_comm *fc = (fwe)->fd.fc; \
94 fc->set_intr(fc, 0); \
97 #define FWE_POLL_DEREGISTER(fwe, ifp) \
99 struct firewire_comm *fc = (fwe)->fd.fc; \
100 ether_poll_deregister(ifp); \
101 fc->set_intr(fc, 1); \
104 static poll_handler_t fwe_poll;
107 fwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
109 struct fwe_softc *fwe;
110 struct firewire_comm *fc;
112 fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
114 if (cmd == POLL_DEREGISTER) {
115 /* enable interrupts */
119 fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
122 #define FWE_POLL_REGISTER(func, fwe, ifp)
123 #define FWE_POLL_DEREGISTER(fwe, ifp)
126 fwe_identify(driver_t *driver, device_t parent)
128 BUS_ADD_CHILD(parent, 0, "if_fwe", device_get_unit(parent));
132 fwe_probe(device_t dev)
136 pa = device_get_parent(dev);
137 if(device_get_unit(dev) != device_get_unit(pa)){
141 device_set_desc(dev, "Ethernet over FireWire");
146 fwe_attach(device_t dev)
148 struct fwe_softc *fwe;
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));
159 fwe->stream_ch = stream_ch;
162 fwe->fd.fc = device_get_ivars(dev);
164 fwe->fd.post_explore = NULL;
165 fwe->eth_softc.fwe = fwe;
167 fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
168 fwe->pkt_hdr.mode.stream.sy = 0;
169 fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
171 /* generate fake MAC address: first and last 3bytes from eui64 */
174 eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0];
176 eui = &fwe->fd.fc->eui;
177 eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
178 eaddr[1] = FW_EUI64_BYTE(eui, 1);
179 eaddr[2] = FW_EUI64_BYTE(eui, 2);
180 eaddr[3] = FW_EUI64_BYTE(eui, 5);
181 eaddr[4] = FW_EUI64_BYTE(eui, 6);
182 eaddr[5] = FW_EUI64_BYTE(eui, 7);
183 printf("if_fwe%d: Fake Ethernet address: "
184 "%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
185 eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
187 /* fill the rest and attach interface */
189 ifp->if_softc = &fwe->eth_softc;
192 ifp->if_name = "fwe";
193 ifp->if_init = fwe_init;
194 ifp->if_output = ether_output;
195 ifp->if_start = fwe_start;
196 ifp->if_ioctl = fwe_ioctl;
197 ifp->if_mtu = ETHERMTU;
198 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
199 ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
202 #if __FreeBSD_version >= 500000
203 ether_ifattach(ifp, eaddr);
205 ether_ifattach(ifp, 1);
209 /* Tell the upper layer(s) we support long frames. */
210 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
211 #if __FreeBSD_version >= 500000
212 ifp->if_capabilities |= IFCAP_VLAN_MTU;
216 FWEDEBUG("interface %s%d created.\n", ifp->if_name, ifp->if_unit);
221 fwe_stop(struct fwe_softc *fwe)
223 struct firewire_comm *fc;
224 struct fw_xferq *xferq;
225 struct ifnet *ifp = &fwe->fwe_if;
226 struct fw_xfer *xfer, *next;
231 FWE_POLL_DEREGISTER(fwe, ifp);
233 if (fwe->dma_ch >= 0) {
234 xferq = fc->ir[fwe->dma_ch];
236 if (xferq->flag & FWXFERQ_RUNNING)
237 fc->irx_disable(fc, fwe->dma_ch);
239 ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
240 FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
243 for (i = 0; i < xferq->bnchunk; i ++)
244 m_freem(xferq->bulkxfer[i].mbuf);
245 free(xferq->bulkxfer, M_FWE);
247 for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
249 next = STAILQ_NEXT(xfer, link);
252 STAILQ_INIT(&fwe->xferlist);
254 xferq->bulkxfer = NULL;
258 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
262 fwe_detach(device_t dev)
264 struct fwe_softc *fwe;
267 fwe = (struct fwe_softc *)device_get_softc(dev);
271 #if __FreeBSD_version >= 500000
272 ether_ifdetach(&fwe->fwe_if);
274 ether_ifdetach(&fwe->fwe_if, 1);
284 struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
285 struct firewire_comm *fc;
286 struct ifnet *ifp = &fwe->fwe_if;
287 struct fw_xferq *xferq;
288 struct fw_xfer *xfer;
292 FWEDEBUG("initializing %s%d\n", ifp->if_name, ifp->if_unit);
294 /* XXX keep promiscoud mode */
295 ifp->if_flags |= IFF_PROMISC;
299 if (fwe->dma_ch < 0) {
301 for (i = START; i < fc->nisodma; i ++) {
303 if ((xferq->flag & FWXFERQ_OPEN) == 0)
308 printf("no free dma channel\n");
312 fwe->stream_ch = stream_ch;
313 fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
314 /* allocate DMA channel and init packet mode */
315 xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
316 FWXFERQ_HANDLER | FWXFERQ_STREAM;
317 xferq->flag &= ~0xff;
318 xferq->flag |= fwe->stream_ch & 0xff;
319 /* register fwe_input handler */
320 xferq->sc = (caddr_t) fwe;
321 xferq->hand = fwe_as_input;
322 xferq->bnchunk = RX_MAX_QUEUE;
324 xferq->psize = MCLBYTES;
327 xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
328 sizeof(struct fw_bulkxfer) * xferq->bnchunk,
330 if (xferq->bulkxfer == NULL) {
331 printf("if_fwe: malloc failed\n");
334 STAILQ_INIT(&xferq->stvalid);
335 STAILQ_INIT(&xferq->stfree);
336 STAILQ_INIT(&xferq->stdma);
337 xferq->stproc = NULL;
338 for (i = 0; i < xferq->bnchunk; i ++) {
340 #if __FreeBSD_version >= 500000
341 m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
343 m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
345 xferq->bulkxfer[i].mbuf = m;
347 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
348 STAILQ_INSERT_TAIL(&xferq->stfree,
349 &xferq->bulkxfer[i], link);
351 printf("fwe_as_input: m_getcl failed\n");
353 STAILQ_INIT(&fwe->xferlist);
354 for (i = 0; i < TX_MAX_QUEUE; i++) {
355 xfer = fw_xfer_alloc(M_FWE);
359 xfer->fc = fwe->fd.fc;
360 xfer->retry_req = fw_asybusy;
361 xfer->sc = (caddr_t)fwe;
362 xfer->act.hand = fwe_output_callback;
363 STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
366 xferq = fc->ir[fwe->dma_ch];
370 if ((xferq->flag & FWXFERQ_RUNNING) == 0)
371 fc->irx_enable(fc, fwe->dma_ch);
373 ifp->if_flags |= IFF_RUNNING;
374 ifp->if_flags &= ~IFF_OACTIVE;
376 FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
378 /* attempt to start output */
385 fwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
387 struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
388 struct ifstat *ifs = NULL;
394 if (ifp->if_flags & IFF_UP) {
395 if (!(ifp->if_flags & IFF_RUNNING))
396 fwe_init(&fwe->eth_softc);
398 if (ifp->if_flags & IFF_RUNNING)
401 /* XXX keep promiscoud mode */
402 ifp->if_flags |= IFF_PROMISC;
411 ifs = (struct ifstat *)data;
412 len = strlen(ifs->ascii);
413 if (len < sizeof(ifs->ascii))
414 snprintf(ifs->ascii + len,
415 sizeof(ifs->ascii) - len,
417 fwe->stream_ch, fwe->dma_ch);
420 #if __FreeBSD_version >= 500000
428 error = ether_ioctl(ifp, cmd, data);
431 #if __FreeBSD_version < 500000
441 fwe_output_callback(struct fw_xfer *xfer)
443 struct fwe_softc *fwe;
447 fwe = (struct fwe_softc *)xfer->sc;
449 /* XXX error check */
450 FWEDEBUG("resp = %d\n", xfer->resp);
455 xfer->send.buf = NULL;
456 fw_xfer_unload(xfer);
459 STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
463 if (ifp->if_snd.ifq_head != NULL)
468 fwe_start(struct ifnet *ifp)
470 struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
473 FWEDEBUG("%s%d starting\n", ifp->if_name, ifp->if_unit);
475 if (fwe->dma_ch < 0) {
476 struct mbuf *m = NULL;
478 FWEDEBUG("%s%d not ready.\n", ifp->if_name, ifp->if_unit);
482 IF_DEQUEUE(&ifp->if_snd, m);
493 ifp->if_flags |= IFF_OACTIVE;
495 if (ifp->if_snd.ifq_len != 0)
496 fwe_as_output(fwe, ifp);
498 ifp->if_flags &= ~IFF_OACTIVE;
504 #define ETHER_ALIGN 2
506 /* Async. stream output */
508 fwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
511 struct fw_xfer *xfer;
512 struct fw_xferq *xferq;
517 xferq = fwe->fd.fc->atq;
518 while (xferq->queued < xferq->maxq - 1) {
519 xfer = STAILQ_FIRST(&fwe->xferlist);
521 printf("if_fwe: lack of xfer\n");
524 IF_DEQUEUE(&ifp->if_snd, m);
527 STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
528 #if __FreeBSD_version >= 500000
531 if (ifp->if_bpf != NULL)
535 /* keep ip packet alignment for alpha */
536 M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
537 fp = (struct fw_pkt *)&xfer->dst; /* XXX */
538 xfer->dst = *((int32_t *)&fwe->pkt_hdr);
539 fp->mode.stream.len = m->m_pkthdr.len;
540 xfer->send.buf = (caddr_t) fp;
542 xfer->send.len = m->m_pkthdr.len + HDR_LEN;
544 if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
547 /* XXX set error code */
548 fwe_output_callback(xfer);
556 printf("%d queued\n", i);
559 xferq->start(fwe->fd.fc);
562 /* Async. stream output */
564 fwe_as_input(struct fw_xferq *xferq)
568 struct fwe_softc *fwe;
569 struct fw_bulkxfer *sxfer;
572 #if __FreeBSD_version < 500000
573 struct ether_header *eh;
576 fwe = (struct fwe_softc *)xferq->sc;
579 FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
581 while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
582 STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
583 if (sxfer->resp != 0)
585 fp = mtod(sxfer->mbuf, struct fw_pkt *);
587 if (fwe->fd.fc->irx_post != NULL)
588 fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
592 sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
594 m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
595 STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
597 printf("fwe_as_input: m_getcl failed\n");
599 m->m_data += HDR_LEN + ETHER_ALIGN;
601 #if __FreeBSD_version < 500000
602 eh = (struct ether_header *)c;
603 m->m_data += sizeof(struct ether_header);
605 m->m_len = m->m_pkthdr.len =
606 fp->mode.stream.len - ETHER_ALIGN;
607 m->m_pkthdr.rcvif = ifp;
609 FWEDEBUG("%02x %02x %02x %02x %02x %02x\n"
610 "%02x %02x %02x %02x %02x %02x\n"
611 "%02x %02x %02x %02x\n"
612 "%02x %02x %02x %02x\n"
613 "%02x %02x %02x %02x\n"
614 "%02x %02x %02x %02x\n",
615 c[0], c[1], c[2], c[3], c[4], c[5],
616 c[6], c[7], c[8], c[9], c[10], c[11],
617 c[12], c[13], c[14], c[15],
618 c[16], c[17], c[18], c[19],
619 c[20], c[21], c[22], c[23],
620 c[20], c[21], c[22], c[23]
623 #if __FreeBSD_version >= 500000
624 (*ifp->if_input)(ifp, m);
626 ether_input(ifp, eh, m);
630 if (STAILQ_FIRST(&xferq->stfree) != NULL)
631 fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
635 static devclass_t fwe_devclass;
637 static device_method_t fwe_methods[] = {
638 /* device interface */
639 DEVMETHOD(device_identify, fwe_identify),
640 DEVMETHOD(device_probe, fwe_probe),
641 DEVMETHOD(device_attach, fwe_attach),
642 DEVMETHOD(device_detach, fwe_detach),
646 static driver_t fwe_driver = {
649 sizeof(struct fwe_softc),
653 DRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
654 MODULE_VERSION(fwe, 1);
655 MODULE_DEPEND(fwe, firewire, 1, 1, 1);