net: dsa: qca8k: fix MDIO error handling
v2 cover net: dsa: qca8k: fix MDIO error handling
These patches fix error handling in the qca8k internal MDIO master paths.
Patch 1 propagates MDIO read and write errors instead of reporting success or returning 0xffff. Apart from the trailers it is the v1 patch.
Patches 2 and 3 fix the two problems the Sashiko review found around v1 [1]. Both predate v1:
- After a failed page select, the MASTER_EN cleanup write went to whichever page the switch still had selected, which can clear the upper half of an unrelated register. The page cache also kept the old page even though the failed write may have reached the switch.
- A management Ethernet busy wait that timed out with successful poll requests carried on to the data read, so a read could return data from an incomplete transaction and never fall back to the MDIO bus.
Testing: each patch builds with W=1 for arm64 (drivers/net/dsa/qca, net/dsa, drivers/net/phy/qcom), and checkpatch --strict reports only the missing sign-off and the Fixes: IDs that my shallow clone cannot resolve. A userspace model compiles the driver's own MDIO functions against a simulated paged switch and a scripted management Ethernet responder, including phylib's read-modify-write helper as a consumer. Each patch fixes its own cases; the base fails all of them.
A Redmi AX5400 (IPQ5018 + QCA8337) was tested with a 6.18 backport of this series in OpenWrt. Connectivity was verified after boot, reboot, cold boot, link re-initialization and MTU changes. That board has its switch PHYs on the SoC MDIO bus and no CPU port 0, so it never uses the internal MDIO master or management Ethernet. On hardware this only shows that normal register access through the page select path still works.
v2:
- Patch 1: removed the blank line between Fixes: and the other trailers; Cc John Crispin, author of the commit named in Fixes:.
- Patches 2 and 3 are new.
v1: https://lore.kernel.org/netdev/[email protected]/
[1] https://lore.kernel.org/netdev/[email protected]/
Yongzhao Chen (3):
net: dsa: qca8k: propagate MDIO errors
net: dsa: qca8k: do not clear MASTER_EN after a failed page select
net: dsa: qca8k: fail mgmt Ethernet MDIO access on busy wait errors
drivers/net/dsa/qca/qca8k-8xxx.c | 66 +++++++++++++++++++------------- 1 file changed, 39 insertions(+), 27 deletions(-)
base-commit: a7bfaba4823e3c165bb2004c74eff7c096672bc7
Signature · 1 line
2.43.0
PATCH v2 1/3 net: dsa: qca8k: propagate MDIO errors
qca8k_mii_write32() ignores errors from both 16-bit half-word writes. Its callers (register writes and read-modify-write helpers) then return the earlier successful page selection or read result, incorrectly reporting success even when writing the switch register failed.
Propagate write errors from both the low and high half-words through the regmap paths and internal MDIO master transactions. Attempt to clear MASTER_EN even if a transaction fails, preserving the initial error, and report a cleanup failure if the transaction itself succeeded. Preserve the existing Ethernet-to-MDIO fallback order.
Propagate read errors through the internal and legacy MDIO bus callbacks instead of masking them as 0xffff. Returning 0xffff causes PHY read-modify-write callers to treat the failed read as valid register data, which can corrupt unrelated bits on writeback.
Stop polling when reading the MDIO busy status fails, and return the error immediately. Because the read helper clears its output on failure, checking only the BUSY bit mistakes an unsuccessful read for transaction completion.
---
v2:
- Removed the blank line between the Fixes: tag and the other trailers.
- Cc John Crispin, author of the commit named in the Fixes: tag.
v1: https://lore.kernel.org/netdev/[email protected]/
drivers/net/dsa/qca/qca8k-8xxx.c | 51 ++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 23 deletions(-)
drivers/net/dsa/qca/qca8k-8xxx.c +28 -23
@@ -129,13 +129,16 @@ qca8k_mii_read32(struct mii_bus *bus, int phy_id, u32 regnum, u32 *val)return ret; } -static void +static int qca8k_mii_write32 (struct mii_bus *bus,int phy_id,u32 regnum,u32 val) { -if (qca8k_mii_write_lo (bus, phy_id, regnum, val) <0 ) -return ; +int ret; + + ret =qca8k_mii_write_lo (bus, phy_id, regnum, val); +if (ret <0 ) +return ret; -qca8k_mii_write_hi (bus, phy_id, regnum +1 , val); +return qca8k_mii_write_hi (bus, phy_id, regnum +1 , val); } static int
@@ -462,7 +465,7 @@ qca8k_write_mii(struct qca8k_priv *priv, uint32_t reg, uint32_t val)if (ret <0 ) goto exit; -qca8k_mii_write32 (bus,0x10 | r2, r1, val); + ret =qca8k_mii_write32 (bus,0x10 | r2, r1, val); exit:mutex_unlock (&bus->mdio_lock);
@@ -492,7 +495,7 @@ qca8k_regmap_update_bits_mii(struct qca8k_priv *priv, uint32_t reg, val &= ~mask; val |= write_val; -qca8k_mii_write32 (bus,0x10 | r2, r1, val); + ret =qca8k_mii_write32 (bus,0x10 | r2, r1, val); exit:mutex_unlock (&bus->mdio_lock);
@@ -799,14 +802,13 @@ qca8k_mdio_busy_wait(struct mii_bus *bus, u32 reg, u32 mask)qca8k_split_addr (reg, &r1, &r2, &page); - ret =read_poll_timeout (qca8k_mii_read_hi, ret1, !(val & mask),0 , + ret =read_poll_timeout (qca8k_mii_read_hi, ret1, + ret1 <0 || !(val & mask),0 ,QCA8K_BUSY_WAIT_TIMEOUT *USEC_PER_MSEC , false, bus,0x10 | r2, r1 +1 , &val); -/* Check if qca8k_read has failed for a different reason -* before returnting -ETIMEDOUT -*/ -if (ret <0 && ret1 <0 ) +/* Preserve an MDIO read error instead of treating it as ready. */ +if (ret1 <0 )return ret1;return ret;
@@ -818,7 +820,7 @@ qca8k_mdio_write(struct qca8k_priv *priv, int phy, int regnum, u16 data)struct mii_bus *bus = priv->bus;u16 r1, r2, page;u32 val; -int ret; +int ret, ret1;if (regnum >=QCA8K_MDIO_MASTER_MAX_REG )return -EINVAL ;
@@ -836,14 +838,18 @@ qca8k_mdio_write(struct qca8k_priv *priv, int phy, int regnum, u16 data)if (ret) goto exit; -qca8k_mii_write32 (bus,0x10 | r2, r1, val); + ret =qca8k_mii_write32 (bus,0x10 | r2, r1, val); +if (ret <0 ) + goto exit; ret =qca8k_mdio_busy_wait (bus,QCA8K_MDIO_MASTER_CTRL ,QCA8K_MDIO_MASTER_BUSY ); exit:/* even if the busy_wait timeouts try to clear the MASTER_EN */ -qca8k_mii_write_hi (bus,0x10 | r2, r1 +1 ,0 ); + ret1 =qca8k_mii_write_hi (bus,0x10 | r2, r1 +1 ,0 ); +if (!ret) + ret = ret1;mutex_unlock (&bus->mdio_lock);
@@ -856,7 +862,7 @@ qca8k_mdio_read(struct qca8k_priv *priv, int phy, int regnum)struct mii_bus *bus = priv->bus;u16 r1, r2, page;u32 val; -int ret; +int ret, ret1;if (regnum >=QCA8K_MDIO_MASTER_MAX_REG )return -EINVAL ;
@@ -873,7 +879,9 @@ qca8k_mdio_read(struct qca8k_priv *priv, int phy, int regnum)if (ret) goto exit; -qca8k_mii_write_hi (bus,0x10 | r2, r1 +1 , val); + ret =qca8k_mii_write_hi (bus,0x10 | r2, r1 +1 , val); +if (ret <0 ) + goto exit; ret =qca8k_mdio_busy_wait (bus,QCA8K_MDIO_MASTER_CTRL ,QCA8K_MDIO_MASTER_BUSY );
@@ -884,7 +892,9 @@ qca8k_mdio_read(struct qca8k_priv *priv, int phy, int regnum) exit:/* even if the busy_wait timeouts try to clear the MASTER_EN */ -qca8k_mii_write_hi (bus,0x10 | r2, r1 +1 ,0 ); + ret1 =qca8k_mii_write_hi (bus,0x10 | r2, r1 +1 ,0 ); +if (!ret) + ret = ret1;mutex_unlock (&bus->mdio_lock);
@@ -919,12 +929,7 @@ qca8k_internal_mdio_read(struct mii_bus *slave_bus, int phy, int regnum)if (ret >=0 )return ret; - ret =qca8k_mdio_read (priv, phy, regnum); - -if (ret <0 ) -return 0xffff ; - -return ret; +return qca8k_mdio_read (priv, phy, regnum); } static int
Signature · 1 line
2.43.0
Andrew Lunn
maintainerRe: [PATCH net v2 1/3] net: dsa: qca8k: propagate MDIO errors
drivers/net/dsa/qca/qca8k-8xxx.c qca8k_mdio_write(struct qca8k_priv *priv, int phy, int regnum, u16 data) · line 823
Quoted 6 lines
drivers/net/dsa/qca/qca8k-8xxx.c +1
-1@@ -818,7 +820,7 @@ qca8k_mdio_write(struct qca8k_priv *priv, int phy, int regnum, u16 data)struct mii_bus *bus = priv->bus;u16 r1, r2, page;u32 val; -int ret; +int ret, ret1;
Reverse Christmas tree.
Please check the rest of the patch, and the others in the series.
Andrew
---
pw-bot: cr
Andrew Lunn
maintainerRe: [PATCH net v2 1/3] net: dsa: qca8k: propagate MDIO errors
In reply to Yongzhao ChenQuoted 16 lines · Yongzhao Chen
qca8k_mii_write32()ignores errors from both 16-bit half-word writes. Its callers (register writes and read-modify-write helpers) then return the earlier successful page selection or read result, incorrectly reporting success even when writing the switch register failed.Propagate write errors from both the low and high half-words through the regmap paths and internal MDIO master transactions. Attempt to clear
MASTER_ENeven if a transaction fails, preserving the initial error, and report a cleanup failure if the transaction itself succeeded. Preserve the existing Ethernet-to-MDIO fallback order.Propagate read errors through the internal and legacy MDIO bus callbacks instead of masking them as
0xffff. Returning0xffffcauses PHY read-modify-write callers to treat the failed read as valid register data, which can corrupt unrelated bits on writeback.
Quoted 6 lines
Stop polling when reading the MDIO busy status fails, and return the error immediately. Because the read helper clears its output on failure, checking only the BUSY bit mistakes an unsuccessful read for transaction completion.
Fixes 6b93fb46480a ("net-next: dsa: add new driver for qca8xxx family")
Does this actually bother anybody? Have there been reports of actual MDIO errors?
If not, please drop the Fixes: tag and post for net-next.
Andrew
Re: [PATCH net v2 1/3] net: dsa: qca8k: propagate MDIO errors
Hi Andrew,
Quoted 2 lines
Does this actually bother anybody? Have there been reports of actual MDIO errors?
I am not aware of any reports of real MDIO failures. The evidence comes from code inspection and fault injection in a model test. In the next revision I have dropped the Fixes tags, and I will post the three patches for net-next.
I have also fixed the reverse Christmas tree ordering you pointed out in your other message, and checked the rest of the series for the same issue.
Thanks,
Yongzhao Chen
PATCH v2 2/3 net: dsa: qca8k: do not clear MASTER_EN after a failed page select
In reply to Yongzhao Chenqca8k_mdio_write() and qca8k_mdio_read() take the common exit path when selecting the page of QCA8K_MDIO_MASTER_CTRL fails. That path clears MASTER_CTRL[31:16] with a raw write to phy 0x10, reg 0x1f, which only addresses MASTER_CTRL while page 0 is selected. After a failed page select the switch is still on the previous page, so the write can clear the upper half of an unrelated register at page * 0x200 + 0x3c.
No MDIO master transaction has been started at that point, so there is nothing to clean up. Unlock and return the error directly.
qca8k_set_page() also keeps the old cached page when the page write fails. If the write did reach the switch, a later access to the cached page skips the page select and reaches the wrong register. Invalidate the cache on failure so that the next access selects the page again.
---
v2: new patch, addressing the Sashiko review of v1:
https://lore.kernel.org/netdev/[email protected]/
drivers/net/dsa/qca/qca8k-8xxx.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/net/dsa/qca/qca8k-8xxx.c +6 -2
@@ -153,6 +153,8 @@ qca8k_set_page(struct qca8k_priv *priv, u16 page) ret = bus->write (bus,0x18 ,0 , page);if (ret <0 ) { +/* The switch may or may not have switched pages. */ + *cached_page =0xffff ;dev_err_ratelimited (&bus->dev,"failed to set qca8k page\n" );return ret;
@@ -836,7 +838,7 @@ qca8k_mdio_write(struct qca8k_priv *priv, int phy, int regnum, u16 data) ret =qca8k_set_page (priv, page);if (ret) - goto exit; + goto unlock; ret =qca8k_mii_write32 (bus,0x10 | r2, r1, val);if (ret <0 )
@@ -851,6 +853,7 @@ qca8k_mdio_write(struct qca8k_priv *priv, int phy, int regnum, u16 data)if (!ret) ret = ret1; +unlock:mutex_unlock (&bus->mdio_lock);return ret;
@@ -877,7 +880,7 @@ qca8k_mdio_read(struct qca8k_priv *priv, int phy, int regnum) ret =qca8k_set_page (priv, page);if (ret) - goto exit; + goto unlock; ret =qca8k_mii_write_hi (bus,0x10 | r2, r1 +1 , val);if (ret <0 )
@@ -896,6 +899,7 @@ qca8k_mdio_read(struct qca8k_priv *priv, int phy, int regnum)if (!ret) ret = ret1; +unlock:mutex_unlock (&bus->mdio_lock);if (ret >=0 )
Signature · 1 line
2.43.0
PATCH v2 3/3 net: dsa: qca8k: fail mgmt Ethernet MDIO access on busy wait errors
In reply to Yongzhao Chenqca8k_phy_eth_command() polls MASTER_CTRL over management Ethernet until BUSY clears, but only bails out when the poll timed out and the last poll request also failed. If every request succeeds while BUSY stays set, the -ETIMEDOUT is dropped. A read then returns MASTER_CTRL data from a transaction that has not completed, and qca8k_internal_mdio_read() does not fall back to the MDIO bus.
A failed poll request does not stop the loop either. qca8k_phy_eth_busy_wait() leaves the value unchanged on failure, so the BUSY test then uses the previous value, or an uninitialized one if the first request fails.
Stop polling when a request fails and return the poll error or timeout. The early return also leaked read_skb, which is only consumed when the read request is sent; free it on this path.
---
v2: new patch, addressing the Sashiko review of v1:
https://lore.kernel.org/netdev/[email protected]/
drivers/net/dsa/qca/qca8k-8xxx.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/dsa/qca/qca8k-8xxx.c +5 -2
@@ -728,12 +728,15 @@ qca8k_phy_eth_command(struct qca8k_priv *priv, bool read, int phy, } ret =read_poll_timeout (qca8k_phy_eth_busy_wait, ret1, - !(val &QCA8K_MDIO_MASTER_BUSY ),0 , + ret1 <0 || !(val &QCA8K_MDIO_MASTER_BUSY ),0 ,QCA8K_BUSY_WAIT_TIMEOUT *USEC_PER_MSEC , false, mgmt_eth_data, read_skb, &val); -if (ret <0 && ret1 <0 ) { +if (ret1 <0 ) ret = ret1; + +if (ret <0 ) { +kfree_skb (read_skb); goto exit; }
Signature · 1 line
2.43.0