1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Framework for finding and configuring PHYs. 3 * Also contains generic PHY driver 4 * 5 * Author: Andy Fleming 6 * 7 * Copyright (c) 2004 Freescale Semiconductor, Inc. 8 */ 9 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12 #include <linux/acpi.h> 13 #include <linux/bitmap.h> 14 #include <linux/delay.h> 15 #include <linux/errno.h> 16 #include <linux/etherdevice.h> 17 #include <linux/ethtool.h> 18 #include <linux/init.h> 19 #include <linux/interrupt.h> 20 #include <linux/io.h> 21 #include <linux/kernel.h> 22 #include <linux/list.h> 23 #include <linux/mdio.h> 24 #include <linux/mii.h> 25 #include <linux/mm.h> 26 #include <linux/module.h> 27 #include <linux/of.h> 28 #include <linux/netdevice.h> 29 #include <linux/phy.h> 30 #include <linux/phylib_stubs.h> 31 #include <linux/phy_led_triggers.h> 32 #include <linux/phy_link_topology.h> 33 #include <linux/phy_port.h> 34 #include <linux/pse-pd/pse.h> 35 #include <linux/property.h> 36 #include <linux/ptp_clock_kernel.h> 37 #include <linux/rtnetlink.h> 38 #include <linux/sfp.h> 39 #include <linux/skbuff.h> 40 #include <linux/slab.h> 41 #include <linux/string.h> 42 #include <linux/uaccess.h> 43 #include <linux/unistd.h> 44 45 #include "phylib-internal.h" 46 #include "phy-caps.h" 47 48 MODULE_DESCRIPTION("PHY library"); 49 MODULE_AUTHOR("Andy Fleming"); 50 MODULE_LICENSE("GPL"); 51 52 static struct phy_driver genphy_c45_driver = { 53 .phy_id = 0xffffffff, 54 .phy_id_mask = 0xffffffff, 55 .name = "Generic Clause 45 PHY", 56 .read_status = genphy_c45_read_status, 57 }; 58 59 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init; 60 EXPORT_SYMBOL_GPL(phy_basic_features); 61 62 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init; 63 EXPORT_SYMBOL_GPL(phy_basic_t1_features); 64 65 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1s_p2mp_features) __ro_after_init; 66 EXPORT_SYMBOL_GPL(phy_basic_t1s_p2mp_features); 67 68 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init; 69 EXPORT_SYMBOL_GPL(phy_gbit_features); 70 71 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init; 72 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features); 73 74 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init; 75 EXPORT_SYMBOL_GPL(phy_10gbit_features); 76 77 const int phy_basic_ports_array[3] = { 78 ETHTOOL_LINK_MODE_Autoneg_BIT, 79 ETHTOOL_LINK_MODE_TP_BIT, 80 ETHTOOL_LINK_MODE_MII_BIT, 81 }; 82 EXPORT_SYMBOL_GPL(phy_basic_ports_array); 83 84 static const int phy_all_ports_features_array[7] __initconst = { 85 ETHTOOL_LINK_MODE_Autoneg_BIT, 86 ETHTOOL_LINK_MODE_TP_BIT, 87 ETHTOOL_LINK_MODE_MII_BIT, 88 ETHTOOL_LINK_MODE_FIBRE_BIT, 89 ETHTOOL_LINK_MODE_AUI_BIT, 90 ETHTOOL_LINK_MODE_BNC_BIT, 91 ETHTOOL_LINK_MODE_Backplane_BIT, 92 }; 93 94 static const int phy_10_100_features_array[4] __initconst = { 95 ETHTOOL_LINK_MODE_10baseT_Half_BIT, 96 ETHTOOL_LINK_MODE_10baseT_Full_BIT, 97 ETHTOOL_LINK_MODE_100baseT_Half_BIT, 98 ETHTOOL_LINK_MODE_100baseT_Full_BIT, 99 }; 100 101 static const int phy_basic_t1_features_array[3] __initconst = { 102 ETHTOOL_LINK_MODE_TP_BIT, 103 ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, 104 ETHTOOL_LINK_MODE_100baseT1_Full_BIT, 105 }; 106 107 static const int phy_basic_t1s_p2mp_features_array[2] __initconst = { 108 ETHTOOL_LINK_MODE_TP_BIT, 109 ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT, 110 }; 111 112 static const int phy_gbit_features_array[2] __initconst = { 113 ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 114 ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 115 }; 116 117 static const int phy_eee_cap1_features_array[] __initconst = { 118 ETHTOOL_LINK_MODE_100baseT_Full_BIT, 119 ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 120 ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 121 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, 122 ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, 123 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, 124 }; 125 126 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap1_features) __ro_after_init; 127 EXPORT_SYMBOL_GPL(phy_eee_cap1_features); 128 129 static const int phy_eee_cap2_features_array[] __initconst = { 130 ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 131 ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 132 }; 133 134 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap2_features) __ro_after_init; 135 EXPORT_SYMBOL_GPL(phy_eee_cap2_features); 136 137 static void __init features_init(void) 138 { 139 /* 10/100 half/full*/ 140 linkmode_set_bit_array(phy_basic_ports_array, 141 ARRAY_SIZE(phy_basic_ports_array), 142 phy_basic_features); 143 linkmode_set_bit_array(phy_10_100_features_array, 144 ARRAY_SIZE(phy_10_100_features_array), 145 phy_basic_features); 146 147 /* 100 full, TP */ 148 linkmode_set_bit_array(phy_basic_t1_features_array, 149 ARRAY_SIZE(phy_basic_t1_features_array), 150 phy_basic_t1_features); 151 152 /* 10 half, P2MP, TP */ 153 linkmode_set_bit_array(phy_basic_t1s_p2mp_features_array, 154 ARRAY_SIZE(phy_basic_t1s_p2mp_features_array), 155 phy_basic_t1s_p2mp_features); 156 157 /* 10/100 half/full + 1000 half/full */ 158 linkmode_set_bit_array(phy_basic_ports_array, 159 ARRAY_SIZE(phy_basic_ports_array), 160 phy_gbit_features); 161 linkmode_set_bit_array(phy_10_100_features_array, 162 ARRAY_SIZE(phy_10_100_features_array), 163 phy_gbit_features); 164 linkmode_set_bit_array(phy_gbit_features_array, 165 ARRAY_SIZE(phy_gbit_features_array), 166 phy_gbit_features); 167 168 /* 10/100 half/full + 1000 half/full + fibre*/ 169 linkmode_set_bit_array(phy_basic_ports_array, 170 ARRAY_SIZE(phy_basic_ports_array), 171 phy_gbit_fibre_features); 172 linkmode_set_bit_array(phy_10_100_features_array, 173 ARRAY_SIZE(phy_10_100_features_array), 174 phy_gbit_fibre_features); 175 linkmode_set_bit_array(phy_gbit_features_array, 176 ARRAY_SIZE(phy_gbit_features_array), 177 phy_gbit_fibre_features); 178 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, phy_gbit_fibre_features); 179 180 /* 10/100 half/full + 1000 half/full + 10G full*/ 181 linkmode_set_bit_array(phy_all_ports_features_array, 182 ARRAY_SIZE(phy_all_ports_features_array), 183 phy_10gbit_features); 184 linkmode_set_bit_array(phy_10_100_features_array, 185 ARRAY_SIZE(phy_10_100_features_array), 186 phy_10gbit_features); 187 linkmode_set_bit_array(phy_gbit_features_array, 188 ARRAY_SIZE(phy_gbit_features_array), 189 phy_10gbit_features); 190 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 191 phy_10gbit_features); 192 193 linkmode_set_bit_array(phy_eee_cap1_features_array, 194 ARRAY_SIZE(phy_eee_cap1_features_array), 195 phy_eee_cap1_features); 196 linkmode_set_bit_array(phy_eee_cap2_features_array, 197 ARRAY_SIZE(phy_eee_cap2_features_array), 198 phy_eee_cap2_features); 199 200 } 201 202 void phy_device_free(struct phy_device *phydev) 203 { 204 put_device(&phydev->mdio.dev); 205 } 206 EXPORT_SYMBOL(phy_device_free); 207 208 static void phy_mdio_device_free(struct mdio_device *mdiodev) 209 { 210 struct phy_device *phydev; 211 212 phydev = container_of(mdiodev, struct phy_device, mdio); 213 phy_device_free(phydev); 214 } 215 216 static void phy_device_release(struct device *dev) 217 { 218 fwnode_handle_put(dev->fwnode); 219 kfree(to_phy_device(dev)); 220 } 221 222 static void phy_mdio_device_remove(struct mdio_device *mdiodev) 223 { 224 struct phy_device *phydev; 225 226 phydev = container_of(mdiodev, struct phy_device, mdio); 227 phy_device_remove(phydev); 228 } 229 230 static struct phy_driver genphy_driver; 231 232 static bool phy_drv_wol_enabled(struct phy_device *phydev) 233 { 234 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; 235 236 phy_ethtool_get_wol(phydev, &wol); 237 238 return wol.wolopts != 0; 239 } 240 241 bool phy_may_wakeup(struct phy_device *phydev) 242 { 243 /* If the PHY is using driver-model based wakeup, use that state. */ 244 if (phy_can_wakeup(phydev)) 245 return device_may_wakeup(&phydev->mdio.dev); 246 247 return phy_drv_wol_enabled(phydev); 248 } 249 EXPORT_SYMBOL_GPL(phy_may_wakeup); 250 251 static void phy_link_change(struct phy_device *phydev, bool up) 252 { 253 struct net_device *netdev = phydev->attached_dev; 254 255 if (up) 256 netif_carrier_on(netdev); 257 else 258 netif_carrier_off(netdev); 259 phydev->adjust_link(netdev); 260 if (phydev->mii_ts && phydev->mii_ts->link_state) 261 phydev->mii_ts->link_state(phydev->mii_ts, phydev); 262 } 263 264 /** 265 * phy_uses_state_machine - test whether consumer driver uses PAL state machine 266 * @phydev: the target PHY device structure 267 * 268 * Ultimately, this aims to indirectly determine whether the PHY is attached 269 * to a consumer which uses the state machine by calling phy_start() and 270 * phy_stop(). 271 * 272 * When the PHY driver consumer uses phylib, it must have previously called 273 * phy_connect_direct() or one of its derivatives, so that phy_prepare_link() 274 * has set up a hook for monitoring state changes. 275 * 276 * When the PHY driver is used by the MAC driver consumer through phylink (the 277 * only other provider of a phy_link_change() method), using the PHY state 278 * machine is not optional. 279 * 280 * Return: true if consumer calls phy_start() and phy_stop(), false otherwise. 281 */ 282 static bool phy_uses_state_machine(struct phy_device *phydev) 283 { 284 if (phydev->phy_link_change == phy_link_change) 285 return phydev->attached_dev && phydev->adjust_link; 286 287 return !!phydev->phy_link_change; 288 } 289 290 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) 291 { 292 struct device_driver *drv = phydev->mdio.dev.driver; 293 struct phy_driver *phydrv = to_phy_driver(drv); 294 struct net_device *netdev = phydev->attached_dev; 295 296 if (!drv || !phydrv->suspend) 297 return false; 298 299 /* If the PHY on the mido bus is not attached but has WOL enabled 300 * we cannot suspend the PHY. 301 */ 302 if (!netdev && phy_may_wakeup(phydev)) 303 return false; 304 305 /* PHY not attached? May suspend if the PHY has not already been 306 * suspended as part of a prior call to phy_disconnect() -> 307 * phy_detach() -> phy_suspend() because the parent netdev might be the 308 * MDIO bus driver and clock gated at this point. 309 */ 310 if (!netdev) 311 goto out; 312 313 if (netdev->ethtool->wol_enabled) 314 return false; 315 316 /* As long as not all affected network drivers support the 317 * wol_enabled flag, let's check for hints that WoL is enabled. 318 * Don't suspend PHY if the attached netdev parent may wake up. 319 * The parent may point to a PCI device, as in tg3 driver. 320 */ 321 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) 322 return false; 323 324 /* Also don't suspend PHY if the netdev itself may wakeup. This 325 * is the case for devices w/o underlaying pwr. mgmt. aware bus, 326 * e.g. SoC devices. 327 */ 328 if (device_may_wakeup(&netdev->dev)) 329 return false; 330 331 out: 332 return !phydev->suspended; 333 } 334 335 static __maybe_unused int mdio_bus_phy_suspend(struct device *dev) 336 { 337 struct phy_device *phydev = to_phy_device(dev); 338 339 if (phydev->mac_managed_pm) 340 return 0; 341 342 /* Wakeup interrupts may occur during the system sleep transition when 343 * the PHY is inaccessible. Set flag to postpone handling until the PHY 344 * has resumed. Wait for concurrent interrupt handler to complete. 345 */ 346 if (phy_interrupt_is_valid(phydev)) { 347 phydev->irq_suspended = 1; 348 synchronize_irq(phydev->irq); 349 } 350 351 /* We must stop the state machine manually, otherwise it stops out of 352 * control, possibly with the phydev->lock held. Upon resume, netdev 353 * may call phy routines that try to grab the same lock, and that may 354 * lead to a deadlock. 355 */ 356 if (phy_uses_state_machine(phydev)) 357 phy_stop_machine(phydev); 358 359 if (!mdio_bus_phy_may_suspend(phydev)) 360 return 0; 361 362 phydev->suspended_by_mdio_bus = 1; 363 364 return phy_suspend(phydev); 365 } 366 367 static __maybe_unused int mdio_bus_phy_resume(struct device *dev) 368 { 369 struct phy_device *phydev = to_phy_device(dev); 370 int ret; 371 372 if (phydev->mac_managed_pm) 373 return 0; 374 375 if (!phydev->suspended_by_mdio_bus) 376 goto no_resume; 377 378 phydev->suspended_by_mdio_bus = 0; 379 380 /* If we managed to get here with the PHY state machine in a state 381 * neither PHY_HALTED, PHY_READY nor PHY_UP, this is an indication 382 * that something went wrong and we should most likely be using 383 * MAC managed PM, but we are not. 384 */ 385 WARN_ON(phydev->state != PHY_HALTED && phydev->state != PHY_READY && 386 phydev->state != PHY_UP); 387 388 ret = phy_init_hw(phydev); 389 if (ret < 0) 390 return ret; 391 392 ret = phy_resume(phydev); 393 if (ret < 0) 394 return ret; 395 no_resume: 396 if (phy_interrupt_is_valid(phydev)) { 397 phydev->irq_suspended = 0; 398 synchronize_irq(phydev->irq); 399 400 /* Rerun interrupts which were postponed by phy_interrupt() 401 * because they occurred during the system sleep transition. 402 */ 403 if (phydev->irq_rerun) { 404 phydev->irq_rerun = 0; 405 enable_irq(phydev->irq); 406 irq_wake_thread(phydev->irq, phydev); 407 } 408 } 409 410 if (phy_uses_state_machine(phydev)) 411 phy_start_machine(phydev); 412 413 return 0; 414 } 415 416 static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend, 417 mdio_bus_phy_resume); 418 419 /** 420 * genphy_match_phy_device - match a PHY device with a PHY driver 421 * @phydev: target phy_device struct 422 * @phydrv: target phy_driver struct 423 * 424 * Description: Checks whether the given PHY device matches the specified 425 * PHY driver. For Clause 45 PHYs, iterates over the available device 426 * identifiers and compares them against the driver's expected PHY ID, 427 * applying the provided mask. For Clause 22 PHYs, a direct ID comparison 428 * is performed. 429 * 430 * Return: 1 if the PHY device matches the driver, 0 otherwise. 431 */ 432 int genphy_match_phy_device(struct phy_device *phydev, 433 const struct phy_driver *phydrv) 434 { 435 if (phydev->is_c45) { 436 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids); 437 int i; 438 439 for (i = 1; i < num_ids; i++) { 440 if (phydev->c45_ids.device_ids[i] == 0xffffffff) 441 continue; 442 443 if (phy_id_compare(phydev->c45_ids.device_ids[i], 444 phydrv->phy_id, phydrv->phy_id_mask)) 445 return 1; 446 } 447 448 return 0; 449 } 450 451 return phy_id_compare(phydev->phy_id, phydrv->phy_id, 452 phydrv->phy_id_mask); 453 } 454 EXPORT_SYMBOL_GPL(genphy_match_phy_device); 455 456 static int phy_bus_match(struct device *dev, const struct device_driver *drv) 457 { 458 struct phy_device *phydev = to_phy_device(dev); 459 const struct phy_driver *phydrv = to_phy_driver(drv); 460 461 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY)) 462 return 0; 463 464 if (phydrv->match_phy_device) 465 return phydrv->match_phy_device(phydev, phydrv); 466 467 return genphy_match_phy_device(phydev, phydrv); 468 } 469 470 static ssize_t 471 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf) 472 { 473 struct phy_device *phydev = to_phy_device(dev); 474 475 return sysfs_emit(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id); 476 } 477 static DEVICE_ATTR_RO(phy_id); 478 479 static ssize_t 480 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf) 481 { 482 struct phy_device *phydev = to_phy_device(dev); 483 const char *mode = NULL; 484 485 if (phydev->is_internal) 486 mode = "internal"; 487 else 488 mode = phy_modes(phydev->interface); 489 490 return sysfs_emit(buf, "%s\n", mode); 491 } 492 static DEVICE_ATTR_RO(phy_interface); 493 494 static ssize_t 495 phy_has_fixups_show(struct device *dev, struct device_attribute *attr, 496 char *buf) 497 { 498 struct phy_device *phydev = to_phy_device(dev); 499 500 return sysfs_emit(buf, "%d\n", phydev->has_fixups); 501 } 502 static DEVICE_ATTR_RO(phy_has_fixups); 503 504 static ssize_t phy_dev_flags_show(struct device *dev, 505 struct device_attribute *attr, 506 char *buf) 507 { 508 struct phy_device *phydev = to_phy_device(dev); 509 510 return sysfs_emit(buf, "0x%08x\n", phydev->dev_flags); 511 } 512 static DEVICE_ATTR_RO(phy_dev_flags); 513 514 static struct attribute *phy_dev_attrs[] = { 515 &dev_attr_phy_id.attr, 516 &dev_attr_phy_interface.attr, 517 &dev_attr_phy_has_fixups.attr, 518 &dev_attr_phy_dev_flags.attr, 519 NULL, 520 }; 521 522 static const struct attribute_group phy_dev_group = { 523 .attrs = phy_dev_attrs, 524 }; 525 526 #define MMD_DEVICE_ID_ATTR(n) \ 527 static ssize_t mmd##n##_device_id_show(struct device *dev, \ 528 struct device_attribute *attr, char *buf) \ 529 { \ 530 struct phy_device *phydev = to_phy_device(dev); \ 531 return sysfs_emit(buf, "0x%.8lx\n", \ 532 (unsigned long)phydev->c45_ids.device_ids[n]); \ 533 } \ 534 static DEVICE_ATTR_RO(mmd##n##_device_id) 535 536 MMD_DEVICE_ID_ATTR(1); 537 MMD_DEVICE_ID_ATTR(2); 538 MMD_DEVICE_ID_ATTR(3); 539 MMD_DEVICE_ID_ATTR(4); 540 MMD_DEVICE_ID_ATTR(5); 541 MMD_DEVICE_ID_ATTR(6); 542 MMD_DEVICE_ID_ATTR(7); 543 MMD_DEVICE_ID_ATTR(8); 544 MMD_DEVICE_ID_ATTR(9); 545 MMD_DEVICE_ID_ATTR(10); 546 MMD_DEVICE_ID_ATTR(11); 547 MMD_DEVICE_ID_ATTR(12); 548 MMD_DEVICE_ID_ATTR(13); 549 MMD_DEVICE_ID_ATTR(14); 550 MMD_DEVICE_ID_ATTR(15); 551 MMD_DEVICE_ID_ATTR(16); 552 MMD_DEVICE_ID_ATTR(17); 553 MMD_DEVICE_ID_ATTR(18); 554 MMD_DEVICE_ID_ATTR(19); 555 MMD_DEVICE_ID_ATTR(20); 556 MMD_DEVICE_ID_ATTR(21); 557 MMD_DEVICE_ID_ATTR(22); 558 MMD_DEVICE_ID_ATTR(23); 559 MMD_DEVICE_ID_ATTR(24); 560 MMD_DEVICE_ID_ATTR(25); 561 MMD_DEVICE_ID_ATTR(26); 562 MMD_DEVICE_ID_ATTR(27); 563 MMD_DEVICE_ID_ATTR(28); 564 MMD_DEVICE_ID_ATTR(29); 565 MMD_DEVICE_ID_ATTR(30); 566 MMD_DEVICE_ID_ATTR(31); 567 568 static struct attribute *phy_mmd_attrs[] = { 569 &dev_attr_mmd1_device_id.attr, 570 &dev_attr_mmd2_device_id.attr, 571 &dev_attr_mmd3_device_id.attr, 572 &dev_attr_mmd4_device_id.attr, 573 &dev_attr_mmd5_device_id.attr, 574 &dev_attr_mmd6_device_id.attr, 575 &dev_attr_mmd7_device_id.attr, 576 &dev_attr_mmd8_device_id.attr, 577 &dev_attr_mmd9_device_id.attr, 578 &dev_attr_mmd10_device_id.attr, 579 &dev_attr_mmd11_device_id.attr, 580 &dev_attr_mmd12_device_id.attr, 581 &dev_attr_mmd13_device_id.attr, 582 &dev_attr_mmd14_device_id.attr, 583 &dev_attr_mmd15_device_id.attr, 584 &dev_attr_mmd16_device_id.attr, 585 &dev_attr_mmd17_device_id.attr, 586 &dev_attr_mmd18_device_id.attr, 587 &dev_attr_mmd19_device_id.attr, 588 &dev_attr_mmd20_device_id.attr, 589 &dev_attr_mmd21_device_id.attr, 590 &dev_attr_mmd22_device_id.attr, 591 &dev_attr_mmd23_device_id.attr, 592 &dev_attr_mmd24_device_id.attr, 593 &dev_attr_mmd25_device_id.attr, 594 &dev_attr_mmd26_device_id.attr, 595 &dev_attr_mmd27_device_id.attr, 596 &dev_attr_mmd28_device_id.attr, 597 &dev_attr_mmd29_device_id.attr, 598 &dev_attr_mmd30_device_id.attr, 599 &dev_attr_mmd31_device_id.attr, 600 NULL 601 }; 602 603 static umode_t phy_mmd_is_visible(struct kobject *kobj, 604 struct attribute *attr, int index) 605 { 606 struct device *dev = kobj_to_dev(kobj); 607 struct phy_device *phydev = to_phy_device(dev); 608 const int i = index + 1; 609 610 if (!phydev->is_c45) 611 return 0; 612 if (i >= ARRAY_SIZE(phydev->c45_ids.device_ids) || 613 phydev->c45_ids.device_ids[i] == 0xffffffff) 614 return 0; 615 616 return attr->mode; 617 } 618 619 static const struct attribute_group phy_mmd_group = { 620 .name = "c45_phy_ids", 621 .attrs = phy_mmd_attrs, 622 .is_visible = phy_mmd_is_visible, 623 }; 624 625 static const struct attribute_group *phy_device_groups[] = { 626 &phy_dev_group, 627 &phy_mmd_group, 628 NULL, 629 }; 630 631 static const struct device_type mdio_bus_phy_type = { 632 .name = "PHY", 633 .groups = phy_device_groups, 634 .release = phy_device_release, 635 .pm = pm_ptr(&mdio_bus_phy_pm_ops), 636 }; 637 638 static int phy_request_driver_module(struct phy_device *dev, u32 phy_id) 639 { 640 int ret; 641 642 ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, 643 MDIO_ID_ARGS(phy_id)); 644 /* We only check for failures in executing the usermode binary, 645 * not whether a PHY driver module exists for the PHY ID. 646 * Accept -ENOENT because this may occur in case no initramfs exists, 647 * then modprobe isn't available. 648 */ 649 if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) { 650 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n", 651 ret, (unsigned long)phy_id); 652 return ret; 653 } 654 655 return 0; 656 } 657 658 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id, 659 bool is_c45, 660 struct phy_c45_device_ids *c45_ids) 661 { 662 struct phy_device *dev; 663 struct mdio_device *mdiodev; 664 int ret = 0; 665 666 /* We allocate the device, and initialize the default values */ 667 dev = kzalloc_obj(*dev); 668 if (!dev) 669 return ERR_PTR(-ENOMEM); 670 671 mdiodev = &dev->mdio; 672 mdiodev->dev.parent = &bus->dev; 673 mdiodev->dev.bus = &mdio_bus_type; 674 mdiodev->dev.type = &mdio_bus_phy_type; 675 mdiodev->bus = bus; 676 mdiodev->bus_match = phy_bus_match; 677 mdiodev->addr = addr; 678 mdiodev->flags = MDIO_DEVICE_FLAG_PHY; 679 mdiodev->device_free = phy_mdio_device_free; 680 mdiodev->device_remove = phy_mdio_device_remove; 681 mdiodev->reset_state = -1; 682 683 dev->speed = SPEED_UNKNOWN; 684 dev->duplex = DUPLEX_UNKNOWN; 685 dev->pause = false; 686 dev->asym_pause = false; 687 dev->link = 0; 688 dev->port = PORT_TP; 689 dev->interface = PHY_INTERFACE_MODE_GMII; 690 691 dev->autoneg = AUTONEG_ENABLE; 692 693 dev->pma_extable = -ENODATA; 694 dev->is_c45 = is_c45; 695 dev->phy_id = phy_id; 696 if (c45_ids) 697 dev->c45_ids = *c45_ids; 698 dev->irq = bus->irq[addr]; 699 700 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr); 701 device_initialize(&mdiodev->dev); 702 703 dev->state = PHY_DOWN; 704 INIT_LIST_HEAD(&dev->leds); 705 INIT_LIST_HEAD(&dev->ports); 706 707 /* The driver's probe function must change that to the real number 708 * of ports possible on the PHY. We assume by default we are dealing 709 * with a single-port PHY 710 */ 711 dev->max_n_ports = 1; 712 713 mutex_init(&dev->lock); 714 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine); 715 716 /* Request the appropriate module unconditionally; don't 717 * bother trying to do so only if it isn't already loaded, 718 * because that gets complicated. A hotplug event would have 719 * done an unconditional modprobe anyway. 720 * We don't do normal hotplug because it won't work for MDIO 721 * -- because it relies on the device staying around for long 722 * enough for the driver to get loaded. With MDIO, the NIC 723 * driver will get bored and give up as soon as it finds that 724 * there's no driver _already_ loaded. 725 */ 726 if (is_c45 && c45_ids) { 727 const int num_ids = ARRAY_SIZE(c45_ids->device_ids); 728 int i; 729 730 for (i = 1; i < num_ids; i++) { 731 if (c45_ids->device_ids[i] == 0xffffffff) 732 continue; 733 734 ret = phy_request_driver_module(dev, 735 c45_ids->device_ids[i]); 736 if (ret) 737 break; 738 } 739 } else { 740 ret = phy_request_driver_module(dev, phy_id); 741 } 742 743 if (ret) { 744 put_device(&mdiodev->dev); 745 dev = ERR_PTR(ret); 746 } 747 748 return dev; 749 } 750 EXPORT_SYMBOL(phy_device_create); 751 752 /* phy_c45_probe_present - checks to see if a MMD is present in the package 753 * @bus: the target MII bus 754 * @prtad: PHY package address on the MII bus 755 * @devad: PHY device (MMD) address 756 * 757 * Read the MDIO_STAT2 register, and check whether a device is responding 758 * at this address. 759 * 760 * Returns: negative error number on bus access error, zero if no device 761 * is responding, or positive if a device is present. 762 */ 763 static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad) 764 { 765 int stat2; 766 767 stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2); 768 if (stat2 < 0) 769 return stat2; 770 771 return (stat2 & MDIO_STAT2_DEVPRST) == MDIO_STAT2_DEVPRST_VAL; 772 } 773 774 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers. 775 * @bus: the target MII bus 776 * @addr: PHY address on the MII bus 777 * @dev_addr: MMD address in the PHY. 778 * @devices_in_package: where to store the devices in package information. 779 * 780 * Description: reads devices in package registers of a MMD at @dev_addr 781 * from PHY at @addr on @bus. 782 * 783 * Returns: 0 on success, -EIO on failure. 784 */ 785 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr, 786 u32 *devices_in_package) 787 { 788 int phy_reg; 789 790 phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2); 791 if (phy_reg < 0) 792 return -EIO; 793 *devices_in_package = phy_reg << 16; 794 795 phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1); 796 if (phy_reg < 0) 797 return -EIO; 798 *devices_in_package |= phy_reg; 799 800 return 0; 801 } 802 803 /** 804 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs. 805 * @bus: the target MII bus 806 * @addr: PHY address on the MII bus 807 * @c45_ids: where to store the c45 ID information. 808 * 809 * Read the PHY "devices in package". If this appears to be valid, read 810 * the PHY identifiers for each device. Return the "devices in package" 811 * and identifiers in @c45_ids. 812 * 813 * Returns zero on success, %-EIO on bus access error, or %-ENODEV if 814 * the "devices in package" is invalid or no device responds. 815 */ 816 static int get_phy_c45_ids(struct mii_bus *bus, int addr, 817 struct phy_c45_device_ids *c45_ids) 818 { 819 const int num_ids = ARRAY_SIZE(c45_ids->device_ids); 820 u32 devs_in_pkg = 0; 821 int i, ret, phy_reg; 822 823 /* Find first non-zero Devices In package. Device zero is reserved 824 * for 802.3 c45 complied PHYs, so don't probe it at first. 825 */ 826 for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 || 827 (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) { 828 if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) { 829 /* Check that there is a device present at this 830 * address before reading the devices-in-package 831 * register to avoid reading garbage from the PHY. 832 * Some PHYs (88x3310) vendor space is not IEEE802.3 833 * compliant. 834 */ 835 ret = phy_c45_probe_present(bus, addr, i); 836 if (ret < 0) 837 /* returning -ENODEV doesn't stop bus 838 * scanning 839 */ 840 return (ret == -EIO || 841 ret == -ENODEV) ? -ENODEV : -EIO; 842 843 if (!ret) 844 continue; 845 } 846 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg); 847 if (phy_reg < 0) 848 return -EIO; 849 } 850 851 if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) { 852 /* If mostly Fs, there is no device there, then let's probe 853 * MMD 0, as some 10G PHYs have zero Devices In package, 854 * e.g. Cortina CS4315/CS4340 PHY. 855 */ 856 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg); 857 if (phy_reg < 0) 858 return -EIO; 859 860 /* no device there, let's get out of here */ 861 if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) 862 return -ENODEV; 863 } 864 865 /* Now probe Device Identifiers for each device present. */ 866 for (i = 1; i < num_ids; i++) { 867 if (!(devs_in_pkg & (1 << i))) 868 continue; 869 870 if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) { 871 /* Probe the "Device Present" bits for the vendor MMDs 872 * to ignore these if they do not contain IEEE 802.3 873 * registers. 874 */ 875 ret = phy_c45_probe_present(bus, addr, i); 876 if (ret < 0) 877 return ret; 878 879 if (!ret) 880 continue; 881 } 882 883 phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1); 884 if (phy_reg < 0) 885 return -EIO; 886 c45_ids->device_ids[i] = phy_reg << 16; 887 888 phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2); 889 if (phy_reg < 0) 890 return -EIO; 891 c45_ids->device_ids[i] |= phy_reg; 892 } 893 894 c45_ids->devices_in_package = devs_in_pkg; 895 /* Bit 0 doesn't represent a device, it indicates c22 regs presence */ 896 c45_ids->mmds_present = devs_in_pkg & ~BIT(0); 897 898 return 0; 899 } 900 901 /** 902 * get_phy_c22_id - reads the specified addr for its clause 22 ID. 903 * @bus: the target MII bus 904 * @addr: PHY address on the MII bus 905 * @phy_id: where to store the ID retrieved. 906 * 907 * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus, 908 * placing it in @phy_id. Return zero on successful read and the ID is 909 * valid, %-EIO on bus access error, or %-ENODEV if no device responds 910 * or invalid ID. 911 */ 912 static int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id) 913 { 914 int phy_reg; 915 916 /* Grab the bits from PHYIR1, and put them in the upper half */ 917 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1); 918 if (phy_reg < 0) { 919 /* returning -ENODEV doesn't stop bus scanning */ 920 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO; 921 } 922 923 *phy_id = phy_reg << 16; 924 925 /* Grab the bits from PHYIR2, and put them in the lower half */ 926 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2); 927 if (phy_reg < 0) { 928 /* returning -ENODEV doesn't stop bus scanning */ 929 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO; 930 } 931 932 *phy_id |= phy_reg; 933 934 /* If the phy_id is mostly Fs, there is no device there */ 935 if ((*phy_id & 0x1fffffff) == 0x1fffffff) 936 return -ENODEV; 937 938 return 0; 939 } 940 941 /* Extract the phy ID from the compatible string of the form 942 * ethernet-phy-idAAAA.BBBB. 943 */ 944 int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id) 945 { 946 unsigned int upper, lower; 947 const char *cp; 948 int ret; 949 950 ret = fwnode_property_read_string(fwnode, "compatible", &cp); 951 if (ret) 952 return ret; 953 954 if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) != 2) 955 return -EINVAL; 956 957 *phy_id = ((upper & GENMASK(15, 0)) << 16) | (lower & GENMASK(15, 0)); 958 return 0; 959 } 960 EXPORT_SYMBOL(fwnode_get_phy_id); 961 962 /** 963 * get_phy_device - reads the specified PHY device and returns its @phy_device 964 * struct 965 * @bus: the target MII bus 966 * @addr: PHY address on the MII bus 967 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol 968 * 969 * Probe for a PHY at @addr on @bus. 970 * 971 * When probing for a clause 22 PHY, then read the ID registers. If we find 972 * a valid ID, allocate and return a &struct phy_device. 973 * 974 * When probing for a clause 45 PHY, read the "devices in package" registers. 975 * If the "devices in package" appears valid, read the ID registers for each 976 * MMD, allocate and return a &struct phy_device. 977 * 978 * Returns an allocated &struct phy_device on success, %-ENODEV if there is 979 * no PHY present, or %-EIO on bus access error. 980 */ 981 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45) 982 { 983 struct phy_c45_device_ids c45_ids; 984 u32 phy_id = 0; 985 int r; 986 987 c45_ids.devices_in_package = 0; 988 c45_ids.mmds_present = 0; 989 memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids)); 990 991 if (is_c45) 992 r = get_phy_c45_ids(bus, addr, &c45_ids); 993 else 994 r = get_phy_c22_id(bus, addr, &phy_id); 995 996 if (r) 997 return ERR_PTR(r); 998 999 /* PHY device such as the Marvell Alaska 88E2110 will return a PHY ID 1000 * of 0 when probed using get_phy_c22_id() with no error. Proceed to 1001 * probe with C45 to see if we're able to get a valid PHY ID in the C45 1002 * space, if successful, create the C45 PHY device. 1003 */ 1004 if (!is_c45 && phy_id == 0 && bus->read_c45) { 1005 r = get_phy_c45_ids(bus, addr, &c45_ids); 1006 if (!r) 1007 return phy_device_create(bus, addr, phy_id, 1008 true, &c45_ids); 1009 } 1010 1011 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids); 1012 } 1013 EXPORT_SYMBOL(get_phy_device); 1014 1015 /** 1016 * phy_device_register - Register the phy device on the MDIO bus 1017 * @phydev: phy_device structure to be added to the MDIO bus 1018 */ 1019 int phy_device_register(struct phy_device *phydev) 1020 { 1021 int err; 1022 1023 err = mdiobus_register_device(&phydev->mdio); 1024 if (err) 1025 return err; 1026 1027 /* Deassert the reset signal */ 1028 phy_device_reset(phydev, 0); 1029 1030 /* Run all of the fixups for this PHY */ 1031 err = phy_scan_fixups(phydev); 1032 if (err) { 1033 phydev_err(phydev, "failed to initialize\n"); 1034 goto out; 1035 } 1036 1037 err = device_add(&phydev->mdio.dev); 1038 if (err) { 1039 phydev_err(phydev, "failed to add\n"); 1040 goto out; 1041 } 1042 1043 return 0; 1044 1045 out: 1046 /* Assert the reset signal */ 1047 phy_device_reset(phydev, 1); 1048 1049 mdiobus_unregister_device(&phydev->mdio); 1050 return err; 1051 } 1052 EXPORT_SYMBOL(phy_device_register); 1053 1054 /** 1055 * phy_device_remove - Remove a previously registered phy device from the MDIO bus 1056 * @phydev: phy_device structure to remove 1057 * 1058 * This doesn't free the phy_device itself, it merely reverses the effects 1059 * of phy_device_register(). Use phy_device_free() to free the device 1060 * after calling this function. 1061 */ 1062 void phy_device_remove(struct phy_device *phydev) 1063 { 1064 unregister_mii_timestamper(phydev->mii_ts); 1065 pse_control_put(phydev->psec); 1066 1067 device_del(&phydev->mdio.dev); 1068 1069 /* Assert the reset signal */ 1070 phy_device_reset(phydev, 1); 1071 1072 mdiobus_unregister_device(&phydev->mdio); 1073 } 1074 EXPORT_SYMBOL(phy_device_remove); 1075 1076 /** 1077 * phy_get_c45_ids - Read 802.3-c45 IDs for phy device. 1078 * @phydev: phy_device structure to read 802.3-c45 IDs 1079 * 1080 * Returns zero on success, %-EIO on bus access error, or %-ENODEV if 1081 * the "devices in package" is invalid. 1082 */ 1083 int phy_get_c45_ids(struct phy_device *phydev) 1084 { 1085 return get_phy_c45_ids(phydev->mdio.bus, phydev->mdio.addr, 1086 &phydev->c45_ids); 1087 } 1088 EXPORT_SYMBOL(phy_get_c45_ids); 1089 1090 /** 1091 * phy_find_next - finds the next PHY device on the bus 1092 * @bus: the target MII bus 1093 * @pos: cursor 1094 * 1095 * Return: next phy_device on the bus, or NULL 1096 */ 1097 struct phy_device *phy_find_next(struct mii_bus *bus, struct phy_device *pos) 1098 { 1099 for (int addr = pos ? pos->mdio.addr + 1 : 0; 1100 addr < PHY_MAX_ADDR; addr++) { 1101 struct phy_device *phydev = mdiobus_get_phy(bus, addr); 1102 1103 if (phydev) 1104 return phydev; 1105 } 1106 return NULL; 1107 } 1108 EXPORT_SYMBOL_GPL(phy_find_next); 1109 1110 /** 1111 * phy_prepare_link - prepares the PHY layer to monitor link status 1112 * @phydev: target phy_device struct 1113 * @handler: callback function for link status change notifications 1114 * 1115 * Description: Tells the PHY infrastructure to handle the 1116 * gory details on monitoring link status (whether through 1117 * polling or an interrupt), and to call back to the 1118 * connected device driver when the link status changes. 1119 * If you want to monitor your own link state, don't call 1120 * this function. 1121 */ 1122 static void phy_prepare_link(struct phy_device *phydev, 1123 void (*handler)(struct net_device *)) 1124 { 1125 phydev->adjust_link = handler; 1126 } 1127 1128 /** 1129 * phy_connect_direct - connect an ethernet device to a specific phy_device 1130 * @dev: the network device to connect 1131 * @phydev: the pointer to the phy device 1132 * @handler: callback function for state change notifications 1133 * @interface: PHY device's interface 1134 */ 1135 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, 1136 void (*handler)(struct net_device *), 1137 phy_interface_t interface) 1138 { 1139 int rc; 1140 1141 if (!dev) 1142 return -EINVAL; 1143 1144 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); 1145 if (rc) 1146 return rc; 1147 1148 phy_prepare_link(phydev, handler); 1149 if (phy_interrupt_is_valid(phydev)) 1150 phy_request_interrupt(phydev); 1151 1152 return 0; 1153 } 1154 EXPORT_SYMBOL(phy_connect_direct); 1155 1156 /** 1157 * phy_connect - connect an ethernet device to a PHY device 1158 * @dev: the network device to connect 1159 * @bus_id: the id string of the PHY device to connect 1160 * @handler: callback function for state change notifications 1161 * @interface: PHY device's interface 1162 * 1163 * Description: Convenience function for connecting ethernet 1164 * devices to PHY devices. The default behavior is for 1165 * the PHY infrastructure to handle everything, and only notify 1166 * the connected driver when the link status changes. If you 1167 * don't want, or can't use the provided functionality, you may 1168 * choose to call only the subset of functions which provide 1169 * the desired functionality. 1170 */ 1171 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id, 1172 void (*handler)(struct net_device *), 1173 phy_interface_t interface) 1174 { 1175 struct phy_device *phydev; 1176 struct device *d; 1177 int rc; 1178 1179 /* Search the list of PHY devices on the mdio bus for the 1180 * PHY with the requested name 1181 */ 1182 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); 1183 if (!d) { 1184 pr_err("PHY %s not found\n", bus_id); 1185 return ERR_PTR(-ENODEV); 1186 } 1187 phydev = to_phy_device(d); 1188 1189 rc = phy_connect_direct(dev, phydev, handler, interface); 1190 put_device(d); 1191 if (rc) 1192 return ERR_PTR(rc); 1193 1194 return phydev; 1195 } 1196 EXPORT_SYMBOL(phy_connect); 1197 1198 /** 1199 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY 1200 * device 1201 * @phydev: target phy_device struct 1202 */ 1203 void phy_disconnect(struct phy_device *phydev) 1204 { 1205 if (phy_is_started(phydev)) 1206 phy_stop(phydev); 1207 1208 if (phy_interrupt_is_valid(phydev)) 1209 phy_free_interrupt(phydev); 1210 1211 phydev->adjust_link = NULL; 1212 1213 phy_detach(phydev); 1214 } 1215 EXPORT_SYMBOL(phy_disconnect); 1216 1217 /** 1218 * phy_poll_reset - Safely wait until a PHY reset has properly completed 1219 * @phydev: The PHY device to poll 1220 * 1221 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as 1222 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR 1223 * register must be polled until the BMCR_RESET bit clears. 1224 * 1225 * Furthermore, any attempts to write to PHY registers may have no effect 1226 * or even generate MDIO bus errors until this is complete. 1227 * 1228 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the 1229 * standard and do not fully reset after the BMCR_RESET bit is set, and may 1230 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an 1231 * effort to support such broken PHYs, this function is separate from the 1232 * standard phy_init_hw() which will zero all the other bits in the BMCR 1233 * and reapply all driver-specific and board-specific fixups. 1234 */ 1235 static int phy_poll_reset(struct phy_device *phydev) 1236 { 1237 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ 1238 int ret, val; 1239 1240 ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET), 1241 50000, 600000, true); 1242 if (ret) 1243 return ret; 1244 /* Some chips (smsc911x) may still need up to another 1ms after the 1245 * BMCR_RESET bit is cleared before they are usable. 1246 */ 1247 msleep(1); 1248 return 0; 1249 } 1250 1251 int phy_init_hw(struct phy_device *phydev) 1252 { 1253 int ret = 0; 1254 1255 /* Deassert the reset signal */ 1256 phy_device_reset(phydev, 0); 1257 1258 if (!phydev->drv) 1259 return 0; 1260 1261 if (phydev->drv->soft_reset) { 1262 ret = phydev->drv->soft_reset(phydev); 1263 if (ret < 0) 1264 return ret; 1265 1266 /* see comment in genphy_soft_reset for an explanation */ 1267 phydev->suspended = 0; 1268 } 1269 1270 ret = phy_scan_fixups(phydev); 1271 if (ret < 0) 1272 return ret; 1273 1274 phy_interface_zero(phydev->possible_interfaces); 1275 1276 if (phydev->drv->config_init) { 1277 ret = phydev->drv->config_init(phydev); 1278 if (ret < 0) 1279 return ret; 1280 } 1281 1282 if (phydev->drv->config_intr) { 1283 ret = phydev->drv->config_intr(phydev); 1284 if (ret < 0) 1285 return ret; 1286 } 1287 1288 /* Re-apply autonomous EEE disable after soft reset */ 1289 if (phydev->autonomous_eee_disabled && 1290 phydev->drv->disable_autonomous_eee) { 1291 ret = phydev->drv->disable_autonomous_eee(phydev); 1292 if (ret) 1293 return ret; 1294 } 1295 1296 return 0; 1297 } 1298 EXPORT_SYMBOL(phy_init_hw); 1299 1300 void phy_attached_info(struct phy_device *phydev) 1301 { 1302 phy_attached_print(phydev, NULL); 1303 } 1304 EXPORT_SYMBOL(phy_attached_info); 1305 1306 #define ATTACHED_FMT "attached PHY driver %s(mii_bus:phy_addr=%s, irq=%s)" 1307 char *phy_attached_info_irq(struct phy_device *phydev) 1308 { 1309 char *irq_str; 1310 char irq_num[8]; 1311 1312 switch(phydev->irq) { 1313 case PHY_POLL: 1314 irq_str = "POLL"; 1315 break; 1316 case PHY_MAC_INTERRUPT: 1317 irq_str = "MAC"; 1318 break; 1319 default: 1320 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq); 1321 irq_str = irq_num; 1322 break; 1323 } 1324 1325 return kasprintf(GFP_KERNEL, "%s", irq_str); 1326 } 1327 EXPORT_SYMBOL(phy_attached_info_irq); 1328 1329 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...) 1330 { 1331 const char *unbound = phydev->drv ? "" : "[unbound] "; 1332 char *irq_str = phy_attached_info_irq(phydev); 1333 1334 if (!fmt) { 1335 phydev_info(phydev, ATTACHED_FMT "\n", unbound, 1336 phydev_name(phydev), irq_str); 1337 } else { 1338 va_list ap; 1339 1340 phydev_info(phydev, ATTACHED_FMT, unbound, 1341 phydev_name(phydev), irq_str); 1342 1343 va_start(ap, fmt); 1344 vprintk(fmt, ap); 1345 va_end(ap); 1346 } 1347 kfree(irq_str); 1348 } 1349 EXPORT_SYMBOL(phy_attached_print); 1350 1351 static void phy_sysfs_create_links(struct phy_device *phydev) 1352 { 1353 struct net_device *dev = phydev->attached_dev; 1354 int err; 1355 1356 if (!dev) 1357 return; 1358 1359 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj, 1360 "attached_dev"); 1361 if (err) 1362 return; 1363 1364 err = sysfs_create_link_nowarn(&dev->dev.kobj, 1365 &phydev->mdio.dev.kobj, 1366 "phydev"); 1367 if (err) { 1368 dev_err(&dev->dev, "could not add device link to %s err %d\n", 1369 kobject_name(&phydev->mdio.dev.kobj), 1370 err); 1371 /* non-fatal - some net drivers can use one netdevice 1372 * with more then one phy 1373 */ 1374 } 1375 1376 phydev->sysfs_links = true; 1377 } 1378 1379 static ssize_t 1380 phy_standalone_show(struct device *dev, struct device_attribute *attr, 1381 char *buf) 1382 { 1383 struct phy_device *phydev = to_phy_device(dev); 1384 1385 return sysfs_emit(buf, "%d\n", !phydev->attached_dev); 1386 } 1387 static DEVICE_ATTR_RO(phy_standalone); 1388 1389 /** 1390 * phy_sfp_connect_phy - Connect the SFP module's PHY to the upstream PHY 1391 * @upstream: pointer to the upstream phy device 1392 * @phy: pointer to the SFP module's phy device 1393 * 1394 * This helper allows keeping track of PHY devices on the link. It adds the 1395 * SFP module's phy to the phy namespace of the upstream phy 1396 * 1397 * Return: 0 on success, otherwise a negative error code. 1398 */ 1399 static int phy_sfp_connect_phy(void *upstream, struct phy_device *phy) 1400 { 1401 struct phy_device *phydev = upstream; 1402 struct net_device *dev = phydev->attached_dev; 1403 1404 if (dev) 1405 return phy_link_topo_add_phy(dev, phy, PHY_UPSTREAM_PHY, phydev); 1406 1407 return 0; 1408 } 1409 1410 /** 1411 * phy_sfp_disconnect_phy - Disconnect the SFP module's PHY from the upstream PHY 1412 * @upstream: pointer to the upstream phy device 1413 * @phy: pointer to the SFP module's phy device 1414 * 1415 * This helper allows keeping track of PHY devices on the link. It removes the 1416 * SFP module's phy to the phy namespace of the upstream phy. As the module phy 1417 * will be destroyed, re-inserting the same module will add a new phy with a 1418 * new index. 1419 */ 1420 static void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy) 1421 { 1422 struct phy_device *phydev = upstream; 1423 struct net_device *dev = phydev->attached_dev; 1424 1425 if (dev) 1426 phy_link_topo_del_phy(dev, phy); 1427 } 1428 1429 /** 1430 * phy_sfp_attach - attach the SFP bus to the PHY upstream network device 1431 * @upstream: pointer to the phy device 1432 * @bus: sfp bus representing cage being attached 1433 * 1434 * This is used to fill in the sfp_upstream_ops .attach member. 1435 */ 1436 static void phy_sfp_attach(void *upstream, struct sfp_bus *bus) 1437 { 1438 struct phy_device *phydev = upstream; 1439 1440 if (phydev->attached_dev) 1441 phydev->attached_dev->sfp_bus = bus; 1442 phydev->sfp_bus_attached = true; 1443 } 1444 1445 /** 1446 * phy_sfp_detach - detach the SFP bus from the PHY upstream network device 1447 * @upstream: pointer to the phy device 1448 * @bus: sfp bus representing cage being attached 1449 * 1450 * This is used to fill in the sfp_upstream_ops .detach member. 1451 */ 1452 static void phy_sfp_detach(void *upstream, struct sfp_bus *bus) 1453 { 1454 struct phy_device *phydev = upstream; 1455 1456 if (phydev->attached_dev) 1457 phydev->attached_dev->sfp_bus = NULL; 1458 phydev->sfp_bus_attached = false; 1459 } 1460 1461 static int phy_sfp_module_insert(void *upstream, const struct sfp_eeprom_id *id) 1462 { 1463 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support); 1464 struct phy_device *phydev = upstream; 1465 const struct sfp_module_caps *caps; 1466 struct phy_port *port; 1467 1468 phy_interface_t iface; 1469 1470 linkmode_zero(sfp_support); 1471 1472 port = phy_get_sfp_port(phydev); 1473 if (!port) 1474 return -EINVAL; 1475 1476 caps = sfp_get_module_caps(phydev->sfp_bus); 1477 1478 linkmode_and(sfp_support, port->supported, caps->link_modes); 1479 if (linkmode_empty(sfp_support)) { 1480 dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n"); 1481 return -EINVAL; 1482 } 1483 1484 iface = sfp_select_interface(phydev->sfp_bus, sfp_support); 1485 if (iface == PHY_INTERFACE_MODE_NA) { 1486 dev_err(&phydev->mdio.dev, "PHY %s does not support the SFP module's requested MII interfaces\n", 1487 phydev_name(phydev)); 1488 return -EINVAL; 1489 } 1490 1491 if (phydev->n_ports == 1) 1492 phydev->port = caps->port; 1493 1494 if (port->ops && port->ops->configure_mii) 1495 return port->ops->configure_mii(port, true, iface); 1496 1497 return 0; 1498 } 1499 1500 static void phy_sfp_module_remove(void *upstream) 1501 { 1502 struct phy_device *phydev = upstream; 1503 struct phy_port *port = phy_get_sfp_port(phydev); 1504 1505 if (port && port->ops && port->ops->configure_mii) 1506 port->ops->configure_mii(port, false, PHY_INTERFACE_MODE_NA); 1507 1508 if (phydev->n_ports == 1) 1509 phydev->port = PORT_NONE; 1510 } 1511 1512 static void phy_sfp_link_up(void *upstream) 1513 { 1514 struct phy_device *phydev = upstream; 1515 struct phy_port *port = phy_get_sfp_port(phydev); 1516 1517 if (port && port->ops && port->ops->link_up) 1518 port->ops->link_up(port); 1519 } 1520 1521 static void phy_sfp_link_down(void *upstream) 1522 { 1523 struct phy_device *phydev = upstream; 1524 struct phy_port *port = phy_get_sfp_port(phydev); 1525 1526 if (port && port->ops && port->ops->link_down) 1527 port->ops->link_down(port); 1528 } 1529 1530 static const struct sfp_upstream_ops sfp_phydev_ops = { 1531 .attach = phy_sfp_attach, 1532 .detach = phy_sfp_detach, 1533 .module_insert = phy_sfp_module_insert, 1534 .module_remove = phy_sfp_module_remove, 1535 .link_up = phy_sfp_link_up, 1536 .link_down = phy_sfp_link_down, 1537 .connect_phy = phy_sfp_connect_phy, 1538 .disconnect_phy = phy_sfp_disconnect_phy, 1539 }; 1540 1541 static int phy_add_port(struct phy_device *phydev, struct phy_port *port) 1542 { 1543 int ret = 0; 1544 1545 if (phydev->n_ports == phydev->max_n_ports) 1546 return -EBUSY; 1547 1548 /* We set all ports as active by default, PHY drivers may deactivate 1549 * them (when unused) 1550 */ 1551 port->active = true; 1552 1553 if (port->is_mii) { 1554 if (phydev->drv && phydev->drv->attach_mii_port) 1555 ret = phydev->drv->attach_mii_port(phydev, port); 1556 } else { 1557 if (phydev->drv && phydev->drv->attach_mdi_port) 1558 ret = phydev->drv->attach_mdi_port(phydev, port); 1559 } 1560 1561 if (ret) 1562 return ret; 1563 1564 /* The PHY driver might have added, removed or set medium/pairs info, 1565 * so update the port supported accordingly. 1566 */ 1567 phy_port_update_supported(port); 1568 1569 list_add(&port->head, &phydev->ports); 1570 1571 phydev->n_ports++; 1572 1573 return 0; 1574 } 1575 1576 static void phy_del_port(struct phy_device *phydev, struct phy_port *port) 1577 { 1578 if (!phydev->n_ports) 1579 return; 1580 1581 list_del(&port->head); 1582 1583 phydev->n_ports--; 1584 } 1585 1586 static int phy_setup_sfp_port(struct phy_device *phydev) 1587 { 1588 struct phy_port *port = phy_port_alloc(); 1589 int ret; 1590 1591 if (!port) 1592 return -ENOMEM; 1593 1594 port->parent_type = PHY_PORT_PHY; 1595 port->phy = phydev; 1596 1597 /* The PHY is a media converter, the port connected to the SFP cage 1598 * is a MII port. 1599 */ 1600 port->is_mii = true; 1601 port->is_sfp = true; 1602 1603 /* The port->supported and port->interfaces list will be populated 1604 * when attaching the port to the phydev. 1605 */ 1606 ret = phy_add_port(phydev, port); 1607 if (ret) 1608 phy_port_destroy(port); 1609 1610 return ret; 1611 } 1612 1613 /** 1614 * phy_sfp_probe - probe for a SFP cage attached to this PHY device 1615 * @phydev: Pointer to phy_device 1616 */ 1617 static int phy_sfp_probe(struct phy_device *phydev) 1618 { 1619 struct sfp_bus *bus; 1620 int ret = 0; 1621 1622 if (phydev->mdio.dev.fwnode) { 1623 bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode); 1624 if (IS_ERR(bus)) 1625 return PTR_ERR(bus); 1626 1627 phydev->sfp_bus = bus; 1628 1629 ret = sfp_bus_add_upstream(bus, phydev, &sfp_phydev_ops); 1630 sfp_bus_put(bus); 1631 1632 if (ret) 1633 phydev->sfp_bus = NULL; 1634 } 1635 1636 if (!ret && phydev->sfp_bus) { 1637 ret = phy_setup_sfp_port(phydev); 1638 if (ret) { 1639 sfp_bus_del_upstream(phydev->sfp_bus); 1640 phydev->sfp_bus = NULL; 1641 } 1642 } 1643 1644 return ret; 1645 } 1646 1647 /** 1648 * phy_sfp_release - release resources set up by phy_sfp_probe() 1649 * @phydev: the PHY device 1650 * 1651 * Release the SFP resources set up by a successful phy_sfp_probe(). Unregister 1652 * the upstream before destroying its phy_port, so SFP upstream callbacks cannot 1653 * race with port destruction. 1654 */ 1655 static void phy_sfp_release(struct phy_device *phydev) 1656 { 1657 struct phy_port *port, *tmp; 1658 1659 sfp_bus_del_upstream(phydev->sfp_bus); 1660 phydev->sfp_bus = NULL; 1661 1662 list_for_each_entry_safe(port, tmp, &phydev->ports, head) { 1663 if (!port->is_sfp) 1664 continue; 1665 1666 phy_del_port(phydev, port); 1667 phy_port_destroy(port); 1668 } 1669 } 1670 1671 static bool phy_drv_supports_irq(const struct phy_driver *phydrv) 1672 { 1673 return phydrv->config_intr && phydrv->handle_interrupt; 1674 } 1675 1676 /** 1677 * phy_detach_internal - detach a PHY device from its network device 1678 * @phydev: target phy_device struct 1679 * @notify_bus: whether to notify the MDIO bus about the PHY detach 1680 * 1681 * This detaches the phy device from its network device and the phy 1682 * driver, and drops the reference count taken in phy_attach_direct(). 1683 */ 1684 static void phy_detach_internal(struct phy_device *phydev, bool notify_bus) 1685 { 1686 struct net_device *dev = phydev->attached_dev; 1687 struct module *ndev_owner = NULL; 1688 struct mii_bus *bus; 1689 1690 if (phydev->devlink) { 1691 device_link_del(phydev->devlink); 1692 phydev->devlink = NULL; 1693 } 1694 1695 if (phydev->sysfs_links) { 1696 if (dev) 1697 sysfs_remove_link(&dev->dev.kobj, "phydev"); 1698 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev"); 1699 } 1700 1701 if (!phydev->attached_dev) 1702 sysfs_remove_file(&phydev->mdio.dev.kobj, 1703 &dev_attr_phy_standalone.attr); 1704 1705 phy_suspend(phydev); 1706 1707 if (notify_bus && phydev->mdio.bus->notify_phy_detach) 1708 phydev->mdio.bus->notify_phy_detach(phydev); 1709 1710 if (dev) { 1711 struct hwtstamp_provider *hwprov; 1712 1713 /* hwprov may technically be protected by ops lock but 1714 * not for devices with a phydev, see phy_link_topo_add_phy() 1715 */ 1716 hwprov = rtnl_dereference(dev->hwprov); 1717 /* Disable timestamp if it is the one selected */ 1718 if (hwprov && hwprov->phydev == phydev) { 1719 rcu_assign_pointer(dev->hwprov, NULL); 1720 kfree_rcu(hwprov, rcu_head); 1721 } 1722 1723 phydev->attached_dev->phydev = NULL; 1724 phydev->attached_dev = NULL; 1725 phy_link_topo_del_phy(dev, phydev); 1726 } 1727 1728 phydev->phy_link_change = NULL; 1729 phydev->phylink = NULL; 1730 1731 if (phydev->mdio.dev.driver) 1732 module_put(phydev->mdio.dev.driver->owner); 1733 1734 /* If the device had no specific driver before (i.e. - it 1735 * was using the generic driver), we unbind the device 1736 * from the generic driver so that there's a chance a 1737 * real driver could be loaded 1738 */ 1739 if (phydev->is_genphy_driven) { 1740 /* The release below lets phy_probe() write this field. */ 1741 phydev->irq = phydev->mdio.bus->irq[phydev->mdio.addr]; 1742 device_release_driver(&phydev->mdio.dev); 1743 phydev->is_genphy_driven = 0; 1744 } 1745 1746 /* Assert the reset signal */ 1747 phy_device_reset(phydev, 1); 1748 1749 /* 1750 * The phydev might go away on the put_device() below, so avoid 1751 * a use-after-free bug by reading the underlying bus first. 1752 */ 1753 bus = phydev->mdio.bus; 1754 1755 put_device(&phydev->mdio.dev); 1756 if (dev) 1757 ndev_owner = dev->dev.parent->driver->owner; 1758 if (ndev_owner != bus->owner) 1759 module_put(bus->owner); 1760 } 1761 1762 /** 1763 * phy_detach - detach a PHY device from its network device 1764 * @phydev: target phy_device struct 1765 * 1766 * This detaches the phy device from its network device and the phy 1767 * driver, and drops the reference count taken in phy_attach_direct(). 1768 */ 1769 void phy_detach(struct phy_device *phydev) 1770 { 1771 /* cleanup including bus notification */ 1772 phy_detach_internal(phydev, true); 1773 } 1774 EXPORT_SYMBOL(phy_detach); 1775 1776 /** 1777 * phy_attach_direct - attach a network device to a given PHY device pointer 1778 * @dev: network device to attach 1779 * @phydev: Pointer to phy_device to attach 1780 * @flags: PHY device's dev_flags 1781 * @interface: PHY device's interface 1782 * 1783 * Description: Called by drivers to attach to a particular PHY 1784 * device. The phy_device is found, and properly hooked up 1785 * to the phy_driver. If no driver is attached, then a 1786 * generic driver is used. The phy_device is given a ptr to 1787 * the attaching device, and given a callback for link status 1788 * change. The phy_device is returned to the attaching driver. 1789 * This function takes a reference on the phy device. 1790 */ 1791 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, 1792 u32 flags, phy_interface_t interface) 1793 { 1794 struct mii_bus *bus = phydev->mdio.bus; 1795 struct device *d = &phydev->mdio.dev; 1796 struct module *ndev_owner = NULL; 1797 int irq = phydev->irq; 1798 int err; 1799 1800 /* For Ethernet device drivers that register their own MDIO bus, we 1801 * will have bus->owner match ndev_mod, so we do not want to increment 1802 * our own module->refcnt here, otherwise we would not be able to 1803 * unload later on. 1804 */ 1805 if (dev) 1806 ndev_owner = dev->dev.parent->driver->owner; 1807 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) { 1808 phydev_err(phydev, "failed to get the bus module\n"); 1809 return -EIO; 1810 } 1811 1812 get_device(d); 1813 1814 /* Assume that if there is no driver, that it doesn't 1815 * exist, and we should use the genphy driver. 1816 */ 1817 if (!d->driver) { 1818 if (phydev->is_c45) 1819 d->driver = &genphy_c45_driver.mdiodrv.driver; 1820 else 1821 d->driver = &genphy_driver.mdiodrv.driver; 1822 1823 phydev->is_genphy_driven = 1; 1824 } 1825 1826 if (!try_module_get(d->driver->owner)) { 1827 phydev_err(phydev, "failed to get the device driver module\n"); 1828 err = -EIO; 1829 goto error_put_device; 1830 } 1831 1832 if (phydev->is_genphy_driven) { 1833 err = d->driver->probe(d); 1834 if (err >= 0) 1835 err = device_bind_driver(d); 1836 1837 if (err) 1838 goto error_module_put; 1839 } 1840 1841 if (phydev->attached_dev) { 1842 dev_err(&dev->dev, "PHY already attached\n"); 1843 err = -EBUSY; 1844 goto error; 1845 } 1846 1847 phydev->phy_link_change = phy_link_change; 1848 if (dev) { 1849 phydev->attached_dev = dev; 1850 dev->phydev = phydev; 1851 1852 if (phydev->sfp_bus_attached) 1853 dev->sfp_bus = phydev->sfp_bus; 1854 1855 err = phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev); 1856 if (err) 1857 goto error; 1858 } 1859 1860 /* Some Ethernet drivers try to connect to a PHY device before 1861 * calling register_netdevice() -> netdev_register_kobject() and 1862 * does the dev->dev.kobj initialization. Here we only check for 1863 * success which indicates that the network device kobject is 1864 * ready. Once we do that we still need to keep track of whether 1865 * links were successfully set up or not for phy_detach() to 1866 * remove them accordingly. 1867 */ 1868 phydev->sysfs_links = false; 1869 1870 phy_sysfs_create_links(phydev); 1871 1872 if (!phydev->attached_dev) { 1873 err = sysfs_create_file(&phydev->mdio.dev.kobj, 1874 &dev_attr_phy_standalone.attr); 1875 if (err) 1876 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n"); 1877 } 1878 1879 phydev->dev_flags |= flags; 1880 1881 phydev->interface = interface; 1882 1883 phydev->state = PHY_READY; 1884 1885 phydev->interrupts = PHY_INTERRUPT_DISABLED; 1886 1887 /* PHYs can request to use poll mode even though they have an 1888 * associated interrupt line. This could be the case if they 1889 * detect a broken interrupt handling. 1890 */ 1891 if (phydev->dev_flags & PHY_F_NO_IRQ) 1892 phydev->irq = PHY_POLL; 1893 1894 if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev)) 1895 phydev->irq = PHY_POLL; 1896 1897 /* Port is set to PORT_TP by default and the actual PHY driver will set 1898 * it to different value depending on the PHY configuration. If we have 1899 * the generic PHY driver we can't figure it out, thus set the old 1900 * legacy PORT_MII value. 1901 */ 1902 if (phydev->is_genphy_driven) 1903 phydev->port = PORT_MII; 1904 1905 /* Initial carrier state is off as the phy is about to be 1906 * (re)initialized. 1907 */ 1908 if (dev) 1909 netif_carrier_off(phydev->attached_dev); 1910 1911 /* Do initial configuration here, now that 1912 * we have certain key parameters 1913 * (dev_flags and interface) 1914 */ 1915 err = phy_init_hw(phydev); 1916 if (err) 1917 goto error; 1918 1919 if (phydev->mdio.bus->notify_phy_attach) { 1920 err = phydev->mdio.bus->notify_phy_attach(phydev); 1921 if (err) 1922 goto error; 1923 } 1924 1925 phy_resume(phydev); 1926 1927 /** 1928 * If the external phy used by current mac interface is managed by 1929 * another mac interface, so we should create a device link between 1930 * phy dev and mac dev. 1931 */ 1932 if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent) 1933 phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev, 1934 DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS); 1935 1936 return err; 1937 1938 error: 1939 /* phy_detach_internal() does all of the cleanup below */ 1940 phy_detach_internal(phydev, false); 1941 return err; 1942 1943 error_module_put: 1944 module_put(d->driver->owner); 1945 phydev->irq = irq; 1946 phydev->is_genphy_driven = 0; 1947 d->driver = NULL; 1948 error_put_device: 1949 put_device(d); 1950 if (ndev_owner != bus->owner) 1951 module_put(bus->owner); 1952 return err; 1953 } 1954 EXPORT_SYMBOL(phy_attach_direct); 1955 1956 int phy_suspend(struct phy_device *phydev) 1957 { 1958 struct net_device *netdev = phydev->attached_dev; 1959 const struct phy_driver *phydrv = phydev->drv; 1960 int ret; 1961 1962 if (phydev->suspended || !phydrv) 1963 return 0; 1964 1965 phydev->wol_enabled = phy_may_wakeup(phydev) || 1966 (netdev && netdev->ethtool->wol_enabled); 1967 /* If the device has WOL enabled, we cannot suspend the PHY */ 1968 if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND)) 1969 return -EBUSY; 1970 1971 if (!phydrv->suspend) 1972 return 0; 1973 1974 ret = phydrv->suspend(phydev); 1975 if (!ret) 1976 phydev->suspended = true; 1977 1978 return ret; 1979 } 1980 EXPORT_SYMBOL(phy_suspend); 1981 1982 int __phy_resume(struct phy_device *phydev) 1983 { 1984 const struct phy_driver *phydrv = phydev->drv; 1985 int ret; 1986 1987 lockdep_assert_held(&phydev->lock); 1988 1989 if (!phydrv || !phydrv->resume) 1990 return 0; 1991 1992 ret = phydrv->resume(phydev); 1993 if (!ret) 1994 phydev->suspended = false; 1995 1996 return ret; 1997 } 1998 EXPORT_SYMBOL(__phy_resume); 1999 2000 int phy_resume(struct phy_device *phydev) 2001 { 2002 int ret; 2003 2004 mutex_lock(&phydev->lock); 2005 ret = __phy_resume(phydev); 2006 mutex_unlock(&phydev->lock); 2007 2008 return ret; 2009 } 2010 EXPORT_SYMBOL(phy_resume); 2011 2012 /** 2013 * phy_reset_after_clk_enable - perform a PHY reset if needed 2014 * @phydev: target phy_device struct 2015 * 2016 * Description: Some PHYs are known to need a reset after their refclk was 2017 * enabled. This function evaluates the flags and perform the reset if it's 2018 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy 2019 * was reset. 2020 */ 2021 int phy_reset_after_clk_enable(struct phy_device *phydev) 2022 { 2023 if (!phydev || !phydev->drv) 2024 return -ENODEV; 2025 2026 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) { 2027 phy_device_reset(phydev, 1); 2028 phy_device_reset(phydev, 0); 2029 return 1; 2030 } 2031 2032 return 0; 2033 } 2034 EXPORT_SYMBOL(phy_reset_after_clk_enable); 2035 2036 /* Generic PHY support and helper functions */ 2037 2038 /** 2039 * genphy_config_advert - sanitize and advertise auto-negotiation parameters 2040 * @phydev: target phy_device struct 2041 * @advert: auto-negotiation parameters to advertise 2042 * 2043 * Description: Writes MII_ADVERTISE with the appropriate values, 2044 * after sanitizing the values to make sure we only advertise 2045 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 2046 * hasn't changed, and > 0 if it has changed. 2047 */ 2048 static int genphy_config_advert(struct phy_device *phydev, 2049 const unsigned long *advert) 2050 { 2051 int err, bmsr, changed = 0; 2052 u32 adv; 2053 2054 adv = linkmode_adv_to_mii_adv_t(advert); 2055 2056 /* Setup standard advertisement */ 2057 err = phy_modify_changed(phydev, MII_ADVERTISE, 2058 ADVERTISE_ALL | ADVERTISE_100BASE4 | 2059 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM, 2060 adv); 2061 if (err < 0) 2062 return err; 2063 if (err > 0) 2064 changed = 1; 2065 2066 bmsr = phy_read(phydev, MII_BMSR); 2067 if (bmsr < 0) 2068 return bmsr; 2069 2070 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all 2071 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a 2072 * logical 1. 2073 */ 2074 if (!(bmsr & BMSR_ESTATEN)) 2075 return changed; 2076 2077 adv = linkmode_adv_to_mii_ctrl1000_t(advert); 2078 2079 err = phy_modify_changed(phydev, MII_CTRL1000, 2080 ADVERTISE_1000FULL | ADVERTISE_1000HALF, 2081 adv); 2082 if (err < 0) 2083 return err; 2084 if (err > 0) 2085 changed = 1; 2086 2087 return changed; 2088 } 2089 2090 /** 2091 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters 2092 * @phydev: target phy_device struct 2093 * 2094 * Description: Writes MII_ADVERTISE with the appropriate values, 2095 * after sanitizing the values to make sure we only advertise 2096 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 2097 * hasn't changed, and > 0 if it has changed. This function is intended 2098 * for Clause 37 1000Base-X mode. 2099 */ 2100 static int genphy_c37_config_advert(struct phy_device *phydev) 2101 { 2102 u16 adv = 0; 2103 2104 /* Only allow advertising what this PHY supports */ 2105 linkmode_and(phydev->advertising, phydev->advertising, 2106 phydev->supported); 2107 2108 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2109 phydev->advertising)) 2110 adv |= ADVERTISE_1000XFULL; 2111 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2112 phydev->advertising)) 2113 adv |= ADVERTISE_1000XPAUSE; 2114 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2115 phydev->advertising)) 2116 adv |= ADVERTISE_1000XPSE_ASYM; 2117 2118 return phy_modify_changed(phydev, MII_ADVERTISE, 2119 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | 2120 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM, 2121 adv); 2122 } 2123 2124 /** 2125 * genphy_setup_forced - configures/forces speed/duplex from @phydev 2126 * @phydev: target phy_device struct 2127 * 2128 * Description: Configures MII_BMCR to force speed/duplex 2129 * to the values in phydev. Assumes that the values are valid. 2130 * Please see phy_sanitize_settings(). 2131 */ 2132 int genphy_setup_forced(struct phy_device *phydev) 2133 { 2134 u16 ctl; 2135 2136 phydev->pause = false; 2137 phydev->asym_pause = false; 2138 2139 ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 2140 2141 return phy_modify(phydev, MII_BMCR, 2142 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl); 2143 } 2144 EXPORT_SYMBOL(genphy_setup_forced); 2145 2146 static int genphy_setup_master_slave(struct phy_device *phydev) 2147 { 2148 u16 ctl = 0; 2149 2150 if (!phydev->is_gigabit_capable) 2151 return 0; 2152 2153 switch (phydev->master_slave_set) { 2154 case MASTER_SLAVE_CFG_MASTER_PREFERRED: 2155 ctl |= CTL1000_PREFER_MASTER; 2156 break; 2157 case MASTER_SLAVE_CFG_SLAVE_PREFERRED: 2158 break; 2159 case MASTER_SLAVE_CFG_MASTER_FORCE: 2160 ctl |= CTL1000_AS_MASTER; 2161 fallthrough; 2162 case MASTER_SLAVE_CFG_SLAVE_FORCE: 2163 ctl |= CTL1000_ENABLE_MASTER; 2164 break; 2165 case MASTER_SLAVE_CFG_UNKNOWN: 2166 case MASTER_SLAVE_CFG_UNSUPPORTED: 2167 return 0; 2168 default: 2169 phydev_warn(phydev, "Unsupported Master/Slave mode\n"); 2170 return -EOPNOTSUPP; 2171 } 2172 2173 return phy_modify_changed(phydev, MII_CTRL1000, 2174 (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER | 2175 CTL1000_PREFER_MASTER), ctl); 2176 } 2177 2178 int genphy_read_master_slave(struct phy_device *phydev) 2179 { 2180 int cfg, state; 2181 int val; 2182 2183 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN; 2184 phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN; 2185 2186 val = phy_read(phydev, MII_CTRL1000); 2187 if (val < 0) 2188 return val; 2189 2190 if (val & CTL1000_ENABLE_MASTER) { 2191 if (val & CTL1000_AS_MASTER) 2192 cfg = MASTER_SLAVE_CFG_MASTER_FORCE; 2193 else 2194 cfg = MASTER_SLAVE_CFG_SLAVE_FORCE; 2195 } else { 2196 if (val & CTL1000_PREFER_MASTER) 2197 cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED; 2198 else 2199 cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED; 2200 } 2201 2202 val = phy_read(phydev, MII_STAT1000); 2203 if (val < 0) 2204 return val; 2205 2206 if (val & LPA_1000MSFAIL) { 2207 state = MASTER_SLAVE_STATE_ERR; 2208 } else if (phydev->link) { 2209 /* this bits are valid only for active link */ 2210 if (val & LPA_1000MSRES) 2211 state = MASTER_SLAVE_STATE_MASTER; 2212 else 2213 state = MASTER_SLAVE_STATE_SLAVE; 2214 } else { 2215 state = MASTER_SLAVE_STATE_UNKNOWN; 2216 } 2217 2218 phydev->master_slave_get = cfg; 2219 phydev->master_slave_state = state; 2220 2221 return 0; 2222 } 2223 EXPORT_SYMBOL(genphy_read_master_slave); 2224 2225 /** 2226 * genphy_restart_aneg - Enable and Restart Autonegotiation 2227 * @phydev: target phy_device struct 2228 */ 2229 int genphy_restart_aneg(struct phy_device *phydev) 2230 { 2231 /* Don't isolate the PHY if we're negotiating */ 2232 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, 2233 BMCR_ANENABLE | BMCR_ANRESTART); 2234 } 2235 EXPORT_SYMBOL(genphy_restart_aneg); 2236 2237 /** 2238 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation 2239 * @phydev: target phy_device struct 2240 * @restart: whether aneg restart is requested 2241 * 2242 * Check, and restart auto-negotiation if needed. 2243 */ 2244 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart) 2245 { 2246 int ret; 2247 2248 if (!restart) { 2249 /* Advertisement hasn't changed, but maybe aneg was never on to 2250 * begin with? Or maybe phy was isolated? 2251 */ 2252 ret = phy_read(phydev, MII_BMCR); 2253 if (ret < 0) 2254 return ret; 2255 2256 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE)) 2257 restart = true; 2258 } 2259 2260 if (restart) 2261 return genphy_restart_aneg(phydev); 2262 2263 return 0; 2264 } 2265 EXPORT_SYMBOL(genphy_check_and_restart_aneg); 2266 2267 /** 2268 * __genphy_config_aneg - restart auto-negotiation or write BMCR 2269 * @phydev: target phy_device struct 2270 * @changed: whether autoneg is requested 2271 * 2272 * Description: If auto-negotiation is enabled, we configure the 2273 * advertising, and then restart auto-negotiation. If it is not 2274 * enabled, then we write the BMCR. 2275 */ 2276 int __genphy_config_aneg(struct phy_device *phydev, bool changed) 2277 { 2278 __ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert); 2279 const struct link_capabilities *c; 2280 unsigned long *advert; 2281 int err; 2282 2283 err = genphy_c45_an_config_eee_aneg(phydev); 2284 if (err < 0) 2285 return err; 2286 else if (err) 2287 changed = true; 2288 2289 err = genphy_setup_master_slave(phydev); 2290 if (err < 0) 2291 return err; 2292 else if (err) 2293 changed = true; 2294 2295 if (phydev->autoneg == AUTONEG_ENABLE) { 2296 /* Only allow advertising what this PHY supports */ 2297 linkmode_and(phydev->advertising, phydev->advertising, 2298 phydev->supported); 2299 advert = phydev->advertising; 2300 } else if (phydev->speed < SPEED_1000) { 2301 return genphy_setup_forced(phydev); 2302 } else { 2303 linkmode_zero(fixed_advert); 2304 2305 c = phy_caps_lookup(phydev->speed, phydev->duplex, 2306 phydev->supported, true); 2307 if (c) 2308 linkmode_and(fixed_advert, phydev->supported, 2309 c->linkmodes); 2310 2311 advert = fixed_advert; 2312 } 2313 2314 err = genphy_config_advert(phydev, advert); 2315 if (err < 0) /* error */ 2316 return err; 2317 else if (err) 2318 changed = true; 2319 2320 return genphy_check_and_restart_aneg(phydev, changed); 2321 } 2322 EXPORT_SYMBOL(__genphy_config_aneg); 2323 2324 /** 2325 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR 2326 * @phydev: target phy_device struct 2327 * 2328 * Description: If auto-negotiation is enabled, we configure the 2329 * advertising, and then restart auto-negotiation. If it is not 2330 * enabled, then we write the BMCR. This function is intended 2331 * for use with Clause 37 1000Base-X mode. 2332 */ 2333 int genphy_c37_config_aneg(struct phy_device *phydev) 2334 { 2335 int err, changed; 2336 2337 if (phydev->autoneg != AUTONEG_ENABLE) 2338 return genphy_setup_forced(phydev); 2339 2340 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100, 2341 BMCR_SPEED1000); 2342 if (err) 2343 return err; 2344 2345 changed = genphy_c37_config_advert(phydev); 2346 if (changed < 0) /* error */ 2347 return changed; 2348 2349 if (!changed) { 2350 /* Advertisement hasn't changed, but maybe aneg was never on to 2351 * begin with? Or maybe phy was isolated? 2352 */ 2353 int ctl = phy_read(phydev, MII_BMCR); 2354 2355 if (ctl < 0) 2356 return ctl; 2357 2358 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) 2359 changed = 1; /* do restart aneg */ 2360 } 2361 2362 /* Only restart aneg if we are advertising something different 2363 * than we were before. 2364 */ 2365 if (changed > 0) 2366 return genphy_restart_aneg(phydev); 2367 2368 return 0; 2369 } 2370 EXPORT_SYMBOL(genphy_c37_config_aneg); 2371 2372 /** 2373 * genphy_aneg_done - return auto-negotiation status 2374 * @phydev: target phy_device struct 2375 * 2376 * Description: Reads the status register and returns 0 either if 2377 * auto-negotiation is incomplete, or if there was an error. 2378 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. 2379 */ 2380 int genphy_aneg_done(struct phy_device *phydev) 2381 { 2382 int retval = phy_read(phydev, MII_BMSR); 2383 2384 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); 2385 } 2386 EXPORT_SYMBOL(genphy_aneg_done); 2387 2388 /** 2389 * genphy_update_link - update link status in @phydev 2390 * @phydev: target phy_device struct 2391 * 2392 * Description: Update the value in phydev->link to reflect the 2393 * current link value. In order to do this, we need to read 2394 * the status register twice, keeping the second value. 2395 */ 2396 int genphy_update_link(struct phy_device *phydev) 2397 { 2398 int status = 0, bmcr; 2399 2400 bmcr = phy_read(phydev, MII_BMCR); 2401 if (bmcr < 0) 2402 return bmcr; 2403 2404 /* Autoneg is being started, therefore disregard BMSR value and 2405 * report link as down. 2406 */ 2407 if (bmcr & BMCR_ANRESTART) 2408 goto done; 2409 2410 /* The link state is latched low so that momentary link 2411 * drops can be detected. Do not double-read the status 2412 * in polling mode to detect such short link drops except 2413 * if the link was already down. 2414 */ 2415 if (!phy_polling_mode(phydev) || !phydev->link) { 2416 status = phy_read(phydev, MII_BMSR); 2417 if (status < 0) 2418 return status; 2419 else if (status & BMSR_LSTATUS) 2420 goto done; 2421 } 2422 2423 /* Read link and autonegotiation status */ 2424 status = phy_read(phydev, MII_BMSR); 2425 if (status < 0) 2426 return status; 2427 done: 2428 phydev->link = status & BMSR_LSTATUS ? 1 : 0; 2429 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0; 2430 2431 /* Consider the case that autoneg was started and "aneg complete" 2432 * bit has been reset, but "link up" bit not yet. 2433 */ 2434 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete) 2435 phydev->link = 0; 2436 2437 return 0; 2438 } 2439 EXPORT_SYMBOL(genphy_update_link); 2440 2441 int genphy_read_lpa(struct phy_device *phydev) 2442 { 2443 int lpa, lpagb; 2444 2445 if (phydev->autoneg == AUTONEG_ENABLE) { 2446 if (!phydev->autoneg_complete) { 2447 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 2448 0); 2449 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0); 2450 return 0; 2451 } 2452 2453 if (phydev->is_gigabit_capable) { 2454 lpagb = phy_read(phydev, MII_STAT1000); 2455 if (lpagb < 0) 2456 return lpagb; 2457 2458 if (lpagb & LPA_1000MSFAIL) { 2459 int adv = phy_read(phydev, MII_CTRL1000); 2460 2461 if (adv < 0) 2462 return adv; 2463 2464 if (adv & CTL1000_ENABLE_MASTER) 2465 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n"); 2466 else 2467 phydev_err(phydev, "Master/Slave resolution failed\n"); 2468 return -ENOLINK; 2469 } 2470 2471 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 2472 lpagb); 2473 } 2474 2475 lpa = phy_read(phydev, MII_LPA); 2476 if (lpa < 0) 2477 return lpa; 2478 2479 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa); 2480 } else { 2481 linkmode_zero(phydev->lp_advertising); 2482 } 2483 2484 return 0; 2485 } 2486 EXPORT_SYMBOL(genphy_read_lpa); 2487 2488 /** 2489 * genphy_read_status_fixed - read the link parameters for !aneg mode 2490 * @phydev: target phy_device struct 2491 * 2492 * Read the current duplex and speed state for a PHY operating with 2493 * autonegotiation disabled. 2494 */ 2495 int genphy_read_status_fixed(struct phy_device *phydev) 2496 { 2497 int bmcr = phy_read(phydev, MII_BMCR); 2498 2499 if (bmcr < 0) 2500 return bmcr; 2501 2502 if (bmcr & BMCR_FULLDPLX) 2503 phydev->duplex = DUPLEX_FULL; 2504 else 2505 phydev->duplex = DUPLEX_HALF; 2506 2507 if (bmcr & BMCR_SPEED1000) 2508 phydev->speed = SPEED_1000; 2509 else if (bmcr & BMCR_SPEED100) 2510 phydev->speed = SPEED_100; 2511 else 2512 phydev->speed = SPEED_10; 2513 2514 return 0; 2515 } 2516 EXPORT_SYMBOL(genphy_read_status_fixed); 2517 2518 /** 2519 * genphy_read_status - check the link status and update current link state 2520 * @phydev: target phy_device struct 2521 * 2522 * Description: Check the link, then figure out the current state 2523 * by comparing what we advertise with what the link partner 2524 * advertises. Start by checking the gigabit possibilities, 2525 * then move on to 10/100. 2526 */ 2527 int genphy_read_status(struct phy_device *phydev) 2528 { 2529 int err, old_link = phydev->link; 2530 2531 /* Update the link, but return if there was an error */ 2532 err = genphy_update_link(phydev); 2533 if (err) 2534 return err; 2535 2536 /* why bother the PHY if nothing can have changed */ 2537 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) 2538 return 0; 2539 2540 phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED; 2541 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED; 2542 phydev->speed = SPEED_UNKNOWN; 2543 phydev->duplex = DUPLEX_UNKNOWN; 2544 phydev->pause = false; 2545 phydev->asym_pause = false; 2546 2547 if (phydev->is_gigabit_capable) { 2548 err = genphy_read_master_slave(phydev); 2549 if (err < 0) 2550 return err; 2551 } 2552 2553 err = genphy_read_lpa(phydev); 2554 if (err < 0) 2555 return err; 2556 2557 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { 2558 phy_resolve_aneg_linkmode(phydev); 2559 } else if (phydev->autoneg == AUTONEG_DISABLE) { 2560 err = genphy_read_status_fixed(phydev); 2561 if (err < 0) 2562 return err; 2563 } 2564 2565 return 0; 2566 } 2567 EXPORT_SYMBOL(genphy_read_status); 2568 2569 /** 2570 * genphy_c37_read_status - check the link status and update current link state 2571 * @phydev: target phy_device struct 2572 * @changed: pointer where to store if link changed 2573 * 2574 * Description: Check the link, then figure out the current state 2575 * by comparing what we advertise with what the link partner 2576 * advertises. This function is for Clause 37 1000Base-X mode. 2577 * 2578 * If link has changed, @changed is set to true, false otherwise. 2579 */ 2580 int genphy_c37_read_status(struct phy_device *phydev, bool *changed) 2581 { 2582 int lpa, err, old_link = phydev->link; 2583 2584 /* Update the link, but return if there was an error */ 2585 err = genphy_update_link(phydev); 2586 if (err) 2587 return err; 2588 2589 /* why bother the PHY if nothing can have changed */ 2590 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) { 2591 *changed = false; 2592 return 0; 2593 } 2594 2595 /* Signal link has changed */ 2596 *changed = true; 2597 phydev->duplex = DUPLEX_UNKNOWN; 2598 phydev->pause = false; 2599 phydev->asym_pause = false; 2600 2601 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { 2602 lpa = phy_read(phydev, MII_LPA); 2603 if (lpa < 0) 2604 return lpa; 2605 2606 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 2607 phydev->lp_advertising, lpa & LPA_LPACK); 2608 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2609 phydev->lp_advertising, lpa & LPA_1000XFULL); 2610 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2611 phydev->lp_advertising, lpa & LPA_1000XPAUSE); 2612 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2613 phydev->lp_advertising, 2614 lpa & LPA_1000XPAUSE_ASYM); 2615 2616 phy_resolve_aneg_linkmode(phydev); 2617 } else if (phydev->autoneg == AUTONEG_DISABLE) { 2618 int bmcr = phy_read(phydev, MII_BMCR); 2619 2620 if (bmcr < 0) 2621 return bmcr; 2622 2623 if (bmcr & BMCR_FULLDPLX) 2624 phydev->duplex = DUPLEX_FULL; 2625 else 2626 phydev->duplex = DUPLEX_HALF; 2627 } 2628 2629 return 0; 2630 } 2631 EXPORT_SYMBOL(genphy_c37_read_status); 2632 2633 /** 2634 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit 2635 * @phydev: target phy_device struct 2636 * 2637 * Description: Perform a software PHY reset using the standard 2638 * BMCR_RESET bit and poll for the reset bit to be cleared. 2639 * 2640 * Returns: 0 on success, < 0 on failure 2641 */ 2642 int genphy_soft_reset(struct phy_device *phydev) 2643 { 2644 u16 res = BMCR_RESET; 2645 int ret; 2646 2647 if (phydev->autoneg == AUTONEG_ENABLE) 2648 res |= BMCR_ANRESTART; 2649 2650 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res); 2651 if (ret < 0) 2652 return ret; 2653 2654 /* Clause 22 states that setting bit BMCR_RESET sets control registers 2655 * to their default value. Therefore the POWER DOWN bit is supposed to 2656 * be cleared after soft reset. 2657 */ 2658 phydev->suspended = 0; 2659 2660 ret = phy_poll_reset(phydev); 2661 if (ret) 2662 return ret; 2663 2664 /* BMCR may be reset to defaults */ 2665 if (phydev->autoneg == AUTONEG_DISABLE) 2666 ret = genphy_setup_forced(phydev); 2667 2668 return ret; 2669 } 2670 EXPORT_SYMBOL(genphy_soft_reset); 2671 2672 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev) 2673 { 2674 /* It seems there are cases where the interrupts are handled by another 2675 * entity (ie an IRQ controller embedded inside the PHY) and do not 2676 * need any other interraction from phylib. In this case, just trigger 2677 * the state machine directly. 2678 */ 2679 phy_trigger_machine(phydev); 2680 2681 return 0; 2682 } 2683 EXPORT_SYMBOL(genphy_handle_interrupt_no_ack); 2684 2685 /** 2686 * genphy_read_abilities - read PHY abilities from Clause 22 registers 2687 * @phydev: target phy_device struct 2688 * 2689 * Description: Reads the PHY's abilities and populates 2690 * phydev->supported accordingly. 2691 * 2692 * Returns: 0 on success, < 0 on failure 2693 */ 2694 int genphy_read_abilities(struct phy_device *phydev) 2695 { 2696 int val; 2697 2698 linkmode_set_bit_array(phy_basic_ports_array, 2699 ARRAY_SIZE(phy_basic_ports_array), 2700 phydev->supported); 2701 2702 val = phy_read(phydev, MII_BMSR); 2703 if (val < 0) 2704 return val; 2705 2706 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported, 2707 val & BMSR_ANEGCAPABLE); 2708 2709 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported, 2710 val & BMSR_100FULL); 2711 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported, 2712 val & BMSR_100HALF); 2713 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported, 2714 val & BMSR_10FULL); 2715 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported, 2716 val & BMSR_10HALF); 2717 2718 if (val & BMSR_ESTATEN) { 2719 val = phy_read(phydev, MII_ESTATUS); 2720 if (val < 0) 2721 return val; 2722 2723 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 2724 phydev->supported, val & ESTATUS_1000_TFULL); 2725 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 2726 phydev->supported, val & ESTATUS_1000_THALF); 2727 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2728 phydev->supported, val & ESTATUS_1000_XFULL); 2729 } 2730 2731 /* This is optional functionality. If not supported, we may get an error 2732 * which should be ignored. 2733 */ 2734 genphy_c45_read_eee_abilities(phydev); 2735 2736 return 0; 2737 } 2738 EXPORT_SYMBOL(genphy_read_abilities); 2739 2740 /* Some PHYs support direct C45 register access but not C22 indirect 2741 * MMD access (registers 13 and 14). When discovered via C22, phylib 2742 * routes MMD access through the indirect path, which won't work on 2743 * these devices. These helpers bypass indirect access and use the bus 2744 * C45 accessors directly. 2745 */ 2746 int genphy_read_mmd_c45(struct phy_device *phydev, int devnum, u16 regnum) 2747 { 2748 struct mii_bus *bus = phydev->mdio.bus; 2749 int addr = phydev->mdio.addr; 2750 2751 return __mdiobus_c45_read(bus, addr, devnum, regnum); 2752 } 2753 EXPORT_SYMBOL(genphy_read_mmd_c45); 2754 2755 int genphy_write_mmd_c45(struct phy_device *phydev, int devnum, u16 regnum, 2756 u16 val) 2757 { 2758 struct mii_bus *bus = phydev->mdio.bus; 2759 int addr = phydev->mdio.addr; 2760 2761 return __mdiobus_c45_write(bus, addr, devnum, regnum, val); 2762 } 2763 EXPORT_SYMBOL(genphy_write_mmd_c45); 2764 2765 /* This is used for the phy device which doesn't support the MMD extended 2766 * register access, but it does have side effect when we are trying to access 2767 * the MMD register via indirect method. 2768 */ 2769 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum) 2770 { 2771 return -EOPNOTSUPP; 2772 } 2773 EXPORT_SYMBOL(genphy_read_mmd_unsupported); 2774 2775 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum, 2776 u16 regnum, u16 val) 2777 { 2778 return -EOPNOTSUPP; 2779 } 2780 EXPORT_SYMBOL(genphy_write_mmd_unsupported); 2781 2782 int genphy_suspend(struct phy_device *phydev) 2783 { 2784 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN); 2785 } 2786 EXPORT_SYMBOL(genphy_suspend); 2787 2788 int genphy_resume(struct phy_device *phydev) 2789 { 2790 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN); 2791 } 2792 EXPORT_SYMBOL(genphy_resume); 2793 2794 int genphy_loopback(struct phy_device *phydev, bool enable, int speed) 2795 { 2796 if (enable) { 2797 u16 ctl = BMCR_LOOPBACK; 2798 int ret, val; 2799 2800 if (speed == SPEED_10 || speed == SPEED_100 || 2801 speed == SPEED_1000) 2802 phydev->speed = speed; 2803 else if (speed) 2804 return -EINVAL; 2805 2806 ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 2807 2808 phy_modify(phydev, MII_BMCR, ~0, ctl); 2809 2810 ret = phy_read_poll_timeout(phydev, MII_BMSR, val, 2811 val & BMSR_LSTATUS, 2812 5000, 500000, true); 2813 if (ret) 2814 return ret; 2815 } else { 2816 phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0); 2817 2818 phy_config_aneg(phydev); 2819 } 2820 2821 return 0; 2822 } 2823 EXPORT_SYMBOL(genphy_loopback); 2824 2825 /** 2826 * phy_remove_link_mode - Remove a supported link mode 2827 * @phydev: phy_device structure to remove link mode from 2828 * @link_mode: Link mode to be removed 2829 * 2830 * Description: Some MACs don't support all link modes which the PHY 2831 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper 2832 * to remove a link mode. 2833 */ 2834 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode) 2835 { 2836 linkmode_clear_bit(link_mode, phydev->supported); 2837 phy_advertise_supported(phydev); 2838 } 2839 EXPORT_SYMBOL(phy_remove_link_mode); 2840 2841 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src) 2842 { 2843 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst, 2844 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src)); 2845 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst, 2846 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src)); 2847 } 2848 2849 /** 2850 * phy_advertise_supported - Advertise all supported modes 2851 * @phydev: target phy_device struct 2852 * 2853 * Description: Called to advertise all supported modes, doesn't touch 2854 * pause mode advertising. 2855 */ 2856 void phy_advertise_supported(struct phy_device *phydev) 2857 { 2858 __ETHTOOL_DECLARE_LINK_MODE_MASK(new); 2859 2860 linkmode_copy(new, phydev->supported); 2861 phy_copy_pause_bits(new, phydev->advertising); 2862 linkmode_copy(phydev->advertising, new); 2863 } 2864 EXPORT_SYMBOL(phy_advertise_supported); 2865 2866 /** 2867 * phy_advertise_eee_all - Advertise all supported EEE modes 2868 * @phydev: target phy_device struct 2869 * 2870 * Description: Per default phylib preserves the EEE advertising at the time of 2871 * phy probing, which might be a subset of the supported EEE modes. Use this 2872 * function when all supported EEE modes should be advertised. This does not 2873 * trigger auto-negotiation, so must be called before phy_start()/ 2874 * phylink_start() which will start auto-negotiation. 2875 */ 2876 void phy_advertise_eee_all(struct phy_device *phydev) 2877 { 2878 linkmode_andnot(phydev->advertising_eee, phydev->supported_eee, 2879 phydev->eee_disabled_modes); 2880 } 2881 EXPORT_SYMBOL_GPL(phy_advertise_eee_all); 2882 2883 /** 2884 * phy_support_eee - Set initial EEE policy configuration 2885 * @phydev: Target phy_device struct 2886 * 2887 * This function configures the initial policy for Energy Efficient Ethernet 2888 * (EEE) on the specified PHY device, influencing that EEE capabilities are 2889 * advertised before the link is established. It should be called during PHY 2890 * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs) 2891 * if MAC supports LPI or PHY is capable to compensate missing LPI functionality 2892 * of the MAC. 2893 * 2894 * The function sets default EEE policy parameters, including preparing the PHY 2895 * to advertise EEE capabilities based on hardware support. 2896 * 2897 * It also sets the expected configuration for Low Power Idle (LPI) in the MAC 2898 * driver. If the PHY framework determines that both local and remote 2899 * advertisements support EEE, and the negotiated link mode is compatible with 2900 * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on 2901 * this setting by enabling the LPI timer if enable_tx_lpi is set. 2902 */ 2903 void phy_support_eee(struct phy_device *phydev) 2904 { 2905 linkmode_andnot(phydev->advertising_eee, phydev->supported_eee, 2906 phydev->eee_disabled_modes); 2907 phydev->eee_cfg.tx_lpi_enabled = true; 2908 phydev->eee_cfg.eee_enabled = true; 2909 2910 /* If the PHY supports autonomous EEE, disable it so the MAC can 2911 * manage LPI signaling instead. The flag is stored so it can be 2912 * re-applied after a PHY soft reset (e.g. suspend/resume). 2913 */ 2914 if (phydev->drv && phydev->drv->disable_autonomous_eee) { 2915 int ret = phydev->drv->disable_autonomous_eee(phydev); 2916 2917 if (ret) 2918 phydev_warn(phydev, "Failed to disable autonomous EEE: %pe\n", 2919 ERR_PTR(ret)); 2920 else 2921 phydev->autonomous_eee_disabled = true; 2922 } 2923 } 2924 EXPORT_SYMBOL(phy_support_eee); 2925 2926 /** 2927 * phy_disable_eee - Disable EEE for the PHY 2928 * @phydev: Target phy_device struct 2929 * 2930 * This function is used by MAC drivers for MAC's which don't support EEE. 2931 * It disables EEE on the PHY layer. 2932 */ 2933 void phy_disable_eee(struct phy_device *phydev) 2934 { 2935 linkmode_zero(phydev->advertising_eee); 2936 phydev->eee_cfg.tx_lpi_enabled = false; 2937 phydev->eee_cfg.eee_enabled = false; 2938 /* don't let userspace re-enable EEE advertisement */ 2939 linkmode_fill(phydev->eee_disabled_modes); 2940 } 2941 EXPORT_SYMBOL_GPL(phy_disable_eee); 2942 2943 /** 2944 * phy_support_sym_pause - Enable support of symmetrical pause 2945 * @phydev: target phy_device struct 2946 * 2947 * Description: Called by the MAC to indicate is supports symmetrical 2948 * Pause, but not asym pause. 2949 */ 2950 void phy_support_sym_pause(struct phy_device *phydev) 2951 { 2952 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 2953 phy_copy_pause_bits(phydev->advertising, phydev->supported); 2954 } 2955 EXPORT_SYMBOL(phy_support_sym_pause); 2956 2957 /** 2958 * phy_support_asym_pause - Enable support of asym pause 2959 * @phydev: target phy_device struct 2960 * 2961 * Description: Called by the MAC to indicate is supports Asym Pause. 2962 */ 2963 void phy_support_asym_pause(struct phy_device *phydev) 2964 { 2965 phy_copy_pause_bits(phydev->advertising, phydev->supported); 2966 } 2967 EXPORT_SYMBOL(phy_support_asym_pause); 2968 2969 /** 2970 * phy_set_sym_pause - Configure symmetric Pause 2971 * @phydev: target phy_device struct 2972 * @rx: Receiver Pause is supported 2973 * @tx: Transmit Pause is supported 2974 * @autoneg: Auto neg should be used 2975 * 2976 * Description: Configure advertised Pause support depending on if 2977 * receiver pause and pause auto neg is supported. Generally called 2978 * from the set_pauseparam .ndo. 2979 */ 2980 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx, 2981 bool autoneg) 2982 { 2983 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 2984 2985 if (rx && tx && autoneg) 2986 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2987 phydev->supported); 2988 2989 linkmode_copy(phydev->advertising, phydev->supported); 2990 } 2991 EXPORT_SYMBOL(phy_set_sym_pause); 2992 2993 /** 2994 * phy_set_asym_pause - Configure Pause and Asym Pause 2995 * @phydev: target phy_device struct 2996 * @rx: Receiver Pause is supported 2997 * @tx: Transmit Pause is supported 2998 * 2999 * Description: Configure advertised Pause support depending on if 3000 * transmit and receiver pause is supported. If there has been a 3001 * change in adverting, trigger a new autoneg. Generally called from 3002 * the set_pauseparam .ndo. 3003 */ 3004 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx) 3005 { 3006 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv); 3007 3008 linkmode_copy(oldadv, phydev->advertising); 3009 linkmode_set_pause(phydev->advertising, tx, rx); 3010 3011 if (!linkmode_equal(oldadv, phydev->advertising) && 3012 phydev->autoneg) 3013 phy_start_aneg(phydev); 3014 } 3015 EXPORT_SYMBOL(phy_set_asym_pause); 3016 3017 /** 3018 * phy_validate_pause - Test if the PHY/MAC support the pause configuration 3019 * @phydev: phy_device struct 3020 * @pp: requested pause configuration 3021 * 3022 * Description: Test if the PHY/MAC combination supports the Pause 3023 * configuration the user is requesting. Returns True if it is 3024 * supported, false otherwise. 3025 */ 3026 bool phy_validate_pause(struct phy_device *phydev, 3027 struct ethtool_pauseparam *pp) 3028 { 3029 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 3030 phydev->supported) && pp->rx_pause) 3031 return false; 3032 3033 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 3034 phydev->supported) && 3035 pp->rx_pause != pp->tx_pause) 3036 return false; 3037 3038 return true; 3039 } 3040 EXPORT_SYMBOL(phy_validate_pause); 3041 3042 /** 3043 * phy_get_pause - resolve negotiated pause modes 3044 * @phydev: phy_device struct 3045 * @tx_pause: pointer to bool to indicate whether transmit pause should be 3046 * enabled. 3047 * @rx_pause: pointer to bool to indicate whether receive pause should be 3048 * enabled. 3049 * 3050 * Resolve and return the flow control modes according to the negotiation 3051 * result. This includes checking that we are operating in full duplex mode. 3052 * See linkmode_resolve_pause() for further details. 3053 */ 3054 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause) 3055 { 3056 if (phydev->duplex != DUPLEX_FULL) { 3057 *tx_pause = false; 3058 *rx_pause = false; 3059 return; 3060 } 3061 3062 return linkmode_resolve_pause(phydev->advertising, 3063 phydev->lp_advertising, 3064 tx_pause, rx_pause); 3065 } 3066 EXPORT_SYMBOL(phy_get_pause); 3067 3068 #if IS_ENABLED(CONFIG_OF_MDIO) 3069 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) 3070 { 3071 return device_property_read_u32(dev, name, val); 3072 } 3073 #else 3074 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) 3075 { 3076 return -EINVAL; 3077 } 3078 #endif 3079 3080 /** 3081 * phy_get_internal_delay - returns the index of the internal delay 3082 * @phydev: phy_device struct 3083 * @delay_values: array of delays the PHY supports 3084 * @size: the size of the delay array 3085 * @is_rx: boolean to indicate to get the rx internal delay 3086 * 3087 * Returns the index within the array of internal delay passed in. 3088 * If the device property is not present then the interface type is checked 3089 * if the interface defines use of internal delay then a 1 is returned otherwise 3090 * a 0 is returned. 3091 * The array must be in ascending order. If PHY does not have an ascending order 3092 * array then size = 0 and the value of the delay property is returned. 3093 * Return -EINVAL if the delay is invalid or cannot be found. 3094 */ 3095 s32 phy_get_internal_delay(struct phy_device *phydev, const int *delay_values, 3096 int size, bool is_rx) 3097 { 3098 struct device *dev = &phydev->mdio.dev; 3099 int i, ret; 3100 u32 delay; 3101 3102 if (is_rx) { 3103 ret = phy_get_u32_property(dev, "rx-internal-delay-ps", &delay); 3104 if (ret < 0 && size == 0) { 3105 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 3106 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) 3107 return 1; 3108 else 3109 return 0; 3110 } 3111 3112 } else { 3113 ret = phy_get_u32_property(dev, "tx-internal-delay-ps", &delay); 3114 if (ret < 0 && size == 0) { 3115 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 3116 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) 3117 return 1; 3118 else 3119 return 0; 3120 } 3121 } 3122 3123 if (ret < 0) 3124 return ret; 3125 3126 if (size == 0) 3127 return delay; 3128 3129 if (delay < delay_values[0] || delay > delay_values[size - 1]) { 3130 phydev_err(phydev, "Delay %d is out of range\n", delay); 3131 return -EINVAL; 3132 } 3133 3134 if (delay == delay_values[0]) 3135 return 0; 3136 3137 for (i = 1; i < size; i++) { 3138 if (delay == delay_values[i]) 3139 return i; 3140 3141 /* Find an approximate index by looking up the table */ 3142 if (delay > delay_values[i - 1] && 3143 delay < delay_values[i]) { 3144 if (delay - delay_values[i - 1] < 3145 delay_values[i] - delay) 3146 return i - 1; 3147 else 3148 return i; 3149 } 3150 } 3151 3152 phydev_err(phydev, "error finding internal delay index for %d\n", 3153 delay); 3154 3155 return -EINVAL; 3156 } 3157 EXPORT_SYMBOL(phy_get_internal_delay); 3158 3159 /** 3160 * phy_get_tx_amplitude_gain - stores tx amplitude gain in @val 3161 * @phydev: phy_device struct 3162 * @dev: pointer to the devices device struct 3163 * @linkmode: linkmode for which the tx amplitude gain should be retrieved 3164 * @val: tx amplitude gain 3165 * 3166 * Returns: 0 on success, < 0 on failure 3167 */ 3168 int phy_get_tx_amplitude_gain(struct phy_device *phydev, struct device *dev, 3169 enum ethtool_link_mode_bit_indices linkmode, 3170 u32 *val) 3171 { 3172 switch (linkmode) { 3173 case ETHTOOL_LINK_MODE_100baseT_Full_BIT: 3174 return phy_get_u32_property(dev, 3175 "tx-amplitude-100base-tx-percent", 3176 val); 3177 default: 3178 return -EINVAL; 3179 } 3180 } 3181 EXPORT_SYMBOL_GPL(phy_get_tx_amplitude_gain); 3182 3183 /** 3184 * phy_get_mac_termination - stores MAC termination in @val 3185 * @phydev: phy_device struct 3186 * @dev: pointer to the devices device struct 3187 * @val: MAC termination 3188 * 3189 * Returns: 0 on success, < 0 on failure 3190 */ 3191 int phy_get_mac_termination(struct phy_device *phydev, struct device *dev, 3192 u32 *val) 3193 { 3194 return phy_get_u32_property(dev, "mac-termination-ohms", val); 3195 } 3196 EXPORT_SYMBOL_GPL(phy_get_mac_termination); 3197 3198 static int phy_led_set_brightness(struct led_classdev *led_cdev, 3199 enum led_brightness value) 3200 { 3201 struct phy_led *phyled = to_phy_led(led_cdev); 3202 struct phy_device *phydev = phyled->phydev; 3203 int err; 3204 3205 mutex_lock(&phydev->lock); 3206 err = phydev->drv->led_brightness_set(phydev, phyled->index, value); 3207 mutex_unlock(&phydev->lock); 3208 3209 return err; 3210 } 3211 3212 static int phy_led_blink_set(struct led_classdev *led_cdev, 3213 unsigned long *delay_on, 3214 unsigned long *delay_off) 3215 { 3216 struct phy_led *phyled = to_phy_led(led_cdev); 3217 struct phy_device *phydev = phyled->phydev; 3218 int err; 3219 3220 mutex_lock(&phydev->lock); 3221 err = phydev->drv->led_blink_set(phydev, phyled->index, 3222 delay_on, delay_off); 3223 mutex_unlock(&phydev->lock); 3224 3225 return err; 3226 } 3227 3228 static __maybe_unused struct device * 3229 phy_led_hw_control_get_device(struct led_classdev *led_cdev) 3230 { 3231 struct phy_led *phyled = to_phy_led(led_cdev); 3232 struct phy_device *phydev = phyled->phydev; 3233 3234 if (phydev->attached_dev) 3235 return &phydev->attached_dev->dev; 3236 return NULL; 3237 } 3238 3239 static int __maybe_unused 3240 phy_led_hw_control_get(struct led_classdev *led_cdev, 3241 unsigned long *rules) 3242 { 3243 struct phy_led *phyled = to_phy_led(led_cdev); 3244 struct phy_device *phydev = phyled->phydev; 3245 int err; 3246 3247 mutex_lock(&phydev->lock); 3248 err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules); 3249 mutex_unlock(&phydev->lock); 3250 3251 return err; 3252 } 3253 3254 static int __maybe_unused 3255 phy_led_hw_control_set(struct led_classdev *led_cdev, 3256 unsigned long rules) 3257 { 3258 struct phy_led *phyled = to_phy_led(led_cdev); 3259 struct phy_device *phydev = phyled->phydev; 3260 int err; 3261 3262 mutex_lock(&phydev->lock); 3263 err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules); 3264 mutex_unlock(&phydev->lock); 3265 3266 return err; 3267 } 3268 3269 static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev, 3270 unsigned long rules) 3271 { 3272 struct phy_led *phyled = to_phy_led(led_cdev); 3273 struct phy_device *phydev = phyled->phydev; 3274 int err; 3275 3276 mutex_lock(&phydev->lock); 3277 err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules); 3278 mutex_unlock(&phydev->lock); 3279 3280 return err; 3281 } 3282 3283 static void phy_leds_unregister(struct phy_device *phydev) 3284 { 3285 struct phy_led *phyled, *tmp; 3286 3287 list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) { 3288 led_classdev_unregister(&phyled->led_cdev); 3289 list_del(&phyled->list); 3290 } 3291 } 3292 3293 static int of_phy_led(struct phy_device *phydev, 3294 struct device_node *led) 3295 { 3296 struct device *dev = &phydev->mdio.dev; 3297 struct led_init_data init_data = {}; 3298 struct led_classdev *cdev; 3299 unsigned long modes = 0; 3300 struct phy_led *phyled; 3301 u32 index; 3302 int err; 3303 3304 phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL); 3305 if (!phyled) 3306 return -ENOMEM; 3307 3308 cdev = &phyled->led_cdev; 3309 phyled->phydev = phydev; 3310 3311 err = of_property_read_u32(led, "reg", &index); 3312 if (err) 3313 return err; 3314 if (index > U8_MAX) 3315 return -EINVAL; 3316 3317 if (of_property_read_bool(led, "active-high")) 3318 set_bit(PHY_LED_ACTIVE_HIGH, &modes); 3319 if (of_property_read_bool(led, "active-low")) 3320 set_bit(PHY_LED_ACTIVE_LOW, &modes); 3321 if (of_property_read_bool(led, "inactive-high-impedance")) 3322 set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes); 3323 3324 if (WARN_ON(modes & BIT(PHY_LED_ACTIVE_LOW) && 3325 modes & BIT(PHY_LED_ACTIVE_HIGH))) 3326 return -EINVAL; 3327 3328 if (modes) { 3329 /* Return error if asked to set polarity modes but not supported */ 3330 if (!phydev->drv->led_polarity_set) 3331 return -EINVAL; 3332 3333 err = phydev->drv->led_polarity_set(phydev, index, modes); 3334 if (err) 3335 return err; 3336 } 3337 3338 phyled->index = index; 3339 if (phydev->drv->led_brightness_set) 3340 cdev->brightness_set_blocking = phy_led_set_brightness; 3341 if (phydev->drv->led_blink_set) 3342 cdev->blink_set = phy_led_blink_set; 3343 3344 #ifdef CONFIG_LEDS_TRIGGERS 3345 if (phydev->drv->led_hw_is_supported && 3346 phydev->drv->led_hw_control_set && 3347 phydev->drv->led_hw_control_get) { 3348 cdev->hw_control_is_supported = phy_led_hw_is_supported; 3349 cdev->hw_control_set = phy_led_hw_control_set; 3350 cdev->hw_control_get = phy_led_hw_control_get; 3351 cdev->hw_control_trigger = "netdev"; 3352 } 3353 3354 cdev->hw_control_get_device = phy_led_hw_control_get_device; 3355 #endif 3356 cdev->max_brightness = 1; 3357 init_data.devicename = dev_name(&phydev->mdio.dev); 3358 init_data.fwnode = of_fwnode_handle(led); 3359 init_data.devname_mandatory = true; 3360 3361 err = led_classdev_register_ext(dev, cdev, &init_data); 3362 if (err) 3363 return err; 3364 3365 list_add(&phyled->list, &phydev->leds); 3366 3367 return 0; 3368 } 3369 3370 static int of_phy_leds(struct phy_device *phydev) 3371 { 3372 struct device_node *node = phydev->mdio.dev.of_node; 3373 struct device_node *leds; 3374 int err; 3375 3376 if (!IS_ENABLED(CONFIG_OF_MDIO)) 3377 return 0; 3378 3379 if (!node) 3380 return 0; 3381 3382 leds = of_get_child_by_name(node, "leds"); 3383 if (!leds) 3384 return 0; 3385 3386 /* Check if the PHY driver have at least an OP to 3387 * set the LEDs. 3388 */ 3389 if (!(phydev->drv->led_brightness_set || 3390 phydev->drv->led_blink_set || 3391 phydev->drv->led_hw_control_set)) { 3392 phydev_dbg(phydev, "ignoring leds node defined with no PHY driver support\n"); 3393 goto exit; 3394 } 3395 3396 for_each_available_child_of_node_scoped(leds, led) { 3397 err = of_phy_led(phydev, led); 3398 if (err) { 3399 of_node_put(leds); 3400 phy_leds_unregister(phydev); 3401 return err; 3402 } 3403 } 3404 3405 exit: 3406 of_node_put(leds); 3407 return 0; 3408 } 3409 3410 static void phy_cleanup_ports(struct phy_device *phydev) 3411 { 3412 struct phy_port *tmp, *port; 3413 3414 list_for_each_entry_safe(port, tmp, &phydev->ports, head) { 3415 phy_del_port(phydev, port); 3416 phy_port_destroy(port); 3417 } 3418 } 3419 3420 static int phy_default_setup_single_port(struct phy_device *phydev) 3421 { 3422 struct phy_port *port = phy_port_alloc(); 3423 unsigned long mode; 3424 int ret; 3425 3426 if (!port) 3427 return -ENOMEM; 3428 3429 port->parent_type = PHY_PORT_PHY; 3430 port->phy = phydev; 3431 3432 /* Let the PHY driver know that this port was never described anywhere. 3433 * This is the usual case, where we assume single-port PHY devices with 3434 * no SFP. In that case, the port supports exactly the same thing as 3435 * the PHY itself. 3436 * 3437 * However, this can also be because we have a combo-port PHY, with 3438 * only one port described in DT, through SFP for example. 3439 * 3440 * In that case, the PHY driver will be in charge of saying what we can 3441 * do on that non-represented port. 3442 */ 3443 port->not_described = true; 3444 linkmode_copy(port->supported, phydev->supported); 3445 port->mediums = phy_caps_mediums_from_linkmodes(port->supported); 3446 3447 for_each_set_bit(mode, port->supported, __ETHTOOL_LINK_MODE_MASK_NBITS) 3448 port->pairs = max_t(int, port->pairs, 3449 ethtool_linkmode_n_pairs(mode)); 3450 3451 ret = phy_add_port(phydev, port); 3452 if (ret) 3453 phy_port_destroy(port); 3454 3455 return ret; 3456 } 3457 3458 static int of_phy_ports(struct phy_device *phydev) 3459 { 3460 struct device_node *node = phydev->mdio.dev.of_node; 3461 struct device_node *mdi; 3462 struct phy_port *port; 3463 int err; 3464 3465 if (!IS_ENABLED(CONFIG_OF_MDIO)) 3466 return 0; 3467 3468 if (!node) 3469 return 0; 3470 3471 mdi = of_get_child_by_name(node, "mdi"); 3472 if (!mdi) 3473 return 0; 3474 3475 for_each_available_child_of_node_scoped(mdi, port_node) { 3476 port = phy_of_parse_port(port_node); 3477 if (IS_ERR(port)) { 3478 err = PTR_ERR(port); 3479 goto out_err; 3480 } 3481 3482 port->parent_type = PHY_PORT_PHY; 3483 port->phy = phydev; 3484 3485 linkmode_copy(port->supported, phydev->supported); 3486 3487 err = phy_add_port(phydev, port); 3488 if (err) { 3489 phy_port_destroy(port); 3490 goto out_err; 3491 } 3492 } 3493 of_node_put(mdi); 3494 3495 return 0; 3496 3497 out_err: 3498 phy_cleanup_ports(phydev); 3499 of_node_put(mdi); 3500 return err; 3501 } 3502 3503 static int phy_setup_ports(struct phy_device *phydev) 3504 { 3505 __ETHTOOL_DECLARE_LINK_MODE_MASK(ports_supported); 3506 struct phy_port *port; 3507 int ret; 3508 3509 ret = of_phy_ports(phydev); 3510 if (ret) 3511 return ret; 3512 3513 /* We don't support SFP with genphy drivers. Also, genphy driver 3514 * binding occurs with RTNL help, which will deadlock the call to 3515 * sfp_bus_add_upstream(). 3516 */ 3517 if (!phydev->is_genphy_driven) { 3518 ret = phy_sfp_probe(phydev); 3519 if (ret) 3520 goto err_ports; 3521 } 3522 3523 if (phydev->n_ports < phydev->max_n_ports) { 3524 ret = phy_default_setup_single_port(phydev); 3525 if (ret) 3526 goto err_sfp; 3527 } 3528 3529 linkmode_zero(ports_supported); 3530 3531 /* Aggregate the supported modes, which are made-up of : 3532 * - What the PHY itself supports 3533 * - What the sum of all ports support 3534 */ 3535 list_for_each_entry(port, &phydev->ports, head) 3536 if (port->active) 3537 linkmode_or(ports_supported, ports_supported, 3538 port->supported); 3539 3540 if (!linkmode_empty(ports_supported)) 3541 linkmode_and(phydev->supported, phydev->supported, 3542 ports_supported); 3543 3544 /* For now, the phy->port field is set as the first active port's type */ 3545 list_for_each_entry(port, &phydev->ports, head) 3546 if (port->active) { 3547 phydev->port = phy_port_get_type(port); 3548 break; 3549 } 3550 3551 return 0; 3552 3553 err_sfp: 3554 phy_sfp_release(phydev); 3555 err_ports: 3556 phy_cleanup_ports(phydev); 3557 return ret; 3558 } 3559 3560 /** 3561 * phy_get_sfp_port() - Returns the first valid SFP port of a PHY 3562 * @phydev: pointer to the PHY device to get the SFP port from 3563 * 3564 * Returns: The first active SFP (serdes) port of a PHY device, NULL if none 3565 * exist. 3566 */ 3567 struct phy_port *phy_get_sfp_port(struct phy_device *phydev) 3568 { 3569 struct phy_port *port; 3570 3571 list_for_each_entry(port, &phydev->ports, head) 3572 if (port->active && port->is_sfp) 3573 return port; 3574 3575 return NULL; 3576 } 3577 EXPORT_SYMBOL_GPL(phy_get_sfp_port); 3578 3579 /** 3580 * fwnode_mdio_find_device - Given a fwnode, find the mdio_device 3581 * @fwnode: pointer to the mdio_device's fwnode 3582 * 3583 * If successful, returns a pointer to the mdio_device with the embedded 3584 * struct device refcount incremented by one, or NULL on failure. 3585 * The caller should call put_device() on the mdio_device after its use. 3586 */ 3587 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode) 3588 { 3589 struct device *d; 3590 3591 if (!fwnode) 3592 return NULL; 3593 3594 d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode); 3595 if (!d) 3596 return NULL; 3597 3598 return to_mdio_device(d); 3599 } 3600 EXPORT_SYMBOL(fwnode_mdio_find_device); 3601 3602 /** 3603 * fwnode_phy_find_device - For provided phy_fwnode, find phy_device. 3604 * 3605 * @phy_fwnode: Pointer to the phy's fwnode. 3606 * 3607 * If successful, returns a pointer to the phy_device with the embedded 3608 * struct device refcount incremented by one, or NULL on failure. 3609 */ 3610 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode) 3611 { 3612 struct mdio_device *mdiodev; 3613 3614 mdiodev = fwnode_mdio_find_device(phy_fwnode); 3615 if (!mdiodev) 3616 return NULL; 3617 3618 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) 3619 return to_phy_device(&mdiodev->dev); 3620 3621 put_device(&mdiodev->dev); 3622 3623 return NULL; 3624 } 3625 EXPORT_SYMBOL(fwnode_phy_find_device); 3626 3627 /** 3628 * fwnode_get_phy_node - Get the phy_node using the named reference. 3629 * @fwnode: Pointer to fwnode from which phy_node has to be obtained. 3630 * 3631 * Refer return conditions of fwnode_find_reference(). 3632 * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy" 3633 * and "phy-device" are not supported in ACPI. DT supports all the three 3634 * named references to the phy node. 3635 */ 3636 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode) 3637 { 3638 struct fwnode_handle *phy_node; 3639 3640 /* Only phy-handle is used for ACPI */ 3641 phy_node = fwnode_find_reference(fwnode, "phy-handle", 0); 3642 if (!IS_ERR(phy_node) || is_acpi_node(fwnode)) 3643 return phy_node; 3644 phy_node = fwnode_find_reference(fwnode, "phy", 0); 3645 if (!IS_ERR(phy_node)) 3646 return phy_node; 3647 return fwnode_find_reference(fwnode, "phy-device", 0); 3648 } 3649 EXPORT_SYMBOL_GPL(fwnode_get_phy_node); 3650 3651 /** 3652 * phy_probe - probe and init a PHY device 3653 * @dev: device to probe and init 3654 * 3655 * Take care of setting up the phy_device structure, set the state to READY. 3656 */ 3657 static int phy_probe(struct device *dev) 3658 { 3659 struct phy_device *phydev = to_phy_device(dev); 3660 struct device_driver *drv = phydev->mdio.dev.driver; 3661 struct phy_driver *phydrv = to_phy_driver(drv); 3662 int err = 0; 3663 3664 phydev->drv = phydrv; 3665 3666 /* Disable the interrupt if the PHY doesn't support it 3667 * but the interrupt is still a valid one 3668 */ 3669 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev)) 3670 phydev->irq = PHY_POLL; 3671 3672 if (phydrv->flags & PHY_IS_INTERNAL) 3673 phydev->is_internal = true; 3674 3675 /* Deassert the reset signal */ 3676 phy_device_reset(phydev, 0); 3677 3678 if (phydev->drv->probe) { 3679 err = phydev->drv->probe(phydev); 3680 if (err) 3681 goto out_reset; 3682 } 3683 3684 phy_disable_interrupts(phydev); 3685 3686 /* Start out supporting everything. Eventually, 3687 * a controller will attach, and may modify one 3688 * or both of these values 3689 */ 3690 if (phydrv->features) { 3691 linkmode_copy(phydev->supported, phydrv->features); 3692 genphy_c45_read_eee_abilities(phydev); 3693 } 3694 else if (phydrv->get_features) 3695 err = phydrv->get_features(phydev); 3696 else if (phydev->is_c45) 3697 err = genphy_c45_pma_read_abilities(phydev); 3698 else 3699 err = genphy_read_abilities(phydev); 3700 3701 if (err) 3702 goto out_remove; 3703 3704 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 3705 phydev->supported)) 3706 phydev->autoneg = 0; 3707 3708 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 3709 phydev->supported)) 3710 phydev->is_gigabit_capable = 1; 3711 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 3712 phydev->supported)) 3713 phydev->is_gigabit_capable = 1; 3714 3715 of_set_phy_supported(phydev); 3716 3717 err = phy_setup_ports(phydev); 3718 if (err) 3719 goto out_remove; 3720 3721 phy_advertise_supported(phydev); 3722 3723 /* Get PHY default EEE advertising modes and handle them as potentially 3724 * safe initial configuration. 3725 */ 3726 err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee); 3727 if (err) 3728 goto out_sfp_release; 3729 3730 /* Get the EEE modes we want to prohibit. */ 3731 of_set_phy_eee_broken(phydev); 3732 3733 /* Some PHYs may advertise, by default, not support EEE modes. So, 3734 * we need to clean them. In addition remove all disabled EEE modes. 3735 */ 3736 linkmode_and(phydev->advertising_eee, phydev->supported_eee, 3737 phydev->advertising_eee); 3738 linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee, 3739 phydev->eee_disabled_modes); 3740 3741 /* There is no "enabled" flag. If PHY is advertising, assume it is 3742 * kind of enabled. 3743 */ 3744 phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee); 3745 3746 /* Get master/slave strap overrides */ 3747 of_set_phy_timing_role(phydev); 3748 3749 /* The Pause Frame bits indicate that the PHY can support passing 3750 * pause frames. During autonegotiation, the PHYs will determine if 3751 * they should allow pause frames to pass. The MAC driver should then 3752 * use that result to determine whether to enable flow control via 3753 * pause frames. 3754 * 3755 * Normally, PHY drivers should not set the Pause bits, and instead 3756 * allow phylib to do that. However, there may be some situations 3757 * (e.g. hardware erratum) where the driver wants to set only one 3758 * of these bits. 3759 */ 3760 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) && 3761 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) { 3762 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 3763 phydev->supported); 3764 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 3765 phydev->supported); 3766 } 3767 3768 /* Register the PHY LED triggers */ 3769 if (!phydev->is_on_sfp_module) 3770 phy_led_triggers_register(phydev); 3771 3772 /* Get the LEDs from the device tree, and instantiate standard 3773 * LEDs for them. 3774 */ 3775 if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev)) { 3776 err = of_phy_leds(phydev); 3777 if (err) 3778 goto out_unreg_led_triggers; 3779 } 3780 3781 /* Set the state to READY by default */ 3782 phydev->state = PHY_READY; 3783 3784 return 0; 3785 3786 out_unreg_led_triggers: 3787 if (!phydev->is_on_sfp_module) 3788 phy_led_triggers_unregister(phydev); 3789 3790 out_sfp_release: 3791 phy_sfp_release(phydev); 3792 phy_cleanup_ports(phydev); 3793 3794 out_remove: 3795 if (phydev->drv->remove) 3796 phydev->drv->remove(phydev); 3797 3798 out_reset: 3799 /* Re-assert the reset signal on error */ 3800 phy_device_reset(phydev, 1); 3801 3802 return err; 3803 } 3804 3805 static int phy_remove(struct device *dev) 3806 { 3807 struct phy_device *phydev = to_phy_device(dev); 3808 3809 cancel_delayed_work_sync(&phydev->state_queue); 3810 3811 if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev)) 3812 phy_leds_unregister(phydev); 3813 3814 if (!phydev->is_on_sfp_module) 3815 phy_led_triggers_unregister(phydev); 3816 3817 phydev->state = PHY_DOWN; 3818 3819 phy_sfp_release(phydev); 3820 phy_cleanup_ports(phydev); 3821 3822 if (phydev->drv && phydev->drv->remove) 3823 phydev->drv->remove(phydev); 3824 3825 /* Assert the reset signal */ 3826 phy_device_reset(phydev, 1); 3827 3828 phydev->drv = NULL; 3829 3830 return 0; 3831 } 3832 3833 /** 3834 * phy_driver_register - register a phy_driver with the PHY layer 3835 * @new_driver: new phy_driver to register 3836 * @owner: module owning this PHY 3837 */ 3838 static int phy_driver_register(struct phy_driver *new_driver, 3839 struct module *owner) 3840 { 3841 int retval; 3842 3843 /* Either the features are hard coded, or dynamically 3844 * determined. It cannot be both. 3845 */ 3846 if (WARN_ON(new_driver->features && new_driver->get_features)) { 3847 pr_err("%s: features and get_features must not both be set\n", 3848 new_driver->name); 3849 return -EINVAL; 3850 } 3851 3852 /* PHYLIB device drivers must not match using a DT compatible table 3853 * as this bypasses our checks that the mdiodev that is being matched 3854 * is backed by a struct phy_device. If such a case happens, we will 3855 * make out-of-bounds accesses and lockup in phydev->lock. 3856 */ 3857 if (WARN(new_driver->mdiodrv.driver.of_match_table, 3858 "%s: driver must not provide a DT match table\n", 3859 new_driver->name)) 3860 return -EINVAL; 3861 3862 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY; 3863 new_driver->mdiodrv.driver.name = new_driver->name; 3864 new_driver->mdiodrv.driver.bus = &mdio_bus_type; 3865 new_driver->mdiodrv.driver.probe = phy_probe; 3866 new_driver->mdiodrv.driver.remove = phy_remove; 3867 new_driver->mdiodrv.driver.owner = owner; 3868 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS; 3869 3870 retval = driver_register(&new_driver->mdiodrv.driver); 3871 if (retval) { 3872 pr_err("%s: Error %d in registering driver\n", 3873 new_driver->name, retval); 3874 3875 return retval; 3876 } 3877 3878 pr_debug("%s: Registered new driver\n", new_driver->name); 3879 3880 return 0; 3881 } 3882 3883 static void phy_driver_unregister(struct phy_driver *drv) 3884 { 3885 driver_unregister(&drv->mdiodrv.driver); 3886 } 3887 3888 int phy_drivers_register(struct phy_driver *new_driver, int n, 3889 struct module *owner) 3890 { 3891 int i, ret = 0; 3892 3893 for (i = 0; i < n; i++) { 3894 ret = phy_driver_register(new_driver + i, owner); 3895 if (ret) { 3896 while (i-- > 0) 3897 phy_driver_unregister(new_driver + i); 3898 break; 3899 } 3900 } 3901 return ret; 3902 } 3903 EXPORT_SYMBOL(phy_drivers_register); 3904 3905 void phy_drivers_unregister(struct phy_driver *drv, int n) 3906 { 3907 int i; 3908 3909 for (i = 0; i < n; i++) 3910 phy_driver_unregister(drv + i); 3911 } 3912 EXPORT_SYMBOL(phy_drivers_unregister); 3913 3914 static struct phy_driver genphy_driver = { 3915 .phy_id = 0xffffffff, 3916 .phy_id_mask = 0xffffffff, 3917 .name = "Generic PHY", 3918 .get_features = genphy_read_abilities, 3919 .suspend = genphy_suspend, 3920 .resume = genphy_resume, 3921 .set_loopback = genphy_loopback, 3922 }; 3923 3924 static const struct ethtool_phy_ops phy_ethtool_phy_ops = { 3925 .get_sset_count = phy_ethtool_get_sset_count, 3926 .get_strings = phy_ethtool_get_strings, 3927 .get_stats = phy_ethtool_get_stats, 3928 .get_plca_cfg = phy_ethtool_get_plca_cfg, 3929 .set_plca_cfg = phy_ethtool_set_plca_cfg, 3930 .get_plca_status = phy_ethtool_get_plca_status, 3931 .start_cable_test = phy_start_cable_test, 3932 .start_cable_test_tdr = phy_start_cable_test_tdr, 3933 }; 3934 3935 static const struct phylib_stubs __phylib_stubs = { 3936 .hwtstamp_get = __phy_hwtstamp_get, 3937 .hwtstamp_set = __phy_hwtstamp_set, 3938 .get_phy_stats = __phy_ethtool_get_phy_stats, 3939 .get_link_ext_stats = __phy_ethtool_get_link_ext_stats, 3940 }; 3941 3942 static void phylib_register_stubs(void) 3943 { 3944 phylib_stubs = &__phylib_stubs; 3945 } 3946 3947 static void phylib_unregister_stubs(void) 3948 { 3949 phylib_stubs = NULL; 3950 } 3951 3952 static int __init phy_init(void) 3953 { 3954 int rc; 3955 3956 rc = class_register(&mdio_bus_class); 3957 if (rc) 3958 return rc; 3959 3960 rc = bus_register(&mdio_bus_type); 3961 if (rc) 3962 goto err_class; 3963 3964 rtnl_lock(); 3965 ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops); 3966 phylib_register_stubs(); 3967 rtnl_unlock(); 3968 3969 rc = phy_caps_init(); 3970 if (rc) 3971 goto err_ethtool_phy_ops; 3972 3973 features_init(); 3974 3975 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE); 3976 if (rc) 3977 goto err_ethtool_phy_ops; 3978 3979 rc = phy_driver_register(&genphy_driver, THIS_MODULE); 3980 if (rc) 3981 goto err_c45; 3982 3983 return 0; 3984 3985 err_c45: 3986 phy_driver_unregister(&genphy_c45_driver); 3987 err_ethtool_phy_ops: 3988 rtnl_lock(); 3989 phylib_unregister_stubs(); 3990 ethtool_set_ethtool_phy_ops(NULL); 3991 rtnl_unlock(); 3992 bus_unregister(&mdio_bus_type); 3993 err_class: 3994 class_unregister(&mdio_bus_class); 3995 3996 return rc; 3997 } 3998 3999 static void __exit phy_exit(void) 4000 { 4001 phy_driver_unregister(&genphy_c45_driver); 4002 phy_driver_unregister(&genphy_driver); 4003 rtnl_lock(); 4004 phylib_unregister_stubs(); 4005 ethtool_set_ethtool_phy_ops(NULL); 4006 rtnl_unlock(); 4007 bus_unregister(&mdio_bus_type); 4008 class_unregister(&mdio_bus_class); 4009 } 4010 4011 subsys_initcall(phy_init); 4012 module_exit(phy_exit); 4013