mt7530_setup() enables the core and io regulators, so they should be disabled by the matching teardown callback, not by the driver remove callback. teardown is the counterpart of setup and runs for every switch that was successfully set up, whereas remove() only runs when the whole driver is unbound. Move the regulator disable from mt7530_remove() to mt753x_teardown(), after the stats polling work is cancelled so that no poll can run against an unpowered switch. This is a plain code movement, with the error reporting kept for now and cleaned up in a later patch. A switch in a multi-switch tree whose sibling fails to set up is torn down without its driver reaching remove(), so its regulators were left enabled until now. Fixes: b8f126a8d543 ("net-next: dsa: add dsa support for Mediatek MT7530 switch") Signed-off-by: Haoran Wang --- drivers/net/dsa/mt7530-mdio.c | 14 +------------- drivers/net/dsa/mt7530.c | 13 +++++++++++++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/drivers/net/dsa/mt7530-mdio.c b/drivers/net/dsa/mt7530-mdio.c index e85ef2aceee5..faefbcad293f 100644 --- a/drivers/net/dsa/mt7530-mdio.c +++ b/drivers/net/dsa/mt7530-mdio.c @@ -227,23 +227,11 @@ static void mt7530_remove(struct mdio_device *mdiodev) { struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev); - int ret = 0, i; + int i; if (!priv) return; - if (priv->id == ID_MT7530) { - ret = regulator_disable(priv->core_pwr); - if (ret < 0) - dev_err(priv->dev, - "Failed to disable core power: %d\n", ret); - - ret = regulator_disable(priv->io_pwr); - if (ret < 0) - dev_err(priv->dev, "Failed to disable io pwr: %d\n", - ret); - } - mt7530_remove_common(priv); for (i = 0; i < 2; ++i) diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c index d5a199293f78..c2f12ae2c6d7 100644 --- a/drivers/net/dsa/mt7530.c +++ b/drivers/net/dsa/mt7530.c @@ -3237,9 +3237,22 @@ static void mt753x_teardown(struct dsa_switch *ds) { struct mt7530_priv *priv = ds->priv; + int ret; if (priv->bus) cancel_delayed_work_sync(&priv->stats_work); + + if (priv->id == ID_MT7530) { + ret = regulator_disable(priv->core_pwr); + if (ret < 0) + dev_err(priv->dev, + "Failed to disable core power: %d\n", ret); + + ret = regulator_disable(priv->io_pwr); + if (ret < 0) + dev_err(priv->dev, "Failed to disable io pwr: %d\n", + ret); + } } static int mt753x_set_mac_eee(struct dsa_switch *ds, int port, Pull the regulator disable out of mt753x_teardown() into a helper so it can also be used on the setup error paths added in later patches. The helper stops reporting regulator_disable() failures. A failed disable on a teardown or error path is not actionable, and on the error paths the failure being handled is already reported, so the extra messages would only add noise. Signed-off-by: Haoran Wang --- drivers/net/dsa/mt7530.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c index c2f12ae2c6d7..eb55cc969f43 100644 --- a/drivers/net/dsa/mt7530.c +++ b/drivers/net/dsa/mt7530.c @@ -2376,6 +2376,16 @@ mt7530_setup_mdio(struct mt7530_priv *priv) return ret; } +static void +mt7530_disable_pwrs(struct mt7530_priv *priv) +{ + if (priv->id != ID_MT7530) + return; + + regulator_disable(priv->core_pwr); + regulator_disable(priv->io_pwr); +} + static int mt7530_setup(struct dsa_switch *ds) { @@ -3237,22 +3247,11 @@ static void mt753x_teardown(struct dsa_switch *ds) { struct mt7530_priv *priv = ds->priv; - int ret; if (priv->bus) cancel_delayed_work_sync(&priv->stats_work); - if (priv->id == ID_MT7530) { - ret = regulator_disable(priv->core_pwr); - if (ret < 0) - dev_err(priv->dev, - "Failed to disable core power: %d\n", ret); - - ret = regulator_disable(priv->io_pwr); - if (ret < 0) - dev_err(priv->dev, "Failed to disable io pwr: %d\n", - ret); - } + mt7530_disable_pwrs(priv); } static int mt753x_set_mac_eee(struct dsa_switch *ds, int port, mt7530_setup_gpio() publishes a GPIO controller that userspace and other drivers can use immediately, but it currently runs before the FDB flush, which is the last step of setup that can fail. Move the GPIO controller registration after the FDB flush so that the controller is only made visible once every fallible step, including the FDB flush, has succeeded. This becomes important in the next patch, where the failure paths disable the regulators and return an error, so the controller must not be registered across a failed setup. Signed-off-by: Haoran Wang --- drivers/net/dsa/mt7530.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c index eb55cc969f43..e208d942b577 100644 --- a/drivers/net/dsa/mt7530.c +++ b/drivers/net/dsa/mt7530.c @@ -2585,6 +2585,11 @@ mt7530_setup(struct dsa_switch *ds) } } + /* Flush the FDB table */ + ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL); + if (ret < 0) + return ret; + #ifdef CONFIG_GPIOLIB if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) { ret = mt7530_setup_gpio(priv); @@ -2593,11 +2598,6 @@ mt7530_setup(struct dsa_switch *ds) } #endif /* CONFIG_GPIOLIB */ - /* Flush the FDB table */ - ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL); - if (ret < 0) - return ret; - return 0; } Once the core and io regulators have been enabled, every later failure in mt7530_setup() returned without disabling them, leaving the rails enabled and making the regulator core warn when devres later puts an enabled regulator. Route those failure paths to a common error label that disables both regulators, and disable the already enabled core regulator if enabling the io regulator fails. Fixes: b8f126a8d543 ("net-next: dsa: add dsa support for Mediatek MT7530 switch") Signed-off-by: Haoran Wang --- drivers/net/dsa/mt7530.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c index e208d942b577..1ac4ac1a0566 100644 --- a/drivers/net/dsa/mt7530.c +++ b/drivers/net/dsa/mt7530.c @@ -2435,6 +2435,7 @@ mt7530_setup(struct dsa_switch *ds) if (ret < 0) { dev_err(priv->dev, "Failed to enable io pwr: %d\n", ret); + regulator_disable(priv->core_pwr); return ret; } } @@ -2457,20 +2458,22 @@ mt7530_setup(struct dsa_switch *ds) val != 0, 20, 1000000); if (ret < 0) { dev_err(priv->dev, "reset timeout\n"); - return ret; + goto err_disable_pwrs; } regmap_read(priv->regmap, MT7530_CREV, &id); id = FIELD_GET(CHIP_NAME_MASK, id); if (id != MT7530_ID) { dev_err(priv->dev, "chip %x can't be supported\n", id); - return -ENODEV; + ret = -ENODEV; + goto err_disable_pwrs; } if ((val & MT7530_XTAL_MASK) == MT7530_XTAL_20MHZ) { dev_err(priv->dev, "MT7530 with a 20MHz XTAL is not supported!\n"); - return -EINVAL; + ret = -EINVAL; + goto err_disable_pwrs; } /* Reset the switch through internal reset */ @@ -2537,7 +2540,7 @@ mt7530_setup(struct dsa_switch *ds) /* Setup VLAN ID 0 for VLAN-unaware bridges */ ret = mt7530_setup_vlan0(priv); if (ret) - return ret; + goto err_disable_pwrs; /* Check for PHY muxing on port 5 */ if (dsa_is_unused_port(ds, 5)) { @@ -2564,7 +2567,7 @@ mt7530_setup(struct dsa_switch *ds) if (ret && ret != -ENODEV) { of_node_put(mac_np); of_node_put(phy_node); - return ret; + goto err_disable_pwrs; } id = of_mdio_parse_addr(ds->dev, phy_node); if (id == 0) @@ -2588,17 +2591,22 @@ mt7530_setup(struct dsa_switch *ds) /* Flush the FDB table */ ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL); if (ret < 0) - return ret; + goto err_disable_pwrs; #ifdef CONFIG_GPIOLIB if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) { ret = mt7530_setup_gpio(priv); if (ret) - return ret; + goto err_disable_pwrs; } #endif /* CONFIG_GPIOLIB */ return 0; + +err_disable_pwrs: + mt7530_disable_pwrs(priv); + + return ret; } static int mt753x_setup() calls the per-variant sw_setup() (mt7530_setup() for the MT7530), which enables the regulators, but the steps that follow returned on error without disabling them. The common case is mt7530_setup_irq() returning -EPROBE_DEFER, which leaves the rails on and re-enables them on every re-probe. Route all failure paths after sw_setup() to a shared error label that disables the regulators, and move the mdio irq teardown there instead of handling it inline. Fixes: b8f126a8d543 ("net-next: dsa: add dsa support for Mediatek MT7530 switch") Signed-off-by: Haoran Wang --- drivers/net/dsa/mt7530.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c index 1ac4ac1a0566..963d14534175 100644 --- a/drivers/net/dsa/mt7530.c +++ b/drivers/net/dsa/mt7530.c @@ -3223,11 +3223,11 @@ mt753x_setup(struct dsa_switch *ds) ret = mt7530_setup_irq(priv); if (ret) - return ret; + goto err_disable_pwrs; ret = mt7530_setup_mdio(priv); if (ret) - return ret; + goto err_disable_pwrs; /* Initialise the PCS devices */ for (i = 0; i < priv->ds->num_ports; i++) { @@ -3239,15 +3239,23 @@ mt753x_setup(struct dsa_switch *ds) if (priv->create_sgmii) ret = priv->create_sgmii(priv); - if (ret && priv->irq_domain) - mt7530_free_mdio_irq(priv); + if (ret) + goto err_free_irq; - if (!ret && priv->bus) { + if (priv->bus) { mt7530_stats_refresh(priv); schedule_delayed_work(&priv->stats_work, MT7530_STATS_POLL_INTERVAL); } + return 0; + +err_free_irq: + if (priv->irq_domain) + mt7530_free_mdio_irq(priv); +err_disable_pwrs: + mt7530_disable_pwrs(priv); + return ret; }