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