2 * Copyright (c) 1997, 1998, 1999, 2000-2003
3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * ASIX Electronics AX88172/AX88178/AX88778 USB 2.0 ethernet driver.
38 * Used in the LinkSys USB200M and various other adapters.
40 * Manuals available from:
41 * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
42 * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
43 * controller) to find the definitions for the RX control register.
44 * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
46 * Written by Bill Paul <wpaul@windriver.com>
52 * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
53 * It uses an external PHY (reference designs use a RealTek chip),
54 * and has a 64-bit multicast hash filter. There is some information
55 * missing from the manual which one needs to know in order to make
58 * - You must set bit 7 in the RX control register, otherwise the
59 * chip won't receive any packets.
60 * - You must initialize all 3 IPG registers, or you won't be able
61 * to send any packets.
63 * Note that this device appears to only support loading the station
64 * address via autload from the EEPROM (i.e. there's no way to manaully
67 * (Adam Weinberger wanted me to name this driver if_gir.c.)
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/endian.h>
73 #include <sys/sockio.h>
75 #include <sys/malloc.h>
77 #include <sys/kernel.h>
78 #include <sys/module.h>
79 #include <sys/socket.h>
83 #include <net/if_arp.h>
84 #include <net/ethernet.h>
85 #include <net/if_dl.h>
86 #include <net/if_media.h>
87 #include <net/if_types.h>
92 #include <machine/bus.h>
94 #include <dev/usb/usb.h>
95 #include <dev/usb/usbdi.h>
96 #include <dev/usb/usbdi_util.h>
97 #include <dev/usb/usbdivar.h>
99 #include <dev/usb/usb_ethersubr.h>
101 #include <dev/mii/mii.h>
102 #include <dev/mii/miivar.h>
104 /* "device miibus" required. See GENERIC if you get errors here. */
105 #include "miibus_if.h"
108 * AXE_178_MAX_FRAME_BURST
109 * max frame burst size for Ax88178 and Ax88772
114 * use the largest your system can handle without usb stalling.
116 * NB: 88772 parts appear to generate lots of input errors with
117 * a 2K rx buffer and 8K is only slightly faster than 4K on an
118 * EHCI port on a T42 so change at your own risk.
120 #define AXE_178_MAX_FRAME_BURST 1
122 #include <dev/usb/if_axereg.h>
125 * Various supported device vendors/products.
127 const struct axe_type axe_devs[] = {
128 { { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UF200}, 0 },
129 { { USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2}, 0 },
130 { { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ETHERNET}, AX772 },
131 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172}, 0 },
132 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772}, AX772 },
133 { { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88178}, AX178 },
134 { { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC210T}, 0 },
135 { { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D5055 }, AX178 },
136 { { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB2AR}, 0},
137 { { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_USB200MV2}, AX772 },
138 { { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
139 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100}, 0 },
140 { { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100B1 }, AX772 },
141 { { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E}, 0 },
142 { { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETGUS2 }, AX178 },
143 { { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1}, 0 },
144 { { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M}, 0 },
145 { { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_USB1000 }, AX178 },
146 { { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
147 { { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120}, 0 },
148 { { USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01PLUS }, AX772 },
149 { { USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GU1000T }, AX178 },
150 { { USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
151 { { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029}, 0 },
152 { { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN028 }, AX178 }
155 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p))
157 static device_probe_t axe_match;
158 static device_attach_t axe_attach;
159 static device_detach_t axe_detach;
160 static device_shutdown_t axe_shutdown;
161 static miibus_readreg_t axe_miibus_readreg;
162 static miibus_writereg_t axe_miibus_writereg;
163 static miibus_statchg_t axe_miibus_statchg;
165 static int axe_encap(struct axe_softc *, struct mbuf *, int);
166 static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
167 static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
168 static void axe_tick(void *);
169 static void axe_tick_task(void *);
170 static void axe_start(struct ifnet *);
171 static int axe_ioctl(struct ifnet *, u_long, caddr_t);
172 static void axe_init(void *);
173 static void axe_stop(struct axe_softc *);
174 static void axe_watchdog(struct ifnet *);
175 static int axe_cmd(struct axe_softc *, int, int, int, void *);
176 static int axe_ifmedia_upd(struct ifnet *);
177 static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
179 static void axe_setmulti(struct axe_softc *);
181 static device_method_t axe_methods[] = {
182 /* Device interface */
183 DEVMETHOD(device_probe, axe_match),
184 DEVMETHOD(device_attach, axe_attach),
185 DEVMETHOD(device_detach, axe_detach),
186 DEVMETHOD(device_shutdown, axe_shutdown),
189 DEVMETHOD(bus_print_child, bus_generic_print_child),
190 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
193 DEVMETHOD(miibus_readreg, axe_miibus_readreg),
194 DEVMETHOD(miibus_writereg, axe_miibus_writereg),
195 DEVMETHOD(miibus_statchg, axe_miibus_statchg),
200 static driver_t axe_driver = {
203 sizeof(struct axe_softc)
206 static devclass_t axe_devclass;
208 DRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, usbd_driver_load, 0);
209 DRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
210 MODULE_DEPEND(axe, usb, 1, 1, 1);
211 MODULE_DEPEND(axe, miibus, 1, 1, 1);
214 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
216 usb_device_request_t req;
219 AXE_SLEEPLOCKASSERT(sc);
223 if (AXE_CMD_DIR(cmd))
224 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
226 req.bmRequestType = UT_READ_VENDOR_DEVICE;
227 req.bRequest = AXE_CMD_CMD(cmd);
228 USETW(req.wValue, val);
229 USETW(req.wIndex, index);
230 USETW(req.wLength, AXE_CMD_LEN(cmd));
232 err = usbd_do_request(sc->axe_udev, &req, buf);
241 axe_miibus_readreg(device_t dev, int phy, int reg)
243 struct axe_softc *sc = device_get_softc(dev);
250 AXE_SLEEPLOCKASSERT(sc);
253 * The chip tells us the MII address of any supported
254 * PHYs attached to the chip, so only read from those.
257 if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0])
260 if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1])
263 if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy)
267 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
268 err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
269 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
273 device_printf(sc->axe_dev, "read PHY failed\n");
277 if (val && val != 0xffff)
278 sc->axe_phyaddrs[0] = phy;
280 return (le16toh(val));
284 axe_miibus_writereg(device_t dev, int phy, int reg, int val)
286 struct axe_softc *sc = device_get_softc(dev);
292 AXE_SLEEPLOCKASSERT(sc);
294 axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
296 err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
297 axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
301 device_printf(sc->axe_dev, "write PHY failed\n");
309 axe_miibus_statchg(device_t dev)
311 struct axe_softc *sc = device_get_softc(dev);
312 struct mii_data *mii = GET_MII(sc);
315 val = (mii->mii_media_active & IFM_GMASK) == IFM_FDX ?
316 AXE_MEDIA_FULL_DUPLEX : 0;
317 if (sc->axe_flags & (AX178|AX772)) {
318 val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
320 switch (IFM_SUBTYPE(mii->mii_media_active)) {
322 val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
325 val |= AXE_178_MEDIA_100TX;
328 /* doesn't need to be handled */
332 err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
334 device_printf(dev, "media change failed, error %d\n", err);
341 axe_ifmedia_upd(struct ifnet *ifp)
343 struct axe_softc *sc = ifp->if_softc;
344 struct mii_data *mii = GET_MII(sc);
347 if (mii->mii_instance) {
348 struct mii_softc *miisc;
349 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
350 mii_phy_reset(miisc);
358 * Report current media status.
361 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
363 struct axe_softc *sc = ifp->if_softc;
364 struct mii_data *mii = GET_MII(sc);
367 ifmr->ifm_active = mii->mii_media_active;
368 ifmr->ifm_status = mii->mii_media_status;
374 axe_setmulti(struct axe_softc *sc)
377 struct ifmultiaddr *ifma;
380 u_int8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
385 axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
386 rxmode = le16toh(rxmode);
388 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
389 rxmode |= AXE_RXCMD_ALLMULTI;
390 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
394 rxmode &= ~AXE_RXCMD_ALLMULTI;
397 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
399 if (ifma->ifma_addr->sa_family != AF_LINK)
401 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
402 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
403 hashtbl[h / 8] |= 1 << (h % 8);
407 axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
408 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
415 axe_ax88178_init(struct axe_softc *sc)
417 int gpio0 = 0, phymode = 0;
420 axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
422 axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
423 eeprom = le16toh(eeprom);
424 axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
426 /* if EEPROM is invalid we have to use to GPIO0 */
427 if (eeprom == 0xffff) {
431 phymode = eeprom & 7;
432 gpio0 = (eeprom & 0x80) ? 0 : 1;
435 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x008c, NULL);
436 usbd_delay_ms(sc->axe_udev, 40);
437 if ((eeprom >> 8) != 1) {
438 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
439 usbd_delay_ms(sc->axe_udev, 30);
441 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x001c, NULL);
442 usbd_delay_ms(sc->axe_udev, 300);
444 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
445 usbd_delay_ms(sc->axe_udev, 30);
447 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x0004, NULL);
448 usbd_delay_ms(sc->axe_udev, 30);
449 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x000c, NULL);
450 usbd_delay_ms(sc->axe_udev, 30);
454 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, 0, NULL);
455 usbd_delay_ms(sc->axe_udev, 150);
456 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
457 AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
458 usbd_delay_ms(sc->axe_udev, 150);
459 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
463 axe_ax88772_init(struct axe_softc *sc)
465 axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
466 usbd_delay_ms(sc->axe_udev, 40);
468 if (sc->axe_phyaddrs[1] == AXE_INTPHY) {
469 /* ask for embedded PHY */
470 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
471 usbd_delay_ms(sc->axe_udev, 10);
473 /* power down and reset state, pin reset state */
474 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
475 usbd_delay_ms(sc->axe_udev, 60);
477 /* power down/reset state, pin operating state */
478 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
479 AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
480 usbd_delay_ms(sc->axe_udev, 150);
482 /* power up, reset */
483 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
485 /* power up, operating */
486 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
487 AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
489 /* ask for external PHY */
490 axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
491 usbd_delay_ms(sc->axe_udev, 10);
493 /* power down/reset state, pin operating state */
494 axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
495 AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
498 usbd_delay_ms(sc->axe_udev, 150);
499 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
503 axe_reset(struct axe_softc *sc)
508 if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1) ||
509 usbd_device2interface_handle(sc->axe_udev, AXE_IFACE_IDX,
511 device_printf(sc->axe_dev, "getting interface handle failed\n");
514 /* Wait a little while for the chip to get its brains in order. */
520 * Probe for a AX88172 chip.
523 axe_match(device_t self)
525 struct usb_attach_arg *uaa = device_get_ivars(self);
529 return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
530 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
534 * Attach the interface. Allocate softc structures, do ifmedia
535 * setup and ethernet/BPF attach.
538 axe_attach(device_t self)
540 struct axe_softc *sc = device_get_softc(self);
541 struct usb_attach_arg *uaa = device_get_ivars(self);
542 const struct axe_type *type;
543 u_char eaddr[ETHER_ADDR_LEN];
545 usb_interface_descriptor_t *id;
546 usb_endpoint_descriptor_t *ed;
549 sc->axe_udev = uaa->device;
551 type = axe_lookup(uaa->vendor, uaa->product);
553 sc->axe_flags = type->axe_flags;
555 if (usbd_set_config_no(sc->axe_udev, AXE_CONFIG_NO, 1)) {
556 device_printf(sc->axe_dev, "getting interface handle failed\n");
560 usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
562 if (usbd_device2interface_handle(uaa->device,
563 AXE_IFACE_IDX, &sc->axe_iface)) {
564 device_printf(sc->axe_dev, "getting interface handle failed\n");
568 sc->axe_boundary = 64;
569 if (sc->axe_flags & (AX178|AX772)) {
570 if (sc->axe_udev->speed == USB_SPEED_HIGH) {
571 sc->axe_bufsz = AXE_178_MAX_BUFSZ;
572 sc->axe_boundary = 512;
574 sc->axe_bufsz = AXE_178_MIN_BUFSZ;
576 sc->axe_bufsz = AXE_172_BUFSZ;
578 device_printf(sc->axe_dev, "%s, bufsz %d, boundary %d\n",
579 sc->axe_flags & AX178 ? "AX88178" :
580 sc->axe_flags & AX772 ? "AX88772" : "AX88172",
581 sc->axe_bufsz, sc->axe_boundary);
584 id = usbd_get_interface_descriptor(sc->axe_iface);
586 /* Find endpoints. */
587 for (i = 0; i < id->bNumEndpoints; i++) {
588 ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
590 device_printf(sc->axe_dev, "couldn't get ep %d\n", i);
593 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
594 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
595 sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
596 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
597 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
598 sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
599 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
600 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
601 sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
605 mtx_init(&sc->axe_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
606 MTX_DEF | MTX_RECURSE);
607 sx_init(&sc->axe_sleeplock, device_get_nameunit(self));
611 /* We need the PHYID for the init dance in some cases */
612 axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
614 if (sc->axe_flags & AX178)
615 axe_ax88178_init(sc);
616 else if (sc->axe_flags & AX772)
617 axe_ax88772_init(sc);
620 * Get station address.
622 if (sc->axe_flags & (AX178|AX772))
623 axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, &eaddr);
625 axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, &eaddr);
630 axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
633 * Work around broken adapters that appear to lie about
634 * their PHY addresses.
636 sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
638 ifp = sc->axe_ifp = if_alloc(IFT_ETHER);
640 device_printf(sc->axe_dev, "can not if_alloc()\n");
643 sx_destroy(&sc->axe_sleeplock);
644 mtx_destroy(&sc->axe_mtx);
648 if_initname(ifp, "axe", device_get_unit(sc->axe_dev));
649 ifp->if_mtu = ETHERMTU;
650 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
652 ifp->if_ioctl = axe_ioctl;
653 ifp->if_start = axe_start;
654 ifp->if_watchdog = axe_watchdog;
655 ifp->if_init = axe_init;
656 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
657 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
658 IFQ_SET_READY(&ifp->if_snd);
660 if (mii_phy_probe(self, &sc->axe_miibus,
661 axe_ifmedia_upd, axe_ifmedia_sts)) {
662 device_printf(sc->axe_dev, "MII without any PHY!\n");
666 sx_destroy(&sc->axe_sleeplock);
667 mtx_destroy(&sc->axe_mtx);
672 * Call MI attach routine.
675 ether_ifattach(ifp, eaddr);
676 callout_handle_init(&sc->axe_stat_ch);
677 usb_register_netisr();
688 axe_detach(device_t dev)
690 struct axe_softc *sc;
693 sc = device_get_softc(dev);
698 untimeout(axe_tick, sc, sc->axe_stat_ch);
699 usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
704 if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
705 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
706 if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
707 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
708 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
709 usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
712 sx_destroy(&sc->axe_sleeplock);
713 mtx_destroy(&sc->axe_mtx);
719 axe_rx_list_init(struct axe_softc *sc)
721 struct axe_cdata *cd;
726 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
727 c = &cd->axe_rx_chain[i];
731 if (c->axe_xfer == NULL) {
732 c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
733 if (c->axe_xfer == NULL)
735 c->axe_buf = usbd_alloc_buffer(c->axe_xfer,
737 if (c->axe_buf == NULL) {
738 usbd_free_xfer(c->axe_xfer);
748 axe_rx_list_free(struct axe_softc *sc)
752 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
753 if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
754 m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
755 sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
757 if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
758 usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
759 sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
765 axe_tx_list_init(struct axe_softc *sc)
767 struct axe_cdata *cd;
772 for (i = 0; i < AXE_TX_LIST_CNT; i++) {
773 c = &cd->axe_tx_chain[i];
777 if (c->axe_xfer == NULL) {
778 c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
779 if (c->axe_xfer == NULL)
781 c->axe_buf = usbd_alloc_buffer(c->axe_xfer,
783 if (c->axe_buf == NULL) {
784 usbd_free_xfer(c->axe_xfer);
794 axe_tx_list_free(struct axe_softc *sc)
798 /* Free TX resources. */
799 for (i = 0; i < AXE_TX_LIST_CNT; i++) {
800 if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
801 m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
802 sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
804 if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
805 usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
806 sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
812 * A frame has been uploaded: pass the resulting mbuf chain up to
813 * the higher level protocols.
816 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
818 struct axe_softc *sc;
819 struct axe_chain *c = (struct axe_chain *) priv;
823 struct axe_sframe_hdr *hdr;
831 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
836 if (status != USBD_NORMAL_COMPLETION) {
837 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
841 if (usbd_ratecheck(&sc->axe_rx_notice))
842 device_printf(sc->axe_dev, "usb error on rx: %s\n",
843 usbd_errstr(status));
844 if (status == USBD_STALLED)
845 usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_RX]);
849 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
854 if (sc->axe_flags & (AX178|AX772)) {
855 if (total_len < sizeof(struct axe_sframe_hdr)) {
859 if ((pktlen % 2) != 0)
863 hdr = (struct axe_sframe_hdr *) buf;
864 total_len -= sizeof(struct axe_sframe_hdr);
865 if ((hdr->len ^ hdr->ilen) != 0xffff) {
869 pktlen = le16toh(hdr->len);
870 if (pktlen > total_len) {
875 buf += sizeof(struct axe_sframe_hdr);
876 total_len -= pktlen + (pktlen % 2);
882 if (pktlen < sizeof(struct ether_header)) {
886 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
891 m->m_data += ETHER_ALIGN;
892 memcpy(mtod(m, void *), buf, pktlen);
893 m->m_pkthdr.len = m->m_len = pktlen;
894 m->m_pkthdr.rcvif = ifp;
896 ifp->if_input(ifp, m);
898 } while (total_len > 0);
901 /* Setup new transfer. */
902 usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
903 c, c->axe_buf, sc->axe_bufsz, USBD_SHORT_XFER_OK | USBD_NO_COPY,
904 USBD_NO_TIMEOUT, axe_rxeof);
912 * A frame was downloaded to the chip. It's safe for us to clean up
917 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
919 struct axe_softc *sc;
929 if (status != USBD_NORMAL_COMPLETION) {
930 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
934 device_printf(sc->axe_dev, "usb error on tx: %s\n",
935 usbd_errstr(status));
936 if (status == USBD_STALLED)
937 usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_TX]);
943 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
944 usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &err);
946 if (c->axe_mbuf != NULL) {
947 m_freem(c->axe_mbuf);
958 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
967 struct axe_softc *sc = xsc;
974 /* Perform periodic stuff in process context */
975 usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER);
979 axe_tick_task(void *xsc)
981 struct axe_softc *sc;
983 struct mii_data *mii;
1002 if (!sc->axe_link && mii->mii_media_status & IFM_ACTIVE &&
1003 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1005 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1009 sc->axe_stat_ch = timeout(axe_tick, sc, hz);
1012 AXE_SLEEPUNLOCK(sc);
1018 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
1020 struct axe_chain *c;
1022 struct axe_sframe_hdr hdr;
1025 c = &sc->axe_cdata.axe_tx_chain[idx];
1028 * Copy the mbuf data into a contiguous buffer, leaving two
1029 * bytes at the beginning to hold the frame length.
1031 if (sc->axe_flags & (AX178|AX772)) {
1032 hdr.len = htole16(m->m_pkthdr.len);
1033 hdr.ilen = ~hdr.len;
1035 memcpy(c->axe_buf, &hdr, sizeof(hdr));
1036 length = sizeof(hdr);
1038 m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf + length);
1039 length += m->m_pkthdr.len;
1041 if ((length % sc->axe_boundary) == 0) {
1044 memcpy(c->axe_buf + length, &hdr, sizeof(hdr));
1045 length += sizeof(hdr);
1048 m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
1049 length = m->m_pkthdr.len;
1053 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
1054 c, c->axe_buf, length, USBD_FORCE_SHORT_XFER, 10000, axe_txeof);
1057 err = usbd_transfer(c->axe_xfer);
1058 if (err != USBD_IN_PROGRESS) {
1059 /* XXX probably don't want to sleep here */
1062 AXE_SLEEPUNLOCK(sc);
1066 sc->axe_cdata.axe_tx_cnt++;
1072 axe_start(struct ifnet *ifp)
1074 struct axe_softc *sc;
1075 struct mbuf *m_head = NULL;
1080 if (!sc->axe_link) {
1085 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1090 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1091 if (m_head == NULL) {
1096 if (axe_encap(sc, m_head, 0)) {
1097 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1098 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1104 * If there's a BPF listener, bounce a copy of this frame
1107 BPF_MTAP(ifp, m_head);
1109 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1112 * Set a timeout in case the chip goes out to lunch.
1123 struct axe_softc *sc = xsc;
1124 struct ifnet *ifp = sc->axe_ifp;
1125 struct axe_chain *c;
1130 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1137 * Cancel pending I/O and free all RX/TX buffers.
1143 /* Set MAC address */
1144 axe_mac(sc, IF_LLADDR(sc->axe_ifp), 1);
1147 /* Enable RX logic. */
1150 if (axe_tx_list_init(sc) == ENOBUFS) {
1151 device_printf(sc->axe_dev, "tx list init failed\n");
1153 AXE_SLEEPUNLOCK(sc);
1158 if (axe_rx_list_init(sc) == ENOBUFS) {
1159 device_printf(sc->axe_dev, "rx list init failed\n");
1161 AXE_SLEEPUNLOCK(sc);
1165 /* Set transmitter IPG values */
1166 if (sc->axe_flags & (AX178|AX772)) {
1167 axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->axe_ipgs[2],
1168 (sc->axe_ipgs[1]<<8) | sc->axe_ipgs[0], NULL);
1170 axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
1171 axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
1172 axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
1175 /* Enable receiver, set RX mode */
1176 rxmode = AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
1177 if (sc->axe_flags & (AX178|AX772)) {
1178 if (sc->axe_bufsz == AXE_178_MAX_BUFSZ)
1179 rxmode |= AXE_178_RXCMD_MFB;
1181 rxmode |= AXE_172_RXCMD_UNICAST;
1183 /* If we want promiscuous mode, set the allframes bit. */
1184 if (ifp->if_flags & IFF_PROMISC)
1185 rxmode |= AXE_RXCMD_PROMISC;
1187 if (ifp->if_flags & IFF_BROADCAST)
1188 rxmode |= AXE_RXCMD_BROADCAST;
1190 axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1192 /* Load the multicast filter. */
1195 /* Open RX and TX pipes. */
1196 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
1197 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
1199 device_printf(sc->axe_dev, "open rx pipe failed: %s\n",
1202 AXE_SLEEPUNLOCK(sc);
1206 err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
1207 USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
1209 device_printf(sc->axe_dev, "open tx pipe failed: %s\n",
1212 AXE_SLEEPUNLOCK(sc);
1216 /* Start up the receive pipe. */
1217 for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1218 c = &sc->axe_cdata.axe_rx_chain[i];
1219 usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
1220 c, c->axe_buf, sc->axe_bufsz,
1221 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1222 USBD_NO_TIMEOUT, axe_rxeof);
1223 usbd_transfer(c->axe_xfer);
1226 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1227 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1230 AXE_SLEEPUNLOCK(sc);
1232 sc->axe_stat_ch = timeout(axe_tick, sc, hz);
1238 axe_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1240 struct axe_softc *sc = ifp->if_softc;
1241 struct ifreq *ifr = (struct ifreq *)data;
1242 struct mii_data *mii;
1248 if (ifp->if_flags & IFF_UP) {
1249 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1250 ifp->if_flags & IFF_PROMISC &&
1251 !(sc->axe_if_flags & IFF_PROMISC)) {
1254 axe_cmd(sc, AXE_CMD_RXCTL_READ,
1255 0, 0, (void *)&rxmode);
1256 rxmode = le16toh(rxmode);
1257 rxmode |= AXE_RXCMD_PROMISC;
1258 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
1262 AXE_SLEEPUNLOCK(sc);
1263 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1264 !(ifp->if_flags & IFF_PROMISC) &&
1265 sc->axe_if_flags & IFF_PROMISC) {
1268 axe_cmd(sc, AXE_CMD_RXCTL_READ,
1269 0, 0, (void *)&rxmode);
1270 rxmode = le16toh(rxmode);
1271 rxmode &= ~AXE_RXCMD_PROMISC;
1272 axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
1276 AXE_SLEEPUNLOCK(sc);
1277 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1280 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1283 AXE_SLEEPUNLOCK(sc);
1286 sc->axe_if_flags = ifp->if_flags;
1293 AXE_SLEEPUNLOCK(sc);
1300 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1301 AXE_SLEEPUNLOCK(sc);
1305 error = ether_ioctl(ifp, command, data);
1313 axe_watchdog(struct ifnet *ifp)
1315 struct axe_softc *sc;
1316 struct axe_chain *c;
1323 device_printf(sc->axe_dev, "watchdog timeout\n");
1325 c = &sc->axe_cdata.axe_tx_chain[0];
1326 usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
1327 axe_txeof(c->axe_xfer, c, stat);
1331 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1338 * Stop the adapter and free any mbufs allocated to the
1342 axe_stop(struct axe_softc *sc)
1347 AXE_SLEEPLOCKASSERT(sc);
1353 untimeout(axe_tick, sc, sc->axe_stat_ch);
1355 /* Stop transfers. */
1356 if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1357 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1359 device_printf(sc->axe_dev, "abort rx pipe failed: %s\n",
1362 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1364 device_printf(sc->axe_dev, "close rx pipe failed: %s\n",
1367 sc->axe_ep[AXE_ENDPT_RX] = NULL;
1370 if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1371 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1373 device_printf(sc->axe_dev, "abort tx pipe failed: %s\n",
1376 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1378 device_printf(sc->axe_dev, "close tx pipe failed: %s\n",
1381 sc->axe_ep[AXE_ENDPT_TX] = NULL;
1384 if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1385 err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1387 device_printf(sc->axe_dev,
1388 "abort intr pipe failed: %s\n", usbd_errstr(err));
1390 err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1392 device_printf(sc->axe_dev,
1393 "close intr pipe failed: %s\n", usbd_errstr(err));
1395 sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1400 /* Free RX resources. */
1401 axe_rx_list_free(sc);
1402 /* Free TX resources. */
1403 axe_tx_list_free(sc);
1405 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1413 * Stop all chip I/O so that the kernel's probe routines don't
1414 * get confused by errant DMAs when rebooting.
1417 axe_shutdown(device_t dev)
1419 struct axe_softc *sc;
1421 sc = device_get_softc(dev);
1425 AXE_SLEEPUNLOCK(sc);