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