if_cue.c revision 1.77 1 /* $NetBSD: if_cue.c,v 1.77 2018/01/21 13:57:12 skrll Exp $ */
2 /*
3 * Copyright (c) 1997, 1998, 1999, 2000
4 * Bill Paul <wpaul (at) ee.columbia.edu>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $FreeBSD: src/sys/dev/usb/if_cue.c,v 1.4 2000/01/16 22:45:06 wpaul Exp $
34 */
35
36 /*
37 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
38 * adapters and others.
39 *
40 * Written by Bill Paul <wpaul (at) ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45 /*
46 * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
47 * RX filter uses a 512-bit multicast hash table, single perfect entry
48 * for the station address, and promiscuous mode. Unlike the ADMtek
49 * and KLSI chips, the CATC ASIC supports read and write combining
50 * mode where multiple packets can be transfered using a single bulk
51 * transaction, which helps performance a great deal.
52 */
53
54 /*
55 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
56 */
57
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: if_cue.c,v 1.77 2018/01/21 13:57:12 skrll Exp $");
60
61 #ifdef _KERNEL_OPT
62 #include "opt_inet.h"
63 #include "opt_usb.h"
64 #endif
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/callout.h>
69 #include <sys/sockio.h>
70 #include <sys/mbuf.h>
71 #include <sys/kernel.h>
72 #include <sys/socket.h>
73 #include <sys/bus.h>
74 #include <sys/device.h>
75
76 #include <net/if.h>
77 #include <net/if_arp.h>
78 #include <net/if_dl.h>
79 #include <net/bpf.h>
80 #include <net/if_ether.h>
81
82 #ifdef INET
83 #include <netinet/in.h>
84 #include <netinet/if_inarp.h>
85 #endif
86
87 #include <dev/usb/usb.h>
88 #include <dev/usb/usbdi.h>
89 #include <dev/usb/usbdi_util.h>
90 #include <dev/usb/usbdivar.h>
91 #include <dev/usb/usbdevs.h>
92
93 #include <dev/usb/if_cuereg.h>
94
95 #ifdef CUE_DEBUG
96 #define DPRINTF(x) if (cuedebug) printf x
97 #define DPRINTFN(n,x) if (cuedebug >= (n)) printf x
98 int cuedebug = 0;
99 #else
100 #define DPRINTF(x)
101 #define DPRINTFN(n,x)
102 #endif
103
104 /*
105 * Various supported device vendors/products.
106 */
107 Static struct usb_devno cue_devs[] = {
108 { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE },
109 { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 },
110 { USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK },
111 /* Belkin F5U111 adapter covered by NETMATE entry */
112 };
113 #define cue_lookup(v, p) (usb_lookup(cue_devs, v, p))
114
115 int cue_match(device_t, cfdata_t, void *);
116 void cue_attach(device_t, device_t, void *);
117 int cue_detach(device_t, int);
118 int cue_activate(device_t, enum devact);
119 extern struct cfdriver cue_cd;
120 CFATTACH_DECL_NEW(cue, sizeof(struct cue_softc), cue_match, cue_attach,
121 cue_detach, cue_activate);
122
123 Static int cue_open_pipes(struct cue_softc *);
124 Static int cue_tx_list_init(struct cue_softc *);
125 Static int cue_rx_list_init(struct cue_softc *);
126 Static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *);
127 Static int cue_send(struct cue_softc *, struct mbuf *, int);
128 Static void cue_rxeof(struct usbd_xfer *, void *, usbd_status);
129 Static void cue_txeof(struct usbd_xfer *, void *, usbd_status);
130 Static void cue_tick(void *);
131 Static void cue_tick_task(void *);
132 Static void cue_start(struct ifnet *);
133 Static int cue_ioctl(struct ifnet *, u_long, void *);
134 Static void cue_init(void *);
135 Static void cue_stop(struct cue_softc *);
136 Static void cue_watchdog(struct ifnet *);
137
138 Static void cue_setmulti(struct cue_softc *);
139 Static uint32_t cue_crc(const char *);
140 Static void cue_reset(struct cue_softc *);
141
142 Static int cue_csr_read_1(struct cue_softc *, int);
143 Static int cue_csr_write_1(struct cue_softc *, int, int);
144 Static int cue_csr_read_2(struct cue_softc *, int);
145 #if 0
146 Static int cue_csr_write_2(struct cue_softc *, int, int);
147 #endif
148 Static int cue_mem(struct cue_softc *, int, int, void *, int);
149 Static int cue_getmac(struct cue_softc *, void *);
150
151 #define CUE_SETBIT(sc, reg, x) \
152 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
153
154 #define CUE_CLRBIT(sc, reg, x) \
155 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
156
157 Static int
158 cue_csr_read_1(struct cue_softc *sc, int reg)
159 {
160 usb_device_request_t req;
161 usbd_status err;
162 uint8_t val = 0;
163
164 if (sc->cue_dying)
165 return 0;
166
167 req.bmRequestType = UT_READ_VENDOR_DEVICE;
168 req.bRequest = CUE_CMD_READREG;
169 USETW(req.wValue, 0);
170 USETW(req.wIndex, reg);
171 USETW(req.wLength, 1);
172
173 err = usbd_do_request(sc->cue_udev, &req, &val);
174
175 if (err) {
176 DPRINTF(("%s: cue_csr_read_1: reg=0x%x err=%s\n",
177 device_xname(sc->cue_dev), reg, usbd_errstr(err)));
178 return 0;
179 }
180
181 DPRINTFN(10,("%s: cue_csr_read_1 reg=0x%x val=0x%x\n",
182 device_xname(sc->cue_dev), reg, val));
183
184 return val;
185 }
186
187 Static int
188 cue_csr_read_2(struct cue_softc *sc, int reg)
189 {
190 usb_device_request_t req;
191 usbd_status err;
192 uWord val;
193
194 if (sc->cue_dying)
195 return 0;
196
197 req.bmRequestType = UT_READ_VENDOR_DEVICE;
198 req.bRequest = CUE_CMD_READREG;
199 USETW(req.wValue, 0);
200 USETW(req.wIndex, reg);
201 USETW(req.wLength, 2);
202
203 err = usbd_do_request(sc->cue_udev, &req, &val);
204
205 DPRINTFN(10,("%s: cue_csr_read_2 reg=0x%x val=0x%x\n",
206 device_xname(sc->cue_dev), reg, UGETW(val)));
207
208 if (err) {
209 DPRINTF(("%s: cue_csr_read_2: reg=0x%x err=%s\n",
210 device_xname(sc->cue_dev), reg, usbd_errstr(err)));
211 return 0;
212 }
213
214 return UGETW(val);
215 }
216
217 Static int
218 cue_csr_write_1(struct cue_softc *sc, int reg, int val)
219 {
220 usb_device_request_t req;
221 usbd_status err;
222
223 if (sc->cue_dying)
224 return 0;
225
226 DPRINTFN(10,("%s: cue_csr_write_1 reg=0x%x val=0x%x\n",
227 device_xname(sc->cue_dev), reg, val));
228
229 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
230 req.bRequest = CUE_CMD_WRITEREG;
231 USETW(req.wValue, val);
232 USETW(req.wIndex, reg);
233 USETW(req.wLength, 0);
234
235 err = usbd_do_request(sc->cue_udev, &req, NULL);
236
237 if (err) {
238 DPRINTF(("%s: cue_csr_write_1: reg=0x%x err=%s\n",
239 device_xname(sc->cue_dev), reg, usbd_errstr(err)));
240 return -1;
241 }
242
243 DPRINTFN(20,("%s: cue_csr_write_1, after reg=0x%x val=0x%x\n",
244 device_xname(sc->cue_dev), reg, cue_csr_read_1(sc, reg)));
245
246 return 0;
247 }
248
249 #if 0
250 Static int
251 cue_csr_write_2(struct cue_softc *sc, int reg, int aval)
252 {
253 usb_device_request_t req;
254 usbd_status err;
255 uWord val;
256 int s;
257
258 if (sc->cue_dying)
259 return 0;
260
261 DPRINTFN(10,("%s: cue_csr_write_2 reg=0x%x val=0x%x\n",
262 device_xname(sc->cue_dev), reg, aval));
263
264 USETW(val, aval);
265 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
266 req.bRequest = CUE_CMD_WRITEREG;
267 USETW(req.wValue, val);
268 USETW(req.wIndex, reg);
269 USETW(req.wLength, 0);
270
271 err = usbd_do_request(sc->cue_udev, &req, NULL);
272
273 if (err) {
274 DPRINTF(("%s: cue_csr_write_2: reg=0x%x err=%s\n",
275 device_xname(sc->cue_dev), reg, usbd_errstr(err)));
276 return -1;
277 }
278
279 return 0;
280 }
281 #endif
282
283 Static int
284 cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len)
285 {
286 usb_device_request_t req;
287 usbd_status err;
288
289 DPRINTFN(10,("%s: cue_mem cmd=0x%x addr=0x%x len=%d\n",
290 device_xname(sc->cue_dev), cmd, addr, len));
291
292 if (cmd == CUE_CMD_READSRAM)
293 req.bmRequestType = UT_READ_VENDOR_DEVICE;
294 else
295 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
296 req.bRequest = cmd;
297 USETW(req.wValue, 0);
298 USETW(req.wIndex, addr);
299 USETW(req.wLength, len);
300
301 err = usbd_do_request(sc->cue_udev, &req, buf);
302
303 if (err) {
304 DPRINTF(("%s: cue_csr_mem: addr=0x%x err=%s\n",
305 device_xname(sc->cue_dev), addr, usbd_errstr(err)));
306 return -1;
307 }
308
309 return 0;
310 }
311
312 Static int
313 cue_getmac(struct cue_softc *sc, void *buf)
314 {
315 usb_device_request_t req;
316 usbd_status err;
317
318 DPRINTFN(10,("%s: cue_getmac\n", device_xname(sc->cue_dev)));
319
320 req.bmRequestType = UT_READ_VENDOR_DEVICE;
321 req.bRequest = CUE_CMD_GET_MACADDR;
322 USETW(req.wValue, 0);
323 USETW(req.wIndex, 0);
324 USETW(req.wLength, ETHER_ADDR_LEN);
325
326 err = usbd_do_request(sc->cue_udev, &req, buf);
327
328 if (err) {
329 printf("%s: read MAC address failed\n",
330 device_xname(sc->cue_dev));
331 return -1;
332 }
333
334 return 0;
335 }
336
337 #define CUE_POLY 0xEDB88320
338 #define CUE_BITS 9
339
340 Static uint32_t
341 cue_crc(const char *addr)
342 {
343 uint32_t idx, bit, data, crc;
344
345 /* Compute CRC for the address value. */
346 crc = 0xFFFFFFFF; /* initial value */
347
348 for (idx = 0; idx < 6; idx++) {
349 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
350 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0);
351 }
352
353 return crc & ((1 << CUE_BITS) - 1);
354 }
355
356 Static void
357 cue_setmulti(struct cue_softc *sc)
358 {
359 struct ifnet *ifp;
360 struct ether_multi *enm;
361 struct ether_multistep step;
362 uint32_t h, i;
363
364 ifp = GET_IFP(sc);
365
366 DPRINTFN(2,("%s: cue_setmulti if_flags=0x%x\n",
367 device_xname(sc->cue_dev), ifp->if_flags));
368
369 if (ifp->if_flags & IFF_PROMISC) {
370 allmulti:
371 ifp->if_flags |= IFF_ALLMULTI;
372 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
373 sc->cue_mctab[i] = 0xFF;
374 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
375 &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
376 return;
377 }
378
379 /* first, zot all the existing hash bits */
380 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
381 sc->cue_mctab[i] = 0;
382
383 /* now program new ones */
384 ETHER_FIRST_MULTI(step, &sc->cue_ec, enm);
385 while (enm != NULL) {
386 if (memcmp(enm->enm_addrlo,
387 enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
388 goto allmulti;
389
390 h = cue_crc(enm->enm_addrlo);
391 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
392 ETHER_NEXT_MULTI(step, enm);
393 }
394
395 ifp->if_flags &= ~IFF_ALLMULTI;
396
397 /*
398 * Also include the broadcast address in the filter
399 * so we can receive broadcast frames.
400 */
401 if (ifp->if_flags & IFF_BROADCAST) {
402 h = cue_crc(etherbroadcastaddr);
403 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
404 }
405
406 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
407 &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
408 }
409
410 Static void
411 cue_reset(struct cue_softc *sc)
412 {
413 usb_device_request_t req;
414 usbd_status err;
415
416 DPRINTFN(2,("%s: cue_reset\n", device_xname(sc->cue_dev)));
417
418 if (sc->cue_dying)
419 return;
420
421 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
422 req.bRequest = CUE_CMD_RESET;
423 USETW(req.wValue, 0);
424 USETW(req.wIndex, 0);
425 USETW(req.wLength, 0);
426
427 err = usbd_do_request(sc->cue_udev, &req, NULL);
428
429 if (err)
430 printf("%s: reset failed\n", device_xname(sc->cue_dev));
431
432 /* Wait a little while for the chip to get its brains in order. */
433 usbd_delay_ms(sc->cue_udev, 1);
434 }
435
436 /*
437 * Probe for a CATC chip.
438 */
439 int
440 cue_match(device_t parent, cfdata_t match, void *aux)
441 {
442 struct usb_attach_arg *uaa = aux;
443
444 return cue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
445 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
446 }
447
448 /*
449 * Attach the interface. Allocate softc structures, do ifmedia
450 * setup and ethernet/BPF attach.
451 */
452 void
453 cue_attach(device_t parent, device_t self, void *aux)
454 {
455 struct cue_softc *sc = device_private(self);
456 struct usb_attach_arg *uaa = aux;
457 char *devinfop;
458 int s;
459 u_char eaddr[ETHER_ADDR_LEN];
460 struct usbd_device * dev = uaa->uaa_device;
461 struct usbd_interface * iface;
462 usbd_status err;
463 struct ifnet *ifp;
464 usb_interface_descriptor_t *id;
465 usb_endpoint_descriptor_t *ed;
466 int i;
467
468 DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev));
469
470 sc->cue_dev = self;
471
472 aprint_naive("\n");
473 aprint_normal("\n");
474
475 devinfop = usbd_devinfo_alloc(dev, 0);
476 aprint_normal_dev(self, "%s\n", devinfop);
477 usbd_devinfo_free(devinfop);
478
479 err = usbd_set_config_no(dev, CUE_CONFIG_NO, 1);
480 if (err) {
481 aprint_error_dev(self, "failed to set configuration"
482 ", err=%s\n", usbd_errstr(err));
483 return;
484 }
485
486 sc->cue_udev = dev;
487 sc->cue_product = uaa->uaa_product;
488 sc->cue_vendor = uaa->uaa_vendor;
489
490 usb_init_task(&sc->cue_tick_task, cue_tick_task, sc, 0);
491 usb_init_task(&sc->cue_stop_task, (void (*)(void *))cue_stop, sc, 0);
492
493 err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &iface);
494 if (err) {
495 aprint_error_dev(self, "getting interface handle failed\n");
496 return;
497 }
498
499 sc->cue_iface = iface;
500 id = usbd_get_interface_descriptor(iface);
501
502 /* Find endpoints. */
503 for (i = 0; i < id->bNumEndpoints; i++) {
504 ed = usbd_interface2endpoint_descriptor(iface, i);
505 if (ed == NULL) {
506 aprint_error_dev(self, "couldn't get ep %d\n", i);
507 return;
508 }
509 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
510 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
511 sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
512 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
513 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
514 sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
515 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
516 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
517 sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
518 }
519 }
520
521 #if 0
522 /* Reset the adapter. */
523 cue_reset(sc);
524 #endif
525 /*
526 * Get station address.
527 */
528 cue_getmac(sc, &eaddr);
529
530 s = splnet();
531
532 /*
533 * A CATC chip was detected. Inform the world.
534 */
535 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
536
537 /* Initialize interface info.*/
538 ifp = GET_IFP(sc);
539 ifp->if_softc = sc;
540 ifp->if_mtu = ETHERMTU;
541 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
542 ifp->if_ioctl = cue_ioctl;
543 ifp->if_start = cue_start;
544 ifp->if_watchdog = cue_watchdog;
545 strlcpy(ifp->if_xname, device_xname(sc->cue_dev), IFNAMSIZ);
546
547 IFQ_SET_READY(&ifp->if_snd);
548
549 /* Attach the interface. */
550 if_attach(ifp);
551 ether_ifattach(ifp, eaddr);
552 rnd_attach_source(&sc->rnd_source, device_xname(sc->cue_dev),
553 RND_TYPE_NET, RND_FLAG_DEFAULT);
554
555 callout_init(&(sc->cue_stat_ch), 0);
556
557 sc->cue_attached = 1;
558 splx(s);
559
560 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev, sc->cue_dev);
561
562 return;
563 }
564
565 int
566 cue_detach(device_t self, int flags)
567 {
568 struct cue_softc *sc = device_private(self);
569 struct ifnet *ifp = GET_IFP(sc);
570 int s;
571
572 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
573
574 callout_stop(&sc->cue_stat_ch);
575 /*
576 * Remove any pending task. It cannot be executing because it run
577 * in the same thread as detach.
578 */
579 usb_rem_task(sc->cue_udev, &sc->cue_tick_task);
580 usb_rem_task(sc->cue_udev, &sc->cue_stop_task);
581
582 if (!sc->cue_attached) {
583 /* Detached before attached finished, so just bail out. */
584 return 0;
585 }
586
587 s = splusb();
588
589 if (ifp->if_flags & IFF_RUNNING)
590 cue_stop(sc);
591
592 rnd_detach_source(&sc->rnd_source);
593 ether_ifdetach(ifp);
594
595 if_detach(ifp);
596
597 #ifdef DIAGNOSTIC
598 if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
599 sc->cue_ep[CUE_ENDPT_RX] != NULL ||
600 sc->cue_ep[CUE_ENDPT_INTR] != NULL)
601 aprint_debug_dev(self, "detach has active endpoints\n");
602 #endif
603
604 sc->cue_attached = 0;
605 splx(s);
606
607 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev, sc->cue_dev);
608
609 return 0;
610 }
611
612 int
613 cue_activate(device_t self, enum devact act)
614 {
615 struct cue_softc *sc = device_private(self);
616
617 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
618
619 switch (act) {
620 case DVACT_DEACTIVATE:
621 /* Deactivate the interface. */
622 if_deactivate(&sc->cue_ec.ec_if);
623 sc->cue_dying = 1;
624 return 0;
625 default:
626 return EOPNOTSUPP;
627 }
628 }
629
630 /*
631 * Initialize an RX descriptor and attach an MBUF cluster.
632 */
633 Static int
634 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
635 {
636 struct mbuf *m_new = NULL;
637
638 if (m == NULL) {
639 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
640 if (m_new == NULL) {
641 printf("%s: no memory for rx list "
642 "-- packet dropped!\n", device_xname(sc->cue_dev));
643 return ENOBUFS;
644 }
645
646 MCLGET(m_new, M_DONTWAIT);
647 if (!(m_new->m_flags & M_EXT)) {
648 printf("%s: no memory for rx list "
649 "-- packet dropped!\n", device_xname(sc->cue_dev));
650 m_freem(m_new);
651 return ENOBUFS;
652 }
653 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
654 } else {
655 m_new = m;
656 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
657 m_new->m_data = m_new->m_ext.ext_buf;
658 }
659
660 m_adj(m_new, ETHER_ALIGN);
661 c->cue_mbuf = m_new;
662
663 return 0;
664 }
665
666 Static int
667 cue_rx_list_init(struct cue_softc *sc)
668 {
669 struct cue_cdata *cd;
670 struct cue_chain *c;
671 int i;
672
673 cd = &sc->cue_cdata;
674 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
675 c = &cd->cue_rx_chain[i];
676 c->cue_sc = sc;
677 c->cue_idx = i;
678 if (cue_newbuf(sc, c, NULL) == ENOBUFS)
679 return ENOBUFS;
680 if (c->cue_xfer == NULL) {
681 int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_RX],
682 CUE_BUFSZ, 0, 0, &c->cue_xfer);
683 if (error)
684 return error;
685 c->cue_buf = usbd_get_buffer(c->cue_xfer);
686 }
687 }
688
689 return 0;
690 }
691
692 Static int
693 cue_tx_list_init(struct cue_softc *sc)
694 {
695 struct cue_cdata *cd;
696 struct cue_chain *c;
697 int i;
698
699 cd = &sc->cue_cdata;
700 for (i = 0; i < CUE_TX_LIST_CNT; i++) {
701 c = &cd->cue_tx_chain[i];
702 c->cue_sc = sc;
703 c->cue_idx = i;
704 c->cue_mbuf = NULL;
705 if (c->cue_xfer == NULL) {
706 int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_TX],
707 CUE_BUFSZ, 0, 0, &c->cue_xfer);
708 if (error)
709 return error;
710 c->cue_buf = usbd_get_buffer(c->cue_xfer);
711 }
712 }
713
714 return 0;
715 }
716
717 /*
718 * A frame has been uploaded: pass the resulting mbuf chain up to
719 * the higher level protocols.
720 */
721 Static void
722 cue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
723 {
724 struct cue_chain *c = priv;
725 struct cue_softc *sc = c->cue_sc;
726 struct ifnet *ifp = GET_IFP(sc);
727 struct mbuf *m;
728 int total_len = 0;
729 uint16_t len;
730 int s;
731
732 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
733 __func__, status));
734
735 if (sc->cue_dying)
736 return;
737
738 if (!(ifp->if_flags & IFF_RUNNING))
739 return;
740
741 if (status != USBD_NORMAL_COMPLETION) {
742 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
743 return;
744 sc->cue_rx_errs++;
745 if (usbd_ratecheck(&sc->cue_rx_notice)) {
746 printf("%s: %u usb errors on rx: %s\n",
747 device_xname(sc->cue_dev), sc->cue_rx_errs,
748 usbd_errstr(status));
749 sc->cue_rx_errs = 0;
750 }
751 if (status == USBD_STALLED)
752 usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_RX]);
753 goto done;
754 }
755
756 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
757
758 memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
759
760 m = c->cue_mbuf;
761 len = UGETW(mtod(m, uint8_t *));
762
763 /* No errors; receive the packet. */
764 total_len = len;
765
766 if (len < sizeof(struct ether_header)) {
767 ifp->if_ierrors++;
768 goto done;
769 }
770
771 m_adj(m, sizeof(uint16_t));
772 m->m_pkthdr.len = m->m_len = total_len;
773
774 m_set_rcvif(m, ifp);
775
776 s = splnet();
777
778 /* XXX ugly */
779 if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
780 ifp->if_ierrors++;
781 goto done1;
782 }
783
784 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->cue_dev),
785 __func__, m->m_len));
786 if_percpuq_enqueue(ifp->if_percpuq, m);
787 done1:
788 splx(s);
789
790 done:
791
792 /* Setup new transfer. */
793 usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
794 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
795 usbd_transfer(c->cue_xfer);
796
797 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->cue_dev),
798 __func__));
799 }
800
801 /*
802 * A frame was downloaded to the chip. It's safe for us to clean up
803 * the list buffers.
804 */
805 Static void
806 cue_txeof(struct usbd_xfer *xfer, void *priv,
807 usbd_status status)
808 {
809 struct cue_chain *c = priv;
810 struct cue_softc *sc = c->cue_sc;
811 struct ifnet *ifp = GET_IFP(sc);
812 int s;
813
814 if (sc->cue_dying)
815 return;
816
817 s = splnet();
818
819 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
820 __func__, status));
821
822 ifp->if_timer = 0;
823 ifp->if_flags &= ~IFF_OACTIVE;
824
825 if (status != USBD_NORMAL_COMPLETION) {
826 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
827 splx(s);
828 return;
829 }
830 ifp->if_oerrors++;
831 printf("%s: usb error on tx: %s\n", device_xname(sc->cue_dev),
832 usbd_errstr(status));
833 if (status == USBD_STALLED)
834 usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_TX]);
835 splx(s);
836 return;
837 }
838
839 ifp->if_opackets++;
840
841 m_freem(c->cue_mbuf);
842 c->cue_mbuf = NULL;
843
844 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
845 cue_start(ifp);
846
847 splx(s);
848 }
849
850 Static void
851 cue_tick(void *xsc)
852 {
853 struct cue_softc *sc = xsc;
854
855 if (sc == NULL)
856 return;
857
858 if (sc->cue_dying)
859 return;
860
861 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
862
863 /* Perform statistics update in process context. */
864 usb_add_task(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER);
865 }
866
867 Static void
868 cue_tick_task(void *xsc)
869 {
870 struct cue_softc *sc = xsc;
871 struct ifnet *ifp;
872
873 if (sc->cue_dying)
874 return;
875
876 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
877
878 ifp = GET_IFP(sc);
879
880 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
881 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
882 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
883
884 if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
885 ifp->if_ierrors++;
886 }
887
888 Static int
889 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
890 {
891 int total_len;
892 struct cue_chain *c;
893 usbd_status err;
894
895 c = &sc->cue_cdata.cue_tx_chain[idx];
896
897 /*
898 * Copy the mbuf data into a contiguous buffer, leaving two
899 * bytes at the beginning to hold the frame length.
900 */
901 m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
902 c->cue_mbuf = m;
903
904 total_len = m->m_pkthdr.len + 2;
905
906 DPRINTFN(10,("%s: %s: total_len=%d\n",
907 device_xname(sc->cue_dev), __func__, total_len));
908
909 /* The first two bytes are the frame length */
910 c->cue_buf[0] = (uint8_t)m->m_pkthdr.len;
911 c->cue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
912
913 /* XXX 10000 */
914 usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, total_len, 0, 10000,
915 cue_txeof);
916
917 /* Transmit */
918 err = usbd_transfer(c->cue_xfer);
919 if (err != USBD_IN_PROGRESS) {
920 printf("%s: cue_send error=%s\n", device_xname(sc->cue_dev),
921 usbd_errstr(err));
922 /* Stop the interface from process context. */
923 usb_add_task(sc->cue_udev, &sc->cue_stop_task,
924 USB_TASKQ_DRIVER);
925 return EIO;
926 }
927
928 sc->cue_cdata.cue_tx_cnt++;
929
930 return 0;
931 }
932
933 Static void
934 cue_start(struct ifnet *ifp)
935 {
936 struct cue_softc *sc = ifp->if_softc;
937 struct mbuf *m_head = NULL;
938
939 if (sc->cue_dying)
940 return;
941
942 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
943
944 if (ifp->if_flags & IFF_OACTIVE)
945 return;
946
947 IFQ_POLL(&ifp->if_snd, m_head);
948 if (m_head == NULL)
949 return;
950
951 if (cue_send(sc, m_head, 0)) {
952 ifp->if_flags |= IFF_OACTIVE;
953 return;
954 }
955
956 IFQ_DEQUEUE(&ifp->if_snd, m_head);
957
958 /*
959 * If there's a BPF listener, bounce a copy of this frame
960 * to him.
961 */
962 bpf_mtap(ifp, m_head);
963
964 ifp->if_flags |= IFF_OACTIVE;
965
966 /*
967 * Set a timeout in case the chip goes out to lunch.
968 */
969 ifp->if_timer = 5;
970 }
971
972 Static void
973 cue_init(void *xsc)
974 {
975 struct cue_softc *sc = xsc;
976 struct ifnet *ifp = GET_IFP(sc);
977 int i, s, ctl;
978 const u_char *eaddr;
979
980 if (sc->cue_dying)
981 return;
982
983 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
984
985 if (ifp->if_flags & IFF_RUNNING)
986 return;
987
988 s = splnet();
989
990 /*
991 * Cancel pending I/O and free all RX/TX buffers.
992 */
993 #if 1
994 cue_reset(sc);
995 #endif
996
997 /* Set advanced operation modes. */
998 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
999 CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
1000
1001 eaddr = CLLADDR(ifp->if_sadl);
1002 /* Set MAC address */
1003 for (i = 0; i < ETHER_ADDR_LEN; i++)
1004 cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
1005
1006 /* Enable RX logic. */
1007 ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
1008 if (ifp->if_flags & IFF_PROMISC)
1009 ctl |= CUE_ETHCTL_PROMISC;
1010 cue_csr_write_1(sc, CUE_ETHCTL, ctl);
1011
1012 /* Load the multicast filter. */
1013 cue_setmulti(sc);
1014
1015 /*
1016 * Set the number of RX and TX buffers that we want
1017 * to reserve inside the ASIC.
1018 */
1019 cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
1020 cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
1021
1022 /* Set advanced operation modes. */
1023 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1024 CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
1025
1026 /* Program the LED operation. */
1027 cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
1028
1029 if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
1030 if (cue_open_pipes(sc)) {
1031 splx(s);
1032 return;
1033 }
1034 }
1035 /* Init TX ring. */
1036 if (cue_tx_list_init(sc)) {
1037 printf("%s: tx list init failed\n", device_xname(sc->cue_dev));
1038 splx(s);
1039 return;
1040 }
1041
1042 /* Init RX ring. */
1043 if (cue_rx_list_init(sc)) {
1044 printf("%s: rx list init failed\n", device_xname(sc->cue_dev));
1045 splx(s);
1046 return;
1047 }
1048
1049
1050 ifp->if_flags |= IFF_RUNNING;
1051 ifp->if_flags &= ~IFF_OACTIVE;
1052
1053 splx(s);
1054
1055 callout_reset(&(sc->cue_stat_ch), (hz), (cue_tick), (sc));
1056 }
1057
1058 Static int
1059 cue_open_pipes(struct cue_softc *sc)
1060 {
1061 struct cue_chain *c;
1062 usbd_status err;
1063 int i;
1064
1065 /* Open RX and TX pipes. */
1066 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
1067 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
1068 if (err) {
1069 printf("%s: open rx pipe failed: %s\n",
1070 device_xname(sc->cue_dev), usbd_errstr(err));
1071 return EIO;
1072 }
1073 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
1074 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
1075 if (err) {
1076 printf("%s: open tx pipe failed: %s\n",
1077 device_xname(sc->cue_dev), usbd_errstr(err));
1078 return EIO;
1079 }
1080
1081 /* Start up the receive pipe. */
1082 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1083 c = &sc->cue_cdata.cue_rx_chain[i];
1084
1085 usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
1086 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
1087 usbd_transfer(c->cue_xfer);
1088 }
1089
1090 return 0;
1091 }
1092
1093 Static int
1094 cue_ioctl(struct ifnet *ifp, u_long command, void *data)
1095 {
1096 struct cue_softc *sc = ifp->if_softc;
1097 struct ifaddr *ifa = (struct ifaddr *)data;
1098 struct ifreq *ifr = (struct ifreq *)data;
1099 int s, error = 0;
1100
1101 if (sc->cue_dying)
1102 return EIO;
1103
1104 s = splnet();
1105
1106 switch(command) {
1107 case SIOCINITIFADDR:
1108 ifp->if_flags |= IFF_UP;
1109 cue_init(sc);
1110
1111 switch (ifa->ifa_addr->sa_family) {
1112 #ifdef INET
1113 case AF_INET:
1114 arp_ifinit(ifp, ifa);
1115 break;
1116 #endif /* INET */
1117 }
1118 break;
1119
1120 case SIOCSIFMTU:
1121 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
1122 error = EINVAL;
1123 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
1124 error = 0;
1125 break;
1126
1127 case SIOCSIFFLAGS:
1128 if ((error = ifioctl_common(ifp, command, data)) != 0)
1129 break;
1130 if (ifp->if_flags & IFF_UP) {
1131 if (ifp->if_flags & IFF_RUNNING &&
1132 ifp->if_flags & IFF_PROMISC &&
1133 !(sc->cue_if_flags & IFF_PROMISC)) {
1134 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1135 cue_setmulti(sc);
1136 } else if (ifp->if_flags & IFF_RUNNING &&
1137 !(ifp->if_flags & IFF_PROMISC) &&
1138 sc->cue_if_flags & IFF_PROMISC) {
1139 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1140 cue_setmulti(sc);
1141 } else if (!(ifp->if_flags & IFF_RUNNING))
1142 cue_init(sc);
1143 } else {
1144 if (ifp->if_flags & IFF_RUNNING)
1145 cue_stop(sc);
1146 }
1147 sc->cue_if_flags = ifp->if_flags;
1148 error = 0;
1149 break;
1150 case SIOCADDMULTI:
1151 case SIOCDELMULTI:
1152 cue_setmulti(sc);
1153 error = 0;
1154 break;
1155 default:
1156 error = ether_ioctl(ifp, command, data);
1157 break;
1158 }
1159
1160 splx(s);
1161
1162 return error;
1163 }
1164
1165 Static void
1166 cue_watchdog(struct ifnet *ifp)
1167 {
1168 struct cue_softc *sc = ifp->if_softc;
1169 struct cue_chain *c;
1170 usbd_status stat;
1171 int s;
1172
1173 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
1174
1175 if (sc->cue_dying)
1176 return;
1177
1178 ifp->if_oerrors++;
1179 printf("%s: watchdog timeout\n", device_xname(sc->cue_dev));
1180
1181 s = splusb();
1182 c = &sc->cue_cdata.cue_tx_chain[0];
1183 usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1184 cue_txeof(c->cue_xfer, c, stat);
1185
1186 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1187 cue_start(ifp);
1188 splx(s);
1189 }
1190
1191 /*
1192 * Stop the adapter and free any mbufs allocated to the
1193 * RX and TX lists.
1194 */
1195 Static void
1196 cue_stop(struct cue_softc *sc)
1197 {
1198 usbd_status err;
1199 struct ifnet *ifp;
1200 int i;
1201
1202 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
1203
1204 ifp = GET_IFP(sc);
1205 ifp->if_timer = 0;
1206
1207 cue_csr_write_1(sc, CUE_ETHCTL, 0);
1208 cue_reset(sc);
1209 callout_stop(&sc->cue_stat_ch);
1210
1211 /* Stop transfers. */
1212 if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1213 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1214 if (err) {
1215 printf("%s: abort rx pipe failed: %s\n",
1216 device_xname(sc->cue_dev), usbd_errstr(err));
1217 }
1218 }
1219
1220 if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1221 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1222 if (err) {
1223 printf("%s: abort tx pipe failed: %s\n",
1224 device_xname(sc->cue_dev), usbd_errstr(err));
1225 }
1226 }
1227
1228 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1229 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1230 if (err) {
1231 printf("%s: abort intr pipe failed: %s\n",
1232 device_xname(sc->cue_dev), usbd_errstr(err));
1233 }
1234 }
1235
1236 /* Free RX resources. */
1237 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1238 if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1239 usbd_destroy_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1240 sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1241 }
1242 }
1243
1244 /* Free TX resources. */
1245 for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1246 if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1247 m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1248 sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1249 }
1250 if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1251 usbd_destroy_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1252 sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1253 }
1254 }
1255
1256 /* Stop transfers. */
1257 if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1258 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1259 if (err) {
1260 printf("%s: close rx pipe failed: %s\n",
1261 device_xname(sc->cue_dev), usbd_errstr(err));
1262 }
1263 sc->cue_ep[CUE_ENDPT_RX] = NULL;
1264 }
1265
1266 if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1267 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1268 if (err) {
1269 printf("%s: close tx pipe failed: %s\n",
1270 device_xname(sc->cue_dev), usbd_errstr(err));
1271 }
1272 sc->cue_ep[CUE_ENDPT_TX] = NULL;
1273 }
1274
1275 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1276 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1277 if (err) {
1278 printf("%s: close intr pipe failed: %s\n",
1279 device_xname(sc->cue_dev), usbd_errstr(err));
1280 }
1281 sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1282 }
1283
1284 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1285 }
1286