Netdev List
 help / color / mirror / Atom feed
* [PATCH v5] net: dsa: mt7530: enable the regulators in probe() and unwind on error
@ 2026-11-25  2:22 Xiangwu Lai
  2026-11-29  3:08 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Xiangwu Lai @ 2026-11-25  2:22 UTC (permalink / raw)
  To: chester.a.unal
  Cc: daniel, andrew, olteanv, sean.wang, netdev, linux-kernel,
	netdev-bot+sashiko, Xiangwu Lai

mt7530_setup() enables the core and io regulators of the MT7530, but the
only matching regulator_disable() calls live in mt7530_remove(), which
never runs when the probe fails.  A failure anywhere between the enables
and a fully registered switch - the remaining steps of mt7530_setup(),
the IRQ chip or MDIO bus registration in mt753x_setup() (both can also
return -EPROBE_DEFER), the SGMII PCS creation, or the DSA core itself -
therefore leaves both regulators enabled.  The devm-acquired handles
are then released with a non-zero enable_count, which triggers the
WARN_ON() in _regulator_put(), and each probe deferral grows the count
further.

Move the enables to mt7530_probe(), right after the regulators are
acquired, and unwind them with plain error labels if a later step in
the probe fails.  mt7530_setup() and mt7530_remove() keep their original
shape, so the enables and disables stay balanced in every combination of
probe, setup and unbind: a failed probe always unwinds, and a switch
that stays bound is always disabled exactly once by mt7530_remove().

On the boards that hold the switch in reset with a GPIO
(mt7623n-bananapi-bpi-r2 and mt7623n-rfb-emmc, which wire the supplies
inline), the reset line has been asserted since it was acquired in
mt7530_probe() with GPIOD_OUT_LOW, so the rails come up while the chip
is held in reset.

On the MCM boards (mt7623a-rfb-emmc and mt7623a-rfb-nand, which get
mediatek,mcm and the supplies from mt7623a.dtsi) there is no reset
GPIO; the reset control handle is only acquired in mt7530_probe() and
the first reset pulse still happens at the top of mt7530_setup().  With
the enables moved to probe, the chip therefore runs on its power-on
defaults during the window between the probe and that pulse.  The
window spans dsa_register_switch() up to the switch setup, it repeats
on every -EPROBE_DEFER retry, and it is unbounded while a multi-switch
tree waits for its other members.  The pulse at the start of
mt7530_setup() re-initializes the chip, so the window behaves like a
cold boot whose external reset arrives late; no configuration is lost.

All of these boards connect the supplies to switchable MT6323 PMIC
outputs, so a failed probe would otherwise leave them on and trip the
WARN_ON(enable_count) splat when the devm handles are released.  MT7621
does not have these supplies and is skipped by the priv->id ==
ID_MT7530 guards, and the MT7988 MMIO variant has no regulators at all.

Found by code inspection; the issue has not been triggered at runtime
and no MT7530 hardware was available for testing.

Fixes: b8f126a8d543 ("net-next: dsa: add dsa support for Mediatek MT7530 switch")
Suggested-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Xiangwu Lai <laixiangwu@cmss.chinamobile.com>
---
Changes in v5:

- Rewrite the commit message safety argument.  The previous version
  claimed the reset line is always asserted since probe, which is only
  true for the boards with a reset GPIO.  On the MCM boards the reset
  control is only acquired in probe and the first reset pulse still
  happens at the start of mt7530_setup(), so the chip runs on its
  power-on defaults between probe and setup; this window is now
  described as it is instead of being masked by a wrong claim.
- Fix the list of affected boards: mt7623n-bananapi-bpi-r2 and
  mt7623n-rfb-emmc include mt7623n.dtsi and wire the supplies and the
  reset GPIO inline; the boards that get mediatek,mcm and the supplies
  from mt7623a.dtsi are mt7623a-rfb-emmc and mt7623a-rfb-nand.

No code change; the diff is identical to v4.

Link: https://lore.kernel.org/r/20261124013422.1408-1-laixiangwu@cmss.chinamobile.com

 drivers/net/dsa/mt7530-mdio.c | 38 ++++++++++++++++++++++++++++++++---
 drivers/net/dsa/mt7530.c      | 18 -----------------
 2 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/drivers/net/dsa/mt7530-mdio.c b/drivers/net/dsa/mt7530-mdio.c
index 0be107dc6..b1daefc90 100644
--- a/drivers/net/dsa/mt7530-mdio.c
+++ b/drivers/net/dsa/mt7530-mdio.c
@@ -210,18 +210,50 @@ mt7530_probe(struct mdio_device *mdiodev)
 		priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
 		if (IS_ERR(priv->io_pwr))
 			return PTR_ERR(priv->io_pwr);
+
+		regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
+		ret = regulator_enable(priv->core_pwr);
+		if (ret < 0) {
+			dev_err(&mdiodev->dev,
+				"Failed to enable core power: %d\n", ret);
+			return ret;
+		}
+
+		regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
+		ret = regulator_enable(priv->io_pwr);
+		if (ret < 0) {
+			dev_err(&mdiodev->dev,
+				"Failed to enable io pwr: %d\n", ret);
+			goto err_disable_core_pwr;
+		}
 	}
 
 	rc.lock_arg = &priv->bus->mdio_lock;
 	priv->regmap = devm_regmap_init(priv->dev, &mt7530_regmap_bus,
 					priv, &rc);
-	if (IS_ERR(priv->regmap))
-		return PTR_ERR(priv->regmap);
+	if (IS_ERR(priv->regmap)) {
+		ret = PTR_ERR(priv->regmap);
+		goto err_disable_powers;
+	}
 
 	if (priv->id == ID_MT7531)
 		priv->create_sgmii = mt7531_create_sgmii;
 
-	return dsa_register_switch(priv->ds);
+	ret = dsa_register_switch(priv->ds);
+	if (ret)
+		goto err_disable_powers;
+
+	return 0;
+
+err_disable_powers:
+	if (priv->id == ID_MT7530)
+		regulator_disable(priv->io_pwr);
+
+err_disable_core_pwr:
+	if (priv->id == ID_MT7530)
+		regulator_disable(priv->core_pwr);
+
+	return ret;
 }
 
 static void
diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index 7781a63b4..b669a94d4 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -2411,24 +2411,6 @@ mt7530_setup(struct dsa_switch *ds)
 	ds->ageing_time_min = 2 * 1000;
 	ds->ageing_time_max = (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1) * 1000;
 
-	if (priv->id == ID_MT7530) {
-		regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
-		ret = regulator_enable(priv->core_pwr);
-		if (ret < 0) {
-			dev_err(priv->dev,
-				"Failed to enable core power: %d\n", ret);
-			return ret;
-		}
-
-		regulator_set_voltage(priv->io_pwr, 3300000, 3300000);
-		ret = regulator_enable(priv->io_pwr);
-		if (ret < 0) {
-			dev_err(priv->dev, "Failed to enable io pwr: %d\n",
-				ret);
-			return ret;
-		}
-	}
-
 	/* Reset whole chip through gpio pin or memory-mapped registers for
 	 * different type of hardware
 	 */
-- 
2.50.1.windows.1




^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v5] net: dsa: mt7530: enable the regulators in probe() and unwind on error
  2026-11-25  2:22 [PATCH v5] net: dsa: mt7530: enable the regulators in probe() and unwind on error Xiangwu Lai
@ 2026-11-29  3:08 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-29  3:08 UTC (permalink / raw)
  To: laixiangwu
  Cc: chester.a.unal, daniel, andrew, olteanv, sean.wang, netdev,
	linux-kernel, netdev-bot+sashiko, kuba

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 (1) · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] On the MCM boards (mt7623a.dtsi sets mediatek,mcm and resets =
  <&ethsys MT2701_ETHSYS_MCM_RST>), mt7530_probe() now enables the core…
- [Low] The commit message lists 'the SGMII PCS creation' among the
  failures that leave the MT7530 core/io regulators enabled.

Pre-existing issues:
- [Medium] mt7530_remove() calls regulator_disable() on core_pwr and
  io_pwr (mt7530-mdio.c:267-277) before mt7530_remove_common() calls…

--

net: dsa: mt7530: enable the regulators in probe() and unwind on error

This moves the MT7530 core and io regulator enables out of mt7530_setup()
and into mt7530_probe(). It also adds error labels that disable them again
if regmap init or dsa_register_switch() fails, so a failed or deferred
probe no longer trips the WARN_ON() in _regulator_put().

Link: https://lore.kernel.org/r/20261124013422.1408-1-laixiangwu@cmss.chinamobile.com

> never runs when the probe fails.  A failure anywhere between the enables
> and a fully registered switch - the remaining steps of mt7530_setup(),
> the IRQ chip or MDIO bus registration in mt753x_setup() (both can also
> return -EPROBE_DEFER), the SGMII PCS creation, or the DSA core itself -
> therefore leaves both regulators enabled.

[Severity: Low]
Can the SGMII PCS creation leave these regulators enabled? The core and io
regulators only exist for ID_MT7530. mt7530_probe() sets create_sgmii only
for ID_MT7531:

	if (priv->id == ID_MT7531)
		priv->create_sgmii = mt7531_create_sgmii;

mt753x_setup() only calls it when the pointer is set:

	if (priv->create_sgmii)
		ret = priv->create_sgmii(priv);

So on MT7530 this step never runs. Should "the SGMII PCS creation" be
removed from the list?

> diff --git a/drivers/net/dsa/mt7530-mdio.c b/drivers/net/dsa/mt7530-mdio.c
> index e85ef2aceee56..2de5e26edad61 100644
> --- a/drivers/net/dsa/mt7530-mdio.c
> +++ b/drivers/net/dsa/mt7530-mdio.c
> @@ -209,18 +209,50 @@ mt7530_probe(struct mdio_device *mdiodev)
>  		priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io");
>  		if (IS_ERR(priv->io_pwr))
>  			return PTR_ERR(priv->io_pwr);
> +
> +		regulator_set_voltage(priv->core_pwr, 1000000, 1000000);
> +		ret = regulator_enable(priv->core_pwr);

[Severity: Medium]
On the MCM boards, is the switch held in reset when the rails come up
here? mt7530_probe() gets the reset control but does not assert it:

	priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm");

The first reset pulse still happens in mt7530_setup():

	if (priv->mcm) {
		reset_control_assert(priv->rstc);
		usleep_range(5000, 5100);
		reset_control_deassert(priv->rstc);

Before this patch, the core supply on these boards was first enabled in
mt7530_setup(), right before that pulse. That supply is mt6323_vpa_reg,
which has neither regulator-boot-on nor regulator-always-on.

Now the chip runs on its power-on defaults from probe until ops->setup.
That window includes dsa_register_switch() deferrals and any wait for the
conduit or other tree members. Each -EPROBE_DEFER retry also power-cycles
the chip.

The commit message says:

  The pulse at the start of mt7530_setup() re-initializes the chip, so the
  window behaves like a cold boot whose external reset arrives late; no
  configuration is lost.

mt7530_setup() clears PCR_MATRIX on every port ("Disable forwarding by
default on all ports") and forces the PMCR links down. That suggests the
power-on default is to forward between front-panel ports.

Could the unconfigured switch forward frames between, for example, the
LAN and WAN ports during this window?

The GPIO reset boards avoid this because devm_gpiod_get_optional() with
GPIOD_OUT_LOW keeps the reset line asserted. Would calling
reset_control_assert(priv->rstc) in mt7530_probe() before the regulator
enables close the window on the MCM boards as well?

> +		if (ret < 0) {
> +			dev_err(&mdiodev->dev,
> +				"Failed to enable core power: %d\n", ret);
> +			return ret;
> +		}

[ ... ]

> -	return dsa_register_switch(priv->ds);
> +	ret = dsa_register_switch(priv->ds);
> +	if (ret)
> +		goto err_disable_powers;
> +
> +	return 0;
> +
> +err_disable_powers:
> +	if (priv->id == ID_MT7530)
> +		regulator_disable(priv->io_pwr);
> +
> +err_disable_core_pwr:
> +	if (priv->id == ID_MT7530)
> +		regulator_disable(priv->core_pwr);
> +
> +	return ret;
>  }

[Severity: Medium]
This is a pre-existing issue, not one introduced by this patch. Since the
patch now relies on mt7530_remove() to balance these enables, is the order
of operations there correct? mt7530_remove() turns off both regulators
before the switch is unregistered:

mt7530_remove() {
	...
	if (priv->id == ID_MT7530) {
		ret = regulator_disable(priv->core_pwr);
		...
		ret = regulator_disable(priv->io_pwr);
		...
	}

	mt7530_remove_common(priv);
	...
}

mt7530_remove_common() then calls dsa_unregister_switch(). So the whole
DSA teardown runs against an unpowered chip:

  - phylink mac_link_down writes PMCR
  - port_disable writes PCR
  - the PHYs are stopped over MDIO
  - stats_work keeps polling the MIB counters

stats_work only stops when mt753x_teardown() cancels it:

	if (priv->bus)
		cancel_delayed_work_sync(&priv->stats_work);

Would these writes be lost, and would the reads return bus-idle values
like 0xffff or 0xffffffff? Reset is not reasserted before power is removed
either.

Should mt7530_remove() call mt7530_remove_common() before it disables the
regulators?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125022231.1936-1-laixiangwu%40cmss.chinamobile.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-29  3:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  2:22 [PATCH v5] net: dsa: mt7530: enable the regulators in probe() and unwind on error Xiangwu Lai
2026-11-29  3:08 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox