Skip to contentSkip to conversation

net: stmmac: program the FSM timeout timer

PATCH New Zxyan Zhu 2 participants 2 messages Nov 26 – Nov 27

Subsystems (4)
lore
Share & tools
lore

Zxyan Zhu

author
#0 lore raw

PATCH v6 1/1 net: stmmac: program the FSM timeout timer

stmmac enables the FSM timeout checker (TMOUTEN) but never programs the MAC_FSM_ACT_Timer register. Per the databook the checker only fires once NTMRMD is programmed with a non-zero duration; NTMRMD == 0 disables the timer, so with TMOUTEN set but the timer left at reset a stuck FSM is never reported and the advertised timeout protection is ineffective.

Program MAC_FSM_ACT_Timer right after TMOUTEN is enabled, deriving the TMR field (CSR clocks per 1us tic) from the CSR clock rate and using a 4s timeout. 4s is chosen because a PAUSE frame with the maximum 0xffff quanta stalls the transmitter for about 3.36s at 10 Mb/s, and a shorter timeout could wrongly report a flow-control stall as a stuck FSM.

The NTMRMD field holds a power-of-two microsecond timeout per the
databook: 0x0 disables it, and 0x1..0xa encode 1us, 1.024ms (~1ms), 16.384ms (~16ms), 65.536ms (~64ms), 262.144ms (~256ms), 1.048s (~1s), 4.194s (~4s), 16.777s (~16s), 33.554s (~32s), 67.108s (~64s).

The issue was hit in testing on dwmac4 and dwxgmac, where a stuck FSM was not reported through the safety interrupt despite TMOUTEN being set.

---
v6:
- Move the STMMAC_NTMRMD_* encodings from include/linux/stmmac.h to the driver's common.h, and add Maxime's Reviewed-by.

v5: https://lore.kernel.org/netdev/[email protected]/
v4: https://lore.kernel.org/netdev/[email protected]/
v3: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/

 drivers/net/ethernet/stmicro/stmmac/common.h   | 12 ++++++++++++
 drivers/net/ethernet/stmicro/stmmac/dwmac5.c   | 17 ++++++++++++++++-
 drivers/net/ethernet/stmicro/stmmac/dwmac5.h   |  6 +++++-
 drivers/net/ethernet/stmicro/stmmac/dwxgmac2.h |  3 +++
 .../ethernet/stmicro/stmmac/dwxgmac2_core.c    | 18 +++++++++++++++++-
 drivers/net/ethernet/stmicro/stmmac/hwif.h     |  3 ++-
 .../net/ethernet/stmicro/stmmac/stmmac_main.c  |  8 +++++++-
 7 files changed, 62 insertions(+), 5 deletions(-)
drivers/net/ethernet/stmicro/stmmac/common.h +12 -0
index a972bee66504..17cc0dc7e79d 100644
@@ -268,6 +268,18 @@ struct stmmac_safety_stats {
 
 #define	MAC_CSR_H_FRQ_MASK	0x20
 
+/* MAC_FSM_ACT_Timer.NTMRMD timeout durations */
+#define STMMAC_NTMRMD_1US		0x1
+#define STMMAC_NTMRMD_1MS		0x2
+#define STMMAC_NTMRMD_16MS		0x3
+#define STMMAC_NTMRMD_64MS		0x4
+#define STMMAC_NTMRMD_256MS		0x5
+#define STMMAC_NTMRMD_1SEC		0x6
+#define STMMAC_NTMRMD_4SEC		0x7
+#define STMMAC_NTMRMD_16SEC		0x8
+#define STMMAC_NTMRMD_32SEC		0x9
+#define STMMAC_NTMRMD_64SEC		0xa
+
 #define HASH_TABLE_SIZE 64
 #define PAUSE_TIME 0xffff
 
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +16 -1
index f1bb981cab7c..37e70b82c8b5 100644
@@ -2,6 +2,7 @@
 // Copyright (c) 2017 Synopsys, Inc. and/or its affiliates.
 // stmmac Support for 5.xx Ethernet QoS cores
 
+#include <linux/bitfield.h>
 #include <linux/bitops.h>
 #include <linux/iopoll.h>
 #include "common.h"
@@ -184,7 +185,8 @@ static void dwmac5_handle_dma_err(struct net_device *ndev,
 }
 
 int dwmac5_safety_feat_config(void __iomem *ioaddr, unsigned int asp,
-			      struct stmmac_safety_feature_cfg *safety_feat_cfg)
+			      struct stmmac_safety_feature_cfg *safety_feat_cfg,
+			      unsigned int csr_clk_mhz)
 {
 	struct stmmac_safety_feature_cfg all_safety_feats = {
 		.tsoee = 1,
@@ -245,6 +247,19 @@ int dwmac5_safety_feat_config(void __iomem *ioaddr, unsigned int asp,
 		value |= TMOUTEN; /* FSM Timeout Feature */
 	writel(value, ioaddr + MAC_FSM_CONTROL);
 
+	/* Program the FSM active timer so the timeout checker actually fires.
+	 * TMR is the number of CSR clocks per 1us tic, i.e. csr_clk in MHz.
+	 * Use a 4s timeout.
+	 */
+	if (safety_feat_cfg->tmouten && csr_clk_mhz) {
+		value = readl(ioaddr + MAC_FSM_ACT_TIMER);
+		value &= ~(MAC_FSM_ACT_TIMER_NTMRMD | MAC_FSM_ACT_TIMER_TMR);
+		value |= FIELD_PREP(MAC_FSM_ACT_TIMER_NTMRMD,
+				    STMMAC_NTMRMD_4SEC) |
+			 FIELD_PREP(MAC_FSM_ACT_TIMER_TMR, csr_clk_mhz);
+		writel(value, ioaddr + MAC_FSM_ACT_TIMER);
+	}
+
 	/* 4. Enable Data Parity Protection */
 	value = readl(ioaddr + MTL_DPP_CONTROL);
 	if (safety_feat_cfg->edpp)
drivers/net/ethernet/stmicro/stmmac/dwmac5.h +5 -1
index 00b151b3b688..734e7630f28d 100644
@@ -8,8 +8,11 @@
 #define MAC_DPP_FSM_INT_STATUS		0x00000140
 #define MAC_AXI_SLV_DPE_ADDR_STATUS	0x00000144
 #define MAC_FSM_CONTROL			0x00000148
+#define MAC_FSM_ACT_TIMER		0x0000014C
 #define PRTYEN				BIT(1)
 #define TMOUTEN				BIT(0)
+#define MAC_FSM_ACT_TIMER_TMR		GENMASK(9, 0)
+#define MAC_FSM_ACT_TIMER_NTMRMD	GENMASK(19, 16)
 
 #define MAC_PPS_CONTROL			0x00000b70
 #define PPS_MAXIDX(x)			((((x) + 1) * 8) - 1)
@@ -82,7 +85,8 @@
 #define GMAC_INT_FPE_EN			BIT(17)
 
 int dwmac5_safety_feat_config(void __iomem *ioaddr, unsigned int asp,
-			      struct stmmac_safety_feature_cfg *safety_cfg);
+			      struct stmmac_safety_feature_cfg *safety_cfg,
+			      unsigned int csr_clk_mhz);
 int dwmac5_safety_feat_irq_status(struct net_device *ndev,
 		void __iomem *ioaddr, unsigned int asp,
 		struct stmmac_safety_stats *stats);
drivers/net/ethernet/stmicro/stmmac/dwxgmac2.h +3 -0
index f8ab347f7b5b..1fd501728b18 100644
@@ -169,8 +169,11 @@
 #define XGMAC_HWFEAT_PCSEL		GENMASK(1, 0)
 #define XGMAC_MAC_DPP_FSM_INT_STATUS	0x00000150
 #define XGMAC_MAC_FSM_CONTROL		0x00000158
+#define XGMAC_MAC_FSM_ACT_TIMER		0x0000015C
 #define XGMAC_PRTYEN			BIT(1)
 #define XGMAC_TMOUTEN			BIT(0)
+#define XGMAC_MAC_FSM_ACT_TIMER_TMR		GENMASK(9, 0)
+#define XGMAC_MAC_FSM_ACT_TIMER_NTMRMD		GENMASK(19, 16)
 #define XGMAC_MDIO_ADDR			0x00000200
 #define XGMAC_MDIO_DATA			0x00000204
 #define XGMAC_MDIO_C22P			0x00000220
drivers/net/ethernet/stmicro/stmmac/dwxgmac2_core.c +17 -1
index 09194b9d8322..d5bcbd6dfadf 100644
@@ -4,6 +4,7 @@
  * stmmac XGMAC support.
  */
 
+#include <linux/bitfield.h>
 #include <linux/bitrev.h>
 #include <linux/crc32.h>
 #include <linux/iopoll.h>
@@ -837,7 +838,8 @@ static void dwxgmac3_handle_dma_err(struct net_device *ndev,
 
 static int
 dwxgmac3_safety_feat_config(void __iomem *ioaddr, unsigned int asp,
-			    struct stmmac_safety_feature_cfg *safety_cfg)
+			    struct stmmac_safety_feature_cfg *safety_cfg,
+			    unsigned int csr_clk_mhz)
 {
 	u32 value;
 
@@ -873,6 +875,20 @@ dwxgmac3_safety_feat_config(void __iomem *ioaddr, unsigned int asp,
 	value |= XGMAC_TMOUTEN; /* FSM Timeout Feature */
 	writel(value, ioaddr + XGMAC_MAC_FSM_CONTROL);
 
+	/* Program the FSM active timer so the timeout checker actually fires.
+	 * TMR is the number of CSR clocks per 1us tic, i.e. csr_clk in MHz.
+	 * Use a 4s timeout.
+	 */
+	if (csr_clk_mhz) {
+		value = readl(ioaddr + XGMAC_MAC_FSM_ACT_TIMER);
+		value &= ~(XGMAC_MAC_FSM_ACT_TIMER_NTMRMD |
+			   XGMAC_MAC_FSM_ACT_TIMER_TMR);
+		value |= FIELD_PREP(XGMAC_MAC_FSM_ACT_TIMER_NTMRMD,
+				    STMMAC_NTMRMD_4SEC) |
+			 FIELD_PREP(XGMAC_MAC_FSM_ACT_TIMER_TMR, csr_clk_mhz);
+		writel(value, ioaddr + XGMAC_MAC_FSM_ACT_TIMER);
+	}
+
 	/* 5. Enable Data Path Parity Protection */
 	value = readl(ioaddr + XGMAC_MTL_DPP_CONTROL);
 	/* already enabled by default, explicit enable it again */
drivers/net/ethernet/stmicro/stmmac/hwif.h +2 -1
index 047bda5cbd92..6895ae21f918 100644
@@ -380,7 +380,8 @@ struct stmmac_ops {
 			     bool srgmi_ral);
 	/* Safety Features */
 	int (*safety_feat_config)(void __iomem *ioaddr, unsigned int asp,
-				  struct stmmac_safety_feature_cfg *safety_cfg);
+				  struct stmmac_safety_feature_cfg *safety_cfg,
+				  unsigned int csr_clk_mhz);
 	int (*safety_feat_irq_status)(struct net_device *ndev,
 			void __iomem *ioaddr, unsigned int asp,
 			struct stmmac_safety_stats *stats);
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +7 -1
index ca4c7fb11b73..4ee4952a1427 100644
@@ -3707,10 +3707,16 @@ static void stmmac_mtl_configuration(struct stmmac_priv *priv)
 
 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
 {
+	unsigned int csr_clk_mhz = 0;
+
 	if (priv->dma_cap.asp) {
 		netdev_info(priv->dev, "Enabling Safety Features\n");
+		if (priv->plat->stmmac_clk)
+			csr_clk_mhz = clk_get_rate(priv->plat->stmmac_clk) /
+				      1000000;
 		stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp,
-					  priv->plat->safety_feat_cfg);
+					  priv->plat->safety_feat_cfg,
+					  csr_clk_mhz);
 	} else {
 		netdev_info(priv->dev, "No Safety Features support found\n");
 	}
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

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

Critical: 0 · High: 1 · Medium: 1 · 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] stmmac_safety_feat_configuration() takes csr_clk_mhz only from clk_get_rate(priv->plat->stmmac_clk) / 1000000.
- [Medium] With NTMRMD now set to 4s, an FSM timeout raises a MAC safety error.
- [Low] The patch fixes a defect that users can see.

--

net: stmmac: program the FSM timeout timer

This patch programs MAC_FSM_ACT_Timer on dwmac5 and xgmac cores after TMOUTEN is enabled, so that the FSM timeout checker actually fires. TMR is derived from clk_get_rate(stmmac_clk) in MHz and NTMRMD is set to a 4s timeout. The safety_feat_config callback gains a csr_clk_mhz argument to carry the rate down.

Quoted 2 lines

The issue was hit in testing on dwmac4 and dwxgmac, where a stuck FSM was not reported through the safety interrupt despite TMOUTEN being set.

[Severity: Low]
Since this fixes a problem users can see, should it carry Fixes: tags and target net? Each core has its own origin commit.

The DWMAC5 path dates from:

Fixes 8bf993a5877e ("net: stmmac: Add support for DWMAC5 and implement Safety Features")

The XGMAC path dates from:

Fixes 56e58d6c8a56 ("net: stmmac: Implement Safety Features in XGMAC core")
Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +0 -0
index f1bb981cab7cd..37e70b82c8b56 100644

[ ... ]

Quoted 6 lines
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +2 -0
@@ -245,6 +247,19 @@ int dwmac5_safety_feat_config(void __iomem *ioaddr, unsigned int asp,
 		value |= TMOUTEN; /* FSM Timeout Feature */
 	writel(value, ioaddr + MAC_FSM_CONTROL);
 
+	/* Program the FSM active timer so the timeout checker actually fires.
+	 * TMR is the number of CSR clocks per 1us tic, i.e. csr_clk in MHz.

drivers/net/ethernet/stmicro/stmmac/dwmac5.c int dwmac5_safety_feat_config(void __iomem *ioaddr, unsigned int asp, · line 261

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +10 -0
+	 * Use a 4s timeout.
+	 */
+	if (safety_feat_cfg->tmouten && csr_clk_mhz) {
+		value = readl(ioaddr + MAC_FSM_ACT_TIMER);
+		value &= ~(MAC_FSM_ACT_TIMER_NTMRMD | MAC_FSM_ACT_TIMER_TMR);
+		value |= FIELD_PREP(MAC_FSM_ACT_TIMER_NTMRMD,
+				    STMMAC_NTMRMD_4SEC) |
+			 FIELD_PREP(MAC_FSM_ACT_TIMER_TMR, csr_clk_mhz);
+		writel(value, ioaddr + MAC_FSM_ACT_TIMER);
+	}

[Severity: Medium]
What happens once this 4s timer expires? Every MAC safety error is handled as uncorrectable in dwmac5_safety_feat_irq_status():

	corr = false;
	if (err) {
		dwmac5_handle_mac_err(ndev, ioaddr, corr, stats);
		ret |= !corr;

stmmac_safety_feat_interrupt() then does:

	if (ret && (ret != -EINVAL)) {
		stmmac_global_err(priv);

That turns the carrier off, sets STMMAC_RESET_REQUESTED and resets the whole controller. dwxgmac3_safety_feat_irq_status() takes the same path. Before this patch NTMRMD was 0, so this path never ran.

The commit message only says a stuck FSM will now be "reported". Should it mention that every timeout now causes a full controller reset?

Is 4s enough margin? The commit message says "a shorter timeout could wrongly report a flow-control stall as a stuck FSM". It only accounts for a single maximum-quanta PAUSE frame (about 3.36s at 10 Mb/s).

A congested link partner can keep sending fresh PAUSE frames before the quanta run out, so the transmitter stays paused indefinitely. If PAUSE stalls count toward the TX FSM activity timer, as the commit message implies, could back-pressure from a peer that lasts longer than about 4.19s cause repeated resets? Whether a fresh PAUSE frame restarts the timer is hardware behaviour that can't be confirmed from the code alone.

Was there a reason not to use one of the longer encodings (16s, 32s or 64s)?

On XGMAC, dwxgmac3_safety_feat_config() programs the timer whenever csr_clk_mhz is non-zero:

	if (csr_clk_mhz) {

It doesn't check safety_cfg->tmouten, so platforms have no way to opt out of the new timeout.

[ ... ]

Quoted 5 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +0 -0
index ca4c7fb11b73f..4ee4952a14273 100644

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static void stmmac_mtl_configuration(struct stmmac_priv *priv) · line 3716

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +5 -0
@@ -3707,10 +3707,16 @@ static void stmmac_mtl_configuration(struct stmmac_priv *priv)
 
 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
 {
+	unsigned int csr_clk_mhz = 0;
+
 	if (priv->dma_cap.asp) {
 		netdev_info(priv->dev, "Enabling Safety Features\n");
+		if (priv->plat->stmmac_clk)
+			csr_clk_mhz = clk_get_rate(priv->plat->stmmac_clk) /
+				      1000000;

[Severity: High]
What happens on platforms that request tmouten but have no stmmac_clk, or whose stmmac_clk runs below 1 MHz? In that case csr_clk_mhz stays 0.

dwmac5_safety_feat_config() still sets TMOUTEN, but skips the timer write without logging anything:

	if (safety_feat_cfg->tmouten && csr_clk_mhz) {

dwxgmac3_safety_feat_config() always sets XGMAC_TMOUTEN and also skips the write when csr_clk_mhz is 0.

At least two in-tree users that explicitly request the timeout checker appear to end up in this state.

stmmac_pci.c sets plat->safety_feat_cfg->tmouten = 1 and plat->clk_csr = STMMAC_CSR_250_300M for the GMAC5 PCI device, but it never assigns plat->stmmac_clk.

dwmac-mediatek.c also sets tmouten = 1 and probes through devm_stmmac_probe_config_dt(), which looks up the "stmmaceth" clock:

	plat->stmmac_clk = devm_clk_get(&pdev->dev,
					STMMAC_RESOURCE_NAME);
	if (IS_ERR(plat->stmmac_clk)) {
		dev_warn(&pdev->dev, "Cannot get CSR clock\n");
		plat->stmmac_clk = NULL;

The mediatek-dwmac binding lists axi, apb, mac_main, ptp_ref, rmii_internal and mac_cg as clock-names, with no "stmmaceth". As a result stmmac_clk ends up NULL.

On both platforms TMOUTEN is enabled while NTMRMD stays 0. That is the state the commit message describes as broken.

Could this fall back to plat->clk_csr? stmmac_mdio_bus_config() already treats plat->clk_csr >= 0 as the platform's CSR clock range. Failing that, could the driver warn, or leave TMOUTEN off when the timer can't be programmed?

Separately, stmmac_clk is not always the CSR clock. dwmac-intel registers it at the PTP reference rate, and include/linux/stmmac.h says it may be hclk_i, clk_csr_i, aclk_i or clk_app_i. Where it differs from the real CSR clock, TMR would be off by a factor of f_stmmac/f_csr, and the 4s timeout would scale by the same factor.

No in-tree platform was found that hits this case, since Intel sets tmouten = 0 and does not use XGMAC.

[ ... ]

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