1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Huawei HiNIC PCI Express Linux driver
3*4882a593Smuzhiyun * Copyright(c) 2017 Huawei Technologies Co., Ltd
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify it
6*4882a593Smuzhiyun * under the terms and conditions of the GNU General Public License,
7*4882a593Smuzhiyun * version 2, as published by the Free Software Foundation.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This program is distributed in the hope it will be useful, but WITHOUT
10*4882a593Smuzhiyun * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12*4882a593Smuzhiyun * for more details.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun #include <linux/netlink.h>
16*4882a593Smuzhiyun #include <net/devlink.h>
17*4882a593Smuzhiyun #include <linux/firmware.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include "hinic_port.h"
20*4882a593Smuzhiyun #include "hinic_devlink.h"
21*4882a593Smuzhiyun #include "hinic_hw_dev.h"
22*4882a593Smuzhiyun
check_image_valid(struct hinic_devlink_priv * priv,const u8 * buf,u32 image_size,struct host_image_st * host_image)23*4882a593Smuzhiyun static bool check_image_valid(struct hinic_devlink_priv *priv, const u8 *buf,
24*4882a593Smuzhiyun u32 image_size, struct host_image_st *host_image)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun struct fw_image_st *fw_image = NULL;
27*4882a593Smuzhiyun u32 len = 0;
28*4882a593Smuzhiyun u32 i;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun fw_image = (struct fw_image_st *)buf;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun if (fw_image->fw_magic != HINIC_MAGIC_NUM) {
33*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "Wrong fw_magic read from file, fw_magic: 0x%x\n",
34*4882a593Smuzhiyun fw_image->fw_magic);
35*4882a593Smuzhiyun return false;
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun if (fw_image->fw_info.fw_section_cnt > MAX_FW_TYPE_NUM) {
39*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "Wrong fw_type_num read from file, fw_type_num: 0x%x\n",
40*4882a593Smuzhiyun fw_image->fw_info.fw_section_cnt);
41*4882a593Smuzhiyun return false;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun for (i = 0; i < fw_image->fw_info.fw_section_cnt; i++) {
45*4882a593Smuzhiyun len += fw_image->fw_section_info[i].fw_section_len;
46*4882a593Smuzhiyun memcpy(&host_image->image_section_info[i],
47*4882a593Smuzhiyun &fw_image->fw_section_info[i],
48*4882a593Smuzhiyun sizeof(struct fw_section_info_st));
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun if (len != fw_image->fw_len ||
52*4882a593Smuzhiyun (fw_image->fw_len + UPDATEFW_IMAGE_HEAD_SIZE) != image_size) {
53*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "Wrong data size read from file\n");
54*4882a593Smuzhiyun return false;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun host_image->image_info.up_total_len = fw_image->fw_len;
58*4882a593Smuzhiyun host_image->image_info.fw_version = fw_image->fw_version;
59*4882a593Smuzhiyun host_image->section_type_num = fw_image->fw_info.fw_section_cnt;
60*4882a593Smuzhiyun host_image->device_id = fw_image->device_id;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun return true;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
check_image_integrity(struct hinic_devlink_priv * priv,struct host_image_st * host_image,u32 update_type)65*4882a593Smuzhiyun static bool check_image_integrity(struct hinic_devlink_priv *priv,
66*4882a593Smuzhiyun struct host_image_st *host_image,
67*4882a593Smuzhiyun u32 update_type)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun u32 collect_section_type = 0;
70*4882a593Smuzhiyun u32 i, type;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun for (i = 0; i < host_image->section_type_num; i++) {
73*4882a593Smuzhiyun type = host_image->image_section_info[i].fw_section_type;
74*4882a593Smuzhiyun if (collect_section_type & (1U << type)) {
75*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "Duplicate section type: %u\n",
76*4882a593Smuzhiyun type);
77*4882a593Smuzhiyun return false;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun collect_section_type |= (1U << type);
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun if (update_type == FW_UPDATE_COLD &&
83*4882a593Smuzhiyun (((collect_section_type & _IMAGE_COLD_SUB_MODULES_MUST_IN) ==
84*4882a593Smuzhiyun _IMAGE_COLD_SUB_MODULES_MUST_IN) ||
85*4882a593Smuzhiyun collect_section_type == _IMAGE_CFG_SUB_MODULES_MUST_IN))
86*4882a593Smuzhiyun return true;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun if (update_type == FW_UPDATE_HOT &&
89*4882a593Smuzhiyun (collect_section_type & _IMAGE_HOT_SUB_MODULES_MUST_IN) ==
90*4882a593Smuzhiyun _IMAGE_HOT_SUB_MODULES_MUST_IN)
91*4882a593Smuzhiyun return true;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun if (update_type == FW_UPDATE_COLD)
94*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "Check file integrity failed, valid: 0x%x or 0x%lx, current: 0x%x\n",
95*4882a593Smuzhiyun _IMAGE_COLD_SUB_MODULES_MUST_IN,
96*4882a593Smuzhiyun _IMAGE_CFG_SUB_MODULES_MUST_IN, collect_section_type);
97*4882a593Smuzhiyun else
98*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "Check file integrity failed, valid:0x%x, current: 0x%x\n",
99*4882a593Smuzhiyun _IMAGE_HOT_SUB_MODULES_MUST_IN, collect_section_type);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun return false;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
check_image_device_type(struct hinic_devlink_priv * priv,u32 image_device_type)104*4882a593Smuzhiyun static int check_image_device_type(struct hinic_devlink_priv *priv,
105*4882a593Smuzhiyun u32 image_device_type)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun struct hinic_comm_board_info board_info = {0};
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun if (hinic_get_board_info(priv->hwdev, &board_info)) {
110*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "Get board info failed\n");
111*4882a593Smuzhiyun return false;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun if (image_device_type == board_info.info.board_type)
115*4882a593Smuzhiyun return true;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun 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");
118*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "The image device type: 0x%x, firmware device type: 0x%x\n",
119*4882a593Smuzhiyun image_device_type, board_info.info.board_type);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun return false;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
hinic_flash_fw(struct hinic_devlink_priv * priv,const u8 * data,struct host_image_st * host_image)124*4882a593Smuzhiyun static int hinic_flash_fw(struct hinic_devlink_priv *priv, const u8 *data,
125*4882a593Smuzhiyun struct host_image_st *host_image)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun u32 section_remain_send_len, send_fragment_len, send_pos, up_total_len;
128*4882a593Smuzhiyun struct hinic_cmd_update_fw *fw_update_msg = NULL;
129*4882a593Smuzhiyun u32 section_type, section_crc, section_version;
130*4882a593Smuzhiyun u32 i, len, section_len, section_offset;
131*4882a593Smuzhiyun u16 out_size = sizeof(*fw_update_msg);
132*4882a593Smuzhiyun int total_len_flag = 0;
133*4882a593Smuzhiyun int err;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun fw_update_msg = kzalloc(sizeof(*fw_update_msg), GFP_KERNEL);
136*4882a593Smuzhiyun if (!fw_update_msg)
137*4882a593Smuzhiyun return -ENOMEM;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun up_total_len = host_image->image_info.up_total_len;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun for (i = 0; i < host_image->section_type_num; i++) {
142*4882a593Smuzhiyun len = host_image->image_section_info[i].fw_section_len;
143*4882a593Smuzhiyun if (host_image->image_section_info[i].fw_section_type ==
144*4882a593Smuzhiyun UP_FW_UPDATE_BOOT) {
145*4882a593Smuzhiyun up_total_len = up_total_len - len;
146*4882a593Smuzhiyun break;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun for (i = 0; i < host_image->section_type_num; i++) {
151*4882a593Smuzhiyun section_len =
152*4882a593Smuzhiyun host_image->image_section_info[i].fw_section_len;
153*4882a593Smuzhiyun section_offset =
154*4882a593Smuzhiyun host_image->image_section_info[i].fw_section_offset;
155*4882a593Smuzhiyun section_remain_send_len = section_len;
156*4882a593Smuzhiyun section_type =
157*4882a593Smuzhiyun host_image->image_section_info[i].fw_section_type;
158*4882a593Smuzhiyun section_crc = host_image->image_section_info[i].fw_section_crc;
159*4882a593Smuzhiyun section_version =
160*4882a593Smuzhiyun host_image->image_section_info[i].fw_section_version;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun if (section_type == UP_FW_UPDATE_BOOT)
163*4882a593Smuzhiyun continue;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun send_fragment_len = 0;
166*4882a593Smuzhiyun send_pos = 0;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun while (section_remain_send_len > 0) {
169*4882a593Smuzhiyun if (!total_len_flag) {
170*4882a593Smuzhiyun fw_update_msg->total_len = up_total_len;
171*4882a593Smuzhiyun total_len_flag = 1;
172*4882a593Smuzhiyun } else {
173*4882a593Smuzhiyun fw_update_msg->total_len = 0;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun memset(fw_update_msg->data, 0, MAX_FW_FRAGMENT_LEN);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun fw_update_msg->ctl_info.SF =
179*4882a593Smuzhiyun (section_remain_send_len == section_len) ?
180*4882a593Smuzhiyun true : false;
181*4882a593Smuzhiyun fw_update_msg->section_info.FW_section_CRC = section_crc;
182*4882a593Smuzhiyun fw_update_msg->fw_section_version = section_version;
183*4882a593Smuzhiyun fw_update_msg->ctl_info.flag = UP_TYPE_A;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun if (section_type <= UP_FW_UPDATE_UP_DATA_B) {
186*4882a593Smuzhiyun fw_update_msg->section_info.FW_section_type =
187*4882a593Smuzhiyun (section_type % 2) ?
188*4882a593Smuzhiyun UP_FW_UPDATE_UP_DATA :
189*4882a593Smuzhiyun UP_FW_UPDATE_UP_TEXT;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun fw_update_msg->ctl_info.flag = UP_TYPE_B;
192*4882a593Smuzhiyun if (section_type <= UP_FW_UPDATE_UP_DATA_A)
193*4882a593Smuzhiyun fw_update_msg->ctl_info.flag = UP_TYPE_A;
194*4882a593Smuzhiyun } else {
195*4882a593Smuzhiyun fw_update_msg->section_info.FW_section_type =
196*4882a593Smuzhiyun section_type - 0x2;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun fw_update_msg->setion_total_len = section_len;
200*4882a593Smuzhiyun fw_update_msg->section_offset = send_pos;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun if (section_remain_send_len <= MAX_FW_FRAGMENT_LEN) {
203*4882a593Smuzhiyun fw_update_msg->ctl_info.SL = true;
204*4882a593Smuzhiyun fw_update_msg->ctl_info.fragment_len =
205*4882a593Smuzhiyun section_remain_send_len;
206*4882a593Smuzhiyun send_fragment_len += section_remain_send_len;
207*4882a593Smuzhiyun } else {
208*4882a593Smuzhiyun fw_update_msg->ctl_info.SL = false;
209*4882a593Smuzhiyun fw_update_msg->ctl_info.fragment_len =
210*4882a593Smuzhiyun MAX_FW_FRAGMENT_LEN;
211*4882a593Smuzhiyun send_fragment_len += MAX_FW_FRAGMENT_LEN;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun memcpy(fw_update_msg->data,
215*4882a593Smuzhiyun data + UPDATEFW_IMAGE_HEAD_SIZE +
216*4882a593Smuzhiyun section_offset + send_pos,
217*4882a593Smuzhiyun fw_update_msg->ctl_info.fragment_len);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun err = hinic_port_msg_cmd(priv->hwdev,
220*4882a593Smuzhiyun HINIC_PORT_CMD_UPDATE_FW,
221*4882a593Smuzhiyun fw_update_msg,
222*4882a593Smuzhiyun sizeof(*fw_update_msg),
223*4882a593Smuzhiyun fw_update_msg, &out_size);
224*4882a593Smuzhiyun if (err || !out_size || fw_update_msg->status) {
225*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "Failed to update firmware, err: %d, status: 0x%x, out size: 0x%x\n",
226*4882a593Smuzhiyun err, fw_update_msg->status, out_size);
227*4882a593Smuzhiyun err = fw_update_msg->status ?
228*4882a593Smuzhiyun fw_update_msg->status : -EIO;
229*4882a593Smuzhiyun kfree(fw_update_msg);
230*4882a593Smuzhiyun return err;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun send_pos = send_fragment_len;
234*4882a593Smuzhiyun section_remain_send_len = section_len -
235*4882a593Smuzhiyun send_fragment_len;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun kfree(fw_update_msg);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun return 0;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
hinic_firmware_update(struct hinic_devlink_priv * priv,const struct firmware * fw,struct netlink_ext_ack * extack)244*4882a593Smuzhiyun static int hinic_firmware_update(struct hinic_devlink_priv *priv,
245*4882a593Smuzhiyun const struct firmware *fw,
246*4882a593Smuzhiyun struct netlink_ext_ack *extack)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun struct host_image_st host_image;
249*4882a593Smuzhiyun int err;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun memset(&host_image, 0, sizeof(struct host_image_st));
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun if (!check_image_valid(priv, fw->data, fw->size, &host_image) ||
254*4882a593Smuzhiyun !check_image_integrity(priv, &host_image, FW_UPDATE_COLD) ||
255*4882a593Smuzhiyun !check_image_device_type(priv, host_image.device_id)) {
256*4882a593Smuzhiyun NL_SET_ERR_MSG_MOD(extack, "Check image failed");
257*4882a593Smuzhiyun return -EINVAL;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun dev_info(&priv->hwdev->hwif->pdev->dev, "Flash firmware begin\n");
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun err = hinic_flash_fw(priv, fw->data, &host_image);
263*4882a593Smuzhiyun if (err) {
264*4882a593Smuzhiyun if (err == HINIC_FW_DISMATCH_ERROR) {
265*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "Firmware image doesn't match this card, please use newer image, err: %d\n",
266*4882a593Smuzhiyun err);
267*4882a593Smuzhiyun NL_SET_ERR_MSG_MOD(extack,
268*4882a593Smuzhiyun "Firmware image doesn't match this card, please use newer image");
269*4882a593Smuzhiyun } else {
270*4882a593Smuzhiyun dev_err(&priv->hwdev->hwif->pdev->dev, "Send firmware image data failed, err: %d\n",
271*4882a593Smuzhiyun err);
272*4882a593Smuzhiyun NL_SET_ERR_MSG_MOD(extack, "Send firmware image data failed");
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun return err;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun dev_info(&priv->hwdev->hwif->pdev->dev, "Flash firmware end\n");
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun return 0;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
hinic_devlink_flash_update(struct devlink * devlink,struct devlink_flash_update_params * params,struct netlink_ext_ack * extack)283*4882a593Smuzhiyun static int hinic_devlink_flash_update(struct devlink *devlink,
284*4882a593Smuzhiyun struct devlink_flash_update_params *params,
285*4882a593Smuzhiyun struct netlink_ext_ack *extack)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun struct hinic_devlink_priv *priv = devlink_priv(devlink);
288*4882a593Smuzhiyun const struct firmware *fw;
289*4882a593Smuzhiyun int err;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun err = request_firmware_direct(&fw, params->file_name,
292*4882a593Smuzhiyun &priv->hwdev->hwif->pdev->dev);
293*4882a593Smuzhiyun if (err)
294*4882a593Smuzhiyun return err;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun err = hinic_firmware_update(priv, fw, extack);
297*4882a593Smuzhiyun release_firmware(fw);
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun return err;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun static const struct devlink_ops hinic_devlink_ops = {
303*4882a593Smuzhiyun .flash_update = hinic_devlink_flash_update,
304*4882a593Smuzhiyun };
305*4882a593Smuzhiyun
hinic_devlink_alloc(void)306*4882a593Smuzhiyun struct devlink *hinic_devlink_alloc(void)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun return devlink_alloc(&hinic_devlink_ops, sizeof(struct hinic_dev));
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
hinic_devlink_free(struct devlink * devlink)311*4882a593Smuzhiyun void hinic_devlink_free(struct devlink *devlink)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun devlink_free(devlink);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
hinic_devlink_register(struct hinic_devlink_priv * priv,struct device * dev)316*4882a593Smuzhiyun int hinic_devlink_register(struct hinic_devlink_priv *priv, struct device *dev)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun struct devlink *devlink = priv_to_devlink(priv);
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun return devlink_register(devlink, dev);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
hinic_devlink_unregister(struct hinic_devlink_priv * priv)323*4882a593Smuzhiyun void hinic_devlink_unregister(struct hinic_devlink_priv *priv)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun struct devlink *devlink = priv_to_devlink(priv);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun devlink_unregister(devlink);
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
chip_fault_show(struct devlink_fmsg * fmsg,struct hinic_fault_event * event)330*4882a593Smuzhiyun static int chip_fault_show(struct devlink_fmsg *fmsg,
331*4882a593Smuzhiyun struct hinic_fault_event *event)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun const char * const level_str[FAULT_LEVEL_MAX + 1] = {
334*4882a593Smuzhiyun "fatal", "reset", "flr", "general", "suggestion", "Unknown"};
335*4882a593Smuzhiyun u8 fault_level;
336*4882a593Smuzhiyun int err;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun fault_level = (event->event.chip.err_level < FAULT_LEVEL_MAX) ?
339*4882a593Smuzhiyun event->event.chip.err_level : FAULT_LEVEL_MAX;
340*4882a593Smuzhiyun if (fault_level == FAULT_LEVEL_SERIOUS_FLR) {
341*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "Function level err func_id",
342*4882a593Smuzhiyun (u32)event->event.chip.func_id);
343*4882a593Smuzhiyun if (err)
344*4882a593Smuzhiyun return err;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun err = devlink_fmsg_u8_pair_put(fmsg, "module_id", event->event.chip.node_id);
348*4882a593Smuzhiyun if (err)
349*4882a593Smuzhiyun return err;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "err_type", (u32)event->event.chip.err_type);
352*4882a593Smuzhiyun if (err)
353*4882a593Smuzhiyun return err;
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun err = devlink_fmsg_string_pair_put(fmsg, "err_level", level_str[fault_level]);
356*4882a593Smuzhiyun if (err)
357*4882a593Smuzhiyun return err;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "err_csr_addr",
360*4882a593Smuzhiyun event->event.chip.err_csr_addr);
361*4882a593Smuzhiyun if (err)
362*4882a593Smuzhiyun return err;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "err_csr_value",
365*4882a593Smuzhiyun event->event.chip.err_csr_value);
366*4882a593Smuzhiyun if (err)
367*4882a593Smuzhiyun return err;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun return 0;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
fault_report_show(struct devlink_fmsg * fmsg,struct hinic_fault_event * event)372*4882a593Smuzhiyun static int fault_report_show(struct devlink_fmsg *fmsg,
373*4882a593Smuzhiyun struct hinic_fault_event *event)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun const char * const type_str[FAULT_TYPE_MAX + 1] = {
376*4882a593Smuzhiyun "chip", "ucode", "mem rd timeout", "mem wr timeout",
377*4882a593Smuzhiyun "reg rd timeout", "reg wr timeout", "phy fault", "Unknown"};
378*4882a593Smuzhiyun u8 fault_type;
379*4882a593Smuzhiyun int err;
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun fault_type = (event->type < FAULT_TYPE_MAX) ? event->type : FAULT_TYPE_MAX;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun err = devlink_fmsg_string_pair_put(fmsg, "Fault type", type_str[fault_type]);
384*4882a593Smuzhiyun if (err)
385*4882a593Smuzhiyun return err;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun err = devlink_fmsg_binary_pair_put(fmsg, "Fault raw data",
388*4882a593Smuzhiyun event->event.val, sizeof(event->event.val));
389*4882a593Smuzhiyun if (err)
390*4882a593Smuzhiyun return err;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun switch (event->type) {
393*4882a593Smuzhiyun case FAULT_TYPE_CHIP:
394*4882a593Smuzhiyun err = chip_fault_show(fmsg, event);
395*4882a593Smuzhiyun if (err)
396*4882a593Smuzhiyun return err;
397*4882a593Smuzhiyun break;
398*4882a593Smuzhiyun case FAULT_TYPE_UCODE:
399*4882a593Smuzhiyun err = devlink_fmsg_u8_pair_put(fmsg, "Cause_id", event->event.ucode.cause_id);
400*4882a593Smuzhiyun if (err)
401*4882a593Smuzhiyun return err;
402*4882a593Smuzhiyun err = devlink_fmsg_u8_pair_put(fmsg, "core_id", event->event.ucode.core_id);
403*4882a593Smuzhiyun if (err)
404*4882a593Smuzhiyun return err;
405*4882a593Smuzhiyun err = devlink_fmsg_u8_pair_put(fmsg, "c_id", event->event.ucode.c_id);
406*4882a593Smuzhiyun if (err)
407*4882a593Smuzhiyun return err;
408*4882a593Smuzhiyun err = devlink_fmsg_u8_pair_put(fmsg, "epc", event->event.ucode.epc);
409*4882a593Smuzhiyun if (err)
410*4882a593Smuzhiyun return err;
411*4882a593Smuzhiyun break;
412*4882a593Smuzhiyun case FAULT_TYPE_MEM_RD_TIMEOUT:
413*4882a593Smuzhiyun case FAULT_TYPE_MEM_WR_TIMEOUT:
414*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "Err_csr_ctrl",
415*4882a593Smuzhiyun event->event.mem_timeout.err_csr_ctrl);
416*4882a593Smuzhiyun if (err)
417*4882a593Smuzhiyun return err;
418*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "err_csr_data",
419*4882a593Smuzhiyun event->event.mem_timeout.err_csr_data);
420*4882a593Smuzhiyun if (err)
421*4882a593Smuzhiyun return err;
422*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "ctrl_tab",
423*4882a593Smuzhiyun event->event.mem_timeout.ctrl_tab);
424*4882a593Smuzhiyun if (err)
425*4882a593Smuzhiyun return err;
426*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "mem_index",
427*4882a593Smuzhiyun event->event.mem_timeout.mem_index);
428*4882a593Smuzhiyun if (err)
429*4882a593Smuzhiyun return err;
430*4882a593Smuzhiyun break;
431*4882a593Smuzhiyun case FAULT_TYPE_REG_RD_TIMEOUT:
432*4882a593Smuzhiyun case FAULT_TYPE_REG_WR_TIMEOUT:
433*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "Err_csr", event->event.reg_timeout.err_csr);
434*4882a593Smuzhiyun if (err)
435*4882a593Smuzhiyun return err;
436*4882a593Smuzhiyun break;
437*4882a593Smuzhiyun case FAULT_TYPE_PHY_FAULT:
438*4882a593Smuzhiyun err = devlink_fmsg_u8_pair_put(fmsg, "Op_type", event->event.phy_fault.op_type);
439*4882a593Smuzhiyun if (err)
440*4882a593Smuzhiyun return err;
441*4882a593Smuzhiyun err = devlink_fmsg_u8_pair_put(fmsg, "port_id", event->event.phy_fault.port_id);
442*4882a593Smuzhiyun if (err)
443*4882a593Smuzhiyun return err;
444*4882a593Smuzhiyun err = devlink_fmsg_u8_pair_put(fmsg, "dev_ad", event->event.phy_fault.dev_ad);
445*4882a593Smuzhiyun if (err)
446*4882a593Smuzhiyun return err;
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "csr_addr", event->event.phy_fault.csr_addr);
449*4882a593Smuzhiyun if (err)
450*4882a593Smuzhiyun return err;
451*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "op_data", event->event.phy_fault.op_data);
452*4882a593Smuzhiyun if (err)
453*4882a593Smuzhiyun return err;
454*4882a593Smuzhiyun break;
455*4882a593Smuzhiyun default:
456*4882a593Smuzhiyun break;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun return 0;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
hinic_hw_reporter_dump(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,void * priv_ctx,struct netlink_ext_ack * extack)462*4882a593Smuzhiyun static int hinic_hw_reporter_dump(struct devlink_health_reporter *reporter,
463*4882a593Smuzhiyun struct devlink_fmsg *fmsg, void *priv_ctx,
464*4882a593Smuzhiyun struct netlink_ext_ack *extack)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun if (priv_ctx)
467*4882a593Smuzhiyun return fault_report_show(fmsg, priv_ctx);
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun return 0;
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
mgmt_watchdog_report_show(struct devlink_fmsg * fmsg,struct hinic_mgmt_watchdog_info * watchdog_info)472*4882a593Smuzhiyun static int mgmt_watchdog_report_show(struct devlink_fmsg *fmsg,
473*4882a593Smuzhiyun struct hinic_mgmt_watchdog_info *watchdog_info)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun int err;
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "Mgmt deadloop time_h", watchdog_info->curr_time_h);
478*4882a593Smuzhiyun if (err)
479*4882a593Smuzhiyun return err;
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "time_l", watchdog_info->curr_time_l);
482*4882a593Smuzhiyun if (err)
483*4882a593Smuzhiyun return err;
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "task_id", watchdog_info->task_id);
486*4882a593Smuzhiyun if (err)
487*4882a593Smuzhiyun return err;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "sp", watchdog_info->sp);
490*4882a593Smuzhiyun if (err)
491*4882a593Smuzhiyun return err;
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "stack_current_used", watchdog_info->curr_used);
494*4882a593Smuzhiyun if (err)
495*4882a593Smuzhiyun return err;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "peak_used", watchdog_info->peak_used);
498*4882a593Smuzhiyun if (err)
499*4882a593Smuzhiyun return err;
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "\n Overflow_flag", watchdog_info->is_overflow);
502*4882a593Smuzhiyun if (err)
503*4882a593Smuzhiyun return err;
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "stack_top", watchdog_info->stack_top);
506*4882a593Smuzhiyun if (err)
507*4882a593Smuzhiyun return err;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "stack_bottom", watchdog_info->stack_bottom);
510*4882a593Smuzhiyun if (err)
511*4882a593Smuzhiyun return err;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "mgmt_pc", watchdog_info->pc);
514*4882a593Smuzhiyun if (err)
515*4882a593Smuzhiyun return err;
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "lr", watchdog_info->lr);
518*4882a593Smuzhiyun if (err)
519*4882a593Smuzhiyun return err;
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun err = devlink_fmsg_u32_pair_put(fmsg, "cpsr", watchdog_info->cpsr);
522*4882a593Smuzhiyun if (err)
523*4882a593Smuzhiyun return err;
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun err = devlink_fmsg_binary_pair_put(fmsg, "Mgmt register info",
526*4882a593Smuzhiyun watchdog_info->reg, sizeof(watchdog_info->reg));
527*4882a593Smuzhiyun if (err)
528*4882a593Smuzhiyun return err;
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun err = devlink_fmsg_binary_pair_put(fmsg, "Mgmt dump stack(start from sp)",
531*4882a593Smuzhiyun watchdog_info->data, sizeof(watchdog_info->data));
532*4882a593Smuzhiyun if (err)
533*4882a593Smuzhiyun return err;
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun return 0;
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun
hinic_fw_reporter_dump(struct devlink_health_reporter * reporter,struct devlink_fmsg * fmsg,void * priv_ctx,struct netlink_ext_ack * extack)538*4882a593Smuzhiyun static int hinic_fw_reporter_dump(struct devlink_health_reporter *reporter,
539*4882a593Smuzhiyun struct devlink_fmsg *fmsg, void *priv_ctx,
540*4882a593Smuzhiyun struct netlink_ext_ack *extack)
541*4882a593Smuzhiyun {
542*4882a593Smuzhiyun if (priv_ctx)
543*4882a593Smuzhiyun return mgmt_watchdog_report_show(fmsg, priv_ctx);
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun return 0;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun static const struct devlink_health_reporter_ops hinic_hw_fault_reporter_ops = {
549*4882a593Smuzhiyun .name = "hw",
550*4882a593Smuzhiyun .dump = hinic_hw_reporter_dump,
551*4882a593Smuzhiyun };
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun static const struct devlink_health_reporter_ops hinic_fw_fault_reporter_ops = {
554*4882a593Smuzhiyun .name = "fw",
555*4882a593Smuzhiyun .dump = hinic_fw_reporter_dump,
556*4882a593Smuzhiyun };
557*4882a593Smuzhiyun
hinic_health_reporters_create(struct hinic_devlink_priv * priv)558*4882a593Smuzhiyun int hinic_health_reporters_create(struct hinic_devlink_priv *priv)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun struct devlink *devlink = priv_to_devlink(priv);
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun priv->hw_fault_reporter =
563*4882a593Smuzhiyun devlink_health_reporter_create(devlink, &hinic_hw_fault_reporter_ops,
564*4882a593Smuzhiyun 0, priv);
565*4882a593Smuzhiyun if (IS_ERR(priv->hw_fault_reporter)) {
566*4882a593Smuzhiyun dev_warn(&priv->hwdev->hwif->pdev->dev, "Failed to create hw fault reporter, err: %ld\n",
567*4882a593Smuzhiyun PTR_ERR(priv->hw_fault_reporter));
568*4882a593Smuzhiyun return PTR_ERR(priv->hw_fault_reporter);
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun priv->fw_fault_reporter =
572*4882a593Smuzhiyun devlink_health_reporter_create(devlink, &hinic_fw_fault_reporter_ops,
573*4882a593Smuzhiyun 0, priv);
574*4882a593Smuzhiyun if (IS_ERR(priv->fw_fault_reporter)) {
575*4882a593Smuzhiyun dev_warn(&priv->hwdev->hwif->pdev->dev, "Failed to create fw fault reporter, err: %ld\n",
576*4882a593Smuzhiyun PTR_ERR(priv->fw_fault_reporter));
577*4882a593Smuzhiyun devlink_health_reporter_destroy(priv->hw_fault_reporter);
578*4882a593Smuzhiyun priv->hw_fault_reporter = NULL;
579*4882a593Smuzhiyun return PTR_ERR(priv->fw_fault_reporter);
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun return 0;
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun
hinic_health_reporters_destroy(struct hinic_devlink_priv * priv)585*4882a593Smuzhiyun void hinic_health_reporters_destroy(struct hinic_devlink_priv *priv)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun if (!IS_ERR_OR_NULL(priv->fw_fault_reporter)) {
588*4882a593Smuzhiyun devlink_health_reporter_destroy(priv->fw_fault_reporter);
589*4882a593Smuzhiyun priv->fw_fault_reporter = NULL;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun if (!IS_ERR_OR_NULL(priv->hw_fault_reporter)) {
593*4882a593Smuzhiyun devlink_health_reporter_destroy(priv->hw_fault_reporter);
594*4882a593Smuzhiyun priv->hw_fault_reporter = NULL;
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun }
597