Some PHY drivers can reliably report link-down events via IRQs,
but may fail to generate reliable link-up IRQs. To support such
cases, polling is often needed - but only selectively.
Extend get_next_update_time() so drivers can return PHY_STATE_IRQ
to indicate that polling is not needed and IRQs are sufficient.
This allows finer control over PHY state machine behavior.
Introduce PHY_STATE_IRQ (UINT_MAX) as a sentinel value, and move
PHY_STATE_TIME to phy.h to allow consistent use across the codebase.
This change complements the previous patch enabling polling when
get_next_update_time() is present.
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
changes v2:
- this patch is added
---
drivers/net/phy/phy.c | 18 +++++++++++-------
include/linux/phy.h | 10 +++++++++-
2 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index 13df28445f02..faf9a48d3b6f 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -39,8 +39,6 @@
#include "phylib-internal.h"
#include "phy-caps.h"
-#define PHY_STATE_TIME HZ
-
#define PHY_STATE_STR(_state) \
case PHY_##_state: \
return __stringify(_state); \
@@ -1575,16 +1573,22 @@ static enum phy_state_work _phy_state_machine(struct phy_device *phydev)
phy_process_state_change(phydev, old_state);
/* Only re-schedule a PHY state machine change if we are polling the
- * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving
- * between states from phy_mac_interrupt().
+ * PHY. If PHY_MAC_INTERRUPT is set or get_next_update_time() returns
+ * PHY_STATE_IRQ, then we rely on interrupts for state changes.
*
* In state PHY_HALTED the PHY gets suspended, so rescheduling the
* state machine would be pointless and possibly error prone when
* called from phy_disconnect() synchronously.
*/
- if (phy_polling_mode(phydev) && phy_is_started(phydev))
- phy_queue_state_machine(phydev,
- phy_get_next_update_time(phydev));
+ if (phy_polling_mode(phydev) && phy_is_started(phydev)) {
+ unsigned int next_time = phy_get_next_update_time(phydev);
+
+ /* Drivers returning PHY_STATE_IRQ opt out of polling.
+ * Use IRQ-only mode by not re-queuing the state machine.
+ */
+ if (next_time != PHY_STATE_IRQ)
+ phy_queue_state_machine(phydev, next_time);
+ }
return state_work;
}
diff --git a/include/linux/phy.h b/include/linux/phy.h
index 3d4e5c41235e..d92258e3ac1a 100644
--- a/include/linux/phy.h
+++ b/include/linux/phy.h
@@ -66,6 +66,10 @@ extern const int phy_basic_ports_array[3];
#define PHY_ALWAYS_CALL_SUSPEND 0x00000008
#define MDIO_DEVICE_IS_PHY 0x80000000
+#define PHY_STATE_TIME HZ
+/* disable polling, rely on IRQs */
+#define PHY_STATE_IRQ UINT_MAX
+
/**
* enum phy_interface_t - Interface Mode definitions
*
@@ -1257,7 +1261,11 @@ struct phy_driver {
* dynamically adjust polling intervals based on link state or other
* conditions.
*
- * Returns the time in jiffies until the next update event.
+ * Returning PHY_STATE_IRQ disables polling and indicates that the
+ * driver relies solely on IRQs for link state changes.
+ *
+ * Returns the time in jiffies until the next update event, or
+ * PHY_STATE_IRQ to disable polling.
*/
unsigned int (*get_next_update_time)(struct phy_device *dev);
};
--
2.39.5
> /* Only re-schedule a PHY state machine change if we are polling the > - * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving > - * between states from phy_mac_interrupt(). > + * PHY. If PHY_MAC_INTERRUPT is set or get_next_update_time() returns > + * PHY_STATE_IRQ, then we rely on interrupts for state changes. > * > * In state PHY_HALTED the PHY gets suspended, so rescheduling the > * state machine would be pointless and possibly error prone when > * called from phy_disconnect() synchronously. > */ > - if (phy_polling_mode(phydev) && phy_is_started(phydev)) > - phy_queue_state_machine(phydev, > - phy_get_next_update_time(phydev)); > + if (phy_polling_mode(phydev) && phy_is_started(phydev)) { > + unsigned int next_time = phy_get_next_update_time(phydev); > + > + /* Drivers returning PHY_STATE_IRQ opt out of polling. > + * Use IRQ-only mode by not re-queuing the state machine. > + */ > + if (next_time != PHY_STATE_IRQ) > + phy_queue_state_machine(phydev, next_time); > + } How does this interact with update_stats()? phy_polling_mode() returns true because the update_stats() op is implemented. phy_get_next_update_time() returns PHY_STATE_IRQ, because the PHY is in a state where interrupts works, and then the statistics overflow. It seems like this code needs to be somehow made part of phy_polling_mode(), so that it has the full picture of why polling is being used. Andrew --- pw-bot: cr
On Wed, Jul 09, 2025 at 04:10:48PM +0200, Andrew Lunn wrote: > > /* Only re-schedule a PHY state machine change if we are polling the > > - * PHY, if PHY_MAC_INTERRUPT is set, then we will be moving > > - * between states from phy_mac_interrupt(). > > + * PHY. If PHY_MAC_INTERRUPT is set or get_next_update_time() returns > > + * PHY_STATE_IRQ, then we rely on interrupts for state changes. > > * > > * In state PHY_HALTED the PHY gets suspended, so rescheduling the > > * state machine would be pointless and possibly error prone when > > * called from phy_disconnect() synchronously. > > */ > > - if (phy_polling_mode(phydev) && phy_is_started(phydev)) > > - phy_queue_state_machine(phydev, > > - phy_get_next_update_time(phydev)); > > + if (phy_polling_mode(phydev) && phy_is_started(phydev)) { > > + unsigned int next_time = phy_get_next_update_time(phydev); > > + > > + /* Drivers returning PHY_STATE_IRQ opt out of polling. > > + * Use IRQ-only mode by not re-queuing the state machine. > > + */ > > + if (next_time != PHY_STATE_IRQ) > > + phy_queue_state_machine(phydev, next_time); > > + } > > How does this interact with update_stats()? > > phy_polling_mode() returns true because the update_stats() op is > implemented. phy_get_next_update_time() returns PHY_STATE_IRQ, because > the PHY is in a state where interrupts works, and then the statistics > overflow. > > It seems like this code needs to be somehow made part of > phy_polling_mode(), so that it has the full picture of why polling is > being used. Ah, good point! I forgot about it. -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
© 2016 - 2025 Red Hat, Inc.