LCOV - code coverage report
Current view: top level - drivers/net/phy - phy_device.c (source / functions) Hit Total Coverage
Test: Real Lines: 42 681 6.2 %
Date: 2020-10-17 15:46:16 Functions: 0 83 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

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

Generated by: LCOV version 1.14