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 $
39 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/module.h>
50 #include <machine/bus.h>
53 #include <net/ethernet.h>
55 #include <net/if_arp.h>
56 #include <net/if_vlan_var.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
61 #include <dev/firewire/firewire.h>
62 #include <dev/firewire/firewirereg.h>
63 #include <dev/firewire/if_fwevar.h>
65 #define FWEDEBUG if (fwedebug) printf
66 #define TX_MAX_QUEUE (FWMAXQUEUE - 1)
67 #define RX_MAX_QUEUE FWMAXQUEUE
69 /* network interface */
70 static void fwe_start __P((struct ifnet *));
71 static int fwe_ioctl __P((struct ifnet *, u_long, caddr_t));
72 static void fwe_init __P((void *));
74 static void fwe_output_callback __P((struct fw_xfer *));
75 static void fwe_as_output __P((struct fwe_softc *, struct ifnet *));
76 static void fwe_as_input __P((struct fw_xferq *));
78 static int fwedebug = 0;
79 static int stream_ch = 1;
81 MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
82 SYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
83 SYSCTL_DECL(_hw_firewire);
84 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
85 "Ethernet Emulation Subsystem");
86 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
87 "Stream channel to use");
90 #define FWE_POLL_REGISTER(func, fwe, ifp) \
91 if (ether_poll_register(func, ifp)) { \
92 struct firewire_comm *fc = (fwe)->fd.fc; \
93 fc->set_intr(fc, 0); \
96 #define FWE_POLL_DEREGISTER(fwe, ifp) \
98 struct firewire_comm *fc = (fwe)->fd.fc; \
99 ether_poll_deregister(ifp); \
100 fc->set_intr(fc, 1); \
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 fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
113 if (cmd == POLL_DEREGISTER) {
114 /* enable interrupts */
118 fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
121 #define FWE_POLL_REGISTER(func, fwe, ifp)
122 #define FWE_POLL_DEREGISTER(fwe, ifp)
125 fwe_identify(driver_t *driver, device_t parent)
127 BUS_ADD_CHILD(parent, 0, "if_fwe", device_get_unit(parent));
131 fwe_probe(device_t dev)
135 pa = device_get_parent(dev);
136 if(device_get_unit(dev) != device_get_unit(pa)){
140 device_set_desc(dev, "Ethernet over FireWire");
145 fwe_attach(device_t dev)
147 struct fwe_softc *fwe;
151 struct fw_eui64 *eui;
153 fwe = ((struct fwe_softc *)device_get_softc(dev));
154 unit = device_get_unit(dev);
156 bzero(fwe, sizeof(struct fwe_softc));
158 fwe->stream_ch = stream_ch;
161 fwe->fd.fc = device_get_ivars(dev);
163 fwe->fd.post_explore = NULL;
164 fwe->eth_softc.fwe = fwe;
166 fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
167 fwe->pkt_hdr.mode.stream.sy = 0;
168 fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
170 /* generate fake MAC address: first and last 3bytes from eui64 */
173 eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0];
175 eui = &fwe->fd.fc->eui;
176 eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
177 eaddr[1] = FW_EUI64_BYTE(eui, 1);
178 eaddr[2] = FW_EUI64_BYTE(eui, 2);
179 eaddr[3] = FW_EUI64_BYTE(eui, 5);
180 eaddr[4] = FW_EUI64_BYTE(eui, 6);
181 eaddr[5] = FW_EUI64_BYTE(eui, 7);
182 printf("if_fwe%d: Fake Ethernet address: "
183 "%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
184 eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
186 /* fill the rest and attach interface */
188 ifp->if_softc = &fwe->eth_softc;
191 ifp->if_name = "fwe";
192 ifp->if_init = fwe_init;
193 ifp->if_output = ether_output;
194 ifp->if_start = fwe_start;
195 ifp->if_ioctl = fwe_ioctl;
196 ifp->if_mtu = ETHERMTU;
197 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
198 ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
201 #if __FreeBSD_version >= 500000
202 ether_ifattach(ifp, eaddr);
204 ether_ifattach(ifp, 1);
208 /* Tell the upper layer(s) we support long frames. */
209 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
210 #if __FreeBSD_version >= 500000
211 ifp->if_capabilities |= IFCAP_VLAN_MTU;
215 FWEDEBUG("interface %s%d created.\n", ifp->if_name, ifp->if_unit);
220 fwe_stop(struct fwe_softc *fwe)
222 struct firewire_comm *fc;
223 struct fw_xferq *xferq;
224 struct ifnet *ifp = &fwe->fwe_if;
225 struct fw_xfer *xfer, *next;
230 FWE_POLL_DEREGISTER(fwe, ifp);
232 if (fwe->dma_ch >= 0) {
233 xferq = fc->ir[fwe->dma_ch];
235 if (xferq->flag & FWXFERQ_RUNNING)
236 fc->irx_disable(fc, fwe->dma_ch);
238 ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
239 FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
242 for (i = 0; i < xferq->bnchunk; i ++)
243 m_freem(xferq->bulkxfer[i].mbuf);
244 free(xferq->bulkxfer, M_FWE);
246 for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
248 next = STAILQ_NEXT(xfer, link);
251 STAILQ_INIT(&fwe->xferlist);
253 xferq->bulkxfer = NULL;
257 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
261 fwe_detach(device_t dev)
263 struct fwe_softc *fwe;
266 fwe = (struct fwe_softc *)device_get_softc(dev);
270 #if __FreeBSD_version >= 500000
271 ether_ifdetach(&fwe->fwe_if);
273 ether_ifdetach(&fwe->fwe_if, 1);
283 struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
284 struct firewire_comm *fc;
285 struct ifnet *ifp = &fwe->fwe_if;
286 struct fw_xferq *xferq;
287 struct fw_xfer *xfer;
291 FWEDEBUG("initializing %s%d\n", ifp->if_name, ifp->if_unit);
293 /* XXX keep promiscoud mode */
294 ifp->if_flags |= IFF_PROMISC;
298 if (fwe->dma_ch < 0) {
300 for (i = START; i < fc->nisodma; i ++) {
302 if ((xferq->flag & FWXFERQ_OPEN) == 0)
307 printf("no free dma channel\n");
311 fwe->stream_ch = stream_ch;
312 fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
313 /* allocate DMA channel and init packet mode */
314 xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
315 FWXFERQ_HANDLER | FWXFERQ_STREAM;
316 xferq->flag &= ~0xff;
317 xferq->flag |= fwe->stream_ch & 0xff;
318 /* register fwe_input handler */
319 xferq->sc = (caddr_t) fwe;
320 xferq->hand = fwe_as_input;
321 xferq->bnchunk = RX_MAX_QUEUE;
323 xferq->psize = MCLBYTES;
326 xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
327 sizeof(struct fw_bulkxfer) * xferq->bnchunk,
329 if (xferq->bulkxfer == NULL) {
330 printf("if_fwe: malloc failed\n");
333 STAILQ_INIT(&xferq->stvalid);
334 STAILQ_INIT(&xferq->stfree);
335 STAILQ_INIT(&xferq->stdma);
336 xferq->stproc = NULL;
337 for (i = 0; i < xferq->bnchunk; i ++) {
339 #if __FreeBSD_version >= 500000
340 m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
342 m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
344 xferq->bulkxfer[i].mbuf = m;
346 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
347 STAILQ_INSERT_TAIL(&xferq->stfree,
348 &xferq->bulkxfer[i], link);
350 printf("fwe_as_input: m_getcl failed\n");
352 STAILQ_INIT(&fwe->xferlist);
353 for (i = 0; i < TX_MAX_QUEUE; i++) {
354 xfer = fw_xfer_alloc(M_FWE);
358 xfer->fc = fwe->fd.fc;
359 xfer->retry_req = fw_asybusy;
360 xfer->sc = (caddr_t)fwe;
361 xfer->act.hand = fwe_output_callback;
362 STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
365 xferq = fc->ir[fwe->dma_ch];
369 if ((xferq->flag & FWXFERQ_RUNNING) == 0)
370 fc->irx_enable(fc, fwe->dma_ch);
372 ifp->if_flags |= IFF_RUNNING;
373 ifp->if_flags &= ~IFF_OACTIVE;
375 FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
377 /* attempt to start output */
384 fwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
386 struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
387 struct ifstat *ifs = NULL;
393 if (ifp->if_flags & IFF_UP) {
394 if (!(ifp->if_flags & IFF_RUNNING))
395 fwe_init(&fwe->eth_softc);
397 if (ifp->if_flags & IFF_RUNNING)
400 /* XXX keep promiscoud mode */
401 ifp->if_flags |= IFF_PROMISC;
410 ifs = (struct ifstat *)data;
411 len = strlen(ifs->ascii);
412 if (len < sizeof(ifs->ascii))
413 snprintf(ifs->ascii + len,
414 sizeof(ifs->ascii) - len,
416 fwe->stream_ch, fwe->dma_ch);
419 #if __FreeBSD_version >= 500000
427 error = ether_ioctl(ifp, cmd, data);
430 #if __FreeBSD_version < 500000
440 fwe_output_callback(struct fw_xfer *xfer)
442 struct fwe_softc *fwe;
446 fwe = (struct fwe_softc *)xfer->sc;
448 /* XXX error check */
449 FWEDEBUG("resp = %d\n", xfer->resp);
454 xfer->send.buf = NULL;
455 fw_xfer_unload(xfer);
458 STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
462 if (ifp->if_snd.ifq_head != NULL)
467 fwe_start(struct ifnet *ifp)
469 struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
472 FWEDEBUG("%s%d starting\n", ifp->if_name, ifp->if_unit);
474 if (fwe->dma_ch < 0) {
475 struct mbuf *m = NULL;
477 FWEDEBUG("%s%d not ready.\n", ifp->if_name, ifp->if_unit);
481 IF_DEQUEUE(&ifp->if_snd, m);
492 ifp->if_flags |= IFF_OACTIVE;
494 if (ifp->if_snd.ifq_len != 0)
495 fwe_as_output(fwe, ifp);
497 ifp->if_flags &= ~IFF_OACTIVE;
503 #define ETHER_ALIGN 2
505 /* Async. stream output */
507 fwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
510 struct fw_xfer *xfer;
511 struct fw_xferq *xferq;
516 xferq = fwe->fd.fc->atq;
517 while (xferq->queued < xferq->maxq - 1) {
518 xfer = STAILQ_FIRST(&fwe->xferlist);
520 printf("if_fwe: lack of xfer\n");
523 IF_DEQUEUE(&ifp->if_snd, m);
526 STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
527 #if __FreeBSD_version >= 500000
530 if (ifp->if_bpf != NULL)
534 /* keep ip packet alignment for alpha */
535 M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
536 fp = (struct fw_pkt *)&xfer->dst; /* XXX */
537 xfer->dst = *((int32_t *)&fwe->pkt_hdr);
538 fp->mode.stream.len = m->m_pkthdr.len;
539 xfer->send.buf = (caddr_t) fp;
541 xfer->send.len = m->m_pkthdr.len + HDR_LEN;
543 if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
546 /* XXX set error code */
547 fwe_output_callback(xfer);
555 printf("%d queued\n", i);
558 xferq->start(fwe->fd.fc);
561 /* Async. stream output */
563 fwe_as_input(struct fw_xferq *xferq)
567 struct fwe_softc *fwe;
568 struct fw_bulkxfer *sxfer;
571 #if __FreeBSD_version < 500000
572 struct ether_header *eh;
575 fwe = (struct fwe_softc *)xferq->sc;
578 FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
580 while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
581 STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
582 if (sxfer->resp != 0)
584 fp = mtod(sxfer->mbuf, struct fw_pkt *);
586 if (fwe->fd.fc->irx_post != NULL)
587 fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
591 sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
593 m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
594 STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
596 printf("fwe_as_input: m_getcl failed\n");
598 m->m_data += HDR_LEN + ETHER_ALIGN;
600 #if __FreeBSD_version < 500000
601 eh = (struct ether_header *)c;
602 m->m_data += sizeof(struct ether_header);
604 m->m_len = m->m_pkthdr.len =
605 fp->mode.stream.len - ETHER_ALIGN;
606 m->m_pkthdr.rcvif = ifp;
608 FWEDEBUG("%02x %02x %02x %02x %02x %02x\n"
609 "%02x %02x %02x %02x %02x %02x\n"
610 "%02x %02x %02x %02x\n"
611 "%02x %02x %02x %02x\n"
612 "%02x %02x %02x %02x\n"
613 "%02x %02x %02x %02x\n",
614 c[0], c[1], c[2], c[3], c[4], c[5],
615 c[6], c[7], c[8], c[9], c[10], c[11],
616 c[12], c[13], c[14], c[15],
617 c[16], c[17], c[18], c[19],
618 c[20], c[21], c[22], c[23],
619 c[20], c[21], c[22], c[23]
622 #if __FreeBSD_version >= 500000
623 (*ifp->if_input)(ifp, m);
625 ether_input(ifp, eh, m);
629 if (STAILQ_FIRST(&xferq->stfree) != NULL)
630 fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
634 static devclass_t fwe_devclass;
636 static device_method_t fwe_methods[] = {
637 /* device interface */
638 DEVMETHOD(device_identify, fwe_identify),
639 DEVMETHOD(device_probe, fwe_probe),
640 DEVMETHOD(device_attach, fwe_attach),
641 DEVMETHOD(device_detach, fwe_detach),
645 static driver_t fwe_driver = {
648 sizeof(struct fwe_softc),
652 DRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
653 MODULE_VERSION(fwe, 1);
654 MODULE_DEPEND(fwe, firewire, 1, 1, 1);