phy_device.c (c0f00d270eba3cba688dd62a7b1a742ad289b879) phy_device.c (a87ae8a963bde755b0962bcc18db83d611f63e7a)
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 */

--- 867 unchanged lines hidden (view full) ---

876 * @phydev: phy_device structure to remove
877 *
878 * This doesn't free the phy_device itself, it merely reverses the effects
879 * of phy_device_register(). Use phy_device_free() to free the device
880 * after calling this function.
881 */
882void phy_device_remove(struct phy_device *phydev)
883{
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 */

--- 867 unchanged lines hidden (view full) ---

876 * @phydev: phy_device structure to remove
877 *
878 * This doesn't free the phy_device itself, it merely reverses the effects
879 * of phy_device_register(). Use phy_device_free() to free the device
880 * after calling this function.
881 */
882void phy_device_remove(struct phy_device *phydev)
883{
884 if (phydev->mii_ts)
885 unregister_mii_timestamper(phydev->mii_ts);
886
884 device_del(&phydev->mdio.dev);
885
886 /* Assert the reset signal */
887 phy_device_reset(phydev, 1);
888
889 mdiobus_unregister_device(&phydev->mdio);
890}
891EXPORT_SYMBOL(phy_device_remove);

--- 22 unchanged lines hidden (view full) ---

914
915 if (do_carrier) {
916 if (up)
917 netif_carrier_on(netdev);
918 else
919 netif_carrier_off(netdev);
920 }
921 phydev->adjust_link(netdev);
887 device_del(&phydev->mdio.dev);
888
889 /* Assert the reset signal */
890 phy_device_reset(phydev, 1);
891
892 mdiobus_unregister_device(&phydev->mdio);
893}
894EXPORT_SYMBOL(phy_device_remove);

--- 22 unchanged lines hidden (view full) ---

917
918 if (do_carrier) {
919 if (up)
920 netif_carrier_on(netdev);
921 else
922 netif_carrier_off(netdev);
923 }
924 phydev->adjust_link(netdev);
925 if (phydev->mii_ts && phydev->mii_ts->link_state)
926 phydev->mii_ts->link_state(phydev->mii_ts, phydev);
922}
923
924/**
925 * phy_prepare_link - prepares the PHY layer to monitor link status
926 * @phydev: target phy_device struct
927 * @handler: callback function for link status change notifications
928 *
929 * Description: Tells the PHY infrastructure to handle the

--- 167 unchanged lines hidden (view full) ---

1097
1098void phy_attached_info(struct phy_device *phydev)
1099{
1100 phy_attached_print(phydev, NULL);
1101}
1102EXPORT_SYMBOL(phy_attached_info);
1103
1104#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
927}
928
929/**
930 * phy_prepare_link - prepares the PHY layer to monitor link status
931 * @phydev: target phy_device struct
932 * @handler: callback function for link status change notifications
933 *
934 * Description: Tells the PHY infrastructure to handle the

--- 167 unchanged lines hidden (view full) ---

1102
1103void phy_attached_info(struct phy_device *phydev)
1104{
1105 phy_attached_print(phydev, NULL);
1106}
1107EXPORT_SYMBOL(phy_attached_info);
1108
1109#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
1105void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1110char *phy_attached_info_irq(struct phy_device *phydev)
1106{
1111{
1107 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
1108 char *irq_str;
1109 char irq_num[8];
1110
1111 switch(phydev->irq) {
1112 case PHY_POLL:
1113 irq_str = "POLL";
1114 break;
1115 case PHY_IGNORE_INTERRUPT:
1116 irq_str = "IGNORE";
1117 break;
1118 default:
1119 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1120 irq_str = irq_num;
1121 break;
1122 }
1123
1112 char *irq_str;
1113 char irq_num[8];
1114
1115 switch(phydev->irq) {
1116 case PHY_POLL:
1117 irq_str = "POLL";
1118 break;
1119 case PHY_IGNORE_INTERRUPT:
1120 irq_str = "IGNORE";
1121 break;
1122 default:
1123 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1124 irq_str = irq_num;
1125 break;
1126 }
1127
1128 return kasprintf(GFP_KERNEL, "%s", irq_str);
1129}
1130EXPORT_SYMBOL(phy_attached_info_irq);
1124
1131
1132void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1133{
1134 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
1135 char *irq_str = phy_attached_info_irq(phydev);
1136
1125 if (!fmt) {
1126 phydev_info(phydev, ATTACHED_FMT "\n",
1127 drv_name, phydev_name(phydev),
1128 irq_str);
1129 } else {
1130 va_list ap;
1131
1132 phydev_info(phydev, ATTACHED_FMT,
1133 drv_name, phydev_name(phydev),
1134 irq_str);
1135
1136 va_start(ap, fmt);
1137 vprintk(fmt, ap);
1138 va_end(ap);
1139 }
1137 if (!fmt) {
1138 phydev_info(phydev, ATTACHED_FMT "\n",
1139 drv_name, phydev_name(phydev),
1140 irq_str);
1141 } else {
1142 va_list ap;
1143
1144 phydev_info(phydev, ATTACHED_FMT,
1145 drv_name, phydev_name(phydev),
1146 irq_str);
1147
1148 va_start(ap, fmt);
1149 vprintk(fmt, ap);
1150 va_end(ap);
1151 }
1152 kfree(irq_str);
1140}
1141EXPORT_SYMBOL(phy_attached_print);
1142
1143static void phy_sysfs_create_links(struct phy_device *phydev)
1144{
1145 struct net_device *dev = phydev->attached_dev;
1146 int err;
1147

--- 618 unchanged lines hidden (view full) ---

1766{
1767 /* Don't isolate the PHY if we're negotiating */
1768 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1769 BMCR_ANENABLE | BMCR_ANRESTART);
1770}
1771EXPORT_SYMBOL(genphy_restart_aneg);
1772
1773/**
1153}
1154EXPORT_SYMBOL(phy_attached_print);
1155
1156static void phy_sysfs_create_links(struct phy_device *phydev)
1157{
1158 struct net_device *dev = phydev->attached_dev;
1159 int err;
1160

--- 618 unchanged lines hidden (view full) ---

1779{
1780 /* Don't isolate the PHY if we're negotiating */
1781 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1782 BMCR_ANENABLE | BMCR_ANRESTART);
1783}
1784EXPORT_SYMBOL(genphy_restart_aneg);
1785
1786/**
1787 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
1788 * @phydev: target phy_device struct
1789 * @restart: whether aneg restart is requested
1790 *
1791 * Check, and restart auto-negotiation if needed.
1792 */
1793int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
1794{
1795 int ret = 0;
1796
1797 if (!restart) {
1798 /* Advertisement hasn't changed, but maybe aneg was never on to
1799 * begin with? Or maybe phy was isolated?
1800 */
1801 ret = phy_read(phydev, MII_BMCR);
1802 if (ret < 0)
1803 return ret;
1804
1805 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
1806 restart = true;
1807 }
1808
1809 if (restart)
1810 ret = genphy_restart_aneg(phydev);
1811
1812 return ret;
1813}
1814EXPORT_SYMBOL(genphy_check_and_restart_aneg);
1815
1816/**
1774 * __genphy_config_aneg - restart auto-negotiation or write BMCR
1775 * @phydev: target phy_device struct
1776 * @changed: whether autoneg is requested
1777 *
1778 * Description: If auto-negotiation is enabled, we configure the
1779 * advertising, and then restart auto-negotiation. If it is not
1780 * enabled, then we write the BMCR.
1781 */

--- 8 unchanged lines hidden (view full) ---

1790 return genphy_setup_forced(phydev);
1791
1792 err = genphy_config_advert(phydev);
1793 if (err < 0) /* error */
1794 return err;
1795 else if (err)
1796 changed = true;
1797
1817 * __genphy_config_aneg - restart auto-negotiation or write BMCR
1818 * @phydev: target phy_device struct
1819 * @changed: whether autoneg is requested
1820 *
1821 * Description: If auto-negotiation is enabled, we configure the
1822 * advertising, and then restart auto-negotiation. If it is not
1823 * enabled, then we write the BMCR.
1824 */

--- 8 unchanged lines hidden (view full) ---

1833 return genphy_setup_forced(phydev);
1834
1835 err = genphy_config_advert(phydev);
1836 if (err < 0) /* error */
1837 return err;
1838 else if (err)
1839 changed = true;
1840
1798 if (!changed) {
1799 /* Advertisement hasn't changed, but maybe aneg was never on to
1800 * begin with? Or maybe phy was isolated?
1801 */
1802 int ctl = phy_read(phydev, MII_BMCR);
1803
1804 if (ctl < 0)
1805 return ctl;
1806
1807 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1808 changed = true; /* do restart aneg */
1809 }
1810
1811 /* Only restart aneg if we are advertising something different
1812 * than we were before.
1813 */
1814 return changed ? genphy_restart_aneg(phydev) : 0;
1841 return genphy_check_and_restart_aneg(phydev, changed);
1815}
1816EXPORT_SYMBOL(__genphy_config_aneg);
1817
1818/**
1819 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
1820 * @phydev: target phy_device struct
1821 *
1822 * Description: If auto-negotiation is enabled, we configure the

--- 151 unchanged lines hidden (view full) ---

1974 linkmode_zero(phydev->lp_advertising);
1975 }
1976
1977 return 0;
1978}
1979EXPORT_SYMBOL(genphy_read_lpa);
1980
1981/**
1842}
1843EXPORT_SYMBOL(__genphy_config_aneg);
1844
1845/**
1846 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
1847 * @phydev: target phy_device struct
1848 *
1849 * Description: If auto-negotiation is enabled, we configure the

--- 151 unchanged lines hidden (view full) ---

2001 linkmode_zero(phydev->lp_advertising);
2002 }
2003
2004 return 0;
2005}
2006EXPORT_SYMBOL(genphy_read_lpa);
2007
2008/**
2009 * genphy_read_status_fixed - read the link parameters for !aneg mode
2010 * @phydev: target phy_device struct
2011 *
2012 * Read the current duplex and speed state for a PHY operating with
2013 * autonegotiation disabled.
2014 */
2015int genphy_read_status_fixed(struct phy_device *phydev)
2016{
2017 int bmcr = phy_read(phydev, MII_BMCR);
2018
2019 if (bmcr < 0)
2020 return bmcr;
2021
2022 if (bmcr & BMCR_FULLDPLX)
2023 phydev->duplex = DUPLEX_FULL;
2024 else
2025 phydev->duplex = DUPLEX_HALF;
2026
2027 if (bmcr & BMCR_SPEED1000)
2028 phydev->speed = SPEED_1000;
2029 else if (bmcr & BMCR_SPEED100)
2030 phydev->speed = SPEED_100;
2031 else
2032 phydev->speed = SPEED_10;
2033
2034 return 0;
2035}
2036EXPORT_SYMBOL(genphy_read_status_fixed);
2037
2038/**
1982 * genphy_read_status - check the link status and update current link state
1983 * @phydev: target phy_device struct
1984 *
1985 * Description: Check the link, then figure out the current state
1986 * by comparing what we advertise with what the link partner
1987 * advertises. Start by checking the gigabit possibilities,
1988 * then move on to 10/100.
1989 */

--- 17 unchanged lines hidden (view full) ---

2007
2008 err = genphy_read_lpa(phydev);
2009 if (err < 0)
2010 return err;
2011
2012 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2013 phy_resolve_aneg_linkmode(phydev);
2014 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2039 * genphy_read_status - check the link status and update current link state
2040 * @phydev: target phy_device struct
2041 *
2042 * Description: Check the link, then figure out the current state
2043 * by comparing what we advertise with what the link partner
2044 * advertises. Start by checking the gigabit possibilities,
2045 * then move on to 10/100.
2046 */

--- 17 unchanged lines hidden (view full) ---

2064
2065 err = genphy_read_lpa(phydev);
2066 if (err < 0)
2067 return err;
2068
2069 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2070 phy_resolve_aneg_linkmode(phydev);
2071 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2015 int bmcr = phy_read(phydev, MII_BMCR);
2016
2017 if (bmcr < 0)
2018 return bmcr;
2019
2020 if (bmcr & BMCR_FULLDPLX)
2021 phydev->duplex = DUPLEX_FULL;
2022 else
2023 phydev->duplex = DUPLEX_HALF;
2024
2025 if (bmcr & BMCR_SPEED1000)
2026 phydev->speed = SPEED_1000;
2027 else if (bmcr & BMCR_SPEED100)
2028 phydev->speed = SPEED_100;
2029 else
2030 phydev->speed = SPEED_10;
2072 err = genphy_read_status_fixed(phydev);
2073 if (err < 0)
2074 return err;
2031 }
2032
2033 return 0;
2034}
2035EXPORT_SYMBOL(genphy_read_status);
2036
2037/**
2038 * genphy_c37_read_status - check the link status and update current link state

--- 321 unchanged lines hidden (view full) ---

2360 phydev->supported) &&
2361 pp->rx_pause != pp->tx_pause)
2362 return false;
2363
2364 return true;
2365}
2366EXPORT_SYMBOL(phy_validate_pause);
2367
2075 }
2076
2077 return 0;
2078}
2079EXPORT_SYMBOL(genphy_read_status);
2080
2081/**
2082 * genphy_c37_read_status - check the link status and update current link state

--- 321 unchanged lines hidden (view full) ---

2404 phydev->supported) &&
2405 pp->rx_pause != pp->tx_pause)
2406 return false;
2407
2408 return true;
2409}
2410EXPORT_SYMBOL(phy_validate_pause);
2411
2412/**
2413 * phy_get_pause - resolve negotiated pause modes
2414 * @phydev: phy_device struct
2415 * @tx_pause: pointer to bool to indicate whether transmit pause should be
2416 * enabled.
2417 * @rx_pause: pointer to bool to indicate whether receive pause should be
2418 * enabled.
2419 *
2420 * Resolve and return the flow control modes according to the negotiation
2421 * result. This includes checking that we are operating in full duplex mode.
2422 * See linkmode_resolve_pause() for further details.
2423 */
2424void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
2425{
2426 if (phydev->duplex != DUPLEX_FULL) {
2427 *tx_pause = false;
2428 *rx_pause = false;
2429 return;
2430 }
2431
2432 return linkmode_resolve_pause(phydev->advertising,
2433 phydev->lp_advertising,
2434 tx_pause, rx_pause);
2435}
2436EXPORT_SYMBOL(phy_get_pause);
2437
2368static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2369{
2370 return phydrv->config_intr && phydrv->ack_interrupt;
2371}
2372
2373/**
2374 * phy_probe - probe and init a PHY device
2375 * @dev: device to probe and init

--- 194 unchanged lines hidden (view full) ---

2570EXPORT_SYMBOL(phy_drivers_unregister);
2571
2572static struct phy_driver genphy_driver = {
2573 .phy_id = 0xffffffff,
2574 .phy_id_mask = 0xffffffff,
2575 .name = "Generic PHY",
2576 .soft_reset = genphy_no_soft_reset,
2577 .get_features = genphy_read_abilities,
2438static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2439{
2440 return phydrv->config_intr && phydrv->ack_interrupt;
2441}
2442
2443/**
2444 * phy_probe - probe and init a PHY device
2445 * @dev: device to probe and init

--- 194 unchanged lines hidden (view full) ---

2640EXPORT_SYMBOL(phy_drivers_unregister);
2641
2642static struct phy_driver genphy_driver = {
2643 .phy_id = 0xffffffff,
2644 .phy_id_mask = 0xffffffff,
2645 .name = "Generic PHY",
2646 .soft_reset = genphy_no_soft_reset,
2647 .get_features = genphy_read_abilities,
2578 .aneg_done = genphy_aneg_done,
2579 .suspend = genphy_suspend,
2580 .resume = genphy_resume,
2581 .set_loopback = genphy_loopback,
2582};
2583
2584static int __init phy_init(void)
2585{
2586 int rc;

--- 30 unchanged lines hidden ---
2648 .suspend = genphy_suspend,
2649 .resume = genphy_resume,
2650 .set_loopback = genphy_loopback,
2651};
2652
2653static int __init phy_init(void)
2654{
2655 int rc;

--- 30 unchanged lines hidden ---