Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by Coia Prant
Tue, 17 Nov 2026 18:25
[PATCH net-next] net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable
Author: Coia Prant
Date: Tue, 17 Nov 2026 18:25
Date: Tue, 17 Nov 2026 18:25
139 lines
14736 bytes
14736 bytes
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
Signed-off-by: Coia Prant <coiaprant@gmail.com>
---
.../net/ethernet/stmicro/stmmac/dwmac-rk.c | 67 ++++++++++---------
1 file changed, 36 insertions(+), 31 deletions(-)
diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
index a37df5767d84..78bc8c71ad10 100644
--- a/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
+++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
@@ -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,
--
2.47.3
Re: [PATCH net-next] net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable
Author: patchwork-bot+netdevbpf
Date: Fri, 20 Nov 2026 00:30
Date: Fri, 20 Nov 2026 00:30
28 lines
8805 bytes
8805 bytes
Hello:
This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:
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."
>
> 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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads