Skip to contentSkip to conversation

net: phylink: add a function to resume PHY alone to fix resume issue with WoL enabled

PATCH In review Clark Wang 2 participants 3 messages Feb 22, 2023

Subsystems (6)
lore
Share & tools
lore

Clark Wang

author
#0 lore raw

PATCH v4 1/2 net: phylink: add a function to resume PHY alone to fix resume issue with WoL enabled

Issue we met:
On some platforms, MAC cannot work after resumed from the suspend with WoL enabled.

The cause of the issue:
1. phylink_resolve() is in a workqueue which will not be executed immediately. This is the call sequence:

       phylink_resolve()->phylink_link_up()->pl->mac_ops->mac_link_up()

For stmmac driver, mac_link_up() will set the correct speed/duplex... values which are from link_state.
2. In stmmac_resume(), it will call stmmac_hw_setup() after called the phylink_resume(), because MAC need PHY rx_clk to do the reset. stmmac_core_init() is called in function stmmac_hw_setup(), which will reset the MAC and set the speed/duplex... to default value.
Conclusion: Because phylink_resolve() cannot determine when it is called, it
cannot be guaranteed to be called after stmmac_core_init(). Once stmmac_core_init() is called after phylink_resolve(), the MAC will be misconfigured and cannot be used.

In order to avoid this problem, add a function called phylink_phy_resume() to resume PHY separately. This eliminates the need to call phylink_resume() before stmmac_hw_setup().

Add another judgement before called phy_start() in phylink_start(). This way phy_start() will not be called multiple times when resumes. At the same time, it may not affect other drivers that do not use phylink_phy_resume().

---
V2 change:
- add mac_resume_phy_separately flag to struct phylink to mark if the MAC driver uses the phylink_phy_resume() first.
V3 change:
- add brace to avoid ambiguous 'else'

V4:
Many thanks to Jakub and Russel for their suggestions, here are the changes for V4.
- Unify MAC and PHY in comments and subject to uppercase.
- Add subject of the sentence.
- Move && to the end of the line
- Add notice in the comment of function phylink_phy_resume()

---
 drivers/net/phy/phylink.c | 35 +++++++++++++++++++++++++++++++++--
 include/linux/phylink.h   |  1 +
 2 files changed, 34 insertions(+), 2 deletions(-)
drivers/net/phy/phylink.c +33 -2
index ea8fcce5b2d9..0be57e9463d9 100644
@@ -80,6 +80,8 @@ struct phylink {
 	DECLARE_PHY_INTERFACE_MASK(sfp_interfaces);
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
 	u8 sfp_port;
+
+	bool mac_resume_phy_separately;
 };
 
 #define phylink_printk(level, pl, fmt, ...) \
@@ -1509,6 +1511,7 @@ struct phylink *phylink_create(struct phylink_config *config,
 		return ERR_PTR(-EINVAL);
 	}
 
+	pl->mac_resume_phy_separately = false;
 	pl->using_mac_select_pcs = using_mac_select_pcs;
 	pl->phy_state.interface = iface;
 	pl->link_interface = iface;
@@ -1942,8 +1945,12 @@ void phylink_start(struct phylink *pl)
 	}
 	if (poll)
 		mod_timer(&pl->link_poll, jiffies + HZ);
-	if (pl->phydev)
-		phy_start(pl->phydev);
+	if (pl->phydev) {
+		if (!pl->mac_resume_phy_separately)
+			phy_start(pl->phydev);
+		else
+			pl->mac_resume_phy_separately = false;
+	}
 	if (pl->sfp_bus)
 		sfp_upstream_start(pl->sfp_bus);
 }
@@ -2023,6 +2030,30 @@ void phylink_suspend(struct phylink *pl, bool mac_wol)
 }
 EXPORT_SYMBOL_GPL(phylink_suspend);
 
+/**
+ * phylink_phy_resume() - resume PHY alone
+ * @pl: a pointer to a &struct phylink returned from phylink_create()
+ *
+ * In the MAC driver using phylink, if the MAC needs the clock of the PHY
+ * when it resumes, it can call this function to resume the PHY separately.
+ * Then proceed to MAC resume operations.
+ * 
+ * Note: This function MUST ONLY be called before calling phylink_start()
+ *       in the MAC resume function.
+ */
+void phylink_phy_resume(struct phylink *pl)
+{
+	ASSERT_RTNL();
+
+	if (!test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state) &&
+	    pl->phydev) {
+		phy_start(pl->phydev);
+		pl->mac_resume_phy_separately = true;
+	}
+
+}
+EXPORT_SYMBOL_GPL(phylink_phy_resume);
+
 /**
  * phylink_resume() - handle a network device resume event
  * @pl: a pointer to a &struct phylink returned from phylink_create()
include/linux/phylink.h +1 -0
index c492c26202b5..6edfab5f754c 100644
@@ -589,6 +589,7 @@ void phylink_stop(struct phylink *);
 
 void phylink_suspend(struct phylink *pl, bool mac_wol);
 void phylink_resume(struct phylink *pl);
+void phylink_phy_resume(struct phylink *pl);
 
 void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *);
 int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *);
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v4 2/2 net: stmmac: resume PHY separately before calling stmmac_hw_setup()

On some platforms, MAC cannot work after resumed from the suspend with WoL enabled.

We found the stmmac_hw_setup() when system resumes will called after the stmmac_mac_link_up(). So the correct values set in stmmac_mac_link_up() are overwritten by stmmac_core_init() in phylink_resume().

So call the new added function in phylink to resume PHY firstly. Then can call the stmmac_hw_setup() before calling phy_resume().

Fixes 90702dcd19c0 ("net: stmmac: fix MAC not working when system resume back with WoL active")

---
V2:
- add Fixes tag
V3: no change
V4:
- Unify MAC and PHY in comments and subject to uppercase.

---
 .../net/ethernet/stmicro/stmmac/stmmac_main.c    | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +7 -9
index e4902a7bb61e..75857b85921a 100644
@@ -7541,16 +7541,9 @@ int stmmac_resume(struct device *dev)
 	}
 
 	rtnl_lock();
-	if (device_may_wakeup(priv->device) && priv->plat->pmt) {
-		phylink_resume(priv->phylink);
-	} else {
-		phylink_resume(priv->phylink);
-		if (device_may_wakeup(priv->device))
-			phylink_speed_up(priv->phylink);
-	}
-	rtnl_unlock();
 
-	rtnl_lock();
+	phylink_phy_resume(priv->phylink);
+
 	mutex_lock(&priv->lock);
 
 	stmmac_reset_queues_param(priv);
@@ -7568,6 +7561,11 @@ int stmmac_resume(struct device *dev)
 	stmmac_enable_all_dma_irq(priv);
 
 	mutex_unlock(&priv->lock);
+
+	phylink_resume(priv->phylink);
+	if (device_may_wakeup(priv->device) && !priv->plat->pmt)
+		phylink_speed_up(priv->phylink);
+
 	rtnl_unlock();
 
 	netif_device_attach(ndev);
Signature · 1 line
2.34.1
#2 lore raw
In reply to Clark Wang

net-next is closed due to the merge window. Please resubmit after 6.3-rc1. Thanks.

Quoted 129 lines · Clark Wang

Issue we met:
On some platforms, MAC cannot work after resumed from the suspend with WoL enabled.

The cause of the issue:
1. phylink_resolve() is in a workqueue which will not be executed immediately. This is the call sequence:

       phylink_resolve()->phylink_link_up()->pl->mac_ops->mac_link_up()

For stmmac driver, mac_link_up() will set the correct speed/duplex... values which are from link_state.
2. In stmmac_resume(), it will call stmmac_hw_setup() after called the phylink_resume(), because MAC need PHY rx_clk to do the reset. stmmac_core_init() is called in function stmmac_hw_setup(), which will reset the MAC and set the speed/duplex... to default value.
Conclusion: Because phylink_resolve() cannot determine when it is called, it
cannot be guaranteed to be called after stmmac_core_init(). Once stmmac_core_init() is called after phylink_resolve(), the MAC will be misconfigured and cannot be used.

In order to avoid this problem, add a function called phylink_phy_resume() to resume PHY separately. This eliminates the need to call phylink_resume() before stmmac_hw_setup().

Add another judgement before called phy_start() in phylink_start(). This way phy_start() will not be called multiple times when resumes. At the same time, it may not affect other drivers that do not use phylink_phy_resume().

---
V2 change:
- add mac_resume_phy_separately flag to struct phylink to mark if the MAC driver uses the phylink_phy_resume() first.
V3 change:
- add brace to avoid ambiguous 'else'

V4:
Many thanks to Jakub and Russel for their suggestions, here are the changes for V4.
- Unify MAC and PHY in comments and subject to uppercase.
- Add subject of the sentence.
- Move && to the end of the line
- Add notice in the comment of function phylink_phy_resume()

---
 drivers/net/phy/phylink.c | 35 +++++++++++++++++++++++++++++++++--
 include/linux/phylink.h   |  1 +
 2 files changed, 34 insertions(+), 2 deletions(-)
drivers/net/phy/phylink.c +33 -2
index ea8fcce5b2d9..0be57e9463d9 100644
@@ -80,6 +80,8 @@ struct phylink {
 	DECLARE_PHY_INTERFACE_MASK(sfp_interfaces);
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(sfp_support);
 	u8 sfp_port;
+
+	bool mac_resume_phy_separately;
 };
 
 #define phylink_printk(level, pl, fmt, ...) \
@@ -1509,6 +1511,7 @@ struct phylink *phylink_create(struct phylink_config *config,
 		return ERR_PTR(-EINVAL);
 	}
 
+	pl->mac_resume_phy_separately = false;
 	pl->using_mac_select_pcs = using_mac_select_pcs;
 	pl->phy_state.interface = iface;
 	pl->link_interface = iface;
@@ -1942,8 +1945,12 @@ void phylink_start(struct phylink *pl)
 	}
 	if (poll)
 		mod_timer(&pl->link_poll, jiffies + HZ);
-	if (pl->phydev)
-		phy_start(pl->phydev);
+	if (pl->phydev) {
+		if (!pl->mac_resume_phy_separately)
+			phy_start(pl->phydev);
+		else
+			pl->mac_resume_phy_separately = false;
+	}
 	if (pl->sfp_bus)
 		sfp_upstream_start(pl->sfp_bus);
 }
@@ -2023,6 +2030,30 @@ void phylink_suspend(struct phylink *pl, bool mac_wol)
 }
 EXPORT_SYMBOL_GPL(phylink_suspend);
 
+/**
+ * phylink_phy_resume() - resume PHY alone
+ * @pl: a pointer to a &struct phylink returned from phylink_create()
+ *
+ * In the MAC driver using phylink, if the MAC needs the clock of the PHY
+ * when it resumes, it can call this function to resume the PHY separately.
+ * Then proceed to MAC resume operations.
+ * 
+ * Note: This function MUST ONLY be called before calling phylink_start()
+ *       in the MAC resume function.
+ */
+void phylink_phy_resume(struct phylink *pl)
+{
+	ASSERT_RTNL();
+
+	if (!test_bit(PHYLINK_DISABLE_MAC_WOL, &pl->phylink_disable_state) &&
+	    pl->phydev) {
+		phy_start(pl->phydev);
+		pl->mac_resume_phy_separately = true;
+	}
+
+}
+EXPORT_SYMBOL_GPL(phylink_phy_resume);
+
 /**
  * phylink_resume() - handle a network device resume event
  * @pl: a pointer to a &struct phylink returned from phylink_create()
include/linux/phylink.h +1 -0
index c492c26202b5..6edfab5f754c 100644
@@ -589,6 +589,7 @@ void phylink_stop(struct phylink *);
 
 void phylink_suspend(struct phylink *pl, bool mac_wol);
 void phylink_resume(struct phylink *pl);
+void phylink_phy_resume(struct phylink *pl);
 
 void phylink_ethtool_get_wol(struct phylink *, struct ethtool_wolinfo *);
 int phylink_ethtool_set_wol(struct phylink *, struct ethtool_wolinfo *);
Signature · 1 line
2.34.1
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!