Skip to contentSkip to conversation

net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable

PATCH Applied Coia Prant 4 participants 4 messages Nov 17 – Nov 20

Subsystems (5)
lore
Share & tools
lore

Coia Prant

author
#0 lore raw

PATCH 1/1 net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable

As the great Linus Torvalds once said in
Documentation/process/coding-style.rst:

"If you need more than 3 levels of indentation, you're screwed anyway,
and should fix your program."

The gmac_clk_enable() function previously used a 'bool enable' parameter to handle both clock enabling and disabling. This resulted in deeply nested if/else blocks, which made the error handling and rollback paths cumbersome and prone to bugs.

This complexity has led to bugs, such as the bulk clock leak fixed by
commit 8db67bb6a1ff ("net: ethernet: stmmac: dwmac-rk: fix bulk clock leak when the PHY clock fails").

Split the function into two distinct helpers: gmac_clk_enable() and gmac_clk_disable(). This flattens the code, removes the boolean flag, makes the caller's intent much clearer, and simplifies the error unwinding logic.

Additionally, this refactoring aids compiler inlining and CPU branch prediction (trust me). The reason is simple: no branch, no prediction. While the compiler might optimize it, there's no guarantee it will :P

---
 .../net/ethernet/stmicro/stmmac/dwmac-rk.c    | 67 ++++++++++---------
 1 file changed, 36 insertions(+), 31 deletions(-)
drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c +36 -31
index a37df5767d84..78bc8c71ad10 100644
@@ -1178,43 +1178,48 @@ static int rk_gmac_clk_init(struct plat_stmmacenet_data *plat)
 	return 0;
 }
 
-static int gmac_clk_enable(struct rk_priv_data *bsp_priv, bool enable)
+static int gmac_clk_enable(struct rk_priv_data *bsp_priv)
 {
 	int ret;
 
-	if (enable) {
-		if (!bsp_priv->clk_enabled) {
-			ret = clk_bulk_prepare_enable(bsp_priv->num_clks,
-						      bsp_priv->clks);
-			if (ret)
-				return ret;
-
-			ret = clk_prepare_enable(bsp_priv->clk_phy);
-			if (ret) {
-				clk_bulk_disable_unprepare(bsp_priv->num_clks,
-							   bsp_priv->clks);
-				return ret;
-			}
+	if (bsp_priv->clk_enabled)
+		return 0;
 
-			rk_configure_io_clksel(bsp_priv);
-			rk_ungate_rmii_clock(bsp_priv);
+	ret = clk_bulk_prepare_enable(bsp_priv->num_clks,
+				      bsp_priv->clks);
+	if (ret)
+		return ret;
 
-			mdelay(5);
-			bsp_priv->clk_enabled = true;
-		}
-	} else {
-		if (bsp_priv->clk_enabled) {
-			rk_gate_rmii_clock(bsp_priv);
+	ret = clk_prepare_enable(bsp_priv->clk_phy);
+	if (ret)
+		goto err_clk_phy;
 
-			clk_bulk_disable_unprepare(bsp_priv->num_clks,
-						   bsp_priv->clks);
-			clk_disable_unprepare(bsp_priv->clk_phy);
+	rk_configure_io_clksel(bsp_priv);
+	rk_ungate_rmii_clock(bsp_priv);
 
-			bsp_priv->clk_enabled = false;
-		}
-	}
+	mdelay(5);
+	bsp_priv->clk_enabled = true;
 
 	return 0;
+
+err_clk_phy:
+	clk_bulk_disable_unprepare(bsp_priv->num_clks,
+				   bsp_priv->clks);
+	return ret;
+}
+
+static void gmac_clk_disable(struct rk_priv_data *bsp_priv)
+{
+	if (!bsp_priv->clk_enabled)
+		return;
+
+	rk_gate_rmii_clock(bsp_priv);
+
+	clk_bulk_disable_unprepare(bsp_priv->num_clks,
+				   bsp_priv->clks);
+	clk_disable_unprepare(bsp_priv->clk_phy);
+
+	bsp_priv->clk_enabled = false;
 }
 
 static int rk_phy_powerup(struct rk_priv_data *bsp_priv)
@@ -1453,7 +1458,7 @@ static int rk_gmac_powerup(struct rk_priv_data *bsp_priv)
 	if (ret)
 		return ret;
 
-	ret = gmac_clk_enable(bsp_priv, true);
+	ret = gmac_clk_enable(bsp_priv);
 	if (ret)
 		return ret;
 
@@ -1544,7 +1549,7 @@ static int rk_gmac_powerup(struct rk_priv_data *bsp_priv)
 	return 0;
 
 clk_disable:
-	gmac_clk_enable(bsp_priv, false);
+	gmac_clk_disable(bsp_priv);
 	return ret;
 }
 
@@ -1556,7 +1561,7 @@ static void rk_gmac_powerdown(struct rk_priv_data *gmac)
 	pm_runtime_put_sync(gmac->dev);
 
 	rk_phy_powerdown(gmac);
-	gmac_clk_enable(gmac, false);
+	gmac_clk_disable(gmac);
 }
 
 static void rk_get_interfaces(struct stmmac_priv *priv, void *bsp_priv,
Signature · 1 line
2.47.3
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi,

I missed that one, please make sure to run get_maintainers :)

Quoted 19 lines · Coia Prant

As the great Linus Torvalds once said in
Documentation/process/coding-style.rst:

"If you need more than 3 levels of indentation, you're screwed anyway,
and should fix your program."

The gmac_clk_enable() function previously used a 'bool enable' parameter to handle both clock enabling and disabling. This resulted in deeply nested if/else blocks, which made the error handling and rollback paths cumbersome and prone to bugs.

This complexity has led to bugs, such as the bulk clock leak fixed by
commit 8db67bb6a1ff ("net: ethernet: stmmac: dwmac-rk: fix bulk clock leak when the PHY clock fails").

Split the function into two distinct helpers: gmac_clk_enable() and gmac_clk_disable(). This flattens the code, removes the boolean flag, makes the caller's intent much clearer, and simplifies the error unwinding logic.

Quoted 5 lines

Additionally, this refactoring aids compiler inlining and CPU branch prediction (trust me). The reason is simple: no branch, no prediction. While the compiler might optimize it, there's no guarantee it will :P

Maxime

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Coia Prant

Am Dienstag, 17. November 2026, 19:25:03 Mitteleuropäische Normalzeit schrieb Coia Prant:

Quoted 19 lines

As the great Linus Torvalds once said in
Documentation/process/coding-style.rst:

"If you need more than 3 levels of indentation, you're screwed anyway,
and should fix your program."

The gmac_clk_enable() function previously used a 'bool enable' parameter to handle both clock enabling and disabling. This resulted in deeply nested if/else blocks, which made the error handling and rollback paths cumbersome and prone to bugs.

This complexity has led to bugs, such as the bulk clock leak fixed by
commit 8db67bb6a1ff ("net: ethernet: stmmac: dwmac-rk: fix bulk clock leak when the PHY clock fails").

Split the function into two distinct helpers: gmac_clk_enable() and gmac_clk_disable(). This flattens the code, removes the boolean flag, makes the caller's intent much clearer, and simplifies the error unwinding logic.

Quoted 5 lines

Additionally, this refactoring aids compiler inlining and CPU branch prediction (trust me). The reason is simple: no branch, no prediction. While the compiler might optimize it, there's no guarantee it will :P

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

#3 lore raw
In reply to Coia Prant
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 6 lines · On Wed, 18 Nov 2026 02:25:03 +0800 you wrote:

As the great Linus Torvalds once said in
Documentation/process/coding-style.rst:

"If you need more than 3 levels of indentation, you're screwed anyway,
and should fix your program."

Quoted 6 lines

The gmac_clk_enable() function previously used a 'bool enable' parameter to handle both clock enabling and disabling. This resulted in deeply nested if/else blocks, which made the error handling and rollback paths cumbersome and prone to bugs.

[...]

Here is the summary with links:
- [net-next] net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable

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

You are awesome, thank you!

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