From 80d9d76b7d5af23e58d13569bd1898604c8e2456 Mon Sep 17 00:00:00 2001
From: Junxian Huang <huangjunxian6@hisilicon.com>
Date: Tue, 1 Aug 2023 17:41:24 +0800
Subject: hikptool/roce: Add ext query for roce_caep cmd

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

Example:
hikptool roce_caep -i eth1 -e

Signed-off-by: Junxian Huang <huangjunxian6@hisilicon.com>
---
 net/roce/roce_caep/hikp_roce_caep.c | 69 +++++++++++++++++++----------
 net/roce/roce_caep/hikp_roce_caep.h | 11 +++++
 2 files changed, 57 insertions(+), 23 deletions(-)

diff --git a/net/roce/roce_caep/hikp_roce_caep.c b/net/roce/roce_caep/hikp_roce_caep.c
index 69a4742..6c92ea1 100644
--- a/net/roce/roce_caep/hikp_roce_caep.c
+++ b/net/roce/roce_caep/hikp_roce_caep.c
@@ -22,6 +22,7 @@ static int hikp_roce_caep_help(struct major_cmd_ctrl *self, const char *argv)
 	printf("  Options:\n\n");
 	printf("    %s, %-25s %s\n", "-h", "--help", "display this help and exit");
 	printf("    %s, %-25s %s\n", "-i", "--interface=<interface>", "device target, e.g. eth0");
+	printf("    %s, %-25s %s\n", "-e", "--extend", "query extend caep registers");
 	printf("\n");
 
 	return 0;
@@ -31,7 +32,7 @@ static int hikp_roce_caep_target(struct major_cmd_ctrl *self, const char *argv)
 {
 	self->err_no = tool_check_and_get_valid_bdf_id(argv, &(g_roce_caep_param_t.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;
 	}
 
@@ -39,18 +40,28 @@ static int hikp_roce_caep_target(struct major_cmd_ctrl *self, const char *argv)
 }
 
 static int hikp_roce_caep_get_data(struct hikp_cmd_ret **cmd_ret,
-				   struct roce_caep_req_param req_data)
+				   uint32_t block_id)
 {
+	struct roce_caep_req_param_ext req_data_ext;
 	struct hikp_cmd_header req_header = { 0 };
+	uint32_t req_size;
+	int ret;
 
-	hikp_cmd_init(&req_header, ROCE_MOD, GET_ROCEE_CAEP_CMD, 0);
-	*cmd_ret = hikp_cmd_alloc(&req_header, &req_data, sizeof(req_data));
-	if (*cmd_ret == NULL) {
-		printf("hikptool roce_caep cmd_ret malloc failed\n");
-		return -EIO;
-	}
-
-	return 0;
+	req_data_ext.origin_param.bdf = g_roce_caep_param_t.target.bdf;
+	req_data_ext.block_id = block_id;
+
+	req_size = (g_roce_caep_param_t.sub_cmd == CAEP_ORIGIN) ?
+		   sizeof(struct roce_caep_req_param) :
+		   sizeof(struct roce_caep_req_param_ext);
+	hikp_cmd_init(&req_header, ROCE_MOD, GET_ROCEE_CAEP_CMD,
+		      g_roce_caep_param_t.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_caep cmd_ret malloc failed, sub_cmd = %u, ret = %d.\n",
+			g_roce_caep_param_t.sub_cmd, ret);
+
+	return ret;
 }
 
 static void hikp_roce_caep_print(uint32_t total_block_num,
@@ -64,29 +75,40 @@ static void hikp_roce_caep_print(uint32_t total_block_num,
 	printf("************************************\n");
 }
 
-static void hikp_roce_caep_execute(struct major_cmd_ctrl *self)
+static void hikp_roce_caep_execute_origin(struct major_cmd_ctrl *self)
 {
 	struct roce_caep_res_param *roce_caep_res;
-	struct roce_caep_req_param req_data;
 	struct hikp_cmd_ret *cmd_ret;
-	int ret;
 
-	req_data.bdf = g_roce_caep_param_t.target.bdf;
-	ret = hikp_roce_caep_get_data(&cmd_ret, req_data);
-	if (ret < 0) {
-		self->err_no = ret;
-		return;
-	} else if (cmd_ret->status != 0) {
-		printf("hikptool roce_caep read data failed\n");
+	self->err_no = hikp_roce_caep_get_data(&cmd_ret, 0);
+	if (self->err_no) {
+		printf("hikptool roce_caep get data failed.\n");
 		goto exec_error;
 	}
 	roce_caep_res = (struct roce_caep_res_param *)cmd_ret->rsp_data;
 	hikp_roce_caep_print(roce_caep_res->total_block_num,
-			     roce_caep_res->reg_data.offset, roce_caep_res->reg_data.data);
+			     roce_caep_res->reg_data.offset,
+			     roce_caep_res->reg_data.data);
 
 exec_error:
-	free(cmd_ret);
-	cmd_ret = NULL;
+	if (cmd_ret)
+		free(cmd_ret);
+}
+
+static void hikp_roce_caep_execute(struct major_cmd_ctrl *self)
+{
+	if (g_roce_caep_param_t.sub_cmd == CAEP_ORIGIN)
+		hikp_roce_caep_execute_origin(self);
+	else
+		hikp_roce_ext_execute(self, GET_ROCEE_CAEP_CMD,
+				      hikp_roce_caep_get_data);
+}
+
+static int hikp_roce_caep_ext_set(struct major_cmd_ctrl *self, const char *argv)
+{
+	g_roce_caep_param_t.sub_cmd = CAEP_EXT;
+
+	return 0;
 }
 
 static void cmd_roce_caep_init(void)
@@ -98,6 +120,7 @@ static void cmd_roce_caep_init(void)
 
 	cmd_option_register("-h", "--help", false, hikp_roce_caep_help);
 	cmd_option_register("-i", "--interface", true, hikp_roce_caep_target);
+	cmd_option_register("-e", "--extend", false, hikp_roce_caep_ext_set);
 }
 
 HIKP_CMD_DECLARE("roce_caep", "get roce_caep registers information", cmd_roce_caep_init);
diff --git a/net/roce/roce_caep/hikp_roce_caep.h b/net/roce/roce_caep/hikp_roce_caep.h
index 804d2df..547a4d6 100644
--- a/net/roce/roce_caep/hikp_roce_caep.h
+++ b/net/roce/roce_caep/hikp_roce_caep.h
@@ -21,12 +21,18 @@
 
 struct cmd_roce_caep_param_t {
 	struct tool_target target;
+	uint32_t sub_cmd;
 };
 
 struct roce_caep_req_param {
 	struct bdf_t bdf;
 };
 
+struct roce_caep_req_param_ext {
+	struct roce_caep_req_param origin_param;
+	uint32_t block_id;
+};
+
 struct roce_caep_res {
 	uint32_t offset[ROCE_HIKP_CAEP_REG_NUM];
 	uint32_t data[ROCE_HIKP_CAEP_REG_NUM];
@@ -37,4 +43,9 @@ struct roce_caep_res_param {
 	struct roce_caep_res reg_data;
 };
 
+enum roce_caep_cmd_type {
+	CAEP_ORIGIN = 0,
+	CAEP_EXT,
+};
+
 #endif /* __HIKP_ROCE_CAEP_H__ */
-- 
2.30.0

