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