Skip to content

Commit fc75ea2

Browse files
vladimirolteankuba-moo
authored andcommitted
net: phy: allow MDIO bus PM ops to start/stop state machine for phylink-controlled PHY
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 fddd910 ("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]/ Fixes: 744d23c ("net: phy: Warn about incorrect mdio_bus_phy_resume() state") Reported-by: Wei Fang <[email protected]> Tested-by: Wei Fang <[email protected]> Signed-off-by: Vladimir Oltean <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent f40a673 commit fc75ea2

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

drivers/net/phy/phy_device.c

+29-2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,33 @@ static void phy_link_change(struct phy_device *phydev, bool up)
257257
phydev->mii_ts->link_state(phydev->mii_ts, phydev);
258258
}
259259

260+
/**
261+
* phy_uses_state_machine - test whether consumer driver uses PAL state machine
262+
* @phydev: the target PHY device structure
263+
*
264+
* Ultimately, this aims to indirectly determine whether the PHY is attached
265+
* to a consumer which uses the state machine by calling phy_start() and
266+
* phy_stop().
267+
*
268+
* When the PHY driver consumer uses phylib, it must have previously called
269+
* phy_connect_direct() or one of its derivatives, so that phy_prepare_link()
270+
* has set up a hook for monitoring state changes.
271+
*
272+
* When the PHY driver is used by the MAC driver consumer through phylink (the
273+
* only other provider of a phy_link_change() method), using the PHY state
274+
* machine is not optional.
275+
*
276+
* Return: true if consumer calls phy_start() and phy_stop(), false otherwise.
277+
*/
278+
static bool phy_uses_state_machine(struct phy_device *phydev)
279+
{
280+
if (phydev->phy_link_change == phy_link_change)
281+
return phydev->attached_dev && phydev->adjust_link;
282+
283+
/* phydev->phy_link_change is implicitly phylink_phy_change() */
284+
return true;
285+
}
286+
260287
static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
261288
{
262289
struct device_driver *drv = phydev->mdio.dev.driver;
@@ -323,7 +350,7 @@ static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
323350
* may call phy routines that try to grab the same lock, and that may
324351
* lead to a deadlock.
325352
*/
326-
if (phydev->attached_dev && phydev->adjust_link)
353+
if (phy_uses_state_machine(phydev))
327354
phy_stop_machine(phydev);
328355

329356
if (!mdio_bus_phy_may_suspend(phydev))
@@ -377,7 +404,7 @@ static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
377404
}
378405
}
379406

380-
if (phydev->attached_dev && phydev->adjust_link)
407+
if (phy_uses_state_machine(phydev))
381408
phy_start_machine(phydev);
382409

383410
return 0;

0 commit comments

Comments
 (0)