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