net: phy: move phy_link_change() prior to mdio_bus_phy_may_suspend()
PATCH 1/3 net: phy: move phy_link_change() prior to mdio_bus_phy_may_suspend()
From: Vladimir Oltean <[email protected]>
[ Upstream commit f40a673d6b4a128fe95dd9b8c3ed02da50a6a862 ]
In an upcoming change, mdio_bus_phy_may_suspend() will need to distinguish a phylib-based PHY client from a phylink PHY client. For that, it will need to compare the phydev->phy_link_change() function pointer with the eponymous phy_link_change() provided by phylib.
To avoid forward function declarations, the default PHY link state change method should be moved upwards. There is no functional change associated with this patch, it is only to reduce the noise from a real bug fix.
---
--- drivers/net/phy/phy_device.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/net/phy/phy_device.c +13 -13
@@ -268,6 +268,19 @@ static struct phy_driver genphy_driver;static LIST_HEAD (phy_fixup_list);static DEFINE_MUTEX (phy_fixup_lock); +static void phy_link_change (struct phy_device *phydev,bool up) +{ +struct net_device *netdev = phydev->attached_dev; + +if (up) +netif_carrier_on (netdev); +else +netif_carrier_off (netdev); + phydev->adjust_link (netdev); +if (phydev->mii_ts && phydev->mii_ts->link_state) + phydev->mii_ts->link_state (phydev->mii_ts, phydev); +} +static bool mdio_bus_phy_may_suspend (struct phy_device *phydev) {struct device_driver *drv = phydev->mdio.dev.driver;
@@ -1069,19 +1082,6 @@ struct phy_device *phy_find_first(struct mii_bus *bus) }EXPORT_SYMBOL (phy_find_first); -static void phy_link_change (struct phy_device *phydev,bool up) -{ -struct net_device *netdev = phydev->attached_dev; - -if (up) -netif_carrier_on (netdev); -else -netif_carrier_off (netdev); - phydev->adjust_link (netdev); -if (phydev->mii_ts && phydev->mii_ts->link_state) - phydev->mii_ts->link_state (phydev->mii_ts, phydev); -} -/** * phy_prepare_link - prepares the PHY layer to monitor link status * @phydev: target phy_device struct
Signature · 1 line
2.34.1
PATCH 2/3 net: phy: allow MDIO bus PM ops to start/stop state machine for phylink-controlled PHY
From: Vladimir Oltean <[email protected]>
[ Upstream commit fc75ea20ffb452652f0d4033f38fe88d7cfdae35 ]
DSA has 2 kinds of drivers:
1. Those who call dsa_switch_suspend() and dsa_switch_resume() from their device PM ops: qca8k-8xxx, bcm_sf2, microchip ksz
2. Those who don't: all others. The above methods should be optional.
For type 1, dsa_switch_suspend() calls dsa_user_suspend() -> phylink_stop(), and dsa_switch_resume() calls dsa_user_resume() -> phylink_start(). These seem good candidates for setting mac_managed_pm = true because that is essentially its definition [1], but that does not seem to be the biggest problem for now, and is not what this change focuses on.
Talking strictly about the 2nd category of DSA drivers here (which do not have MAC managed PM, meaning that for their attached PHYs, mdio_bus_phy_suspend() and mdio_bus_phy_resume() should run in full), I have noticed that the following warning from mdio_bus_phy_resume() is triggered:
WARN_ON(phydev->state != PHY_HALTED && phydev->state != PHY_READY && phydev->state != PHY_UP);
because the PHY state machine is running.
It's running as a result of a previous dsa_user_open() -> ... -> phylink_start() -> phy_start() having been initiated by the user.
The previous mdio_bus_phy_suspend() was supposed to have called phy_stop_machine(), but it didn't. So this is why the PHY is in state PHY_NOLINK by the time mdio_bus_phy_resume() runs.
mdio_bus_phy_suspend() did not call phy_stop_machine() because for phylink, the phydev->adjust_link function pointer is NULL. This seems a technicality introduced by commit fddd91016d16 ("phylib: fix PAL state machine restart on resume"). That commit was written before phylink existed, and was intended to avoid crashing with consumer drivers which don't use the PHY state machine - phylink always does, when using a PHY. But phylink itself has historically not been developed with suspend/resume in mind, and apparently not tested too much in that scenario, allowing this bug to exist unnoticed for so long. Plus, prior to the WARN_ON(), it would have likely been invisible.
This issue is not in fact restricted to type 2 DSA drivers (according to the above ad-hoc classification), but can be extrapolated to any MAC driver with phylink and MDIO-bus-managed PHY PM ops. DSA is just where the issue was reported. Assuming mac_managed_pm is set correctly, a quick search indicates the following other drivers might be affected:
$ grep -Zlr PHYLINK_NETDEV drivers/ | xargs -0 grep -L mac_managed_pm drivers/net/ethernet/atheros/ag71xx.c drivers/net/ethernet/microchip/sparx5/sparx5_main.c drivers/net/ethernet/microchip/lan966x/lan966x_main.c drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c drivers/net/ethernet/freescale/fs_enet/fs_enet-main.c drivers/net/ethernet/freescale/dpaa/dpaa_eth.c drivers/net/ethernet/freescale/ucc_geth.c drivers/net/ethernet/freescale/enetc/enetc_pf_common.c drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c drivers/net/ethernet/marvell/mvneta.c drivers/net/ethernet/marvell/prestera/prestera_main.c drivers/net/ethernet/mediatek/mtk_eth_soc.c drivers/net/ethernet/altera/altera_tse_main.c drivers/net/ethernet/wangxun/txgbe/txgbe_phy.c drivers/net/ethernet/meta/fbnic/fbnic_phylink.c drivers/net/ethernet/tehuti/tn40_phy.c drivers/net/ethernet/mscc/ocelot_net.c
Make the existing conditions dependent on the PHY device having a phydev->phy_link_change() implementation equal to the default phy_link_change() provided by phylib. Otherwise, we implicitly know that the phydev has the phylink-provided phylink_phy_change() callback, and when phylink is used, the PHY state machine always needs to be stopped/ started on the suspend/resume path. The code is structured as such that if phydev->phy_link_change() is absent, it is a matter of time until the kernel will crash - no need to further complicate the test.
Thus, for the situation where the PM is not managed by the MAC, we will make the MDIO bus PM ops treat identically the phylink-controlled PHYs with the phylib-controlled PHYs where an adjust_link() callback is supplied. In both cases, the MDIO bus PM ops should stop and restart the PHY state machine.
[1] https://lore.kernel.org/netdev/[email protected]/
---
--- drivers/net/phy/phy_device.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-)
drivers/net/phy/phy_device.c +29 -2
@@ -281,6 +281,33 @@ static void phy_link_change(struct phy_device *phydev, bool up) phydev->mii_ts->link_state (phydev->mii_ts, phydev); } +/** +* phy_uses_state_machine - test whether consumer driver uses PAL state machine +* @phydev: the target PHY device structure +* +* Ultimately, this aims to indirectly determine whether the PHY is attached +* to a consumer which uses the state machine by calling phy_start() and +* phy_stop(). +* +* When the PHY driver consumer uses phylib, it must have previously called +* phy_connect_direct() or one of its derivatives, so that phy_prepare_link() +* has set up a hook for monitoring state changes. +* +* When the PHY driver is used by the MAC driver consumer through phylink (the +* only other provider of a phy_link_change() method), using the PHY state +* machine is not optional. +* +* Return: true if consumer calls phy_start() and phy_stop(), false otherwise. +*/ +static bool phy_uses_state_machine (struct phy_device *phydev) +{ +if (phydev->phy_link_change == phy_link_change) +return phydev->attached_dev && phydev->adjust_link; + +/* phydev->phy_link_change is implicitly phylink_phy_change() */ +return true; +} +static bool mdio_bus_phy_may_suspend (struct phy_device *phydev) {struct device_driver *drv = phydev->mdio.dev.driver;
@@ -341,7 +368,7 @@ static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)* may call phy routines that try to grab the same lock, and that may * lead to a deadlock. */ -if (phydev->attached_dev && phydev->adjust_link) +if (phy_uses_state_machine (phydev))phy_stop_machine (phydev);if (!mdio_bus_phy_may_suspend (phydev))
@@ -395,7 +422,7 @@ static __maybe_unused int mdio_bus_phy_resume(struct device *dev) } } -if (phydev->attached_dev && phydev->adjust_link) +if (phy_uses_state_machine (phydev))phy_start_machine (phydev);return 0 ;
Signature · 1 line
2.34.1
PATCH 3/3 net: phy: fix phy_uses_state_machine()
In reply to Rajani KanthaFrom: "Russell King (Oracle)" <[email protected]>
[ Upstream commit e0d1c55501d377163eb57feed863777ed1c973ad ]
The blamed commit changed the conditions which phylib uses to stop and start the state machine in the suspend and resume paths, and while improving it, has caused two issues.
The original code used this test:
phydev->attached_dev && phydev->adjust_link
and if true, the paths would handle the PHY state machine. This test evaluates true for normal drivers that are using phylib directly while the PHY is attached to the network device, but false in all other cases, which include the following cases:
- when the PHY has never been attached to a network device.
- when the PHY has been detached from a network device (as phy_detach() sets phydev->attached_dev to NULL, phy_disconnect() calls phy_detach() and additionally sets phydev->adjust_link NULL.)
- when phylink is using the driver (as phydev->adjust_link is NULL.)
Only the third case was incorrect, and the blamed commit attempted to fix this by changing this test to (simplified for brevity, see phy_uses_state_machine()):
phydev->phy_link_change == phy_link_change ? phydev->attached_dev && phydev->adjust_link : true
However, this also incorrectly evaluates true in the first two cases.
Fix the first case by ensuring that phy_uses_state_machine() returns false when phydev->phy_link_change is NULL.
Fix the second case by ensuring that phydev->phy_link_change is set to NULL when phy_detach() is called.
--- drivers/net/phy/phy_device.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/net/phy/phy_device.c +3 -2
@@ -304,8 +304,7 @@ static bool phy_uses_state_machine(struct phy_device *phydev)if (phydev->phy_link_change == phy_link_change)return phydev->attached_dev && phydev->adjust_link; -/* phydev->phy_link_change is implicitly phylink_phy_change() */ -return true; +return !!phydev->phy_link_change; }static bool mdio_bus_phy_may_suspend (struct phy_device *phydev)
@@ -1853,6 +1852,8 @@ void phy_detach(struct phy_device *phydev) phydev->attached_dev->phydev =NULL ; phydev->attached_dev =NULL ; } + + phydev->phy_link_change =NULL ; phydev->phylink =NULL ;if (!phydev->is_on_sfp_module)
Signature · 1 line
2.34.1