1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Framework for configuring and reading PHY devices 3 * Based on code in sungem_phy.c and gianfar_phy.c 4 * 5 * Author: Andy Fleming 6 * 7 * Copyright (c) 2004 Freescale Semiconductor, Inc. 8 * Copyright (c) 2006, 2007 Maciej W. Rozycki 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/string.h> 13 #include <linux/errno.h> 14 #include <linux/unistd.h> 15 #include <linux/interrupt.h> 16 #include <linux/delay.h> 17 #include <linux/netdevice.h> 18 #include <linux/etherdevice.h> 19 #include <linux/skbuff.h> 20 #include <linux/mm.h> 21 #include <linux/module.h> 22 #include <linux/mii.h> 23 #include <linux/ethtool.h> 24 #include <linux/phy.h> 25 #include <linux/phy_led_triggers.h> 26 #include <linux/workqueue.h> 27 #include <linux/mdio.h> 28 #include <linux/io.h> 29 #include <linux/uaccess.h> 30 #include <linux/atomic.h> 31 32 #define PHY_STATE_STR(_state) \ 33 case PHY_##_state: \ 34 return __stringify(_state); \ 35 36 static const char *phy_state_to_str(enum phy_state st) 37 { 38 switch (st) { 39 PHY_STATE_STR(DOWN) 40 PHY_STATE_STR(READY) 41 PHY_STATE_STR(UP) 42 PHY_STATE_STR(RUNNING) 43 PHY_STATE_STR(NOLINK) 44 PHY_STATE_STR(FORCING) 45 PHY_STATE_STR(HALTED) 46 PHY_STATE_STR(RESUMING) 47 } 48 49 return NULL; 50 } 51 52 static void phy_link_up(struct phy_device *phydev) 53 { 54 phydev->phy_link_change(phydev, true, true); 55 phy_led_trigger_change_speed(phydev); 56 } 57 58 static void phy_link_down(struct phy_device *phydev, bool do_carrier) 59 { 60 phydev->phy_link_change(phydev, false, do_carrier); 61 phy_led_trigger_change_speed(phydev); 62 } 63 64 /** 65 * phy_print_status - Convenience function to print out the current phy status 66 * @phydev: the phy_device struct 67 */ 68 void phy_print_status(struct phy_device *phydev) 69 { 70 if (phydev->link) { 71 netdev_info(phydev->attached_dev, 72 "Link is Up - %s/%s - flow control %s\n", 73 phy_speed_to_str(phydev->speed), 74 phy_duplex_to_str(phydev->duplex), 75 phydev->pause ? "rx/tx" : "off"); 76 } else { 77 netdev_info(phydev->attached_dev, "Link is Down\n"); 78 } 79 } 80 EXPORT_SYMBOL(phy_print_status); 81 82 /** 83 * phy_clear_interrupt - Ack the phy device's interrupt 84 * @phydev: the phy_device struct 85 * 86 * If the @phydev driver has an ack_interrupt function, call it to 87 * ack and clear the phy device's interrupt. 88 * 89 * Returns 0 on success or < 0 on error. 90 */ 91 static int phy_clear_interrupt(struct phy_device *phydev) 92 { 93 if (phydev->drv->ack_interrupt) 94 return phydev->drv->ack_interrupt(phydev); 95 96 return 0; 97 } 98 99 /** 100 * phy_config_interrupt - configure the PHY device for the requested interrupts 101 * @phydev: the phy_device struct 102 * @interrupts: interrupt flags to configure for this @phydev 103 * 104 * Returns 0 on success or < 0 on error. 105 */ 106 static int phy_config_interrupt(struct phy_device *phydev, bool interrupts) 107 { 108 phydev->interrupts = interrupts ? 1 : 0; 109 if (phydev->drv->config_intr) 110 return phydev->drv->config_intr(phydev); 111 112 return 0; 113 } 114 115 /** 116 * phy_restart_aneg - restart auto-negotiation 117 * @phydev: target phy_device struct 118 * 119 * Restart the autonegotiation on @phydev. Returns >= 0 on success or 120 * negative errno on error. 121 */ 122 int phy_restart_aneg(struct phy_device *phydev) 123 { 124 int ret; 125 126 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0))) 127 ret = genphy_c45_restart_aneg(phydev); 128 else 129 ret = genphy_restart_aneg(phydev); 130 131 return ret; 132 } 133 EXPORT_SYMBOL_GPL(phy_restart_aneg); 134 135 /** 136 * phy_aneg_done - return auto-negotiation status 137 * @phydev: target phy_device struct 138 * 139 * Description: Return the auto-negotiation status from this @phydev 140 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation 141 * is still pending. 142 */ 143 int phy_aneg_done(struct phy_device *phydev) 144 { 145 if (phydev->drv && phydev->drv->aneg_done) 146 return phydev->drv->aneg_done(phydev); 147 148 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not 149 * implement Clause 22 registers 150 */ 151 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0))) 152 return -EINVAL; 153 154 return genphy_aneg_done(phydev); 155 } 156 EXPORT_SYMBOL(phy_aneg_done); 157 158 /** 159 * phy_find_valid - find a PHY setting that matches the requested parameters 160 * @speed: desired speed 161 * @duplex: desired duplex 162 * @supported: mask of supported link modes 163 * 164 * Locate a supported phy setting that is, in priority order: 165 * - an exact match for the specified speed and duplex mode 166 * - a match for the specified speed, or slower speed 167 * - the slowest supported speed 168 * Returns the matched phy_setting entry, or %NULL if no supported phy 169 * settings were found. 170 */ 171 static const struct phy_setting * 172 phy_find_valid(int speed, int duplex, unsigned long *supported) 173 { 174 return phy_lookup_setting(speed, duplex, supported, false); 175 } 176 177 /** 178 * phy_supported_speeds - return all speeds currently supported by a phy device 179 * @phy: The phy device to return supported speeds of. 180 * @speeds: buffer to store supported speeds in. 181 * @size: size of speeds buffer. 182 * 183 * Description: Returns the number of supported speeds, and fills the speeds 184 * buffer with the supported speeds. If speeds buffer is too small to contain 185 * all currently supported speeds, will return as many speeds as can fit. 186 */ 187 unsigned int phy_supported_speeds(struct phy_device *phy, 188 unsigned int *speeds, 189 unsigned int size) 190 { 191 return phy_speeds(speeds, size, phy->supported); 192 } 193 194 /** 195 * phy_check_valid - check if there is a valid PHY setting which matches 196 * speed, duplex, and feature mask 197 * @speed: speed to match 198 * @duplex: duplex to match 199 * @features: A mask of the valid settings 200 * 201 * Description: Returns true if there is a valid setting, false otherwise. 202 */ 203 static inline bool phy_check_valid(int speed, int duplex, 204 unsigned long *features) 205 { 206 return !!phy_lookup_setting(speed, duplex, features, true); 207 } 208 209 /** 210 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex 211 * @phydev: the target phy_device struct 212 * 213 * Description: Make sure the PHY is set to supported speeds and 214 * duplexes. Drop down by one in this order: 1000/FULL, 215 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. 216 */ 217 static void phy_sanitize_settings(struct phy_device *phydev) 218 { 219 const struct phy_setting *setting; 220 221 /* Sanitize settings based on PHY capabilities */ 222 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported)) 223 phydev->autoneg = AUTONEG_DISABLE; 224 225 setting = phy_find_valid(phydev->speed, phydev->duplex, 226 phydev->supported); 227 if (setting) { 228 phydev->speed = setting->speed; 229 phydev->duplex = setting->duplex; 230 } else { 231 /* We failed to find anything (no supported speeds?) */ 232 phydev->speed = SPEED_UNKNOWN; 233 phydev->duplex = DUPLEX_UNKNOWN; 234 } 235 } 236 237 /** 238 * phy_ethtool_sset - generic ethtool sset function, handles all the details 239 * @phydev: target phy_device struct 240 * @cmd: ethtool_cmd 241 * 242 * A few notes about parameter checking: 243 * 244 * - We don't set port or transceiver, so we don't care what they 245 * were set to. 246 * - phy_start_aneg() will make sure forced settings are sane, and 247 * choose the next best ones from the ones selected, so we don't 248 * care if ethtool tries to give us bad values. 249 */ 250 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd) 251 { 252 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 253 u32 speed = ethtool_cmd_speed(cmd); 254 255 if (cmd->phy_address != phydev->mdio.addr) 256 return -EINVAL; 257 258 /* We make sure that we don't pass unsupported values in to the PHY */ 259 ethtool_convert_legacy_u32_to_link_mode(advertising, cmd->advertising); 260 linkmode_and(advertising, advertising, phydev->supported); 261 262 /* Verify the settings we care about. */ 263 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE) 264 return -EINVAL; 265 266 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0) 267 return -EINVAL; 268 269 if (cmd->autoneg == AUTONEG_DISABLE && 270 ((speed != SPEED_1000 && 271 speed != SPEED_100 && 272 speed != SPEED_10) || 273 (cmd->duplex != DUPLEX_HALF && 274 cmd->duplex != DUPLEX_FULL))) 275 return -EINVAL; 276 277 phydev->autoneg = cmd->autoneg; 278 279 phydev->speed = speed; 280 281 linkmode_copy(phydev->advertising, advertising); 282 283 if (AUTONEG_ENABLE == cmd->autoneg) 284 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 285 phydev->advertising); 286 else 287 linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 288 phydev->advertising); 289 290 phydev->duplex = cmd->duplex; 291 292 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl; 293 294 /* Restart the PHY */ 295 phy_start_aneg(phydev); 296 297 return 0; 298 } 299 EXPORT_SYMBOL(phy_ethtool_sset); 300 301 int phy_ethtool_ksettings_set(struct phy_device *phydev, 302 const struct ethtool_link_ksettings *cmd) 303 { 304 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 305 u8 autoneg = cmd->base.autoneg; 306 u8 duplex = cmd->base.duplex; 307 u32 speed = cmd->base.speed; 308 309 if (cmd->base.phy_address != phydev->mdio.addr) 310 return -EINVAL; 311 312 linkmode_copy(advertising, cmd->link_modes.advertising); 313 314 /* We make sure that we don't pass unsupported values in to the PHY */ 315 linkmode_and(advertising, advertising, phydev->supported); 316 317 /* Verify the settings we care about. */ 318 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE) 319 return -EINVAL; 320 321 if (autoneg == AUTONEG_ENABLE && linkmode_empty(advertising)) 322 return -EINVAL; 323 324 if (autoneg == AUTONEG_DISABLE && 325 ((speed != SPEED_1000 && 326 speed != SPEED_100 && 327 speed != SPEED_10) || 328 (duplex != DUPLEX_HALF && 329 duplex != DUPLEX_FULL))) 330 return -EINVAL; 331 332 phydev->autoneg = autoneg; 333 334 phydev->speed = speed; 335 336 linkmode_copy(phydev->advertising, advertising); 337 338 if (autoneg == AUTONEG_ENABLE) 339 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 340 phydev->advertising); 341 else 342 linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 343 phydev->advertising); 344 345 phydev->duplex = duplex; 346 347 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl; 348 349 /* Restart the PHY */ 350 phy_start_aneg(phydev); 351 352 return 0; 353 } 354 EXPORT_SYMBOL(phy_ethtool_ksettings_set); 355 356 void phy_ethtool_ksettings_get(struct phy_device *phydev, 357 struct ethtool_link_ksettings *cmd) 358 { 359 linkmode_copy(cmd->link_modes.supported, phydev->supported); 360 linkmode_copy(cmd->link_modes.advertising, phydev->advertising); 361 linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising); 362 363 cmd->base.speed = phydev->speed; 364 cmd->base.duplex = phydev->duplex; 365 if (phydev->interface == PHY_INTERFACE_MODE_MOCA) 366 cmd->base.port = PORT_BNC; 367 else 368 cmd->base.port = PORT_MII; 369 cmd->base.transceiver = phy_is_internal(phydev) ? 370 XCVR_INTERNAL : XCVR_EXTERNAL; 371 cmd->base.phy_address = phydev->mdio.addr; 372 cmd->base.autoneg = phydev->autoneg; 373 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl; 374 cmd->base.eth_tp_mdix = phydev->mdix; 375 } 376 EXPORT_SYMBOL(phy_ethtool_ksettings_get); 377 378 /** 379 * phy_mii_ioctl - generic PHY MII ioctl interface 380 * @phydev: the phy_device struct 381 * @ifr: &struct ifreq for socket ioctl's 382 * @cmd: ioctl cmd to execute 383 * 384 * Note that this function is currently incompatible with the 385 * PHYCONTROL layer. It changes registers without regard to 386 * current state. Use at own risk. 387 */ 388 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd) 389 { 390 struct mii_ioctl_data *mii_data = if_mii(ifr); 391 u16 val = mii_data->val_in; 392 bool change_autoneg = false; 393 394 switch (cmd) { 395 case SIOCGMIIPHY: 396 mii_data->phy_id = phydev->mdio.addr; 397 /* fall through */ 398 399 case SIOCGMIIREG: 400 mii_data->val_out = mdiobus_read(phydev->mdio.bus, 401 mii_data->phy_id, 402 mii_data->reg_num); 403 return 0; 404 405 case SIOCSMIIREG: 406 if (mii_data->phy_id == phydev->mdio.addr) { 407 switch (mii_data->reg_num) { 408 case MII_BMCR: 409 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) { 410 if (phydev->autoneg == AUTONEG_ENABLE) 411 change_autoneg = true; 412 phydev->autoneg = AUTONEG_DISABLE; 413 if (val & BMCR_FULLDPLX) 414 phydev->duplex = DUPLEX_FULL; 415 else 416 phydev->duplex = DUPLEX_HALF; 417 if (val & BMCR_SPEED1000) 418 phydev->speed = SPEED_1000; 419 else if (val & BMCR_SPEED100) 420 phydev->speed = SPEED_100; 421 else phydev->speed = SPEED_10; 422 } 423 else { 424 if (phydev->autoneg == AUTONEG_DISABLE) 425 change_autoneg = true; 426 phydev->autoneg = AUTONEG_ENABLE; 427 } 428 break; 429 case MII_ADVERTISE: 430 mii_adv_mod_linkmode_adv_t(phydev->advertising, 431 val); 432 change_autoneg = true; 433 break; 434 default: 435 /* do nothing */ 436 break; 437 } 438 } 439 440 mdiobus_write(phydev->mdio.bus, mii_data->phy_id, 441 mii_data->reg_num, val); 442 443 if (mii_data->phy_id == phydev->mdio.addr && 444 mii_data->reg_num == MII_BMCR && 445 val & BMCR_RESET) 446 return phy_init_hw(phydev); 447 448 if (change_autoneg) 449 return phy_start_aneg(phydev); 450 451 return 0; 452 453 case SIOCSHWTSTAMP: 454 if (phydev->drv && phydev->drv->hwtstamp) 455 return phydev->drv->hwtstamp(phydev, ifr); 456 /* fall through */ 457 458 default: 459 return -EOPNOTSUPP; 460 } 461 } 462 EXPORT_SYMBOL(phy_mii_ioctl); 463 464 static void phy_queue_state_machine(struct phy_device *phydev, 465 unsigned int secs) 466 { 467 mod_delayed_work(system_power_efficient_wq, &phydev->state_queue, 468 secs * HZ); 469 } 470 471 static void phy_trigger_machine(struct phy_device *phydev) 472 { 473 phy_queue_state_machine(phydev, 0); 474 } 475 476 static int phy_config_aneg(struct phy_device *phydev) 477 { 478 if (phydev->drv->config_aneg) 479 return phydev->drv->config_aneg(phydev); 480 481 /* Clause 45 PHYs that don't implement Clause 22 registers are not 482 * allowed to call genphy_config_aneg() 483 */ 484 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0))) 485 return -EOPNOTSUPP; 486 487 return genphy_config_aneg(phydev); 488 } 489 490 /** 491 * phy_check_link_status - check link status and set state accordingly 492 * @phydev: the phy_device struct 493 * 494 * Description: Check for link and whether autoneg was triggered / is running 495 * and set state accordingly 496 */ 497 static int phy_check_link_status(struct phy_device *phydev) 498 { 499 int err; 500 501 WARN_ON(!mutex_is_locked(&phydev->lock)); 502 503 err = phy_read_status(phydev); 504 if (err) 505 return err; 506 507 if (phydev->link && phydev->state != PHY_RUNNING) { 508 phydev->state = PHY_RUNNING; 509 phy_link_up(phydev); 510 } else if (!phydev->link && phydev->state != PHY_NOLINK) { 511 phydev->state = PHY_NOLINK; 512 phy_link_down(phydev, true); 513 } 514 515 return 0; 516 } 517 518 /** 519 * phy_start_aneg - start auto-negotiation for this PHY device 520 * @phydev: the phy_device struct 521 * 522 * Description: Sanitizes the settings (if we're not autonegotiating 523 * them), and then calls the driver's config_aneg function. 524 * If the PHYCONTROL Layer is operating, we change the state to 525 * reflect the beginning of Auto-negotiation or forcing. 526 */ 527 int phy_start_aneg(struct phy_device *phydev) 528 { 529 int err; 530 531 if (!phydev->drv) 532 return -EIO; 533 534 mutex_lock(&phydev->lock); 535 536 if (AUTONEG_DISABLE == phydev->autoneg) 537 phy_sanitize_settings(phydev); 538 539 /* Invalidate LP advertising flags */ 540 linkmode_zero(phydev->lp_advertising); 541 542 err = phy_config_aneg(phydev); 543 if (err < 0) 544 goto out_unlock; 545 546 if (phy_is_started(phydev)) { 547 if (phydev->autoneg == AUTONEG_ENABLE) { 548 err = phy_check_link_status(phydev); 549 } else { 550 phydev->state = PHY_FORCING; 551 phydev->link_timeout = PHY_FORCE_TIMEOUT; 552 } 553 } 554 555 out_unlock: 556 mutex_unlock(&phydev->lock); 557 558 return err; 559 } 560 EXPORT_SYMBOL(phy_start_aneg); 561 562 static int phy_poll_aneg_done(struct phy_device *phydev) 563 { 564 unsigned int retries = 100; 565 int ret; 566 567 do { 568 msleep(100); 569 ret = phy_aneg_done(phydev); 570 } while (!ret && --retries); 571 572 if (!ret) 573 return -ETIMEDOUT; 574 575 return ret < 0 ? ret : 0; 576 } 577 578 /** 579 * phy_speed_down - set speed to lowest speed supported by both link partners 580 * @phydev: the phy_device struct 581 * @sync: perform action synchronously 582 * 583 * Description: Typically used to save energy when waiting for a WoL packet 584 * 585 * WARNING: Setting sync to false may cause the system being unable to suspend 586 * in case the PHY generates an interrupt when finishing the autonegotiation. 587 * This interrupt may wake up the system immediately after suspend. 588 * Therefore use sync = false only if you're sure it's safe with the respective 589 * network chip. 590 */ 591 int phy_speed_down(struct phy_device *phydev, bool sync) 592 { 593 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old); 594 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv); 595 int ret; 596 597 if (phydev->autoneg != AUTONEG_ENABLE) 598 return 0; 599 600 linkmode_copy(adv_old, phydev->advertising); 601 linkmode_copy(adv, phydev->lp_advertising); 602 linkmode_and(adv, adv, phydev->supported); 603 604 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, adv) || 605 linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, adv)) { 606 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, 607 phydev->advertising); 608 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 609 phydev->advertising); 610 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 611 phydev->advertising); 612 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 613 phydev->advertising); 614 } else if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, 615 adv) || 616 linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 617 adv)) { 618 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 619 phydev->advertising); 620 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 621 phydev->advertising); 622 } 623 624 if (linkmode_equal(phydev->advertising, adv_old)) 625 return 0; 626 627 ret = phy_config_aneg(phydev); 628 if (ret) 629 return ret; 630 631 return sync ? phy_poll_aneg_done(phydev) : 0; 632 } 633 EXPORT_SYMBOL_GPL(phy_speed_down); 634 635 /** 636 * phy_speed_up - (re)set advertised speeds to all supported speeds 637 * @phydev: the phy_device struct 638 * 639 * Description: Used to revert the effect of phy_speed_down 640 */ 641 int phy_speed_up(struct phy_device *phydev) 642 { 643 __ETHTOOL_DECLARE_LINK_MODE_MASK(all_speeds) = { 0, }; 644 __ETHTOOL_DECLARE_LINK_MODE_MASK(not_speeds); 645 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported); 646 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old); 647 __ETHTOOL_DECLARE_LINK_MODE_MASK(speeds); 648 649 linkmode_copy(adv_old, phydev->advertising); 650 651 if (phydev->autoneg != AUTONEG_ENABLE) 652 return 0; 653 654 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, all_speeds); 655 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, all_speeds); 656 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, all_speeds); 657 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, all_speeds); 658 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, all_speeds); 659 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, all_speeds); 660 661 linkmode_andnot(not_speeds, adv_old, all_speeds); 662 linkmode_copy(supported, phydev->supported); 663 linkmode_and(speeds, supported, all_speeds); 664 linkmode_or(phydev->advertising, not_speeds, speeds); 665 666 if (linkmode_equal(phydev->advertising, adv_old)) 667 return 0; 668 669 return phy_config_aneg(phydev); 670 } 671 EXPORT_SYMBOL_GPL(phy_speed_up); 672 673 /** 674 * phy_start_machine - start PHY state machine tracking 675 * @phydev: the phy_device struct 676 * 677 * Description: The PHY infrastructure can run a state machine 678 * which tracks whether the PHY is starting up, negotiating, 679 * etc. This function starts the delayed workqueue which tracks 680 * the state of the PHY. If you want to maintain your own state machine, 681 * do not call this function. 682 */ 683 void phy_start_machine(struct phy_device *phydev) 684 { 685 phy_trigger_machine(phydev); 686 } 687 EXPORT_SYMBOL_GPL(phy_start_machine); 688 689 /** 690 * phy_stop_machine - stop the PHY state machine tracking 691 * @phydev: target phy_device struct 692 * 693 * Description: Stops the state machine delayed workqueue, sets the 694 * state to UP (unless it wasn't up yet). This function must be 695 * called BEFORE phy_detach. 696 */ 697 void phy_stop_machine(struct phy_device *phydev) 698 { 699 cancel_delayed_work_sync(&phydev->state_queue); 700 701 mutex_lock(&phydev->lock); 702 if (phy_is_started(phydev)) 703 phydev->state = PHY_UP; 704 mutex_unlock(&phydev->lock); 705 } 706 707 /** 708 * phy_error - enter HALTED state for this PHY device 709 * @phydev: target phy_device struct 710 * 711 * Moves the PHY to the HALTED state in response to a read 712 * or write error, and tells the controller the link is down. 713 * Must not be called from interrupt context, or while the 714 * phydev->lock is held. 715 */ 716 static void phy_error(struct phy_device *phydev) 717 { 718 WARN_ON(1); 719 720 mutex_lock(&phydev->lock); 721 phydev->state = PHY_HALTED; 722 mutex_unlock(&phydev->lock); 723 724 phy_trigger_machine(phydev); 725 } 726 727 /** 728 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side 729 * @phydev: target phy_device struct 730 */ 731 static int phy_disable_interrupts(struct phy_device *phydev) 732 { 733 int err; 734 735 /* Disable PHY interrupts */ 736 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); 737 if (err) 738 return err; 739 740 /* Clear the interrupt */ 741 return phy_clear_interrupt(phydev); 742 } 743 744 /** 745 * phy_interrupt - PHY interrupt handler 746 * @irq: interrupt line 747 * @phy_dat: phy_device pointer 748 * 749 * Description: Handle PHY interrupt 750 */ 751 static irqreturn_t phy_interrupt(int irq, void *phy_dat) 752 { 753 struct phy_device *phydev = phy_dat; 754 755 if (phydev->drv->did_interrupt && !phydev->drv->did_interrupt(phydev)) 756 return IRQ_NONE; 757 758 /* reschedule state queue work to run as soon as possible */ 759 phy_trigger_machine(phydev); 760 761 if (phy_clear_interrupt(phydev)) 762 goto phy_err; 763 return IRQ_HANDLED; 764 765 phy_err: 766 phy_error(phydev); 767 return IRQ_NONE; 768 } 769 770 /** 771 * phy_enable_interrupts - Enable the interrupts from the PHY side 772 * @phydev: target phy_device struct 773 */ 774 static int phy_enable_interrupts(struct phy_device *phydev) 775 { 776 int err = phy_clear_interrupt(phydev); 777 778 if (err < 0) 779 return err; 780 781 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); 782 } 783 784 /** 785 * phy_request_interrupt - request interrupt for a PHY device 786 * @phydev: target phy_device struct 787 * 788 * Description: Request the interrupt for the given PHY. 789 * If this fails, then we set irq to PHY_POLL. 790 * This should only be called with a valid IRQ number. 791 */ 792 void phy_request_interrupt(struct phy_device *phydev) 793 { 794 int err; 795 796 err = request_threaded_irq(phydev->irq, NULL, phy_interrupt, 797 IRQF_ONESHOT | IRQF_SHARED, 798 phydev_name(phydev), phydev); 799 if (err) { 800 phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n", 801 err, phydev->irq); 802 phydev->irq = PHY_POLL; 803 } 804 } 805 EXPORT_SYMBOL(phy_request_interrupt); 806 807 /** 808 * phy_stop - Bring down the PHY link, and stop checking the status 809 * @phydev: target phy_device struct 810 */ 811 void phy_stop(struct phy_device *phydev) 812 { 813 if (!phy_is_started(phydev)) { 814 WARN(1, "called from state %s\n", 815 phy_state_to_str(phydev->state)); 816 return; 817 } 818 819 mutex_lock(&phydev->lock); 820 821 if (phy_interrupt_is_valid(phydev)) 822 phy_disable_interrupts(phydev); 823 824 phydev->state = PHY_HALTED; 825 826 mutex_unlock(&phydev->lock); 827 828 phy_state_machine(&phydev->state_queue.work); 829 phy_stop_machine(phydev); 830 831 /* Cannot call flush_scheduled_work() here as desired because 832 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler 833 * will not reenable interrupts. 834 */ 835 } 836 EXPORT_SYMBOL(phy_stop); 837 838 /** 839 * phy_start - start or restart a PHY device 840 * @phydev: target phy_device struct 841 * 842 * Description: Indicates the attached device's readiness to 843 * handle PHY-related work. Used during startup to start the 844 * PHY, and after a call to phy_stop() to resume operation. 845 * Also used to indicate the MDIO bus has cleared an error 846 * condition. 847 */ 848 void phy_start(struct phy_device *phydev) 849 { 850 int err; 851 852 mutex_lock(&phydev->lock); 853 854 if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) { 855 WARN(1, "called from state %s\n", 856 phy_state_to_str(phydev->state)); 857 goto out; 858 } 859 860 /* if phy was suspended, bring the physical link up again */ 861 __phy_resume(phydev); 862 863 /* make sure interrupts are enabled for the PHY */ 864 if (phy_interrupt_is_valid(phydev)) { 865 err = phy_enable_interrupts(phydev); 866 if (err < 0) 867 goto out; 868 } 869 870 if (phydev->state == PHY_READY) 871 phydev->state = PHY_UP; 872 else 873 phydev->state = PHY_RESUMING; 874 875 phy_start_machine(phydev); 876 out: 877 mutex_unlock(&phydev->lock); 878 } 879 EXPORT_SYMBOL(phy_start); 880 881 /** 882 * phy_state_machine - Handle the state machine 883 * @work: work_struct that describes the work to be done 884 */ 885 void phy_state_machine(struct work_struct *work) 886 { 887 struct delayed_work *dwork = to_delayed_work(work); 888 struct phy_device *phydev = 889 container_of(dwork, struct phy_device, state_queue); 890 bool needs_aneg = false, do_suspend = false; 891 enum phy_state old_state; 892 int err = 0; 893 894 mutex_lock(&phydev->lock); 895 896 old_state = phydev->state; 897 898 if (phydev->drv && phydev->drv->link_change_notify) 899 phydev->drv->link_change_notify(phydev); 900 901 switch (phydev->state) { 902 case PHY_DOWN: 903 case PHY_READY: 904 break; 905 case PHY_UP: 906 needs_aneg = true; 907 908 break; 909 case PHY_NOLINK: 910 case PHY_RUNNING: 911 case PHY_RESUMING: 912 err = phy_check_link_status(phydev); 913 break; 914 case PHY_FORCING: 915 err = genphy_update_link(phydev); 916 if (err) 917 break; 918 919 if (phydev->link) { 920 phydev->state = PHY_RUNNING; 921 phy_link_up(phydev); 922 } else { 923 if (0 == phydev->link_timeout--) 924 needs_aneg = true; 925 phy_link_down(phydev, false); 926 } 927 break; 928 case PHY_HALTED: 929 if (phydev->link) { 930 phydev->link = 0; 931 phy_link_down(phydev, true); 932 do_suspend = true; 933 } 934 break; 935 } 936 937 mutex_unlock(&phydev->lock); 938 939 if (needs_aneg) 940 err = phy_start_aneg(phydev); 941 else if (do_suspend) 942 phy_suspend(phydev); 943 944 if (err < 0) 945 phy_error(phydev); 946 947 if (old_state != phydev->state) 948 phydev_dbg(phydev, "PHY state change %s -> %s\n", 949 phy_state_to_str(old_state), 950 phy_state_to_str(phydev->state)); 951 952 /* Only re-schedule a PHY state machine change if we are polling the 953 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving 954 * between states from phy_mac_interrupt(). 955 * 956 * In state PHY_HALTED the PHY gets suspended, so rescheduling the 957 * state machine would be pointless and possibly error prone when 958 * called from phy_disconnect() synchronously. 959 */ 960 mutex_lock(&phydev->lock); 961 if (phy_polling_mode(phydev) && phy_is_started(phydev)) 962 phy_queue_state_machine(phydev, PHY_STATE_TIME); 963 mutex_unlock(&phydev->lock); 964 } 965 966 /** 967 * phy_mac_interrupt - MAC says the link has changed 968 * @phydev: phy_device struct with changed link 969 * 970 * The MAC layer is able to indicate there has been a change in the PHY link 971 * status. Trigger the state machine and work a work queue. 972 */ 973 void phy_mac_interrupt(struct phy_device *phydev) 974 { 975 /* Trigger a state machine change */ 976 phy_trigger_machine(phydev); 977 } 978 EXPORT_SYMBOL(phy_mac_interrupt); 979 980 static void mmd_eee_adv_to_linkmode(unsigned long *advertising, u16 eee_adv) 981 { 982 linkmode_zero(advertising); 983 984 if (eee_adv & MDIO_EEE_100TX) 985 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, 986 advertising); 987 if (eee_adv & MDIO_EEE_1000T) 988 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 989 advertising); 990 if (eee_adv & MDIO_EEE_10GT) 991 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 992 advertising); 993 if (eee_adv & MDIO_EEE_1000KX) 994 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, 995 advertising); 996 if (eee_adv & MDIO_EEE_10GKX4) 997 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, 998 advertising); 999 if (eee_adv & MDIO_EEE_10GKR) 1000 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, 1001 advertising); 1002 } 1003 1004 /** 1005 * phy_init_eee - init and check the EEE feature 1006 * @phydev: target phy_device struct 1007 * @clk_stop_enable: PHY may stop the clock during LPI 1008 * 1009 * Description: it checks if the Energy-Efficient Ethernet (EEE) 1010 * is supported by looking at the MMD registers 3.20 and 7.60/61 1011 * and it programs the MMD register 3.0 setting the "Clock stop enable" 1012 * bit if required. 1013 */ 1014 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable) 1015 { 1016 if (!phydev->drv) 1017 return -EIO; 1018 1019 /* According to 802.3az,the EEE is supported only in full duplex-mode. 1020 */ 1021 if (phydev->duplex == DUPLEX_FULL) { 1022 __ETHTOOL_DECLARE_LINK_MODE_MASK(common); 1023 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp); 1024 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv); 1025 int eee_lp, eee_cap, eee_adv; 1026 int status; 1027 u32 cap; 1028 1029 /* Read phy status to properly get the right settings */ 1030 status = phy_read_status(phydev); 1031 if (status) 1032 return status; 1033 1034 /* First check if the EEE ability is supported */ 1035 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); 1036 if (eee_cap <= 0) 1037 goto eee_exit_err; 1038 1039 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap); 1040 if (!cap) 1041 goto eee_exit_err; 1042 1043 /* Check which link settings negotiated and verify it in 1044 * the EEE advertising registers. 1045 */ 1046 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); 1047 if (eee_lp <= 0) 1048 goto eee_exit_err; 1049 1050 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 1051 if (eee_adv <= 0) 1052 goto eee_exit_err; 1053 1054 mmd_eee_adv_to_linkmode(adv, eee_adv); 1055 mmd_eee_adv_to_linkmode(lp, eee_lp); 1056 linkmode_and(common, adv, lp); 1057 1058 if (!phy_check_valid(phydev->speed, phydev->duplex, common)) 1059 goto eee_exit_err; 1060 1061 if (clk_stop_enable) 1062 /* Configure the PHY to stop receiving xMII 1063 * clock while it is signaling LPI. 1064 */ 1065 phy_set_bits_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, 1066 MDIO_PCS_CTRL1_CLKSTOP_EN); 1067 1068 return 0; /* EEE supported */ 1069 } 1070 eee_exit_err: 1071 return -EPROTONOSUPPORT; 1072 } 1073 EXPORT_SYMBOL(phy_init_eee); 1074 1075 /** 1076 * phy_get_eee_err - report the EEE wake error count 1077 * @phydev: target phy_device struct 1078 * 1079 * Description: it is to report the number of time where the PHY 1080 * failed to complete its normal wake sequence. 1081 */ 1082 int phy_get_eee_err(struct phy_device *phydev) 1083 { 1084 if (!phydev->drv) 1085 return -EIO; 1086 1087 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR); 1088 } 1089 EXPORT_SYMBOL(phy_get_eee_err); 1090 1091 /** 1092 * phy_ethtool_get_eee - get EEE supported and status 1093 * @phydev: target phy_device struct 1094 * @data: ethtool_eee data 1095 * 1096 * Description: it reportes the Supported/Advertisement/LP Advertisement 1097 * capabilities. 1098 */ 1099 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data) 1100 { 1101 int val; 1102 1103 if (!phydev->drv) 1104 return -EIO; 1105 1106 /* Get Supported EEE */ 1107 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); 1108 if (val < 0) 1109 return val; 1110 data->supported = mmd_eee_cap_to_ethtool_sup_t(val); 1111 1112 /* Get advertisement EEE */ 1113 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 1114 if (val < 0) 1115 return val; 1116 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val); 1117 data->eee_enabled = !!data->advertised; 1118 1119 /* Get LP advertisement EEE */ 1120 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE); 1121 if (val < 0) 1122 return val; 1123 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val); 1124 1125 data->eee_active = !!(data->advertised & data->lp_advertised); 1126 1127 return 0; 1128 } 1129 EXPORT_SYMBOL(phy_ethtool_get_eee); 1130 1131 /** 1132 * phy_ethtool_set_eee - set EEE supported and status 1133 * @phydev: target phy_device struct 1134 * @data: ethtool_eee data 1135 * 1136 * Description: it is to program the Advertisement EEE register. 1137 */ 1138 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data) 1139 { 1140 int cap, old_adv, adv = 0, ret; 1141 1142 if (!phydev->drv) 1143 return -EIO; 1144 1145 /* Get Supported EEE */ 1146 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE); 1147 if (cap < 0) 1148 return cap; 1149 1150 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV); 1151 if (old_adv < 0) 1152 return old_adv; 1153 1154 if (data->eee_enabled) { 1155 adv = !data->advertised ? cap : 1156 ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap; 1157 /* Mask prohibited EEE modes */ 1158 adv &= ~phydev->eee_broken_modes; 1159 } 1160 1161 if (old_adv != adv) { 1162 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv); 1163 if (ret < 0) 1164 return ret; 1165 1166 /* Restart autonegotiation so the new modes get sent to the 1167 * link partner. 1168 */ 1169 ret = phy_restart_aneg(phydev); 1170 if (ret < 0) 1171 return ret; 1172 } 1173 1174 return 0; 1175 } 1176 EXPORT_SYMBOL(phy_ethtool_set_eee); 1177 1178 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) 1179 { 1180 if (phydev->drv && phydev->drv->set_wol) 1181 return phydev->drv->set_wol(phydev, wol); 1182 1183 return -EOPNOTSUPP; 1184 } 1185 EXPORT_SYMBOL(phy_ethtool_set_wol); 1186 1187 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) 1188 { 1189 if (phydev->drv && phydev->drv->get_wol) 1190 phydev->drv->get_wol(phydev, wol); 1191 } 1192 EXPORT_SYMBOL(phy_ethtool_get_wol); 1193 1194 int phy_ethtool_get_link_ksettings(struct net_device *ndev, 1195 struct ethtool_link_ksettings *cmd) 1196 { 1197 struct phy_device *phydev = ndev->phydev; 1198 1199 if (!phydev) 1200 return -ENODEV; 1201 1202 phy_ethtool_ksettings_get(phydev, cmd); 1203 1204 return 0; 1205 } 1206 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings); 1207 1208 int phy_ethtool_set_link_ksettings(struct net_device *ndev, 1209 const struct ethtool_link_ksettings *cmd) 1210 { 1211 struct phy_device *phydev = ndev->phydev; 1212 1213 if (!phydev) 1214 return -ENODEV; 1215 1216 return phy_ethtool_ksettings_set(phydev, cmd); 1217 } 1218 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings); 1219 1220 int phy_ethtool_nway_reset(struct net_device *ndev) 1221 { 1222 struct phy_device *phydev = ndev->phydev; 1223 1224 if (!phydev) 1225 return -ENODEV; 1226 1227 if (!phydev->drv) 1228 return -EIO; 1229 1230 return phy_restart_aneg(phydev); 1231 } 1232 EXPORT_SYMBOL(phy_ethtool_nway_reset); 1233