if_cue.c revision 1.84 1 /* $NetBSD: if_cue.c,v 1.84 2019/05/28 07:41:50 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.84 2019/05/28 07:41:50 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 ethercom *ec = &sc->cue_ec;
360 struct ifnet *ifp;
361 struct ether_multi *enm;
362 struct ether_multistep step;
363 uint32_t h, i;
364
365 ifp = GET_IFP(sc);
366
367 DPRINTFN(2,("%s: cue_setmulti if_flags=0x%x\n",
368 device_xname(sc->cue_dev), ifp->if_flags));
369
370 if (ifp->if_flags & IFF_PROMISC) {
371 allmulti:
372 ifp->if_flags |= IFF_ALLMULTI;
373 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
374 sc->cue_mctab[i] = 0xFF;
375 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
376 &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
377 return;
378 }
379
380 /* first, zot all the existing hash bits */
381 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
382 sc->cue_mctab[i] = 0;
383
384 /* now program new ones */
385 ETHER_LOCK(ec);
386 ETHER_FIRST_MULTI(step, ec, enm);
387 while (enm != NULL) {
388 if (memcmp(enm->enm_addrlo,
389 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
390 ETHER_UNLOCK(ec);
391 goto allmulti;
392 }
393
394 h = cue_crc(enm->enm_addrlo);
395 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
396 ETHER_NEXT_MULTI(step, enm);
397 }
398 ETHER_UNLOCK(ec);
399
400 ifp->if_flags &= ~IFF_ALLMULTI;
401
402 /*
403 * Also include the broadcast address in the filter
404 * so we can receive broadcast frames.
405 */
406 if (ifp->if_flags & IFF_BROADCAST) {
407 h = cue_crc(etherbroadcastaddr);
408 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
409 }
410
411 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
412 &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
413 }
414
415 Static void
416 cue_reset(struct cue_softc *sc)
417 {
418 usb_device_request_t req;
419 usbd_status err;
420
421 DPRINTFN(2,("%s: cue_reset\n", device_xname(sc->cue_dev)));
422
423 if (sc->cue_dying)
424 return;
425
426 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
427 req.bRequest = CUE_CMD_RESET;
428 USETW(req.wValue, 0);
429 USETW(req.wIndex, 0);
430 USETW(req.wLength, 0);
431
432 err = usbd_do_request(sc->cue_udev, &req, NULL);
433
434 if (err)
435 printf("%s: reset failed\n", device_xname(sc->cue_dev));
436
437 /* Wait a little while for the chip to get its brains in order. */
438 usbd_delay_ms(sc->cue_udev, 1);
439 }
440
441 /*
442 * Probe for a CATC chip.
443 */
444 int
445 cue_match(device_t parent, cfdata_t match, void *aux)
446 {
447 struct usb_attach_arg *uaa = aux;
448
449 return cue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
450 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
451 }
452
453 /*
454 * Attach the interface. Allocate softc structures, do ifmedia
455 * setup and ethernet/BPF attach.
456 */
457 void
458 cue_attach(device_t parent, device_t self, void *aux)
459 {
460 struct cue_softc *sc = device_private(self);
461 struct usb_attach_arg *uaa = aux;
462 char *devinfop;
463 int s;
464 u_char eaddr[ETHER_ADDR_LEN];
465 struct usbd_device * dev = uaa->uaa_device;
466 struct usbd_interface * iface;
467 usbd_status err;
468 struct ifnet *ifp;
469 usb_interface_descriptor_t *id;
470 usb_endpoint_descriptor_t *ed;
471 int i;
472
473 DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev));
474
475 sc->cue_dev = self;
476
477 aprint_naive("\n");
478 aprint_normal("\n");
479
480 devinfop = usbd_devinfo_alloc(dev, 0);
481 aprint_normal_dev(self, "%s\n", devinfop);
482 usbd_devinfo_free(devinfop);
483
484 err = usbd_set_config_no(dev, CUE_CONFIG_NO, 1);
485 if (err) {
486 aprint_error_dev(self, "failed to set configuration"
487 ", err=%s\n", usbd_errstr(err));
488 return;
489 }
490
491 sc->cue_udev = dev;
492 sc->cue_product = uaa->uaa_product;
493 sc->cue_vendor = uaa->uaa_vendor;
494
495 usb_init_task(&sc->cue_tick_task, cue_tick_task, sc, 0);
496 usb_init_task(&sc->cue_stop_task, (void (*)(void *))cue_stop, sc, 0);
497
498 err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &iface);
499 if (err) {
500 aprint_error_dev(self, "getting interface handle failed\n");
501 return;
502 }
503
504 sc->cue_iface = iface;
505 id = usbd_get_interface_descriptor(iface);
506
507 /* Find endpoints. */
508 for (i = 0; i < id->bNumEndpoints; i++) {
509 ed = usbd_interface2endpoint_descriptor(iface, i);
510 if (ed == NULL) {
511 aprint_error_dev(self, "couldn't get ep %d\n", i);
512 return;
513 }
514 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
515 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
516 sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
517 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
518 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
519 sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
520 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
521 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
522 sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
523 }
524 }
525
526 #if 0
527 /* Reset the adapter. */
528 cue_reset(sc);
529 #endif
530 /*
531 * Get station address.
532 */
533 cue_getmac(sc, &eaddr);
534
535 s = splnet();
536
537 /*
538 * A CATC chip was detected. Inform the world.
539 */
540 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
541
542 /* Initialize interface info.*/
543 ifp = GET_IFP(sc);
544 ifp->if_softc = sc;
545 ifp->if_mtu = ETHERMTU;
546 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
547 ifp->if_ioctl = cue_ioctl;
548 ifp->if_start = cue_start;
549 ifp->if_watchdog = cue_watchdog;
550 strlcpy(ifp->if_xname, device_xname(sc->cue_dev), IFNAMSIZ);
551
552 IFQ_SET_READY(&ifp->if_snd);
553
554 /* Attach the interface. */
555 if_attach(ifp);
556 ether_ifattach(ifp, eaddr);
557 rnd_attach_source(&sc->rnd_source, device_xname(sc->cue_dev),
558 RND_TYPE_NET, RND_FLAG_DEFAULT);
559
560 callout_init(&(sc->cue_stat_ch), 0);
561
562 sc->cue_attached = 1;
563 splx(s);
564
565 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev, sc->cue_dev);
566
567 return;
568 }
569
570 int
571 cue_detach(device_t self, int flags)
572 {
573 struct cue_softc *sc = device_private(self);
574 struct ifnet *ifp = GET_IFP(sc);
575 int s;
576
577 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
578
579 /*
580 * XXX Halting callout guarantees no more tick tasks. What
581 * guarantees no more stop tasks? What guarantees no more
582 * calls to cue_send? Don't we need to wait for if_detach or
583 * something? Should we set sc->cue_dying here? Is device
584 * deactivation guaranteed to have already happened?
585 */
586 callout_halt(&sc->cue_stat_ch, NULL);
587 usb_rem_task_wait(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER,
588 NULL);
589 usb_rem_task_wait(sc->cue_udev, &sc->cue_stop_task, USB_TASKQ_DRIVER,
590 NULL);
591
592 if (!sc->cue_attached) {
593 /* Detached before attached finished, so just bail out. */
594 return 0;
595 }
596
597 s = splusb();
598
599 if (ifp->if_flags & IFF_RUNNING)
600 cue_stop(sc);
601
602 rnd_detach_source(&sc->rnd_source);
603 ether_ifdetach(ifp);
604
605 if_detach(ifp);
606
607 #ifdef DIAGNOSTIC
608 if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
609 sc->cue_ep[CUE_ENDPT_RX] != NULL ||
610 sc->cue_ep[CUE_ENDPT_INTR] != NULL)
611 aprint_debug_dev(self, "detach has active endpoints\n");
612 #endif
613
614 sc->cue_attached = 0;
615 splx(s);
616
617 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev, sc->cue_dev);
618
619 return 0;
620 }
621
622 int
623 cue_activate(device_t self, enum devact act)
624 {
625 struct cue_softc *sc = device_private(self);
626
627 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
628
629 switch (act) {
630 case DVACT_DEACTIVATE:
631 /* Deactivate the interface. */
632 if_deactivate(&sc->cue_ec.ec_if);
633 sc->cue_dying = 1;
634 return 0;
635 default:
636 return EOPNOTSUPP;
637 }
638 }
639
640 /*
641 * Initialize an RX descriptor and attach an MBUF cluster.
642 */
643 Static int
644 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
645 {
646 struct mbuf *m_new = NULL;
647
648 if (m == NULL) {
649 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
650 if (m_new == NULL) {
651 printf("%s: no memory for rx list "
652 "-- packet dropped!\n", device_xname(sc->cue_dev));
653 return ENOBUFS;
654 }
655
656 MCLGET(m_new, M_DONTWAIT);
657 if (!(m_new->m_flags & M_EXT)) {
658 printf("%s: no memory for rx list "
659 "-- packet dropped!\n", device_xname(sc->cue_dev));
660 m_freem(m_new);
661 return ENOBUFS;
662 }
663 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
664 } else {
665 m_new = m;
666 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
667 m_new->m_data = m_new->m_ext.ext_buf;
668 }
669
670 m_adj(m_new, ETHER_ALIGN);
671 c->cue_mbuf = m_new;
672
673 return 0;
674 }
675
676 Static int
677 cue_rx_list_init(struct cue_softc *sc)
678 {
679 struct cue_cdata *cd;
680 struct cue_chain *c;
681 int i;
682
683 cd = &sc->cue_cdata;
684 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
685 c = &cd->cue_rx_chain[i];
686 c->cue_sc = sc;
687 c->cue_idx = i;
688 if (cue_newbuf(sc, c, NULL) == ENOBUFS)
689 return ENOBUFS;
690 if (c->cue_xfer == NULL) {
691 int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_RX],
692 CUE_BUFSZ, 0, 0, &c->cue_xfer);
693 if (error)
694 return error;
695 c->cue_buf = usbd_get_buffer(c->cue_xfer);
696 }
697 }
698
699 return 0;
700 }
701
702 Static int
703 cue_tx_list_init(struct cue_softc *sc)
704 {
705 struct cue_cdata *cd;
706 struct cue_chain *c;
707 int i;
708
709 cd = &sc->cue_cdata;
710 for (i = 0; i < CUE_TX_LIST_CNT; i++) {
711 c = &cd->cue_tx_chain[i];
712 c->cue_sc = sc;
713 c->cue_idx = i;
714 c->cue_mbuf = NULL;
715 if (c->cue_xfer == NULL) {
716 int error = usbd_create_xfer(sc->cue_ep[CUE_ENDPT_TX],
717 CUE_BUFSZ, 0, 0, &c->cue_xfer);
718 if (error)
719 return error;
720 c->cue_buf = usbd_get_buffer(c->cue_xfer);
721 }
722 }
723
724 return 0;
725 }
726
727 /*
728 * A frame has been uploaded: pass the resulting mbuf chain up to
729 * the higher level protocols.
730 */
731 Static void
732 cue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
733 {
734 struct cue_chain *c = priv;
735 struct cue_softc *sc = c->cue_sc;
736 struct ifnet *ifp = GET_IFP(sc);
737 struct mbuf *m;
738 int total_len = 0;
739 uint16_t len;
740 int s;
741
742 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
743 __func__, status));
744
745 if (sc->cue_dying)
746 return;
747
748 if (!(ifp->if_flags & IFF_RUNNING))
749 return;
750
751 if (status != USBD_NORMAL_COMPLETION) {
752 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
753 return;
754 sc->cue_rx_errs++;
755 if (usbd_ratecheck(&sc->cue_rx_notice)) {
756 printf("%s: %u usb errors on rx: %s\n",
757 device_xname(sc->cue_dev), sc->cue_rx_errs,
758 usbd_errstr(status));
759 sc->cue_rx_errs = 0;
760 }
761 if (status == USBD_STALLED)
762 usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_RX]);
763 goto done;
764 }
765
766 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
767
768 memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
769
770 m = c->cue_mbuf;
771 len = UGETW(mtod(m, uint8_t *));
772
773 /* No errors; receive the packet. */
774 total_len = len;
775
776 if (len < sizeof(struct ether_header)) {
777 ifp->if_ierrors++;
778 goto done;
779 }
780
781 m_adj(m, sizeof(uint16_t));
782 m->m_pkthdr.len = m->m_len = total_len;
783
784 m_set_rcvif(m, ifp);
785
786 s = splnet();
787
788 /* XXX ugly */
789 if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
790 ifp->if_ierrors++;
791 goto done1;
792 }
793
794 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->cue_dev),
795 __func__, m->m_len));
796 if_percpuq_enqueue(ifp->if_percpuq, m);
797 done1:
798 splx(s);
799
800 done:
801
802 /* Setup new transfer. */
803 usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
804 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
805 usbd_transfer(c->cue_xfer);
806
807 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->cue_dev),
808 __func__));
809 }
810
811 /*
812 * A frame was downloaded to the chip. It's safe for us to clean up
813 * the list buffers.
814 */
815 Static void
816 cue_txeof(struct usbd_xfer *xfer, void *priv,
817 usbd_status status)
818 {
819 struct cue_chain *c = priv;
820 struct cue_softc *sc = c->cue_sc;
821 struct ifnet *ifp = GET_IFP(sc);
822 int s;
823
824 if (sc->cue_dying)
825 return;
826
827 s = splnet();
828
829 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->cue_dev),
830 __func__, status));
831
832 ifp->if_timer = 0;
833 ifp->if_flags &= ~IFF_OACTIVE;
834
835 if (status != USBD_NORMAL_COMPLETION) {
836 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
837 splx(s);
838 return;
839 }
840 ifp->if_oerrors++;
841 printf("%s: usb error on tx: %s\n", device_xname(sc->cue_dev),
842 usbd_errstr(status));
843 if (status == USBD_STALLED)
844 usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_TX]);
845 splx(s);
846 return;
847 }
848
849 ifp->if_opackets++;
850
851 m_freem(c->cue_mbuf);
852 c->cue_mbuf = NULL;
853
854 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
855 cue_start(ifp);
856
857 splx(s);
858 }
859
860 Static void
861 cue_tick(void *xsc)
862 {
863 struct cue_softc *sc = xsc;
864
865 if (sc == NULL)
866 return;
867
868 if (sc->cue_dying)
869 return;
870
871 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
872
873 /* Perform statistics update in process context. */
874 usb_add_task(sc->cue_udev, &sc->cue_tick_task, USB_TASKQ_DRIVER);
875 }
876
877 Static void
878 cue_tick_task(void *xsc)
879 {
880 struct cue_softc *sc = xsc;
881 struct ifnet *ifp;
882
883 if (sc->cue_dying)
884 return;
885
886 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
887
888 ifp = GET_IFP(sc);
889
890 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
891 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
892 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
893
894 if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
895 ifp->if_ierrors++;
896 }
897
898 Static int
899 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
900 {
901 int total_len;
902 struct cue_chain *c;
903 usbd_status err;
904
905 c = &sc->cue_cdata.cue_tx_chain[idx];
906
907 /*
908 * Copy the mbuf data into a contiguous buffer, leaving two
909 * bytes at the beginning to hold the frame length.
910 */
911 m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
912 c->cue_mbuf = m;
913
914 total_len = m->m_pkthdr.len + 2;
915
916 DPRINTFN(10,("%s: %s: total_len=%d\n",
917 device_xname(sc->cue_dev), __func__, total_len));
918
919 /* The first two bytes are the frame length */
920 c->cue_buf[0] = (uint8_t)m->m_pkthdr.len;
921 c->cue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
922
923 /* XXX 10000 */
924 usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, total_len, 0, 10000,
925 cue_txeof);
926
927 /* Transmit */
928 err = usbd_transfer(c->cue_xfer);
929 if (err != USBD_IN_PROGRESS) {
930 printf("%s: cue_send error=%s\n", device_xname(sc->cue_dev),
931 usbd_errstr(err));
932 /* Stop the interface from process context. */
933 usb_add_task(sc->cue_udev, &sc->cue_stop_task,
934 USB_TASKQ_DRIVER);
935 return EIO;
936 }
937
938 sc->cue_cdata.cue_tx_cnt++;
939
940 return 0;
941 }
942
943 Static void
944 cue_start(struct ifnet *ifp)
945 {
946 struct cue_softc *sc = ifp->if_softc;
947 struct mbuf *m_head = NULL;
948
949 if (sc->cue_dying)
950 return;
951
952 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
953
954 if (ifp->if_flags & IFF_OACTIVE)
955 return;
956
957 IFQ_POLL(&ifp->if_snd, m_head);
958 if (m_head == NULL)
959 return;
960
961 if (cue_send(sc, m_head, 0)) {
962 ifp->if_flags |= IFF_OACTIVE;
963 return;
964 }
965
966 IFQ_DEQUEUE(&ifp->if_snd, m_head);
967
968 /*
969 * If there's a BPF listener, bounce a copy of this frame
970 * to him.
971 */
972 bpf_mtap(ifp, m_head, BPF_D_OUT);
973
974 ifp->if_flags |= IFF_OACTIVE;
975
976 /*
977 * Set a timeout in case the chip goes out to lunch.
978 */
979 ifp->if_timer = 5;
980 }
981
982 Static void
983 cue_init(void *xsc)
984 {
985 struct cue_softc *sc = xsc;
986 struct ifnet *ifp = GET_IFP(sc);
987 int i, s, ctl;
988 const u_char *eaddr;
989
990 if (sc->cue_dying)
991 return;
992
993 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
994
995 if (ifp->if_flags & IFF_RUNNING)
996 return;
997
998 s = splnet();
999
1000 /*
1001 * Cancel pending I/O and free all RX/TX buffers.
1002 */
1003 #if 1
1004 cue_reset(sc);
1005 #endif
1006
1007 /* Set advanced operation modes. */
1008 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1009 CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
1010
1011 eaddr = CLLADDR(ifp->if_sadl);
1012 /* Set MAC address */
1013 for (i = 0; i < ETHER_ADDR_LEN; i++)
1014 cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
1015
1016 /* Enable RX logic. */
1017 ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
1018 if (ifp->if_flags & IFF_PROMISC)
1019 ctl |= CUE_ETHCTL_PROMISC;
1020 cue_csr_write_1(sc, CUE_ETHCTL, ctl);
1021
1022 /* Load the multicast filter. */
1023 cue_setmulti(sc);
1024
1025 /*
1026 * Set the number of RX and TX buffers that we want
1027 * to reserve inside the ASIC.
1028 */
1029 cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
1030 cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
1031
1032 /* Set advanced operation modes. */
1033 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1034 CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
1035
1036 /* Program the LED operation. */
1037 cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
1038
1039 if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
1040 if (cue_open_pipes(sc)) {
1041 splx(s);
1042 return;
1043 }
1044 }
1045 /* Init TX ring. */
1046 if (cue_tx_list_init(sc)) {
1047 printf("%s: tx list init failed\n", device_xname(sc->cue_dev));
1048 splx(s);
1049 return;
1050 }
1051
1052 /* Init RX ring. */
1053 if (cue_rx_list_init(sc)) {
1054 printf("%s: rx list init failed\n", device_xname(sc->cue_dev));
1055 splx(s);
1056 return;
1057 }
1058
1059
1060 ifp->if_flags |= IFF_RUNNING;
1061 ifp->if_flags &= ~IFF_OACTIVE;
1062
1063 splx(s);
1064
1065 callout_reset(&(sc->cue_stat_ch), (hz), (cue_tick), (sc));
1066 }
1067
1068 Static int
1069 cue_open_pipes(struct cue_softc *sc)
1070 {
1071 struct cue_chain *c;
1072 usbd_status err;
1073 int i;
1074
1075 /* Open RX and TX pipes. */
1076 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
1077 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
1078 if (err) {
1079 printf("%s: open rx pipe failed: %s\n",
1080 device_xname(sc->cue_dev), usbd_errstr(err));
1081 return EIO;
1082 }
1083 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
1084 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
1085 if (err) {
1086 printf("%s: open tx pipe failed: %s\n",
1087 device_xname(sc->cue_dev), usbd_errstr(err));
1088 return EIO;
1089 }
1090
1091 /* Start up the receive pipe. */
1092 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1093 c = &sc->cue_cdata.cue_rx_chain[i];
1094
1095 usbd_setup_xfer(c->cue_xfer, c, c->cue_buf, CUE_BUFSZ,
1096 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
1097 usbd_transfer(c->cue_xfer);
1098 }
1099
1100 return 0;
1101 }
1102
1103 Static int
1104 cue_ioctl(struct ifnet *ifp, u_long command, void *data)
1105 {
1106 struct cue_softc *sc = ifp->if_softc;
1107 struct ifaddr *ifa = (struct ifaddr *)data;
1108 struct ifreq *ifr = (struct ifreq *)data;
1109 int s, error = 0;
1110
1111 if (sc->cue_dying)
1112 return EIO;
1113
1114 s = splnet();
1115
1116 switch (command) {
1117 case SIOCINITIFADDR:
1118 ifp->if_flags |= IFF_UP;
1119 cue_init(sc);
1120
1121 switch (ifa->ifa_addr->sa_family) {
1122 #ifdef INET
1123 case AF_INET:
1124 arp_ifinit(ifp, ifa);
1125 break;
1126 #endif /* INET */
1127 }
1128 break;
1129
1130 case SIOCSIFMTU:
1131 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
1132 error = EINVAL;
1133 else if ((error = ifioctl_common(ifp, command, data))
1134 == ENETRESET)
1135 error = 0;
1136 break;
1137
1138 case SIOCSIFFLAGS:
1139 if ((error = ifioctl_common(ifp, command, data)) != 0)
1140 break;
1141 if (ifp->if_flags & IFF_UP) {
1142 if (ifp->if_flags & IFF_RUNNING &&
1143 ifp->if_flags & IFF_PROMISC &&
1144 !(sc->cue_if_flags & IFF_PROMISC)) {
1145 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1146 cue_setmulti(sc);
1147 } else if (ifp->if_flags & IFF_RUNNING &&
1148 !(ifp->if_flags & IFF_PROMISC) &&
1149 sc->cue_if_flags & IFF_PROMISC) {
1150 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1151 cue_setmulti(sc);
1152 } else if (!(ifp->if_flags & IFF_RUNNING))
1153 cue_init(sc);
1154 } else {
1155 if (ifp->if_flags & IFF_RUNNING)
1156 cue_stop(sc);
1157 }
1158 sc->cue_if_flags = ifp->if_flags;
1159 error = 0;
1160 break;
1161 case SIOCADDMULTI:
1162 case SIOCDELMULTI:
1163 cue_setmulti(sc);
1164 error = 0;
1165 break;
1166 default:
1167 error = ether_ioctl(ifp, command, data);
1168 break;
1169 }
1170
1171 splx(s);
1172
1173 return error;
1174 }
1175
1176 Static void
1177 cue_watchdog(struct ifnet *ifp)
1178 {
1179 struct cue_softc *sc = ifp->if_softc;
1180 struct cue_chain *c;
1181 usbd_status stat;
1182 int s;
1183
1184 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->cue_dev), __func__));
1185
1186 if (sc->cue_dying)
1187 return;
1188
1189 ifp->if_oerrors++;
1190 printf("%s: watchdog timeout\n", device_xname(sc->cue_dev));
1191
1192 s = splusb();
1193 c = &sc->cue_cdata.cue_tx_chain[0];
1194 usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1195 cue_txeof(c->cue_xfer, c, stat);
1196
1197 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1198 cue_start(ifp);
1199 splx(s);
1200 }
1201
1202 /*
1203 * Stop the adapter and free any mbufs allocated to the
1204 * RX and TX lists.
1205 */
1206 Static void
1207 cue_stop(struct cue_softc *sc)
1208 {
1209 usbd_status err;
1210 struct ifnet *ifp;
1211 int i;
1212
1213 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->cue_dev),__func__));
1214
1215 ifp = GET_IFP(sc);
1216 ifp->if_timer = 0;
1217
1218 cue_csr_write_1(sc, CUE_ETHCTL, 0);
1219 cue_reset(sc);
1220 callout_stop(&sc->cue_stat_ch);
1221
1222 /* Stop transfers. */
1223 if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1224 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1225 if (err) {
1226 printf("%s: abort rx pipe failed: %s\n",
1227 device_xname(sc->cue_dev), usbd_errstr(err));
1228 }
1229 }
1230
1231 if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1232 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1233 if (err) {
1234 printf("%s: abort tx pipe failed: %s\n",
1235 device_xname(sc->cue_dev), usbd_errstr(err));
1236 }
1237 }
1238
1239 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1240 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1241 if (err) {
1242 printf("%s: abort intr pipe failed: %s\n",
1243 device_xname(sc->cue_dev), usbd_errstr(err));
1244 }
1245 }
1246
1247 /* Free RX resources. */
1248 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1249 if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1250 usbd_destroy_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1251 sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1252 }
1253 }
1254
1255 /* Free TX resources. */
1256 for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1257 if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1258 m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1259 sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1260 }
1261 if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1262 usbd_destroy_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1263 sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1264 }
1265 }
1266
1267 /* Stop transfers. */
1268 if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1269 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1270 if (err) {
1271 printf("%s: close rx pipe failed: %s\n",
1272 device_xname(sc->cue_dev), usbd_errstr(err));
1273 }
1274 sc->cue_ep[CUE_ENDPT_RX] = NULL;
1275 }
1276
1277 if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1278 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1279 if (err) {
1280 printf("%s: close tx pipe failed: %s\n",
1281 device_xname(sc->cue_dev), usbd_errstr(err));
1282 }
1283 sc->cue_ep[CUE_ENDPT_TX] = NULL;
1284 }
1285
1286 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1287 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1288 if (err) {
1289 printf("%s: close intr pipe failed: %s\n",
1290 device_xname(sc->cue_dev), usbd_errstr(err));
1291 }
1292 sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1293 }
1294
1295 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1296 }
1297