1 /* $NetBSD: if_smsc.c,v 1.21 2014/09/14 21:06:12 jmcneill Exp $ */ 2 3 /* $OpenBSD: if_smsc.c,v 1.4 2012/09/27 12:38:11 jsg Exp $ */ 4 /* $FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */ 5 /*- 6 * Copyright (c) 2012 7 * Ben Gray <bgray@freebsd.org>. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* 32 * SMSC LAN9xxx devices (http://www.smsc.com/) 33 * 34 * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that 35 * support USB 2.0 and 10/100 Mbps Ethernet. 36 * 37 * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter. 38 * The driver only covers the Ethernet part, the standard USB hub driver 39 * supports the hub part. 40 * 41 * This driver is closely modelled on the Linux driver written and copyrighted 42 * by SMSC. 43 * 44 * H/W TCP & UDP Checksum Offloading 45 * --------------------------------- 46 * The chip supports both tx and rx offloading of UDP & TCP checksums, this 47 * feature can be dynamically enabled/disabled. 48 * 49 * RX checksuming is performed across bytes after the IPv4 header to the end of 50 * the Ethernet frame, this means if the frame is padded with non-zero values 51 * the H/W checksum will be incorrect, however the rx code compensates for this. 52 * 53 * TX checksuming is more complicated, the device requires a special header to 54 * be prefixed onto the start of the frame which indicates the start and end 55 * positions of the UDP or TCP frame. This requires the driver to manually 56 * go through the packet data and decode the headers prior to sending. 57 * On Linux they generally provide cues to the location of the csum and the 58 * area to calculate it over, on FreeBSD we seem to have to do it all ourselves, 59 * hence this is not as optimal and therefore h/w TX checksum is currently not 60 * implemented. 61 */ 62 63 #ifdef _KERNEL_OPT 64 #include "opt_usb.h" 65 #include "opt_inet.h" 66 #endif 67 68 #include <sys/param.h> 69 #include <sys/bus.h> 70 #include <sys/systm.h> 71 #include <sys/sockio.h> 72 #include <sys/mbuf.h> 73 #include <sys/mutex.h> 74 #include <sys/kernel.h> 75 #include <sys/proc.h> 76 #include <sys/socket.h> 77 78 #include <sys/device.h> 79 80 #include <sys/rnd.h> 81 82 #include <net/if.h> 83 #include <net/if_dl.h> 84 #include <net/if_media.h> 85 #include <net/if_ether.h> 86 87 #include <net/bpf.h> 88 89 #ifdef INET 90 #include <netinet/in.h> 91 #include <netinet/if_inarp.h> 92 #endif 93 94 #include <dev/mii/mii.h> 95 #include <dev/mii/miivar.h> 96 97 #include <dev/usb/usb.h> 98 #include <dev/usb/usbdi.h> 99 #include <dev/usb/usbdi_util.h> 100 #include <dev/usb/usbdivar.h> 101 #include <dev/usb/usbdevs.h> 102 103 #include <dev/usb/if_smscreg.h> 104 #include <dev/usb/if_smscvar.h> 105 106 #include "ioconf.h" 107 108 #ifdef USB_DEBUG 109 int smsc_debug = 0; 110 #endif 111 112 #define ETHER_ALIGN 2 113 /* 114 * Various supported device vendors/products. 115 */ 116 static const struct usb_devno smsc_devs[] = { 117 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN89530 }, 118 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN9530 }, 119 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_LAN9730 }, 120 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500 }, 121 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A }, 122 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_ALT }, 123 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_HAL }, 124 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500A_SAL10 }, 125 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500_ALT }, 126 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9500_SAL10 }, 127 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505 }, 128 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A }, 129 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A_HAL }, 130 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505A_SAL10 }, 131 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9505_SAL10 }, 132 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14 }, 133 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14_ALT }, 134 { USB_VENDOR_SMSC, USB_PRODUCT_SMSC_SMSC9512_14_SAL10 } 135 }; 136 137 #ifdef USB_DEBUG 138 #define smsc_dbg_printf(sc, fmt, args...) \ 139 do { \ 140 if (smsc_debug > 0) \ 141 printf("debug: " fmt, ##args); \ 142 } while(0) 143 #else 144 #define smsc_dbg_printf(sc, fmt, args...) 145 #endif 146 147 #define smsc_warn_printf(sc, fmt, args...) \ 148 printf("%s: warning: " fmt, device_xname((sc)->sc_dev), ##args) 149 150 #define smsc_err_printf(sc, fmt, args...) \ 151 printf("%s: error: " fmt, device_xname((sc)->sc_dev), ##args) 152 153 /* Function declarations */ 154 int smsc_chip_init(struct smsc_softc *); 155 void smsc_setmulti(struct smsc_softc *); 156 int smsc_setmacaddress(struct smsc_softc *, const uint8_t *); 157 158 int smsc_match(device_t, cfdata_t, void *); 159 void smsc_attach(device_t, device_t, void *); 160 int smsc_detach(device_t, int); 161 int smsc_activate(device_t, enum devact); 162 163 int smsc_init(struct ifnet *); 164 void smsc_start(struct ifnet *); 165 int smsc_ioctl(struct ifnet *, u_long, void *); 166 void smsc_stop(struct ifnet *, int); 167 168 void smsc_reset(struct smsc_softc *); 169 struct mbuf *smsc_newbuf(void); 170 171 void smsc_tick(void *); 172 void smsc_tick_task(void *); 173 void smsc_miibus_statchg(struct ifnet *); 174 int smsc_miibus_readreg(device_t, int, int); 175 void smsc_miibus_writereg(device_t, int, int, int); 176 int smsc_ifmedia_upd(struct ifnet *); 177 void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *); 178 void smsc_lock_mii(struct smsc_softc *); 179 void smsc_unlock_mii(struct smsc_softc *); 180 181 int smsc_tx_list_init(struct smsc_softc *); 182 int smsc_rx_list_init(struct smsc_softc *); 183 int smsc_encap(struct smsc_softc *, struct mbuf *, int); 184 void smsc_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 185 void smsc_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 186 187 int smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *); 188 int smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t); 189 int smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t); 190 int smsc_sethwcsum(struct smsc_softc *); 191 192 CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc), smsc_match, smsc_attach, 193 smsc_detach, smsc_activate); 194 195 int 196 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data) 197 { 198 usb_device_request_t req; 199 uint32_t buf; 200 usbd_status err; 201 202 req.bmRequestType = UT_READ_VENDOR_DEVICE; 203 req.bRequest = SMSC_UR_READ_REG; 204 USETW(req.wValue, 0); 205 USETW(req.wIndex, off); 206 USETW(req.wLength, 4); 207 208 err = usbd_do_request(sc->sc_udev, &req, &buf); 209 if (err != 0) 210 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off); 211 212 *data = le32toh(buf); 213 214 return (err); 215 } 216 217 int 218 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data) 219 { 220 usb_device_request_t req; 221 uint32_t buf; 222 usbd_status err; 223 224 buf = htole32(data); 225 226 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 227 req.bRequest = SMSC_UR_WRITE_REG; 228 USETW(req.wValue, 0); 229 USETW(req.wIndex, off); 230 USETW(req.wLength, 4); 231 232 err = usbd_do_request(sc->sc_udev, &req, &buf); 233 if (err != 0) 234 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off); 235 236 return (err); 237 } 238 239 int 240 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits) 241 { 242 uint32_t val; 243 int err, i; 244 245 for (i = 0; i < 100; i++) { 246 if ((err = smsc_read_reg(sc, reg, &val)) != 0) 247 return (err); 248 if (!(val & bits)) 249 return (0); 250 DELAY(5); 251 } 252 253 return (1); 254 } 255 256 int 257 smsc_miibus_readreg(device_t dev, int phy, int reg) 258 { 259 struct smsc_softc *sc = device_private(dev); 260 uint32_t addr; 261 uint32_t val = 0; 262 263 smsc_lock_mii(sc); 264 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { 265 smsc_warn_printf(sc, "MII is busy\n"); 266 goto done; 267 } 268 269 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ; 270 smsc_write_reg(sc, SMSC_MII_ADDR, addr); 271 272 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) 273 smsc_warn_printf(sc, "MII read timeout\n"); 274 275 smsc_read_reg(sc, SMSC_MII_DATA, &val); 276 277 done: 278 smsc_unlock_mii(sc); 279 280 return (val & 0xFFFF); 281 } 282 283 void 284 smsc_miibus_writereg(device_t dev, int phy, int reg, int val) 285 { 286 struct smsc_softc *sc = device_private(dev); 287 uint32_t addr; 288 289 if (sc->sc_phyno != phy) 290 return; 291 292 smsc_lock_mii(sc); 293 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { 294 smsc_warn_printf(sc, "MII is busy\n"); 295 smsc_unlock_mii(sc); 296 return; 297 } 298 299 smsc_write_reg(sc, SMSC_MII_DATA, val); 300 301 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE; 302 smsc_write_reg(sc, SMSC_MII_ADDR, addr); 303 smsc_unlock_mii(sc); 304 305 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) 306 smsc_warn_printf(sc, "MII write timeout\n"); 307 } 308 309 void 310 smsc_miibus_statchg(struct ifnet *ifp) 311 { 312 struct smsc_softc *sc = ifp->if_softc; 313 struct mii_data *mii = &sc->sc_mii; 314 int err; 315 uint32_t flow; 316 uint32_t afc_cfg; 317 318 if (mii == NULL || ifp == NULL || 319 (ifp->if_flags & IFF_RUNNING) == 0) 320 return; 321 322 /* Use the MII status to determine link status */ 323 sc->sc_flags &= ~SMSC_FLAG_LINK; 324 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 325 (IFM_ACTIVE | IFM_AVALID)) { 326 switch (IFM_SUBTYPE(mii->mii_media_active)) { 327 case IFM_10_T: 328 case IFM_100_TX: 329 sc->sc_flags |= SMSC_FLAG_LINK; 330 break; 331 case IFM_1000_T: 332 /* Gigabit ethernet not supported by chipset */ 333 break; 334 default: 335 break; 336 } 337 } 338 339 /* Lost link, do nothing. */ 340 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) { 341 smsc_dbg_printf(sc, "link flag not set\n"); 342 return; 343 } 344 345 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg); 346 if (err) { 347 smsc_warn_printf(sc, "failed to read initial AFC_CFG, " 348 "error %d\n", err); 349 return; 350 } 351 352 /* Enable/disable full duplex operation and TX/RX pause */ 353 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) { 354 smsc_dbg_printf(sc, "full duplex operation\n"); 355 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN; 356 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX; 357 358 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0) 359 flow = 0xffff0002; 360 else 361 flow = 0; 362 363 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0) 364 afc_cfg |= 0xf; 365 else 366 afc_cfg &= ~0xf; 367 368 } else { 369 smsc_dbg_printf(sc, "half duplex operation\n"); 370 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX; 371 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN; 372 373 flow = 0; 374 afc_cfg |= 0xf; 375 } 376 377 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 378 err += smsc_write_reg(sc, SMSC_FLOW, flow); 379 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg); 380 if (err) 381 smsc_warn_printf(sc, "media change failed, error %d\n", err); 382 } 383 384 int 385 smsc_ifmedia_upd(struct ifnet *ifp) 386 { 387 struct smsc_softc *sc = ifp->if_softc; 388 struct mii_data *mii = &sc->sc_mii; 389 int err; 390 391 if (mii->mii_instance) { 392 struct mii_softc *miisc; 393 394 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 395 mii_phy_reset(miisc); 396 } 397 err = mii_mediachg(mii); 398 return (err); 399 } 400 401 void 402 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 403 { 404 struct smsc_softc *sc = ifp->if_softc; 405 struct mii_data *mii = &sc->sc_mii; 406 407 mii_pollstat(mii); 408 409 ifmr->ifm_active = mii->mii_media_active; 410 ifmr->ifm_status = mii->mii_media_status; 411 } 412 413 static inline uint32_t 414 smsc_hash(uint8_t addr[ETHER_ADDR_LEN]) 415 { 416 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f; 417 } 418 419 void 420 smsc_setmulti(struct smsc_softc *sc) 421 { 422 struct ifnet *ifp = &sc->sc_ec.ec_if; 423 struct ether_multi *enm; 424 struct ether_multistep step; 425 uint32_t hashtbl[2] = { 0, 0 }; 426 uint32_t hash; 427 428 if (sc->sc_dying) 429 return; 430 431 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) { 432 allmulti: 433 smsc_dbg_printf(sc, "receive all multicast enabled\n"); 434 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS; 435 sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT; 436 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 437 return; 438 } else { 439 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT; 440 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS); 441 } 442 443 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm); 444 while (enm != NULL) { 445 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 446 ETHER_ADDR_LEN) != 0) 447 goto allmulti; 448 449 hash = smsc_hash(enm->enm_addrlo); 450 hashtbl[hash >> 5] |= 1 << (hash & 0x1F); 451 ETHER_NEXT_MULTI(step, enm); 452 } 453 454 /* Debug */ 455 if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) { 456 smsc_dbg_printf(sc, "receive select group of macs\n"); 457 } else { 458 smsc_dbg_printf(sc, "receive own packets only\n"); 459 } 460 461 /* Write the hash table and mac control registers */ 462 ifp->if_flags &= ~IFF_ALLMULTI; 463 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]); 464 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]); 465 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 466 } 467 468 int 469 smsc_sethwcsum(struct smsc_softc *sc) 470 { 471 struct ifnet *ifp = &sc->sc_ec.ec_if; 472 uint32_t val; 473 int err; 474 475 if (!ifp) 476 return EIO; 477 478 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val); 479 if (err != 0) { 480 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", 481 err); 482 return (err); 483 } 484 485 /* Enable/disable the Rx checksum */ 486 if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx)) 487 val |= (SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE); 488 else 489 val &= ~(SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE); 490 491 /* Enable/disable the Tx checksum (currently not supported) */ 492 if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_UDPv4_Tx)) 493 val |= SMSC_COE_CTRL_TX_EN; 494 else 495 val &= ~SMSC_COE_CTRL_TX_EN; 496 497 sc->sc_coe_ctrl = val; 498 499 err = smsc_write_reg(sc, SMSC_COE_CTRL, val); 500 if (err != 0) { 501 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", 502 err); 503 return (err); 504 } 505 506 return (0); 507 } 508 509 int 510 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr) 511 { 512 int err; 513 uint32_t val; 514 515 smsc_dbg_printf(sc, "setting mac address to " 516 "%02x:%02x:%02x:%02x:%02x:%02x\n", 517 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 518 519 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0]; 520 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0) 521 goto done; 522 523 val = (addr[5] << 8) | addr[4]; 524 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val); 525 526 done: 527 return (err); 528 } 529 530 void 531 smsc_reset(struct smsc_softc *sc) 532 { 533 if (sc->sc_dying) 534 return; 535 536 /* Wait a little while for the chip to get its brains in order. */ 537 DELAY(1000); 538 539 /* Reinitialize controller to achieve full reset. */ 540 smsc_chip_init(sc); 541 } 542 543 int 544 smsc_init(struct ifnet *ifp) 545 { 546 struct smsc_softc *sc = ifp->if_softc; 547 struct smsc_chain *c; 548 usbd_status err; 549 int s, i; 550 551 if (sc->sc_dying) 552 return EIO; 553 554 s = splnet(); 555 556 /* Cancel pending I/O */ 557 if (ifp->if_flags & IFF_RUNNING) 558 smsc_stop(ifp, 1); 559 560 /* Reset the ethernet interface. */ 561 smsc_reset(sc); 562 563 /* Init RX ring. */ 564 if (smsc_rx_list_init(sc) == ENOBUFS) { 565 aprint_error_dev(sc->sc_dev, "rx list init failed\n"); 566 splx(s); 567 return EIO; 568 } 569 570 /* Init TX ring. */ 571 if (smsc_tx_list_init(sc) == ENOBUFS) { 572 aprint_error_dev(sc->sc_dev, "tx list init failed\n"); 573 splx(s); 574 return EIO; 575 } 576 577 /* Load the multicast filter. */ 578 smsc_setmulti(sc); 579 580 /* TCP/UDP checksum offload engines. */ 581 smsc_sethwcsum(sc); 582 583 /* Open RX and TX pipes. */ 584 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX], 585 USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_RX]); 586 if (err) { 587 printf("%s: open rx pipe failed: %s\n", 588 device_xname(sc->sc_dev), usbd_errstr(err)); 589 splx(s); 590 return EIO; 591 } 592 593 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX], 594 USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_TX]); 595 if (err) { 596 printf("%s: open tx pipe failed: %s\n", 597 device_xname(sc->sc_dev), usbd_errstr(err)); 598 splx(s); 599 return EIO; 600 } 601 602 /* Start up the receive pipe. */ 603 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 604 c = &sc->sc_cdata.rx_chain[i]; 605 usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_RX], 606 c, c->sc_buf, sc->sc_bufsz, 607 USBD_SHORT_XFER_OK | USBD_NO_COPY, 608 USBD_NO_TIMEOUT, smsc_rxeof); 609 usbd_transfer(c->sc_xfer); 610 } 611 612 /* Indicate we are up and running. */ 613 ifp->if_flags |= IFF_RUNNING; 614 ifp->if_flags &= ~IFF_OACTIVE; 615 616 splx(s); 617 618 callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc); 619 620 return 0; 621 } 622 623 void 624 smsc_start(struct ifnet *ifp) 625 { 626 struct smsc_softc *sc = ifp->if_softc; 627 struct mbuf *m_head = NULL; 628 629 /* Don't send anything if there is no link or controller is busy. */ 630 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) { 631 return; 632 } 633 634 if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING) 635 return; 636 637 IFQ_POLL(&ifp->if_snd, m_head); 638 if (m_head == NULL) 639 return; 640 641 if (smsc_encap(sc, m_head, 0)) { 642 ifp->if_flags |= IFF_OACTIVE; 643 return; 644 } 645 IFQ_DEQUEUE(&ifp->if_snd, m_head); 646 647 bpf_mtap(ifp, m_head); 648 649 ifp->if_flags |= IFF_OACTIVE; 650 651 /* 652 * Set a timeout in case the chip goes out to lunch. 653 */ 654 ifp->if_timer = 5; 655 } 656 657 void 658 smsc_tick(void *xsc) 659 { 660 struct smsc_softc *sc = xsc; 661 662 if (sc == NULL) 663 return; 664 665 if (sc->sc_dying) 666 return; 667 668 usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER); 669 } 670 671 void 672 smsc_stop(struct ifnet *ifp, int disable) 673 { 674 usbd_status err; 675 struct smsc_softc *sc = ifp->if_softc; 676 int i; 677 678 smsc_reset(sc); 679 680 ifp = &sc->sc_ec.ec_if; 681 ifp->if_timer = 0; 682 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 683 684 callout_stop(&sc->sc_stat_ch); 685 686 /* Stop transfers. */ 687 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) { 688 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 689 if (err) { 690 printf("%s: abort rx pipe failed: %s\n", 691 device_xname(sc->sc_dev), usbd_errstr(err)); 692 } 693 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 694 if (err) { 695 printf("%s: close rx pipe failed: %s\n", 696 device_xname(sc->sc_dev), usbd_errstr(err)); 697 } 698 sc->sc_ep[SMSC_ENDPT_RX] = NULL; 699 } 700 701 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) { 702 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 703 if (err) { 704 printf("%s: abort tx pipe failed: %s\n", 705 device_xname(sc->sc_dev), usbd_errstr(err)); 706 } 707 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 708 if (err) { 709 printf("%s: close tx pipe failed: %s\n", 710 device_xname(sc->sc_dev), usbd_errstr(err)); 711 } 712 sc->sc_ep[SMSC_ENDPT_TX] = NULL; 713 } 714 715 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) { 716 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 717 if (err) { 718 printf("%s: abort intr pipe failed: %s\n", 719 device_xname(sc->sc_dev), usbd_errstr(err)); 720 } 721 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 722 if (err) { 723 printf("%s: close intr pipe failed: %s\n", 724 device_xname(sc->sc_dev), usbd_errstr(err)); 725 } 726 sc->sc_ep[SMSC_ENDPT_INTR] = NULL; 727 } 728 729 /* Free RX resources. */ 730 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 731 if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) { 732 m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf); 733 sc->sc_cdata.rx_chain[i].sc_mbuf = NULL; 734 } 735 if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) { 736 usbd_free_xfer(sc->sc_cdata.rx_chain[i].sc_xfer); 737 sc->sc_cdata.rx_chain[i].sc_xfer = NULL; 738 } 739 } 740 741 /* Free TX resources. */ 742 for (i = 0; i < SMSC_TX_LIST_CNT; i++) { 743 if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) { 744 m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf); 745 sc->sc_cdata.tx_chain[i].sc_mbuf = NULL; 746 } 747 if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) { 748 usbd_free_xfer(sc->sc_cdata.tx_chain[i].sc_xfer); 749 sc->sc_cdata.tx_chain[i].sc_xfer = NULL; 750 } 751 } 752 } 753 754 int 755 smsc_chip_init(struct smsc_softc *sc) 756 { 757 int err; 758 uint32_t reg_val; 759 int burst_cap; 760 761 /* Enter H/W config mode */ 762 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST); 763 764 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, 765 SMSC_HW_CFG_LRST)) != 0) { 766 smsc_warn_printf(sc, "timed-out waiting for reset to " 767 "complete\n"); 768 goto init_failed; 769 } 770 771 /* Reset the PHY */ 772 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST); 773 774 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, 775 SMSC_PM_CTRL_PHY_RST) != 0)) { 776 smsc_warn_printf(sc, "timed-out waiting for phy reset to " 777 "complete\n"); 778 goto init_failed; 779 } 780 usbd_delay_ms(sc->sc_udev, 40); 781 782 /* Set the mac address */ 783 struct ifnet *ifp = &sc->sc_ec.ec_if; 784 const char *eaddr = CLLADDR(ifp->if_sadl); 785 if ((err = smsc_setmacaddress(sc, eaddr)) != 0) { 786 smsc_warn_printf(sc, "failed to set the MAC address\n"); 787 goto init_failed; 788 } 789 790 /* 791 * Don't know what the HW_CFG_BIR bit is, but following the reset 792 * sequence as used in the Linux driver. 793 */ 794 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) { 795 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err); 796 goto init_failed; 797 } 798 reg_val |= SMSC_HW_CFG_BIR; 799 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 800 801 /* 802 * There is a so called 'turbo mode' that the linux driver supports, it 803 * seems to allow you to jam multiple frames per Rx transaction. 804 * By default this driver supports that and therefore allows multiple 805 * frames per USB transfer. 806 * 807 * The xfer buffer size needs to reflect this as well, therefore based 808 * on the calculations in the Linux driver the RX bufsize is set to 809 * 18944, 810 * bufsz = (16 * 1024 + 5 * 512) 811 * 812 * Burst capability is the number of URBs that can be in a burst of 813 * data/ethernet frames. 814 */ 815 816 if (sc->sc_udev->speed == USB_SPEED_HIGH) 817 burst_cap = 37; 818 else 819 burst_cap = 128; 820 821 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap); 822 823 /* Set the default bulk in delay (magic value from Linux driver) */ 824 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000); 825 826 /* 827 * Initialise the RX interface 828 */ 829 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) { 830 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", 831 err); 832 goto init_failed; 833 } 834 835 /* 836 * The following settings are used for 'turbo mode', a.k.a multiple 837 * frames per Rx transaction (again info taken form Linux driver). 838 */ 839 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE); 840 841 /* 842 * set Rx data offset to ETHER_ALIGN which will make the IP header 843 * align on a word boundary. 844 */ 845 reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT; 846 847 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 848 849 /* Clear the status register ? */ 850 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff); 851 852 /* Read and display the revision register */ 853 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) { 854 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err); 855 goto init_failed; 856 } 857 858 /* GPIO/LED setup */ 859 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED | 860 SMSC_LED_GPIO_CFG_FDX_LED; 861 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val); 862 863 /* 864 * Initialise the TX interface 865 */ 866 smsc_write_reg(sc, SMSC_FLOW, 0); 867 868 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT); 869 870 /* Read the current MAC configuration */ 871 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) { 872 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err); 873 goto init_failed; 874 } 875 876 /* disable pad stripping, collides with checksum offload */ 877 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR; 878 879 /* Vlan */ 880 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN); 881 882 /* 883 * Start TX 884 */ 885 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN; 886 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 887 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON); 888 889 /* 890 * Start RX 891 */ 892 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN; 893 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 894 895 return (0); 896 897 init_failed: 898 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err); 899 return (err); 900 } 901 902 int 903 smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data) 904 { 905 struct smsc_softc *sc = ifp->if_softc; 906 struct ifreq /*const*/ *ifr = data; 907 int s, error = 0; 908 909 if (sc->sc_dying) 910 return EIO; 911 912 s = splnet(); 913 914 switch(cmd) { 915 case SIOCSIFFLAGS: 916 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 917 break; 918 919 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { 920 case IFF_RUNNING: 921 smsc_stop(ifp, 1); 922 break; 923 case IFF_UP: 924 smsc_init(ifp); 925 break; 926 case IFF_UP | IFF_RUNNING: 927 if (ifp->if_flags & IFF_PROMISC && 928 !(sc->sc_if_flags & IFF_PROMISC)) { 929 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS; 930 smsc_write_reg(sc, SMSC_MAC_CSR, 931 sc->sc_mac_csr); 932 smsc_setmulti(sc); 933 } else if (!(ifp->if_flags & IFF_PROMISC) && 934 sc->sc_if_flags & IFF_PROMISC) { 935 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS; 936 smsc_write_reg(sc, SMSC_MAC_CSR, 937 sc->sc_mac_csr); 938 smsc_setmulti(sc); 939 } else { 940 smsc_init(ifp); 941 } 942 break; 943 } 944 sc->sc_if_flags = ifp->if_flags; 945 break; 946 947 case SIOCGIFMEDIA: 948 case SIOCSIFMEDIA: 949 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd); 950 break; 951 952 default: 953 if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET) 954 break; 955 956 error = 0; 957 958 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) 959 smsc_setmulti(sc); 960 961 } 962 splx(s); 963 964 return error; 965 } 966 967 int 968 smsc_match(device_t parent, cfdata_t match, void *aux) 969 { 970 struct usb_attach_arg *uaa = aux; 971 972 return (usb_lookup(smsc_devs, uaa->vendor, uaa->product) != NULL) ? 973 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 974 } 975 976 void 977 smsc_attach(device_t parent, device_t self, void *aux) 978 { 979 struct smsc_softc *sc = device_private(self); 980 struct usb_attach_arg *uaa = aux; 981 usbd_device_handle dev = uaa->device; 982 usb_interface_descriptor_t *id; 983 usb_endpoint_descriptor_t *ed; 984 char *devinfop; 985 struct mii_data *mii; 986 struct ifnet *ifp; 987 int err, s, i; 988 uint32_t mac_h, mac_l; 989 990 sc->sc_dev = self; 991 sc->sc_udev = dev; 992 993 aprint_naive("\n"); 994 aprint_normal("\n"); 995 996 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0); 997 aprint_normal_dev(self, "%s\n", devinfop); 998 usbd_devinfo_free(devinfop); 999 1000 err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1); 1001 if (err) { 1002 aprint_error_dev(self, "failed to set configuration" 1003 ", err=%s\n", usbd_errstr(err)); 1004 return; 1005 } 1006 /* Setup the endpoints for the SMSC LAN95xx device(s) */ 1007 usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, 0); 1008 usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc, 0); 1009 mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE); 1010 1011 err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface); 1012 if (err) { 1013 aprint_error_dev(self, "getting interface handle failed\n"); 1014 return; 1015 } 1016 1017 id = usbd_get_interface_descriptor(sc->sc_iface); 1018 1019 if (sc->sc_udev->speed >= USB_SPEED_HIGH) 1020 sc->sc_bufsz = SMSC_MAX_BUFSZ; 1021 else 1022 sc->sc_bufsz = SMSC_MIN_BUFSZ; 1023 1024 /* Find endpoints. */ 1025 for (i = 0; i < id->bNumEndpoints; i++) { 1026 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 1027 if (!ed) { 1028 aprint_error_dev(self, "couldn't get ep %d\n", i); 1029 return; 1030 } 1031 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 1032 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 1033 sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress; 1034 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 1035 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 1036 sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress; 1037 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 1038 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 1039 sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress; 1040 } 1041 } 1042 1043 s = splnet(); 1044 1045 ifp = &sc->sc_ec.ec_if; 1046 ifp->if_softc = sc; 1047 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ); 1048 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1049 ifp->if_init = smsc_init; 1050 ifp->if_ioctl = smsc_ioctl; 1051 ifp->if_start = smsc_start; 1052 ifp->if_stop = smsc_stop; 1053 1054 #ifdef notyet 1055 /* 1056 * We can do TCPv4, and UDPv4 checksums in hardware. 1057 */ 1058 ifp->if_capabilities |= 1059 /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx | 1060 /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx; 1061 #endif 1062 1063 sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU; 1064 1065 /* Setup some of the basics */ 1066 sc->sc_phyno = 1; 1067 1068 /* 1069 * Attempt to get the mac address, if an EEPROM is not attached this 1070 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC 1071 * address based on urandom. 1072 */ 1073 memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN); 1074 1075 prop_dictionary_t dict = device_properties(self); 1076 prop_data_t eaprop = prop_dictionary_get(dict, "mac-address"); 1077 1078 if (eaprop != NULL) { 1079 KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA); 1080 KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN); 1081 memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop), 1082 ETHER_ADDR_LEN); 1083 } else 1084 /* Check if there is already a MAC address in the register */ 1085 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) && 1086 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) { 1087 sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); 1088 sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff); 1089 sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); 1090 sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); 1091 sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); 1092 sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff); 1093 } 1094 1095 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(sc->sc_enaddr)); 1096 1097 IFQ_SET_READY(&ifp->if_snd); 1098 1099 /* Initialize MII/media info. */ 1100 mii = &sc->sc_mii; 1101 mii->mii_ifp = ifp; 1102 mii->mii_readreg = smsc_miibus_readreg; 1103 mii->mii_writereg = smsc_miibus_writereg; 1104 mii->mii_statchg = smsc_miibus_statchg; 1105 mii->mii_flags = MIIF_AUTOTSLEEP; 1106 sc->sc_ec.ec_mii = mii; 1107 ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts); 1108 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 1109 1110 if (LIST_FIRST(&mii->mii_phys) == NULL) { 1111 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 1112 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 1113 } else 1114 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 1115 1116 if_attach(ifp); 1117 ether_ifattach(ifp, sc->sc_enaddr); 1118 1119 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev), 1120 RND_TYPE_NET, RND_FLAG_DEFAULT); 1121 1122 callout_init(&sc->sc_stat_ch, 0); 1123 1124 splx(s); 1125 1126 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 1127 } 1128 1129 int 1130 smsc_detach(device_t self, int flags) 1131 { 1132 struct smsc_softc *sc = device_private(self); 1133 struct ifnet *ifp = &sc->sc_ec.ec_if; 1134 int s; 1135 1136 callout_stop(&sc->sc_stat_ch); 1137 1138 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) 1139 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 1140 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) 1141 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 1142 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) 1143 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 1144 1145 /* 1146 * Remove any pending tasks. They cannot be executing because they run 1147 * in the same thread as detach. 1148 */ 1149 usb_rem_task(sc->sc_udev, &sc->sc_tick_task); 1150 usb_rem_task(sc->sc_udev, &sc->sc_stop_task); 1151 1152 s = splusb(); 1153 1154 if (--sc->sc_refcnt >= 0) { 1155 /* Wait for processes to go away */ 1156 usb_detach_waitold(sc->sc_dev); 1157 } 1158 1159 if (ifp->if_flags & IFF_RUNNING) 1160 smsc_stop(ifp ,1); 1161 1162 rnd_detach_source(&sc->sc_rnd_source); 1163 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 1164 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 1165 if (ifp->if_softc != NULL) { 1166 ether_ifdetach(ifp); 1167 if_detach(ifp); 1168 } 1169 1170 #ifdef DIAGNOSTIC 1171 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL || 1172 sc->sc_ep[SMSC_ENDPT_RX] != NULL || 1173 sc->sc_ep[SMSC_ENDPT_INTR] != NULL) 1174 printf("%s: detach has active endpoints\n", 1175 device_xname(sc->sc_dev)); 1176 #endif 1177 1178 if (--sc->sc_refcnt >= 0) { 1179 /* Wait for processes to go away. */ 1180 usb_detach_waitold(sc->sc_dev); 1181 } 1182 splx(s); 1183 1184 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 1185 1186 mutex_destroy(&sc->sc_mii_lock); 1187 1188 return (0); 1189 } 1190 1191 void 1192 smsc_tick_task(void *xsc) 1193 { 1194 int s; 1195 struct smsc_softc *sc = xsc; 1196 struct ifnet *ifp; 1197 struct mii_data *mii; 1198 1199 if (sc == NULL) 1200 return; 1201 1202 if (sc->sc_dying) 1203 return; 1204 ifp = &sc->sc_ec.ec_if; 1205 mii = &sc->sc_mii; 1206 if (mii == NULL) 1207 return; 1208 1209 s = splnet(); 1210 1211 mii_tick(mii); 1212 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) 1213 smsc_miibus_statchg(ifp); 1214 callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc); 1215 1216 splx(s); 1217 } 1218 1219 int 1220 smsc_activate(device_t self, enum devact act) 1221 { 1222 struct smsc_softc *sc = device_private(self); 1223 1224 switch (act) { 1225 case DVACT_DEACTIVATE: 1226 if_deactivate(&sc->sc_ec.ec_if); 1227 sc->sc_dying = 1; 1228 return 0; 1229 default: 1230 return EOPNOTSUPP; 1231 } 1232 return (0); 1233 } 1234 1235 void 1236 smsc_lock_mii(struct smsc_softc *sc) 1237 { 1238 sc->sc_refcnt++; 1239 mutex_enter(&sc->sc_mii_lock); 1240 } 1241 1242 void 1243 smsc_unlock_mii(struct smsc_softc *sc) 1244 { 1245 mutex_exit(&sc->sc_mii_lock); 1246 if (--sc->sc_refcnt < 0) 1247 usb_detach_wakeupold(sc->sc_dev); 1248 } 1249 1250 void 1251 smsc_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1252 { 1253 struct smsc_chain *c = (struct smsc_chain *)priv; 1254 struct smsc_softc *sc = c->sc_sc; 1255 struct ifnet *ifp = &sc->sc_ec.ec_if; 1256 u_char *buf = c->sc_buf; 1257 uint32_t total_len; 1258 uint32_t rxhdr; 1259 uint16_t pktlen; 1260 struct mbuf *m; 1261 int s; 1262 1263 if (sc->sc_dying) 1264 return; 1265 1266 if (!(ifp->if_flags & IFF_RUNNING)) 1267 return; 1268 1269 if (status != USBD_NORMAL_COMPLETION) { 1270 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1271 return; 1272 if (usbd_ratecheck(&sc->sc_rx_notice)) { 1273 printf("%s: usb errors on rx: %s\n", 1274 device_xname(sc->sc_dev), usbd_errstr(status)); 1275 } 1276 if (status == USBD_STALLED) 1277 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]); 1278 goto done; 1279 } 1280 1281 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 1282 smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len); 1283 1284 while (total_len != 0) { 1285 if (total_len < sizeof(rxhdr)) { 1286 smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n", 1287 total_len, sizeof(rxhdr)); 1288 ifp->if_ierrors++; 1289 goto done; 1290 } 1291 1292 memcpy(&rxhdr, buf, sizeof(rxhdr)); 1293 rxhdr = le32toh(rxhdr); 1294 buf += sizeof(rxhdr); 1295 total_len -= sizeof(rxhdr); 1296 1297 if (rxhdr & SMSC_RX_STAT_ERROR) { 1298 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr); 1299 ifp->if_ierrors++; 1300 goto done; 1301 } 1302 1303 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr); 1304 smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr " 1305 "0x%08x\n", total_len, pktlen, rxhdr); 1306 1307 pktlen += ETHER_ALIGN; 1308 1309 if (pktlen > MCLBYTES) { 1310 smsc_dbg_printf(sc, "pktlen %d > MCLBYTES %d\n", 1311 pktlen, MCLBYTES); 1312 ifp->if_ierrors++; 1313 goto done; 1314 } 1315 1316 if (pktlen > total_len) { 1317 smsc_dbg_printf(sc, "pktlen %d > total_len %d\n", 1318 pktlen, total_len); 1319 ifp->if_ierrors++; 1320 goto done; 1321 } 1322 1323 m = smsc_newbuf(); 1324 if (m == NULL) { 1325 smsc_dbg_printf(sc, "smc_newbuf returned NULL\n"); 1326 ifp->if_ierrors++; 1327 goto done; 1328 } 1329 1330 ifp->if_ipackets++; 1331 m->m_pkthdr.rcvif = ifp; 1332 m->m_pkthdr.len = m->m_len = pktlen; 1333 m->m_flags |= M_HASFCS; 1334 m_adj(m, ETHER_ALIGN); 1335 1336 KASSERT(m->m_len < MCLBYTES); 1337 memcpy(mtod(m, char *), buf + ETHER_ALIGN, m->m_len); 1338 1339 /* Check if RX TCP/UDP checksumming is being offloaded */ 1340 if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) { 1341 smsc_dbg_printf(sc,"RX checksum offload checking\n"); 1342 struct ether_header *eh; 1343 1344 eh = mtod(m, struct ether_header *); 1345 1346 /* Remove the extra 2 bytes of the csum */ 1347 m_adj(m, -2); 1348 1349 /* 1350 * The checksum appears to be simplistically calculated 1351 * over the udp/tcp header and data up to the end of the 1352 * eth frame. Which means if the eth frame is padded 1353 * the csum calculation is incorrectly performed over 1354 * the padding bytes as well. Therefore to be safe we 1355 * ignore the H/W csum on frames less than or equal to 1356 * 64 bytes. 1357 * 1358 * Ignore H/W csum for non-IPv4 packets. 1359 */ 1360 smsc_dbg_printf(sc,"Ethertype %02x pktlen %02x\n", 1361 be16toh(eh->ether_type), pktlen); 1362 if (be16toh(eh->ether_type) == ETHERTYPE_IP && 1363 pktlen > ETHER_MIN_LEN) { 1364 1365 m->m_pkthdr.csum_flags |= 1366 (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA); 1367 1368 /* 1369 * Copy the TCP/UDP checksum from the last 2 1370 * bytes of the transfer and put in the 1371 * csum_data field. 1372 */ 1373 memcpy(&m->m_pkthdr.csum_data, 1374 buf + pktlen - 2, 2); 1375 /* 1376 * The data is copied in network order, but the 1377 * csum algorithm in the kernel expects it to be 1378 * in host network order. 1379 */ 1380 m->m_pkthdr.csum_data = 1381 ntohs(m->m_pkthdr.csum_data); 1382 smsc_dbg_printf(sc, 1383 "RX checksum offloaded (0x%04x)\n", 1384 m->m_pkthdr.csum_data); 1385 } 1386 } 1387 1388 /* round up to next longword */ 1389 pktlen = (pktlen + 3) & ~0x3; 1390 1391 /* total_len does not include the padding */ 1392 if (pktlen > total_len) 1393 pktlen = total_len; 1394 1395 buf += pktlen; 1396 total_len -= pktlen; 1397 1398 /* push the packet up */ 1399 s = splnet(); 1400 bpf_mtap(ifp, m); 1401 ifp->if_input(ifp, m); 1402 splx(s); 1403 } 1404 1405 done: 1406 /* Setup new transfer. */ 1407 usbd_setup_xfer(xfer, sc->sc_ep[SMSC_ENDPT_RX], 1408 c, c->sc_buf, sc->sc_bufsz, 1409 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1410 USBD_NO_TIMEOUT, smsc_rxeof); 1411 usbd_transfer(xfer); 1412 1413 return; 1414 } 1415 1416 void 1417 smsc_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1418 { 1419 struct smsc_softc *sc; 1420 struct smsc_chain *c; 1421 struct ifnet *ifp; 1422 int s; 1423 1424 c = priv; 1425 sc = c->sc_sc; 1426 ifp = &sc->sc_ec.ec_if; 1427 1428 if (sc->sc_dying) 1429 return; 1430 1431 s = splnet(); 1432 1433 ifp->if_timer = 0; 1434 ifp->if_flags &= ~IFF_OACTIVE; 1435 1436 if (status != USBD_NORMAL_COMPLETION) { 1437 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1438 splx(s); 1439 return; 1440 } 1441 ifp->if_oerrors++; 1442 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev), 1443 usbd_errstr(status)); 1444 if (status == USBD_STALLED) 1445 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]); 1446 splx(s); 1447 return; 1448 } 1449 ifp->if_opackets++; 1450 1451 m_freem(c->sc_mbuf); 1452 c->sc_mbuf = NULL; 1453 1454 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1455 smsc_start(ifp); 1456 1457 splx(s); 1458 } 1459 1460 int 1461 smsc_tx_list_init(struct smsc_softc *sc) 1462 { 1463 struct smsc_cdata *cd; 1464 struct smsc_chain *c; 1465 int i; 1466 1467 cd = &sc->sc_cdata; 1468 for (i = 0; i < SMSC_TX_LIST_CNT; i++) { 1469 c = &cd->tx_chain[i]; 1470 c->sc_sc = sc; 1471 c->sc_idx = i; 1472 c->sc_mbuf = NULL; 1473 if (c->sc_xfer == NULL) { 1474 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev); 1475 if (c->sc_xfer == NULL) 1476 return (ENOBUFS); 1477 c->sc_buf = usbd_alloc_buffer(c->sc_xfer, 1478 sc->sc_bufsz); 1479 if (c->sc_buf == NULL) { 1480 usbd_free_xfer(c->sc_xfer); 1481 return (ENOBUFS); 1482 } 1483 } 1484 } 1485 1486 return (0); 1487 } 1488 1489 int 1490 smsc_rx_list_init(struct smsc_softc *sc) 1491 { 1492 struct smsc_cdata *cd; 1493 struct smsc_chain *c; 1494 int i; 1495 1496 cd = &sc->sc_cdata; 1497 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 1498 c = &cd->rx_chain[i]; 1499 c->sc_sc = sc; 1500 c->sc_idx = i; 1501 c->sc_mbuf = NULL; 1502 if (c->sc_xfer == NULL) { 1503 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev); 1504 if (c->sc_xfer == NULL) 1505 return (ENOBUFS); 1506 c->sc_buf = usbd_alloc_buffer(c->sc_xfer, 1507 sc->sc_bufsz); 1508 if (c->sc_buf == NULL) { 1509 usbd_free_xfer(c->sc_xfer); 1510 return (ENOBUFS); 1511 } 1512 } 1513 } 1514 1515 return (0); 1516 } 1517 1518 struct mbuf * 1519 smsc_newbuf(void) 1520 { 1521 struct mbuf *m; 1522 1523 MGETHDR(m, M_DONTWAIT, MT_DATA); 1524 if (m == NULL) 1525 return (NULL); 1526 1527 MCLGET(m, M_DONTWAIT); 1528 if (!(m->m_flags & M_EXT)) { 1529 m_freem(m); 1530 return (NULL); 1531 } 1532 1533 return (m); 1534 } 1535 1536 int 1537 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx) 1538 { 1539 struct ifnet *ifp = &sc->sc_ec.ec_if; 1540 struct smsc_chain *c; 1541 usbd_status err; 1542 uint32_t txhdr; 1543 uint32_t frm_len = 0; 1544 1545 c = &sc->sc_cdata.tx_chain[idx]; 1546 1547 /* 1548 * Each frame is prefixed with two 32-bit values describing the 1549 * length of the packet and buffer. 1550 */ 1551 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) | 1552 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG; 1553 txhdr = htole32(txhdr); 1554 memcpy(c->sc_buf, &txhdr, sizeof(txhdr)); 1555 1556 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len); 1557 txhdr = htole32(txhdr); 1558 memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr)); 1559 1560 frm_len += 8; 1561 1562 /* Next copy in the actual packet */ 1563 m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len); 1564 frm_len += m->m_pkthdr.len; 1565 1566 c->sc_mbuf = m; 1567 1568 usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_TX], 1569 c, c->sc_buf, frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 1570 10000, smsc_txeof); 1571 1572 err = usbd_transfer(c->sc_xfer); 1573 /* XXXNH get task to stop interface */ 1574 if (err != USBD_IN_PROGRESS) { 1575 smsc_stop(ifp, 0); 1576 return (EIO); 1577 } 1578 1579 sc->sc_cdata.tx_cnt++; 1580 1581 return (0); 1582 } 1583