1 /* $NetBSD: if_smsc.c,v 1.29 2016/06/10 13:27:15 ozaki-r 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/rndsource.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(struct usbd_xfer *, void *, usbd_status); 185 void smsc_txeof(struct usbd_xfer *, void *, 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 /* Load the multicast filter. */ 564 smsc_setmulti(sc); 565 566 /* TCP/UDP checksum offload engines. */ 567 smsc_sethwcsum(sc); 568 569 /* Open RX and TX pipes. */ 570 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX], 571 USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_RX]); 572 if (err) { 573 printf("%s: open rx pipe failed: %s\n", 574 device_xname(sc->sc_dev), usbd_errstr(err)); 575 splx(s); 576 return EIO; 577 } 578 579 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX], 580 USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_TX]); 581 if (err) { 582 printf("%s: open tx pipe failed: %s\n", 583 device_xname(sc->sc_dev), usbd_errstr(err)); 584 splx(s); 585 return EIO; 586 } 587 588 /* Init RX ring. */ 589 if (smsc_rx_list_init(sc)) { 590 aprint_error_dev(sc->sc_dev, "rx list init failed\n"); 591 splx(s); 592 return EIO; 593 } 594 595 /* Init TX ring. */ 596 if (smsc_tx_list_init(sc)) { 597 aprint_error_dev(sc->sc_dev, "tx list init failed\n"); 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, c, c->sc_buf, sc->sc_bufsz, 606 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, smsc_rxeof); 607 usbd_transfer(c->sc_xfer); 608 } 609 610 /* Indicate we are up and running. */ 611 ifp->if_flags |= IFF_RUNNING; 612 ifp->if_flags &= ~IFF_OACTIVE; 613 614 splx(s); 615 616 callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc); 617 618 return 0; 619 } 620 621 void 622 smsc_start(struct ifnet *ifp) 623 { 624 struct smsc_softc *sc = ifp->if_softc; 625 struct mbuf *m_head = NULL; 626 627 /* Don't send anything if there is no link or controller is busy. */ 628 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) { 629 return; 630 } 631 632 if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING) 633 return; 634 635 IFQ_POLL(&ifp->if_snd, m_head); 636 if (m_head == NULL) 637 return; 638 639 if (smsc_encap(sc, m_head, 0)) { 640 ifp->if_flags |= IFF_OACTIVE; 641 return; 642 } 643 IFQ_DEQUEUE(&ifp->if_snd, m_head); 644 645 bpf_mtap(ifp, m_head); 646 647 ifp->if_flags |= IFF_OACTIVE; 648 649 /* 650 * Set a timeout in case the chip goes out to lunch. 651 */ 652 ifp->if_timer = 5; 653 } 654 655 void 656 smsc_tick(void *xsc) 657 { 658 struct smsc_softc *sc = xsc; 659 660 if (sc == NULL) 661 return; 662 663 if (sc->sc_dying) 664 return; 665 666 usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER); 667 } 668 669 void 670 smsc_stop(struct ifnet *ifp, int disable) 671 { 672 usbd_status err; 673 struct smsc_softc *sc = ifp->if_softc; 674 int i; 675 676 smsc_reset(sc); 677 678 ifp = &sc->sc_ec.ec_if; 679 ifp->if_timer = 0; 680 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 681 682 callout_stop(&sc->sc_stat_ch); 683 684 /* Stop transfers. */ 685 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) { 686 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 687 if (err) { 688 printf("%s: abort rx pipe failed: %s\n", 689 device_xname(sc->sc_dev), usbd_errstr(err)); 690 } 691 } 692 693 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) { 694 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 695 if (err) { 696 printf("%s: abort tx pipe failed: %s\n", 697 device_xname(sc->sc_dev), usbd_errstr(err)); 698 } 699 } 700 701 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) { 702 err = usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 703 if (err) { 704 printf("%s: abort intr pipe failed: %s\n", 705 device_xname(sc->sc_dev), usbd_errstr(err)); 706 } 707 } 708 709 /* Free RX resources. */ 710 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 711 if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) { 712 m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf); 713 sc->sc_cdata.rx_chain[i].sc_mbuf = NULL; 714 } 715 if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) { 716 usbd_destroy_xfer(sc->sc_cdata.rx_chain[i].sc_xfer); 717 sc->sc_cdata.rx_chain[i].sc_xfer = NULL; 718 } 719 } 720 721 /* Free TX resources. */ 722 for (i = 0; i < SMSC_TX_LIST_CNT; i++) { 723 if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) { 724 m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf); 725 sc->sc_cdata.tx_chain[i].sc_mbuf = NULL; 726 } 727 if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) { 728 usbd_destroy_xfer(sc->sc_cdata.tx_chain[i].sc_xfer); 729 sc->sc_cdata.tx_chain[i].sc_xfer = NULL; 730 } 731 } 732 /* Close pipes */ 733 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) { 734 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 735 if (err) { 736 printf("%s: close rx pipe failed: %s\n", 737 device_xname(sc->sc_dev), usbd_errstr(err)); 738 } 739 sc->sc_ep[SMSC_ENDPT_RX] = NULL; 740 } 741 742 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) { 743 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 744 if (err) { 745 printf("%s: close tx pipe failed: %s\n", 746 device_xname(sc->sc_dev), usbd_errstr(err)); 747 } 748 sc->sc_ep[SMSC_ENDPT_TX] = NULL; 749 } 750 751 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) { 752 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 753 if (err) { 754 printf("%s: close intr pipe failed: %s\n", 755 device_xname(sc->sc_dev), usbd_errstr(err)); 756 } 757 sc->sc_ep[SMSC_ENDPT_INTR] = NULL; 758 } 759 } 760 761 int 762 smsc_chip_init(struct smsc_softc *sc) 763 { 764 int err; 765 uint32_t reg_val; 766 int burst_cap; 767 768 /* Enter H/W config mode */ 769 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST); 770 771 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, 772 SMSC_HW_CFG_LRST)) != 0) { 773 smsc_warn_printf(sc, "timed-out waiting for reset to " 774 "complete\n"); 775 goto init_failed; 776 } 777 778 /* Reset the PHY */ 779 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST); 780 781 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, 782 SMSC_PM_CTRL_PHY_RST)) != 0) { 783 smsc_warn_printf(sc, "timed-out waiting for phy reset to " 784 "complete\n"); 785 goto init_failed; 786 } 787 usbd_delay_ms(sc->sc_udev, 40); 788 789 /* Set the mac address */ 790 struct ifnet *ifp = &sc->sc_ec.ec_if; 791 const char *eaddr = CLLADDR(ifp->if_sadl); 792 if ((err = smsc_setmacaddress(sc, eaddr)) != 0) { 793 smsc_warn_printf(sc, "failed to set the MAC address\n"); 794 goto init_failed; 795 } 796 797 /* 798 * Don't know what the HW_CFG_BIR bit is, but following the reset 799 * sequence as used in the Linux driver. 800 */ 801 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) { 802 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err); 803 goto init_failed; 804 } 805 reg_val |= SMSC_HW_CFG_BIR; 806 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 807 808 /* 809 * There is a so called 'turbo mode' that the linux driver supports, it 810 * seems to allow you to jam multiple frames per Rx transaction. 811 * By default this driver supports that and therefore allows multiple 812 * frames per USB transfer. 813 * 814 * The xfer buffer size needs to reflect this as well, therefore based 815 * on the calculations in the Linux driver the RX bufsize is set to 816 * 18944, 817 * bufsz = (16 * 1024 + 5 * 512) 818 * 819 * Burst capability is the number of URBs that can be in a burst of 820 * data/ethernet frames. 821 */ 822 823 if (sc->sc_udev->ud_speed == USB_SPEED_HIGH) 824 burst_cap = 37; 825 else 826 burst_cap = 128; 827 828 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap); 829 830 /* Set the default bulk in delay (magic value from Linux driver) */ 831 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000); 832 833 /* 834 * Initialise the RX interface 835 */ 836 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) { 837 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", 838 err); 839 goto init_failed; 840 } 841 842 /* 843 * The following settings are used for 'turbo mode', a.k.a multiple 844 * frames per Rx transaction (again info taken form Linux driver). 845 */ 846 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE); 847 848 /* 849 * set Rx data offset to ETHER_ALIGN which will make the IP header 850 * align on a word boundary. 851 */ 852 reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT; 853 854 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 855 856 /* Clear the status register ? */ 857 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff); 858 859 /* Read and display the revision register */ 860 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) { 861 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err); 862 goto init_failed; 863 } 864 865 /* GPIO/LED setup */ 866 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED | 867 SMSC_LED_GPIO_CFG_FDX_LED; 868 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val); 869 870 /* 871 * Initialise the TX interface 872 */ 873 smsc_write_reg(sc, SMSC_FLOW, 0); 874 875 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT); 876 877 /* Read the current MAC configuration */ 878 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) { 879 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err); 880 goto init_failed; 881 } 882 883 /* disable pad stripping, collides with checksum offload */ 884 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR; 885 886 /* Vlan */ 887 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN); 888 889 /* 890 * Start TX 891 */ 892 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN; 893 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 894 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON); 895 896 /* 897 * Start RX 898 */ 899 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN; 900 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 901 902 return 0; 903 904 init_failed: 905 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err); 906 return err; 907 } 908 909 int 910 smsc_ioctl(struct ifnet *ifp, u_long cmd, void *data) 911 { 912 struct smsc_softc *sc = ifp->if_softc; 913 struct ifreq /*const*/ *ifr = data; 914 int s, error = 0; 915 916 if (sc->sc_dying) 917 return EIO; 918 919 s = splnet(); 920 921 switch(cmd) { 922 case SIOCSIFFLAGS: 923 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 924 break; 925 926 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) { 927 case IFF_RUNNING: 928 smsc_stop(ifp, 1); 929 break; 930 case IFF_UP: 931 smsc_init(ifp); 932 break; 933 case IFF_UP | IFF_RUNNING: 934 if (ifp->if_flags & IFF_PROMISC && 935 !(sc->sc_if_flags & IFF_PROMISC)) { 936 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS; 937 smsc_write_reg(sc, SMSC_MAC_CSR, 938 sc->sc_mac_csr); 939 smsc_setmulti(sc); 940 } else if (!(ifp->if_flags & IFF_PROMISC) && 941 sc->sc_if_flags & IFF_PROMISC) { 942 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS; 943 smsc_write_reg(sc, SMSC_MAC_CSR, 944 sc->sc_mac_csr); 945 smsc_setmulti(sc); 946 } else { 947 smsc_init(ifp); 948 } 949 break; 950 } 951 sc->sc_if_flags = ifp->if_flags; 952 break; 953 954 case SIOCGIFMEDIA: 955 case SIOCSIFMEDIA: 956 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd); 957 break; 958 959 default: 960 if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET) 961 break; 962 963 error = 0; 964 965 if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) 966 smsc_setmulti(sc); 967 968 } 969 splx(s); 970 971 return error; 972 } 973 974 int 975 smsc_match(device_t parent, cfdata_t match, void *aux) 976 { 977 struct usb_attach_arg *uaa = aux; 978 979 return (usb_lookup(smsc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ? 980 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 981 } 982 983 void 984 smsc_attach(device_t parent, device_t self, void *aux) 985 { 986 struct smsc_softc *sc = device_private(self); 987 struct usb_attach_arg *uaa = aux; 988 struct usbd_device *dev = uaa->uaa_device; 989 usb_interface_descriptor_t *id; 990 usb_endpoint_descriptor_t *ed; 991 char *devinfop; 992 struct mii_data *mii; 993 struct ifnet *ifp; 994 int err, s, i; 995 uint32_t mac_h, mac_l; 996 997 sc->sc_dev = self; 998 sc->sc_udev = dev; 999 1000 aprint_naive("\n"); 1001 aprint_normal("\n"); 1002 1003 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0); 1004 aprint_normal_dev(self, "%s\n", devinfop); 1005 usbd_devinfo_free(devinfop); 1006 1007 err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1); 1008 if (err) { 1009 aprint_error_dev(self, "failed to set configuration" 1010 ", err=%s\n", usbd_errstr(err)); 1011 return; 1012 } 1013 /* Setup the endpoints for the SMSC LAN95xx device(s) */ 1014 usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, 0); 1015 usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc, 0); 1016 mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE); 1017 1018 err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface); 1019 if (err) { 1020 aprint_error_dev(self, "getting interface handle failed\n"); 1021 return; 1022 } 1023 1024 id = usbd_get_interface_descriptor(sc->sc_iface); 1025 1026 if (sc->sc_udev->ud_speed >= USB_SPEED_HIGH) 1027 sc->sc_bufsz = SMSC_MAX_BUFSZ; 1028 else 1029 sc->sc_bufsz = SMSC_MIN_BUFSZ; 1030 1031 /* Find endpoints. */ 1032 for (i = 0; i < id->bNumEndpoints; i++) { 1033 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 1034 if (!ed) { 1035 aprint_error_dev(self, "couldn't get ep %d\n", i); 1036 return; 1037 } 1038 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 1039 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 1040 sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress; 1041 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 1042 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 1043 sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress; 1044 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 1045 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 1046 sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress; 1047 } 1048 } 1049 1050 s = splnet(); 1051 1052 ifp = &sc->sc_ec.ec_if; 1053 ifp->if_softc = sc; 1054 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ); 1055 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1056 ifp->if_init = smsc_init; 1057 ifp->if_ioctl = smsc_ioctl; 1058 ifp->if_start = smsc_start; 1059 ifp->if_stop = smsc_stop; 1060 1061 #ifdef notyet 1062 /* 1063 * We can do TCPv4, and UDPv4 checksums in hardware. 1064 */ 1065 ifp->if_capabilities |= 1066 /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx | 1067 /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx; 1068 #endif 1069 1070 sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU; 1071 1072 /* Setup some of the basics */ 1073 sc->sc_phyno = 1; 1074 1075 /* 1076 * Attempt to get the mac address, if an EEPROM is not attached this 1077 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC 1078 * address based on urandom. 1079 */ 1080 memset(sc->sc_enaddr, 0xff, ETHER_ADDR_LEN); 1081 1082 prop_dictionary_t dict = device_properties(self); 1083 prop_data_t eaprop = prop_dictionary_get(dict, "mac-address"); 1084 1085 if (eaprop != NULL) { 1086 KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA); 1087 KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN); 1088 memcpy(sc->sc_enaddr, prop_data_data_nocopy(eaprop), 1089 ETHER_ADDR_LEN); 1090 } else 1091 /* Check if there is already a MAC address in the register */ 1092 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) && 1093 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) { 1094 sc->sc_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); 1095 sc->sc_enaddr[4] = (uint8_t)((mac_h) & 0xff); 1096 sc->sc_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); 1097 sc->sc_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); 1098 sc->sc_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); 1099 sc->sc_enaddr[0] = (uint8_t)((mac_l) & 0xff); 1100 } 1101 1102 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(sc->sc_enaddr)); 1103 1104 IFQ_SET_READY(&ifp->if_snd); 1105 1106 /* Initialize MII/media info. */ 1107 mii = &sc->sc_mii; 1108 mii->mii_ifp = ifp; 1109 mii->mii_readreg = smsc_miibus_readreg; 1110 mii->mii_writereg = smsc_miibus_writereg; 1111 mii->mii_statchg = smsc_miibus_statchg; 1112 mii->mii_flags = MIIF_AUTOTSLEEP; 1113 sc->sc_ec.ec_mii = mii; 1114 ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts); 1115 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 1116 1117 if (LIST_FIRST(&mii->mii_phys) == NULL) { 1118 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 1119 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 1120 } else 1121 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 1122 1123 if_attach(ifp); 1124 ether_ifattach(ifp, sc->sc_enaddr); 1125 1126 rnd_attach_source(&sc->sc_rnd_source, device_xname(sc->sc_dev), 1127 RND_TYPE_NET, RND_FLAG_DEFAULT); 1128 1129 callout_init(&sc->sc_stat_ch, 0); 1130 1131 splx(s); 1132 1133 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 1134 } 1135 1136 int 1137 smsc_detach(device_t self, int flags) 1138 { 1139 struct smsc_softc *sc = device_private(self); 1140 struct ifnet *ifp = &sc->sc_ec.ec_if; 1141 int s; 1142 1143 callout_stop(&sc->sc_stat_ch); 1144 1145 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) 1146 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 1147 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) 1148 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 1149 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) 1150 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 1151 1152 /* 1153 * Remove any pending tasks. They cannot be executing because they run 1154 * in the same thread as detach. 1155 */ 1156 usb_rem_task(sc->sc_udev, &sc->sc_tick_task); 1157 usb_rem_task(sc->sc_udev, &sc->sc_stop_task); 1158 1159 s = splusb(); 1160 1161 if (--sc->sc_refcnt >= 0) { 1162 /* Wait for processes to go away */ 1163 usb_detach_waitold(sc->sc_dev); 1164 } 1165 1166 if (ifp->if_flags & IFF_RUNNING) 1167 smsc_stop(ifp ,1); 1168 1169 rnd_detach_source(&sc->sc_rnd_source); 1170 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 1171 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 1172 if (ifp->if_softc != NULL) { 1173 ether_ifdetach(ifp); 1174 if_detach(ifp); 1175 } 1176 1177 #ifdef DIAGNOSTIC 1178 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL || 1179 sc->sc_ep[SMSC_ENDPT_RX] != NULL || 1180 sc->sc_ep[SMSC_ENDPT_INTR] != NULL) 1181 printf("%s: detach has active endpoints\n", 1182 device_xname(sc->sc_dev)); 1183 #endif 1184 1185 if (--sc->sc_refcnt >= 0) { 1186 /* Wait for processes to go away. */ 1187 usb_detach_waitold(sc->sc_dev); 1188 } 1189 splx(s); 1190 1191 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 1192 1193 mutex_destroy(&sc->sc_mii_lock); 1194 1195 return 0; 1196 } 1197 1198 void 1199 smsc_tick_task(void *xsc) 1200 { 1201 int s; 1202 struct smsc_softc *sc = xsc; 1203 struct ifnet *ifp; 1204 struct mii_data *mii; 1205 1206 if (sc == NULL) 1207 return; 1208 1209 if (sc->sc_dying) 1210 return; 1211 ifp = &sc->sc_ec.ec_if; 1212 mii = &sc->sc_mii; 1213 if (mii == NULL) 1214 return; 1215 1216 s = splnet(); 1217 1218 mii_tick(mii); 1219 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) 1220 smsc_miibus_statchg(ifp); 1221 callout_reset(&sc->sc_stat_ch, hz, smsc_tick, sc); 1222 1223 splx(s); 1224 } 1225 1226 int 1227 smsc_activate(device_t self, enum devact act) 1228 { 1229 struct smsc_softc *sc = device_private(self); 1230 1231 switch (act) { 1232 case DVACT_DEACTIVATE: 1233 if_deactivate(&sc->sc_ec.ec_if); 1234 sc->sc_dying = 1; 1235 return 0; 1236 default: 1237 return EOPNOTSUPP; 1238 } 1239 return 0; 1240 } 1241 1242 void 1243 smsc_lock_mii(struct smsc_softc *sc) 1244 { 1245 sc->sc_refcnt++; 1246 mutex_enter(&sc->sc_mii_lock); 1247 } 1248 1249 void 1250 smsc_unlock_mii(struct smsc_softc *sc) 1251 { 1252 mutex_exit(&sc->sc_mii_lock); 1253 if (--sc->sc_refcnt < 0) 1254 usb_detach_wakeupold(sc->sc_dev); 1255 } 1256 1257 void 1258 smsc_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1259 { 1260 struct smsc_chain *c = (struct smsc_chain *)priv; 1261 struct smsc_softc *sc = c->sc_sc; 1262 struct ifnet *ifp = &sc->sc_ec.ec_if; 1263 u_char *buf = c->sc_buf; 1264 uint32_t total_len; 1265 uint32_t rxhdr; 1266 uint16_t pktlen; 1267 struct mbuf *m; 1268 int s; 1269 1270 if (sc->sc_dying) 1271 return; 1272 1273 if (!(ifp->if_flags & IFF_RUNNING)) 1274 return; 1275 1276 if (status != USBD_NORMAL_COMPLETION) { 1277 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1278 return; 1279 if (usbd_ratecheck(&sc->sc_rx_notice)) { 1280 printf("%s: usb errors on rx: %s\n", 1281 device_xname(sc->sc_dev), usbd_errstr(status)); 1282 } 1283 if (status == USBD_STALLED) 1284 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]); 1285 goto done; 1286 } 1287 1288 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 1289 smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len); 1290 1291 while (total_len != 0) { 1292 if (total_len < sizeof(rxhdr)) { 1293 smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %zu\n", 1294 total_len, sizeof(rxhdr)); 1295 ifp->if_ierrors++; 1296 goto done; 1297 } 1298 1299 memcpy(&rxhdr, buf, sizeof(rxhdr)); 1300 rxhdr = le32toh(rxhdr); 1301 buf += sizeof(rxhdr); 1302 total_len -= sizeof(rxhdr); 1303 1304 if (rxhdr & SMSC_RX_STAT_COLLISION) 1305 ifp->if_collisions++; 1306 1307 if (rxhdr & (SMSC_RX_STAT_ERROR 1308 | SMSC_RX_STAT_LENGTH_ERROR 1309 | SMSC_RX_STAT_MII_ERROR)) { 1310 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr); 1311 ifp->if_ierrors++; 1312 goto done; 1313 } 1314 1315 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr); 1316 smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr " 1317 "0x%08x\n", total_len, pktlen, rxhdr); 1318 1319 if (pktlen < ETHER_HDR_LEN) { 1320 smsc_dbg_printf(sc, "pktlen %d < ETHER_HDR_LEN %d\n", 1321 pktlen, ETHER_HDR_LEN); 1322 ifp->if_ierrors++; 1323 goto done; 1324 } 1325 1326 pktlen += ETHER_ALIGN; 1327 1328 if (pktlen > MCLBYTES) { 1329 smsc_dbg_printf(sc, "pktlen %d > MCLBYTES %d\n", 1330 pktlen, MCLBYTES); 1331 ifp->if_ierrors++; 1332 goto done; 1333 } 1334 1335 if (pktlen > total_len) { 1336 smsc_dbg_printf(sc, "pktlen %d > total_len %d\n", 1337 pktlen, total_len); 1338 ifp->if_ierrors++; 1339 goto done; 1340 } 1341 1342 m = smsc_newbuf(); 1343 if (m == NULL) { 1344 smsc_dbg_printf(sc, "smc_newbuf returned NULL\n"); 1345 ifp->if_ierrors++; 1346 goto done; 1347 } 1348 1349 ifp->if_ipackets++; 1350 m_set_rcvif(m, ifp); 1351 m->m_pkthdr.len = m->m_len = pktlen; 1352 m->m_flags |= M_HASFCS; 1353 m_adj(m, ETHER_ALIGN); 1354 1355 KASSERT(m->m_len < MCLBYTES); 1356 memcpy(mtod(m, char *), buf + ETHER_ALIGN, m->m_len); 1357 1358 /* Check if RX TCP/UDP checksumming is being offloaded */ 1359 if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) { 1360 smsc_dbg_printf(sc,"RX checksum offload checking\n"); 1361 struct ether_header *eh; 1362 1363 eh = mtod(m, struct ether_header *); 1364 1365 /* Remove the extra 2 bytes of the csum */ 1366 m_adj(m, -2); 1367 1368 /* 1369 * The checksum appears to be simplistically calculated 1370 * over the udp/tcp header and data up to the end of the 1371 * eth frame. Which means if the eth frame is padded 1372 * the csum calculation is incorrectly performed over 1373 * the padding bytes as well. Therefore to be safe we 1374 * ignore the H/W csum on frames less than or equal to 1375 * 64 bytes. 1376 * 1377 * Ignore H/W csum for non-IPv4 packets. 1378 */ 1379 smsc_dbg_printf(sc,"Ethertype %02x pktlen %02x\n", 1380 be16toh(eh->ether_type), pktlen); 1381 if (be16toh(eh->ether_type) == ETHERTYPE_IP && 1382 pktlen > ETHER_MIN_LEN) { 1383 1384 m->m_pkthdr.csum_flags |= 1385 (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA); 1386 1387 /* 1388 * Copy the TCP/UDP checksum from the last 2 1389 * bytes of the transfer and put in the 1390 * csum_data field. 1391 */ 1392 memcpy(&m->m_pkthdr.csum_data, 1393 buf + pktlen - 2, 2); 1394 /* 1395 * The data is copied in network order, but the 1396 * csum algorithm in the kernel expects it to be 1397 * in host network order. 1398 */ 1399 m->m_pkthdr.csum_data = 1400 ntohs(m->m_pkthdr.csum_data); 1401 smsc_dbg_printf(sc, 1402 "RX checksum offloaded (0x%04x)\n", 1403 m->m_pkthdr.csum_data); 1404 } 1405 } 1406 1407 /* round up to next longword */ 1408 pktlen = (pktlen + 3) & ~0x3; 1409 1410 /* total_len does not include the padding */ 1411 if (pktlen > total_len) 1412 pktlen = total_len; 1413 1414 buf += pktlen; 1415 total_len -= pktlen; 1416 1417 /* push the packet up */ 1418 s = splnet(); 1419 bpf_mtap(ifp, m); 1420 if_percpuq_enqueue(ifp->if_percpuq, m); 1421 splx(s); 1422 } 1423 1424 done: 1425 /* Setup new transfer. */ 1426 usbd_setup_xfer(xfer, c, c->sc_buf, sc->sc_bufsz, USBD_SHORT_XFER_OK, 1427 USBD_NO_TIMEOUT, smsc_rxeof); 1428 usbd_transfer(xfer); 1429 1430 return; 1431 } 1432 1433 void 1434 smsc_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1435 { 1436 struct smsc_softc *sc; 1437 struct smsc_chain *c; 1438 struct ifnet *ifp; 1439 int s; 1440 1441 c = priv; 1442 sc = c->sc_sc; 1443 ifp = &sc->sc_ec.ec_if; 1444 1445 if (sc->sc_dying) 1446 return; 1447 1448 s = splnet(); 1449 1450 ifp->if_timer = 0; 1451 ifp->if_flags &= ~IFF_OACTIVE; 1452 1453 if (status != USBD_NORMAL_COMPLETION) { 1454 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1455 splx(s); 1456 return; 1457 } 1458 ifp->if_oerrors++; 1459 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev), 1460 usbd_errstr(status)); 1461 if (status == USBD_STALLED) 1462 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]); 1463 splx(s); 1464 return; 1465 } 1466 ifp->if_opackets++; 1467 1468 m_freem(c->sc_mbuf); 1469 c->sc_mbuf = NULL; 1470 1471 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1472 smsc_start(ifp); 1473 1474 splx(s); 1475 } 1476 1477 int 1478 smsc_tx_list_init(struct smsc_softc *sc) 1479 { 1480 struct smsc_cdata *cd; 1481 struct smsc_chain *c; 1482 int i; 1483 1484 cd = &sc->sc_cdata; 1485 for (i = 0; i < SMSC_TX_LIST_CNT; i++) { 1486 c = &cd->tx_chain[i]; 1487 c->sc_sc = sc; 1488 c->sc_idx = i; 1489 c->sc_mbuf = NULL; 1490 if (c->sc_xfer == NULL) { 1491 int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_TX], 1492 sc->sc_bufsz, USBD_FORCE_SHORT_XFER, 0, 1493 &c->sc_xfer); 1494 if (error) 1495 return EIO; 1496 c->sc_buf = usbd_get_buffer(c->sc_xfer); 1497 } 1498 } 1499 1500 return 0; 1501 } 1502 1503 int 1504 smsc_rx_list_init(struct smsc_softc *sc) 1505 { 1506 struct smsc_cdata *cd; 1507 struct smsc_chain *c; 1508 int i; 1509 1510 cd = &sc->sc_cdata; 1511 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 1512 c = &cd->rx_chain[i]; 1513 c->sc_sc = sc; 1514 c->sc_idx = i; 1515 c->sc_mbuf = NULL; 1516 if (c->sc_xfer == NULL) { 1517 int error = usbd_create_xfer(sc->sc_ep[SMSC_ENDPT_RX], 1518 sc->sc_bufsz, USBD_SHORT_XFER_OK, 0, &c->sc_xfer); 1519 if (error) 1520 return error; 1521 c->sc_buf = usbd_get_buffer(c->sc_xfer); 1522 } 1523 } 1524 1525 return 0; 1526 } 1527 1528 struct mbuf * 1529 smsc_newbuf(void) 1530 { 1531 struct mbuf *m; 1532 1533 MGETHDR(m, M_DONTWAIT, MT_DATA); 1534 if (m == NULL) 1535 return NULL; 1536 1537 MCLGET(m, M_DONTWAIT); 1538 if (!(m->m_flags & M_EXT)) { 1539 m_freem(m); 1540 return NULL; 1541 } 1542 1543 return m; 1544 } 1545 1546 int 1547 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx) 1548 { 1549 struct ifnet *ifp = &sc->sc_ec.ec_if; 1550 struct smsc_chain *c; 1551 usbd_status err; 1552 uint32_t txhdr; 1553 uint32_t frm_len = 0; 1554 1555 c = &sc->sc_cdata.tx_chain[idx]; 1556 1557 /* 1558 * Each frame is prefixed with two 32-bit values describing the 1559 * length of the packet and buffer. 1560 */ 1561 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) | 1562 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG; 1563 txhdr = htole32(txhdr); 1564 memcpy(c->sc_buf, &txhdr, sizeof(txhdr)); 1565 1566 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len); 1567 txhdr = htole32(txhdr); 1568 memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr)); 1569 1570 frm_len += 8; 1571 1572 /* Next copy in the actual packet */ 1573 m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len); 1574 frm_len += m->m_pkthdr.len; 1575 1576 c->sc_mbuf = m; 1577 1578 usbd_setup_xfer(c->sc_xfer, c, c->sc_buf, frm_len, 1579 USBD_FORCE_SHORT_XFER, 10000, smsc_txeof); 1580 1581 err = usbd_transfer(c->sc_xfer); 1582 /* XXXNH get task to stop interface */ 1583 if (err != USBD_IN_PROGRESS) { 1584 smsc_stop(ifp, 0); 1585 return EIO; 1586 } 1587 1588 sc->sc_cdata.tx_cnt++; 1589 1590 return 0; 1591 } 1592