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