From 450591abedaabee32a21d53a50cb5b65440ce971 Mon Sep 17 00:00:00 2001
From: "Min Hu (Connor)" <humin29@huawei.com>
Date: Fri, 21 Oct 2022 15:36:41 +0800
Subject: [PATCH 165/189] net/hns3: support Rx/Tx descriptor dump

This patch support query HW descriptor from hns3 device. HW descriptor
is also called BD (buffer description) which is shared memory between
software and hardware.

Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
Signed-off-by: Dongdong Liu <liudongdong3@huawei.com>
Acked-by: Ferruh Yigit <ferruh.yigit@xilinx.com>
---
 drivers/net/hns3/hns3_dump.c      | 88 +++++++++++++++++++++++++++++++
 drivers/net/hns3/hns3_dump.h      |  4 ++
 drivers/net/hns3/hns3_ethdev.c    |  2 +
 drivers/net/hns3/hns3_ethdev_vf.c |  2 +
 4 files changed, 96 insertions(+)

diff --git a/drivers/net/hns3/hns3_dump.c b/drivers/net/hns3/hns3_dump.c
index cf5b500be1..1007b09bd2 100644
--- a/drivers/net/hns3/hns3_dump.c
+++ b/drivers/net/hns3/hns3_dump.c
@@ -11,6 +11,9 @@
 #include "hns3_logs.h"
 #include "hns3_dump.h"
 
+#define HNS3_BD_DW_NUM 8
+#define HNS3_BD_ADDRESS_LAST_DW 2
+
 static const char *
 hns3_get_adapter_state_name(enum hns3_adapter_state state)
 {
@@ -873,3 +876,88 @@ hns3_eth_dev_priv_dump(struct rte_eth_dev *dev, FILE *file)
 
 	return 0;
 }
+
+int
+hns3_rx_descriptor_dump(const struct rte_eth_dev *dev, uint16_t queue_id,
+			uint16_t offset, uint16_t num, FILE *file)
+{
+	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+	struct hns3_rx_queue *rxq = dev->data->rx_queues[queue_id];
+	uint32_t *bd_data;
+	uint16_t count = 0;
+	uint16_t desc_id;
+	int i;
+
+	if (offset >= rxq->nb_rx_desc)
+		return -EINVAL;
+
+	if (num > rxq->nb_rx_desc) {
+		hns3_err(hw, "Invalid BD num=%u\n", num);
+		return -EINVAL;
+	}
+
+	while (count < num) {
+		desc_id = (rxq->next_to_use + offset + count) % rxq->nb_rx_desc;
+		bd_data = (uint32_t *)(&rxq->rx_ring[desc_id]);
+		fprintf(file, "Rx queue id:%u BD id:%u\n", queue_id, desc_id);
+		for (i = 0; i < HNS3_BD_DW_NUM; i++) {
+			/*
+			 * For the sake of security, first 8 bytes of BD which
+			 * stands for physical address of packet should not be
+			 * shown.
+			 */
+			if (i < HNS3_BD_ADDRESS_LAST_DW) {
+				fprintf(file, "RX BD WORD[%d]:0x%08x\n", i, 0);
+				continue;
+			}
+			fprintf(file, "RX BD WORD[%d]:0x%08x\n", i,
+				*(bd_data + i));
+		}
+		count++;
+	}
+
+	return 0;
+}
+
+int
+hns3_tx_descriptor_dump(const struct rte_eth_dev *dev, uint16_t queue_id,
+			uint16_t offset, uint16_t num, FILE *file)
+{
+	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+	struct hns3_tx_queue *txq = dev->data->tx_queues[queue_id];
+	uint32_t *bd_data;
+	uint16_t count = 0;
+	uint16_t desc_id;
+	int i;
+
+	if (offset >= txq->nb_tx_desc)
+		return -EINVAL;
+
+	if (num > txq->nb_tx_desc) {
+		hns3_err(hw, "Invalid BD num=%u\n", num);
+		return -EINVAL;
+	}
+
+	while (count < num) {
+		desc_id = (txq->next_to_use + offset + count) % txq->nb_tx_desc;
+		bd_data = (uint32_t *)(&txq->tx_ring[desc_id]);
+		fprintf(file, "Tx queue id:%u BD id:%u\n", queue_id, desc_id);
+		for (i = 0; i < HNS3_BD_DW_NUM; i++) {
+			/*
+			 * For the sake of security, first 8 bytes of BD which
+			 * stands for physical address of packet should not be
+			 * shown.
+			 */
+			if (i < HNS3_BD_ADDRESS_LAST_DW) {
+				fprintf(file, "TX BD WORD[%d]:0x%08x\n", i, 0);
+				continue;
+			}
+
+			fprintf(file, "Tx BD WORD[%d]:0x%08x\n", i,
+				*(bd_data + i));
+		}
+		count++;
+	}
+
+	return 0;
+}
diff --git a/drivers/net/hns3/hns3_dump.h b/drivers/net/hns3/hns3_dump.h
index 616cb70d6e..021ce1bbdb 100644
--- a/drivers/net/hns3/hns3_dump.h
+++ b/drivers/net/hns3/hns3_dump.h
@@ -7,4 +7,8 @@
 
 int hns3_eth_dev_priv_dump(struct rte_eth_dev *dev, FILE *file);
 
+int hns3_rx_descriptor_dump(const struct rte_eth_dev *dev, uint16_t queue_id,
+			    uint16_t offset, uint16_t num, FILE *file);
+int hns3_tx_descriptor_dump(const struct rte_eth_dev *dev, uint16_t queue_id,
+			    uint16_t offset, uint16_t num, FILE *file);
 #endif /* HNS3_DUMP_H */
diff --git a/drivers/net/hns3/hns3_ethdev.c b/drivers/net/hns3/hns3_ethdev.c
index 7330515535..f83cff4d98 100644
--- a/drivers/net/hns3/hns3_ethdev.c
+++ b/drivers/net/hns3/hns3_ethdev.c
@@ -6558,6 +6558,8 @@ static const struct eth_dev_ops hns3_eth_dev_ops = {
 	.timesync_read_time         = hns3_timesync_read_time,
 	.timesync_write_time        = hns3_timesync_write_time,
 	.eth_dev_priv_dump          = hns3_eth_dev_priv_dump,
+	.eth_rx_descriptor_dump     = hns3_rx_descriptor_dump,
+	.eth_tx_descriptor_dump     = hns3_tx_descriptor_dump,
 };
 
 static const struct hns3_reset_ops hns3_reset_ops = {
diff --git a/drivers/net/hns3/hns3_ethdev_vf.c b/drivers/net/hns3/hns3_ethdev_vf.c
index 6976a9f23d..1022b02697 100644
--- a/drivers/net/hns3/hns3_ethdev_vf.c
+++ b/drivers/net/hns3/hns3_ethdev_vf.c
@@ -2302,6 +2302,8 @@ static const struct eth_dev_ops hns3vf_eth_dev_ops = {
 	.dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
 	.tx_done_cleanup    = hns3_tx_done_cleanup,
 	.eth_dev_priv_dump  = hns3_eth_dev_priv_dump,
+	.eth_rx_descriptor_dump = hns3_rx_descriptor_dump,
+	.eth_tx_descriptor_dump = hns3_tx_descriptor_dump,
 };
 
 static const struct hns3_reset_ops hns3vf_reset_ops = {
-- 
2.23.0

