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