The old USB ethernet code utilized a netisr to hand packets over
[dragonfly.git] / sys / dev / netif / cue / if_cue.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_cue.c,v 1.45 2003/12/08 07:54:14 obrien Exp $
33  * $DragonFly: src/sys/dev/netif/cue/if_cue.c,v 1.17 2005/02/16 22:50:28 joerg Exp $
34  *
35  */
36
37 /*
38  * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
39  * adapters and others.
40  *
41  * Written by Bill Paul <wpaul@ee.columbia.edu>
42  * Electrical Engineering Department
43  * Columbia University, New York City
44  */
45
46 /*
47  * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
48  * RX filter uses a 512-bit multicast hash table, single perfect entry
49  * for the station address, and promiscuous mode. Unlike the ADMtek
50  * and KLSI chips, the CATC ASIC supports read and write combining
51  * mode where multiple packets can be transfered using a single bulk
52  * transaction, which helps performance a great deal.
53  */
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/sockio.h>
58 #include <sys/mbuf.h>
59 #include <sys/malloc.h>
60 #include <sys/kernel.h>
61 #include <sys/socket.h>
62
63 #include <net/if.h>
64 #include <net/ifq_var.h>
65 #include <net/if_arp.h>
66 #include <net/ethernet.h>
67 #include <net/if_dl.h>
68
69 #include <net/bpf.h>
70
71 #include <sys/bus.h>
72 #include <machine/bus.h>
73 #if defined(__DragonFly__) || __FreeBSD_version < 500000
74 #include <machine/clock.h>
75 #endif
76
77 #include <bus/usb/usb.h>
78 #include <bus/usb/usbdi.h>
79 #include <bus/usb/usbdi_util.h>
80 #include <bus/usb/usbdivar.h>
81 #include <bus/usb/usbdevs.h>
82 #include <bus/usb/usb_ethersubr.h>
83
84 #include "if_cuereg.h"
85
86 /*
87  * Various supported device vendors/products.
88  */
89 Static struct cue_type cue_devs[] = {
90         { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE },
91         { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 },
92         { USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK },
93         { 0, 0 }
94 };
95
96 Static int cue_match(device_ptr_t);
97 Static int cue_attach(device_ptr_t);
98 Static int cue_detach(device_ptr_t);
99
100 Static int cue_tx_list_init(struct cue_softc *);
101 Static int cue_rx_list_init(struct cue_softc *);
102 Static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *);
103 Static int cue_encap(struct cue_softc *, struct mbuf *, int);
104 Static void cue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
105 Static void cue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
106 Static void cue_tick(void *);
107 Static void cue_rxstart(struct ifnet *);
108 Static void cue_start(struct ifnet *);
109 Static int cue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
110 Static void cue_init(void *);
111 Static void cue_stop(struct cue_softc *);
112 Static void cue_watchdog(struct ifnet *);
113 Static void cue_shutdown(device_ptr_t);
114
115 Static void cue_setmulti(struct cue_softc *);
116 Static uint32_t cue_mchash(const uint8_t *);
117 Static void cue_reset(struct cue_softc *);
118
119 Static int cue_csr_read_1(struct cue_softc *, int);
120 Static int cue_csr_write_1(struct cue_softc *, int, int);
121 Static int cue_csr_read_2(struct cue_softc *, int);
122 #ifdef notdef
123 Static int cue_csr_write_2(struct cue_softc *, int, int);
124 #endif
125 Static int cue_mem(struct cue_softc *, int, int, void *, int);
126 Static int cue_getmac(struct cue_softc *, void *);
127
128 Static device_method_t cue_methods[] = {
129         /* Device interface */
130         DEVMETHOD(device_probe,         cue_match),
131         DEVMETHOD(device_attach,        cue_attach),
132         DEVMETHOD(device_detach,        cue_detach),
133         DEVMETHOD(device_shutdown,      cue_shutdown),
134
135         { 0, 0 }
136 };
137
138 Static driver_t cue_driver = {
139         "cue",
140         cue_methods,
141         sizeof(struct cue_softc)
142 };
143
144 Static devclass_t cue_devclass;
145
146 DECLARE_DUMMY_MODULE(if_cue);
147 DRIVER_MODULE(cue, uhub, cue_driver, cue_devclass, usbd_driver_load, 0);
148 MODULE_DEPEND(cue, usb, 1, 1, 1);
149
150 #define CUE_SETBIT(sc, reg, x)                          \
151         cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
152
153 #define CUE_CLRBIT(sc, reg, x)                          \
154         cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
155
156 Static int
157 cue_csr_read_1(struct cue_softc *sc, int reg)
158 {
159         usb_device_request_t    req;
160         usbd_status             err;
161         u_int8_t                val = 0;
162
163         if (sc->cue_dying)
164                 return(0);
165
166         CUE_LOCK(sc);
167
168         req.bmRequestType = UT_READ_VENDOR_DEVICE;
169         req.bRequest = CUE_CMD_READREG;
170         USETW(req.wValue, 0);
171         USETW(req.wIndex, reg);
172         USETW(req.wLength, 1);
173
174         err = usbd_do_request(sc->cue_udev, &req, &val);
175
176         CUE_UNLOCK(sc);
177
178         if (err)
179                 return(0);
180
181         return(val);
182 }
183
184 Static int
185 cue_csr_read_2(struct cue_softc *sc, int reg)
186 {
187         usb_device_request_t    req;
188         usbd_status             err;
189         u_int16_t               val = 0;
190
191         if (sc->cue_dying)
192                 return(0);
193
194         CUE_LOCK(sc);
195
196         req.bmRequestType = UT_READ_VENDOR_DEVICE;
197         req.bRequest = CUE_CMD_READREG;
198         USETW(req.wValue, 0);
199         USETW(req.wIndex, reg);
200         USETW(req.wLength, 2);
201
202         err = usbd_do_request(sc->cue_udev, &req, &val);
203
204         CUE_UNLOCK(sc);
205
206         if (err)
207                 return(0);
208
209         return(val);
210 }
211
212 Static int
213 cue_csr_write_1(struct cue_softc *sc, int reg, int val)
214 {
215         usb_device_request_t    req;
216         usbd_status             err;
217
218         if (sc->cue_dying)
219                 return(0);
220
221         CUE_LOCK(sc);
222
223         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
224         req.bRequest = CUE_CMD_WRITEREG;
225         USETW(req.wValue, val);
226         USETW(req.wIndex, reg);
227         USETW(req.wLength, 0);
228
229         err = usbd_do_request(sc->cue_udev, &req, NULL);
230
231         CUE_UNLOCK(sc);
232
233         if (err)
234                 return(-1);
235
236         return(0);
237 }
238
239 #ifdef notdef
240 Static int
241 cue_csr_write_2(struct cue_softc *sc, int reg, int val)
242 {
243         usb_device_request_t    req;
244         usbd_status             err;
245
246         if (sc->cue_dying)
247                 return(0);
248
249         CUE_LOCK(sc);
250
251         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
252         req.bRequest = CUE_CMD_WRITEREG;
253         USETW(req.wValue, val);
254         USETW(req.wIndex, reg);
255         USETW(req.wLength, 0);
256
257         err = usbd_do_request(sc->cue_udev, &req, NULL);
258
259         CUE_UNLOCK(sc);
260
261         if (err)
262                 return(-1);
263
264         return(0);
265 }
266 #endif
267
268 Static int
269 cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len)
270 {
271         usb_device_request_t    req;
272         usbd_status             err;
273
274         if (sc->cue_dying)
275                 return(0);
276
277         CUE_LOCK(sc);
278
279         if (cmd == CUE_CMD_READSRAM)
280                 req.bmRequestType = UT_READ_VENDOR_DEVICE;
281         else
282                 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
283         req.bRequest = cmd;
284         USETW(req.wValue, 0);
285         USETW(req.wIndex, addr);
286         USETW(req.wLength, len);
287
288         err = usbd_do_request(sc->cue_udev, &req, buf);
289
290         CUE_UNLOCK(sc);
291
292         if (err)
293                 return(-1);
294
295         return(0);
296 }
297
298 Static int
299 cue_getmac(struct cue_softc *sc, void *buf)
300 {
301         usb_device_request_t    req;
302         usbd_status             err;
303
304         if (sc->cue_dying)
305                 return(0);
306
307         CUE_LOCK(sc);
308
309         req.bmRequestType = UT_READ_VENDOR_DEVICE;
310         req.bRequest = CUE_CMD_GET_MACADDR;
311         USETW(req.wValue, 0);
312         USETW(req.wIndex, 0);
313         USETW(req.wLength, ETHER_ADDR_LEN);
314
315         err = usbd_do_request(sc->cue_udev, &req, buf);
316
317         CUE_UNLOCK(sc);
318
319         if (err) {
320                 printf("cue%d: read MAC address failed\n", sc->cue_unit);
321                 return(-1);
322         }
323
324         return(0);
325 }
326
327 #define CUE_POLY        0xEDB88320
328 #define CUE_BITS        9
329
330 Static uint32_t
331 cue_mchash(const uint8_t *addr)
332 {
333         uint32_t crc;
334         int idx, bit;
335         uint8_t data;
336
337         /* Compute CRC for the address value. */
338         crc = 0xFFFFFFFF; /* initial value */
339
340         for (idx = 0; idx < 6; idx++) {
341                 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
342                         crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0);
343         }
344
345         return (crc & ((1 << CUE_BITS) - 1));
346 }
347
348 Static void
349 cue_setmulti(struct cue_softc *sc)
350 {
351         struct ifnet            *ifp;
352         struct ifmultiaddr      *ifma;
353         u_int32_t               h = 0, i;
354
355         ifp = &sc->arpcom.ac_if;
356
357         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
358                 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
359                         sc->cue_mctab[i] = 0xFF;
360                 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
361                     &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
362                 return;
363         }
364
365         /* first, zot all the existing hash bits */
366         for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
367                 sc->cue_mctab[i] = 0;
368
369         /* now program new ones */
370 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
371         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
372 #else
373         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
374 #endif
375         {
376                 if (ifma->ifma_addr->sa_family != AF_LINK)
377                         continue;
378                 h = cue_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
379                 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
380         }
381
382         /*
383          * Also include the broadcast address in the filter
384          * so we can receive broadcast frames.
385          */
386         if (ifp->if_flags & IFF_BROADCAST) {
387                 h = cue_mchash(ifp->if_broadcastaddr);
388                 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
389         }
390
391         cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
392             &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
393
394         return;
395 }
396
397 Static void
398 cue_reset(struct cue_softc *sc)
399 {
400         usb_device_request_t    req;
401         usbd_status             err;
402
403         if (sc->cue_dying)
404                 return;
405
406         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
407         req.bRequest = CUE_CMD_RESET;
408         USETW(req.wValue, 0);
409         USETW(req.wIndex, 0);
410         USETW(req.wLength, 0);
411         err = usbd_do_request(sc->cue_udev, &req, NULL);
412         if (err)
413                 printf("cue%d: reset failed\n", sc->cue_unit);
414
415         /* Wait a little while for the chip to get its brains in order. */
416         DELAY(1000);
417         return;
418 }
419
420 /*
421  * Probe for a Pegasus chip.
422  */
423 USB_MATCH(cue)
424 {
425         USB_MATCH_START(cue, uaa);
426         struct cue_type                 *t;
427
428         if (!uaa->iface)
429                 return(UMATCH_NONE);
430
431         t = cue_devs;
432         while(t->cue_vid) {
433                 if (uaa->vendor == t->cue_vid &&
434                     uaa->product == t->cue_did) {
435                         return(UMATCH_VENDOR_PRODUCT);
436                 }
437                 t++;
438         }
439
440         return(UMATCH_NONE);
441 }
442
443 /*
444  * Attach the interface. Allocate softc structures, do ifmedia
445  * setup and ethernet/BPF attach.
446  */
447 USB_ATTACH(cue)
448 {
449         USB_ATTACH_START(cue, sc, uaa);
450         char                    devinfo[1024];
451         u_char                  eaddr[ETHER_ADDR_LEN];
452         struct ifnet            *ifp;
453         usb_interface_descriptor_t      *id;
454         usb_endpoint_descriptor_t       *ed;
455         int                     i;
456
457         bzero(sc, sizeof(struct cue_softc));
458         sc->cue_iface = uaa->iface;
459         sc->cue_udev = uaa->device;
460         sc->cue_unit = device_get_unit(self);
461         callout_init(&sc->cue_stat_timer);
462
463         if (usbd_set_config_no(sc->cue_udev, CUE_CONFIG_NO, 0)) {
464                 printf("cue%d: getting interface handle failed\n",
465                     sc->cue_unit);
466                 USB_ATTACH_ERROR_RETURN;
467         }
468
469         id = usbd_get_interface_descriptor(uaa->iface);
470
471         usbd_devinfo(uaa->device, 0, devinfo);
472         device_set_desc_copy(self, devinfo);
473         printf("%s: %s\n", USBDEVNAME(self), devinfo);
474
475         /* Find endpoints. */
476         for (i = 0; i < id->bNumEndpoints; i++) {
477                 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
478                 if (!ed) {
479                         printf("cue%d: couldn't get ep %d\n",
480                             sc->cue_unit, i);
481                         USB_ATTACH_ERROR_RETURN;
482                 }
483                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
484                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
485                         sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
486                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
487                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
488                         sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
489                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
490                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
491                         sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
492                 }
493         }
494
495 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
496         mtx_init(&sc->cue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
497             MTX_DEF | MTX_RECURSE);
498 #endif
499         CUE_LOCK(sc);
500
501 #ifdef notdef
502         /* Reset the adapter. */
503         cue_reset(sc);
504 #endif
505         /*
506          * Get station address.
507          */
508         cue_getmac(sc, &eaddr);
509
510         ifp = &sc->arpcom.ac_if;
511         ifp->if_softc = sc;
512         if_initname(ifp, "cue", sc->cue_unit);
513         ifp->if_mtu = ETHERMTU;
514         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
515         ifp->if_ioctl = cue_ioctl;
516         ifp->if_start = cue_start;
517         ifp->if_watchdog = cue_watchdog;
518         ifp->if_init = cue_init;
519         ifp->if_baudrate = 10000000;
520         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
521         ifq_set_ready(&ifp->if_snd);
522
523         /*
524          * Call MI attach routine.
525          */
526         ether_ifattach(ifp, eaddr);
527         usb_register_netisr();
528         sc->cue_dying = 0;
529
530         CUE_UNLOCK(sc);
531         USB_ATTACH_SUCCESS_RETURN;
532 }
533
534 Static int
535 cue_detach(device_ptr_t dev)
536 {
537         struct cue_softc        *sc;
538         struct ifnet            *ifp;
539
540         sc = device_get_softc(dev);
541         CUE_LOCK(sc);
542         ifp = &sc->arpcom.ac_if;
543
544         sc->cue_dying = 1;
545         callout_stop(&sc->cue_stat_timer);
546         ether_ifdetach(ifp);
547
548         if (sc->cue_ep[CUE_ENDPT_TX] != NULL)
549                 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
550         if (sc->cue_ep[CUE_ENDPT_RX] != NULL)
551                 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
552         if (sc->cue_ep[CUE_ENDPT_INTR] != NULL)
553                 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
554
555         CUE_UNLOCK(sc);
556 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
557         mtx_destroy(&sc->cue_mtx);
558 #endif
559
560         return(0);
561 }
562
563 /*
564  * Initialize an RX descriptor and attach an MBUF cluster.
565  */
566 Static int
567 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
568 {
569         struct mbuf             *m_new = NULL;
570
571         if (m == NULL) {
572                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
573                 if (m_new == NULL) {
574                         printf("cue%d: no memory for rx list "
575                             "-- packet dropped!\n", sc->cue_unit);
576                         return(ENOBUFS);
577                 }
578
579                 MCLGET(m_new, MB_DONTWAIT);
580                 if (!(m_new->m_flags & M_EXT)) {
581                         printf("cue%d: no memory for rx list "
582                             "-- packet dropped!\n", sc->cue_unit);
583                         m_freem(m_new);
584                         return(ENOBUFS);
585                 }
586                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
587         } else {
588                 m_new = m;
589                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
590                 m_new->m_data = m_new->m_ext.ext_buf;
591         }
592
593         m_adj(m_new, ETHER_ALIGN);
594         c->cue_mbuf = m_new;
595
596         return(0);
597 }
598
599 Static int
600 cue_rx_list_init(struct cue_softc *sc)
601 {
602         struct cue_cdata        *cd;
603         struct cue_chain        *c;
604         int                     i;
605
606         cd = &sc->cue_cdata;
607         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
608                 c = &cd->cue_rx_chain[i];
609                 c->cue_sc = sc;
610                 c->cue_idx = i;
611                 if (cue_newbuf(sc, c, NULL) == ENOBUFS)
612                         return(ENOBUFS);
613                 if (c->cue_xfer == NULL) {
614                         c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
615                         if (c->cue_xfer == NULL)
616                                 return(ENOBUFS);
617                 }
618         }
619
620         return(0);
621 }
622
623 Static int
624 cue_tx_list_init(struct cue_softc *sc)
625 {
626         struct cue_cdata        *cd;
627         struct cue_chain        *c;
628         int                     i;
629
630         cd = &sc->cue_cdata;
631         for (i = 0; i < CUE_TX_LIST_CNT; i++) {
632                 c = &cd->cue_tx_chain[i];
633                 c->cue_sc = sc;
634                 c->cue_idx = i;
635                 c->cue_mbuf = NULL;
636                 if (c->cue_xfer == NULL) {
637                         c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
638                         if (c->cue_xfer == NULL)
639                                 return(ENOBUFS);
640                 }
641                 c->cue_buf = malloc(CUE_BUFSZ, M_USBDEV, M_WAITOK);
642         }
643
644         return(0);
645 }
646
647 Static void
648 cue_rxstart(struct ifnet *ifp)
649 {
650         struct cue_softc        *sc;
651         struct cue_chain        *c;
652
653         sc = ifp->if_softc;
654         CUE_LOCK(sc);
655         c = &sc->cue_cdata.cue_rx_chain[sc->cue_cdata.cue_rx_prod];
656
657         if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
658                 ifp->if_ierrors++;
659                 CUE_UNLOCK(sc);
660                 return;
661         }
662
663         /* Setup new transfer. */
664         usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
665             c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK,
666             USBD_NO_TIMEOUT, cue_rxeof);
667         usbd_transfer(c->cue_xfer);
668         CUE_UNLOCK(sc);
669
670         return;
671 }
672
673 /*
674  * A frame has been uploaded: pass the resulting mbuf chain up to
675  * the higher level protocols.
676  */
677 Static void
678 cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
679 {
680         struct cue_softc        *sc;
681         struct cue_chain        *c;
682         struct mbuf             *m;
683         struct ifnet            *ifp;
684         int                     total_len = 0;
685         u_int16_t               len;
686
687         c = priv;
688         sc = c->cue_sc;
689         CUE_LOCK(sc);
690         ifp = &sc->arpcom.ac_if;
691
692         if (!(ifp->if_flags & IFF_RUNNING)) {
693                 CUE_UNLOCK(sc);
694                 return;
695         }
696
697         if (status != USBD_NORMAL_COMPLETION) {
698                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
699                         CUE_UNLOCK(sc);
700                         return;
701                 }
702                 if (usbd_ratecheck(&sc->cue_rx_notice))
703                         printf("cue%d: usb error on rx: %s\n", sc->cue_unit,
704                             usbd_errstr(status));
705                 if (status == USBD_STALLED)
706                         usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]);
707                 goto done;
708         }
709
710         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
711
712         m = c->cue_mbuf;
713         len = *mtod(m, u_int16_t *);
714
715         /* No errors; receive the packet. */
716         total_len = len;
717
718         if (len < sizeof(struct ether_header)) {
719                 ifp->if_ierrors++;
720                 goto done;
721         }
722
723         ifp->if_ipackets++;
724         m_adj(m, sizeof(u_int16_t));
725         m->m_pkthdr.rcvif = ifp;
726         m->m_pkthdr.len = m->m_len = total_len;
727
728         /* Put the packet on the special USB input queue. */
729         usb_ether_input(m);
730         cue_rxstart(ifp);
731
732         CUE_UNLOCK(sc);
733
734         return;
735 done:
736         /* Setup new transfer. */
737         usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
738             c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK,
739             USBD_NO_TIMEOUT, cue_rxeof);
740         usbd_transfer(c->cue_xfer);
741         CUE_UNLOCK(sc);
742
743         return;
744 }
745
746 /*
747  * A frame was downloaded to the chip. It's safe for us to clean up
748  * the list buffers.
749  */
750
751 Static void
752 cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
753 {
754         struct cue_softc        *sc;
755         struct cue_chain        *c;
756         struct ifnet            *ifp;
757         usbd_status             err;
758
759         c = priv;
760         sc = c->cue_sc;
761         CUE_LOCK(sc);
762         ifp = &sc->arpcom.ac_if;
763
764         if (status != USBD_NORMAL_COMPLETION) {
765                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
766                         CUE_UNLOCK(sc);
767                         return;
768                 }
769                 printf("cue%d: usb error on tx: %s\n", sc->cue_unit,
770                     usbd_errstr(status));
771                 if (status == USBD_STALLED)
772                         usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
773                 CUE_UNLOCK(sc);
774                 return;
775         }
776
777         ifp->if_timer = 0;
778         ifp->if_flags &= ~IFF_OACTIVE;
779         usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &err);
780
781         if (c->cue_mbuf != NULL) {
782                 m_freem(c->cue_mbuf);
783                 c->cue_mbuf = NULL;
784         }
785
786         if (err)
787                 ifp->if_oerrors++;
788         else
789                 ifp->if_opackets++;
790
791         if (!ifq_is_empty(&ifp->if_snd))
792                 (*ifp->if_start)(ifp);
793
794         CUE_UNLOCK(sc);
795
796         return;
797 }
798
799 Static void
800 cue_tick(void *xsc)
801 {
802         struct cue_softc        *sc;
803         struct ifnet            *ifp;
804
805         sc = xsc;
806
807         if (sc == NULL)
808                 return;
809
810         CUE_LOCK(sc);
811
812         ifp = &sc->arpcom.ac_if;
813
814         ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
815         ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
816         ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
817
818         if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
819                 ifp->if_ierrors++;
820
821         callout_reset(&sc->cue_stat_timer, hz, cue_tick, sc);
822
823         CUE_UNLOCK(sc);
824
825         return;
826 }
827
828 Static int
829 cue_encap(struct cue_softc *sc, struct mbuf *m, int idx)
830 {
831         int                     total_len;
832         struct cue_chain        *c;
833         usbd_status             err;
834
835         c = &sc->cue_cdata.cue_tx_chain[idx];
836
837         /*
838          * Copy the mbuf data into a contiguous buffer, leaving two
839          * bytes at the beginning to hold the frame length.
840          */
841         m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
842         c->cue_mbuf = m;
843
844         total_len = m->m_pkthdr.len + 2;
845
846         /* The first two bytes are the frame length */
847         c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
848         c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
849
850         usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
851             c, c->cue_buf, total_len, 0, 10000, cue_txeof);
852
853         /* Transmit */
854         err = usbd_transfer(c->cue_xfer);
855         if (err != USBD_IN_PROGRESS) {
856                 cue_stop(sc);
857                 return(EIO);
858         }
859
860         sc->cue_cdata.cue_tx_cnt++;
861
862         return(0);
863 }
864
865 Static void
866 cue_start(struct ifnet *ifp)
867 {
868         struct cue_softc        *sc;
869         struct mbuf             *m_head = NULL;
870
871         sc = ifp->if_softc;
872         CUE_LOCK(sc);
873
874         if (ifp->if_flags & IFF_OACTIVE) {
875                 CUE_UNLOCK(sc);
876                 return;
877         }
878
879         m_head = ifq_poll(&ifp->if_snd);
880         if (m_head == NULL) {
881                 CUE_UNLOCK(sc);
882                 return;
883         }
884
885         if (cue_encap(sc, m_head, 0)) {
886                 ifp->if_flags |= IFF_OACTIVE;
887                 CUE_UNLOCK(sc);
888                 return;
889         }
890         m_head = ifq_dequeue(&ifp->if_snd);
891
892         /*
893          * If there's a BPF listener, bounce a copy of this frame
894          * to him.
895          */
896         BPF_MTAP(ifp, m_head);
897
898         ifp->if_flags |= IFF_OACTIVE;
899
900         /*
901          * Set a timeout in case the chip goes out to lunch.
902          */
903         ifp->if_timer = 5;
904         CUE_UNLOCK(sc);
905
906         return;
907 }
908
909 Static void
910 cue_init(void *xsc)
911 {
912         struct cue_softc        *sc = xsc;
913         struct ifnet            *ifp = &sc->arpcom.ac_if;
914         struct cue_chain        *c;
915         usbd_status             err;
916         int                     i;
917
918         if (ifp->if_flags & IFF_RUNNING)
919                 return;
920
921         CUE_LOCK(sc);
922
923         /*
924          * Cancel pending I/O and free all RX/TX buffers.
925          */
926 #ifdef foo
927         cue_reset(sc);
928 #endif
929
930         /* Set MAC address */
931         for (i = 0; i < ETHER_ADDR_LEN; i++)
932                 cue_csr_write_1(sc, CUE_PAR0 - i, sc->arpcom.ac_enaddr[i]);
933
934         /* Enable RX logic. */
935         cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON|CUE_ETHCTL_MCAST_ON);
936
937          /* If we want promiscuous mode, set the allframes bit. */
938         if (ifp->if_flags & IFF_PROMISC) {
939                 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
940         } else {
941                 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
942         }
943
944         /* Init TX ring. */
945         if (cue_tx_list_init(sc) == ENOBUFS) {
946                 printf("cue%d: tx list init failed\n", sc->cue_unit);
947                 CUE_UNLOCK(sc);
948                 return;
949         }
950
951         /* Init RX ring. */
952         if (cue_rx_list_init(sc) == ENOBUFS) {
953                 printf("cue%d: rx list init failed\n", sc->cue_unit);
954                 CUE_UNLOCK(sc);
955                 return;
956         }
957
958         /* Load the multicast filter. */
959         cue_setmulti(sc);
960
961         /*
962          * Set the number of RX and TX buffers that we want
963          * to reserve inside the ASIC.
964          */
965         cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
966         cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
967
968         /* Set advanced operation modes. */
969         cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
970             CUE_AOP_EMBED_RXLEN|0x01); /* 1 wait state */
971
972         /* Program the LED operation. */
973         cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
974
975         /* Open RX and TX pipes. */
976         err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
977             USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
978         if (err) {
979                 printf("cue%d: open rx pipe failed: %s\n",
980                     sc->cue_unit, usbd_errstr(err));
981                 CUE_UNLOCK(sc);
982                 return;
983         }
984         err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
985             USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
986         if (err) {
987                 printf("cue%d: open tx pipe failed: %s\n",
988                     sc->cue_unit, usbd_errstr(err));
989                 CUE_UNLOCK(sc);
990                 return;
991         }
992
993         /* Start up the receive pipe. */
994         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
995                 c = &sc->cue_cdata.cue_rx_chain[i];
996                 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
997                     c, mtod(c->cue_mbuf, char *), CUE_BUFSZ,
998                     USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
999                 usbd_transfer(c->cue_xfer);
1000         }
1001
1002         ifp->if_flags |= IFF_RUNNING;
1003         ifp->if_flags &= ~IFF_OACTIVE;
1004
1005         CUE_UNLOCK(sc);
1006
1007         callout_reset(&sc->cue_stat_timer, hz, cue_tick, sc);
1008 }
1009
1010 Static int
1011 cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1012 {
1013         struct cue_softc        *sc = ifp->if_softc;
1014         int                     error = 0;
1015
1016         CUE_LOCK(sc);
1017
1018         switch(command) {
1019         case SIOCSIFFLAGS:
1020                 if (ifp->if_flags & IFF_UP) {
1021                         if (ifp->if_flags & IFF_RUNNING &&
1022                             ifp->if_flags & IFF_PROMISC &&
1023                             !(sc->cue_if_flags & IFF_PROMISC)) {
1024                                 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1025                                 cue_setmulti(sc);
1026                         } else if (ifp->if_flags & IFF_RUNNING &&
1027                             !(ifp->if_flags & IFF_PROMISC) &&
1028                             sc->cue_if_flags & IFF_PROMISC) {
1029                                 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1030                                 cue_setmulti(sc);
1031                         } else if (!(ifp->if_flags & IFF_RUNNING))
1032                                 cue_init(sc);
1033                 } else {
1034                         if (ifp->if_flags & IFF_RUNNING)
1035                                 cue_stop(sc);
1036                 }
1037                 sc->cue_if_flags = ifp->if_flags;
1038                 error = 0;
1039                 break;
1040         case SIOCADDMULTI:
1041         case SIOCDELMULTI:
1042                 cue_setmulti(sc);
1043                 error = 0;
1044                 break;
1045         default:
1046                 error = ether_ioctl(ifp, command, data);
1047                 break;
1048         }
1049
1050         CUE_UNLOCK(sc);
1051
1052         return(error);
1053 }
1054
1055 Static void
1056 cue_watchdog(struct ifnet *ifp)
1057 {
1058         struct cue_softc        *sc;
1059         struct cue_chain        *c;
1060         usbd_status             stat;
1061
1062         sc = ifp->if_softc;
1063         CUE_LOCK(sc);
1064
1065         ifp->if_oerrors++;
1066         printf("cue%d: watchdog timeout\n", sc->cue_unit);
1067
1068         c = &sc->cue_cdata.cue_tx_chain[0];
1069         usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1070         cue_txeof(c->cue_xfer, c, stat);
1071
1072         if (!ifq_is_empty(&ifp->if_snd))
1073                 cue_start(ifp);
1074         CUE_UNLOCK(sc);
1075
1076         return;
1077 }
1078
1079 /*
1080  * Stop the adapter and free any mbufs allocated to the
1081  * RX and TX lists.
1082  */
1083 Static void
1084 cue_stop(struct cue_softc *sc)
1085 {
1086         usbd_status             err;
1087         struct ifnet            *ifp;
1088         int                     i;
1089
1090         CUE_LOCK(sc);
1091
1092         ifp = &sc->arpcom.ac_if;
1093         ifp->if_timer = 0;
1094
1095         cue_csr_write_1(sc, CUE_ETHCTL, 0);
1096         cue_reset(sc);
1097         callout_stop(&sc->cue_stat_timer);
1098
1099         /* Stop transfers. */
1100         if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1101                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1102                 if (err) {
1103                         printf("cue%d: abort rx pipe failed: %s\n",
1104                         sc->cue_unit, usbd_errstr(err));
1105                 }
1106                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1107                 if (err) {
1108                         printf("cue%d: close rx pipe failed: %s\n",
1109                         sc->cue_unit, usbd_errstr(err));
1110                 }
1111                 sc->cue_ep[CUE_ENDPT_RX] = NULL;
1112         }
1113
1114         if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1115                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1116                 if (err) {
1117                         printf("cue%d: abort tx pipe failed: %s\n",
1118                         sc->cue_unit, usbd_errstr(err));
1119                 }
1120                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1121                 if (err) {
1122                         printf("cue%d: close tx pipe failed: %s\n",
1123                             sc->cue_unit, usbd_errstr(err));
1124                 }
1125                 sc->cue_ep[CUE_ENDPT_TX] = NULL;
1126         }
1127
1128         if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1129                 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1130                 if (err) {
1131                         printf("cue%d: abort intr pipe failed: %s\n",
1132                         sc->cue_unit, usbd_errstr(err));
1133                 }
1134                 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1135                 if (err) {
1136                         printf("cue%d: close intr pipe failed: %s\n",
1137                             sc->cue_unit, usbd_errstr(err));
1138                 }
1139                 sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1140         }
1141
1142         /* Free RX resources. */
1143         for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1144                 if (sc->cue_cdata.cue_rx_chain[i].cue_buf != NULL) {
1145                         free(sc->cue_cdata.cue_rx_chain[i].cue_buf, M_USBDEV);
1146                         sc->cue_cdata.cue_rx_chain[i].cue_buf = NULL;
1147                 }
1148                 if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
1149                         m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
1150                         sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
1151                 }
1152                 if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1153                         usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1154                         sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1155                 }
1156         }
1157
1158         /* Free TX resources. */
1159         for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1160                 if (sc->cue_cdata.cue_tx_chain[i].cue_buf != NULL) {
1161                         free(sc->cue_cdata.cue_tx_chain[i].cue_buf, M_USBDEV);
1162                         sc->cue_cdata.cue_tx_chain[i].cue_buf = NULL;
1163                 }
1164                 if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1165                         m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1166                         sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1167                 }
1168                 if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1169                         usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1170                         sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1171                 }
1172         }
1173
1174         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1175         CUE_UNLOCK(sc);
1176
1177         return;
1178 }
1179
1180 /*
1181  * Stop all chip I/O so that the kernel's probe routines don't
1182  * get confused by errant DMAs when rebooting.
1183  */
1184 Static void
1185 cue_shutdown(device_ptr_t dev)
1186 {
1187         struct cue_softc        *sc;
1188
1189         sc = device_get_softc(dev);
1190
1191         CUE_LOCK(sc);
1192         cue_reset(sc);
1193         cue_stop(sc);
1194         CUE_UNLOCK(sc);
1195
1196         return;
1197 }