Merge from vendor branch OPENSSL:
[dragonfly.git] / sys / dev / netif / axe / if_axe.c
1 /*
2  * Copyright (c) 1997, 1998, 1999, 2000-2003
3  *      Bill Paul <wpaul@windriver.com>.  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  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/dev/usb/if_axe.c,v 1.10 2003/12/08 07:54:14 obrien Exp $
33  * $DragonFly: src/sys/dev/netif/axe/if_axe.c,v 1.23 2007/07/01 21:24:02 hasso Exp $
34  */
35 /*
36  * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the
37  * LinkSys USB200M and various other adapters.
38  *
39  * Manuals available from:
40  * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
41  * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
42  * controller) to find the definitions for the RX control register.
43  * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
44  *
45  * Written by Bill Paul <wpaul@windriver.com>
46  * Senior Engineer
47  * Wind River Systems
48  */
49
50 /*
51  * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
52  * It uses an external PHY (reference designs use a RealTek chip),
53  * and has a 64-bit multicast hash filter. There is some information
54  * missing from the manual which one needs to know in order to make
55  * the chip function:
56  *
57  * - You must set bit 7 in the RX control register, otherwise the
58  *   chip won't receive any packets.
59  * - You must initialize all 3 IPG registers, or you won't be able
60  *   to send any packets.
61  *
62  * Note that this device appears to only support loading the station
63  * address via autload from the EEPROM (i.e. there's no way to manaully
64  * set it).
65  *
66  * (Adam Weinberger wanted me to name this driver if_gir.c.)
67  */
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/sockio.h>
72 #include <sys/mbuf.h>
73 #include <sys/malloc.h>
74 #include <sys/kernel.h>
75 #include <sys/socket.h>
76 #include <sys/bus.h>
77 #include <sys/thread2.h>
78
79 #include <net/if.h>
80 #include <net/ifq_var.h>
81 #include <net/if_arp.h>
82 #include <net/ethernet.h>
83 #include <net/if_dl.h>
84 #include <net/if_media.h>
85
86 #include <net/bpf.h>
87
88 #include <bus/usb/usb.h>
89 #include <bus/usb/usbdi.h>
90 #include <bus/usb/usbdi_util.h>
91 #include <bus/usb/usbdivar.h>
92 #include <bus/usb/usbdevs.h>
93 #include <bus/usb/usb_ethersubr.h>
94
95 #include "../mii_layer//mii.h"
96 #include "../mii_layer/miivar.h"
97
98 /* "controller miibus0" required.  See GENERIC if you get errors here. */
99 #include "miibus_if.h"
100
101 #include "if_axereg.h"
102
103 /*
104  * Various supported device vendors/products.
105  */
106 static struct axe_type axe_devs[] = {
107         { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172 },
108         { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100 },
109         { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1 },
110         { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M },
111         { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX },
112         { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120 },
113         { USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL },
114         { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029 },
115         { 0, 0 }
116 };
117
118 static int axe_match(device_t);
119 static int axe_attach(device_t);
120 static int axe_detach(device_t);
121
122 static int axe_tx_list_init(struct axe_softc *);
123 static int axe_rx_list_init(struct axe_softc *);
124 static int axe_newbuf(struct axe_softc *, struct axe_chain *, struct mbuf *);
125 static int axe_encap(struct axe_softc *, struct mbuf *, int);
126 static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
127 static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
128 static void axe_tick(void *);
129 static void axe_rxstart(struct ifnet *);
130 static void axe_start(struct ifnet *);
131 static int axe_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
132 static void axe_init(void *);
133 static void axe_stop(struct axe_softc *);
134 static void axe_watchdog(struct ifnet *);
135 static void axe_shutdown(device_t);
136 static int axe_miibus_readreg(device_t, int, int);
137 static int axe_miibus_writereg(device_t, int, int, int);
138 static void axe_miibus_statchg(device_t);
139 static int axe_cmd(struct axe_softc *, int, int, int, void *);
140 static int axe_ifmedia_upd(struct ifnet *);
141 static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
142
143 static void axe_setmulti(struct axe_softc *);
144
145 static device_method_t axe_methods[] = {
146         /* Device interface */
147         DEVMETHOD(device_probe,         axe_match),
148         DEVMETHOD(device_attach,        axe_attach),
149         DEVMETHOD(device_detach,        axe_detach),
150         DEVMETHOD(device_shutdown,      axe_shutdown),
151
152         /* bus interface */
153         DEVMETHOD(bus_print_child,      bus_generic_print_child),
154         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
155
156         /* MII interface */
157         DEVMETHOD(miibus_readreg,       axe_miibus_readreg),
158         DEVMETHOD(miibus_writereg,      axe_miibus_writereg),
159         DEVMETHOD(miibus_statchg,       axe_miibus_statchg),
160
161         { 0, 0 }
162 };
163
164 static driver_t axe_driver = {
165         "axe",
166         axe_methods,
167         sizeof(struct axe_softc)
168 };
169
170 static devclass_t axe_devclass;
171
172 DRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, usbd_driver_load, 0);
173 DRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
174 MODULE_DEPEND(axe, usb, 1, 1, 1);
175 MODULE_DEPEND(axe, miibus, 1, 1, 1);
176
177 static int
178 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
179 {
180         usb_device_request_t    req;
181         usbd_status             err;
182
183         if (sc->axe_dying)
184                 return(0);
185
186         if (AXE_CMD_DIR(cmd))
187                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
188         else
189                 req.bmRequestType = UT_READ_VENDOR_DEVICE;
190         req.bRequest = AXE_CMD_CMD(cmd);
191         USETW(req.wValue, val);
192         USETW(req.wIndex, index);
193         USETW(req.wLength, AXE_CMD_LEN(cmd));
194
195         err = usbd_do_request(sc->axe_udev, &req, buf);
196
197         if (err)
198                 return(-1);
199
200         return(0);
201 }
202
203 static int
204 axe_miibus_readreg(device_t dev, int phy, int reg)
205 {
206         struct axe_softc        *sc = device_get_softc(dev);
207         usbd_status             err;
208         u_int16_t               val;
209
210         if (sc->axe_dying) {
211                 return(0);
212         }
213
214 #ifdef notdef
215         /*
216          * The chip tells us the MII address of any supported
217          * PHYs attached to the chip, so only read from those.
218          */
219
220         if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0]) {
221                 return (0);
222         }
223
224         if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1]) {
225                 return (0);
226         }
227 #endif
228         if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy) {
229                 return (0);
230         }
231
232         axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
233         err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
234         axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
235
236         if (err) {
237                 if_printf(&sc->arpcom.ac_if, "read PHY failed\n");
238                 return(-1);
239         }
240
241         if (val)
242                 sc->axe_phyaddrs[0] = phy;
243
244         return (val);
245 }
246
247 static int
248 axe_miibus_writereg(device_t dev, int phy, int reg, int val)
249 {
250         struct axe_softc        *sc = device_get_softc(dev);
251         usbd_status             err;
252
253         if (sc->axe_dying) {
254                 return(0);
255         }
256
257         axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
258         err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
259         axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
260
261         if (err) {
262                 if_printf(&sc->arpcom.ac_if, "write PHY failed\n");
263                 return(-1);
264         }
265
266         return (0);
267 }
268
269 static void
270 axe_miibus_statchg(device_t dev)
271 {
272 #ifdef notdef
273         struct axe_softc        *sc = device_get_softc(dev);
274         struct mii_data         *mii = GET_MII(sc);
275 #endif
276         /* doesn't seem to be necessary */
277
278         return;
279 }
280
281 /*
282  * Set media options.
283  */
284 static int
285 axe_ifmedia_upd(struct ifnet *ifp)
286 {
287         struct axe_softc        *sc = ifp->if_softc;
288         struct mii_data         *mii = GET_MII(sc);
289
290         sc->axe_link = 0;
291         if (mii->mii_instance) {
292                 struct mii_softc        *miisc;
293                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
294                          mii_phy_reset(miisc);
295         }
296         mii_mediachg(mii);
297
298         return (0);
299 }
300
301 /*
302  * Report current media status.
303  */
304 static void
305 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
306 {
307         struct axe_softc        *sc = ifp->if_softc;
308         struct mii_data         *mii = GET_MII(sc);
309
310         mii_pollstat(mii);
311         ifmr->ifm_active = mii->mii_media_active;
312         ifmr->ifm_status = mii->mii_media_status;
313
314         return;
315 }
316
317 static void
318 axe_setmulti(struct axe_softc *sc)
319 {
320         struct ifnet *ifp = &sc->arpcom.ac_if;
321         struct ifmultiaddr      *ifma;
322         u_int32_t               h = 0;
323         u_int16_t               rxmode;
324         u_int8_t                hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
325
326         axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
327
328         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
329                 rxmode |= AXE_RXCMD_ALLMULTI;
330                 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
331                 return;
332         } else
333                 rxmode &= ~AXE_RXCMD_ALLMULTI;
334
335         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
336                 if (ifma->ifma_addr->sa_family != AF_LINK)
337                         continue;
338                 h = ether_crc32_be(
339                         LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
340                         ETHER_ADDR_LEN);
341                 /* the filter bit position */
342                 h = (h >> 26) & 0x0000003F;
343                 hashtbl[h / 8] |= 1 << (h % 8);
344         }
345
346         axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
347         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
348 }
349
350 static void
351 axe_reset(struct axe_softc *sc)
352 {
353         if (sc->axe_dying)
354                 return;
355
356         if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1) ||
357             usbd_device2interface_handle(sc->axe_udev, AXE_IFACE_IDX,
358             &sc->axe_iface)) {
359                 if_printf(&sc->arpcom.ac_if,
360                           "getting interface handle failed\n");
361         }
362
363         /* Wait a little while for the chip to get its brains in order. */
364         DELAY(1000);
365         return;
366 }
367
368 /*
369  * Probe for a AX88172 chip.
370  */
371 static int
372 axe_match(device_t self)
373 {
374         struct usb_attach_arg *uaa = device_get_ivars(self);
375         struct axe_type                 *t;
376
377         if (!uaa->iface)
378                 return(UMATCH_NONE);
379
380         t = axe_devs;
381         while(t->axe_vid) {
382                 if (uaa->vendor == t->axe_vid &&
383                     uaa->product == t->axe_did) {
384                         return(UMATCH_VENDOR_PRODUCT);
385                 }
386                 t++;
387         }
388
389         return(UMATCH_NONE);
390 }
391
392 /*
393  * Attach the interface. Allocate softc structures, do ifmedia
394  * setup and ethernet/BPF attach.
395  */
396 static int
397 axe_attach(device_t self)
398 {
399         struct axe_softc *sc = device_get_softc(self);
400         struct usb_attach_arg *uaa = device_get_ivars(self);
401         char                    devinfo[1024];
402         u_char                  eaddr[ETHER_ADDR_LEN];
403         struct ifnet            *ifp;
404         usb_interface_descriptor_t      *id;
405         usb_endpoint_descriptor_t       *ed;
406         int                     i;
407
408         sc->axe_udev = uaa->device;
409         callout_init(&sc->axe_stat_timer);
410
411         if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1)) {
412                 device_printf(self, "setting config no %d failed\n",
413                     AXE_CONFIG_NO);
414                 return ENXIO;
415         }
416
417         if (usbd_device2interface_handle(uaa->device,
418             AXE_IFACE_IDX, &sc->axe_iface)) {
419                 device_printf(self, "getting interface handle failed\n");
420                 return ENXIO;
421         }
422
423         id = usbd_get_interface_descriptor(sc->axe_iface);
424
425         usbd_devinfo(uaa->device, 0, devinfo);
426         device_set_desc_copy(self, devinfo);
427         device_printf(self, "%s\n", devinfo);
428
429         /* Find endpoints. */
430         for (i = 0; i < id->bNumEndpoints; i++) {
431                 ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
432                 if (!ed) {
433                         device_printf(self, "couldn't get ep %d\n", i);
434                         return ENXIO;
435                 }
436                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
437                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
438                         sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
439                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
440                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
441                         sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
442                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
443                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
444                         sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
445                 }
446         }
447
448         /*
449          * Get station address.
450          */
451         axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
452
453         /*
454          * Load IPG values and PHY indexes.
455          */
456         axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
457         axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
458
459         /*
460          * Work around broken adapters that appear to lie about
461          * their PHY addresses.
462          */
463         sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
464
465         ifp = &sc->arpcom.ac_if;
466         ifp->if_softc = sc;
467         if_initname(ifp, device_get_name(self), device_get_unit(self));
468         ifp->if_mtu = ETHERMTU;
469         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
470         ifp->if_ioctl = axe_ioctl;
471         ifp->if_start = axe_start;
472         ifp->if_watchdog = axe_watchdog;
473         ifp->if_init = axe_init;
474         ifp->if_baudrate = 10000000;
475         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
476         ifq_set_ready(&ifp->if_snd);
477
478         if (mii_phy_probe(self, &sc->axe_miibus,
479             axe_ifmedia_upd, axe_ifmedia_sts)) {
480                 device_printf(self, "MII without any PHY!\n");
481                 return ENXIO;
482         }
483
484         /*
485          * Call MI attach routine.
486          */
487
488         ether_ifattach(ifp, eaddr, NULL);
489
490         sc->axe_dying = 0;
491
492         usb_register_netisr();
493
494         return 0;
495 }
496
497 static int
498 axe_detach(device_t dev)
499 {
500         struct axe_softc *sc = device_get_softc(dev);
501         struct ifnet *ifp = &sc->arpcom.ac_if;
502
503         sc->axe_dying = 1;
504         callout_stop(&sc->axe_stat_timer);
505         ether_ifdetach(ifp);
506
507         if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
508                 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
509         if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
510                 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
511         if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
512                 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
513
514         return(0);
515 }
516
517 /*
518  * Initialize an RX descriptor and attach an MBUF cluster.
519  */
520 static int
521 axe_newbuf(struct axe_softc *sc, struct axe_chain *c, struct mbuf *m)
522 {
523         struct mbuf             *m_new = NULL;
524
525         if (m == NULL) {
526                 m_new = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
527                 if (m_new == NULL) {
528                         if_printf(&sc->arpcom.ac_if, "no memory for rx list "
529                             "-- packet dropped!\n");
530                         return(ENOBUFS);
531                 }
532                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
533         } else {
534                 m_new = m;
535                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
536                 m_new->m_data = m_new->m_ext.ext_buf;
537         }
538
539         m_adj(m_new, ETHER_ALIGN);
540         c->axe_mbuf = m_new;
541
542         return(0);
543 }
544
545 static int
546 axe_rx_list_init(struct axe_softc *sc)
547 {
548         struct axe_cdata        *cd;
549         struct axe_chain        *c;
550         int                     i;
551
552         cd = &sc->axe_cdata;
553         for (i = 0; i < AXE_RX_LIST_CNT; i++) {
554                 c = &cd->axe_rx_chain[i];
555                 c->axe_sc = sc;
556                 c->axe_idx = i;
557                 if (axe_newbuf(sc, c, NULL) == ENOBUFS)
558                         return(ENOBUFS);
559                 if (c->axe_xfer == NULL) {
560                         c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
561                         if (c->axe_xfer == NULL)
562                                 return(ENOBUFS);
563                 }
564         }
565
566         return(0);
567 }
568
569 static int
570 axe_tx_list_init(struct axe_softc *sc)
571 {
572         struct axe_cdata        *cd;
573         struct axe_chain        *c;
574         int                     i;
575
576         cd = &sc->axe_cdata;
577         for (i = 0; i < AXE_TX_LIST_CNT; i++) {
578                 c = &cd->axe_tx_chain[i];
579                 c->axe_sc = sc;
580                 c->axe_idx = i;
581                 c->axe_mbuf = NULL;
582                 if (c->axe_xfer == NULL) {
583                         c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
584                         if (c->axe_xfer == NULL)
585                                 return(ENOBUFS);
586                 }
587                 c->axe_buf = kmalloc(AXE_BUFSZ, M_USBDEV, M_WAITOK);
588                 if (c->axe_buf == NULL)
589                         return(ENOBUFS);
590         }
591
592         return(0);
593 }
594
595 static void
596 axe_rxstart(struct ifnet *ifp)
597 {
598         struct axe_softc *sc = ifp->if_softc;
599         struct axe_chain *c;
600
601         c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
602
603         if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
604                 ifp->if_ierrors++;
605                 return;
606         }
607
608         /* Setup new transfer. */
609         usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
610             c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
611             USBD_NO_TIMEOUT, axe_rxeof);
612         usbd_transfer(c->axe_xfer);
613 }
614
615 /*
616  * A frame has been uploaded: pass the resulting mbuf chain up to
617  * the higher level protocols.
618  */
619 static void
620 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
621 {
622         struct axe_chain *c = priv;
623         struct axe_softc *sc = c->axe_sc;
624         struct ifnet *ifp = &sc->arpcom.ac_if;
625         struct mbuf *m;
626         int total_len = 0;
627
628         lwkt_serialize_enter(ifp->if_serializer);
629
630         if (!(ifp->if_flags & IFF_RUNNING)) {
631                 lwkt_serialize_exit(ifp->if_serializer);
632                 return;
633         }
634
635         if (status != USBD_NORMAL_COMPLETION) {
636                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
637                         lwkt_serialize_exit(ifp->if_serializer);
638                         return;
639                 }
640                 if (usbd_ratecheck(&sc->axe_rx_notice)) {
641                         if_printf(ifp, "usb error on rx: %s\n",
642                             usbd_errstr(status));
643                 }
644                 if (status == USBD_STALLED)
645                         usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_RX]);
646                 goto done;
647         }
648
649         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
650
651         m = c->axe_mbuf;
652
653         if (total_len < sizeof(struct ether_header)) {
654                 ifp->if_ierrors++;
655                 goto done;
656         }
657
658         ifp->if_ipackets++;
659         m->m_pkthdr.rcvif = ifp;
660         m->m_pkthdr.len = m->m_len = total_len;
661
662         /* Put the packet on the special USB input queue. */
663         usb_ether_input(m);
664         axe_rxstart(ifp);
665         lwkt_serialize_exit(ifp->if_serializer);
666         return;
667 done:
668         /* Setup new transfer. */
669         usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
670             c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
671             USBD_NO_TIMEOUT, axe_rxeof);
672         usbd_transfer(c->axe_xfer);
673         lwkt_serialize_exit(ifp->if_serializer);
674 }
675
676 /*
677  * A frame was downloaded to the chip. It's safe for us to clean up
678  * the list buffers.
679  */
680
681 static void
682 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
683 {
684         struct axe_chain *c = priv;
685         struct axe_softc *sc = c->axe_sc;
686         struct ifnet *ifp = &sc->arpcom.ac_if;
687         usbd_status err;
688
689         lwkt_serialize_enter(ifp->if_serializer);
690         if (status != USBD_NORMAL_COMPLETION) {
691                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
692                         lwkt_serialize_exit(ifp->if_serializer);
693                         return;
694                 }
695                 if_printf(ifp, "usb error on tx: %s\n", usbd_errstr(status));
696                 if (status == USBD_STALLED)
697                         usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_TX]);
698                 lwkt_serialize_exit(ifp->if_serializer);
699                 return;
700         }
701
702         ifp->if_timer = 0;
703         ifp->if_flags &= ~IFF_OACTIVE;
704         usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &err);
705
706         if (c->axe_mbuf != NULL) {
707                 m_freem(c->axe_mbuf);
708                 c->axe_mbuf = NULL;
709         }
710
711         if (err)
712                 ifp->if_oerrors++;
713         else
714                 ifp->if_opackets++;
715
716         if (!ifq_is_empty(&ifp->if_snd))
717                 (*ifp->if_start)(ifp);
718         lwkt_serialize_exit(ifp->if_serializer);
719 }
720
721 static void
722 axe_tick(void *xsc)
723 {
724         struct axe_softc *sc = xsc;
725         struct ifnet *ifp = &sc->arpcom.ac_if;
726         struct mii_data         *mii;
727
728         mii = GET_MII(sc);
729         if (mii == NULL)
730                 return;
731
732         lwkt_serialize_enter(ifp->if_serializer);
733         mii_tick(mii);
734         if (!sc->axe_link && mii->mii_media_status & IFM_ACTIVE &&
735             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
736                 sc->axe_link++;
737                 if (!ifq_is_empty(&ifp->if_snd))
738                         axe_start(ifp);
739         }
740         callout_reset(&sc->axe_stat_timer, hz, axe_tick, sc);
741         lwkt_serialize_exit(ifp->if_serializer);
742 }
743
744 static int
745 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
746 {
747         struct axe_chain        *c;
748         usbd_status             err;
749
750         c = &sc->axe_cdata.axe_tx_chain[idx];
751
752         /*
753          * Copy the mbuf data into a contiguous buffer, leaving two
754          * bytes at the beginning to hold the frame length.
755          */
756         m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
757         c->axe_mbuf = m;
758
759         usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
760             c, c->axe_buf, m->m_pkthdr.len, USBD_FORCE_SHORT_XFER,
761             10000, axe_txeof);
762
763         /* Transmit */
764         err = usbd_transfer(c->axe_xfer);
765         if (err != USBD_IN_PROGRESS) {
766                 axe_stop(sc);
767                 return(EIO);
768         }
769
770         sc->axe_cdata.axe_tx_cnt++;
771
772         return(0);
773 }
774
775 static void
776 axe_start(struct ifnet *ifp)
777 {
778         struct axe_softc *sc = ifp->if_softc;
779         struct mbuf *m_head = NULL;
780
781         if (!sc->axe_link) {
782                 return;
783         }
784
785         if (ifp->if_flags & IFF_OACTIVE) {
786                 return;
787         }
788
789         m_head = ifq_poll(&ifp->if_snd);
790         if (m_head == NULL) {
791                 return;
792         }
793
794         if (axe_encap(sc, m_head, 0)) {
795                 ifp->if_flags |= IFF_OACTIVE;
796                 return;
797         }
798         ifq_dequeue(&ifp->if_snd, m_head);
799
800         /*
801          * If there's a BPF listener, bounce a copy of this frame
802          * to him.
803          */
804         BPF_MTAP(ifp, m_head);
805
806         ifp->if_flags |= IFF_OACTIVE;
807
808         /*
809          * Set a timeout in case the chip goes out to lunch.
810          */
811         ifp->if_timer = 5;
812 }
813
814 static void
815 axe_init(void *xsc)
816 {
817         struct axe_softc        *sc = xsc;
818         struct ifnet            *ifp = &sc->arpcom.ac_if;
819         struct axe_chain        *c;
820         usbd_status             err;
821         int i, rxmode;
822
823         if (ifp->if_flags & IFF_RUNNING) {
824                 return;
825         }
826
827         /*
828          * Cancel pending I/O and free all RX/TX buffers.
829          */
830
831         axe_reset(sc);
832
833 #ifdef notdef
834         /* Set MAC address */
835         axe_mac(sc, sc->arpcom.ac_enaddr, 1);
836 #endif
837
838         /* Enable RX logic. */
839
840         /* Init TX ring. */
841         if (axe_tx_list_init(sc) == ENOBUFS) {
842                 if_printf(ifp, "tx list init failed\n");
843                 return;
844         }
845
846         /* Init RX ring. */
847         if (axe_rx_list_init(sc) == ENOBUFS) {
848                 if_printf(ifp, "rx list init failed\n");
849                 return;
850         }
851
852         /* Set transmitter IPG values */
853         axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
854         axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
855         axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
856
857         /* Enable receiver, set RX mode */
858         rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
859
860         /* If we want promiscuous mode, set the allframes bit. */
861         if (ifp->if_flags & IFF_PROMISC)
862                 rxmode |= AXE_RXCMD_PROMISC;
863
864         if (ifp->if_flags & IFF_BROADCAST)
865                 rxmode |= AXE_RXCMD_BROADCAST;
866
867         axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
868
869         /* Load the multicast filter. */
870         axe_setmulti(sc);
871
872         /* Open RX and TX pipes. */
873         err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
874             USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
875         if (err) {
876                 if_printf(ifp, "open rx pipe failed: %s\n", usbd_errstr(err));
877                 return;
878         }
879
880         err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
881             USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
882         if (err) {
883                 if_printf(ifp, "open tx pipe failed: %s\n", usbd_errstr(err));
884                 return;
885         }
886
887         /* Start up the receive pipe. */
888         for (i = 0; i < AXE_RX_LIST_CNT; i++) {
889                 c = &sc->axe_cdata.axe_rx_chain[i];
890                 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
891                     c, mtod(c->axe_mbuf, char *), AXE_BUFSZ,
892                     USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
893                 usbd_transfer(c->axe_xfer);
894         }
895
896         ifp->if_flags |= IFF_RUNNING;
897         ifp->if_flags &= ~IFF_OACTIVE;
898
899         callout_reset(&sc->axe_stat_timer, hz, axe_tick, sc);
900 }
901
902 static int
903 axe_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
904 {
905         struct axe_softc        *sc = ifp->if_softc;
906         struct ifreq            *ifr = (struct ifreq *)data;
907         struct mii_data         *mii;
908         u_int16_t               rxmode;
909         int error = 0;
910
911         switch(command) {
912         case SIOCSIFFLAGS:
913                 if (ifp->if_flags & IFF_UP) {
914                         if (ifp->if_flags & IFF_RUNNING &&
915                             ifp->if_flags & IFF_PROMISC &&
916                             !(sc->axe_if_flags & IFF_PROMISC)) {
917                                 axe_cmd(sc, AXE_CMD_RXCTL_READ,
918                                         0, 0, (void *)&rxmode);
919                                 rxmode |= AXE_RXCMD_PROMISC;
920                                 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
921                                         0, rxmode, NULL);
922                                 axe_setmulti(sc);
923                         } else if (ifp->if_flags & IFF_RUNNING &&
924                             !(ifp->if_flags & IFF_PROMISC) &&
925                             sc->axe_if_flags & IFF_PROMISC) {
926                                 axe_cmd(sc, AXE_CMD_RXCTL_READ,
927                                         0, 0, (void *)&rxmode);
928                                 rxmode &= ~AXE_RXCMD_PROMISC;
929                                 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
930                                         0, rxmode, NULL);
931                                 axe_setmulti(sc);
932                         } else if (!(ifp->if_flags & IFF_RUNNING))
933                                 axe_init(sc);
934                 } else {
935                         if (ifp->if_flags & IFF_RUNNING)
936                                 axe_stop(sc);
937                 }
938                 sc->axe_if_flags = ifp->if_flags;
939                 error = 0;
940                 break;
941         case SIOCADDMULTI:
942         case SIOCDELMULTI:
943                 axe_setmulti(sc);
944                 error = 0;
945                 break;
946         case SIOCGIFMEDIA:
947         case SIOCSIFMEDIA:
948                 mii = GET_MII(sc);
949                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
950                 break;
951
952         default:
953                 error = ether_ioctl(ifp, command, data);
954                 break;
955         }
956         return(error);
957 }
958
959 static void
960 axe_watchdog(struct ifnet *ifp)
961 {
962         struct axe_softc *sc = ifp->if_softc;
963         struct axe_chain *c;
964         usbd_status stat;
965
966         ifp->if_oerrors++;
967         if_printf(ifp, "watchdog timeout\n");
968
969         c = &sc->axe_cdata.axe_tx_chain[0];
970         usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
971         axe_txeof(c->axe_xfer, c, stat);
972
973         if (!ifq_is_empty(&ifp->if_snd))
974                 axe_start(ifp);
975 }
976
977 /*
978  * Stop the adapter and free any mbufs allocated to the
979  * RX and TX lists.
980  */
981 static void
982 axe_stop(struct axe_softc *sc)
983 {
984         usbd_status             err;
985         struct ifnet            *ifp;
986         int i;
987
988         ifp = &sc->arpcom.ac_if;
989         ifp->if_timer = 0;
990
991         callout_stop(&sc->axe_stat_timer);
992
993         /* Stop transfers. */
994         if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
995                 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
996                 if (err) {
997                         if_printf(ifp, "abort rx pipe failed: %s\n",
998                             usbd_errstr(err));
999                 }
1000                 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1001                 if (err) {
1002                         if_printf(ifp, "close rx pipe failed: %s\n",
1003                             usbd_errstr(err));
1004                 }
1005                 sc->axe_ep[AXE_ENDPT_RX] = NULL;
1006         }
1007
1008         if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1009                 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1010                 if (err) {
1011                         if_printf(ifp, "abort tx pipe failed: %s\n",
1012                             usbd_errstr(err));
1013                 }
1014                 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1015                 if (err) {
1016                         if_printf(ifp, "close tx pipe failed: %s\n",
1017                             usbd_errstr(err));
1018                 }
1019                 sc->axe_ep[AXE_ENDPT_TX] = NULL;
1020         }
1021
1022         if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1023                 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1024                 if (err) {
1025                         if_printf(ifp, "abort intr pipe failed: %s\n",
1026                             usbd_errstr(err));
1027                 }
1028                 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1029                 if (err) {
1030                         if_printf(ifp, "close intr pipe failed: %s\n",
1031                             usbd_errstr(err));
1032                 }
1033                 sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1034         }
1035
1036         axe_reset(sc);
1037
1038         /* Free RX resources. */
1039         for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1040                 if (sc->axe_cdata.axe_rx_chain[i].axe_buf != NULL) {
1041                         kfree(sc->axe_cdata.axe_rx_chain[i].axe_buf, M_USBDEV);
1042                         sc->axe_cdata.axe_rx_chain[i].axe_buf = NULL;
1043                 }
1044                 if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
1045                         m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
1046                         sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
1047                 }
1048                 if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1049                         usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1050                         sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1051                 }
1052         }
1053
1054         /* Free TX resources. */
1055         for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1056                 if (sc->axe_cdata.axe_tx_chain[i].axe_buf != NULL) {
1057                         kfree(sc->axe_cdata.axe_tx_chain[i].axe_buf, M_USBDEV);
1058                         sc->axe_cdata.axe_tx_chain[i].axe_buf = NULL;
1059                 }
1060                 if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
1061                         m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
1062                         sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
1063                 }
1064                 if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1065                         usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1066                         sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1067                 }
1068         }
1069
1070         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1071         sc->axe_link = 0;
1072 }
1073
1074 /*
1075  * Stop all chip I/O so that the kernel's probe routines don't
1076  * get confused by errant DMAs when rebooting.
1077  */
1078 static void
1079 axe_shutdown(device_t dev)
1080 {
1081         struct axe_softc        *sc;
1082
1083         sc = device_get_softc(dev);
1084
1085         axe_stop(sc);
1086 }