From: Markus Stockhausen Date: Sat, 17 May 2025 06:22:09 +0000 (-0400) Subject: realtek: fix mdio parent/child locking issues X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=461fc06f9d72d032b2acc86fbb7662511e07dd3c;p=thirdparty%2Fopenwrt.git realtek: fix mdio parent/child locking issues Since the early beginning of the Realtek DSA driver there is an uncovered locking issue between the standard (parent) mdio bus and the DSA (child) mdio bus. This comes from the fact that the DSA bus simply links to the parent read and write functions and calls them directly. This leads to the following lock issue. - Child bus calls phy_read/write functions and uses its internal lock - Parent bus calls phy_read/write functions and uses its internal lock It becomes clear that critical section can be accessed twice without knowing that a operation from the other bus is currently active. This can lead to critical malfunctions because the mdio driver needs a lot of internal magic to get page selection done right. Effects are: - The original page is lost after a phy_write/read_paged() call - dmesg like "Realtek RTL8218B (external) rtl838x slave mii-0:00: Expected external RTL8218B, found PHY-ID 6b23" Other DSA drivers simply use the read/write functions from the parent bus and thus avoid locking issues. Do it the same way. Fixes: 2b88563ee5aafd9 ("realtek: update the tree to the latest refactored version") Signed-off-by: Markus Stockhausen Link: https://github.com/openwrt/openwrt/pull/18824 Signed-off-by: Robert Marko --- diff --git a/target/linux/realtek/files-6.6/drivers/net/dsa/rtl83xx/common.c b/target/linux/realtek/files-6.6/drivers/net/dsa/rtl83xx/common.c index 9d37cccabf4..d88a761209a 100644 --- a/target/linux/realtek/files-6.6/drivers/net/dsa/rtl83xx/common.c +++ b/target/linux/realtek/files-6.6/drivers/net/dsa/rtl83xx/common.c @@ -271,6 +271,20 @@ int write_phy(u32 port, u32 page, u32 reg, u32 val) return -1; } +static int rtldsa_mdio_read(struct mii_bus *bus, int addr, int regnum) +{ + struct rtl838x_switch_priv *priv = bus->priv; + + return mdiobus_read_nested(priv->parent_bus, addr, regnum); +} + +static int rtldsa_mdio_write(struct mii_bus *bus, int addr, int regnum, u16 val) +{ + struct rtl838x_switch_priv *priv = bus->priv; + + return mdiobus_write_nested(priv->parent_bus, addr, regnum, val); +} + static int __init rtl83xx_mdio_probe(struct rtl838x_switch_priv *priv) { struct device *dev = priv->dev; @@ -288,8 +302,8 @@ static int __init rtl83xx_mdio_probe(struct rtl838x_switch_priv *priv) return -ENODEV; } - priv->mii_bus = of_mdio_find_bus(mii_np); - if (!priv->mii_bus) { + priv->parent_bus = of_mdio_find_bus(mii_np); + if (!priv->parent_bus) { pr_debug("Deferring probe of mdio bus\n"); return -EPROBE_DEFER; } @@ -300,18 +314,14 @@ static int __init rtl83xx_mdio_probe(struct rtl838x_switch_priv *priv) if (!bus) return -ENOMEM; - bus->name = "rtl838x slave mii"; - - /* Since the NIC driver is loaded first, we can use the mdio rw functions - * assigned there. - */ - bus->read = priv->mii_bus->read; - bus->write = priv->mii_bus->write; + bus->name = "rtldsa_mdio"; + bus->read = rtldsa_mdio_read; + bus->write = rtldsa_mdio_write; snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", bus->name, dev->id); bus->parent = dev; priv->ds->slave_mii_bus = bus; - priv->ds->slave_mii_bus->priv = priv->mii_bus->priv; + priv->ds->slave_mii_bus->priv = priv; ret = mdiobus_register(priv->ds->slave_mii_bus); if (ret && mii_np) { diff --git a/target/linux/realtek/files-6.6/drivers/net/dsa/rtl83xx/rtl838x.h b/target/linux/realtek/files-6.6/drivers/net/dsa/rtl83xx/rtl838x.h index 16c60983783..c721b4d0705 100644 --- a/target/linux/realtek/files-6.6/drivers/net/dsa/rtl83xx/rtl838x.h +++ b/target/linux/realtek/files-6.6/drivers/net/dsa/rtl83xx/rtl838x.h @@ -1081,7 +1081,7 @@ struct rtl838x_switch_priv { struct mutex pie_mutex; /* Mutex for Packet Inspection Engine */ int link_state_irq; int mirror_group_ports[4]; - struct mii_bus *mii_bus; + struct mii_bus *parent_bus; const struct rtl838x_reg *r; u8 cpu_port; u8 port_mask;