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