root/drivers/net/phy/phy_device.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. features_init
  2. phy_device_free
  3. phy_mdio_device_free
  4. phy_device_release
  5. phy_mdio_device_remove
  6. mdio_bus_phy_may_suspend
  7. mdio_bus_phy_suspend
  8. mdio_bus_phy_resume
  9. mdio_bus_phy_restore
  10. phy_register_fixup
  11. phy_register_fixup_for_uid
  12. phy_register_fixup_for_id
  13. phy_unregister_fixup
  14. phy_unregister_fixup_for_uid
  15. phy_unregister_fixup_for_id
  16. phy_needs_fixup
  17. phy_scan_fixups
  18. phy_bus_match
  19. phy_id_show
  20. phy_interface_show
  21. phy_has_fixups_show
  22. phy_request_driver_module
  23. phy_device_create
  24. get_phy_c45_devs_in_pkg
  25. get_phy_c45_ids
  26. get_phy_id
  27. get_phy_device
  28. phy_device_register
  29. phy_device_remove
  30. phy_find_first
  31. phy_link_change
  32. phy_prepare_link
  33. phy_connect_direct
  34. phy_connect
  35. phy_disconnect
  36. phy_poll_reset
  37. phy_init_hw
  38. phy_attached_info
  39. phy_attached_print
  40. phy_sysfs_create_links
  41. phy_standalone_show
  42. phy_attach_direct
  43. phy_attach
  44. phy_driver_is_genphy_kind
  45. phy_driver_is_genphy
  46. phy_driver_is_genphy_10g
  47. phy_detach
  48. phy_suspend
  49. __phy_resume
  50. phy_resume
  51. phy_loopback
  52. phy_reset_after_clk_enable
  53. genphy_config_advert
  54. genphy_config_eee_advert
  55. genphy_setup_forced
  56. genphy_restart_aneg
  57. __genphy_config_aneg
  58. genphy_aneg_done
  59. genphy_update_link
  60. genphy_read_lpa
  61. genphy_read_status
  62. genphy_soft_reset
  63. genphy_read_abilities
  64. genphy_read_mmd_unsupported
  65. genphy_write_mmd_unsupported
  66. genphy_suspend
  67. genphy_resume
  68. genphy_loopback
  69. phy_remove_link_mode
  70. phy_copy_pause_bits
  71. phy_advertise_supported
  72. phy_support_sym_pause
  73. phy_support_asym_pause
  74. phy_set_sym_pause
  75. phy_set_asym_pause
  76. phy_validate_pause
  77. phy_drv_supports_irq
  78. phy_probe
  79. phy_remove
  80. phy_driver_register
  81. phy_drivers_register
  82. phy_driver_unregister
  83. phy_drivers_unregister
  84. phy_init
  85. phy_exit

   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/kernel.h>
  13 #include <linux/string.h>
  14 #include <linux/errno.h>
  15 #include <linux/unistd.h>
  16 #include <linux/slab.h>
  17 #include <linux/interrupt.h>
  18 #include <linux/init.h>
  19 #include <linux/delay.h>
  20 #include <linux/netdevice.h>
  21 #include <linux/etherdevice.h>
  22 #include <linux/skbuff.h>
  23 #include <linux/mm.h>
  24 #include <linux/module.h>
  25 #include <linux/mii.h>
  26 #include <linux/ethtool.h>
  27 #include <linux/bitmap.h>
  28 #include <linux/phy.h>
  29 #include <linux/phy_led_triggers.h>
  30 #include <linux/mdio.h>
  31 #include <linux/io.h>
  32 #include <linux/uaccess.h>
  33 
  34 MODULE_DESCRIPTION("PHY library");
  35 MODULE_AUTHOR("Andy Fleming");
  36 MODULE_LICENSE("GPL");
  37 
  38 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
  39 EXPORT_SYMBOL_GPL(phy_basic_features);
  40 
  41 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
  42 EXPORT_SYMBOL_GPL(phy_basic_t1_features);
  43 
  44 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
  45 EXPORT_SYMBOL_GPL(phy_gbit_features);
  46 
  47 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
  48 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
  49 
  50 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
  51 EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
  52 
  53 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
  54 EXPORT_SYMBOL_GPL(phy_10gbit_features);
  55 
  56 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
  57 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
  58 
  59 const int phy_basic_ports_array[3] = {
  60         ETHTOOL_LINK_MODE_Autoneg_BIT,
  61         ETHTOOL_LINK_MODE_TP_BIT,
  62         ETHTOOL_LINK_MODE_MII_BIT,
  63 };
  64 EXPORT_SYMBOL_GPL(phy_basic_ports_array);
  65 
  66 const int phy_fibre_port_array[1] = {
  67         ETHTOOL_LINK_MODE_FIBRE_BIT,
  68 };
  69 EXPORT_SYMBOL_GPL(phy_fibre_port_array);
  70 
  71 const int phy_all_ports_features_array[7] = {
  72         ETHTOOL_LINK_MODE_Autoneg_BIT,
  73         ETHTOOL_LINK_MODE_TP_BIT,
  74         ETHTOOL_LINK_MODE_MII_BIT,
  75         ETHTOOL_LINK_MODE_FIBRE_BIT,
  76         ETHTOOL_LINK_MODE_AUI_BIT,
  77         ETHTOOL_LINK_MODE_BNC_BIT,
  78         ETHTOOL_LINK_MODE_Backplane_BIT,
  79 };
  80 EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
  81 
  82 const int phy_10_100_features_array[4] = {
  83         ETHTOOL_LINK_MODE_10baseT_Half_BIT,
  84         ETHTOOL_LINK_MODE_10baseT_Full_BIT,
  85         ETHTOOL_LINK_MODE_100baseT_Half_BIT,
  86         ETHTOOL_LINK_MODE_100baseT_Full_BIT,
  87 };
  88 EXPORT_SYMBOL_GPL(phy_10_100_features_array);
  89 
  90 const int phy_basic_t1_features_array[2] = {
  91         ETHTOOL_LINK_MODE_TP_BIT,
  92         ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
  93 };
  94 EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
  95 
  96 const int phy_gbit_features_array[2] = {
  97         ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  98         ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  99 };
 100 EXPORT_SYMBOL_GPL(phy_gbit_features_array);
 101 
 102 const int phy_10gbit_features_array[1] = {
 103         ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
 104 };
 105 EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
 106 
 107 const int phy_10gbit_fec_features_array[1] = {
 108         ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
 109 };
 110 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features_array);
 111 
 112 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
 113 EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
 114 
 115 static const int phy_10gbit_full_features_array[] = {
 116         ETHTOOL_LINK_MODE_10baseT_Full_BIT,
 117         ETHTOOL_LINK_MODE_100baseT_Full_BIT,
 118         ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
 119         ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
 120 };
 121 
 122 static void features_init(void)
 123 {
 124         /* 10/100 half/full*/
 125         linkmode_set_bit_array(phy_basic_ports_array,
 126                                ARRAY_SIZE(phy_basic_ports_array),
 127                                phy_basic_features);
 128         linkmode_set_bit_array(phy_10_100_features_array,
 129                                ARRAY_SIZE(phy_10_100_features_array),
 130                                phy_basic_features);
 131 
 132         /* 100 full, TP */
 133         linkmode_set_bit_array(phy_basic_t1_features_array,
 134                                ARRAY_SIZE(phy_basic_t1_features_array),
 135                                phy_basic_t1_features);
 136 
 137         /* 10/100 half/full + 1000 half/full */
 138         linkmode_set_bit_array(phy_basic_ports_array,
 139                                ARRAY_SIZE(phy_basic_ports_array),
 140                                phy_gbit_features);
 141         linkmode_set_bit_array(phy_10_100_features_array,
 142                                ARRAY_SIZE(phy_10_100_features_array),
 143                                phy_gbit_features);
 144         linkmode_set_bit_array(phy_gbit_features_array,
 145                                ARRAY_SIZE(phy_gbit_features_array),
 146                                phy_gbit_features);
 147 
 148         /* 10/100 half/full + 1000 half/full + fibre*/
 149         linkmode_set_bit_array(phy_basic_ports_array,
 150                                ARRAY_SIZE(phy_basic_ports_array),
 151                                phy_gbit_fibre_features);
 152         linkmode_set_bit_array(phy_10_100_features_array,
 153                                ARRAY_SIZE(phy_10_100_features_array),
 154                                phy_gbit_fibre_features);
 155         linkmode_set_bit_array(phy_gbit_features_array,
 156                                ARRAY_SIZE(phy_gbit_features_array),
 157                                phy_gbit_fibre_features);
 158         linkmode_set_bit_array(phy_fibre_port_array,
 159                                ARRAY_SIZE(phy_fibre_port_array),
 160                                phy_gbit_fibre_features);
 161 
 162         /* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
 163         linkmode_set_bit_array(phy_all_ports_features_array,
 164                                ARRAY_SIZE(phy_all_ports_features_array),
 165                                phy_gbit_all_ports_features);
 166         linkmode_set_bit_array(phy_10_100_features_array,
 167                                ARRAY_SIZE(phy_10_100_features_array),
 168                                phy_gbit_all_ports_features);
 169         linkmode_set_bit_array(phy_gbit_features_array,
 170                                ARRAY_SIZE(phy_gbit_features_array),
 171                                phy_gbit_all_ports_features);
 172 
 173         /* 10/100 half/full + 1000 half/full + 10G full*/
 174         linkmode_set_bit_array(phy_all_ports_features_array,
 175                                ARRAY_SIZE(phy_all_ports_features_array),
 176                                phy_10gbit_features);
 177         linkmode_set_bit_array(phy_10_100_features_array,
 178                                ARRAY_SIZE(phy_10_100_features_array),
 179                                phy_10gbit_features);
 180         linkmode_set_bit_array(phy_gbit_features_array,
 181                                ARRAY_SIZE(phy_gbit_features_array),
 182                                phy_10gbit_features);
 183         linkmode_set_bit_array(phy_10gbit_features_array,
 184                                ARRAY_SIZE(phy_10gbit_features_array),
 185                                phy_10gbit_features);
 186 
 187         /* 10/100/1000/10G full */
 188         linkmode_set_bit_array(phy_all_ports_features_array,
 189                                ARRAY_SIZE(phy_all_ports_features_array),
 190                                phy_10gbit_full_features);
 191         linkmode_set_bit_array(phy_10gbit_full_features_array,
 192                                ARRAY_SIZE(phy_10gbit_full_features_array),
 193                                phy_10gbit_full_features);
 194         /* 10G FEC only */
 195         linkmode_set_bit_array(phy_10gbit_fec_features_array,
 196                                ARRAY_SIZE(phy_10gbit_fec_features_array),
 197                                phy_10gbit_fec_features);
 198 }
 199 
 200 void phy_device_free(struct phy_device *phydev)
 201 {
 202         put_device(&phydev->mdio.dev);
 203 }
 204 EXPORT_SYMBOL(phy_device_free);
 205 
 206 static void phy_mdio_device_free(struct mdio_device *mdiodev)
 207 {
 208         struct phy_device *phydev;
 209 
 210         phydev = container_of(mdiodev, struct phy_device, mdio);
 211         phy_device_free(phydev);
 212 }
 213 
 214 static void phy_device_release(struct device *dev)
 215 {
 216         kfree(to_phy_device(dev));
 217 }
 218 
 219 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
 220 {
 221         struct phy_device *phydev;
 222 
 223         phydev = container_of(mdiodev, struct phy_device, mdio);
 224         phy_device_remove(phydev);
 225 }
 226 
 227 static struct phy_driver genphy_driver;
 228 extern struct phy_driver genphy_c45_driver;
 229 
 230 static LIST_HEAD(phy_fixup_list);
 231 static DEFINE_MUTEX(phy_fixup_lock);
 232 
 233 #ifdef CONFIG_PM
 234 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
 235 {
 236         struct device_driver *drv = phydev->mdio.dev.driver;
 237         struct phy_driver *phydrv = to_phy_driver(drv);
 238         struct net_device *netdev = phydev->attached_dev;
 239 
 240         if (!drv || !phydrv->suspend)
 241                 return false;
 242 
 243         /* PHY not attached? May suspend if the PHY has not already been
 244          * suspended as part of a prior call to phy_disconnect() ->
 245          * phy_detach() -> phy_suspend() because the parent netdev might be the
 246          * MDIO bus driver and clock gated at this point.
 247          */
 248         if (!netdev)
 249                 goto out;
 250 
 251         if (netdev->wol_enabled)
 252                 return false;
 253 
 254         /* As long as not all affected network drivers support the
 255          * wol_enabled flag, let's check for hints that WoL is enabled.
 256          * Don't suspend PHY if the attached netdev parent may wake up.
 257          * The parent may point to a PCI device, as in tg3 driver.
 258          */
 259         if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
 260                 return false;
 261 
 262         /* Also don't suspend PHY if the netdev itself may wakeup. This
 263          * is the case for devices w/o underlaying pwr. mgmt. aware bus,
 264          * e.g. SoC devices.
 265          */
 266         if (device_may_wakeup(&netdev->dev))
 267                 return false;
 268 
 269 out:
 270         return !phydev->suspended;
 271 }
 272 
 273 static int mdio_bus_phy_suspend(struct device *dev)
 274 {
 275         struct phy_device *phydev = to_phy_device(dev);
 276 
 277         /* We must stop the state machine manually, otherwise it stops out of
 278          * control, possibly with the phydev->lock held. Upon resume, netdev
 279          * may call phy routines that try to grab the same lock, and that may
 280          * lead to a deadlock.
 281          */
 282         if (phydev->attached_dev && phydev->adjust_link)
 283                 phy_stop_machine(phydev);
 284 
 285         if (!mdio_bus_phy_may_suspend(phydev))
 286                 return 0;
 287 
 288         phydev->suspended_by_mdio_bus = 1;
 289 
 290         return phy_suspend(phydev);
 291 }
 292 
 293 static int mdio_bus_phy_resume(struct device *dev)
 294 {
 295         struct phy_device *phydev = to_phy_device(dev);
 296         int ret;
 297 
 298         if (!phydev->suspended_by_mdio_bus)
 299                 goto no_resume;
 300 
 301         phydev->suspended_by_mdio_bus = 0;
 302 
 303         ret = phy_resume(phydev);
 304         if (ret < 0)
 305                 return ret;
 306 
 307 no_resume:
 308         if (phydev->attached_dev && phydev->adjust_link)
 309                 phy_start_machine(phydev);
 310 
 311         return 0;
 312 }
 313 
 314 static int mdio_bus_phy_restore(struct device *dev)
 315 {
 316         struct phy_device *phydev = to_phy_device(dev);
 317         struct net_device *netdev = phydev->attached_dev;
 318         int ret;
 319 
 320         if (!netdev)
 321                 return 0;
 322 
 323         ret = phy_init_hw(phydev);
 324         if (ret < 0)
 325                 return ret;
 326 
 327         if (phydev->attached_dev && phydev->adjust_link)
 328                 phy_start_machine(phydev);
 329 
 330         return 0;
 331 }
 332 
 333 static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
 334         .suspend = mdio_bus_phy_suspend,
 335         .resume = mdio_bus_phy_resume,
 336         .freeze = mdio_bus_phy_suspend,
 337         .thaw = mdio_bus_phy_resume,
 338         .restore = mdio_bus_phy_restore,
 339 };
 340 
 341 #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
 342 
 343 #else
 344 
 345 #define MDIO_BUS_PHY_PM_OPS NULL
 346 
 347 #endif /* CONFIG_PM */
 348 
 349 /**
 350  * phy_register_fixup - creates a new phy_fixup and adds it to the list
 351  * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
 352  * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
 353  *      It can also be PHY_ANY_UID
 354  * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
 355  *      comparison
 356  * @run: The actual code to be run when a matching PHY is found
 357  */
 358 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
 359                        int (*run)(struct phy_device *))
 360 {
 361         struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
 362 
 363         if (!fixup)
 364                 return -ENOMEM;
 365 
 366         strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
 367         fixup->phy_uid = phy_uid;
 368         fixup->phy_uid_mask = phy_uid_mask;
 369         fixup->run = run;
 370 
 371         mutex_lock(&phy_fixup_lock);
 372         list_add_tail(&fixup->list, &phy_fixup_list);
 373         mutex_unlock(&phy_fixup_lock);
 374 
 375         return 0;
 376 }
 377 EXPORT_SYMBOL(phy_register_fixup);
 378 
 379 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
 380 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
 381                                int (*run)(struct phy_device *))
 382 {
 383         return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
 384 }
 385 EXPORT_SYMBOL(phy_register_fixup_for_uid);
 386 
 387 /* Registers a fixup to be run on the PHY with id string bus_id */
 388 int phy_register_fixup_for_id(const char *bus_id,
 389                               int (*run)(struct phy_device *))
 390 {
 391         return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
 392 }
 393 EXPORT_SYMBOL(phy_register_fixup_for_id);
 394 
 395 /**
 396  * phy_unregister_fixup - remove a phy_fixup from the list
 397  * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
 398  * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
 399  * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
 400  */
 401 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
 402 {
 403         struct list_head *pos, *n;
 404         struct phy_fixup *fixup;
 405         int ret;
 406 
 407         ret = -ENODEV;
 408 
 409         mutex_lock(&phy_fixup_lock);
 410         list_for_each_safe(pos, n, &phy_fixup_list) {
 411                 fixup = list_entry(pos, struct phy_fixup, list);
 412 
 413                 if ((!strcmp(fixup->bus_id, bus_id)) &&
 414                     ((fixup->phy_uid & phy_uid_mask) ==
 415                      (phy_uid & phy_uid_mask))) {
 416                         list_del(&fixup->list);
 417                         kfree(fixup);
 418                         ret = 0;
 419                         break;
 420                 }
 421         }
 422         mutex_unlock(&phy_fixup_lock);
 423 
 424         return ret;
 425 }
 426 EXPORT_SYMBOL(phy_unregister_fixup);
 427 
 428 /* Unregisters a fixup of any PHY with the UID in phy_uid */
 429 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
 430 {
 431         return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
 432 }
 433 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
 434 
 435 /* Unregisters a fixup of the PHY with id string bus_id */
 436 int phy_unregister_fixup_for_id(const char *bus_id)
 437 {
 438         return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
 439 }
 440 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
 441 
 442 /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
 443  * Fixups can be set to match any in one or more fields.
 444  */
 445 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
 446 {
 447         if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
 448                 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
 449                         return 0;
 450 
 451         if ((fixup->phy_uid & fixup->phy_uid_mask) !=
 452             (phydev->phy_id & fixup->phy_uid_mask))
 453                 if (fixup->phy_uid != PHY_ANY_UID)
 454                         return 0;
 455 
 456         return 1;
 457 }
 458 
 459 /* Runs any matching fixups for this phydev */
 460 static int phy_scan_fixups(struct phy_device *phydev)
 461 {
 462         struct phy_fixup *fixup;
 463 
 464         mutex_lock(&phy_fixup_lock);
 465         list_for_each_entry(fixup, &phy_fixup_list, list) {
 466                 if (phy_needs_fixup(phydev, fixup)) {
 467                         int err = fixup->run(phydev);
 468 
 469                         if (err < 0) {
 470                                 mutex_unlock(&phy_fixup_lock);
 471                                 return err;
 472                         }
 473                         phydev->has_fixups = true;
 474                 }
 475         }
 476         mutex_unlock(&phy_fixup_lock);
 477 
 478         return 0;
 479 }
 480 
 481 static int phy_bus_match(struct device *dev, struct device_driver *drv)
 482 {
 483         struct phy_device *phydev = to_phy_device(dev);
 484         struct phy_driver *phydrv = to_phy_driver(drv);
 485         const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
 486         int i;
 487 
 488         if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
 489                 return 0;
 490 
 491         if (phydrv->match_phy_device)
 492                 return phydrv->match_phy_device(phydev);
 493 
 494         if (phydev->is_c45) {
 495                 for (i = 1; i < num_ids; i++) {
 496                         if (phydev->c45_ids.device_ids[i] == 0xffffffff)
 497                                 continue;
 498 
 499                         if ((phydrv->phy_id & phydrv->phy_id_mask) ==
 500                             (phydev->c45_ids.device_ids[i] &
 501                              phydrv->phy_id_mask))
 502                                 return 1;
 503                 }
 504                 return 0;
 505         } else {
 506                 return (phydrv->phy_id & phydrv->phy_id_mask) ==
 507                         (phydev->phy_id & phydrv->phy_id_mask);
 508         }
 509 }
 510 
 511 static ssize_t
 512 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
 513 {
 514         struct phy_device *phydev = to_phy_device(dev);
 515 
 516         return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
 517 }
 518 static DEVICE_ATTR_RO(phy_id);
 519 
 520 static ssize_t
 521 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
 522 {
 523         struct phy_device *phydev = to_phy_device(dev);
 524         const char *mode = NULL;
 525 
 526         if (phy_is_internal(phydev))
 527                 mode = "internal";
 528         else
 529                 mode = phy_modes(phydev->interface);
 530 
 531         return sprintf(buf, "%s\n", mode);
 532 }
 533 static DEVICE_ATTR_RO(phy_interface);
 534 
 535 static ssize_t
 536 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
 537                     char *buf)
 538 {
 539         struct phy_device *phydev = to_phy_device(dev);
 540 
 541         return sprintf(buf, "%d\n", phydev->has_fixups);
 542 }
 543 static DEVICE_ATTR_RO(phy_has_fixups);
 544 
 545 static struct attribute *phy_dev_attrs[] = {
 546         &dev_attr_phy_id.attr,
 547         &dev_attr_phy_interface.attr,
 548         &dev_attr_phy_has_fixups.attr,
 549         NULL,
 550 };
 551 ATTRIBUTE_GROUPS(phy_dev);
 552 
 553 static const struct device_type mdio_bus_phy_type = {
 554         .name = "PHY",
 555         .groups = phy_dev_groups,
 556         .release = phy_device_release,
 557         .pm = MDIO_BUS_PHY_PM_OPS,
 558 };
 559 
 560 static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
 561 {
 562         int ret;
 563 
 564         ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
 565                              MDIO_ID_ARGS(phy_id));
 566         /* We only check for failures in executing the usermode binary,
 567          * not whether a PHY driver module exists for the PHY ID.
 568          * Accept -ENOENT because this may occur in case no initramfs exists,
 569          * then modprobe isn't available.
 570          */
 571         if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
 572                 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
 573                            ret, (unsigned long)phy_id);
 574                 return ret;
 575         }
 576 
 577         return 0;
 578 }
 579 
 580 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
 581                                      bool is_c45,
 582                                      struct phy_c45_device_ids *c45_ids)
 583 {
 584         struct phy_device *dev;
 585         struct mdio_device *mdiodev;
 586         int ret = 0;
 587 
 588         /* We allocate the device, and initialize the default values */
 589         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 590         if (!dev)
 591                 return ERR_PTR(-ENOMEM);
 592 
 593         mdiodev = &dev->mdio;
 594         mdiodev->dev.parent = &bus->dev;
 595         mdiodev->dev.bus = &mdio_bus_type;
 596         mdiodev->dev.type = &mdio_bus_phy_type;
 597         mdiodev->bus = bus;
 598         mdiodev->bus_match = phy_bus_match;
 599         mdiodev->addr = addr;
 600         mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
 601         mdiodev->device_free = phy_mdio_device_free;
 602         mdiodev->device_remove = phy_mdio_device_remove;
 603 
 604         dev->speed = SPEED_UNKNOWN;
 605         dev->duplex = DUPLEX_UNKNOWN;
 606         dev->pause = 0;
 607         dev->asym_pause = 0;
 608         dev->link = 0;
 609         dev->interface = PHY_INTERFACE_MODE_GMII;
 610 
 611         dev->autoneg = AUTONEG_ENABLE;
 612 
 613         dev->is_c45 = is_c45;
 614         dev->phy_id = phy_id;
 615         if (c45_ids)
 616                 dev->c45_ids = *c45_ids;
 617         dev->irq = bus->irq[addr];
 618         dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
 619 
 620         dev->state = PHY_DOWN;
 621 
 622         mutex_init(&dev->lock);
 623         INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
 624 
 625         /* Request the appropriate module unconditionally; don't
 626          * bother trying to do so only if it isn't already loaded,
 627          * because that gets complicated. A hotplug event would have
 628          * done an unconditional modprobe anyway.
 629          * We don't do normal hotplug because it won't work for MDIO
 630          * -- because it relies on the device staying around for long
 631          * enough for the driver to get loaded. With MDIO, the NIC
 632          * driver will get bored and give up as soon as it finds that
 633          * there's no driver _already_ loaded.
 634          */
 635         if (is_c45 && c45_ids) {
 636                 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
 637                 int i;
 638 
 639                 for (i = 1; i < num_ids; i++) {
 640                         if (c45_ids->device_ids[i] == 0xffffffff)
 641                                 continue;
 642 
 643                         ret = phy_request_driver_module(dev,
 644                                                 c45_ids->device_ids[i]);
 645                         if (ret)
 646                                 break;
 647                 }
 648         } else {
 649                 ret = phy_request_driver_module(dev, phy_id);
 650         }
 651 
 652         if (!ret) {
 653                 device_initialize(&mdiodev->dev);
 654         } else {
 655                 kfree(dev);
 656                 dev = ERR_PTR(ret);
 657         }
 658 
 659         return dev;
 660 }
 661 EXPORT_SYMBOL(phy_device_create);
 662 
 663 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
 664  * @bus: the target MII bus
 665  * @addr: PHY address on the MII bus
 666  * @dev_addr: MMD address in the PHY.
 667  * @devices_in_package: where to store the devices in package information.
 668  *
 669  * Description: reads devices in package registers of a MMD at @dev_addr
 670  * from PHY at @addr on @bus.
 671  *
 672  * Returns: 0 on success, -EIO on failure.
 673  */
 674 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
 675                                    u32 *devices_in_package)
 676 {
 677         int phy_reg, reg_addr;
 678 
 679         reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
 680         phy_reg = mdiobus_read(bus, addr, reg_addr);
 681         if (phy_reg < 0)
 682                 return -EIO;
 683         *devices_in_package = phy_reg << 16;
 684 
 685         reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
 686         phy_reg = mdiobus_read(bus, addr, reg_addr);
 687         if (phy_reg < 0)
 688                 return -EIO;
 689         *devices_in_package |= phy_reg;
 690 
 691         /* Bit 0 doesn't represent a device, it indicates c22 regs presence */
 692         *devices_in_package &= ~BIT(0);
 693 
 694         return 0;
 695 }
 696 
 697 /**
 698  * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
 699  * @bus: the target MII bus
 700  * @addr: PHY address on the MII bus
 701  * @phy_id: where to store the ID retrieved.
 702  * @c45_ids: where to store the c45 ID information.
 703  *
 704  *   If the PHY devices-in-package appears to be valid, it and the
 705  *   corresponding identifiers are stored in @c45_ids, zero is stored
 706  *   in @phy_id.  Otherwise 0xffffffff is stored in @phy_id.  Returns
 707  *   zero on success.
 708  *
 709  */
 710 static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
 711                            struct phy_c45_device_ids *c45_ids) {
 712         int phy_reg;
 713         int i, reg_addr;
 714         const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
 715         u32 *devs = &c45_ids->devices_in_package;
 716 
 717         /* Find first non-zero Devices In package. Device zero is reserved
 718          * for 802.3 c45 complied PHYs, so don't probe it at first.
 719          */
 720         for (i = 1; i < num_ids && *devs == 0; i++) {
 721                 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
 722                 if (phy_reg < 0)
 723                         return -EIO;
 724 
 725                 if ((*devs & 0x1fffffff) == 0x1fffffff) {
 726                         /*  If mostly Fs, there is no device there,
 727                          *  then let's continue to probe more, as some
 728                          *  10G PHYs have zero Devices In package,
 729                          *  e.g. Cortina CS4315/CS4340 PHY.
 730                          */
 731                         phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
 732                         if (phy_reg < 0)
 733                                 return -EIO;
 734                         /* no device there, let's get out of here */
 735                         if ((*devs & 0x1fffffff) == 0x1fffffff) {
 736                                 *phy_id = 0xffffffff;
 737                                 return 0;
 738                         } else {
 739                                 break;
 740                         }
 741                 }
 742         }
 743 
 744         /* Now probe Device Identifiers for each device present. */
 745         for (i = 1; i < num_ids; i++) {
 746                 if (!(c45_ids->devices_in_package & (1 << i)))
 747                         continue;
 748 
 749                 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
 750                 phy_reg = mdiobus_read(bus, addr, reg_addr);
 751                 if (phy_reg < 0)
 752                         return -EIO;
 753                 c45_ids->device_ids[i] = phy_reg << 16;
 754 
 755                 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
 756                 phy_reg = mdiobus_read(bus, addr, reg_addr);
 757                 if (phy_reg < 0)
 758                         return -EIO;
 759                 c45_ids->device_ids[i] |= phy_reg;
 760         }
 761         *phy_id = 0;
 762         return 0;
 763 }
 764 
 765 /**
 766  * get_phy_id - reads the specified addr for its ID.
 767  * @bus: the target MII bus
 768  * @addr: PHY address on the MII bus
 769  * @phy_id: where to store the ID retrieved.
 770  * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
 771  * @c45_ids: where to store the c45 ID information.
 772  *
 773  * Description: In the case of a 802.3-c22 PHY, reads the ID registers
 774  *   of the PHY at @addr on the @bus, stores it in @phy_id and returns
 775  *   zero on success.
 776  *
 777  *   In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
 778  *   its return value is in turn returned.
 779  *
 780  */
 781 static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
 782                       bool is_c45, struct phy_c45_device_ids *c45_ids)
 783 {
 784         int phy_reg;
 785 
 786         if (is_c45)
 787                 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
 788 
 789         /* Grab the bits from PHYIR1, and put them in the upper half */
 790         phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
 791         if (phy_reg < 0) {
 792                 /* returning -ENODEV doesn't stop bus scanning */
 793                 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
 794         }
 795 
 796         *phy_id = phy_reg << 16;
 797 
 798         /* Grab the bits from PHYIR2, and put them in the lower half */
 799         phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
 800         if (phy_reg < 0)
 801                 return -EIO;
 802 
 803         *phy_id |= phy_reg;
 804 
 805         return 0;
 806 }
 807 
 808 /**
 809  * get_phy_device - reads the specified PHY device and returns its @phy_device
 810  *                  struct
 811  * @bus: the target MII bus
 812  * @addr: PHY address on the MII bus
 813  * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
 814  *
 815  * Description: Reads the ID registers of the PHY at @addr on the
 816  *   @bus, then allocates and returns the phy_device to represent it.
 817  */
 818 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
 819 {
 820         struct phy_c45_device_ids c45_ids;
 821         u32 phy_id = 0;
 822         int r;
 823 
 824         c45_ids.devices_in_package = 0;
 825         memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
 826 
 827         r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
 828         if (r)
 829                 return ERR_PTR(r);
 830 
 831         /* If the phy_id is mostly Fs, there is no device there */
 832         if ((phy_id & 0x1fffffff) == 0x1fffffff)
 833                 return ERR_PTR(-ENODEV);
 834 
 835         return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
 836 }
 837 EXPORT_SYMBOL(get_phy_device);
 838 
 839 /**
 840  * phy_device_register - Register the phy device on the MDIO bus
 841  * @phydev: phy_device structure to be added to the MDIO bus
 842  */
 843 int phy_device_register(struct phy_device *phydev)
 844 {
 845         int err;
 846 
 847         err = mdiobus_register_device(&phydev->mdio);
 848         if (err)
 849                 return err;
 850 
 851         /* Deassert the reset signal */
 852         phy_device_reset(phydev, 0);
 853 
 854         /* Run all of the fixups for this PHY */
 855         err = phy_scan_fixups(phydev);
 856         if (err) {
 857                 phydev_err(phydev, "failed to initialize\n");
 858                 goto out;
 859         }
 860 
 861         err = device_add(&phydev->mdio.dev);
 862         if (err) {
 863                 phydev_err(phydev, "failed to add\n");
 864                 goto out;
 865         }
 866 
 867         return 0;
 868 
 869  out:
 870         /* Assert the reset signal */
 871         phy_device_reset(phydev, 1);
 872 
 873         mdiobus_unregister_device(&phydev->mdio);
 874         return err;
 875 }
 876 EXPORT_SYMBOL(phy_device_register);
 877 
 878 /**
 879  * phy_device_remove - Remove a previously registered phy device from the MDIO bus
 880  * @phydev: phy_device structure to remove
 881  *
 882  * This doesn't free the phy_device itself, it merely reverses the effects
 883  * of phy_device_register(). Use phy_device_free() to free the device
 884  * after calling this function.
 885  */
 886 void phy_device_remove(struct phy_device *phydev)
 887 {
 888         device_del(&phydev->mdio.dev);
 889 
 890         /* Assert the reset signal */
 891         phy_device_reset(phydev, 1);
 892 
 893         mdiobus_unregister_device(&phydev->mdio);
 894 }
 895 EXPORT_SYMBOL(phy_device_remove);
 896 
 897 /**
 898  * phy_find_first - finds the first PHY device on the bus
 899  * @bus: the target MII bus
 900  */
 901 struct phy_device *phy_find_first(struct mii_bus *bus)
 902 {
 903         struct phy_device *phydev;
 904         int addr;
 905 
 906         for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
 907                 phydev = mdiobus_get_phy(bus, addr);
 908                 if (phydev)
 909                         return phydev;
 910         }
 911         return NULL;
 912 }
 913 EXPORT_SYMBOL(phy_find_first);
 914 
 915 static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
 916 {
 917         struct net_device *netdev = phydev->attached_dev;
 918 
 919         if (do_carrier) {
 920                 if (up)
 921                         netif_carrier_on(netdev);
 922                 else
 923                         netif_carrier_off(netdev);
 924         }
 925         phydev->adjust_link(netdev);
 926 }
 927 
 928 /**
 929  * phy_prepare_link - prepares the PHY layer to monitor link status
 930  * @phydev: target phy_device struct
 931  * @handler: callback function for link status change notifications
 932  *
 933  * Description: Tells the PHY infrastructure to handle the
 934  *   gory details on monitoring link status (whether through
 935  *   polling or an interrupt), and to call back to the
 936  *   connected device driver when the link status changes.
 937  *   If you want to monitor your own link state, don't call
 938  *   this function.
 939  */
 940 static void phy_prepare_link(struct phy_device *phydev,
 941                              void (*handler)(struct net_device *))
 942 {
 943         phydev->adjust_link = handler;
 944 }
 945 
 946 /**
 947  * phy_connect_direct - connect an ethernet device to a specific phy_device
 948  * @dev: the network device to connect
 949  * @phydev: the pointer to the phy device
 950  * @handler: callback function for state change notifications
 951  * @interface: PHY device's interface
 952  */
 953 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
 954                        void (*handler)(struct net_device *),
 955                        phy_interface_t interface)
 956 {
 957         int rc;
 958 
 959         if (!dev)
 960                 return -EINVAL;
 961 
 962         rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
 963         if (rc)
 964                 return rc;
 965 
 966         phy_prepare_link(phydev, handler);
 967         if (phy_interrupt_is_valid(phydev))
 968                 phy_request_interrupt(phydev);
 969 
 970         return 0;
 971 }
 972 EXPORT_SYMBOL(phy_connect_direct);
 973 
 974 /**
 975  * phy_connect - connect an ethernet device to a PHY device
 976  * @dev: the network device to connect
 977  * @bus_id: the id string of the PHY device to connect
 978  * @handler: callback function for state change notifications
 979  * @interface: PHY device's interface
 980  *
 981  * Description: Convenience function for connecting ethernet
 982  *   devices to PHY devices.  The default behavior is for
 983  *   the PHY infrastructure to handle everything, and only notify
 984  *   the connected driver when the link status changes.  If you
 985  *   don't want, or can't use the provided functionality, you may
 986  *   choose to call only the subset of functions which provide
 987  *   the desired functionality.
 988  */
 989 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
 990                                void (*handler)(struct net_device *),
 991                                phy_interface_t interface)
 992 {
 993         struct phy_device *phydev;
 994         struct device *d;
 995         int rc;
 996 
 997         /* Search the list of PHY devices on the mdio bus for the
 998          * PHY with the requested name
 999          */
1000         d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1001         if (!d) {
1002                 pr_err("PHY %s not found\n", bus_id);
1003                 return ERR_PTR(-ENODEV);
1004         }
1005         phydev = to_phy_device(d);
1006 
1007         rc = phy_connect_direct(dev, phydev, handler, interface);
1008         put_device(d);
1009         if (rc)
1010                 return ERR_PTR(rc);
1011 
1012         return phydev;
1013 }
1014 EXPORT_SYMBOL(phy_connect);
1015 
1016 /**
1017  * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1018  *                  device
1019  * @phydev: target phy_device struct
1020  */
1021 void phy_disconnect(struct phy_device *phydev)
1022 {
1023         if (phy_is_started(phydev))
1024                 phy_stop(phydev);
1025 
1026         if (phy_interrupt_is_valid(phydev))
1027                 phy_free_interrupt(phydev);
1028 
1029         phydev->adjust_link = NULL;
1030 
1031         phy_detach(phydev);
1032 }
1033 EXPORT_SYMBOL(phy_disconnect);
1034 
1035 /**
1036  * phy_poll_reset - Safely wait until a PHY reset has properly completed
1037  * @phydev: The PHY device to poll
1038  *
1039  * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1040  *   published in 2008, a PHY reset may take up to 0.5 seconds.  The MII BMCR
1041  *   register must be polled until the BMCR_RESET bit clears.
1042  *
1043  *   Furthermore, any attempts to write to PHY registers may have no effect
1044  *   or even generate MDIO bus errors until this is complete.
1045  *
1046  *   Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1047  *   standard and do not fully reset after the BMCR_RESET bit is set, and may
1048  *   even *REQUIRE* a soft-reset to properly restart autonegotiation.  In an
1049  *   effort to support such broken PHYs, this function is separate from the
1050  *   standard phy_init_hw() which will zero all the other bits in the BMCR
1051  *   and reapply all driver-specific and board-specific fixups.
1052  */
1053 static int phy_poll_reset(struct phy_device *phydev)
1054 {
1055         /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1056         unsigned int retries = 12;
1057         int ret;
1058 
1059         do {
1060                 msleep(50);
1061                 ret = phy_read(phydev, MII_BMCR);
1062                 if (ret < 0)
1063                         return ret;
1064         } while (ret & BMCR_RESET && --retries);
1065         if (ret & BMCR_RESET)
1066                 return -ETIMEDOUT;
1067 
1068         /* Some chips (smsc911x) may still need up to another 1ms after the
1069          * BMCR_RESET bit is cleared before they are usable.
1070          */
1071         msleep(1);
1072         return 0;
1073 }
1074 
1075 int phy_init_hw(struct phy_device *phydev)
1076 {
1077         int ret = 0;
1078 
1079         /* Deassert the reset signal */
1080         phy_device_reset(phydev, 0);
1081 
1082         if (!phydev->drv)
1083                 return 0;
1084 
1085         if (phydev->drv->soft_reset)
1086                 ret = phydev->drv->soft_reset(phydev);
1087 
1088         if (ret < 0)
1089                 return ret;
1090 
1091         ret = phy_scan_fixups(phydev);
1092         if (ret < 0)
1093                 return ret;
1094 
1095         if (phydev->drv->config_init)
1096                 ret = phydev->drv->config_init(phydev);
1097 
1098         return ret;
1099 }
1100 EXPORT_SYMBOL(phy_init_hw);
1101 
1102 void phy_attached_info(struct phy_device *phydev)
1103 {
1104         phy_attached_print(phydev, NULL);
1105 }
1106 EXPORT_SYMBOL(phy_attached_info);
1107 
1108 #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
1109 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1110 {
1111         const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
1112         char *irq_str;
1113         char irq_num[8];
1114 
1115         switch(phydev->irq) {
1116         case PHY_POLL:
1117                 irq_str = "POLL";
1118                 break;
1119         case PHY_IGNORE_INTERRUPT:
1120                 irq_str = "IGNORE";
1121                 break;
1122         default:
1123                 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1124                 irq_str = irq_num;
1125                 break;
1126         }
1127 
1128 
1129         if (!fmt) {
1130                 phydev_info(phydev, ATTACHED_FMT "\n",
1131                          drv_name, phydev_name(phydev),
1132                          irq_str);
1133         } else {
1134                 va_list ap;
1135 
1136                 phydev_info(phydev, ATTACHED_FMT,
1137                          drv_name, phydev_name(phydev),
1138                          irq_str);
1139 
1140                 va_start(ap, fmt);
1141                 vprintk(fmt, ap);
1142                 va_end(ap);
1143         }
1144 }
1145 EXPORT_SYMBOL(phy_attached_print);
1146 
1147 static void phy_sysfs_create_links(struct phy_device *phydev)
1148 {
1149         struct net_device *dev = phydev->attached_dev;
1150         int err;
1151 
1152         if (!dev)
1153                 return;
1154 
1155         err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1156                                 "attached_dev");
1157         if (err)
1158                 return;
1159 
1160         err = sysfs_create_link_nowarn(&dev->dev.kobj,
1161                                        &phydev->mdio.dev.kobj,
1162                                        "phydev");
1163         if (err) {
1164                 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1165                         kobject_name(&phydev->mdio.dev.kobj),
1166                         err);
1167                 /* non-fatal - some net drivers can use one netdevice
1168                  * with more then one phy
1169                  */
1170         }
1171 
1172         phydev->sysfs_links = true;
1173 }
1174 
1175 static ssize_t
1176 phy_standalone_show(struct device *dev, struct device_attribute *attr,
1177                     char *buf)
1178 {
1179         struct phy_device *phydev = to_phy_device(dev);
1180 
1181         return sprintf(buf, "%d\n", !phydev->attached_dev);
1182 }
1183 static DEVICE_ATTR_RO(phy_standalone);
1184 
1185 /**
1186  * phy_attach_direct - attach a network device to a given PHY device pointer
1187  * @dev: network device to attach
1188  * @phydev: Pointer to phy_device to attach
1189  * @flags: PHY device's dev_flags
1190  * @interface: PHY device's interface
1191  *
1192  * Description: Called by drivers to attach to a particular PHY
1193  *     device. The phy_device is found, and properly hooked up
1194  *     to the phy_driver.  If no driver is attached, then a
1195  *     generic driver is used.  The phy_device is given a ptr to
1196  *     the attaching device, and given a callback for link status
1197  *     change.  The phy_device is returned to the attaching driver.
1198  *     This function takes a reference on the phy device.
1199  */
1200 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1201                       u32 flags, phy_interface_t interface)
1202 {
1203         struct mii_bus *bus = phydev->mdio.bus;
1204         struct device *d = &phydev->mdio.dev;
1205         struct module *ndev_owner = NULL;
1206         bool using_genphy = false;
1207         int err;
1208 
1209         /* For Ethernet device drivers that register their own MDIO bus, we
1210          * will have bus->owner match ndev_mod, so we do not want to increment
1211          * our own module->refcnt here, otherwise we would not be able to
1212          * unload later on.
1213          */
1214         if (dev)
1215                 ndev_owner = dev->dev.parent->driver->owner;
1216         if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1217                 phydev_err(phydev, "failed to get the bus module\n");
1218                 return -EIO;
1219         }
1220 
1221         get_device(d);
1222 
1223         /* Assume that if there is no driver, that it doesn't
1224          * exist, and we should use the genphy driver.
1225          */
1226         if (!d->driver) {
1227                 if (phydev->is_c45)
1228                         d->driver = &genphy_c45_driver.mdiodrv.driver;
1229                 else
1230                         d->driver = &genphy_driver.mdiodrv.driver;
1231 
1232                 using_genphy = true;
1233         }
1234 
1235         if (!try_module_get(d->driver->owner)) {
1236                 phydev_err(phydev, "failed to get the device driver module\n");
1237                 err = -EIO;
1238                 goto error_put_device;
1239         }
1240 
1241         if (using_genphy) {
1242                 err = d->driver->probe(d);
1243                 if (err >= 0)
1244                         err = device_bind_driver(d);
1245 
1246                 if (err)
1247                         goto error_module_put;
1248         }
1249 
1250         if (phydev->attached_dev) {
1251                 dev_err(&dev->dev, "PHY already attached\n");
1252                 err = -EBUSY;
1253                 goto error;
1254         }
1255 
1256         phydev->phy_link_change = phy_link_change;
1257         if (dev) {
1258                 phydev->attached_dev = dev;
1259                 dev->phydev = phydev;
1260         }
1261 
1262         /* Some Ethernet drivers try to connect to a PHY device before
1263          * calling register_netdevice() -> netdev_register_kobject() and
1264          * does the dev->dev.kobj initialization. Here we only check for
1265          * success which indicates that the network device kobject is
1266          * ready. Once we do that we still need to keep track of whether
1267          * links were successfully set up or not for phy_detach() to
1268          * remove them accordingly.
1269          */
1270         phydev->sysfs_links = false;
1271 
1272         phy_sysfs_create_links(phydev);
1273 
1274         if (!phydev->attached_dev) {
1275                 err = sysfs_create_file(&phydev->mdio.dev.kobj,
1276                                         &dev_attr_phy_standalone.attr);
1277                 if (err)
1278                         phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1279         }
1280 
1281         phydev->dev_flags = flags;
1282 
1283         phydev->interface = interface;
1284 
1285         phydev->state = PHY_READY;
1286 
1287         /* Initial carrier state is off as the phy is about to be
1288          * (re)initialized.
1289          */
1290         if (dev)
1291                 netif_carrier_off(phydev->attached_dev);
1292 
1293         /* Do initial configuration here, now that
1294          * we have certain key parameters
1295          * (dev_flags and interface)
1296          */
1297         err = phy_init_hw(phydev);
1298         if (err)
1299                 goto error;
1300 
1301         phy_resume(phydev);
1302         phy_led_triggers_register(phydev);
1303 
1304         return err;
1305 
1306 error:
1307         /* phy_detach() does all of the cleanup below */
1308         phy_detach(phydev);
1309         return err;
1310 
1311 error_module_put:
1312         module_put(d->driver->owner);
1313 error_put_device:
1314         put_device(d);
1315         if (ndev_owner != bus->owner)
1316                 module_put(bus->owner);
1317         return err;
1318 }
1319 EXPORT_SYMBOL(phy_attach_direct);
1320 
1321 /**
1322  * phy_attach - attach a network device to a particular PHY device
1323  * @dev: network device to attach
1324  * @bus_id: Bus ID of PHY device to attach
1325  * @interface: PHY device's interface
1326  *
1327  * Description: Same as phy_attach_direct() except that a PHY bus_id
1328  *     string is passed instead of a pointer to a struct phy_device.
1329  */
1330 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1331                               phy_interface_t interface)
1332 {
1333         struct bus_type *bus = &mdio_bus_type;
1334         struct phy_device *phydev;
1335         struct device *d;
1336         int rc;
1337 
1338         if (!dev)
1339                 return ERR_PTR(-EINVAL);
1340 
1341         /* Search the list of PHY devices on the mdio bus for the
1342          * PHY with the requested name
1343          */
1344         d = bus_find_device_by_name(bus, NULL, bus_id);
1345         if (!d) {
1346                 pr_err("PHY %s not found\n", bus_id);
1347                 return ERR_PTR(-ENODEV);
1348         }
1349         phydev = to_phy_device(d);
1350 
1351         rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1352         put_device(d);
1353         if (rc)
1354                 return ERR_PTR(rc);
1355 
1356         return phydev;
1357 }
1358 EXPORT_SYMBOL(phy_attach);
1359 
1360 static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1361                                       struct device_driver *driver)
1362 {
1363         struct device *d = &phydev->mdio.dev;
1364         bool ret = false;
1365 
1366         if (!phydev->drv)
1367                 return ret;
1368 
1369         get_device(d);
1370         ret = d->driver == driver;
1371         put_device(d);
1372 
1373         return ret;
1374 }
1375 
1376 bool phy_driver_is_genphy(struct phy_device *phydev)
1377 {
1378         return phy_driver_is_genphy_kind(phydev,
1379                                          &genphy_driver.mdiodrv.driver);
1380 }
1381 EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1382 
1383 bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1384 {
1385         return phy_driver_is_genphy_kind(phydev,
1386                                          &genphy_c45_driver.mdiodrv.driver);
1387 }
1388 EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1389 
1390 /**
1391  * phy_detach - detach a PHY device from its network device
1392  * @phydev: target phy_device struct
1393  *
1394  * This detaches the phy device from its network device and the phy
1395  * driver, and drops the reference count taken in phy_attach_direct().
1396  */
1397 void phy_detach(struct phy_device *phydev)
1398 {
1399         struct net_device *dev = phydev->attached_dev;
1400         struct module *ndev_owner = NULL;
1401         struct mii_bus *bus;
1402 
1403         if (phydev->sysfs_links) {
1404                 if (dev)
1405                         sysfs_remove_link(&dev->dev.kobj, "phydev");
1406                 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1407         }
1408 
1409         if (!phydev->attached_dev)
1410                 sysfs_remove_file(&phydev->mdio.dev.kobj,
1411                                   &dev_attr_phy_standalone.attr);
1412 
1413         phy_suspend(phydev);
1414         if (dev) {
1415                 phydev->attached_dev->phydev = NULL;
1416                 phydev->attached_dev = NULL;
1417         }
1418         phydev->phylink = NULL;
1419 
1420         phy_led_triggers_unregister(phydev);
1421 
1422         module_put(phydev->mdio.dev.driver->owner);
1423 
1424         /* If the device had no specific driver before (i.e. - it
1425          * was using the generic driver), we unbind the device
1426          * from the generic driver so that there's a chance a
1427          * real driver could be loaded
1428          */
1429         if (phy_driver_is_genphy(phydev) ||
1430             phy_driver_is_genphy_10g(phydev))
1431                 device_release_driver(&phydev->mdio.dev);
1432 
1433         /*
1434          * The phydev might go away on the put_device() below, so avoid
1435          * a use-after-free bug by reading the underlying bus first.
1436          */
1437         bus = phydev->mdio.bus;
1438 
1439         put_device(&phydev->mdio.dev);
1440         if (dev)
1441                 ndev_owner = dev->dev.parent->driver->owner;
1442         if (ndev_owner != bus->owner)
1443                 module_put(bus->owner);
1444 
1445         /* Assert the reset signal */
1446         phy_device_reset(phydev, 1);
1447 }
1448 EXPORT_SYMBOL(phy_detach);
1449 
1450 int phy_suspend(struct phy_device *phydev)
1451 {
1452         struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1453         struct net_device *netdev = phydev->attached_dev;
1454         struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1455         int ret = 0;
1456 
1457         /* If the device has WOL enabled, we cannot suspend the PHY */
1458         phy_ethtool_get_wol(phydev, &wol);
1459         if (wol.wolopts || (netdev && netdev->wol_enabled))
1460                 return -EBUSY;
1461 
1462         if (phydev->drv && phydrv->suspend)
1463                 ret = phydrv->suspend(phydev);
1464 
1465         if (ret)
1466                 return ret;
1467 
1468         phydev->suspended = true;
1469 
1470         return ret;
1471 }
1472 EXPORT_SYMBOL(phy_suspend);
1473 
1474 int __phy_resume(struct phy_device *phydev)
1475 {
1476         struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1477         int ret = 0;
1478 
1479         WARN_ON(!mutex_is_locked(&phydev->lock));
1480 
1481         if (phydev->drv && phydrv->resume)
1482                 ret = phydrv->resume(phydev);
1483 
1484         if (ret)
1485                 return ret;
1486 
1487         phydev->suspended = false;
1488 
1489         return ret;
1490 }
1491 EXPORT_SYMBOL(__phy_resume);
1492 
1493 int phy_resume(struct phy_device *phydev)
1494 {
1495         int ret;
1496 
1497         mutex_lock(&phydev->lock);
1498         ret = __phy_resume(phydev);
1499         mutex_unlock(&phydev->lock);
1500 
1501         return ret;
1502 }
1503 EXPORT_SYMBOL(phy_resume);
1504 
1505 int phy_loopback(struct phy_device *phydev, bool enable)
1506 {
1507         struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1508         int ret = 0;
1509 
1510         mutex_lock(&phydev->lock);
1511 
1512         if (enable && phydev->loopback_enabled) {
1513                 ret = -EBUSY;
1514                 goto out;
1515         }
1516 
1517         if (!enable && !phydev->loopback_enabled) {
1518                 ret = -EINVAL;
1519                 goto out;
1520         }
1521 
1522         if (phydev->drv && phydrv->set_loopback)
1523                 ret = phydrv->set_loopback(phydev, enable);
1524         else
1525                 ret = -EOPNOTSUPP;
1526 
1527         if (ret)
1528                 goto out;
1529 
1530         phydev->loopback_enabled = enable;
1531 
1532 out:
1533         mutex_unlock(&phydev->lock);
1534         return ret;
1535 }
1536 EXPORT_SYMBOL(phy_loopback);
1537 
1538 /**
1539  * phy_reset_after_clk_enable - perform a PHY reset if needed
1540  * @phydev: target phy_device struct
1541  *
1542  * Description: Some PHYs are known to need a reset after their refclk was
1543  *   enabled. This function evaluates the flags and perform the reset if it's
1544  *   needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1545  *   was reset.
1546  */
1547 int phy_reset_after_clk_enable(struct phy_device *phydev)
1548 {
1549         if (!phydev || !phydev->drv)
1550                 return -ENODEV;
1551 
1552         if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1553                 phy_device_reset(phydev, 1);
1554                 phy_device_reset(phydev, 0);
1555                 return 1;
1556         }
1557 
1558         return 0;
1559 }
1560 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1561 
1562 /* Generic PHY support and helper functions */
1563 
1564 /**
1565  * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1566  * @phydev: target phy_device struct
1567  *
1568  * Description: Writes MII_ADVERTISE with the appropriate values,
1569  *   after sanitizing the values to make sure we only advertise
1570  *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
1571  *   hasn't changed, and > 0 if it has changed.
1572  */
1573 static int genphy_config_advert(struct phy_device *phydev)
1574 {
1575         int err, bmsr, changed = 0;
1576         u32 adv;
1577 
1578         /* Only allow advertising what this PHY supports */
1579         linkmode_and(phydev->advertising, phydev->advertising,
1580                      phydev->supported);
1581 
1582         adv = linkmode_adv_to_mii_adv_t(phydev->advertising);
1583 
1584         /* Setup standard advertisement */
1585         err = phy_modify_changed(phydev, MII_ADVERTISE,
1586                                  ADVERTISE_ALL | ADVERTISE_100BASE4 |
1587                                  ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1588                                  adv);
1589         if (err < 0)
1590                 return err;
1591         if (err > 0)
1592                 changed = 1;
1593 
1594         bmsr = phy_read(phydev, MII_BMSR);
1595         if (bmsr < 0)
1596                 return bmsr;
1597 
1598         /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1599          * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1600          * logical 1.
1601          */
1602         if (!(bmsr & BMSR_ESTATEN))
1603                 return changed;
1604 
1605         adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
1606 
1607         err = phy_modify_changed(phydev, MII_CTRL1000,
1608                                  ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1609                                  adv);
1610         if (err < 0)
1611                 return err;
1612         if (err > 0)
1613                 changed = 1;
1614 
1615         return changed;
1616 }
1617 
1618 /**
1619  * genphy_config_eee_advert - disable unwanted eee mode advertisement
1620  * @phydev: target phy_device struct
1621  *
1622  * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1623  *   efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1624  *   changed, and 1 if it has changed.
1625  */
1626 int genphy_config_eee_advert(struct phy_device *phydev)
1627 {
1628         int err;
1629 
1630         /* Nothing to disable */
1631         if (!phydev->eee_broken_modes)
1632                 return 0;
1633 
1634         err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
1635                                      phydev->eee_broken_modes, 0);
1636         /* If the call failed, we assume that EEE is not supported */
1637         return err < 0 ? 0 : err;
1638 }
1639 EXPORT_SYMBOL(genphy_config_eee_advert);
1640 
1641 /**
1642  * genphy_setup_forced - configures/forces speed/duplex from @phydev
1643  * @phydev: target phy_device struct
1644  *
1645  * Description: Configures MII_BMCR to force speed/duplex
1646  *   to the values in phydev. Assumes that the values are valid.
1647  *   Please see phy_sanitize_settings().
1648  */
1649 int genphy_setup_forced(struct phy_device *phydev)
1650 {
1651         u16 ctl = 0;
1652 
1653         phydev->pause = 0;
1654         phydev->asym_pause = 0;
1655 
1656         if (SPEED_1000 == phydev->speed)
1657                 ctl |= BMCR_SPEED1000;
1658         else if (SPEED_100 == phydev->speed)
1659                 ctl |= BMCR_SPEED100;
1660 
1661         if (DUPLEX_FULL == phydev->duplex)
1662                 ctl |= BMCR_FULLDPLX;
1663 
1664         return phy_modify(phydev, MII_BMCR,
1665                           ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1666 }
1667 EXPORT_SYMBOL(genphy_setup_forced);
1668 
1669 /**
1670  * genphy_restart_aneg - Enable and Restart Autonegotiation
1671  * @phydev: target phy_device struct
1672  */
1673 int genphy_restart_aneg(struct phy_device *phydev)
1674 {
1675         /* Don't isolate the PHY if we're negotiating */
1676         return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1677                           BMCR_ANENABLE | BMCR_ANRESTART);
1678 }
1679 EXPORT_SYMBOL(genphy_restart_aneg);
1680 
1681 /**
1682  * __genphy_config_aneg - restart auto-negotiation or write BMCR
1683  * @phydev: target phy_device struct
1684  * @changed: whether autoneg is requested
1685  *
1686  * Description: If auto-negotiation is enabled, we configure the
1687  *   advertising, and then restart auto-negotiation.  If it is not
1688  *   enabled, then we write the BMCR.
1689  */
1690 int __genphy_config_aneg(struct phy_device *phydev, bool changed)
1691 {
1692         int err;
1693 
1694         if (genphy_config_eee_advert(phydev))
1695                 changed = true;
1696 
1697         if (AUTONEG_ENABLE != phydev->autoneg)
1698                 return genphy_setup_forced(phydev);
1699 
1700         err = genphy_config_advert(phydev);
1701         if (err < 0) /* error */
1702                 return err;
1703         else if (err)
1704                 changed = true;
1705 
1706         if (!changed) {
1707                 /* Advertisement hasn't changed, but maybe aneg was never on to
1708                  * begin with?  Or maybe phy was isolated?
1709                  */
1710                 int ctl = phy_read(phydev, MII_BMCR);
1711 
1712                 if (ctl < 0)
1713                         return ctl;
1714 
1715                 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1716                         changed = true; /* do restart aneg */
1717         }
1718 
1719         /* Only restart aneg if we are advertising something different
1720          * than we were before.
1721          */
1722         return changed ? genphy_restart_aneg(phydev) : 0;
1723 }
1724 EXPORT_SYMBOL(__genphy_config_aneg);
1725 
1726 /**
1727  * genphy_aneg_done - return auto-negotiation status
1728  * @phydev: target phy_device struct
1729  *
1730  * Description: Reads the status register and returns 0 either if
1731  *   auto-negotiation is incomplete, or if there was an error.
1732  *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1733  */
1734 int genphy_aneg_done(struct phy_device *phydev)
1735 {
1736         int retval = phy_read(phydev, MII_BMSR);
1737 
1738         return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1739 }
1740 EXPORT_SYMBOL(genphy_aneg_done);
1741 
1742 /**
1743  * genphy_update_link - update link status in @phydev
1744  * @phydev: target phy_device struct
1745  *
1746  * Description: Update the value in phydev->link to reflect the
1747  *   current link value.  In order to do this, we need to read
1748  *   the status register twice, keeping the second value.
1749  */
1750 int genphy_update_link(struct phy_device *phydev)
1751 {
1752         int status = 0, bmcr;
1753 
1754         bmcr = phy_read(phydev, MII_BMCR);
1755         if (bmcr < 0)
1756                 return bmcr;
1757 
1758         /* Autoneg is being started, therefore disregard BMSR value and
1759          * report link as down.
1760          */
1761         if (bmcr & BMCR_ANRESTART)
1762                 goto done;
1763 
1764         /* The link state is latched low so that momentary link
1765          * drops can be detected. Do not double-read the status
1766          * in polling mode to detect such short link drops.
1767          */
1768         if (!phy_polling_mode(phydev)) {
1769                 status = phy_read(phydev, MII_BMSR);
1770                 if (status < 0)
1771                         return status;
1772                 else if (status & BMSR_LSTATUS)
1773                         goto done;
1774         }
1775 
1776         /* Read link and autonegotiation status */
1777         status = phy_read(phydev, MII_BMSR);
1778         if (status < 0)
1779                 return status;
1780 done:
1781         phydev->link = status & BMSR_LSTATUS ? 1 : 0;
1782         phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
1783 
1784         /* Consider the case that autoneg was started and "aneg complete"
1785          * bit has been reset, but "link up" bit not yet.
1786          */
1787         if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
1788                 phydev->link = 0;
1789 
1790         return 0;
1791 }
1792 EXPORT_SYMBOL(genphy_update_link);
1793 
1794 int genphy_read_lpa(struct phy_device *phydev)
1795 {
1796         int lpa, lpagb;
1797 
1798         if (phydev->autoneg == AUTONEG_ENABLE) {
1799                 if (!phydev->autoneg_complete) {
1800                         mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1801                                                         0);
1802                         mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
1803                         return 0;
1804                 }
1805 
1806                 if (phydev->is_gigabit_capable) {
1807                         lpagb = phy_read(phydev, MII_STAT1000);
1808                         if (lpagb < 0)
1809                                 return lpagb;
1810 
1811                         if (lpagb & LPA_1000MSFAIL) {
1812                                 int adv = phy_read(phydev, MII_CTRL1000);
1813 
1814                                 if (adv < 0)
1815                                         return adv;
1816 
1817                                 if (adv & CTL1000_ENABLE_MASTER)
1818                                         phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1819                                 else
1820                                         phydev_err(phydev, "Master/Slave resolution failed\n");
1821                                 return -ENOLINK;
1822                         }
1823 
1824                         mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1825                                                         lpagb);
1826                 }
1827 
1828                 lpa = phy_read(phydev, MII_LPA);
1829                 if (lpa < 0)
1830                         return lpa;
1831 
1832                 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
1833         } else {
1834                 linkmode_zero(phydev->lp_advertising);
1835         }
1836 
1837         return 0;
1838 }
1839 EXPORT_SYMBOL(genphy_read_lpa);
1840 
1841 /**
1842  * genphy_read_status - check the link status and update current link state
1843  * @phydev: target phy_device struct
1844  *
1845  * Description: Check the link, then figure out the current state
1846  *   by comparing what we advertise with what the link partner
1847  *   advertises.  Start by checking the gigabit possibilities,
1848  *   then move on to 10/100.
1849  */
1850 int genphy_read_status(struct phy_device *phydev)
1851 {
1852         int err, old_link = phydev->link;
1853 
1854         /* Update the link, but return if there was an error */
1855         err = genphy_update_link(phydev);
1856         if (err)
1857                 return err;
1858 
1859         /* why bother the PHY if nothing can have changed */
1860         if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
1861                 return 0;
1862 
1863         phydev->speed = SPEED_UNKNOWN;
1864         phydev->duplex = DUPLEX_UNKNOWN;
1865         phydev->pause = 0;
1866         phydev->asym_pause = 0;
1867 
1868         err = genphy_read_lpa(phydev);
1869         if (err < 0)
1870                 return err;
1871 
1872         if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
1873                 phy_resolve_aneg_linkmode(phydev);
1874         } else if (phydev->autoneg == AUTONEG_DISABLE) {
1875                 int bmcr = phy_read(phydev, MII_BMCR);
1876 
1877                 if (bmcr < 0)
1878                         return bmcr;
1879 
1880                 if (bmcr & BMCR_FULLDPLX)
1881                         phydev->duplex = DUPLEX_FULL;
1882                 else
1883                         phydev->duplex = DUPLEX_HALF;
1884 
1885                 if (bmcr & BMCR_SPEED1000)
1886                         phydev->speed = SPEED_1000;
1887                 else if (bmcr & BMCR_SPEED100)
1888                         phydev->speed = SPEED_100;
1889                 else
1890                         phydev->speed = SPEED_10;
1891         }
1892 
1893         return 0;
1894 }
1895 EXPORT_SYMBOL(genphy_read_status);
1896 
1897 /**
1898  * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1899  * @phydev: target phy_device struct
1900  *
1901  * Description: Perform a software PHY reset using the standard
1902  * BMCR_RESET bit and poll for the reset bit to be cleared.
1903  *
1904  * Returns: 0 on success, < 0 on failure
1905  */
1906 int genphy_soft_reset(struct phy_device *phydev)
1907 {
1908         u16 res = BMCR_RESET;
1909         int ret;
1910 
1911         if (phydev->autoneg == AUTONEG_ENABLE)
1912                 res |= BMCR_ANRESTART;
1913 
1914         ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
1915         if (ret < 0)
1916                 return ret;
1917 
1918         ret = phy_poll_reset(phydev);
1919         if (ret)
1920                 return ret;
1921 
1922         /* BMCR may be reset to defaults */
1923         if (phydev->autoneg == AUTONEG_DISABLE)
1924                 ret = genphy_setup_forced(phydev);
1925 
1926         return ret;
1927 }
1928 EXPORT_SYMBOL(genphy_soft_reset);
1929 
1930 /**
1931  * genphy_read_abilities - read PHY abilities from Clause 22 registers
1932  * @phydev: target phy_device struct
1933  *
1934  * Description: Reads the PHY's abilities and populates
1935  * phydev->supported accordingly.
1936  *
1937  * Returns: 0 on success, < 0 on failure
1938  */
1939 int genphy_read_abilities(struct phy_device *phydev)
1940 {
1941         int val;
1942 
1943         linkmode_set_bit_array(phy_basic_ports_array,
1944                                ARRAY_SIZE(phy_basic_ports_array),
1945                                phydev->supported);
1946 
1947         val = phy_read(phydev, MII_BMSR);
1948         if (val < 0)
1949                 return val;
1950 
1951         linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
1952                          val & BMSR_ANEGCAPABLE);
1953 
1954         linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
1955                          val & BMSR_100FULL);
1956         linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
1957                          val & BMSR_100HALF);
1958         linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
1959                          val & BMSR_10FULL);
1960         linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
1961                          val & BMSR_10HALF);
1962 
1963         if (val & BMSR_ESTATEN) {
1964                 val = phy_read(phydev, MII_ESTATUS);
1965                 if (val < 0)
1966                         return val;
1967 
1968                 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1969                                  phydev->supported, val & ESTATUS_1000_TFULL);
1970                 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1971                                  phydev->supported, val & ESTATUS_1000_THALF);
1972                 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1973                                  phydev->supported, val & ESTATUS_1000_XFULL);
1974         }
1975 
1976         return 0;
1977 }
1978 EXPORT_SYMBOL(genphy_read_abilities);
1979 
1980 /* This is used for the phy device which doesn't support the MMD extended
1981  * register access, but it does have side effect when we are trying to access
1982  * the MMD register via indirect method.
1983  */
1984 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
1985 {
1986         return -EOPNOTSUPP;
1987 }
1988 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
1989 
1990 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1991                                  u16 regnum, u16 val)
1992 {
1993         return -EOPNOTSUPP;
1994 }
1995 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
1996 
1997 int genphy_suspend(struct phy_device *phydev)
1998 {
1999         return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2000 }
2001 EXPORT_SYMBOL(genphy_suspend);
2002 
2003 int genphy_resume(struct phy_device *phydev)
2004 {
2005         return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2006 }
2007 EXPORT_SYMBOL(genphy_resume);
2008 
2009 int genphy_loopback(struct phy_device *phydev, bool enable)
2010 {
2011         return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
2012                           enable ? BMCR_LOOPBACK : 0);
2013 }
2014 EXPORT_SYMBOL(genphy_loopback);
2015 
2016 /**
2017  * phy_remove_link_mode - Remove a supported link mode
2018  * @phydev: phy_device structure to remove link mode from
2019  * @link_mode: Link mode to be removed
2020  *
2021  * Description: Some MACs don't support all link modes which the PHY
2022  * does.  e.g. a 1G MAC often does not support 1000Half. Add a helper
2023  * to remove a link mode.
2024  */
2025 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2026 {
2027         linkmode_clear_bit(link_mode, phydev->supported);
2028         phy_advertise_supported(phydev);
2029 }
2030 EXPORT_SYMBOL(phy_remove_link_mode);
2031 
2032 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2033 {
2034         linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2035                 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2036         linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2037                 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2038 }
2039 
2040 /**
2041  * phy_advertise_supported - Advertise all supported modes
2042  * @phydev: target phy_device struct
2043  *
2044  * Description: Called to advertise all supported modes, doesn't touch
2045  * pause mode advertising.
2046  */
2047 void phy_advertise_supported(struct phy_device *phydev)
2048 {
2049         __ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2050 
2051         linkmode_copy(new, phydev->supported);
2052         phy_copy_pause_bits(new, phydev->advertising);
2053         linkmode_copy(phydev->advertising, new);
2054 }
2055 EXPORT_SYMBOL(phy_advertise_supported);
2056 
2057 /**
2058  * phy_support_sym_pause - Enable support of symmetrical pause
2059  * @phydev: target phy_device struct
2060  *
2061  * Description: Called by the MAC to indicate is supports symmetrical
2062  * Pause, but not asym pause.
2063  */
2064 void phy_support_sym_pause(struct phy_device *phydev)
2065 {
2066         linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2067         phy_copy_pause_bits(phydev->advertising, phydev->supported);
2068 }
2069 EXPORT_SYMBOL(phy_support_sym_pause);
2070 
2071 /**
2072  * phy_support_asym_pause - Enable support of asym pause
2073  * @phydev: target phy_device struct
2074  *
2075  * Description: Called by the MAC to indicate is supports Asym Pause.
2076  */
2077 void phy_support_asym_pause(struct phy_device *phydev)
2078 {
2079         phy_copy_pause_bits(phydev->advertising, phydev->supported);
2080 }
2081 EXPORT_SYMBOL(phy_support_asym_pause);
2082 
2083 /**
2084  * phy_set_sym_pause - Configure symmetric Pause
2085  * @phydev: target phy_device struct
2086  * @rx: Receiver Pause is supported
2087  * @tx: Transmit Pause is supported
2088  * @autoneg: Auto neg should be used
2089  *
2090  * Description: Configure advertised Pause support depending on if
2091  * receiver pause and pause auto neg is supported. Generally called
2092  * from the set_pauseparam .ndo.
2093  */
2094 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2095                        bool autoneg)
2096 {
2097         linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2098 
2099         if (rx && tx && autoneg)
2100                 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2101                                  phydev->supported);
2102 
2103         linkmode_copy(phydev->advertising, phydev->supported);
2104 }
2105 EXPORT_SYMBOL(phy_set_sym_pause);
2106 
2107 /**
2108  * phy_set_asym_pause - Configure Pause and Asym Pause
2109  * @phydev: target phy_device struct
2110  * @rx: Receiver Pause is supported
2111  * @tx: Transmit Pause is supported
2112  *
2113  * Description: Configure advertised Pause support depending on if
2114  * transmit and receiver pause is supported. If there has been a
2115  * change in adverting, trigger a new autoneg. Generally called from
2116  * the set_pauseparam .ndo.
2117  */
2118 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2119 {
2120         __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
2121 
2122         linkmode_copy(oldadv, phydev->advertising);
2123 
2124         linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2125                            phydev->advertising);
2126         linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2127                            phydev->advertising);
2128 
2129         if (rx) {
2130                 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2131                                  phydev->advertising);
2132                 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2133                                  phydev->advertising);
2134         }
2135 
2136         if (tx)
2137                 linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2138                                     phydev->advertising);
2139 
2140         if (!linkmode_equal(oldadv, phydev->advertising) &&
2141             phydev->autoneg)
2142                 phy_start_aneg(phydev);
2143 }
2144 EXPORT_SYMBOL(phy_set_asym_pause);
2145 
2146 /**
2147  * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2148  * @phydev: phy_device struct
2149  * @pp: requested pause configuration
2150  *
2151  * Description: Test if the PHY/MAC combination supports the Pause
2152  * configuration the user is requesting. Returns True if it is
2153  * supported, false otherwise.
2154  */
2155 bool phy_validate_pause(struct phy_device *phydev,
2156                         struct ethtool_pauseparam *pp)
2157 {
2158         if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2159                                phydev->supported) && pp->rx_pause)
2160                 return false;
2161 
2162         if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2163                                phydev->supported) &&
2164             pp->rx_pause != pp->tx_pause)
2165                 return false;
2166 
2167         return true;
2168 }
2169 EXPORT_SYMBOL(phy_validate_pause);
2170 
2171 static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2172 {
2173         return phydrv->config_intr && phydrv->ack_interrupt;
2174 }
2175 
2176 /**
2177  * phy_probe - probe and init a PHY device
2178  * @dev: device to probe and init
2179  *
2180  * Description: Take care of setting up the phy_device structure,
2181  *   set the state to READY (the driver's init function should
2182  *   set it to STARTING if needed).
2183  */
2184 static int phy_probe(struct device *dev)
2185 {
2186         struct phy_device *phydev = to_phy_device(dev);
2187         struct device_driver *drv = phydev->mdio.dev.driver;
2188         struct phy_driver *phydrv = to_phy_driver(drv);
2189         int err = 0;
2190 
2191         phydev->drv = phydrv;
2192 
2193         /* Disable the interrupt if the PHY doesn't support it
2194          * but the interrupt is still a valid one
2195          */
2196          if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
2197                 phydev->irq = PHY_POLL;
2198 
2199         if (phydrv->flags & PHY_IS_INTERNAL)
2200                 phydev->is_internal = true;
2201 
2202         mutex_lock(&phydev->lock);
2203 
2204         if (phydev->drv->probe) {
2205                 /* Deassert the reset signal */
2206                 phy_device_reset(phydev, 0);
2207 
2208                 err = phydev->drv->probe(phydev);
2209                 if (err) {
2210                         /* Assert the reset signal */
2211                         phy_device_reset(phydev, 1);
2212                         goto out;
2213                 }
2214         }
2215 
2216         /* Start out supporting everything. Eventually,
2217          * a controller will attach, and may modify one
2218          * or both of these values
2219          */
2220         if (phydrv->features) {
2221                 linkmode_copy(phydev->supported, phydrv->features);
2222         } else if (phydrv->get_features) {
2223                 err = phydrv->get_features(phydev);
2224         } else if (phydev->is_c45) {
2225                 err = genphy_c45_pma_read_abilities(phydev);
2226         } else {
2227                 err = genphy_read_abilities(phydev);
2228         }
2229 
2230         if (err)
2231                 goto out;
2232 
2233         if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2234                                phydev->supported))
2235                 phydev->autoneg = 0;
2236 
2237         if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2238                               phydev->supported))
2239                 phydev->is_gigabit_capable = 1;
2240         if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2241                               phydev->supported))
2242                 phydev->is_gigabit_capable = 1;
2243 
2244         of_set_phy_supported(phydev);
2245         phy_advertise_supported(phydev);
2246 
2247         /* Get the EEE modes we want to prohibit. We will ask
2248          * the PHY stop advertising these mode later on
2249          */
2250         of_set_phy_eee_broken(phydev);
2251 
2252         /* The Pause Frame bits indicate that the PHY can support passing
2253          * pause frames. During autonegotiation, the PHYs will determine if
2254          * they should allow pause frames to pass.  The MAC driver should then
2255          * use that result to determine whether to enable flow control via
2256          * pause frames.
2257          *
2258          * Normally, PHY drivers should not set the Pause bits, and instead
2259          * allow phylib to do that.  However, there may be some situations
2260          * (e.g. hardware erratum) where the driver wants to set only one
2261          * of these bits.
2262          */
2263         if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
2264             !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
2265                 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2266                                  phydev->supported);
2267                 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2268                                  phydev->supported);
2269         }
2270 
2271         /* Set the state to READY by default */
2272         phydev->state = PHY_READY;
2273 
2274 out:
2275         mutex_unlock(&phydev->lock);
2276 
2277         return err;
2278 }
2279 
2280 static int phy_remove(struct device *dev)
2281 {
2282         struct phy_device *phydev = to_phy_device(dev);
2283 
2284         cancel_delayed_work_sync(&phydev->state_queue);
2285 
2286         mutex_lock(&phydev->lock);
2287         phydev->state = PHY_DOWN;
2288         mutex_unlock(&phydev->lock);
2289 
2290         if (phydev->drv && phydev->drv->remove) {
2291                 phydev->drv->remove(phydev);
2292 
2293                 /* Assert the reset signal */
2294                 phy_device_reset(phydev, 1);
2295         }
2296         phydev->drv = NULL;
2297 
2298         return 0;
2299 }
2300 
2301 /**
2302  * phy_driver_register - register a phy_driver with the PHY layer
2303  * @new_driver: new phy_driver to register
2304  * @owner: module owning this PHY
2305  */
2306 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
2307 {
2308         int retval;
2309 
2310         /* Either the features are hard coded, or dynamically
2311          * determined. It cannot be both.
2312          */
2313         if (WARN_ON(new_driver->features && new_driver->get_features)) {
2314                 pr_err("%s: features and get_features must not both be set\n",
2315                        new_driver->name);
2316                 return -EINVAL;
2317         }
2318 
2319         new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
2320         new_driver->mdiodrv.driver.name = new_driver->name;
2321         new_driver->mdiodrv.driver.bus = &mdio_bus_type;
2322         new_driver->mdiodrv.driver.probe = phy_probe;
2323         new_driver->mdiodrv.driver.remove = phy_remove;
2324         new_driver->mdiodrv.driver.owner = owner;
2325 
2326         retval = driver_register(&new_driver->mdiodrv.driver);
2327         if (retval) {
2328                 pr_err("%s: Error %d in registering driver\n",
2329                        new_driver->name, retval);
2330 
2331                 return retval;
2332         }
2333 
2334         pr_debug("%s: Registered new driver\n", new_driver->name);
2335 
2336         return 0;
2337 }
2338 EXPORT_SYMBOL(phy_driver_register);
2339 
2340 int phy_drivers_register(struct phy_driver *new_driver, int n,
2341                          struct module *owner)
2342 {
2343         int i, ret = 0;
2344 
2345         for (i = 0; i < n; i++) {
2346                 ret = phy_driver_register(new_driver + i, owner);
2347                 if (ret) {
2348                         while (i-- > 0)
2349                                 phy_driver_unregister(new_driver + i);
2350                         break;
2351                 }
2352         }
2353         return ret;
2354 }
2355 EXPORT_SYMBOL(phy_drivers_register);
2356 
2357 void phy_driver_unregister(struct phy_driver *drv)
2358 {
2359         driver_unregister(&drv->mdiodrv.driver);
2360 }
2361 EXPORT_SYMBOL(phy_driver_unregister);
2362 
2363 void phy_drivers_unregister(struct phy_driver *drv, int n)
2364 {
2365         int i;
2366 
2367         for (i = 0; i < n; i++)
2368                 phy_driver_unregister(drv + i);
2369 }
2370 EXPORT_SYMBOL(phy_drivers_unregister);
2371 
2372 static struct phy_driver genphy_driver = {
2373         .phy_id         = 0xffffffff,
2374         .phy_id_mask    = 0xffffffff,
2375         .name           = "Generic PHY",
2376         .soft_reset     = genphy_no_soft_reset,
2377         .get_features   = genphy_read_abilities,
2378         .aneg_done      = genphy_aneg_done,
2379         .suspend        = genphy_suspend,
2380         .resume         = genphy_resume,
2381         .set_loopback   = genphy_loopback,
2382 };
2383 
2384 static int __init phy_init(void)
2385 {
2386         int rc;
2387 
2388         rc = mdio_bus_init();
2389         if (rc)
2390                 return rc;
2391 
2392         features_init();
2393 
2394         rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
2395         if (rc)
2396                 goto err_c45;
2397 
2398         rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2399         if (rc) {
2400                 phy_driver_unregister(&genphy_c45_driver);
2401 err_c45:
2402                 mdio_bus_exit();
2403         }
2404 
2405         return rc;
2406 }
2407 
2408 static void __exit phy_exit(void)
2409 {
2410         phy_driver_unregister(&genphy_c45_driver);
2411         phy_driver_unregister(&genphy_driver);
2412         mdio_bus_exit();
2413 }
2414 
2415 subsys_initcall(phy_init);
2416 module_exit(phy_exit);

/* [<][>][^][v][top][bottom][index][help] */