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 = 0; 829 dev->asym_pause = 0; 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_first - finds the first PHY device on the bus 1228 * @bus: the target MII bus 1229 */ 1230 struct phy_device *phy_find_first(struct mii_bus *bus) 1231 { 1232 struct phy_device *phydev; 1233 int addr; 1234 1235 for (addr = 0; addr < PHY_MAX_ADDR; addr++) { 1236 phydev = mdiobus_get_phy(bus, addr); 1237 if (phydev) 1238 return phydev; 1239 } 1240 return NULL; 1241 } 1242 EXPORT_SYMBOL(phy_find_first); 1243 1244 /** 1245 * phy_prepare_link - prepares the PHY layer to monitor link status 1246 * @phydev: target phy_device struct 1247 * @handler: callback function for link status change notifications 1248 * 1249 * Description: Tells the PHY infrastructure to handle the 1250 * gory details on monitoring link status (whether through 1251 * polling or an interrupt), and to call back to the 1252 * connected device driver when the link status changes. 1253 * If you want to monitor your own link state, don't call 1254 * this function. 1255 */ 1256 static void phy_prepare_link(struct phy_device *phydev, 1257 void (*handler)(struct net_device *)) 1258 { 1259 phydev->adjust_link = handler; 1260 } 1261 1262 /** 1263 * phy_connect_direct - connect an ethernet device to a specific phy_device 1264 * @dev: the network device to connect 1265 * @phydev: the pointer to the phy device 1266 * @handler: callback function for state change notifications 1267 * @interface: PHY device's interface 1268 */ 1269 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, 1270 void (*handler)(struct net_device *), 1271 phy_interface_t interface) 1272 { 1273 int rc; 1274 1275 if (!dev) 1276 return -EINVAL; 1277 1278 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); 1279 if (rc) 1280 return rc; 1281 1282 phy_prepare_link(phydev, handler); 1283 if (phy_interrupt_is_valid(phydev)) 1284 phy_request_interrupt(phydev); 1285 1286 return 0; 1287 } 1288 EXPORT_SYMBOL(phy_connect_direct); 1289 1290 /** 1291 * phy_connect - connect an ethernet device to a PHY device 1292 * @dev: the network device to connect 1293 * @bus_id: the id string of the PHY device to connect 1294 * @handler: callback function for state change notifications 1295 * @interface: PHY device's interface 1296 * 1297 * Description: Convenience function for connecting ethernet 1298 * devices to PHY devices. The default behavior is for 1299 * the PHY infrastructure to handle everything, and only notify 1300 * the connected driver when the link status changes. If you 1301 * don't want, or can't use the provided functionality, you may 1302 * choose to call only the subset of functions which provide 1303 * the desired functionality. 1304 */ 1305 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id, 1306 void (*handler)(struct net_device *), 1307 phy_interface_t interface) 1308 { 1309 struct phy_device *phydev; 1310 struct device *d; 1311 int rc; 1312 1313 /* Search the list of PHY devices on the mdio bus for the 1314 * PHY with the requested name 1315 */ 1316 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); 1317 if (!d) { 1318 pr_err("PHY %s not found\n", bus_id); 1319 return ERR_PTR(-ENODEV); 1320 } 1321 phydev = to_phy_device(d); 1322 1323 rc = phy_connect_direct(dev, phydev, handler, interface); 1324 put_device(d); 1325 if (rc) 1326 return ERR_PTR(rc); 1327 1328 return phydev; 1329 } 1330 EXPORT_SYMBOL(phy_connect); 1331 1332 /** 1333 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY 1334 * device 1335 * @phydev: target phy_device struct 1336 */ 1337 void phy_disconnect(struct phy_device *phydev) 1338 { 1339 if (phy_is_started(phydev)) 1340 phy_stop(phydev); 1341 1342 if (phy_interrupt_is_valid(phydev)) 1343 phy_free_interrupt(phydev); 1344 1345 phydev->adjust_link = NULL; 1346 1347 phy_detach(phydev); 1348 } 1349 EXPORT_SYMBOL(phy_disconnect); 1350 1351 /** 1352 * phy_poll_reset - Safely wait until a PHY reset has properly completed 1353 * @phydev: The PHY device to poll 1354 * 1355 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as 1356 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR 1357 * register must be polled until the BMCR_RESET bit clears. 1358 * 1359 * Furthermore, any attempts to write to PHY registers may have no effect 1360 * or even generate MDIO bus errors until this is complete. 1361 * 1362 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the 1363 * standard and do not fully reset after the BMCR_RESET bit is set, and may 1364 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an 1365 * effort to support such broken PHYs, this function is separate from the 1366 * standard phy_init_hw() which will zero all the other bits in the BMCR 1367 * and reapply all driver-specific and board-specific fixups. 1368 */ 1369 static int phy_poll_reset(struct phy_device *phydev) 1370 { 1371 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ 1372 int ret, val; 1373 1374 ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET), 1375 50000, 600000, true); 1376 if (ret) 1377 return ret; 1378 /* Some chips (smsc911x) may still need up to another 1ms after the 1379 * BMCR_RESET bit is cleared before they are usable. 1380 */ 1381 msleep(1); 1382 return 0; 1383 } 1384 1385 int phy_init_hw(struct phy_device *phydev) 1386 { 1387 int ret = 0; 1388 1389 /* Deassert the reset signal */ 1390 phy_device_reset(phydev, 0); 1391 1392 if (!phydev->drv) 1393 return 0; 1394 1395 if (phydev->drv->soft_reset) { 1396 ret = phydev->drv->soft_reset(phydev); 1397 if (ret < 0) 1398 return ret; 1399 1400 /* see comment in genphy_soft_reset for an explanation */ 1401 phydev->suspended = 0; 1402 } 1403 1404 ret = phy_scan_fixups(phydev); 1405 if (ret < 0) 1406 return ret; 1407 1408 phy_interface_zero(phydev->possible_interfaces); 1409 1410 if (phydev->drv->config_init) { 1411 ret = phydev->drv->config_init(phydev); 1412 if (ret < 0) 1413 return ret; 1414 } 1415 1416 if (phydev->drv->config_intr) { 1417 ret = phydev->drv->config_intr(phydev); 1418 if (ret < 0) 1419 return ret; 1420 } 1421 1422 return 0; 1423 } 1424 EXPORT_SYMBOL(phy_init_hw); 1425 1426 void phy_attached_info(struct phy_device *phydev) 1427 { 1428 phy_attached_print(phydev, NULL); 1429 } 1430 EXPORT_SYMBOL(phy_attached_info); 1431 1432 #define ATTACHED_FMT "attached PHY driver %s(mii_bus:phy_addr=%s, irq=%s)" 1433 char *phy_attached_info_irq(struct phy_device *phydev) 1434 { 1435 char *irq_str; 1436 char irq_num[8]; 1437 1438 switch(phydev->irq) { 1439 case PHY_POLL: 1440 irq_str = "POLL"; 1441 break; 1442 case PHY_MAC_INTERRUPT: 1443 irq_str = "MAC"; 1444 break; 1445 default: 1446 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq); 1447 irq_str = irq_num; 1448 break; 1449 } 1450 1451 return kasprintf(GFP_KERNEL, "%s", irq_str); 1452 } 1453 EXPORT_SYMBOL(phy_attached_info_irq); 1454 1455 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...) 1456 { 1457 const char *unbound = phydev->drv ? "" : "[unbound] "; 1458 char *irq_str = phy_attached_info_irq(phydev); 1459 1460 if (!fmt) { 1461 phydev_info(phydev, ATTACHED_FMT "\n", unbound, 1462 phydev_name(phydev), irq_str); 1463 } else { 1464 va_list ap; 1465 1466 phydev_info(phydev, ATTACHED_FMT, unbound, 1467 phydev_name(phydev), irq_str); 1468 1469 va_start(ap, fmt); 1470 vprintk(fmt, ap); 1471 va_end(ap); 1472 } 1473 kfree(irq_str); 1474 } 1475 EXPORT_SYMBOL(phy_attached_print); 1476 1477 static void phy_sysfs_create_links(struct phy_device *phydev) 1478 { 1479 struct net_device *dev = phydev->attached_dev; 1480 int err; 1481 1482 if (!dev) 1483 return; 1484 1485 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj, 1486 "attached_dev"); 1487 if (err) 1488 return; 1489 1490 err = sysfs_create_link_nowarn(&dev->dev.kobj, 1491 &phydev->mdio.dev.kobj, 1492 "phydev"); 1493 if (err) { 1494 dev_err(&dev->dev, "could not add device link to %s err %d\n", 1495 kobject_name(&phydev->mdio.dev.kobj), 1496 err); 1497 /* non-fatal - some net drivers can use one netdevice 1498 * with more then one phy 1499 */ 1500 } 1501 1502 phydev->sysfs_links = true; 1503 } 1504 1505 static ssize_t 1506 phy_standalone_show(struct device *dev, struct device_attribute *attr, 1507 char *buf) 1508 { 1509 struct phy_device *phydev = to_phy_device(dev); 1510 1511 return sysfs_emit(buf, "%d\n", !phydev->attached_dev); 1512 } 1513 static DEVICE_ATTR_RO(phy_standalone); 1514 1515 /** 1516 * phy_sfp_connect_phy - Connect the SFP module's PHY to the upstream PHY 1517 * @upstream: pointer to the upstream phy device 1518 * @phy: pointer to the SFP module's phy device 1519 * 1520 * This helper allows keeping track of PHY devices on the link. It adds the 1521 * SFP module's phy to the phy namespace of the upstream phy 1522 * 1523 * Return: 0 on success, otherwise a negative error code. 1524 */ 1525 int phy_sfp_connect_phy(void *upstream, struct phy_device *phy) 1526 { 1527 struct phy_device *phydev = upstream; 1528 struct net_device *dev = phydev->attached_dev; 1529 1530 if (dev) 1531 return phy_link_topo_add_phy(dev, phy, PHY_UPSTREAM_PHY, phydev); 1532 1533 return 0; 1534 } 1535 EXPORT_SYMBOL(phy_sfp_connect_phy); 1536 1537 /** 1538 * phy_sfp_disconnect_phy - Disconnect the SFP module's PHY from the upstream PHY 1539 * @upstream: pointer to the upstream phy device 1540 * @phy: pointer to the SFP module's phy device 1541 * 1542 * This helper allows keeping track of PHY devices on the link. It removes the 1543 * SFP module's phy to the phy namespace of the upstream phy. As the module phy 1544 * will be destroyed, re-inserting the same module will add a new phy with a 1545 * new index. 1546 */ 1547 void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy) 1548 { 1549 struct phy_device *phydev = upstream; 1550 struct net_device *dev = phydev->attached_dev; 1551 1552 if (dev) 1553 phy_link_topo_del_phy(dev, phy); 1554 } 1555 EXPORT_SYMBOL(phy_sfp_disconnect_phy); 1556 1557 /** 1558 * phy_sfp_attach - attach the SFP bus to the PHY upstream network device 1559 * @upstream: pointer to the phy device 1560 * @bus: sfp bus representing cage being attached 1561 * 1562 * This is used to fill in the sfp_upstream_ops .attach member. 1563 */ 1564 void phy_sfp_attach(void *upstream, struct sfp_bus *bus) 1565 { 1566 struct phy_device *phydev = upstream; 1567 1568 if (phydev->attached_dev) 1569 phydev->attached_dev->sfp_bus = bus; 1570 phydev->sfp_bus_attached = true; 1571 } 1572 EXPORT_SYMBOL(phy_sfp_attach); 1573 1574 /** 1575 * phy_sfp_detach - detach the SFP bus from the PHY upstream network device 1576 * @upstream: pointer to the phy device 1577 * @bus: sfp bus representing cage being attached 1578 * 1579 * This is used to fill in the sfp_upstream_ops .detach member. 1580 */ 1581 void phy_sfp_detach(void *upstream, struct sfp_bus *bus) 1582 { 1583 struct phy_device *phydev = upstream; 1584 1585 if (phydev->attached_dev) 1586 phydev->attached_dev->sfp_bus = NULL; 1587 phydev->sfp_bus_attached = false; 1588 } 1589 EXPORT_SYMBOL(phy_sfp_detach); 1590 1591 /** 1592 * phy_sfp_probe - probe for a SFP cage attached to this PHY device 1593 * @phydev: Pointer to phy_device 1594 * @ops: SFP's upstream operations 1595 */ 1596 int phy_sfp_probe(struct phy_device *phydev, 1597 const struct sfp_upstream_ops *ops) 1598 { 1599 struct sfp_bus *bus; 1600 int ret = 0; 1601 1602 if (phydev->mdio.dev.fwnode) { 1603 bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode); 1604 if (IS_ERR(bus)) 1605 return PTR_ERR(bus); 1606 1607 phydev->sfp_bus = bus; 1608 1609 ret = sfp_bus_add_upstream(bus, phydev, ops); 1610 sfp_bus_put(bus); 1611 } 1612 return ret; 1613 } 1614 EXPORT_SYMBOL(phy_sfp_probe); 1615 1616 static bool phy_drv_supports_irq(const struct phy_driver *phydrv) 1617 { 1618 return phydrv->config_intr && phydrv->handle_interrupt; 1619 } 1620 1621 /** 1622 * phy_attach_direct - attach a network device to a given PHY device pointer 1623 * @dev: network device to attach 1624 * @phydev: Pointer to phy_device to attach 1625 * @flags: PHY device's dev_flags 1626 * @interface: PHY device's interface 1627 * 1628 * Description: Called by drivers to attach to a particular PHY 1629 * device. The phy_device is found, and properly hooked up 1630 * to the phy_driver. If no driver is attached, then a 1631 * generic driver is used. The phy_device is given a ptr to 1632 * the attaching device, and given a callback for link status 1633 * change. The phy_device is returned to the attaching driver. 1634 * This function takes a reference on the phy device. 1635 */ 1636 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, 1637 u32 flags, phy_interface_t interface) 1638 { 1639 struct mii_bus *bus = phydev->mdio.bus; 1640 struct device *d = &phydev->mdio.dev; 1641 struct module *ndev_owner = NULL; 1642 int err; 1643 1644 /* For Ethernet device drivers that register their own MDIO bus, we 1645 * will have bus->owner match ndev_mod, so we do not want to increment 1646 * our own module->refcnt here, otherwise we would not be able to 1647 * unload later on. 1648 */ 1649 if (dev) 1650 ndev_owner = dev->dev.parent->driver->owner; 1651 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) { 1652 phydev_err(phydev, "failed to get the bus module\n"); 1653 return -EIO; 1654 } 1655 1656 get_device(d); 1657 1658 /* Assume that if there is no driver, that it doesn't 1659 * exist, and we should use the genphy driver. 1660 */ 1661 if (!d->driver) { 1662 if (phydev->is_c45) 1663 d->driver = &genphy_c45_driver.mdiodrv.driver; 1664 else 1665 d->driver = &genphy_driver.mdiodrv.driver; 1666 1667 phydev->is_genphy_driven = 1; 1668 } 1669 1670 if (!try_module_get(d->driver->owner)) { 1671 phydev_err(phydev, "failed to get the device driver module\n"); 1672 err = -EIO; 1673 goto error_put_device; 1674 } 1675 1676 if (phydev->is_genphy_driven) { 1677 err = d->driver->probe(d); 1678 if (err >= 0) 1679 err = device_bind_driver(d); 1680 1681 if (err) 1682 goto error_module_put; 1683 } 1684 1685 if (phydev->attached_dev) { 1686 dev_err(&dev->dev, "PHY already attached\n"); 1687 err = -EBUSY; 1688 goto error; 1689 } 1690 1691 phydev->phy_link_change = phy_link_change; 1692 if (dev) { 1693 phydev->attached_dev = dev; 1694 dev->phydev = phydev; 1695 1696 if (phydev->sfp_bus_attached) 1697 dev->sfp_bus = phydev->sfp_bus; 1698 1699 err = phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev); 1700 if (err) 1701 goto error; 1702 } 1703 1704 /* Some Ethernet drivers try to connect to a PHY device before 1705 * calling register_netdevice() -> netdev_register_kobject() and 1706 * does the dev->dev.kobj initialization. Here we only check for 1707 * success which indicates that the network device kobject is 1708 * ready. Once we do that we still need to keep track of whether 1709 * links were successfully set up or not for phy_detach() to 1710 * remove them accordingly. 1711 */ 1712 phydev->sysfs_links = false; 1713 1714 phy_sysfs_create_links(phydev); 1715 1716 if (!phydev->attached_dev) { 1717 err = sysfs_create_file(&phydev->mdio.dev.kobj, 1718 &dev_attr_phy_standalone.attr); 1719 if (err) 1720 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n"); 1721 } 1722 1723 phydev->dev_flags |= flags; 1724 1725 phydev->interface = interface; 1726 1727 phydev->state = PHY_READY; 1728 1729 phydev->interrupts = PHY_INTERRUPT_DISABLED; 1730 1731 /* PHYs can request to use poll mode even though they have an 1732 * associated interrupt line. This could be the case if they 1733 * detect a broken interrupt handling. 1734 */ 1735 if (phydev->dev_flags & PHY_F_NO_IRQ) 1736 phydev->irq = PHY_POLL; 1737 1738 if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev)) 1739 phydev->irq = PHY_POLL; 1740 1741 /* Port is set to PORT_TP by default and the actual PHY driver will set 1742 * it to different value depending on the PHY configuration. If we have 1743 * the generic PHY driver we can't figure it out, thus set the old 1744 * legacy PORT_MII value. 1745 */ 1746 if (phydev->is_genphy_driven) 1747 phydev->port = PORT_MII; 1748 1749 /* Initial carrier state is off as the phy is about to be 1750 * (re)initialized. 1751 */ 1752 if (dev) 1753 netif_carrier_off(phydev->attached_dev); 1754 1755 /* Do initial configuration here, now that 1756 * we have certain key parameters 1757 * (dev_flags and interface) 1758 */ 1759 err = phy_init_hw(phydev); 1760 if (err) 1761 goto error; 1762 1763 phy_resume(phydev); 1764 if (!phydev->is_on_sfp_module) 1765 phy_led_triggers_register(phydev); 1766 1767 /** 1768 * If the external phy used by current mac interface is managed by 1769 * another mac interface, so we should create a device link between 1770 * phy dev and mac dev. 1771 */ 1772 if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent) 1773 phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev, 1774 DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS); 1775 1776 return err; 1777 1778 error: 1779 /* phy_detach() does all of the cleanup below */ 1780 phy_detach(phydev); 1781 return err; 1782 1783 error_module_put: 1784 module_put(d->driver->owner); 1785 phydev->is_genphy_driven = 0; 1786 d->driver = NULL; 1787 error_put_device: 1788 put_device(d); 1789 if (ndev_owner != bus->owner) 1790 module_put(bus->owner); 1791 return err; 1792 } 1793 EXPORT_SYMBOL(phy_attach_direct); 1794 1795 /** 1796 * phy_attach - attach a network device to a particular PHY device 1797 * @dev: network device to attach 1798 * @bus_id: Bus ID of PHY device to attach 1799 * @interface: PHY device's interface 1800 * 1801 * Description: Same as phy_attach_direct() except that a PHY bus_id 1802 * string is passed instead of a pointer to a struct phy_device. 1803 */ 1804 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, 1805 phy_interface_t interface) 1806 { 1807 struct phy_device *phydev; 1808 struct device *d; 1809 int rc; 1810 1811 if (!dev) 1812 return ERR_PTR(-EINVAL); 1813 1814 /* Search the list of PHY devices on the mdio bus for the 1815 * PHY with the requested name 1816 */ 1817 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); 1818 if (!d) { 1819 pr_err("PHY %s not found\n", bus_id); 1820 return ERR_PTR(-ENODEV); 1821 } 1822 phydev = to_phy_device(d); 1823 1824 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); 1825 put_device(d); 1826 if (rc) 1827 return ERR_PTR(rc); 1828 1829 return phydev; 1830 } 1831 EXPORT_SYMBOL(phy_attach); 1832 1833 /** 1834 * phy_detach - detach a PHY device from its network device 1835 * @phydev: target phy_device struct 1836 * 1837 * This detaches the phy device from its network device and the phy 1838 * driver, and drops the reference count taken in phy_attach_direct(). 1839 */ 1840 void phy_detach(struct phy_device *phydev) 1841 { 1842 struct net_device *dev = phydev->attached_dev; 1843 struct module *ndev_owner = NULL; 1844 struct mii_bus *bus; 1845 1846 if (phydev->devlink) { 1847 device_link_del(phydev->devlink); 1848 phydev->devlink = NULL; 1849 } 1850 1851 if (phydev->sysfs_links) { 1852 if (dev) 1853 sysfs_remove_link(&dev->dev.kobj, "phydev"); 1854 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev"); 1855 } 1856 1857 if (!phydev->attached_dev) 1858 sysfs_remove_file(&phydev->mdio.dev.kobj, 1859 &dev_attr_phy_standalone.attr); 1860 1861 phy_suspend(phydev); 1862 if (dev) { 1863 struct hwtstamp_provider *hwprov; 1864 1865 hwprov = rtnl_dereference(dev->hwprov); 1866 /* Disable timestamp if it is the one selected */ 1867 if (hwprov && hwprov->phydev == phydev) { 1868 rcu_assign_pointer(dev->hwprov, NULL); 1869 kfree_rcu(hwprov, rcu_head); 1870 } 1871 1872 phydev->attached_dev->phydev = NULL; 1873 phydev->attached_dev = NULL; 1874 phy_link_topo_del_phy(dev, phydev); 1875 } 1876 1877 phydev->phy_link_change = NULL; 1878 phydev->phylink = NULL; 1879 1880 if (!phydev->is_on_sfp_module) 1881 phy_led_triggers_unregister(phydev); 1882 1883 if (phydev->mdio.dev.driver) 1884 module_put(phydev->mdio.dev.driver->owner); 1885 1886 /* If the device had no specific driver before (i.e. - it 1887 * was using the generic driver), we unbind the device 1888 * from the generic driver so that there's a chance a 1889 * real driver could be loaded 1890 */ 1891 if (phydev->is_genphy_driven) { 1892 device_release_driver(&phydev->mdio.dev); 1893 phydev->is_genphy_driven = 0; 1894 } 1895 1896 /* Assert the reset signal */ 1897 phy_device_reset(phydev, 1); 1898 1899 /* 1900 * The phydev might go away on the put_device() below, so avoid 1901 * a use-after-free bug by reading the underlying bus first. 1902 */ 1903 bus = phydev->mdio.bus; 1904 1905 put_device(&phydev->mdio.dev); 1906 if (dev) 1907 ndev_owner = dev->dev.parent->driver->owner; 1908 if (ndev_owner != bus->owner) 1909 module_put(bus->owner); 1910 } 1911 EXPORT_SYMBOL(phy_detach); 1912 1913 int phy_suspend(struct phy_device *phydev) 1914 { 1915 struct net_device *netdev = phydev->attached_dev; 1916 const struct phy_driver *phydrv = phydev->drv; 1917 int ret; 1918 1919 if (phydev->suspended || !phydrv) 1920 return 0; 1921 1922 phydev->wol_enabled = phy_may_wakeup(phydev) || 1923 (netdev && netdev->ethtool->wol_enabled); 1924 /* If the device has WOL enabled, we cannot suspend the PHY */ 1925 if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND)) 1926 return -EBUSY; 1927 1928 if (!phydrv->suspend) 1929 return 0; 1930 1931 ret = phydrv->suspend(phydev); 1932 if (!ret) 1933 phydev->suspended = true; 1934 1935 return ret; 1936 } 1937 EXPORT_SYMBOL(phy_suspend); 1938 1939 int __phy_resume(struct phy_device *phydev) 1940 { 1941 const struct phy_driver *phydrv = phydev->drv; 1942 int ret; 1943 1944 lockdep_assert_held(&phydev->lock); 1945 1946 if (!phydrv || !phydrv->resume) 1947 return 0; 1948 1949 ret = phydrv->resume(phydev); 1950 if (!ret) 1951 phydev->suspended = false; 1952 1953 return ret; 1954 } 1955 EXPORT_SYMBOL(__phy_resume); 1956 1957 int phy_resume(struct phy_device *phydev) 1958 { 1959 int ret; 1960 1961 mutex_lock(&phydev->lock); 1962 ret = __phy_resume(phydev); 1963 mutex_unlock(&phydev->lock); 1964 1965 return ret; 1966 } 1967 EXPORT_SYMBOL(phy_resume); 1968 1969 /** 1970 * phy_reset_after_clk_enable - perform a PHY reset if needed 1971 * @phydev: target phy_device struct 1972 * 1973 * Description: Some PHYs are known to need a reset after their refclk was 1974 * enabled. This function evaluates the flags and perform the reset if it's 1975 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy 1976 * was reset. 1977 */ 1978 int phy_reset_after_clk_enable(struct phy_device *phydev) 1979 { 1980 if (!phydev || !phydev->drv) 1981 return -ENODEV; 1982 1983 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) { 1984 phy_device_reset(phydev, 1); 1985 phy_device_reset(phydev, 0); 1986 return 1; 1987 } 1988 1989 return 0; 1990 } 1991 EXPORT_SYMBOL(phy_reset_after_clk_enable); 1992 1993 /* Generic PHY support and helper functions */ 1994 1995 /** 1996 * genphy_config_advert - sanitize and advertise auto-negotiation parameters 1997 * @phydev: target phy_device struct 1998 * @advert: auto-negotiation parameters to advertise 1999 * 2000 * Description: Writes MII_ADVERTISE with the appropriate values, 2001 * after sanitizing the values to make sure we only advertise 2002 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 2003 * hasn't changed, and > 0 if it has changed. 2004 */ 2005 static int genphy_config_advert(struct phy_device *phydev, 2006 const unsigned long *advert) 2007 { 2008 int err, bmsr, changed = 0; 2009 u32 adv; 2010 2011 adv = linkmode_adv_to_mii_adv_t(advert); 2012 2013 /* Setup standard advertisement */ 2014 err = phy_modify_changed(phydev, MII_ADVERTISE, 2015 ADVERTISE_ALL | ADVERTISE_100BASE4 | 2016 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM, 2017 adv); 2018 if (err < 0) 2019 return err; 2020 if (err > 0) 2021 changed = 1; 2022 2023 bmsr = phy_read(phydev, MII_BMSR); 2024 if (bmsr < 0) 2025 return bmsr; 2026 2027 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all 2028 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a 2029 * logical 1. 2030 */ 2031 if (!(bmsr & BMSR_ESTATEN)) 2032 return changed; 2033 2034 adv = linkmode_adv_to_mii_ctrl1000_t(advert); 2035 2036 err = phy_modify_changed(phydev, MII_CTRL1000, 2037 ADVERTISE_1000FULL | ADVERTISE_1000HALF, 2038 adv); 2039 if (err < 0) 2040 return err; 2041 if (err > 0) 2042 changed = 1; 2043 2044 return changed; 2045 } 2046 2047 /** 2048 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters 2049 * @phydev: target phy_device struct 2050 * 2051 * Description: Writes MII_ADVERTISE with the appropriate values, 2052 * after sanitizing the values to make sure we only advertise 2053 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 2054 * hasn't changed, and > 0 if it has changed. This function is intended 2055 * for Clause 37 1000Base-X mode. 2056 */ 2057 static int genphy_c37_config_advert(struct phy_device *phydev) 2058 { 2059 u16 adv = 0; 2060 2061 /* Only allow advertising what this PHY supports */ 2062 linkmode_and(phydev->advertising, phydev->advertising, 2063 phydev->supported); 2064 2065 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2066 phydev->advertising)) 2067 adv |= ADVERTISE_1000XFULL; 2068 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2069 phydev->advertising)) 2070 adv |= ADVERTISE_1000XPAUSE; 2071 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2072 phydev->advertising)) 2073 adv |= ADVERTISE_1000XPSE_ASYM; 2074 2075 return phy_modify_changed(phydev, MII_ADVERTISE, 2076 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | 2077 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM, 2078 adv); 2079 } 2080 2081 /** 2082 * genphy_setup_forced - configures/forces speed/duplex from @phydev 2083 * @phydev: target phy_device struct 2084 * 2085 * Description: Configures MII_BMCR to force speed/duplex 2086 * to the values in phydev. Assumes that the values are valid. 2087 * Please see phy_sanitize_settings(). 2088 */ 2089 int genphy_setup_forced(struct phy_device *phydev) 2090 { 2091 u16 ctl; 2092 2093 phydev->pause = 0; 2094 phydev->asym_pause = 0; 2095 2096 ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 2097 2098 return phy_modify(phydev, MII_BMCR, 2099 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl); 2100 } 2101 EXPORT_SYMBOL(genphy_setup_forced); 2102 2103 static int genphy_setup_master_slave(struct phy_device *phydev) 2104 { 2105 u16 ctl = 0; 2106 2107 if (!phydev->is_gigabit_capable) 2108 return 0; 2109 2110 switch (phydev->master_slave_set) { 2111 case MASTER_SLAVE_CFG_MASTER_PREFERRED: 2112 ctl |= CTL1000_PREFER_MASTER; 2113 break; 2114 case MASTER_SLAVE_CFG_SLAVE_PREFERRED: 2115 break; 2116 case MASTER_SLAVE_CFG_MASTER_FORCE: 2117 ctl |= CTL1000_AS_MASTER; 2118 fallthrough; 2119 case MASTER_SLAVE_CFG_SLAVE_FORCE: 2120 ctl |= CTL1000_ENABLE_MASTER; 2121 break; 2122 case MASTER_SLAVE_CFG_UNKNOWN: 2123 case MASTER_SLAVE_CFG_UNSUPPORTED: 2124 return 0; 2125 default: 2126 phydev_warn(phydev, "Unsupported Master/Slave mode\n"); 2127 return -EOPNOTSUPP; 2128 } 2129 2130 return phy_modify_changed(phydev, MII_CTRL1000, 2131 (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER | 2132 CTL1000_PREFER_MASTER), ctl); 2133 } 2134 2135 int genphy_read_master_slave(struct phy_device *phydev) 2136 { 2137 int cfg, state; 2138 int val; 2139 2140 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN; 2141 phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN; 2142 2143 val = phy_read(phydev, MII_CTRL1000); 2144 if (val < 0) 2145 return val; 2146 2147 if (val & CTL1000_ENABLE_MASTER) { 2148 if (val & CTL1000_AS_MASTER) 2149 cfg = MASTER_SLAVE_CFG_MASTER_FORCE; 2150 else 2151 cfg = MASTER_SLAVE_CFG_SLAVE_FORCE; 2152 } else { 2153 if (val & CTL1000_PREFER_MASTER) 2154 cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED; 2155 else 2156 cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED; 2157 } 2158 2159 val = phy_read(phydev, MII_STAT1000); 2160 if (val < 0) 2161 return val; 2162 2163 if (val & LPA_1000MSFAIL) { 2164 state = MASTER_SLAVE_STATE_ERR; 2165 } else if (phydev->link) { 2166 /* this bits are valid only for active link */ 2167 if (val & LPA_1000MSRES) 2168 state = MASTER_SLAVE_STATE_MASTER; 2169 else 2170 state = MASTER_SLAVE_STATE_SLAVE; 2171 } else { 2172 state = MASTER_SLAVE_STATE_UNKNOWN; 2173 } 2174 2175 phydev->master_slave_get = cfg; 2176 phydev->master_slave_state = state; 2177 2178 return 0; 2179 } 2180 EXPORT_SYMBOL(genphy_read_master_slave); 2181 2182 /** 2183 * genphy_restart_aneg - Enable and Restart Autonegotiation 2184 * @phydev: target phy_device struct 2185 */ 2186 int genphy_restart_aneg(struct phy_device *phydev) 2187 { 2188 /* Don't isolate the PHY if we're negotiating */ 2189 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, 2190 BMCR_ANENABLE | BMCR_ANRESTART); 2191 } 2192 EXPORT_SYMBOL(genphy_restart_aneg); 2193 2194 /** 2195 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation 2196 * @phydev: target phy_device struct 2197 * @restart: whether aneg restart is requested 2198 * 2199 * Check, and restart auto-negotiation if needed. 2200 */ 2201 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart) 2202 { 2203 int ret; 2204 2205 if (!restart) { 2206 /* Advertisement hasn't changed, but maybe aneg was never on to 2207 * begin with? Or maybe phy was isolated? 2208 */ 2209 ret = phy_read(phydev, MII_BMCR); 2210 if (ret < 0) 2211 return ret; 2212 2213 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE)) 2214 restart = true; 2215 } 2216 2217 if (restart) 2218 return genphy_restart_aneg(phydev); 2219 2220 return 0; 2221 } 2222 EXPORT_SYMBOL(genphy_check_and_restart_aneg); 2223 2224 /** 2225 * __genphy_config_aneg - restart auto-negotiation or write BMCR 2226 * @phydev: target phy_device struct 2227 * @changed: whether autoneg is requested 2228 * 2229 * Description: If auto-negotiation is enabled, we configure the 2230 * advertising, and then restart auto-negotiation. If it is not 2231 * enabled, then we write the BMCR. 2232 */ 2233 int __genphy_config_aneg(struct phy_device *phydev, bool changed) 2234 { 2235 __ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert); 2236 const struct link_capabilities *c; 2237 unsigned long *advert; 2238 int err; 2239 2240 err = genphy_c45_an_config_eee_aneg(phydev); 2241 if (err < 0) 2242 return err; 2243 else if (err) 2244 changed = true; 2245 2246 err = genphy_setup_master_slave(phydev); 2247 if (err < 0) 2248 return err; 2249 else if (err) 2250 changed = true; 2251 2252 if (phydev->autoneg == AUTONEG_ENABLE) { 2253 /* Only allow advertising what this PHY supports */ 2254 linkmode_and(phydev->advertising, phydev->advertising, 2255 phydev->supported); 2256 advert = phydev->advertising; 2257 } else if (phydev->speed < SPEED_1000) { 2258 return genphy_setup_forced(phydev); 2259 } else { 2260 linkmode_zero(fixed_advert); 2261 2262 c = phy_caps_lookup(phydev->speed, phydev->duplex, 2263 phydev->supported, true); 2264 if (c) 2265 linkmode_and(fixed_advert, phydev->supported, 2266 c->linkmodes); 2267 2268 advert = fixed_advert; 2269 } 2270 2271 err = genphy_config_advert(phydev, advert); 2272 if (err < 0) /* error */ 2273 return err; 2274 else if (err) 2275 changed = true; 2276 2277 return genphy_check_and_restart_aneg(phydev, changed); 2278 } 2279 EXPORT_SYMBOL(__genphy_config_aneg); 2280 2281 /** 2282 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR 2283 * @phydev: target phy_device struct 2284 * 2285 * Description: If auto-negotiation is enabled, we configure the 2286 * advertising, and then restart auto-negotiation. If it is not 2287 * enabled, then we write the BMCR. This function is intended 2288 * for use with Clause 37 1000Base-X mode. 2289 */ 2290 int genphy_c37_config_aneg(struct phy_device *phydev) 2291 { 2292 int err, changed; 2293 2294 if (phydev->autoneg != AUTONEG_ENABLE) 2295 return genphy_setup_forced(phydev); 2296 2297 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100, 2298 BMCR_SPEED1000); 2299 if (err) 2300 return err; 2301 2302 changed = genphy_c37_config_advert(phydev); 2303 if (changed < 0) /* error */ 2304 return changed; 2305 2306 if (!changed) { 2307 /* Advertisement hasn't changed, but maybe aneg was never on to 2308 * begin with? Or maybe phy was isolated? 2309 */ 2310 int ctl = phy_read(phydev, MII_BMCR); 2311 2312 if (ctl < 0) 2313 return ctl; 2314 2315 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) 2316 changed = 1; /* do restart aneg */ 2317 } 2318 2319 /* Only restart aneg if we are advertising something different 2320 * than we were before. 2321 */ 2322 if (changed > 0) 2323 return genphy_restart_aneg(phydev); 2324 2325 return 0; 2326 } 2327 EXPORT_SYMBOL(genphy_c37_config_aneg); 2328 2329 /** 2330 * genphy_aneg_done - return auto-negotiation status 2331 * @phydev: target phy_device struct 2332 * 2333 * Description: Reads the status register and returns 0 either if 2334 * auto-negotiation is incomplete, or if there was an error. 2335 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. 2336 */ 2337 int genphy_aneg_done(struct phy_device *phydev) 2338 { 2339 int retval = phy_read(phydev, MII_BMSR); 2340 2341 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); 2342 } 2343 EXPORT_SYMBOL(genphy_aneg_done); 2344 2345 /** 2346 * genphy_update_link - update link status in @phydev 2347 * @phydev: target phy_device struct 2348 * 2349 * Description: Update the value in phydev->link to reflect the 2350 * current link value. In order to do this, we need to read 2351 * the status register twice, keeping the second value. 2352 */ 2353 int genphy_update_link(struct phy_device *phydev) 2354 { 2355 int status = 0, bmcr; 2356 2357 bmcr = phy_read(phydev, MII_BMCR); 2358 if (bmcr < 0) 2359 return bmcr; 2360 2361 /* Autoneg is being started, therefore disregard BMSR value and 2362 * report link as down. 2363 */ 2364 if (bmcr & BMCR_ANRESTART) 2365 goto done; 2366 2367 /* The link state is latched low so that momentary link 2368 * drops can be detected. Do not double-read the status 2369 * in polling mode to detect such short link drops except 2370 * the link was already down. 2371 */ 2372 if (!phy_polling_mode(phydev) || !phydev->link) { 2373 status = phy_read(phydev, MII_BMSR); 2374 if (status < 0) 2375 return status; 2376 else if (status & BMSR_LSTATUS) 2377 goto done; 2378 } 2379 2380 /* Read link and autonegotiation status */ 2381 status = phy_read(phydev, MII_BMSR); 2382 if (status < 0) 2383 return status; 2384 done: 2385 phydev->link = status & BMSR_LSTATUS ? 1 : 0; 2386 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0; 2387 2388 /* Consider the case that autoneg was started and "aneg complete" 2389 * bit has been reset, but "link up" bit not yet. 2390 */ 2391 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete) 2392 phydev->link = 0; 2393 2394 return 0; 2395 } 2396 EXPORT_SYMBOL(genphy_update_link); 2397 2398 int genphy_read_lpa(struct phy_device *phydev) 2399 { 2400 int lpa, lpagb; 2401 2402 if (phydev->autoneg == AUTONEG_ENABLE) { 2403 if (!phydev->autoneg_complete) { 2404 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 2405 0); 2406 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0); 2407 return 0; 2408 } 2409 2410 if (phydev->is_gigabit_capable) { 2411 lpagb = phy_read(phydev, MII_STAT1000); 2412 if (lpagb < 0) 2413 return lpagb; 2414 2415 if (lpagb & LPA_1000MSFAIL) { 2416 int adv = phy_read(phydev, MII_CTRL1000); 2417 2418 if (adv < 0) 2419 return adv; 2420 2421 if (adv & CTL1000_ENABLE_MASTER) 2422 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n"); 2423 else 2424 phydev_err(phydev, "Master/Slave resolution failed\n"); 2425 return -ENOLINK; 2426 } 2427 2428 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 2429 lpagb); 2430 } 2431 2432 lpa = phy_read(phydev, MII_LPA); 2433 if (lpa < 0) 2434 return lpa; 2435 2436 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa); 2437 } else { 2438 linkmode_zero(phydev->lp_advertising); 2439 } 2440 2441 return 0; 2442 } 2443 EXPORT_SYMBOL(genphy_read_lpa); 2444 2445 /** 2446 * genphy_read_status_fixed - read the link parameters for !aneg mode 2447 * @phydev: target phy_device struct 2448 * 2449 * Read the current duplex and speed state for a PHY operating with 2450 * autonegotiation disabled. 2451 */ 2452 int genphy_read_status_fixed(struct phy_device *phydev) 2453 { 2454 int bmcr = phy_read(phydev, MII_BMCR); 2455 2456 if (bmcr < 0) 2457 return bmcr; 2458 2459 if (bmcr & BMCR_FULLDPLX) 2460 phydev->duplex = DUPLEX_FULL; 2461 else 2462 phydev->duplex = DUPLEX_HALF; 2463 2464 if (bmcr & BMCR_SPEED1000) 2465 phydev->speed = SPEED_1000; 2466 else if (bmcr & BMCR_SPEED100) 2467 phydev->speed = SPEED_100; 2468 else 2469 phydev->speed = SPEED_10; 2470 2471 return 0; 2472 } 2473 EXPORT_SYMBOL(genphy_read_status_fixed); 2474 2475 /** 2476 * genphy_read_status - check the link status and update current link state 2477 * @phydev: target phy_device struct 2478 * 2479 * Description: Check the link, then figure out the current state 2480 * by comparing what we advertise with what the link partner 2481 * advertises. Start by checking the gigabit possibilities, 2482 * then move on to 10/100. 2483 */ 2484 int genphy_read_status(struct phy_device *phydev) 2485 { 2486 int err, old_link = phydev->link; 2487 2488 /* Update the link, but return if there was an error */ 2489 err = genphy_update_link(phydev); 2490 if (err) 2491 return err; 2492 2493 /* why bother the PHY if nothing can have changed */ 2494 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) 2495 return 0; 2496 2497 phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED; 2498 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED; 2499 phydev->speed = SPEED_UNKNOWN; 2500 phydev->duplex = DUPLEX_UNKNOWN; 2501 phydev->pause = 0; 2502 phydev->asym_pause = 0; 2503 2504 if (phydev->is_gigabit_capable) { 2505 err = genphy_read_master_slave(phydev); 2506 if (err < 0) 2507 return err; 2508 } 2509 2510 err = genphy_read_lpa(phydev); 2511 if (err < 0) 2512 return err; 2513 2514 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { 2515 phy_resolve_aneg_linkmode(phydev); 2516 } else if (phydev->autoneg == AUTONEG_DISABLE) { 2517 err = genphy_read_status_fixed(phydev); 2518 if (err < 0) 2519 return err; 2520 } 2521 2522 return 0; 2523 } 2524 EXPORT_SYMBOL(genphy_read_status); 2525 2526 /** 2527 * genphy_c37_read_status - check the link status and update current link state 2528 * @phydev: target phy_device struct 2529 * @changed: pointer where to store if link changed 2530 * 2531 * Description: Check the link, then figure out the current state 2532 * by comparing what we advertise with what the link partner 2533 * advertises. This function is for Clause 37 1000Base-X mode. 2534 * 2535 * If link has changed, @changed is set to true, false otherwise. 2536 */ 2537 int genphy_c37_read_status(struct phy_device *phydev, bool *changed) 2538 { 2539 int lpa, err, old_link = phydev->link; 2540 2541 /* Update the link, but return if there was an error */ 2542 err = genphy_update_link(phydev); 2543 if (err) 2544 return err; 2545 2546 /* why bother the PHY if nothing can have changed */ 2547 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) { 2548 *changed = false; 2549 return 0; 2550 } 2551 2552 /* Signal link has changed */ 2553 *changed = true; 2554 phydev->duplex = DUPLEX_UNKNOWN; 2555 phydev->pause = 0; 2556 phydev->asym_pause = 0; 2557 2558 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { 2559 lpa = phy_read(phydev, MII_LPA); 2560 if (lpa < 0) 2561 return lpa; 2562 2563 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 2564 phydev->lp_advertising, lpa & LPA_LPACK); 2565 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2566 phydev->lp_advertising, lpa & LPA_1000XFULL); 2567 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2568 phydev->lp_advertising, lpa & LPA_1000XPAUSE); 2569 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2570 phydev->lp_advertising, 2571 lpa & LPA_1000XPAUSE_ASYM); 2572 2573 phy_resolve_aneg_linkmode(phydev); 2574 } else if (phydev->autoneg == AUTONEG_DISABLE) { 2575 int bmcr = phy_read(phydev, MII_BMCR); 2576 2577 if (bmcr < 0) 2578 return bmcr; 2579 2580 if (bmcr & BMCR_FULLDPLX) 2581 phydev->duplex = DUPLEX_FULL; 2582 else 2583 phydev->duplex = DUPLEX_HALF; 2584 } 2585 2586 return 0; 2587 } 2588 EXPORT_SYMBOL(genphy_c37_read_status); 2589 2590 /** 2591 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit 2592 * @phydev: target phy_device struct 2593 * 2594 * Description: Perform a software PHY reset using the standard 2595 * BMCR_RESET bit and poll for the reset bit to be cleared. 2596 * 2597 * Returns: 0 on success, < 0 on failure 2598 */ 2599 int genphy_soft_reset(struct phy_device *phydev) 2600 { 2601 u16 res = BMCR_RESET; 2602 int ret; 2603 2604 if (phydev->autoneg == AUTONEG_ENABLE) 2605 res |= BMCR_ANRESTART; 2606 2607 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res); 2608 if (ret < 0) 2609 return ret; 2610 2611 /* Clause 22 states that setting bit BMCR_RESET sets control registers 2612 * to their default value. Therefore the POWER DOWN bit is supposed to 2613 * be cleared after soft reset. 2614 */ 2615 phydev->suspended = 0; 2616 2617 ret = phy_poll_reset(phydev); 2618 if (ret) 2619 return ret; 2620 2621 /* BMCR may be reset to defaults */ 2622 if (phydev->autoneg == AUTONEG_DISABLE) 2623 ret = genphy_setup_forced(phydev); 2624 2625 return ret; 2626 } 2627 EXPORT_SYMBOL(genphy_soft_reset); 2628 2629 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev) 2630 { 2631 /* It seems there are cases where the interrupts are handled by another 2632 * entity (ie an IRQ controller embedded inside the PHY) and do not 2633 * need any other interraction from phylib. In this case, just trigger 2634 * the state machine directly. 2635 */ 2636 phy_trigger_machine(phydev); 2637 2638 return 0; 2639 } 2640 EXPORT_SYMBOL(genphy_handle_interrupt_no_ack); 2641 2642 /** 2643 * genphy_read_abilities - read PHY abilities from Clause 22 registers 2644 * @phydev: target phy_device struct 2645 * 2646 * Description: Reads the PHY's abilities and populates 2647 * phydev->supported accordingly. 2648 * 2649 * Returns: 0 on success, < 0 on failure 2650 */ 2651 int genphy_read_abilities(struct phy_device *phydev) 2652 { 2653 int val; 2654 2655 linkmode_set_bit_array(phy_basic_ports_array, 2656 ARRAY_SIZE(phy_basic_ports_array), 2657 phydev->supported); 2658 2659 val = phy_read(phydev, MII_BMSR); 2660 if (val < 0) 2661 return val; 2662 2663 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported, 2664 val & BMSR_ANEGCAPABLE); 2665 2666 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported, 2667 val & BMSR_100FULL); 2668 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported, 2669 val & BMSR_100HALF); 2670 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported, 2671 val & BMSR_10FULL); 2672 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported, 2673 val & BMSR_10HALF); 2674 2675 if (val & BMSR_ESTATEN) { 2676 val = phy_read(phydev, MII_ESTATUS); 2677 if (val < 0) 2678 return val; 2679 2680 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 2681 phydev->supported, val & ESTATUS_1000_TFULL); 2682 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 2683 phydev->supported, val & ESTATUS_1000_THALF); 2684 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2685 phydev->supported, val & ESTATUS_1000_XFULL); 2686 } 2687 2688 /* This is optional functionality. If not supported, we may get an error 2689 * which should be ignored. 2690 */ 2691 genphy_c45_read_eee_abilities(phydev); 2692 2693 return 0; 2694 } 2695 EXPORT_SYMBOL(genphy_read_abilities); 2696 2697 /* This is used for the phy device which doesn't support the MMD extended 2698 * register access, but it does have side effect when we are trying to access 2699 * the MMD register via indirect method. 2700 */ 2701 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum) 2702 { 2703 return -EOPNOTSUPP; 2704 } 2705 EXPORT_SYMBOL(genphy_read_mmd_unsupported); 2706 2707 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum, 2708 u16 regnum, u16 val) 2709 { 2710 return -EOPNOTSUPP; 2711 } 2712 EXPORT_SYMBOL(genphy_write_mmd_unsupported); 2713 2714 int genphy_suspend(struct phy_device *phydev) 2715 { 2716 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN); 2717 } 2718 EXPORT_SYMBOL(genphy_suspend); 2719 2720 int genphy_resume(struct phy_device *phydev) 2721 { 2722 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN); 2723 } 2724 EXPORT_SYMBOL(genphy_resume); 2725 2726 int genphy_loopback(struct phy_device *phydev, bool enable, int speed) 2727 { 2728 if (enable) { 2729 u16 ctl = BMCR_LOOPBACK; 2730 int ret, val; 2731 2732 if (speed == SPEED_10 || speed == SPEED_100 || 2733 speed == SPEED_1000) 2734 phydev->speed = speed; 2735 else if (speed) 2736 return -EINVAL; 2737 2738 ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 2739 2740 phy_modify(phydev, MII_BMCR, ~0, ctl); 2741 2742 ret = phy_read_poll_timeout(phydev, MII_BMSR, val, 2743 val & BMSR_LSTATUS, 2744 5000, 500000, true); 2745 if (ret) 2746 return ret; 2747 } else { 2748 phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0); 2749 2750 phy_config_aneg(phydev); 2751 } 2752 2753 return 0; 2754 } 2755 EXPORT_SYMBOL(genphy_loopback); 2756 2757 /** 2758 * phy_remove_link_mode - Remove a supported link mode 2759 * @phydev: phy_device structure to remove link mode from 2760 * @link_mode: Link mode to be removed 2761 * 2762 * Description: Some MACs don't support all link modes which the PHY 2763 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper 2764 * to remove a link mode. 2765 */ 2766 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode) 2767 { 2768 linkmode_clear_bit(link_mode, phydev->supported); 2769 phy_advertise_supported(phydev); 2770 } 2771 EXPORT_SYMBOL(phy_remove_link_mode); 2772 2773 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src) 2774 { 2775 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst, 2776 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src)); 2777 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst, 2778 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src)); 2779 } 2780 2781 /** 2782 * phy_advertise_supported - Advertise all supported modes 2783 * @phydev: target phy_device struct 2784 * 2785 * Description: Called to advertise all supported modes, doesn't touch 2786 * pause mode advertising. 2787 */ 2788 void phy_advertise_supported(struct phy_device *phydev) 2789 { 2790 __ETHTOOL_DECLARE_LINK_MODE_MASK(new); 2791 2792 linkmode_copy(new, phydev->supported); 2793 phy_copy_pause_bits(new, phydev->advertising); 2794 linkmode_copy(phydev->advertising, new); 2795 } 2796 EXPORT_SYMBOL(phy_advertise_supported); 2797 2798 /** 2799 * phy_advertise_eee_all - Advertise all supported EEE modes 2800 * @phydev: target phy_device struct 2801 * 2802 * Description: Per default phylib preserves the EEE advertising at the time of 2803 * phy probing, which might be a subset of the supported EEE modes. Use this 2804 * function when all supported EEE modes should be advertised. This does not 2805 * trigger auto-negotiation, so must be called before phy_start()/ 2806 * phylink_start() which will start auto-negotiation. 2807 */ 2808 void phy_advertise_eee_all(struct phy_device *phydev) 2809 { 2810 linkmode_copy(phydev->advertising_eee, phydev->supported_eee); 2811 } 2812 EXPORT_SYMBOL_GPL(phy_advertise_eee_all); 2813 2814 /** 2815 * phy_support_eee - Set initial EEE policy configuration 2816 * @phydev: Target phy_device struct 2817 * 2818 * This function configures the initial policy for Energy Efficient Ethernet 2819 * (EEE) on the specified PHY device, influencing that EEE capabilities are 2820 * advertised before the link is established. It should be called during PHY 2821 * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs) 2822 * if MAC supports LPI or PHY is capable to compensate missing LPI functionality 2823 * of the MAC. 2824 * 2825 * The function sets default EEE policy parameters, including preparing the PHY 2826 * to advertise EEE capabilities based on hardware support. 2827 * 2828 * It also sets the expected configuration for Low Power Idle (LPI) in the MAC 2829 * driver. If the PHY framework determines that both local and remote 2830 * advertisements support EEE, and the negotiated link mode is compatible with 2831 * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on 2832 * this setting by enabling the LPI timer if enable_tx_lpi is set. 2833 */ 2834 void phy_support_eee(struct phy_device *phydev) 2835 { 2836 linkmode_copy(phydev->advertising_eee, phydev->supported_eee); 2837 phydev->eee_cfg.tx_lpi_enabled = true; 2838 phydev->eee_cfg.eee_enabled = true; 2839 } 2840 EXPORT_SYMBOL(phy_support_eee); 2841 2842 /** 2843 * phy_disable_eee - Disable EEE for the PHY 2844 * @phydev: Target phy_device struct 2845 * 2846 * This function is used by MAC drivers for MAC's which don't support EEE. 2847 * It disables EEE on the PHY layer. 2848 */ 2849 void phy_disable_eee(struct phy_device *phydev) 2850 { 2851 linkmode_zero(phydev->advertising_eee); 2852 phydev->eee_cfg.tx_lpi_enabled = false; 2853 phydev->eee_cfg.eee_enabled = false; 2854 /* don't let userspace re-enable EEE advertisement */ 2855 linkmode_fill(phydev->eee_disabled_modes); 2856 } 2857 EXPORT_SYMBOL_GPL(phy_disable_eee); 2858 2859 /** 2860 * phy_support_sym_pause - Enable support of symmetrical pause 2861 * @phydev: target phy_device struct 2862 * 2863 * Description: Called by the MAC to indicate is supports symmetrical 2864 * Pause, but not asym pause. 2865 */ 2866 void phy_support_sym_pause(struct phy_device *phydev) 2867 { 2868 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 2869 phy_copy_pause_bits(phydev->advertising, phydev->supported); 2870 } 2871 EXPORT_SYMBOL(phy_support_sym_pause); 2872 2873 /** 2874 * phy_support_asym_pause - Enable support of asym pause 2875 * @phydev: target phy_device struct 2876 * 2877 * Description: Called by the MAC to indicate is supports Asym Pause. 2878 */ 2879 void phy_support_asym_pause(struct phy_device *phydev) 2880 { 2881 phy_copy_pause_bits(phydev->advertising, phydev->supported); 2882 } 2883 EXPORT_SYMBOL(phy_support_asym_pause); 2884 2885 /** 2886 * phy_set_sym_pause - Configure symmetric Pause 2887 * @phydev: target phy_device struct 2888 * @rx: Receiver Pause is supported 2889 * @tx: Transmit Pause is supported 2890 * @autoneg: Auto neg should be used 2891 * 2892 * Description: Configure advertised Pause support depending on if 2893 * receiver pause and pause auto neg is supported. Generally called 2894 * from the set_pauseparam .ndo. 2895 */ 2896 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx, 2897 bool autoneg) 2898 { 2899 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 2900 2901 if (rx && tx && autoneg) 2902 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2903 phydev->supported); 2904 2905 linkmode_copy(phydev->advertising, phydev->supported); 2906 } 2907 EXPORT_SYMBOL(phy_set_sym_pause); 2908 2909 /** 2910 * phy_set_asym_pause - Configure Pause and Asym Pause 2911 * @phydev: target phy_device struct 2912 * @rx: Receiver Pause is supported 2913 * @tx: Transmit Pause is supported 2914 * 2915 * Description: Configure advertised Pause support depending on if 2916 * transmit and receiver pause is supported. If there has been a 2917 * change in adverting, trigger a new autoneg. Generally called from 2918 * the set_pauseparam .ndo. 2919 */ 2920 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx) 2921 { 2922 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv); 2923 2924 linkmode_copy(oldadv, phydev->advertising); 2925 linkmode_set_pause(phydev->advertising, tx, rx); 2926 2927 if (!linkmode_equal(oldadv, phydev->advertising) && 2928 phydev->autoneg) 2929 phy_start_aneg(phydev); 2930 } 2931 EXPORT_SYMBOL(phy_set_asym_pause); 2932 2933 /** 2934 * phy_validate_pause - Test if the PHY/MAC support the pause configuration 2935 * @phydev: phy_device struct 2936 * @pp: requested pause configuration 2937 * 2938 * Description: Test if the PHY/MAC combination supports the Pause 2939 * configuration the user is requesting. Returns True if it is 2940 * supported, false otherwise. 2941 */ 2942 bool phy_validate_pause(struct phy_device *phydev, 2943 struct ethtool_pauseparam *pp) 2944 { 2945 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2946 phydev->supported) && pp->rx_pause) 2947 return false; 2948 2949 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2950 phydev->supported) && 2951 pp->rx_pause != pp->tx_pause) 2952 return false; 2953 2954 return true; 2955 } 2956 EXPORT_SYMBOL(phy_validate_pause); 2957 2958 /** 2959 * phy_get_pause - resolve negotiated pause modes 2960 * @phydev: phy_device struct 2961 * @tx_pause: pointer to bool to indicate whether transmit pause should be 2962 * enabled. 2963 * @rx_pause: pointer to bool to indicate whether receive pause should be 2964 * enabled. 2965 * 2966 * Resolve and return the flow control modes according to the negotiation 2967 * result. This includes checking that we are operating in full duplex mode. 2968 * See linkmode_resolve_pause() for further details. 2969 */ 2970 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause) 2971 { 2972 if (phydev->duplex != DUPLEX_FULL) { 2973 *tx_pause = false; 2974 *rx_pause = false; 2975 return; 2976 } 2977 2978 return linkmode_resolve_pause(phydev->advertising, 2979 phydev->lp_advertising, 2980 tx_pause, rx_pause); 2981 } 2982 EXPORT_SYMBOL(phy_get_pause); 2983 2984 #if IS_ENABLED(CONFIG_OF_MDIO) 2985 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) 2986 { 2987 return device_property_read_u32(dev, name, val); 2988 } 2989 #else 2990 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) 2991 { 2992 return -EINVAL; 2993 } 2994 #endif 2995 2996 /** 2997 * phy_get_internal_delay - returns the index of the internal delay 2998 * @phydev: phy_device struct 2999 * @delay_values: array of delays the PHY supports 3000 * @size: the size of the delay array 3001 * @is_rx: boolean to indicate to get the rx internal delay 3002 * 3003 * Returns the index within the array of internal delay passed in. 3004 * If the device property is not present then the interface type is checked 3005 * if the interface defines use of internal delay then a 1 is returned otherwise 3006 * a 0 is returned. 3007 * The array must be in ascending order. If PHY does not have an ascending order 3008 * array then size = 0 and the value of the delay property is returned. 3009 * Return -EINVAL if the delay is invalid or cannot be found. 3010 */ 3011 s32 phy_get_internal_delay(struct phy_device *phydev, const int *delay_values, 3012 int size, bool is_rx) 3013 { 3014 struct device *dev = &phydev->mdio.dev; 3015 int i, ret; 3016 u32 delay; 3017 3018 if (is_rx) { 3019 ret = phy_get_u32_property(dev, "rx-internal-delay-ps", &delay); 3020 if (ret < 0 && size == 0) { 3021 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 3022 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) 3023 return 1; 3024 else 3025 return 0; 3026 } 3027 3028 } else { 3029 ret = phy_get_u32_property(dev, "tx-internal-delay-ps", &delay); 3030 if (ret < 0 && size == 0) { 3031 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 3032 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) 3033 return 1; 3034 else 3035 return 0; 3036 } 3037 } 3038 3039 if (ret < 0) 3040 return ret; 3041 3042 if (size == 0) 3043 return delay; 3044 3045 if (delay < delay_values[0] || delay > delay_values[size - 1]) { 3046 phydev_err(phydev, "Delay %d is out of range\n", delay); 3047 return -EINVAL; 3048 } 3049 3050 if (delay == delay_values[0]) 3051 return 0; 3052 3053 for (i = 1; i < size; i++) { 3054 if (delay == delay_values[i]) 3055 return i; 3056 3057 /* Find an approximate index by looking up the table */ 3058 if (delay > delay_values[i - 1] && 3059 delay < delay_values[i]) { 3060 if (delay - delay_values[i - 1] < 3061 delay_values[i] - delay) 3062 return i - 1; 3063 else 3064 return i; 3065 } 3066 } 3067 3068 phydev_err(phydev, "error finding internal delay index for %d\n", 3069 delay); 3070 3071 return -EINVAL; 3072 } 3073 EXPORT_SYMBOL(phy_get_internal_delay); 3074 3075 /** 3076 * phy_get_tx_amplitude_gain - stores tx amplitude gain in @val 3077 * @phydev: phy_device struct 3078 * @dev: pointer to the devices device struct 3079 * @linkmode: linkmode for which the tx amplitude gain should be retrieved 3080 * @val: tx amplitude gain 3081 * 3082 * Returns: 0 on success, < 0 on failure 3083 */ 3084 int phy_get_tx_amplitude_gain(struct phy_device *phydev, struct device *dev, 3085 enum ethtool_link_mode_bit_indices linkmode, 3086 u32 *val) 3087 { 3088 switch (linkmode) { 3089 case ETHTOOL_LINK_MODE_100baseT_Full_BIT: 3090 return phy_get_u32_property(dev, 3091 "tx-amplitude-100base-tx-percent", 3092 val); 3093 default: 3094 return -EINVAL; 3095 } 3096 } 3097 EXPORT_SYMBOL_GPL(phy_get_tx_amplitude_gain); 3098 3099 /** 3100 * phy_get_mac_termination - stores MAC termination in @val 3101 * @phydev: phy_device struct 3102 * @dev: pointer to the devices device struct 3103 * @val: MAC termination 3104 * 3105 * Returns: 0 on success, < 0 on failure 3106 */ 3107 int phy_get_mac_termination(struct phy_device *phydev, struct device *dev, 3108 u32 *val) 3109 { 3110 return phy_get_u32_property(dev, "mac-termination-ohms", val); 3111 } 3112 EXPORT_SYMBOL_GPL(phy_get_mac_termination); 3113 3114 static int phy_led_set_brightness(struct led_classdev *led_cdev, 3115 enum led_brightness value) 3116 { 3117 struct phy_led *phyled = to_phy_led(led_cdev); 3118 struct phy_device *phydev = phyled->phydev; 3119 int err; 3120 3121 mutex_lock(&phydev->lock); 3122 err = phydev->drv->led_brightness_set(phydev, phyled->index, value); 3123 mutex_unlock(&phydev->lock); 3124 3125 return err; 3126 } 3127 3128 static int phy_led_blink_set(struct led_classdev *led_cdev, 3129 unsigned long *delay_on, 3130 unsigned long *delay_off) 3131 { 3132 struct phy_led *phyled = to_phy_led(led_cdev); 3133 struct phy_device *phydev = phyled->phydev; 3134 int err; 3135 3136 mutex_lock(&phydev->lock); 3137 err = phydev->drv->led_blink_set(phydev, phyled->index, 3138 delay_on, delay_off); 3139 mutex_unlock(&phydev->lock); 3140 3141 return err; 3142 } 3143 3144 static __maybe_unused struct device * 3145 phy_led_hw_control_get_device(struct led_classdev *led_cdev) 3146 { 3147 struct phy_led *phyled = to_phy_led(led_cdev); 3148 struct phy_device *phydev = phyled->phydev; 3149 3150 if (phydev->attached_dev) 3151 return &phydev->attached_dev->dev; 3152 return NULL; 3153 } 3154 3155 static int __maybe_unused 3156 phy_led_hw_control_get(struct led_classdev *led_cdev, 3157 unsigned long *rules) 3158 { 3159 struct phy_led *phyled = to_phy_led(led_cdev); 3160 struct phy_device *phydev = phyled->phydev; 3161 int err; 3162 3163 mutex_lock(&phydev->lock); 3164 err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules); 3165 mutex_unlock(&phydev->lock); 3166 3167 return err; 3168 } 3169 3170 static int __maybe_unused 3171 phy_led_hw_control_set(struct led_classdev *led_cdev, 3172 unsigned long rules) 3173 { 3174 struct phy_led *phyled = to_phy_led(led_cdev); 3175 struct phy_device *phydev = phyled->phydev; 3176 int err; 3177 3178 mutex_lock(&phydev->lock); 3179 err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules); 3180 mutex_unlock(&phydev->lock); 3181 3182 return err; 3183 } 3184 3185 static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev, 3186 unsigned long rules) 3187 { 3188 struct phy_led *phyled = to_phy_led(led_cdev); 3189 struct phy_device *phydev = phyled->phydev; 3190 int err; 3191 3192 mutex_lock(&phydev->lock); 3193 err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules); 3194 mutex_unlock(&phydev->lock); 3195 3196 return err; 3197 } 3198 3199 static void phy_leds_unregister(struct phy_device *phydev) 3200 { 3201 struct phy_led *phyled, *tmp; 3202 3203 list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) { 3204 led_classdev_unregister(&phyled->led_cdev); 3205 list_del(&phyled->list); 3206 } 3207 } 3208 3209 static int of_phy_led(struct phy_device *phydev, 3210 struct device_node *led) 3211 { 3212 struct device *dev = &phydev->mdio.dev; 3213 struct led_init_data init_data = {}; 3214 struct led_classdev *cdev; 3215 unsigned long modes = 0; 3216 struct phy_led *phyled; 3217 u32 index; 3218 int err; 3219 3220 phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL); 3221 if (!phyled) 3222 return -ENOMEM; 3223 3224 cdev = &phyled->led_cdev; 3225 phyled->phydev = phydev; 3226 3227 err = of_property_read_u32(led, "reg", &index); 3228 if (err) 3229 return err; 3230 if (index > U8_MAX) 3231 return -EINVAL; 3232 3233 if (of_property_read_bool(led, "active-high")) 3234 set_bit(PHY_LED_ACTIVE_HIGH, &modes); 3235 if (of_property_read_bool(led, "active-low")) 3236 set_bit(PHY_LED_ACTIVE_LOW, &modes); 3237 if (of_property_read_bool(led, "inactive-high-impedance")) 3238 set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes); 3239 3240 if (WARN_ON(modes & BIT(PHY_LED_ACTIVE_LOW) && 3241 modes & BIT(PHY_LED_ACTIVE_HIGH))) 3242 return -EINVAL; 3243 3244 if (modes) { 3245 /* Return error if asked to set polarity modes but not supported */ 3246 if (!phydev->drv->led_polarity_set) 3247 return -EINVAL; 3248 3249 err = phydev->drv->led_polarity_set(phydev, index, modes); 3250 if (err) 3251 return err; 3252 } 3253 3254 phyled->index = index; 3255 if (phydev->drv->led_brightness_set) 3256 cdev->brightness_set_blocking = phy_led_set_brightness; 3257 if (phydev->drv->led_blink_set) 3258 cdev->blink_set = phy_led_blink_set; 3259 3260 #ifdef CONFIG_LEDS_TRIGGERS 3261 if (phydev->drv->led_hw_is_supported && 3262 phydev->drv->led_hw_control_set && 3263 phydev->drv->led_hw_control_get) { 3264 cdev->hw_control_is_supported = phy_led_hw_is_supported; 3265 cdev->hw_control_set = phy_led_hw_control_set; 3266 cdev->hw_control_get = phy_led_hw_control_get; 3267 cdev->hw_control_trigger = "netdev"; 3268 } 3269 3270 cdev->hw_control_get_device = phy_led_hw_control_get_device; 3271 #endif 3272 cdev->max_brightness = 1; 3273 init_data.devicename = dev_name(&phydev->mdio.dev); 3274 init_data.fwnode = of_fwnode_handle(led); 3275 init_data.devname_mandatory = true; 3276 3277 err = led_classdev_register_ext(dev, cdev, &init_data); 3278 if (err) 3279 return err; 3280 3281 list_add(&phyled->list, &phydev->leds); 3282 3283 return 0; 3284 } 3285 3286 static int of_phy_leds(struct phy_device *phydev) 3287 { 3288 struct device_node *node = phydev->mdio.dev.of_node; 3289 struct device_node *leds; 3290 int err; 3291 3292 if (!IS_ENABLED(CONFIG_OF_MDIO)) 3293 return 0; 3294 3295 if (!node) 3296 return 0; 3297 3298 leds = of_get_child_by_name(node, "leds"); 3299 if (!leds) 3300 return 0; 3301 3302 /* Check if the PHY driver have at least an OP to 3303 * set the LEDs. 3304 */ 3305 if (!(phydev->drv->led_brightness_set || 3306 phydev->drv->led_blink_set || 3307 phydev->drv->led_hw_control_set)) { 3308 phydev_dbg(phydev, "ignoring leds node defined with no PHY driver support\n"); 3309 goto exit; 3310 } 3311 3312 for_each_available_child_of_node_scoped(leds, led) { 3313 err = of_phy_led(phydev, led); 3314 if (err) { 3315 of_node_put(leds); 3316 phy_leds_unregister(phydev); 3317 return err; 3318 } 3319 } 3320 3321 exit: 3322 of_node_put(leds); 3323 return 0; 3324 } 3325 3326 /** 3327 * fwnode_mdio_find_device - Given a fwnode, find the mdio_device 3328 * @fwnode: pointer to the mdio_device's fwnode 3329 * 3330 * If successful, returns a pointer to the mdio_device with the embedded 3331 * struct device refcount incremented by one, or NULL on failure. 3332 * The caller should call put_device() on the mdio_device after its use. 3333 */ 3334 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode) 3335 { 3336 struct device *d; 3337 3338 if (!fwnode) 3339 return NULL; 3340 3341 d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode); 3342 if (!d) 3343 return NULL; 3344 3345 return to_mdio_device(d); 3346 } 3347 EXPORT_SYMBOL(fwnode_mdio_find_device); 3348 3349 /** 3350 * fwnode_phy_find_device - For provided phy_fwnode, find phy_device. 3351 * 3352 * @phy_fwnode: Pointer to the phy's fwnode. 3353 * 3354 * If successful, returns a pointer to the phy_device with the embedded 3355 * struct device refcount incremented by one, or NULL on failure. 3356 */ 3357 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode) 3358 { 3359 struct mdio_device *mdiodev; 3360 3361 mdiodev = fwnode_mdio_find_device(phy_fwnode); 3362 if (!mdiodev) 3363 return NULL; 3364 3365 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) 3366 return to_phy_device(&mdiodev->dev); 3367 3368 put_device(&mdiodev->dev); 3369 3370 return NULL; 3371 } 3372 EXPORT_SYMBOL(fwnode_phy_find_device); 3373 3374 /** 3375 * fwnode_get_phy_node - Get the phy_node using the named reference. 3376 * @fwnode: Pointer to fwnode from which phy_node has to be obtained. 3377 * 3378 * Refer return conditions of fwnode_find_reference(). 3379 * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy" 3380 * and "phy-device" are not supported in ACPI. DT supports all the three 3381 * named references to the phy node. 3382 */ 3383 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode) 3384 { 3385 struct fwnode_handle *phy_node; 3386 3387 /* Only phy-handle is used for ACPI */ 3388 phy_node = fwnode_find_reference(fwnode, "phy-handle", 0); 3389 if (!IS_ERR(phy_node) || is_acpi_node(fwnode)) 3390 return phy_node; 3391 phy_node = fwnode_find_reference(fwnode, "phy", 0); 3392 if (!IS_ERR(phy_node)) 3393 return phy_node; 3394 return fwnode_find_reference(fwnode, "phy-device", 0); 3395 } 3396 EXPORT_SYMBOL_GPL(fwnode_get_phy_node); 3397 3398 /** 3399 * phy_probe - probe and init a PHY device 3400 * @dev: device to probe and init 3401 * 3402 * Take care of setting up the phy_device structure, set the state to READY. 3403 */ 3404 static int phy_probe(struct device *dev) 3405 { 3406 struct phy_device *phydev = to_phy_device(dev); 3407 struct device_driver *drv = phydev->mdio.dev.driver; 3408 struct phy_driver *phydrv = to_phy_driver(drv); 3409 int err = 0; 3410 3411 phydev->drv = phydrv; 3412 3413 /* Disable the interrupt if the PHY doesn't support it 3414 * but the interrupt is still a valid one 3415 */ 3416 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev)) 3417 phydev->irq = PHY_POLL; 3418 3419 if (phydrv->flags & PHY_IS_INTERNAL) 3420 phydev->is_internal = true; 3421 3422 /* Deassert the reset signal */ 3423 phy_device_reset(phydev, 0); 3424 3425 if (phydev->drv->probe) { 3426 err = phydev->drv->probe(phydev); 3427 if (err) 3428 goto out; 3429 } 3430 3431 phy_disable_interrupts(phydev); 3432 3433 /* Start out supporting everything. Eventually, 3434 * a controller will attach, and may modify one 3435 * or both of these values 3436 */ 3437 if (phydrv->features) { 3438 linkmode_copy(phydev->supported, phydrv->features); 3439 genphy_c45_read_eee_abilities(phydev); 3440 } 3441 else if (phydrv->get_features) 3442 err = phydrv->get_features(phydev); 3443 else if (phydev->is_c45) 3444 err = genphy_c45_pma_read_abilities(phydev); 3445 else 3446 err = genphy_read_abilities(phydev); 3447 3448 if (err) 3449 goto out; 3450 3451 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 3452 phydev->supported)) 3453 phydev->autoneg = 0; 3454 3455 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 3456 phydev->supported)) 3457 phydev->is_gigabit_capable = 1; 3458 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 3459 phydev->supported)) 3460 phydev->is_gigabit_capable = 1; 3461 3462 of_set_phy_supported(phydev); 3463 phy_advertise_supported(phydev); 3464 3465 /* Get PHY default EEE advertising modes and handle them as potentially 3466 * safe initial configuration. 3467 */ 3468 err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee); 3469 if (err) 3470 goto out; 3471 3472 /* Get the EEE modes we want to prohibit. */ 3473 of_set_phy_eee_broken(phydev); 3474 3475 /* Some PHYs may advertise, by default, not support EEE modes. So, 3476 * we need to clean them. In addition remove all disabled EEE modes. 3477 */ 3478 linkmode_and(phydev->advertising_eee, phydev->supported_eee, 3479 phydev->advertising_eee); 3480 linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee, 3481 phydev->eee_disabled_modes); 3482 3483 /* There is no "enabled" flag. If PHY is advertising, assume it is 3484 * kind of enabled. 3485 */ 3486 phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee); 3487 3488 /* Get master/slave strap overrides */ 3489 of_set_phy_timing_role(phydev); 3490 3491 /* The Pause Frame bits indicate that the PHY can support passing 3492 * pause frames. During autonegotiation, the PHYs will determine if 3493 * they should allow pause frames to pass. The MAC driver should then 3494 * use that result to determine whether to enable flow control via 3495 * pause frames. 3496 * 3497 * Normally, PHY drivers should not set the Pause bits, and instead 3498 * allow phylib to do that. However, there may be some situations 3499 * (e.g. hardware erratum) where the driver wants to set only one 3500 * of these bits. 3501 */ 3502 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) && 3503 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) { 3504 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 3505 phydev->supported); 3506 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 3507 phydev->supported); 3508 } 3509 3510 /* Set the state to READY by default */ 3511 phydev->state = PHY_READY; 3512 3513 /* Get the LEDs from the device tree, and instantiate standard 3514 * LEDs for them. 3515 */ 3516 if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev)) 3517 err = of_phy_leds(phydev); 3518 3519 out: 3520 /* Re-assert the reset signal on error */ 3521 if (err) 3522 phy_device_reset(phydev, 1); 3523 3524 return err; 3525 } 3526 3527 static int phy_remove(struct device *dev) 3528 { 3529 struct phy_device *phydev = to_phy_device(dev); 3530 3531 cancel_delayed_work_sync(&phydev->state_queue); 3532 3533 if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev)) 3534 phy_leds_unregister(phydev); 3535 3536 phydev->state = PHY_DOWN; 3537 3538 sfp_bus_del_upstream(phydev->sfp_bus); 3539 phydev->sfp_bus = NULL; 3540 3541 if (phydev->drv && phydev->drv->remove) 3542 phydev->drv->remove(phydev); 3543 3544 /* Assert the reset signal */ 3545 phy_device_reset(phydev, 1); 3546 3547 phydev->drv = NULL; 3548 3549 return 0; 3550 } 3551 3552 /** 3553 * phy_driver_register - register a phy_driver with the PHY layer 3554 * @new_driver: new phy_driver to register 3555 * @owner: module owning this PHY 3556 */ 3557 static int phy_driver_register(struct phy_driver *new_driver, 3558 struct module *owner) 3559 { 3560 int retval; 3561 3562 /* Either the features are hard coded, or dynamically 3563 * determined. It cannot be both. 3564 */ 3565 if (WARN_ON(new_driver->features && new_driver->get_features)) { 3566 pr_err("%s: features and get_features must not both be set\n", 3567 new_driver->name); 3568 return -EINVAL; 3569 } 3570 3571 /* PHYLIB device drivers must not match using a DT compatible table 3572 * as this bypasses our checks that the mdiodev that is being matched 3573 * is backed by a struct phy_device. If such a case happens, we will 3574 * make out-of-bounds accesses and lockup in phydev->lock. 3575 */ 3576 if (WARN(new_driver->mdiodrv.driver.of_match_table, 3577 "%s: driver must not provide a DT match table\n", 3578 new_driver->name)) 3579 return -EINVAL; 3580 3581 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY; 3582 new_driver->mdiodrv.driver.name = new_driver->name; 3583 new_driver->mdiodrv.driver.bus = &mdio_bus_type; 3584 new_driver->mdiodrv.driver.probe = phy_probe; 3585 new_driver->mdiodrv.driver.remove = phy_remove; 3586 new_driver->mdiodrv.driver.owner = owner; 3587 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS; 3588 3589 retval = driver_register(&new_driver->mdiodrv.driver); 3590 if (retval) { 3591 pr_err("%s: Error %d in registering driver\n", 3592 new_driver->name, retval); 3593 3594 return retval; 3595 } 3596 3597 pr_debug("%s: Registered new driver\n", new_driver->name); 3598 3599 return 0; 3600 } 3601 3602 static void phy_driver_unregister(struct phy_driver *drv) 3603 { 3604 driver_unregister(&drv->mdiodrv.driver); 3605 } 3606 3607 int phy_drivers_register(struct phy_driver *new_driver, int n, 3608 struct module *owner) 3609 { 3610 int i, ret = 0; 3611 3612 for (i = 0; i < n; i++) { 3613 ret = phy_driver_register(new_driver + i, owner); 3614 if (ret) { 3615 while (i-- > 0) 3616 phy_driver_unregister(new_driver + i); 3617 break; 3618 } 3619 } 3620 return ret; 3621 } 3622 EXPORT_SYMBOL(phy_drivers_register); 3623 3624 void phy_drivers_unregister(struct phy_driver *drv, int n) 3625 { 3626 int i; 3627 3628 for (i = 0; i < n; i++) 3629 phy_driver_unregister(drv + i); 3630 } 3631 EXPORT_SYMBOL(phy_drivers_unregister); 3632 3633 static struct phy_driver genphy_driver = { 3634 .phy_id = 0xffffffff, 3635 .phy_id_mask = 0xffffffff, 3636 .name = "Generic PHY", 3637 .get_features = genphy_read_abilities, 3638 .suspend = genphy_suspend, 3639 .resume = genphy_resume, 3640 .set_loopback = genphy_loopback, 3641 }; 3642 3643 static const struct ethtool_phy_ops phy_ethtool_phy_ops = { 3644 .get_sset_count = phy_ethtool_get_sset_count, 3645 .get_strings = phy_ethtool_get_strings, 3646 .get_stats = phy_ethtool_get_stats, 3647 .get_plca_cfg = phy_ethtool_get_plca_cfg, 3648 .set_plca_cfg = phy_ethtool_set_plca_cfg, 3649 .get_plca_status = phy_ethtool_get_plca_status, 3650 .start_cable_test = phy_start_cable_test, 3651 .start_cable_test_tdr = phy_start_cable_test_tdr, 3652 }; 3653 3654 static const struct phylib_stubs __phylib_stubs = { 3655 .hwtstamp_get = __phy_hwtstamp_get, 3656 .hwtstamp_set = __phy_hwtstamp_set, 3657 .get_phy_stats = __phy_ethtool_get_phy_stats, 3658 .get_link_ext_stats = __phy_ethtool_get_link_ext_stats, 3659 }; 3660 3661 static void phylib_register_stubs(void) 3662 { 3663 phylib_stubs = &__phylib_stubs; 3664 } 3665 3666 static void phylib_unregister_stubs(void) 3667 { 3668 phylib_stubs = NULL; 3669 } 3670 3671 static int __init phy_init(void) 3672 { 3673 int rc; 3674 3675 rtnl_lock(); 3676 ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops); 3677 phylib_register_stubs(); 3678 rtnl_unlock(); 3679 3680 rc = phy_caps_init(); 3681 if (rc) 3682 goto err_ethtool_phy_ops; 3683 3684 features_init(); 3685 3686 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE); 3687 if (rc) 3688 goto err_ethtool_phy_ops; 3689 3690 rc = phy_driver_register(&genphy_driver, THIS_MODULE); 3691 if (rc) 3692 goto err_c45; 3693 3694 return 0; 3695 3696 err_c45: 3697 phy_driver_unregister(&genphy_c45_driver); 3698 err_ethtool_phy_ops: 3699 rtnl_lock(); 3700 phylib_unregister_stubs(); 3701 ethtool_set_ethtool_phy_ops(NULL); 3702 rtnl_unlock(); 3703 3704 return rc; 3705 } 3706 3707 static void __exit phy_exit(void) 3708 { 3709 phy_driver_unregister(&genphy_c45_driver); 3710 phy_driver_unregister(&genphy_driver); 3711 rtnl_lock(); 3712 phylib_unregister_stubs(); 3713 ethtool_set_ethtool_phy_ops(NULL); 3714 rtnl_unlock(); 3715 } 3716 3717 subsys_initcall(phy_init); 3718 module_exit(phy_exit); 3719