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