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