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