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