From 84d1e8a09e69ffe3809f0e4fa27d586c7ef8c706 Mon Sep 17 00:00:00 2001
From: Jie Wang <wangjie125@huawei.com>
Date: Mon, 28 Aug 2023 10:35:19 +0800
Subject: [PATCH] hikptool: add new cmd to support query port fault info

Add nic port fault cmd for query nic port fault status.

Bugfix or Feature: Feature

Signed-off-by: Jie Wang <wangjie125@huawei.com>
---
 net/hikp_net_lib.h                   |   1 +
 net/nic/nic_ft/hikp_nic_port_fault.c | 139 +++++++++++++++++++++++++++
 net/nic/nic_ft/hikp_nic_port_fault.h |  54 +++++++++++
 3 files changed, 194 insertions(+)
 create mode 100644 net/nic/nic_ft/hikp_nic_port_fault.c
 create mode 100644 net/nic/nic_ft/hikp_nic_port_fault.h

diff --git a/net/hikp_net_lib.h b/net/hikp_net_lib.h
index 06ae598..cb537ff 100644
--- a/net/hikp_net_lib.h
+++ b/net/hikp_net_lib.h
@@ -80,6 +80,7 @@ enum nic_cmd_type {
 	GET_GRO_INFO_CMD,
 	GET_NCSI_INFO_CMD,
 	GET_NOTIFY_PKT_CMD,
+	GET_PORT_FAULT_STATUS = 0xE,
 };
 
 enum roh_cmd_type {
diff --git a/net/nic/nic_ft/hikp_nic_port_fault.c b/net/nic/nic_ft/hikp_nic_port_fault.c
new file mode 100644
index 0000000..fd94214
--- /dev/null
+++ b/net/nic/nic_ft/hikp_nic_port_fault.c
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2023 Hisilicon Technologies Co., Ltd.
+ * Hikptool is licensed under Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ *
+ * See the Mulan PSL v2 for more details.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include "hikp_nic_port_fault.h"
+
+static struct tool_target g_port_fault_target;
+
+static int hikp_nic_port_fault_query(const struct bdf_t *bdf,
+				     struct nic_port_fault_status *info)
+{
+	struct nic_port_fault_req_para req = { 0 };
+	struct hikp_cmd_header header = { 0 };
+	struct nic_port_fault_rsp *rsp;
+	struct hikp_cmd_ret *cmd_ret;
+
+	req.bdf = *bdf;
+	hikp_cmd_init(&header, NIC_MOD, GET_PORT_FAULT_STATUS,
+		      NIC_PORT_FAULT_INFO_DUMP);
+	cmd_ret = hikp_cmd_alloc(&header, &req, sizeof(req));
+	if (cmd_ret == NULL || cmd_ret->status != 0) {
+		HIKP_ERROR_PRINT("fail to get port fault, retcode: %u\n",
+				 cmd_ret ? cmd_ret->status : EIO);
+		if (cmd_ret != NULL)
+			free(cmd_ret);
+
+		return -EIO;
+	}
+
+	rsp = (struct nic_port_fault_rsp *)cmd_ret->rsp_data;
+	*info = *(struct nic_port_fault_status *)rsp->data;
+	free(cmd_ret);
+
+	return 0;
+}
+
+static void hikp_nic_format_port_fault_info(struct nic_port_fault_status *info)
+{
+	if (info->cdr_core_status > NIC_PORT_FAULT_INVALID)
+		info->cdr_core_status = NIC_PORT_FAULT_INVALID;
+
+	if (info->cdr_flash_status > NIC_PORT_FAULT_INVALID)
+		info->cdr_flash_status = NIC_PORT_FAULT_INVALID;
+
+	if (info->fault_9545_status > NIC_PORT_FAULT_INVALID)
+		info->fault_9545_status = NIC_PORT_FAULT_INVALID;
+
+	if (info->hilink_ref_status > NIC_PORT_FAULT_INVALID)
+		info->hilink_ref_status = NIC_PORT_FAULT_INVALID;
+}
+
+static void hikp_nic_port_fault_show(struct nic_port_fault_status *info)
+{
+	const char *port_fault_info[] = {
+		"OK",
+		"Device error",
+		"Device not support",
+		"Invalid"
+	};
+
+	hikp_nic_format_port_fault_info(info);
+	printf("############ NIC port fault status ###############\n");
+	printf("cdr flash : %s.\n", port_fault_info[info->cdr_flash_status]);
+	printf("cdr core  : %s.\n", port_fault_info[info->cdr_core_status]);
+	printf("9545 fault: %s.\n", port_fault_info[info->fault_9545_status]);
+	printf("hilink ref: %s.\n", port_fault_info[info->hilink_ref_status]);
+	printf("#################### END #######################\n");
+}
+
+static void hikp_nic_port_fault_cmd_execute(struct major_cmd_ctrl *self)
+{
+	struct bdf_t *bdf = &g_port_fault_target.bdf;
+	struct nic_port_fault_status info;
+	int ret;
+
+	ret = hikp_nic_port_fault_query(bdf, &info);
+	if (ret != 0) {
+		snprintf(self->err_str, sizeof(self->err_str), "fail to get fault info.");
+		self->err_no = ret;
+		return;
+	}
+
+	hikp_nic_port_fault_show(&info);
+}
+
+static int hikp_nic_port_fault_cmd_help(struct major_cmd_ctrl *self, const char *argv)
+{
+	printf("\n  Usage: %s %s\n", self->cmd_ptr->name, "-i <device>");
+	printf("\n         %s\n", self->cmd_ptr->help_info);
+	printf("\n  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 or bdf id, e.g. eth0~7 or 0000:35:00.0");
+
+	return 0;
+}
+
+static int hikp_nic_port_fault_get_target(struct major_cmd_ctrl *self, const char *argv)
+{
+	self->err_no = tool_check_and_get_valid_bdf_id(argv, &g_port_fault_target);
+	if (self->err_no != 0) {
+		snprintf(self->err_str, sizeof(self->err_str), "unknown device!");
+		return self->err_no;
+	}
+
+	if (g_port_fault_target.bdf.dev_id != 0) {
+		snprintf(self->err_str, sizeof(self->err_str), "VF is not supported!");
+		self->err_no = -EOPNOTSUPP;
+		return self->err_no;
+	}
+
+	return 0;
+}
+
+static void cmd_nic_port_fault_init(void)
+{
+	struct major_cmd_ctrl *major_cmd = get_major_cmd();
+
+	major_cmd->option_count = 0;
+	major_cmd->execute = hikp_nic_port_fault_cmd_execute;
+
+	cmd_option_register("-h", "--help", false, hikp_nic_port_fault_cmd_help);
+	cmd_option_register("-i", "--interface", true, hikp_nic_port_fault_get_target);
+}
+
+HIKP_CMD_DECLARE("nic_port_fault", "dump port fault of nic!", cmd_nic_port_fault_init);
diff --git a/net/nic/nic_ft/hikp_nic_port_fault.h b/net/nic/nic_ft/hikp_nic_port_fault.h
new file mode 100644
index 0000000..7db54f7
--- /dev/null
+++ b/net/nic/nic_ft/hikp_nic_port_fault.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2023 Hisilicon Technologies Co., Ltd.
+ * Hikptool is licensed under Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ *
+ * See the Mulan PSL v2 for more details.
+ */
+
+#ifndef HIKP_NIC_PORT_FAULT_H
+#define HIKP_NIC_PORT_FAULT_H
+
+#include "hikp_net_lib.h"
+
+enum nic_port_fault_sub_cmd_type {
+	NIC_PORT_FAULT_INFO_DUMP,
+};
+
+enum nic_port_fault_info_type {
+	NIC_PORT_FAULT_OK,
+	NIC_PORT_FAULT_ERR,
+	NIC_PORT_FAULT_NOTSUP,
+	NIC_PORT_FAULT_INVALID
+};
+
+struct nic_port_fault_req_para {
+	struct bdf_t bdf;
+	uint8_t block_id;
+};
+
+struct nic_port_fault_rsp_head {
+	uint8_t total_blk_num;
+	uint8_t curr_blk_size;
+	uint16_t rsv;
+};
+
+#define NIC_PORT_FAULT_MAX_RSP_DATA    1
+struct nic_port_fault_rsp {
+	struct nic_port_fault_rsp_head head;
+	uint32_t data[NIC_PORT_FAULT_MAX_RSP_DATA];
+};
+
+struct nic_port_fault_status {
+	uint8_t cdr_flash_status;
+	uint8_t fault_9545_status;
+	uint8_t cdr_core_status;
+	uint8_t hilink_ref_status;
+};
+
+#endif /* HIKP_NIC_PORT_FAULT_H */
-- 
2.30.0

