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/kue/if_kue.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
    3  *      Bill Paul <wpaul@ee.columbia.edu>.  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_kue.c,v 1.17.2.9 2003/04/13 02:39:25 murray Exp $
   33  */
   34 
   35 /*
   36  * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
   37  *
   38  * Written by Bill Paul <wpaul@ee.columbia.edu>
   39  * Electrical Engineering Department
   40  * Columbia University, New York City
   41  */
   42 
   43 /*
   44  * The KLSI USB to ethernet adapter chip contains an USB serial interface,
   45  * ethernet MAC and embedded microcontroller (called the QT Engine).
   46  * The chip must have firmware loaded into it before it will operate.
   47  * Packets are passed between the chip and host via bulk transfers.
   48  * There is an interrupt endpoint mentioned in the software spec, however
   49  * it's currently unused. This device is 10Mbps half-duplex only, hence
   50  * there is no media selection logic. The MAC supports a 128 entry
   51  * multicast filter, though the exact size of the filter can depend
   52  * on the firmware. Curiously, while the software spec describes various
   53  * ethernet statistics counters, my sample adapter and firmware combination
   54  * claims not to support any statistics counters at all.
   55  *
   56  * Note that once we load the firmware in the device, we have to be
   57  * careful not to load it again: if you restart your computer but
   58  * leave the adapter attached to the USB controller, it may remain
   59  * powered on and retain its firmware. In this case, we don't need
   60  * to load the firmware a second time.
   61  *
   62  * Special thanks to Rob Furr for providing an ADS Technologies
   63  * adapter for development and testing. No monkeys were harmed during
   64  * the development of this driver.
   65  */
   66 
   67 #include <sys/param.h>
   68 #include <sys/systm.h>
   69 #include <sys/sockio.h>
   70 #include <sys/mbuf.h>
   71 #include <sys/malloc.h>
   72 #include <sys/kernel.h>
   73 #include <sys/socket.h>
   74 #include <sys/bus.h>
   75 
   76 #include <net/if.h>
   77 #include <net/ifq_var.h>
   78 #include <net/if_arp.h>
   79 #include <net/ethernet.h>
   80 #include <net/if_dl.h>
   81 #include <net/if_media.h>
   82 #include <net/bpf.h>
   83 
   84 #include <machine/clock.h>
   85 
   86 #include <bus/usb/usb.h>
   87 #include <bus/usb/usbdi.h>
   88 #include <bus/usb/usbdi_util.h>
   89 #include <bus/usb/usbdivar.h>
   90 #include <bus/usb/usb_ethersubr.h>
   91 
   92 #include "if_kuereg.h"
   93 #include <bus/usb/kue_fw.h>
   94 
   95 MODULE_DEPEND(kue, usb, 1, 1, 1);
   96 
   97 /*
   98  * Various supported device vendors/products.
   99  */
  100 static const struct usb_devno kue_devs[] = {
  101         { USB_DEVICE(0x03e8, 0x0008) }, /* AOX USB101 */
  102         { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USBETT */
  103         { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
  104         { USB_DEVICE(0x0557, 0x2002) }, /* ATen UC10T */
  105         { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Ethernet adapter */
  106         { USB_DEVICE(0x0565, 0x0003) }, /* Peracom Ethernet adapter*/
  107         { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Ethernet adapter */
  108         { USB_DEVICE(0x05e9, 0x0008) }, /* AOX (rebranded Kawasaki?) USB101*/
  109         { USB_DEVICE(0x05e9, 0x0008) }, /* Kawasaki LSI DUH3E10BT */
  110         { USB_DEVICE(0x05e9, 0x0009) }, /* Kawasaki LSI DUH3E10BTN */
  111         { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
  112         { USB_DEVICE(0x06e1, 0x0008) }, /* ADS UBS-10BT */
  113         { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2102USB */
  114         { USB_DEVICE(0x07aa, 0x0001) }, /* Corega USB-T*/
  115         { USB_DEVICE(0x07b8, 0x4000) }, /* AboCom URE450 */
  116         { USB_DEVICE(0x0846, 0x1001) }, /* Netgear EA101 */
  117         { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45*/
  118         { USB_DEVICE(0x2001, 0x4000) }, /* D-Link DSB650C */
  119 };
  120 
  121 static int kue_match(device_t);
  122 static int kue_attach(device_t);
  123 static int kue_detach(device_t);
  124 static void kue_shutdown(device_t);
  125 static int kue_tx_list_init(struct kue_softc *);
  126 static int kue_rx_list_init(struct kue_softc *);
  127 static int kue_newbuf(struct kue_softc *, struct kue_chain *, struct mbuf *);
  128 static int kue_encap(struct kue_softc *, struct mbuf *, int);
  129 static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
  130 static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
  131 static void kue_start(struct ifnet *, struct ifaltq_subque *);
  132 static void kue_rxstart(struct ifnet *);
  133 static int kue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
  134 static void kue_init(void *);
  135 static void kue_stop(struct kue_softc *);
  136 static void kue_watchdog(struct ifnet *);
  137 
  138 static void kue_setmulti(struct kue_softc *);
  139 static void kue_reset(struct kue_softc *);
  140 
  141 static usbd_status kue_do_request(usbd_device_handle,
  142                                   usb_device_request_t *, void *);
  143 static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
  144                            u_int16_t, char *, int);
  145 static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
  146 static int kue_load_fw(struct kue_softc *);
  147 
  148 static device_method_t kue_methods[] = {
  149         /* Device interface */
  150         DEVMETHOD(device_probe,         kue_match),
  151         DEVMETHOD(device_attach,        kue_attach),
  152         DEVMETHOD(device_detach,        kue_detach),
  153         DEVMETHOD(device_shutdown,      kue_shutdown),
  154 
  155         DEVMETHOD_END
  156 };
  157 
  158 static driver_t kue_driver = {
  159         "kue",
  160         kue_methods,
  161         sizeof(struct kue_softc)
  162 };
  163 
  164 static devclass_t kue_devclass;
  165 
  166 DECLARE_DUMMY_MODULE(if_kue);
  167 DRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, usbd_driver_load, NULL);
  168 
  169 /*
  170  * We have a custom do_request function which is almost like the
  171  * regular do_request function, except it has a much longer timeout.
  172  * Why? Because we need to make requests over the control endpoint
  173  * to download the firmware to the device, which can take longer
  174  * than the default timeout.
  175  */
  176 static usbd_status
  177 kue_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
  178 {
  179         usbd_xfer_handle        xfer;
  180         usbd_status             err;
  181 
  182         xfer = usbd_alloc_xfer(dev);
  183         usbd_setup_default_xfer(xfer, dev, 0, 500000, req,
  184             data, UGETW(req->wLength), USBD_SHORT_XFER_OK, 0);
  185         err = usbd_sync_transfer(xfer);
  186         usbd_free_xfer(xfer);
  187         return(err);
  188 }
  189 
  190 static usbd_status
  191 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
  192 {
  193         usbd_device_handle      dev;
  194         usb_device_request_t    req;
  195         usbd_status             err;
  196 
  197         if (sc->kue_dying)
  198                 return(USBD_NORMAL_COMPLETION);
  199 
  200         dev = sc->kue_udev;
  201 
  202         KUE_LOCK(sc);
  203 
  204         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
  205 
  206         req.bRequest = breq;
  207         USETW(req.wValue, word);
  208         USETW(req.wIndex, 0);
  209         USETW(req.wLength, 0);
  210 
  211         err = kue_do_request(dev, &req, NULL);
  212 
  213         KUE_UNLOCK(sc);
  214 
  215         return(err);
  216 }
  217 
  218 static usbd_status
  219 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
  220         char *data, int len)
  221 {
  222         usbd_device_handle      dev;
  223         usb_device_request_t    req;
  224         usbd_status             err;
  225 
  226         dev = sc->kue_udev;
  227 
  228         if (sc->kue_dying)
  229                 return(USBD_NORMAL_COMPLETION);
  230 
  231         KUE_LOCK(sc);
  232 
  233         if (rw == KUE_CTL_WRITE)
  234                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
  235         else
  236                 req.bmRequestType = UT_READ_VENDOR_DEVICE;
  237 
  238         req.bRequest = breq;
  239         USETW(req.wValue, val);
  240         USETW(req.wIndex, 0);
  241         USETW(req.wLength, len);
  242 
  243         err = kue_do_request(dev, &req, data);
  244 
  245         KUE_UNLOCK(sc);
  246 
  247         return(err);
  248 }
  249 
  250 static int
  251 kue_load_fw(struct kue_softc *sc)
  252 {
  253         usbd_status             err;
  254         usb_device_descriptor_t *dd;
  255         int                     hwrev;
  256 
  257         dd = &sc->kue_udev->ddesc;
  258         hwrev = UGETW(dd->bcdDevice);
  259 
  260         /*
  261          * First, check if we even need to load the firmware.
  262          * If the device was still attached when the system was
  263          * rebooted, it may already have firmware loaded in it.
  264          * If this is the case, we don't need to do it again.
  265          * And in fact, if we try to load it again, we'll hang,
  266          * so we have to avoid this condition if we don't want
  267          * to look stupid.
  268          *
  269          * We can test this quickly by checking the bcdRevision
  270          * code. The NIC will return a different revision code if
  271          * it's probed while the firmware is still loaded and
  272          * running.
  273          */
  274         if (hwrev == 0x0202)
  275                 return(0);
  276 
  277         /* Load code segment */
  278         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
  279             0, kue_code_seg, sizeof(kue_code_seg));
  280         if (err) {
  281                 kprintf("kue%d: failed to load code segment: %s\n",
  282                     sc->kue_unit, usbd_errstr(err));
  283                         return(ENXIO);
  284         }
  285 
  286         /* Load fixup segment */
  287         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
  288             0, kue_fix_seg, sizeof(kue_fix_seg));
  289         if (err) {
  290                 kprintf("kue%d: failed to load fixup segment: %s\n",
  291                     sc->kue_unit, usbd_errstr(err));
  292                         return(ENXIO);
  293         }
  294 
  295         /* Send trigger command. */
  296         err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
  297             0, kue_trig_seg, sizeof(kue_trig_seg));
  298         if (err) {
  299                 kprintf("kue%d: failed to load trigger segment: %s\n",
  300                     sc->kue_unit, usbd_errstr(err));
  301                         return(ENXIO);
  302         }
  303 
  304         return(0);
  305 }
  306 
  307 static void
  308 kue_setmulti(struct kue_softc *sc)
  309 {
  310         struct ifnet            *ifp;
  311         struct ifmultiaddr      *ifma;
  312         int                     i = 0;
  313 
  314         ifp = &sc->arpcom.ac_if;
  315 
  316         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
  317                 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
  318                 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
  319                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
  320                 return;
  321         }
  322 
  323         sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
  324 
  325         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
  326                 if (ifma->ifma_addr->sa_family != AF_LINK)
  327                         continue;
  328                 /*
  329                  * If there are too many addresses for the
  330                  * internal filter, switch over to allmulti mode.
  331                  */
  332                 if (i == KUE_MCFILTCNT(sc))
  333                         break;
  334                 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
  335                     KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
  336                 i++;
  337         }
  338 
  339         if (i == KUE_MCFILTCNT(sc))
  340                 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
  341         else {
  342                 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
  343                 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
  344                     i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
  345         }
  346 
  347         kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
  348 
  349         return;
  350 }
  351 
  352 /*
  353  * Issue a SET_CONFIGURATION command to reset the MAC. This should be
  354  * done after the firmware is loaded into the adapter in order to
  355  * bring it into proper operation.
  356  */
  357 static void
  358 kue_reset(struct kue_softc *sc)
  359 {
  360         if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0) ||
  361             usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
  362             &sc->kue_iface)) {
  363                 kprintf("kue%d: getting interface handle failed\n",
  364                     sc->kue_unit);
  365         }
  366 
  367         /* Wait a little while for the chip to get its brains in order. */
  368         DELAY(1000);
  369         return;
  370 }
  371 
  372 /*
  373  * Probe for a KLSI chip.
  374  */
  375 static int
  376 kue_match(device_t self)
  377 {
  378         struct usb_attach_arg *uaa = device_get_ivars(self);
  379 
  380         if (uaa->iface == NULL)
  381                 return(UMATCH_NONE);
  382 
  383         return (usb_lookup(kue_devs, uaa->vendor, uaa->product) != NULL ?
  384             UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
  385 }
  386 
  387 /*
  388  * Attach the interface. Allocate softc structures, do
  389  * setup and ethernet/BPF attach.
  390  */
  391 static int
  392 kue_attach(device_t self)
  393 {
  394         struct kue_softc *sc = device_get_softc(self);
  395         struct usb_attach_arg *uaa = device_get_ivars(self);
  396         struct ifnet            *ifp;
  397         usb_interface_descriptor_t      *id;
  398         usb_endpoint_descriptor_t       *ed;
  399         int                     i;
  400 
  401         sc->kue_iface = uaa->iface;
  402         sc->kue_udev = uaa->device;
  403         sc->kue_unit = device_get_unit(self);
  404 
  405         id = usbd_get_interface_descriptor(uaa->iface);
  406 
  407         /* Find endpoints. */
  408         for (i = 0; i < id->bNumEndpoints; i++) {
  409                 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
  410                 if (!ed) {
  411                         kprintf("kue%d: couldn't get ep %d\n",
  412                             sc->kue_unit, i);
  413                         return ENXIO;
  414                 }
  415                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
  416                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
  417                         sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
  418                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
  419                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
  420                         sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
  421                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
  422                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
  423                         sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
  424                 }
  425         }
  426 
  427 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
  428         mtx_init(&sc->kue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
  429             MTX_DEF | MTX_RECURSE);
  430 #endif
  431         KUE_LOCK(sc);
  432 
  433         /* Load the firmware into the NIC. */
  434         if (kue_load_fw(sc)) {
  435                 KUE_UNLOCK(sc);
  436 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
  437                 mtx_destroy(&sc->kue_mtx);
  438 #endif
  439                 return ENXIO;
  440         }
  441 
  442         /* Reset the adapter. */
  443         kue_reset(sc);
  444 
  445         /* Read ethernet descriptor */
  446         kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
  447             0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
  448 
  449         sc->kue_mcfilters = kmalloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
  450             M_USBDEV, M_WAITOK);
  451 
  452         ifp = &sc->arpcom.ac_if;
  453         ifp->if_softc = sc;
  454         if_initname(ifp, "kue", sc->kue_unit);
  455         ifp->if_mtu = ETHERMTU;
  456         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
  457         ifp->if_ioctl = kue_ioctl;
  458         ifp->if_start = kue_start;
  459         ifp->if_watchdog = kue_watchdog;
  460         ifp->if_init = kue_init;
  461         ifp->if_baudrate = 10000000;
  462         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
  463         ifq_set_ready(&ifp->if_snd);
  464 
  465         /*
  466          * Call MI attach routine.
  467          */
  468         ether_ifattach(ifp, sc->kue_desc.kue_macaddr, NULL);
  469         usb_register_netisr();
  470         sc->kue_dying = 0;
  471 
  472         KUE_UNLOCK(sc);
  473 
  474         return 0;
  475 }
  476 
  477 static int
  478 kue_detach(device_t dev)
  479 {
  480         struct kue_softc        *sc;
  481         struct ifnet            *ifp;
  482 
  483         sc = device_get_softc(dev);
  484         KUE_LOCK(sc);
  485         ifp = &sc->arpcom.ac_if;
  486 
  487         sc->kue_dying = 1;
  488 
  489         if (ifp != NULL)
  490                 ether_ifdetach(ifp);
  491 
  492         if (sc->kue_ep[KUE_ENDPT_TX] != NULL)
  493                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
  494         if (sc->kue_ep[KUE_ENDPT_RX] != NULL)
  495                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
  496         if (sc->kue_ep[KUE_ENDPT_INTR] != NULL)
  497                 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
  498 
  499         if (sc->kue_mcfilters != NULL)
  500                 kfree(sc->kue_mcfilters, M_USBDEV);
  501 
  502         KUE_UNLOCK(sc);
  503 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
  504         mtx_destroy(&sc->kue_mtx);
  505 #endif
  506 
  507         return(0);
  508 }
  509 
  510 /*
  511  * Initialize an RX descriptor and attach an MBUF cluster.
  512  */
  513 static int
  514 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
  515 {
  516         struct mbuf             *m_new = NULL;
  517 
  518         if (m == NULL) {
  519                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
  520                 if (m_new == NULL) {
  521                         kprintf("kue%d: no memory for rx list "
  522                             "-- packet dropped!\n", sc->kue_unit);
  523                         return(ENOBUFS);
  524                 }
  525 
  526                 MCLGET(m_new, MB_DONTWAIT);
  527                 if (!(m_new->m_flags & M_EXT)) {
  528                         kprintf("kue%d: no memory for rx list "
  529                             "-- packet dropped!\n", sc->kue_unit);
  530                         m_freem(m_new);
  531                         return(ENOBUFS);
  532                 }
  533                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
  534         } else {
  535                 m_new = m;
  536                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
  537                 m_new->m_data = m_new->m_ext.ext_buf;
  538         }
  539 
  540         c->kue_mbuf = m_new;
  541 
  542         return(0);
  543 }
  544 
  545 static int
  546 kue_rx_list_init(struct kue_softc *sc)
  547 {
  548         struct kue_cdata        *cd;
  549         struct kue_chain        *c;
  550         int                     i;
  551 
  552         cd = &sc->kue_cdata;
  553         for (i = 0; i < KUE_RX_LIST_CNT; i++) {
  554                 c = &cd->kue_rx_chain[i];
  555                 c->kue_sc = sc;
  556                 c->kue_idx = i;
  557                 if (kue_newbuf(sc, c, NULL) == ENOBUFS)
  558                         return(ENOBUFS);
  559                 if (c->kue_xfer == NULL) {
  560                         c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
  561                         if (c->kue_xfer == NULL)
  562                                 return(ENOBUFS);
  563                 }
  564         }
  565 
  566         return(0);
  567 }
  568 
  569 static int
  570 kue_tx_list_init(struct kue_softc *sc)
  571 {
  572         struct kue_cdata        *cd;
  573         struct kue_chain        *c;
  574         int                     i;
  575 
  576         cd = &sc->kue_cdata;
  577         for (i = 0; i < KUE_TX_LIST_CNT; i++) {
  578                 c = &cd->kue_tx_chain[i];
  579                 c->kue_sc = sc;
  580                 c->kue_idx = i;
  581                 c->kue_mbuf = NULL;
  582                 if (c->kue_xfer == NULL) {
  583                         c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
  584                         if (c->kue_xfer == NULL)
  585                                 return(ENOBUFS);
  586                 }
  587                 c->kue_buf = kmalloc(KUE_BUFSZ, M_USBDEV, M_WAITOK);
  588         }
  589 
  590         return(0);
  591 }
  592 
  593 static void
  594 kue_rxstart(struct ifnet *ifp)
  595 {
  596         struct kue_softc        *sc;
  597         struct kue_chain        *c;
  598 
  599         sc = ifp->if_softc;
  600         KUE_LOCK(sc);
  601         c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod];
  602 
  603         if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
  604                 IFNET_STAT_INC(ifp, ierrors, 1);
  605                 return;
  606         }
  607 
  608         /* Setup new transfer. */
  609         usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
  610             c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
  611             USBD_NO_TIMEOUT, kue_rxeof);
  612         usbd_transfer(c->kue_xfer);
  613 
  614         KUE_UNLOCK(sc);
  615 
  616         return;
  617 }
  618 
  619 /*
  620  * A frame has been uploaded: pass the resulting mbuf chain up to
  621  * the higher level protocols.
  622  */
  623 static void
  624 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
  625 {
  626         struct kue_softc        *sc;
  627         struct kue_chain        *c;
  628         struct mbuf             *m;
  629         struct ifnet            *ifp;
  630         int                     total_len = 0;
  631         u_int16_t               len;
  632 
  633         c = priv;
  634         sc = c->kue_sc;
  635         KUE_LOCK(sc);
  636         ifp = &sc->arpcom.ac_if;
  637 
  638         if (!(ifp->if_flags & IFF_RUNNING)) {
  639                 KUE_UNLOCK(sc);
  640                 return;
  641         }
  642 
  643         if (status != USBD_NORMAL_COMPLETION) {
  644                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
  645                         KUE_UNLOCK(sc);
  646                         return;
  647                 }
  648                 if (usbd_ratecheck(&sc->kue_rx_notice))
  649                         kprintf("kue%d: usb error on rx: %s\n", sc->kue_unit,
  650                             usbd_errstr(status));
  651                 if (status == USBD_STALLED)
  652                         usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
  653                 goto done;
  654         }
  655 
  656         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
  657         m = c->kue_mbuf;
  658         if (total_len <= 1)
  659                 goto done;
  660 
  661         len = *mtod(m, u_int16_t *);
  662         m_adj(m, sizeof(u_int16_t));
  663 
  664         /* No errors; receive the packet. */
  665         total_len = len;
  666 
  667         if (len < sizeof(struct ether_header)) {
  668                 IFNET_STAT_INC(ifp, ierrors, 1);
  669                 goto done;
  670         }
  671 
  672         IFNET_STAT_INC(ifp, ipackets, 1);
  673         m->m_pkthdr.rcvif = ifp;
  674         m->m_pkthdr.len = m->m_len = total_len;
  675 
  676         /* Put the packet on the special USB input queue. */
  677         usb_ether_input(m);
  678         kue_rxstart(ifp);
  679 
  680         KUE_UNLOCK(sc);
  681 
  682         return;
  683 done:
  684 
  685         /* Setup new transfer. */
  686         usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
  687             c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
  688             USBD_NO_TIMEOUT, kue_rxeof);
  689         usbd_transfer(c->kue_xfer);
  690         KUE_UNLOCK(sc);
  691 
  692         return;
  693 }
  694 
  695 /*
  696  * A frame was downloaded to the chip. It's safe for us to clean up
  697  * the list buffers.
  698  */
  699 
  700 static void
  701 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
  702 {
  703         struct kue_softc        *sc;
  704         struct kue_chain        *c;
  705         struct ifnet            *ifp;
  706         usbd_status             err;
  707 
  708         c = priv;
  709         sc = c->kue_sc;
  710         KUE_LOCK(sc);
  711 
  712         ifp = &sc->arpcom.ac_if;
  713         ifp->if_timer = 0;
  714         ifq_clr_oactive(&ifp->if_snd);
  715 
  716         if (status != USBD_NORMAL_COMPLETION) {
  717                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
  718                         KUE_UNLOCK(sc);
  719                         return;
  720                 }
  721                 kprintf("kue%d: usb error on tx: %s\n", sc->kue_unit,
  722                     usbd_errstr(status));
  723                 if (status == USBD_STALLED)
  724                         usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
  725                 KUE_UNLOCK(sc);
  726                 return;
  727         }
  728 
  729         usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &err);
  730 
  731         if (c->kue_mbuf != NULL) {
  732                 m_freem(c->kue_mbuf);
  733                 c->kue_mbuf = NULL;
  734         }
  735 
  736         if (err)
  737                 IFNET_STAT_INC(ifp, oerrors, 1);
  738         else
  739                 IFNET_STAT_INC(ifp, opackets, 1);
  740 
  741         if (!ifq_is_empty(&ifp->if_snd))
  742                 if_devstart(ifp);
  743 
  744         KUE_UNLOCK(sc);
  745 
  746         return;
  747 }
  748 
  749 static int
  750 kue_encap(struct kue_softc *sc, struct mbuf *m, int idx)
  751 {
  752         int                     total_len;
  753         struct kue_chain        *c;
  754         usbd_status             err;
  755 
  756         c = &sc->kue_cdata.kue_tx_chain[idx];
  757 
  758         /*
  759          * Copy the mbuf data into a contiguous buffer, leaving two
  760          * bytes at the beginning to hold the frame length.
  761          */
  762         m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
  763         c->kue_mbuf = m;
  764 
  765         total_len = m->m_pkthdr.len + 2;
  766         total_len += 64 - (total_len % 64);
  767 
  768         /* Frame length is specified in the first 2 bytes of the buffer. */
  769         c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
  770         c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
  771 
  772         usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
  773             c, c->kue_buf, total_len, 0, 10000, kue_txeof);
  774 
  775         /* Transmit */
  776         err = usbd_transfer(c->kue_xfer);
  777         if (err != USBD_IN_PROGRESS) {
  778                 kue_stop(sc);
  779                 return(EIO);
  780         }
  781 
  782         sc->kue_cdata.kue_tx_cnt++;
  783 
  784         return(0);
  785 }
  786 
  787 static void
  788 kue_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
  789 {
  790         struct kue_softc        *sc;
  791         struct mbuf             *m_head = NULL;
  792 
  793         ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
  794 
  795         sc = ifp->if_softc;
  796         KUE_LOCK(sc);
  797 
  798         if (ifq_is_oactive(&ifp->if_snd)) {
  799                 KUE_UNLOCK(sc);
  800                 return;
  801         }
  802 
  803         m_head = ifq_dequeue(&ifp->if_snd);
  804         if (m_head == NULL) {
  805                 KUE_UNLOCK(sc);
  806                 return;
  807         }
  808 
  809         if (kue_encap(sc, m_head, 0)) {
  810                 /* kue_encap() will free m_head, if we reach here */
  811                 ifq_set_oactive(&ifp->if_snd);
  812                 KUE_UNLOCK(sc);
  813                 return;
  814         }
  815 
  816         /*
  817          * If there's a BPF listener, bounce a copy of this frame
  818          * to him.
  819          */
  820         BPF_MTAP(ifp, m_head);
  821 
  822         ifq_set_oactive(&ifp->if_snd);
  823 
  824         /*
  825          * Set a timeout in case the chip goes out to lunch.
  826          */
  827         ifp->if_timer = 5;
  828         KUE_UNLOCK(sc);
  829 
  830         return;
  831 }
  832 
  833 static void
  834 kue_init(void *xsc)
  835 {
  836         struct kue_softc        *sc = xsc;
  837         struct ifnet            *ifp = &sc->arpcom.ac_if;
  838         struct kue_chain        *c;
  839         usbd_status             err;
  840         int                     i;
  841 
  842         KUE_LOCK(sc);
  843 
  844         if (ifp->if_flags & IFF_RUNNING) {
  845                 KUE_UNLOCK(sc);
  846                 return;
  847         }
  848 
  849         /* Set MAC address */
  850         kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
  851             0, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
  852 
  853         sc->kue_rxfilt = KUE_RXFILT_UNICAST|KUE_RXFILT_BROADCAST;
  854 
  855          /* If we want promiscuous mode, set the allframes bit. */
  856         if (ifp->if_flags & IFF_PROMISC)
  857                 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
  858 
  859         kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
  860 
  861         /* I'm not sure how to tune these. */
  862 #ifdef notdef
  863         /*
  864          * Leave this one alone for now; setting it
  865          * wrong causes lockups on some machines/controllers.
  866          */
  867         kue_setword(sc, KUE_CMD_SET_SOFS, 1);
  868 #endif
  869         kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
  870 
  871         /* Init TX ring. */
  872         if (kue_tx_list_init(sc) == ENOBUFS) {
  873                 kprintf("kue%d: tx list init failed\n", sc->kue_unit);
  874                 KUE_UNLOCK(sc);
  875                 return;
  876         }
  877 
  878         /* Init RX ring. */
  879         if (kue_rx_list_init(sc) == ENOBUFS) {
  880                 kprintf("kue%d: rx list init failed\n", sc->kue_unit);
  881                 KUE_UNLOCK(sc);
  882                 return;
  883         }
  884 
  885         /* Load the multicast filter. */
  886         kue_setmulti(sc);
  887 
  888         /* Open RX and TX pipes. */
  889         err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
  890             USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
  891         if (err) {
  892                 kprintf("kue%d: open rx pipe failed: %s\n",
  893                     sc->kue_unit, usbd_errstr(err));
  894                 KUE_UNLOCK(sc);
  895                 return;
  896         }
  897 
  898         err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
  899             USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
  900         if (err) {
  901                 kprintf("kue%d: open tx pipe failed: %s\n",
  902                     sc->kue_unit, usbd_errstr(err));
  903                 KUE_UNLOCK(sc);
  904                 return;
  905         }
  906 
  907         /* Start up the receive pipe. */
  908         for (i = 0; i < KUE_RX_LIST_CNT; i++) {
  909                 c = &sc->kue_cdata.kue_rx_chain[i];
  910                 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
  911                     c, mtod(c->kue_mbuf, char *), KUE_BUFSZ,
  912                 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
  913                 usbd_transfer(c->kue_xfer);
  914         }
  915 
  916         ifp->if_flags |= IFF_RUNNING;
  917         ifq_clr_oactive(&ifp->if_snd)
  918 
  919         KUE_UNLOCK(sc);
  920 
  921         return;
  922 }
  923 
  924 static int
  925 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
  926 {
  927         struct kue_softc        *sc = ifp->if_softc;
  928         int                     error = 0;
  929 
  930         KUE_LOCK(sc);
  931 
  932         switch(command) {
  933         case SIOCSIFFLAGS:
  934                 if (ifp->if_flags & IFF_UP) {
  935                         if (ifp->if_flags & IFF_RUNNING &&
  936                             ifp->if_flags & IFF_PROMISC &&
  937                             !(sc->kue_if_flags & IFF_PROMISC)) {
  938                                 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
  939                                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
  940                                     sc->kue_rxfilt);
  941                         } else if (ifp->if_flags & IFF_RUNNING &&
  942                             !(ifp->if_flags & IFF_PROMISC) &&
  943                             sc->kue_if_flags & IFF_PROMISC) {
  944                                 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
  945                                 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
  946                                     sc->kue_rxfilt);
  947                         } else if (!(ifp->if_flags & IFF_RUNNING))
  948                                 kue_init(sc);
  949                 } else {
  950                         if (ifp->if_flags & IFF_RUNNING)
  951                                 kue_stop(sc);
  952                 }
  953                 sc->kue_if_flags = ifp->if_flags;
  954                 error = 0;
  955                 break;
  956         case SIOCADDMULTI:
  957         case SIOCDELMULTI:
  958                 kue_setmulti(sc);
  959                 error = 0;
  960                 break;
  961         default:
  962                 error = ether_ioctl(ifp, command, data);
  963                 break;
  964         }
  965 
  966         KUE_UNLOCK(sc);
  967 
  968         return(error);
  969 }
  970 
  971 static void
  972 kue_watchdog(struct ifnet *ifp)
  973 {
  974         struct kue_softc        *sc;
  975         struct kue_chain        *c;
  976         usbd_status             stat;
  977 
  978         sc = ifp->if_softc;
  979         KUE_LOCK(sc);
  980         IFNET_STAT_INC(ifp, oerrors, 1);
  981         kprintf("kue%d: watchdog timeout\n", sc->kue_unit);
  982 
  983         c = &sc->kue_cdata.kue_tx_chain[0];
  984         usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
  985         kue_txeof(c->kue_xfer, c, stat);
  986 
  987         if (!ifq_is_empty(&ifp->if_snd))
  988                 if_devstart(ifp);
  989         KUE_UNLOCK(sc);
  990 
  991         return;
  992 }
  993 
  994 /*
  995  * Stop the adapter and free any mbufs allocated to the
  996  * RX and TX lists.
  997  */
  998 static void
  999 kue_stop(struct kue_softc *sc)
 1000 {
 1001         usbd_status             err;
 1002         struct ifnet            *ifp;
 1003         int                     i;
 1004 
 1005         KUE_LOCK(sc);
 1006         ifp = &sc->arpcom.ac_if;
 1007         ifp->if_timer = 0;
 1008 
 1009         /* Stop transfers. */
 1010         if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
 1011                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
 1012                 if (err) {
 1013                         kprintf("kue%d: abort rx pipe failed: %s\n",
 1014                         sc->kue_unit, usbd_errstr(err));
 1015                 }
 1016                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
 1017                 if (err) {
 1018                         kprintf("kue%d: close rx pipe failed: %s\n",
 1019                         sc->kue_unit, usbd_errstr(err));
 1020                 }
 1021                 sc->kue_ep[KUE_ENDPT_RX] = NULL;
 1022         }
 1023 
 1024         if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
 1025                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
 1026                 if (err) {
 1027                         kprintf("kue%d: abort tx pipe failed: %s\n",
 1028                         sc->kue_unit, usbd_errstr(err));
 1029                 }
 1030                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
 1031                 if (err) {
 1032                         kprintf("kue%d: close tx pipe failed: %s\n",
 1033                             sc->kue_unit, usbd_errstr(err));
 1034                 }
 1035                 sc->kue_ep[KUE_ENDPT_TX] = NULL;
 1036         }
 1037 
 1038         if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
 1039                 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
 1040                 if (err) {
 1041                         kprintf("kue%d: abort intr pipe failed: %s\n",
 1042                         sc->kue_unit, usbd_errstr(err));
 1043                 }
 1044                 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
 1045                 if (err) {
 1046                         kprintf("kue%d: close intr pipe failed: %s\n",
 1047                             sc->kue_unit, usbd_errstr(err));
 1048                 }
 1049                 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
 1050         }
 1051 
 1052         /* Free RX resources. */
 1053         for (i = 0; i < KUE_RX_LIST_CNT; i++) {
 1054                 if (sc->kue_cdata.kue_rx_chain[i].kue_buf != NULL) {
 1055                         kfree(sc->kue_cdata.kue_rx_chain[i].kue_buf, M_USBDEV);
 1056                         sc->kue_cdata.kue_rx_chain[i].kue_buf = NULL;
 1057                 }
 1058                 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
 1059                         m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
 1060                         sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
 1061                 }
 1062                 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
 1063                         usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
 1064                         sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
 1065                 }
 1066         }
 1067 
 1068         /* Free TX resources. */
 1069         for (i = 0; i < KUE_TX_LIST_CNT; i++) {
 1070                 if (sc->kue_cdata.kue_tx_chain[i].kue_buf != NULL) {
 1071                         kfree(sc->kue_cdata.kue_tx_chain[i].kue_buf, M_USBDEV);
 1072                         sc->kue_cdata.kue_tx_chain[i].kue_buf = NULL;
 1073                 }
 1074                 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
 1075                         m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
 1076                         sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
 1077                 }
 1078                 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
 1079                         usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
 1080                         sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
 1081                 }
 1082         }
 1083 
 1084         ifp->if_flags &= ~IFF_RUNNING;
 1085         ifq_clr_oactive(&ifp->if_snd);
 1086         KUE_UNLOCK(sc);
 1087 
 1088         return;
 1089 }
 1090 
 1091 /*
 1092  * Stop all chip I/O so that the kernel's probe routines don't
 1093  * get confused by errant DMAs when rebooting.
 1094  */
 1095 static void
 1096 kue_shutdown(device_t dev)
 1097 {
 1098         struct kue_softc        *sc;
 1099 
 1100         sc = device_get_softc(dev);
 1101 
 1102         kue_stop(sc);
 1103 
 1104         return;
 1105 }

Cache object: f8022caa7a25209adcd2e5bcd6fb7dc8


[ 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.