🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
2 messages
2 total messages Started by Juraj Sladek via B4 Relay Fri, 27 Nov 2026 13:15
[PATCH net 1/2] net: dsa: realtek: rtl8365mb: reset SerDes data path when conduit comes up
#23546
Author: Juraj Sladek via B4 Relay
Date: Fri, 27 Nov 2026 13:15
152 lines
16035 bytes
From: Juraj Sladek <juraj.sladek@littleit.sk>

The SerDes of the external interface is configured from phylink long
before the conduit Ethernet controller starts transmitting on the link.
On a TP-Link Archer AX55 v1, where the IPQ5018 GMAC is connected to port
6 of an RTL8367S over a 2500base-x (HSGMII) fixed link, the SerDes
receiver regularly comes out of this in a state where every frame
received from the CPU is counted with an FCS error, so the CPU port does
not work at all until the switch is reset.

rtl8365mb_pcs_config() already resets the SerDes data path right after
taking the SerDes out of reset. Repeating that reset once the link
partner actually transmits recovers the receiver, so factor it out and
call it again from the .conduit_state_change() callback when the conduit
becomes operational.

Fixes: 987137345f33 ("net: dsa: realtek: rtl8365mb: add HSGMII support for RTL8367S")
Signed-off-by: Juraj Sladek <juraj.sladek@littleit.sk>
---
 drivers/net/dsa/realtek/rtl8365mb_main.c | 66 +++++++++++++++++++++++++-------
 1 file changed, 53 insertions(+), 13 deletions(-)

diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
index da06f952a..54d90121e 100644
--- a/drivers/net/dsa/realtek/rtl8365mb_main.c
+++ b/drivers/net/dsa/realtek/rtl8365mb_main.c
@@ -858,6 +858,7 @@ struct rtl8365mb_port {
  * @pcs: PCS for the SerDes external interface
  * @sds_supported: SerDes tuning parameters match the chip option, so the
  *                 SerDes interface modes can be advertised
+ * @sds_in_use: the SerDes external interface has been configured
  *
  * Private data for this driver.
  */
@@ -870,6 +871,7 @@ struct rtl8365mb {
 	struct rtl8365mb_port ports[RTL8365MB_MAX_NUM_PORTS];
 	struct phylink_pcs pcs;
 	bool sds_supported;
+	bool sds_in_use;
 };
 
 #define pcs_to_rtl8365mb(_pcs) container_of((_pcs), struct rtl8365mb, pcs)
@@ -1380,6 +1382,24 @@ static int rtl8365mb_sds_raise_rate_limits(struct realtek_priv *priv)
 				  RTL8365MB_PORT6_EGRESSBW_CTRL1_MASK);
 }
 
+/* Reset the SerDes data path and resync its PLL, mirroring what the vendor
+ * firmware does right after deasserting the SerDes reset. This flushes the
+ * FIFOs and ensures a clean state for the link, preventing silent drops and
+ * CRC errors.
+ */
+static int rtl8365mb_sds_reset_datapath(struct realtek_priv *priv)
+{
+	int ret;
+
+	ret = rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_BMCR,
+				  RTL8365MB_SDS_BMCR_DPRST_PHASE1);
+	if (ret)
+		return ret;
+
+	return rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_BMCR,
+				   RTL8365MB_SDS_BMCR_DPRST_PHASE2);
+}
+
 static int rtl8365mb_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
 				phy_interface_t interface,
 				const unsigned long *advertising,
@@ -1467,18 +1487,7 @@ static int rtl8365mb_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
 	if (ret)
 		return ret;
 
-	/* Reset the SerDes data path and resync its PLL, mirroring what the
-	 * vendor firmware does right after deasserting the SerDes reset.
-	 * This flushes the FIFOs and ensures a clean state for the link,
-	 * preventing silent drops and CRC errors.
-	 */
-	ret = rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_BMCR,
-				  RTL8365MB_SDS_BMCR_DPRST_PHASE1);
-	if (ret)
-		return ret;
-
-	ret = rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_BMCR,
-				  RTL8365MB_SDS_BMCR_DPRST_PHASE2);
+	ret = rtl8365mb_sds_reset_datapath(priv);
 	if (ret)
 		return ret;
 
@@ -1492,7 +1501,13 @@ static int rtl8365mb_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
 	val &= ~RTL8365MB_SDS_NWAY_EN_MASK;
 	val |= RTL8365MB_SDS_NWAY_RESTART_MASK;
 
-	return rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_NWAY, val);
+	ret = rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_NWAY, val);
+	if (ret)
+		return ret;
+
+	mb->sds_in_use = true;
+
+	return 0;
 }
 
 static bool rtl8365mb_interface_is_serdes(phy_interface_t interface)
@@ -3079,6 +3094,29 @@ static int rtl8365mb_reset_chip(struct realtek_priv *priv)
 					20000, 1e6);
 }
 
+/* The SerDes is configured from phylink long before the conduit Ethernet
+ * controller starts transmitting on the link, so its receiver initially locks
+ * on an idle line. On an RTL8367S connected to an IPQ5018 over HSGMII this
+ * regularly left the switch receiving every frame from the CPU with an FCS
+ * error. Reset the SerDes data path again once the conduit is up.
+ */
+static void rtl8365mb_conduit_state_change(struct dsa_switch *ds,
+					   const struct net_device *conduit,
+					   bool operational)
+{
+	struct realtek_priv *priv = ds->priv;
+	struct rtl8365mb *mb = priv->chip_data;
+	int ret;
+
+	if (!operational || !mb->sds_in_use)
+		return;
+
+	ret = rtl8365mb_sds_reset_datapath(priv);
+	if (ret)
+		dev_err(priv->dev, "failed to reset SerDes data path: %pe\n",
+			ERR_PTR(ret));
+}
+
 static int rtl8365mb_setup(struct dsa_switch *ds)
 {
 	struct realtek_priv *priv = ds->priv;
@@ -3093,6 +3131,7 @@ static int rtl8365mb_setup(struct dsa_switch *ds)
 	cpu = &mb->cpu;
 
 	mb->pcs.ops = &rtl8365mb_pcs_ops;
+	mb->sds_in_use = false;
 
 	/* The SerDes has no link interrupt wired up, so phylink must poll the
 	 * PCS for link changes when it tracks the link through pcs_get_state()
@@ -3414,6 +3453,7 @@ static int rtl8365mb_set_mac_eee(struct dsa_switch *ds, int port,
 }
 
 static const struct dsa_switch_ops rtl8365mb_switch_ops = {
+	.conduit_state_change = rtl8365mb_conduit_state_change,
 	.get_tag_protocol = rtl8365mb_get_tag_protocol,
 	.change_tag_protocol = rtl8365mb_change_tag_protocol,
 	.setup = rtl8365mb_setup,

-- 
2.53.0
Re: [PATCH net 1/2] net: dsa: realtek: rtl8365mb: reset SerDes data path when conduit comes up
#23868
Author: netdev-bot+sashiko
Date: Sat, 28 Nov 2026 13:21
192 lines
14953 bytes
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · 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).

- [Medium] rtl8365mb_conduit_state_change()
  (drivers/net/dsa/realtek/rtl8365mb_main.c) resets the port-6 SerDes…
- [Medium] The patch adds a caller of the SerDes indirect-access window
  that is not serialised against the existing ones.
- [Low] rtl8365mb_conduit_state_change() reads the plain bool
  mb->sds_in_use under rtnl only, with no READ_ONCE and no shared lock,…

--

net: dsa: realtek: rtl8365mb: reset SerDes data path when conduit comes up

This patch moves the SerDes data path reset out of rtl8365mb_pcs_config()
into a new helper, rtl8365mb_sds_reset_datapath(). The helper is called
again from a new .conduit_state_change() callback when the conduit becomes
operational. This fixes the FCS errors seen on the CPU port of an RTL8367S
connected to an IPQ5018 over HSGMII.

> diff --git a/drivers/net/dsa/realtek/rtl8365mb_main.c b/drivers/net/dsa/realtek/rtl8365mb_main.c
> index da06f952a053d..54d90121ecd26 100644
> --- a/drivers/net/dsa/realtek/rtl8365mb_main.c
> +++ b/drivers/net/dsa/realtek/rtl8365mb_main.c

[ ... ]

> @@ -1492,7 +1501,13 @@ static int rtl8365mb_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
>  	val &= ~RTL8365MB_SDS_NWAY_EN_MASK;
>  	val |= RTL8365MB_SDS_NWAY_RESTART_MASK;
>  
> -	return rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_NWAY, val);
> +	ret = rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_NWAY, val);
> +	if (ret)
> +		return ret;
> +
> +	mb->sds_in_use = true;

[Severity: Low]
mb->sds_in_use is written here under the phylink state_mutex. The new
rtl8365mb_conduit_state_change() reads it with only rtnl held, with no
READ_ONCE() and no shared lock, and then touches the SerDes:

	if (!operational || !mb->sds_in_use)
		return;

	ret = rtl8365mb_sds_reset_datapath(priv);

Once set, the flag stays true until the next rtl8365mb_setup(). So it does
not exclude a later rtl8365mb_pcs_config() reprogramming the SerDes from
the phylink_resolve() worker.

Isn't this the flag-as-gate pattern, where a flag decides whether to touch
shared hardware state without a lock held?

On its own the effect is small. A stale read only skips one extra reset or
lets one through. A fix for the SDS window race described below would need
to do the sds_in_use check and the data path reset under the same lock that
rtl8365mb_pcs_config() and rtl8365mb_pcs_get_state() hold. Locking each SDS
access separately would not be enough.

The final patch in the series keeps the same lockless check.

> +
> +	return 0;
>  }

[ ... ]

> @@ -3079,6 +3094,29 @@ static int rtl8365mb_reset_chip(struct realtek_priv *priv)

[ ... ]

> +static void rtl8365mb_conduit_state_change(struct dsa_switch *ds,
> +					   const struct net_device *conduit,
> +					   bool operational)
> +{
> +	struct realtek_priv *priv = ds->priv;
> +	struct rtl8365mb *mb = priv->chip_data;
> +	int ret;
> +
> +	if (!operational || !mb->sds_in_use)
> +		return;

[Severity: Medium]
Should this check which conduit changed state? The conduit argument is
not used. Nothing checks that port 6 is a CPU port, or that it is the CPU
port behind this conduit.

The DSA core does not filter these calls either.
dsa_switch_conduit_state_change() forwards every conduit's change:

net/dsa/switch.c:dsa_switch_conduit_state_change() {
    ...
	ds->ops->conduit_state_change(ds, info->conduit, info->operational);
    ...
}

The SerDes PCS is not limited to CPU ports. For any SerDes interface,
rtl8365mb_phylink_mac_select_pcs() returns it:

	if (rtl8365mb_interface_is_serdes(interface))
		return &mb->pcs;

and rtl8365mb_pcs_config() sets sds_in_use whatever role port 6 has.

RTL8367S/RTL8367SB also have a second external interface, RGMII on port 7:

	{ 6, 1, PHY_INTF(SGMII) | PHY_INTF(HSGMII) },
	{ 7, 2, PHY_INTF(MII) | PHY_INTF(TMII) | PHY_INTF(RMII) | PHY_INTF(RGMII) },

This matters in two setups: port 7 is the CPU port and port 6 is an
SGMII/HSGMII user port, or ports 6 and 7 are both CPU ports. In either
case, if the port 7 conduit comes up or its carrier flaps (NETDEV_CHANGE),
won't this write DPRST_PHASE1/PHASE2 to the port 6 SerDes BMCR? That
would flush the FIFOs and resync the PLL on a port 6 link that may be
carrying traffic.

The later patch "net: dsa: realtek: rtl8365mb: soft reset switch core
after conduit comes up" keeps the same unfiltered check
(if (!mb->sds_in_use) return;). It also schedules a soft reset of the
whole switch core 3 seconds after any conduit becomes operational. By the
end of the series this would affect every switch port.

> +
> +	ret = rtl8365mb_sds_reset_datapath(priv);

[Severity: Medium]
Can this race with the phylink_resolve() worker on the SerDes indirect
access window?

rtl8365mb_conduit_state_change() runs from the conduit netdev notifier
with only rtnl_lock held. Each rtl8365mb_sds_write() is three separate
regmap transactions: DATA, ADR, then CMD=RUN|WR. rtl8365mb_sds_read() is
ADR, CMD=RUN, a busy poll, then DATA. priv->map_lock only covers one
transaction at a time.

Before this patch, every SDS access came from the port 6 PCS ops, and
phylink serialises those with pl->state_mutex. phylink_resolve() takes
state_mutex but not rtnl. It can reach the SDS window two ways:

phylink_resolve()
  rtl8365mb_pcs_get_state()
    rtl8365mb_sds_read(priv, RTL8365MB_SDS_REG_LINK_STATUS, &status)

This path is polled, because rtl8365mb_setup() sets mb->pcs.poll = true
for in-band mode.

phylink_resolve()
  phylink_major_config()
    rtl8365mb_pcs_config()

This path runs when a PHY switches between SGMII and 2500BASE-X.

One possible interleaving:

conduit path                         resolve worker
DATA = DPRST_PHASE1
ADR = BMCR
                                     ADR = LINK_STATUS
CMD = RUN | WR

Here PHASE1 would land in the link status register instead of BMCR.

Other interleavings are possible too. If a read is in flight, a concurrent
CMD=RUN|WR could write the read result into BMCR. The jam table writes and
the NWAY read-modify-write in rtl8365mb_pcs_config() could also be
corrupted.

The OCP PHY window in rtl8365mb_phy_ocp_read() and
rtl8365mb_phy_ocp_write() uses rtl83xx_lock() and priv->map_nolock for
this reason. Does the SDS window need a similar lock? As far as I can
tell, the final patch in the series does not add one.

> +	if (ret)
> +		dev_err(priv->dev, "failed to reset SerDes data path: %pe\n",
> +			ERR_PTR(ret));
> +}

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127-rtl8365mb-hsgmii-fix-v1-0-f2827126aa6a%40littleit.sk
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads