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 device_release_driver(&phydev->mdio.dev); 1741 phydev->is_genphy_driven = 0; 1742 } 1743 1744 /* Assert the reset signal */ 1745 phy_device_reset(phydev, 1); 1746 1747 /* 1748 * The phydev might go away on the put_device() below, so avoid 1749 * a use-after-free bug by reading the underlying bus first. 1750 */ 1751 bus = phydev->mdio.bus; 1752 1753 put_device(&phydev->mdio.dev); 1754 if (dev) 1755 ndev_owner = dev->dev.parent->driver->owner; 1756 if (ndev_owner != bus->owner) 1757 module_put(bus->owner); 1758 } 1759 1760 /** 1761 * phy_detach - detach a PHY device from its network device 1762 * @phydev: target phy_device struct 1763 * 1764 * This detaches the phy device from its network device and the phy 1765 * driver, and drops the reference count taken in phy_attach_direct(). 1766 */ 1767 void phy_detach(struct phy_device *phydev) 1768 { 1769 /* cleanup including bus notification */ 1770 phy_detach_internal(phydev, true); 1771 } 1772 EXPORT_SYMBOL(phy_detach); 1773 1774 /** 1775 * phy_attach_direct - attach a network device to a given PHY device pointer 1776 * @dev: network device to attach 1777 * @phydev: Pointer to phy_device to attach 1778 * @flags: PHY device's dev_flags 1779 * @interface: PHY device's interface 1780 * 1781 * Description: Called by drivers to attach to a particular PHY 1782 * device. The phy_device is found, and properly hooked up 1783 * to the phy_driver. If no driver is attached, then a 1784 * generic driver is used. The phy_device is given a ptr to 1785 * the attaching device, and given a callback for link status 1786 * change. The phy_device is returned to the attaching driver. 1787 * This function takes a reference on the phy device. 1788 */ 1789 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, 1790 u32 flags, phy_interface_t interface) 1791 { 1792 struct mii_bus *bus = phydev->mdio.bus; 1793 struct device *d = &phydev->mdio.dev; 1794 struct module *ndev_owner = NULL; 1795 int err; 1796 1797 /* For Ethernet device drivers that register their own MDIO bus, we 1798 * will have bus->owner match ndev_mod, so we do not want to increment 1799 * our own module->refcnt here, otherwise we would not be able to 1800 * unload later on. 1801 */ 1802 if (dev) 1803 ndev_owner = dev->dev.parent->driver->owner; 1804 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) { 1805 phydev_err(phydev, "failed to get the bus module\n"); 1806 return -EIO; 1807 } 1808 1809 get_device(d); 1810 1811 /* Assume that if there is no driver, that it doesn't 1812 * exist, and we should use the genphy driver. 1813 */ 1814 if (!d->driver) { 1815 if (phydev->is_c45) 1816 d->driver = &genphy_c45_driver.mdiodrv.driver; 1817 else 1818 d->driver = &genphy_driver.mdiodrv.driver; 1819 1820 phydev->is_genphy_driven = 1; 1821 } 1822 1823 if (!try_module_get(d->driver->owner)) { 1824 phydev_err(phydev, "failed to get the device driver module\n"); 1825 err = -EIO; 1826 goto error_put_device; 1827 } 1828 1829 if (phydev->is_genphy_driven) { 1830 err = d->driver->probe(d); 1831 if (err >= 0) 1832 err = device_bind_driver(d); 1833 1834 if (err) 1835 goto error_module_put; 1836 } 1837 1838 if (phydev->attached_dev) { 1839 dev_err(&dev->dev, "PHY already attached\n"); 1840 err = -EBUSY; 1841 goto error; 1842 } 1843 1844 phydev->phy_link_change = phy_link_change; 1845 if (dev) { 1846 phydev->attached_dev = dev; 1847 dev->phydev = phydev; 1848 1849 if (phydev->sfp_bus_attached) 1850 dev->sfp_bus = phydev->sfp_bus; 1851 1852 err = phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev); 1853 if (err) 1854 goto error; 1855 } 1856 1857 /* Some Ethernet drivers try to connect to a PHY device before 1858 * calling register_netdevice() -> netdev_register_kobject() and 1859 * does the dev->dev.kobj initialization. Here we only check for 1860 * success which indicates that the network device kobject is 1861 * ready. Once we do that we still need to keep track of whether 1862 * links were successfully set up or not for phy_detach() to 1863 * remove them accordingly. 1864 */ 1865 phydev->sysfs_links = false; 1866 1867 phy_sysfs_create_links(phydev); 1868 1869 if (!phydev->attached_dev) { 1870 err = sysfs_create_file(&phydev->mdio.dev.kobj, 1871 &dev_attr_phy_standalone.attr); 1872 if (err) 1873 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n"); 1874 } 1875 1876 phydev->dev_flags |= flags; 1877 1878 phydev->interface = interface; 1879 1880 phydev->state = PHY_READY; 1881 1882 phydev->interrupts = PHY_INTERRUPT_DISABLED; 1883 1884 /* PHYs can request to use poll mode even though they have an 1885 * associated interrupt line. This could be the case if they 1886 * detect a broken interrupt handling. 1887 */ 1888 if (phydev->dev_flags & PHY_F_NO_IRQ) 1889 phydev->irq = PHY_POLL; 1890 1891 if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev)) 1892 phydev->irq = PHY_POLL; 1893 1894 /* Port is set to PORT_TP by default and the actual PHY driver will set 1895 * it to different value depending on the PHY configuration. If we have 1896 * the generic PHY driver we can't figure it out, thus set the old 1897 * legacy PORT_MII value. 1898 */ 1899 if (phydev->is_genphy_driven) 1900 phydev->port = PORT_MII; 1901 1902 /* Initial carrier state is off as the phy is about to be 1903 * (re)initialized. 1904 */ 1905 if (dev) 1906 netif_carrier_off(phydev->attached_dev); 1907 1908 /* Do initial configuration here, now that 1909 * we have certain key parameters 1910 * (dev_flags and interface) 1911 */ 1912 err = phy_init_hw(phydev); 1913 if (err) 1914 goto error; 1915 1916 if (phydev->mdio.bus->notify_phy_attach) { 1917 err = phydev->mdio.bus->notify_phy_attach(phydev); 1918 if (err) 1919 goto error; 1920 } 1921 1922 phy_resume(phydev); 1923 1924 /** 1925 * If the external phy used by current mac interface is managed by 1926 * another mac interface, so we should create a device link between 1927 * phy dev and mac dev. 1928 */ 1929 if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent) 1930 phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev, 1931 DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS); 1932 1933 return err; 1934 1935 error: 1936 /* phy_detach_internal() does all of the cleanup below */ 1937 phy_detach_internal(phydev, false); 1938 return err; 1939 1940 error_module_put: 1941 module_put(d->driver->owner); 1942 phydev->is_genphy_driven = 0; 1943 d->driver = NULL; 1944 error_put_device: 1945 put_device(d); 1946 if (ndev_owner != bus->owner) 1947 module_put(bus->owner); 1948 return err; 1949 } 1950 EXPORT_SYMBOL(phy_attach_direct); 1951 1952 int phy_suspend(struct phy_device *phydev) 1953 { 1954 struct net_device *netdev = phydev->attached_dev; 1955 const struct phy_driver *phydrv = phydev->drv; 1956 int ret; 1957 1958 if (phydev->suspended || !phydrv) 1959 return 0; 1960 1961 phydev->wol_enabled = phy_may_wakeup(phydev) || 1962 (netdev && netdev->ethtool->wol_enabled); 1963 /* If the device has WOL enabled, we cannot suspend the PHY */ 1964 if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND)) 1965 return -EBUSY; 1966 1967 if (!phydrv->suspend) 1968 return 0; 1969 1970 ret = phydrv->suspend(phydev); 1971 if (!ret) 1972 phydev->suspended = true; 1973 1974 return ret; 1975 } 1976 EXPORT_SYMBOL(phy_suspend); 1977 1978 int __phy_resume(struct phy_device *phydev) 1979 { 1980 const struct phy_driver *phydrv = phydev->drv; 1981 int ret; 1982 1983 lockdep_assert_held(&phydev->lock); 1984 1985 if (!phydrv || !phydrv->resume) 1986 return 0; 1987 1988 ret = phydrv->resume(phydev); 1989 if (!ret) 1990 phydev->suspended = false; 1991 1992 return ret; 1993 } 1994 EXPORT_SYMBOL(__phy_resume); 1995 1996 int phy_resume(struct phy_device *phydev) 1997 { 1998 int ret; 1999 2000 mutex_lock(&phydev->lock); 2001 ret = __phy_resume(phydev); 2002 mutex_unlock(&phydev->lock); 2003 2004 return ret; 2005 } 2006 EXPORT_SYMBOL(phy_resume); 2007 2008 /** 2009 * phy_reset_after_clk_enable - perform a PHY reset if needed 2010 * @phydev: target phy_device struct 2011 * 2012 * Description: Some PHYs are known to need a reset after their refclk was 2013 * enabled. This function evaluates the flags and perform the reset if it's 2014 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy 2015 * was reset. 2016 */ 2017 int phy_reset_after_clk_enable(struct phy_device *phydev) 2018 { 2019 if (!phydev || !phydev->drv) 2020 return -ENODEV; 2021 2022 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) { 2023 phy_device_reset(phydev, 1); 2024 phy_device_reset(phydev, 0); 2025 return 1; 2026 } 2027 2028 return 0; 2029 } 2030 EXPORT_SYMBOL(phy_reset_after_clk_enable); 2031 2032 /* Generic PHY support and helper functions */ 2033 2034 /** 2035 * genphy_config_advert - sanitize and advertise auto-negotiation parameters 2036 * @phydev: target phy_device struct 2037 * @advert: auto-negotiation parameters to advertise 2038 * 2039 * Description: Writes MII_ADVERTISE with the appropriate values, 2040 * after sanitizing the values to make sure we only advertise 2041 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 2042 * hasn't changed, and > 0 if it has changed. 2043 */ 2044 static int genphy_config_advert(struct phy_device *phydev, 2045 const unsigned long *advert) 2046 { 2047 int err, bmsr, changed = 0; 2048 u32 adv; 2049 2050 adv = linkmode_adv_to_mii_adv_t(advert); 2051 2052 /* Setup standard advertisement */ 2053 err = phy_modify_changed(phydev, MII_ADVERTISE, 2054 ADVERTISE_ALL | ADVERTISE_100BASE4 | 2055 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM, 2056 adv); 2057 if (err < 0) 2058 return err; 2059 if (err > 0) 2060 changed = 1; 2061 2062 bmsr = phy_read(phydev, MII_BMSR); 2063 if (bmsr < 0) 2064 return bmsr; 2065 2066 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all 2067 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a 2068 * logical 1. 2069 */ 2070 if (!(bmsr & BMSR_ESTATEN)) 2071 return changed; 2072 2073 adv = linkmode_adv_to_mii_ctrl1000_t(advert); 2074 2075 err = phy_modify_changed(phydev, MII_CTRL1000, 2076 ADVERTISE_1000FULL | ADVERTISE_1000HALF, 2077 adv); 2078 if (err < 0) 2079 return err; 2080 if (err > 0) 2081 changed = 1; 2082 2083 return changed; 2084 } 2085 2086 /** 2087 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters 2088 * @phydev: target phy_device struct 2089 * 2090 * Description: Writes MII_ADVERTISE with the appropriate values, 2091 * after sanitizing the values to make sure we only advertise 2092 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 2093 * hasn't changed, and > 0 if it has changed. This function is intended 2094 * for Clause 37 1000Base-X mode. 2095 */ 2096 static int genphy_c37_config_advert(struct phy_device *phydev) 2097 { 2098 u16 adv = 0; 2099 2100 /* Only allow advertising what this PHY supports */ 2101 linkmode_and(phydev->advertising, phydev->advertising, 2102 phydev->supported); 2103 2104 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2105 phydev->advertising)) 2106 adv |= ADVERTISE_1000XFULL; 2107 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2108 phydev->advertising)) 2109 adv |= ADVERTISE_1000XPAUSE; 2110 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2111 phydev->advertising)) 2112 adv |= ADVERTISE_1000XPSE_ASYM; 2113 2114 return phy_modify_changed(phydev, MII_ADVERTISE, 2115 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | 2116 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM, 2117 adv); 2118 } 2119 2120 /** 2121 * genphy_setup_forced - configures/forces speed/duplex from @phydev 2122 * @phydev: target phy_device struct 2123 * 2124 * Description: Configures MII_BMCR to force speed/duplex 2125 * to the values in phydev. Assumes that the values are valid. 2126 * Please see phy_sanitize_settings(). 2127 */ 2128 int genphy_setup_forced(struct phy_device *phydev) 2129 { 2130 u16 ctl; 2131 2132 phydev->pause = false; 2133 phydev->asym_pause = false; 2134 2135 ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 2136 2137 return phy_modify(phydev, MII_BMCR, 2138 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl); 2139 } 2140 EXPORT_SYMBOL(genphy_setup_forced); 2141 2142 static int genphy_setup_master_slave(struct phy_device *phydev) 2143 { 2144 u16 ctl = 0; 2145 2146 if (!phydev->is_gigabit_capable) 2147 return 0; 2148 2149 switch (phydev->master_slave_set) { 2150 case MASTER_SLAVE_CFG_MASTER_PREFERRED: 2151 ctl |= CTL1000_PREFER_MASTER; 2152 break; 2153 case MASTER_SLAVE_CFG_SLAVE_PREFERRED: 2154 break; 2155 case MASTER_SLAVE_CFG_MASTER_FORCE: 2156 ctl |= CTL1000_AS_MASTER; 2157 fallthrough; 2158 case MASTER_SLAVE_CFG_SLAVE_FORCE: 2159 ctl |= CTL1000_ENABLE_MASTER; 2160 break; 2161 case MASTER_SLAVE_CFG_UNKNOWN: 2162 case MASTER_SLAVE_CFG_UNSUPPORTED: 2163 return 0; 2164 default: 2165 phydev_warn(phydev, "Unsupported Master/Slave mode\n"); 2166 return -EOPNOTSUPP; 2167 } 2168 2169 return phy_modify_changed(phydev, MII_CTRL1000, 2170 (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER | 2171 CTL1000_PREFER_MASTER), ctl); 2172 } 2173 2174 int genphy_read_master_slave(struct phy_device *phydev) 2175 { 2176 int cfg, state; 2177 int val; 2178 2179 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN; 2180 phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN; 2181 2182 val = phy_read(phydev, MII_CTRL1000); 2183 if (val < 0) 2184 return val; 2185 2186 if (val & CTL1000_ENABLE_MASTER) { 2187 if (val & CTL1000_AS_MASTER) 2188 cfg = MASTER_SLAVE_CFG_MASTER_FORCE; 2189 else 2190 cfg = MASTER_SLAVE_CFG_SLAVE_FORCE; 2191 } else { 2192 if (val & CTL1000_PREFER_MASTER) 2193 cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED; 2194 else 2195 cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED; 2196 } 2197 2198 val = phy_read(phydev, MII_STAT1000); 2199 if (val < 0) 2200 return val; 2201 2202 if (val & LPA_1000MSFAIL) { 2203 state = MASTER_SLAVE_STATE_ERR; 2204 } else if (phydev->link) { 2205 /* this bits are valid only for active link */ 2206 if (val & LPA_1000MSRES) 2207 state = MASTER_SLAVE_STATE_MASTER; 2208 else 2209 state = MASTER_SLAVE_STATE_SLAVE; 2210 } else { 2211 state = MASTER_SLAVE_STATE_UNKNOWN; 2212 } 2213 2214 phydev->master_slave_get = cfg; 2215 phydev->master_slave_state = state; 2216 2217 return 0; 2218 } 2219 EXPORT_SYMBOL(genphy_read_master_slave); 2220 2221 /** 2222 * genphy_restart_aneg - Enable and Restart Autonegotiation 2223 * @phydev: target phy_device struct 2224 */ 2225 int genphy_restart_aneg(struct phy_device *phydev) 2226 { 2227 /* Don't isolate the PHY if we're negotiating */ 2228 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, 2229 BMCR_ANENABLE | BMCR_ANRESTART); 2230 } 2231 EXPORT_SYMBOL(genphy_restart_aneg); 2232 2233 /** 2234 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation 2235 * @phydev: target phy_device struct 2236 * @restart: whether aneg restart is requested 2237 * 2238 * Check, and restart auto-negotiation if needed. 2239 */ 2240 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart) 2241 { 2242 int ret; 2243 2244 if (!restart) { 2245 /* Advertisement hasn't changed, but maybe aneg was never on to 2246 * begin with? Or maybe phy was isolated? 2247 */ 2248 ret = phy_read(phydev, MII_BMCR); 2249 if (ret < 0) 2250 return ret; 2251 2252 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE)) 2253 restart = true; 2254 } 2255 2256 if (restart) 2257 return genphy_restart_aneg(phydev); 2258 2259 return 0; 2260 } 2261 EXPORT_SYMBOL(genphy_check_and_restart_aneg); 2262 2263 /** 2264 * __genphy_config_aneg - restart auto-negotiation or write BMCR 2265 * @phydev: target phy_device struct 2266 * @changed: whether autoneg is requested 2267 * 2268 * Description: If auto-negotiation is enabled, we configure the 2269 * advertising, and then restart auto-negotiation. If it is not 2270 * enabled, then we write the BMCR. 2271 */ 2272 int __genphy_config_aneg(struct phy_device *phydev, bool changed) 2273 { 2274 __ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert); 2275 const struct link_capabilities *c; 2276 unsigned long *advert; 2277 int err; 2278 2279 err = genphy_c45_an_config_eee_aneg(phydev); 2280 if (err < 0) 2281 return err; 2282 else if (err) 2283 changed = true; 2284 2285 err = genphy_setup_master_slave(phydev); 2286 if (err < 0) 2287 return err; 2288 else if (err) 2289 changed = true; 2290 2291 if (phydev->autoneg == AUTONEG_ENABLE) { 2292 /* Only allow advertising what this PHY supports */ 2293 linkmode_and(phydev->advertising, phydev->advertising, 2294 phydev->supported); 2295 advert = phydev->advertising; 2296 } else if (phydev->speed < SPEED_1000) { 2297 return genphy_setup_forced(phydev); 2298 } else { 2299 linkmode_zero(fixed_advert); 2300 2301 c = phy_caps_lookup(phydev->speed, phydev->duplex, 2302 phydev->supported, true); 2303 if (c) 2304 linkmode_and(fixed_advert, phydev->supported, 2305 c->linkmodes); 2306 2307 advert = fixed_advert; 2308 } 2309 2310 err = genphy_config_advert(phydev, advert); 2311 if (err < 0) /* error */ 2312 return err; 2313 else if (err) 2314 changed = true; 2315 2316 return genphy_check_and_restart_aneg(phydev, changed); 2317 } 2318 EXPORT_SYMBOL(__genphy_config_aneg); 2319 2320 /** 2321 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR 2322 * @phydev: target phy_device struct 2323 * 2324 * Description: If auto-negotiation is enabled, we configure the 2325 * advertising, and then restart auto-negotiation. If it is not 2326 * enabled, then we write the BMCR. This function is intended 2327 * for use with Clause 37 1000Base-X mode. 2328 */ 2329 int genphy_c37_config_aneg(struct phy_device *phydev) 2330 { 2331 int err, changed; 2332 2333 if (phydev->autoneg != AUTONEG_ENABLE) 2334 return genphy_setup_forced(phydev); 2335 2336 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100, 2337 BMCR_SPEED1000); 2338 if (err) 2339 return err; 2340 2341 changed = genphy_c37_config_advert(phydev); 2342 if (changed < 0) /* error */ 2343 return changed; 2344 2345 if (!changed) { 2346 /* Advertisement hasn't changed, but maybe aneg was never on to 2347 * begin with? Or maybe phy was isolated? 2348 */ 2349 int ctl = phy_read(phydev, MII_BMCR); 2350 2351 if (ctl < 0) 2352 return ctl; 2353 2354 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) 2355 changed = 1; /* do restart aneg */ 2356 } 2357 2358 /* Only restart aneg if we are advertising something different 2359 * than we were before. 2360 */ 2361 if (changed > 0) 2362 return genphy_restart_aneg(phydev); 2363 2364 return 0; 2365 } 2366 EXPORT_SYMBOL(genphy_c37_config_aneg); 2367 2368 /** 2369 * genphy_aneg_done - return auto-negotiation status 2370 * @phydev: target phy_device struct 2371 * 2372 * Description: Reads the status register and returns 0 either if 2373 * auto-negotiation is incomplete, or if there was an error. 2374 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. 2375 */ 2376 int genphy_aneg_done(struct phy_device *phydev) 2377 { 2378 int retval = phy_read(phydev, MII_BMSR); 2379 2380 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); 2381 } 2382 EXPORT_SYMBOL(genphy_aneg_done); 2383 2384 /** 2385 * genphy_update_link - update link status in @phydev 2386 * @phydev: target phy_device struct 2387 * 2388 * Description: Update the value in phydev->link to reflect the 2389 * current link value. In order to do this, we need to read 2390 * the status register twice, keeping the second value. 2391 */ 2392 int genphy_update_link(struct phy_device *phydev) 2393 { 2394 int status = 0, bmcr; 2395 2396 bmcr = phy_read(phydev, MII_BMCR); 2397 if (bmcr < 0) 2398 return bmcr; 2399 2400 /* Autoneg is being started, therefore disregard BMSR value and 2401 * report link as down. 2402 */ 2403 if (bmcr & BMCR_ANRESTART) 2404 goto done; 2405 2406 /* The link state is latched low so that momentary link 2407 * drops can be detected. Do not double-read the status 2408 * in polling mode to detect such short link drops except 2409 * if the link was already down. 2410 */ 2411 if (!phy_polling_mode(phydev) || !phydev->link) { 2412 status = phy_read(phydev, MII_BMSR); 2413 if (status < 0) 2414 return status; 2415 else if (status & BMSR_LSTATUS) 2416 goto done; 2417 } 2418 2419 /* Read link and autonegotiation status */ 2420 status = phy_read(phydev, MII_BMSR); 2421 if (status < 0) 2422 return status; 2423 done: 2424 phydev->link = status & BMSR_LSTATUS ? 1 : 0; 2425 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0; 2426 2427 /* Consider the case that autoneg was started and "aneg complete" 2428 * bit has been reset, but "link up" bit not yet. 2429 */ 2430 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete) 2431 phydev->link = 0; 2432 2433 return 0; 2434 } 2435 EXPORT_SYMBOL(genphy_update_link); 2436 2437 int genphy_read_lpa(struct phy_device *phydev) 2438 { 2439 int lpa, lpagb; 2440 2441 if (phydev->autoneg == AUTONEG_ENABLE) { 2442 if (!phydev->autoneg_complete) { 2443 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 2444 0); 2445 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0); 2446 return 0; 2447 } 2448 2449 if (phydev->is_gigabit_capable) { 2450 lpagb = phy_read(phydev, MII_STAT1000); 2451 if (lpagb < 0) 2452 return lpagb; 2453 2454 if (lpagb & LPA_1000MSFAIL) { 2455 int adv = phy_read(phydev, MII_CTRL1000); 2456 2457 if (adv < 0) 2458 return adv; 2459 2460 if (adv & CTL1000_ENABLE_MASTER) 2461 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n"); 2462 else 2463 phydev_err(phydev, "Master/Slave resolution failed\n"); 2464 return -ENOLINK; 2465 } 2466 2467 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 2468 lpagb); 2469 } 2470 2471 lpa = phy_read(phydev, MII_LPA); 2472 if (lpa < 0) 2473 return lpa; 2474 2475 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa); 2476 } else { 2477 linkmode_zero(phydev->lp_advertising); 2478 } 2479 2480 return 0; 2481 } 2482 EXPORT_SYMBOL(genphy_read_lpa); 2483 2484 /** 2485 * genphy_read_status_fixed - read the link parameters for !aneg mode 2486 * @phydev: target phy_device struct 2487 * 2488 * Read the current duplex and speed state for a PHY operating with 2489 * autonegotiation disabled. 2490 */ 2491 int genphy_read_status_fixed(struct phy_device *phydev) 2492 { 2493 int bmcr = phy_read(phydev, MII_BMCR); 2494 2495 if (bmcr < 0) 2496 return bmcr; 2497 2498 if (bmcr & BMCR_FULLDPLX) 2499 phydev->duplex = DUPLEX_FULL; 2500 else 2501 phydev->duplex = DUPLEX_HALF; 2502 2503 if (bmcr & BMCR_SPEED1000) 2504 phydev->speed = SPEED_1000; 2505 else if (bmcr & BMCR_SPEED100) 2506 phydev->speed = SPEED_100; 2507 else 2508 phydev->speed = SPEED_10; 2509 2510 return 0; 2511 } 2512 EXPORT_SYMBOL(genphy_read_status_fixed); 2513 2514 /** 2515 * genphy_read_status - check the link status and update current link state 2516 * @phydev: target phy_device struct 2517 * 2518 * Description: Check the link, then figure out the current state 2519 * by comparing what we advertise with what the link partner 2520 * advertises. Start by checking the gigabit possibilities, 2521 * then move on to 10/100. 2522 */ 2523 int genphy_read_status(struct phy_device *phydev) 2524 { 2525 int err, old_link = phydev->link; 2526 2527 /* Update the link, but return if there was an error */ 2528 err = genphy_update_link(phydev); 2529 if (err) 2530 return err; 2531 2532 /* why bother the PHY if nothing can have changed */ 2533 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) 2534 return 0; 2535 2536 phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED; 2537 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED; 2538 phydev->speed = SPEED_UNKNOWN; 2539 phydev->duplex = DUPLEX_UNKNOWN; 2540 phydev->pause = false; 2541 phydev->asym_pause = false; 2542 2543 if (phydev->is_gigabit_capable) { 2544 err = genphy_read_master_slave(phydev); 2545 if (err < 0) 2546 return err; 2547 } 2548 2549 err = genphy_read_lpa(phydev); 2550 if (err < 0) 2551 return err; 2552 2553 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { 2554 phy_resolve_aneg_linkmode(phydev); 2555 } else if (phydev->autoneg == AUTONEG_DISABLE) { 2556 err = genphy_read_status_fixed(phydev); 2557 if (err < 0) 2558 return err; 2559 } 2560 2561 return 0; 2562 } 2563 EXPORT_SYMBOL(genphy_read_status); 2564 2565 /** 2566 * genphy_c37_read_status - check the link status and update current link state 2567 * @phydev: target phy_device struct 2568 * @changed: pointer where to store if link changed 2569 * 2570 * Description: Check the link, then figure out the current state 2571 * by comparing what we advertise with what the link partner 2572 * advertises. This function is for Clause 37 1000Base-X mode. 2573 * 2574 * If link has changed, @changed is set to true, false otherwise. 2575 */ 2576 int genphy_c37_read_status(struct phy_device *phydev, bool *changed) 2577 { 2578 int lpa, err, old_link = phydev->link; 2579 2580 /* Update the link, but return if there was an error */ 2581 err = genphy_update_link(phydev); 2582 if (err) 2583 return err; 2584 2585 /* why bother the PHY if nothing can have changed */ 2586 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) { 2587 *changed = false; 2588 return 0; 2589 } 2590 2591 /* Signal link has changed */ 2592 *changed = true; 2593 phydev->duplex = DUPLEX_UNKNOWN; 2594 phydev->pause = false; 2595 phydev->asym_pause = false; 2596 2597 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { 2598 lpa = phy_read(phydev, MII_LPA); 2599 if (lpa < 0) 2600 return lpa; 2601 2602 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 2603 phydev->lp_advertising, lpa & LPA_LPACK); 2604 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2605 phydev->lp_advertising, lpa & LPA_1000XFULL); 2606 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2607 phydev->lp_advertising, lpa & LPA_1000XPAUSE); 2608 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2609 phydev->lp_advertising, 2610 lpa & LPA_1000XPAUSE_ASYM); 2611 2612 phy_resolve_aneg_linkmode(phydev); 2613 } else if (phydev->autoneg == AUTONEG_DISABLE) { 2614 int bmcr = phy_read(phydev, MII_BMCR); 2615 2616 if (bmcr < 0) 2617 return bmcr; 2618 2619 if (bmcr & BMCR_FULLDPLX) 2620 phydev->duplex = DUPLEX_FULL; 2621 else 2622 phydev->duplex = DUPLEX_HALF; 2623 } 2624 2625 return 0; 2626 } 2627 EXPORT_SYMBOL(genphy_c37_read_status); 2628 2629 /** 2630 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit 2631 * @phydev: target phy_device struct 2632 * 2633 * Description: Perform a software PHY reset using the standard 2634 * BMCR_RESET bit and poll for the reset bit to be cleared. 2635 * 2636 * Returns: 0 on success, < 0 on failure 2637 */ 2638 int genphy_soft_reset(struct phy_device *phydev) 2639 { 2640 u16 res = BMCR_RESET; 2641 int ret; 2642 2643 if (phydev->autoneg == AUTONEG_ENABLE) 2644 res |= BMCR_ANRESTART; 2645 2646 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res); 2647 if (ret < 0) 2648 return ret; 2649 2650 /* Clause 22 states that setting bit BMCR_RESET sets control registers 2651 * to their default value. Therefore the POWER DOWN bit is supposed to 2652 * be cleared after soft reset. 2653 */ 2654 phydev->suspended = 0; 2655 2656 ret = phy_poll_reset(phydev); 2657 if (ret) 2658 return ret; 2659 2660 /* BMCR may be reset to defaults */ 2661 if (phydev->autoneg == AUTONEG_DISABLE) 2662 ret = genphy_setup_forced(phydev); 2663 2664 return ret; 2665 } 2666 EXPORT_SYMBOL(genphy_soft_reset); 2667 2668 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev) 2669 { 2670 /* It seems there are cases where the interrupts are handled by another 2671 * entity (ie an IRQ controller embedded inside the PHY) and do not 2672 * need any other interraction from phylib. In this case, just trigger 2673 * the state machine directly. 2674 */ 2675 phy_trigger_machine(phydev); 2676 2677 return 0; 2678 } 2679 EXPORT_SYMBOL(genphy_handle_interrupt_no_ack); 2680 2681 /** 2682 * genphy_read_abilities - read PHY abilities from Clause 22 registers 2683 * @phydev: target phy_device struct 2684 * 2685 * Description: Reads the PHY's abilities and populates 2686 * phydev->supported accordingly. 2687 * 2688 * Returns: 0 on success, < 0 on failure 2689 */ 2690 int genphy_read_abilities(struct phy_device *phydev) 2691 { 2692 int val; 2693 2694 linkmode_set_bit_array(phy_basic_ports_array, 2695 ARRAY_SIZE(phy_basic_ports_array), 2696 phydev->supported); 2697 2698 val = phy_read(phydev, MII_BMSR); 2699 if (val < 0) 2700 return val; 2701 2702 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported, 2703 val & BMSR_ANEGCAPABLE); 2704 2705 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported, 2706 val & BMSR_100FULL); 2707 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported, 2708 val & BMSR_100HALF); 2709 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported, 2710 val & BMSR_10FULL); 2711 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported, 2712 val & BMSR_10HALF); 2713 2714 if (val & BMSR_ESTATEN) { 2715 val = phy_read(phydev, MII_ESTATUS); 2716 if (val < 0) 2717 return val; 2718 2719 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 2720 phydev->supported, val & ESTATUS_1000_TFULL); 2721 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 2722 phydev->supported, val & ESTATUS_1000_THALF); 2723 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2724 phydev->supported, val & ESTATUS_1000_XFULL); 2725 } 2726 2727 /* This is optional functionality. If not supported, we may get an error 2728 * which should be ignored. 2729 */ 2730 genphy_c45_read_eee_abilities(phydev); 2731 2732 return 0; 2733 } 2734 EXPORT_SYMBOL(genphy_read_abilities); 2735 2736 /* Some PHYs support direct C45 register access but not C22 indirect 2737 * MMD access (registers 13 and 14). When discovered via C22, phylib 2738 * routes MMD access through the indirect path, which won't work on 2739 * these devices. These helpers bypass indirect access and use the bus 2740 * C45 accessors directly. 2741 */ 2742 int genphy_read_mmd_c45(struct phy_device *phydev, int devnum, u16 regnum) 2743 { 2744 struct mii_bus *bus = phydev->mdio.bus; 2745 int addr = phydev->mdio.addr; 2746 2747 return __mdiobus_c45_read(bus, addr, devnum, regnum); 2748 } 2749 EXPORT_SYMBOL(genphy_read_mmd_c45); 2750 2751 int genphy_write_mmd_c45(struct phy_device *phydev, int devnum, u16 regnum, 2752 u16 val) 2753 { 2754 struct mii_bus *bus = phydev->mdio.bus; 2755 int addr = phydev->mdio.addr; 2756 2757 return __mdiobus_c45_write(bus, addr, devnum, regnum, val); 2758 } 2759 EXPORT_SYMBOL(genphy_write_mmd_c45); 2760 2761 /* This is used for the phy device which doesn't support the MMD extended 2762 * register access, but it does have side effect when we are trying to access 2763 * the MMD register via indirect method. 2764 */ 2765 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum) 2766 { 2767 return -EOPNOTSUPP; 2768 } 2769 EXPORT_SYMBOL(genphy_read_mmd_unsupported); 2770 2771 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum, 2772 u16 regnum, u16 val) 2773 { 2774 return -EOPNOTSUPP; 2775 } 2776 EXPORT_SYMBOL(genphy_write_mmd_unsupported); 2777 2778 int genphy_suspend(struct phy_device *phydev) 2779 { 2780 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN); 2781 } 2782 EXPORT_SYMBOL(genphy_suspend); 2783 2784 int genphy_resume(struct phy_device *phydev) 2785 { 2786 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN); 2787 } 2788 EXPORT_SYMBOL(genphy_resume); 2789 2790 int genphy_loopback(struct phy_device *phydev, bool enable, int speed) 2791 { 2792 if (enable) { 2793 u16 ctl = BMCR_LOOPBACK; 2794 int ret, val; 2795 2796 if (speed == SPEED_10 || speed == SPEED_100 || 2797 speed == SPEED_1000) 2798 phydev->speed = speed; 2799 else if (speed) 2800 return -EINVAL; 2801 2802 ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 2803 2804 phy_modify(phydev, MII_BMCR, ~0, ctl); 2805 2806 ret = phy_read_poll_timeout(phydev, MII_BMSR, val, 2807 val & BMSR_LSTATUS, 2808 5000, 500000, true); 2809 if (ret) 2810 return ret; 2811 } else { 2812 phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0); 2813 2814 phy_config_aneg(phydev); 2815 } 2816 2817 return 0; 2818 } 2819 EXPORT_SYMBOL(genphy_loopback); 2820 2821 /** 2822 * phy_remove_link_mode - Remove a supported link mode 2823 * @phydev: phy_device structure to remove link mode from 2824 * @link_mode: Link mode to be removed 2825 * 2826 * Description: Some MACs don't support all link modes which the PHY 2827 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper 2828 * to remove a link mode. 2829 */ 2830 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode) 2831 { 2832 linkmode_clear_bit(link_mode, phydev->supported); 2833 phy_advertise_supported(phydev); 2834 } 2835 EXPORT_SYMBOL(phy_remove_link_mode); 2836 2837 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src) 2838 { 2839 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst, 2840 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src)); 2841 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst, 2842 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src)); 2843 } 2844 2845 /** 2846 * phy_advertise_supported - Advertise all supported modes 2847 * @phydev: target phy_device struct 2848 * 2849 * Description: Called to advertise all supported modes, doesn't touch 2850 * pause mode advertising. 2851 */ 2852 void phy_advertise_supported(struct phy_device *phydev) 2853 { 2854 __ETHTOOL_DECLARE_LINK_MODE_MASK(new); 2855 2856 linkmode_copy(new, phydev->supported); 2857 phy_copy_pause_bits(new, phydev->advertising); 2858 linkmode_copy(phydev->advertising, new); 2859 } 2860 EXPORT_SYMBOL(phy_advertise_supported); 2861 2862 /** 2863 * phy_advertise_eee_all - Advertise all supported EEE modes 2864 * @phydev: target phy_device struct 2865 * 2866 * Description: Per default phylib preserves the EEE advertising at the time of 2867 * phy probing, which might be a subset of the supported EEE modes. Use this 2868 * function when all supported EEE modes should be advertised. This does not 2869 * trigger auto-negotiation, so must be called before phy_start()/ 2870 * phylink_start() which will start auto-negotiation. 2871 */ 2872 void phy_advertise_eee_all(struct phy_device *phydev) 2873 { 2874 linkmode_andnot(phydev->advertising_eee, phydev->supported_eee, 2875 phydev->eee_disabled_modes); 2876 } 2877 EXPORT_SYMBOL_GPL(phy_advertise_eee_all); 2878 2879 /** 2880 * phy_support_eee - Set initial EEE policy configuration 2881 * @phydev: Target phy_device struct 2882 * 2883 * This function configures the initial policy for Energy Efficient Ethernet 2884 * (EEE) on the specified PHY device, influencing that EEE capabilities are 2885 * advertised before the link is established. It should be called during PHY 2886 * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs) 2887 * if MAC supports LPI or PHY is capable to compensate missing LPI functionality 2888 * of the MAC. 2889 * 2890 * The function sets default EEE policy parameters, including preparing the PHY 2891 * to advertise EEE capabilities based on hardware support. 2892 * 2893 * It also sets the expected configuration for Low Power Idle (LPI) in the MAC 2894 * driver. If the PHY framework determines that both local and remote 2895 * advertisements support EEE, and the negotiated link mode is compatible with 2896 * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on 2897 * this setting by enabling the LPI timer if enable_tx_lpi is set. 2898 */ 2899 void phy_support_eee(struct phy_device *phydev) 2900 { 2901 linkmode_andnot(phydev->advertising_eee, phydev->supported_eee, 2902 phydev->eee_disabled_modes); 2903 phydev->eee_cfg.tx_lpi_enabled = true; 2904 phydev->eee_cfg.eee_enabled = true; 2905 2906 /* If the PHY supports autonomous EEE, disable it so the MAC can 2907 * manage LPI signaling instead. The flag is stored so it can be 2908 * re-applied after a PHY soft reset (e.g. suspend/resume). 2909 */ 2910 if (phydev->drv && phydev->drv->disable_autonomous_eee) { 2911 int ret = phydev->drv->disable_autonomous_eee(phydev); 2912 2913 if (ret) 2914 phydev_warn(phydev, "Failed to disable autonomous EEE: %pe\n", 2915 ERR_PTR(ret)); 2916 else 2917 phydev->autonomous_eee_disabled = true; 2918 } 2919 } 2920 EXPORT_SYMBOL(phy_support_eee); 2921 2922 /** 2923 * phy_disable_eee - Disable EEE for the PHY 2924 * @phydev: Target phy_device struct 2925 * 2926 * This function is used by MAC drivers for MAC's which don't support EEE. 2927 * It disables EEE on the PHY layer. 2928 */ 2929 void phy_disable_eee(struct phy_device *phydev) 2930 { 2931 linkmode_zero(phydev->advertising_eee); 2932 phydev->eee_cfg.tx_lpi_enabled = false; 2933 phydev->eee_cfg.eee_enabled = false; 2934 /* don't let userspace re-enable EEE advertisement */ 2935 linkmode_fill(phydev->eee_disabled_modes); 2936 } 2937 EXPORT_SYMBOL_GPL(phy_disable_eee); 2938 2939 /** 2940 * phy_support_sym_pause - Enable support of symmetrical pause 2941 * @phydev: target phy_device struct 2942 * 2943 * Description: Called by the MAC to indicate is supports symmetrical 2944 * Pause, but not asym pause. 2945 */ 2946 void phy_support_sym_pause(struct phy_device *phydev) 2947 { 2948 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 2949 phy_copy_pause_bits(phydev->advertising, phydev->supported); 2950 } 2951 EXPORT_SYMBOL(phy_support_sym_pause); 2952 2953 /** 2954 * phy_support_asym_pause - Enable support of asym pause 2955 * @phydev: target phy_device struct 2956 * 2957 * Description: Called by the MAC to indicate is supports Asym Pause. 2958 */ 2959 void phy_support_asym_pause(struct phy_device *phydev) 2960 { 2961 phy_copy_pause_bits(phydev->advertising, phydev->supported); 2962 } 2963 EXPORT_SYMBOL(phy_support_asym_pause); 2964 2965 /** 2966 * phy_set_sym_pause - Configure symmetric Pause 2967 * @phydev: target phy_device struct 2968 * @rx: Receiver Pause is supported 2969 * @tx: Transmit Pause is supported 2970 * @autoneg: Auto neg should be used 2971 * 2972 * Description: Configure advertised Pause support depending on if 2973 * receiver pause and pause auto neg is supported. Generally called 2974 * from the set_pauseparam .ndo. 2975 */ 2976 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx, 2977 bool autoneg) 2978 { 2979 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 2980 2981 if (rx && tx && autoneg) 2982 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2983 phydev->supported); 2984 2985 linkmode_copy(phydev->advertising, phydev->supported); 2986 } 2987 EXPORT_SYMBOL(phy_set_sym_pause); 2988 2989 /** 2990 * phy_set_asym_pause - Configure Pause and Asym Pause 2991 * @phydev: target phy_device struct 2992 * @rx: Receiver Pause is supported 2993 * @tx: Transmit Pause is supported 2994 * 2995 * Description: Configure advertised Pause support depending on if 2996 * transmit and receiver pause is supported. If there has been a 2997 * change in adverting, trigger a new autoneg. Generally called from 2998 * the set_pauseparam .ndo. 2999 */ 3000 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx) 3001 { 3002 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv); 3003 3004 linkmode_copy(oldadv, phydev->advertising); 3005 linkmode_set_pause(phydev->advertising, tx, rx); 3006 3007 if (!linkmode_equal(oldadv, phydev->advertising) && 3008 phydev->autoneg) 3009 phy_start_aneg(phydev); 3010 } 3011 EXPORT_SYMBOL(phy_set_asym_pause); 3012 3013 /** 3014 * phy_validate_pause - Test if the PHY/MAC support the pause configuration 3015 * @phydev: phy_device struct 3016 * @pp: requested pause configuration 3017 * 3018 * Description: Test if the PHY/MAC combination supports the Pause 3019 * configuration the user is requesting. Returns True if it is 3020 * supported, false otherwise. 3021 */ 3022 bool phy_validate_pause(struct phy_device *phydev, 3023 struct ethtool_pauseparam *pp) 3024 { 3025 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 3026 phydev->supported) && pp->rx_pause) 3027 return false; 3028 3029 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 3030 phydev->supported) && 3031 pp->rx_pause != pp->tx_pause) 3032 return false; 3033 3034 return true; 3035 } 3036 EXPORT_SYMBOL(phy_validate_pause); 3037 3038 /** 3039 * phy_get_pause - resolve negotiated pause modes 3040 * @phydev: phy_device struct 3041 * @tx_pause: pointer to bool to indicate whether transmit pause should be 3042 * enabled. 3043 * @rx_pause: pointer to bool to indicate whether receive pause should be 3044 * enabled. 3045 * 3046 * Resolve and return the flow control modes according to the negotiation 3047 * result. This includes checking that we are operating in full duplex mode. 3048 * See linkmode_resolve_pause() for further details. 3049 */ 3050 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause) 3051 { 3052 if (phydev->duplex != DUPLEX_FULL) { 3053 *tx_pause = false; 3054 *rx_pause = false; 3055 return; 3056 } 3057 3058 return linkmode_resolve_pause(phydev->advertising, 3059 phydev->lp_advertising, 3060 tx_pause, rx_pause); 3061 } 3062 EXPORT_SYMBOL(phy_get_pause); 3063 3064 #if IS_ENABLED(CONFIG_OF_MDIO) 3065 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) 3066 { 3067 return device_property_read_u32(dev, name, val); 3068 } 3069 #else 3070 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) 3071 { 3072 return -EINVAL; 3073 } 3074 #endif 3075 3076 /** 3077 * phy_get_internal_delay - returns the index of the internal delay 3078 * @phydev: phy_device struct 3079 * @delay_values: array of delays the PHY supports 3080 * @size: the size of the delay array 3081 * @is_rx: boolean to indicate to get the rx internal delay 3082 * 3083 * Returns the index within the array of internal delay passed in. 3084 * If the device property is not present then the interface type is checked 3085 * if the interface defines use of internal delay then a 1 is returned otherwise 3086 * a 0 is returned. 3087 * The array must be in ascending order. If PHY does not have an ascending order 3088 * array then size = 0 and the value of the delay property is returned. 3089 * Return -EINVAL if the delay is invalid or cannot be found. 3090 */ 3091 s32 phy_get_internal_delay(struct phy_device *phydev, const int *delay_values, 3092 int size, bool is_rx) 3093 { 3094 struct device *dev = &phydev->mdio.dev; 3095 int i, ret; 3096 u32 delay; 3097 3098 if (is_rx) { 3099 ret = phy_get_u32_property(dev, "rx-internal-delay-ps", &delay); 3100 if (ret < 0 && size == 0) { 3101 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 3102 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) 3103 return 1; 3104 else 3105 return 0; 3106 } 3107 3108 } else { 3109 ret = phy_get_u32_property(dev, "tx-internal-delay-ps", &delay); 3110 if (ret < 0 && size == 0) { 3111 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 3112 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) 3113 return 1; 3114 else 3115 return 0; 3116 } 3117 } 3118 3119 if (ret < 0) 3120 return ret; 3121 3122 if (size == 0) 3123 return delay; 3124 3125 if (delay < delay_values[0] || delay > delay_values[size - 1]) { 3126 phydev_err(phydev, "Delay %d is out of range\n", delay); 3127 return -EINVAL; 3128 } 3129 3130 if (delay == delay_values[0]) 3131 return 0; 3132 3133 for (i = 1; i < size; i++) { 3134 if (delay == delay_values[i]) 3135 return i; 3136 3137 /* Find an approximate index by looking up the table */ 3138 if (delay > delay_values[i - 1] && 3139 delay < delay_values[i]) { 3140 if (delay - delay_values[i - 1] < 3141 delay_values[i] - delay) 3142 return i - 1; 3143 else 3144 return i; 3145 } 3146 } 3147 3148 phydev_err(phydev, "error finding internal delay index for %d\n", 3149 delay); 3150 3151 return -EINVAL; 3152 } 3153 EXPORT_SYMBOL(phy_get_internal_delay); 3154 3155 /** 3156 * phy_get_tx_amplitude_gain - stores tx amplitude gain in @val 3157 * @phydev: phy_device struct 3158 * @dev: pointer to the devices device struct 3159 * @linkmode: linkmode for which the tx amplitude gain should be retrieved 3160 * @val: tx amplitude gain 3161 * 3162 * Returns: 0 on success, < 0 on failure 3163 */ 3164 int phy_get_tx_amplitude_gain(struct phy_device *phydev, struct device *dev, 3165 enum ethtool_link_mode_bit_indices linkmode, 3166 u32 *val) 3167 { 3168 switch (linkmode) { 3169 case ETHTOOL_LINK_MODE_100baseT_Full_BIT: 3170 return phy_get_u32_property(dev, 3171 "tx-amplitude-100base-tx-percent", 3172 val); 3173 default: 3174 return -EINVAL; 3175 } 3176 } 3177 EXPORT_SYMBOL_GPL(phy_get_tx_amplitude_gain); 3178 3179 /** 3180 * phy_get_mac_termination - stores MAC termination in @val 3181 * @phydev: phy_device struct 3182 * @dev: pointer to the devices device struct 3183 * @val: MAC termination 3184 * 3185 * Returns: 0 on success, < 0 on failure 3186 */ 3187 int phy_get_mac_termination(struct phy_device *phydev, struct device *dev, 3188 u32 *val) 3189 { 3190 return phy_get_u32_property(dev, "mac-termination-ohms", val); 3191 } 3192 EXPORT_SYMBOL_GPL(phy_get_mac_termination); 3193 3194 static int phy_led_set_brightness(struct led_classdev *led_cdev, 3195 enum led_brightness value) 3196 { 3197 struct phy_led *phyled = to_phy_led(led_cdev); 3198 struct phy_device *phydev = phyled->phydev; 3199 int err; 3200 3201 mutex_lock(&phydev->lock); 3202 err = phydev->drv->led_brightness_set(phydev, phyled->index, value); 3203 mutex_unlock(&phydev->lock); 3204 3205 return err; 3206 } 3207 3208 static int phy_led_blink_set(struct led_classdev *led_cdev, 3209 unsigned long *delay_on, 3210 unsigned long *delay_off) 3211 { 3212 struct phy_led *phyled = to_phy_led(led_cdev); 3213 struct phy_device *phydev = phyled->phydev; 3214 int err; 3215 3216 mutex_lock(&phydev->lock); 3217 err = phydev->drv->led_blink_set(phydev, phyled->index, 3218 delay_on, delay_off); 3219 mutex_unlock(&phydev->lock); 3220 3221 return err; 3222 } 3223 3224 static __maybe_unused struct device * 3225 phy_led_hw_control_get_device(struct led_classdev *led_cdev) 3226 { 3227 struct phy_led *phyled = to_phy_led(led_cdev); 3228 struct phy_device *phydev = phyled->phydev; 3229 3230 if (phydev->attached_dev) 3231 return &phydev->attached_dev->dev; 3232 return NULL; 3233 } 3234 3235 static int __maybe_unused 3236 phy_led_hw_control_get(struct led_classdev *led_cdev, 3237 unsigned long *rules) 3238 { 3239 struct phy_led *phyled = to_phy_led(led_cdev); 3240 struct phy_device *phydev = phyled->phydev; 3241 int err; 3242 3243 mutex_lock(&phydev->lock); 3244 err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules); 3245 mutex_unlock(&phydev->lock); 3246 3247 return err; 3248 } 3249 3250 static int __maybe_unused 3251 phy_led_hw_control_set(struct led_classdev *led_cdev, 3252 unsigned long rules) 3253 { 3254 struct phy_led *phyled = to_phy_led(led_cdev); 3255 struct phy_device *phydev = phyled->phydev; 3256 int err; 3257 3258 mutex_lock(&phydev->lock); 3259 err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules); 3260 mutex_unlock(&phydev->lock); 3261 3262 return err; 3263 } 3264 3265 static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev, 3266 unsigned long rules) 3267 { 3268 struct phy_led *phyled = to_phy_led(led_cdev); 3269 struct phy_device *phydev = phyled->phydev; 3270 int err; 3271 3272 mutex_lock(&phydev->lock); 3273 err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules); 3274 mutex_unlock(&phydev->lock); 3275 3276 return err; 3277 } 3278 3279 static void phy_leds_unregister(struct phy_device *phydev) 3280 { 3281 struct phy_led *phyled, *tmp; 3282 3283 list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) { 3284 led_classdev_unregister(&phyled->led_cdev); 3285 list_del(&phyled->list); 3286 } 3287 } 3288 3289 static int of_phy_led(struct phy_device *phydev, 3290 struct device_node *led) 3291 { 3292 struct device *dev = &phydev->mdio.dev; 3293 struct led_init_data init_data = {}; 3294 struct led_classdev *cdev; 3295 unsigned long modes = 0; 3296 struct phy_led *phyled; 3297 u32 index; 3298 int err; 3299 3300 phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL); 3301 if (!phyled) 3302 return -ENOMEM; 3303 3304 cdev = &phyled->led_cdev; 3305 phyled->phydev = phydev; 3306 3307 err = of_property_read_u32(led, "reg", &index); 3308 if (err) 3309 return err; 3310 if (index > U8_MAX) 3311 return -EINVAL; 3312 3313 if (of_property_read_bool(led, "active-high")) 3314 set_bit(PHY_LED_ACTIVE_HIGH, &modes); 3315 if (of_property_read_bool(led, "active-low")) 3316 set_bit(PHY_LED_ACTIVE_LOW, &modes); 3317 if (of_property_read_bool(led, "inactive-high-impedance")) 3318 set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes); 3319 3320 if (WARN_ON(modes & BIT(PHY_LED_ACTIVE_LOW) && 3321 modes & BIT(PHY_LED_ACTIVE_HIGH))) 3322 return -EINVAL; 3323 3324 if (modes) { 3325 /* Return error if asked to set polarity modes but not supported */ 3326 if (!phydev->drv->led_polarity_set) 3327 return -EINVAL; 3328 3329 err = phydev->drv->led_polarity_set(phydev, index, modes); 3330 if (err) 3331 return err; 3332 } 3333 3334 phyled->index = index; 3335 if (phydev->drv->led_brightness_set) 3336 cdev->brightness_set_blocking = phy_led_set_brightness; 3337 if (phydev->drv->led_blink_set) 3338 cdev->blink_set = phy_led_blink_set; 3339 3340 #ifdef CONFIG_LEDS_TRIGGERS 3341 if (phydev->drv->led_hw_is_supported && 3342 phydev->drv->led_hw_control_set && 3343 phydev->drv->led_hw_control_get) { 3344 cdev->hw_control_is_supported = phy_led_hw_is_supported; 3345 cdev->hw_control_set = phy_led_hw_control_set; 3346 cdev->hw_control_get = phy_led_hw_control_get; 3347 cdev->hw_control_trigger = "netdev"; 3348 } 3349 3350 cdev->hw_control_get_device = phy_led_hw_control_get_device; 3351 #endif 3352 cdev->max_brightness = 1; 3353 init_data.devicename = dev_name(&phydev->mdio.dev); 3354 init_data.fwnode = of_fwnode_handle(led); 3355 init_data.devname_mandatory = true; 3356 3357 err = led_classdev_register_ext(dev, cdev, &init_data); 3358 if (err) 3359 return err; 3360 3361 list_add(&phyled->list, &phydev->leds); 3362 3363 return 0; 3364 } 3365 3366 static int of_phy_leds(struct phy_device *phydev) 3367 { 3368 struct device_node *node = phydev->mdio.dev.of_node; 3369 struct device_node *leds; 3370 int err; 3371 3372 if (!IS_ENABLED(CONFIG_OF_MDIO)) 3373 return 0; 3374 3375 if (!node) 3376 return 0; 3377 3378 leds = of_get_child_by_name(node, "leds"); 3379 if (!leds) 3380 return 0; 3381 3382 /* Check if the PHY driver have at least an OP to 3383 * set the LEDs. 3384 */ 3385 if (!(phydev->drv->led_brightness_set || 3386 phydev->drv->led_blink_set || 3387 phydev->drv->led_hw_control_set)) { 3388 phydev_dbg(phydev, "ignoring leds node defined with no PHY driver support\n"); 3389 goto exit; 3390 } 3391 3392 for_each_available_child_of_node_scoped(leds, led) { 3393 err = of_phy_led(phydev, led); 3394 if (err) { 3395 of_node_put(leds); 3396 phy_leds_unregister(phydev); 3397 return err; 3398 } 3399 } 3400 3401 exit: 3402 of_node_put(leds); 3403 return 0; 3404 } 3405 3406 static void phy_cleanup_ports(struct phy_device *phydev) 3407 { 3408 struct phy_port *tmp, *port; 3409 3410 list_for_each_entry_safe(port, tmp, &phydev->ports, head) { 3411 phy_del_port(phydev, port); 3412 phy_port_destroy(port); 3413 } 3414 } 3415 3416 static int phy_default_setup_single_port(struct phy_device *phydev) 3417 { 3418 struct phy_port *port = phy_port_alloc(); 3419 unsigned long mode; 3420 int ret; 3421 3422 if (!port) 3423 return -ENOMEM; 3424 3425 port->parent_type = PHY_PORT_PHY; 3426 port->phy = phydev; 3427 3428 /* Let the PHY driver know that this port was never described anywhere. 3429 * This is the usual case, where we assume single-port PHY devices with 3430 * no SFP. In that case, the port supports exactly the same thing as 3431 * the PHY itself. 3432 * 3433 * However, this can also be because we have a combo-port PHY, with 3434 * only one port described in DT, through SFP for example. 3435 * 3436 * In that case, the PHY driver will be in charge of saying what we can 3437 * do on that non-represented port. 3438 */ 3439 port->not_described = true; 3440 linkmode_copy(port->supported, phydev->supported); 3441 port->mediums = phy_caps_mediums_from_linkmodes(port->supported); 3442 3443 for_each_set_bit(mode, port->supported, __ETHTOOL_LINK_MODE_MASK_NBITS) 3444 port->pairs = max_t(int, port->pairs, 3445 ethtool_linkmode_n_pairs(mode)); 3446 3447 ret = phy_add_port(phydev, port); 3448 if (ret) 3449 phy_port_destroy(port); 3450 3451 return ret; 3452 } 3453 3454 static int of_phy_ports(struct phy_device *phydev) 3455 { 3456 struct device_node *node = phydev->mdio.dev.of_node; 3457 struct device_node *mdi; 3458 struct phy_port *port; 3459 int err; 3460 3461 if (!IS_ENABLED(CONFIG_OF_MDIO)) 3462 return 0; 3463 3464 if (!node) 3465 return 0; 3466 3467 mdi = of_get_child_by_name(node, "mdi"); 3468 if (!mdi) 3469 return 0; 3470 3471 for_each_available_child_of_node_scoped(mdi, port_node) { 3472 port = phy_of_parse_port(port_node); 3473 if (IS_ERR(port)) { 3474 err = PTR_ERR(port); 3475 goto out_err; 3476 } 3477 3478 port->parent_type = PHY_PORT_PHY; 3479 port->phy = phydev; 3480 3481 linkmode_copy(port->supported, phydev->supported); 3482 3483 err = phy_add_port(phydev, port); 3484 if (err) { 3485 phy_port_destroy(port); 3486 goto out_err; 3487 } 3488 } 3489 of_node_put(mdi); 3490 3491 return 0; 3492 3493 out_err: 3494 phy_cleanup_ports(phydev); 3495 of_node_put(mdi); 3496 return err; 3497 } 3498 3499 static int phy_setup_ports(struct phy_device *phydev) 3500 { 3501 __ETHTOOL_DECLARE_LINK_MODE_MASK(ports_supported); 3502 struct phy_port *port; 3503 int ret; 3504 3505 ret = of_phy_ports(phydev); 3506 if (ret) 3507 return ret; 3508 3509 /* We don't support SFP with genphy drivers. Also, genphy driver 3510 * binding occurs with RTNL help, which will deadlock the call to 3511 * sfp_bus_add_upstream(). 3512 */ 3513 if (!phydev->is_genphy_driven) { 3514 ret = phy_sfp_probe(phydev); 3515 if (ret) 3516 goto err_ports; 3517 } 3518 3519 if (phydev->n_ports < phydev->max_n_ports) { 3520 ret = phy_default_setup_single_port(phydev); 3521 if (ret) 3522 goto err_sfp; 3523 } 3524 3525 linkmode_zero(ports_supported); 3526 3527 /* Aggregate the supported modes, which are made-up of : 3528 * - What the PHY itself supports 3529 * - What the sum of all ports support 3530 */ 3531 list_for_each_entry(port, &phydev->ports, head) 3532 if (port->active) 3533 linkmode_or(ports_supported, ports_supported, 3534 port->supported); 3535 3536 if (!linkmode_empty(ports_supported)) 3537 linkmode_and(phydev->supported, phydev->supported, 3538 ports_supported); 3539 3540 /* For now, the phy->port field is set as the first active port's type */ 3541 list_for_each_entry(port, &phydev->ports, head) 3542 if (port->active) { 3543 phydev->port = phy_port_get_type(port); 3544 break; 3545 } 3546 3547 return 0; 3548 3549 err_sfp: 3550 phy_sfp_release(phydev); 3551 err_ports: 3552 phy_cleanup_ports(phydev); 3553 return ret; 3554 } 3555 3556 /** 3557 * phy_get_sfp_port() - Returns the first valid SFP port of a PHY 3558 * @phydev: pointer to the PHY device to get the SFP port from 3559 * 3560 * Returns: The first active SFP (serdes) port of a PHY device, NULL if none 3561 * exist. 3562 */ 3563 struct phy_port *phy_get_sfp_port(struct phy_device *phydev) 3564 { 3565 struct phy_port *port; 3566 3567 list_for_each_entry(port, &phydev->ports, head) 3568 if (port->active && port->is_sfp) 3569 return port; 3570 3571 return NULL; 3572 } 3573 EXPORT_SYMBOL_GPL(phy_get_sfp_port); 3574 3575 /** 3576 * fwnode_mdio_find_device - Given a fwnode, find the mdio_device 3577 * @fwnode: pointer to the mdio_device's fwnode 3578 * 3579 * If successful, returns a pointer to the mdio_device with the embedded 3580 * struct device refcount incremented by one, or NULL on failure. 3581 * The caller should call put_device() on the mdio_device after its use. 3582 */ 3583 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode) 3584 { 3585 struct device *d; 3586 3587 if (!fwnode) 3588 return NULL; 3589 3590 d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode); 3591 if (!d) 3592 return NULL; 3593 3594 return to_mdio_device(d); 3595 } 3596 EXPORT_SYMBOL(fwnode_mdio_find_device); 3597 3598 /** 3599 * fwnode_phy_find_device - For provided phy_fwnode, find phy_device. 3600 * 3601 * @phy_fwnode: Pointer to the phy's fwnode. 3602 * 3603 * If successful, returns a pointer to the phy_device with the embedded 3604 * struct device refcount incremented by one, or NULL on failure. 3605 */ 3606 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode) 3607 { 3608 struct mdio_device *mdiodev; 3609 3610 mdiodev = fwnode_mdio_find_device(phy_fwnode); 3611 if (!mdiodev) 3612 return NULL; 3613 3614 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) 3615 return to_phy_device(&mdiodev->dev); 3616 3617 put_device(&mdiodev->dev); 3618 3619 return NULL; 3620 } 3621 EXPORT_SYMBOL(fwnode_phy_find_device); 3622 3623 /** 3624 * fwnode_get_phy_node - Get the phy_node using the named reference. 3625 * @fwnode: Pointer to fwnode from which phy_node has to be obtained. 3626 * 3627 * Refer return conditions of fwnode_find_reference(). 3628 * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy" 3629 * and "phy-device" are not supported in ACPI. DT supports all the three 3630 * named references to the phy node. 3631 */ 3632 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode) 3633 { 3634 struct fwnode_handle *phy_node; 3635 3636 /* Only phy-handle is used for ACPI */ 3637 phy_node = fwnode_find_reference(fwnode, "phy-handle", 0); 3638 if (!IS_ERR(phy_node) || is_acpi_node(fwnode)) 3639 return phy_node; 3640 phy_node = fwnode_find_reference(fwnode, "phy", 0); 3641 if (!IS_ERR(phy_node)) 3642 return phy_node; 3643 return fwnode_find_reference(fwnode, "phy-device", 0); 3644 } 3645 EXPORT_SYMBOL_GPL(fwnode_get_phy_node); 3646 3647 /** 3648 * phy_probe - probe and init a PHY device 3649 * @dev: device to probe and init 3650 * 3651 * Take care of setting up the phy_device structure, set the state to READY. 3652 */ 3653 static int phy_probe(struct device *dev) 3654 { 3655 struct phy_device *phydev = to_phy_device(dev); 3656 struct device_driver *drv = phydev->mdio.dev.driver; 3657 struct phy_driver *phydrv = to_phy_driver(drv); 3658 int err = 0; 3659 3660 phydev->drv = phydrv; 3661 3662 /* Disable the interrupt if the PHY doesn't support it 3663 * but the interrupt is still a valid one 3664 */ 3665 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev)) 3666 phydev->irq = PHY_POLL; 3667 3668 if (phydrv->flags & PHY_IS_INTERNAL) 3669 phydev->is_internal = true; 3670 3671 /* Deassert the reset signal */ 3672 phy_device_reset(phydev, 0); 3673 3674 if (phydev->drv->probe) { 3675 err = phydev->drv->probe(phydev); 3676 if (err) 3677 goto out_reset; 3678 } 3679 3680 phy_disable_interrupts(phydev); 3681 3682 /* Start out supporting everything. Eventually, 3683 * a controller will attach, and may modify one 3684 * or both of these values 3685 */ 3686 if (phydrv->features) { 3687 linkmode_copy(phydev->supported, phydrv->features); 3688 genphy_c45_read_eee_abilities(phydev); 3689 } 3690 else if (phydrv->get_features) 3691 err = phydrv->get_features(phydev); 3692 else if (phydev->is_c45) 3693 err = genphy_c45_pma_read_abilities(phydev); 3694 else 3695 err = genphy_read_abilities(phydev); 3696 3697 if (err) 3698 goto out_remove; 3699 3700 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 3701 phydev->supported)) 3702 phydev->autoneg = 0; 3703 3704 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 3705 phydev->supported)) 3706 phydev->is_gigabit_capable = 1; 3707 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 3708 phydev->supported)) 3709 phydev->is_gigabit_capable = 1; 3710 3711 of_set_phy_supported(phydev); 3712 3713 err = phy_setup_ports(phydev); 3714 if (err) 3715 goto out_remove; 3716 3717 phy_advertise_supported(phydev); 3718 3719 /* Get PHY default EEE advertising modes and handle them as potentially 3720 * safe initial configuration. 3721 */ 3722 err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee); 3723 if (err) 3724 goto out_sfp_release; 3725 3726 /* Get the EEE modes we want to prohibit. */ 3727 of_set_phy_eee_broken(phydev); 3728 3729 /* Some PHYs may advertise, by default, not support EEE modes. So, 3730 * we need to clean them. In addition remove all disabled EEE modes. 3731 */ 3732 linkmode_and(phydev->advertising_eee, phydev->supported_eee, 3733 phydev->advertising_eee); 3734 linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee, 3735 phydev->eee_disabled_modes); 3736 3737 /* There is no "enabled" flag. If PHY is advertising, assume it is 3738 * kind of enabled. 3739 */ 3740 phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee); 3741 3742 /* Get master/slave strap overrides */ 3743 of_set_phy_timing_role(phydev); 3744 3745 /* The Pause Frame bits indicate that the PHY can support passing 3746 * pause frames. During autonegotiation, the PHYs will determine if 3747 * they should allow pause frames to pass. The MAC driver should then 3748 * use that result to determine whether to enable flow control via 3749 * pause frames. 3750 * 3751 * Normally, PHY drivers should not set the Pause bits, and instead 3752 * allow phylib to do that. However, there may be some situations 3753 * (e.g. hardware erratum) where the driver wants to set only one 3754 * of these bits. 3755 */ 3756 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) && 3757 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) { 3758 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 3759 phydev->supported); 3760 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 3761 phydev->supported); 3762 } 3763 3764 /* Register the PHY LED triggers */ 3765 if (!phydev->is_on_sfp_module) 3766 phy_led_triggers_register(phydev); 3767 3768 /* Get the LEDs from the device tree, and instantiate standard 3769 * LEDs for them. 3770 */ 3771 if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev)) { 3772 err = of_phy_leds(phydev); 3773 if (err) 3774 goto out_unreg_led_triggers; 3775 } 3776 3777 /* Set the state to READY by default */ 3778 phydev->state = PHY_READY; 3779 3780 return 0; 3781 3782 out_unreg_led_triggers: 3783 if (!phydev->is_on_sfp_module) 3784 phy_led_triggers_unregister(phydev); 3785 3786 out_sfp_release: 3787 phy_sfp_release(phydev); 3788 phy_cleanup_ports(phydev); 3789 3790 out_remove: 3791 if (phydev->drv->remove) 3792 phydev->drv->remove(phydev); 3793 3794 out_reset: 3795 /* Re-assert the reset signal on error */ 3796 phy_device_reset(phydev, 1); 3797 3798 return err; 3799 } 3800 3801 static int phy_remove(struct device *dev) 3802 { 3803 struct phy_device *phydev = to_phy_device(dev); 3804 3805 cancel_delayed_work_sync(&phydev->state_queue); 3806 3807 if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev)) 3808 phy_leds_unregister(phydev); 3809 3810 if (!phydev->is_on_sfp_module) 3811 phy_led_triggers_unregister(phydev); 3812 3813 phydev->state = PHY_DOWN; 3814 3815 phy_sfp_release(phydev); 3816 phy_cleanup_ports(phydev); 3817 3818 if (phydev->drv && phydev->drv->remove) 3819 phydev->drv->remove(phydev); 3820 3821 /* Assert the reset signal */ 3822 phy_device_reset(phydev, 1); 3823 3824 phydev->drv = NULL; 3825 3826 return 0; 3827 } 3828 3829 /** 3830 * phy_driver_register - register a phy_driver with the PHY layer 3831 * @new_driver: new phy_driver to register 3832 * @owner: module owning this PHY 3833 */ 3834 static int phy_driver_register(struct phy_driver *new_driver, 3835 struct module *owner) 3836 { 3837 int retval; 3838 3839 /* Either the features are hard coded, or dynamically 3840 * determined. It cannot be both. 3841 */ 3842 if (WARN_ON(new_driver->features && new_driver->get_features)) { 3843 pr_err("%s: features and get_features must not both be set\n", 3844 new_driver->name); 3845 return -EINVAL; 3846 } 3847 3848 /* PHYLIB device drivers must not match using a DT compatible table 3849 * as this bypasses our checks that the mdiodev that is being matched 3850 * is backed by a struct phy_device. If such a case happens, we will 3851 * make out-of-bounds accesses and lockup in phydev->lock. 3852 */ 3853 if (WARN(new_driver->mdiodrv.driver.of_match_table, 3854 "%s: driver must not provide a DT match table\n", 3855 new_driver->name)) 3856 return -EINVAL; 3857 3858 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY; 3859 new_driver->mdiodrv.driver.name = new_driver->name; 3860 new_driver->mdiodrv.driver.bus = &mdio_bus_type; 3861 new_driver->mdiodrv.driver.probe = phy_probe; 3862 new_driver->mdiodrv.driver.remove = phy_remove; 3863 new_driver->mdiodrv.driver.owner = owner; 3864 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS; 3865 3866 retval = driver_register(&new_driver->mdiodrv.driver); 3867 if (retval) { 3868 pr_err("%s: Error %d in registering driver\n", 3869 new_driver->name, retval); 3870 3871 return retval; 3872 } 3873 3874 pr_debug("%s: Registered new driver\n", new_driver->name); 3875 3876 return 0; 3877 } 3878 3879 static void phy_driver_unregister(struct phy_driver *drv) 3880 { 3881 driver_unregister(&drv->mdiodrv.driver); 3882 } 3883 3884 int phy_drivers_register(struct phy_driver *new_driver, int n, 3885 struct module *owner) 3886 { 3887 int i, ret = 0; 3888 3889 for (i = 0; i < n; i++) { 3890 ret = phy_driver_register(new_driver + i, owner); 3891 if (ret) { 3892 while (i-- > 0) 3893 phy_driver_unregister(new_driver + i); 3894 break; 3895 } 3896 } 3897 return ret; 3898 } 3899 EXPORT_SYMBOL(phy_drivers_register); 3900 3901 void phy_drivers_unregister(struct phy_driver *drv, int n) 3902 { 3903 int i; 3904 3905 for (i = 0; i < n; i++) 3906 phy_driver_unregister(drv + i); 3907 } 3908 EXPORT_SYMBOL(phy_drivers_unregister); 3909 3910 static struct phy_driver genphy_driver = { 3911 .phy_id = 0xffffffff, 3912 .phy_id_mask = 0xffffffff, 3913 .name = "Generic PHY", 3914 .get_features = genphy_read_abilities, 3915 .suspend = genphy_suspend, 3916 .resume = genphy_resume, 3917 .set_loopback = genphy_loopback, 3918 }; 3919 3920 static const struct ethtool_phy_ops phy_ethtool_phy_ops = { 3921 .get_sset_count = phy_ethtool_get_sset_count, 3922 .get_strings = phy_ethtool_get_strings, 3923 .get_stats = phy_ethtool_get_stats, 3924 .get_plca_cfg = phy_ethtool_get_plca_cfg, 3925 .set_plca_cfg = phy_ethtool_set_plca_cfg, 3926 .get_plca_status = phy_ethtool_get_plca_status, 3927 .start_cable_test = phy_start_cable_test, 3928 .start_cable_test_tdr = phy_start_cable_test_tdr, 3929 }; 3930 3931 static const struct phylib_stubs __phylib_stubs = { 3932 .hwtstamp_get = __phy_hwtstamp_get, 3933 .hwtstamp_set = __phy_hwtstamp_set, 3934 .get_phy_stats = __phy_ethtool_get_phy_stats, 3935 .get_link_ext_stats = __phy_ethtool_get_link_ext_stats, 3936 }; 3937 3938 static void phylib_register_stubs(void) 3939 { 3940 phylib_stubs = &__phylib_stubs; 3941 } 3942 3943 static void phylib_unregister_stubs(void) 3944 { 3945 phylib_stubs = NULL; 3946 } 3947 3948 static int __init phy_init(void) 3949 { 3950 int rc; 3951 3952 rc = class_register(&mdio_bus_class); 3953 if (rc) 3954 return rc; 3955 3956 rc = bus_register(&mdio_bus_type); 3957 if (rc) 3958 goto err_class; 3959 3960 rtnl_lock(); 3961 ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops); 3962 phylib_register_stubs(); 3963 rtnl_unlock(); 3964 3965 rc = phy_caps_init(); 3966 if (rc) 3967 goto err_ethtool_phy_ops; 3968 3969 features_init(); 3970 3971 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE); 3972 if (rc) 3973 goto err_ethtool_phy_ops; 3974 3975 rc = phy_driver_register(&genphy_driver, THIS_MODULE); 3976 if (rc) 3977 goto err_c45; 3978 3979 return 0; 3980 3981 err_c45: 3982 phy_driver_unregister(&genphy_c45_driver); 3983 err_ethtool_phy_ops: 3984 rtnl_lock(); 3985 phylib_unregister_stubs(); 3986 ethtool_set_ethtool_phy_ops(NULL); 3987 rtnl_unlock(); 3988 bus_unregister(&mdio_bus_type); 3989 err_class: 3990 class_unregister(&mdio_bus_class); 3991 3992 return rc; 3993 } 3994 3995 static void __exit phy_exit(void) 3996 { 3997 phy_driver_unregister(&genphy_c45_driver); 3998 phy_driver_unregister(&genphy_driver); 3999 rtnl_lock(); 4000 phylib_unregister_stubs(); 4001 ethtool_set_ethtool_phy_ops(NULL); 4002 rtnl_unlock(); 4003 bus_unregister(&mdio_bus_type); 4004 class_unregister(&mdio_bus_class); 4005 } 4006 4007 subsys_initcall(phy_init); 4008 module_exit(phy_exit); 4009