The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/netif/axe/if_axe.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 94bc3833a004987a06558c2a8234aa76


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.