From: Oleksij Rempel o.rempel@pengutronix.de
[ Upstream commit 60f887b1290b43a4f5a3497982a725687b193fa4 ]
When a PHY is halted (e.g. `ip link set dev lan2 down`), several fields in struct phy_device may still reflect the last active connection. This leads to ethtool showing stale values even though the link is down.
Reset selected fields in _phy_state_machine() when transitioning to PHY_HALTED and the link was previously up:
- speed/duplex -> UNKNOWN, but only in autoneg mode (in forced mode these fields carry configuration, not status) - master_slave_state -> UNKNOWN if previously supported - mdix -> INVALID (state only, same meaning as "unknown") - lp_advertising -> always cleared
The cleanup is skipped if the PHY is in PHY_ERROR state, so the last values remain available for diagnostics.
Signed-off-by: Oleksij Rempel o.rempel@pengutronix.de Reviewed-by: Andrew Lunn andrew@lunn.ch Link: https://patch.msgid.link/20250917094751.2101285-1-o.rempel@pengutronix.de Signed-off-by: Jakub Kicinski kuba@kernel.org Signed-off-by: Sasha Levin sashal@kernel.org ---
LLM Generated explanations, may be completely bogus:
YES - `_phy_state_machine()` now clears the runtime status fields (speed, duplex, master/slave, MDI-X, partner advertising) when transitioning a previously up PHY into `PHY_HALTED`, so administrative link-down stops reporting stale values to ethtool (`drivers/net/phy/phy.c:1551-1561`). - These members are exactly what `phy_ethtool_ksettings_get()` surfaces to user space, so leaving them stale makes `ethtool link`/`ip link` misreport the link after an admin down; the new resets ensure the user-visible API reflects that the link is unknown/down (`drivers/net/phy/phy.c:273-296`). - The change is careful to leave forced-mode configurations intact (`phydev->autoneg == AUTONEG_ENABLE` guard at `drivers/net/phy/phy.c:1552-1555`) and avoids touching hardware registers, which keeps the risk of behavioural regressions low. - Master/slave state is only reset when the feature is supported, while diagnostics in `PHY_ERROR` still retain the last negotiated information thanks to the guarded fall-through (`drivers/net/phy/phy.c:1556-1569`). - The touched fields are long-standing members of `struct phy_device` (`include/linux/phy.h:665-713`), so the patch is self-contained, architecture-neutral, and aligns with an earlier mainline fix that already clears EEE runtime flags on the same state transition.
drivers/net/phy/phy.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c index e046dd858f151..02da4a203ddd4 100644 --- a/drivers/net/phy/phy.c +++ b/drivers/net/phy/phy.c @@ -1548,6 +1548,19 @@ static enum phy_state_work _phy_state_machine(struct phy_device *phydev) } break; case PHY_HALTED: + if (phydev->link) { + if (phydev->autoneg == AUTONEG_ENABLE) { + phydev->speed = SPEED_UNKNOWN; + phydev->duplex = DUPLEX_UNKNOWN; + } + if (phydev->master_slave_state != + MASTER_SLAVE_STATE_UNSUPPORTED) + phydev->master_slave_state = + MASTER_SLAVE_STATE_UNKNOWN; + phydev->mdix = ETH_TP_MDI_INVALID; + linkmode_zero(phydev->lp_advertising); + } + fallthrough; case PHY_ERROR: if (phydev->link) { phydev->link = 0;