1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 2012 Ben Gray <bgray@freebsd.org>. 5 * Copyright (C) 2018 The FreeBSD Foundation. 6 * 7 * This software was developed by Arshan Khanifar <arshankhanifar@gmail.com> 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD$ 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 /* 38 * USB-To-Ethernet adapter driver for Microchip's LAN78XX and related families. 39 * 40 * USB 3.1 to 10/100/1000 Mbps Ethernet 41 * LAN7800 http://www.microchip.com/wwwproducts/en/LAN7800 42 * 43 * USB 2.0 to 10/100/1000 Mbps Ethernet 44 * LAN7850 http://www.microchip.com/wwwproducts/en/LAN7850 45 * 46 * USB 2 to 10/100/1000 Mbps Ethernet with built-in USB hub 47 * LAN7515 (no datasheet available, but probes and functions as LAN7800) 48 * 49 * This driver is based on the if_smsc driver, with lan78xx-specific 50 * functionality modelled on Microchip's Linux lan78xx driver. 51 * 52 * UNIMPLEMENTED FEATURES 53 * ------------------ 54 * A number of features supported by the lan78xx are not yet implemented in 55 * this driver: 56 * 57 * - TX checksum offloading: Nothing has been implemented yet. 58 * - Direct address translation filtering: Implemented but untested. 59 * - VLAN tag removal. 60 * - Support for USB interrupt endpoints. 61 * - Latency Tolerance Messaging (LTM) support. 62 * - TCP LSO support. 63 * 64 */ 65 66 #include <sys/param.h> 67 #include <sys/bus.h> 68 #include <sys/callout.h> 69 #include <sys/condvar.h> 70 #include <sys/kernel.h> 71 #include <sys/lock.h> 72 #include <sys/malloc.h> 73 #include <sys/module.h> 74 #include <sys/mutex.h> 75 #include <sys/priv.h> 76 #include <sys/queue.h> 77 #include <sys/random.h> 78 #include <sys/socket.h> 79 #include <sys/stddef.h> 80 #include <sys/stdint.h> 81 #include <sys/sx.h> 82 #include <sys/sysctl.h> 83 #include <sys/systm.h> 84 #include <sys/unistd.h> 85 86 #include <net/if.h> 87 #include <net/if_var.h> 88 #include <net/if_media.h> 89 90 #include <dev/mii/mii.h> 91 #include <dev/mii/miivar.h> 92 93 #include <netinet/in.h> 94 #include <netinet/ip.h> 95 96 #include "opt_platform.h" 97 98 #ifdef FDT 99 #include <dev/fdt/fdt_common.h> 100 #include <dev/ofw/ofw_bus.h> 101 #include <dev/ofw/ofw_bus_subr.h> 102 #include <dev/usb/usb_fdt_support.h> 103 #endif 104 105 #include <dev/usb/usb.h> 106 #include <dev/usb/usbdi.h> 107 #include <dev/usb/usbdi_util.h> 108 #include "usbdevs.h" 109 110 #define USB_DEBUG_VAR lan78xx_debug 111 #include <dev/usb/usb_debug.h> 112 #include <dev/usb/usb_process.h> 113 114 #include <dev/usb/net/usb_ethernet.h> 115 116 #include <dev/usb/net/if_mugereg.h> 117 118 #include "miibus_if.h" 119 120 #ifdef USB_DEBUG 121 static int muge_debug = 0; 122 123 SYSCTL_NODE(_hw_usb, OID_AUTO, muge, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 124 "Microchip LAN78xx USB-GigE"); 125 SYSCTL_INT(_hw_usb_muge, OID_AUTO, debug, CTLFLAG_RWTUN, &muge_debug, 0, 126 "Debug level"); 127 #endif 128 129 #define MUGE_DEFAULT_TX_CSUM_ENABLE (false) 130 #define MUGE_DEFAULT_TSO_ENABLE (false) 131 132 /* Supported Vendor and Product IDs. */ 133 static const struct usb_device_id lan78xx_devs[] = { 134 #define MUGE_DEV(p,i) { USB_VPI(USB_VENDOR_SMC2, USB_PRODUCT_SMC2_##p, i) } 135 MUGE_DEV(LAN7800_ETH, 0), 136 MUGE_DEV(LAN7801_ETH, 0), 137 MUGE_DEV(LAN7850_ETH, 0), 138 #undef MUGE_DEV 139 }; 140 141 #ifdef USB_DEBUG 142 #define muge_dbg_printf(sc, fmt, args...) \ 143 do { \ 144 if (muge_debug > 0) \ 145 device_printf((sc)->sc_ue.ue_dev, "debug: " fmt, ##args); \ 146 } while(0) 147 #else 148 #define muge_dbg_printf(sc, fmt, args...) do { } while (0) 149 #endif 150 151 #define muge_warn_printf(sc, fmt, args...) \ 152 device_printf((sc)->sc_ue.ue_dev, "warning: " fmt, ##args) 153 154 #define muge_err_printf(sc, fmt, args...) \ 155 device_printf((sc)->sc_ue.ue_dev, "error: " fmt, ##args) 156 157 #define ETHER_IS_VALID(addr) \ 158 (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr)) 159 160 /* USB endpoints. */ 161 162 enum { 163 MUGE_BULK_DT_RD, 164 MUGE_BULK_DT_WR, 165 #if 0 /* Ignore interrupt endpoints for now as we poll on MII status. */ 166 MUGE_INTR_DT_WR, 167 MUGE_INTR_DT_RD, 168 #endif 169 MUGE_N_TRANSFER, 170 }; 171 172 struct muge_softc { 173 struct usb_ether sc_ue; 174 struct mtx sc_mtx; 175 struct usb_xfer *sc_xfer[MUGE_N_TRANSFER]; 176 int sc_phyno; 177 uint32_t sc_leds; 178 uint16_t sc_led_modes; 179 uint16_t sc_led_modes_mask; 180 181 /* Settings for the mac control (MAC_CSR) register. */ 182 uint32_t sc_rfe_ctl; 183 uint32_t sc_mdix_ctl; 184 uint16_t chipid; 185 uint16_t chiprev; 186 uint32_t sc_mchash_table[ETH_DP_SEL_VHF_HASH_LEN]; 187 uint32_t sc_pfilter_table[MUGE_NUM_PFILTER_ADDRS_][2]; 188 189 uint32_t sc_flags; 190 #define MUGE_FLAG_LINK 0x0001 191 #define MUGE_FLAG_INIT_DONE 0x0002 192 }; 193 194 #define MUGE_IFACE_IDX 0 195 196 #define MUGE_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 197 #define MUGE_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 198 #define MUGE_LOCK_ASSERT(_sc, t) mtx_assert(&(_sc)->sc_mtx, t) 199 200 static device_probe_t muge_probe; 201 static device_attach_t muge_attach; 202 static device_detach_t muge_detach; 203 204 static usb_callback_t muge_bulk_read_callback; 205 static usb_callback_t muge_bulk_write_callback; 206 207 static miibus_readreg_t lan78xx_miibus_readreg; 208 static miibus_writereg_t lan78xx_miibus_writereg; 209 static miibus_statchg_t lan78xx_miibus_statchg; 210 211 static int muge_attach_post_sub(struct usb_ether *ue); 212 static uether_fn_t muge_attach_post; 213 static uether_fn_t muge_init; 214 static uether_fn_t muge_stop; 215 static uether_fn_t muge_start; 216 static uether_fn_t muge_tick; 217 static uether_fn_t muge_setmulti; 218 static uether_fn_t muge_setpromisc; 219 220 static int muge_ifmedia_upd(struct ifnet *); 221 static void muge_ifmedia_sts(struct ifnet *, struct ifmediareq *); 222 223 static int lan78xx_chip_init(struct muge_softc *sc); 224 static int muge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 225 226 static const struct usb_config muge_config[MUGE_N_TRANSFER] = { 227 228 [MUGE_BULK_DT_WR] = { 229 .type = UE_BULK, 230 .endpoint = UE_ADDR_ANY, 231 .direction = UE_DIR_OUT, 232 .frames = 16, 233 .bufsize = 16 * (MCLBYTES + 16), 234 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 235 .callback = muge_bulk_write_callback, 236 .timeout = 10000, /* 10 seconds */ 237 }, 238 239 [MUGE_BULK_DT_RD] = { 240 .type = UE_BULK, 241 .endpoint = UE_ADDR_ANY, 242 .direction = UE_DIR_IN, 243 .bufsize = 20480, /* bytes */ 244 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 245 .callback = muge_bulk_read_callback, 246 .timeout = 0, /* no timeout */ 247 }, 248 /* 249 * The chip supports interrupt endpoints, however they aren't 250 * needed as we poll on the MII status. 251 */ 252 }; 253 254 static const struct usb_ether_methods muge_ue_methods = { 255 .ue_attach_post = muge_attach_post, 256 .ue_attach_post_sub = muge_attach_post_sub, 257 .ue_start = muge_start, 258 .ue_ioctl = muge_ioctl, 259 .ue_init = muge_init, 260 .ue_stop = muge_stop, 261 .ue_tick = muge_tick, 262 .ue_setmulti = muge_setmulti, 263 .ue_setpromisc = muge_setpromisc, 264 .ue_mii_upd = muge_ifmedia_upd, 265 .ue_mii_sts = muge_ifmedia_sts, 266 }; 267 268 /** 269 * lan78xx_read_reg - Read a 32-bit register on the device 270 * @sc: driver soft context 271 * @off: offset of the register 272 * @data: pointer a value that will be populated with the register value 273 * 274 * LOCKING: 275 * The device lock must be held before calling this function. 276 * 277 * RETURNS: 278 * 0 on success, a USB_ERR_?? error code on failure. 279 */ 280 static int 281 lan78xx_read_reg(struct muge_softc *sc, uint32_t off, uint32_t *data) 282 { 283 struct usb_device_request req; 284 uint32_t buf; 285 usb_error_t err; 286 287 MUGE_LOCK_ASSERT(sc, MA_OWNED); 288 289 req.bmRequestType = UT_READ_VENDOR_DEVICE; 290 req.bRequest = UVR_READ_REG; 291 USETW(req.wValue, 0); 292 USETW(req.wIndex, off); 293 USETW(req.wLength, 4); 294 295 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000); 296 if (err != 0) 297 muge_warn_printf(sc, "Failed to read register 0x%0x\n", off); 298 *data = le32toh(buf); 299 return (err); 300 } 301 302 /** 303 * lan78xx_write_reg - Write a 32-bit register on the device 304 * @sc: driver soft context 305 * @off: offset of the register 306 * @data: the 32-bit value to write into the register 307 * 308 * LOCKING: 309 * The device lock must be held before calling this function. 310 * 311 * RETURNS: 312 * 0 on success, a USB_ERR_?? error code on failure. 313 */ 314 static int 315 lan78xx_write_reg(struct muge_softc *sc, uint32_t off, uint32_t data) 316 { 317 struct usb_device_request req; 318 uint32_t buf; 319 usb_error_t err; 320 321 MUGE_LOCK_ASSERT(sc, MA_OWNED); 322 323 buf = htole32(data); 324 325 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 326 req.bRequest = UVR_WRITE_REG; 327 USETW(req.wValue, 0); 328 USETW(req.wIndex, off); 329 USETW(req.wLength, 4); 330 331 err = uether_do_request(&sc->sc_ue, &req, &buf, 1000); 332 if (err != 0) 333 muge_warn_printf(sc, "Failed to write register 0x%0x\n", off); 334 return (err); 335 } 336 337 /** 338 * lan78xx_wait_for_bits - Poll on a register value until bits are cleared 339 * @sc: soft context 340 * @reg: offset of the register 341 * @bits: if the bits are clear the function returns 342 * 343 * LOCKING: 344 * The device lock must be held before calling this function. 345 * 346 * RETURNS: 347 * 0 on success, or a USB_ERR_?? error code on failure. 348 */ 349 static int 350 lan78xx_wait_for_bits(struct muge_softc *sc, uint32_t reg, uint32_t bits) 351 { 352 usb_ticks_t start_ticks; 353 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000); 354 uint32_t val; 355 int err; 356 357 MUGE_LOCK_ASSERT(sc, MA_OWNED); 358 359 start_ticks = (usb_ticks_t)ticks; 360 do { 361 if ((err = lan78xx_read_reg(sc, reg, &val)) != 0) 362 return (err); 363 if (!(val & bits)) 364 return (0); 365 uether_pause(&sc->sc_ue, hz / 100); 366 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks); 367 368 return (USB_ERR_TIMEOUT); 369 } 370 371 /** 372 * lan78xx_eeprom_read_raw - Read the attached EEPROM 373 * @sc: soft context 374 * @off: the eeprom address offset 375 * @buf: stores the bytes 376 * @buflen: the number of bytes to read 377 * 378 * Simply reads bytes from an attached eeprom. 379 * 380 * LOCKING: 381 * The function takes and releases the device lock if not already held. 382 * 383 * RETURNS: 384 * 0 on success, or a USB_ERR_?? error code on failure. 385 */ 386 static int 387 lan78xx_eeprom_read_raw(struct muge_softc *sc, uint16_t off, uint8_t *buf, 388 uint16_t buflen) 389 { 390 usb_ticks_t start_ticks; 391 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000); 392 int err; 393 uint32_t val, saved; 394 uint16_t i; 395 bool locked; 396 397 locked = mtx_owned(&sc->sc_mtx); /* XXX */ 398 if (!locked) 399 MUGE_LOCK(sc); 400 401 if (sc->chipid == ETH_ID_REV_CHIP_ID_7800_) { 402 /* EEDO/EECLK muxed with LED0/LED1 on LAN7800. */ 403 err = lan78xx_read_reg(sc, ETH_HW_CFG, &val); 404 saved = val; 405 406 val &= ~(ETH_HW_CFG_LEDO_EN_ | ETH_HW_CFG_LED1_EN_); 407 err = lan78xx_write_reg(sc, ETH_HW_CFG, val); 408 } 409 410 err = lan78xx_wait_for_bits(sc, ETH_E2P_CMD, ETH_E2P_CMD_BUSY_); 411 if (err != 0) { 412 muge_warn_printf(sc, "eeprom busy, failed to read data\n"); 413 goto done; 414 } 415 416 /* Start reading the bytes, one at a time. */ 417 for (i = 0; i < buflen; i++) { 418 val = ETH_E2P_CMD_BUSY_ | ETH_E2P_CMD_READ_; 419 val |= (ETH_E2P_CMD_ADDR_MASK_ & (off + i)); 420 if ((err = lan78xx_write_reg(sc, ETH_E2P_CMD, val)) != 0) 421 goto done; 422 423 start_ticks = (usb_ticks_t)ticks; 424 do { 425 if ((err = lan78xx_read_reg(sc, ETH_E2P_CMD, &val)) != 426 0) 427 goto done; 428 if (!(val & ETH_E2P_CMD_BUSY_) || 429 (val & ETH_E2P_CMD_TIMEOUT_)) 430 break; 431 432 uether_pause(&sc->sc_ue, hz / 100); 433 } while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks); 434 435 if (val & (ETH_E2P_CMD_BUSY_ | ETH_E2P_CMD_TIMEOUT_)) { 436 muge_warn_printf(sc, "eeprom command failed\n"); 437 err = USB_ERR_IOERROR; 438 break; 439 } 440 441 if ((err = lan78xx_read_reg(sc, ETH_E2P_DATA, &val)) != 0) 442 goto done; 443 444 buf[i] = (val & 0xff); 445 } 446 447 done: 448 if (!locked) 449 MUGE_UNLOCK(sc); 450 if (sc->chipid == ETH_ID_REV_CHIP_ID_7800_) { 451 /* Restore saved LED configuration. */ 452 lan78xx_write_reg(sc, ETH_HW_CFG, saved); 453 } 454 return (err); 455 } 456 457 static bool 458 lan78xx_eeprom_present(struct muge_softc *sc) 459 { 460 int ret; 461 uint8_t sig; 462 463 ret = lan78xx_eeprom_read_raw(sc, ETH_E2P_INDICATOR_OFFSET, &sig, 1); 464 return (ret == 0 && sig == ETH_E2P_INDICATOR); 465 } 466 467 /** 468 * lan78xx_otp_read_raw 469 * @sc: soft context 470 * @off: the otp address offset 471 * @buf: stores the bytes 472 * @buflen: the number of bytes to read 473 * 474 * Simply reads bytes from the OTP. 475 * 476 * LOCKING: 477 * The function takes and releases the device lock if not already held. 478 * 479 * RETURNS: 480 * 0 on success, or a USB_ERR_?? error code on failure. 481 * 482 */ 483 static int 484 lan78xx_otp_read_raw(struct muge_softc *sc, uint16_t off, uint8_t *buf, 485 uint16_t buflen) 486 { 487 int err; 488 uint32_t val; 489 uint16_t i; 490 bool locked; 491 locked = mtx_owned(&sc->sc_mtx); 492 if (!locked) 493 MUGE_LOCK(sc); 494 495 err = lan78xx_read_reg(sc, OTP_PWR_DN, &val); 496 497 /* Checking if bit is set. */ 498 if (val & OTP_PWR_DN_PWRDN_N) { 499 /* Clear it, then wait for it to be cleared. */ 500 lan78xx_write_reg(sc, OTP_PWR_DN, 0); 501 err = lan78xx_wait_for_bits(sc, OTP_PWR_DN, OTP_PWR_DN_PWRDN_N); 502 if (err != 0) { 503 muge_warn_printf(sc, "OTP off? failed to read data\n"); 504 goto done; 505 } 506 } 507 /* Start reading the bytes, one at a time. */ 508 for (i = 0; i < buflen; i++) { 509 err = lan78xx_write_reg(sc, OTP_ADDR1, 510 ((off + i) >> 8) & OTP_ADDR1_15_11); 511 err = lan78xx_write_reg(sc, OTP_ADDR2, 512 ((off + i) & OTP_ADDR2_10_3)); 513 err = lan78xx_write_reg(sc, OTP_FUNC_CMD, OTP_FUNC_CMD_READ_); 514 err = lan78xx_write_reg(sc, OTP_CMD_GO, OTP_CMD_GO_GO_); 515 516 err = lan78xx_wait_for_bits(sc, OTP_STATUS, OTP_STATUS_BUSY_); 517 if (err != 0) { 518 muge_warn_printf(sc, "OTP busy failed to read data\n"); 519 goto done; 520 } 521 522 if ((err = lan78xx_read_reg(sc, OTP_RD_DATA, &val)) != 0) 523 goto done; 524 525 buf[i] = (uint8_t)(val & 0xff); 526 } 527 528 done: 529 if (!locked) 530 MUGE_UNLOCK(sc); 531 return (err); 532 } 533 534 /** 535 * lan78xx_otp_read 536 * @sc: soft context 537 * @off: the otp address offset 538 * @buf: stores the bytes 539 * @buflen: the number of bytes to read 540 * 541 * Simply reads bytes from the otp. 542 * 543 * LOCKING: 544 * The function takes and releases device lock if it is not already held. 545 * 546 * RETURNS: 547 * 0 on success, or a USB_ERR_?? error code on failure. 548 */ 549 static int 550 lan78xx_otp_read(struct muge_softc *sc, uint16_t off, uint8_t *buf, 551 uint16_t buflen) 552 { 553 uint8_t sig; 554 int err; 555 556 err = lan78xx_otp_read_raw(sc, OTP_INDICATOR_OFFSET, &sig, 1); 557 if (err == 0) { 558 if (sig == OTP_INDICATOR_1) { 559 } else if (sig == OTP_INDICATOR_2) { 560 off += 0x100; /* XXX */ 561 } else { 562 err = -EINVAL; 563 } 564 if (!err) 565 err = lan78xx_otp_read_raw(sc, off, buf, buflen); 566 } 567 return (err); 568 } 569 570 /** 571 * lan78xx_setmacaddress - Set the mac address in the device 572 * @sc: driver soft context 573 * @addr: pointer to array contain at least 6 bytes of the mac 574 * 575 * LOCKING: 576 * Should be called with the MUGE lock held. 577 * 578 * RETURNS: 579 * Returns 0 on success or a negative error code. 580 */ 581 static int 582 lan78xx_setmacaddress(struct muge_softc *sc, const uint8_t *addr) 583 { 584 int err; 585 uint32_t val; 586 587 muge_dbg_printf(sc, 588 "setting mac address to %02x:%02x:%02x:%02x:%02x:%02x\n", 589 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 590 591 MUGE_LOCK_ASSERT(sc, MA_OWNED); 592 593 val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0]; 594 if ((err = lan78xx_write_reg(sc, ETH_RX_ADDRL, val)) != 0) 595 goto done; 596 597 val = (addr[5] << 8) | addr[4]; 598 err = lan78xx_write_reg(sc, ETH_RX_ADDRH, val); 599 600 done: 601 return (err); 602 } 603 604 /** 605 * lan78xx_set_rx_max_frame_length 606 * @sc: driver soft context 607 * @size: pointer to array contain at least 6 bytes of the mac 608 * 609 * Sets the maximum frame length to be received. Frames bigger than 610 * this size are aborted. 611 * 612 * RETURNS: 613 * Returns 0 on success or a negative error code. 614 */ 615 static int 616 lan78xx_set_rx_max_frame_length(struct muge_softc *sc, int size) 617 { 618 int err = 0; 619 uint32_t buf; 620 bool rxenabled; 621 622 /* First we have to disable rx before changing the length. */ 623 err = lan78xx_read_reg(sc, ETH_MAC_RX, &buf); 624 rxenabled = ((buf & ETH_MAC_RX_EN_) != 0); 625 626 if (rxenabled) { 627 buf &= ~ETH_MAC_RX_EN_; 628 err = lan78xx_write_reg(sc, ETH_MAC_RX, buf); 629 } 630 631 /* Setting max frame length. */ 632 buf &= ~ETH_MAC_RX_MAX_FR_SIZE_MASK_; 633 buf |= (((size + 4) << ETH_MAC_RX_MAX_FR_SIZE_SHIFT_) & 634 ETH_MAC_RX_MAX_FR_SIZE_MASK_); 635 err = lan78xx_write_reg(sc, ETH_MAC_RX, buf); 636 637 /* If it were enabled before, we enable it back. */ 638 639 if (rxenabled) { 640 buf |= ETH_MAC_RX_EN_; 641 err = lan78xx_write_reg(sc, ETH_MAC_RX, buf); 642 } 643 644 return (0); 645 } 646 647 /** 648 * lan78xx_miibus_readreg - Read a MII/MDIO register 649 * @dev: usb ether device 650 * @phy: the number of phy reading from 651 * @reg: the register address 652 * 653 * LOCKING: 654 * Takes and releases the device mutex lock if not already held. 655 * 656 * RETURNS: 657 * Returns the 16-bits read from the MII register, if this function fails 658 * 0 is returned. 659 */ 660 static int 661 lan78xx_miibus_readreg(device_t dev, int phy, int reg) 662 { 663 struct muge_softc *sc = device_get_softc(dev); 664 uint32_t addr, val; 665 bool locked; 666 667 val = 0; 668 locked = mtx_owned(&sc->sc_mtx); 669 if (!locked) 670 MUGE_LOCK(sc); 671 672 if (lan78xx_wait_for_bits(sc, ETH_MII_ACC, ETH_MII_ACC_MII_BUSY_) != 673 0) { 674 muge_warn_printf(sc, "MII is busy\n"); 675 goto done; 676 } 677 678 addr = (phy << 11) | (reg << 6) | 679 ETH_MII_ACC_MII_READ_ | ETH_MII_ACC_MII_BUSY_; 680 lan78xx_write_reg(sc, ETH_MII_ACC, addr); 681 682 if (lan78xx_wait_for_bits(sc, ETH_MII_ACC, ETH_MII_ACC_MII_BUSY_) != 683 0) { 684 muge_warn_printf(sc, "MII read timeout\n"); 685 goto done; 686 } 687 688 lan78xx_read_reg(sc, ETH_MII_DATA, &val); 689 val = le32toh(val); 690 691 done: 692 if (!locked) 693 MUGE_UNLOCK(sc); 694 695 return (val & 0xFFFF); 696 } 697 698 /** 699 * lan78xx_miibus_writereg - Writes a MII/MDIO register 700 * @dev: usb ether device 701 * @phy: the number of phy writing to 702 * @reg: the register address 703 * @val: the value to write 704 * 705 * Attempts to write a PHY register through the usb controller registers. 706 * 707 * LOCKING: 708 * Takes and releases the device mutex lock if not already held. 709 * 710 * RETURNS: 711 * Always returns 0 regardless of success or failure. 712 */ 713 static int 714 lan78xx_miibus_writereg(device_t dev, int phy, int reg, int val) 715 { 716 struct muge_softc *sc = device_get_softc(dev); 717 uint32_t addr; 718 bool locked; 719 720 if (sc->sc_phyno != phy) 721 return (0); 722 723 locked = mtx_owned(&sc->sc_mtx); 724 if (!locked) 725 MUGE_LOCK(sc); 726 727 if (lan78xx_wait_for_bits(sc, ETH_MII_ACC, ETH_MII_ACC_MII_BUSY_) != 728 0) { 729 muge_warn_printf(sc, "MII is busy\n"); 730 goto done; 731 } 732 733 val = htole32(val); 734 lan78xx_write_reg(sc, ETH_MII_DATA, val); 735 736 addr = (phy << 11) | (reg << 6) | 737 ETH_MII_ACC_MII_WRITE_ | ETH_MII_ACC_MII_BUSY_; 738 lan78xx_write_reg(sc, ETH_MII_ACC, addr); 739 740 if (lan78xx_wait_for_bits(sc, ETH_MII_ACC, ETH_MII_ACC_MII_BUSY_) != 0) 741 muge_warn_printf(sc, "MII write timeout\n"); 742 743 done: 744 if (!locked) 745 MUGE_UNLOCK(sc); 746 return (0); 747 } 748 749 /* 750 * lan78xx_miibus_statchg - Called to detect phy status change 751 * @dev: usb ether device 752 * 753 * This function is called periodically by the system to poll for status 754 * changes of the link. 755 * 756 * LOCKING: 757 * Takes and releases the device mutex lock if not already held. 758 */ 759 static void 760 lan78xx_miibus_statchg(device_t dev) 761 { 762 struct muge_softc *sc = device_get_softc(dev); 763 struct mii_data *mii = uether_getmii(&sc->sc_ue); 764 struct ifnet *ifp; 765 int err; 766 uint32_t flow = 0; 767 uint32_t fct_flow = 0; 768 bool locked; 769 770 locked = mtx_owned(&sc->sc_mtx); 771 if (!locked) 772 MUGE_LOCK(sc); 773 774 ifp = uether_getifp(&sc->sc_ue); 775 if (mii == NULL || ifp == NULL || 776 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 777 goto done; 778 779 /* Use the MII status to determine link status */ 780 sc->sc_flags &= ~MUGE_FLAG_LINK; 781 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 782 (IFM_ACTIVE | IFM_AVALID)) { 783 muge_dbg_printf(sc, "media is active\n"); 784 switch (IFM_SUBTYPE(mii->mii_media_active)) { 785 case IFM_10_T: 786 case IFM_100_TX: 787 sc->sc_flags |= MUGE_FLAG_LINK; 788 muge_dbg_printf(sc, "10/100 ethernet\n"); 789 break; 790 case IFM_1000_T: 791 sc->sc_flags |= MUGE_FLAG_LINK; 792 muge_dbg_printf(sc, "Gigabit ethernet\n"); 793 break; 794 default: 795 break; 796 } 797 } 798 /* Lost link, do nothing. */ 799 if ((sc->sc_flags & MUGE_FLAG_LINK) == 0) { 800 muge_dbg_printf(sc, "link flag not set\n"); 801 goto done; 802 } 803 804 err = lan78xx_read_reg(sc, ETH_FCT_FLOW, &fct_flow); 805 if (err) { 806 muge_warn_printf(sc, 807 "failed to read initial flow control thresholds, error %d\n", 808 err); 809 goto done; 810 } 811 812 /* Enable/disable full duplex operation and TX/RX pause. */ 813 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) { 814 muge_dbg_printf(sc, "full duplex operation\n"); 815 816 /* Enable transmit MAC flow control function. */ 817 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0) 818 flow |= ETH_FLOW_CR_TX_FCEN_ | 0xFFFF; 819 820 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0) 821 flow |= ETH_FLOW_CR_RX_FCEN_; 822 } 823 824 /* XXX Flow control settings obtained from Microchip's driver. */ 825 switch(usbd_get_speed(sc->sc_ue.ue_udev)) { 826 case USB_SPEED_SUPER: 827 fct_flow = 0x817; 828 break; 829 case USB_SPEED_HIGH: 830 fct_flow = 0x211; 831 break; 832 default: 833 break; 834 } 835 836 err += lan78xx_write_reg(sc, ETH_FLOW, flow); 837 err += lan78xx_write_reg(sc, ETH_FCT_FLOW, fct_flow); 838 if (err) 839 muge_warn_printf(sc, "media change failed, error %d\n", err); 840 841 done: 842 if (!locked) 843 MUGE_UNLOCK(sc); 844 } 845 846 /* 847 * lan78xx_set_mdix_auto - Configure the device to enable automatic 848 * crossover and polarity detection. LAN7800 provides HP Auto-MDIX 849 * functionality for seamless crossover and polarity detection. 850 * 851 * @sc: driver soft context 852 * 853 * LOCKING: 854 * Takes and releases the device mutex lock if not already held. 855 */ 856 static void 857 lan78xx_set_mdix_auto(struct muge_softc *sc) 858 { 859 uint32_t buf, err; 860 861 err = lan78xx_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, 862 MUGE_EXT_PAGE_ACCESS, MUGE_EXT_PAGE_SPACE_1); 863 864 buf = lan78xx_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, 865 MUGE_EXT_MODE_CTRL); 866 buf &= ~MUGE_EXT_MODE_CTRL_MDIX_MASK_; 867 buf |= MUGE_EXT_MODE_CTRL_AUTO_MDIX_; 868 869 lan78xx_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR); 870 err += lan78xx_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, 871 MUGE_EXT_MODE_CTRL, buf); 872 873 err += lan78xx_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, 874 MUGE_EXT_PAGE_ACCESS, MUGE_EXT_PAGE_SPACE_0); 875 876 if (err != 0) 877 muge_warn_printf(sc, "error setting PHY's MDIX status\n"); 878 879 sc->sc_mdix_ctl = buf; 880 } 881 882 /** 883 * lan78xx_phy_init - Initialises the in-built MUGE phy 884 * @sc: driver soft context 885 * 886 * Resets the PHY part of the chip and then initialises it to default 887 * values. The 'link down' and 'auto-negotiation complete' interrupts 888 * from the PHY are also enabled, however we don't monitor the interrupt 889 * endpoints for the moment. 890 * 891 * RETURNS: 892 * Returns 0 on success or EIO if failed to reset the PHY. 893 */ 894 static int 895 lan78xx_phy_init(struct muge_softc *sc) 896 { 897 muge_dbg_printf(sc, "Initializing PHY.\n"); 898 uint16_t bmcr, lmsr; 899 usb_ticks_t start_ticks; 900 uint32_t hw_reg; 901 const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000); 902 903 MUGE_LOCK_ASSERT(sc, MA_OWNED); 904 905 /* Reset phy and wait for reset to complete. */ 906 lan78xx_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, 907 BMCR_RESET); 908 909 start_ticks = ticks; 910 do { 911 uether_pause(&sc->sc_ue, hz / 100); 912 bmcr = lan78xx_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, 913 MII_BMCR); 914 } while ((bmcr & BMCR_RESET) && ((ticks - start_ticks) < max_ticks)); 915 916 if (((usb_ticks_t)(ticks - start_ticks)) >= max_ticks) { 917 muge_err_printf(sc, "PHY reset timed-out\n"); 918 return (EIO); 919 } 920 921 /* Setup phy to interrupt upon link down or autoneg completion. */ 922 lan78xx_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, 923 MUGE_PHY_INTR_STAT); 924 lan78xx_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, 925 MUGE_PHY_INTR_MASK, 926 (MUGE_PHY_INTR_ANEG_COMP | MUGE_PHY_INTR_LINK_CHANGE)); 927 928 /* Enable Auto-MDIX for crossover and polarity detection. */ 929 lan78xx_set_mdix_auto(sc); 930 931 /* Enable all modes. */ 932 lan78xx_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_ANAR, 933 ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD | 934 ANAR_CSMA | ANAR_FC | ANAR_PAUSE_ASYM); 935 936 /* Restart auto-negotation. */ 937 bmcr |= BMCR_STARTNEG; 938 bmcr |= BMCR_AUTOEN; 939 lan78xx_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr); 940 bmcr = lan78xx_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR); 941 942 /* Configure LED Modes. */ 943 if (sc->sc_led_modes_mask != 0) { 944 lmsr = lan78xx_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, 945 MUGE_PHY_LED_MODE); 946 lmsr &= ~sc->sc_led_modes_mask; 947 lmsr |= sc->sc_led_modes; 948 lan78xx_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, 949 MUGE_PHY_LED_MODE, lmsr); 950 } 951 952 /* Enable appropriate LEDs. */ 953 if (sc->sc_leds != 0 && 954 lan78xx_read_reg(sc, ETH_HW_CFG, &hw_reg) == 0) { 955 hw_reg &= ~(ETH_HW_CFG_LEDO_EN_ | ETH_HW_CFG_LED1_EN_ | 956 ETH_HW_CFG_LED2_EN_ | ETH_HW_CFG_LED3_EN_ ); 957 hw_reg |= sc->sc_leds; 958 lan78xx_write_reg(sc, ETH_HW_CFG, hw_reg); 959 } 960 return (0); 961 } 962 963 /** 964 * lan78xx_chip_init - Initialises the chip after power on 965 * @sc: driver soft context 966 * 967 * This initialisation sequence is modelled on the procedure in the Linux 968 * driver. 969 * 970 * RETURNS: 971 * Returns 0 on success or an error code on failure. 972 */ 973 static int 974 lan78xx_chip_init(struct muge_softc *sc) 975 { 976 int err; 977 uint32_t buf; 978 uint32_t burst_cap; 979 980 MUGE_LOCK_ASSERT(sc, MA_OWNED); 981 982 /* Enter H/W config mode. */ 983 lan78xx_write_reg(sc, ETH_HW_CFG, ETH_HW_CFG_LRST_); 984 985 if ((err = lan78xx_wait_for_bits(sc, ETH_HW_CFG, ETH_HW_CFG_LRST_)) != 986 0) { 987 muge_warn_printf(sc, 988 "timed-out waiting for lite reset to complete\n"); 989 goto init_failed; 990 } 991 992 /* Set the mac address. */ 993 if ((err = lan78xx_setmacaddress(sc, sc->sc_ue.ue_eaddr)) != 0) { 994 muge_warn_printf(sc, "failed to set the MAC address\n"); 995 goto init_failed; 996 } 997 998 /* Read and display the revision register. */ 999 if ((err = lan78xx_read_reg(sc, ETH_ID_REV, &buf)) < 0) { 1000 muge_warn_printf(sc, "failed to read ETH_ID_REV (err = %d)\n", 1001 err); 1002 goto init_failed; 1003 } 1004 sc->chipid = (buf & ETH_ID_REV_CHIP_ID_MASK_) >> 16; 1005 sc->chiprev = buf & ETH_ID_REV_CHIP_REV_MASK_; 1006 switch (sc->chipid) { 1007 case ETH_ID_REV_CHIP_ID_7800_: 1008 case ETH_ID_REV_CHIP_ID_7850_: 1009 break; 1010 default: 1011 muge_warn_printf(sc, "Chip ID 0x%04x not yet supported\n", 1012 sc->chipid); 1013 goto init_failed; 1014 } 1015 device_printf(sc->sc_ue.ue_dev, "Chip ID 0x%04x rev %04x\n", sc->chipid, 1016 sc->chiprev); 1017 1018 /* Respond to BULK-IN tokens with a NAK when RX FIFO is empty. */ 1019 if ((err = lan78xx_read_reg(sc, ETH_USB_CFG0, &buf)) != 0) { 1020 muge_warn_printf(sc, "failed to read ETH_USB_CFG0 (err=%d)\n", err); 1021 goto init_failed; 1022 } 1023 buf |= ETH_USB_CFG_BIR_; 1024 lan78xx_write_reg(sc, ETH_USB_CFG0, buf); 1025 1026 /* 1027 * XXX LTM support will go here. 1028 */ 1029 1030 /* Configuring the burst cap. */ 1031 switch (usbd_get_speed(sc->sc_ue.ue_udev)) { 1032 case USB_SPEED_SUPER: 1033 burst_cap = MUGE_DEFAULT_BURST_CAP_SIZE/MUGE_SS_USB_PKT_SIZE; 1034 break; 1035 case USB_SPEED_HIGH: 1036 burst_cap = MUGE_DEFAULT_BURST_CAP_SIZE/MUGE_HS_USB_PKT_SIZE; 1037 break; 1038 default: 1039 burst_cap = MUGE_DEFAULT_BURST_CAP_SIZE/MUGE_FS_USB_PKT_SIZE; 1040 } 1041 1042 lan78xx_write_reg(sc, ETH_BURST_CAP, burst_cap); 1043 1044 /* Set the default bulk in delay (same value from Linux driver). */ 1045 lan78xx_write_reg(sc, ETH_BULK_IN_DLY, MUGE_DEFAULT_BULK_IN_DELAY); 1046 1047 /* Multiple ethernet frames per USB packets. */ 1048 err = lan78xx_read_reg(sc, ETH_HW_CFG, &buf); 1049 buf |= ETH_HW_CFG_MEF_; 1050 err = lan78xx_write_reg(sc, ETH_HW_CFG, buf); 1051 1052 /* Enable burst cap. */ 1053 if ((err = lan78xx_read_reg(sc, ETH_USB_CFG0, &buf)) < 0) { 1054 muge_warn_printf(sc, "failed to read ETH_USB_CFG0 (err=%d)\n", 1055 err); 1056 goto init_failed; 1057 } 1058 buf |= ETH_USB_CFG_BCE_; 1059 err = lan78xx_write_reg(sc, ETH_USB_CFG0, buf); 1060 1061 /* 1062 * Set FCL's RX and TX FIFO sizes: according to data sheet this is 1063 * already the default value. But we initialize it to the same value 1064 * anyways, as that's what the Linux driver does. 1065 * 1066 */ 1067 buf = (MUGE_MAX_RX_FIFO_SIZE - 512) / 512; 1068 err = lan78xx_write_reg(sc, ETH_FCT_RX_FIFO_END, buf); 1069 1070 buf = (MUGE_MAX_TX_FIFO_SIZE - 512) / 512; 1071 err = lan78xx_write_reg(sc, ETH_FCT_TX_FIFO_END, buf); 1072 1073 /* Enabling interrupts. (Not using them for now) */ 1074 err = lan78xx_write_reg(sc, ETH_INT_STS, ETH_INT_STS_CLEAR_ALL_); 1075 1076 /* 1077 * Initializing flow control registers to 0. These registers are 1078 * properly set is handled in link-reset function in the Linux driver. 1079 */ 1080 err = lan78xx_write_reg(sc, ETH_FLOW, 0); 1081 err = lan78xx_write_reg(sc, ETH_FCT_FLOW, 0); 1082 1083 /* 1084 * Settings for the RFE, we enable broadcast and destination address 1085 * perfect filtering. 1086 */ 1087 err = lan78xx_read_reg(sc, ETH_RFE_CTL, &buf); 1088 buf |= ETH_RFE_CTL_BCAST_EN_ | ETH_RFE_CTL_DA_PERFECT_; 1089 err = lan78xx_write_reg(sc, ETH_RFE_CTL, buf); 1090 1091 /* 1092 * At this point the Linux driver writes multicast tables, and enables 1093 * checksum engines. But in FreeBSD that gets done in muge_init, 1094 * which gets called when the interface is brought up. 1095 */ 1096 1097 /* Reset the PHY. */ 1098 lan78xx_write_reg(sc, ETH_PMT_CTL, ETH_PMT_CTL_PHY_RST_); 1099 if ((err = lan78xx_wait_for_bits(sc, ETH_PMT_CTL, 1100 ETH_PMT_CTL_PHY_RST_)) != 0) { 1101 muge_warn_printf(sc, 1102 "timed-out waiting for phy reset to complete\n"); 1103 goto init_failed; 1104 } 1105 1106 err = lan78xx_read_reg(sc, ETH_MAC_CR, &buf); 1107 if (sc->chipid == ETH_ID_REV_CHIP_ID_7800_ && 1108 !lan78xx_eeprom_present(sc)) { 1109 /* Set automatic duplex and speed on LAN7800 without EEPROM. */ 1110 buf |= ETH_MAC_CR_AUTO_DUPLEX_ | ETH_MAC_CR_AUTO_SPEED_; 1111 } 1112 err = lan78xx_write_reg(sc, ETH_MAC_CR, buf); 1113 1114 /* 1115 * Enable PHY interrupts (Not really getting used for now) 1116 * ETH_INT_EP_CTL: interrupt endpoint control register 1117 * phy events cause interrupts to be issued 1118 */ 1119 err = lan78xx_read_reg(sc, ETH_INT_EP_CTL, &buf); 1120 buf |= ETH_INT_ENP_PHY_INT; 1121 err = lan78xx_write_reg(sc, ETH_INT_EP_CTL, buf); 1122 1123 /* 1124 * Enables mac's transmitter. It will transmit frames from the buffer 1125 * onto the cable. 1126 */ 1127 err = lan78xx_read_reg(sc, ETH_MAC_TX, &buf); 1128 buf |= ETH_MAC_TX_TXEN_; 1129 err = lan78xx_write_reg(sc, ETH_MAC_TX, buf); 1130 1131 /* FIFO is capable of transmitting frames to MAC. */ 1132 err = lan78xx_read_reg(sc, ETH_FCT_TX_CTL, &buf); 1133 buf |= ETH_FCT_TX_CTL_EN_; 1134 err = lan78xx_write_reg(sc, ETH_FCT_TX_CTL, buf); 1135 1136 /* 1137 * Set max frame length. In linux this is dev->mtu (which by default 1138 * is 1500) + VLAN_ETH_HLEN = 1518. 1139 */ 1140 err = lan78xx_set_rx_max_frame_length(sc, ETHER_MAX_LEN); 1141 1142 /* Initialise the PHY. */ 1143 if ((err = lan78xx_phy_init(sc)) != 0) 1144 goto init_failed; 1145 1146 /* Enable MAC RX. */ 1147 err = lan78xx_read_reg(sc, ETH_MAC_RX, &buf); 1148 buf |= ETH_MAC_RX_EN_; 1149 err = lan78xx_write_reg(sc, ETH_MAC_RX, buf); 1150 1151 /* Enable FIFO controller RX. */ 1152 err = lan78xx_read_reg(sc, ETH_FCT_RX_CTL, &buf); 1153 buf |= ETH_FCT_TX_CTL_EN_; 1154 err = lan78xx_write_reg(sc, ETH_FCT_RX_CTL, buf); 1155 1156 sc->sc_flags |= MUGE_FLAG_INIT_DONE; 1157 return (0); 1158 1159 init_failed: 1160 muge_err_printf(sc, "lan78xx_chip_init failed (err=%d)\n", err); 1161 return (err); 1162 } 1163 1164 static void 1165 muge_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 1166 { 1167 struct muge_softc *sc = usbd_xfer_softc(xfer); 1168 struct usb_ether *ue = &sc->sc_ue; 1169 struct ifnet *ifp = uether_getifp(ue); 1170 struct mbuf *m; 1171 struct usb_page_cache *pc; 1172 uint16_t pktlen; 1173 uint32_t rx_cmd_a, rx_cmd_b; 1174 uint16_t rx_cmd_c; 1175 int off; 1176 int actlen; 1177 1178 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1179 muge_dbg_printf(sc, "rx : actlen %d\n", actlen); 1180 1181 switch (USB_GET_STATE(xfer)) { 1182 case USB_ST_TRANSFERRED: 1183 /* 1184 * There is always a zero length frame after bringing the 1185 * interface up. 1186 */ 1187 if (actlen < (sizeof(rx_cmd_a) + ETHER_CRC_LEN)) 1188 goto tr_setup; 1189 1190 /* 1191 * There may be multiple packets in the USB frame. Each will 1192 * have a header and each needs to have its own mbuf allocated 1193 * and populated for it. 1194 */ 1195 pc = usbd_xfer_get_frame(xfer, 0); 1196 off = 0; 1197 1198 while (off < actlen) { 1199 /* The frame header is aligned on a 4 byte boundary. */ 1200 off = ((off + 0x3) & ~0x3); 1201 1202 /* Extract RX CMD A. */ 1203 if (off + sizeof(rx_cmd_a) > actlen) 1204 goto tr_setup; 1205 usbd_copy_out(pc, off, &rx_cmd_a, sizeof(rx_cmd_a)); 1206 off += (sizeof(rx_cmd_a)); 1207 rx_cmd_a = le32toh(rx_cmd_a); 1208 1209 /* Extract RX CMD B. */ 1210 if (off + sizeof(rx_cmd_b) > actlen) 1211 goto tr_setup; 1212 usbd_copy_out(pc, off, &rx_cmd_b, sizeof(rx_cmd_b)); 1213 off += (sizeof(rx_cmd_b)); 1214 rx_cmd_b = le32toh(rx_cmd_b); 1215 1216 /* Extract RX CMD C. */ 1217 if (off + sizeof(rx_cmd_c) > actlen) 1218 goto tr_setup; 1219 usbd_copy_out(pc, off, &rx_cmd_c, sizeof(rx_cmd_c)); 1220 off += (sizeof(rx_cmd_c)); 1221 rx_cmd_c = le16toh(rx_cmd_c); 1222 1223 if (off > actlen) 1224 goto tr_setup; 1225 1226 pktlen = (rx_cmd_a & RX_CMD_A_LEN_MASK_); 1227 1228 muge_dbg_printf(sc, 1229 "rx_cmd_a 0x%08x rx_cmd_b 0x%08x rx_cmd_c 0x%04x " 1230 " pktlen %d actlen %d off %d\n", 1231 rx_cmd_a, rx_cmd_b, rx_cmd_c, pktlen, actlen, off); 1232 1233 if (rx_cmd_a & RX_CMD_A_RED_) { 1234 muge_dbg_printf(sc, 1235 "rx error (hdr 0x%08x)\n", rx_cmd_a); 1236 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 1237 } else { 1238 /* Ethernet frame too big or too small? */ 1239 if ((pktlen < ETHER_HDR_LEN) || 1240 (pktlen > (actlen - off))) 1241 goto tr_setup; 1242 1243 /* Create a new mbuf to store the packet. */ 1244 m = uether_newbuf(); 1245 if (m == NULL) { 1246 muge_warn_printf(sc, 1247 "failed to create new mbuf\n"); 1248 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1249 1); 1250 goto tr_setup; 1251 } 1252 1253 usbd_copy_out(pc, off, mtod(m, uint8_t *), 1254 pktlen); 1255 1256 /* 1257 * Check if RX checksums are computed, and 1258 * offload them 1259 */ 1260 if ((ifp->if_capenable & IFCAP_RXCSUM) && 1261 !(rx_cmd_a & RX_CMD_A_ICSM_)) { 1262 struct ether_header *eh; 1263 eh = mtod(m, struct ether_header *); 1264 /* 1265 * Remove the extra 2 bytes of the csum 1266 * 1267 * The checksum appears to be 1268 * simplistically calculated over the 1269 * protocol headers up to the end of the 1270 * eth frame. Which means if the eth 1271 * frame is padded the csum calculation 1272 * is incorrectly performed over the 1273 * padding bytes as well. Therefore to 1274 * be safe we ignore the H/W csum on 1275 * frames less than or equal to 1276 * 64 bytes. 1277 * 1278 * Protocols checksummed: 1279 * TCP, UDP, ICMP, IGMP, IP 1280 */ 1281 if (pktlen > ETHER_MIN_LEN) { 1282 m->m_pkthdr.csum_flags |= 1283 CSUM_DATA_VALID | 1284 CSUM_PSEUDO_HDR; 1285 1286 /* 1287 * Copy the checksum from the 1288 * last 2 bytes of the transfer 1289 * and put in the csum_data 1290 * field. 1291 */ 1292 usbd_copy_out(pc, 1293 (off + pktlen), 1294 &m->m_pkthdr.csum_data, 2); 1295 1296 /* 1297 * The data is copied in network 1298 * order, but the csum algorithm 1299 * in the kernel expects it to 1300 * be in host network order. 1301 */ 1302 m->m_pkthdr.csum_data = 1303 ntohs(0xffff); 1304 1305 muge_dbg_printf(sc, 1306 "RX checksum offloaded (0x%04x)\n", 1307 m->m_pkthdr.csum_data); 1308 } 1309 } 1310 1311 /* Enqueue the mbuf on the receive queue. */ 1312 if (pktlen < (4 + ETHER_HDR_LEN)) { 1313 m_freem(m); 1314 goto tr_setup; 1315 } 1316 /* Remove 4 trailing bytes */ 1317 uether_rxmbuf(ue, m, pktlen - 4); 1318 } 1319 1320 /* 1321 * Update the offset to move to the next potential 1322 * packet. 1323 */ 1324 off += pktlen; 1325 } 1326 /* FALLTHROUGH */ 1327 case USB_ST_SETUP: 1328 tr_setup: 1329 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1330 usbd_transfer_submit(xfer); 1331 uether_rxflush(ue); 1332 return; 1333 default: 1334 if (error != USB_ERR_CANCELLED) { 1335 muge_warn_printf(sc, "bulk read error, %s\n", 1336 usbd_errstr(error)); 1337 usbd_xfer_set_stall(xfer); 1338 goto tr_setup; 1339 } 1340 return; 1341 } 1342 } 1343 1344 /** 1345 * muge_bulk_write_callback - Write callback used to send ethernet frame(s) 1346 * @xfer: the USB transfer 1347 * @error: error code if the transfers is in an errored state 1348 * 1349 * The main write function that pulls ethernet frames off the queue and 1350 * sends them out. 1351 * 1352 */ 1353 static void 1354 muge_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 1355 { 1356 struct muge_softc *sc = usbd_xfer_softc(xfer); 1357 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 1358 struct usb_page_cache *pc; 1359 struct mbuf *m; 1360 int nframes; 1361 uint32_t frm_len = 0, tx_cmd_a = 0, tx_cmd_b = 0; 1362 1363 switch (USB_GET_STATE(xfer)) { 1364 case USB_ST_TRANSFERRED: 1365 muge_dbg_printf(sc, 1366 "USB TRANSFER status: USB_ST_TRANSFERRED\n"); 1367 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1368 /* FALLTHROUGH */ 1369 case USB_ST_SETUP: 1370 muge_dbg_printf(sc, "USB TRANSFER status: USB_ST_SETUP\n"); 1371 tr_setup: 1372 if ((sc->sc_flags & MUGE_FLAG_LINK) == 0 || 1373 (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) { 1374 muge_dbg_printf(sc, 1375 "sc->sc_flags & MUGE_FLAG_LINK: %d\n", 1376 (sc->sc_flags & MUGE_FLAG_LINK)); 1377 muge_dbg_printf(sc, 1378 "ifp->if_drv_flags & IFF_DRV_OACTIVE: %d\n", 1379 (ifp->if_drv_flags & IFF_DRV_OACTIVE)); 1380 muge_dbg_printf(sc, 1381 "USB TRANSFER not sending: no link or controller is busy \n"); 1382 /* 1383 * Don't send anything if there is no link or 1384 * controller is busy. 1385 */ 1386 return; 1387 } 1388 for (nframes = 0; 1389 nframes < 16 && !IFQ_DRV_IS_EMPTY(&ifp->if_snd); 1390 nframes++) { 1391 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1392 if (m == NULL) 1393 break; 1394 usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES, 1395 nframes); 1396 frm_len = 0; 1397 pc = usbd_xfer_get_frame(xfer, nframes); 1398 1399 /* 1400 * Each frame is prefixed with two 32-bit values 1401 * describing the length of the packet and buffer. 1402 */ 1403 tx_cmd_a = (m->m_pkthdr.len & TX_CMD_A_LEN_MASK_) | 1404 TX_CMD_A_FCS_; 1405 tx_cmd_a = htole32(tx_cmd_a); 1406 usbd_copy_in(pc, 0, &tx_cmd_a, sizeof(tx_cmd_a)); 1407 1408 tx_cmd_b = 0; 1409 1410 /* TCP LSO Support will probably be implemented here. */ 1411 tx_cmd_b = htole32(tx_cmd_b); 1412 usbd_copy_in(pc, 4, &tx_cmd_b, sizeof(tx_cmd_b)); 1413 1414 frm_len += 8; 1415 1416 /* Next copy in the actual packet */ 1417 usbd_m_copy_in(pc, frm_len, m, 0, m->m_pkthdr.len); 1418 frm_len += m->m_pkthdr.len; 1419 1420 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1421 1422 /* 1423 * If there's a BPF listener, bounce a copy of this 1424 * frame to it. 1425 */ 1426 BPF_MTAP(ifp, m); 1427 m_freem(m); 1428 1429 /* Set frame length. */ 1430 usbd_xfer_set_frame_len(xfer, nframes, frm_len); 1431 } 1432 1433 muge_dbg_printf(sc, "USB TRANSFER nframes: %d\n", nframes); 1434 if (nframes != 0) { 1435 muge_dbg_printf(sc, "USB TRANSFER submit attempt\n"); 1436 usbd_xfer_set_frames(xfer, nframes); 1437 usbd_transfer_submit(xfer); 1438 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1439 } 1440 return; 1441 1442 default: 1443 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 1444 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1445 1446 if (error != USB_ERR_CANCELLED) { 1447 muge_err_printf(sc, 1448 "usb error on tx: %s\n", usbd_errstr(error)); 1449 usbd_xfer_set_stall(xfer); 1450 goto tr_setup; 1451 } 1452 return; 1453 } 1454 } 1455 1456 /** 1457 * muge_set_mac_addr - Initiailizes NIC MAC address 1458 * @ue: the USB ethernet device 1459 * 1460 * Tries to obtain MAC address from number of sources: registers, 1461 * EEPROM, DTB blob. If all sources fail - generates random MAC. 1462 */ 1463 static void 1464 muge_set_mac_addr(struct usb_ether *ue) 1465 { 1466 struct muge_softc *sc = uether_getsc(ue); 1467 uint32_t mac_h, mac_l; 1468 1469 memset(ue->ue_eaddr, 0xff, ETHER_ADDR_LEN); 1470 1471 uint32_t val; 1472 lan78xx_read_reg(sc, 0, &val); 1473 1474 /* Read current MAC address from RX_ADDRx registers. */ 1475 if ((lan78xx_read_reg(sc, ETH_RX_ADDRL, &mac_l) == 0) && 1476 (lan78xx_read_reg(sc, ETH_RX_ADDRH, &mac_h) == 0)) { 1477 ue->ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff); 1478 ue->ue_eaddr[4] = (uint8_t)((mac_h) & 0xff); 1479 ue->ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff); 1480 ue->ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff); 1481 ue->ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff); 1482 ue->ue_eaddr[0] = (uint8_t)((mac_l) & 0xff); 1483 } 1484 1485 /* 1486 * If RX_ADDRx did not provide a valid MAC address, try EEPROM. If that 1487 * doesn't work, try OTP. Whether any of these methods work or not, try 1488 * FDT data, because it is allowed to override the EEPROM/OTP values. 1489 */ 1490 if (ETHER_IS_VALID(ue->ue_eaddr)) { 1491 muge_dbg_printf(sc, "MAC assigned from registers\n"); 1492 } else if (lan78xx_eeprom_present(sc) && lan78xx_eeprom_read_raw(sc, 1493 ETH_E2P_MAC_OFFSET, ue->ue_eaddr, ETHER_ADDR_LEN) == 0 && 1494 ETHER_IS_VALID(ue->ue_eaddr)) { 1495 muge_dbg_printf(sc, "MAC assigned from EEPROM\n"); 1496 } else if (lan78xx_otp_read(sc, OTP_MAC_OFFSET, ue->ue_eaddr, 1497 ETHER_ADDR_LEN) == 0 && ETHER_IS_VALID(ue->ue_eaddr)) { 1498 muge_dbg_printf(sc, "MAC assigned from OTP\n"); 1499 } 1500 1501 #ifdef FDT 1502 /* ue->ue_eaddr modified only if config exists for this dev instance. */ 1503 usb_fdt_get_mac_addr(ue->ue_dev, ue); 1504 if (ETHER_IS_VALID(ue->ue_eaddr)) { 1505 muge_dbg_printf(sc, "MAC assigned from FDT data\n"); 1506 } 1507 #endif 1508 1509 if (!ETHER_IS_VALID(ue->ue_eaddr)) { 1510 muge_dbg_printf(sc, "MAC assigned randomly\n"); 1511 arc4rand(ue->ue_eaddr, ETHER_ADDR_LEN, 0); 1512 ue->ue_eaddr[0] &= ~0x01; /* unicast */ 1513 ue->ue_eaddr[0] |= 0x02; /* locally administered */ 1514 } 1515 } 1516 1517 /** 1518 * muge_set_leds - Initializes NIC LEDs pattern 1519 * @ue: the USB ethernet device 1520 * 1521 * Tries to store the LED modes. 1522 * Supports only DTB blob like the Linux driver does. 1523 */ 1524 static void 1525 muge_set_leds(struct usb_ether *ue) 1526 { 1527 #ifdef FDT 1528 struct muge_softc *sc = uether_getsc(ue); 1529 phandle_t node; 1530 pcell_t modes[4]; /* 4 LEDs are possible */ 1531 ssize_t proplen; 1532 uint32_t count; 1533 1534 if ((node = usb_fdt_get_node(ue->ue_dev, ue->ue_udev)) != -1 && 1535 (proplen = OF_getencprop(node, "microchip,led-modes", modes, 1536 sizeof(modes))) > 0) { 1537 count = proplen / sizeof( uint32_t ); 1538 sc->sc_leds = (count > 0) * ETH_HW_CFG_LEDO_EN_ | 1539 (count > 1) * ETH_HW_CFG_LED1_EN_ | 1540 (count > 2) * ETH_HW_CFG_LED2_EN_ | 1541 (count > 3) * ETH_HW_CFG_LED3_EN_; 1542 while (count-- > 0) { 1543 sc->sc_led_modes |= (modes[count] & 0xf) << (4 * count); 1544 sc->sc_led_modes_mask |= 0xf << (4 * count); 1545 } 1546 muge_dbg_printf(sc, "LED modes set from FDT data\n"); 1547 } 1548 #endif 1549 } 1550 1551 /** 1552 * muge_attach_post - Called after the driver attached to the USB interface 1553 * @ue: the USB ethernet device 1554 * 1555 * This is where the chip is intialised for the first time. This is 1556 * different from the muge_init() function in that that one is designed to 1557 * setup the H/W to match the UE settings and can be called after a reset. 1558 * 1559 */ 1560 static void 1561 muge_attach_post(struct usb_ether *ue) 1562 { 1563 struct muge_softc *sc = uether_getsc(ue); 1564 1565 muge_dbg_printf(sc, "Calling muge_attach_post.\n"); 1566 1567 /* Setup some of the basics */ 1568 sc->sc_phyno = 1; 1569 1570 muge_set_mac_addr(ue); 1571 muge_set_leds(ue); 1572 1573 /* Initialise the chip for the first time */ 1574 lan78xx_chip_init(sc); 1575 } 1576 1577 /** 1578 * muge_attach_post_sub - Called after attach to the USB interface 1579 * @ue: the USB ethernet device 1580 * 1581 * Most of this is boilerplate code and copied from the base USB ethernet 1582 * driver. It has been overriden so that we can indicate to the system 1583 * that the chip supports H/W checksumming. 1584 * 1585 * RETURNS: 1586 * Returns 0 on success or a negative error code. 1587 */ 1588 static int 1589 muge_attach_post_sub(struct usb_ether *ue) 1590 { 1591 struct muge_softc *sc; 1592 struct ifnet *ifp; 1593 int error; 1594 1595 sc = uether_getsc(ue); 1596 muge_dbg_printf(sc, "Calling muge_attach_post_sub.\n"); 1597 ifp = ue->ue_ifp; 1598 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1599 ifp->if_start = uether_start; 1600 ifp->if_ioctl = muge_ioctl; 1601 ifp->if_init = uether_init; 1602 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 1603 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 1604 IFQ_SET_READY(&ifp->if_snd); 1605 1606 /* 1607 * The chip supports TCP/UDP checksum offloading on TX and RX paths, 1608 * however currently only RX checksum is supported in the driver 1609 * (see top of file). 1610 */ 1611 ifp->if_capabilities |= IFCAP_VLAN_MTU; 1612 ifp->if_hwassist = 0; 1613 ifp->if_capabilities |= IFCAP_RXCSUM; 1614 1615 if (MUGE_DEFAULT_TX_CSUM_ENABLE) 1616 ifp->if_capabilities |= IFCAP_TXCSUM; 1617 1618 /* 1619 * In the Linux driver they also enable scatter/gather (NETIF_F_SG) 1620 * here, that's something related to socket buffers used in Linux. 1621 * FreeBSD doesn't have that as an interface feature. 1622 */ 1623 if (MUGE_DEFAULT_TSO_ENABLE) 1624 ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_TSO6; 1625 1626 #if 0 1627 /* TX checksuming is disabled since not yet implemented. */ 1628 ifp->if_capabilities |= IFCAP_TXCSUM; 1629 ifp->if_capenable |= IFCAP_TXCSUM; 1630 ifp->if_hwassist = CSUM_TCP | CSUM_UDP; 1631 #endif 1632 1633 ifp->if_capenable = ifp->if_capabilities; 1634 1635 mtx_lock(&Giant); 1636 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, uether_ifmedia_upd, 1637 ue->ue_methods->ue_mii_sts, BMSR_DEFCAPMASK, sc->sc_phyno, 1638 MII_OFFSET_ANY, 0); 1639 mtx_unlock(&Giant); 1640 1641 return (0); 1642 } 1643 1644 /** 1645 * muge_start - Starts communication with the LAN78xx chip 1646 * @ue: USB ether interface 1647 */ 1648 static void 1649 muge_start(struct usb_ether *ue) 1650 { 1651 struct muge_softc *sc = uether_getsc(ue); 1652 1653 /* 1654 * Start the USB transfers, if not already started. 1655 */ 1656 usbd_transfer_start(sc->sc_xfer[MUGE_BULK_DT_RD]); 1657 usbd_transfer_start(sc->sc_xfer[MUGE_BULK_DT_WR]); 1658 } 1659 1660 /** 1661 * muge_ioctl - ioctl function for the device 1662 * @ifp: interface pointer 1663 * @cmd: the ioctl command 1664 * @data: data passed in the ioctl call, typically a pointer to struct 1665 * ifreq. 1666 * 1667 * The ioctl routine is overridden to detect change requests for the H/W 1668 * checksum capabilities. 1669 * 1670 * RETURNS: 1671 * 0 on success and an error code on failure. 1672 */ 1673 static int 1674 muge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1675 { 1676 struct usb_ether *ue = ifp->if_softc; 1677 struct muge_softc *sc; 1678 struct ifreq *ifr; 1679 int rc; 1680 int mask; 1681 int reinit; 1682 1683 if (cmd == SIOCSIFCAP) { 1684 sc = uether_getsc(ue); 1685 ifr = (struct ifreq *)data; 1686 1687 MUGE_LOCK(sc); 1688 1689 rc = 0; 1690 reinit = 0; 1691 1692 mask = ifr->ifr_reqcap ^ ifp->if_capenable; 1693 1694 /* Modify the RX CSUM enable bits. */ 1695 if ((mask & IFCAP_RXCSUM) != 0 && 1696 (ifp->if_capabilities & IFCAP_RXCSUM) != 0) { 1697 ifp->if_capenable ^= IFCAP_RXCSUM; 1698 1699 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1700 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1701 reinit = 1; 1702 } 1703 } 1704 1705 MUGE_UNLOCK(sc); 1706 if (reinit) 1707 uether_init(ue); 1708 } else { 1709 rc = uether_ioctl(ifp, cmd, data); 1710 } 1711 1712 return (rc); 1713 } 1714 1715 /** 1716 * muge_reset - Reset the SMSC chip 1717 * @sc: device soft context 1718 * 1719 * LOCKING: 1720 * Should be called with the SMSC lock held. 1721 */ 1722 static void 1723 muge_reset(struct muge_softc *sc) 1724 { 1725 struct usb_config_descriptor *cd; 1726 usb_error_t err; 1727 1728 cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev); 1729 1730 err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx, 1731 cd->bConfigurationValue); 1732 if (err) 1733 muge_warn_printf(sc, "reset failed (ignored)\n"); 1734 1735 /* Wait a little while for the chip to get its brains in order. */ 1736 uether_pause(&sc->sc_ue, hz / 100); 1737 1738 /* Reinitialize controller to achieve full reset. */ 1739 lan78xx_chip_init(sc); 1740 } 1741 1742 /** 1743 * muge_set_addr_filter 1744 * 1745 * @sc: device soft context 1746 * @index: index of the entry to the perfect address table 1747 * @addr: address to be written 1748 * 1749 */ 1750 static void 1751 muge_set_addr_filter(struct muge_softc *sc, int index, 1752 uint8_t addr[ETHER_ADDR_LEN]) 1753 { 1754 uint32_t tmp; 1755 1756 if ((sc) && (index > 0) && (index < MUGE_NUM_PFILTER_ADDRS_)) { 1757 tmp = addr[3]; 1758 tmp |= addr[2] | (tmp << 8); 1759 tmp |= addr[1] | (tmp << 8); 1760 tmp |= addr[0] | (tmp << 8); 1761 sc->sc_pfilter_table[index][1] = tmp; 1762 tmp = addr[5]; 1763 tmp |= addr[4] | (tmp << 8); 1764 tmp |= ETH_MAF_HI_VALID_ | ETH_MAF_HI_TYPE_DST_; 1765 sc->sc_pfilter_table[index][0] = tmp; 1766 } 1767 } 1768 1769 /** 1770 * lan78xx_dataport_write - write to the selected RAM 1771 * @sc: The device soft context. 1772 * @ram_select: Select which RAM to access. 1773 * @addr: Starting address to write to. 1774 * @buf: word-sized buffer to write to RAM, starting at @addr. 1775 * @length: length of @buf 1776 * 1777 * 1778 * RETURNS: 1779 * 0 if write successful. 1780 */ 1781 static int 1782 lan78xx_dataport_write(struct muge_softc *sc, uint32_t ram_select, 1783 uint32_t addr, uint32_t length, uint32_t *buf) 1784 { 1785 uint32_t dp_sel; 1786 int i, ret; 1787 1788 MUGE_LOCK_ASSERT(sc, MA_OWNED); 1789 ret = lan78xx_wait_for_bits(sc, ETH_DP_SEL, ETH_DP_SEL_DPRDY_); 1790 if (ret < 0) 1791 goto done; 1792 1793 ret = lan78xx_read_reg(sc, ETH_DP_SEL, &dp_sel); 1794 1795 dp_sel &= ~ETH_DP_SEL_RSEL_MASK_; 1796 dp_sel |= ram_select; 1797 1798 ret = lan78xx_write_reg(sc, ETH_DP_SEL, dp_sel); 1799 1800 for (i = 0; i < length; i++) { 1801 ret = lan78xx_write_reg(sc, ETH_DP_ADDR, addr + i); 1802 ret = lan78xx_write_reg(sc, ETH_DP_DATA, buf[i]); 1803 ret = lan78xx_write_reg(sc, ETH_DP_CMD, ETH_DP_CMD_WRITE_); 1804 ret = lan78xx_wait_for_bits(sc, ETH_DP_SEL, ETH_DP_SEL_DPRDY_); 1805 if (ret != 0) 1806 goto done; 1807 } 1808 1809 done: 1810 return (ret); 1811 } 1812 1813 /** 1814 * muge_multicast_write 1815 * @sc: device's soft context 1816 * 1817 * Writes perfect addres filters and hash address filters to their 1818 * corresponding registers and RAMs. 1819 * 1820 */ 1821 static void 1822 muge_multicast_write(struct muge_softc *sc) 1823 { 1824 int i, ret; 1825 lan78xx_dataport_write(sc, ETH_DP_SEL_RSEL_VLAN_DA_, 1826 ETH_DP_SEL_VHF_VLAN_LEN, ETH_DP_SEL_VHF_HASH_LEN, 1827 sc->sc_mchash_table); 1828 1829 for (i = 1; i < MUGE_NUM_PFILTER_ADDRS_; i++) { 1830 ret = lan78xx_write_reg(sc, PFILTER_HI(i), 0); 1831 ret = lan78xx_write_reg(sc, PFILTER_LO(i), 1832 sc->sc_pfilter_table[i][1]); 1833 ret = lan78xx_write_reg(sc, PFILTER_HI(i), 1834 sc->sc_pfilter_table[i][0]); 1835 } 1836 } 1837 1838 /** 1839 * muge_hash - Calculate the hash of a mac address 1840 * @addr: The mac address to calculate the hash on 1841 * 1842 * This function is used when configuring a range of multicast mac 1843 * addresses to filter on. The hash of the mac address is put in the 1844 * device's mac hash table. 1845 * 1846 * RETURNS: 1847 * Returns a value from 0-63 value which is the hash of the mac address. 1848 */ 1849 static inline uint32_t 1850 muge_hash(uint8_t addr[ETHER_ADDR_LEN]) 1851 { 1852 return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 23) & 0x1ff; 1853 } 1854 1855 static u_int 1856 muge_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt) 1857 { 1858 struct muge_softc *sc = arg; 1859 uint32_t bitnum; 1860 1861 /* First fill up the perfect address table. */ 1862 if (cnt < 32 /* XXX */) 1863 muge_set_addr_filter(sc, cnt + 1, LLADDR(sdl)); 1864 else { 1865 bitnum = muge_hash(LLADDR(sdl)); 1866 sc->sc_mchash_table[bitnum / 32] |= (1 << (bitnum % 32)); 1867 sc->sc_rfe_ctl |= ETH_RFE_CTL_MCAST_HASH_; 1868 } 1869 1870 return (1); 1871 } 1872 1873 /** 1874 * muge_setmulti - Setup multicast 1875 * @ue: usb ethernet device context 1876 * 1877 * Tells the device to either accept frames with a multicast mac address, 1878 * a select group of m'cast mac addresses or just the devices mac address. 1879 * 1880 * LOCKING: 1881 * Should be called with the MUGE lock held. 1882 */ 1883 static void 1884 muge_setmulti(struct usb_ether *ue) 1885 { 1886 struct muge_softc *sc = uether_getsc(ue); 1887 struct ifnet *ifp = uether_getifp(ue); 1888 uint8_t i; 1889 1890 MUGE_LOCK_ASSERT(sc, MA_OWNED); 1891 1892 sc->sc_rfe_ctl &= ~(ETH_RFE_CTL_UCAST_EN_ | ETH_RFE_CTL_MCAST_EN_ | 1893 ETH_RFE_CTL_DA_PERFECT_ | ETH_RFE_CTL_MCAST_HASH_); 1894 1895 /* Initialize hash filter table. */ 1896 for (i = 0; i < ETH_DP_SEL_VHF_HASH_LEN; i++) 1897 sc->sc_mchash_table[i] = 0; 1898 1899 /* Initialize perfect filter table. */ 1900 for (i = 1; i < MUGE_NUM_PFILTER_ADDRS_; i++) { 1901 sc->sc_pfilter_table[i][0] = sc->sc_pfilter_table[i][1] = 0; 1902 } 1903 1904 sc->sc_rfe_ctl |= ETH_RFE_CTL_BCAST_EN_; 1905 1906 if (ifp->if_flags & IFF_PROMISC) { 1907 muge_dbg_printf(sc, "promiscuous mode enabled\n"); 1908 sc->sc_rfe_ctl |= ETH_RFE_CTL_MCAST_EN_ | ETH_RFE_CTL_UCAST_EN_; 1909 } else if (ifp->if_flags & IFF_ALLMULTI) { 1910 muge_dbg_printf(sc, "receive all multicast enabled\n"); 1911 sc->sc_rfe_ctl |= ETH_RFE_CTL_MCAST_EN_; 1912 } else { 1913 if_foreach_llmaddr(ifp, muge_hash_maddr, sc); 1914 muge_multicast_write(sc); 1915 } 1916 lan78xx_write_reg(sc, ETH_RFE_CTL, sc->sc_rfe_ctl); 1917 } 1918 1919 /** 1920 * muge_setpromisc - Enables/disables promiscuous mode 1921 * @ue: usb ethernet device context 1922 * 1923 * LOCKING: 1924 * Should be called with the MUGE lock held. 1925 */ 1926 static void 1927 muge_setpromisc(struct usb_ether *ue) 1928 { 1929 struct muge_softc *sc = uether_getsc(ue); 1930 struct ifnet *ifp = uether_getifp(ue); 1931 1932 muge_dbg_printf(sc, "promiscuous mode %sabled\n", 1933 (ifp->if_flags & IFF_PROMISC) ? "en" : "dis"); 1934 1935 MUGE_LOCK_ASSERT(sc, MA_OWNED); 1936 1937 if (ifp->if_flags & IFF_PROMISC) 1938 sc->sc_rfe_ctl |= ETH_RFE_CTL_MCAST_EN_ | ETH_RFE_CTL_UCAST_EN_; 1939 else 1940 sc->sc_rfe_ctl &= ~(ETH_RFE_CTL_MCAST_EN_); 1941 1942 lan78xx_write_reg(sc, ETH_RFE_CTL, sc->sc_rfe_ctl); 1943 } 1944 1945 /** 1946 * muge_sethwcsum - Enable or disable H/W UDP and TCP checksumming 1947 * @sc: driver soft context 1948 * 1949 * LOCKING: 1950 * Should be called with the MUGE lock held. 1951 * 1952 * RETURNS: 1953 * Returns 0 on success or a negative error code. 1954 */ 1955 static int 1956 muge_sethwcsum(struct muge_softc *sc) 1957 { 1958 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 1959 int err; 1960 1961 if (!ifp) 1962 return (-EIO); 1963 1964 MUGE_LOCK_ASSERT(sc, MA_OWNED); 1965 1966 if (ifp->if_capenable & IFCAP_RXCSUM) { 1967 sc->sc_rfe_ctl |= ETH_RFE_CTL_IGMP_COE_ | ETH_RFE_CTL_ICMP_COE_; 1968 sc->sc_rfe_ctl |= ETH_RFE_CTL_TCPUDP_COE_ | ETH_RFE_CTL_IP_COE_; 1969 } else { 1970 sc->sc_rfe_ctl &= 1971 ~(ETH_RFE_CTL_IGMP_COE_ | ETH_RFE_CTL_ICMP_COE_); 1972 sc->sc_rfe_ctl &= 1973 ~(ETH_RFE_CTL_TCPUDP_COE_ | ETH_RFE_CTL_IP_COE_); 1974 } 1975 1976 sc->sc_rfe_ctl &= ~ETH_RFE_CTL_VLAN_FILTER_; 1977 1978 err = lan78xx_write_reg(sc, ETH_RFE_CTL, sc->sc_rfe_ctl); 1979 1980 if (err != 0) { 1981 muge_warn_printf(sc, "failed to write ETH_RFE_CTL (err=%d)\n", 1982 err); 1983 return (err); 1984 } 1985 1986 return (0); 1987 } 1988 1989 /** 1990 * muge_ifmedia_upd - Set media options 1991 * @ifp: interface pointer 1992 * 1993 * Basically boilerplate code that simply calls the mii functions to set 1994 * the media options. 1995 * 1996 * LOCKING: 1997 * The device lock must be held before this function is called. 1998 * 1999 * RETURNS: 2000 * Returns 0 on success or a negative error code. 2001 */ 2002 static int 2003 muge_ifmedia_upd(struct ifnet *ifp) 2004 { 2005 struct muge_softc *sc = ifp->if_softc; 2006 muge_dbg_printf(sc, "Calling muge_ifmedia_upd.\n"); 2007 struct mii_data *mii = uether_getmii(&sc->sc_ue); 2008 struct mii_softc *miisc; 2009 int err; 2010 2011 MUGE_LOCK_ASSERT(sc, MA_OWNED); 2012 2013 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 2014 PHY_RESET(miisc); 2015 err = mii_mediachg(mii); 2016 return (err); 2017 } 2018 2019 /** 2020 * muge_init - Initialises the LAN95xx chip 2021 * @ue: USB ether interface 2022 * 2023 * Called when the interface is brought up (i.e. ifconfig ue0 up), this 2024 * initialise the interface and the rx/tx pipes. 2025 * 2026 * LOCKING: 2027 * Should be called with the MUGE lock held. 2028 */ 2029 static void 2030 muge_init(struct usb_ether *ue) 2031 { 2032 struct muge_softc *sc = uether_getsc(ue); 2033 muge_dbg_printf(sc, "Calling muge_init.\n"); 2034 struct ifnet *ifp = uether_getifp(ue); 2035 MUGE_LOCK_ASSERT(sc, MA_OWNED); 2036 2037 if (lan78xx_setmacaddress(sc, IF_LLADDR(ifp))) 2038 muge_dbg_printf(sc, "setting MAC address failed\n"); 2039 2040 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 2041 return; 2042 2043 /* Cancel pending I/O. */ 2044 muge_stop(ue); 2045 2046 /* Reset the ethernet interface. */ 2047 muge_reset(sc); 2048 2049 /* Load the multicast filter. */ 2050 muge_setmulti(ue); 2051 2052 /* TCP/UDP checksum offload engines. */ 2053 muge_sethwcsum(sc); 2054 2055 usbd_xfer_set_stall(sc->sc_xfer[MUGE_BULK_DT_WR]); 2056 2057 /* Indicate we are up and running. */ 2058 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2059 2060 /* Switch to selected media. */ 2061 muge_ifmedia_upd(ifp); 2062 muge_start(ue); 2063 } 2064 2065 /** 2066 * muge_stop - Stops communication with the LAN78xx chip 2067 * @ue: USB ether interface 2068 */ 2069 static void 2070 muge_stop(struct usb_ether *ue) 2071 { 2072 struct muge_softc *sc = uether_getsc(ue); 2073 struct ifnet *ifp = uether_getifp(ue); 2074 2075 MUGE_LOCK_ASSERT(sc, MA_OWNED); 2076 2077 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2078 sc->sc_flags &= ~MUGE_FLAG_LINK; 2079 2080 /* 2081 * Stop all the transfers, if not already stopped. 2082 */ 2083 usbd_transfer_stop(sc->sc_xfer[MUGE_BULK_DT_WR]); 2084 usbd_transfer_stop(sc->sc_xfer[MUGE_BULK_DT_RD]); 2085 } 2086 2087 /** 2088 * muge_tick - Called periodically to monitor the state of the LAN95xx chip 2089 * @ue: USB ether interface 2090 * 2091 * Simply calls the mii status functions to check the state of the link. 2092 * 2093 * LOCKING: 2094 * Should be called with the MUGE lock held. 2095 */ 2096 static void 2097 muge_tick(struct usb_ether *ue) 2098 { 2099 2100 struct muge_softc *sc = uether_getsc(ue); 2101 struct mii_data *mii = uether_getmii(&sc->sc_ue); 2102 2103 MUGE_LOCK_ASSERT(sc, MA_OWNED); 2104 2105 mii_tick(mii); 2106 if ((sc->sc_flags & MUGE_FLAG_LINK) == 0) { 2107 lan78xx_miibus_statchg(ue->ue_dev); 2108 if ((sc->sc_flags & MUGE_FLAG_LINK) != 0) 2109 muge_start(ue); 2110 } 2111 } 2112 2113 /** 2114 * muge_ifmedia_sts - Report current media status 2115 * @ifp: inet interface pointer 2116 * @ifmr: interface media request 2117 * 2118 * Call the mii functions to get the media status. 2119 * 2120 * LOCKING: 2121 * Internally takes and releases the device lock. 2122 */ 2123 static void 2124 muge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 2125 { 2126 struct muge_softc *sc = ifp->if_softc; 2127 struct mii_data *mii = uether_getmii(&sc->sc_ue); 2128 2129 MUGE_LOCK(sc); 2130 mii_pollstat(mii); 2131 ifmr->ifm_active = mii->mii_media_active; 2132 ifmr->ifm_status = mii->mii_media_status; 2133 MUGE_UNLOCK(sc); 2134 } 2135 2136 /** 2137 * muge_probe - Probe the interface. 2138 * @dev: muge device handle 2139 * 2140 * Checks if the device is a match for this driver. 2141 * 2142 * RETURNS: 2143 * Returns 0 on success or an error code on failure. 2144 */ 2145 static int 2146 muge_probe(device_t dev) 2147 { 2148 struct usb_attach_arg *uaa = device_get_ivars(dev); 2149 2150 if (uaa->usb_mode != USB_MODE_HOST) 2151 return (ENXIO); 2152 if (uaa->info.bConfigIndex != MUGE_CONFIG_INDEX) 2153 return (ENXIO); 2154 if (uaa->info.bIfaceIndex != MUGE_IFACE_IDX) 2155 return (ENXIO); 2156 return (usbd_lookup_id_by_uaa(lan78xx_devs, sizeof(lan78xx_devs), uaa)); 2157 } 2158 2159 /** 2160 * muge_attach - Attach the interface. 2161 * @dev: muge device handle 2162 * 2163 * Allocate softc structures, do ifmedia setup and ethernet/BPF attach. 2164 * 2165 * RETURNS: 2166 * Returns 0 on success or a negative error code. 2167 */ 2168 static int 2169 muge_attach(device_t dev) 2170 { 2171 struct usb_attach_arg *uaa = device_get_ivars(dev); 2172 struct muge_softc *sc = device_get_softc(dev); 2173 struct usb_ether *ue = &sc->sc_ue; 2174 uint8_t iface_index; 2175 int err; 2176 2177 sc->sc_flags = USB_GET_DRIVER_INFO(uaa); 2178 2179 device_set_usb_desc(dev); 2180 2181 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 2182 2183 /* Setup the endpoints for the Microchip LAN78xx device. */ 2184 iface_index = MUGE_IFACE_IDX; 2185 err = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 2186 muge_config, MUGE_N_TRANSFER, sc, &sc->sc_mtx); 2187 if (err) { 2188 device_printf(dev, "error: allocating USB transfers failed\n"); 2189 goto err; 2190 } 2191 2192 ue->ue_sc = sc; 2193 ue->ue_dev = dev; 2194 ue->ue_udev = uaa->device; 2195 ue->ue_mtx = &sc->sc_mtx; 2196 ue->ue_methods = &muge_ue_methods; 2197 2198 err = uether_ifattach(ue); 2199 if (err) { 2200 device_printf(dev, "error: could not attach interface\n"); 2201 goto err_usbd; 2202 } 2203 2204 /* Wait for lan78xx_chip_init from post-attach callback to complete. */ 2205 uether_ifattach_wait(ue); 2206 if (!(sc->sc_flags & MUGE_FLAG_INIT_DONE)) 2207 goto err_attached; 2208 2209 return (0); 2210 2211 err_attached: 2212 uether_ifdetach(ue); 2213 err_usbd: 2214 usbd_transfer_unsetup(sc->sc_xfer, MUGE_N_TRANSFER); 2215 err: 2216 mtx_destroy(&sc->sc_mtx); 2217 return (ENXIO); 2218 } 2219 2220 /** 2221 * muge_detach - Detach the interface. 2222 * @dev: muge device handle 2223 * 2224 * RETURNS: 2225 * Returns 0. 2226 */ 2227 static int 2228 muge_detach(device_t dev) 2229 { 2230 2231 struct muge_softc *sc = device_get_softc(dev); 2232 struct usb_ether *ue = &sc->sc_ue; 2233 2234 usbd_transfer_unsetup(sc->sc_xfer, MUGE_N_TRANSFER); 2235 uether_ifdetach(ue); 2236 mtx_destroy(&sc->sc_mtx); 2237 2238 return (0); 2239 } 2240 2241 static device_method_t muge_methods[] = { 2242 /* Device interface */ 2243 DEVMETHOD(device_probe, muge_probe), 2244 DEVMETHOD(device_attach, muge_attach), 2245 DEVMETHOD(device_detach, muge_detach), 2246 2247 /* Bus interface */ 2248 DEVMETHOD(bus_print_child, bus_generic_print_child), 2249 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 2250 2251 /* MII interface */ 2252 DEVMETHOD(miibus_readreg, lan78xx_miibus_readreg), 2253 DEVMETHOD(miibus_writereg, lan78xx_miibus_writereg), 2254 DEVMETHOD(miibus_statchg, lan78xx_miibus_statchg), 2255 2256 DEVMETHOD_END 2257 }; 2258 2259 static driver_t muge_driver = { 2260 .name = "muge", 2261 .methods = muge_methods, 2262 .size = sizeof(struct muge_softc), 2263 }; 2264 2265 static devclass_t muge_devclass; 2266 2267 DRIVER_MODULE(muge, uhub, muge_driver, muge_devclass, NULL, NULL); 2268 DRIVER_MODULE(miibus, muge, miibus_driver, miibus_devclass, NULL, NULL); 2269 MODULE_DEPEND(muge, uether, 1, 1, 1); 2270 MODULE_DEPEND(muge, usb, 1, 1, 1); 2271 MODULE_DEPEND(muge, ether, 1, 1, 1); 2272 MODULE_DEPEND(muge, miibus, 1, 1, 1); 2273 MODULE_VERSION(muge, 1); 2274 USB_PNP_HOST_INFO(lan78xx_devs); 2275