[PATCH] staging: mt7621-eth: Refactor ethtool stats

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch removes the ugly macro hack to make sure hw_stats and ethtool
strings are consisten, instead define a new struct which will hold the
stat string and his index within the hw_stats struct.

Signed-off-by: Kamal Heib <kamalheib1@xxxxxxxxx>
---
 drivers/staging/mt7621-eth/ethtool.c     | 49 +++++++++++++++++++++++---------
 drivers/staging/mt7621-eth/mtk_eth_soc.h | 29 ++++++++-----------
 2 files changed, 48 insertions(+), 30 deletions(-)

diff --git a/drivers/staging/mt7621-eth/ethtool.c b/drivers/staging/mt7621-eth/ethtool.c
index 7c059cd94e56..f241132bd140 100644
--- a/drivers/staging/mt7621-eth/ethtool.c
+++ b/drivers/staging/mt7621-eth/ethtool.c
@@ -15,12 +15,33 @@
 #include "mtk_eth_soc.h"
 #include "ethtool.h"
 
-static const char mtk_gdma_str[][ETH_GSTRING_LEN] = {
-#define _FE(x...)	# x,
-MTK_STAT_REG_DECLARE
-#undef _FE
+struct mtk_stat {
+	char name[ETH_GSTRING_LEN];
+	unsigned int idx;
 };
 
+#define MTK_HW_STAT(stat) { \
+	.name = #stat, \
+	.idx = offsetof(struct mtk_hw_stats, stat) / sizeof(u64) \
+}
+
+static const struct mtk_stat mtk_ethtool_hw_stats[] = {
+	MTK_HW_STAT(tx_bytes),
+	MTK_HW_STAT(tx_packets),
+	MTK_HW_STAT(tx_skip),
+	MTK_HW_STAT(tx_collisions),
+	MTK_HW_STAT(rx_bytes),
+	MTK_HW_STAT(rx_packets),
+	MTK_HW_STAT(rx_overflow),
+	MTK_HW_STAT(rx_fcs_errors),
+	MTK_HW_STAT(rx_short_errors),
+	MTK_HW_STAT(rx_long_errors),
+	MTK_HW_STAT(rx_checksum_errors),
+	MTK_HW_STAT(rx_flow_control_packets),
+};
+
+#define MTK_HW_STATS_LEN	ARRAY_SIZE(mtk_ethtool_hw_stats)
+
 static int mtk_get_link_ksettings(struct net_device *dev,
 				  struct ethtool_link_ksettings *cmd)
 {
@@ -75,7 +96,7 @@ static void mtk_get_drvinfo(struct net_device *dev,
 	strlcpy(info->bus_info, dev_name(mac->hw->dev), sizeof(info->bus_info));
 
 	if (soc->reg_table[MTK_REG_MTK_COUNTER_BASE])
-		info->n_stats = ARRAY_SIZE(mtk_gdma_str);
+		info->n_stats = MTK_HW_STATS_LEN;
 }
 
 static u32 mtk_get_msglevel(struct net_device *dev)
@@ -154,9 +175,15 @@ static void mtk_get_ringparam(struct net_device *dev,
 
 static void mtk_get_strings(struct net_device *dev, u32 stringset, u8 *data)
 {
+	int i;
+
 	switch (stringset) {
 	case ETH_SS_STATS:
-		memcpy(data, *mtk_gdma_str, sizeof(mtk_gdma_str));
+		for (i = 0; i < MTK_HW_STATS_LEN; i++) {
+			memcpy(data, mtk_ethtool_hw_stats[i].name,
+			       ETH_GSTRING_LEN);
+			data += ETH_GSTRING_LEN;
+		}
 		break;
 	}
 }
@@ -165,7 +192,7 @@ static int mtk_get_sset_count(struct net_device *dev, int sset)
 {
 	switch (sset) {
 	case ETH_SS_STATS:
-		return ARRAY_SIZE(mtk_gdma_str);
+		return MTK_HW_STATS_LEN;
 	default:
 		return -EOPNOTSUPP;
 	}
@@ -176,7 +203,6 @@ static void mtk_get_ethtool_stats(struct net_device *dev,
 {
 	struct mtk_mac *mac = netdev_priv(dev);
 	struct mtk_hw_stats *hwstats = mac->hw_stats;
-	u64 *data_src, *data_dst;
 	unsigned int start;
 	int i;
 
@@ -188,12 +214,9 @@ static void mtk_get_ethtool_stats(struct net_device *dev,
 	}
 
 	do {
-		data_src = &hwstats->tx_bytes;
-		data_dst = data;
 		start = u64_stats_fetch_begin_irq(&hwstats->syncp);
-
-		for (i = 0; i < ARRAY_SIZE(mtk_gdma_str); i++)
-			*data_dst++ = *data_src++;
+		for (i = 0; i < MTK_HW_STATS_LEN; i++)
+			data[i] = ((u64 *)hwstats)[mtk_ethtool_hw_stats[i].idx];
 
 	} while (u64_stats_fetch_retry_irq(&hwstats->syncp, start));
 }
diff --git a/drivers/staging/mt7621-eth/mtk_eth_soc.h b/drivers/staging/mt7621-eth/mtk_eth_soc.h
index 443f88d8af65..df4175828d6b 100644
--- a/drivers/staging/mt7621-eth/mtk_eth_soc.h
+++ b/drivers/staging/mt7621-eth/mtk_eth_soc.h
@@ -501,21 +501,7 @@ struct mtk_soc_data {
 	u32 has_switch:1;
 };
 
-/* ugly macro hack to make sure hw_stats and ethtool strings are consistent */
 #define MTK_STAT_OFFSET			0x40
-#define MTK_STAT_REG_DECLARE		\
-	_FE(tx_bytes)			\
-	_FE(tx_packets)			\
-	_FE(tx_skip)			\
-	_FE(tx_collisions)		\
-	_FE(rx_bytes)			\
-	_FE(rx_packets)			\
-	_FE(rx_overflow)		\
-	_FE(rx_fcs_errors)		\
-	_FE(rx_short_errors)		\
-	_FE(rx_long_errors)		\
-	_FE(rx_checksum_errors)		\
-	_FE(rx_flow_control_packets)
 
 /* struct mtk_hw_stats - the structure that holds the traffic statistics.
  * @stats_lock:		make sure that stats operations are atomic
@@ -531,9 +517,18 @@ struct mtk_hw_stats {
 	u32 reg_offset;
 	struct u64_stats_sync syncp;
 
-#define _FE(x) u64 x;
-	MTK_STAT_REG_DECLARE
-#undef _FE
+	u64 tx_bytes;
+	u64 tx_packets;
+	u64 tx_skip;
+	u64 tx_collisions;
+	u64 rx_bytes;
+	u64 rx_packets;
+	u64 rx_overflow;
+	u64 rx_fcs_errors;
+	u64 rx_short_errors;
+	u64 rx_long_errors;
+	u64 rx_checksum_errors;
+	u64 rx_flow_control_packets;
 };
 
 /* PDMA descriptor can point at 1-2 segments. This enum allows us to track how
-- 
2.14.3

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel



[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux