Skip to contentSkip to conversation

net: dsa: mt7530: modernize MIB handling + fix

PATCH Applied Christian Marangi 3 participants 10 messages Apr 10, 2025 – Jun 28, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover net: dsa: mt7530: modernize MIB handling + fix

This small series modernize MIB handling for MT7530 and also implement .get_stats64.

It was reported that kernel and Switch MIB desync in scenario where a packet is forwarded from a port to another. In such case, the forwarding is offloaded and the kernel is not aware of the transmitted packet. To handle this, read the counter directly from Switch registers.

Christian Marangi (6):
net: dsa: mt7530: generalize read port stats logic
net: dsa: mt7530: move pkt size and rx err MIB counter to rmon stats
API
net: dsa: mt7530: move pause MIB counter to eth_ctrl stats API
net: dsa: mt7530: move pkt stats and err MIB counter to eth_mac stats
API
net: dsa: mt7530: move remaining MIB counter to define
net: dsa: mt7530: implement .get_stats64

 drivers/net/dsa/mt7530.c | 246 +++++++++++++++++++++++++++++++--------
 drivers/net/dsa/mt7530.h |  42 +++++++
 2 files changed, 239 insertions(+), 49 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/6 net: dsa: mt7530: generalize read port stats logic

In preparation for migration to use of standard MIB API, generalize the read port stats logic to a dedicated function.

This will permit to manually provide the offset and size of the MIB counter to directly access specific counter.

---
 drivers/net/dsa/mt7530.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
drivers/net/dsa/mt7530.c +18 -8
index d70399bce5b9..85a040853194 100644
@@ -789,24 +789,34 @@ mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset,
 		ethtool_puts(&data, mt7530_mib[i].name);
 }
 
+static void
+mt7530_read_port_stats(struct mt7530_priv *priv, int port,
+		       u32 offset, u8 size, uint64_t *data)
+{
+	u32 val, reg = MT7530_PORT_MIB_COUNTER(port) + offset;
+
+	val = mt7530_read(priv, reg);
+	*data = val;
+
+	if (size == 2) {
+		val = mt7530_read(priv, reg + 4);
+		*data |= (u64)val << 32;
+	}
+}
+
 static void
 mt7530_get_ethtool_stats(struct dsa_switch *ds, int port,
 			 uint64_t *data)
 {
 	struct mt7530_priv *priv = ds->priv;
 	const struct mt7530_mib_desc *mib;
-	u32 reg, i;
-	u64 hi;
+	int i;
 
 	for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) {
 		mib = &mt7530_mib[i];
-		reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset;
 
-		data[i] = mt7530_read(priv, reg);
-		if (mib->size == 2) {
-			hi = mt7530_read(priv, reg + 4);
-			data[i] |= hi << 32;
-		}
+		mt7530_read_port_stats(priv, port, mib->offset, mib->size,
+				       data + i);
 	}
 }
 
Signature · 1 line
2.48.1
#2 lore raw

PATCH 2/6 net: dsa: mt7530: move pkt size and rx err MIB counter to rmon stats API

In reply to Christian Marangi

Drop custom handling of packet size and RX error MIB counter and handle them in the standard .get_rmon_stats API

The MIB entry are dropped from the custom MIB table and converted to a define providing only the MIB offset.

---
 drivers/net/dsa/mt7530.c | 71 +++++++++++++++++++++++++++++++---------
 drivers/net/dsa/mt7530.h | 17 ++++++++++
 2 files changed, 72 insertions(+), 16 deletions(-)
drivers/net/dsa/mt7530.c +55 -16
index 85a040853194..54a6ddc380e9 100644
@@ -44,12 +44,6 @@ static const struct mt7530_mib_desc mt7530_mib[] = {
 	MIB_DESC(1, 0x24, "TxLateCollision"),
 	MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
 	MIB_DESC(1, 0x2c, "TxPause"),
-	MIB_DESC(1, 0x30, "TxPktSz64"),
-	MIB_DESC(1, 0x34, "TxPktSz65To127"),
-	MIB_DESC(1, 0x38, "TxPktSz128To255"),
-	MIB_DESC(1, 0x3c, "TxPktSz256To511"),
-	MIB_DESC(1, 0x40, "TxPktSz512To1023"),
-	MIB_DESC(1, 0x44, "Tx1024ToMax"),
 	MIB_DESC(2, 0x48, "TxBytes"),
 	MIB_DESC(1, 0x60, "RxDrop"),
 	MIB_DESC(1, 0x64, "RxFiltering"),
@@ -58,17 +52,7 @@ static const struct mt7530_mib_desc mt7530_mib[] = {
 	MIB_DESC(1, 0x70, "RxBroadcast"),
 	MIB_DESC(1, 0x74, "RxAlignErr"),
 	MIB_DESC(1, 0x78, "RxCrcErr"),
-	MIB_DESC(1, 0x7c, "RxUnderSizeErr"),
-	MIB_DESC(1, 0x80, "RxFragErr"),
-	MIB_DESC(1, 0x84, "RxOverSzErr"),
-	MIB_DESC(1, 0x88, "RxJabberErr"),
 	MIB_DESC(1, 0x8c, "RxPause"),
-	MIB_DESC(1, 0x90, "RxPktSz64"),
-	MIB_DESC(1, 0x94, "RxPktSz65To127"),
-	MIB_DESC(1, 0x98, "RxPktSz128To255"),
-	MIB_DESC(1, 0x9c, "RxPktSz256To511"),
-	MIB_DESC(1, 0xa0, "RxPktSz512To1023"),
-	MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"),
 	MIB_DESC(2, 0xa8, "RxBytes"),
 	MIB_DESC(1, 0xb0, "RxCtrlDrop"),
 	MIB_DESC(1, 0xb4, "RxIngressDrop"),
@@ -829,6 +813,60 @@ mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
 	return ARRAY_SIZE(mt7530_mib);
 }
 
+static const struct ethtool_rmon_hist_range mt7530_rmon_ranges[] = {
+	{ 0, 64 },
+	{ 65, 127 },
+	{ 128, 255 },
+	{ 256, 511 },
+	{ 512, 1023 },
+	{ 1024, MT7530_MAX_MTU },
+	{}
+};
+
+static void mt7530_get_rmon_stats(struct dsa_switch *ds, int port,
+				  struct ethtool_rmon_stats *rmon_stats,
+				  const struct ethtool_rmon_hist_range **ranges)
+{
+	struct mt7530_priv *priv = ds->priv;
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_UNDER_SIZE_ERR, 1,
+			       &rmon_stats->undersize_pkts);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_OVER_SZ_ERR, 1,
+			       &rmon_stats->oversize_pkts);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_FRAG_ERR, 1,
+			       &rmon_stats->fragments);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_JABBER_ERR, 1,
+			       &rmon_stats->jabbers);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_PKT_SZ_64, 1,
+			       &rmon_stats->hist[0]);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_PKT_SZ_65_TO_127, 1,
+			       &rmon_stats->hist[1]);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_PKT_SZ_128_TO_255, 1,
+			       &rmon_stats->hist[2]);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_PKT_SZ_256_TO_511, 1,
+			       &rmon_stats->hist[3]);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_PKT_SZ_512_TO_1023, 1,
+			       &rmon_stats->hist[4]);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_PKT_SZ_1024_TO_MAX, 1,
+			       &rmon_stats->hist[5]);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_PKT_SZ_64, 1,
+			       &rmon_stats->hist_tx[0]);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_PKT_SZ_65_TO_127, 1,
+			       &rmon_stats->hist_tx[1]);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_PKT_SZ_128_TO_255, 1,
+			       &rmon_stats->hist_tx[2]);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_PKT_SZ_256_TO_511, 1,
+			       &rmon_stats->hist_tx[3]);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_PKT_SZ_512_TO_1023, 1,
+			       &rmon_stats->hist_tx[4]);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_PKT_SZ_1024_TO_MAX, 1,
+			       &rmon_stats->hist_tx[5]);
+
+	*ranges = mt7530_rmon_ranges;
+}
+
 static int
 mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
 {
@@ -3115,6 +3153,7 @@ const struct dsa_switch_ops mt7530_switch_ops = {
 	.get_strings		= mt7530_get_strings,
 	.get_ethtool_stats	= mt7530_get_ethtool_stats,
 	.get_sset_count		= mt7530_get_sset_count,
+	.get_rmon_stats		= mt7530_get_rmon_stats,
 	.set_ageing_time	= mt7530_set_ageing_time,
 	.port_enable		= mt7530_port_enable,
 	.port_disable		= mt7530_port_disable,
drivers/net/dsa/mt7530.h +17 -0
index c3ea403d7acf..9bc90d1678f7 100644
@@ -423,6 +423,23 @@ enum mt7530_vlan_port_acc_frm {
 
 /* Register for MIB */
 #define MT7530_PORT_MIB_COUNTER(x)	(0x4000 + (x) * 0x100)
+/* Each define is an offset of MT7530_PORT_MIB_COUNTER */
+#define   MT7530_PORT_MIB_TX_PKT_SZ_64	0x30
+#define   MT7530_PORT_MIB_TX_PKT_SZ_65_TO_127 0x34
+#define   MT7530_PORT_MIB_TX_PKT_SZ_128_TO_255 0x38
+#define   MT7530_PORT_MIB_TX_PKT_SZ_256_TO_511 0x3c
+#define   MT7530_PORT_MIB_TX_PKT_SZ_512_TO_1023 0x40
+#define   MT7530_PORT_MIB_TX_PKT_SZ_1024_TO_MAX 0x44
+#define   MT7530_PORT_MIB_RX_UNDER_SIZE_ERR 0x7c
+#define   MT7530_PORT_MIB_RX_FRAG_ERR	0x80
+#define   MT7530_PORT_MIB_RX_OVER_SZ_ERR 0x84
+#define   MT7530_PORT_MIB_RX_JABBER_ERR	0x88
+#define   MT7530_PORT_MIB_RX_PKT_SZ_64	0x90
+#define   MT7530_PORT_MIB_RX_PKT_SZ_65_TO_127 0x94
+#define   MT7530_PORT_MIB_RX_PKT_SZ_128_TO_255 0x98
+#define   MT7530_PORT_MIB_RX_PKT_SZ_256_TO_511 0x9c
+#define   MT7530_PORT_MIB_RX_PKT_SZ_512_TO_1023 0xa0
+#define   MT7530_PORT_MIB_RX_PKT_SZ_1024_TO_MAX 0xa4
 #define MT7530_MIB_CCR			0x4fe0
 #define  CCR_MIB_ENABLE			BIT(31)
 #define  CCR_RX_OCT_CNT_GOOD		BIT(7)
Signature · 1 line
2.48.1
#3 lore raw

PATCH 3/6 net: dsa: mt7530: move pause MIB counter to eth_ctrl stats API

In reply to Christian Marangi

Drop custom handling of TX/RX pause frame MIB counter and handle them in the standard .get_eth_ctrl_stats API

The MIB entry are dropped from the custom MIB table and converted to a define providing only the MIB offset.

---
 drivers/net/dsa/mt7530.c | 15 +++++++++++++--
 drivers/net/dsa/mt7530.h |  2 ++
 2 files changed, 15 insertions(+), 2 deletions(-)
drivers/net/dsa/mt7530.c +13 -2
index 54a6ddc380e9..f183a604355e 100644
@@ -43,7 +43,6 @@ static const struct mt7530_mib_desc mt7530_mib[] = {
 	MIB_DESC(1, 0x20, "TxDeferred"),
 	MIB_DESC(1, 0x24, "TxLateCollision"),
 	MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
-	MIB_DESC(1, 0x2c, "TxPause"),
 	MIB_DESC(2, 0x48, "TxBytes"),
 	MIB_DESC(1, 0x60, "RxDrop"),
 	MIB_DESC(1, 0x64, "RxFiltering"),
@@ -52,7 +51,6 @@ static const struct mt7530_mib_desc mt7530_mib[] = {
 	MIB_DESC(1, 0x70, "RxBroadcast"),
 	MIB_DESC(1, 0x74, "RxAlignErr"),
 	MIB_DESC(1, 0x78, "RxCrcErr"),
-	MIB_DESC(1, 0x8c, "RxPause"),
 	MIB_DESC(2, 0xa8, "RxBytes"),
 	MIB_DESC(1, 0xb0, "RxCtrlDrop"),
 	MIB_DESC(1, 0xb4, "RxIngressDrop"),
@@ -867,6 +865,18 @@ static void mt7530_get_rmon_stats(struct dsa_switch *ds, int port,
 	*ranges = mt7530_rmon_ranges;
 }
 
+static void mt7530_get_eth_ctrl_stats(struct dsa_switch *ds, int port,
+				      struct ethtool_eth_ctrl_stats *ctrl_stats)
+{
+	struct mt7530_priv *priv = ds->priv;
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_PAUSE, 1,
+			       &ctrl_stats->MACControlFramesTransmitted);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_PAUSE, 1,
+			       &ctrl_stats->MACControlFramesReceived);
+}
+
 static int
 mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
 {
@@ -3154,6 +3164,7 @@ const struct dsa_switch_ops mt7530_switch_ops = {
 	.get_ethtool_stats	= mt7530_get_ethtool_stats,
 	.get_sset_count		= mt7530_get_sset_count,
 	.get_rmon_stats		= mt7530_get_rmon_stats,
+	.get_eth_ctrl_stats	= mt7530_get_eth_ctrl_stats,
 	.set_ageing_time	= mt7530_set_ageing_time,
 	.port_enable		= mt7530_port_enable,
 	.port_disable		= mt7530_port_disable,
drivers/net/dsa/mt7530.h +2 -0
index 9bc90d1678f7..a651ad29b750 100644
@@ -424,6 +424,7 @@ enum mt7530_vlan_port_acc_frm {
 /* Register for MIB */
 #define MT7530_PORT_MIB_COUNTER(x)	(0x4000 + (x) * 0x100)
 /* Each define is an offset of MT7530_PORT_MIB_COUNTER */
+#define   MT7530_PORT_MIB_TX_PAUSE	0x2c
 #define   MT7530_PORT_MIB_TX_PKT_SZ_64	0x30
 #define   MT7530_PORT_MIB_TX_PKT_SZ_65_TO_127 0x34
 #define   MT7530_PORT_MIB_TX_PKT_SZ_128_TO_255 0x38
@@ -434,6 +435,7 @@ enum mt7530_vlan_port_acc_frm {
 #define   MT7530_PORT_MIB_RX_FRAG_ERR	0x80
 #define   MT7530_PORT_MIB_RX_OVER_SZ_ERR 0x84
 #define   MT7530_PORT_MIB_RX_JABBER_ERR	0x88
+#define   MT7530_PORT_MIB_RX_PAUSE	0x8c
 #define   MT7530_PORT_MIB_RX_PKT_SZ_64	0x90
 #define   MT7530_PORT_MIB_RX_PKT_SZ_65_TO_127 0x94
 #define   MT7530_PORT_MIB_RX_PKT_SZ_128_TO_255 0x98
Signature · 1 line
2.48.1
#4 lore raw

PATCH 4/6 net: dsa: mt7530: move pkt stats and err MIB counter to eth_mac stats API

In reply to Christian Marangi

Drop custom handling of TX/RX packet stats and error MIB counter and handle them in the standard .get_eth_mac_stats API

The MIB entry are dropped from the custom MIB table and converted to a define providing only the MIB offset.

---
 drivers/net/dsa/mt7530.c | 70 ++++++++++++++++++++++++++++++++--------
 drivers/net/dsa/mt7530.h | 14 ++++++++
 2 files changed, 70 insertions(+), 14 deletions(-)
drivers/net/dsa/mt7530.c +56 -14
index f183a604355e..2202c657930e 100644
@@ -34,24 +34,10 @@ static struct mt753x_pcs *pcs_to_mt753x_pcs(struct phylink_pcs *pcs)
 static const struct mt7530_mib_desc mt7530_mib[] = {
 	MIB_DESC(1, 0x00, "TxDrop"),
 	MIB_DESC(1, 0x04, "TxCrcErr"),
-	MIB_DESC(1, 0x08, "TxUnicast"),
-	MIB_DESC(1, 0x0c, "TxMulticast"),
-	MIB_DESC(1, 0x10, "TxBroadcast"),
 	MIB_DESC(1, 0x14, "TxCollision"),
-	MIB_DESC(1, 0x18, "TxSingleCollision"),
-	MIB_DESC(1, 0x1c, "TxMultipleCollision"),
-	MIB_DESC(1, 0x20, "TxDeferred"),
-	MIB_DESC(1, 0x24, "TxLateCollision"),
-	MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
-	MIB_DESC(2, 0x48, "TxBytes"),
 	MIB_DESC(1, 0x60, "RxDrop"),
 	MIB_DESC(1, 0x64, "RxFiltering"),
-	MIB_DESC(1, 0x68, "RxUnicast"),
-	MIB_DESC(1, 0x6c, "RxMulticast"),
-	MIB_DESC(1, 0x70, "RxBroadcast"),
-	MIB_DESC(1, 0x74, "RxAlignErr"),
 	MIB_DESC(1, 0x78, "RxCrcErr"),
-	MIB_DESC(2, 0xa8, "RxBytes"),
 	MIB_DESC(1, 0xb0, "RxCtrlDrop"),
 	MIB_DESC(1, 0xb4, "RxIngressDrop"),
 	MIB_DESC(1, 0xb8, "RxArlDrop"),
@@ -811,6 +797,61 @@ mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset)
 	return ARRAY_SIZE(mt7530_mib);
 }
 
+static void mt7530_get_eth_mac_stats(struct dsa_switch *ds, int port,
+				     struct ethtool_eth_mac_stats *mac_stats)
+{
+	struct mt7530_priv *priv = ds->priv;
+
+	/* MIB counter doesn't provide a FramesTransmittedOK but instead
+	 * provide stats for Unicast, Broadcast and Multicast frames separately.
+	 * To simulate a global frame counter, read Unicast and addition Multicast
+	 * and Broadcast later
+	 */
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_UNICAST, 1,
+			       &mac_stats->FramesTransmittedOK);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_SINGLE_COLLISION, 1,
+			       &mac_stats->SingleCollisionFrames);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_MULTIPLE_COLLISION, 1,
+			       &mac_stats->MultipleCollisionFrames);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_UNICAST, 1,
+			       &mac_stats->FramesReceivedOK);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_BYTES, 2,
+			       &mac_stats->OctetsTransmittedOK);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_ALIGN_ERR, 1,
+			       &mac_stats->AlignmentErrors);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_DEFERRED, 1,
+			       &mac_stats->FramesWithDeferredXmissions);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_LATE_COLLISION, 1,
+			       &mac_stats->LateCollisions);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_EXCESSIVE_COLLISION, 1,
+			       &mac_stats->FramesAbortedDueToXSColls);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_BYTES, 2,
+			       &mac_stats->OctetsReceivedOK);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_MULTICAST, 1,
+			       &mac_stats->MulticastFramesXmittedOK);
+	mac_stats->FramesTransmittedOK += mac_stats->MulticastFramesXmittedOK;
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_BROADCAST, 1,
+			       &mac_stats->BroadcastFramesXmittedOK);
+	mac_stats->FramesTransmittedOK += mac_stats->BroadcastFramesXmittedOK;
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_MULTICAST, 1,
+			       &mac_stats->MulticastFramesReceivedOK);
+	mac_stats->FramesReceivedOK += mac_stats->MulticastFramesReceivedOK;
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_BROADCAST, 1,
+			       &mac_stats->BroadcastFramesReceivedOK);
+	mac_stats->FramesReceivedOK += mac_stats->BroadcastFramesReceivedOK;
+}
+
 static const struct ethtool_rmon_hist_range mt7530_rmon_ranges[] = {
 	{ 0, 64 },
 	{ 65, 127 },
@@ -3163,6 +3204,7 @@ const struct dsa_switch_ops mt7530_switch_ops = {
 	.get_strings		= mt7530_get_strings,
 	.get_ethtool_stats	= mt7530_get_ethtool_stats,
 	.get_sset_count		= mt7530_get_sset_count,
+	.get_eth_mac_stats	= mt7530_get_eth_mac_stats,
 	.get_rmon_stats		= mt7530_get_rmon_stats,
 	.get_eth_ctrl_stats	= mt7530_get_eth_ctrl_stats,
 	.set_ageing_time	= mt7530_set_ageing_time,
drivers/net/dsa/mt7530.h +14 -0
index a651ad29b750..0cc999fa1380 100644
@@ -424,6 +424,14 @@ enum mt7530_vlan_port_acc_frm {
 /* Register for MIB */
 #define MT7530_PORT_MIB_COUNTER(x)	(0x4000 + (x) * 0x100)
 /* Each define is an offset of MT7530_PORT_MIB_COUNTER */
+#define   MT7530_PORT_MIB_TX_UNICAST	0x08
+#define   MT7530_PORT_MIB_TX_MULTICAST	0x0c
+#define   MT7530_PORT_MIB_TX_BROADCAST	0x10
+#define   MT7530_PORT_MIB_TX_SINGLE_COLLISION 0x18
+#define   MT7530_PORT_MIB_TX_MULTIPLE_COLLISION 0x1c
+#define   MT7530_PORT_MIB_TX_DEFERRED	0x20
+#define   MT7530_PORT_MIB_TX_LATE_COLLISION 0x24
+#define   MT7530_PORT_MIB_TX_EXCESSIVE_COLLISION 0x28
 #define   MT7530_PORT_MIB_TX_PAUSE	0x2c
 #define   MT7530_PORT_MIB_TX_PKT_SZ_64	0x30
 #define   MT7530_PORT_MIB_TX_PKT_SZ_65_TO_127 0x34
@@ -431,6 +439,11 @@ enum mt7530_vlan_port_acc_frm {
 #define   MT7530_PORT_MIB_TX_PKT_SZ_256_TO_511 0x3c
 #define   MT7530_PORT_MIB_TX_PKT_SZ_512_TO_1023 0x40
 #define   MT7530_PORT_MIB_TX_PKT_SZ_1024_TO_MAX 0x44
+#define   MT7530_PORT_MIB_TX_BYTES	0x48 /* 64 bytes */
+#define   MT7530_PORT_MIB_RX_UNICAST	0x68
+#define   MT7530_PORT_MIB_RX_MULTICAST	0x6c
+#define   MT7530_PORT_MIB_RX_BROADCAST	0x70
+#define   MT7530_PORT_MIB_RX_ALIGN_ERR	0x74
 #define   MT7530_PORT_MIB_RX_UNDER_SIZE_ERR 0x7c
 #define   MT7530_PORT_MIB_RX_FRAG_ERR	0x80
 #define   MT7530_PORT_MIB_RX_OVER_SZ_ERR 0x84
@@ -442,6 +455,7 @@ enum mt7530_vlan_port_acc_frm {
 #define   MT7530_PORT_MIB_RX_PKT_SZ_256_TO_511 0x9c
 #define   MT7530_PORT_MIB_RX_PKT_SZ_512_TO_1023 0xa0
 #define   MT7530_PORT_MIB_RX_PKT_SZ_1024_TO_MAX 0xa4
+#define   MT7530_PORT_MIB_RX_BYTES	0xa8 /* 64 bytes */
 #define MT7530_MIB_CCR			0x4fe0
 #define  CCR_MIB_ENABLE			BIT(31)
 #define  CCR_RX_OCT_CNT_GOOD		BIT(7)
Signature · 1 line
2.48.1
#5 lore raw

PATCH 5/6 net: dsa: mt7530: move remaining MIB counter to define

In reply to Christian Marangi

For consistency with the other MIB counter, move also the remaining MIB counter to define and update the custom MIB table.

---
 drivers/net/dsa/mt7530.c | 18 +++++++++---------
 drivers/net/dsa/mt7530.h |  9 +++++++++
 2 files changed, 18 insertions(+), 9 deletions(-)
drivers/net/dsa/mt7530.c +9 -9
index 2202c657930e..fdceefb2083c 100644
@@ -32,15 +32,15 @@ static struct mt753x_pcs *pcs_to_mt753x_pcs(struct phylink_pcs *pcs)
 
 /* String, offset, and register size in bytes if different from 4 bytes */
 static const struct mt7530_mib_desc mt7530_mib[] = {
-	MIB_DESC(1, 0x00, "TxDrop"),
-	MIB_DESC(1, 0x04, "TxCrcErr"),
-	MIB_DESC(1, 0x14, "TxCollision"),
-	MIB_DESC(1, 0x60, "RxDrop"),
-	MIB_DESC(1, 0x64, "RxFiltering"),
-	MIB_DESC(1, 0x78, "RxCrcErr"),
-	MIB_DESC(1, 0xb0, "RxCtrlDrop"),
-	MIB_DESC(1, 0xb4, "RxIngressDrop"),
-	MIB_DESC(1, 0xb8, "RxArlDrop"),
+	MIB_DESC(1, MT7530_PORT_MIB_TX_DROP, "TxDrop"),
+	MIB_DESC(1, MT7530_PORT_MIB_TX_CRC_ERR, "TxCrcErr"),
+	MIB_DESC(1, MT7530_PORT_MIB_TX_COLLISION, "TxCollision"),
+	MIB_DESC(1, MT7530_PORT_MIB_RX_DROP, "RxDrop"),
+	MIB_DESC(1, MT7530_PORT_MIB_RX_FILTERING, "RxFiltering"),
+	MIB_DESC(1, MT7530_PORT_MIB_RX_CRC_ERR, "RxCrcErr"),
+	MIB_DESC(1, MT7530_PORT_MIB_RX_CTRL_DROP, "RxCtrlDrop"),
+	MIB_DESC(1, MT7530_PORT_MIB_RX_INGRESS_DROP, "RxIngressDrop"),
+	MIB_DESC(1, MT7530_PORT_MIB_RX_ARL_DROP, "RxArlDrop"),
 };
 
 static void
drivers/net/dsa/mt7530.h +9 -0
index 0cc999fa1380..d4b838a055ad 100644
@@ -424,9 +424,12 @@ enum mt7530_vlan_port_acc_frm {
 /* Register for MIB */
 #define MT7530_PORT_MIB_COUNTER(x)	(0x4000 + (x) * 0x100)
 /* Each define is an offset of MT7530_PORT_MIB_COUNTER */
+#define   MT7530_PORT_MIB_TX_DROP	0x00
+#define   MT7530_PORT_MIB_TX_CRC_ERR	0x04
 #define   MT7530_PORT_MIB_TX_UNICAST	0x08
 #define   MT7530_PORT_MIB_TX_MULTICAST	0x0c
 #define   MT7530_PORT_MIB_TX_BROADCAST	0x10
+#define   MT7530_PORT_MIB_TX_COLLISION	0x14
 #define   MT7530_PORT_MIB_TX_SINGLE_COLLISION 0x18
 #define   MT7530_PORT_MIB_TX_MULTIPLE_COLLISION 0x1c
 #define   MT7530_PORT_MIB_TX_DEFERRED	0x20
@@ -440,10 +443,13 @@ enum mt7530_vlan_port_acc_frm {
 #define   MT7530_PORT_MIB_TX_PKT_SZ_512_TO_1023 0x40
 #define   MT7530_PORT_MIB_TX_PKT_SZ_1024_TO_MAX 0x44
 #define   MT7530_PORT_MIB_TX_BYTES	0x48 /* 64 bytes */
+#define   MT7530_PORT_MIB_RX_DROP	0x60
+#define   MT7530_PORT_MIB_RX_FILTERING	0x64
 #define   MT7530_PORT_MIB_RX_UNICAST	0x68
 #define   MT7530_PORT_MIB_RX_MULTICAST	0x6c
 #define   MT7530_PORT_MIB_RX_BROADCAST	0x70
 #define   MT7530_PORT_MIB_RX_ALIGN_ERR	0x74
+#define   MT7530_PORT_MIB_RX_CRC_ERR	0x78
 #define   MT7530_PORT_MIB_RX_UNDER_SIZE_ERR 0x7c
 #define   MT7530_PORT_MIB_RX_FRAG_ERR	0x80
 #define   MT7530_PORT_MIB_RX_OVER_SZ_ERR 0x84
@@ -456,6 +462,9 @@ enum mt7530_vlan_port_acc_frm {
 #define   MT7530_PORT_MIB_RX_PKT_SZ_512_TO_1023 0xa0
 #define   MT7530_PORT_MIB_RX_PKT_SZ_1024_TO_MAX 0xa4
 #define   MT7530_PORT_MIB_RX_BYTES	0xa8 /* 64 bytes */
+#define   MT7530_PORT_MIB_RX_CTRL_DROP	0xb0
+#define   MT7530_PORT_MIB_RX_INGRESS_DROP 0xb4
+#define   MT7530_PORT_MIB_RX_ARL_DROP	0xb8
 #define MT7530_MIB_CCR			0x4fe0
 #define  CCR_MIB_ENABLE			BIT(31)
 #define  CCR_RX_OCT_CNT_GOOD		BIT(7)
Signature · 1 line
2.48.1
#6 lore raw

PATCH 6/6 net: dsa: mt7530: implement .get_stats64

In reply to Christian Marangi

It was reported that the internally calculated counter might differ from the real one from the Switch MIB. This can happen if the switch directly forward packets between the ports or offload small packets like ARP request. In such case, the kernel counter will desync compared to the real one transmitted and received by the Switch.

To correctly provide the real info to the kernel, implement .get_stats64 that will directly read the current MIB counter from the switch register.

---
 drivers/net/dsa/mt7530.c | 46 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
drivers/net/dsa/mt7530.c +46 -0
index fdceefb2083c..0a33ca1dd7ca 100644
@@ -906,6 +906,51 @@ static void mt7530_get_rmon_stats(struct dsa_switch *ds, int port,
 	*ranges = mt7530_rmon_ranges;
 }
 
+static void mt7530_get_stats64(struct dsa_switch *ds, int port,
+			       struct rtnl_link_stats64 *storage)
+{
+	struct mt7530_priv *priv = ds->priv;
+	uint64_t data;
+
+	/* MIB counter doesn't provide a FramesTransmittedOK but instead
+	 * provide stats for Unicast, Broadcast and Multicast frames separately.
+	 * To simulate a global frame counter, read Unicast and addition Multicast
+	 * and Broadcast later
+	 */
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_UNICAST, 1,
+			       &storage->rx_packets);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_MULTICAST, 1,
+			       &storage->multicast);
+	storage->rx_packets += storage->multicast;
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_BROADCAST, 1,
+			       &data);
+	storage->rx_packets += data;
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_UNICAST, 1,
+			       &storage->tx_packets);
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_MULTICAST, 1,
+			       &data);
+	storage->tx_packets += data;
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_BROADCAST, 1,
+			       &data);
+	storage->tx_packets += data;
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_BYTES, 2,
+			       &storage->rx_bytes);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_BYTES, 2,
+			       &storage->tx_bytes);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_DROP, 1,
+			       &storage->rx_dropped);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_TX_DROP, 1,
+			       &storage->tx_dropped);
+
+	mt7530_read_port_stats(priv, port, MT7530_PORT_MIB_RX_CRC_ERR, 1,
+			       &storage->rx_crc_errors);
+}
+
 static void mt7530_get_eth_ctrl_stats(struct dsa_switch *ds, int port,
 				      struct ethtool_eth_ctrl_stats *ctrl_stats)
 {
@@ -3207,6 +3252,7 @@ const struct dsa_switch_ops mt7530_switch_ops = {
 	.get_eth_mac_stats	= mt7530_get_eth_mac_stats,
 	.get_rmon_stats		= mt7530_get_rmon_stats,
 	.get_eth_ctrl_stats	= mt7530_get_eth_ctrl_stats,
+	.get_stats64		= mt7530_get_stats64,
 	.set_ageing_time	= mt7530_set_ageing_time,
 	.port_enable		= mt7530_port_enable,
 	.port_disable		= mt7530_port_disable,
Signature · 1 line
2.48.1
#7 lore raw
In reply to Christian Marangi
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 10 lines · On Thu, 10 Apr 2025 18:30:08 +0200 you wrote:

This small series modernize MIB handling for MT7530 and also implement .get_stats64.

It was reported that kernel and Switch MIB desync in scenario where a packet is forwarded from a port to another. In such case, the forwarding is offloaded and the kernel is not aware of the transmitted packet. To handle this, read the counter directly from Switch registers.

[...]

Here is the summary with links:
- [net-next,1/6] net: dsa: mt7530: generalize read port stats logic

    https://git.kernel.org/netdev/net-next/c/ee6a2db281a3
  - [net-next,2/6] net: dsa: mt7530: move pkt size and rx err MIB counter to rmon stats API
    https://git.kernel.org/netdev/net-next/c/33bc7af2b281
  - [net-next,3/6] net: dsa: mt7530: move pause MIB counter to eth_ctrl stats API
    https://git.kernel.org/netdev/net-next/c/e12989ab719c
  - [net-next,4/6] net: dsa: mt7530: move pkt stats and err MIB counter to eth_mac stats API
    https://git.kernel.org/netdev/net-next/c/dcf9eb6d33a2
  - [net-next,5/6] net: dsa: mt7530: move remaining MIB counter to define
    https://git.kernel.org/netdev/net-next/c/c3b904c6dd81
  - [net-next,6/6] net: dsa: mt7530: implement .get_stats64
    https://git.kernel.org/netdev/net-next/c/88c810f35ed5

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
maintainer
#8 lore raw
In reply to Christian Marangi

Hi Christian,

Quoted 9 lines · Christian Marangi

This small series modernize MIB handling for MT7530 and also implement .get_stats64.

It was reported that kernel and Switch MIB desync in scenario where a packet is forwarded from a port to another. In such case, the forwarding is offloaded and the kernel is not aware of the transmitted packet. To handle this, read the counter directly from Switch registers.

Quoted 9 lines

Christian Marangi (6):
net: dsa: mt7530: generalize read port stats logic
net: dsa: mt7530: move pkt size and rx err MIB counter to rmon stats
API
net: dsa: mt7530: move pause MIB counter to eth_ctrl stats API
net: dsa: mt7530: move pkt stats and err MIB counter to eth_mac stats
API
net: dsa: mt7530: move remaining MIB counter to define
net: dsa: mt7530: implement .get_stats64

After this series being applied I see lockdep warnings every time the interface counters are being read on MT7531 connected via MDIO:

[  234.374708] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:579
[  234.383200] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 3667, name: ifconfig
[  234.391202] preempt_count: 1, expected: 0
[  234.395226] INFO: lockdep is turned off.
[  234.399150] CPU: 3 UID: 0 PID: 3667 Comm: ifconfig Tainted: G        W  O        6.16.0-rc1+ #0 NONE 
[  234.399158] Tainted: [W]=WARN, [O]=OOT_MODULE
[  234.399160] Hardware name: Bananapi BPI-R3 (DT)
[  234.399162] Call trace:
[  234.399165]  show_stack+0x28/0x78 (C)
[  234.399179]  dump_stack_lvl+0x68/0x8c
[  234.399184]  dump_stack+0x14/0x1c
[  234.399188]  __might_resched+0x138/0x250
[  234.399197]  __might_sleep+0x44/0x80
[  234.399201]  __mutex_lock+0x4c/0x934
[  234.399209]  mutex_lock_nested+0x20/0x28
[  234.399215]  mt7530_get_stats64+0x40/0x2ac
[  234.399222]  dsa_user_get_stats64+0x2c/0x40
[  234.399229]  dev_get_stats+0x44/0x1e0
[  234.399237]  dev_seq_printf_stats+0x24/0xe0
[  234.399244]  dev_seq_show+0x14/0x40
[  234.399248]  seq_read_iter+0x368/0x464
[  234.399257]  seq_read+0xd0/0xfc
[  234.399263]  proc_reg_read+0xa8/0xf0
[  234.399268]  vfs_read+0x98/0x2b0
[  234.399275]  ksys_read+0x54/0xdc
[  234.399280]  __arm64_sys_read+0x18/0x20
[  234.399286]  invoke_syscall.constprop.0+0x4c/0xd0
[  234.399293]  do_el0_svc+0x3c/0xd0
[  234.399298]  el0_svc+0x34/0xa0
[  234.399303]  el0t_64_sync_handler+0x104/0x138
[  234.399308]  el0t_64_sync+0x158/0x15c

Note that this only shows with some lock debugging options being set and may not actually be a problem, but I believe it anyway should be fixed somehow.

#
# Lock Debugging (spinlocks, mutexes, etc...)
#
CONFIG_LOCK_DEBUGGING_SUPPORT=y
CONFIG_PROVE_LOCKING=y
CONFIG_PROVE_RAW_LOCK_NESTING=y
# CONFIG_LOCK_STAT is not set
CONFIG_DEBUG_RT_MUTEXES=y
CONFIG_DEBUG_SPINLOCK=y
CONFIG_DEBUG_MUTEXES=y
CONFIG_DEBUG_WW_MUTEX_SLOWPATH=y
CONFIG_DEBUG_RWSEMS=y
CONFIG_DEBUG_LOCK_ALLOC=y
CONFIG_LOCKDEP=y
CONFIG_LOCKDEP_BITS=15
CONFIG_LOCKDEP_CHAINS_BITS=16
CONFIG_LOCKDEP_STACK_TRACE_BITS=19
CONFIG_LOCKDEP_STACK_TRACE_HASH_BITS=14
CONFIG_LOCKDEP_CIRCULAR_QUEUE_BITS=12
# CONFIG_DEBUG_LOCKDEP is not set
CONFIG_DEBUG_ATOMIC_SLEEP=y
# CONFIG_DEBUG_LOCKING_API_SELFTESTS is not set
# CONFIG_LOCK_TORTURE_TEST is not set
# CONFIG_WW_MUTEX_SELFTEST is not set
# CONFIG_SCF_TORTURE_TEST is not set
# CONFIG_CSD_LOCK_WAIT_DEBUG is not set

# end of Lock Debugging (spinlocks, mutexes, etc...)

CONFIG_TRACE_IRQFLAGS=y
CONFIG_TRACE_IRQFLAGS_NMI=y
# CONFIG_DEBUG_IRQFLAGS is not set
CONFIG_STACKTRACE=y
# CONFIG_WARN_ALL_UNSEEDED_RANDOM is not set
# CONFIG_DEBUG_KOBJECT is not set

Cheers

Daniel

#9 lore raw
Quoted 23 lines

Hi Christian,

Quoted 9 lines · Christian Marangi

This small series modernize MIB handling for MT7530 and also implement .get_stats64.

It was reported that kernel and Switch MIB desync in scenario where a packet is forwarded from a port to another. In such case, the forwarding is offloaded and the kernel is not aware of the transmitted packet. To handle this, read the counter directly from Switch registers.

Quoted 9 lines

Christian Marangi (6):
net: dsa: mt7530: generalize read port stats logic
net: dsa: mt7530: move pkt size and rx err MIB counter to rmon stats
API
net: dsa: mt7530: move pause MIB counter to eth_ctrl stats API
net: dsa: mt7530: move pkt stats and err MIB counter to eth_mac stats
API
net: dsa: mt7530: move remaining MIB counter to define
net: dsa: mt7530: implement .get_stats64

Quoted 3 lines

After this series being applied I see lockdep warnings every time the interface counters are being read on MT7531 connected via MDIO:

Thanks for the report, I will try to fix this and post a followup patch for this.
Also I assume this is only present for MDIO and MMIO is not affected.

Quoted 76 lines
[  234.374708] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:579
[  234.383200] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 3667, name: ifconfig
[  234.391202] preempt_count: 1, expected: 0
[  234.395226] INFO: lockdep is turned off.
[  234.399150] CPU: 3 UID: 0 PID: 3667 Comm: ifconfig Tainted: G        W  O        6.16.0-rc1+ #0 NONE
[  234.399158] Tainted: [W]=WARN, [O]=OOT_MODULE
[  234.399160] Hardware name: Bananapi BPI-R3 (DT)
[  234.399162] Call trace:
[  234.399165]  show_stack+0x28/0x78 (C)
[  234.399179]  dump_stack_lvl+0x68/0x8c
[  234.399184]  dump_stack+0x14/0x1c
[  234.399188]  __might_resched+0x138/0x250
[  234.399197]  __might_sleep+0x44/0x80
[  234.399201]  __mutex_lock+0x4c/0x934
[  234.399209]  mutex_lock_nested+0x20/0x28
[  234.399215]  mt7530_get_stats64+0x40/0x2ac
[  234.399222]  dsa_user_get_stats64+0x2c/0x40
[  234.399229]  dev_get_stats+0x44/0x1e0
[  234.399237]  dev_seq_printf_stats+0x24/0xe0
[  234.399244]  dev_seq_show+0x14/0x40
[  234.399248]  seq_read_iter+0x368/0x464
[  234.399257]  seq_read+0xd0/0xfc
[  234.399263]  proc_reg_read+0xa8/0xf0
[  234.399268]  vfs_read+0x98/0x2b0
[  234.399275]  ksys_read+0x54/0xdc
[  234.399280]  __arm64_sys_read+0x18/0x20
[  234.399286]  invoke_syscall.constprop.0+0x4c/0xd0
[  234.399293]  do_el0_svc+0x3c/0xd0
[  234.399298]  el0_svc+0x34/0xa0
[  234.399303]  el0t_64_sync_handler+0x104/0x138
[  234.399308]  el0t_64_sync+0x158/0x15c

Note that this only shows with some lock debugging options being set and may not actually be a problem, but I believe it anyway should be fixed somehow.

#
# Lock Debugging (spinlocks, mutexes, etc...)
#
CONFIG_LOCK_DEBUGGING_SUPPORT=y
CONFIG_PROVE_LOCKING=y
CONFIG_PROVE_RAW_LOCK_NESTING=y
# CONFIG_LOCK_STAT is not set
CONFIG_DEBUG_RT_MUTEXES=y
CONFIG_DEBUG_SPINLOCK=y
CONFIG_DEBUG_MUTEXES=y
CONFIG_DEBUG_WW_MUTEX_SLOWPATH=y
CONFIG_DEBUG_RWSEMS=y
CONFIG_DEBUG_LOCK_ALLOC=y
CONFIG_LOCKDEP=y
CONFIG_LOCKDEP_BITS=15
CONFIG_LOCKDEP_CHAINS_BITS=16
CONFIG_LOCKDEP_STACK_TRACE_BITS=19
CONFIG_LOCKDEP_STACK_TRACE_HASH_BITS=14
CONFIG_LOCKDEP_CIRCULAR_QUEUE_BITS=12
# CONFIG_DEBUG_LOCKDEP is not set
CONFIG_DEBUG_ATOMIC_SLEEP=y
# CONFIG_DEBUG_LOCKING_API_SELFTESTS is not set
# CONFIG_LOCK_TORTURE_TEST is not set
# CONFIG_WW_MUTEX_SELFTEST is not set
# CONFIG_SCF_TORTURE_TEST is not set
# CONFIG_CSD_LOCK_WAIT_DEBUG is not set

# end of Lock Debugging (spinlocks, mutexes, etc...)

CONFIG_TRACE_IRQFLAGS=y
CONFIG_TRACE_IRQFLAGS_NMI=y
# CONFIG_DEBUG_IRQFLAGS is not set
CONFIG_STACKTRACE=y
# CONFIG_WARN_ALL_UNSEEDED_RANDOM is not set
# CONFIG_DEBUG_KOBJECT is not set

Cheers

Daniel