15e126e7cSLuo bin // SPDX-License-Identifier: GPL-2.0
25e126e7cSLuo bin /* Huawei HiNIC PCI Express Linux driver
35e126e7cSLuo bin  * Copyright(c) 2017 Huawei Technologies Co., Ltd
45e126e7cSLuo bin  *
55e126e7cSLuo bin  * This program is free software; you can redistribute it and/or modify it
65e126e7cSLuo bin  * under the terms and conditions of the GNU General Public License,
75e126e7cSLuo bin  * version 2, as published by the Free Software Foundation.
85e126e7cSLuo bin  *
95e126e7cSLuo bin  * This program is distributed in the hope it will be useful, but WITHOUT
105e126e7cSLuo bin  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
115e126e7cSLuo bin  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
125e126e7cSLuo bin  * for more details.
135e126e7cSLuo bin  *
145e126e7cSLuo bin  */
155e126e7cSLuo bin #include <linux/netlink.h>
165e126e7cSLuo bin #include <net/devlink.h>
175e126e7cSLuo bin #include <linux/firmware.h>
185e126e7cSLuo bin 
195e126e7cSLuo bin #include "hinic_port.h"
205e126e7cSLuo bin #include "hinic_devlink.h"
21c15850c7SLuo bin #include "hinic_hw_dev.h"
225e126e7cSLuo bin 
check_image_valid(struct hinic_devlink_priv * priv,const u8 * buf,u32 image_size,struct host_image_st * host_image)235e126e7cSLuo bin static bool check_image_valid(struct hinic_devlink_priv *priv, const u8 *buf,
245e126e7cSLuo bin 			      u32 image_size, struct host_image_st *host_image)
255e126e7cSLuo bin {
265e126e7cSLuo bin 	struct fw_image_st *fw_image = NULL;
275e126e7cSLuo bin 	u32 len = 0;
285e126e7cSLuo bin 	u32 i;
295e126e7cSLuo bin 
305e126e7cSLuo bin 	fw_image = (struct fw_image_st *)buf;
315e126e7cSLuo bin 
325e126e7cSLuo bin 	if (fw_image->fw_magic != HINIC_MAGIC_NUM) {
335e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Wrong fw_magic read from file, fw_magic: 0x%x\n",
345e126e7cSLuo bin 			fw_image->fw_magic);
355e126e7cSLuo bin 		return false;
365e126e7cSLuo bin 	}
375e126e7cSLuo bin 
385e126e7cSLuo bin 	if (fw_image->fw_info.fw_section_cnt > MAX_FW_TYPE_NUM) {
395e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Wrong fw_type_num read from file, fw_type_num: 0x%x\n",
405e126e7cSLuo bin 			fw_image->fw_info.fw_section_cnt);
415e126e7cSLuo bin 		return false;
425e126e7cSLuo bin 	}
435e126e7cSLuo bin 
445e126e7cSLuo bin 	for (i = 0; i < fw_image->fw_info.fw_section_cnt; i++) {
455e126e7cSLuo bin 		len += fw_image->fw_section_info[i].fw_section_len;
46*1e70212eSKees Cook 		host_image->image_section_info[i] = fw_image->fw_section_info[i];
475e126e7cSLuo bin 	}
485e126e7cSLuo bin 
495e126e7cSLuo bin 	if (len != fw_image->fw_len ||
505e126e7cSLuo bin 	    (fw_image->fw_len + UPDATEFW_IMAGE_HEAD_SIZE) != image_size) {
515e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Wrong data size read from file\n");
525e126e7cSLuo bin 		return false;
535e126e7cSLuo bin 	}
545e126e7cSLuo bin 
555e126e7cSLuo bin 	host_image->image_info.up_total_len = fw_image->fw_len;
565e126e7cSLuo bin 	host_image->image_info.fw_version = fw_image->fw_version;
575e126e7cSLuo bin 	host_image->section_type_num = fw_image->fw_info.fw_section_cnt;
585e126e7cSLuo bin 	host_image->device_id = fw_image->device_id;
595e126e7cSLuo bin 
605e126e7cSLuo bin 	return true;
615e126e7cSLuo bin }
625e126e7cSLuo bin 
check_image_integrity(struct hinic_devlink_priv * priv,struct host_image_st * host_image,u32 update_type)635e126e7cSLuo bin static bool check_image_integrity(struct hinic_devlink_priv *priv,
645e126e7cSLuo bin 				  struct host_image_st *host_image,
655e126e7cSLuo bin 				  u32 update_type)
665e126e7cSLuo bin {
675e126e7cSLuo bin 	u32 collect_section_type = 0;
685e126e7cSLuo bin 	u32 i, type;
695e126e7cSLuo bin 
705e126e7cSLuo bin 	for (i = 0; i < host_image->section_type_num; i++) {
715e126e7cSLuo bin 		type = host_image->image_section_info[i].fw_section_type;
725e126e7cSLuo bin 		if (collect_section_type & (1U << type)) {
735e126e7cSLuo bin 			dev_err(&priv->hwdev->hwif->pdev->dev, "Duplicate section type: %u\n",
745e126e7cSLuo bin 				type);
755e126e7cSLuo bin 			return false;
765e126e7cSLuo bin 		}
775e126e7cSLuo bin 		collect_section_type |= (1U << type);
785e126e7cSLuo bin 	}
795e126e7cSLuo bin 
805e126e7cSLuo bin 	if (update_type == FW_UPDATE_COLD &&
815e126e7cSLuo bin 	    (((collect_section_type & _IMAGE_COLD_SUB_MODULES_MUST_IN) ==
825e126e7cSLuo bin 	       _IMAGE_COLD_SUB_MODULES_MUST_IN) ||
835e126e7cSLuo bin 	      collect_section_type == _IMAGE_CFG_SUB_MODULES_MUST_IN))
845e126e7cSLuo bin 		return true;
855e126e7cSLuo bin 
865e126e7cSLuo bin 	if (update_type == FW_UPDATE_HOT &&
875e126e7cSLuo bin 	    (collect_section_type & _IMAGE_HOT_SUB_MODULES_MUST_IN) ==
885e126e7cSLuo bin 	    _IMAGE_HOT_SUB_MODULES_MUST_IN)
895e126e7cSLuo bin 		return true;
905e126e7cSLuo bin 
915e126e7cSLuo bin 	if (update_type == FW_UPDATE_COLD)
925e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Check file integrity failed, valid: 0x%x or 0x%lx, current: 0x%x\n",
935e126e7cSLuo bin 			_IMAGE_COLD_SUB_MODULES_MUST_IN,
945e126e7cSLuo bin 			_IMAGE_CFG_SUB_MODULES_MUST_IN, collect_section_type);
955e126e7cSLuo bin 	else
965e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Check file integrity failed, valid:0x%x, current: 0x%x\n",
975e126e7cSLuo bin 			_IMAGE_HOT_SUB_MODULES_MUST_IN, collect_section_type);
985e126e7cSLuo bin 
995e126e7cSLuo bin 	return false;
1005e126e7cSLuo bin }
1015e126e7cSLuo bin 
check_image_device_type(struct hinic_devlink_priv * priv,u32 image_device_type)1025e126e7cSLuo bin static int check_image_device_type(struct hinic_devlink_priv *priv,
1035e126e7cSLuo bin 				   u32 image_device_type)
1045e126e7cSLuo bin {
1055e126e7cSLuo bin 	struct hinic_comm_board_info board_info = {0};
1065e126e7cSLuo bin 
1075e126e7cSLuo bin 	if (hinic_get_board_info(priv->hwdev, &board_info)) {
1085e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Get board info failed\n");
1095e126e7cSLuo bin 		return false;
1105e126e7cSLuo bin 	}
1115e126e7cSLuo bin 
1125e126e7cSLuo bin 	if (image_device_type == board_info.info.board_type)
1135e126e7cSLuo bin 		return true;
1145e126e7cSLuo bin 
1155e126e7cSLuo bin 	dev_err(&priv->hwdev->hwif->pdev->dev, "The device type of upgrade file doesn't match the device type of current firmware, please check the upgrade file\n");
1165e126e7cSLuo bin 	dev_err(&priv->hwdev->hwif->pdev->dev, "The image device type: 0x%x, firmware device type: 0x%x\n",
1175e126e7cSLuo bin 		image_device_type, board_info.info.board_type);
1185e126e7cSLuo bin 
1195e126e7cSLuo bin 	return false;
1205e126e7cSLuo bin }
1215e126e7cSLuo bin 
hinic_flash_fw(struct hinic_devlink_priv * priv,const u8 * data,struct host_image_st * host_image)1225e126e7cSLuo bin static int hinic_flash_fw(struct hinic_devlink_priv *priv, const u8 *data,
1235e126e7cSLuo bin 			  struct host_image_st *host_image)
1245e126e7cSLuo bin {
1255e126e7cSLuo bin 	u32 section_remain_send_len, send_fragment_len, send_pos, up_total_len;
1265e126e7cSLuo bin 	struct hinic_cmd_update_fw *fw_update_msg = NULL;
1275e126e7cSLuo bin 	u32 section_type, section_crc, section_version;
1285e126e7cSLuo bin 	u32 i, len, section_len, section_offset;
1295e126e7cSLuo bin 	u16 out_size = sizeof(*fw_update_msg);
1305e126e7cSLuo bin 	int total_len_flag = 0;
1315e126e7cSLuo bin 	int err;
1325e126e7cSLuo bin 
1335e126e7cSLuo bin 	fw_update_msg = kzalloc(sizeof(*fw_update_msg), GFP_KERNEL);
1345e126e7cSLuo bin 	if (!fw_update_msg)
1355e126e7cSLuo bin 		return -ENOMEM;
1365e126e7cSLuo bin 
1375e126e7cSLuo bin 	up_total_len = host_image->image_info.up_total_len;
1385e126e7cSLuo bin 
1395e126e7cSLuo bin 	for (i = 0; i < host_image->section_type_num; i++) {
1405e126e7cSLuo bin 		len = host_image->image_section_info[i].fw_section_len;
1415e126e7cSLuo bin 		if (host_image->image_section_info[i].fw_section_type ==
1425e126e7cSLuo bin 		    UP_FW_UPDATE_BOOT) {
1435e126e7cSLuo bin 			up_total_len = up_total_len - len;
1445e126e7cSLuo bin 			break;
1455e126e7cSLuo bin 		}
1465e126e7cSLuo bin 	}
1475e126e7cSLuo bin 
1485e126e7cSLuo bin 	for (i = 0; i < host_image->section_type_num; i++) {
1495e126e7cSLuo bin 		section_len =
1505e126e7cSLuo bin 			host_image->image_section_info[i].fw_section_len;
1515e126e7cSLuo bin 		section_offset =
1525e126e7cSLuo bin 			host_image->image_section_info[i].fw_section_offset;
1535e126e7cSLuo bin 		section_remain_send_len = section_len;
1545e126e7cSLuo bin 		section_type =
1555e126e7cSLuo bin 			host_image->image_section_info[i].fw_section_type;
1565e126e7cSLuo bin 		section_crc = host_image->image_section_info[i].fw_section_crc;
1575e126e7cSLuo bin 		section_version =
1585e126e7cSLuo bin 			host_image->image_section_info[i].fw_section_version;
1595e126e7cSLuo bin 
1605e126e7cSLuo bin 		if (section_type == UP_FW_UPDATE_BOOT)
1615e126e7cSLuo bin 			continue;
1625e126e7cSLuo bin 
1635e126e7cSLuo bin 		send_fragment_len = 0;
1645e126e7cSLuo bin 		send_pos = 0;
1655e126e7cSLuo bin 
1665e126e7cSLuo bin 		while (section_remain_send_len > 0) {
1675e126e7cSLuo bin 			if (!total_len_flag) {
1685e126e7cSLuo bin 				fw_update_msg->total_len = up_total_len;
1695e126e7cSLuo bin 				total_len_flag = 1;
1705e126e7cSLuo bin 			} else {
1715e126e7cSLuo bin 				fw_update_msg->total_len = 0;
1725e126e7cSLuo bin 			}
1735e126e7cSLuo bin 
1745e126e7cSLuo bin 			memset(fw_update_msg->data, 0, MAX_FW_FRAGMENT_LEN);
1755e126e7cSLuo bin 
1765e126e7cSLuo bin 			fw_update_msg->ctl_info.SF =
1775e126e7cSLuo bin 				(section_remain_send_len == section_len) ?
1785e126e7cSLuo bin 				true : false;
1795e126e7cSLuo bin 			fw_update_msg->section_info.FW_section_CRC = section_crc;
1805e126e7cSLuo bin 			fw_update_msg->fw_section_version = section_version;
1815e126e7cSLuo bin 			fw_update_msg->ctl_info.flag = UP_TYPE_A;
1825e126e7cSLuo bin 
1835e126e7cSLuo bin 			if (section_type <= UP_FW_UPDATE_UP_DATA_B) {
1845e126e7cSLuo bin 				fw_update_msg->section_info.FW_section_type =
1855e126e7cSLuo bin 					(section_type % 2) ?
1865e126e7cSLuo bin 					UP_FW_UPDATE_UP_DATA :
1875e126e7cSLuo bin 					UP_FW_UPDATE_UP_TEXT;
1885e126e7cSLuo bin 
1895e126e7cSLuo bin 				fw_update_msg->ctl_info.flag = UP_TYPE_B;
1905e126e7cSLuo bin 				if (section_type <= UP_FW_UPDATE_UP_DATA_A)
1915e126e7cSLuo bin 					fw_update_msg->ctl_info.flag = UP_TYPE_A;
1925e126e7cSLuo bin 			} else {
1935e126e7cSLuo bin 				fw_update_msg->section_info.FW_section_type =
1945e126e7cSLuo bin 					section_type - 0x2;
1955e126e7cSLuo bin 			}
1965e126e7cSLuo bin 
1975e126e7cSLuo bin 			fw_update_msg->setion_total_len = section_len;
1985e126e7cSLuo bin 			fw_update_msg->section_offset = send_pos;
1995e126e7cSLuo bin 
2005e126e7cSLuo bin 			if (section_remain_send_len <= MAX_FW_FRAGMENT_LEN) {
2015e126e7cSLuo bin 				fw_update_msg->ctl_info.SL = true;
2025e126e7cSLuo bin 				fw_update_msg->ctl_info.fragment_len =
2035e126e7cSLuo bin 					section_remain_send_len;
2045e126e7cSLuo bin 				send_fragment_len += section_remain_send_len;
2055e126e7cSLuo bin 			} else {
2065e126e7cSLuo bin 				fw_update_msg->ctl_info.SL = false;
2075e126e7cSLuo bin 				fw_update_msg->ctl_info.fragment_len =
2085e126e7cSLuo bin 					MAX_FW_FRAGMENT_LEN;
2095e126e7cSLuo bin 				send_fragment_len += MAX_FW_FRAGMENT_LEN;
2105e126e7cSLuo bin 			}
2115e126e7cSLuo bin 
2125e126e7cSLuo bin 			memcpy(fw_update_msg->data,
2135e126e7cSLuo bin 			       data + UPDATEFW_IMAGE_HEAD_SIZE +
2145e126e7cSLuo bin 			       section_offset + send_pos,
2155e126e7cSLuo bin 			       fw_update_msg->ctl_info.fragment_len);
2165e126e7cSLuo bin 
2175e126e7cSLuo bin 			err = hinic_port_msg_cmd(priv->hwdev,
2185e126e7cSLuo bin 						 HINIC_PORT_CMD_UPDATE_FW,
2195e126e7cSLuo bin 						 fw_update_msg,
2205e126e7cSLuo bin 						 sizeof(*fw_update_msg),
2215e126e7cSLuo bin 						 fw_update_msg, &out_size);
2225e126e7cSLuo bin 			if (err || !out_size || fw_update_msg->status) {
2235e126e7cSLuo bin 				dev_err(&priv->hwdev->hwif->pdev->dev, "Failed to update firmware, err: %d, status: 0x%x, out size: 0x%x\n",
2245e126e7cSLuo bin 					err, fw_update_msg->status, out_size);
2255e126e7cSLuo bin 				err = fw_update_msg->status ?
2265e126e7cSLuo bin 					fw_update_msg->status : -EIO;
2275e126e7cSLuo bin 				kfree(fw_update_msg);
2285e126e7cSLuo bin 				return err;
2295e126e7cSLuo bin 			}
2305e126e7cSLuo bin 
2315e126e7cSLuo bin 			send_pos = send_fragment_len;
2325e126e7cSLuo bin 			section_remain_send_len = section_len -
2335e126e7cSLuo bin 						  send_fragment_len;
2345e126e7cSLuo bin 		}
2355e126e7cSLuo bin 	}
2365e126e7cSLuo bin 
2375e126e7cSLuo bin 	kfree(fw_update_msg);
2385e126e7cSLuo bin 
2395e126e7cSLuo bin 	return 0;
2405e126e7cSLuo bin }
2415e126e7cSLuo bin 
hinic_firmware_update(struct hinic_devlink_priv * priv,const struct firmware * fw,struct netlink_ext_ack * extack)2425e126e7cSLuo bin static int hinic_firmware_update(struct hinic_devlink_priv *priv,
2435e126e7cSLuo bin 				 const struct firmware *fw,
2445e126e7cSLuo bin 				 struct netlink_ext_ack *extack)
2455e126e7cSLuo bin {
2465e126e7cSLuo bin 	struct host_image_st host_image;
2475e126e7cSLuo bin 	int err;
2485e126e7cSLuo bin 
2495e126e7cSLuo bin 	memset(&host_image, 0, sizeof(struct host_image_st));
2505e126e7cSLuo bin 
2515e126e7cSLuo bin 	if (!check_image_valid(priv, fw->data, fw->size, &host_image) ||
2525e126e7cSLuo bin 	    !check_image_integrity(priv, &host_image, FW_UPDATE_COLD) ||
2535e126e7cSLuo bin 	    !check_image_device_type(priv, host_image.device_id)) {
2545e126e7cSLuo bin 		NL_SET_ERR_MSG_MOD(extack, "Check image failed");
2555e126e7cSLuo bin 		return -EINVAL;
2565e126e7cSLuo bin 	}
2575e126e7cSLuo bin 
2585e126e7cSLuo bin 	dev_info(&priv->hwdev->hwif->pdev->dev, "Flash firmware begin\n");
2595e126e7cSLuo bin 
2605e126e7cSLuo bin 	err = hinic_flash_fw(priv, fw->data, &host_image);
2615e126e7cSLuo bin 	if (err) {
2625e126e7cSLuo bin 		if (err == HINIC_FW_DISMATCH_ERROR) {
2635e126e7cSLuo bin 			dev_err(&priv->hwdev->hwif->pdev->dev, "Firmware image doesn't match this card, please use newer image, err: %d\n",
2645e126e7cSLuo bin 				err);
2655e126e7cSLuo bin 			NL_SET_ERR_MSG_MOD(extack,
2665e126e7cSLuo bin 					   "Firmware image doesn't match this card, please use newer image");
2675e126e7cSLuo bin 		} else {
2685e126e7cSLuo bin 			dev_err(&priv->hwdev->hwif->pdev->dev, "Send firmware image data failed, err: %d\n",
2695e126e7cSLuo bin 				err);
2705e126e7cSLuo bin 			NL_SET_ERR_MSG_MOD(extack, "Send firmware image data failed");
2715e126e7cSLuo bin 		}
2725e126e7cSLuo bin 
2735e126e7cSLuo bin 		return err;
2745e126e7cSLuo bin 	}
2755e126e7cSLuo bin 
2765e126e7cSLuo bin 	dev_info(&priv->hwdev->hwif->pdev->dev, "Flash firmware end\n");
2775e126e7cSLuo bin 
2785e126e7cSLuo bin 	return 0;
2795e126e7cSLuo bin }
2805e126e7cSLuo bin 
hinic_devlink_flash_update(struct devlink * devlink,struct devlink_flash_update_params * params,struct netlink_ext_ack * extack)2815e126e7cSLuo bin static int hinic_devlink_flash_update(struct devlink *devlink,
282bc75c054SJacob Keller 				      struct devlink_flash_update_params *params,
2835e126e7cSLuo bin 				      struct netlink_ext_ack *extack)
2845e126e7cSLuo bin {
2855e126e7cSLuo bin 	struct hinic_devlink_priv *priv = devlink_priv(devlink);
2865e126e7cSLuo bin 
287b44cfd4fSJacob Keller 	return hinic_firmware_update(priv, params->fw, extack);
2885e126e7cSLuo bin }
2895e126e7cSLuo bin 
2905e126e7cSLuo bin static const struct devlink_ops hinic_devlink_ops = {
2915e126e7cSLuo bin 	.flash_update = hinic_devlink_flash_update,
2925e126e7cSLuo bin };
2935e126e7cSLuo bin 
hinic_devlink_alloc(struct device * dev)294919d13a7SLeon Romanovsky struct devlink *hinic_devlink_alloc(struct device *dev)
2955e126e7cSLuo bin {
296919d13a7SLeon Romanovsky 	return devlink_alloc(&hinic_devlink_ops, sizeof(struct hinic_dev), dev);
2975e126e7cSLuo bin }
2985e126e7cSLuo bin 
hinic_devlink_free(struct devlink * devlink)2995e126e7cSLuo bin void hinic_devlink_free(struct devlink *devlink)
3005e126e7cSLuo bin {
3015e126e7cSLuo bin 	devlink_free(devlink);
3025e126e7cSLuo bin }
3035e126e7cSLuo bin 
hinic_devlink_register(struct hinic_devlink_priv * priv)304db4278c5SLeon Romanovsky void hinic_devlink_register(struct hinic_devlink_priv *priv)
3055e126e7cSLuo bin {
306c15850c7SLuo bin 	struct devlink *devlink = priv_to_devlink(priv);
307c15850c7SLuo bin 
308db4278c5SLeon Romanovsky 	devlink_register(devlink);
3095e126e7cSLuo bin }
3105e126e7cSLuo bin 
hinic_devlink_unregister(struct hinic_devlink_priv * priv)311c15850c7SLuo bin void hinic_devlink_unregister(struct hinic_devlink_priv *priv)
3125e126e7cSLuo bin {
313c15850c7SLuo bin 	struct devlink *devlink = priv_to_devlink(priv);
314c15850c7SLuo bin 
3155e126e7cSLuo bin 	devlink_unregister(devlink);
3165e126e7cSLuo bin }
317c15850c7SLuo bin 
chip_fault_show(struct devlink_fmsg * fmsg,struct hinic_fault_event * event)318c15850c7SLuo bin static int chip_fault_show(struct devlink_fmsg *fmsg,
319c15850c7SLuo bin 			   struct hinic_fault_event *event)
320c15850c7SLuo bin {
3211dab5877SLuo bin 	const char * const level_str[FAULT_LEVEL_MAX + 1] = {
3221dab5877SLuo bin 		"fatal", "reset", "flr", "general", "suggestion", "Unknown"};
3231dab5877SLuo bin 	u8 fault_level;
324c15850c7SLuo bin 	int err;
325c15850c7SLuo bin 
3261dab5877SLuo bin 	fault_level = (event->event.chip.err_level < FAULT_LEVEL_MAX) ?
3271dab5877SLuo bin 		event->event.chip.err_level : FAULT_LEVEL_MAX;
3281dab5877SLuo bin 	if (fault_level == FAULT_LEVEL_SERIOUS_FLR) {
329c15850c7SLuo bin 		err = devlink_fmsg_u32_pair_put(fmsg, "Function level err func_id",
330c15850c7SLuo bin 						(u32)event->event.chip.func_id);
331c15850c7SLuo bin 		if (err)
332c15850c7SLuo bin 			return err;
333c15850c7SLuo bin 	}
334c15850c7SLuo bin 
335c15850c7SLuo bin 	err = devlink_fmsg_u8_pair_put(fmsg, "module_id", event->event.chip.node_id);
336c15850c7SLuo bin 	if (err)
337c15850c7SLuo bin 		return err;
338c15850c7SLuo bin 
339c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "err_type", (u32)event->event.chip.err_type);
340c15850c7SLuo bin 	if (err)
341c15850c7SLuo bin 		return err;
342c15850c7SLuo bin 
3431dab5877SLuo bin 	err = devlink_fmsg_string_pair_put(fmsg, "err_level", level_str[fault_level]);
344c15850c7SLuo bin 	if (err)
345c15850c7SLuo bin 		return err;
346c15850c7SLuo bin 
347c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "err_csr_addr",
348c15850c7SLuo bin 					event->event.chip.err_csr_addr);
349c15850c7SLuo bin 	if (err)
350c15850c7SLuo bin 		return err;
351c15850c7SLuo bin 
352c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "err_csr_value",
353c15850c7SLuo bin 					event->event.chip.err_csr_value);
354c15850c7SLuo bin 	if (err)
355c15850c7SLuo bin 		return err;
356c15850c7SLuo bin 
357c15850c7SLuo bin 	return 0;
358c15850c7SLuo bin }
359c15850c7SLuo bin 
fault_report_show(struct devlink_fmsg * fmsg,struct hinic_fault_event * event)360c15850c7SLuo bin static int fault_report_show(struct devlink_fmsg *fmsg,
361c15850c7SLuo bin 			     struct hinic_fault_event *event)
362c15850c7SLuo bin {
3631dab5877SLuo bin 	const char * const type_str[FAULT_TYPE_MAX + 1] = {
364c15850c7SLuo bin 		"chip", "ucode", "mem rd timeout", "mem wr timeout",
3651dab5877SLuo bin 		"reg rd timeout", "reg wr timeout", "phy fault", "Unknown"};
3661dab5877SLuo bin 	u8 fault_type;
367c15850c7SLuo bin 	int err;
368c15850c7SLuo bin 
3691dab5877SLuo bin 	fault_type = (event->type < FAULT_TYPE_MAX) ? event->type : FAULT_TYPE_MAX;
370c15850c7SLuo bin 
3711dab5877SLuo bin 	err = devlink_fmsg_string_pair_put(fmsg, "Fault type", type_str[fault_type]);
372c15850c7SLuo bin 	if (err)
373c15850c7SLuo bin 		return err;
374c15850c7SLuo bin 
375c15850c7SLuo bin 	err = devlink_fmsg_binary_pair_put(fmsg, "Fault raw data",
376c15850c7SLuo bin 					   event->event.val, sizeof(event->event.val));
377c15850c7SLuo bin 	if (err)
378c15850c7SLuo bin 		return err;
379c15850c7SLuo bin 
380c15850c7SLuo bin 	switch (event->type) {
381c15850c7SLuo bin 	case FAULT_TYPE_CHIP:
382c15850c7SLuo bin 		err = chip_fault_show(fmsg, event);
383c15850c7SLuo bin 		if (err)
384c15850c7SLuo bin 			return err;
385c15850c7SLuo bin 		break;
386c15850c7SLuo bin 	case FAULT_TYPE_UCODE:
387c15850c7SLuo bin 		err = devlink_fmsg_u8_pair_put(fmsg, "Cause_id", event->event.ucode.cause_id);
388c15850c7SLuo bin 		if (err)
389c15850c7SLuo bin 			return err;
390c15850c7SLuo bin 		err = devlink_fmsg_u8_pair_put(fmsg, "core_id", event->event.ucode.core_id);
391c15850c7SLuo bin 		if (err)
392c15850c7SLuo bin 			return err;
393c15850c7SLuo bin 		err = devlink_fmsg_u8_pair_put(fmsg, "c_id", event->event.ucode.c_id);
394c15850c7SLuo bin 		if (err)
395c15850c7SLuo bin 			return err;
396c15850c7SLuo bin 		err = devlink_fmsg_u8_pair_put(fmsg, "epc", event->event.ucode.epc);
397c15850c7SLuo bin 		if (err)
398c15850c7SLuo bin 			return err;
399c15850c7SLuo bin 		break;
400c15850c7SLuo bin 	case FAULT_TYPE_MEM_RD_TIMEOUT:
401c15850c7SLuo bin 	case FAULT_TYPE_MEM_WR_TIMEOUT:
402c15850c7SLuo bin 		err = devlink_fmsg_u32_pair_put(fmsg, "Err_csr_ctrl",
403c15850c7SLuo bin 						event->event.mem_timeout.err_csr_ctrl);
404c15850c7SLuo bin 		if (err)
405c15850c7SLuo bin 			return err;
406c15850c7SLuo bin 		err = devlink_fmsg_u32_pair_put(fmsg, "err_csr_data",
407c15850c7SLuo bin 						event->event.mem_timeout.err_csr_data);
408c15850c7SLuo bin 		if (err)
409c15850c7SLuo bin 			return err;
410c15850c7SLuo bin 		err = devlink_fmsg_u32_pair_put(fmsg, "ctrl_tab",
411c15850c7SLuo bin 						event->event.mem_timeout.ctrl_tab);
412c15850c7SLuo bin 		if (err)
413c15850c7SLuo bin 			return err;
414c15850c7SLuo bin 		err = devlink_fmsg_u32_pair_put(fmsg, "mem_index",
415c15850c7SLuo bin 						event->event.mem_timeout.mem_index);
416c15850c7SLuo bin 		if (err)
417c15850c7SLuo bin 			return err;
418c15850c7SLuo bin 		break;
419c15850c7SLuo bin 	case FAULT_TYPE_REG_RD_TIMEOUT:
420c15850c7SLuo bin 	case FAULT_TYPE_REG_WR_TIMEOUT:
421c15850c7SLuo bin 		err = devlink_fmsg_u32_pair_put(fmsg, "Err_csr", event->event.reg_timeout.err_csr);
422c15850c7SLuo bin 		if (err)
423c15850c7SLuo bin 			return err;
424c15850c7SLuo bin 		break;
425c15850c7SLuo bin 	case FAULT_TYPE_PHY_FAULT:
426c15850c7SLuo bin 		err = devlink_fmsg_u8_pair_put(fmsg, "Op_type", event->event.phy_fault.op_type);
427c15850c7SLuo bin 		if (err)
428c15850c7SLuo bin 			return err;
429c15850c7SLuo bin 		err = devlink_fmsg_u8_pair_put(fmsg, "port_id", event->event.phy_fault.port_id);
430c15850c7SLuo bin 		if (err)
431c15850c7SLuo bin 			return err;
432c15850c7SLuo bin 		err = devlink_fmsg_u8_pair_put(fmsg, "dev_ad", event->event.phy_fault.dev_ad);
433c15850c7SLuo bin 		if (err)
434c15850c7SLuo bin 			return err;
435c15850c7SLuo bin 
436c15850c7SLuo bin 		err = devlink_fmsg_u32_pair_put(fmsg, "csr_addr", event->event.phy_fault.csr_addr);
437c15850c7SLuo bin 		if (err)
438c15850c7SLuo bin 			return err;
439c15850c7SLuo bin 		err = devlink_fmsg_u32_pair_put(fmsg, "op_data", event->event.phy_fault.op_data);
440c15850c7SLuo bin 		if (err)
441c15850c7SLuo bin 			return err;
442c15850c7SLuo bin 		break;
443c15850c7SLuo bin 	default:
444c15850c7SLuo bin 		break;
445c15850c7SLuo bin 	}
446c15850c7SLuo bin 
447c15850c7SLuo bin 	return 0;
448c15850c7SLuo bin }
449c15850c7SLuo bin 
hinic_hw_reporter_dump(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,void * priv_ctx,struct netlink_ext_ack * extack)450c15850c7SLuo bin static int hinic_hw_reporter_dump(struct devlink_health_reporter *reporter,
451c15850c7SLuo bin 				  struct devlink_fmsg *fmsg, void *priv_ctx,
452c15850c7SLuo bin 				  struct netlink_ext_ack *extack)
453c15850c7SLuo bin {
454c15850c7SLuo bin 	if (priv_ctx)
455c15850c7SLuo bin 		return fault_report_show(fmsg, priv_ctx);
456c15850c7SLuo bin 
457c15850c7SLuo bin 	return 0;
458c15850c7SLuo bin }
459c15850c7SLuo bin 
mgmt_watchdog_report_show(struct devlink_fmsg * fmsg,struct hinic_mgmt_watchdog_info * watchdog_info)460c15850c7SLuo bin static int mgmt_watchdog_report_show(struct devlink_fmsg *fmsg,
461c15850c7SLuo bin 				     struct hinic_mgmt_watchdog_info *watchdog_info)
462c15850c7SLuo bin {
463c15850c7SLuo bin 	int err;
464c15850c7SLuo bin 
465c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "Mgmt deadloop time_h", watchdog_info->curr_time_h);
466c15850c7SLuo bin 	if (err)
467c15850c7SLuo bin 		return err;
468c15850c7SLuo bin 
469c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "time_l", watchdog_info->curr_time_l);
470c15850c7SLuo bin 	if (err)
471c15850c7SLuo bin 		return err;
472c15850c7SLuo bin 
473c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "task_id", watchdog_info->task_id);
474c15850c7SLuo bin 	if (err)
475c15850c7SLuo bin 		return err;
476c15850c7SLuo bin 
477c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "sp", watchdog_info->sp);
478c15850c7SLuo bin 	if (err)
479c15850c7SLuo bin 		return err;
480c15850c7SLuo bin 
481c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "stack_current_used", watchdog_info->curr_used);
482c15850c7SLuo bin 	if (err)
483c15850c7SLuo bin 		return err;
484c15850c7SLuo bin 
485c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "peak_used", watchdog_info->peak_used);
486c15850c7SLuo bin 	if (err)
487c15850c7SLuo bin 		return err;
488c15850c7SLuo bin 
489c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "\n Overflow_flag", watchdog_info->is_overflow);
490c15850c7SLuo bin 	if (err)
491c15850c7SLuo bin 		return err;
492c15850c7SLuo bin 
493c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "stack_top", watchdog_info->stack_top);
494c15850c7SLuo bin 	if (err)
495c15850c7SLuo bin 		return err;
496c15850c7SLuo bin 
497c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "stack_bottom", watchdog_info->stack_bottom);
498c15850c7SLuo bin 	if (err)
499c15850c7SLuo bin 		return err;
500c15850c7SLuo bin 
501c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "mgmt_pc", watchdog_info->pc);
502c15850c7SLuo bin 	if (err)
503c15850c7SLuo bin 		return err;
504c15850c7SLuo bin 
505c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "lr", watchdog_info->lr);
506c15850c7SLuo bin 	if (err)
507c15850c7SLuo bin 		return err;
508c15850c7SLuo bin 
509c15850c7SLuo bin 	err = devlink_fmsg_u32_pair_put(fmsg, "cpsr", watchdog_info->cpsr);
510c15850c7SLuo bin 	if (err)
511c15850c7SLuo bin 		return err;
512c15850c7SLuo bin 
513c15850c7SLuo bin 	err = devlink_fmsg_binary_pair_put(fmsg, "Mgmt register info",
514c15850c7SLuo bin 					   watchdog_info->reg, sizeof(watchdog_info->reg));
515c15850c7SLuo bin 	if (err)
516c15850c7SLuo bin 		return err;
517c15850c7SLuo bin 
518c15850c7SLuo bin 	err = devlink_fmsg_binary_pair_put(fmsg, "Mgmt dump stack(start from sp)",
519c15850c7SLuo bin 					   watchdog_info->data, sizeof(watchdog_info->data));
520c15850c7SLuo bin 	if (err)
521c15850c7SLuo bin 		return err;
522c15850c7SLuo bin 
523c15850c7SLuo bin 	return 0;
524c15850c7SLuo bin }
525c15850c7SLuo bin 
hinic_fw_reporter_dump(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,void * priv_ctx,struct netlink_ext_ack * extack)526c15850c7SLuo bin static int hinic_fw_reporter_dump(struct devlink_health_reporter *reporter,
527c15850c7SLuo bin 				  struct devlink_fmsg *fmsg, void *priv_ctx,
528c15850c7SLuo bin 				  struct netlink_ext_ack *extack)
529c15850c7SLuo bin {
530c15850c7SLuo bin 	if (priv_ctx)
531c15850c7SLuo bin 		return mgmt_watchdog_report_show(fmsg, priv_ctx);
532c15850c7SLuo bin 
533c15850c7SLuo bin 	return 0;
534c15850c7SLuo bin }
535c15850c7SLuo bin 
536c15850c7SLuo bin static const struct devlink_health_reporter_ops hinic_hw_fault_reporter_ops = {
537c15850c7SLuo bin 	.name = "hw",
538c15850c7SLuo bin 	.dump = hinic_hw_reporter_dump,
539c15850c7SLuo bin };
540c15850c7SLuo bin 
541c15850c7SLuo bin static const struct devlink_health_reporter_ops hinic_fw_fault_reporter_ops = {
542c15850c7SLuo bin 	.name = "fw",
543c15850c7SLuo bin 	.dump = hinic_fw_reporter_dump,
544c15850c7SLuo bin };
545c15850c7SLuo bin 
hinic_health_reporters_create(struct hinic_devlink_priv * priv)546c15850c7SLuo bin int hinic_health_reporters_create(struct hinic_devlink_priv *priv)
547c15850c7SLuo bin {
548c15850c7SLuo bin 	struct devlink *devlink = priv_to_devlink(priv);
549c15850c7SLuo bin 
550c15850c7SLuo bin 	priv->hw_fault_reporter =
551c15850c7SLuo bin 		devlink_health_reporter_create(devlink, &hinic_hw_fault_reporter_ops,
552c15850c7SLuo bin 					       0, priv);
553c15850c7SLuo bin 	if (IS_ERR(priv->hw_fault_reporter)) {
554c15850c7SLuo bin 		dev_warn(&priv->hwdev->hwif->pdev->dev, "Failed to create hw fault reporter, err: %ld\n",
555c15850c7SLuo bin 			 PTR_ERR(priv->hw_fault_reporter));
556c15850c7SLuo bin 		return PTR_ERR(priv->hw_fault_reporter);
557c15850c7SLuo bin 	}
558c15850c7SLuo bin 
559c15850c7SLuo bin 	priv->fw_fault_reporter =
560c15850c7SLuo bin 		devlink_health_reporter_create(devlink, &hinic_fw_fault_reporter_ops,
561c15850c7SLuo bin 					       0, priv);
562c15850c7SLuo bin 	if (IS_ERR(priv->fw_fault_reporter)) {
563c15850c7SLuo bin 		dev_warn(&priv->hwdev->hwif->pdev->dev, "Failed to create fw fault reporter, err: %ld\n",
564c15850c7SLuo bin 			 PTR_ERR(priv->fw_fault_reporter));
565c15850c7SLuo bin 		devlink_health_reporter_destroy(priv->hw_fault_reporter);
566c15850c7SLuo bin 		priv->hw_fault_reporter = NULL;
567c15850c7SLuo bin 		return PTR_ERR(priv->fw_fault_reporter);
568c15850c7SLuo bin 	}
569c15850c7SLuo bin 
570c15850c7SLuo bin 	return 0;
571c15850c7SLuo bin }
572c15850c7SLuo bin 
hinic_health_reporters_destroy(struct hinic_devlink_priv * priv)573c15850c7SLuo bin void hinic_health_reporters_destroy(struct hinic_devlink_priv *priv)
574c15850c7SLuo bin {
575c15850c7SLuo bin 	if (!IS_ERR_OR_NULL(priv->fw_fault_reporter)) {
576c15850c7SLuo bin 		devlink_health_reporter_destroy(priv->fw_fault_reporter);
577c15850c7SLuo bin 		priv->fw_fault_reporter = NULL;
578c15850c7SLuo bin 	}
579c15850c7SLuo bin 
580c15850c7SLuo bin 	if (!IS_ERR_OR_NULL(priv->hw_fault_reporter)) {
581c15850c7SLuo bin 		devlink_health_reporter_destroy(priv->hw_fault_reporter);
582c15850c7SLuo bin 		priv->hw_fault_reporter = NULL;
583c15850c7SLuo bin 	}
584c15850c7SLuo bin }
585