Subject: [PATCH v7 net-next 4/5] net: ena: PHC stats through sysfs

From: David Arinzon <[email-masked]>
Date: Tue, 18 Feb 2025 20:39:47 +0200

The patch allows retrieving PHC statistics
through sysfs.
In case the feature is not enabled (through `phc_enable`
sysfs entry), no output will be written.

Signed-off-by: David Arinzon <[email-masked]>
---
 drivers/net/ethernet/amazon/ena/ena_ethtool.c |  5 --
 drivers/net/ethernet/amazon/ena/ena_netdev.h  |  6 +++
 drivers/net/ethernet/amazon/ena/ena_sysfs.c   | 50 +++++++++++++++++++
 3 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/amazon/ena/ena_ethtool.c b/drivers/net/ethernet/amazon/ena/ena_ethtool.c
index b442d247..28cc0163 100644
--- a/drivers/net/ethernet/amazon/ena/ena_ethtool.c
+++ b/drivers/net/ethernet/amazon/ena/ena_ethtool.c
@@ -11,11 +11,6 @@
 #include "ena_xdp.h"
 #include "ena_phc.h"
 
-struct ena_stats {
-	char name[ETH_GSTRING_LEN];
-	int stat_offset;
-};
-
 struct ena_hw_metrics {
 	char name[ETH_GSTRING_LEN];
 };
diff --git a/drivers/net/ethernet/amazon/ena/ena_netdev.h b/drivers/net/ethernet/amazon/ena/ena_netdev.h
index e3c7ed9c..c10d1305 100644
--- a/drivers/net/ethernet/amazon/ena/ena_netdev.h
+++ b/drivers/net/ethernet/amazon/ena/ena_netdev.h
@@ -14,6 +14,7 @@
 #include <linux/interrupt.h>
 #include <linux/netdevice.h>
 #include <linux/skbuff.h>
+#include <linux/ethtool.h>
 #include <net/xdp.h>
 #include <uapi/linux/bpf.h>
 
@@ -389,6 +390,11 @@ struct ena_adapter {
 	u32 xdp_num_queues;
 };
 
+struct ena_stats {
+	char name[ETH_GSTRING_LEN];
+	int stat_offset;
+};
+
 void ena_set_ethtool_ops(struct net_device *netdev);
 
 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter);
diff --git a/drivers/net/ethernet/amazon/ena/ena_sysfs.c b/drivers/net/ethernet/amazon/ena/ena_sysfs.c
index d0ded92d..10993594 100644
--- a/drivers/net/ethernet/amazon/ena/ena_sysfs.c
+++ b/drivers/net/ethernet/amazon/ena/ena_sysfs.c
@@ -65,6 +65,52 @@ static ssize_t ena_phc_enable_get(struct device *dev,
 static DEVICE_ATTR(phc_enable, S_IRUGO | S_IWUSR, ena_phc_enable_get,
 		   ena_phc_enable_set);
 
+#define ENA_STAT_ENA_COM_PHC_ENTRY(stat) { \
+	.name = #stat, \
+	.stat_offset = offsetof(struct ena_com_stats_phc, stat) / sizeof(u64) \
+}
+
+const struct ena_stats ena_stats_ena_com_phc_strings[] = {
+	ENA_STAT_ENA_COM_PHC_ENTRY(phc_cnt),
+	ENA_STAT_ENA_COM_PHC_ENTRY(phc_exp),
+	ENA_STAT_ENA_COM_PHC_ENTRY(phc_skp),
+	ENA_STAT_ENA_COM_PHC_ENTRY(phc_err),
+};
+
+u16 ena_stats_array_ena_com_phc_size = ARRAY_SIZE(ena_stats_ena_com_phc_strings);
+
+static ssize_t ena_phc_stats_show(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	struct ena_adapter *adapter = dev_get_drvdata(dev);
+	int i, rc, chars_written = 0;
+
+	if (!ena_phc_is_active(adapter))
+		return 0;
+
+	for (i = 0; i < ena_stats_array_ena_com_phc_size; i++) {
+		const struct ena_stats *ena_stats;
+		u64 *ptr;
+
+		ena_stats = &ena_stats_ena_com_phc_strings[i];
+		ptr = (u64 *)&adapter->ena_dev->phc.stats +
+		      ena_stats->stat_offset;
+		rc = snprintf(buf,
+			      ETH_GSTRING_LEN + sizeof(u64),
+			      "%s: %llu\n",
+			      ena_stats->name,
+			      *ptr);
+
+		buf += rc;
+		chars_written += rc;
+	}
+
+	return chars_written;
+}
+
+static DEVICE_ATTR(phc_stats, S_IRUGO, ena_phc_stats_show, NULL);
+
 /******************************************************************************
  *****************************************************************************/
 int ena_sysfs_init(struct device *dev)
@@ -72,6 +118,9 @@ int ena_sysfs_init(struct device *dev)
 	if (device_create_file(dev, &dev_attr_phc_enable))
 		dev_err(dev, "Failed to create phc_enable sysfs entry");
 
+	if (device_create_file(dev, &dev_attr_phc_stats))
+		dev_err(dev, "Failed to create phc_stats sysfs entry");
+
 	return 0;
 }
 
@@ -80,4 +129,5 @@ int ena_sysfs_init(struct device *dev)
 void ena_sysfs_terminate(struct device *dev)
 {
 	device_remove_file(dev, &dev_attr_phc_enable);
+	device_remove_file(dev, &dev_attr_phc_stats);
 }
-- 
2.47.1