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