1 /* $NetBSD: if_smsc.c,v 1.45 2019/05/23 13:10:52 msaitoh 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 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: if_smsc.c,v 1.45 2019/05/23 13:10:52 msaitoh Exp $"); 65 66 #ifdef _KERNEL_OPT 67 #include "opt_usb.h" 68 #include "opt_inet.h" 69 #endif 70 71 #include <sys/param.h> 72 #include <sys/bus.h> 73 #include <sys/device.h> 74 #include <sys/kernel.h> 75 #include <sys/mbuf.h> 76 #include <sys/mutex.h> 77 #include <sys/proc.h> 78 #include <sys/rndsource.h> 79 #include <sys/socket.h> 80 #include <sys/sockio.h> 81 #include <sys/systm.h> 82 83 #include <net/if.h> 84 #include <net/if_dl.h> 85 #include <net/if_media.h> 86 #include <net/if_ether.h> 87 88 #include <net/bpf.h> 89 90 #ifdef INET 91 #include <netinet/in.h> 92 #include <netinet/if_inarp.h> 93 #endif 94 95 #include <dev/mii/mii.h> 96 #include <dev/mii/miivar.h> 97 98 #include <dev/usb/usb.h> 99 #include <dev/usb/usbdi.h> 100 #include <dev/usb/usbdi_util.h> 101 #include <dev/usb/usbdivar.h> 102 #include <dev/usb/usbdevs.h> 103 104 #include <dev/usb/if_smscreg.h> 105 #include <dev/usb/if_smscvar.h> 106 107 #include "ioconf.h" 108 109 #ifdef USB_DEBUG 110 int smsc_debug = 0; 111 #endif 112 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 int smsc_init_locked(struct ifnet *); 165 void smsc_start(struct ifnet *); 166 void smsc_start_locked(struct ifnet *); 167 int smsc_ioctl(struct ifnet *, u_long, void *); 168 void smsc_stop(struct ifnet *, int); 169 void smsc_stop_locked(struct ifnet *, int); 170 171 void smsc_reset(struct smsc_softc *); 172 struct mbuf *smsc_newbuf(void); 173 174 void smsc_tick(void *); 175 void smsc_tick_task(void *); 176 void smsc_miibus_statchg(struct ifnet *); 177 void smsc_miibus_statchg_locked(struct ifnet *); 178 int smsc_miibus_readreg(device_t, int, int, uint16_t *); 179 int smsc_miibus_writereg(device_t, int, int, uint16_t); 180 int smsc_ifmedia_upd(struct ifnet *); 181 void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *); 182 void smsc_lock_mii(struct smsc_softc *); 183 void smsc_unlock_mii(struct smsc_softc *); 184 185 int smsc_tx_list_init(struct smsc_softc *); 186 void smsc_tx_list_free(struct smsc_softc *); 187 int smsc_rx_list_init(struct smsc_softc *); 188 void smsc_rx_list_free(struct smsc_softc *); 189 int smsc_encap(struct smsc_softc *, struct mbuf *, int); 190 void smsc_rxeof(struct usbd_xfer *, void *, usbd_status); 191 void smsc_txeof(struct usbd_xfer *, void *, usbd_status); 192 193 int smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *); 194 int smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t); 195 int smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t); 196 int smsc_sethwcsum(struct smsc_softc *); 197 198 CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc), smsc_match, smsc_attach, 199 smsc_detach, smsc_activate); 200 201 int 202 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data) 203 { 204 usb_device_request_t req; 205 uint32_t buf; 206 usbd_status err; 207 208 req.bmRequestType = UT_READ_VENDOR_DEVICE; 209 req.bRequest = SMSC_UR_READ_REG; 210 USETW(req.wValue, 0); 211 USETW(req.wIndex, off); 212 USETW(req.wLength, 4); 213 214 err = usbd_do_request(sc->sc_udev, &req, &buf); 215 if (err != 0) 216 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off); 217 218 *data = le32toh(buf); 219 220 return err; 221 } 222 223 int 224 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data) 225 { 226 usb_device_request_t req; 227 uint32_t buf; 228 usbd_status err; 229 230 buf = htole32(data); 231 232 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 233 req.bRequest = SMSC_UR_WRITE_REG; 234 USETW(req.wValue, 0); 235 USETW(req.wIndex, off); 236 USETW(req.wLength, 4); 237 238 err = usbd_do_request(sc->sc_udev, &req, &buf); 239 if (err != 0) 240 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off); 241 242 return err; 243 } 244 245 int 246 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits) 247 { 248 uint32_t val; 249 int err, i; 250 251 for (i = 0; i < 100; i++) { 252 if ((err = smsc_read_reg(sc, reg, &val)) != 0) 253 return err; 254 if (!(val & bits)) 255 return 0; 256 DELAY(5); 257 } 258 259 return 1; 260 } 261 262 int 263 smsc_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val) 264 { 265 struct smsc_softc * const sc = device_private(dev); 266 uint32_t addr; 267 uint32_t data = 0; 268 int rv = 0; 269 270 smsc_lock_mii(sc); 271 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { 272 smsc_warn_printf(sc, "MII is busy\n"); 273 rv = -1; 274 goto done; 275 } 276 277 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ; 278 smsc_write_reg(sc, SMSC_MII_ADDR, addr); 279 280 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { 281 smsc_warn_printf(sc, "MII read timeout\n"); 282 rv = ETIMEDOUT; 283 } 284 285 smsc_read_reg(sc, SMSC_MII_DATA, &data); 286 287 done: 288 smsc_unlock_mii(sc); 289 290 *val = data & 0xffff; 291 return rv; 292 } 293 294 int 295 smsc_miibus_writereg(device_t dev, int phy, int reg, uint16_t val) 296 { 297 struct smsc_softc * const sc = device_private(dev); 298 uint32_t addr; 299 300 if (sc->sc_phyno != phy) 301 return -1; 302 303 smsc_lock_mii(sc); 304 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { 305 smsc_warn_printf(sc, "MII is busy\n"); 306 smsc_unlock_mii(sc); 307 return -1; 308 } 309 310 smsc_write_reg(sc, SMSC_MII_DATA, val); 311 312 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE; 313 smsc_write_reg(sc, SMSC_MII_ADDR, addr); 314 smsc_unlock_mii(sc); 315 316 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { 317 smsc_warn_printf(sc, "MII write timeout\n"); 318 return ETIMEDOUT; 319 } 320 321 return 0; 322 } 323 324 void 325 smsc_miibus_statchg(struct ifnet *ifp) 326 { 327 if (ifp == NULL) 328 return; 329 330 struct smsc_softc * const sc = ifp->if_softc; 331 332 mutex_enter(&sc->sc_lock); 333 if (sc->sc_dying) { 334 mutex_exit(&sc->sc_lock); 335 return; 336 } 337 smsc_miibus_statchg_locked(ifp); 338 339 mutex_exit(&sc->sc_lock); 340 } 341 342 343 void 344 smsc_miibus_statchg_locked(struct ifnet *ifp) 345 { 346 struct smsc_softc * const sc = ifp->if_softc; 347 struct mii_data * const mii = &sc->sc_mii; 348 int err; 349 uint32_t flow; 350 uint32_t afc_cfg; 351 352 KASSERT(mutex_owned(&sc->sc_lock)); 353 354 if ((ifp->if_flags & IFF_RUNNING) == 0) { 355 smsc_dbg_printf(sc, "%s: not running\n", __func__); 356 return; 357 } 358 359 /* Use the MII status to determine link status */ 360 sc->sc_flags &= ~SMSC_FLAG_LINK; 361 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 362 (IFM_ACTIVE | IFM_AVALID)) { 363 switch (IFM_SUBTYPE(mii->mii_media_active)) { 364 case IFM_10_T: 365 case IFM_100_TX: 366 sc->sc_flags |= SMSC_FLAG_LINK; 367 break; 368 case IFM_1000_T: 369 /* Gigabit ethernet not supported by chipset */ 370 break; 371 default: 372 break; 373 } 374 } 375 376 /* Lost link, do nothing. */ 377 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) { 378 smsc_dbg_printf(sc, "link flag not set\n"); 379 return; 380 } 381 382 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg); 383 if (err) { 384 smsc_warn_printf(sc, "failed to read initial AFC_CFG, " 385 "error %d\n", err); 386 return; 387 } 388 389 /* Enable/disable full duplex operation and TX/RX pause */ 390 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) { 391 smsc_dbg_printf(sc, "full duplex operation\n"); 392 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN; 393 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX; 394 395 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0) 396 flow = 0xffff0002; 397 else 398 flow = 0; 399 400 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0) 401 afc_cfg |= 0xf; 402 else 403 afc_cfg &= ~0xf; 404 } else { 405 smsc_dbg_printf(sc, "half duplex operation\n"); 406 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX; 407 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN; 408 409 flow = 0; 410 afc_cfg |= 0xf; 411 } 412 413 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 414 err += smsc_write_reg(sc, SMSC_FLOW, flow); 415 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg); 416 if (err) 417 smsc_warn_printf(sc, "media change failed, error %d\n", err); 418 } 419 420 int 421 smsc_ifmedia_upd(struct ifnet *ifp) 422 { 423 struct smsc_softc * const sc = ifp->if_softc; 424 struct mii_data * const mii = &sc->sc_mii; 425 int err; 426 427 if (mii->mii_instance) { 428 struct mii_softc *miisc; 429 430 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 431 mii_phy_reset(miisc); 432 } 433 err = mii_mediachg(mii); 434 return err; 435 } 436 437 void 438 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 439 { 440 struct smsc_softc * const sc = ifp->if_softc; 441 struct mii_data * const mii = &sc->sc_mii; 442 443 /* SMSC_LOCK */ 444 445 mii_pollstat(mii); 446 447 ifmr->ifm_active = mii->mii_media_active; 448 ifmr->ifm_status = mii->mii_media_status; 449 450 /* SMSC_UNLOCK */ 451 } 452 453 static inline uint32_t 454 smsc_hash(uint8_t addr[ETHER_ADDR_LEN]) 455 { 456 457 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f; 458 } 459 460 void 461 smsc_setmulti(struct smsc_softc *sc) 462 { 463 struct ethercom *ec = &sc->sc_ec; 464 struct ifnet * const ifp = &ec->ec_if; 465 struct ether_multi *enm; 466 struct ether_multistep step; 467 uint32_t hashtbl[2] = { 0, 0 }; 468 uint32_t hash; 469 470 KASSERT(mutex_owned(&sc->sc_lock)); 471 472 if (sc->sc_dying) 473 return; 474 475 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) { 476 allmulti: 477 smsc_dbg_printf(sc, "receive all multicast enabled\n"); 478 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS; 479 sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT; 480 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 481 return; 482 } else { 483 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT; 484 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS); 485 } 486 487 ETHER_LOCK(ec); 488 ETHER_FIRST_MULTI(step, ec, enm); 489 while (enm != NULL) { 490 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) { 491 ETHER_UNLOCK(ec); 492 goto allmulti; 493 } 494 495 hash = smsc_hash(enm->enm_addrlo); 496 hashtbl[hash >> 5] |= 1 << (hash & 0x1F); 497 ETHER_NEXT_MULTI(step, enm); 498 } 499 ETHER_UNLOCK(ec); 500 501 /* Debug */ 502 if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) { 503 smsc_dbg_printf(sc, "receive select group of macs\n"); 504 } else { 505 smsc_dbg_printf(sc, "receive own packets only\n"); 506 } 507 508 /* Write the hash table and mac control registers */ 509 ifp->if_flags &= ~IFF_ALLMULTI; 510 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]); 511 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]); 512 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 513 } 514 515 int 516 smsc_sethwcsum(struct smsc_softc *sc) 517 { 518 struct ifnet * const ifp = &sc->sc_ec.ec_if; 519 uint32_t val; 520 int err; 521 522 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val); 523 if (err != 0) { 524 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", 525 err); 526 return err; 527 } 528 529 /* Enable/disable the Rx checksum */ 530 if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx)) 531 val |= (SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE); 532 else 533 val &= ~(SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE); 534 535 /* Enable/disable the Tx checksum (currently not supported) */ 536 if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx)) 537 val |= SMSC_COE_CTRL_TX_EN; 538 else 539 val &= ~SMSC_COE_CTRL_TX_EN; 540 541 sc->sc_coe_ctrl = val; 542 543 err = smsc_write_reg(sc, SMSC_COE_CTRL, val); 544 if (err != 0) { 545 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", 546 err); 547 return err; 548 } 549 550 return 0; 551 } 552 553 int 554 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr) 555 { 556 int err; 557 uint32_t val; 558 559 smsc_dbg_printf(sc, "setting mac address to " 560 "%02x:%02x:%02x:%02x:%02x:%02x\n", 561 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 562 563 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0]; 564 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0) 565 goto done; 566 567 val = (addr[5] << 8) | addr[4]; 568 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val); 569 570 done: 571 return err; 572 } 573 574 void 575 smsc_reset(struct smsc_softc *sc) 576 { 577 KASSERT(mutex_owned(&sc->sc_lock)); 578 if (sc->sc_dying) 579 return; 580 581 /* Wait a little while for the chip to get its brains in order. */ 582 DELAY(1000); 583 584 /* Reinitialize controller to achieve full reset. */ 585 smsc_chip_init(sc); 586 } 587 588 int 589 smsc_init(struct ifnet *ifp) 590 { 591 struct smsc_softc * const sc = ifp->if_softc; 592 593 mutex_enter(&sc->sc_lock); 594 int ret = smsc_init_locked(ifp); 595 mutex_exit(&sc->sc_lock); 596 597 return ret; 598 } 599 600 int 601 smsc_init_locked(struct ifnet *ifp) 602 { 603 struct smsc_softc * const sc = ifp->if_softc; 604 usbd_status err; 605 606 if (sc->sc_dying) 607 return EIO; 608 609 /* Cancel pending I/O */ 610 smsc_stop_locked(ifp, 1); 611 612 /* Reset the ethernet interface. */ 613 smsc_reset(sc); 614 615 /* Load the multicast filter. */ 616 smsc_setmulti(sc); 617 618 /* TCP/UDP checksum offload engines. */ 619 smsc_sethwcsum(sc); 620 621 /* Open RX and TX pipes. */ 622 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX], 623 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_ep[SMSC_ENDPT_RX]); 624 if (err) { 625 printf("%s: open rx pipe failed: %s\n", 626 device_xname(sc->sc_dev), usbd_errstr(err)); 627 goto fail; 628 } 629 630 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX], 631 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_ep[SMSC_ENDPT_TX]); 632 if (err) { 633 printf("%s: open tx pipe failed: %s\n", 634 device_xname(sc->sc_dev), usbd_errstr(err)); 635 goto fail1; 636 } 637 638 /* Init RX ring. */ 639 if (smsc_rx_list_init(sc)) { 640 aprint_error_dev(sc->sc_dev, "rx list init failed\n"); 641 goto fail2; 642 } 643 644 /* Init TX ring. */ 645 if (smsc_tx_list_init(sc)) { 646 aprint_error_dev(sc->sc_dev, "tx list init failed\n"); 647 goto fail3; 648 } 649 650 mutex_enter(&sc->sc_rxlock); 651 mutex_enter(&sc->sc_txlock); 652 sc->sc_stopping = false; 653 654 /* Start up the receive pipe. */ 655 for (size_t i = 0; i < SMSC_RX_LIST_CNT; i++) { 656 struct smsc_chain * const c = &sc->sc_cdata.rx_chain[i]; 657 usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, sc->sc_bufsz, 658 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, smsc_rxeof); 659 usbd_transfer(c->sc_xfer); 660 } 661 662 mutex_exit(&sc->sc_txlock); 663 mutex_exit(&sc->sc_rxlock); 664 665 /* Indicate we are up and running. */ 666 ifp->if_flags |= IFF_RUNNING; 667 ifp->if_flags &= ~IFF_OACTIVE; 668 669 callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc); 670 671 return 0; 672 673 fail3: 674 smsc_rx_list_free(sc); 675 fail2: 676 usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 677 fail1: 678 usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 679 fail: 680 return EIO; 681 } 682 683 void 684 smsc_start(struct ifnet *ifp) 685 { 686 struct smsc_softc * const sc = ifp->if_softc; 687 KASSERT(ifp->if_extflags & IFEF_MPSAFE); 688 689 mutex_enter(&sc->sc_txlock); 690 if (!sc->sc_stopping) 691 smsc_start_locked(ifp); 692 mutex_exit(&sc->sc_txlock); 693 } 694 695 void 696 smsc_start_locked(struct ifnet *ifp) 697 { 698 struct smsc_softc * const sc = ifp->if_softc; 699 struct mbuf *m_head = NULL; 700 701 KASSERT(mutex_owned(&sc->sc_txlock)); 702 703 /* Don't send anything if there is no link or controller is busy. */ 704 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) { 705 smsc_dbg_printf(sc, "%s: no link\n", __func__); 706 return; 707 } 708 709 /* Any free USB transfers? */ 710 if (sc->sc_cdata.tx_free == 0) { 711 smsc_dbg_printf(sc, "%s: all USB transfers in use\n", __func__); 712 return; 713 } 714 715 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING) { 716 smsc_dbg_printf(sc, "%s: not running\n", __func__); 717 return; 718 } 719 720 IFQ_POLL(&ifp->if_snd, m_head); 721 if (m_head == NULL) 722 return; 723 724 sc->sc_cdata.tx_free--; 725 726 IFQ_DEQUEUE(&ifp->if_snd, m_head); 727 if (smsc_encap(sc, m_head, sc->sc_cdata.tx_next)) { 728 m_free(m_head); 729 sc->sc_cdata.tx_free++; 730 return; 731 } 732 733 sc->sc_cdata.tx_next = (sc->sc_cdata.tx_next + 1) % SMSC_TX_LIST_CNT; 734 735 bpf_mtap(ifp, m_head, BPF_D_OUT); 736 737 if (sc->sc_cdata.tx_free == 0) 738 ifp->if_flags |= IFF_OACTIVE; 739 740 /* 741 * Set a timeout in case the chip goes out to lunch. 742 */ 743 ifp->if_timer = 5; 744 } 745 746 void 747 smsc_tick(void *xsc) 748 { 749 struct smsc_softc * const sc = xsc; 750 751 if (sc == NULL) 752 return; 753 754 mutex_enter(&sc->sc_lock); 755 756 if (sc->sc_dying) { 757 mutex_exit(&sc->sc_lock); 758 return; 759 } 760 761 if (!sc->sc_ttpending) { 762 sc->sc_ttpending = true; 763 usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER); 764 } 765 766 mutex_exit(&sc->sc_lock); 767 } 768 769 void 770 smsc_stop(struct ifnet *ifp, int disable) 771 { 772 struct smsc_softc * const sc = ifp->if_softc; 773 774 mutex_enter(&sc->sc_lock); 775 smsc_stop_locked(ifp, disable); 776 mutex_exit(&sc->sc_lock); 777 } 778 779 void 780 smsc_stop_locked(struct ifnet *ifp, int disable) 781 { 782 struct smsc_softc * const sc = ifp->if_softc; 783 usbd_status err; 784 785 KASSERT(mutex_owned(&sc->sc_lock)); 786 mutex_enter(&sc->sc_rxlock); 787 mutex_enter(&sc->sc_txlock); 788 sc->sc_stopping = true; 789 mutex_exit(&sc->sc_txlock); 790 mutex_exit(&sc->sc_rxlock); 791 792 callout_stop(&sc->sc_stat_ch); 793 794 /* Stop transfers. */ 795 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) { 796 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 797 if (err) { 798 printf("%s: abort rx pipe failed: %s\n", 799 device_xname(sc->sc_dev), usbd_errstr(err)); 800 } 801 } 802 803 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) { 804 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 805 if (err) { 806 printf("%s: abort tx pipe failed: %s\n", 807 device_xname(sc->sc_dev), usbd_errstr(err)); 808 } 809 } 810 811 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) { 812 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 813 if (err) { 814 printf("%s: abort intr pipe failed: %s\n", 815 device_xname(sc->sc_dev), usbd_errstr(err)); 816 } 817 } 818 819 smsc_rx_list_free(sc); 820 821 smsc_tx_list_free(sc); 822 823 /* Close pipes */ 824 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) { 825 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 826 if (err) { 827 printf("%s: close rx pipe failed: %s\n", 828 device_xname(sc->sc_dev), usbd_errstr(err)); 829 } 830 sc->sc_ep[SMSC_ENDPT_RX] = NULL; 831 } 832 833 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) { 834 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 835 if (err) { 836 printf("%s: close tx pipe failed: %s\n", 837 device_xname(sc->sc_dev), usbd_errstr(err)); 838 } 839 sc->sc_ep[SMSC_ENDPT_TX] = NULL; 840 } 841 842 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) { 843 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 844 if (err) { 845 printf("%s: close intr pipe failed: %s\n", 846 device_xname(sc->sc_dev), usbd_errstr(err)); 847 } 848 sc->sc_ep[SMSC_ENDPT_INTR] = NULL; 849 } 850 851 ifp->if_timer = 0; 852 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 853 854 if (disable) { 855 /* drain */ 856 } 857 } 858 859 int 860 smsc_chip_init(struct smsc_softc *sc) 861 { 862 int err; 863 uint32_t reg_val; 864 int burst_cap; 865 866 /* Enter H/W config mode */ 867 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST); 868 869 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, 870 SMSC_HW_CFG_LRST)) != 0) { 871 smsc_warn_printf(sc, "timed-out waiting for reset to " 872 "complete\n"); 873 goto init_failed; 874 } 875 876 /* Reset the PHY */ 877 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST); 878 879 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, 880 SMSC_PM_CTRL_PHY_RST)) != 0) { 881 smsc_warn_printf(sc, "timed-out waiting for phy reset to " 882 "complete\n"); 883 goto init_failed; 884 } 885 usbd_delay_ms(sc->sc_udev, 40); 886 887 /* Set the mac address */ 888 struct ifnet * const ifp = &sc->sc_ec.ec_if; 889 const char *eaddr = CLLADDR(ifp->if_sadl); 890 if ((err = smsc_setmacaddress(sc, eaddr)) != 0) { 891 smsc_warn_printf(sc, "failed to set the MAC address\n"); 892 goto init_failed; 893 } 894 895 /* 896 * Don't know what the HW_CFG_BIR bit is, but following the reset 897 * sequence as used in the Linux driver. 898 */ 899 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) { 900 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err); 901 goto init_failed; 902 } 903 reg_val |= SMSC_HW_CFG_BIR; 904 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 905 906 /* 907 * There is a so called 'turbo mode' that the linux driver supports, it 908 * seems to allow you to jam multiple frames per Rx transaction. 909 * By default this driver supports that and therefore allows multiple 910 * frames per USB transfer. 911 * 912 * The xfer buffer size needs to reflect this as well, therefore based 913 * on the calculations in the Linux driver the RX bufsize is set to 914 * 18944, 915 * bufsz = (16 * 1024 + 5 * 512) 916 * 917 * Burst capability is the number of URBs that can be in a burst of 918 * data/ethernet frames. 919 */ 920 921 if (sc->sc_udev->ud_speed == USB_SPEED_HIGH) 922 burst_cap = 37; 923 else 924 burst_cap = 128; 925 926 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap); 927 928 /* Set the default bulk in delay (magic value from Linux driver) */ 929 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000); 930 931 /* 932 * Initialise the RX interface 933 */ 934 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) { 935 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", 936 err); 937 goto init_failed; 938 } 939 940 /* 941 * The following settings are used for 'turbo mode', a.k.a multiple 942 * frames per Rx transaction (again info taken form Linux driver). 943 */ 944 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE); 945 946 /* 947 * set Rx data offset to ETHER_ALIGN which will make the IP header 948 * align on a word boundary. 949 */ 950 reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT; 951 952 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 953 954 /* Clear the status register ? */ 955 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff); 956 957 /* Read and display the revision register */ 958 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) { 959 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err); 960 goto init_failed; 961 } 962 963 /* GPIO/LED setup */ 964 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED | 965 SMSC_LED_GPIO_CFG_FDX_LED; 966 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val); 967 968 /* 969 * Initialise the TX interface 970 */ 971 smsc_write_reg(sc, SMSC_FLOW, 0); 972 973 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT); 974 975 /* Read the current MAC configuration */ 976 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) { 977 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err); 978 goto init_failed; 979 } 980 981 /* disable pad stripping, collides with checksum offload */ 982 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR; 983 984 /* Vlan */ 985 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN); 986 987 /* 988 * Start TX 989 */ 990 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN; 991 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 992 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON); 993 994 /* 995 * Start RX 996 */ 997 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN; 998 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 999 1000 return 0; 1001 1002 init_failed: 1003 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err); 1004 return err; 1005 } 1006 1007 static int 1008 smsc_ifflags_cb(struct ethercom *ec) 1009 { 1010 struct ifnet *ifp = &ec->ec_if; 1011 struct smsc_softc *sc = ifp->if_softc; 1012 1013 mutex_enter(&sc->sc_lock); 1014 1015 const int change = ifp->if_flags ^ sc->sc_if_flags; 1016 if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) { 1017 mutex_exit(&sc->sc_lock); 1018 return ENETRESET; 1019 } 1020 1021 smsc_dbg_printf(sc, "%s: change %x\n", __func__, change); 1022 1023 if ((change & IFF_PROMISC) != 0) { 1024 if (ifp->if_flags & IFF_PROMISC) { 1025 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS; 1026 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 1027 } else if (!(ifp->if_flags & IFF_PROMISC)) { 1028 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS; 1029 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 1030 } 1031 smsc_setmulti(sc); 1032 } 1033 1034 mutex_exit(&sc->sc_lock); 1035 1036 return 0; 1037 } 1038 1039 1040 int 1041 smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1042 { 1043 struct smsc_softc * const sc = ifp->if_softc; 1044 1045 smsc_dbg_printf(sc, "%s: cmd %0lx data %p\n", __func__, cmd, data); 1046 1047 int error = ether_ioctl(ifp, cmd, data); 1048 1049 if (error == ENETRESET) { 1050 error = 0; 1051 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) { 1052 if (ifp->if_flags & IFF_RUNNING) { 1053 mutex_enter(&sc->sc_lock); 1054 smsc_setmulti(sc); 1055 mutex_exit(&sc->sc_lock); 1056 } 1057 } 1058 } 1059 1060 mutex_enter(&sc->sc_rxlock); 1061 mutex_enter(&sc->sc_txlock); 1062 sc->sc_if_flags = ifp->if_flags; 1063 mutex_exit(&sc->sc_txlock); 1064 mutex_exit(&sc->sc_rxlock); 1065 1066 return error; 1067 } 1068 1069 int 1070 smsc_match(device_t parent, cfdata_t match, void *aux) 1071 { 1072 struct usb_attach_arg *uaa = aux; 1073 1074 return (usb_lookup(smsc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ? 1075 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 1076 } 1077 1078 void 1079 smsc_attach(device_t parent, device_t self, void *aux) 1080 { 1081 struct smsc_softc *sc = device_private(self); 1082 struct usb_attach_arg *uaa = aux; 1083 struct usbd_device *dev = uaa->uaa_device; 1084 usb_interface_descriptor_t *id; 1085 usb_endpoint_descriptor_t *ed; 1086 char *devinfop; 1087 struct mii_data *mii; 1088 struct ifnet *ifp; 1089 int err, i; 1090 uint32_t mac_h, mac_l; 1091 1092 sc->sc_dev = self; 1093 sc->sc_udev = dev; 1094 sc->sc_dying = false; 1095 sc->sc_stopping = false; 1096 sc->sc_ttpending = false; 1097 1098 aprint_naive("\n"); 1099 aprint_normal("\n"); 1100 1101 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0); 1102 aprint_normal_dev(self, "%s\n", devinfop); 1103 usbd_devinfo_free(devinfop); 1104 1105 err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1); 1106 if (err) { 1107 aprint_error_dev(self, "failed to set configuration" 1108 ", err=%s\n", usbd_errstr(err)); 1109 return; 1110 } 1111 1112 /* Setup the endpoints for the SMSC LAN95xx device(s) */ 1113 err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface); 1114 if (err) { 1115 aprint_error_dev(self, "getting interface handle failed\n"); 1116 return; 1117 } 1118 1119 id = usbd_get_interface_descriptor(sc->sc_iface); 1120 1121 if (sc->sc_udev->ud_speed >= USB_SPEED_HIGH) 1122 sc->sc_bufsz = SMSC_MAX_BUFSZ; 1123 else 1124 sc->sc_bufsz = SMSC_MIN_BUFSZ; 1125 1126 /* Find endpoints. */ 1127 for (i = 0; i < id->bNumEndpoints; i++) { 1128 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 1129 if (!ed) { 1130 aprint_error_dev(self, "couldn't get ep %d\n", i); 1131 return; 1132 } 1133 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 1134 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 1135 sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress; 1136 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 1137 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 1138 sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress; 1139 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 1140 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 1141 sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress; 1142 } 1143 } 1144 1145 usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, USB_TASKQ_MPSAFE); 1146 1147 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 1148 mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); 1149 mutex_init(&sc->sc_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); 1150 mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE); 1151 cv_init(&sc->sc_detachcv, "smsc_det"); 1152 1153 ifp = &sc->sc_ec.ec_if; 1154 ifp->if_softc = sc; 1155 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ); 1156 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1157 ifp->if_extflags = IFEF_MPSAFE; 1158 ifp->if_init = smsc_init; 1159 ifp->if_ioctl = smsc_ioctl; 1160 ifp->if_start = smsc_start; 1161 ifp->if_stop = smsc_stop; 1162 1163 #ifdef notyet 1164 /* 1165 * We can do TCPv4, and UDPv4 checksums in hardware. 1166 */ 1167 ifp->if_capabilities |= 1168 /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx | 1169 /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx; 1170 #endif 1171 1172 sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU; 1173 1174 /* Setup some of the basics */ 1175 sc->sc_phyno = 1; 1176 1177 /* 1178 * Attempt to get the mac address, if an EEPROM is not attached this 1179 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC 1180 * address based on urandom. 1181 */ 1182 memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN); 1183 1184 prop_dictionary_t dict = device_properties(self); 1185 prop_data_t eaprop = prop_dictionary_get(dict, "mac-address"); 1186 1187 if (eaprop != NULL) { 1188 KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA); 1189 KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN); 1190 memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop), 1191 ETHER_ADDR_LEN); 1192 } else { 1193 /* Check if there is already a MAC address in the register */ 1194 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) && 1195 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) { 1196 sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); 1197 sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff); 1198 sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); 1199 sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); 1200 sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); 1201 sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff); 1202 } 1203 } 1204 1205 aprint_normal_dev(self, "Ethernet address %s\n", 1206 ether_sprintf(sc->sc_enaddr)); 1207 1208 IFQ_SET_READY(&ifp->if_snd); 1209 1210 /* Initialize MII/media info. */ 1211 mii = &sc->sc_mii; 1212 mii->mii_ifp = ifp; 1213 mii->mii_readreg = smsc_miibus_readreg; 1214 mii->mii_writereg = smsc_miibus_writereg; 1215 mii->mii_statchg = smsc_miibus_statchg; 1216 mii->mii_flags = MIIF_AUTOTSLEEP; 1217 sc->sc_ec.ec_mii = mii; 1218 ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts); 1219 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 1220 1221 if (LIST_FIRST(&mii->mii_phys) == NULL) { 1222 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 1223 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 1224 } else 1225 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 1226 1227 callout_init(&sc->sc_stat_ch, CALLOUT_MPSAFE); 1228 1229 if_initialize(ifp); 1230 sc->sc_ipq = if_percpuq_create(&sc->sc_ec.ec_if); 1231 ether_ifattach(ifp, sc->sc_enaddr); 1232 ether_set_ifflags_cb(&sc->sc_ec, smsc_ifflags_cb); 1233 if_register(ifp); 1234 1235 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev), 1236 RND_TYPE_NET, RND_FLAG_DEFAULT); 1237 1238 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 1239 } 1240 1241 int 1242 smsc_detach(device_t self, int flags) 1243 { 1244 struct smsc_softc *sc = device_private(self); 1245 struct ifnet *ifp = &sc->sc_ec.ec_if; 1246 1247 mutex_enter(&sc->sc_lock); 1248 sc->sc_dying = true; 1249 mutex_exit(&sc->sc_lock); 1250 1251 callout_halt(&sc->sc_stat_ch, NULL); 1252 1253 if (ifp->if_flags & IFF_RUNNING) 1254 smsc_stop_locked(ifp, 1); 1255 1256 /* 1257 * Remove any pending tasks. They cannot be executing because they run 1258 * in the same thread as detach. 1259 */ 1260 usb_rem_task_wait(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER, 1261 NULL); 1262 1263 mutex_enter(&sc->sc_lock); 1264 sc->sc_refcnt--; 1265 while (sc->sc_refcnt > 0) { 1266 /* Wait for processes to go away */ 1267 cv_wait(&sc->sc_detachcv, &sc->sc_lock); 1268 } 1269 1270 #ifdef DIAGNOSTIC 1271 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL || 1272 sc->sc_ep[SMSC_ENDPT_RX] != NULL || 1273 sc->sc_ep[SMSC_ENDPT_INTR] != NULL) 1274 printf("%s: detach has active endpoints\n", 1275 device_xname(sc->sc_dev)); 1276 #endif 1277 1278 mutex_exit(&sc->sc_lock); 1279 1280 rnd_detach_source(&sc->sc_rnd_source); 1281 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 1282 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 1283 if (ifp->if_softc != NULL) { 1284 ether_ifdetach(ifp); 1285 if_detach(ifp); 1286 } 1287 1288 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 1289 1290 cv_destroy(&sc->sc_detachcv); 1291 mutex_destroy(&sc->sc_mii_lock); 1292 mutex_destroy(&sc->sc_rxlock); 1293 mutex_destroy(&sc->sc_txlock); 1294 mutex_destroy(&sc->sc_lock); 1295 1296 return 0; 1297 } 1298 1299 void 1300 smsc_tick_task(void *xsc) 1301 { 1302 struct smsc_softc * const sc = xsc; 1303 1304 if (sc == NULL) 1305 return; 1306 1307 mutex_enter(&sc->sc_lock); 1308 1309 if (sc->sc_dying) { 1310 mutex_exit(&sc->sc_lock); 1311 return; 1312 } 1313 1314 struct ifnet * const ifp = &sc->sc_ec.ec_if; 1315 struct mii_data * const mii = &sc->sc_mii; 1316 1317 sc->sc_refcnt++; 1318 mutex_exit(&sc->sc_lock); 1319 1320 mii_tick(mii); 1321 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) 1322 smsc_miibus_statchg(ifp); 1323 1324 mutex_enter(&sc->sc_lock); 1325 sc->sc_ttpending = false; 1326 1327 if (--sc->sc_refcnt < 0) 1328 cv_broadcast(&sc->sc_detachcv); 1329 1330 if (sc->sc_dying) { 1331 mutex_exit(&sc->sc_lock); 1332 return; 1333 } 1334 callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc); 1335 1336 mutex_exit(&sc->sc_lock); 1337 } 1338 1339 int 1340 smsc_activate(device_t self, enum devact act) 1341 { 1342 struct smsc_softc *sc = device_private(self); 1343 1344 switch (act) { 1345 case DVACT_DEACTIVATE: 1346 if_deactivate(&sc->sc_ec.ec_if); 1347 1348 mutex_enter(&sc->sc_lock); 1349 sc->sc_dying = true; 1350 1351 mutex_enter(&sc->sc_rxlock); 1352 mutex_enter(&sc->sc_txlock); 1353 sc->sc_stopping = true; 1354 mutex_exit(&sc->sc_txlock); 1355 mutex_exit(&sc->sc_rxlock); 1356 1357 mutex_exit(&sc->sc_lock); 1358 return 0; 1359 default: 1360 return EOPNOTSUPP; 1361 } 1362 return 0; 1363 } 1364 1365 void 1366 smsc_lock_mii(struct smsc_softc *sc) 1367 { 1368 1369 mutex_enter(&sc->sc_lock); 1370 sc->sc_refcnt++; 1371 mutex_exit(&sc->sc_lock); 1372 1373 mutex_enter(&sc->sc_mii_lock); 1374 } 1375 1376 void 1377 smsc_unlock_mii(struct smsc_softc *sc) 1378 { 1379 1380 mutex_exit(&sc->sc_mii_lock); 1381 mutex_enter(&sc->sc_lock); 1382 if (--sc->sc_refcnt < 0) 1383 cv_broadcast(&sc->sc_detachcv); 1384 mutex_exit(&sc->sc_lock); 1385 } 1386 1387 void 1388 smsc_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1389 { 1390 struct smsc_chain * const c = (struct smsc_chain *)priv; 1391 struct smsc_softc * const sc = c->sc_sc; 1392 struct ifnet * const ifp = &sc->sc_ec.ec_if; 1393 u_char *buf = c->sc_buf; 1394 uint32_t total_len; 1395 1396 mutex_enter(&sc->sc_rxlock); 1397 if (sc->sc_stopping) { 1398 smsc_dbg_printf(sc, "%s: stopping\n", __func__); 1399 mutex_exit(&sc->sc_rxlock); 1400 return; 1401 } 1402 1403 if (!(sc->sc_if_flags & IFF_RUNNING)) { 1404 smsc_dbg_printf(sc, "%s: not running\n", __func__); 1405 mutex_exit(&sc->sc_rxlock); 1406 return; 1407 } 1408 1409 if (status != USBD_NORMAL_COMPLETION) { 1410 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1411 mutex_exit(&sc->sc_rxlock); 1412 return; 1413 } 1414 if (usbd_ratecheck(&sc->sc_rx_notice)) { 1415 printf("%s: usb errors on rx: %s\n", 1416 device_xname(sc->sc_dev), usbd_errstr(status)); 1417 } 1418 if (status == USBD_STALLED) 1419 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]); 1420 goto done; 1421 } 1422 1423 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 1424 smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len); 1425 1426 while (total_len != 0) { 1427 uint32_t rxhdr; 1428 if (total_len < sizeof(rxhdr)) { 1429 smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n", 1430 total_len, sizeof(rxhdr)); 1431 ifp->if_ierrors++; 1432 goto done; 1433 } 1434 1435 memcpy(&rxhdr, buf, sizeof(rxhdr)); 1436 rxhdr = le32toh(rxhdr); 1437 buf += sizeof(rxhdr); 1438 total_len -= sizeof(rxhdr); 1439 1440 if (rxhdr & SMSC_RX_STAT_COLLISION) 1441 ifp->if_collisions++; 1442 1443 if (rxhdr & (SMSC_RX_STAT_ERROR 1444 | SMSC_RX_STAT_LENGTH_ERROR 1445 | SMSC_RX_STAT_MII_ERROR)) { 1446 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr); 1447 ifp->if_ierrors++; 1448 goto done; 1449 } 1450 1451 uint16_t pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr); 1452 smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr " 1453 "0x%08x\n", total_len, pktlen, rxhdr); 1454 1455 if (pktlen < ETHER_HDR_LEN) { 1456 smsc_dbg_printf(sc, "pktlen %d < ETHER_HDR_LEN %d\n", 1457 pktlen, ETHER_HDR_LEN); 1458 ifp->if_ierrors++; 1459 goto done; 1460 } 1461 1462 pktlen += ETHER_ALIGN; 1463 1464 if (pktlen > MCLBYTES) { 1465 smsc_dbg_printf(sc, "pktlen %d > MCLBYTES %d\n", 1466 pktlen, MCLBYTES); 1467 ifp->if_ierrors++; 1468 goto done; 1469 } 1470 1471 if (pktlen > total_len) { 1472 smsc_dbg_printf(sc, "pktlen %d > total_len %d\n", 1473 pktlen, total_len); 1474 ifp->if_ierrors++; 1475 goto done; 1476 } 1477 1478 struct mbuf *m = smsc_newbuf(); 1479 if (m == NULL) { 1480 smsc_dbg_printf(sc, "smc_newbuf returned NULL\n"); 1481 ifp->if_ierrors++; 1482 goto done; 1483 } 1484 1485 m_set_rcvif(m, ifp); 1486 m->m_pkthdr.len = m->m_len = pktlen; 1487 m->m_flags |= M_HASFCS; 1488 m_adj(m, ETHER_ALIGN); 1489 1490 KASSERT(m->m_len < MCLBYTES); 1491 memcpy(mtod(m, char *), buf + ETHER_ALIGN, m->m_len); 1492 1493 /* Check if RX TCP/UDP checksumming is being offloaded */ 1494 if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) { 1495 smsc_dbg_printf(sc,"RX checksum offload checking\n"); 1496 struct ether_header *eh; 1497 1498 eh = mtod(m, struct ether_header *); 1499 1500 /* Remove the extra 2 bytes of the csum */ 1501 m_adj(m, -2); 1502 1503 /* 1504 * The checksum appears to be simplistically calculated 1505 * over the udp/tcp header and data up to the end of the 1506 * eth frame. Which means if the eth frame is padded 1507 * the csum calculation is incorrectly performed over 1508 * the padding bytes as well. Therefore to be safe we 1509 * ignore the H/W csum on frames less than or equal to 1510 * 64 bytes. 1511 * 1512 * Ignore H/W csum for non-IPv4 packets. 1513 */ 1514 smsc_dbg_printf(sc,"Ethertype %02x pktlen %02x\n", 1515 be16toh(eh->ether_type), pktlen); 1516 if (be16toh(eh->ether_type) == ETHERTYPE_IP && 1517 pktlen > ETHER_MIN_LEN) { 1518 1519 m->m_pkthdr.csum_flags |= 1520 (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA); 1521 1522 /* 1523 * Copy the TCP/UDP checksum from the last 2 1524 * bytes of the transfer and put in the 1525 * csum_data field. 1526 */ 1527 memcpy(&m->m_pkthdr.csum_data, 1528 buf + pktlen - 2, 2); 1529 /* 1530 * The data is copied in network order, but the 1531 * csum algorithm in the kernel expects it to be 1532 * in host network order. 1533 */ 1534 m->m_pkthdr.csum_data = 1535 ntohs(m->m_pkthdr.csum_data); 1536 smsc_dbg_printf(sc, 1537 "RX checksum offloaded (0x%04x)\n", 1538 m->m_pkthdr.csum_data); 1539 } 1540 } 1541 1542 /* round up to next longword */ 1543 pktlen = (pktlen + 3) & ~0x3; 1544 1545 /* total_len does not include the padding */ 1546 if (pktlen > total_len) 1547 pktlen = total_len; 1548 1549 buf += pktlen; 1550 total_len -= pktlen; 1551 1552 mutex_exit(&sc->sc_rxlock); 1553 1554 /* push the packet up */ 1555 if_percpuq_enqueue(sc->sc_ipq, m); 1556 1557 mutex_enter(&sc->sc_rxlock); 1558 if (sc->sc_stopping) { 1559 smsc_dbg_printf(sc, "%s: stopping\n", __func__); 1560 mutex_exit(&sc->sc_rxlock); 1561 return; 1562 } 1563 } 1564 1565 done: 1566 mutex_exit(&sc->sc_rxlock); 1567 1568 /* Setup new transfer. */ 1569 usbd_setup_xfer(xfer, c, c->sc_buf, sc->sc_bufsz, USBD_SHORT_XFER_OK, 1570 USBD_NO_TIMEOUT, smsc_rxeof); 1571 usbd_transfer(xfer); 1572 1573 return; 1574 } 1575 1576 void 1577 smsc_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1578 { 1579 struct smsc_chain *c = priv; 1580 struct smsc_softc *sc = c->sc_sc; 1581 struct ifnet *ifp = &sc->sc_ec.ec_if; 1582 1583 mutex_enter(&sc->sc_txlock); 1584 if (sc->sc_stopping) { 1585 smsc_dbg_printf(sc, "%s: stopping\n", __func__); 1586 mutex_exit(&sc->sc_txlock); 1587 return; 1588 } 1589 1590 sc->sc_cdata.tx_free++; 1591 ifp->if_timer = 0; 1592 ifp->if_flags &= ~IFF_OACTIVE; 1593 1594 if (status != USBD_NORMAL_COMPLETION) { 1595 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1596 mutex_exit(&sc->sc_txlock); 1597 return; 1598 } 1599 ifp->if_oerrors++; 1600 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev), 1601 usbd_errstr(status)); 1602 if (status == USBD_STALLED) 1603 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]); 1604 mutex_exit(&sc->sc_txlock); 1605 return; 1606 } 1607 ifp->if_opackets++; 1608 1609 m_freem(c->sc_mbuf); 1610 c->sc_mbuf = NULL; 1611 1612 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1613 smsc_start_locked(ifp); 1614 1615 mutex_exit(&sc->sc_txlock); 1616 } 1617 1618 int 1619 smsc_tx_list_init(struct smsc_softc *sc) 1620 { 1621 struct smsc_cdata *cd = &sc->sc_cdata; 1622 struct smsc_chain *c; 1623 int i; 1624 1625 for (i = 0; i < SMSC_TX_LIST_CNT; i++) { 1626 c = &cd->tx_chain[i]; 1627 c->sc_sc = sc; 1628 c->sc_idx = i; 1629 c->sc_mbuf = NULL; 1630 if (c->sc_xfer == NULL) { 1631 int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_TX], 1632 sc->sc_bufsz, USBD_FORCE_SHORT_XFER, 0, 1633 &c->sc_xfer); 1634 if (error) 1635 return EIO; 1636 c->sc_buf = usbd_get_buffer(c->sc_xfer); 1637 } 1638 } 1639 1640 cd->tx_free = SMSC_TX_LIST_CNT; 1641 cd->tx_next = 0; 1642 1643 return 0; 1644 } 1645 1646 void 1647 smsc_tx_list_free(struct smsc_softc *sc) 1648 { 1649 /* Free TX resources. */ 1650 for (size_t i = 0; i < SMSC_TX_LIST_CNT; i++) { 1651 if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) { 1652 m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf); 1653 sc->sc_cdata.tx_chain[i].sc_mbuf = NULL; 1654 } 1655 if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) { 1656 usbd_destroy_xfer(sc->sc_cdata.tx_chain[i].sc_xfer); 1657 sc->sc_cdata.tx_chain[i].sc_xfer = NULL; 1658 } 1659 } 1660 } 1661 1662 int 1663 smsc_rx_list_init(struct smsc_softc *sc) 1664 { 1665 struct smsc_cdata *cd = &sc->sc_cdata; 1666 struct smsc_chain *c; 1667 int i; 1668 1669 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 1670 c = &cd->rx_chain[i]; 1671 c->sc_sc = sc; 1672 c->sc_idx = i; 1673 c->sc_mbuf = NULL; 1674 if (c->sc_xfer == NULL) { 1675 int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_RX], 1676 sc->sc_bufsz, USBD_SHORT_XFER_OK, 0, &c->sc_xfer); 1677 if (error) 1678 return error; 1679 c->sc_buf = usbd_get_buffer(c->sc_xfer); 1680 } 1681 } 1682 1683 return 0; 1684 } 1685 1686 void 1687 smsc_rx_list_free(struct smsc_softc *sc) 1688 { 1689 /* Free RX resources. */ 1690 for (size_t i = 0; i < SMSC_RX_LIST_CNT; i++) { 1691 if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) { 1692 m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf); 1693 sc->sc_cdata.rx_chain[i].sc_mbuf = NULL; 1694 } 1695 if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) { 1696 usbd_destroy_xfer(sc->sc_cdata.rx_chain[i].sc_xfer); 1697 sc->sc_cdata.rx_chain[i].sc_xfer = NULL; 1698 } 1699 } 1700 } 1701 1702 struct mbuf * 1703 smsc_newbuf(void) 1704 { 1705 struct mbuf *m; 1706 1707 MGETHDR(m, M_DONTWAIT, MT_DATA); 1708 if (m == NULL) 1709 return NULL; 1710 1711 MCLGET(m, M_DONTWAIT); 1712 if (!(m->m_flags & M_EXT)) { 1713 m_freem(m); 1714 return NULL; 1715 } 1716 1717 return m; 1718 } 1719 1720 int 1721 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx) 1722 { 1723 struct smsc_chain * const c = &sc->sc_cdata.tx_chain[idx]; 1724 uint32_t txhdr; 1725 uint32_t frm_len = 0; 1726 1727 /* 1728 * Each frame is prefixed with two 32-bit values describing the 1729 * length of the packet and buffer. 1730 */ 1731 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) | 1732 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG; 1733 txhdr = htole32(txhdr); 1734 memcpy(c->sc_buf, &txhdr, sizeof(txhdr)); 1735 1736 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len); 1737 txhdr = htole32(txhdr); 1738 memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr)); 1739 1740 frm_len += 8; 1741 1742 /* Next copy in the actual packet */ 1743 m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len); 1744 frm_len += m->m_pkthdr.len; 1745 1746 c->sc_mbuf = m; 1747 1748 usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, frm_len, 1749 USBD_FORCE_SHORT_XFER, 10000, smsc_txeof); 1750 1751 usbd_status err = usbd_transfer(c->sc_xfer); 1752 if (err != USBD_IN_PROGRESS) { 1753 return EIO; 1754 } 1755 1756 return 0; 1757 } 1758