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/netlink.h> 19 #include <linux/etherdevice.h> 20 #include <linux/skbuff.h> 21 #include <linux/mm.h> 22 #include <linux/module.h> 23 #include <linux/mii.h> 24 #include <linux/ethtool.h> 25 #include <linux/ethtool_netlink.h> 26 #include <linux/phy.h> 27 #include <linux/phy_led_triggers.h> 28 #include <linux/sfp.h> 29 #include <linux/workqueue.h> 30 #include <linux/mdio.h> 31 #include <linux/io.h> 32 #include <linux/uaccess.h> 33 #include <linux/atomic.h> 34 #include <linux/suspend.h> 35 #include <net/netlink.h> 36 #include <net/genetlink.h> 37 #include <net/sock.h> 38 39 #include "phylib-internal.h" 40 41 #define PHY_STATE_TIME HZ 42 43 #define PHY_STATE_STR(_state) \ 44 case PHY_##_state: \ 45 return __stringify(_state); \ 46 47 static const char *phy_state_to_str(enum phy_state st) 48 { 49 switch (st) { 50 PHY_STATE_STR(DOWN) 51 PHY_STATE_STR(READY) 52 PHY_STATE_STR(UP) 53 PHY_STATE_STR(RUNNING) 54 PHY_STATE_STR(NOLINK) 55 PHY_STATE_STR(CABLETEST) 56 PHY_STATE_STR(HALTED) 57 PHY_STATE_STR(ERROR) 58 } 59 60 return NULL; 61 } 62 63 static void phy_process_state_change(struct phy_device *phydev, 64 enum phy_state old_state) 65 { 66 if (old_state != phydev->state) { 67 phydev_dbg(phydev, "PHY state change %s -> %s\n", 68 phy_state_to_str(old_state), 69 phy_state_to_str(phydev->state)); 70 if (phydev->drv && phydev->drv->link_change_notify) 71 phydev->drv->link_change_notify(phydev); 72 } 73 } 74 75 static void phy_link_up(struct phy_device *phydev) 76 { 77 phydev->phy_link_change(phydev, true); 78 phy_led_trigger_change_speed(phydev); 79 } 80 81 static void phy_link_down(struct phy_device *phydev) 82 { 83 phydev->phy_link_change(phydev, false); 84 phy_led_trigger_change_speed(phydev); 85 WRITE_ONCE(phydev->link_down_events, phydev->link_down_events + 1); 86 } 87 88 static const char *phy_pause_str(struct phy_device *phydev) 89 { 90 bool local_pause, local_asym_pause; 91 92 if (phydev->autoneg == AUTONEG_DISABLE) 93 goto no_pause; 94 95 local_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 96 phydev->advertising); 97 local_asym_pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 98 phydev->advertising); 99 100 if (local_pause && phydev->pause) 101 return "rx/tx"; 102 103 if (local_asym_pause && phydev->asym_pause) { 104 if (local_pause) 105 return "rx"; 106 if (phydev->pause) 107 return "tx"; 108 } 109 110 no_pause: 111 return "off"; 112 } 113 114 /** 115 * phy_print_status - Convenience function to print out the current phy status 116 * @phydev: the phy_device struct 117 */ 118 void phy_print_status(struct phy_device *phydev) 119 { 120 if (phydev->link) { 121 netdev_info(phydev->attached_dev, 122 "Link is Up - %s/%s %s- flow control %s\n", 123 phy_speed_to_str(phydev->speed), 124 phy_duplex_to_str(phydev->duplex), 125 phydev->downshifted_rate ? "(downshifted) " : "", 126 phy_pause_str(phydev)); 127 } else { 128 netdev_info(phydev->attached_dev, "Link is Down\n"); 129 } 130 } 131 EXPORT_SYMBOL(phy_print_status); 132 133 /** 134 * phy_get_rate_matching - determine if rate matching is supported 135 * @phydev: The phy device to return rate matching for 136 * @iface: The interface mode to use 137 * 138 * This determines the type of rate matching (if any) that @phy supports 139 * using @iface. @iface may be %PHY_INTERFACE_MODE_NA to determine if any 140 * interface supports rate matching. 141 * 142 * Return: The type of rate matching @phy supports for @iface, or 143 * %RATE_MATCH_NONE. 144 */ 145 int phy_get_rate_matching(struct phy_device *phydev, 146 phy_interface_t iface) 147 { 148 int ret = RATE_MATCH_NONE; 149 150 if (phydev->drv->get_rate_matching) { 151 mutex_lock(&phydev->lock); 152 ret = phydev->drv->get_rate_matching(phydev, iface); 153 mutex_unlock(&phydev->lock); 154 } 155 156 return ret; 157 } 158 EXPORT_SYMBOL_GPL(phy_get_rate_matching); 159 160 /** 161 * phy_config_interrupt - configure the PHY device for the requested interrupts 162 * @phydev: the phy_device struct 163 * @interrupts: interrupt flags to configure for this @phydev 164 * 165 * Returns 0 on success or < 0 on error. 166 */ 167 static int phy_config_interrupt(struct phy_device *phydev, bool interrupts) 168 { 169 phydev->interrupts = interrupts ? 1 : 0; 170 if (phydev->drv->config_intr) 171 return phydev->drv->config_intr(phydev); 172 173 return 0; 174 } 175 176 /** 177 * phy_restart_aneg - restart auto-negotiation 178 * @phydev: target phy_device struct 179 * 180 * Restart the autonegotiation on @phydev. Returns >= 0 on success or 181 * negative errno on error. 182 */ 183 int phy_restart_aneg(struct phy_device *phydev) 184 { 185 int ret; 186 187 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0))) 188 ret = genphy_c45_restart_aneg(phydev); 189 else 190 ret = genphy_restart_aneg(phydev); 191 192 return ret; 193 } 194 EXPORT_SYMBOL_GPL(phy_restart_aneg); 195 196 /** 197 * phy_aneg_done - return auto-negotiation status 198 * @phydev: target phy_device struct 199 * 200 * Description: Return the auto-negotiation status from this @phydev 201 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation 202 * is still pending. 203 */ 204 int phy_aneg_done(struct phy_device *phydev) 205 { 206 if (phydev->drv && phydev->drv->aneg_done) 207 return phydev->drv->aneg_done(phydev); 208 else if (phydev->is_c45) 209 return genphy_c45_aneg_done(phydev); 210 else 211 return genphy_aneg_done(phydev); 212 } 213 EXPORT_SYMBOL(phy_aneg_done); 214 215 /** 216 * phy_find_valid - find a PHY setting that matches the requested parameters 217 * @speed: desired speed 218 * @duplex: desired duplex 219 * @supported: mask of supported link modes 220 * 221 * Locate a supported phy setting that is, in priority order: 222 * - an exact match for the specified speed and duplex mode 223 * - a match for the specified speed, or slower speed 224 * - the slowest supported speed 225 * Returns the matched phy_setting entry, or %NULL if no supported phy 226 * settings were found. 227 */ 228 static const struct phy_setting * 229 phy_find_valid(int speed, int duplex, unsigned long *supported) 230 { 231 return phy_lookup_setting(speed, duplex, supported, false); 232 } 233 234 /** 235 * phy_supported_speeds - return all speeds currently supported by a phy device 236 * @phy: The phy device to return supported speeds of. 237 * @speeds: buffer to store supported speeds in. 238 * @size: size of speeds buffer. 239 * 240 * Description: Returns the number of supported speeds, and fills the speeds 241 * buffer with the supported speeds. If speeds buffer is too small to contain 242 * all currently supported speeds, will return as many speeds as can fit. 243 */ 244 unsigned int phy_supported_speeds(struct phy_device *phy, 245 unsigned int *speeds, 246 unsigned int size) 247 { 248 return phy_speeds(speeds, size, phy->supported); 249 } 250 251 /** 252 * phy_check_valid - check if there is a valid PHY setting which matches 253 * speed, duplex, and feature mask 254 * @speed: speed to match 255 * @duplex: duplex to match 256 * @features: A mask of the valid settings 257 * 258 * Description: Returns true if there is a valid setting, false otherwise. 259 */ 260 bool phy_check_valid(int speed, int duplex, unsigned long *features) 261 { 262 return !!phy_lookup_setting(speed, duplex, features, true); 263 } 264 EXPORT_SYMBOL(phy_check_valid); 265 266 /** 267 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex 268 * @phydev: the target phy_device struct 269 * 270 * Description: Make sure the PHY is set to supported speeds and 271 * duplexes. Drop down by one in this order: 1000/FULL, 272 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. 273 */ 274 static void phy_sanitize_settings(struct phy_device *phydev) 275 { 276 const struct phy_setting *setting; 277 278 setting = phy_find_valid(phydev->speed, phydev->duplex, 279 phydev->supported); 280 if (setting) { 281 phydev->speed = setting->speed; 282 phydev->duplex = setting->duplex; 283 } else { 284 /* We failed to find anything (no supported speeds?) */ 285 phydev->speed = SPEED_UNKNOWN; 286 phydev->duplex = DUPLEX_UNKNOWN; 287 } 288 } 289 290 void phy_ethtool_ksettings_get(struct phy_device *phydev, 291 struct ethtool_link_ksettings *cmd) 292 { 293 mutex_lock(&phydev->lock); 294 linkmode_copy(cmd->link_modes.supported, phydev->supported); 295 linkmode_copy(cmd->link_modes.advertising, phydev->advertising); 296 linkmode_copy(cmd->link_modes.lp_advertising, phydev->lp_advertising); 297 298 cmd->base.speed = phydev->speed; 299 cmd->base.duplex = phydev->duplex; 300 cmd->base.master_slave_cfg = phydev->master_slave_get; 301 cmd->base.master_slave_state = phydev->master_slave_state; 302 cmd->base.rate_matching = phydev->rate_matching; 303 if (phydev->interface == PHY_INTERFACE_MODE_MOCA) 304 cmd->base.port = PORT_BNC; 305 else 306 cmd->base.port = phydev->port; 307 cmd->base.transceiver = phydev->is_internal ? 308 XCVR_INTERNAL : XCVR_EXTERNAL; 309 cmd->base.phy_address = phydev->mdio.addr; 310 cmd->base.autoneg = phydev->autoneg; 311 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl; 312 cmd->base.eth_tp_mdix = phydev->mdix; 313 mutex_unlock(&phydev->lock); 314 } 315 EXPORT_SYMBOL(phy_ethtool_ksettings_get); 316 317 /** 318 * phy_mii_ioctl - generic PHY MII ioctl interface 319 * @phydev: the phy_device struct 320 * @ifr: &struct ifreq for socket ioctl's 321 * @cmd: ioctl cmd to execute 322 * 323 * Note that this function is currently incompatible with the 324 * PHYCONTROL layer. It changes registers without regard to 325 * current state. Use at own risk. 326 */ 327 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd) 328 { 329 struct mii_ioctl_data *mii_data = if_mii(ifr); 330 struct kernel_hwtstamp_config kernel_cfg; 331 struct netlink_ext_ack extack = {}; 332 u16 val = mii_data->val_in; 333 bool change_autoneg = false; 334 struct hwtstamp_config cfg; 335 int prtad, devad; 336 int ret; 337 338 switch (cmd) { 339 case SIOCGMIIPHY: 340 mii_data->phy_id = phydev->mdio.addr; 341 fallthrough; 342 343 case SIOCGMIIREG: 344 if (mdio_phy_id_is_c45(mii_data->phy_id)) { 345 prtad = mdio_phy_id_prtad(mii_data->phy_id); 346 devad = mdio_phy_id_devad(mii_data->phy_id); 347 ret = mdiobus_c45_read(phydev->mdio.bus, prtad, devad, 348 mii_data->reg_num); 349 350 } else { 351 ret = mdiobus_read(phydev->mdio.bus, mii_data->phy_id, 352 mii_data->reg_num); 353 } 354 355 if (ret < 0) 356 return ret; 357 358 mii_data->val_out = ret; 359 360 return 0; 361 362 case SIOCSMIIREG: 363 if (mdio_phy_id_is_c45(mii_data->phy_id)) { 364 prtad = mdio_phy_id_prtad(mii_data->phy_id); 365 devad = mdio_phy_id_devad(mii_data->phy_id); 366 } else { 367 prtad = mii_data->phy_id; 368 devad = mii_data->reg_num; 369 } 370 if (prtad == phydev->mdio.addr) { 371 switch (devad) { 372 case MII_BMCR: 373 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) { 374 if (phydev->autoneg == AUTONEG_ENABLE) 375 change_autoneg = true; 376 phydev->autoneg = AUTONEG_DISABLE; 377 if (val & BMCR_FULLDPLX) 378 phydev->duplex = DUPLEX_FULL; 379 else 380 phydev->duplex = DUPLEX_HALF; 381 if (val & BMCR_SPEED1000) 382 phydev->speed = SPEED_1000; 383 else if (val & BMCR_SPEED100) 384 phydev->speed = SPEED_100; 385 else phydev->speed = SPEED_10; 386 } else { 387 if (phydev->autoneg == AUTONEG_DISABLE) 388 change_autoneg = true; 389 phydev->autoneg = AUTONEG_ENABLE; 390 } 391 break; 392 case MII_ADVERTISE: 393 mii_adv_mod_linkmode_adv_t(phydev->advertising, 394 val); 395 change_autoneg = true; 396 break; 397 case MII_CTRL1000: 398 mii_ctrl1000_mod_linkmode_adv_t(phydev->advertising, 399 val); 400 change_autoneg = true; 401 break; 402 default: 403 /* do nothing */ 404 break; 405 } 406 } 407 408 if (mdio_phy_id_is_c45(mii_data->phy_id)) 409 mdiobus_c45_write(phydev->mdio.bus, prtad, devad, 410 mii_data->reg_num, val); 411 else 412 mdiobus_write(phydev->mdio.bus, prtad, devad, val); 413 414 if (prtad == phydev->mdio.addr && 415 devad == MII_BMCR && 416 val & BMCR_RESET) 417 return phy_init_hw(phydev); 418 419 if (change_autoneg) 420 return phy_start_aneg(phydev); 421 422 return 0; 423 424 case SIOCSHWTSTAMP: 425 if (phydev->mii_ts && phydev->mii_ts->hwtstamp) { 426 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 427 return -EFAULT; 428 429 hwtstamp_config_to_kernel(&kernel_cfg, &cfg); 430 ret = phydev->mii_ts->hwtstamp(phydev->mii_ts, &kernel_cfg, &extack); 431 if (ret) 432 return ret; 433 434 hwtstamp_config_from_kernel(&cfg, &kernel_cfg); 435 if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg))) 436 return -EFAULT; 437 438 return 0; 439 } 440 fallthrough; 441 442 default: 443 return -EOPNOTSUPP; 444 } 445 } 446 EXPORT_SYMBOL(phy_mii_ioctl); 447 448 /** 449 * phy_do_ioctl - generic ndo_eth_ioctl implementation 450 * @dev: the net_device struct 451 * @ifr: &struct ifreq for socket ioctl's 452 * @cmd: ioctl cmd to execute 453 */ 454 int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 455 { 456 if (!dev->phydev) 457 return -ENODEV; 458 459 return phy_mii_ioctl(dev->phydev, ifr, cmd); 460 } 461 EXPORT_SYMBOL(phy_do_ioctl); 462 463 /** 464 * phy_do_ioctl_running - generic ndo_eth_ioctl implementation but test first 465 * 466 * @dev: the net_device struct 467 * @ifr: &struct ifreq for socket ioctl's 468 * @cmd: ioctl cmd to execute 469 * 470 * Same as phy_do_ioctl, but ensures that net_device is running before 471 * handling the ioctl. 472 */ 473 int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd) 474 { 475 if (!netif_running(dev)) 476 return -ENODEV; 477 478 return phy_do_ioctl(dev, ifr, cmd); 479 } 480 EXPORT_SYMBOL(phy_do_ioctl_running); 481 482 /** 483 * __phy_hwtstamp_get - Get hardware timestamping configuration from PHY 484 * 485 * @phydev: the PHY device structure 486 * @config: structure holding the timestamping configuration 487 * 488 * Query the PHY device for its current hardware timestamping configuration. 489 */ 490 int __phy_hwtstamp_get(struct phy_device *phydev, 491 struct kernel_hwtstamp_config *config) 492 { 493 if (!phydev) 494 return -ENODEV; 495 496 return -EOPNOTSUPP; 497 } 498 499 /** 500 * __phy_hwtstamp_set - Modify PHY hardware timestamping configuration 501 * 502 * @phydev: the PHY device structure 503 * @config: structure holding the timestamping configuration 504 * @extack: netlink extended ack structure, for error reporting 505 */ 506 int __phy_hwtstamp_set(struct phy_device *phydev, 507 struct kernel_hwtstamp_config *config, 508 struct netlink_ext_ack *extack) 509 { 510 if (!phydev) 511 return -ENODEV; 512 513 if (phydev->mii_ts && phydev->mii_ts->hwtstamp) 514 return phydev->mii_ts->hwtstamp(phydev->mii_ts, config, extack); 515 516 return -EOPNOTSUPP; 517 } 518 519 /** 520 * phy_queue_state_machine - Trigger the state machine to run soon 521 * 522 * @phydev: the phy_device struct 523 * @jiffies: Run the state machine after these jiffies 524 */ 525 static void phy_queue_state_machine(struct phy_device *phydev, 526 unsigned long jiffies) 527 { 528 mod_delayed_work(system_power_efficient_wq, &phydev->state_queue, 529 jiffies); 530 } 531 532 /** 533 * phy_trigger_machine - Trigger the state machine to run now 534 * 535 * @phydev: the phy_device struct 536 */ 537 void phy_trigger_machine(struct phy_device *phydev) 538 { 539 phy_queue_state_machine(phydev, 0); 540 } 541 EXPORT_SYMBOL(phy_trigger_machine); 542 543 static void phy_abort_cable_test(struct phy_device *phydev) 544 { 545 int err; 546 547 ethnl_cable_test_finished(phydev); 548 549 err = phy_init_hw(phydev); 550 if (err) 551 phydev_err(phydev, "Error while aborting cable test"); 552 } 553 554 /** 555 * phy_ethtool_get_strings - Get the statistic counter names 556 * 557 * @phydev: the phy_device struct 558 * @data: Where to put the strings 559 */ 560 int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data) 561 { 562 if (!phydev->drv) 563 return -EIO; 564 565 mutex_lock(&phydev->lock); 566 phydev->drv->get_strings(phydev, data); 567 mutex_unlock(&phydev->lock); 568 569 return 0; 570 } 571 EXPORT_SYMBOL(phy_ethtool_get_strings); 572 573 /** 574 * phy_ethtool_get_sset_count - Get the number of statistic counters 575 * 576 * @phydev: the phy_device struct 577 */ 578 int phy_ethtool_get_sset_count(struct phy_device *phydev) 579 { 580 int ret; 581 582 if (!phydev->drv) 583 return -EIO; 584 585 if (phydev->drv->get_sset_count && 586 phydev->drv->get_strings && 587 phydev->drv->get_stats) { 588 mutex_lock(&phydev->lock); 589 ret = phydev->drv->get_sset_count(phydev); 590 mutex_unlock(&phydev->lock); 591 592 return ret; 593 } 594 595 return -EOPNOTSUPP; 596 } 597 EXPORT_SYMBOL(phy_ethtool_get_sset_count); 598 599 /** 600 * phy_ethtool_get_stats - Get the statistic counters 601 * 602 * @phydev: the phy_device struct 603 * @stats: What counters to get 604 * @data: Where to store the counters 605 */ 606 int phy_ethtool_get_stats(struct phy_device *phydev, 607 struct ethtool_stats *stats, u64 *data) 608 { 609 if (!phydev->drv) 610 return -EIO; 611 612 mutex_lock(&phydev->lock); 613 phydev->drv->get_stats(phydev, stats, data); 614 mutex_unlock(&phydev->lock); 615 616 return 0; 617 } 618 EXPORT_SYMBOL(phy_ethtool_get_stats); 619 620 /** 621 * __phy_ethtool_get_phy_stats - Retrieve standardized PHY statistics 622 * @phydev: Pointer to the PHY device 623 * @phy_stats: Pointer to ethtool_eth_phy_stats structure 624 * @phydev_stats: Pointer to ethtool_phy_stats structure 625 * 626 * Fetches PHY statistics using a kernel-defined interface for consistent 627 * diagnostics. Unlike phy_ethtool_get_stats(), which allows custom stats, 628 * this function enforces a standardized format for better interoperability. 629 */ 630 void __phy_ethtool_get_phy_stats(struct phy_device *phydev, 631 struct ethtool_eth_phy_stats *phy_stats, 632 struct ethtool_phy_stats *phydev_stats) 633 { 634 if (!phydev->drv || !phydev->drv->get_phy_stats) 635 return; 636 637 mutex_lock(&phydev->lock); 638 phydev->drv->get_phy_stats(phydev, phy_stats, phydev_stats); 639 mutex_unlock(&phydev->lock); 640 } 641 642 /** 643 * __phy_ethtool_get_link_ext_stats - Retrieve extended link statistics for a PHY 644 * @phydev: Pointer to the PHY device 645 * @link_stats: Pointer to the structure to store extended link statistics 646 * 647 * Populates the ethtool_link_ext_stats structure with link down event counts 648 * and additional driver-specific link statistics, if available. 649 */ 650 void __phy_ethtool_get_link_ext_stats(struct phy_device *phydev, 651 struct ethtool_link_ext_stats *link_stats) 652 { 653 link_stats->link_down_events = READ_ONCE(phydev->link_down_events); 654 655 if (!phydev->drv || !phydev->drv->get_link_stats) 656 return; 657 658 mutex_lock(&phydev->lock); 659 phydev->drv->get_link_stats(phydev, link_stats); 660 mutex_unlock(&phydev->lock); 661 } 662 663 /** 664 * phy_ethtool_get_plca_cfg - Get PLCA RS configuration 665 * @phydev: the phy_device struct 666 * @plca_cfg: where to store the retrieved configuration 667 * 668 * Retrieve the PLCA configuration from the PHY. Return 0 on success or a 669 * negative value if an error occurred. 670 */ 671 int phy_ethtool_get_plca_cfg(struct phy_device *phydev, 672 struct phy_plca_cfg *plca_cfg) 673 { 674 int ret; 675 676 if (!phydev->drv) { 677 ret = -EIO; 678 goto out; 679 } 680 681 if (!phydev->drv->get_plca_cfg) { 682 ret = -EOPNOTSUPP; 683 goto out; 684 } 685 686 mutex_lock(&phydev->lock); 687 ret = phydev->drv->get_plca_cfg(phydev, plca_cfg); 688 689 mutex_unlock(&phydev->lock); 690 out: 691 return ret; 692 } 693 694 /** 695 * plca_check_valid - Check PLCA configuration before enabling 696 * @phydev: the phy_device struct 697 * @plca_cfg: current PLCA configuration 698 * @extack: extack for reporting useful error messages 699 * 700 * Checks whether the PLCA and PHY configuration are consistent and it is safe 701 * to enable PLCA. Returns 0 on success or a negative value if the PLCA or PHY 702 * configuration is not consistent. 703 */ 704 static int plca_check_valid(struct phy_device *phydev, 705 const struct phy_plca_cfg *plca_cfg, 706 struct netlink_ext_ack *extack) 707 { 708 int ret = 0; 709 710 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT, 711 phydev->advertising)) { 712 ret = -EOPNOTSUPP; 713 NL_SET_ERR_MSG(extack, 714 "Point to Multi-Point mode is not enabled"); 715 } else if (plca_cfg->node_id >= 255) { 716 NL_SET_ERR_MSG(extack, "PLCA node ID is not set"); 717 ret = -EINVAL; 718 } 719 720 return ret; 721 } 722 723 /** 724 * phy_ethtool_set_plca_cfg - Set PLCA RS configuration 725 * @phydev: the phy_device struct 726 * @plca_cfg: new PLCA configuration to apply 727 * @extack: extack for reporting useful error messages 728 * 729 * Sets the PLCA configuration in the PHY. Return 0 on success or a 730 * negative value if an error occurred. 731 */ 732 int phy_ethtool_set_plca_cfg(struct phy_device *phydev, 733 const struct phy_plca_cfg *plca_cfg, 734 struct netlink_ext_ack *extack) 735 { 736 struct phy_plca_cfg *curr_plca_cfg; 737 int ret; 738 739 if (!phydev->drv) { 740 ret = -EIO; 741 goto out; 742 } 743 744 if (!phydev->drv->set_plca_cfg || 745 !phydev->drv->get_plca_cfg) { 746 ret = -EOPNOTSUPP; 747 goto out; 748 } 749 750 curr_plca_cfg = kmalloc(sizeof(*curr_plca_cfg), GFP_KERNEL); 751 if (!curr_plca_cfg) { 752 ret = -ENOMEM; 753 goto out; 754 } 755 756 mutex_lock(&phydev->lock); 757 758 ret = phydev->drv->get_plca_cfg(phydev, curr_plca_cfg); 759 if (ret) 760 goto out_drv; 761 762 if (curr_plca_cfg->enabled < 0 && plca_cfg->enabled >= 0) { 763 NL_SET_ERR_MSG(extack, 764 "PHY does not support changing the PLCA 'enable' attribute"); 765 ret = -EINVAL; 766 goto out_drv; 767 } 768 769 if (curr_plca_cfg->node_id < 0 && plca_cfg->node_id >= 0) { 770 NL_SET_ERR_MSG(extack, 771 "PHY does not support changing the PLCA 'local node ID' attribute"); 772 ret = -EINVAL; 773 goto out_drv; 774 } 775 776 if (curr_plca_cfg->node_cnt < 0 && plca_cfg->node_cnt >= 0) { 777 NL_SET_ERR_MSG(extack, 778 "PHY does not support changing the PLCA 'node count' attribute"); 779 ret = -EINVAL; 780 goto out_drv; 781 } 782 783 if (curr_plca_cfg->to_tmr < 0 && plca_cfg->to_tmr >= 0) { 784 NL_SET_ERR_MSG(extack, 785 "PHY does not support changing the PLCA 'TO timer' attribute"); 786 ret = -EINVAL; 787 goto out_drv; 788 } 789 790 if (curr_plca_cfg->burst_cnt < 0 && plca_cfg->burst_cnt >= 0) { 791 NL_SET_ERR_MSG(extack, 792 "PHY does not support changing the PLCA 'burst count' attribute"); 793 ret = -EINVAL; 794 goto out_drv; 795 } 796 797 if (curr_plca_cfg->burst_tmr < 0 && plca_cfg->burst_tmr >= 0) { 798 NL_SET_ERR_MSG(extack, 799 "PHY does not support changing the PLCA 'burst timer' attribute"); 800 ret = -EINVAL; 801 goto out_drv; 802 } 803 804 // if enabling PLCA, perform a few sanity checks 805 if (plca_cfg->enabled > 0) { 806 // allow setting node_id concurrently with enabled 807 if (plca_cfg->node_id >= 0) 808 curr_plca_cfg->node_id = plca_cfg->node_id; 809 810 ret = plca_check_valid(phydev, curr_plca_cfg, extack); 811 if (ret) 812 goto out_drv; 813 } 814 815 ret = phydev->drv->set_plca_cfg(phydev, plca_cfg); 816 817 out_drv: 818 kfree(curr_plca_cfg); 819 mutex_unlock(&phydev->lock); 820 out: 821 return ret; 822 } 823 824 /** 825 * phy_ethtool_get_plca_status - Get PLCA RS status information 826 * @phydev: the phy_device struct 827 * @plca_st: where to store the retrieved status information 828 * 829 * Retrieve the PLCA status information from the PHY. Return 0 on success or a 830 * negative value if an error occurred. 831 */ 832 int phy_ethtool_get_plca_status(struct phy_device *phydev, 833 struct phy_plca_status *plca_st) 834 { 835 int ret; 836 837 if (!phydev->drv) { 838 ret = -EIO; 839 goto out; 840 } 841 842 if (!phydev->drv->get_plca_status) { 843 ret = -EOPNOTSUPP; 844 goto out; 845 } 846 847 mutex_lock(&phydev->lock); 848 ret = phydev->drv->get_plca_status(phydev, plca_st); 849 850 mutex_unlock(&phydev->lock); 851 out: 852 return ret; 853 } 854 855 /** 856 * phy_start_cable_test - Start a cable test 857 * 858 * @phydev: the phy_device struct 859 * @extack: extack for reporting useful error messages 860 */ 861 int phy_start_cable_test(struct phy_device *phydev, 862 struct netlink_ext_ack *extack) 863 { 864 struct net_device *dev = phydev->attached_dev; 865 int err = -ENOMEM; 866 867 if (!(phydev->drv && 868 phydev->drv->cable_test_start && 869 phydev->drv->cable_test_get_status)) { 870 NL_SET_ERR_MSG(extack, 871 "PHY driver does not support cable testing"); 872 return -EOPNOTSUPP; 873 } 874 875 mutex_lock(&phydev->lock); 876 if (phydev->state == PHY_CABLETEST) { 877 NL_SET_ERR_MSG(extack, 878 "PHY already performing a test"); 879 err = -EBUSY; 880 goto out; 881 } 882 883 if (phydev->state < PHY_UP || 884 phydev->state > PHY_CABLETEST) { 885 NL_SET_ERR_MSG(extack, 886 "PHY not configured. Try setting interface up"); 887 err = -EBUSY; 888 goto out; 889 } 890 891 err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_NTF); 892 if (err) 893 goto out; 894 895 /* Mark the carrier down until the test is complete */ 896 phy_link_down(phydev); 897 898 netif_testing_on(dev); 899 err = phydev->drv->cable_test_start(phydev); 900 if (err) { 901 netif_testing_off(dev); 902 phy_link_up(phydev); 903 goto out_free; 904 } 905 906 phydev->state = PHY_CABLETEST; 907 908 if (phy_polling_mode(phydev)) 909 phy_trigger_machine(phydev); 910 911 mutex_unlock(&phydev->lock); 912 913 return 0; 914 915 out_free: 916 ethnl_cable_test_free(phydev); 917 out: 918 mutex_unlock(&phydev->lock); 919 920 return err; 921 } 922 EXPORT_SYMBOL(phy_start_cable_test); 923 924 /** 925 * phy_start_cable_test_tdr - Start a raw TDR cable test 926 * 927 * @phydev: the phy_device struct 928 * @extack: extack for reporting useful error messages 929 * @config: Configuration of the test to run 930 */ 931 int phy_start_cable_test_tdr(struct phy_device *phydev, 932 struct netlink_ext_ack *extack, 933 const struct phy_tdr_config *config) 934 { 935 struct net_device *dev = phydev->attached_dev; 936 int err = -ENOMEM; 937 938 if (!(phydev->drv && 939 phydev->drv->cable_test_tdr_start && 940 phydev->drv->cable_test_get_status)) { 941 NL_SET_ERR_MSG(extack, 942 "PHY driver does not support cable test TDR"); 943 return -EOPNOTSUPP; 944 } 945 946 mutex_lock(&phydev->lock); 947 if (phydev->state == PHY_CABLETEST) { 948 NL_SET_ERR_MSG(extack, 949 "PHY already performing a test"); 950 err = -EBUSY; 951 goto out; 952 } 953 954 if (phydev->state < PHY_UP || 955 phydev->state > PHY_CABLETEST) { 956 NL_SET_ERR_MSG(extack, 957 "PHY not configured. Try setting interface up"); 958 err = -EBUSY; 959 goto out; 960 } 961 962 err = ethnl_cable_test_alloc(phydev, ETHTOOL_MSG_CABLE_TEST_TDR_NTF); 963 if (err) 964 goto out; 965 966 /* Mark the carrier down until the test is complete */ 967 phy_link_down(phydev); 968 969 netif_testing_on(dev); 970 err = phydev->drv->cable_test_tdr_start(phydev, config); 971 if (err) { 972 netif_testing_off(dev); 973 phy_link_up(phydev); 974 goto out_free; 975 } 976 977 phydev->state = PHY_CABLETEST; 978 979 if (phy_polling_mode(phydev)) 980 phy_trigger_machine(phydev); 981 982 mutex_unlock(&phydev->lock); 983 984 return 0; 985 986 out_free: 987 ethnl_cable_test_free(phydev); 988 out: 989 mutex_unlock(&phydev->lock); 990 991 return err; 992 } 993 EXPORT_SYMBOL(phy_start_cable_test_tdr); 994 995 int phy_config_aneg(struct phy_device *phydev) 996 { 997 if (phydev->drv->config_aneg) 998 return phydev->drv->config_aneg(phydev); 999 1000 /* Clause 45 PHYs that don't implement Clause 22 registers are not 1001 * allowed to call genphy_config_aneg() 1002 */ 1003 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0))) 1004 return genphy_c45_config_aneg(phydev); 1005 1006 return genphy_config_aneg(phydev); 1007 } 1008 EXPORT_SYMBOL(phy_config_aneg); 1009 1010 /** 1011 * phy_check_link_status - check link status and set state accordingly 1012 * @phydev: the phy_device struct 1013 * 1014 * Description: Check for link and whether autoneg was triggered / is running 1015 * and set state accordingly 1016 */ 1017 static int phy_check_link_status(struct phy_device *phydev) 1018 { 1019 int err; 1020 1021 lockdep_assert_held(&phydev->lock); 1022 1023 /* Keep previous state if loopback is enabled because some PHYs 1024 * report that Link is Down when loopback is enabled. 1025 */ 1026 if (phydev->loopback_enabled) 1027 return 0; 1028 1029 err = phy_read_status(phydev); 1030 if (err) 1031 return err; 1032 1033 if (phydev->link && phydev->state != PHY_RUNNING) { 1034 phy_check_downshift(phydev); 1035 phydev->state = PHY_RUNNING; 1036 err = genphy_c45_eee_is_active(phydev, NULL); 1037 phydev->eee_active = err > 0; 1038 phydev->enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled && 1039 phydev->eee_active; 1040 1041 phy_link_up(phydev); 1042 } else if (!phydev->link && phydev->state != PHY_NOLINK) { 1043 phydev->state = PHY_NOLINK; 1044 phydev->eee_active = false; 1045 phydev->enable_tx_lpi = false; 1046 phy_link_down(phydev); 1047 } 1048 1049 return 0; 1050 } 1051 1052 /** 1053 * phy_inband_caps - query which in-band signalling modes are supported 1054 * @phydev: a pointer to a &struct phy_device 1055 * @interface: the interface mode for the PHY 1056 * 1057 * Returns zero if it is unknown what in-band signalling is supported by the 1058 * PHY (e.g. because the PHY driver doesn't implement the method.) Otherwise, 1059 * returns a bit mask of the LINK_INBAND_* values from 1060 * &enum link_inband_signalling to describe which inband modes are supported 1061 * by the PHY for this interface mode. 1062 */ 1063 unsigned int phy_inband_caps(struct phy_device *phydev, 1064 phy_interface_t interface) 1065 { 1066 if (phydev->drv && phydev->drv->inband_caps) 1067 return phydev->drv->inband_caps(phydev, interface); 1068 1069 return 0; 1070 } 1071 EXPORT_SYMBOL_GPL(phy_inband_caps); 1072 1073 /** 1074 * phy_config_inband - configure the desired PHY in-band mode 1075 * @phydev: the phy_device struct 1076 * @modes: in-band modes to configure 1077 * 1078 * Description: disables, enables or enables-with-bypass in-band signalling 1079 * between the PHY and host system. 1080 * 1081 * Returns: zero on success, or negative errno value. 1082 */ 1083 int phy_config_inband(struct phy_device *phydev, unsigned int modes) 1084 { 1085 int err; 1086 1087 if (!!(modes & LINK_INBAND_DISABLE) + 1088 !!(modes & LINK_INBAND_ENABLE) + 1089 !!(modes & LINK_INBAND_BYPASS) != 1) 1090 return -EINVAL; 1091 1092 mutex_lock(&phydev->lock); 1093 if (!phydev->drv) 1094 err = -EIO; 1095 else if (!phydev->drv->config_inband) 1096 err = -EOPNOTSUPP; 1097 else 1098 err = phydev->drv->config_inband(phydev, modes); 1099 mutex_unlock(&phydev->lock); 1100 1101 return err; 1102 } 1103 EXPORT_SYMBOL(phy_config_inband); 1104 1105 /** 1106 * _phy_start_aneg - start auto-negotiation for this PHY device 1107 * @phydev: the phy_device struct 1108 * 1109 * Description: Sanitizes the settings (if we're not autonegotiating 1110 * them), and then calls the driver's config_aneg function. 1111 * If the PHYCONTROL Layer is operating, we change the state to 1112 * reflect the beginning of Auto-negotiation or forcing. 1113 */ 1114 int _phy_start_aneg(struct phy_device *phydev) 1115 { 1116 int err; 1117 1118 lockdep_assert_held(&phydev->lock); 1119 1120 if (!phydev->drv) 1121 return -EIO; 1122 1123 if (AUTONEG_DISABLE == phydev->autoneg) 1124 phy_sanitize_settings(phydev); 1125 1126 err = phy_config_aneg(phydev); 1127 if (err < 0) 1128 return err; 1129 1130 if (phy_is_started(phydev)) 1131 err = phy_check_link_status(phydev); 1132 1133 return err; 1134 } 1135 EXPORT_SYMBOL(_phy_start_aneg); 1136 1137 /** 1138 * phy_start_aneg - start auto-negotiation for this PHY device 1139 * @phydev: the phy_device struct 1140 * 1141 * Description: Sanitizes the settings (if we're not autonegotiating 1142 * them), and then calls the driver's config_aneg function. 1143 * If the PHYCONTROL Layer is operating, we change the state to 1144 * reflect the beginning of Auto-negotiation or forcing. 1145 */ 1146 int phy_start_aneg(struct phy_device *phydev) 1147 { 1148 int err; 1149 1150 mutex_lock(&phydev->lock); 1151 err = _phy_start_aneg(phydev); 1152 mutex_unlock(&phydev->lock); 1153 1154 return err; 1155 } 1156 EXPORT_SYMBOL(phy_start_aneg); 1157 1158 static int phy_poll_aneg_done(struct phy_device *phydev) 1159 { 1160 unsigned int retries = 100; 1161 int ret; 1162 1163 do { 1164 msleep(100); 1165 ret = phy_aneg_done(phydev); 1166 } while (!ret && --retries); 1167 1168 if (!ret) 1169 return -ETIMEDOUT; 1170 1171 return ret < 0 ? ret : 0; 1172 } 1173 1174 int phy_ethtool_ksettings_set(struct phy_device *phydev, 1175 const struct ethtool_link_ksettings *cmd) 1176 { 1177 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising); 1178 u8 autoneg = cmd->base.autoneg; 1179 u8 duplex = cmd->base.duplex; 1180 u32 speed = cmd->base.speed; 1181 1182 if (cmd->base.phy_address != phydev->mdio.addr) 1183 return -EINVAL; 1184 1185 linkmode_copy(advertising, cmd->link_modes.advertising); 1186 1187 /* We make sure that we don't pass unsupported values in to the PHY */ 1188 linkmode_and(advertising, advertising, phydev->supported); 1189 1190 /* Verify the settings we care about. */ 1191 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE) 1192 return -EINVAL; 1193 1194 if (autoneg == AUTONEG_ENABLE && 1195 (linkmode_empty(advertising) || 1196 !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 1197 phydev->supported))) 1198 return -EINVAL; 1199 1200 if (autoneg == AUTONEG_DISABLE && 1201 ((speed != SPEED_1000 && 1202 speed != SPEED_100 && 1203 speed != SPEED_10) || 1204 (duplex != DUPLEX_HALF && 1205 duplex != DUPLEX_FULL))) 1206 return -EINVAL; 1207 1208 mutex_lock(&phydev->lock); 1209 phydev->autoneg = autoneg; 1210 1211 if (autoneg == AUTONEG_DISABLE) { 1212 phydev->speed = speed; 1213 phydev->duplex = duplex; 1214 } 1215 1216 linkmode_copy(phydev->advertising, advertising); 1217 1218 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 1219 phydev->advertising, autoneg == AUTONEG_ENABLE); 1220 1221 phydev->master_slave_set = cmd->base.master_slave_cfg; 1222 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl; 1223 1224 /* Restart the PHY */ 1225 if (phy_is_started(phydev)) { 1226 phydev->state = PHY_UP; 1227 phy_trigger_machine(phydev); 1228 } else { 1229 _phy_start_aneg(phydev); 1230 } 1231 1232 mutex_unlock(&phydev->lock); 1233 return 0; 1234 } 1235 EXPORT_SYMBOL(phy_ethtool_ksettings_set); 1236 1237 /** 1238 * phy_speed_down - set speed to lowest speed supported by both link partners 1239 * @phydev: the phy_device struct 1240 * @sync: perform action synchronously 1241 * 1242 * Description: Typically used to save energy when waiting for a WoL packet 1243 * 1244 * WARNING: Setting sync to false may cause the system being unable to suspend 1245 * in case the PHY generates an interrupt when finishing the autonegotiation. 1246 * This interrupt may wake up the system immediately after suspend. 1247 * Therefore use sync = false only if you're sure it's safe with the respective 1248 * network chip. 1249 */ 1250 int phy_speed_down(struct phy_device *phydev, bool sync) 1251 { 1252 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp); 1253 int ret = 0; 1254 1255 mutex_lock(&phydev->lock); 1256 1257 if (phydev->autoneg != AUTONEG_ENABLE) 1258 goto out; 1259 1260 linkmode_copy(adv_tmp, phydev->advertising); 1261 1262 ret = phy_speed_down_core(phydev); 1263 if (ret) 1264 goto out; 1265 1266 linkmode_copy(phydev->adv_old, adv_tmp); 1267 1268 if (linkmode_equal(phydev->advertising, adv_tmp)) { 1269 ret = 0; 1270 goto out; 1271 } 1272 1273 ret = phy_config_aneg(phydev); 1274 if (ret) 1275 goto out; 1276 1277 ret = sync ? phy_poll_aneg_done(phydev) : 0; 1278 out: 1279 mutex_unlock(&phydev->lock); 1280 1281 return ret; 1282 } 1283 EXPORT_SYMBOL_GPL(phy_speed_down); 1284 1285 /** 1286 * phy_speed_up - (re)set advertised speeds to all supported speeds 1287 * @phydev: the phy_device struct 1288 * 1289 * Description: Used to revert the effect of phy_speed_down 1290 */ 1291 int phy_speed_up(struct phy_device *phydev) 1292 { 1293 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_tmp); 1294 int ret = 0; 1295 1296 mutex_lock(&phydev->lock); 1297 1298 if (phydev->autoneg != AUTONEG_ENABLE) 1299 goto out; 1300 1301 if (linkmode_empty(phydev->adv_old)) 1302 goto out; 1303 1304 linkmode_copy(adv_tmp, phydev->advertising); 1305 linkmode_copy(phydev->advertising, phydev->adv_old); 1306 linkmode_zero(phydev->adv_old); 1307 1308 if (linkmode_equal(phydev->advertising, adv_tmp)) 1309 goto out; 1310 1311 ret = phy_config_aneg(phydev); 1312 out: 1313 mutex_unlock(&phydev->lock); 1314 1315 return ret; 1316 } 1317 EXPORT_SYMBOL_GPL(phy_speed_up); 1318 1319 /** 1320 * phy_start_machine - start PHY state machine tracking 1321 * @phydev: the phy_device struct 1322 * 1323 * Description: The PHY infrastructure can run a state machine 1324 * which tracks whether the PHY is starting up, negotiating, 1325 * etc. This function starts the delayed workqueue which tracks 1326 * the state of the PHY. If you want to maintain your own state machine, 1327 * do not call this function. 1328 */ 1329 void phy_start_machine(struct phy_device *phydev) 1330 { 1331 phy_trigger_machine(phydev); 1332 } 1333 EXPORT_SYMBOL_GPL(phy_start_machine); 1334 1335 /** 1336 * phy_stop_machine - stop the PHY state machine tracking 1337 * @phydev: target phy_device struct 1338 * 1339 * Description: Stops the state machine delayed workqueue, sets the 1340 * state to UP (unless it wasn't up yet). This function must be 1341 * called BEFORE phy_detach. 1342 */ 1343 void phy_stop_machine(struct phy_device *phydev) 1344 { 1345 cancel_delayed_work_sync(&phydev->state_queue); 1346 1347 mutex_lock(&phydev->lock); 1348 if (phy_is_started(phydev)) 1349 phydev->state = PHY_UP; 1350 mutex_unlock(&phydev->lock); 1351 } 1352 1353 static void phy_process_error(struct phy_device *phydev) 1354 { 1355 /* phydev->lock must be held for the state change to be safe */ 1356 if (!mutex_is_locked(&phydev->lock)) 1357 phydev_err(phydev, "PHY-device data unsafe context\n"); 1358 1359 phydev->state = PHY_ERROR; 1360 1361 phy_trigger_machine(phydev); 1362 } 1363 1364 static void phy_error_precise(struct phy_device *phydev, 1365 const void *func, int err) 1366 { 1367 WARN(1, "%pS: returned: %d\n", func, err); 1368 phy_process_error(phydev); 1369 } 1370 1371 /** 1372 * phy_error - enter ERROR state for this PHY device 1373 * @phydev: target phy_device struct 1374 * 1375 * Moves the PHY to the ERROR state in response to a read 1376 * or write error, and tells the controller the link is down. 1377 * Must be called with phydev->lock held. 1378 */ 1379 void phy_error(struct phy_device *phydev) 1380 { 1381 WARN_ON(1); 1382 phy_process_error(phydev); 1383 } 1384 EXPORT_SYMBOL(phy_error); 1385 1386 /** 1387 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side 1388 * @phydev: target phy_device struct 1389 */ 1390 int phy_disable_interrupts(struct phy_device *phydev) 1391 { 1392 /* Disable PHY interrupts */ 1393 return phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); 1394 } 1395 1396 /** 1397 * phy_interrupt - PHY interrupt handler 1398 * @irq: interrupt line 1399 * @phy_dat: phy_device pointer 1400 * 1401 * Description: Handle PHY interrupt 1402 */ 1403 static irqreturn_t phy_interrupt(int irq, void *phy_dat) 1404 { 1405 struct phy_device *phydev = phy_dat; 1406 irqreturn_t ret; 1407 1408 /* Wakeup interrupts may occur during a system sleep transition. 1409 * Postpone handling until the PHY has resumed. 1410 */ 1411 if (IS_ENABLED(CONFIG_PM_SLEEP) && phydev->irq_suspended) { 1412 struct net_device *netdev = phydev->attached_dev; 1413 1414 if (netdev) { 1415 struct device *parent = netdev->dev.parent; 1416 1417 if (netdev->ethtool->wol_enabled) 1418 pm_system_wakeup(); 1419 else if (device_may_wakeup(&netdev->dev)) 1420 pm_wakeup_dev_event(&netdev->dev, 0, true); 1421 else if (parent && device_may_wakeup(parent)) 1422 pm_wakeup_dev_event(parent, 0, true); 1423 } 1424 1425 phydev->irq_rerun = 1; 1426 disable_irq_nosync(irq); 1427 return IRQ_HANDLED; 1428 } 1429 1430 mutex_lock(&phydev->lock); 1431 ret = phydev->drv->handle_interrupt(phydev); 1432 mutex_unlock(&phydev->lock); 1433 1434 return ret; 1435 } 1436 1437 /** 1438 * phy_enable_interrupts - Enable the interrupts from the PHY side 1439 * @phydev: target phy_device struct 1440 */ 1441 static int phy_enable_interrupts(struct phy_device *phydev) 1442 { 1443 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); 1444 } 1445 1446 /** 1447 * phy_update_stats - Update PHY device statistics if supported. 1448 * @phydev: Pointer to the PHY device structure. 1449 * 1450 * If the PHY driver provides an update_stats callback, this function 1451 * invokes it to update the PHY statistics. If not, it returns 0. 1452 * 1453 * Return: 0 on success, or a negative error code if the callback fails. 1454 */ 1455 static int phy_update_stats(struct phy_device *phydev) 1456 { 1457 if (!phydev->drv->update_stats) 1458 return 0; 1459 1460 return phydev->drv->update_stats(phydev); 1461 } 1462 1463 /** 1464 * phy_request_interrupt - request and enable interrupt for a PHY device 1465 * @phydev: target phy_device struct 1466 * 1467 * Description: Request and enable the interrupt for the given PHY. 1468 * If this fails, then we set irq to PHY_POLL. 1469 * This should only be called with a valid IRQ number. 1470 */ 1471 void phy_request_interrupt(struct phy_device *phydev) 1472 { 1473 int err; 1474 1475 err = request_threaded_irq(phydev->irq, NULL, phy_interrupt, 1476 IRQF_ONESHOT | IRQF_SHARED, 1477 phydev_name(phydev), phydev); 1478 if (err) { 1479 phydev_warn(phydev, "Error %d requesting IRQ %d, falling back to polling\n", 1480 err, phydev->irq); 1481 phydev->irq = PHY_POLL; 1482 } else { 1483 if (phy_enable_interrupts(phydev)) { 1484 phydev_warn(phydev, "Can't enable interrupt, falling back to polling\n"); 1485 phy_free_interrupt(phydev); 1486 phydev->irq = PHY_POLL; 1487 } 1488 } 1489 } 1490 EXPORT_SYMBOL(phy_request_interrupt); 1491 1492 /** 1493 * phy_free_interrupt - disable and free interrupt for a PHY device 1494 * @phydev: target phy_device struct 1495 * 1496 * Description: Disable and free the interrupt for the given PHY. 1497 * This should only be called with a valid IRQ number. 1498 */ 1499 void phy_free_interrupt(struct phy_device *phydev) 1500 { 1501 phy_disable_interrupts(phydev); 1502 free_irq(phydev->irq, phydev); 1503 } 1504 EXPORT_SYMBOL(phy_free_interrupt); 1505 1506 /** 1507 * phy_get_next_update_time - Determine the next PHY update time 1508 * @phydev: Pointer to the phy_device structure 1509 * 1510 * This function queries the PHY driver to get the time for the next polling 1511 * event. If the driver does not implement the callback, a default value is 1512 * used. 1513 * 1514 * Return: The time for the next polling event in jiffies 1515 */ 1516 static unsigned int phy_get_next_update_time(struct phy_device *phydev) 1517 { 1518 if (phydev->drv && phydev->drv->get_next_update_time) 1519 return phydev->drv->get_next_update_time(phydev); 1520 1521 return PHY_STATE_TIME; 1522 } 1523 1524 enum phy_state_work { 1525 PHY_STATE_WORK_NONE, 1526 PHY_STATE_WORK_ANEG, 1527 PHY_STATE_WORK_SUSPEND, 1528 }; 1529 1530 static enum phy_state_work _phy_state_machine(struct phy_device *phydev) 1531 { 1532 enum phy_state_work state_work = PHY_STATE_WORK_NONE; 1533 struct net_device *dev = phydev->attached_dev; 1534 enum phy_state old_state = phydev->state; 1535 const void *func = NULL; 1536 bool finished = false; 1537 int err = 0; 1538 1539 switch (phydev->state) { 1540 case PHY_DOWN: 1541 case PHY_READY: 1542 break; 1543 case PHY_UP: 1544 state_work = PHY_STATE_WORK_ANEG; 1545 break; 1546 case PHY_NOLINK: 1547 case PHY_RUNNING: 1548 err = phy_check_link_status(phydev); 1549 func = &phy_check_link_status; 1550 1551 if (!err) 1552 err = phy_update_stats(phydev); 1553 break; 1554 case PHY_CABLETEST: 1555 err = phydev->drv->cable_test_get_status(phydev, &finished); 1556 if (err) { 1557 phy_abort_cable_test(phydev); 1558 netif_testing_off(dev); 1559 state_work = PHY_STATE_WORK_ANEG; 1560 phydev->state = PHY_UP; 1561 break; 1562 } 1563 1564 if (finished) { 1565 ethnl_cable_test_finished(phydev); 1566 netif_testing_off(dev); 1567 state_work = PHY_STATE_WORK_ANEG; 1568 phydev->state = PHY_UP; 1569 } 1570 break; 1571 case PHY_HALTED: 1572 case PHY_ERROR: 1573 if (phydev->link) { 1574 phydev->link = 0; 1575 phy_link_down(phydev); 1576 } 1577 state_work = PHY_STATE_WORK_SUSPEND; 1578 break; 1579 } 1580 1581 if (state_work == PHY_STATE_WORK_ANEG) { 1582 err = _phy_start_aneg(phydev); 1583 func = &_phy_start_aneg; 1584 } 1585 1586 if (err == -ENODEV) 1587 return state_work; 1588 1589 if (err < 0) 1590 phy_error_precise(phydev, func, err); 1591 1592 phy_process_state_change(phydev, old_state); 1593 1594 /* Only re-schedule a PHY state machine change if we are polling the 1595 * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving 1596 * between states from phy_mac_interrupt(). 1597 * 1598 * In state PHY_HALTED the PHY gets suspended, so rescheduling the 1599 * state machine would be pointless and possibly error prone when 1600 * called from phy_disconnect() synchronously. 1601 */ 1602 if (phy_polling_mode(phydev) && phy_is_started(phydev)) 1603 phy_queue_state_machine(phydev, 1604 phy_get_next_update_time(phydev)); 1605 1606 return state_work; 1607 } 1608 1609 /* unlocked part of the PHY state machine */ 1610 static void _phy_state_machine_post_work(struct phy_device *phydev, 1611 enum phy_state_work state_work) 1612 { 1613 if (state_work == PHY_STATE_WORK_SUSPEND) 1614 phy_suspend(phydev); 1615 } 1616 1617 /** 1618 * phy_state_machine - Handle the state machine 1619 * @work: work_struct that describes the work to be done 1620 */ 1621 void phy_state_machine(struct work_struct *work) 1622 { 1623 struct delayed_work *dwork = to_delayed_work(work); 1624 struct phy_device *phydev = 1625 container_of(dwork, struct phy_device, state_queue); 1626 enum phy_state_work state_work; 1627 1628 mutex_lock(&phydev->lock); 1629 state_work = _phy_state_machine(phydev); 1630 mutex_unlock(&phydev->lock); 1631 1632 _phy_state_machine_post_work(phydev, state_work); 1633 } 1634 1635 /** 1636 * phy_stop - Bring down the PHY link, and stop checking the status 1637 * @phydev: target phy_device struct 1638 */ 1639 void phy_stop(struct phy_device *phydev) 1640 { 1641 struct net_device *dev = phydev->attached_dev; 1642 enum phy_state_work state_work; 1643 enum phy_state old_state; 1644 1645 if (!phy_is_started(phydev) && phydev->state != PHY_DOWN && 1646 phydev->state != PHY_ERROR) { 1647 WARN(1, "called from state %s\n", 1648 phy_state_to_str(phydev->state)); 1649 return; 1650 } 1651 1652 mutex_lock(&phydev->lock); 1653 old_state = phydev->state; 1654 1655 if (phydev->state == PHY_CABLETEST) { 1656 phy_abort_cable_test(phydev); 1657 netif_testing_off(dev); 1658 } 1659 1660 if (phydev->sfp_bus) 1661 sfp_upstream_stop(phydev->sfp_bus); 1662 1663 phydev->state = PHY_HALTED; 1664 phy_process_state_change(phydev, old_state); 1665 1666 state_work = _phy_state_machine(phydev); 1667 mutex_unlock(&phydev->lock); 1668 1669 _phy_state_machine_post_work(phydev, state_work); 1670 phy_stop_machine(phydev); 1671 1672 /* Cannot call flush_scheduled_work() here as desired because 1673 * of rtnl_lock(), but PHY_HALTED shall guarantee irq handler 1674 * will not reenable interrupts. 1675 */ 1676 } 1677 EXPORT_SYMBOL(phy_stop); 1678 1679 /** 1680 * phy_start - start or restart a PHY device 1681 * @phydev: target phy_device struct 1682 * 1683 * Description: Indicates the attached device's readiness to 1684 * handle PHY-related work. Used during startup to start the 1685 * PHY, and after a call to phy_stop() to resume operation. 1686 * Also used to indicate the MDIO bus has cleared an error 1687 * condition. 1688 */ 1689 void phy_start(struct phy_device *phydev) 1690 { 1691 mutex_lock(&phydev->lock); 1692 1693 if (phydev->state != PHY_READY && phydev->state != PHY_HALTED) { 1694 WARN(1, "called from state %s\n", 1695 phy_state_to_str(phydev->state)); 1696 goto out; 1697 } 1698 1699 if (phydev->sfp_bus) 1700 sfp_upstream_start(phydev->sfp_bus); 1701 1702 /* if phy was suspended, bring the physical link up again */ 1703 __phy_resume(phydev); 1704 1705 phydev->state = PHY_UP; 1706 1707 phy_start_machine(phydev); 1708 out: 1709 mutex_unlock(&phydev->lock); 1710 } 1711 EXPORT_SYMBOL(phy_start); 1712 1713 /** 1714 * phy_mac_interrupt - MAC says the link has changed 1715 * @phydev: phy_device struct with changed link 1716 * 1717 * The MAC layer is able to indicate there has been a change in the PHY link 1718 * status. Trigger the state machine and work a work queue. 1719 */ 1720 void phy_mac_interrupt(struct phy_device *phydev) 1721 { 1722 /* Trigger a state machine change */ 1723 phy_trigger_machine(phydev); 1724 } 1725 EXPORT_SYMBOL(phy_mac_interrupt); 1726 1727 /** 1728 * phy_eee_tx_clock_stop_capable() - indicate whether the MAC can stop tx clock 1729 * @phydev: target phy_device struct 1730 * 1731 * Indicate whether the MAC can disable the transmit xMII clock while in LPI 1732 * state. Returns 1 if the MAC may stop the transmit clock, 0 if the MAC must 1733 * not stop the transmit clock, or negative error. 1734 */ 1735 int phy_eee_tx_clock_stop_capable(struct phy_device *phydev) 1736 { 1737 int stat1; 1738 1739 stat1 = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1); 1740 if (stat1 < 0) 1741 return stat1; 1742 1743 return !!(stat1 & MDIO_PCS_STAT1_CLKSTOP_CAP); 1744 } 1745 EXPORT_SYMBOL_GPL(phy_eee_tx_clock_stop_capable); 1746 1747 /** 1748 * phy_eee_rx_clock_stop() - configure PHY receive clock in LPI 1749 * @phydev: target phy_device struct 1750 * @clk_stop_enable: flag to indicate whether the clock can be stopped 1751 * 1752 * Configure whether the PHY can disable its receive clock during LPI mode, 1753 * See IEEE 802.3 sections 22.2.2.2, 35.2.2.10, and 45.2.3.1.4. 1754 * 1755 * Returns: 0 or negative error. 1756 */ 1757 int phy_eee_rx_clock_stop(struct phy_device *phydev, bool clk_stop_enable) 1758 { 1759 /* Configure the PHY to stop receiving xMII 1760 * clock while it is signaling LPI. 1761 */ 1762 return phy_modify_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, 1763 MDIO_PCS_CTRL1_CLKSTOP_EN, 1764 clk_stop_enable ? MDIO_PCS_CTRL1_CLKSTOP_EN : 0); 1765 } 1766 EXPORT_SYMBOL_GPL(phy_eee_rx_clock_stop); 1767 1768 /** 1769 * phy_init_eee - init and check the EEE feature 1770 * @phydev: target phy_device struct 1771 * @clk_stop_enable: PHY may stop the clock during LPI 1772 * 1773 * Description: it checks if the Energy-Efficient Ethernet (EEE) 1774 * is supported by looking at the MMD registers 3.20 and 7.60/61 1775 * and it programs the MMD register 3.0 setting the "Clock stop enable" 1776 * bit if required. 1777 */ 1778 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable) 1779 { 1780 int ret; 1781 1782 if (!phydev->drv) 1783 return -EIO; 1784 1785 ret = genphy_c45_eee_is_active(phydev, NULL); 1786 if (ret < 0) 1787 return ret; 1788 if (!ret) 1789 return -EPROTONOSUPPORT; 1790 1791 if (clk_stop_enable) 1792 ret = phy_eee_rx_clock_stop(phydev, true); 1793 1794 return ret < 0 ? ret : 0; 1795 } 1796 EXPORT_SYMBOL(phy_init_eee); 1797 1798 /** 1799 * phy_get_eee_err - report the EEE wake error count 1800 * @phydev: target phy_device struct 1801 * 1802 * Description: it is to report the number of time where the PHY 1803 * failed to complete its normal wake sequence. 1804 */ 1805 int phy_get_eee_err(struct phy_device *phydev) 1806 { 1807 int ret; 1808 1809 if (!phydev->drv) 1810 return -EIO; 1811 1812 mutex_lock(&phydev->lock); 1813 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR); 1814 mutex_unlock(&phydev->lock); 1815 1816 return ret; 1817 } 1818 EXPORT_SYMBOL(phy_get_eee_err); 1819 1820 /** 1821 * phy_ethtool_get_eee - get EEE supported and status 1822 * @phydev: target phy_device struct 1823 * @data: ethtool_keee data 1824 * 1825 * Description: get the current EEE settings, filling in all members of 1826 * @data. 1827 */ 1828 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_keee *data) 1829 { 1830 int ret; 1831 1832 if (!phydev->drv) 1833 return -EIO; 1834 1835 mutex_lock(&phydev->lock); 1836 ret = genphy_c45_ethtool_get_eee(phydev, data); 1837 eeecfg_to_eee(data, &phydev->eee_cfg); 1838 mutex_unlock(&phydev->lock); 1839 1840 return ret; 1841 } 1842 EXPORT_SYMBOL(phy_ethtool_get_eee); 1843 1844 /** 1845 * phy_ethtool_set_eee_noneg - Adjusts MAC LPI configuration without PHY 1846 * renegotiation 1847 * @phydev: pointer to the target PHY device structure 1848 * @old_cfg: pointer to the eee_config structure containing the old EEE settings 1849 * 1850 * This function updates the Energy Efficient Ethernet (EEE) configuration 1851 * for cases where only the MAC's Low Power Idle (LPI) configuration changes, 1852 * without triggering PHY renegotiation. It ensures that the MAC is properly 1853 * informed of the new LPI settings by cycling the link down and up, which 1854 * is necessary for the MAC to adopt the new configuration. This adjustment 1855 * is done only if there is a change in the tx_lpi_enabled or tx_lpi_timer 1856 * configuration. 1857 */ 1858 static void phy_ethtool_set_eee_noneg(struct phy_device *phydev, 1859 const struct eee_config *old_cfg) 1860 { 1861 bool enable_tx_lpi; 1862 1863 if (!phydev->link) 1864 return; 1865 1866 enable_tx_lpi = phydev->eee_cfg.tx_lpi_enabled && phydev->eee_active; 1867 1868 if (phydev->enable_tx_lpi != enable_tx_lpi || 1869 phydev->eee_cfg.tx_lpi_timer != old_cfg->tx_lpi_timer) { 1870 phydev->enable_tx_lpi = false; 1871 phydev->link = false; 1872 phy_link_down(phydev); 1873 phydev->enable_tx_lpi = enable_tx_lpi; 1874 phydev->link = true; 1875 phy_link_up(phydev); 1876 } 1877 } 1878 1879 /** 1880 * phy_ethtool_set_eee - set EEE supported and status 1881 * @phydev: target phy_device struct 1882 * @data: ethtool_keee data 1883 * 1884 * Description: it is to program the Advertisement EEE register. 1885 */ 1886 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_keee *data) 1887 { 1888 struct eee_config old_cfg; 1889 int ret; 1890 1891 if (!phydev->drv) 1892 return -EIO; 1893 1894 mutex_lock(&phydev->lock); 1895 1896 old_cfg = phydev->eee_cfg; 1897 eee_to_eeecfg(&phydev->eee_cfg, data); 1898 1899 ret = genphy_c45_ethtool_set_eee(phydev, data); 1900 if (ret == 0) 1901 phy_ethtool_set_eee_noneg(phydev, &old_cfg); 1902 else if (ret < 0) 1903 phydev->eee_cfg = old_cfg; 1904 1905 mutex_unlock(&phydev->lock); 1906 1907 return ret < 0 ? ret : 0; 1908 } 1909 EXPORT_SYMBOL(phy_ethtool_set_eee); 1910 1911 /** 1912 * phy_ethtool_set_wol - Configure Wake On LAN 1913 * 1914 * @phydev: target phy_device struct 1915 * @wol: Configuration requested 1916 */ 1917 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) 1918 { 1919 int ret; 1920 1921 if (phydev->drv && phydev->drv->set_wol) { 1922 mutex_lock(&phydev->lock); 1923 ret = phydev->drv->set_wol(phydev, wol); 1924 mutex_unlock(&phydev->lock); 1925 1926 return ret; 1927 } 1928 1929 return -EOPNOTSUPP; 1930 } 1931 EXPORT_SYMBOL(phy_ethtool_set_wol); 1932 1933 /** 1934 * phy_ethtool_get_wol - Get the current Wake On LAN configuration 1935 * 1936 * @phydev: target phy_device struct 1937 * @wol: Store the current configuration here 1938 */ 1939 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol) 1940 { 1941 if (phydev->drv && phydev->drv->get_wol) { 1942 mutex_lock(&phydev->lock); 1943 phydev->drv->get_wol(phydev, wol); 1944 mutex_unlock(&phydev->lock); 1945 } 1946 } 1947 EXPORT_SYMBOL(phy_ethtool_get_wol); 1948 1949 int phy_ethtool_get_link_ksettings(struct net_device *ndev, 1950 struct ethtool_link_ksettings *cmd) 1951 { 1952 struct phy_device *phydev = ndev->phydev; 1953 1954 if (!phydev) 1955 return -ENODEV; 1956 1957 phy_ethtool_ksettings_get(phydev, cmd); 1958 1959 return 0; 1960 } 1961 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings); 1962 1963 int phy_ethtool_set_link_ksettings(struct net_device *ndev, 1964 const struct ethtool_link_ksettings *cmd) 1965 { 1966 struct phy_device *phydev = ndev->phydev; 1967 1968 if (!phydev) 1969 return -ENODEV; 1970 1971 return phy_ethtool_ksettings_set(phydev, cmd); 1972 } 1973 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings); 1974 1975 /** 1976 * phy_ethtool_nway_reset - Restart auto negotiation 1977 * @ndev: Network device to restart autoneg for 1978 */ 1979 int phy_ethtool_nway_reset(struct net_device *ndev) 1980 { 1981 struct phy_device *phydev = ndev->phydev; 1982 int ret; 1983 1984 if (!phydev) 1985 return -ENODEV; 1986 1987 if (!phydev->drv) 1988 return -EIO; 1989 1990 mutex_lock(&phydev->lock); 1991 ret = phy_restart_aneg(phydev); 1992 mutex_unlock(&phydev->lock); 1993 1994 return ret; 1995 } 1996 EXPORT_SYMBOL(phy_ethtool_nway_reset); 1997