1 /* $OpenBSD: if_smsc.c,v 1.37 2020/07/31 10:49:32 mglocker 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[128]; 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_dequeue(&ifp->if_snd); 653 if (m_head == NULL) 654 return; 655 656 if (smsc_encap(sc, m_head, 0)) { 657 m_freem(m_head); 658 ifq_set_oactive(&ifp->if_snd); 659 return; 660 } 661 662 #if NBPFILTER > 0 663 if (ifp->if_bpf) 664 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT); 665 #endif 666 ifq_set_oactive(&ifp->if_snd); 667 } 668 669 void 670 smsc_tick(void *xsc) 671 { 672 struct smsc_softc *sc = xsc; 673 674 if (sc == NULL) 675 return; 676 677 if (usbd_is_dying(sc->sc_udev)) 678 return; 679 680 usb_add_task(sc->sc_udev, &sc->sc_tick_task); 681 } 682 683 void 684 smsc_stop(struct smsc_softc *sc) 685 { 686 usbd_status err; 687 struct ifnet *ifp; 688 int i; 689 690 smsc_reset(sc); 691 692 ifp = &sc->sc_ac.ac_if; 693 ifp->if_timer = 0; 694 ifp->if_flags &= ~IFF_RUNNING; 695 ifq_clr_oactive(&ifp->if_snd); 696 697 timeout_del(&sc->sc_stat_ch); 698 699 /* Stop transfers. */ 700 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) { 701 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 702 if (err) { 703 printf("%s: close rx pipe failed: %s\n", 704 sc->sc_dev.dv_xname, usbd_errstr(err)); 705 } 706 sc->sc_ep[SMSC_ENDPT_RX] = NULL; 707 } 708 709 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) { 710 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 711 if (err) { 712 printf("%s: close tx pipe failed: %s\n", 713 sc->sc_dev.dv_xname, usbd_errstr(err)); 714 } 715 sc->sc_ep[SMSC_ENDPT_TX] = NULL; 716 } 717 718 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) { 719 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 720 if (err) { 721 printf("%s: close intr pipe failed: %s\n", 722 sc->sc_dev.dv_xname, usbd_errstr(err)); 723 } 724 sc->sc_ep[SMSC_ENDPT_INTR] = NULL; 725 } 726 727 /* Free RX resources. */ 728 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 729 if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) { 730 m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf); 731 sc->sc_cdata.rx_chain[i].sc_mbuf = NULL; 732 } 733 if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) { 734 usbd_free_xfer(sc->sc_cdata.rx_chain[i].sc_xfer); 735 sc->sc_cdata.rx_chain[i].sc_xfer = NULL; 736 } 737 } 738 739 /* Free TX resources. */ 740 for (i = 0; i < SMSC_TX_LIST_CNT; i++) { 741 if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) { 742 m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf); 743 sc->sc_cdata.tx_chain[i].sc_mbuf = NULL; 744 } 745 if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) { 746 usbd_free_xfer(sc->sc_cdata.tx_chain[i].sc_xfer); 747 sc->sc_cdata.tx_chain[i].sc_xfer = NULL; 748 } 749 } 750 } 751 752 int 753 smsc_chip_init(struct smsc_softc *sc) 754 { 755 int err; 756 uint32_t reg_val; 757 int burst_cap; 758 759 /* Enter H/W config mode */ 760 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST); 761 762 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, 763 SMSC_HW_CFG_LRST)) != 0) { 764 smsc_warn_printf(sc, "timed-out waiting for reset to " 765 "complete\n"); 766 goto init_failed; 767 } 768 769 /* Reset the PHY */ 770 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST); 771 772 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, 773 SMSC_PM_CTRL_PHY_RST)) != 0) { 774 smsc_warn_printf(sc, "timed-out waiting for phy reset to " 775 "complete\n"); 776 goto init_failed; 777 } 778 usbd_delay_ms(sc->sc_udev, 40); 779 780 /* Set the mac address */ 781 if ((err = smsc_setmacaddress(sc, sc->sc_ac.ac_enaddr)) != 0) { 782 smsc_warn_printf(sc, "failed to set the MAC address\n"); 783 goto init_failed; 784 } 785 786 /* 787 * Don't know what the HW_CFG_BIR bit is, but following the reset 788 * sequence as used in the Linux driver. 789 */ 790 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) { 791 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err); 792 goto init_failed; 793 } 794 reg_val |= SMSC_HW_CFG_BIR; 795 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 796 797 /* 798 * There is a so called 'turbo mode' that the linux driver supports, it 799 * seems to allow you to jam multiple frames per Rx transaction. 800 * By default this driver supports that and therefore allows multiple 801 * frames per URB. 802 * 803 * The xfer buffer size needs to reflect this as well, therefore based 804 * on the calculations in the Linux driver the RX bufsize is set to 805 * 18944, 806 * bufsz = (16 * 1024 + 5 * 512) 807 * 808 * Burst capability is the number of URBs that can be in a burst of 809 * data/ethernet frames. 810 */ 811 #ifdef SMSC_TURBO 812 if (sc->sc_udev->speed == USB_SPEED_HIGH) 813 burst_cap = 37; 814 else 815 burst_cap = 128; 816 #else 817 burst_cap = 0; 818 #endif 819 820 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap); 821 822 /* Set the default bulk in delay (magic value from Linux driver) */ 823 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000); 824 825 826 827 /* 828 * Initialise the RX interface 829 */ 830 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) { 831 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", 832 err); 833 goto init_failed; 834 } 835 836 /* 837 * The following setings are used for 'turbo mode', a.k.a multiple 838 * frames per Rx transaction (again info taken form Linux driver). 839 */ 840 #ifdef SMSC_TURBO 841 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE); 842 #endif 843 844 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 845 846 /* Clear the status register ? */ 847 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff); 848 849 /* Read and display the revision register */ 850 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) { 851 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err); 852 goto init_failed; 853 } 854 855 /* GPIO/LED setup */ 856 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED | 857 SMSC_LED_GPIO_CFG_FDX_LED; 858 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val); 859 860 /* 861 * Initialise the TX interface 862 */ 863 smsc_write_reg(sc, SMSC_FLOW, 0); 864 865 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT); 866 867 /* Read the current MAC configuration */ 868 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) { 869 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err); 870 goto init_failed; 871 } 872 873 /* Vlan */ 874 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN); 875 876 /* 877 * Start TX 878 */ 879 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN; 880 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 881 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON); 882 883 /* 884 * Start RX 885 */ 886 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN; 887 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 888 889 return (0); 890 891 init_failed: 892 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err); 893 return (err); 894 } 895 896 int 897 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 898 { 899 struct smsc_softc *sc = ifp->if_softc; 900 struct ifreq *ifr = (struct ifreq *)data; 901 int s, error = 0; 902 903 s = splnet(); 904 905 switch(cmd) { 906 case SIOCSIFADDR: 907 ifp->if_flags |= IFF_UP; 908 if (!(ifp->if_flags & IFF_RUNNING)) 909 smsc_init(sc); 910 break; 911 912 case SIOCSIFFLAGS: 913 if (ifp->if_flags & IFF_UP) { 914 if (ifp->if_flags & IFF_RUNNING) 915 error = ENETRESET; 916 else 917 smsc_init(sc); 918 } else { 919 if (ifp->if_flags & IFF_RUNNING) 920 smsc_stop(sc); 921 } 922 break; 923 924 case SIOCGIFMEDIA: 925 case SIOCSIFMEDIA: 926 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd); 927 break; 928 929 default: 930 error = ether_ioctl(ifp, &sc->sc_ac, cmd, data); 931 } 932 933 if (error == ENETRESET) { 934 if (ifp->if_flags & IFF_RUNNING) 935 smsc_iff(sc); 936 error = 0; 937 } 938 939 splx(s); 940 return(error); 941 } 942 943 int 944 smsc_match(struct device *parent, void *match, void *aux) 945 { 946 struct usb_attach_arg *uaa = aux; 947 948 if (uaa->iface == NULL || uaa->configno != 1) 949 return UMATCH_NONE; 950 951 return (usb_lookup(smsc_devs, uaa->vendor, uaa->product) != NULL) ? 952 UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE; 953 } 954 955 void 956 smsc_attach(struct device *parent, struct device *self, void *aux) 957 { 958 struct smsc_softc *sc = (struct smsc_softc *)self; 959 struct usb_attach_arg *uaa = aux; 960 usb_interface_descriptor_t *id; 961 usb_endpoint_descriptor_t *ed; 962 struct mii_data *mii; 963 struct ifnet *ifp; 964 uint32_t mac_h, mac_l; 965 int s, i; 966 967 sc->sc_udev = uaa->device; 968 sc->sc_iface = uaa->iface; 969 970 /* Setup the endpoints for the SMSC LAN95xx device(s) */ 971 usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, 972 USB_TASK_TYPE_GENERIC); 973 rw_init(&sc->sc_mii_lock, "smscmii"); 974 usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc, 975 USB_TASK_TYPE_GENERIC); 976 977 id = usbd_get_interface_descriptor(sc->sc_iface); 978 979 if (sc->sc_udev->speed >= USB_SPEED_HIGH) 980 sc->sc_bufsz = SMSC_MAX_BUFSZ; 981 else 982 sc->sc_bufsz = SMSC_MIN_BUFSZ; 983 984 /* Find endpoints. */ 985 for (i = 0; i < id->bNumEndpoints; i++) { 986 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 987 if (!ed) { 988 printf("%s: couldn't get ep %d\n", 989 sc->sc_dev.dv_xname, i); 990 return; 991 } 992 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 993 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 994 sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress; 995 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 996 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 997 sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress; 998 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 999 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 1000 sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress; 1001 } 1002 } 1003 1004 s = splnet(); 1005 1006 ifp = &sc->sc_ac.ac_if; 1007 ifp->if_softc = sc; 1008 strlcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 1009 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1010 ifp->if_ioctl = smsc_ioctl; 1011 ifp->if_start = smsc_start; 1012 ifp->if_capabilities = IFCAP_VLAN_MTU; 1013 1014 /* Setup some of the basics */ 1015 sc->sc_phyno = 1; 1016 1017 /* 1018 * Attempt to get the mac address, if an EEPROM is not attached this 1019 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC 1020 * address based on urandom. 1021 */ 1022 memset(sc->sc_ac.ac_enaddr, 0xff, ETHER_ADDR_LEN); 1023 1024 /* Check if there is already a MAC address in the register */ 1025 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) && 1026 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) { 1027 sc->sc_ac.ac_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); 1028 sc->sc_ac.ac_enaddr[4] = (uint8_t)((mac_h) & 0xff); 1029 sc->sc_ac.ac_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); 1030 sc->sc_ac.ac_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); 1031 sc->sc_ac.ac_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); 1032 sc->sc_ac.ac_enaddr[0] = (uint8_t)((mac_l) & 0xff); 1033 } 1034 1035 smsc_enaddr_OF(sc); 1036 1037 printf("%s: address %s\n", sc->sc_dev.dv_xname, 1038 ether_sprintf(sc->sc_ac.ac_enaddr)); 1039 1040 /* Initialise the chip for the first time */ 1041 smsc_chip_init(sc); 1042 1043 /* Initialize MII/media info. */ 1044 mii = &sc->sc_mii; 1045 mii->mii_ifp = ifp; 1046 mii->mii_readreg = smsc_miibus_readreg; 1047 mii->mii_writereg = smsc_miibus_writereg; 1048 mii->mii_statchg = smsc_miibus_statchg; 1049 mii->mii_flags = MIIF_AUTOTSLEEP; 1050 1051 ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts); 1052 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 1053 1054 if (LIST_FIRST(&mii->mii_phys) == NULL) { 1055 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 1056 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 1057 } else 1058 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 1059 1060 if_attach(ifp); 1061 ether_ifattach(ifp); 1062 1063 timeout_set(&sc->sc_stat_ch, smsc_tick, sc); 1064 1065 splx(s); 1066 } 1067 1068 int 1069 smsc_detach(struct device *self, int flags) 1070 { 1071 struct smsc_softc *sc = (struct smsc_softc *)self; 1072 struct ifnet *ifp = &sc->sc_ac.ac_if; 1073 int s; 1074 1075 if (timeout_initialized(&sc->sc_stat_ch)) 1076 timeout_del(&sc->sc_stat_ch); 1077 1078 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) 1079 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 1080 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) 1081 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 1082 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) 1083 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 1084 1085 /* 1086 * Remove any pending tasks. They cannot be executing because they run 1087 * in the same thread as detach. 1088 */ 1089 usb_rem_task(sc->sc_udev, &sc->sc_tick_task); 1090 usb_rem_task(sc->sc_udev, &sc->sc_stop_task); 1091 1092 s = splusb(); 1093 1094 if (--sc->sc_refcnt >= 0) { 1095 /* Wait for processes to go away */ 1096 usb_detach_wait(&sc->sc_dev); 1097 } 1098 1099 if (ifp->if_flags & IFF_RUNNING) 1100 smsc_stop(sc); 1101 1102 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 1103 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 1104 if (ifp->if_softc != NULL) { 1105 ether_ifdetach(ifp); 1106 if_detach(ifp); 1107 } 1108 1109 #ifdef DIAGNOSTIC 1110 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL || 1111 sc->sc_ep[SMSC_ENDPT_RX] != NULL || 1112 sc->sc_ep[SMSC_ENDPT_INTR] != NULL) 1113 printf("%s: detach has active endpoints\n", 1114 sc->sc_dev.dv_xname); 1115 #endif 1116 1117 splx(s); 1118 1119 return (0); 1120 } 1121 1122 void 1123 smsc_tick_task(void *xsc) 1124 { 1125 int s; 1126 struct smsc_softc *sc = xsc; 1127 struct mii_data *mii; 1128 1129 if (sc == NULL) 1130 return; 1131 1132 if (usbd_is_dying(sc->sc_udev)) 1133 return; 1134 mii = &sc->sc_mii; 1135 if (mii == NULL) 1136 return; 1137 1138 s = splnet(); 1139 1140 mii_tick(mii); 1141 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) 1142 smsc_miibus_statchg(&sc->sc_dev); 1143 timeout_add_sec(&sc->sc_stat_ch, 1); 1144 1145 splx(s); 1146 } 1147 1148 void 1149 smsc_lock_mii(struct smsc_softc *sc) 1150 { 1151 sc->sc_refcnt++; 1152 rw_enter_write(&sc->sc_mii_lock); 1153 } 1154 1155 void 1156 smsc_unlock_mii(struct smsc_softc *sc) 1157 { 1158 rw_exit_write(&sc->sc_mii_lock); 1159 if (--sc->sc_refcnt < 0) 1160 usb_detach_wakeup(&sc->sc_dev); 1161 } 1162 1163 void 1164 smsc_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1165 { 1166 struct smsc_chain *c = (struct smsc_chain *)priv; 1167 struct smsc_softc *sc = c->sc_sc; 1168 struct ifnet *ifp = &sc->sc_ac.ac_if; 1169 u_char *buf = c->sc_buf; 1170 uint32_t total_len; 1171 uint16_t pktlen = 0; 1172 struct mbuf_list ml = MBUF_LIST_INITIALIZER(); 1173 struct mbuf *m; 1174 int s; 1175 uint32_t rxhdr; 1176 1177 if (usbd_is_dying(sc->sc_udev)) 1178 return; 1179 1180 if (!(ifp->if_flags & IFF_RUNNING)) 1181 return; 1182 1183 if (status != USBD_NORMAL_COMPLETION) { 1184 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1185 return; 1186 if (usbd_ratecheck(&sc->sc_rx_notice)) { 1187 printf("%s: usb errors on rx: %s\n", 1188 sc->sc_dev.dv_xname, usbd_errstr(status)); 1189 } 1190 if (status == USBD_STALLED) 1191 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]); 1192 goto done; 1193 } 1194 1195 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 1196 smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len); 1197 1198 do { 1199 if (total_len < sizeof(rxhdr)) { 1200 smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %d\n", 1201 total_len, sizeof(rxhdr)); 1202 ifp->if_ierrors++; 1203 goto done; 1204 } 1205 1206 buf += pktlen; 1207 1208 memcpy(&rxhdr, buf, sizeof(rxhdr)); 1209 rxhdr = letoh32(rxhdr); 1210 total_len -= sizeof(rxhdr); 1211 1212 if (rxhdr & SMSC_RX_STAT_ERROR) { 1213 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr); 1214 ifp->if_ierrors++; 1215 goto done; 1216 } 1217 1218 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr); 1219 smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr " 1220 "0x%08x\n", total_len, pktlen, rxhdr); 1221 if (pktlen > total_len) { 1222 smsc_dbg_printf(sc, "pktlen %d > total_len %d\n", 1223 pktlen, total_len); 1224 ifp->if_ierrors++; 1225 goto done; 1226 } 1227 1228 buf += sizeof(rxhdr); 1229 1230 if (total_len < pktlen) 1231 total_len = 0; 1232 else 1233 total_len -= pktlen; 1234 1235 m = m_devget(buf, pktlen, ETHER_ALIGN); 1236 if (m == NULL) { 1237 smsc_dbg_printf(sc, "m_devget returned NULL\n"); 1238 ifp->if_ierrors++; 1239 goto done; 1240 } 1241 1242 ml_enqueue(&ml, m); 1243 } while (total_len > 0); 1244 1245 done: 1246 s = splnet(); 1247 if_input(ifp, &ml); 1248 splx(s); 1249 memset(c->sc_buf, 0, sc->sc_bufsz); 1250 1251 /* Setup new transfer. */ 1252 usbd_setup_xfer(xfer, sc->sc_ep[SMSC_ENDPT_RX], 1253 c, c->sc_buf, sc->sc_bufsz, 1254 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1255 USBD_NO_TIMEOUT, smsc_rxeof); 1256 usbd_transfer(xfer); 1257 1258 return; 1259 } 1260 1261 void 1262 smsc_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1263 { 1264 struct smsc_softc *sc; 1265 struct smsc_chain *c; 1266 struct ifnet *ifp; 1267 int s; 1268 1269 c = priv; 1270 sc = c->sc_sc; 1271 ifp = &sc->sc_ac.ac_if; 1272 1273 if (usbd_is_dying(sc->sc_udev)) 1274 return; 1275 1276 s = splnet(); 1277 1278 if (status != USBD_NORMAL_COMPLETION) { 1279 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1280 splx(s); 1281 return; 1282 } 1283 ifp->if_oerrors++; 1284 printf("%s: usb error on tx: %s\n", sc->sc_dev.dv_xname, 1285 usbd_errstr(status)); 1286 if (status == USBD_STALLED) 1287 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]); 1288 splx(s); 1289 return; 1290 } 1291 1292 ifp->if_timer = 0; 1293 ifq_clr_oactive(&ifp->if_snd); 1294 1295 m_freem(c->sc_mbuf); 1296 c->sc_mbuf = NULL; 1297 1298 if (ifq_empty(&ifp->if_snd) == 0) 1299 smsc_start(ifp); 1300 1301 splx(s); 1302 } 1303 1304 int 1305 smsc_tx_list_init(struct smsc_softc *sc) 1306 { 1307 struct smsc_cdata *cd; 1308 struct smsc_chain *c; 1309 int i; 1310 1311 cd = &sc->sc_cdata; 1312 for (i = 0; i < SMSC_TX_LIST_CNT; i++) { 1313 c = &cd->tx_chain[i]; 1314 c->sc_sc = sc; 1315 c->sc_idx = i; 1316 c->sc_mbuf = NULL; 1317 if (c->sc_xfer == NULL) { 1318 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev); 1319 if (c->sc_xfer == NULL) 1320 return (ENOBUFS); 1321 c->sc_buf = usbd_alloc_buffer(c->sc_xfer, 1322 sc->sc_bufsz); 1323 if (c->sc_buf == NULL) { 1324 usbd_free_xfer(c->sc_xfer); 1325 return (ENOBUFS); 1326 } 1327 } 1328 } 1329 1330 return (0); 1331 } 1332 1333 int 1334 smsc_rx_list_init(struct smsc_softc *sc) 1335 { 1336 struct smsc_cdata *cd; 1337 struct smsc_chain *c; 1338 int i; 1339 1340 cd = &sc->sc_cdata; 1341 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 1342 c = &cd->rx_chain[i]; 1343 c->sc_sc = sc; 1344 c->sc_idx = i; 1345 c->sc_mbuf = NULL; 1346 if (c->sc_xfer == NULL) { 1347 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev); 1348 if (c->sc_xfer == NULL) 1349 return (ENOBUFS); 1350 c->sc_buf = usbd_alloc_buffer(c->sc_xfer, 1351 sc->sc_bufsz); 1352 if (c->sc_buf == NULL) { 1353 usbd_free_xfer(c->sc_xfer); 1354 return (ENOBUFS); 1355 } 1356 } 1357 } 1358 1359 return (0); 1360 } 1361 1362 int 1363 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx) 1364 { 1365 struct smsc_chain *c; 1366 usbd_status err; 1367 uint32_t txhdr; 1368 uint32_t frm_len = 0; 1369 1370 c = &sc->sc_cdata.tx_chain[idx]; 1371 1372 /* 1373 * Each frame is prefixed with two 32-bit values describing the 1374 * length of the packet and buffer. 1375 */ 1376 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) | 1377 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG; 1378 txhdr = htole32(txhdr); 1379 memcpy(c->sc_buf, &txhdr, sizeof(txhdr)); 1380 1381 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len); 1382 txhdr = htole32(txhdr); 1383 memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr)); 1384 1385 frm_len += 8; 1386 1387 /* Next copy in the actual packet */ 1388 m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len); 1389 frm_len += m->m_pkthdr.len; 1390 1391 c->sc_mbuf = m; 1392 1393 usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_TX], 1394 c, c->sc_buf, frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 1395 10000, smsc_txeof); 1396 1397 err = usbd_transfer(c->sc_xfer); 1398 if (err != USBD_IN_PROGRESS) { 1399 c->sc_mbuf = NULL; 1400 smsc_stop(sc); 1401 return (EIO); 1402 } 1403 1404 sc->sc_cdata.tx_cnt++; 1405 1406 return (0); 1407 } 1408