* [PATCH net-next] net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable
@ 2026-11-17 18:25 Coia Prant
2026-11-19 8:37 ` Maxime Chevallier
2026-11-19 12:22 ` Heiko Stübner
0 siblings, 2 replies; 3+ messages in thread
From: Coia Prant @ 2026-11-17 18:25 UTC (permalink / raw)
To: Jakub Kicinski, Paolo Abeni
Cc: Andrew Lunn, Heiko Stuebner, netdev, linux-rockchip,
linux-arm-kernel, linux-kernel, 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.
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
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH net-next] net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable
2026-11-17 18:25 [PATCH net-next] net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable Coia Prant
@ 2026-11-19 8:37 ` Maxime Chevallier
2026-11-19 12:22 ` Heiko Stübner
1 sibling, 0 replies; 3+ messages in thread
From: Maxime Chevallier @ 2026-11-19 8:37 UTC (permalink / raw)
To: Coia Prant, Jakub Kicinski, Paolo Abeni
Cc: Andrew Lunn, Heiko Stuebner, netdev, linux-rockchip,
linux-arm-kernel, linux-kernel
Hi,
I missed that one, please make sure to run get_maintainers :)
On 11/17/26 19:25, Coia Prant 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.
>
> 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>
Reviewed-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Maxime
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH net-next] net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable
2026-11-17 18:25 [PATCH net-next] net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable Coia Prant
2026-11-19 8:37 ` Maxime Chevallier
@ 2026-11-19 12:22 ` Heiko Stübner
1 sibling, 0 replies; 3+ messages in thread
From: Heiko Stübner @ 2026-11-19 12:22 UTC (permalink / raw)
To: Jakub Kicinski, Paolo Abeni, Coia Prant
Cc: Andrew Lunn, netdev, linux-rockchip, linux-arm-kernel,
linux-kernel, Coia Prant
Am Dienstag, 17. November 2026, 19:25:03 Mitteleuropäische Normalzeit schrieb 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.
>
> 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>
Reviewed-by: Heiko Stuebner <heiko@sntech.de>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-19 12:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 18:25 [PATCH net-next] net: stmmac: dwmac-rk: split gmac_clk_enable into enable/disable Coia Prant
2026-11-19 8:37 ` Maxime Chevallier
2026-11-19 12:22 ` Heiko Stübner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox