Skip to contentSkip to conversation

net: phy: mxl-gpy: enhance delay time required by loopback disable function

PATCH Applied Xu Liang 2 participants 2 messages Mar 14, 2023 – Mar 16, 2023

Subsystems (4)
lore
Share & tools
lore

Xu Liang

author maintainer
#0 lore raw

PATCH v5 1/1 net: phy: mxl-gpy: enhance delay time required by loopback disable function

GPY2xx devices need 3 seconds to fully switch out of loopback mode before it can safely re-enter loopback mode. Implement timeout mechanism to guarantee 3 seconds waited before re-enter loopback mode.

---
v2 changes:
Update comments.

v3 changes:
Submit for net-next.

v4 changes:
Implement timeout mechanism as re-enter loopback mode is quite rare use case. The delay is not required to resume normal function.

v5 changes:
Fix race condition introduced in v4.
Remove the bool variable lb_dis_chk added in v4.

 drivers/net/phy/mxl-gpy.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)
drivers/net/phy/mxl-gpy.c +29 -6
index e5972b4ef6e8..8e6bb97b5f85 100644
@@ -107,6 +107,13 @@ struct gpy_priv {
 
 	u8 fw_major;
 	u8 fw_minor;
+
+	/* It takes 3 seconds to fully switch out of loopback mode before
+	 * it can safely re-enter loopback mode. Record the time when
+	 * loopback is disabled. Check and wait if necessary before loopback
+	 * is enabled.
+	 */
+	u64 lb_dis_to;
 };
 
 static const struct {
@@ -769,18 +776,34 @@ static void gpy_get_wol(struct phy_device *phydev,
 
 static int gpy_loopback(struct phy_device *phydev, bool enable)
 {
+	struct gpy_priv *priv = phydev->priv;
+	u16 set = 0;
 	int ret;
 
-	ret = phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
-			 enable ? BMCR_LOOPBACK : 0);
-	if (!ret) {
-		/* It takes some time for PHY device to switch
-		 * into/out-of loopback mode.
+	if (enable) {
+		u64 now = get_jiffies_64();
+
+		/* wait until 3 seconds from last disable */
+		if (time_before64(now, priv->lb_dis_to))
+			msleep(jiffies64_to_msecs(priv->lb_dis_to - now));
+
+		set = BMCR_LOOPBACK;
+	}
+
+	ret = phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, set);
+	if (ret <= 0)
+		return ret;
+
+	if (enable) {
+		/* It takes some time for PHY device to switch into
+		 * loopback mode.
 		 */
 		msleep(100);
+	} else {
+		priv->lb_dis_to = get_jiffies_64() + HZ * 3;
 	}
 
-	return ret;
+	return 0;
 }
 
 static int gpy115_loopback(struct phy_device *phydev, bool enable)
Signature · 1 line
2.17.1
#1 lore raw
This patch was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Quoted 10 lines · On Wed, 15 Mar 2023 00:30:23 +0800 you wrote:

GPY2xx devices need 3 seconds to fully switch out of loopback mode before it can safely re-enter loopback mode. Implement timeout mechanism to guarantee 3 seconds waited before re-enter loopback mode.

---
v2 changes:
Update comments.

[...]

Here is the summary with links:
- [net-next,v5] net: phy: mxl-gpy: enhance delay time required by loopback disable function

    https://git.kernel.org/netdev/net-next/c/0ba13995be9b

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html