]> git.ipfire.org Git - thirdparty/openwrt.git/commitdiff
realtek: fix mdio parent/child locking issues 18824/head
authorMarkus Stockhausen <markus.stockhausen@gmx.de>
Sat, 17 May 2025 06:22:09 +0000 (02:22 -0400)
committerRobert Marko <robimarko@gmail.com>
Thu, 22 May 2025 16:58:58 +0000 (18:58 +0200)
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 <markus.stockhausen@gmx.de>
Link: https://github.com/openwrt/openwrt/pull/18824
Signed-off-by: Robert Marko <robimarko@gmail.com>
target/linux/realtek/files-6.6/drivers/net/dsa/rtl83xx/common.c
target/linux/realtek/files-6.6/drivers/net/dsa/rtl83xx/rtl838x.h

index 9d37cccabf47b604a1cbd06cb6157e42fba3dfed..d88a761209a5a4e030b4a22eb3d423aa4a12d91d 100644 (file)
@@ -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) {
index 16c609837832877bbb91ab6858f1d5f08d1bd70e..c721b4d0705ea43cae4c6d3314d7a8c8c1dae8c3 100644 (file)
@@ -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;