7da8fa31d31511d4690691df103589501b85a021
[dragonfly.git] / sys / dev / netif / fwe / 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: src/sys/dev/firewire/if_fwe.c,v 1.27 2004/01/08 14:58:09 simokawa Exp $
35  * $DragonFly: src/sys/dev/netif/fwe/if_fwe.c,v 1.31 2008/05/14 11:59:20 sephe Exp $
36  */
37
38 #include "opt_inet.h"
39 #include "opt_polling.h"
40
41 #include <sys/param.h>
42 #include <sys/conf.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 <sys/thread2.h>
53
54 #include <net/bpf.h>
55 #include <net/ethernet.h>
56 #include <net/if.h>
57 #include <net/if_arp.h>
58 #ifdef __DragonFly__
59 #include <net/ifq_var.h>
60 #include <net/vlan/if_vlan_var.h>
61 #include <bus/firewire/firewire.h>
62 #include <bus/firewire/firewirereg.h>
63 #include "if_fwevar.h"
64 #else
65 #include <net/if_vlan_var.h>
66
67 #include <dev/firewire/firewire.h>
68 #include <dev/firewire/firewirereg.h>
69 #include <dev/firewire/if_fwevar.h>
70 #endif
71
72 #define FWEDEBUG        if (fwedebug) if_printf
73 #define TX_MAX_QUEUE    (FWMAXQUEUE - 1)
74
75 /* network interface */
76 static void fwe_start (struct ifnet *);
77 static int fwe_ioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
78 static void fwe_init (void *);
79
80 static void fwe_output_callback (struct fw_xfer *);
81 static void fwe_as_output (struct fwe_softc *, struct ifnet *);
82 static void fwe_as_input (struct fw_xferq *);
83
84 static int fwedebug = 0;
85 static int stream_ch = 1;
86 static int tx_speed = 2;
87 static int rx_queue_len = FWMAXQUEUE;
88
89 MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
90 SYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
91 SYSCTL_DECL(_hw_firewire);
92 SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
93         "Ethernet emulation subsystem");
94 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
95         "Stream channel to use");
96 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RW, &tx_speed, 0,
97         "Transmission speed");
98 SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
99         0, "Length of the receive queue");
100
101 TUNABLE_INT("hw.firewire.fwe.stream_ch", &stream_ch);
102 TUNABLE_INT("hw.firewire.fwe.tx_speed", &tx_speed);
103 TUNABLE_INT("hw.firewire.fwe.rx_queue_len", &rx_queue_len);
104
105 #ifdef DEVICE_POLLING
106
107 static void
108 fwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
109 {
110         struct fwe_softc *fwe;
111         struct firewire_comm *fc;
112
113         fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
114         fc = fwe->fd.fc;
115         switch(cmd) {
116         case POLL_REGISTER:
117                 /* disable interrupts */
118                 fc->set_intr(fc, 0);
119                 break;
120         case POLL_DEREGISTER:
121                 /* enable interrupts */
122                 fc->set_intr(fc, 1);
123                 break;
124         default:
125                 fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
126                 break;
127         }
128 }
129
130 #endif
131
132 static int
133 fwe_probe(device_t dev)
134 {
135         device_t pa;
136
137         pa = device_get_parent(dev);
138         if(device_get_unit(dev) != device_get_unit(pa)){
139                 return(ENXIO);
140         }
141
142         device_set_desc(dev, "Ethernet over FireWire");
143         return (0);
144 }
145
146 static int
147 fwe_attach(device_t dev)
148 {
149         struct fwe_softc *fwe;
150         struct ifnet *ifp;
151         uint8_t eaddr[ETHER_ADDR_LEN];
152         struct fw_eui64 *eui;
153
154         fwe = ((struct fwe_softc *)device_get_softc(dev));
155
156         /* XXX */
157         fwe->stream_ch = stream_ch;
158         fwe->dma_ch = -1;
159
160         fwe->fd.fc = device_get_ivars(dev);
161         if (tx_speed < 0)
162                 tx_speed = fwe->fd.fc->speed;
163
164         fwe->fd.dev = dev;
165         fwe->fd.post_explore = NULL;
166         fwe->eth_softc.fwe = fwe;
167
168         fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
169         fwe->pkt_hdr.mode.stream.sy = 0;
170         fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
171
172         /* generate fake MAC address: first and last 3bytes from eui64 */
173 #define LOCAL (0x02)
174 #define GROUP (0x01)
175
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
184         /* fill the rest and attach interface */        
185         ifp = &fwe->fwe_if;
186         ifp->if_softc = &fwe->eth_softc;
187
188         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
189         ifp->if_init = fwe_init;
190         ifp->if_start = fwe_start;
191         ifp->if_ioctl = fwe_ioctl;
192         ifp->if_capabilities = IFCAP_VLAN_MTU;
193 #ifdef DEVICE_POLLING
194         ifp->if_poll = fwe_poll;
195 #endif
196         ifp->if_mtu = ETHERMTU;
197         ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
198         ifq_set_maxlen(&ifp->if_snd, TX_MAX_QUEUE);
199         ifq_set_ready(&ifp->if_snd);
200
201         ether_ifattach(ifp, eaddr, NULL);
202
203         /* Tell the upper layer(s) we support long frames. */
204         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
205
206
207         FWEDEBUG(ifp, "interface created\n");
208         return 0;
209 }
210
211 static void
212 fwe_stop(struct fwe_softc *fwe)
213 {
214         struct firewire_comm *fc;
215         struct fw_xferq *xferq;
216         struct ifnet *ifp = &fwe->fwe_if;
217         struct fw_xfer *xfer, *next;
218         int i;
219
220         fc = fwe->fd.fc;
221
222         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
223
224         if (fwe->dma_ch >= 0) {
225                 xferq = fc->ir[fwe->dma_ch];
226
227                 if (xferq->flag & FWXFERQ_RUNNING)
228                         fc->irx_disable(fc, fwe->dma_ch);
229                 xferq->flag &= 
230                         ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
231                         FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
232                 xferq->hand =  NULL;
233
234                 for (i = 0; i < xferq->bnchunk; i ++)
235                         m_freem(xferq->bulkxfer[i].mbuf);
236                 kfree(xferq->bulkxfer, M_FWE);
237
238                 for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
239                                         xfer = next) {
240                         next = STAILQ_NEXT(xfer, link);
241                         fw_xfer_free(xfer);
242                 }
243                 STAILQ_INIT(&fwe->xferlist);
244
245                 xferq->bulkxfer =  NULL;
246                 fwe->dma_ch = -1;
247         }
248 }
249
250 static int
251 fwe_detach(device_t dev)
252 {
253         struct fwe_softc *fwe = device_get_softc(dev);
254
255         lwkt_serialize_enter(fwe->fwe_if.if_serializer);
256         fwe_stop(fwe);
257         lwkt_serialize_exit(fwe->fwe_if.if_serializer);
258
259         ether_ifdetach(&fwe->fwe_if);
260         return 0;
261 }
262
263 static void
264 fwe_init(void *arg)
265 {
266         struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
267         struct firewire_comm *fc;
268         struct ifnet *ifp = &fwe->fwe_if;
269         struct fw_xferq *xferq;
270         struct fw_xfer *xfer;
271         struct mbuf *m;
272         int i;
273
274         FWEDEBUG(ifp, "initializing\n");
275
276         /* XXX keep promiscoud mode */
277         ifp->if_flags |= IFF_PROMISC;
278
279         fc = fwe->fd.fc;
280 #define START 0
281         if (fwe->dma_ch < 0) {
282                 for (i = START; i < fc->nisodma; i ++) {
283                         xferq = fc->ir[i];
284                         if ((xferq->flag & FWXFERQ_OPEN) == 0)
285                                 goto found;
286                 }
287                 if_printf(ifp, "no free dma channel\n");
288                 return;
289 found:
290                 fwe->dma_ch = i;
291                 fwe->stream_ch = stream_ch;
292                 fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
293                 /* allocate DMA channel and init packet mode */
294                 xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
295                                 FWXFERQ_HANDLER | FWXFERQ_STREAM;
296                 xferq->flag &= ~0xff;
297                 xferq->flag |= fwe->stream_ch & 0xff;
298                 /* register fwe_input handler */
299                 xferq->sc = (caddr_t) fwe;
300                 xferq->hand = fwe_as_input;
301                 xferq->bnchunk = rx_queue_len;
302                 xferq->bnpacket = 1;
303                 xferq->psize = MCLBYTES;
304                 xferq->queued = 0;
305                 xferq->buf = NULL;
306                 xferq->bulkxfer = (struct fw_bulkxfer *) kmalloc(
307                         sizeof(struct fw_bulkxfer) * xferq->bnchunk,
308                                                         M_FWE, M_WAITOK);
309                 STAILQ_INIT(&xferq->stvalid);
310                 STAILQ_INIT(&xferq->stfree);
311                 STAILQ_INIT(&xferq->stdma);
312                 xferq->stproc = NULL;
313                 for (i = 0; i < xferq->bnchunk; i ++) {
314                         m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
315                         xferq->bulkxfer[i].mbuf = m;
316                         if (m != NULL) {
317                                 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
318                                 STAILQ_INSERT_TAIL(&xferq->stfree,
319                                                 &xferq->bulkxfer[i], link);
320                         } else {
321                                 if_printf(ifp, "fwe_init: m_getcl failed\n");
322                         }
323                 }
324                 STAILQ_INIT(&fwe->xferlist);
325                 for (i = 0; i < TX_MAX_QUEUE; i++) {
326                         xfer = fw_xfer_alloc(M_FWE);
327                         if (xfer == NULL)
328                                 break;
329                         xfer->send.spd = tx_speed;
330                         xfer->fc = fwe->fd.fc;
331                         xfer->retry_req = fw_asybusy;
332                         xfer->sc = (caddr_t)fwe;
333                         xfer->act.hand = fwe_output_callback;
334                         STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
335                 }
336         } else
337                 xferq = fc->ir[fwe->dma_ch];
338
339 #ifdef DEVICE_POLLING
340         /* Disable interrupt, if polling(4) is enabled */
341         if (ifp->if_flags & IFF_POLLING)
342                 fc->set_intr(fc, 0);
343         else
344 #endif
345         fc->set_intr(fc, 1);
346
347         /* start dma */
348         if ((xferq->flag & FWXFERQ_RUNNING) == 0)
349                 fc->irx_enable(fc, fwe->dma_ch);
350
351         ifp->if_flags |= IFF_RUNNING;
352         ifp->if_flags &= ~IFF_OACTIVE;
353 }
354
355
356 static int
357 fwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
358 {
359         struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
360         struct ifstat *ifs = NULL;
361         int error = 0, len;
362
363         switch (cmd) {
364         case SIOCSIFFLAGS:
365                 if (ifp->if_flags & IFF_UP) {
366                         if (!(ifp->if_flags & IFF_RUNNING))
367                                 fwe_init(&fwe->eth_softc);
368                 } else {
369                         if (ifp->if_flags & IFF_RUNNING)
370                                 fwe_stop(fwe);
371                 }
372                 /* XXX keep promiscoud mode */
373                 ifp->if_flags |= IFF_PROMISC;
374                 break;
375         case SIOCADDMULTI:
376         case SIOCDELMULTI:
377                 break;
378
379         case SIOCGIFSTATUS:
380                 ifs = (struct ifstat *)data;
381                 len = strlen(ifs->ascii);
382                 if (len < sizeof(ifs->ascii))
383                         ksnprintf(ifs->ascii + len,
384                                 sizeof(ifs->ascii) - len,
385                                 "\tch %d dma %d\n",
386                                         fwe->stream_ch, fwe->dma_ch);
387                 break;
388         default:
389                 error = ether_ioctl(ifp, cmd, data);
390                 break;
391         }
392         return (error);
393 }
394
395 static void
396 fwe_output_callback(struct fw_xfer *xfer)
397 {
398         struct fwe_softc *fwe;
399         struct ifnet *ifp;
400
401         fwe = (struct fwe_softc *)xfer->sc;
402         ifp = &fwe->fwe_if;
403         lwkt_serialize_enter(ifp->if_serializer);
404         /* XXX error check */
405         FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
406         if (xfer->resp != 0)
407                 ifp->if_oerrors ++;
408                 
409         m_freem(xfer->mbuf);
410         fw_xfer_unload(xfer);
411
412         STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
413
414         /* for queue full */
415         if (!ifq_is_empty(&ifp->if_snd))
416                 if_devstart(ifp);
417         lwkt_serialize_exit(ifp->if_serializer);
418 }
419
420 static void
421 fwe_start(struct ifnet *ifp)
422 {
423         struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
424
425         FWEDEBUG(ifp, "starting\n");
426
427         if (fwe->dma_ch < 0) {
428                 FWEDEBUG(ifp, "not ready\n");
429
430                 ifq_purge(&ifp->if_snd);
431         } else {
432                 ifp->if_flags |= IFF_OACTIVE;
433
434                 if (!ifq_is_empty(&ifp->if_snd))
435                         fwe_as_output(fwe, ifp);
436
437                 ifp->if_flags &= ~IFF_OACTIVE;
438         }
439 }
440
441 #define HDR_LEN 4
442 #ifndef ETHER_ALIGN
443 #define ETHER_ALIGN 2
444 #endif
445 /* Async. stream output */
446 static void
447 fwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
448 {
449         struct mbuf *m;
450         struct fw_xfer *xfer;
451         struct fw_xferq *xferq;
452         struct fw_pkt *fp;
453         int i = 0;
454
455         xfer = NULL;
456         xferq = fwe->fd.fc->atq;
457         while (xferq->queued < xferq->maxq - 1) {
458                 xfer = STAILQ_FIRST(&fwe->xferlist);
459                 if (xfer == NULL) {
460                         if_printf(ifp, "lack of xfer\n");
461                         return;
462                 }
463                 m = ifq_dequeue(&ifp->if_snd, NULL);
464                 if (m == NULL)
465                         break;
466                 STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
467                 BPF_MTAP(ifp, m);
468
469                 /* keep ip packet alignment for alpha */
470                 M_PREPEND(m, ETHER_ALIGN, MB_DONTWAIT);
471                 fp = &xfer->send.hdr;
472                 *(u_int32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
473                 fp->mode.stream.len = m->m_pkthdr.len;
474                 xfer->mbuf = m;
475                 xfer->send.pay_len = m->m_pkthdr.len;
476
477                 if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
478                         /* error */
479                         ifp->if_oerrors ++;
480                         /* XXX set error code */
481                         fwe_output_callback(xfer);
482                 } else {
483                         ifp->if_opackets ++;
484                         i++;
485                 }
486         }
487 #if 0
488         if (i > 1)
489                 if_printf(ifp, "%d queued\n", i);
490 #endif
491         if (i > 0)
492                 xferq->start(fwe->fd.fc);
493 }
494
495 /* Async. stream output */
496 static void
497 fwe_as_input(struct fw_xferq *xferq)
498 {
499         struct mbuf *m, *m0;
500         struct ifnet *ifp;
501         struct fwe_softc *fwe;
502         struct fw_bulkxfer *sxfer;
503         struct fw_pkt *fp;
504         u_char *c;
505
506         fwe = (struct fwe_softc *)xferq->sc;
507         ifp = &fwe->fwe_if;
508         lwkt_serialize_enter(ifp->if_serializer);
509         while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
510                 STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
511                 fp = mtod(sxfer->mbuf, struct fw_pkt *);
512                 if (fwe->fd.fc->irx_post != NULL)
513                         fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
514                 m = sxfer->mbuf;
515
516                 /* insert new rbuf */
517                 sxfer->mbuf = m0 = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
518                 if (m0 != NULL) {
519                         m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
520                         STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
521                 } else {
522                         if_printf(ifp, "fwe_as_input: m_getcl failed\n");
523                 }
524
525                 if (sxfer->resp != 0 || fp->mode.stream.len <
526                     ETHER_ALIGN + sizeof(struct ether_header)) {
527                         m_freem(m);
528                         ifp->if_ierrors ++;
529                         continue;
530                 }
531
532                 m->m_data += HDR_LEN + ETHER_ALIGN;
533                 c = mtod(m, char *);
534                 m->m_len = m->m_pkthdr.len =
535                                 fp->mode.stream.len - ETHER_ALIGN;
536                 m->m_pkthdr.rcvif = ifp;
537 #if 0
538                 FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n"
539                          "%02x %02x %02x %02x %02x %02x\n"
540                          "%02x %02x %02x %02x\n"
541                          "%02x %02x %02x %02x\n"
542                          "%02x %02x %02x %02x\n"
543                          "%02x %02x %02x %02x\n",
544                          c[0], c[1], c[2], c[3], c[4], c[5],
545                          c[6], c[7], c[8], c[9], c[10], c[11],
546                          c[12], c[13], c[14], c[15],
547                          c[16], c[17], c[18], c[19],
548                          c[20], c[21], c[22], c[23],
549                          c[20], c[21], c[22], c[23]
550                  );
551 #endif
552                 ifp->if_input(ifp, m);
553                 ifp->if_ipackets ++;
554         }
555         if (STAILQ_FIRST(&xferq->stfree) != NULL)
556                 fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
557         lwkt_serialize_exit(ifp->if_serializer);
558 }
559
560
561 static devclass_t fwe_devclass;
562
563 /*
564  * Because fwe is a static device that always exists under any attached
565  * firewire device, and not scanned by the firewire device, we need an 
566  * identify function to install the device.
567  */
568 static device_method_t fwe_methods[] = {
569         /* device interface */
570         DEVMETHOD(device_identify,      bus_generic_identify_sameunit),
571         DEVMETHOD(device_probe,         fwe_probe),
572         DEVMETHOD(device_attach,        fwe_attach),
573         DEVMETHOD(device_detach,        fwe_detach),
574         { 0, 0 }
575 };
576
577 static driver_t fwe_driver = {
578         "fwe",
579         fwe_methods,
580         sizeof(struct fwe_softc),
581 };
582
583
584 DECLARE_DUMMY_MODULE(fwe);
585 DRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
586 MODULE_VERSION(fwe, 1);
587 MODULE_DEPEND(fwe, firewire, 1, 1, 1);