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