1 /* $OpenBSD: if_smsc.c,v 1.28 2016/07/31 12:51:49 kettenis 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 int 180 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data) 181 { 182 usb_device_request_t req; 183 uint32_t buf; 184 usbd_status err; 185 186 req.bmRequestType = UT_READ_VENDOR_DEVICE; 187 req.bRequest = SMSC_UR_READ_REG; 188 USETW(req.wValue, 0); 189 USETW(req.wIndex, off); 190 USETW(req.wLength, 4); 191 192 err = usbd_do_request(sc->sc_udev, &req, &buf); 193 if (err != 0) 194 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off); 195 196 *data = letoh32(buf); 197 198 return (err); 199 } 200 201 int 202 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data) 203 { 204 usb_device_request_t req; 205 uint32_t buf; 206 usbd_status err; 207 208 buf = htole32(data); 209 210 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 211 req.bRequest = SMSC_UR_WRITE_REG; 212 USETW(req.wValue, 0); 213 USETW(req.wIndex, off); 214 USETW(req.wLength, 4); 215 216 err = usbd_do_request(sc->sc_udev, &req, &buf); 217 if (err != 0) 218 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off); 219 220 return (err); 221 } 222 223 int 224 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits) 225 { 226 uint32_t val; 227 int err, i; 228 229 for (i = 0; i < 100; i++) { 230 if ((err = smsc_read_reg(sc, reg, &val)) != 0) 231 return (err); 232 if (!(val & bits)) 233 return (0); 234 DELAY(5); 235 } 236 237 return (1); 238 } 239 240 int 241 smsc_miibus_readreg(struct device *dev, int phy, int reg) 242 { 243 struct smsc_softc *sc = (struct smsc_softc *)dev; 244 uint32_t addr; 245 uint32_t val = 0; 246 247 smsc_lock_mii(sc); 248 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { 249 smsc_warn_printf(sc, "MII is busy\n"); 250 goto done; 251 } 252 253 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ; 254 smsc_write_reg(sc, SMSC_MII_ADDR, addr); 255 256 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) 257 smsc_warn_printf(sc, "MII read timeout\n"); 258 259 smsc_read_reg(sc, SMSC_MII_DATA, &val); 260 261 done: 262 smsc_unlock_mii(sc); 263 return (val & 0xFFFF); 264 } 265 266 void 267 smsc_miibus_writereg(struct device *dev, int phy, int reg, int val) 268 { 269 struct smsc_softc *sc = (struct smsc_softc *)dev; 270 uint32_t addr; 271 272 if (sc->sc_phyno != phy) 273 return; 274 275 smsc_lock_mii(sc); 276 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { 277 smsc_warn_printf(sc, "MII is busy\n"); 278 smsc_unlock_mii(sc); 279 return; 280 } 281 282 smsc_write_reg(sc, SMSC_MII_DATA, val); 283 284 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE; 285 smsc_write_reg(sc, SMSC_MII_ADDR, addr); 286 smsc_unlock_mii(sc); 287 288 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) 289 smsc_warn_printf(sc, "MII write timeout\n"); 290 } 291 292 void 293 smsc_miibus_statchg(struct device *dev) 294 { 295 struct smsc_softc *sc = (struct smsc_softc *)dev; 296 struct mii_data *mii = &sc->sc_mii; 297 struct ifnet *ifp = &sc->sc_ac.ac_if; 298 int err; 299 uint32_t flow; 300 uint32_t afc_cfg; 301 302 if (mii == NULL || ifp == NULL || 303 (ifp->if_flags & IFF_RUNNING) == 0) 304 return; 305 306 /* Use the MII status to determine link status */ 307 sc->sc_flags &= ~SMSC_FLAG_LINK; 308 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 309 (IFM_ACTIVE | IFM_AVALID)) { 310 switch (IFM_SUBTYPE(mii->mii_media_active)) { 311 case IFM_10_T: 312 case IFM_100_TX: 313 sc->sc_flags |= SMSC_FLAG_LINK; 314 break; 315 case IFM_1000_T: 316 /* Gigabit ethernet not supported by chipset */ 317 break; 318 default: 319 break; 320 } 321 } 322 323 /* Lost link, do nothing. */ 324 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) { 325 smsc_dbg_printf(sc, "link flag not set\n"); 326 return; 327 } 328 329 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg); 330 if (err) { 331 smsc_warn_printf(sc, "failed to read initial AFC_CFG, " 332 "error %d\n", err); 333 return; 334 } 335 336 /* Enable/disable full duplex operation and TX/RX pause */ 337 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) { 338 smsc_dbg_printf(sc, "full duplex operation\n"); 339 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN; 340 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX; 341 342 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0) 343 flow = 0xffff0002; 344 else 345 flow = 0; 346 347 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0) 348 afc_cfg |= 0xf; 349 else 350 afc_cfg &= ~0xf; 351 352 } else { 353 smsc_dbg_printf(sc, "half duplex operation\n"); 354 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX; 355 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN; 356 357 flow = 0; 358 afc_cfg |= 0xf; 359 } 360 361 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 362 err += smsc_write_reg(sc, SMSC_FLOW, flow); 363 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg); 364 if (err) 365 smsc_warn_printf(sc, "media change failed, error %d\n", err); 366 } 367 368 int 369 smsc_ifmedia_upd(struct ifnet *ifp) 370 { 371 struct smsc_softc *sc = ifp->if_softc; 372 struct mii_data *mii = &sc->sc_mii; 373 int err; 374 375 if (mii->mii_instance) { 376 struct mii_softc *miisc; 377 378 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 379 mii_phy_reset(miisc); 380 } 381 err = mii_mediachg(mii); 382 return (err); 383 } 384 385 void 386 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 387 { 388 struct smsc_softc *sc = ifp->if_softc; 389 struct mii_data *mii = &sc->sc_mii; 390 391 mii_pollstat(mii); 392 393 ifmr->ifm_active = mii->mii_media_active; 394 ifmr->ifm_status = mii->mii_media_status; 395 } 396 397 static inline uint32_t 398 smsc_hash(uint8_t addr[ETHER_ADDR_LEN]) 399 { 400 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f; 401 } 402 403 void 404 smsc_iff(struct smsc_softc *sc) 405 { 406 struct ifnet *ifp = &sc->sc_ac.ac_if; 407 struct arpcom *ac = &sc->sc_ac; 408 struct ether_multi *enm; 409 struct ether_multistep step; 410 uint32_t hashtbl[2] = { 0, 0 }; 411 uint32_t hash; 412 413 if (usbd_is_dying(sc->sc_udev)) 414 return; 415 416 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_HPFILT | SMSC_MAC_CSR_MCPAS | 417 SMSC_MAC_CSR_PRMS); 418 ifp->if_flags &= ~IFF_ALLMULTI; 419 420 if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0) { 421 ifp->if_flags |= IFF_ALLMULTI; 422 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS; 423 if (ifp->if_flags & IFF_PROMISC) 424 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS; 425 } else { 426 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT; 427 428 ETHER_FIRST_MULTI(step, ac, enm); 429 while (enm != NULL) { 430 hash = smsc_hash(enm->enm_addrlo); 431 432 hashtbl[hash >> 5] |= 1 << (hash & 0x1F); 433 434 ETHER_NEXT_MULTI(step, enm); 435 } 436 } 437 438 /* Debug */ 439 if (sc->sc_mac_csr & SMSC_MAC_CSR_MCPAS) 440 smsc_dbg_printf(sc, "receive all multicast enabled\n"); 441 else if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) 442 smsc_dbg_printf(sc, "receive select group of macs\n"); 443 444 /* Write the hash table and mac control registers */ 445 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]); 446 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]); 447 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 448 } 449 450 int 451 smsc_sethwcsum(struct smsc_softc *sc) 452 { 453 struct ifnet *ifp = &sc->sc_ac.ac_if; 454 uint32_t val; 455 int err; 456 457 if (!ifp) 458 return (-EIO); 459 460 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val); 461 if (err != 0) { 462 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", 463 err); 464 return (err); 465 } 466 467 /* Enable/disable the Rx checksum */ 468 if (ifp->if_capabilities & IFCAP_CSUM_IPv4) 469 val |= SMSC_COE_CTRL_RX_EN; 470 else 471 val &= ~SMSC_COE_CTRL_RX_EN; 472 473 /* Enable/disable the Tx checksum (currently not supported) */ 474 if (ifp->if_capabilities & IFCAP_CSUM_IPv4) 475 val |= SMSC_COE_CTRL_TX_EN; 476 else 477 val &= ~SMSC_COE_CTRL_TX_EN; 478 479 err = smsc_write_reg(sc, SMSC_COE_CTRL, val); 480 if (err != 0) { 481 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", 482 err); 483 return (err); 484 } 485 486 return (0); 487 } 488 489 int 490 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr) 491 { 492 int err; 493 uint32_t val; 494 495 smsc_dbg_printf(sc, "setting mac address to " 496 "%02x:%02x:%02x:%02x:%02x:%02x\n", 497 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 498 499 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0]; 500 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0) 501 goto done; 502 503 val = (addr[5] << 8) | addr[4]; 504 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val); 505 506 done: 507 return (err); 508 } 509 510 void 511 smsc_reset(struct smsc_softc *sc) 512 { 513 if (usbd_is_dying(sc->sc_udev)) 514 return; 515 516 /* Wait a little while for the chip to get its brains in order. */ 517 DELAY(1000); 518 519 /* Reinitialize controller to achieve full reset. */ 520 smsc_chip_init(sc); 521 } 522 523 void 524 smsc_init(void *xsc) 525 { 526 struct smsc_softc *sc = xsc; 527 struct ifnet *ifp = &sc->sc_ac.ac_if; 528 struct smsc_chain *c; 529 usbd_status err; 530 int s, i; 531 532 s = splnet(); 533 534 /* Cancel pending I/O */ 535 smsc_stop(sc); 536 537 /* Reset the ethernet interface. */ 538 smsc_reset(sc); 539 540 /* Init RX ring. */ 541 if (smsc_rx_list_init(sc) == ENOBUFS) { 542 printf("%s: rx list init failed\n", sc->sc_dev.dv_xname); 543 splx(s); 544 return; 545 } 546 547 /* Init TX ring. */ 548 if (smsc_tx_list_init(sc) == ENOBUFS) { 549 printf("%s: tx list init failed\n", sc->sc_dev.dv_xname); 550 splx(s); 551 return; 552 } 553 554 /* Program promiscuous mode and multicast filters. */ 555 smsc_iff(sc); 556 557 /* Open RX and TX pipes. */ 558 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX], 559 USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_RX]); 560 if (err) { 561 printf("%s: open rx pipe failed: %s\n", 562 sc->sc_dev.dv_xname, usbd_errstr(err)); 563 splx(s); 564 return; 565 } 566 567 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX], 568 USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_TX]); 569 if (err) { 570 printf("%s: open tx pipe failed: %s\n", 571 sc->sc_dev.dv_xname, usbd_errstr(err)); 572 splx(s); 573 return; 574 } 575 576 /* Start up the receive pipe. */ 577 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 578 c = &sc->sc_cdata.rx_chain[i]; 579 usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_RX], 580 c, c->sc_buf, sc->sc_bufsz, 581 USBD_SHORT_XFER_OK | USBD_NO_COPY, 582 USBD_NO_TIMEOUT, smsc_rxeof); 583 usbd_transfer(c->sc_xfer); 584 } 585 586 /* TCP/UDP checksum offload engines. */ 587 smsc_sethwcsum(sc); 588 589 /* Indicate we are up and running. */ 590 ifp->if_flags |= IFF_RUNNING; 591 ifq_clr_oactive(&ifp->if_snd); 592 593 timeout_add_sec(&sc->sc_stat_ch, 1); 594 595 splx(s); 596 } 597 598 void 599 smsc_start(struct ifnet *ifp) 600 { 601 struct smsc_softc *sc = ifp->if_softc; 602 struct mbuf *m_head = NULL; 603 604 /* Don't send anything if there is no link or controller is busy. */ 605 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 || 606 ifq_is_oactive(&ifp->if_snd)) { 607 return; 608 } 609 610 m_head = ifq_deq_begin(&ifp->if_snd); 611 if (m_head == NULL) 612 return; 613 614 if (smsc_encap(sc, m_head, 0)) { 615 ifq_deq_rollback(&ifp->if_snd, m_head); 616 ifq_set_oactive(&ifp->if_snd); 617 return; 618 } 619 ifq_deq_commit(&ifp->if_snd, m_head); 620 621 #if NBPFILTER > 0 622 if (ifp->if_bpf) 623 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT); 624 #endif 625 ifq_set_oactive(&ifp->if_snd); 626 } 627 628 void 629 smsc_tick(void *xsc) 630 { 631 struct smsc_softc *sc = xsc; 632 633 if (sc == NULL) 634 return; 635 636 if (usbd_is_dying(sc->sc_udev)) 637 return; 638 639 usb_add_task(sc->sc_udev, &sc->sc_tick_task); 640 } 641 642 void 643 smsc_stop(struct smsc_softc *sc) 644 { 645 usbd_status err; 646 struct ifnet *ifp; 647 int i; 648 649 smsc_reset(sc); 650 651 ifp = &sc->sc_ac.ac_if; 652 ifp->if_timer = 0; 653 ifp->if_flags &= ~IFF_RUNNING; 654 ifq_clr_oactive(&ifp->if_snd); 655 656 timeout_del(&sc->sc_stat_ch); 657 658 /* Stop transfers. */ 659 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) { 660 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 661 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 662 if (err) { 663 printf("%s: close rx pipe failed: %s\n", 664 sc->sc_dev.dv_xname, usbd_errstr(err)); 665 } 666 sc->sc_ep[SMSC_ENDPT_RX] = NULL; 667 } 668 669 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) { 670 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 671 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 672 if (err) { 673 printf("%s: close tx pipe failed: %s\n", 674 sc->sc_dev.dv_xname, usbd_errstr(err)); 675 } 676 sc->sc_ep[SMSC_ENDPT_TX] = NULL; 677 } 678 679 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) { 680 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 681 err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 682 if (err) { 683 printf("%s: close intr pipe failed: %s\n", 684 sc->sc_dev.dv_xname, usbd_errstr(err)); 685 } 686 sc->sc_ep[SMSC_ENDPT_INTR] = NULL; 687 } 688 689 /* Free RX resources. */ 690 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 691 if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) { 692 m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf); 693 sc->sc_cdata.rx_chain[i].sc_mbuf = NULL; 694 } 695 if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) { 696 usbd_free_xfer(sc->sc_cdata.rx_chain[i].sc_xfer); 697 sc->sc_cdata.rx_chain[i].sc_xfer = NULL; 698 } 699 } 700 701 /* Free TX resources. */ 702 for (i = 0; i < SMSC_TX_LIST_CNT; i++) { 703 if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) { 704 m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf); 705 sc->sc_cdata.tx_chain[i].sc_mbuf = NULL; 706 } 707 if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) { 708 usbd_free_xfer(sc->sc_cdata.tx_chain[i].sc_xfer); 709 sc->sc_cdata.tx_chain[i].sc_xfer = NULL; 710 } 711 } 712 } 713 714 int 715 smsc_chip_init(struct smsc_softc *sc) 716 { 717 int err; 718 uint32_t reg_val; 719 int burst_cap; 720 721 /* Enter H/W config mode */ 722 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST); 723 724 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, 725 SMSC_HW_CFG_LRST)) != 0) { 726 smsc_warn_printf(sc, "timed-out waiting for reset to " 727 "complete\n"); 728 goto init_failed; 729 } 730 731 /* Reset the PHY */ 732 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST); 733 734 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, 735 SMSC_PM_CTRL_PHY_RST) != 0)) { 736 smsc_warn_printf(sc, "timed-out waiting for phy reset to " 737 "complete\n"); 738 goto init_failed; 739 } 740 usbd_delay_ms(sc->sc_udev, 40); 741 742 /* Set the mac address */ 743 if ((err = smsc_setmacaddress(sc, sc->sc_ac.ac_enaddr)) != 0) { 744 smsc_warn_printf(sc, "failed to set the MAC address\n"); 745 goto init_failed; 746 } 747 748 /* 749 * Don't know what the HW_CFG_BIR bit is, but following the reset 750 * sequence as used in the Linux driver. 751 */ 752 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) { 753 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err); 754 goto init_failed; 755 } 756 reg_val |= SMSC_HW_CFG_BIR; 757 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 758 759 /* 760 * There is a so called 'turbo mode' that the linux driver supports, it 761 * seems to allow you to jam multiple frames per Rx transaction. 762 * By default this driver supports that and therefore allows multiple 763 * frames per URB. 764 * 765 * The xfer buffer size needs to reflect this as well, therefore based 766 * on the calculations in the Linux driver the RX bufsize is set to 767 * 18944, 768 * bufsz = (16 * 1024 + 5 * 512) 769 * 770 * Burst capability is the number of URBs that can be in a burst of 771 * data/ethernet frames. 772 */ 773 #ifdef SMSC_TURBO 774 if (sc->sc_udev->speed == USB_SPEED_HIGH) 775 burst_cap = 37; 776 else 777 burst_cap = 128; 778 #else 779 burst_cap = 0; 780 #endif 781 782 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap); 783 784 /* Set the default bulk in delay (magic value from Linux driver) */ 785 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000); 786 787 788 789 /* 790 * Initialise the RX interface 791 */ 792 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) { 793 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", 794 err); 795 goto init_failed; 796 } 797 798 /* 799 * The following setings are used for 'turbo mode', a.k.a multiple 800 * frames per Rx transaction (again info taken form Linux driver). 801 */ 802 #ifdef SMSC_TURBO 803 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE); 804 #endif 805 806 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 807 808 /* Clear the status register ? */ 809 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff); 810 811 /* Read and display the revision register */ 812 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) { 813 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err); 814 goto init_failed; 815 } 816 817 /* GPIO/LED setup */ 818 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED | 819 SMSC_LED_GPIO_CFG_FDX_LED; 820 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val); 821 822 /* 823 * Initialise the TX interface 824 */ 825 smsc_write_reg(sc, SMSC_FLOW, 0); 826 827 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT); 828 829 /* Read the current MAC configuration */ 830 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) { 831 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err); 832 goto init_failed; 833 } 834 835 /* Vlan */ 836 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN); 837 838 /* 839 * Start TX 840 */ 841 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN; 842 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 843 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON); 844 845 /* 846 * Start RX 847 */ 848 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN; 849 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 850 851 return (0); 852 853 init_failed: 854 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err); 855 return (err); 856 } 857 858 int 859 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 860 { 861 struct smsc_softc *sc = ifp->if_softc; 862 struct ifreq *ifr = (struct ifreq *)data; 863 int s, error = 0; 864 865 s = splnet(); 866 867 switch(cmd) { 868 case SIOCSIFADDR: 869 ifp->if_flags |= IFF_UP; 870 if (!(ifp->if_flags & IFF_RUNNING)) 871 smsc_init(sc); 872 break; 873 874 case SIOCSIFFLAGS: 875 if (ifp->if_flags & IFF_UP) { 876 if (ifp->if_flags & IFF_RUNNING) 877 error = ENETRESET; 878 else 879 smsc_init(sc); 880 } else { 881 if (ifp->if_flags & IFF_RUNNING) 882 smsc_stop(sc); 883 } 884 break; 885 886 case SIOCGIFMEDIA: 887 case SIOCSIFMEDIA: 888 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd); 889 break; 890 891 default: 892 error = ether_ioctl(ifp, &sc->sc_ac, cmd, data); 893 } 894 895 if (error == ENETRESET) { 896 if (ifp->if_flags & IFF_RUNNING) 897 smsc_iff(sc); 898 error = 0; 899 } 900 901 splx(s); 902 return(error); 903 } 904 905 int 906 smsc_match(struct device *parent, void *match, void *aux) 907 { 908 struct usb_attach_arg *uaa = aux; 909 910 if (uaa->iface == NULL || uaa->configno != 1) 911 return UMATCH_NONE; 912 913 return (usb_lookup(smsc_devs, uaa->vendor, uaa->product) != NULL) ? 914 UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE; 915 } 916 917 void 918 smsc_attach(struct device *parent, struct device *self, void *aux) 919 { 920 struct smsc_softc *sc = (struct smsc_softc *)self; 921 struct usb_attach_arg *uaa = aux; 922 usb_interface_descriptor_t *id; 923 usb_endpoint_descriptor_t *ed; 924 struct mii_data *mii; 925 struct ifnet *ifp; 926 uint32_t mac_h, mac_l; 927 int s, i; 928 929 sc->sc_udev = uaa->device; 930 sc->sc_iface = uaa->iface; 931 932 /* Setup the endpoints for the SMSC LAN95xx device(s) */ 933 usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc, 934 USB_TASK_TYPE_GENERIC); 935 rw_init(&sc->sc_mii_lock, "smscmii"); 936 usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc, 937 USB_TASK_TYPE_GENERIC); 938 939 id = usbd_get_interface_descriptor(sc->sc_iface); 940 941 if (sc->sc_udev->speed >= USB_SPEED_HIGH) 942 sc->sc_bufsz = SMSC_MAX_BUFSZ; 943 else 944 sc->sc_bufsz = SMSC_MIN_BUFSZ; 945 946 /* Find endpoints. */ 947 for (i = 0; i < id->bNumEndpoints; i++) { 948 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 949 if (!ed) { 950 printf("%s: couldn't get ep %d\n", 951 sc->sc_dev.dv_xname, i); 952 return; 953 } 954 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 955 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 956 sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress; 957 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 958 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 959 sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress; 960 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 961 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 962 sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress; 963 } 964 } 965 966 s = splnet(); 967 968 ifp = &sc->sc_ac.ac_if; 969 ifp->if_softc = sc; 970 strlcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 971 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 972 ifp->if_ioctl = smsc_ioctl; 973 ifp->if_start = smsc_start; 974 ifp->if_capabilities = IFCAP_VLAN_MTU; 975 976 /* Setup some of the basics */ 977 sc->sc_phyno = 1; 978 979 /* 980 * Attempt to get the mac address, if an EEPROM is not attached this 981 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC 982 * address based on urandom. 983 */ 984 memset(sc->sc_ac.ac_enaddr, 0xff, ETHER_ADDR_LEN); 985 986 /* Check if there is already a MAC address in the register */ 987 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) && 988 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) { 989 sc->sc_ac.ac_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); 990 sc->sc_ac.ac_enaddr[4] = (uint8_t)((mac_h) & 0xff); 991 sc->sc_ac.ac_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); 992 sc->sc_ac.ac_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); 993 sc->sc_ac.ac_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); 994 sc->sc_ac.ac_enaddr[0] = (uint8_t)((mac_l) & 0xff); 995 } 996 997 printf("%s: address %s\n", sc->sc_dev.dv_xname, 998 ether_sprintf(sc->sc_ac.ac_enaddr)); 999 1000 /* Initialise the chip for the first time */ 1001 smsc_chip_init(sc); 1002 1003 /* Initialize MII/media info. */ 1004 mii = &sc->sc_mii; 1005 mii->mii_ifp = ifp; 1006 mii->mii_readreg = smsc_miibus_readreg; 1007 mii->mii_writereg = smsc_miibus_writereg; 1008 mii->mii_statchg = smsc_miibus_statchg; 1009 mii->mii_flags = MIIF_AUTOTSLEEP; 1010 1011 ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts); 1012 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 1013 1014 if (LIST_FIRST(&mii->mii_phys) == NULL) { 1015 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 1016 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 1017 } else 1018 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 1019 1020 if_attach(ifp); 1021 ether_ifattach(ifp); 1022 1023 timeout_set(&sc->sc_stat_ch, smsc_tick, sc); 1024 1025 splx(s); 1026 } 1027 1028 int 1029 smsc_detach(struct device *self, int flags) 1030 { 1031 struct smsc_softc *sc = (struct smsc_softc *)self; 1032 struct ifnet *ifp = &sc->sc_ac.ac_if; 1033 int s; 1034 1035 if (timeout_initialized(&sc->sc_stat_ch)) 1036 timeout_del(&sc->sc_stat_ch); 1037 1038 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) 1039 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]); 1040 if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) 1041 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]); 1042 if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) 1043 usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]); 1044 1045 /* 1046 * Remove any pending tasks. They cannot be executing because they run 1047 * in the same thread as detach. 1048 */ 1049 usb_rem_task(sc->sc_udev, &sc->sc_tick_task); 1050 usb_rem_task(sc->sc_udev, &sc->sc_stop_task); 1051 1052 s = splusb(); 1053 1054 if (--sc->sc_refcnt >= 0) { 1055 /* Wait for processes to go away */ 1056 usb_detach_wait(&sc->sc_dev); 1057 } 1058 1059 if (ifp->if_flags & IFF_RUNNING) 1060 smsc_stop(sc); 1061 1062 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 1063 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 1064 if (ifp->if_softc != NULL) { 1065 ether_ifdetach(ifp); 1066 if_detach(ifp); 1067 } 1068 1069 #ifdef DIAGNOSTIC 1070 if (sc->sc_ep[SMSC_ENDPT_TX] != NULL || 1071 sc->sc_ep[SMSC_ENDPT_RX] != NULL || 1072 sc->sc_ep[SMSC_ENDPT_INTR] != NULL) 1073 printf("%s: detach has active endpoints\n", 1074 sc->sc_dev.dv_xname); 1075 #endif 1076 1077 if (--sc->sc_refcnt >= 0) { 1078 /* Wait for processes to go away. */ 1079 usb_detach_wait(&sc->sc_dev); 1080 } 1081 splx(s); 1082 1083 return (0); 1084 } 1085 1086 void 1087 smsc_tick_task(void *xsc) 1088 { 1089 int s; 1090 struct smsc_softc *sc = xsc; 1091 struct mii_data *mii; 1092 1093 if (sc == NULL) 1094 return; 1095 1096 if (usbd_is_dying(sc->sc_udev)) 1097 return; 1098 mii = &sc->sc_mii; 1099 if (mii == NULL) 1100 return; 1101 1102 s = splnet(); 1103 1104 mii_tick(mii); 1105 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) 1106 smsc_miibus_statchg(&sc->sc_dev); 1107 timeout_add_sec(&sc->sc_stat_ch, 1); 1108 1109 splx(s); 1110 } 1111 1112 void 1113 smsc_lock_mii(struct smsc_softc *sc) 1114 { 1115 sc->sc_refcnt++; 1116 rw_enter_write(&sc->sc_mii_lock); 1117 } 1118 1119 void 1120 smsc_unlock_mii(struct smsc_softc *sc) 1121 { 1122 rw_exit_write(&sc->sc_mii_lock); 1123 if (--sc->sc_refcnt < 0) 1124 usb_detach_wakeup(&sc->sc_dev); 1125 } 1126 1127 void 1128 smsc_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1129 { 1130 struct smsc_chain *c = (struct smsc_chain *)priv; 1131 struct smsc_softc *sc = c->sc_sc; 1132 struct ifnet *ifp = &sc->sc_ac.ac_if; 1133 u_char *buf = c->sc_buf; 1134 uint32_t total_len; 1135 uint16_t pktlen = 0; 1136 struct mbuf_list ml = MBUF_LIST_INITIALIZER(); 1137 struct mbuf *m; 1138 int s; 1139 uint32_t rxhdr; 1140 1141 if (usbd_is_dying(sc->sc_udev)) 1142 return; 1143 1144 if (!(ifp->if_flags & IFF_RUNNING)) 1145 return; 1146 1147 if (status != USBD_NORMAL_COMPLETION) { 1148 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1149 return; 1150 if (usbd_ratecheck(&sc->sc_rx_notice)) { 1151 printf("%s: usb errors on rx: %s\n", 1152 sc->sc_dev.dv_xname, usbd_errstr(status)); 1153 } 1154 if (status == USBD_STALLED) 1155 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]); 1156 goto done; 1157 } 1158 1159 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 1160 smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len); 1161 1162 do { 1163 if (total_len < sizeof(rxhdr)) { 1164 smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %d\n", 1165 total_len, sizeof(rxhdr)); 1166 ifp->if_ierrors++; 1167 goto done; 1168 } 1169 1170 buf += pktlen; 1171 1172 memcpy(&rxhdr, buf, sizeof(rxhdr)); 1173 rxhdr = letoh32(rxhdr); 1174 total_len -= sizeof(rxhdr); 1175 1176 if (rxhdr & SMSC_RX_STAT_ERROR) { 1177 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr); 1178 ifp->if_ierrors++; 1179 goto done; 1180 } 1181 1182 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr); 1183 smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr " 1184 "0x%08x\n", total_len, pktlen, rxhdr); 1185 if (pktlen > total_len) { 1186 smsc_dbg_printf(sc, "pktlen %d > total_len %d\n", 1187 pktlen, total_len); 1188 ifp->if_ierrors++; 1189 goto done; 1190 } 1191 1192 buf += sizeof(rxhdr); 1193 1194 if (total_len < pktlen) 1195 total_len = 0; 1196 else 1197 total_len -= pktlen; 1198 1199 m = m_devget(buf, pktlen, ETHER_ALIGN); 1200 if (m == NULL) { 1201 smsc_dbg_printf(sc, "m_devget returned NULL\n"); 1202 ifp->if_ierrors++; 1203 goto done; 1204 } 1205 1206 ml_enqueue(&ml, m); 1207 } while (total_len > 0); 1208 1209 done: 1210 s = splnet(); 1211 if_input(ifp, &ml); 1212 splx(s); 1213 memset(c->sc_buf, 0, sc->sc_bufsz); 1214 1215 /* Setup new transfer. */ 1216 usbd_setup_xfer(xfer, sc->sc_ep[SMSC_ENDPT_RX], 1217 c, c->sc_buf, sc->sc_bufsz, 1218 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1219 USBD_NO_TIMEOUT, smsc_rxeof); 1220 usbd_transfer(xfer); 1221 1222 return; 1223 } 1224 1225 void 1226 smsc_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1227 { 1228 struct smsc_softc *sc; 1229 struct smsc_chain *c; 1230 struct ifnet *ifp; 1231 int s; 1232 1233 c = priv; 1234 sc = c->sc_sc; 1235 ifp = &sc->sc_ac.ac_if; 1236 1237 if (usbd_is_dying(sc->sc_udev)) 1238 return; 1239 1240 s = splnet(); 1241 1242 if (status != USBD_NORMAL_COMPLETION) { 1243 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1244 splx(s); 1245 return; 1246 } 1247 ifp->if_oerrors++; 1248 printf("%s: usb error on tx: %s\n", sc->sc_dev.dv_xname, 1249 usbd_errstr(status)); 1250 if (status == USBD_STALLED) 1251 usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]); 1252 splx(s); 1253 return; 1254 } 1255 1256 ifp->if_timer = 0; 1257 ifq_clr_oactive(&ifp->if_snd); 1258 1259 m_freem(c->sc_mbuf); 1260 c->sc_mbuf = NULL; 1261 1262 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1263 smsc_start(ifp); 1264 1265 ifp->if_opackets++; 1266 splx(s); 1267 } 1268 1269 int 1270 smsc_tx_list_init(struct smsc_softc *sc) 1271 { 1272 struct smsc_cdata *cd; 1273 struct smsc_chain *c; 1274 int i; 1275 1276 cd = &sc->sc_cdata; 1277 for (i = 0; i < SMSC_TX_LIST_CNT; i++) { 1278 c = &cd->tx_chain[i]; 1279 c->sc_sc = sc; 1280 c->sc_idx = i; 1281 c->sc_mbuf = NULL; 1282 if (c->sc_xfer == NULL) { 1283 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev); 1284 if (c->sc_xfer == NULL) 1285 return (ENOBUFS); 1286 c->sc_buf = usbd_alloc_buffer(c->sc_xfer, 1287 sc->sc_bufsz); 1288 if (c->sc_buf == NULL) { 1289 usbd_free_xfer(c->sc_xfer); 1290 return (ENOBUFS); 1291 } 1292 } 1293 } 1294 1295 return (0); 1296 } 1297 1298 int 1299 smsc_rx_list_init(struct smsc_softc *sc) 1300 { 1301 struct smsc_cdata *cd; 1302 struct smsc_chain *c; 1303 int i; 1304 1305 cd = &sc->sc_cdata; 1306 for (i = 0; i < SMSC_RX_LIST_CNT; i++) { 1307 c = &cd->rx_chain[i]; 1308 c->sc_sc = sc; 1309 c->sc_idx = i; 1310 c->sc_mbuf = NULL; 1311 if (c->sc_xfer == NULL) { 1312 c->sc_xfer = usbd_alloc_xfer(sc->sc_udev); 1313 if (c->sc_xfer == NULL) 1314 return (ENOBUFS); 1315 c->sc_buf = usbd_alloc_buffer(c->sc_xfer, 1316 sc->sc_bufsz); 1317 if (c->sc_buf == NULL) { 1318 usbd_free_xfer(c->sc_xfer); 1319 return (ENOBUFS); 1320 } 1321 } 1322 } 1323 1324 return (0); 1325 } 1326 1327 int 1328 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx) 1329 { 1330 struct smsc_chain *c; 1331 usbd_status err; 1332 uint32_t txhdr; 1333 uint32_t frm_len = 0; 1334 1335 c = &sc->sc_cdata.tx_chain[idx]; 1336 1337 /* 1338 * Each frame is prefixed with two 32-bit values describing the 1339 * length of the packet and buffer. 1340 */ 1341 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) | 1342 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG; 1343 txhdr = htole32(txhdr); 1344 memcpy(c->sc_buf, &txhdr, sizeof(txhdr)); 1345 1346 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len); 1347 txhdr = htole32(txhdr); 1348 memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr)); 1349 1350 frm_len += 8; 1351 1352 /* Next copy in the actual packet */ 1353 m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len); 1354 frm_len += m->m_pkthdr.len; 1355 1356 c->sc_mbuf = m; 1357 1358 usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_TX], 1359 c, c->sc_buf, frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 1360 10000, smsc_txeof); 1361 1362 err = usbd_transfer(c->sc_xfer); 1363 if (err != USBD_IN_PROGRESS) { 1364 smsc_stop(sc); 1365 return (EIO); 1366 } 1367 1368 sc->sc_cdata.tx_cnt++; 1369 1370 return (0); 1371 } 1372