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