1 /*- 2 * Copyright (c) 2012 3 * Ben Gray <bgray@freebsd.org>. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* 31 * SMSC LAN9xxx devices (http://www.smsc.com/) 32 * 33 * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that 34 * support USB 2.0 and 10/100 Mbps Ethernet. 35 * 36 * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter. 37 * The driver only covers the Ethernet part, the standard USB hub driver 38 * supports the hub part. 39 * 40 * This driver is closely modelled on the Linux driver written and copyrighted 41 * by SMSC. 42 * 43 * 44 * 45 * 46 * H/W TCP & UDP Checksum Offloading 47 * --------------------------------- 48 * The chip supports both tx and rx offloading of UDP & TCP checksums, this 49 * feature can be dynamically enabled/disabled. 50 * 51 * RX checksuming is performed across bytes after the IPv4 header to the end of 52 * the Ethernet frame, this means if the frame is padded with non-zero values 53 * the H/W checksum will be incorrect, however the rx code compensates for this. 54 * 55 * TX checksuming is more complicated, the device requires a special header to 56 * be prefixed onto the start of the frame which indicates the start and end 57 * positions of the UDP or TCP frame. This requires the driver to manually 58 * go through the packet data and decode the headers prior to sending. 59 * On Linux they generally provide cues to the location of the csum and the 60 * area to calculate it over, on FreeBSD we seem to have to do it all ourselves, 61 * hence this is not as optimal and therefore h/w tX checksum is currently not 62 * implemented. 63 * 64 */ 65 #include <sys/stdint.h> 66 #include <sys/stddef.h> 67 #include <sys/param.h> 68 #include <sys/queue.h> 69 #include <sys/types.h> 70 #include <sys/systm.h> 71 #include <sys/kernel.h> 72 #include <sys/bus.h> 73 #include <sys/module.h> 74 #include <sys/lock.h> 75 #include <sys/mutex.h> 76 #include <sys/condvar.h> 77 #include <sys/socket.h> 78 #include <sys/sysctl.h> 79 #include <sys/sx.h> 80 #include <sys/unistd.h> 81 #include <sys/callout.h> 82 #include <sys/malloc.h> 83 #include <sys/priv.h> 84 #include <sys/random.h> 85 86 #include <net/if.h> 87 #include <net/if_var.h> 88 89 #include "opt_platform.h" 90 91 #ifdef FDT 92 #include <dev/fdt/fdt_common.h> 93 #include <dev/ofw/ofw_bus.h> 94 #include <dev/ofw/ofw_bus_subr.h> 95 #endif 96 97 #include <dev/usb/usb.h> 98 #include <dev/usb/usbdi.h> 99 #include <dev/usb/usbdi_util.h> 100 #include "usbdevs.h" 101 102 #define USB_DEBUG_VAR smsc_debug 103 #include <dev/usb/usb_debug.h> 104 #include <dev/usb/usb_process.h> 105 106 #include <dev/usb/net/usb_ethernet.h> 107 108 #include <dev/usb/net/if_smscreg.h> 109 110 #ifdef USB_DEBUG 111 static int smsc_debug = 0; 112 113 SYSCTL_NODE(_hw_usb, OID_AUTO, smsc, CTLFLAG_RW, 0, "USB smsc"); 114 SYSCTL_INT(_hw_usb_smsc, OID_AUTO, debug, CTLFLAG_RW, &smsc_debug, 0, 115 "Debug level"); 116 #endif 117 118 /* 119 * Various supported device vendors/products. 120 */ 121 static const struct usb_device_id smsc_devs[] = { 122 #define SMSC_DEV(p,i) { USB_VPI(USB_VENDOR_SMC2, USB_PRODUCT_SMC2_##p, i) } 123 SMSC_DEV(LAN9514_ETH, 0), 124 #undef SMSC_DEV 125 }; 126 127 128 #ifdef USB_DEBUG 129 #define smsc_dbg_printf(sc, fmt, args...) \ 130 do { \ 131 if (smsc_debug > 0) \ 132 device_printf((sc)->sc_ue.ue_dev, "debug: " fmt, ##args); \ 133 } while(0) 134 #else 135 #define smsc_dbg_printf(sc, fmt, args...) 136 #endif 137 138 #define smsc_warn_printf(sc, fmt, args...) \ 139 device_printf((sc)->sc_ue.ue_dev, "warning: " fmt, ##args) 140 141 #define smsc_err_printf(sc, fmt, args...) \ 142 device_printf((sc)->sc_ue.ue_dev, "error: " fmt, ##args) 143 144 145 #define ETHER_IS_ZERO(addr) \ 146 (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) 147 148 #define ETHER_IS_VALID(addr) \ 149 (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr)) 150 151 static device_probe_t smsc_probe; 152 static device_attach_t smsc_attach; 153 static device_detach_t smsc_detach; 154 155 static usb_callback_t smsc_bulk_read_callback; 156 static usb_callback_t smsc_bulk_write_callback; 157 158 static miibus_readreg_t smsc_miibus_readreg; 159 static miibus_writereg_t smsc_miibus_writereg; 160 static miibus_statchg_t smsc_miibus_statchg; 161 162 #if __FreeBSD_version > 1000000 163 static int smsc_attach_post_sub(struct usb_ether *ue); 164 #endif 165 static uether_fn_t smsc_attach_post; 166 static uether_fn_t smsc_init; 167 static uether_fn_t smsc_stop; 168 static uether_fn_t smsc_start; 169 static uether_fn_t smsc_tick; 170 static uether_fn_t smsc_setmulti; 171 static uether_fn_t smsc_setpromisc; 172 173 static int smsc_ifmedia_upd(struct ifnet *); 174 static void smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *); 175 176 static int smsc_chip_init(struct smsc_softc *sc); 177 static int smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 178 179 static const struct usb_config smsc_config[SMSC_N_TRANSFER] = { 180 181 [SMSC_BULK_DT_WR] = { 182 .type = UE_BULK, 183 .endpoint = UE_ADDR_ANY, 184 .direction = UE_DIR_OUT, 185 .frames = 16, 186 .bufsize = 16 * (MCLBYTES + 16), 187 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 188 .callback = smsc_bulk_write_callback, 189 .timeout = 10000, /* 10 seconds */ 190 }, 191 192 [SMSC_BULK_DT_RD] = { 193 .type = UE_BULK, 194 .endpoint = UE_ADDR_ANY, 195 .direction = UE_DIR_IN, 196 .bufsize = 20480, /* bytes */ 197 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 198 .callback = smsc_bulk_read_callback, 199 .timeout = 0, /* no timeout */ 200 }, 201 202 /* The SMSC chip supports an interrupt endpoints, however they aren't 203 * needed as we poll on the MII status. 204 */ 205 }; 206 207 static const struct usb_ether_methods smsc_ue_methods = { 208 .ue_attach_post = smsc_attach_post, 209 #if __FreeBSD_version > 1000000 210 .ue_attach_post_sub = smsc_attach_post_sub, 211 #endif 212 .ue_start = smsc_start, 213 .ue_ioctl = smsc_ioctl, 214 .ue_init = smsc_init, 215 .ue_stop = smsc_stop, 216 .ue_tick = smsc_tick, 217 .ue_setmulti = smsc_setmulti, 218 .ue_setpromisc = smsc_setpromisc, 219 .ue_mii_upd = smsc_ifmedia_upd, 220 .ue_mii_sts = smsc_ifmedia_sts, 221 }; 222 223 /** 224 * smsc_read_reg - Reads a 32-bit register on the device 225 * @sc: driver soft context 226 * @off: offset of the register 227 * @data: pointer a value that will be populated with the register value 228 * 229 * LOCKING: 230 * The device lock must be held before calling this function. 231 * 232 * RETURNS: 233 * 0 on success, a USB_ERR_?? error code on failure. 234 */ 235 static int 236 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data) 237 { 238 struct usb_device_request req; 239 uint32_t buf; 240 usb_error_t err; 241 242 SMSC_LOCK_ASSERT(sc, MA_OWNED); 243 244 req.bmRequestType = UT_READ_VENDOR_DEVICE; 245 req.bRequest = SMSC_UR_READ_REG; 246 USETW(req.wValue, 0); 247 USETW(req.wIndex, off); 248 USETW(req.wLength, 4); 249 250 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000); 251 if (err != 0) 252 smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off); 253 254 *data = le32toh(buf); 255 256 return (err); 257 } 258 259 /** 260 * smsc_write_reg - Writes a 32-bit register on the device 261 * @sc: driver soft context 262 * @off: offset of the register 263 * @data: the 32-bit value to write into the register 264 * 265 * LOCKING: 266 * The device lock must be held before calling this function. 267 * 268 * RETURNS: 269 * 0 on success, a USB_ERR_?? error code on failure. 270 */ 271 static int 272 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data) 273 { 274 struct usb_device_request req; 275 uint32_t buf; 276 usb_error_t err; 277 278 SMSC_LOCK_ASSERT(sc, MA_OWNED); 279 280 buf = htole32(data); 281 282 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 283 req.bRequest = SMSC_UR_WRITE_REG; 284 USETW(req.wValue, 0); 285 USETW(req.wIndex, off); 286 USETW(req.wLength, 4); 287 288 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000); 289 if (err != 0) 290 smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off); 291 292 return (err); 293 } 294 295 /** 296 * smsc_wait_for_bits - Polls on a register value until bits are cleared 297 * @sc: soft context 298 * @reg: offset of the register 299 * @bits: if the bits are clear the function returns 300 * 301 * LOCKING: 302 * The device lock must be held before calling this function. 303 * 304 * RETURNS: 305 * 0 on success, or a USB_ERR_?? error code on failure. 306 */ 307 static int 308 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits) 309 { 310 usb_ticks_t start_ticks; 311 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000); 312 uint32_t val; 313 int err; 314 315 SMSC_LOCK_ASSERT(sc, MA_OWNED); 316 317 start_ticks = (usb_ticks_t)ticks; 318 do { 319 if ((err = smsc_read_reg(sc, reg, &val)) != 0) 320 return (err); 321 if (!(val & bits)) 322 return (0); 323 324 uether_pause(&sc->sc_ue, hz / 100); 325 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks); 326 327 return (USB_ERR_TIMEOUT); 328 } 329 330 /** 331 * smsc_eeprom_read - Reads the attached EEPROM 332 * @sc: soft context 333 * @off: the eeprom address offset 334 * @buf: stores the bytes 335 * @buflen: the number of bytes to read 336 * 337 * Simply reads bytes from an attached eeprom. 338 * 339 * LOCKING: 340 * The function takes and releases the device lock if it is not already held. 341 * 342 * RETURNS: 343 * 0 on success, or a USB_ERR_?? error code on failure. 344 */ 345 static int 346 smsc_eeprom_read(struct smsc_softc *sc, uint16_t off, uint8_t *buf, uint16_t buflen) 347 { 348 usb_ticks_t start_ticks; 349 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000); 350 int err; 351 int locked; 352 uint32_t val; 353 uint16_t i; 354 355 locked = mtx_owned(&sc->sc_mtx); 356 if (!locked) 357 SMSC_LOCK(sc); 358 359 err = smsc_wait_for_bits(sc, SMSC_EEPROM_CMD, SMSC_EEPROM_CMD_BUSY); 360 if (err != 0) { 361 smsc_warn_printf(sc, "eeprom busy, failed to read data\n"); 362 goto done; 363 } 364 365 /* start reading the bytes, one at a time */ 366 for (i = 0; i < buflen; i++) { 367 368 val = SMSC_EEPROM_CMD_BUSY | (SMSC_EEPROM_CMD_ADDR_MASK & (off + i)); 369 if ((err = smsc_write_reg(sc, SMSC_EEPROM_CMD, val)) != 0) 370 goto done; 371 372 start_ticks = (usb_ticks_t)ticks; 373 do { 374 if ((err = smsc_read_reg(sc, SMSC_EEPROM_CMD, &val)) != 0) 375 goto done; 376 if (!(val & SMSC_EEPROM_CMD_BUSY) || (val & SMSC_EEPROM_CMD_TIMEOUT)) 377 break; 378 379 uether_pause(&sc->sc_ue, hz / 100); 380 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks); 381 382 if (val & (SMSC_EEPROM_CMD_BUSY | SMSC_EEPROM_CMD_TIMEOUT)) { 383 smsc_warn_printf(sc, "eeprom command failed\n"); 384 err = USB_ERR_IOERROR; 385 break; 386 } 387 388 if ((err = smsc_read_reg(sc, SMSC_EEPROM_DATA, &val)) != 0) 389 goto done; 390 391 buf[i] = (val & 0xff); 392 } 393 394 done: 395 if (!locked) 396 SMSC_UNLOCK(sc); 397 398 return (err); 399 } 400 401 /** 402 * smsc_miibus_readreg - Reads a MII/MDIO register 403 * @dev: usb ether device 404 * @phy: the number of phy reading from 405 * @reg: the register address 406 * 407 * Attempts to read a phy register over the MII bus. 408 * 409 * LOCKING: 410 * Takes and releases the device mutex lock if not already held. 411 * 412 * RETURNS: 413 * Returns the 16-bits read from the MII register, if this function fails 0 414 * is returned. 415 */ 416 static int 417 smsc_miibus_readreg(device_t dev, int phy, int reg) 418 { 419 struct smsc_softc *sc = device_get_softc(dev); 420 int locked; 421 uint32_t addr; 422 uint32_t val = 0; 423 424 locked = mtx_owned(&sc->sc_mtx); 425 if (!locked) 426 SMSC_LOCK(sc); 427 428 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { 429 smsc_warn_printf(sc, "MII is busy\n"); 430 goto done; 431 } 432 433 addr = (phy << 11) | (reg << 6) | SMSC_MII_READ; 434 smsc_write_reg(sc, SMSC_MII_ADDR, addr); 435 436 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) 437 smsc_warn_printf(sc, "MII read timeout\n"); 438 439 smsc_read_reg(sc, SMSC_MII_DATA, &val); 440 val = le32toh(val); 441 442 done: 443 if (!locked) 444 SMSC_UNLOCK(sc); 445 446 return (val & 0xFFFF); 447 } 448 449 /** 450 * smsc_miibus_writereg - Writes a MII/MDIO register 451 * @dev: usb ether device 452 * @phy: the number of phy writing to 453 * @reg: the register address 454 * @val: the value to write 455 * 456 * Attempts to write a phy register over the MII bus. 457 * 458 * LOCKING: 459 * Takes and releases the device mutex lock if not already held. 460 * 461 * RETURNS: 462 * Always returns 0 regardless of success or failure. 463 */ 464 static int 465 smsc_miibus_writereg(device_t dev, int phy, int reg, int val) 466 { 467 struct smsc_softc *sc = device_get_softc(dev); 468 int locked; 469 uint32_t addr; 470 471 if (sc->sc_phyno != phy) 472 return (0); 473 474 locked = mtx_owned(&sc->sc_mtx); 475 if (!locked) 476 SMSC_LOCK(sc); 477 478 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) { 479 smsc_warn_printf(sc, "MII is busy\n"); 480 goto done; 481 } 482 483 val = htole32(val); 484 smsc_write_reg(sc, SMSC_MII_DATA, val); 485 486 addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE; 487 smsc_write_reg(sc, SMSC_MII_ADDR, addr); 488 489 if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) 490 smsc_warn_printf(sc, "MII write timeout\n"); 491 492 done: 493 if (!locked) 494 SMSC_UNLOCK(sc); 495 return (0); 496 } 497 498 499 500 /** 501 * smsc_miibus_statchg - Called to detect phy status change 502 * @dev: usb ether device 503 * 504 * This function is called periodically by the system to poll for status 505 * changes of the link. 506 * 507 * LOCKING: 508 * Takes and releases the device mutex lock if not already held. 509 */ 510 static void 511 smsc_miibus_statchg(device_t dev) 512 { 513 struct smsc_softc *sc = device_get_softc(dev); 514 struct mii_data *mii = uether_getmii(&sc->sc_ue); 515 struct ifnet *ifp; 516 int locked; 517 int err; 518 uint32_t flow; 519 uint32_t afc_cfg; 520 521 locked = mtx_owned(&sc->sc_mtx); 522 if (!locked) 523 SMSC_LOCK(sc); 524 525 ifp = uether_getifp(&sc->sc_ue); 526 if (mii == NULL || ifp == NULL || 527 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 528 goto done; 529 530 /* Use the MII status to determine link status */ 531 sc->sc_flags &= ~SMSC_FLAG_LINK; 532 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 533 (IFM_ACTIVE | IFM_AVALID)) { 534 switch (IFM_SUBTYPE(mii->mii_media_active)) { 535 case IFM_10_T: 536 case IFM_100_TX: 537 sc->sc_flags |= SMSC_FLAG_LINK; 538 break; 539 case IFM_1000_T: 540 /* Gigabit ethernet not supported by chipset */ 541 break; 542 default: 543 break; 544 } 545 } 546 547 /* Lost link, do nothing. */ 548 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) { 549 smsc_dbg_printf(sc, "link flag not set\n"); 550 goto done; 551 } 552 553 err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg); 554 if (err) { 555 smsc_warn_printf(sc, "failed to read initial AFC_CFG, error %d\n", err); 556 goto done; 557 } 558 559 /* Enable/disable full duplex operation and TX/RX pause */ 560 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) { 561 smsc_dbg_printf(sc, "full duplex operation\n"); 562 sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN; 563 sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX; 564 565 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0) 566 flow = 0xffff0002; 567 else 568 flow = 0; 569 570 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0) 571 afc_cfg |= 0xf; 572 else 573 afc_cfg &= ~0xf; 574 575 } else { 576 smsc_dbg_printf(sc, "half duplex operation\n"); 577 sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX; 578 sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN; 579 580 flow = 0; 581 afc_cfg |= 0xf; 582 } 583 584 err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 585 err += smsc_write_reg(sc, SMSC_FLOW, flow); 586 err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg); 587 if (err) 588 smsc_warn_printf(sc, "media change failed, error %d\n", err); 589 590 done: 591 if (!locked) 592 SMSC_UNLOCK(sc); 593 } 594 595 /** 596 * smsc_ifmedia_upd - Set media options 597 * @ifp: interface pointer 598 * 599 * Basically boilerplate code that simply calls the mii functions to set the 600 * media options. 601 * 602 * LOCKING: 603 * The device lock must be held before this function is called. 604 * 605 * RETURNS: 606 * Returns 0 on success or a negative error code. 607 */ 608 static int 609 smsc_ifmedia_upd(struct ifnet *ifp) 610 { 611 struct smsc_softc *sc = ifp->if_softc; 612 struct mii_data *mii = uether_getmii(&sc->sc_ue); 613 struct mii_softc *miisc; 614 int err; 615 616 SMSC_LOCK_ASSERT(sc, MA_OWNED); 617 618 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 619 PHY_RESET(miisc); 620 err = mii_mediachg(mii); 621 return (err); 622 } 623 624 /** 625 * smsc_ifmedia_sts - Report current media status 626 * @ifp: inet interface pointer 627 * @ifmr: interface media request 628 * 629 * Basically boilerplate code that simply calls the mii functions to get the 630 * media status. 631 * 632 * LOCKING: 633 * Internally takes and releases the device lock. 634 */ 635 static void 636 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 637 { 638 struct smsc_softc *sc = ifp->if_softc; 639 struct mii_data *mii = uether_getmii(&sc->sc_ue); 640 641 SMSC_LOCK(sc); 642 mii_pollstat(mii); 643 ifmr->ifm_active = mii->mii_media_active; 644 ifmr->ifm_status = mii->mii_media_status; 645 SMSC_UNLOCK(sc); 646 } 647 648 /** 649 * smsc_hash - Calculate the hash of a mac address 650 * @addr: The mac address to calculate the hash on 651 * 652 * This function is used when configuring a range of m'cast mac addresses to 653 * filter on. The hash of the mac address is put in the device's mac hash 654 * table. 655 * 656 * RETURNS: 657 * Returns a value from 0-63 value which is the hash of the mac address. 658 */ 659 static inline uint32_t 660 smsc_hash(uint8_t addr[ETHER_ADDR_LEN]) 661 { 662 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f; 663 } 664 665 /** 666 * smsc_setmulti - Setup multicast 667 * @ue: usb ethernet device context 668 * 669 * Tells the device to either accept frames with a multicast mac address, a 670 * select group of m'cast mac addresses or just the devices mac address. 671 * 672 * LOCKING: 673 * Should be called with the SMSC lock held. 674 */ 675 static void 676 smsc_setmulti(struct usb_ether *ue) 677 { 678 struct smsc_softc *sc = uether_getsc(ue); 679 struct ifnet *ifp = uether_getifp(ue); 680 struct ifmultiaddr *ifma; 681 uint32_t hashtbl[2] = { 0, 0 }; 682 uint32_t hash; 683 684 SMSC_LOCK_ASSERT(sc, MA_OWNED); 685 686 if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) { 687 smsc_dbg_printf(sc, "receive all multicast enabled\n"); 688 sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS; 689 sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT; 690 691 } else { 692 /* Take the lock of the mac address list before hashing each of them */ 693 if_maddr_rlock(ifp); 694 695 if (!TAILQ_EMPTY(&ifp->if_multiaddrs)) { 696 /* We are filtering on a set of address so calculate hashes of each 697 * of the address and set the corresponding bits in the register. 698 */ 699 sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT; 700 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS); 701 702 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 703 if (ifma->ifma_addr->sa_family != AF_LINK) 704 continue; 705 706 hash = smsc_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 707 hashtbl[hash >> 5] |= 1 << (hash & 0x1F); 708 } 709 } else { 710 /* Only receive packets with destination set to our mac address */ 711 sc->sc_mac_csr &= ~(SMSC_MAC_CSR_MCPAS | SMSC_MAC_CSR_HPFILT); 712 } 713 714 if_maddr_runlock(ifp); 715 716 /* Debug */ 717 if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) 718 smsc_dbg_printf(sc, "receive select group of macs\n"); 719 else 720 smsc_dbg_printf(sc, "receive own packets only\n"); 721 } 722 723 /* Write the hash table and mac control registers */ 724 smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]); 725 smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]); 726 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 727 } 728 729 730 /** 731 * smsc_setpromisc - Enables/disables promiscuous mode 732 * @ue: usb ethernet device context 733 * 734 * LOCKING: 735 * Should be called with the SMSC lock held. 736 */ 737 static void 738 smsc_setpromisc(struct usb_ether *ue) 739 { 740 struct smsc_softc *sc = uether_getsc(ue); 741 struct ifnet *ifp = uether_getifp(ue); 742 743 smsc_dbg_printf(sc, "promiscuous mode %sabled\n", 744 (ifp->if_flags & IFF_PROMISC) ? "en" : "dis"); 745 746 SMSC_LOCK_ASSERT(sc, MA_OWNED); 747 748 if (ifp->if_flags & IFF_PROMISC) 749 sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS; 750 else 751 sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS; 752 753 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 754 } 755 756 757 /** 758 * smsc_sethwcsum - Enable or disable H/W UDP and TCP checksumming 759 * @sc: driver soft context 760 * 761 * LOCKING: 762 * Should be called with the SMSC lock held. 763 * 764 * RETURNS: 765 * Returns 0 on success or a negative error code. 766 */ 767 static int smsc_sethwcsum(struct smsc_softc *sc) 768 { 769 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 770 uint32_t val; 771 int err; 772 773 if (!ifp) 774 return (-EIO); 775 776 SMSC_LOCK_ASSERT(sc, MA_OWNED); 777 778 err = smsc_read_reg(sc, SMSC_COE_CTRL, &val); 779 if (err != 0) { 780 smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", err); 781 return (err); 782 } 783 784 /* Enable/disable the Rx checksum */ 785 if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_RXCSUM) 786 val |= SMSC_COE_CTRL_RX_EN; 787 else 788 val &= ~SMSC_COE_CTRL_RX_EN; 789 790 /* Enable/disable the Tx checksum (currently not supported) */ 791 if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_TXCSUM) 792 val |= SMSC_COE_CTRL_TX_EN; 793 else 794 val &= ~SMSC_COE_CTRL_TX_EN; 795 796 err = smsc_write_reg(sc, SMSC_COE_CTRL, val); 797 if (err != 0) { 798 smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", err); 799 return (err); 800 } 801 802 return (0); 803 } 804 805 806 /** 807 * smsc_setmacaddress - Sets the mac address in the device 808 * @sc: driver soft context 809 * @addr: pointer to array contain at least 6 bytes of the mac 810 * 811 * Writes the MAC address into the device, usually the MAC is programmed with 812 * values from the EEPROM. 813 * 814 * LOCKING: 815 * Should be called with the SMSC lock held. 816 * 817 * RETURNS: 818 * Returns 0 on success or a negative error code. 819 */ 820 static int 821 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr) 822 { 823 int err; 824 uint32_t val; 825 826 smsc_dbg_printf(sc, "setting mac address to %02x:%02x:%02x:%02x:%02x:%02x\n", 827 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 828 829 SMSC_LOCK_ASSERT(sc, MA_OWNED); 830 831 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0]; 832 if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0) 833 goto done; 834 835 val = (addr[5] << 8) | addr[4]; 836 err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val); 837 838 done: 839 return (err); 840 } 841 842 /** 843 * smsc_reset - Reset the SMSC chip 844 * @sc: device soft context 845 * 846 * LOCKING: 847 * Should be called with the SMSC lock held. 848 */ 849 static void 850 smsc_reset(struct smsc_softc *sc) 851 { 852 struct usb_config_descriptor *cd; 853 usb_error_t err; 854 855 cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev); 856 857 err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx, 858 cd->bConfigurationValue); 859 if (err) 860 smsc_warn_printf(sc, "reset failed (ignored)\n"); 861 862 /* Wait a little while for the chip to get its brains in order. */ 863 uether_pause(&sc->sc_ue, hz / 100); 864 865 /* Reinitialize controller to achieve full reset. */ 866 smsc_chip_init(sc); 867 } 868 869 870 /** 871 * smsc_init - Initialises the LAN95xx chip 872 * @ue: USB ether interface 873 * 874 * Called when the interface is brought up (i.e. ifconfig ue0 up), this 875 * initialise the interface and the rx/tx pipes. 876 * 877 * LOCKING: 878 * Should be called with the SMSC lock held. 879 */ 880 static void 881 smsc_init(struct usb_ether *ue) 882 { 883 struct smsc_softc *sc = uether_getsc(ue); 884 struct ifnet *ifp = uether_getifp(ue); 885 886 SMSC_LOCK_ASSERT(sc, MA_OWNED); 887 888 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 889 return; 890 891 /* Cancel pending I/O */ 892 smsc_stop(ue); 893 894 #if __FreeBSD_version <= 1000000 895 /* On earlier versions this was the first place we could tell the system 896 * that we supported h/w csuming, however this is only called after the 897 * the interface has been brought up - not ideal. 898 */ 899 if (!(ifp->if_capabilities & IFCAP_RXCSUM)) { 900 ifp->if_capabilities |= IFCAP_RXCSUM; 901 ifp->if_capenable |= IFCAP_RXCSUM; 902 ifp->if_hwassist = 0; 903 } 904 905 /* TX checksuming is disabled for now 906 ifp->if_capabilities |= IFCAP_TXCSUM; 907 ifp->if_capenable |= IFCAP_TXCSUM; 908 ifp->if_hwassist = CSUM_TCP | CSUM_UDP; 909 */ 910 #endif 911 912 /* Reset the ethernet interface. */ 913 smsc_reset(sc); 914 915 /* Load the multicast filter. */ 916 smsc_setmulti(ue); 917 918 /* TCP/UDP checksum offload engines. */ 919 smsc_sethwcsum(sc); 920 921 usbd_xfer_set_stall(sc->sc_xfer[SMSC_BULK_DT_WR]); 922 923 /* Indicate we are up and running. */ 924 ifp->if_drv_flags |= IFF_DRV_RUNNING; 925 926 /* Switch to selected media. */ 927 smsc_ifmedia_upd(ifp); 928 smsc_start(ue); 929 } 930 931 /** 932 * smsc_bulk_read_callback - Read callback used to process the USB URB 933 * @xfer: the USB transfer 934 * @error: 935 * 936 * Reads the URB data which can contain one or more ethernet frames, the 937 * frames are copyed into a mbuf and given to the system. 938 * 939 * LOCKING: 940 * No locking required, doesn't access internal driver settings. 941 */ 942 static void 943 smsc_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 944 { 945 struct smsc_softc *sc = usbd_xfer_softc(xfer); 946 struct usb_ether *ue = &sc->sc_ue; 947 struct ifnet *ifp = uether_getifp(ue); 948 struct mbuf *m; 949 struct usb_page_cache *pc; 950 uint32_t rxhdr; 951 uint16_t pktlen; 952 int off; 953 int actlen; 954 955 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 956 smsc_dbg_printf(sc, "rx : actlen %d\n", actlen); 957 958 switch (USB_GET_STATE(xfer)) { 959 case USB_ST_TRANSFERRED: 960 961 /* There is always a zero length frame after bringing the IF up */ 962 if (actlen < (sizeof(rxhdr) + ETHER_CRC_LEN)) 963 goto tr_setup; 964 965 /* There maybe multiple packets in the USB frame, each will have a 966 * header and each needs to have it's own mbuf allocated and populated 967 * for it. 968 */ 969 pc = usbd_xfer_get_frame(xfer, 0); 970 off = 0; 971 972 while (off < actlen) { 973 974 /* The frame header is always aligned on a 4 byte boundary */ 975 off = ((off + 0x3) & ~0x3); 976 977 usbd_copy_out(pc, off, &rxhdr, sizeof(rxhdr)); 978 off += (sizeof(rxhdr) + ETHER_ALIGN); 979 rxhdr = le32toh(rxhdr); 980 981 pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr); 982 983 smsc_dbg_printf(sc, "rx : rxhdr 0x%08x : pktlen %d : actlen %d : " 984 "off %d\n", rxhdr, pktlen, actlen, off); 985 986 987 if (rxhdr & SMSC_RX_STAT_ERROR) { 988 smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr); 989 ifp->if_ierrors++; 990 if (rxhdr & SMSC_RX_STAT_COLLISION) 991 ifp->if_collisions++; 992 } else { 993 994 /* Check if the ethernet frame is too big or too small */ 995 if ((pktlen < ETHER_HDR_LEN) || (pktlen > (actlen - off))) 996 goto tr_setup; 997 998 /* Create a new mbuf to store the packet in */ 999 m = uether_newbuf(); 1000 if (m == NULL) { 1001 smsc_warn_printf(sc, "failed to create new mbuf\n"); 1002 ifp->if_iqdrops++; 1003 goto tr_setup; 1004 } 1005 1006 usbd_copy_out(pc, off, mtod(m, uint8_t *), pktlen); 1007 1008 /* Check if RX TCP/UDP checksumming is being offloaded */ 1009 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) { 1010 1011 struct ether_header *eh; 1012 1013 eh = mtod(m, struct ether_header *); 1014 1015 /* Remove the extra 2 bytes of the csum */ 1016 pktlen -= 2; 1017 1018 /* The checksum appears to be simplistically calculated 1019 * over the udp/tcp header and data up to the end of the 1020 * eth frame. Which means if the eth frame is padded 1021 * the csum calculation is incorrectly performed over 1022 * the padding bytes as well. Therefore to be safe we 1023 * ignore the H/W csum on frames less than or equal to 1024 * 64 bytes. 1025 * 1026 * Ignore H/W csum for non-IPv4 packets. 1027 */ 1028 if (be16toh(eh->ether_type) == ETHERTYPE_IP && pktlen > ETHER_MIN_LEN) { 1029 1030 /* Indicate the UDP/TCP csum has been calculated */ 1031 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID; 1032 1033 /* Copy the TCP/UDP checksum from the last 2 bytes 1034 * of the transfer and put in the csum_data field. 1035 */ 1036 usbd_copy_out(pc, (off + pktlen), 1037 &m->m_pkthdr.csum_data, 2); 1038 1039 /* The data is copied in network order, but the 1040 * csum algorithm in the kernel expects it to be 1041 * in host network order. 1042 */ 1043 m->m_pkthdr.csum_data = ntohs(m->m_pkthdr.csum_data); 1044 1045 smsc_dbg_printf(sc, "RX checksum offloaded (0x%04x)\n", 1046 m->m_pkthdr.csum_data); 1047 } 1048 1049 /* Need to adjust the offset as well or we'll be off 1050 * by 2 because the csum is removed from the packet 1051 * length. 1052 */ 1053 off += 2; 1054 } 1055 1056 /* Finally enqueue the mbuf on the receive queue */ 1057 /* Remove 4 trailing bytes */ 1058 if (pktlen < (4 + ETHER_HDR_LEN)) { 1059 m_freem(m); 1060 goto tr_setup; 1061 } 1062 uether_rxmbuf(ue, m, pktlen - 4); 1063 } 1064 1065 /* Update the offset to move to the next potential packet */ 1066 off += pktlen; 1067 } 1068 1069 /* FALLTHROUGH */ 1070 1071 case USB_ST_SETUP: 1072 tr_setup: 1073 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1074 usbd_transfer_submit(xfer); 1075 uether_rxflush(ue); 1076 return; 1077 1078 default: 1079 if (error != USB_ERR_CANCELLED) { 1080 smsc_warn_printf(sc, "bulk read error, %s\n", usbd_errstr(error)); 1081 usbd_xfer_set_stall(xfer); 1082 goto tr_setup; 1083 } 1084 return; 1085 } 1086 } 1087 1088 /** 1089 * smsc_bulk_write_callback - Write callback used to send ethernet frame(s) 1090 * @xfer: the USB transfer 1091 * @error: error code if the transfers is in an errored state 1092 * 1093 * The main write function that pulls ethernet frames off the queue and sends 1094 * them out. 1095 * 1096 * LOCKING: 1097 * 1098 */ 1099 static void 1100 smsc_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 1101 { 1102 struct smsc_softc *sc = usbd_xfer_softc(xfer); 1103 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 1104 struct usb_page_cache *pc; 1105 struct mbuf *m; 1106 uint32_t txhdr; 1107 uint32_t frm_len = 0; 1108 int nframes; 1109 1110 switch (USB_GET_STATE(xfer)) { 1111 case USB_ST_TRANSFERRED: 1112 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1113 /* FALLTHROUGH */ 1114 1115 case USB_ST_SETUP: 1116 tr_setup: 1117 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 || 1118 (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) { 1119 /* Don't send anything if there is no link or controller is busy. */ 1120 return; 1121 } 1122 1123 for (nframes = 0; nframes < 16 && 1124 !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) { 1125 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1126 if (m == NULL) 1127 break; 1128 usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES, 1129 nframes); 1130 frm_len = 0; 1131 pc = usbd_xfer_get_frame(xfer, nframes); 1132 1133 /* Each frame is prefixed with two 32-bit values describing the 1134 * length of the packet and buffer. 1135 */ 1136 txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) | 1137 SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG; 1138 txhdr = htole32(txhdr); 1139 usbd_copy_in(pc, 0, &txhdr, sizeof(txhdr)); 1140 1141 txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len); 1142 txhdr = htole32(txhdr); 1143 usbd_copy_in(pc, 4, &txhdr, sizeof(txhdr)); 1144 1145 frm_len += 8; 1146 1147 /* Next copy in the actual packet */ 1148 usbd_m_copy_in(pc, frm_len, m, 0, m->m_pkthdr.len); 1149 frm_len += m->m_pkthdr.len; 1150 1151 ifp->if_opackets++; 1152 1153 /* If there's a BPF listener, bounce a copy of this frame to him */ 1154 BPF_MTAP(ifp, m); 1155 1156 m_freem(m); 1157 1158 /* Set frame length. */ 1159 usbd_xfer_set_frame_len(xfer, nframes, frm_len); 1160 } 1161 if (nframes != 0) { 1162 usbd_xfer_set_frames(xfer, nframes); 1163 usbd_transfer_submit(xfer); 1164 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1165 } 1166 return; 1167 1168 default: 1169 ifp->if_oerrors++; 1170 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1171 1172 if (error != USB_ERR_CANCELLED) { 1173 smsc_err_printf(sc, "usb error on tx: %s\n", usbd_errstr(error)); 1174 usbd_xfer_set_stall(xfer); 1175 goto tr_setup; 1176 } 1177 return; 1178 } 1179 } 1180 1181 /** 1182 * smsc_tick - Called periodically to monitor the state of the LAN95xx chip 1183 * @ue: USB ether interface 1184 * 1185 * Simply calls the mii status functions to check the state of the link. 1186 * 1187 * LOCKING: 1188 * Should be called with the SMSC lock held. 1189 */ 1190 static void 1191 smsc_tick(struct usb_ether *ue) 1192 { 1193 struct smsc_softc *sc = uether_getsc(ue); 1194 struct mii_data *mii = uether_getmii(&sc->sc_ue); 1195 1196 SMSC_LOCK_ASSERT(sc, MA_OWNED); 1197 1198 mii_tick(mii); 1199 if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) { 1200 smsc_miibus_statchg(ue->ue_dev); 1201 if ((sc->sc_flags & SMSC_FLAG_LINK) != 0) 1202 smsc_start(ue); 1203 } 1204 } 1205 1206 /** 1207 * smsc_start - Starts communication with the LAN95xx chip 1208 * @ue: USB ether interface 1209 * 1210 * 1211 * 1212 */ 1213 static void 1214 smsc_start(struct usb_ether *ue) 1215 { 1216 struct smsc_softc *sc = uether_getsc(ue); 1217 1218 /* 1219 * start the USB transfers, if not already started: 1220 */ 1221 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_RD]); 1222 usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_WR]); 1223 } 1224 1225 /** 1226 * smsc_stop - Stops communication with the LAN95xx chip 1227 * @ue: USB ether interface 1228 * 1229 * 1230 * 1231 */ 1232 static void 1233 smsc_stop(struct usb_ether *ue) 1234 { 1235 struct smsc_softc *sc = uether_getsc(ue); 1236 struct ifnet *ifp = uether_getifp(ue); 1237 1238 SMSC_LOCK_ASSERT(sc, MA_OWNED); 1239 1240 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1241 sc->sc_flags &= ~SMSC_FLAG_LINK; 1242 1243 /* 1244 * stop all the transfers, if not already stopped: 1245 */ 1246 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_WR]); 1247 usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_RD]); 1248 } 1249 1250 /** 1251 * smsc_phy_init - Initialises the in-built SMSC phy 1252 * @sc: driver soft context 1253 * 1254 * Resets the PHY part of the chip and then initialises it to default 1255 * values. The 'link down' and 'auto-negotiation complete' interrupts 1256 * from the PHY are also enabled, however we don't monitor the interrupt 1257 * endpoints for the moment. 1258 * 1259 * RETURNS: 1260 * Returns 0 on success or EIO if failed to reset the PHY. 1261 */ 1262 static int 1263 smsc_phy_init(struct smsc_softc *sc) 1264 { 1265 int bmcr; 1266 usb_ticks_t start_ticks; 1267 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000); 1268 1269 SMSC_LOCK_ASSERT(sc, MA_OWNED); 1270 1271 /* Reset phy and wait for reset to complete */ 1272 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, BMCR_RESET); 1273 1274 start_ticks = ticks; 1275 do { 1276 uether_pause(&sc->sc_ue, hz / 100); 1277 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR); 1278 } while ((bmcr & MII_BMCR) && ((ticks - start_ticks) < max_ticks)); 1279 1280 if (((usb_ticks_t)(ticks - start_ticks)) >= max_ticks) { 1281 smsc_err_printf(sc, "PHY reset timed-out"); 1282 return (EIO); 1283 } 1284 1285 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_ANAR, 1286 ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD | /* all modes */ 1287 ANAR_CSMA | 1288 ANAR_FC | 1289 ANAR_PAUSE_ASYM); 1290 1291 /* Setup the phy to interrupt when the link goes down or autoneg completes */ 1292 smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_STAT); 1293 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_MASK, 1294 (SMSC_PHY_INTR_ANEG_COMP | SMSC_PHY_INTR_LINK_DOWN)); 1295 1296 /* Restart auto-negotation */ 1297 bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR); 1298 bmcr |= BMCR_STARTNEG; 1299 smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr); 1300 1301 return (0); 1302 } 1303 1304 1305 /** 1306 * smsc_chip_init - Initialises the chip after power on 1307 * @sc: driver soft context 1308 * 1309 * This initialisation sequence is modelled on the procedure in the Linux 1310 * driver. 1311 * 1312 * RETURNS: 1313 * Returns 0 on success or an error code on failure. 1314 */ 1315 static int 1316 smsc_chip_init(struct smsc_softc *sc) 1317 { 1318 int err; 1319 int locked; 1320 uint32_t reg_val; 1321 int burst_cap; 1322 1323 locked = mtx_owned(&sc->sc_mtx); 1324 if (!locked) 1325 SMSC_LOCK(sc); 1326 1327 /* Enter H/W config mode */ 1328 smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST); 1329 1330 if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST)) != 0) { 1331 smsc_warn_printf(sc, "timed-out waiting for reset to complete\n"); 1332 goto init_failed; 1333 } 1334 1335 /* Reset the PHY */ 1336 smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST); 1337 1338 if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST) != 0)) { 1339 smsc_warn_printf(sc, "timed-out waiting for phy reset to complete\n"); 1340 goto init_failed; 1341 } 1342 1343 /* Set the mac address */ 1344 if ((err = smsc_setmacaddress(sc, sc->sc_ue.ue_eaddr)) != 0) { 1345 smsc_warn_printf(sc, "failed to set the MAC address\n"); 1346 goto init_failed; 1347 } 1348 1349 /* Don't know what the HW_CFG_BIR bit is, but following the reset sequence 1350 * as used in the Linux driver. 1351 */ 1352 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) != 0) { 1353 smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err); 1354 goto init_failed; 1355 } 1356 reg_val |= SMSC_HW_CFG_BIR; 1357 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 1358 1359 /* There is a so called 'turbo mode' that the linux driver supports, it 1360 * seems to allow you to jam multiple frames per Rx transaction. By default 1361 * this driver supports that and therefore allows multiple frames per URB. 1362 * 1363 * The xfer buffer size needs to reflect this as well, therefore based on 1364 * the calculations in the Linux driver the RX bufsize is set to 18944, 1365 * bufsz = (16 * 1024 + 5 * 512) 1366 * 1367 * Burst capability is the number of URBs that can be in a burst of data/ 1368 * ethernet frames. 1369 */ 1370 if (usbd_get_speed(sc->sc_ue.ue_udev) == USB_SPEED_HIGH) 1371 burst_cap = 37; 1372 else 1373 burst_cap = 128; 1374 1375 smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap); 1376 1377 /* Set the default bulk in delay (magic value from Linux driver) */ 1378 smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000); 1379 1380 1381 1382 /* 1383 * Initialise the RX interface 1384 */ 1385 if ((err = smsc_read_reg(sc, SMSC_HW_CFG, ®_val)) < 0) { 1386 smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", err); 1387 goto init_failed; 1388 } 1389 1390 /* Adjust the packet offset in the buffer (designed to try and align IP 1391 * header on 4 byte boundary) 1392 */ 1393 reg_val &= ~SMSC_HW_CFG_RXDOFF; 1394 reg_val |= (ETHER_ALIGN << 9) & SMSC_HW_CFG_RXDOFF; 1395 1396 /* The following setings are used for 'turbo mode', a.k.a multiple frames 1397 * per Rx transaction (again info taken form Linux driver). 1398 */ 1399 reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE); 1400 1401 smsc_write_reg(sc, SMSC_HW_CFG, reg_val); 1402 1403 /* Clear the status register ? */ 1404 smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff); 1405 1406 /* Read and display the revision register */ 1407 if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) { 1408 smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err); 1409 goto init_failed; 1410 } 1411 1412 device_printf(sc->sc_ue.ue_dev, "chip 0x%04lx, rev. %04lx\n", 1413 (sc->sc_rev_id & SMSC_ID_REV_CHIP_ID_MASK) >> 16, 1414 (sc->sc_rev_id & SMSC_ID_REV_CHIP_REV_MASK)); 1415 1416 /* GPIO/LED setup */ 1417 reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED | 1418 SMSC_LED_GPIO_CFG_FDX_LED; 1419 smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val); 1420 1421 /* 1422 * Initialise the TX interface 1423 */ 1424 smsc_write_reg(sc, SMSC_FLOW, 0); 1425 1426 smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT); 1427 1428 /* Read the current MAC configuration */ 1429 if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) { 1430 smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err); 1431 goto init_failed; 1432 } 1433 1434 /* Vlan */ 1435 smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN); 1436 1437 /* 1438 * Initialise the PHY 1439 */ 1440 if ((err = smsc_phy_init(sc)) != 0) 1441 goto init_failed; 1442 1443 1444 /* 1445 * Start TX 1446 */ 1447 sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN; 1448 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 1449 smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON); 1450 1451 /* 1452 * Start RX 1453 */ 1454 sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN; 1455 smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr); 1456 1457 if (!locked) 1458 SMSC_UNLOCK(sc); 1459 1460 return (0); 1461 1462 init_failed: 1463 if (!locked) 1464 SMSC_UNLOCK(sc); 1465 1466 smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err); 1467 return (err); 1468 } 1469 1470 1471 /** 1472 * smsc_ioctl - ioctl function for the device 1473 * @ifp: interface pointer 1474 * @cmd: the ioctl command 1475 * @data: data passed in the ioctl call, typically a pointer to struct ifreq. 1476 * 1477 * The ioctl routine is overridden to detect change requests for the H/W 1478 * checksum capabilities. 1479 * 1480 * RETURNS: 1481 * 0 on success and an error code on failure. 1482 */ 1483 static int 1484 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1485 { 1486 struct usb_ether *ue = ifp->if_softc; 1487 struct smsc_softc *sc; 1488 struct ifreq *ifr; 1489 int rc; 1490 int mask; 1491 int reinit; 1492 1493 if (cmd == SIOCSIFCAP) { 1494 1495 sc = uether_getsc(ue); 1496 ifr = (struct ifreq *)data; 1497 1498 SMSC_LOCK(sc); 1499 1500 rc = 0; 1501 reinit = 0; 1502 1503 mask = ifr->ifr_reqcap ^ ifp->if_capenable; 1504 1505 /* Modify the RX CSUM enable bits */ 1506 if ((mask & IFCAP_RXCSUM) != 0 && 1507 (ifp->if_capabilities & IFCAP_RXCSUM) != 0) { 1508 ifp->if_capenable ^= IFCAP_RXCSUM; 1509 1510 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1511 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1512 reinit = 1; 1513 } 1514 } 1515 1516 SMSC_UNLOCK(sc); 1517 if (reinit) 1518 #if __FreeBSD_version > 1000000 1519 uether_init(ue); 1520 #else 1521 ifp->if_init(ue); 1522 #endif 1523 1524 } else { 1525 rc = uether_ioctl(ifp, cmd, data); 1526 } 1527 1528 return (rc); 1529 } 1530 1531 #ifdef FDT 1532 /** 1533 * Get MAC address from FDT blob. Firmware or loader should fill 1534 * mac-address or local-mac-address property Returns 0 if MAC address 1535 * obtained, error code otherwise 1536 */ 1537 static int 1538 smsc_fdt_find_mac(unsigned char *mac) 1539 { 1540 phandle_t child, parent, root; 1541 int len; 1542 1543 root = OF_finddevice("/"); 1544 len = 0; 1545 parent = root; 1546 1547 /* Traverse through entire tree to find nodes usb ethernet nodes */ 1548 for (child = OF_child(parent); child != 0; child = OF_peer(child)) { 1549 1550 /* Find a 'leaf'. Start the search from this node. */ 1551 while (OF_child(child)) { 1552 parent = child; 1553 child = OF_child(child); 1554 } 1555 1556 if (fdt_is_compatible(child, "net,ethernet") && 1557 fdt_is_compatible(child, "usb,device")) { 1558 1559 /* Check if there is property */ 1560 if ((len = OF_getproplen(child, "local-mac-address")) > 0) { 1561 if (len != ETHER_ADDR_LEN) 1562 return (EINVAL); 1563 1564 OF_getprop(child, "local-mac-address", mac, 1565 ETHER_ADDR_LEN); 1566 return (0); 1567 } 1568 1569 if ((len = OF_getproplen(child, "mac-address")) > 0) { 1570 if (len != ETHER_ADDR_LEN) 1571 return (EINVAL); 1572 1573 OF_getprop(child, "mac-address", mac, 1574 ETHER_ADDR_LEN); 1575 return (0); 1576 } 1577 } 1578 1579 if (OF_peer(child) == 0) { 1580 /* No more siblings. */ 1581 child = parent; 1582 parent = OF_parent(child); 1583 } 1584 } 1585 1586 return (ENXIO); 1587 } 1588 #endif 1589 1590 /** 1591 * smsc_attach_post - Called after the driver attached to the USB interface 1592 * @ue: the USB ethernet device 1593 * 1594 * This is where the chip is intialised for the first time. This is different 1595 * from the smsc_init() function in that that one is designed to setup the 1596 * H/W to match the UE settings and can be called after a reset. 1597 * 1598 * 1599 */ 1600 static void 1601 smsc_attach_post(struct usb_ether *ue) 1602 { 1603 struct smsc_softc *sc = uether_getsc(ue); 1604 uint32_t mac_h, mac_l; 1605 int err; 1606 1607 smsc_dbg_printf(sc, "smsc_attach_post\n"); 1608 1609 /* Setup some of the basics */ 1610 sc->sc_phyno = 1; 1611 1612 1613 /* Attempt to get the mac address, if an EEPROM is not attached this 1614 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC 1615 * address based on urandom. 1616 */ 1617 memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN); 1618 1619 /* Check if there is already a MAC address in the register */ 1620 if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) && 1621 (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) { 1622 sc->sc_ue.ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); 1623 sc->sc_ue.ue_eaddr[4] = (uint8_t)((mac_h) & 0xff); 1624 sc->sc_ue.ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); 1625 sc->sc_ue.ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); 1626 sc->sc_ue.ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); 1627 sc->sc_ue.ue_eaddr[0] = (uint8_t)((mac_l) & 0xff); 1628 } 1629 1630 /* MAC address is not set so try to read from EEPROM, if that fails generate 1631 * a random MAC address. 1632 */ 1633 if (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) { 1634 1635 err = smsc_eeprom_read(sc, 0x01, sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN); 1636 #ifdef FDT 1637 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) 1638 err = smsc_fdt_find_mac(sc->sc_ue.ue_eaddr); 1639 #endif 1640 if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) { 1641 read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN); 1642 sc->sc_ue.ue_eaddr[0] &= ~0x01; /* unicast */ 1643 sc->sc_ue.ue_eaddr[0] |= 0x02; /* locally administered */ 1644 } 1645 } 1646 1647 /* Initialise the chip for the first time */ 1648 smsc_chip_init(sc); 1649 } 1650 1651 1652 /** 1653 * smsc_attach_post_sub - Called after the driver attached to the USB interface 1654 * @ue: the USB ethernet device 1655 * 1656 * Most of this is boilerplate code and copied from the base USB ethernet 1657 * driver. It has been overriden so that we can indicate to the system that 1658 * the chip supports H/W checksumming. 1659 * 1660 * RETURNS: 1661 * Returns 0 on success or a negative error code. 1662 */ 1663 #if __FreeBSD_version > 1000000 1664 static int 1665 smsc_attach_post_sub(struct usb_ether *ue) 1666 { 1667 struct smsc_softc *sc; 1668 struct ifnet *ifp; 1669 int error; 1670 1671 sc = uether_getsc(ue); 1672 ifp = ue->ue_ifp; 1673 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1674 ifp->if_start = uether_start; 1675 ifp->if_ioctl = smsc_ioctl; 1676 ifp->if_init = uether_init; 1677 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 1678 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 1679 IFQ_SET_READY(&ifp->if_snd); 1680 1681 /* The chip supports TCP/UDP checksum offloading on TX and RX paths, however 1682 * currently only RX checksum is supported in the driver (see top of file). 1683 */ 1684 ifp->if_capabilities |= IFCAP_RXCSUM; 1685 ifp->if_hwassist = 0; 1686 1687 /* TX checksuming is disabled (for now?) 1688 ifp->if_capabilities |= IFCAP_TXCSUM; 1689 ifp->if_capenable |= IFCAP_TXCSUM; 1690 ifp->if_hwassist = CSUM_TCP | CSUM_UDP; 1691 */ 1692 1693 ifp->if_capenable = ifp->if_capabilities; 1694 1695 mtx_lock(&Giant); 1696 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, 1697 uether_ifmedia_upd, ue->ue_methods->ue_mii_sts, 1698 BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0); 1699 mtx_unlock(&Giant); 1700 1701 return (error); 1702 } 1703 #endif /* __FreeBSD_version > 1000000 */ 1704 1705 1706 /** 1707 * smsc_probe - Probe the interface. 1708 * @dev: smsc device handle 1709 * 1710 * Checks if the device is a match for this driver. 1711 * 1712 * RETURNS: 1713 * Returns 0 on success or an error code on failure. 1714 */ 1715 static int 1716 smsc_probe(device_t dev) 1717 { 1718 struct usb_attach_arg *uaa = device_get_ivars(dev); 1719 1720 if (uaa->usb_mode != USB_MODE_HOST) 1721 return (ENXIO); 1722 if (uaa->info.bConfigIndex != SMSC_CONFIG_INDEX) 1723 return (ENXIO); 1724 if (uaa->info.bIfaceIndex != SMSC_IFACE_IDX) 1725 return (ENXIO); 1726 1727 return (usbd_lookup_id_by_uaa(smsc_devs, sizeof(smsc_devs), uaa)); 1728 } 1729 1730 1731 /** 1732 * smsc_attach - Attach the interface. 1733 * @dev: smsc device handle 1734 * 1735 * Allocate softc structures, do ifmedia setup and ethernet/BPF attach. 1736 * 1737 * RETURNS: 1738 * Returns 0 on success or a negative error code. 1739 */ 1740 static int 1741 smsc_attach(device_t dev) 1742 { 1743 struct usb_attach_arg *uaa = device_get_ivars(dev); 1744 struct smsc_softc *sc = device_get_softc(dev); 1745 struct usb_ether *ue = &sc->sc_ue; 1746 uint8_t iface_index; 1747 int err; 1748 1749 sc->sc_flags = USB_GET_DRIVER_INFO(uaa); 1750 1751 device_set_usb_desc(dev); 1752 1753 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 1754 1755 /* Setup the endpoints for the SMSC LAN95xx device(s) */ 1756 iface_index = SMSC_IFACE_IDX; 1757 err = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 1758 smsc_config, SMSC_N_TRANSFER, sc, &sc->sc_mtx); 1759 if (err) { 1760 device_printf(dev, "error: allocating USB transfers failed\n"); 1761 goto detach; 1762 } 1763 1764 ue->ue_sc = sc; 1765 ue->ue_dev = dev; 1766 ue->ue_udev = uaa->device; 1767 ue->ue_mtx = &sc->sc_mtx; 1768 ue->ue_methods = &smsc_ue_methods; 1769 1770 err = uether_ifattach(ue); 1771 if (err) { 1772 device_printf(dev, "error: could not attach interface\n"); 1773 goto detach; 1774 } 1775 return (0); /* success */ 1776 1777 detach: 1778 smsc_detach(dev); 1779 return (ENXIO); /* failure */ 1780 } 1781 1782 /** 1783 * smsc_detach - Detach the interface. 1784 * @dev: smsc device handle 1785 * 1786 * RETURNS: 1787 * Returns 0. 1788 */ 1789 static int 1790 smsc_detach(device_t dev) 1791 { 1792 struct smsc_softc *sc = device_get_softc(dev); 1793 struct usb_ether *ue = &sc->sc_ue; 1794 1795 usbd_transfer_unsetup(sc->sc_xfer, SMSC_N_TRANSFER); 1796 uether_ifdetach(ue); 1797 mtx_destroy(&sc->sc_mtx); 1798 1799 return (0); 1800 } 1801 1802 static device_method_t smsc_methods[] = { 1803 /* Device interface */ 1804 DEVMETHOD(device_probe, smsc_probe), 1805 DEVMETHOD(device_attach, smsc_attach), 1806 DEVMETHOD(device_detach, smsc_detach), 1807 1808 /* bus interface */ 1809 DEVMETHOD(bus_print_child, bus_generic_print_child), 1810 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 1811 1812 /* MII interface */ 1813 DEVMETHOD(miibus_readreg, smsc_miibus_readreg), 1814 DEVMETHOD(miibus_writereg, smsc_miibus_writereg), 1815 DEVMETHOD(miibus_statchg, smsc_miibus_statchg), 1816 1817 DEVMETHOD_END 1818 }; 1819 1820 static driver_t smsc_driver = { 1821 .name = "smsc", 1822 .methods = smsc_methods, 1823 .size = sizeof(struct smsc_softc), 1824 }; 1825 1826 static devclass_t smsc_devclass; 1827 1828 DRIVER_MODULE(smsc, uhub, smsc_driver, smsc_devclass, NULL, 0); 1829 DRIVER_MODULE(miibus, smsc, miibus_driver, miibus_devclass, 0, 0); 1830 MODULE_DEPEND(smsc, uether, 1, 1, 1); 1831 MODULE_DEPEND(smsc, usb, 1, 1, 1); 1832 MODULE_DEPEND(smsc, ether, 1, 1, 1); 1833 MODULE_DEPEND(smsc, miibus, 1, 1, 1); 1834 MODULE_VERSION(smsc, 1); 1835