[PATCH net 3/4] net: phy: quiesce the state machine during MII resets
From: James Hilliard
Date: Fri Nov 13 2026 - 05:11:56 EST
phy_mii_ioctl() performs a BMCR reset write and calls phy_init_hw() while
the PHY state machine remains active. The worker can access the PHY after
reset but before the driver has restored its configuration and software
state. A statistics update in this window can interpret cleared hardware
counters as a wraparound.
Disable and drain the state-machine work before resetting the attached
Clause 22 PHY. Reenable it and trigger a new check afterward, including on
failure. Disabling the work also prevents interrupt notifications from
queuing it during reinitialization.
Wait for BMCR_RESET to clear before calling phy_init_hw(). Drivers without
a soft_reset callback otherwise begin initialization while the requested
reset may still be in progress. Share phy_poll_reset() internally for this
wait and propagate its read errors or timeout before initializing the PHY.
Do not hold the PHY mutex across phy_init_hw(): some driver initialization
callbacks acquire that mutex themselves.
Fixes: 00db8189d984 ("This patch adds a PHY Abstraction Layer to the Linux Kernel, enabling ethernet drivers to remain as ignorant as is reasonable of the connected PHY's design and operation details.")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/net/phy/phy.c | 27 +++++++++++++++++++++++++++
drivers/net/phy/phy_device.c | 2 +-
drivers/net/phy/phylib-internal.h | 1 +
3 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index 4ac72a1b4f42..5da8a699446c 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -297,6 +297,30 @@ void phy_ethtool_ksettings_get(struct phy_device *phydev,
}
EXPORT_SYMBOL(phy_ethtool_ksettings_get);
+static int phy_mii_ioctl_reset(struct phy_device *phydev, u16 val)
+{
+ int ret;
+
+ /* Keep the worker out until the driver has restored its state. */
+ disable_delayed_work_sync(&phydev->state_queue);
+
+ ret = phy_write(phydev, MII_BMCR, val);
+ if (ret < 0)
+ goto out;
+
+ ret = phy_poll_reset(phydev);
+ if (ret)
+ goto out;
+
+ phydev->autoneg = AUTONEG_ENABLE;
+ ret = phy_init_hw(phydev);
+out:
+ if (enable_delayed_work(&phydev->state_queue))
+ phy_trigger_machine(phydev);
+
+ return ret;
+}
+
/**
* phy_mii_ioctl - generic PHY MII ioctl interface
* @phydev: the phy_device struct
@@ -349,6 +373,9 @@ int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
} else {
prtad = mii_data->phy_id;
devad = mii_data->reg_num;
+ if (prtad == phydev->mdio.addr &&
+ devad == MII_BMCR && val & BMCR_RESET)
+ return phy_mii_ioctl_reset(phydev, val);
}
if (mdio_phy_id_is_c45(mii_data->phy_id))
diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
index b39e945cebd7..8b0c1f28963b 100644
--- a/drivers/net/phy/phy_device.c
+++ b/drivers/net/phy/phy_device.c
@@ -1232,7 +1232,7 @@ EXPORT_SYMBOL(phy_disconnect);
* standard phy_init_hw() which will zero all the other bits in the BMCR
* and reapply all driver-specific and board-specific fixups.
*/
-static int phy_poll_reset(struct phy_device *phydev)
+int phy_poll_reset(struct phy_device *phydev)
{
/* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
int ret, val;
diff --git a/drivers/net/phy/phylib-internal.h b/drivers/net/phy/phylib-internal.h
index 38ee294f3c96..680823633d98 100644
--- a/drivers/net/phy/phylib-internal.h
+++ b/drivers/net/phy/phylib-internal.h
@@ -24,6 +24,7 @@ void of_set_phy_timing_role(struct phy_device *phydev);
int phy_speed_down_core(struct phy_device *phydev);
void phy_check_downshift(struct phy_device *phydev);
int phy_scan_fixups(struct phy_device *phydev);
+int phy_poll_reset(struct phy_device *phydev);
int mdiobus_register_device(struct mdio_device *mdiodev);
int mdiobus_unregister_device(struct mdio_device *mdiodev);
--
2.53.0