From 87f8605c32d308938df17b29816720ca97b0a23f Mon Sep 17 00:00:00 2001
From: Junxian Huang <huangjunxian6@hisilicon.com>
Date: Sat, 26 Aug 2023 15:05:53 +0800
Subject: hikptool/roce: Add ext query for roce_qmm cmd

Add ext query for roce_qmm cmd. To query these ext registers, add
-e to the query cmd.

Example:
hikptool roce_qmm -i eth1 -e

Signed-off-by: Junxian Huang <huangjunxian6@hisilicon.com>
---
 net/roce/roce_qmm/hikp_roce_qmm.c | 137 ++++++++++++++----------------
 net/roce/roce_qmm/hikp_roce_qmm.h |  11 +++
 2 files changed, 75 insertions(+), 73 deletions(-)

diff --git a/net/roce/roce_qmm/hikp_roce_qmm.c b/net/roce/roce_qmm/hikp_roce_qmm.c
index c7e0ca4..b020c6a 100644
--- a/net/roce/roce_qmm/hikp_roce_qmm.c
+++ b/net/roce/roce_qmm/hikp_roce_qmm.c
@@ -25,6 +25,7 @@ static int hikp_roce_qmm_help(struct major_cmd_ctrl *self, const char *argv)
 	printf("    %s, %-25s %s\n", "-i", "--interface=<interface>", "device target, e.g. eth0");
 	printf("    %s, %-25s %s\n", "-b", "--bank=<bank>",
 	       "[option]bank number, e.g. 0~7. (default 0)");
+	printf("    %s, %-25s %s\n", "-e", "--extend", "query extend qmm registers");
 	printf("\n");
 
 	return 0;
@@ -34,7 +35,7 @@ static int hikp_roce_qmm_target(struct major_cmd_ctrl *self, const char *argv)
 {
 	self->err_no = tool_check_and_get_valid_bdf_id(argv, &(g_roce_qmm_param.target));
 	if (self->err_no != 0) {
-		snprintf(self->err_str, sizeof(self->err_str), "Unknown device %s.", argv);
+		snprintf(self->err_str, sizeof(self->err_str), "Unknown device %s.\n", argv);
 		return self->err_no;
 	}
 
@@ -61,6 +62,8 @@ static void hikp_roce_qmm_print(struct roce_qmm_rsp_data *qmm_rsp)
 {
 	int index = 0;
 
+	printf("**************QMM %s INFO*************\n",
+	       g_roce_qmm_param.sub_name);
 	while (index < qmm_rsp->reg_num) {
 		printf("0x%08X : 0x%08X\n", qmm_rsp->qmm_content[index][0],
 		       qmm_rsp->qmm_content[index][1]);
@@ -69,101 +72,88 @@ static void hikp_roce_qmm_print(struct roce_qmm_rsp_data *qmm_rsp)
 	printf("***************************************\n");
 }
 
-static int hikp_roce_qmm_show_cqc(struct major_cmd_ctrl *self)
+static int hikp_roce_qmm_get_data(struct hikp_cmd_ret **cmd_ret,
+				  uint32_t block_id)
 {
-	struct roce_qmm_req_para req_data = { 0 };
-	struct roce_qmm_rsp_data *qmm_rsp = NULL;
+	struct roce_qmm_req_para_ext req_data_ext;
 	struct hikp_cmd_header req_header = { 0 };
-	struct hikp_cmd_ret *cmd_ret = NULL;
+	uint32_t req_size;
 	int ret;
 
-	req_data.bdf = g_roce_qmm_param.target.bdf;
+	req_data_ext.origin_param.bdf = g_roce_qmm_param.target.bdf;
+	req_data_ext.origin_param.bank_id = g_roce_qmm_param.bank_id;
+	req_data_ext.block_id = block_id;
+
+	req_size = g_roce_qmm_param.ext_flag ?
+		   sizeof(struct roce_qmm_req_para) :
+		   sizeof(struct roce_qmm_req_para_ext);
+	hikp_cmd_init(&req_header, ROCE_MOD, GET_ROCEE_QMM_CMD,
+		      g_roce_qmm_param.sub_cmd);
+	*cmd_ret = hikp_cmd_alloc(&req_header, &req_data_ext, req_size);
+	ret = hikp_rsp_normal_check(*cmd_ret);
+	if (ret)
+		printf("hikptool roce_qmm cmd_ret malloc failed, sub_cmd = %u, ret = %d.\n",
+			g_roce_qmm_param.sub_cmd, ret);
 
-	hikp_cmd_init(&req_header, ROCE_MOD, GET_ROCEE_QMM_CMD, QMM_SHOW_CQC);
-	cmd_ret = hikp_cmd_alloc(&req_header, &req_data, sizeof(req_data));
-	ret = hikp_rsp_normal_check(cmd_ret);
-	if (ret != 0)
-		goto out;
-
-	qmm_rsp = (struct roce_qmm_rsp_data *)(cmd_ret->rsp_data);
-	printf("**************QMM CQC INFO*************\n");
-	hikp_roce_qmm_print(qmm_rsp);
-out:
-	free(cmd_ret);
-	cmd_ret = NULL;
-	return ret;
-}
-
-static int hikp_roce_qmm_show_qpc(struct major_cmd_ctrl *self)
-{
-	struct roce_qmm_req_para req_data = { 0 };
-	struct roce_qmm_rsp_data *qmm_rsp = NULL;
-	struct hikp_cmd_header req_header = { 0 };
-	struct hikp_cmd_ret *cmd_ret = NULL;
-	int ret;
-
-	req_data.bdf = g_roce_qmm_param.target.bdf;
-	req_data.bank_id = g_roce_qmm_param.bank_id;
-
-	hikp_cmd_init(&req_header, ROCE_MOD, GET_ROCEE_QMM_CMD, QMM_SHOW_QPC);
-	cmd_ret = hikp_cmd_alloc(&req_header, &req_data, sizeof(req_data));
-	ret = hikp_rsp_normal_check(cmd_ret);
-	if (ret != 0)
-		goto out;
-
-	qmm_rsp = (struct roce_qmm_rsp_data *)(cmd_ret->rsp_data);
-	printf("**************QMM QPC INFO*************\n");
-	hikp_roce_qmm_print(qmm_rsp);
-out:
-	free(cmd_ret);
-	cmd_ret = NULL;
 	return ret;
 }
 
-static int hikp_roce_qmm_show_top(struct major_cmd_ctrl *self)
+static void hikp_roce_qmm_execute_origin(struct major_cmd_ctrl *self)
 {
-	struct roce_qmm_req_para req_data = { 0 };
-	struct roce_qmm_rsp_data *qmm_rsp = NULL;
-	struct hikp_cmd_header req_header = { 0 };
-	struct hikp_cmd_ret *cmd_ret = NULL;
-	int ret;
+	struct roce_qmm_rsp_data *roce_qmm_res;
+	struct hikp_cmd_ret *cmd_ret;
 
-	req_data.bdf = g_roce_qmm_param.target.bdf;
-
-	hikp_cmd_init(&req_header, ROCE_MOD, GET_ROCEE_QMM_CMD, QMM_SHOW_TOP);
-	cmd_ret = hikp_cmd_alloc(&req_header, &req_data, sizeof(req_data));
-	ret = hikp_rsp_normal_check(cmd_ret);
-	if (ret != 0)
-		goto out;
+	self->err_no = hikp_roce_qmm_get_data(&cmd_ret, 0);
+	if (self->err_no) {
+		printf("hikptool roce_qmm get data failed.\n");
+		goto exec_error;
+	}
+	roce_qmm_res = (struct roce_qmm_rsp_data *)cmd_ret->rsp_data;
+	hikp_roce_qmm_print(roce_qmm_res);
 
-	qmm_rsp = (struct roce_qmm_rsp_data *)(cmd_ret->rsp_data);
-	printf("**************QMM TOP INFO*************\n");
-	hikp_roce_qmm_print(qmm_rsp);
-out:
-	free(cmd_ret);
-	cmd_ret = NULL;
-	return ret;
+exec_error:
+	if (cmd_ret)
+		free(cmd_ret);
 }
 
 static void hikp_roce_qmm_execute(struct major_cmd_ctrl *self)
 {
-	int (*func[])(struct major_cmd_ctrl *self) = {
-		hikp_roce_qmm_show_cqc, hikp_roce_qmm_show_qpc, hikp_roce_qmm_show_top
+	const struct cmd_type_info {
+		enum roce_qmm_cmd_type sub_cmd;
+		enum roce_qmm_cmd_type sub_ext_cmd;
+		const char *sub_name;
+	} sub_cmd_info_table[] = {
+		{QMM_SHOW_CQC, QMM_SHOW_CQC_EXT, "CQC"},
+		{QMM_SHOW_QPC, QMM_SHOW_QPC_EXT, "QPC"},
+		{QMM_SHOW_TOP, QMM_SHOW_TOP_EXT, "TOP"},
 	};
-	char *function[] = {"show cqc", "show qpc", "show top"};
-	int ret;
 
-	for (int i = 0; i < HIKP_ARRAY_SIZE(func); i++) {
-		ret = func[i](self);
-		if (ret != 0) {
-			self->err_no = -EINVAL;
+	for (int i = 0; i < HIKP_ARRAY_SIZE(sub_cmd_info_table); i++) {
+		g_roce_qmm_param.sub_name = sub_cmd_info_table[i].sub_name;
+		if (g_roce_qmm_param.ext_flag) {
+			g_roce_qmm_param.sub_cmd = sub_cmd_info_table[i].sub_ext_cmd;
+			hikp_roce_ext_execute(self, GET_ROCEE_QMM_CMD,
+					      hikp_roce_qmm_get_data);
+		} else {
+			g_roce_qmm_param.sub_cmd = sub_cmd_info_table[i].sub_cmd;
+			hikp_roce_qmm_execute_origin(self);
+		}
+		if (self->err_no) {
 			snprintf(self->err_str, sizeof(self->err_str),
-				 "roce_qmm %s function failed\n", function[i]);
+				 "roce_qmm show %s function failed\n",
+				 sub_cmd_info_table[i].sub_name);
 			break;
 		}
 	}
 }
 
+static int hikp_roce_qmm_ext_set(struct major_cmd_ctrl *self, const char *argv)
+{
+	g_roce_qmm_param.ext_flag = true;
+
+	return 0;
+}
+
 static void cmd_roce_qmm_init(void)
 {
 	struct major_cmd_ctrl *major_cmd = get_major_cmd();
@@ -174,6 +164,7 @@ static void cmd_roce_qmm_init(void)
 	cmd_option_register("-h", "--help", false, hikp_roce_qmm_help);
 	cmd_option_register("-i", "--interface", true, hikp_roce_qmm_target);
 	cmd_option_register("-b", "--bank", true, hikp_roce_qmm_bank_get);
+	cmd_option_register("-e", "--extend", false, hikp_roce_qmm_ext_set);
 }
 
 HIKP_CMD_DECLARE("roce_qmm", "get roce_qmm registers information", cmd_roce_qmm_init);
diff --git a/net/roce/roce_qmm/hikp_roce_qmm.h b/net/roce/roce_qmm/hikp_roce_qmm.h
index 0645ab3..06e6bcf 100644
--- a/net/roce/roce_qmm/hikp_roce_qmm.h
+++ b/net/roce/roce_qmm/hikp_roce_qmm.h
@@ -22,6 +22,9 @@
 struct cmd_roce_qmm_param_t {
 	struct tool_target target;
 	uint32_t bank_id;
+	uint32_t sub_cmd;
+	const char *sub_name;
+	bool ext_flag;
 };
 
 struct roce_qmm_rsp_data {
@@ -34,10 +37,18 @@ struct roce_qmm_req_para {
 	uint32_t bank_id;
 };
 
+struct roce_qmm_req_para_ext {
+	struct roce_qmm_req_para origin_param;
+	uint32_t block_id;
+};
+
 enum roce_qmm_cmd_type {
 	QMM_SHOW_CQC = 0x1,
 	QMM_SHOW_QPC = 0x2,
 	QMM_SHOW_TOP = 0x3,
+	QMM_SHOW_CQC_EXT,
+	QMM_SHOW_QPC_EXT,
+	QMM_SHOW_TOP_EXT,
 };
 
 #endif /* __HIKP_ROCE_QMM_H__ */
-- 
2.30.0

