Skip to contentSkip to conversation

net: dsa: motorcomm: Resync SMI slave before register access

PATCH Superseded David Yang 3 participants 3 messages Nov 16 – Nov 18

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

David Yang

author maintainer
#0 lore raw

PATCH v2 1/1 net: dsa: motorcomm: Resync SMI slave before register access

A register access over MDIO is 4 frames: 2 address writes followed by 2 data reads or writes. The SMI slave tracks the phase in a state machine, so an access interrupted between frames, e.g. on an MDIO bus error, leaves the machine in an unknown state, and every following access fails or acts on a stale address until the machine is realigned.

The driver has so far assumed every access succeeds, and relied on manual resetting when the state machine wedged. However, without a dedicated reset line, the reset itself goes through the same SMI interface, causing a chicken-and-egg problem.

Realign the machine once at reset, before the first register access.

---
v2:
- only reset once at setup
v1: https://lore.kernel.org/r/[email protected]

 drivers/net/dsa/motorcomm/chip.c | 101 +++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)
drivers/net/dsa/motorcomm/chip.c +101 -0
index 35e70b322608..5d6390abd7cb 100644
@@ -98,6 +98,103 @@ struct yt921x_reg_mdio {
 
 /* TODO: SPI/I2C */
 
+/* Realign the SMI state machine _on a best-effort basis_ - see below.
+ *
+ * An SMI register access is 4 MDIO frames. An interrupted access leaves the
+ * machine in an unknown state, and every following access fails or acts on a
+ * stale address.
+ *
+ * The driver used to assume every access succeeds, and relied on manual
+ * resetting when the state machine wedged. However, without a dedicated reset
+ * line, the reset itself goes through the same SMI interface, causing a
+ * chicken-and-egg problem. Furthermore, the driver verifies the chip id before
+ * firing the reset, so the reset can never be triggered reliably.
+ *
+ * (A/D: address/data, H/L: hi/lo byte, r/w: read/write, ?: the same flag within
+ * a rule)
+ *
+ * State Input Next Comment
+ * ----- ----- ---- -------
+ * AH    aw?   AL?  step #1
+ * AL?   aw?   DH?  step #2
+ * DH?   d?    DL?  step #3
+ * DL?   d?    AH   step #4
+ * A?    dr    A?   `0xdead` sentinel
+ * ALw   awr   ALw  ignored
+ * D?r   awr   AH   abort, frame discarded
+ * DHw   awr   AH   abort, frame discarded
+ * DHw   dr    DHw  `0xdead` sentinel
+ * DLr   dw    ?    `0xdead` on following drs; next state undetermined
+ * DLw   awr   ALr  abort, frame consumed as next access's addr-hi
+ * DLw   dr    ?    `0xdead` sentinel; next state undetermined
+ *
+ * Any other REGAD/op combination is not characterized and never sent.
+ *
+ * The reset sequence is awr, dr, dr, awr, awr, dr, dr, plus one more dr
+ * as a heuristic sanity check. The `0xdead` sentinel cannot distinguish between
+ * AH, AL, or D?w, but when this happens, it should be classified as a model or
+ * hardware error - in either case, there is no use to do more check.
+ *
+ * This is bus-related, but I'm not sure whether it should go to
+ * yt921x_reg_ops - decide it later when we do have a second bus type.
+ */
+static int yt921x_reg_mdio_reset(void *context)
+{
+	struct yt921x_reg_mdio *mdio = context;
+	struct mii_bus *bus = mdio->bus;
+	int addr = mdio->addr;
+	u32 reg_addr;
+	u32 reg_data;
+	int res;
+
+	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
+
+	reg_addr = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_ADDR |
+		   YT921X_SMI_READ;
+	reg_data = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_DATA |
+		   YT921X_SMI_READ;
+
+	/* awr */
+	res = __mdiobus_write(bus, addr, reg_addr, 0);
+	if (res)
+		goto end;
+	/* dr */
+	res = __mdiobus_read(bus, addr, reg_data);
+	if (res < 0)
+		goto end;
+	/* dr */
+	res = __mdiobus_read(bus, addr, reg_data);
+	if (res < 0)
+		goto end;
+	/* awr */
+	res = __mdiobus_write(bus, addr, reg_addr, 0);
+	if (res)
+		goto end;
+	/* awr */
+	res = __mdiobus_write(bus, addr, reg_addr, 0);
+	if (res)
+		goto end;
+	/* dr */
+	res = __mdiobus_read(bus, addr, reg_data);
+	if (res < 0)
+		goto end;
+	/* dr */
+	res = __mdiobus_read(bus, addr, reg_data);
+	if (res < 0)
+		goto end;
+
+	/* Heuristic sanity check: expect AH + dr -> AH `0xdead` sentinel. */
+	res = __mdiobus_read(bus, addr, reg_data);
+	if (res < 0)
+		goto end;
+	res = res != 0xdead ? -EIO : 0;
+
+end:
+	mutex_unlock(&bus->mdio_lock);
+
+	return res;
+}
+
 static int yt921x_reg_mdio_read(void *context, u32 reg, u32 *valp)
 {
 	struct yt921x_reg_mdio *mdio = context;
@@ -2225,6 +2322,10 @@ static int yt921x_chip_reset(struct yt921x_priv *priv)
 	u32 val;
 	int res;
 
+	res = yt921x_reg_mdio_reset(priv->reg_ctx);
+	if (res)
+		return res;
+
 	res = yt921x_chip_detect(priv);
 	if (res)
 		return res;
Signature · 1 line
2.53.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · David Yang

A register access over MDIO is 4 frames: 2 address writes followed by 2 data reads or writes. The SMI slave tracks the phase in a state machine, so an access interrupted between frames, e.g. on an MDIO bus error, leaves the machine in an unknown state, and every following access fails or acts on a stale address until the machine is realigned.

Quoted 8 lines

The driver has so far assumed every access succeeds, and relied on manual resetting when the state machine wedged. However, without a dedicated reset line, the reset itself goes through the same SMI interface, causing a chicken-and-egg problem.

Realign the machine once at reset, before the first register access.

Andrew

#2 lore raw
In reply to David Yang
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The resync sequence in yt921x_reg_mdio_reset() (drivers/net/dsa/motorcomm/chip.c) cannot bring the SMI slave out of…
- [Low] The patch adds a new way for probe to fail, and the commit message does not mention it.

--

net: dsa: motorcomm: Resync SMI slave before register access

This patch adds yt921x_reg_mdio_reset(). It sends a fixed sequence of SMI address and data frames to realign the slave's 4-frame state machine. The function is called from yt921x_chip_reset() before yt921x_chip_detect(), and it ends with a read that is expected to return the 0xdead sentinel.

Quoted 5 lines
drivers/net/dsa/motorcomm/chip.c +0 -0
index 35e70b3226089..5d6390abd7cb5 100644
@@ -98,6 +98,103 @@ struct yt921x_reg_mdio {

[ ... ]

Quoted 21 lines

+ * State Input Next Comment
+ * ----- ----- ---- -------

+ * AH    aw?   AL?  step #1
+ * AL?   aw?   DH?  step #2
+ * DH?   d?    DL?  step #3
+ * DL?   d?    AH   step #4
+ * A?    dr    A?   `0xdead` sentinel
+ * ALw   awr   ALw  ignored
+ * D?r   awr   AH   abort, frame discarded
+ * DHw   awr   AH   abort, frame discarded
+ * DHw   dr    DHw  `0xdead` sentinel
+ * DLr   dw    ?    `0xdead` on following drs; next state undetermined
+ * DLw   awr   ALr  abort, frame consumed as next access's addr-hi
+ * DLw   dr    ?    `0xdead` sentinel; next state undetermined

+ *
+ * Any other REGAD/op combination is not characterized and never sent.
+ *
+ * The reset sequence is awr, dr, dr, awr, awr, dr, dr, plus one more dr
+ * as a heuristic sanity check. The 0xdead sentinel cannot distinguish between
+ * AH, AL, or D?w, but when this happens, it should be classified as a model or
+ * hardware error - in either case, there is no use to do more check.

[ ... ]

Quoted 4 lines
drivers/net/dsa/motorcomm/chip.c +4 -0
+	reg_addr = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_ADDR |
+		   YT921X_SMI_READ;
+	reg_data = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_DATA |
+		   YT921X_SMI_READ;

[ ... ]

drivers/net/dsa/motorcomm/chip.c struct yt921x_reg_mdio { · line 190

Quoted 5 lines
drivers/net/dsa/motorcomm/chip.c +5 -0
+	/* Heuristic sanity check: expect AH + dr -> AH `0xdead` sentinel. */
+	res = __mdiobus_read(bus, addr, reg_data);
+	if (res < 0)
+		goto end;
+	res = res != 0xdead ? -EIO : 0;

[Severity: High]
Can this sequence get the slave out of the ALw state?

The table above gives these transitions for ALw:

  A?    dr    A?   0xdead sentinel
  ALw   awr   ALw  ignored

The only characterized way out of ALw is "AL? aw? DH?" with an aww frame. yt921x_reg_mdio_reset() only builds read-flag address and data REGADs. So by the table, every awr and dr it sends is a no-op in ALw.

I stepped through the table for the other states. AH, ALr, DHr, DHw, DLr and DLw all end up back in AH, so ALw looks like the only state that does not converge.

The final check does not catch this either. ALw answers dr with 0xdead just like AH does, so yt921x_reg_mdio_reset() returns 0 while the slave is still out of phase.

The slave can end up in ALw through yt921x_reg_mdio_write(). Its first address frame is aww, because YT921X_SMI_ADDR | YT921X_SMI_WRITE is 0:

	reg_addr = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_ADDR |
		   YT921X_SMI_WRITE;
	res = __mdiobus_write(bus, addr, reg_addr, (u16)(reg >> 16));
	if (res)
		goto end;
	res = __mdiobus_write(bus, addr, reg_addr, (u16)reg);

If the second frame never reaches the slave, the slave stays in ALw. That can happen on an MDIO bus error or a host reset between frames, and firmware could also leave the slave in that state. This is the "access interrupted between frames, e.g. on an MDIO bus error" case from the commit message.

On the next probe, yt921x_dsa_setup()->yt921x_chip_reset()-> yt921x_reg_mdio_reset() reports success. Then yt921x_chip_detect() reads CHIP_ID. Both awr frames are ignored and both dr frames return 0xdead, so chipid is 0xdeaddead.

That matches neither YT9215_MAJOR nor YT9218_MAJOR. Probe fails with "Unexpected chipid" and -ENODEV before the YT921X_RST_HW write is reached.

Every re-probe repeats the same no-op sequence. Wouldn't the switch stay unusable until a power cycle? That looks like the chicken-and-egg problem this patch is meant to remove.

The comment also says an ambiguous 0xdead result "should be classified as a model or hardware error". The model in the same comment predicts ALw -> ALw for this sequence, so the two seem to disagree.

Would this need a characterized aww step? Another option might be to check the real CHIP_ID read result and then send aww followed by awr.

[ ... ]

drivers/net/dsa/motorcomm/chip.c static int yt921x_chip_reset(struct yt921x_priv *priv) · line 2329

Quoted 9 lines
drivers/net/dsa/motorcomm/chip.c +4 -0
@@ -2225,6 +2322,10 @@ static int yt921x_chip_reset(struct yt921x_priv *priv)
 	u32 val;
 	int res;
 
+	res = yt921x_reg_mdio_reset(priv->reg_ctx);
+	if (res)
+		return res;
+
 	res = yt921x_chip_detect(priv);

[Severity: Low]
This adds a new probe failure path, and the commit message does not mention it.

yt921x_reg_mdio_reset() returns -EIO whenever the final read is not 0xdead. It runs before yt921x_chip_detect(), so the check applies to every entry in yt921x_infos[] before the driver knows which chip it is talking to.

Has the 0xdead sentinel behaviour been confirmed on both the YT9215_MAJOR and YT9218_MAJOR parts?

The -EIO is also passed up through yt921x_chip_reset() and yt921x_dsa_setup() without a dev_err(). The other early failures in yt921x_chip_detect() print "Unexpected chipid" or "Unsupported chipid". If the heuristic misfires, the user only sees a generic probe error -5.

Could this path print a message that points at the SMI resync?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116162303.1368472-1-mmyangfl%40gmail.com