Netdev List
 help / color / mirror / Atom feed
From: David Yang <mmyangfl@gmail.com>
To: netdev@vger.kernel.org
Cc: David Yang <mmyangfl@gmail.com>, Andrew Lunn <andrew@lunn.ch>,
	Vladimir Oltean <olteanv@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Russell King <linux@armlinux.org.uk>
Subject: [PATCH net-next] net: dsa: motorcomm: Unregister the MDIO buses on teardown
Date: Thu, 26 Nov 2026 11:47:22 +0800	[thread overview]
Message-ID: <20261126034725.64709-1-mmyangfl@gmail.com> (raw)

.setup() can run again within one registration: removing another switch
from the tree tears the whole tree down, and re-probing sets every
member switch up again.

Since our MDIO buses are registered in .setup() and not released in
.teardown(), the second registration then collides with the bus that is
still registered (same name under the same parent) and fails in
__mdiobus_register(), so the whole tree setup fails.

Register the buses manually and free them in .teardown().

Not carrying a fix tag since there are no known multi-chip yt921x users.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/dsa/motorcomm/chip.c     | 18 ++++++++++---
 drivers/net/dsa/motorcomm/mdio_bus.c | 40 +++++++++++++++++++++++-----
 drivers/net/dsa/motorcomm/mdio_bus.h |  2 ++
 3 files changed, 50 insertions(+), 10 deletions(-)

diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c
index a21d4bb5e141..789a08dbe3b9 100644
--- a/drivers/net/dsa/motorcomm/chip.c
+++ b/drivers/net/dsa/motorcomm/chip.c
@@ -2240,13 +2240,16 @@ static int yt921x_edata_read(struct yt921x_priv *priv, u8 addr, u8 *valp)
 
 static void yt921x_dsa_teardown(struct dsa_switch *ds)
 {
-#if IS_ENABLED(CONFIG_NET_DSA_YT921X_LEDS)
 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
 
+#if IS_ENABLED(CONFIG_NET_DSA_YT921X_LEDS)
 	yt921x_leds_remove(priv);
 #endif
 
 	dsa_devlink_resources_unregister(ds);
+
+	yt921x_mbus_ext_remove(priv);
+	yt921x_mbus_int_remove(priv);
 }
 
 static int yt921x_chip_detect(struct yt921x_priv *priv)
@@ -2558,10 +2561,11 @@ static int yt921x_dsa_setup(struct dsa_switch *ds)
 		res = yt921x_mbus_ext_init(priv, child);
 		of_node_put(child);
 		if (res)
-			return res;
+			goto err_mbus_int;
 
 		dev_err(dev, "Untested external mdio bus\n");
-		return -ENODEV;
+		res = -ENODEV;
+		goto err;
 	}
 
 	mask = priv->info->serdes_mask;
@@ -2586,7 +2590,7 @@ static int yt921x_dsa_setup(struct dsa_switch *ds)
 	mutex_unlock(&priv->reg_lock);
 
 	if (res)
-		return res;
+		goto err;
 
 	res = yt921x_devlink_setup(priv);
 	if (res)
@@ -2599,6 +2603,12 @@ static int yt921x_dsa_setup(struct dsa_switch *ds)
 #endif
 
 	return 0;
+
+err:
+	yt921x_mbus_ext_remove(priv);
+err_mbus_int:
+	yt921x_mbus_int_remove(priv);
+	return res;
 }
 
 static const struct phylink_mac_ops yt921x_phylink_mac_ops = {
diff --git a/drivers/net/dsa/motorcomm/mdio_bus.c b/drivers/net/dsa/motorcomm/mdio_bus.c
index dd6c52f37bbf..3aea4fd3102e 100644
--- a/drivers/net/dsa/motorcomm/mdio_bus.c
+++ b/drivers/net/dsa/motorcomm/mdio_bus.c
@@ -147,7 +147,7 @@ int yt921x_mbus_int_init(struct yt921x_priv *priv, struct device_node *mnp)
 	struct mii_bus *mbus;
 	int res;
 
-	mbus = devm_mdiobus_alloc(dev);
+	mbus = mdiobus_alloc();
 	if (!mbus)
 		return -ENOMEM;
 
@@ -159,15 +159,29 @@ int yt921x_mbus_int_init(struct yt921x_priv *priv, struct device_node *mnp)
 	mbus->parent = dev;
 	mbus->phy_mask = (u32)~GENMASK(YT921X_PORT_NUM - 1, 0);
 
-	res = devm_of_mdiobus_register(dev, mbus, mnp);
-	if (res)
+	res = of_mdiobus_register(mbus, mnp);
+	if (res) {
+		mdiobus_free(mbus);
 		return res;
+	}
 
 	priv->mbus_int = mbus;
 
 	return 0;
 }
 
+void yt921x_mbus_int_remove(struct yt921x_priv *priv)
+{
+	struct mii_bus *mbus = priv->mbus_int;
+
+	if (!mbus)
+		return;
+	priv->mbus_int = NULL;
+
+	mdiobus_unregister(mbus);
+	mdiobus_free(mbus);
+}
+
 static int yt921x_extif_wait(struct yt921x_priv *priv)
 {
 	u32 val = 0;
@@ -277,7 +291,7 @@ int yt921x_mbus_ext_init(struct yt921x_priv *priv, struct device_node *mnp)
 	struct mii_bus *mbus;
 	int res;
 
-	mbus = devm_mdiobus_alloc(dev);
+	mbus = mdiobus_alloc();
 	if (!mbus)
 		return -ENOMEM;
 
@@ -289,11 +303,25 @@ int yt921x_mbus_ext_init(struct yt921x_priv *priv, struct device_node *mnp)
 	mbus->write = yt921x_mbus_ext_write;
 	mbus->parent = dev;
 
-	res = devm_of_mdiobus_register(dev, mbus, mnp);
-	if (res)
+	res = of_mdiobus_register(mbus, mnp);
+	if (res) {
+		mdiobus_free(mbus);
 		return res;
+	}
 
 	priv->mbus_ext = mbus;
 
 	return 0;
 }
+
+void yt921x_mbus_ext_remove(struct yt921x_priv *priv)
+{
+	struct mii_bus *mbus = priv->mbus_ext;
+
+	if (!mbus)
+		return;
+	priv->mbus_ext = NULL;
+
+	mdiobus_unregister(mbus);
+	mdiobus_free(mbus);
+}
diff --git a/drivers/net/dsa/motorcomm/mdio_bus.h b/drivers/net/dsa/motorcomm/mdio_bus.h
index e79b725d435b..9cb1824714e3 100644
--- a/drivers/net/dsa/motorcomm/mdio_bus.h
+++ b/drivers/net/dsa/motorcomm/mdio_bus.h
@@ -49,6 +49,8 @@ yt921x_intif_modify(struct yt921x_priv *priv, int port, int reg, u16 mask,
 }
 
 int yt921x_mbus_int_init(struct yt921x_priv *priv, struct device_node *mnp);
+void yt921x_mbus_int_remove(struct yt921x_priv *priv);
 int yt921x_mbus_ext_init(struct yt921x_priv *priv, struct device_node *mnp);
+void yt921x_mbus_ext_remove(struct yt921x_priv *priv);
 
 #endif
-- 
2.53.0


             reply	other threads:[~2026-11-26  3:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  3:47 David Yang [this message]
2026-11-26  3:52 ` [PATCH net-next] net: dsa: motorcomm: Unregister the MDIO buses on teardown netdev-bot+sinfo
2026-11-26  4:00   ` David Yang
2026-11-26 13:53 ` Andrew Lunn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126034725.64709-1-mmyangfl@gmail.com \
    --to=mmyangfl@gmail.com \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=netdev@vger.kernel.org \
    --cc=olteanv@gmail.com \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox