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