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