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