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