Skip to contentSkip to conversation

net: stmmac: program the FSM timeout timer

PATCH Superseded Zxyan Zhu 3 participants 5 messages Nov 16 – Nov 18

Subsystems (4)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Zxyan Zhu

author
#0 lore raw

PATCH v3 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 defaulting to a 1s timeout. A platform can override the default through the new ntmrmd field in stmmac_safety_feature_cfg.

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.

---
v3:
- Make the MAC_FSM_ACT_Timer write a read-modify-write so the other fields (e.g. LTMRMD) are preserved.
- Warn when a valid CSR clock rate cannot be obtained.
- Document the NTMRMD encodings and fix 0x2 to 1ms (was 4ms).

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

 drivers/net/ethernet/stmicro/stmmac/dwmac5.c  | 22 +++++++++++++++++-
 drivers/net/ethernet/stmicro/stmmac/dwmac5.h  |  6 ++++-
 .../net/ethernet/stmicro/stmmac/dwxgmac2.h    |  3 +++
 .../ethernet/stmicro/stmmac/dwxgmac2_core.c   | 23 ++++++++++++++++++-
 drivers/net/ethernet/stmicro/stmmac/hwif.h    |  3 ++-
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 11 ++++++++-
 include/linux/stmmac.h                        | 17 ++++++++++++++
 7 files changed, 80 insertions(+), 5 deletions(-)
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +21 -1
index f1bb981cab7c..c1e3d634f153 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,24 @@ 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.
+	 * Default to a 1s timeout when the platform did not select one.
+	 */
+	if (safety_feat_cfg->tmouten && csr_clk_mhz) {
+		u32 ntmrmd = safety_feat_cfg->ntmrmd;
+
+		ntmrmd = (ntmrmd >= STMMAC_NTMRMD_1US &&
+			  ntmrmd <= STMMAC_NTMRMD_64SEC) ?
+			 ntmrmd : STMMAC_NTMRMD_1SEC;
+
+		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, ntmrmd) |
+			 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 +22 -1
index 7ad08651675e..b3f99a6dfcd7 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,25 @@ 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.
+	 * Default to a 1s timeout when the platform did not select one.
+	 */
+	if (safety_cfg && safety_cfg->tmouten && csr_clk_mhz) {
+		u32 ntmrmd = safety_cfg->ntmrmd;
+
+		ntmrmd = (ntmrmd >= STMMAC_NTMRMD_1US &&
+			  ntmrmd <= STMMAC_NTMRMD_64SEC) ?
+			 ntmrmd : STMMAC_NTMRMD_1SEC;
+
+		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, ntmrmd) |
+			 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 8ae95e91a36a..395655b01ea8 100644
@@ -378,7 +378,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 +10 -1
index 552840d29668..949ed3e5bb05 100644
@@ -3691,10 +3691,19 @@ 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;
+		if (!csr_clk_mhz)
+			netdev_warn(priv->dev,
+				    "unable to get a valid CSR clock rate\n");
 		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");
 	}
include/linux/stmmac.h +17 -0
index 00be2df63d22..35c39cd6b630 100644
@@ -168,8 +168,25 @@ struct stmmac_safety_feature_cfg {
 	u32 edpp;
 	u32 prtyen;
 	u32 tmouten;
+	/* FSM normal-mode timeout value (MAC_FSM_ACT_Timer.NTMRMD). The TMR
+	 * field (CSR clocks per 1us) is derived from the CSR clock by stmmac.
+	 * 0 selects the default 1s timeout.
+	 */
+	u32 ntmrmd;
 };
 
+/* 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
+
 /* Addresses that may be customized by a platform */
 struct dwmac4_addrs {
 	u32 dma_chan;
Signature · 1 line
2.34.1
#1 lore raw

Hi,

Quoted 198 lines · Zxyan Zhu

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 defaulting to a 1s timeout. A platform can override the default through the new ntmrmd field in stmmac_safety_feature_cfg.

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.

---
v3:
- Make the MAC_FSM_ACT_Timer write a read-modify-write so the other fields (e.g. LTMRMD) are preserved.
- Warn when a valid CSR clock rate cannot be obtained.
- Document the NTMRMD encodings and fix 0x2 to 1ms (was 4ms).

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

 drivers/net/ethernet/stmicro/stmmac/dwmac5.c  | 22 +++++++++++++++++-
 drivers/net/ethernet/stmicro/stmmac/dwmac5.h  |  6 ++++-
 .../net/ethernet/stmicro/stmmac/dwxgmac2.h    |  3 +++
 .../ethernet/stmicro/stmmac/dwxgmac2_core.c   | 23 ++++++++++++++++++-
 drivers/net/ethernet/stmicro/stmmac/hwif.h    |  3 ++-
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 11 ++++++++-
 include/linux/stmmac.h                        | 17 ++++++++++++++
 7 files changed, 80 insertions(+), 5 deletions(-)
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +21 -1
index f1bb981cab7c..c1e3d634f153 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,24 @@ 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.
+	 * Default to a 1s timeout when the platform did not select one.
+	 */
+	if (safety_feat_cfg->tmouten && csr_clk_mhz) {
+		u32 ntmrmd = safety_feat_cfg->ntmrmd;
+
+		ntmrmd = (ntmrmd >= STMMAC_NTMRMD_1US &&
+			  ntmrmd <= STMMAC_NTMRMD_64SEC) ?
+			 ntmrmd : STMMAC_NTMRMD_1SEC;
+
+		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, ntmrmd) |
+			 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 +22 -1
index 7ad08651675e..b3f99a6dfcd7 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,25 @@ 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.
+	 * Default to a 1s timeout when the platform did not select one.
+	 */
+	if (safety_cfg && safety_cfg->tmouten && csr_clk_mhz) {
+		u32 ntmrmd = safety_cfg->ntmrmd;
+
+		ntmrmd = (ntmrmd >= STMMAC_NTMRMD_1US &&
+			  ntmrmd <= STMMAC_NTMRMD_64SEC) ?
+			 ntmrmd : STMMAC_NTMRMD_1SEC;
+
+		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, ntmrmd) |
+			 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 8ae95e91a36a..395655b01ea8 100644
@@ -378,7 +378,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 +0 -0
index 552840d29668..949ed3e5bb05 100644
@@ -3691,10 +3691,19 @@ static void stmmac_mtl_configuration(struct stmmac_priv *priv)
 
 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
 {

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

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +8 -0
+	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;
+		if (!csr_clk_mhz)
+			netdev_warn(priv->dev,
+				    "unable to get a valid CSR clock rate\n");

I know it's the bot who asked for that, but I don't agree with this log here.

FWIU it's legit to have ASP but no CSR clock (e.g. stmmac PCI)

Quoted 12 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2 -1
 		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");
 	}
include/linux/stmmac.h +0 -0
index 00be2df63d22..35c39cd6b630 100644

include/linux/stmmac.h struct stmmac_safety_feature_cfg { · line 174

Quoted 7 lines
include/linux/stmmac.h +4 -0
@@ -168,8 +168,25 @@ struct stmmac_safety_feature_cfg {
 	u32 edpp;
 	u32 prtyen;
 	u32 tmouten;
+	/* FSM normal-mode timeout value (MAC_FSM_ACT_Timer.NTMRMD). The TMR
+	 * field (CSR clocks per 1us) is derived from the CSR clock by stmmac.
+	 * 0 selects the default 1s timeout.
+	 */

I'd rather this comment to be with the register value definitions :)

include/linux/stmmac.h struct stmmac_safety_feature_cfg { · line 175

Quoted 1 line
include/linux/stmmac.h +1 -0
+	u32 ntmrmd;

I don't see any user for ntmrmd. If this is prep work for upcoming glue drivers, then this should come with the driver in question. If really needed, can't we just program a reasonable hardcoded default for the time being ?

Maxime

author
#2 lore raw

Hi Maxime,

Quoted 215 lines · Maxime Chevallier

Hi,

Quoted 198 lines · Zxyan Zhu

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 defaulting to a 1s timeout. A platform can override the default through the new ntmrmd field in stmmac_safety_feature_cfg.

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.

---
v3:
- Make the MAC_FSM_ACT_Timer write a read-modify-write so the other fields (e.g. LTMRMD) are preserved.
- Warn when a valid CSR clock rate cannot be obtained.
- Document the NTMRMD encodings and fix 0x2 to 1ms (was 4ms).

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

 drivers/net/ethernet/stmicro/stmmac/dwmac5.c  | 22 +++++++++++++++++-
 drivers/net/ethernet/stmicro/stmmac/dwmac5.h  |  6 ++++-
 .../net/ethernet/stmicro/stmmac/dwxgmac2.h    |  3 +++
 .../ethernet/stmicro/stmmac/dwxgmac2_core.c   | 23 ++++++++++++++++++-
 drivers/net/ethernet/stmicro/stmmac/hwif.h    |  3 ++-
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 11 ++++++++-
 include/linux/stmmac.h                        | 17 ++++++++++++++
 7 files changed, 80 insertions(+), 5 deletions(-)
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +21 -1
index f1bb981cab7c..c1e3d634f153 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,24 @@ 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.
+      * Default to a 1s timeout when the platform did not select one.
+      */
+     if (safety_feat_cfg->tmouten && csr_clk_mhz) {
+             u32 ntmrmd = safety_feat_cfg->ntmrmd;
+
+             ntmrmd = (ntmrmd >= STMMAC_NTMRMD_1US &&
+                       ntmrmd <= STMMAC_NTMRMD_64SEC) ?
+                      ntmrmd : STMMAC_NTMRMD_1SEC;
+
+             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, ntmrmd) |
+                      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 +22 -1
index 7ad08651675e..b3f99a6dfcd7 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,25 @@ 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.
+      * Default to a 1s timeout when the platform did not select one.
+      */
+     if (safety_cfg && safety_cfg->tmouten && csr_clk_mhz) {
+             u32 ntmrmd = safety_cfg->ntmrmd;
+
+             ntmrmd = (ntmrmd >= STMMAC_NTMRMD_1US &&
+                       ntmrmd <= STMMAC_NTMRMD_64SEC) ?
+                      ntmrmd : STMMAC_NTMRMD_1SEC;
+
+             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, ntmrmd) |
+                      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 8ae95e91a36a..395655b01ea8 100644
@@ -378,7 +378,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 +0 -0
index 552840d29668..949ed3e5bb05 100644
@@ -3691,10 +3691,19 @@ static void stmmac_mtl_configuration(struct stmmac_priv *priv)
 
 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
 {

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

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +8 -0
+     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;
+             if (!csr_clk_mhz)
+                     netdev_warn(priv->dev,
+                                 "unable to get a valid CSR clock rate\n");

I know it's the bot who asked for that, but I don't agree with this log here.

Quoted 1 line

FWIU it's legit to have ASP but no CSR clock (e.g. stmmac PCI)

Agreed, I'll drop the warning.

Quoted 23 lines
Quoted 13 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2 -1
              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");
      }
include/linux/stmmac.h +0 -0
index 00be2df63d22..35c39cd6b630 100644

include/linux/stmmac.h struct stmmac_safety_feature_cfg { · line 174

Quoted 7 lines
include/linux/stmmac.h +4 -0
@@ -168,8 +168,25 @@ struct stmmac_safety_feature_cfg {
      u32 edpp;
      u32 prtyen;
      u32 tmouten;
+     /* FSM normal-mode timeout value (MAC_FSM_ACT_Timer.NTMRMD). The TMR
+      * field (CSR clocks per 1us) is derived from the CSR clock by stmmac.
+      * 0 selects the default 1s timeout.
+      */

I'd rather this comment to be with the register value definitions :)

Quoted 6 lines

include/linux/stmmac.h struct stmmac_safety_feature_cfg { · line 175

Quoted 1 line
include/linux/stmmac.h +1 -0
+     u32 ntmrmd;

I don't see any user for ntmrmd. If this is prep work for upcoming glue drivers, then this should come with the driver in question. If really needed, can't we just program a reasonable hardcoded default for the time being ?

Right, ntmrmd was indeed added for upcoming glue drivers and has no in-tree user yet. I'll drop the field and just program a hardcoded default for now.

What default would you prefer? The databook doesn't suggest a particular value for NTMRMD. 1s (NTMRMD = 0x6) is my choice because FSM transactions complete in microseconds, so 1s is comfortably above that to avoid false timeouts, and it sits in the middle of the supported range. I'm happy to use whatever you deem appropriate.

Quoted 2 lines

Maxime

Thanks,
Zxyan

#3 lore raw

Hi,

Quoted 11 lines · zhu xin

Hi Maxime,

Right, ntmrmd was indeed added for upcoming glue drivers and has no in-tree user yet. I'll drop the field and just program a hardcoded default for now.

What default would you prefer? The databook doesn't suggest a particular value for NTMRMD. 1s (NTMRMD = 0x6) is my choice because FSM transactions complete in microseconds, so 1s is comfortably above that to avoid false timeouts, and it sits in the middle of the supported range. I'm happy to use whatever you deem appropriate.

1s seems reasonable to me :) let's go with that,

Thanks,

Maxime

#4 lore raw
In reply to Zxyan Zhu
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider.

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

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

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 FSM timer fix does nothing on any in-tree XGMAC device.
- [Medium] On dwmac5-class cores, the patch arms a watchdog by default, and when it fires the driver resets the whole device.
- [Medium] The value written to TMR (CSR clocks per 1us) comes only from clk_get_rate(plat->stmmac_clk) (stmmac_main.c:3698-3700).
- [Low] The commit is presented as a bug fix ('the advertised timeout protection is ineffective', 'The issue was hit in testing on dwmac4…
- [Low] stmmac_safety_feat_configuration() logs 'unable to get a valid CSR clock rate' whenever dma_cap.asp is non-zero and csr_clk_mhz is 0…

--

net: stmmac: program the FSM timeout timer

This patch programs MAC_FSM_ACT_Timer after TMOUTEN is enabled on dwmac5 and XGMAC cores. TMR is derived from the CSR clock rate and NTMRMD defaults to a 1s timeout. A csr_clk_mhz argument is added to the safety_feat_config callback, and an ntmrmd field is added to struct stmmac_safety_feature_cfg so platforms can override the default.

Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +0 -0
index f1bb981cab7cd..c1e3d634f1530 100644

[ ... ]

Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +0 -0
@@ -245,6 +247,24 @@ int dwmac5_safety_feat_config(void __iomem *ioaddr, unsigned int asp,
 		value |= TMOUTEN; /* FSM Timeout Feature */
 	writel(value, ioaddr + MAC_FSM_CONTROL);
 

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

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +10 -0
+	/* 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.
+	 * Default to a 1s timeout when the platform did not select one.
+	 */
+	if (safety_feat_cfg->tmouten && csr_clk_mhz) {
+		u32 ntmrmd = safety_feat_cfg->ntmrmd;
+
+		ntmrmd = (ntmrmd >= STMMAC_NTMRMD_1US &&
+			  ntmrmd <= STMMAC_NTMRMD_64SEC) ?
+			 ntmrmd : STMMAC_NTMRMD_1SEC;

[Severity: Medium]
If the platform passes no safety_feat_cfg, dwmac5_safety_feat_config() uses all_safety_feats instead, which has tmouten = 1 and ntmrmd = 0. With this change, ntmrmd = 0 becomes STMMAC_NTMRMD_1SEC. So every dwmac5 platform with asp >= 2 and a CSR clock now has a 1s FSM timeout armed by default. dwmac-mediatek, which sets tmouten = 1, is affected too.

Before this patch the checker never fired. Now an FSM timeout is reported through MCSIS, and dwmac5_safety_feat_irq_status() always treats it as uncorrectable:

	err = (mtl & MCSIS) || (dma & MCSIS);
	corr = false;
	if (err) {
		dwmac5_handle_mac_err(ndev, ioaddr, corr, stats);
		ret |= !corr;
	}

stmmac_safety_feat_interrupt() then turns that into a full device reset:

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

Is a 1s default safe for FSMs that can legitimately stay active longer? One example is TX held off by repeated PAUSE frames at 10 Mbit/s, where a single maximum pause quantum is about 3.4s.

The commit message only says a stuck FSM is now reported. Could it also say that a timeout now triggers stmmac_global_err()? Could it explain why only NTMRMD is programmed and LTMRMD keeps its previous value?

Quoted 8 lines
drivers/net/ethernet/stmicro/stmmac/dwmac5.c +8 -0
+
+		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, ntmrmd) |
+			 FIELD_PREP(MAC_FSM_ACT_TIMER_TMR, csr_clk_mhz);
+		writel(value, ioaddr + MAC_FSM_ACT_TIMER);
+	}
+

[ ... ]

Quoted 4 lines
drivers/net/ethernet/stmicro/stmmac/dwxgmac2_core.c +0 -0
index 7ad08651675e8..b3f99a6dfcd72 100644

[ ... ]

drivers/net/ethernet/stmicro/stmmac/dwxgmac2_core.c dwxgmac3_safety_feat_config(void __iomem *ioaddr, unsigned int asp, · line 882

Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/dwxgmac2_core.c +5 -0
@@ -873,6 +875,25 @@ 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.
+	 * Default to a 1s timeout when the platform did not select one.
+	 */
+	if (safety_cfg && safety_cfg->tmouten && csr_clk_mhz) {

[Severity: High]
Does this block ever run on an in-tree XGMAC device?

XGMAC_TMOUTEN is set unconditionally just above, but the timer write requires a non-NULL safety_cfg. dwmac5_safety_feat_config() has a NULL fallback:

	if (!safety_feat_cfg)
		safety_feat_cfg = &all_safety_feats;

dwxgmac3_safety_feat_config() has no such fallback.

As far as I can see, plat->safety_feat_cfg is only allocated by dwmac-intel.c, dwmac-mediatek.c and stmmac_pci.c, and all of those are GMAC4 cores. The XGMAC users never set it: dwmac-socfpga.c, dwmac-tegra.c and the generic "snps,dwxgmac" binding in stmmac_platform.c. stmmac_safety_feat_configuration() passes the pointer through unchanged.

Both dwxgmac210_ops and dwxlgmac2_ops use this function, so the path is:

stmmac_hw_setup()
stmmac_safety_feat_configuration()
dwxgmac3_safety_feat_config(safety_cfg == NULL)
XGMAC_TMOUTEN set
timer block skipped

That leaves TMOUTEN on and NTMRMD at its reset value, which is the state the commit message calls the bug. Yet the commit message says the problem was hit on dwxgmac.

Two comments also don't match XGMAC behaviour: "Default to a 1s timeout when the platform did not select one" here, and "0 selects the default 1s timeout" in include/linux/stmmac.h.

Would it work to copy the dwmac5 NULL fallback here? Another option is to gate the timer write the same way TMOUTEN is gated.

Quoted 5 lines
drivers/net/ethernet/stmicro/stmmac/dwxgmac2_core.c +5 -0
+		u32 ntmrmd = safety_cfg->ntmrmd;
+
+		ntmrmd = (ntmrmd >= STMMAC_NTMRMD_1US &&
+			  ntmrmd <= STMMAC_NTMRMD_64SEC) ?
+			 ntmrmd : STMMAC_NTMRMD_1SEC;

[ ... ]

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

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

Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +5 -0
@@ -3691,10 +3691,19 @@ 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: Medium]
TMR is derived only from plat->stmmac_clk here. Elsewhere the driver treats a fixed plat->clk_csr as authoritative, as in stmmac_mdio_bus_config():

	/* If a specific clk_csr value is passed from the platform, this means
	 * that the CSR Clock Range value should not be computed from the CSR
	 * clock.
	 */
	if (priv->plat->clk_csr >= 0)

Does this leave the timer unprogrammed on the Synopsys GMAC5 PCI platform? In stmmac_pci.c, snps_gmac5_default_data() sets clk_csr = STMMAC_CSR_250_300M with core_type GMAC4, and stmmac_pci_probe() sets:

	plat->safety_feat_cfg->tmouten = 1;

Nothing assigns stmmac_clk for PCI devices, so csr_clk_mhz is 0. dwmac5_safety_feat_config() then sets TMOUTEN and skips the timer block because of the "safety_feat_cfg->tmouten && csr_clk_mhz" check. That is the state this patch is meant to fix, on a platform that explicitly asks for the timeout.

There is also a latent problem on Intel mGbE. intel_mgbe_common_data() registers stmmac_clk as a fixed-rate clock at the PTP rate:

	plat->stmmac_clk = clk_register_fixed_rate(&pdev->dev,
						   clk_name, NULL, 0,
						   plat->clk_ptp_rate);

clk_csr is set separately, to STMMAC_CSR_20_35M or STMMAC_CSR_250_300M. Intel currently sets tmouten = 0. If it ever enabled tmouten, would TMR get the PTP rate instead of the CSR clock rate?

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

Quoted 3 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +3 -0
+		if (!csr_clk_mhz)
+			netdev_warn(priv->dev,
+				    "unable to get a valid CSR clock rate\n");

[Severity: Low]
Can this warning show up on platforms that never use the rate? It fires whenever dma_cap.asp is non-zero and csr_clk_mhz is 0. The callees only use the rate when they program the FSM timer:

- dwmac5: asp > 1 and tmouten set
- XGMAC: asp not 2 or 4, and a non-NULL cfg with tmouten set

stmmac_clk is optional. stmmac_probe_config_dt() does:

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

stmmac_pci.c never sets stmmac_clk at all. So dwmac5 with asp == 1, configurations with tmouten = 0, and the XGMAC platforms would print this warning on every stmmac_hw_setup() call (open, resume, reinit).

Could the warning be limited to the cases where the timer is actually programmed?

Quoted 14 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2 -1
 		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");
 	}
include/linux/stmmac.h +0 -0
index 00be2df63d220..35c39cd6b6307 100644
@@ -168,8 +168,25 @@ struct stmmac_safety_feature_cfg {
 	u32 edpp;
 	u32 prtyen;

include/linux/stmmac.h struct stmmac_safety_feature_cfg { · line 179

Quoted 10 lines
include/linux/stmmac.h +7 -0
 	u32 tmouten;
+	/* FSM normal-mode timeout value (MAC_FSM_ACT_Timer.NTMRMD). The TMR
+	 * field (CSR clocks per 1us) is derived from the CSR clock by stmmac.
+	 * 0 selects the default 1s timeout.
+	 */
+	u32 ntmrmd;
 };
 
+/* MAC_FSM_ACT_Timer.NTMRMD timeout durations */
+#define STMMAC_NTMRMD_1US		0x1

[Severity: Low]
This isn't a bug, but no in-tree driver assigns ->ntmrmd. So the new field and the STMMAC_NTMRMD_* constants have no in-tree users, and netdev usually doesn't accept interfaces without in-tree users.

The commit is also presented as a fix ("the advertised timeout protection is ineffective", "The issue was hit in testing on dwmac4 and dwxgmac"). There is no Fixes: tag pointing at the commits that added dwmac5/XGMAC safety feature support.

Would it make sense to split the fix from the new platform knob and add a Fixes: tag?

Quoted 2 lines
include/linux/stmmac.h +2 -0
+#define STMMAC_NTMRMD_1MS		0x2
+#define STMMAC_NTMRMD_16MS		0x3

[ ... ]

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