if_cue.c revision 1.82 1 /* $NetBSD: if_cue.c,v 1.82 2019/05/23 10:57:29 msaitoh 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.82 2019/05/23 10:57:29 msaitoh 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
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 /*
575 * XXX Halting callout guarantees no more tick tasks. What
576 * guarantees no more stop tasks? What guarantees no more
577 * calls to cue_send? Don't we need to wait for if_detach or
578 * something? Should we set sc->cue_dying here? Is device
579 * deactivation guaranteed to have already happened?
580 */
581 callout_halt(&sc->cue_stat_ch, NULL);
582 usb_rem_task_wait(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER,
583 NULL);
584 usb_rem_task_wait(sc->cue_udev, &sc->cue_stop_task, USB_TASKQ_DRIVER,
585 NULL);
586
587 if (!sc->cue_attached) {
588 /* Detached before attached finished, so just bail out. */
589 return 0;
590 }
591
592 s = splusb();
593
594 if (ifp->if_flags & IFF_RUNNING)
595 cue_stop(sc);
596
597 rnd_detach_source(&sc->rnd_source);
598 ether_ifdetach(ifp);
599
600 if_detach(ifp);
601
602 #ifdef DIAGNOSTIC
603 if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
604 sc->cue_ep[CUE_ENDPT_RX] != NULL ||
605 sc->cue_ep[CUE_ENDPT_INTR] != NULL)
606 aprint_debug_dev(self, "detach has active endpoints\n");
607 #endif
608
609 sc->cue_attached = 0;
610 splx(s);
611
612 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev, sc->cue_dev);
613
614 return 0;
615 }
616
617 int
618 cue_activate(device_t self, enum devact act)
619 {
620 struct cue_softc *sc = device_private(self);
621
622 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
623
624 switch (act) {
625 case DVACT_DEACTIVATE:
626 /* Deactivate the interface. */
627 if_deactivate(&sc->cue_ec.ec_if);
628 sc->cue_dying = 1;
629 return 0;
630 default:
631 return EOPNOTSUPP;
632 }
633 }
634
635 /*
636 * Initialize an RX descriptor and attach an MBUF cluster.
637 */
638 Static int
639 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
640 {
641 struct mbuf *m_new = NULL;
642
643 if (m == NULL) {
644 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
645 if (m_new == NULL) {
646 printf("%s: no memory for rx list "
647 "-- packet dropped!\n", device_xname(sc->cue_dev));
648 return ENOBUFS;
649 }
650
651 MCLGET(m_new, M_DONTWAIT);
652 if (!(m_new->m_flags & M_EXT)) {
653 printf("%s: no memory for rx list "
654 "-- packet dropped!\n", device_xname(sc->cue_dev));
655 m_freem(m_new);
656 return ENOBUFS;
657 }
658 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
659 } else {
660 m_new = m;
661 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
662 m_new->m_data = m_new->m_ext.ext_buf;
663 }
664
665 m_adj(m_new, ETHER_ALIGN);
666 c->cue_mbuf = m_new;
667
668 return 0;
669 }
670
671 Static int
672 cue_rx_list_init(struct cue_softc *sc)
673 {
674 struct cue_cdata *cd;
675 struct cue_chain *c;
676 int i;
677
678 cd = &sc->cue_cdata;
679 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
680 c = &cd->cue_rx_chain[i];
681 c->cue_sc = sc;
682 c->cue_idx = i;
683 if (cue_newbuf(sc, c, NULL) == ENOBUFS)
684 return ENOBUFS;
685 if (c->cue_xfer == NULL) {
686 int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_RX],
687 CUE_BUFSZ, 0, 0, &c->cue_xfer);
688 if (error)
689 return error;
690 c->cue_buf = usbd_get_buffer(c->cue_xfer);
691 }
692 }
693
694 return 0;
695 }
696
697 Static int
698 cue_tx_list_init(struct cue_softc *sc)
699 {
700 struct cue_cdata *cd;
701 struct cue_chain *c;
702 int i;
703
704 cd = &sc->cue_cdata;
705 for (i = 0; i < CUE_TX_LIST_CNT; i++) {
706 c = &cd->cue_tx_chain[i];
707 c->cue_sc = sc;
708 c->cue_idx = i;
709 c->cue_mbuf = NULL;
710 if (c->cue_xfer == NULL) {
711 int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_TX],
712 CUE_BUFSZ, 0, 0, &c->cue_xfer);
713 if (error)
714 return error;
715 c->cue_buf = usbd_get_buffer(c->cue_xfer);
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(struct usbd_xfer *xfer, void *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 uint16_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, uint8_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 m_adj(m, sizeof(uint16_t));
777 m->m_pkthdr.len = m->m_len = total_len;
778
779 m_set_rcvif(m, ifp);
780
781 s = splnet();
782
783 /* XXX ugly */
784 if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
785 ifp->if_ierrors++;
786 goto done1;
787 }
788
789 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->cue_dev),
790 __func__, m->m_len));
791 if_percpuq_enqueue(ifp->if_percpuq, m);
792 done1:
793 splx(s);
794
795 done:
796
797 /* Setup new transfer. */
798 usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
799 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
800 usbd_transfer(c->cue_xfer);
801
802 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->cue_dev),
803 __func__));
804 }
805
806 /*
807 * A frame was downloaded to the chip. It's safe for us to clean up
808 * the list buffers.
809 */
810 Static void
811 cue_txeof(struct usbd_xfer *xfer, void *priv,
812 usbd_status status)
813 {
814 struct cue_chain *c = priv;
815 struct cue_softc *sc = c->cue_sc;
816 struct ifnet *ifp = GET_IFP(sc);
817 int s;
818
819 if (sc->cue_dying)
820 return;
821
822 s = splnet();
823
824 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
825 __func__, status));
826
827 ifp->if_timer = 0;
828 ifp->if_flags &= ~IFF_OACTIVE;
829
830 if (status != USBD_NORMAL_COMPLETION) {
831 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
832 splx(s);
833 return;
834 }
835 ifp->if_oerrors++;
836 printf("%s: usb error on tx: %s\n", device_xname(sc->cue_dev),
837 usbd_errstr(status));
838 if (status == USBD_STALLED)
839 usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_TX]);
840 splx(s);
841 return;
842 }
843
844 ifp->if_opackets++;
845
846 m_freem(c->cue_mbuf);
847 c->cue_mbuf = NULL;
848
849 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
850 cue_start(ifp);
851
852 splx(s);
853 }
854
855 Static void
856 cue_tick(void *xsc)
857 {
858 struct cue_softc *sc = xsc;
859
860 if (sc == NULL)
861 return;
862
863 if (sc->cue_dying)
864 return;
865
866 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
867
868 /* Perform statistics update in process context. */
869 usb_add_task(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER);
870 }
871
872 Static void
873 cue_tick_task(void *xsc)
874 {
875 struct cue_softc *sc = xsc;
876 struct ifnet *ifp;
877
878 if (sc->cue_dying)
879 return;
880
881 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
882
883 ifp = GET_IFP(sc);
884
885 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
886 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
887 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
888
889 if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
890 ifp->if_ierrors++;
891 }
892
893 Static int
894 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
895 {
896 int total_len;
897 struct cue_chain *c;
898 usbd_status err;
899
900 c = &sc->cue_cdata.cue_tx_chain[idx];
901
902 /*
903 * Copy the mbuf data into a contiguous buffer, leaving two
904 * bytes at the beginning to hold the frame length.
905 */
906 m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
907 c->cue_mbuf = m;
908
909 total_len = m->m_pkthdr.len + 2;
910
911 DPRINTFN(10,("%s: %s: total_len=%d\n",
912 device_xname(sc->cue_dev), __func__, total_len));
913
914 /* The first two bytes are the frame length */
915 c->cue_buf[0] = (uint8_t)m->m_pkthdr.len;
916 c->cue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
917
918 /* XXX 10000 */
919 usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, total_len, 0, 10000,
920 cue_txeof);
921
922 /* Transmit */
923 err = usbd_transfer(c->cue_xfer);
924 if (err != USBD_IN_PROGRESS) {
925 printf("%s: cue_send error=%s\n", device_xname(sc->cue_dev),
926 usbd_errstr(err));
927 /* Stop the interface from process context. */
928 usb_add_task(sc->cue_udev, &sc->cue_stop_task,
929 USB_TASKQ_DRIVER);
930 return EIO;
931 }
932
933 sc->cue_cdata.cue_tx_cnt++;
934
935 return 0;
936 }
937
938 Static void
939 cue_start(struct ifnet *ifp)
940 {
941 struct cue_softc *sc = ifp->if_softc;
942 struct mbuf *m_head = NULL;
943
944 if (sc->cue_dying)
945 return;
946
947 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
948
949 if (ifp->if_flags & IFF_OACTIVE)
950 return;
951
952 IFQ_POLL(&ifp->if_snd, m_head);
953 if (m_head == NULL)
954 return;
955
956 if (cue_send(sc, m_head, 0)) {
957 ifp->if_flags |= IFF_OACTIVE;
958 return;
959 }
960
961 IFQ_DEQUEUE(&ifp->if_snd, m_head);
962
963 /*
964 * If there's a BPF listener, bounce a copy of this frame
965 * to him.
966 */
967 bpf_mtap(ifp, m_head, BPF_D_OUT);
968
969 ifp->if_flags |= IFF_OACTIVE;
970
971 /*
972 * Set a timeout in case the chip goes out to lunch.
973 */
974 ifp->if_timer = 5;
975 }
976
977 Static void
978 cue_init(void *xsc)
979 {
980 struct cue_softc *sc = xsc;
981 struct ifnet *ifp = GET_IFP(sc);
982 int i, s, ctl;
983 const u_char *eaddr;
984
985 if (sc->cue_dying)
986 return;
987
988 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
989
990 if (ifp->if_flags & IFF_RUNNING)
991 return;
992
993 s = splnet();
994
995 /*
996 * Cancel pending I/O and free all RX/TX buffers.
997 */
998 #if 1
999 cue_reset(sc);
1000 #endif
1001
1002 /* Set advanced operation modes. */
1003 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1004 CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
1005
1006 eaddr = CLLADDR(ifp->if_sadl);
1007 /* Set MAC address */
1008 for (i = 0; i < ETHER_ADDR_LEN; i++)
1009 cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
1010
1011 /* Enable RX logic. */
1012 ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
1013 if (ifp->if_flags & IFF_PROMISC)
1014 ctl |= CUE_ETHCTL_PROMISC;
1015 cue_csr_write_1(sc, CUE_ETHCTL, ctl);
1016
1017 /* Load the multicast filter. */
1018 cue_setmulti(sc);
1019
1020 /*
1021 * Set the number of RX and TX buffers that we want
1022 * to reserve inside the ASIC.
1023 */
1024 cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
1025 cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
1026
1027 /* Set advanced operation modes. */
1028 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1029 CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
1030
1031 /* Program the LED operation. */
1032 cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
1033
1034 if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
1035 if (cue_open_pipes(sc)) {
1036 splx(s);
1037 return;
1038 }
1039 }
1040 /* Init TX ring. */
1041 if (cue_tx_list_init(sc)) {
1042 printf("%s: tx list init failed\n", device_xname(sc->cue_dev));
1043 splx(s);
1044 return;
1045 }
1046
1047 /* Init RX ring. */
1048 if (cue_rx_list_init(sc)) {
1049 printf("%s: rx list init failed\n", device_xname(sc->cue_dev));
1050 splx(s);
1051 return;
1052 }
1053
1054
1055 ifp->if_flags |= IFF_RUNNING;
1056 ifp->if_flags &= ~IFF_OACTIVE;
1057
1058 splx(s);
1059
1060 callout_reset(&(sc->cue_stat_ch), (hz), (cue_tick), (sc));
1061 }
1062
1063 Static int
1064 cue_open_pipes(struct cue_softc *sc)
1065 {
1066 struct cue_chain *c;
1067 usbd_status err;
1068 int i;
1069
1070 /* Open RX and TX pipes. */
1071 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
1072 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
1073 if (err) {
1074 printf("%s: open rx pipe failed: %s\n",
1075 device_xname(sc->cue_dev), usbd_errstr(err));
1076 return EIO;
1077 }
1078 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
1079 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
1080 if (err) {
1081 printf("%s: open tx pipe failed: %s\n",
1082 device_xname(sc->cue_dev), usbd_errstr(err));
1083 return EIO;
1084 }
1085
1086 /* Start up the receive pipe. */
1087 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1088 c = &sc->cue_cdata.cue_rx_chain[i];
1089
1090 usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
1091 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
1092 usbd_transfer(c->cue_xfer);
1093 }
1094
1095 return 0;
1096 }
1097
1098 Static int
1099 cue_ioctl(struct ifnet *ifp, u_long command, void *data)
1100 {
1101 struct cue_softc *sc = ifp->if_softc;
1102 struct ifaddr *ifa = (struct ifaddr *)data;
1103 struct ifreq *ifr = (struct ifreq *)data;
1104 int s, error = 0;
1105
1106 if (sc->cue_dying)
1107 return EIO;
1108
1109 s = splnet();
1110
1111 switch (command) {
1112 case SIOCINITIFADDR:
1113 ifp->if_flags |= IFF_UP;
1114 cue_init(sc);
1115
1116 switch (ifa->ifa_addr->sa_family) {
1117 #ifdef INET
1118 case AF_INET:
1119 arp_ifinit(ifp, ifa);
1120 break;
1121 #endif /* INET */
1122 }
1123 break;
1124
1125 case SIOCSIFMTU:
1126 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
1127 error = EINVAL;
1128 else if ((error = ifioctl_common(ifp, command, data))
1129 == ENETRESET)
1130 error = 0;
1131 break;
1132
1133 case SIOCSIFFLAGS:
1134 if ((error = ifioctl_common(ifp, command, data)) != 0)
1135 break;
1136 if (ifp->if_flags & IFF_UP) {
1137 if (ifp->if_flags & IFF_RUNNING &&
1138 ifp->if_flags & IFF_PROMISC &&
1139 !(sc->cue_if_flags & IFF_PROMISC)) {
1140 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1141 cue_setmulti(sc);
1142 } else if (ifp->if_flags & IFF_RUNNING &&
1143 !(ifp->if_flags & IFF_PROMISC) &&
1144 sc->cue_if_flags & IFF_PROMISC) {
1145 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1146 cue_setmulti(sc);
1147 } else if (!(ifp->if_flags & IFF_RUNNING))
1148 cue_init(sc);
1149 } else {
1150 if (ifp->if_flags & IFF_RUNNING)
1151 cue_stop(sc);
1152 }
1153 sc->cue_if_flags = ifp->if_flags;
1154 error = 0;
1155 break;
1156 case SIOCADDMULTI:
1157 case SIOCDELMULTI:
1158 cue_setmulti(sc);
1159 error = 0;
1160 break;
1161 default:
1162 error = ether_ioctl(ifp, command, data);
1163 break;
1164 }
1165
1166 splx(s);
1167
1168 return error;
1169 }
1170
1171 Static void
1172 cue_watchdog(struct ifnet *ifp)
1173 {
1174 struct cue_softc *sc = ifp->if_softc;
1175 struct cue_chain *c;
1176 usbd_status stat;
1177 int s;
1178
1179 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
1180
1181 if (sc->cue_dying)
1182 return;
1183
1184 ifp->if_oerrors++;
1185 printf("%s: watchdog timeout\n", device_xname(sc->cue_dev));
1186
1187 s = splusb();
1188 c = &sc->cue_cdata.cue_tx_chain[0];
1189 usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1190 cue_txeof(c->cue_xfer, c, stat);
1191
1192 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1193 cue_start(ifp);
1194 splx(s);
1195 }
1196
1197 /*
1198 * Stop the adapter and free any mbufs allocated to the
1199 * RX and TX lists.
1200 */
1201 Static void
1202 cue_stop(struct cue_softc *sc)
1203 {
1204 usbd_status err;
1205 struct ifnet *ifp;
1206 int i;
1207
1208 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
1209
1210 ifp = GET_IFP(sc);
1211 ifp->if_timer = 0;
1212
1213 cue_csr_write_1(sc, CUE_ETHCTL, 0);
1214 cue_reset(sc);
1215 callout_stop(&sc->cue_stat_ch);
1216
1217 /* Stop transfers. */
1218 if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1219 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1220 if (err) {
1221 printf("%s: abort rx pipe failed: %s\n",
1222 device_xname(sc->cue_dev), usbd_errstr(err));
1223 }
1224 }
1225
1226 if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1227 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1228 if (err) {
1229 printf("%s: abort tx pipe failed: %s\n",
1230 device_xname(sc->cue_dev), usbd_errstr(err));
1231 }
1232 }
1233
1234 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1235 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1236 if (err) {
1237 printf("%s: abort intr pipe failed: %s\n",
1238 device_xname(sc->cue_dev), usbd_errstr(err));
1239 }
1240 }
1241
1242 /* Free RX resources. */
1243 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1244 if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1245 usbd_destroy_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1246 sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1247 }
1248 }
1249
1250 /* Free TX resources. */
1251 for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1252 if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1253 m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1254 sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1255 }
1256 if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1257 usbd_destroy_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1258 sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1259 }
1260 }
1261
1262 /* Stop transfers. */
1263 if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1264 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1265 if (err) {
1266 printf("%s: close rx pipe failed: %s\n",
1267 device_xname(sc->cue_dev), usbd_errstr(err));
1268 }
1269 sc->cue_ep[CUE_ENDPT_RX] = NULL;
1270 }
1271
1272 if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1273 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1274 if (err) {
1275 printf("%s: close tx pipe failed: %s\n",
1276 device_xname(sc->cue_dev), usbd_errstr(err));
1277 }
1278 sc->cue_ep[CUE_ENDPT_TX] = NULL;
1279 }
1280
1281 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1282 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1283 if (err) {
1284 printf("%s: close intr pipe failed: %s\n",
1285 device_xname(sc->cue_dev), usbd_errstr(err));
1286 }
1287 sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1288 }
1289
1290 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1291 }
1292