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