if_cue.c revision 187970
1/*- 2 * Copyright (c) 1997, 1998, 1999, 2000 3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_cue2.c 187970 2009-02-01 00:51:25Z thompsa $"); 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@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 * NOTE: all function names beginning like "cue_cfg_" can only 56 * be called from within the config thread function ! 57 */ 58 59#include <dev/usb2/include/usb2_devid.h> 60#include <dev/usb2/include/usb2_standard.h> 61#include <dev/usb2/include/usb2_mfunc.h> 62#include <dev/usb2/include/usb2_error.h> 63 64#define usb2_config_td_cc usb2_ether_cc 65#define usb2_config_td_softc cue_softc 66 67#define USB_DEBUG_VAR cue_debug 68 69#include <dev/usb2/core/usb2_core.h> 70#include <dev/usb2/core/usb2_lookup.h> 71#include <dev/usb2/core/usb2_process.h> 72#include <dev/usb2/core/usb2_config_td.h> 73#include <dev/usb2/core/usb2_debug.h> 74#include <dev/usb2/core/usb2_request.h> 75#include <dev/usb2/core/usb2_busdma.h> 76#include <dev/usb2/core/usb2_util.h> 77 78#include <dev/usb2/ethernet/usb2_ethernet.h> 79#include <dev/usb2/ethernet/if_cuereg.h> 80 81/* 82 * Various supported device vendors/products. 83 */ 84 85/* Belkin F5U111 adapter covered by NETMATE entry */ 86 87static const struct usb2_device_id cue_devs[] = { 88 {USB_VPI(USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE, 0)}, 89 {USB_VPI(USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2, 0)}, 90 {USB_VPI(USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK, 0)}, 91}; 92 93/* prototypes */ 94 95static device_probe_t cue_probe; 96static device_attach_t cue_attach; 97static device_detach_t cue_detach; 98static device_shutdown_t cue_shutdown; 99 100static usb2_callback_t cue_bulk_read_clear_stall_callback; 101static usb2_callback_t cue_bulk_read_callback; 102static usb2_callback_t cue_bulk_write_clear_stall_callback; 103static usb2_callback_t cue_bulk_write_callback; 104 105static usb2_config_td_command_t cue_cfg_promisc_upd; 106static usb2_config_td_command_t cue_config_copy; 107static usb2_config_td_command_t cue_cfg_first_time_setup; 108static usb2_config_td_command_t cue_cfg_tick; 109static usb2_config_td_command_t cue_cfg_pre_init; 110static usb2_config_td_command_t cue_cfg_init; 111static usb2_config_td_command_t cue_cfg_pre_stop; 112static usb2_config_td_command_t cue_cfg_stop; 113 114static void cue_cfg_do_request(struct cue_softc *, 115 struct usb2_device_request *, void *); 116static uint8_t cue_cfg_csr_read_1(struct cue_softc *, uint16_t); 117static uint16_t cue_cfg_csr_read_2(struct cue_softc *, uint8_t); 118static void cue_cfg_csr_write_1(struct cue_softc *, uint16_t, uint16_t); 119static void cue_cfg_mem(struct cue_softc *, uint8_t, uint16_t, void *, 120 uint16_t); 121static void cue_cfg_getmac(struct cue_softc *, void *); 122static void cue_mchash(struct usb2_config_td_cc *, const uint8_t *); 123static void cue_cfg_reset(struct cue_softc *); 124static void cue_start_cb(struct ifnet *); 125static void cue_start_transfers(struct cue_softc *); 126static void cue_init_cb(void *); 127static int cue_ioctl_cb(struct ifnet *, u_long, caddr_t); 128static void cue_watchdog(void *); 129 130#if USB_DEBUG 131static int cue_debug = 0; 132 133SYSCTL_NODE(_hw_usb2, OID_AUTO, cue, CTLFLAG_RW, 0, "USB cue"); 134SYSCTL_INT(_hw_usb2_cue, OID_AUTO, debug, CTLFLAG_RW, &cue_debug, 0, 135 "Debug level"); 136#endif 137 138static const struct usb2_config cue_config[CUE_N_TRANSFER] = { 139 140 [CUE_BULK_DT_WR] = { 141 .type = UE_BULK, 142 .endpoint = UE_ADDR_ANY, 143 .direction = UE_DIR_OUT, 144 .mh.bufsize = (MCLBYTES + 2), 145 .mh.flags = {.pipe_bof = 1,}, 146 .mh.callback = &cue_bulk_write_callback, 147 .mh.timeout = 10000, /* 10 seconds */ 148 }, 149 150 [CUE_BULK_DT_RD] = { 151 .type = UE_BULK, 152 .endpoint = UE_ADDR_ANY, 153 .direction = UE_DIR_IN, 154 .mh.bufsize = (MCLBYTES + 2), 155 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 156 .mh.callback = &cue_bulk_read_callback, 157 }, 158 159 [CUE_BULK_CS_WR] = { 160 .type = UE_CONTROL, 161 .endpoint = 0x00, /* Control pipe */ 162 .direction = UE_DIR_ANY, 163 .mh.bufsize = sizeof(struct usb2_device_request), 164 .mh.flags = {}, 165 .mh.callback = &cue_bulk_write_clear_stall_callback, 166 .mh.timeout = 1000, /* 1 second */ 167 .mh.interval = 50, /* 50ms */ 168 }, 169 170 [CUE_BULK_CS_RD] = { 171 .type = UE_CONTROL, 172 .endpoint = 0x00, /* Control pipe */ 173 .direction = UE_DIR_ANY, 174 .mh.bufsize = sizeof(struct usb2_device_request), 175 .mh.flags = {}, 176 .mh.callback = &cue_bulk_read_clear_stall_callback, 177 .mh.timeout = 1000, /* 1 second */ 178 .mh.interval = 50, /* 50ms */ 179 }, 180}; 181 182static device_method_t cue_methods[] = { 183 /* Device interface */ 184 DEVMETHOD(device_probe, cue_probe), 185 DEVMETHOD(device_attach, cue_attach), 186 DEVMETHOD(device_detach, cue_detach), 187 DEVMETHOD(device_shutdown, cue_shutdown), 188 189 {0, 0} 190}; 191 192static driver_t cue_driver = { 193 .name = "cue", 194 .methods = cue_methods, 195 .size = sizeof(struct cue_softc), 196}; 197 198static devclass_t cue_devclass; 199 200DRIVER_MODULE(cue, ushub, cue_driver, cue_devclass, NULL, 0); 201MODULE_DEPEND(cue, usb2_ethernet, 1, 1, 1); 202MODULE_DEPEND(cue, usb2_core, 1, 1, 1); 203MODULE_DEPEND(cue, ether, 1, 1, 1); 204 205static void 206cue_cfg_do_request(struct cue_softc *sc, struct usb2_device_request *req, 207 void *data) 208{ 209 uint16_t length; 210 usb2_error_t err; 211 212 if (usb2_config_td_is_gone(&sc->sc_config_td)) { 213 goto error; 214 } 215 err = usb2_do_request_flags 216 (sc->sc_udev, &sc->sc_mtx, req, data, 0, NULL, 1000); 217 218 if (err) { 219 220 DPRINTF("device request failed, err=%s " 221 "(ignored)\n", usb2_errstr(err)); 222 223error: 224 length = UGETW(req->wLength); 225 226 if ((req->bmRequestType & UT_READ) && length) { 227 bzero(data, length); 228 } 229 } 230} 231 232#define CUE_CFG_SETBIT(sc, reg, x) \ 233 cue_cfg_csr_write_1(sc, reg, cue_cfg_csr_read_1(sc, reg) | (x)) 234 235#define CUE_CFG_CLRBIT(sc, reg, x) \ 236 cue_cfg_csr_write_1(sc, reg, cue_cfg_csr_read_1(sc, reg) & ~(x)) 237 238static uint8_t 239cue_cfg_csr_read_1(struct cue_softc *sc, uint16_t reg) 240{ 241 struct usb2_device_request req; 242 uint8_t val; 243 244 req.bmRequestType = UT_READ_VENDOR_DEVICE; 245 req.bRequest = CUE_CMD_READREG; 246 USETW(req.wValue, 0); 247 USETW(req.wIndex, reg); 248 USETW(req.wLength, 1); 249 250 cue_cfg_do_request(sc, &req, &val); 251 return (val); 252} 253 254static uint16_t 255cue_cfg_csr_read_2(struct cue_softc *sc, uint8_t reg) 256{ 257 struct usb2_device_request req; 258 uint16_t val; 259 260 req.bmRequestType = UT_READ_VENDOR_DEVICE; 261 req.bRequest = CUE_CMD_READREG; 262 USETW(req.wValue, 0); 263 USETW(req.wIndex, reg); 264 USETW(req.wLength, 2); 265 266 cue_cfg_do_request(sc, &req, &val); 267 return (le16toh(val)); 268} 269 270static void 271cue_cfg_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val) 272{ 273 struct usb2_device_request req; 274 275 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 276 req.bRequest = CUE_CMD_WRITEREG; 277 USETW(req.wValue, val); 278 USETW(req.wIndex, reg); 279 USETW(req.wLength, 0); 280 281 cue_cfg_do_request(sc, &req, NULL); 282} 283 284static void 285cue_cfg_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr, 286 void *buf, uint16_t len) 287{ 288 struct usb2_device_request req; 289 290 if (cmd == CUE_CMD_READSRAM) { 291 req.bmRequestType = UT_READ_VENDOR_DEVICE; 292 } else { 293 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 294 } 295 req.bRequest = cmd; 296 USETW(req.wValue, 0); 297 USETW(req.wIndex, addr); 298 USETW(req.wLength, len); 299 300 cue_cfg_do_request(sc, &req, buf); 301} 302 303static void 304cue_cfg_getmac(struct cue_softc *sc, void *buf) 305{ 306 struct usb2_device_request req; 307 308 req.bmRequestType = UT_READ_VENDOR_DEVICE; 309 req.bRequest = CUE_CMD_GET_MACADDR; 310 USETW(req.wValue, 0); 311 USETW(req.wIndex, 0); 312 USETW(req.wLength, ETHER_ADDR_LEN); 313 314 cue_cfg_do_request(sc, &req, buf); 315} 316 317#define CUE_BITS 9 318 319static void 320cue_mchash(struct usb2_config_td_cc *cc, const uint8_t *addr) 321{ 322 uint16_t h; 323 324 h = ether_crc32_le(addr, ETHER_ADDR_LEN) & 325 ((1 << CUE_BITS) - 1); 326 cc->if_hash[h >> 3] |= 1 << (h & 0x7); 327} 328 329static void 330cue_cfg_promisc_upd(struct cue_softc *sc, 331 struct usb2_config_td_cc *cc, uint16_t refcount) 332{ 333 /* if we want promiscuous mode, set the allframes bit */ 334 335 if (cc->if_flags & IFF_PROMISC) { 336 CUE_CFG_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); 337 } else { 338 CUE_CFG_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); 339 } 340 341 /* write multicast hash-bits */ 342 343 cue_cfg_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, 344 cc->if_hash, CUE_MCAST_TABLE_LEN); 345} 346 347static void 348cue_config_copy(struct cue_softc *sc, 349 struct usb2_config_td_cc *cc, uint16_t refcount) 350{ 351 bzero(cc, sizeof(*cc)); 352 usb2_ether_cc(sc->sc_ifp, &cue_mchash, cc); 353} 354 355static void 356cue_cfg_reset(struct cue_softc *sc) 357{ 358 struct usb2_device_request req; 359 360 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 361 req.bRequest = CUE_CMD_RESET; 362 USETW(req.wValue, 0); 363 USETW(req.wIndex, 0); 364 USETW(req.wLength, 0); 365 366 cue_cfg_do_request(sc, &req, NULL); 367 368 /* 369 * wait a little while for the chip to get its brains in order: 370 */ 371 372 (void)usb2_config_td_sleep(&sc->sc_config_td, hz / 100); 373} 374 375static int 376cue_probe(device_t dev) 377{ 378 struct usb2_attach_arg *uaa = device_get_ivars(dev); 379 380 if (uaa->usb2_mode != USB_MODE_HOST) { 381 return (ENXIO); 382 } 383 if (uaa->info.bConfigIndex != CUE_CONFIG_IDX) { 384 return (ENXIO); 385 } 386 if (uaa->info.bIfaceIndex != CUE_IFACE_IDX) { 387 return (ENXIO); 388 } 389 return (usb2_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa)); 390} 391 392static int 393cue_attach(device_t dev) 394{ 395 struct usb2_attach_arg *uaa = device_get_ivars(dev); 396 struct cue_softc *sc = device_get_softc(dev); 397 uint8_t iface_index; 398 int32_t error; 399 400 sc->sc_udev = uaa->device; 401 sc->sc_dev = dev; 402 sc->sc_unit = device_get_unit(dev); 403 404 device_set_usb2_desc(dev); 405 406 mtx_init(&sc->sc_mtx, "cue lock", NULL, MTX_DEF | MTX_RECURSE); 407 408 usb2_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0); 409 410 iface_index = CUE_IFACE_IDX; 411 error = usb2_transfer_setup(uaa->device, &iface_index, 412 sc->sc_xfer, cue_config, CUE_N_TRANSFER, sc, &sc->sc_mtx); 413 if (error) { 414 device_printf(dev, "allocating USB " 415 "transfers failed!\n"); 416 goto detach; 417 } 418 error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx, 419 NULL, sizeof(struct usb2_config_td_cc), 16); 420 if (error) { 421 device_printf(dev, "could not setup config " 422 "thread!\n"); 423 goto detach; 424 } 425 mtx_lock(&sc->sc_mtx); 426 427 /* start setup */ 428 429 usb2_config_td_queue_command 430 (&sc->sc_config_td, NULL, &cue_cfg_first_time_setup, 0, 0); 431 432 cue_watchdog(sc); 433 mtx_unlock(&sc->sc_mtx); 434 return (0); /* success */ 435 436detach: 437 cue_detach(dev); 438 return (ENXIO); /* failure */ 439} 440 441static void 442cue_cfg_first_time_setup(struct cue_softc *sc, 443 struct usb2_config_td_cc *cc, uint16_t refcount) 444{ 445 uint8_t eaddr[ETHER_ADDR_LEN]; 446 struct ifnet *ifp; 447 448#if 0 449 /* Reset the adapter. */ 450 cue_cfg_reset(sc); 451#endif 452 /* 453 * Get station address. 454 */ 455 cue_cfg_getmac(sc, eaddr); 456 457 mtx_unlock(&sc->sc_mtx); 458 459 ifp = if_alloc(IFT_ETHER); 460 461 mtx_lock(&sc->sc_mtx); 462 463 if (ifp == NULL) { 464 printf("cue%d: could not if_alloc()\n", 465 sc->sc_unit); 466 goto done; 467 } 468 469 ifp->if_softc = sc; 470 if_initname(ifp, "cue", sc->sc_unit); 471 ifp->if_mtu = ETHERMTU; 472 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 473 ifp->if_ioctl = cue_ioctl_cb; 474 ifp->if_start = cue_start_cb; 475 ifp->if_watchdog = NULL; 476 ifp->if_init = cue_init_cb; 477 ifp->if_baudrate = 10000000; 478 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 479 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 480 IFQ_SET_READY(&ifp->if_snd); 481 482 sc->sc_ifp = ifp; 483 484 mtx_unlock(&sc->sc_mtx); 485 486 ether_ifattach(ifp, eaddr); 487 488 mtx_lock(&sc->sc_mtx); 489 490done: 491 return; 492} 493 494static int 495cue_detach(device_t dev) 496{ 497 struct cue_softc *sc = device_get_softc(dev); 498 struct ifnet *ifp; 499 500 usb2_config_td_drain(&sc->sc_config_td); 501 502 mtx_lock(&sc->sc_mtx); 503 504 usb2_callout_stop(&sc->sc_watchdog); 505 506 cue_cfg_pre_stop(sc, NULL, 0); 507 508 ifp = sc->sc_ifp; 509 510 mtx_unlock(&sc->sc_mtx); 511 512 /* stop all USB transfers first */ 513 usb2_transfer_unsetup(sc->sc_xfer, CUE_N_TRANSFER); 514 515 /* get rid of any late children */ 516 bus_generic_detach(dev); 517 518 if (ifp) { 519 ether_ifdetach(ifp); 520 if_free(ifp); 521 } 522 usb2_config_td_unsetup(&sc->sc_config_td); 523 524 usb2_callout_drain(&sc->sc_watchdog); 525 526 mtx_destroy(&sc->sc_mtx); 527 528 return (0); 529} 530 531static void 532cue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer) 533{ 534 struct cue_softc *sc = xfer->priv_sc; 535 struct usb2_xfer *xfer_other = sc->sc_xfer[CUE_BULK_DT_RD]; 536 537 if (usb2_clear_stall_callback(xfer, xfer_other)) { 538 DPRINTF("stall cleared\n"); 539 sc->sc_flags &= ~CUE_FLAG_READ_STALL; 540 usb2_transfer_start(xfer_other); 541 } 542} 543 544static void 545cue_bulk_read_callback(struct usb2_xfer *xfer) 546{ 547 struct cue_softc *sc = xfer->priv_sc; 548 struct ifnet *ifp = sc->sc_ifp; 549 struct mbuf *m = NULL; 550 uint8_t buf[2]; 551 uint16_t len; 552 553 switch (USB_GET_STATE(xfer)) { 554 case USB_ST_TRANSFERRED: 555 556 if (xfer->actlen <= (2 + sizeof(struct ether_header))) { 557 ifp->if_ierrors++; 558 goto tr_setup; 559 } 560 usb2_copy_out(xfer->frbuffers, 0, buf, 2); 561 562 len = buf[0] | (buf[1] << 8); 563 564 xfer->actlen -= 2; 565 566 m = usb2_ether_get_mbuf(); 567 568 if (m == NULL) { 569 ifp->if_ierrors++; 570 goto tr_setup; 571 } 572 xfer->actlen = min(xfer->actlen, m->m_len); 573 xfer->actlen = min(xfer->actlen, len); 574 575 usb2_copy_out(xfer->frbuffers, 2, m->m_data, xfer->actlen); 576 577 ifp->if_ipackets++; 578 m->m_pkthdr.rcvif = ifp; 579 m->m_pkthdr.len = m->m_len = xfer->actlen; 580 581 case USB_ST_SETUP: 582tr_setup: 583 584 if (sc->sc_flags & CUE_FLAG_READ_STALL) { 585 usb2_transfer_start(sc->sc_xfer[CUE_BULK_CS_RD]); 586 } else { 587 xfer->frlengths[0] = xfer->max_data_length; 588 usb2_start_hardware(xfer); 589 } 590 591 /* 592 * At the end of a USB callback it is always safe to unlock 593 * the private mutex of a device! That is why we do the 594 * "if_input" here, and not some lines up! 595 */ 596 if (m) { 597 mtx_unlock(&sc->sc_mtx); 598 (ifp->if_input) (ifp, m); 599 mtx_lock(&sc->sc_mtx); 600 } 601 return; 602 603 default: /* Error */ 604 if (xfer->error != USB_ERR_CANCELLED) { 605 /* try to clear stall first */ 606 sc->sc_flags |= CUE_FLAG_READ_STALL; 607 usb2_transfer_start(sc->sc_xfer[CUE_BULK_CS_RD]); 608 } 609 DPRINTF("bulk read error, %s\n", 610 usb2_errstr(xfer->error)); 611 return; 612 613 } 614} 615 616static void 617cue_cfg_tick(struct cue_softc *sc, 618 struct usb2_config_td_cc *cc, uint16_t refcount) 619{ 620 struct ifnet *ifp = sc->sc_ifp; 621 622 if ((ifp == NULL)) { 623 /* not ready */ 624 return; 625 } 626 ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_SINGLECOLL); 627 ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_MULTICOLL); 628 ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_EXCESSCOLL); 629 630 if (cue_cfg_csr_read_2(sc, CUE_RX_FRAMEERR)) { 631 ifp->if_ierrors++; 632 } 633 /* start stopped transfers, if any */ 634 635 cue_start_transfers(sc); 636} 637 638static void 639cue_start_cb(struct ifnet *ifp) 640{ 641 struct cue_softc *sc = ifp->if_softc; 642 643 mtx_lock(&sc->sc_mtx); 644 645 cue_start_transfers(sc); 646 647 mtx_unlock(&sc->sc_mtx); 648} 649 650static void 651cue_start_transfers(struct cue_softc *sc) 652{ 653 if ((sc->sc_flags & CUE_FLAG_LL_READY) && 654 (sc->sc_flags & CUE_FLAG_HL_READY)) { 655 656 /* 657 * start the USB transfers, if not already started: 658 */ 659 usb2_transfer_start(sc->sc_xfer[CUE_BULK_DT_RD]); 660 usb2_transfer_start(sc->sc_xfer[CUE_BULK_DT_WR]); 661 } 662} 663 664static void 665cue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer) 666{ 667 struct cue_softc *sc = xfer->priv_sc; 668 struct usb2_xfer *xfer_other = sc->sc_xfer[CUE_BULK_DT_WR]; 669 670 if (usb2_clear_stall_callback(xfer, xfer_other)) { 671 DPRINTF("stall cleared\n"); 672 sc->sc_flags &= ~CUE_FLAG_WRITE_STALL; 673 usb2_transfer_start(xfer_other); 674 } 675} 676 677static void 678cue_bulk_write_callback(struct usb2_xfer *xfer) 679{ 680 struct cue_softc *sc = xfer->priv_sc; 681 struct ifnet *ifp = sc->sc_ifp; 682 struct mbuf *m; 683 uint8_t buf[2]; 684 685 switch (USB_GET_STATE(xfer)) { 686 case USB_ST_TRANSFERRED: 687 DPRINTFN(11, "transfer complete\n"); 688 689 ifp->if_opackets++; 690 691 case USB_ST_SETUP: 692 693 if (sc->sc_flags & CUE_FLAG_WRITE_STALL) { 694 usb2_transfer_start(sc->sc_xfer[CUE_BULK_CS_WR]); 695 goto done; 696 } 697 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 698 699 if (m == NULL) { 700 goto done; 701 } 702 if (m->m_pkthdr.len > MCLBYTES) { 703 m->m_pkthdr.len = MCLBYTES; 704 } 705 xfer->frlengths[0] = (m->m_pkthdr.len + 2); 706 707 /* the first two bytes are the frame length */ 708 709 buf[0] = (uint8_t)(m->m_pkthdr.len); 710 buf[1] = (uint8_t)(m->m_pkthdr.len >> 8); 711 712 usb2_copy_in(xfer->frbuffers, 0, buf, 2); 713 714 usb2_m_copy_in(xfer->frbuffers, 2, 715 m, 0, m->m_pkthdr.len); 716 717 /* 718 * If there's a BPF listener, bounce a copy of this frame 719 * to him. 720 */ 721 BPF_MTAP(ifp, m); 722 723 m_freem(m); 724 725 usb2_start_hardware(xfer); 726 727done: 728 return; 729 730 default: /* Error */ 731 DPRINTFN(11, "transfer error, %s\n", 732 usb2_errstr(xfer->error)); 733 734 if (xfer->error != USB_ERR_CANCELLED) { 735 /* try to clear stall first */ 736 sc->sc_flags |= CUE_FLAG_WRITE_STALL; 737 usb2_transfer_start(sc->sc_xfer[CUE_BULK_CS_WR]); 738 } 739 ifp->if_oerrors++; 740 return; 741 742 } 743} 744 745static void 746cue_init_cb(void *arg) 747{ 748 struct cue_softc *sc = arg; 749 750 mtx_lock(&sc->sc_mtx); 751 usb2_config_td_queue_command 752 (&sc->sc_config_td, &cue_cfg_pre_init, 753 &cue_cfg_init, 0, 0); 754 mtx_unlock(&sc->sc_mtx); 755} 756 757static void 758cue_cfg_pre_init(struct cue_softc *sc, 759 struct usb2_config_td_cc *cc, uint16_t refcount) 760{ 761 struct ifnet *ifp = sc->sc_ifp; 762 763 /* immediate configuration */ 764 765 cue_cfg_pre_stop(sc, cc, 0); 766 767 ifp->if_drv_flags |= IFF_DRV_RUNNING; 768 769 sc->sc_flags |= CUE_FLAG_HL_READY; 770} 771 772static void 773cue_cfg_init(struct cue_softc *sc, 774 struct usb2_config_td_cc *cc, uint16_t refcount) 775{ 776 uint8_t i; 777 778 /* 779 * Cancel pending I/O and free all RX/TX buffers. 780 */ 781 cue_cfg_stop(sc, cc, 0); 782#if 0 783 cue_cfg_reset(sc); 784#endif 785 /* Set MAC address */ 786 787 for (i = 0; i < ETHER_ADDR_LEN; i++) { 788 cue_cfg_csr_write_1(sc, CUE_PAR0 - i, cc->if_lladdr[i]); 789 } 790 791 /* Enable RX logic. */ 792 cue_cfg_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON); 793 794 /* Load the multicast filter */ 795 cue_cfg_promisc_upd(sc, cc, 0); 796 797 /* 798 * Set the number of RX and TX buffers that we want 799 * to reserve inside the ASIC. 800 */ 801 cue_cfg_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES); 802 cue_cfg_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES); 803 804 /* Set advanced operation modes. */ 805 cue_cfg_csr_write_1(sc, CUE_ADVANCED_OPMODES, 806 CUE_AOP_EMBED_RXLEN | 0x01);/* 1 wait state */ 807 808 /* Program the LED operation. */ 809 cue_cfg_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK); 810 811 sc->sc_flags |= (CUE_FLAG_READ_STALL | 812 CUE_FLAG_WRITE_STALL | 813 CUE_FLAG_LL_READY); 814 815 cue_start_transfers(sc); 816} 817 818static int 819cue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data) 820{ 821 struct cue_softc *sc = ifp->if_softc; 822 int error = 0; 823 824 switch (command) { 825 case SIOCSIFFLAGS: 826 mtx_lock(&sc->sc_mtx); 827 if (ifp->if_flags & IFF_UP) { 828 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 829 usb2_config_td_queue_command 830 (&sc->sc_config_td, &cue_config_copy, 831 &cue_cfg_promisc_upd, 0, 0); 832 } else { 833 usb2_config_td_queue_command 834 (&sc->sc_config_td, &cue_cfg_pre_init, 835 &cue_cfg_init, 0, 0); 836 } 837 } else { 838 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 839 usb2_config_td_queue_command 840 (&sc->sc_config_td, &cue_cfg_pre_stop, 841 &cue_cfg_stop, 0, 0); 842 } 843 } 844 mtx_unlock(&sc->sc_mtx); 845 break; 846 847 case SIOCADDMULTI: 848 case SIOCDELMULTI: 849 mtx_lock(&sc->sc_mtx); 850 usb2_config_td_queue_command 851 (&sc->sc_config_td, &cue_config_copy, 852 &cue_cfg_promisc_upd, 0, 0); 853 mtx_unlock(&sc->sc_mtx); 854 break; 855 856 default: 857 error = ether_ioctl(ifp, command, data); 858 break; 859 } 860 return (error); 861} 862 863static void 864cue_watchdog(void *arg) 865{ 866 struct cue_softc *sc = arg; 867 868 mtx_assert(&sc->sc_mtx, MA_OWNED); 869 870 usb2_config_td_queue_command 871 (&sc->sc_config_td, NULL, &cue_cfg_tick, 0, 0); 872 873 usb2_callout_reset(&sc->sc_watchdog, 874 hz, &cue_watchdog, sc); 875} 876 877/* 878 * Stop the adapter and free any mbufs allocated to the 879 * RX and TX lists. 880 */ 881static void 882cue_cfg_pre_stop(struct cue_softc *sc, 883 struct usb2_config_td_cc *cc, uint16_t refcount) 884{ 885 struct ifnet *ifp = sc->sc_ifp; 886 887 if (cc) { 888 /* copy the needed configuration */ 889 cue_config_copy(sc, cc, refcount); 890 } 891 /* immediate configuration */ 892 893 if (ifp) { 894 /* clear flags */ 895 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 896 } 897 sc->sc_flags &= ~(CUE_FLAG_HL_READY | 898 CUE_FLAG_LL_READY); 899 900 /* 901 * stop all the transfers, if not already stopped: 902 */ 903 usb2_transfer_stop(sc->sc_xfer[CUE_BULK_DT_WR]); 904 usb2_transfer_stop(sc->sc_xfer[CUE_BULK_DT_RD]); 905 usb2_transfer_stop(sc->sc_xfer[CUE_BULK_CS_WR]); 906 usb2_transfer_stop(sc->sc_xfer[CUE_BULK_CS_RD]); 907} 908 909static void 910cue_cfg_stop(struct cue_softc *sc, 911 struct usb2_config_td_cc *cc, uint16_t refcount) 912{ 913 cue_cfg_csr_write_1(sc, CUE_ETHCTL, 0); 914 cue_cfg_reset(sc); 915} 916 917/* 918 * Stop all chip I/O so that the kernel's probe routines don't 919 * get confused by errant DMAs when rebooting. 920 */ 921static int 922cue_shutdown(device_t dev) 923{ 924 struct cue_softc *sc = device_get_softc(dev); 925 926 mtx_lock(&sc->sc_mtx); 927 928 usb2_config_td_queue_command 929 (&sc->sc_config_td, &cue_cfg_pre_stop, 930 &cue_cfg_stop, 0, 0); 931 932 mtx_unlock(&sc->sc_mtx); 933 934 return (0); 935} 936