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