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 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 EXPORT_SYMBOL(phy_sfp_connect_phy); 1546 1547 /** 1548 * phy_sfp_disconnect_phy - Disconnect the SFP module's PHY from the upstream PHY 1549 * @upstream: pointer to the upstream phy device 1550 * @phy: pointer to the SFP module's phy device 1551 * 1552 * This helper allows keeping track of PHY devices on the link. It removes the 1553 * SFP module's phy to the phy namespace of the upstream phy. As the module phy 1554 * will be destroyed, re-inserting the same module will add a new phy with a 1555 * new index. 1556 */ 1557 void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy) 1558 { 1559 struct phy_device *phydev = upstream; 1560 struct net_device *dev = phydev->attached_dev; 1561 1562 if (dev) 1563 phy_link_topo_del_phy(dev, phy); 1564 } 1565 EXPORT_SYMBOL(phy_sfp_disconnect_phy); 1566 1567 /** 1568 * phy_sfp_attach - attach the SFP bus to the PHY upstream network device 1569 * @upstream: pointer to the phy device 1570 * @bus: sfp bus representing cage being attached 1571 * 1572 * This is used to fill in the sfp_upstream_ops .attach member. 1573 */ 1574 void phy_sfp_attach(void *upstream, struct sfp_bus *bus) 1575 { 1576 struct phy_device *phydev = upstream; 1577 1578 if (phydev->attached_dev) 1579 phydev->attached_dev->sfp_bus = bus; 1580 phydev->sfp_bus_attached = true; 1581 } 1582 EXPORT_SYMBOL(phy_sfp_attach); 1583 1584 /** 1585 * phy_sfp_detach - detach the SFP bus from the PHY upstream network device 1586 * @upstream: pointer to the phy device 1587 * @bus: sfp bus representing cage being attached 1588 * 1589 * This is used to fill in the sfp_upstream_ops .detach member. 1590 */ 1591 void phy_sfp_detach(void *upstream, struct sfp_bus *bus) 1592 { 1593 struct phy_device *phydev = upstream; 1594 1595 if (phydev->attached_dev) 1596 phydev->attached_dev->sfp_bus = NULL; 1597 phydev->sfp_bus_attached = false; 1598 } 1599 EXPORT_SYMBOL(phy_sfp_detach); 1600 1601 static int phy_sfp_module_insert(void *upstream, const struct sfp_eeprom_id *id) 1602 { 1603 __ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support); 1604 struct phy_device *phydev = upstream; 1605 const struct sfp_module_caps *caps; 1606 struct phy_port *port; 1607 1608 phy_interface_t iface; 1609 1610 linkmode_zero(sfp_support); 1611 1612 port = phy_get_sfp_port(phydev); 1613 if (!port) 1614 return -EINVAL; 1615 1616 caps = sfp_get_module_caps(phydev->sfp_bus); 1617 1618 linkmode_and(sfp_support, port->supported, caps->link_modes); 1619 if (linkmode_empty(sfp_support)) { 1620 dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n"); 1621 return -EINVAL; 1622 } 1623 1624 iface = sfp_select_interface(phydev->sfp_bus, sfp_support); 1625 if (iface == PHY_INTERFACE_MODE_NA) { 1626 dev_err(&phydev->mdio.dev, "PHY %s does not support the SFP module's requested MII interfaces\n", 1627 phydev_name(phydev)); 1628 return -EINVAL; 1629 } 1630 1631 if (phydev->n_ports == 1) 1632 phydev->port = caps->port; 1633 1634 if (port->ops && port->ops->configure_mii) 1635 return port->ops->configure_mii(port, true, iface); 1636 1637 return 0; 1638 } 1639 1640 static void phy_sfp_module_remove(void *upstream) 1641 { 1642 struct phy_device *phydev = upstream; 1643 struct phy_port *port = phy_get_sfp_port(phydev); 1644 1645 if (port && port->ops && port->ops->configure_mii) 1646 port->ops->configure_mii(port, false, PHY_INTERFACE_MODE_NA); 1647 1648 if (phydev->n_ports == 1) 1649 phydev->port = PORT_NONE; 1650 } 1651 1652 static void phy_sfp_link_up(void *upstream) 1653 { 1654 struct phy_device *phydev = upstream; 1655 struct phy_port *port = phy_get_sfp_port(phydev); 1656 1657 if (port && port->ops && port->ops->link_up) 1658 port->ops->link_up(port); 1659 } 1660 1661 static void phy_sfp_link_down(void *upstream) 1662 { 1663 struct phy_device *phydev = upstream; 1664 struct phy_port *port = phy_get_sfp_port(phydev); 1665 1666 if (port && port->ops && port->ops->link_down) 1667 port->ops->link_down(port); 1668 } 1669 1670 static const struct sfp_upstream_ops sfp_phydev_ops = { 1671 .attach = phy_sfp_attach, 1672 .detach = phy_sfp_detach, 1673 .module_insert = phy_sfp_module_insert, 1674 .module_remove = phy_sfp_module_remove, 1675 .link_up = phy_sfp_link_up, 1676 .link_down = phy_sfp_link_down, 1677 .connect_phy = phy_sfp_connect_phy, 1678 .disconnect_phy = phy_sfp_disconnect_phy, 1679 }; 1680 1681 static int phy_add_port(struct phy_device *phydev, struct phy_port *port) 1682 { 1683 int ret = 0; 1684 1685 if (phydev->n_ports == phydev->max_n_ports) 1686 return -EBUSY; 1687 1688 /* We set all ports as active by default, PHY drivers may deactivate 1689 * them (when unused) 1690 */ 1691 port->active = true; 1692 1693 if (port->is_mii) { 1694 if (phydev->drv && phydev->drv->attach_mii_port) 1695 ret = phydev->drv->attach_mii_port(phydev, port); 1696 } else { 1697 if (phydev->drv && phydev->drv->attach_mdi_port) 1698 ret = phydev->drv->attach_mdi_port(phydev, port); 1699 } 1700 1701 if (ret) 1702 return ret; 1703 1704 /* The PHY driver might have added, removed or set medium/pairs info, 1705 * so update the port supported accordingly. 1706 */ 1707 phy_port_update_supported(port); 1708 1709 list_add(&port->head, &phydev->ports); 1710 1711 phydev->n_ports++; 1712 1713 return 0; 1714 } 1715 1716 static void phy_del_port(struct phy_device *phydev, struct phy_port *port) 1717 { 1718 if (!phydev->n_ports) 1719 return; 1720 1721 list_del(&port->head); 1722 1723 phydev->n_ports--; 1724 } 1725 1726 static int phy_setup_sfp_port(struct phy_device *phydev) 1727 { 1728 struct phy_port *port = phy_port_alloc(); 1729 int ret; 1730 1731 if (!port) 1732 return -ENOMEM; 1733 1734 port->parent_type = PHY_PORT_PHY; 1735 port->phy = phydev; 1736 1737 /* The PHY is a media converter, the port connected to the SFP cage 1738 * is a MII port. 1739 */ 1740 port->is_mii = true; 1741 port->is_sfp = true; 1742 1743 /* The port->supported and port->interfaces list will be populated 1744 * when attaching the port to the phydev. 1745 */ 1746 ret = phy_add_port(phydev, port); 1747 if (ret) 1748 phy_port_destroy(port); 1749 1750 return ret; 1751 } 1752 1753 /** 1754 * phy_sfp_probe - probe for a SFP cage attached to this PHY device 1755 * @phydev: Pointer to phy_device 1756 * @ops: SFP's upstream operations 1757 */ 1758 int phy_sfp_probe(struct phy_device *phydev, 1759 const struct sfp_upstream_ops *ops) 1760 { 1761 struct sfp_bus *bus; 1762 int ret = 0; 1763 1764 if (phydev->mdio.dev.fwnode) { 1765 bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode); 1766 if (IS_ERR(bus)) 1767 return PTR_ERR(bus); 1768 1769 phydev->sfp_bus = bus; 1770 1771 ret = sfp_bus_add_upstream(bus, phydev, ops); 1772 sfp_bus_put(bus); 1773 } 1774 1775 if (!ret && phydev->sfp_bus) 1776 ret = phy_setup_sfp_port(phydev); 1777 1778 return ret; 1779 } 1780 EXPORT_SYMBOL(phy_sfp_probe); 1781 1782 static bool phy_drv_supports_irq(const struct phy_driver *phydrv) 1783 { 1784 return phydrv->config_intr && phydrv->handle_interrupt; 1785 } 1786 1787 /** 1788 * phy_attach_direct - attach a network device to a given PHY device pointer 1789 * @dev: network device to attach 1790 * @phydev: Pointer to phy_device to attach 1791 * @flags: PHY device's dev_flags 1792 * @interface: PHY device's interface 1793 * 1794 * Description: Called by drivers to attach to a particular PHY 1795 * device. The phy_device is found, and properly hooked up 1796 * to the phy_driver. If no driver is attached, then a 1797 * generic driver is used. The phy_device is given a ptr to 1798 * the attaching device, and given a callback for link status 1799 * change. The phy_device is returned to the attaching driver. 1800 * This function takes a reference on the phy device. 1801 */ 1802 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, 1803 u32 flags, phy_interface_t interface) 1804 { 1805 struct mii_bus *bus = phydev->mdio.bus; 1806 struct device *d = &phydev->mdio.dev; 1807 struct module *ndev_owner = NULL; 1808 int err; 1809 1810 /* For Ethernet device drivers that register their own MDIO bus, we 1811 * will have bus->owner match ndev_mod, so we do not want to increment 1812 * our own module->refcnt here, otherwise we would not be able to 1813 * unload later on. 1814 */ 1815 if (dev) 1816 ndev_owner = dev->dev.parent->driver->owner; 1817 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) { 1818 phydev_err(phydev, "failed to get the bus module\n"); 1819 return -EIO; 1820 } 1821 1822 get_device(d); 1823 1824 /* Assume that if there is no driver, that it doesn't 1825 * exist, and we should use the genphy driver. 1826 */ 1827 if (!d->driver) { 1828 if (phydev->is_c45) 1829 d->driver = &genphy_c45_driver.mdiodrv.driver; 1830 else 1831 d->driver = &genphy_driver.mdiodrv.driver; 1832 1833 phydev->is_genphy_driven = 1; 1834 } 1835 1836 if (!try_module_get(d->driver->owner)) { 1837 phydev_err(phydev, "failed to get the device driver module\n"); 1838 err = -EIO; 1839 goto error_put_device; 1840 } 1841 1842 if (phydev->is_genphy_driven) { 1843 err = d->driver->probe(d); 1844 if (err >= 0) 1845 err = device_bind_driver(d); 1846 1847 if (err) 1848 goto error_module_put; 1849 } 1850 1851 if (phydev->attached_dev) { 1852 dev_err(&dev->dev, "PHY already attached\n"); 1853 err = -EBUSY; 1854 goto error; 1855 } 1856 1857 phydev->phy_link_change = phy_link_change; 1858 if (dev) { 1859 phydev->attached_dev = dev; 1860 dev->phydev = phydev; 1861 1862 if (phydev->sfp_bus_attached) 1863 dev->sfp_bus = phydev->sfp_bus; 1864 1865 err = phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev); 1866 if (err) 1867 goto error; 1868 } 1869 1870 /* Some Ethernet drivers try to connect to a PHY device before 1871 * calling register_netdevice() -> netdev_register_kobject() and 1872 * does the dev->dev.kobj initialization. Here we only check for 1873 * success which indicates that the network device kobject is 1874 * ready. Once we do that we still need to keep track of whether 1875 * links were successfully set up or not for phy_detach() to 1876 * remove them accordingly. 1877 */ 1878 phydev->sysfs_links = false; 1879 1880 phy_sysfs_create_links(phydev); 1881 1882 if (!phydev->attached_dev) { 1883 err = sysfs_create_file(&phydev->mdio.dev.kobj, 1884 &dev_attr_phy_standalone.attr); 1885 if (err) 1886 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n"); 1887 } 1888 1889 phydev->dev_flags |= flags; 1890 1891 phydev->interface = interface; 1892 1893 phydev->state = PHY_READY; 1894 1895 phydev->interrupts = PHY_INTERRUPT_DISABLED; 1896 1897 /* PHYs can request to use poll mode even though they have an 1898 * associated interrupt line. This could be the case if they 1899 * detect a broken interrupt handling. 1900 */ 1901 if (phydev->dev_flags & PHY_F_NO_IRQ) 1902 phydev->irq = PHY_POLL; 1903 1904 if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev)) 1905 phydev->irq = PHY_POLL; 1906 1907 /* Port is set to PORT_TP by default and the actual PHY driver will set 1908 * it to different value depending on the PHY configuration. If we have 1909 * the generic PHY driver we can't figure it out, thus set the old 1910 * legacy PORT_MII value. 1911 */ 1912 if (phydev->is_genphy_driven) 1913 phydev->port = PORT_MII; 1914 1915 /* Initial carrier state is off as the phy is about to be 1916 * (re)initialized. 1917 */ 1918 if (dev) 1919 netif_carrier_off(phydev->attached_dev); 1920 1921 /* Do initial configuration here, now that 1922 * we have certain key parameters 1923 * (dev_flags and interface) 1924 */ 1925 err = phy_init_hw(phydev); 1926 if (err) 1927 goto error; 1928 1929 phy_resume(phydev); 1930 if (!phydev->is_on_sfp_module) 1931 phy_led_triggers_register(phydev); 1932 1933 /** 1934 * If the external phy used by current mac interface is managed by 1935 * another mac interface, so we should create a device link between 1936 * phy dev and mac dev. 1937 */ 1938 if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent) 1939 phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev, 1940 DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS); 1941 1942 return err; 1943 1944 error: 1945 /* phy_detach() does all of the cleanup below */ 1946 phy_detach(phydev); 1947 return err; 1948 1949 error_module_put: 1950 module_put(d->driver->owner); 1951 phydev->is_genphy_driven = 0; 1952 d->driver = NULL; 1953 error_put_device: 1954 put_device(d); 1955 if (ndev_owner != bus->owner) 1956 module_put(bus->owner); 1957 return err; 1958 } 1959 EXPORT_SYMBOL(phy_attach_direct); 1960 1961 /** 1962 * phy_attach - attach a network device to a particular PHY device 1963 * @dev: network device to attach 1964 * @bus_id: Bus ID of PHY device to attach 1965 * @interface: PHY device's interface 1966 * 1967 * Description: Same as phy_attach_direct() except that a PHY bus_id 1968 * string is passed instead of a pointer to a struct phy_device. 1969 */ 1970 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, 1971 phy_interface_t interface) 1972 { 1973 struct phy_device *phydev; 1974 struct device *d; 1975 int rc; 1976 1977 if (!dev) 1978 return ERR_PTR(-EINVAL); 1979 1980 /* Search the list of PHY devices on the mdio bus for the 1981 * PHY with the requested name 1982 */ 1983 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); 1984 if (!d) { 1985 pr_err("PHY %s not found\n", bus_id); 1986 return ERR_PTR(-ENODEV); 1987 } 1988 phydev = to_phy_device(d); 1989 1990 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); 1991 put_device(d); 1992 if (rc) 1993 return ERR_PTR(rc); 1994 1995 return phydev; 1996 } 1997 EXPORT_SYMBOL(phy_attach); 1998 1999 /** 2000 * phy_detach - detach a PHY device from its network device 2001 * @phydev: target phy_device struct 2002 * 2003 * This detaches the phy device from its network device and the phy 2004 * driver, and drops the reference count taken in phy_attach_direct(). 2005 */ 2006 void phy_detach(struct phy_device *phydev) 2007 { 2008 struct net_device *dev = phydev->attached_dev; 2009 struct module *ndev_owner = NULL; 2010 struct mii_bus *bus; 2011 2012 if (phydev->devlink) { 2013 device_link_del(phydev->devlink); 2014 phydev->devlink = NULL; 2015 } 2016 2017 if (phydev->sysfs_links) { 2018 if (dev) 2019 sysfs_remove_link(&dev->dev.kobj, "phydev"); 2020 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev"); 2021 } 2022 2023 if (!phydev->attached_dev) 2024 sysfs_remove_file(&phydev->mdio.dev.kobj, 2025 &dev_attr_phy_standalone.attr); 2026 2027 phy_suspend(phydev); 2028 if (dev) { 2029 struct hwtstamp_provider *hwprov; 2030 2031 hwprov = rtnl_dereference(dev->hwprov); 2032 /* Disable timestamp if it is the one selected */ 2033 if (hwprov && hwprov->phydev == phydev) { 2034 rcu_assign_pointer(dev->hwprov, NULL); 2035 kfree_rcu(hwprov, rcu_head); 2036 } 2037 2038 phydev->attached_dev->phydev = NULL; 2039 phydev->attached_dev = NULL; 2040 phy_link_topo_del_phy(dev, phydev); 2041 } 2042 2043 phydev->phy_link_change = NULL; 2044 phydev->phylink = NULL; 2045 2046 if (!phydev->is_on_sfp_module) 2047 phy_led_triggers_unregister(phydev); 2048 2049 if (phydev->mdio.dev.driver) 2050 module_put(phydev->mdio.dev.driver->owner); 2051 2052 /* If the device had no specific driver before (i.e. - it 2053 * was using the generic driver), we unbind the device 2054 * from the generic driver so that there's a chance a 2055 * real driver could be loaded 2056 */ 2057 if (phydev->is_genphy_driven) { 2058 device_release_driver(&phydev->mdio.dev); 2059 phydev->is_genphy_driven = 0; 2060 } 2061 2062 /* Assert the reset signal */ 2063 phy_device_reset(phydev, 1); 2064 2065 /* 2066 * The phydev might go away on the put_device() below, so avoid 2067 * a use-after-free bug by reading the underlying bus first. 2068 */ 2069 bus = phydev->mdio.bus; 2070 2071 put_device(&phydev->mdio.dev); 2072 if (dev) 2073 ndev_owner = dev->dev.parent->driver->owner; 2074 if (ndev_owner != bus->owner) 2075 module_put(bus->owner); 2076 } 2077 EXPORT_SYMBOL(phy_detach); 2078 2079 int phy_suspend(struct phy_device *phydev) 2080 { 2081 struct net_device *netdev = phydev->attached_dev; 2082 const struct phy_driver *phydrv = phydev->drv; 2083 int ret; 2084 2085 if (phydev->suspended || !phydrv) 2086 return 0; 2087 2088 phydev->wol_enabled = phy_may_wakeup(phydev) || 2089 (netdev && netdev->ethtool->wol_enabled); 2090 /* If the device has WOL enabled, we cannot suspend the PHY */ 2091 if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND)) 2092 return -EBUSY; 2093 2094 if (!phydrv->suspend) 2095 return 0; 2096 2097 ret = phydrv->suspend(phydev); 2098 if (!ret) 2099 phydev->suspended = true; 2100 2101 return ret; 2102 } 2103 EXPORT_SYMBOL(phy_suspend); 2104 2105 int __phy_resume(struct phy_device *phydev) 2106 { 2107 const struct phy_driver *phydrv = phydev->drv; 2108 int ret; 2109 2110 lockdep_assert_held(&phydev->lock); 2111 2112 if (!phydrv || !phydrv->resume) 2113 return 0; 2114 2115 ret = phydrv->resume(phydev); 2116 if (!ret) 2117 phydev->suspended = false; 2118 2119 return ret; 2120 } 2121 EXPORT_SYMBOL(__phy_resume); 2122 2123 int phy_resume(struct phy_device *phydev) 2124 { 2125 int ret; 2126 2127 mutex_lock(&phydev->lock); 2128 ret = __phy_resume(phydev); 2129 mutex_unlock(&phydev->lock); 2130 2131 return ret; 2132 } 2133 EXPORT_SYMBOL(phy_resume); 2134 2135 /** 2136 * phy_reset_after_clk_enable - perform a PHY reset if needed 2137 * @phydev: target phy_device struct 2138 * 2139 * Description: Some PHYs are known to need a reset after their refclk was 2140 * enabled. This function evaluates the flags and perform the reset if it's 2141 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy 2142 * was reset. 2143 */ 2144 int phy_reset_after_clk_enable(struct phy_device *phydev) 2145 { 2146 if (!phydev || !phydev->drv) 2147 return -ENODEV; 2148 2149 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) { 2150 phy_device_reset(phydev, 1); 2151 phy_device_reset(phydev, 0); 2152 return 1; 2153 } 2154 2155 return 0; 2156 } 2157 EXPORT_SYMBOL(phy_reset_after_clk_enable); 2158 2159 /* Generic PHY support and helper functions */ 2160 2161 /** 2162 * genphy_config_advert - sanitize and advertise auto-negotiation parameters 2163 * @phydev: target phy_device struct 2164 * @advert: auto-negotiation parameters to advertise 2165 * 2166 * Description: Writes MII_ADVERTISE with the appropriate values, 2167 * after sanitizing the values to make sure we only advertise 2168 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 2169 * hasn't changed, and > 0 if it has changed. 2170 */ 2171 static int genphy_config_advert(struct phy_device *phydev, 2172 const unsigned long *advert) 2173 { 2174 int err, bmsr, changed = 0; 2175 u32 adv; 2176 2177 adv = linkmode_adv_to_mii_adv_t(advert); 2178 2179 /* Setup standard advertisement */ 2180 err = phy_modify_changed(phydev, MII_ADVERTISE, 2181 ADVERTISE_ALL | ADVERTISE_100BASE4 | 2182 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM, 2183 adv); 2184 if (err < 0) 2185 return err; 2186 if (err > 0) 2187 changed = 1; 2188 2189 bmsr = phy_read(phydev, MII_BMSR); 2190 if (bmsr < 0) 2191 return bmsr; 2192 2193 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all 2194 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a 2195 * logical 1. 2196 */ 2197 if (!(bmsr & BMSR_ESTATEN)) 2198 return changed; 2199 2200 adv = linkmode_adv_to_mii_ctrl1000_t(advert); 2201 2202 err = phy_modify_changed(phydev, MII_CTRL1000, 2203 ADVERTISE_1000FULL | ADVERTISE_1000HALF, 2204 adv); 2205 if (err < 0) 2206 return err; 2207 if (err > 0) 2208 changed = 1; 2209 2210 return changed; 2211 } 2212 2213 /** 2214 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters 2215 * @phydev: target phy_device struct 2216 * 2217 * Description: Writes MII_ADVERTISE with the appropriate values, 2218 * after sanitizing the values to make sure we only advertise 2219 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 2220 * hasn't changed, and > 0 if it has changed. This function is intended 2221 * for Clause 37 1000Base-X mode. 2222 */ 2223 static int genphy_c37_config_advert(struct phy_device *phydev) 2224 { 2225 u16 adv = 0; 2226 2227 /* Only allow advertising what this PHY supports */ 2228 linkmode_and(phydev->advertising, phydev->advertising, 2229 phydev->supported); 2230 2231 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2232 phydev->advertising)) 2233 adv |= ADVERTISE_1000XFULL; 2234 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2235 phydev->advertising)) 2236 adv |= ADVERTISE_1000XPAUSE; 2237 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2238 phydev->advertising)) 2239 adv |= ADVERTISE_1000XPSE_ASYM; 2240 2241 return phy_modify_changed(phydev, MII_ADVERTISE, 2242 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE | 2243 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM, 2244 adv); 2245 } 2246 2247 /** 2248 * genphy_setup_forced - configures/forces speed/duplex from @phydev 2249 * @phydev: target phy_device struct 2250 * 2251 * Description: Configures MII_BMCR to force speed/duplex 2252 * to the values in phydev. Assumes that the values are valid. 2253 * Please see phy_sanitize_settings(). 2254 */ 2255 int genphy_setup_forced(struct phy_device *phydev) 2256 { 2257 u16 ctl; 2258 2259 phydev->pause = false; 2260 phydev->asym_pause = false; 2261 2262 ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 2263 2264 return phy_modify(phydev, MII_BMCR, 2265 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl); 2266 } 2267 EXPORT_SYMBOL(genphy_setup_forced); 2268 2269 static int genphy_setup_master_slave(struct phy_device *phydev) 2270 { 2271 u16 ctl = 0; 2272 2273 if (!phydev->is_gigabit_capable) 2274 return 0; 2275 2276 switch (phydev->master_slave_set) { 2277 case MASTER_SLAVE_CFG_MASTER_PREFERRED: 2278 ctl |= CTL1000_PREFER_MASTER; 2279 break; 2280 case MASTER_SLAVE_CFG_SLAVE_PREFERRED: 2281 break; 2282 case MASTER_SLAVE_CFG_MASTER_FORCE: 2283 ctl |= CTL1000_AS_MASTER; 2284 fallthrough; 2285 case MASTER_SLAVE_CFG_SLAVE_FORCE: 2286 ctl |= CTL1000_ENABLE_MASTER; 2287 break; 2288 case MASTER_SLAVE_CFG_UNKNOWN: 2289 case MASTER_SLAVE_CFG_UNSUPPORTED: 2290 return 0; 2291 default: 2292 phydev_warn(phydev, "Unsupported Master/Slave mode\n"); 2293 return -EOPNOTSUPP; 2294 } 2295 2296 return phy_modify_changed(phydev, MII_CTRL1000, 2297 (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER | 2298 CTL1000_PREFER_MASTER), ctl); 2299 } 2300 2301 int genphy_read_master_slave(struct phy_device *phydev) 2302 { 2303 int cfg, state; 2304 int val; 2305 2306 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN; 2307 phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN; 2308 2309 val = phy_read(phydev, MII_CTRL1000); 2310 if (val < 0) 2311 return val; 2312 2313 if (val & CTL1000_ENABLE_MASTER) { 2314 if (val & CTL1000_AS_MASTER) 2315 cfg = MASTER_SLAVE_CFG_MASTER_FORCE; 2316 else 2317 cfg = MASTER_SLAVE_CFG_SLAVE_FORCE; 2318 } else { 2319 if (val & CTL1000_PREFER_MASTER) 2320 cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED; 2321 else 2322 cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED; 2323 } 2324 2325 val = phy_read(phydev, MII_STAT1000); 2326 if (val < 0) 2327 return val; 2328 2329 if (val & LPA_1000MSFAIL) { 2330 state = MASTER_SLAVE_STATE_ERR; 2331 } else if (phydev->link) { 2332 /* this bits are valid only for active link */ 2333 if (val & LPA_1000MSRES) 2334 state = MASTER_SLAVE_STATE_MASTER; 2335 else 2336 state = MASTER_SLAVE_STATE_SLAVE; 2337 } else { 2338 state = MASTER_SLAVE_STATE_UNKNOWN; 2339 } 2340 2341 phydev->master_slave_get = cfg; 2342 phydev->master_slave_state = state; 2343 2344 return 0; 2345 } 2346 EXPORT_SYMBOL(genphy_read_master_slave); 2347 2348 /** 2349 * genphy_restart_aneg - Enable and Restart Autonegotiation 2350 * @phydev: target phy_device struct 2351 */ 2352 int genphy_restart_aneg(struct phy_device *phydev) 2353 { 2354 /* Don't isolate the PHY if we're negotiating */ 2355 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, 2356 BMCR_ANENABLE | BMCR_ANRESTART); 2357 } 2358 EXPORT_SYMBOL(genphy_restart_aneg); 2359 2360 /** 2361 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation 2362 * @phydev: target phy_device struct 2363 * @restart: whether aneg restart is requested 2364 * 2365 * Check, and restart auto-negotiation if needed. 2366 */ 2367 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart) 2368 { 2369 int ret; 2370 2371 if (!restart) { 2372 /* Advertisement hasn't changed, but maybe aneg was never on to 2373 * begin with? Or maybe phy was isolated? 2374 */ 2375 ret = phy_read(phydev, MII_BMCR); 2376 if (ret < 0) 2377 return ret; 2378 2379 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE)) 2380 restart = true; 2381 } 2382 2383 if (restart) 2384 return genphy_restart_aneg(phydev); 2385 2386 return 0; 2387 } 2388 EXPORT_SYMBOL(genphy_check_and_restart_aneg); 2389 2390 /** 2391 * __genphy_config_aneg - restart auto-negotiation or write BMCR 2392 * @phydev: target phy_device struct 2393 * @changed: whether autoneg is requested 2394 * 2395 * Description: If auto-negotiation is enabled, we configure the 2396 * advertising, and then restart auto-negotiation. If it is not 2397 * enabled, then we write the BMCR. 2398 */ 2399 int __genphy_config_aneg(struct phy_device *phydev, bool changed) 2400 { 2401 __ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert); 2402 const struct link_capabilities *c; 2403 unsigned long *advert; 2404 int err; 2405 2406 err = genphy_c45_an_config_eee_aneg(phydev); 2407 if (err < 0) 2408 return err; 2409 else if (err) 2410 changed = true; 2411 2412 err = genphy_setup_master_slave(phydev); 2413 if (err < 0) 2414 return err; 2415 else if (err) 2416 changed = true; 2417 2418 if (phydev->autoneg == AUTONEG_ENABLE) { 2419 /* Only allow advertising what this PHY supports */ 2420 linkmode_and(phydev->advertising, phydev->advertising, 2421 phydev->supported); 2422 advert = phydev->advertising; 2423 } else if (phydev->speed < SPEED_1000) { 2424 return genphy_setup_forced(phydev); 2425 } else { 2426 linkmode_zero(fixed_advert); 2427 2428 c = phy_caps_lookup(phydev->speed, phydev->duplex, 2429 phydev->supported, true); 2430 if (c) 2431 linkmode_and(fixed_advert, phydev->supported, 2432 c->linkmodes); 2433 2434 advert = fixed_advert; 2435 } 2436 2437 err = genphy_config_advert(phydev, advert); 2438 if (err < 0) /* error */ 2439 return err; 2440 else if (err) 2441 changed = true; 2442 2443 return genphy_check_and_restart_aneg(phydev, changed); 2444 } 2445 EXPORT_SYMBOL(__genphy_config_aneg); 2446 2447 /** 2448 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR 2449 * @phydev: target phy_device struct 2450 * 2451 * Description: If auto-negotiation is enabled, we configure the 2452 * advertising, and then restart auto-negotiation. If it is not 2453 * enabled, then we write the BMCR. This function is intended 2454 * for use with Clause 37 1000Base-X mode. 2455 */ 2456 int genphy_c37_config_aneg(struct phy_device *phydev) 2457 { 2458 int err, changed; 2459 2460 if (phydev->autoneg != AUTONEG_ENABLE) 2461 return genphy_setup_forced(phydev); 2462 2463 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100, 2464 BMCR_SPEED1000); 2465 if (err) 2466 return err; 2467 2468 changed = genphy_c37_config_advert(phydev); 2469 if (changed < 0) /* error */ 2470 return changed; 2471 2472 if (!changed) { 2473 /* Advertisement hasn't changed, but maybe aneg was never on to 2474 * begin with? Or maybe phy was isolated? 2475 */ 2476 int ctl = phy_read(phydev, MII_BMCR); 2477 2478 if (ctl < 0) 2479 return ctl; 2480 2481 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) 2482 changed = 1; /* do restart aneg */ 2483 } 2484 2485 /* Only restart aneg if we are advertising something different 2486 * than we were before. 2487 */ 2488 if (changed > 0) 2489 return genphy_restart_aneg(phydev); 2490 2491 return 0; 2492 } 2493 EXPORT_SYMBOL(genphy_c37_config_aneg); 2494 2495 /** 2496 * genphy_aneg_done - return auto-negotiation status 2497 * @phydev: target phy_device struct 2498 * 2499 * Description: Reads the status register and returns 0 either if 2500 * auto-negotiation is incomplete, or if there was an error. 2501 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. 2502 */ 2503 int genphy_aneg_done(struct phy_device *phydev) 2504 { 2505 int retval = phy_read(phydev, MII_BMSR); 2506 2507 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); 2508 } 2509 EXPORT_SYMBOL(genphy_aneg_done); 2510 2511 /** 2512 * genphy_update_link - update link status in @phydev 2513 * @phydev: target phy_device struct 2514 * 2515 * Description: Update the value in phydev->link to reflect the 2516 * current link value. In order to do this, we need to read 2517 * the status register twice, keeping the second value. 2518 */ 2519 int genphy_update_link(struct phy_device *phydev) 2520 { 2521 int status = 0, bmcr; 2522 2523 bmcr = phy_read(phydev, MII_BMCR); 2524 if (bmcr < 0) 2525 return bmcr; 2526 2527 /* Autoneg is being started, therefore disregard BMSR value and 2528 * report link as down. 2529 */ 2530 if (bmcr & BMCR_ANRESTART) 2531 goto done; 2532 2533 /* The link state is latched low so that momentary link 2534 * drops can be detected. Do not double-read the status 2535 * in polling mode to detect such short link drops except 2536 * the link was already down. 2537 */ 2538 if (!phy_polling_mode(phydev) || !phydev->link) { 2539 status = phy_read(phydev, MII_BMSR); 2540 if (status < 0) 2541 return status; 2542 else if (status & BMSR_LSTATUS) 2543 goto done; 2544 } 2545 2546 /* Read link and autonegotiation status */ 2547 status = phy_read(phydev, MII_BMSR); 2548 if (status < 0) 2549 return status; 2550 done: 2551 phydev->link = status & BMSR_LSTATUS ? 1 : 0; 2552 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0; 2553 2554 /* Consider the case that autoneg was started and "aneg complete" 2555 * bit has been reset, but "link up" bit not yet. 2556 */ 2557 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete) 2558 phydev->link = 0; 2559 2560 return 0; 2561 } 2562 EXPORT_SYMBOL(genphy_update_link); 2563 2564 int genphy_read_lpa(struct phy_device *phydev) 2565 { 2566 int lpa, lpagb; 2567 2568 if (phydev->autoneg == AUTONEG_ENABLE) { 2569 if (!phydev->autoneg_complete) { 2570 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 2571 0); 2572 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0); 2573 return 0; 2574 } 2575 2576 if (phydev->is_gigabit_capable) { 2577 lpagb = phy_read(phydev, MII_STAT1000); 2578 if (lpagb < 0) 2579 return lpagb; 2580 2581 if (lpagb & LPA_1000MSFAIL) { 2582 int adv = phy_read(phydev, MII_CTRL1000); 2583 2584 if (adv < 0) 2585 return adv; 2586 2587 if (adv & CTL1000_ENABLE_MASTER) 2588 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n"); 2589 else 2590 phydev_err(phydev, "Master/Slave resolution failed\n"); 2591 return -ENOLINK; 2592 } 2593 2594 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, 2595 lpagb); 2596 } 2597 2598 lpa = phy_read(phydev, MII_LPA); 2599 if (lpa < 0) 2600 return lpa; 2601 2602 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa); 2603 } else { 2604 linkmode_zero(phydev->lp_advertising); 2605 } 2606 2607 return 0; 2608 } 2609 EXPORT_SYMBOL(genphy_read_lpa); 2610 2611 /** 2612 * genphy_read_status_fixed - read the link parameters for !aneg mode 2613 * @phydev: target phy_device struct 2614 * 2615 * Read the current duplex and speed state for a PHY operating with 2616 * autonegotiation disabled. 2617 */ 2618 int genphy_read_status_fixed(struct phy_device *phydev) 2619 { 2620 int bmcr = phy_read(phydev, MII_BMCR); 2621 2622 if (bmcr < 0) 2623 return bmcr; 2624 2625 if (bmcr & BMCR_FULLDPLX) 2626 phydev->duplex = DUPLEX_FULL; 2627 else 2628 phydev->duplex = DUPLEX_HALF; 2629 2630 if (bmcr & BMCR_SPEED1000) 2631 phydev->speed = SPEED_1000; 2632 else if (bmcr & BMCR_SPEED100) 2633 phydev->speed = SPEED_100; 2634 else 2635 phydev->speed = SPEED_10; 2636 2637 return 0; 2638 } 2639 EXPORT_SYMBOL(genphy_read_status_fixed); 2640 2641 /** 2642 * genphy_read_status - check the link status and update current link state 2643 * @phydev: target phy_device struct 2644 * 2645 * Description: Check the link, then figure out the current state 2646 * by comparing what we advertise with what the link partner 2647 * advertises. Start by checking the gigabit possibilities, 2648 * then move on to 10/100. 2649 */ 2650 int genphy_read_status(struct phy_device *phydev) 2651 { 2652 int err, old_link = phydev->link; 2653 2654 /* Update the link, but return if there was an error */ 2655 err = genphy_update_link(phydev); 2656 if (err) 2657 return err; 2658 2659 /* why bother the PHY if nothing can have changed */ 2660 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) 2661 return 0; 2662 2663 phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED; 2664 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED; 2665 phydev->speed = SPEED_UNKNOWN; 2666 phydev->duplex = DUPLEX_UNKNOWN; 2667 phydev->pause = false; 2668 phydev->asym_pause = false; 2669 2670 if (phydev->is_gigabit_capable) { 2671 err = genphy_read_master_slave(phydev); 2672 if (err < 0) 2673 return err; 2674 } 2675 2676 err = genphy_read_lpa(phydev); 2677 if (err < 0) 2678 return err; 2679 2680 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { 2681 phy_resolve_aneg_linkmode(phydev); 2682 } else if (phydev->autoneg == AUTONEG_DISABLE) { 2683 err = genphy_read_status_fixed(phydev); 2684 if (err < 0) 2685 return err; 2686 } 2687 2688 return 0; 2689 } 2690 EXPORT_SYMBOL(genphy_read_status); 2691 2692 /** 2693 * genphy_c37_read_status - check the link status and update current link state 2694 * @phydev: target phy_device struct 2695 * @changed: pointer where to store if link changed 2696 * 2697 * Description: Check the link, then figure out the current state 2698 * by comparing what we advertise with what the link partner 2699 * advertises. This function is for Clause 37 1000Base-X mode. 2700 * 2701 * If link has changed, @changed is set to true, false otherwise. 2702 */ 2703 int genphy_c37_read_status(struct phy_device *phydev, bool *changed) 2704 { 2705 int lpa, err, old_link = phydev->link; 2706 2707 /* Update the link, but return if there was an error */ 2708 err = genphy_update_link(phydev); 2709 if (err) 2710 return err; 2711 2712 /* why bother the PHY if nothing can have changed */ 2713 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) { 2714 *changed = false; 2715 return 0; 2716 } 2717 2718 /* Signal link has changed */ 2719 *changed = true; 2720 phydev->duplex = DUPLEX_UNKNOWN; 2721 phydev->pause = false; 2722 phydev->asym_pause = false; 2723 2724 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) { 2725 lpa = phy_read(phydev, MII_LPA); 2726 if (lpa < 0) 2727 return lpa; 2728 2729 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 2730 phydev->lp_advertising, lpa & LPA_LPACK); 2731 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2732 phydev->lp_advertising, lpa & LPA_1000XFULL); 2733 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, 2734 phydev->lp_advertising, lpa & LPA_1000XPAUSE); 2735 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 2736 phydev->lp_advertising, 2737 lpa & LPA_1000XPAUSE_ASYM); 2738 2739 phy_resolve_aneg_linkmode(phydev); 2740 } else if (phydev->autoneg == AUTONEG_DISABLE) { 2741 int bmcr = phy_read(phydev, MII_BMCR); 2742 2743 if (bmcr < 0) 2744 return bmcr; 2745 2746 if (bmcr & BMCR_FULLDPLX) 2747 phydev->duplex = DUPLEX_FULL; 2748 else 2749 phydev->duplex = DUPLEX_HALF; 2750 } 2751 2752 return 0; 2753 } 2754 EXPORT_SYMBOL(genphy_c37_read_status); 2755 2756 /** 2757 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit 2758 * @phydev: target phy_device struct 2759 * 2760 * Description: Perform a software PHY reset using the standard 2761 * BMCR_RESET bit and poll for the reset bit to be cleared. 2762 * 2763 * Returns: 0 on success, < 0 on failure 2764 */ 2765 int genphy_soft_reset(struct phy_device *phydev) 2766 { 2767 u16 res = BMCR_RESET; 2768 int ret; 2769 2770 if (phydev->autoneg == AUTONEG_ENABLE) 2771 res |= BMCR_ANRESTART; 2772 2773 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res); 2774 if (ret < 0) 2775 return ret; 2776 2777 /* Clause 22 states that setting bit BMCR_RESET sets control registers 2778 * to their default value. Therefore the POWER DOWN bit is supposed to 2779 * be cleared after soft reset. 2780 */ 2781 phydev->suspended = 0; 2782 2783 ret = phy_poll_reset(phydev); 2784 if (ret) 2785 return ret; 2786 2787 /* BMCR may be reset to defaults */ 2788 if (phydev->autoneg == AUTONEG_DISABLE) 2789 ret = genphy_setup_forced(phydev); 2790 2791 return ret; 2792 } 2793 EXPORT_SYMBOL(genphy_soft_reset); 2794 2795 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev) 2796 { 2797 /* It seems there are cases where the interrupts are handled by another 2798 * entity (ie an IRQ controller embedded inside the PHY) and do not 2799 * need any other interraction from phylib. In this case, just trigger 2800 * the state machine directly. 2801 */ 2802 phy_trigger_machine(phydev); 2803 2804 return 0; 2805 } 2806 EXPORT_SYMBOL(genphy_handle_interrupt_no_ack); 2807 2808 /** 2809 * genphy_read_abilities - read PHY abilities from Clause 22 registers 2810 * @phydev: target phy_device struct 2811 * 2812 * Description: Reads the PHY's abilities and populates 2813 * phydev->supported accordingly. 2814 * 2815 * Returns: 0 on success, < 0 on failure 2816 */ 2817 int genphy_read_abilities(struct phy_device *phydev) 2818 { 2819 int val; 2820 2821 linkmode_set_bit_array(phy_basic_ports_array, 2822 ARRAY_SIZE(phy_basic_ports_array), 2823 phydev->supported); 2824 2825 val = phy_read(phydev, MII_BMSR); 2826 if (val < 0) 2827 return val; 2828 2829 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported, 2830 val & BMSR_ANEGCAPABLE); 2831 2832 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported, 2833 val & BMSR_100FULL); 2834 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported, 2835 val & BMSR_100HALF); 2836 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported, 2837 val & BMSR_10FULL); 2838 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported, 2839 val & BMSR_10HALF); 2840 2841 if (val & BMSR_ESTATEN) { 2842 val = phy_read(phydev, MII_ESTATUS); 2843 if (val < 0) 2844 return val; 2845 2846 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 2847 phydev->supported, val & ESTATUS_1000_TFULL); 2848 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 2849 phydev->supported, val & ESTATUS_1000_THALF); 2850 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT, 2851 phydev->supported, val & ESTATUS_1000_XFULL); 2852 } 2853 2854 /* This is optional functionality. If not supported, we may get an error 2855 * which should be ignored. 2856 */ 2857 genphy_c45_read_eee_abilities(phydev); 2858 2859 return 0; 2860 } 2861 EXPORT_SYMBOL(genphy_read_abilities); 2862 2863 /* This is used for the phy device which doesn't support the MMD extended 2864 * register access, but it does have side effect when we are trying to access 2865 * the MMD register via indirect method. 2866 */ 2867 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum) 2868 { 2869 return -EOPNOTSUPP; 2870 } 2871 EXPORT_SYMBOL(genphy_read_mmd_unsupported); 2872 2873 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum, 2874 u16 regnum, u16 val) 2875 { 2876 return -EOPNOTSUPP; 2877 } 2878 EXPORT_SYMBOL(genphy_write_mmd_unsupported); 2879 2880 int genphy_suspend(struct phy_device *phydev) 2881 { 2882 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN); 2883 } 2884 EXPORT_SYMBOL(genphy_suspend); 2885 2886 int genphy_resume(struct phy_device *phydev) 2887 { 2888 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN); 2889 } 2890 EXPORT_SYMBOL(genphy_resume); 2891 2892 int genphy_loopback(struct phy_device *phydev, bool enable, int speed) 2893 { 2894 if (enable) { 2895 u16 ctl = BMCR_LOOPBACK; 2896 int ret, val; 2897 2898 if (speed == SPEED_10 || speed == SPEED_100 || 2899 speed == SPEED_1000) 2900 phydev->speed = speed; 2901 else if (speed) 2902 return -EINVAL; 2903 2904 ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex); 2905 2906 phy_modify(phydev, MII_BMCR, ~0, ctl); 2907 2908 ret = phy_read_poll_timeout(phydev, MII_BMSR, val, 2909 val & BMSR_LSTATUS, 2910 5000, 500000, true); 2911 if (ret) 2912 return ret; 2913 } else { 2914 phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0); 2915 2916 phy_config_aneg(phydev); 2917 } 2918 2919 return 0; 2920 } 2921 EXPORT_SYMBOL(genphy_loopback); 2922 2923 /** 2924 * phy_remove_link_mode - Remove a supported link mode 2925 * @phydev: phy_device structure to remove link mode from 2926 * @link_mode: Link mode to be removed 2927 * 2928 * Description: Some MACs don't support all link modes which the PHY 2929 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper 2930 * to remove a link mode. 2931 */ 2932 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode) 2933 { 2934 linkmode_clear_bit(link_mode, phydev->supported); 2935 phy_advertise_supported(phydev); 2936 } 2937 EXPORT_SYMBOL(phy_remove_link_mode); 2938 2939 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src) 2940 { 2941 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst, 2942 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src)); 2943 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst, 2944 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src)); 2945 } 2946 2947 /** 2948 * phy_advertise_supported - Advertise all supported modes 2949 * @phydev: target phy_device struct 2950 * 2951 * Description: Called to advertise all supported modes, doesn't touch 2952 * pause mode advertising. 2953 */ 2954 void phy_advertise_supported(struct phy_device *phydev) 2955 { 2956 __ETHTOOL_DECLARE_LINK_MODE_MASK(new); 2957 2958 linkmode_copy(new, phydev->supported); 2959 phy_copy_pause_bits(new, phydev->advertising); 2960 linkmode_copy(phydev->advertising, new); 2961 } 2962 EXPORT_SYMBOL(phy_advertise_supported); 2963 2964 /** 2965 * phy_advertise_eee_all - Advertise all supported EEE modes 2966 * @phydev: target phy_device struct 2967 * 2968 * Description: Per default phylib preserves the EEE advertising at the time of 2969 * phy probing, which might be a subset of the supported EEE modes. Use this 2970 * function when all supported EEE modes should be advertised. This does not 2971 * trigger auto-negotiation, so must be called before phy_start()/ 2972 * phylink_start() which will start auto-negotiation. 2973 */ 2974 void phy_advertise_eee_all(struct phy_device *phydev) 2975 { 2976 linkmode_copy(phydev->advertising_eee, phydev->supported_eee); 2977 } 2978 EXPORT_SYMBOL_GPL(phy_advertise_eee_all); 2979 2980 /** 2981 * phy_support_eee - Set initial EEE policy configuration 2982 * @phydev: Target phy_device struct 2983 * 2984 * This function configures the initial policy for Energy Efficient Ethernet 2985 * (EEE) on the specified PHY device, influencing that EEE capabilities are 2986 * advertised before the link is established. It should be called during PHY 2987 * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs) 2988 * if MAC supports LPI or PHY is capable to compensate missing LPI functionality 2989 * of the MAC. 2990 * 2991 * The function sets default EEE policy parameters, including preparing the PHY 2992 * to advertise EEE capabilities based on hardware support. 2993 * 2994 * It also sets the expected configuration for Low Power Idle (LPI) in the MAC 2995 * driver. If the PHY framework determines that both local and remote 2996 * advertisements support EEE, and the negotiated link mode is compatible with 2997 * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on 2998 * this setting by enabling the LPI timer if enable_tx_lpi is set. 2999 */ 3000 void phy_support_eee(struct phy_device *phydev) 3001 { 3002 linkmode_copy(phydev->advertising_eee, phydev->supported_eee); 3003 phydev->eee_cfg.tx_lpi_enabled = true; 3004 phydev->eee_cfg.eee_enabled = true; 3005 } 3006 EXPORT_SYMBOL(phy_support_eee); 3007 3008 /** 3009 * phy_disable_eee - Disable EEE for the PHY 3010 * @phydev: Target phy_device struct 3011 * 3012 * This function is used by MAC drivers for MAC's which don't support EEE. 3013 * It disables EEE on the PHY layer. 3014 */ 3015 void phy_disable_eee(struct phy_device *phydev) 3016 { 3017 linkmode_zero(phydev->advertising_eee); 3018 phydev->eee_cfg.tx_lpi_enabled = false; 3019 phydev->eee_cfg.eee_enabled = false; 3020 /* don't let userspace re-enable EEE advertisement */ 3021 linkmode_fill(phydev->eee_disabled_modes); 3022 } 3023 EXPORT_SYMBOL_GPL(phy_disable_eee); 3024 3025 /** 3026 * phy_support_sym_pause - Enable support of symmetrical pause 3027 * @phydev: target phy_device struct 3028 * 3029 * Description: Called by the MAC to indicate is supports symmetrical 3030 * Pause, but not asym pause. 3031 */ 3032 void phy_support_sym_pause(struct phy_device *phydev) 3033 { 3034 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported); 3035 phy_copy_pause_bits(phydev->advertising, phydev->supported); 3036 } 3037 EXPORT_SYMBOL(phy_support_sym_pause); 3038 3039 /** 3040 * phy_support_asym_pause - Enable support of asym pause 3041 * @phydev: target phy_device struct 3042 * 3043 * Description: Called by the MAC to indicate is supports Asym Pause. 3044 */ 3045 void phy_support_asym_pause(struct phy_device *phydev) 3046 { 3047 phy_copy_pause_bits(phydev->advertising, phydev->supported); 3048 } 3049 EXPORT_SYMBOL(phy_support_asym_pause); 3050 3051 /** 3052 * phy_set_sym_pause - Configure symmetric Pause 3053 * @phydev: target phy_device struct 3054 * @rx: Receiver Pause is supported 3055 * @tx: Transmit Pause is supported 3056 * @autoneg: Auto neg should be used 3057 * 3058 * Description: Configure advertised Pause support depending on if 3059 * receiver pause and pause auto neg is supported. Generally called 3060 * from the set_pauseparam .ndo. 3061 */ 3062 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx, 3063 bool autoneg) 3064 { 3065 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported); 3066 3067 if (rx && tx && autoneg) 3068 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 3069 phydev->supported); 3070 3071 linkmode_copy(phydev->advertising, phydev->supported); 3072 } 3073 EXPORT_SYMBOL(phy_set_sym_pause); 3074 3075 /** 3076 * phy_set_asym_pause - Configure Pause and Asym Pause 3077 * @phydev: target phy_device struct 3078 * @rx: Receiver Pause is supported 3079 * @tx: Transmit Pause is supported 3080 * 3081 * Description: Configure advertised Pause support depending on if 3082 * transmit and receiver pause is supported. If there has been a 3083 * change in adverting, trigger a new autoneg. Generally called from 3084 * the set_pauseparam .ndo. 3085 */ 3086 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx) 3087 { 3088 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv); 3089 3090 linkmode_copy(oldadv, phydev->advertising); 3091 linkmode_set_pause(phydev->advertising, tx, rx); 3092 3093 if (!linkmode_equal(oldadv, phydev->advertising) && 3094 phydev->autoneg) 3095 phy_start_aneg(phydev); 3096 } 3097 EXPORT_SYMBOL(phy_set_asym_pause); 3098 3099 /** 3100 * phy_validate_pause - Test if the PHY/MAC support the pause configuration 3101 * @phydev: phy_device struct 3102 * @pp: requested pause configuration 3103 * 3104 * Description: Test if the PHY/MAC combination supports the Pause 3105 * configuration the user is requesting. Returns True if it is 3106 * supported, false otherwise. 3107 */ 3108 bool phy_validate_pause(struct phy_device *phydev, 3109 struct ethtool_pauseparam *pp) 3110 { 3111 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 3112 phydev->supported) && pp->rx_pause) 3113 return false; 3114 3115 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 3116 phydev->supported) && 3117 pp->rx_pause != pp->tx_pause) 3118 return false; 3119 3120 return true; 3121 } 3122 EXPORT_SYMBOL(phy_validate_pause); 3123 3124 /** 3125 * phy_get_pause - resolve negotiated pause modes 3126 * @phydev: phy_device struct 3127 * @tx_pause: pointer to bool to indicate whether transmit pause should be 3128 * enabled. 3129 * @rx_pause: pointer to bool to indicate whether receive pause should be 3130 * enabled. 3131 * 3132 * Resolve and return the flow control modes according to the negotiation 3133 * result. This includes checking that we are operating in full duplex mode. 3134 * See linkmode_resolve_pause() for further details. 3135 */ 3136 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause) 3137 { 3138 if (phydev->duplex != DUPLEX_FULL) { 3139 *tx_pause = false; 3140 *rx_pause = false; 3141 return; 3142 } 3143 3144 return linkmode_resolve_pause(phydev->advertising, 3145 phydev->lp_advertising, 3146 tx_pause, rx_pause); 3147 } 3148 EXPORT_SYMBOL(phy_get_pause); 3149 3150 #if IS_ENABLED(CONFIG_OF_MDIO) 3151 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) 3152 { 3153 return device_property_read_u32(dev, name, val); 3154 } 3155 #else 3156 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val) 3157 { 3158 return -EINVAL; 3159 } 3160 #endif 3161 3162 /** 3163 * phy_get_internal_delay - returns the index of the internal delay 3164 * @phydev: phy_device struct 3165 * @delay_values: array of delays the PHY supports 3166 * @size: the size of the delay array 3167 * @is_rx: boolean to indicate to get the rx internal delay 3168 * 3169 * Returns the index within the array of internal delay passed in. 3170 * If the device property is not present then the interface type is checked 3171 * if the interface defines use of internal delay then a 1 is returned otherwise 3172 * a 0 is returned. 3173 * The array must be in ascending order. If PHY does not have an ascending order 3174 * array then size = 0 and the value of the delay property is returned. 3175 * Return -EINVAL if the delay is invalid or cannot be found. 3176 */ 3177 s32 phy_get_internal_delay(struct phy_device *phydev, const int *delay_values, 3178 int size, bool is_rx) 3179 { 3180 struct device *dev = &phydev->mdio.dev; 3181 int i, ret; 3182 u32 delay; 3183 3184 if (is_rx) { 3185 ret = phy_get_u32_property(dev, "rx-internal-delay-ps", &delay); 3186 if (ret < 0 && size == 0) { 3187 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 3188 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) 3189 return 1; 3190 else 3191 return 0; 3192 } 3193 3194 } else { 3195 ret = phy_get_u32_property(dev, "tx-internal-delay-ps", &delay); 3196 if (ret < 0 && size == 0) { 3197 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID || 3198 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) 3199 return 1; 3200 else 3201 return 0; 3202 } 3203 } 3204 3205 if (ret < 0) 3206 return ret; 3207 3208 if (size == 0) 3209 return delay; 3210 3211 if (delay < delay_values[0] || delay > delay_values[size - 1]) { 3212 phydev_err(phydev, "Delay %d is out of range\n", delay); 3213 return -EINVAL; 3214 } 3215 3216 if (delay == delay_values[0]) 3217 return 0; 3218 3219 for (i = 1; i < size; i++) { 3220 if (delay == delay_values[i]) 3221 return i; 3222 3223 /* Find an approximate index by looking up the table */ 3224 if (delay > delay_values[i - 1] && 3225 delay < delay_values[i]) { 3226 if (delay - delay_values[i - 1] < 3227 delay_values[i] - delay) 3228 return i - 1; 3229 else 3230 return i; 3231 } 3232 } 3233 3234 phydev_err(phydev, "error finding internal delay index for %d\n", 3235 delay); 3236 3237 return -EINVAL; 3238 } 3239 EXPORT_SYMBOL(phy_get_internal_delay); 3240 3241 /** 3242 * phy_get_tx_amplitude_gain - stores tx amplitude gain in @val 3243 * @phydev: phy_device struct 3244 * @dev: pointer to the devices device struct 3245 * @linkmode: linkmode for which the tx amplitude gain should be retrieved 3246 * @val: tx amplitude gain 3247 * 3248 * Returns: 0 on success, < 0 on failure 3249 */ 3250 int phy_get_tx_amplitude_gain(struct phy_device *phydev, struct device *dev, 3251 enum ethtool_link_mode_bit_indices linkmode, 3252 u32 *val) 3253 { 3254 switch (linkmode) { 3255 case ETHTOOL_LINK_MODE_100baseT_Full_BIT: 3256 return phy_get_u32_property(dev, 3257 "tx-amplitude-100base-tx-percent", 3258 val); 3259 default: 3260 return -EINVAL; 3261 } 3262 } 3263 EXPORT_SYMBOL_GPL(phy_get_tx_amplitude_gain); 3264 3265 /** 3266 * phy_get_mac_termination - stores MAC termination in @val 3267 * @phydev: phy_device struct 3268 * @dev: pointer to the devices device struct 3269 * @val: MAC termination 3270 * 3271 * Returns: 0 on success, < 0 on failure 3272 */ 3273 int phy_get_mac_termination(struct phy_device *phydev, struct device *dev, 3274 u32 *val) 3275 { 3276 return phy_get_u32_property(dev, "mac-termination-ohms", val); 3277 } 3278 EXPORT_SYMBOL_GPL(phy_get_mac_termination); 3279 3280 static int phy_led_set_brightness(struct led_classdev *led_cdev, 3281 enum led_brightness value) 3282 { 3283 struct phy_led *phyled = to_phy_led(led_cdev); 3284 struct phy_device *phydev = phyled->phydev; 3285 int err; 3286 3287 mutex_lock(&phydev->lock); 3288 err = phydev->drv->led_brightness_set(phydev, phyled->index, value); 3289 mutex_unlock(&phydev->lock); 3290 3291 return err; 3292 } 3293 3294 static int phy_led_blink_set(struct led_classdev *led_cdev, 3295 unsigned long *delay_on, 3296 unsigned long *delay_off) 3297 { 3298 struct phy_led *phyled = to_phy_led(led_cdev); 3299 struct phy_device *phydev = phyled->phydev; 3300 int err; 3301 3302 mutex_lock(&phydev->lock); 3303 err = phydev->drv->led_blink_set(phydev, phyled->index, 3304 delay_on, delay_off); 3305 mutex_unlock(&phydev->lock); 3306 3307 return err; 3308 } 3309 3310 static __maybe_unused struct device * 3311 phy_led_hw_control_get_device(struct led_classdev *led_cdev) 3312 { 3313 struct phy_led *phyled = to_phy_led(led_cdev); 3314 struct phy_device *phydev = phyled->phydev; 3315 3316 if (phydev->attached_dev) 3317 return &phydev->attached_dev->dev; 3318 return NULL; 3319 } 3320 3321 static int __maybe_unused 3322 phy_led_hw_control_get(struct led_classdev *led_cdev, 3323 unsigned long *rules) 3324 { 3325 struct phy_led *phyled = to_phy_led(led_cdev); 3326 struct phy_device *phydev = phyled->phydev; 3327 int err; 3328 3329 mutex_lock(&phydev->lock); 3330 err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules); 3331 mutex_unlock(&phydev->lock); 3332 3333 return err; 3334 } 3335 3336 static int __maybe_unused 3337 phy_led_hw_control_set(struct led_classdev *led_cdev, 3338 unsigned long rules) 3339 { 3340 struct phy_led *phyled = to_phy_led(led_cdev); 3341 struct phy_device *phydev = phyled->phydev; 3342 int err; 3343 3344 mutex_lock(&phydev->lock); 3345 err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules); 3346 mutex_unlock(&phydev->lock); 3347 3348 return err; 3349 } 3350 3351 static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev, 3352 unsigned long rules) 3353 { 3354 struct phy_led *phyled = to_phy_led(led_cdev); 3355 struct phy_device *phydev = phyled->phydev; 3356 int err; 3357 3358 mutex_lock(&phydev->lock); 3359 err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules); 3360 mutex_unlock(&phydev->lock); 3361 3362 return err; 3363 } 3364 3365 static void phy_leds_unregister(struct phy_device *phydev) 3366 { 3367 struct phy_led *phyled, *tmp; 3368 3369 list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) { 3370 led_classdev_unregister(&phyled->led_cdev); 3371 list_del(&phyled->list); 3372 } 3373 } 3374 3375 static int of_phy_led(struct phy_device *phydev, 3376 struct device_node *led) 3377 { 3378 struct device *dev = &phydev->mdio.dev; 3379 struct led_init_data init_data = {}; 3380 struct led_classdev *cdev; 3381 unsigned long modes = 0; 3382 struct phy_led *phyled; 3383 u32 index; 3384 int err; 3385 3386 phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL); 3387 if (!phyled) 3388 return -ENOMEM; 3389 3390 cdev = &phyled->led_cdev; 3391 phyled->phydev = phydev; 3392 3393 err = of_property_read_u32(led, "reg", &index); 3394 if (err) 3395 return err; 3396 if (index > U8_MAX) 3397 return -EINVAL; 3398 3399 if (of_property_read_bool(led, "active-high")) 3400 set_bit(PHY_LED_ACTIVE_HIGH, &modes); 3401 if (of_property_read_bool(led, "active-low")) 3402 set_bit(PHY_LED_ACTIVE_LOW, &modes); 3403 if (of_property_read_bool(led, "inactive-high-impedance")) 3404 set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes); 3405 3406 if (WARN_ON(modes & BIT(PHY_LED_ACTIVE_LOW) && 3407 modes & BIT(PHY_LED_ACTIVE_HIGH))) 3408 return -EINVAL; 3409 3410 if (modes) { 3411 /* Return error if asked to set polarity modes but not supported */ 3412 if (!phydev->drv->led_polarity_set) 3413 return -EINVAL; 3414 3415 err = phydev->drv->led_polarity_set(phydev, index, modes); 3416 if (err) 3417 return err; 3418 } 3419 3420 phyled->index = index; 3421 if (phydev->drv->led_brightness_set) 3422 cdev->brightness_set_blocking = phy_led_set_brightness; 3423 if (phydev->drv->led_blink_set) 3424 cdev->blink_set = phy_led_blink_set; 3425 3426 #ifdef CONFIG_LEDS_TRIGGERS 3427 if (phydev->drv->led_hw_is_supported && 3428 phydev->drv->led_hw_control_set && 3429 phydev->drv->led_hw_control_get) { 3430 cdev->hw_control_is_supported = phy_led_hw_is_supported; 3431 cdev->hw_control_set = phy_led_hw_control_set; 3432 cdev->hw_control_get = phy_led_hw_control_get; 3433 cdev->hw_control_trigger = "netdev"; 3434 } 3435 3436 cdev->hw_control_get_device = phy_led_hw_control_get_device; 3437 #endif 3438 cdev->max_brightness = 1; 3439 init_data.devicename = dev_name(&phydev->mdio.dev); 3440 init_data.fwnode = of_fwnode_handle(led); 3441 init_data.devname_mandatory = true; 3442 3443 err = led_classdev_register_ext(dev, cdev, &init_data); 3444 if (err) 3445 return err; 3446 3447 list_add(&phyled->list, &phydev->leds); 3448 3449 return 0; 3450 } 3451 3452 static int of_phy_leds(struct phy_device *phydev) 3453 { 3454 struct device_node *node = phydev->mdio.dev.of_node; 3455 struct device_node *leds; 3456 int err; 3457 3458 if (!IS_ENABLED(CONFIG_OF_MDIO)) 3459 return 0; 3460 3461 if (!node) 3462 return 0; 3463 3464 leds = of_get_child_by_name(node, "leds"); 3465 if (!leds) 3466 return 0; 3467 3468 /* Check if the PHY driver have at least an OP to 3469 * set the LEDs. 3470 */ 3471 if (!(phydev->drv->led_brightness_set || 3472 phydev->drv->led_blink_set || 3473 phydev->drv->led_hw_control_set)) { 3474 phydev_dbg(phydev, "ignoring leds node defined with no PHY driver support\n"); 3475 goto exit; 3476 } 3477 3478 for_each_available_child_of_node_scoped(leds, led) { 3479 err = of_phy_led(phydev, led); 3480 if (err) { 3481 of_node_put(leds); 3482 phy_leds_unregister(phydev); 3483 return err; 3484 } 3485 } 3486 3487 exit: 3488 of_node_put(leds); 3489 return 0; 3490 } 3491 3492 static void phy_cleanup_ports(struct phy_device *phydev) 3493 { 3494 struct phy_port *tmp, *port; 3495 3496 list_for_each_entry_safe(port, tmp, &phydev->ports, head) { 3497 phy_del_port(phydev, port); 3498 phy_port_destroy(port); 3499 } 3500 } 3501 3502 static int phy_default_setup_single_port(struct phy_device *phydev) 3503 { 3504 struct phy_port *port = phy_port_alloc(); 3505 unsigned long mode; 3506 3507 if (!port) 3508 return -ENOMEM; 3509 3510 port->parent_type = PHY_PORT_PHY; 3511 port->phy = phydev; 3512 3513 /* Let the PHY driver know that this port was never described anywhere. 3514 * This is the usual case, where we assume single-port PHY devices with 3515 * no SFP. In that case, the port supports exactly the same thing as 3516 * the PHY itself. 3517 * 3518 * However, this can also be because we have a combo-port PHY, with 3519 * only one port described in DT, through SFP for example. 3520 * 3521 * In that case, the PHY driver will be in charge of saying what we can 3522 * do on that non-represented port. 3523 */ 3524 port->not_described = true; 3525 linkmode_copy(port->supported, phydev->supported); 3526 port->mediums = phy_caps_mediums_from_linkmodes(port->supported); 3527 3528 for_each_set_bit(mode, port->supported, __ETHTOOL_LINK_MODE_MASK_NBITS) 3529 port->pairs = max_t(int, port->pairs, 3530 ethtool_linkmode_n_pairs(mode)); 3531 3532 phy_add_port(phydev, port); 3533 3534 return 0; 3535 } 3536 3537 static int of_phy_ports(struct phy_device *phydev) 3538 { 3539 struct device_node *node = phydev->mdio.dev.of_node; 3540 struct device_node *mdi; 3541 struct phy_port *port; 3542 int err; 3543 3544 if (!IS_ENABLED(CONFIG_OF_MDIO)) 3545 return 0; 3546 3547 if (!node) 3548 return 0; 3549 3550 mdi = of_get_child_by_name(node, "mdi"); 3551 if (!mdi) 3552 return 0; 3553 3554 for_each_available_child_of_node_scoped(mdi, port_node) { 3555 port = phy_of_parse_port(port_node); 3556 if (IS_ERR(port)) { 3557 err = PTR_ERR(port); 3558 goto out_err; 3559 } 3560 3561 port->parent_type = PHY_PORT_PHY; 3562 port->phy = phydev; 3563 err = phy_add_port(phydev, port); 3564 if (err) { 3565 phy_port_destroy(port); 3566 goto out_err; 3567 } 3568 } 3569 of_node_put(mdi); 3570 3571 return 0; 3572 3573 out_err: 3574 phy_cleanup_ports(phydev); 3575 of_node_put(mdi); 3576 return err; 3577 } 3578 3579 static int phy_setup_ports(struct phy_device *phydev) 3580 { 3581 __ETHTOOL_DECLARE_LINK_MODE_MASK(ports_supported); 3582 struct phy_port *port; 3583 int ret; 3584 3585 ret = of_phy_ports(phydev); 3586 if (ret) 3587 return ret; 3588 3589 /* Use generic SFP probing only if the driver didn't do so already */ 3590 if (!phydev->sfp_bus) { 3591 ret = phy_sfp_probe(phydev, &sfp_phydev_ops); 3592 if (ret) 3593 goto out; 3594 } 3595 3596 if (phydev->n_ports < phydev->max_n_ports) { 3597 ret = phy_default_setup_single_port(phydev); 3598 if (ret) 3599 goto out; 3600 } 3601 3602 linkmode_zero(ports_supported); 3603 3604 /* Aggregate the supported modes, which are made-up of : 3605 * - What the PHY itself supports 3606 * - What the sum of all ports support 3607 */ 3608 list_for_each_entry(port, &phydev->ports, head) 3609 if (port->active) 3610 linkmode_or(ports_supported, ports_supported, 3611 port->supported); 3612 3613 if (!linkmode_empty(ports_supported)) 3614 linkmode_and(phydev->supported, phydev->supported, 3615 ports_supported); 3616 3617 /* For now, the phy->port field is set as the first active port's type */ 3618 list_for_each_entry(port, &phydev->ports, head) 3619 if (port->active) { 3620 phydev->port = phy_port_get_type(port); 3621 break; 3622 } 3623 3624 return 0; 3625 3626 out: 3627 phy_cleanup_ports(phydev); 3628 return ret; 3629 } 3630 3631 /** 3632 * phy_get_sfp_port() - Returns the first valid SFP port of a PHY 3633 * @phydev: pointer to the PHY device to get the SFP port from 3634 * 3635 * Returns: The first active SFP (serdes) port of a PHY device, NULL if none 3636 * exist. 3637 */ 3638 struct phy_port *phy_get_sfp_port(struct phy_device *phydev) 3639 { 3640 struct phy_port *port; 3641 3642 list_for_each_entry(port, &phydev->ports, head) 3643 if (port->active && port->is_sfp) 3644 return port; 3645 3646 return NULL; 3647 } 3648 EXPORT_SYMBOL_GPL(phy_get_sfp_port); 3649 3650 /** 3651 * fwnode_mdio_find_device - Given a fwnode, find the mdio_device 3652 * @fwnode: pointer to the mdio_device's fwnode 3653 * 3654 * If successful, returns a pointer to the mdio_device with the embedded 3655 * struct device refcount incremented by one, or NULL on failure. 3656 * The caller should call put_device() on the mdio_device after its use. 3657 */ 3658 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode) 3659 { 3660 struct device *d; 3661 3662 if (!fwnode) 3663 return NULL; 3664 3665 d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode); 3666 if (!d) 3667 return NULL; 3668 3669 return to_mdio_device(d); 3670 } 3671 EXPORT_SYMBOL(fwnode_mdio_find_device); 3672 3673 /** 3674 * fwnode_phy_find_device - For provided phy_fwnode, find phy_device. 3675 * 3676 * @phy_fwnode: Pointer to the phy's fwnode. 3677 * 3678 * If successful, returns a pointer to the phy_device with the embedded 3679 * struct device refcount incremented by one, or NULL on failure. 3680 */ 3681 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode) 3682 { 3683 struct mdio_device *mdiodev; 3684 3685 mdiodev = fwnode_mdio_find_device(phy_fwnode); 3686 if (!mdiodev) 3687 return NULL; 3688 3689 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) 3690 return to_phy_device(&mdiodev->dev); 3691 3692 put_device(&mdiodev->dev); 3693 3694 return NULL; 3695 } 3696 EXPORT_SYMBOL(fwnode_phy_find_device); 3697 3698 /** 3699 * fwnode_get_phy_node - Get the phy_node using the named reference. 3700 * @fwnode: Pointer to fwnode from which phy_node has to be obtained. 3701 * 3702 * Refer return conditions of fwnode_find_reference(). 3703 * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy" 3704 * and "phy-device" are not supported in ACPI. DT supports all the three 3705 * named references to the phy node. 3706 */ 3707 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode) 3708 { 3709 struct fwnode_handle *phy_node; 3710 3711 /* Only phy-handle is used for ACPI */ 3712 phy_node = fwnode_find_reference(fwnode, "phy-handle", 0); 3713 if (!IS_ERR(phy_node) || is_acpi_node(fwnode)) 3714 return phy_node; 3715 phy_node = fwnode_find_reference(fwnode, "phy", 0); 3716 if (!IS_ERR(phy_node)) 3717 return phy_node; 3718 return fwnode_find_reference(fwnode, "phy-device", 0); 3719 } 3720 EXPORT_SYMBOL_GPL(fwnode_get_phy_node); 3721 3722 /** 3723 * phy_probe - probe and init a PHY device 3724 * @dev: device to probe and init 3725 * 3726 * Take care of setting up the phy_device structure, set the state to READY. 3727 */ 3728 static int phy_probe(struct device *dev) 3729 { 3730 struct phy_device *phydev = to_phy_device(dev); 3731 struct device_driver *drv = phydev->mdio.dev.driver; 3732 struct phy_driver *phydrv = to_phy_driver(drv); 3733 int err = 0; 3734 3735 phydev->drv = phydrv; 3736 3737 /* Disable the interrupt if the PHY doesn't support it 3738 * but the interrupt is still a valid one 3739 */ 3740 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev)) 3741 phydev->irq = PHY_POLL; 3742 3743 if (phydrv->flags & PHY_IS_INTERNAL) 3744 phydev->is_internal = true; 3745 3746 /* Deassert the reset signal */ 3747 phy_device_reset(phydev, 0); 3748 3749 if (phydev->drv->probe) { 3750 err = phydev->drv->probe(phydev); 3751 if (err) 3752 goto out; 3753 } 3754 3755 phy_disable_interrupts(phydev); 3756 3757 /* Start out supporting everything. Eventually, 3758 * a controller will attach, and may modify one 3759 * or both of these values 3760 */ 3761 if (phydrv->features) { 3762 linkmode_copy(phydev->supported, phydrv->features); 3763 genphy_c45_read_eee_abilities(phydev); 3764 } 3765 else if (phydrv->get_features) 3766 err = phydrv->get_features(phydev); 3767 else if (phydev->is_c45) 3768 err = genphy_c45_pma_read_abilities(phydev); 3769 else 3770 err = genphy_read_abilities(phydev); 3771 3772 if (err) 3773 goto out; 3774 3775 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 3776 phydev->supported)) 3777 phydev->autoneg = 0; 3778 3779 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 3780 phydev->supported)) 3781 phydev->is_gigabit_capable = 1; 3782 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 3783 phydev->supported)) 3784 phydev->is_gigabit_capable = 1; 3785 3786 of_set_phy_supported(phydev); 3787 3788 err = phy_setup_ports(phydev); 3789 if (err) 3790 goto out; 3791 3792 phy_advertise_supported(phydev); 3793 3794 /* Get PHY default EEE advertising modes and handle them as potentially 3795 * safe initial configuration. 3796 */ 3797 err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee); 3798 if (err) 3799 goto out; 3800 3801 /* Get the EEE modes we want to prohibit. */ 3802 of_set_phy_eee_broken(phydev); 3803 3804 /* Some PHYs may advertise, by default, not support EEE modes. So, 3805 * we need to clean them. In addition remove all disabled EEE modes. 3806 */ 3807 linkmode_and(phydev->advertising_eee, phydev->supported_eee, 3808 phydev->advertising_eee); 3809 linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee, 3810 phydev->eee_disabled_modes); 3811 3812 /* There is no "enabled" flag. If PHY is advertising, assume it is 3813 * kind of enabled. 3814 */ 3815 phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee); 3816 3817 /* Get master/slave strap overrides */ 3818 of_set_phy_timing_role(phydev); 3819 3820 /* The Pause Frame bits indicate that the PHY can support passing 3821 * pause frames. During autonegotiation, the PHYs will determine if 3822 * they should allow pause frames to pass. The MAC driver should then 3823 * use that result to determine whether to enable flow control via 3824 * pause frames. 3825 * 3826 * Normally, PHY drivers should not set the Pause bits, and instead 3827 * allow phylib to do that. However, there may be some situations 3828 * (e.g. hardware erratum) where the driver wants to set only one 3829 * of these bits. 3830 */ 3831 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) && 3832 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) { 3833 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, 3834 phydev->supported); 3835 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 3836 phydev->supported); 3837 } 3838 3839 /* Set the state to READY by default */ 3840 phydev->state = PHY_READY; 3841 3842 /* Get the LEDs from the device tree, and instantiate standard 3843 * LEDs for them. 3844 */ 3845 if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev)) 3846 err = of_phy_leds(phydev); 3847 3848 out: 3849 /* Re-assert the reset signal on error */ 3850 if (err) 3851 phy_device_reset(phydev, 1); 3852 3853 return err; 3854 } 3855 3856 static int phy_remove(struct device *dev) 3857 { 3858 struct phy_device *phydev = to_phy_device(dev); 3859 3860 cancel_delayed_work_sync(&phydev->state_queue); 3861 3862 if (IS_ENABLED(CONFIG_PHYLIB_LEDS) && !phy_driver_is_genphy(phydev)) 3863 phy_leds_unregister(phydev); 3864 3865 phydev->state = PHY_DOWN; 3866 3867 phy_cleanup_ports(phydev); 3868 3869 sfp_bus_del_upstream(phydev->sfp_bus); 3870 phydev->sfp_bus = NULL; 3871 3872 if (phydev->drv && phydev->drv->remove) 3873 phydev->drv->remove(phydev); 3874 3875 /* Assert the reset signal */ 3876 phy_device_reset(phydev, 1); 3877 3878 phydev->drv = NULL; 3879 3880 return 0; 3881 } 3882 3883 /** 3884 * phy_driver_register - register a phy_driver with the PHY layer 3885 * @new_driver: new phy_driver to register 3886 * @owner: module owning this PHY 3887 */ 3888 static int phy_driver_register(struct phy_driver *new_driver, 3889 struct module *owner) 3890 { 3891 int retval; 3892 3893 /* Either the features are hard coded, or dynamically 3894 * determined. It cannot be both. 3895 */ 3896 if (WARN_ON(new_driver->features && new_driver->get_features)) { 3897 pr_err("%s: features and get_features must not both be set\n", 3898 new_driver->name); 3899 return -EINVAL; 3900 } 3901 3902 /* PHYLIB device drivers must not match using a DT compatible table 3903 * as this bypasses our checks that the mdiodev that is being matched 3904 * is backed by a struct phy_device. If such a case happens, we will 3905 * make out-of-bounds accesses and lockup in phydev->lock. 3906 */ 3907 if (WARN(new_driver->mdiodrv.driver.of_match_table, 3908 "%s: driver must not provide a DT match table\n", 3909 new_driver->name)) 3910 return -EINVAL; 3911 3912 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY; 3913 new_driver->mdiodrv.driver.name = new_driver->name; 3914 new_driver->mdiodrv.driver.bus = &mdio_bus_type; 3915 new_driver->mdiodrv.driver.probe = phy_probe; 3916 new_driver->mdiodrv.driver.remove = phy_remove; 3917 new_driver->mdiodrv.driver.owner = owner; 3918 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS; 3919 3920 retval = driver_register(&new_driver->mdiodrv.driver); 3921 if (retval) { 3922 pr_err("%s: Error %d in registering driver\n", 3923 new_driver->name, retval); 3924 3925 return retval; 3926 } 3927 3928 pr_debug("%s: Registered new driver\n", new_driver->name); 3929 3930 return 0; 3931 } 3932 3933 static void phy_driver_unregister(struct phy_driver *drv) 3934 { 3935 driver_unregister(&drv->mdiodrv.driver); 3936 } 3937 3938 int phy_drivers_register(struct phy_driver *new_driver, int n, 3939 struct module *owner) 3940 { 3941 int i, ret = 0; 3942 3943 for (i = 0; i < n; i++) { 3944 ret = phy_driver_register(new_driver + i, owner); 3945 if (ret) { 3946 while (i-- > 0) 3947 phy_driver_unregister(new_driver + i); 3948 break; 3949 } 3950 } 3951 return ret; 3952 } 3953 EXPORT_SYMBOL(phy_drivers_register); 3954 3955 void phy_drivers_unregister(struct phy_driver *drv, int n) 3956 { 3957 int i; 3958 3959 for (i = 0; i < n; i++) 3960 phy_driver_unregister(drv + i); 3961 } 3962 EXPORT_SYMBOL(phy_drivers_unregister); 3963 3964 static struct phy_driver genphy_driver = { 3965 .phy_id = 0xffffffff, 3966 .phy_id_mask = 0xffffffff, 3967 .name = "Generic PHY", 3968 .get_features = genphy_read_abilities, 3969 .suspend = genphy_suspend, 3970 .resume = genphy_resume, 3971 .set_loopback = genphy_loopback, 3972 }; 3973 3974 static const struct ethtool_phy_ops phy_ethtool_phy_ops = { 3975 .get_sset_count = phy_ethtool_get_sset_count, 3976 .get_strings = phy_ethtool_get_strings, 3977 .get_stats = phy_ethtool_get_stats, 3978 .get_plca_cfg = phy_ethtool_get_plca_cfg, 3979 .set_plca_cfg = phy_ethtool_set_plca_cfg, 3980 .get_plca_status = phy_ethtool_get_plca_status, 3981 .start_cable_test = phy_start_cable_test, 3982 .start_cable_test_tdr = phy_start_cable_test_tdr, 3983 }; 3984 3985 static const struct phylib_stubs __phylib_stubs = { 3986 .hwtstamp_get = __phy_hwtstamp_get, 3987 .hwtstamp_set = __phy_hwtstamp_set, 3988 .get_phy_stats = __phy_ethtool_get_phy_stats, 3989 .get_link_ext_stats = __phy_ethtool_get_link_ext_stats, 3990 }; 3991 3992 static void phylib_register_stubs(void) 3993 { 3994 phylib_stubs = &__phylib_stubs; 3995 } 3996 3997 static void phylib_unregister_stubs(void) 3998 { 3999 phylib_stubs = NULL; 4000 } 4001 4002 static int __init phy_init(void) 4003 { 4004 int rc; 4005 4006 rtnl_lock(); 4007 ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops); 4008 phylib_register_stubs(); 4009 rtnl_unlock(); 4010 4011 rc = phy_caps_init(); 4012 if (rc) 4013 goto err_ethtool_phy_ops; 4014 4015 features_init(); 4016 4017 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE); 4018 if (rc) 4019 goto err_ethtool_phy_ops; 4020 4021 rc = phy_driver_register(&genphy_driver, THIS_MODULE); 4022 if (rc) 4023 goto err_c45; 4024 4025 return 0; 4026 4027 err_c45: 4028 phy_driver_unregister(&genphy_c45_driver); 4029 err_ethtool_phy_ops: 4030 rtnl_lock(); 4031 phylib_unregister_stubs(); 4032 ethtool_set_ethtool_phy_ops(NULL); 4033 rtnl_unlock(); 4034 4035 return rc; 4036 } 4037 4038 static void __exit phy_exit(void) 4039 { 4040 phy_driver_unregister(&genphy_c45_driver); 4041 phy_driver_unregister(&genphy_driver); 4042 rtnl_lock(); 4043 phylib_unregister_stubs(); 4044 ethtool_set_ethtool_phy_ops(NULL); 4045 rtnl_unlock(); 4046 } 4047 4048 subsys_initcall(phy_init); 4049 module_exit(phy_exit); 4050