xref: /linux/drivers/net/ethernet/huawei/hinic/hinic_devlink.c (revision 5e126e7c4e52754e3aac0fbc5325abcbe1629388)
1*5e126e7cSLuo bin // SPDX-License-Identifier: GPL-2.0
2*5e126e7cSLuo bin /* Huawei HiNIC PCI Express Linux driver
3*5e126e7cSLuo bin  * Copyright(c) 2017 Huawei Technologies Co., Ltd
4*5e126e7cSLuo bin  *
5*5e126e7cSLuo bin  * This program is free software; you can redistribute it and/or modify it
6*5e126e7cSLuo bin  * under the terms and conditions of the GNU General Public License,
7*5e126e7cSLuo bin  * version 2, as published by the Free Software Foundation.
8*5e126e7cSLuo bin  *
9*5e126e7cSLuo bin  * This program is distributed in the hope it will be useful, but WITHOUT
10*5e126e7cSLuo bin  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11*5e126e7cSLuo bin  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12*5e126e7cSLuo bin  * for more details.
13*5e126e7cSLuo bin  *
14*5e126e7cSLuo bin  */
15*5e126e7cSLuo bin #include <linux/netlink.h>
16*5e126e7cSLuo bin #include <net/devlink.h>
17*5e126e7cSLuo bin #include <linux/firmware.h>
18*5e126e7cSLuo bin 
19*5e126e7cSLuo bin #include "hinic_dev.h"
20*5e126e7cSLuo bin #include "hinic_port.h"
21*5e126e7cSLuo bin #include "hinic_devlink.h"
22*5e126e7cSLuo bin 
23*5e126e7cSLuo bin static bool check_image_valid(struct hinic_devlink_priv *priv, const u8 *buf,
24*5e126e7cSLuo bin 			      u32 image_size, struct host_image_st *host_image)
25*5e126e7cSLuo bin {
26*5e126e7cSLuo bin 	struct fw_image_st *fw_image = NULL;
27*5e126e7cSLuo bin 	u32 len = 0;
28*5e126e7cSLuo bin 	u32 i;
29*5e126e7cSLuo bin 
30*5e126e7cSLuo bin 	fw_image = (struct fw_image_st *)buf;
31*5e126e7cSLuo bin 
32*5e126e7cSLuo bin 	if (fw_image->fw_magic != HINIC_MAGIC_NUM) {
33*5e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Wrong fw_magic read from file, fw_magic: 0x%x\n",
34*5e126e7cSLuo bin 			fw_image->fw_magic);
35*5e126e7cSLuo bin 		return false;
36*5e126e7cSLuo bin 	}
37*5e126e7cSLuo bin 
38*5e126e7cSLuo bin 	if (fw_image->fw_info.fw_section_cnt > MAX_FW_TYPE_NUM) {
39*5e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Wrong fw_type_num read from file, fw_type_num: 0x%x\n",
40*5e126e7cSLuo bin 			fw_image->fw_info.fw_section_cnt);
41*5e126e7cSLuo bin 		return false;
42*5e126e7cSLuo bin 	}
43*5e126e7cSLuo bin 
44*5e126e7cSLuo bin 	for (i = 0; i < fw_image->fw_info.fw_section_cnt; i++) {
45*5e126e7cSLuo bin 		len += fw_image->fw_section_info[i].fw_section_len;
46*5e126e7cSLuo bin 		memcpy(&host_image->image_section_info[i],
47*5e126e7cSLuo bin 		       &fw_image->fw_section_info[i],
48*5e126e7cSLuo bin 		       sizeof(struct fw_section_info_st));
49*5e126e7cSLuo bin 	}
50*5e126e7cSLuo bin 
51*5e126e7cSLuo bin 	if (len != fw_image->fw_len ||
52*5e126e7cSLuo bin 	    (fw_image->fw_len + UPDATEFW_IMAGE_HEAD_SIZE) != image_size) {
53*5e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Wrong data size read from file\n");
54*5e126e7cSLuo bin 		return false;
55*5e126e7cSLuo bin 	}
56*5e126e7cSLuo bin 
57*5e126e7cSLuo bin 	host_image->image_info.up_total_len = fw_image->fw_len;
58*5e126e7cSLuo bin 	host_image->image_info.fw_version = fw_image->fw_version;
59*5e126e7cSLuo bin 	host_image->section_type_num = fw_image->fw_info.fw_section_cnt;
60*5e126e7cSLuo bin 	host_image->device_id = fw_image->device_id;
61*5e126e7cSLuo bin 
62*5e126e7cSLuo bin 	return true;
63*5e126e7cSLuo bin }
64*5e126e7cSLuo bin 
65*5e126e7cSLuo bin static bool check_image_integrity(struct hinic_devlink_priv *priv,
66*5e126e7cSLuo bin 				  struct host_image_st *host_image,
67*5e126e7cSLuo bin 				  u32 update_type)
68*5e126e7cSLuo bin {
69*5e126e7cSLuo bin 	u32 collect_section_type = 0;
70*5e126e7cSLuo bin 	u32 i, type;
71*5e126e7cSLuo bin 
72*5e126e7cSLuo bin 	for (i = 0; i < host_image->section_type_num; i++) {
73*5e126e7cSLuo bin 		type = host_image->image_section_info[i].fw_section_type;
74*5e126e7cSLuo bin 		if (collect_section_type & (1U << type)) {
75*5e126e7cSLuo bin 			dev_err(&priv->hwdev->hwif->pdev->dev, "Duplicate section type: %u\n",
76*5e126e7cSLuo bin 				type);
77*5e126e7cSLuo bin 			return false;
78*5e126e7cSLuo bin 		}
79*5e126e7cSLuo bin 		collect_section_type |= (1U << type);
80*5e126e7cSLuo bin 	}
81*5e126e7cSLuo bin 
82*5e126e7cSLuo bin 	if (update_type == FW_UPDATE_COLD &&
83*5e126e7cSLuo bin 	    (((collect_section_type & _IMAGE_COLD_SUB_MODULES_MUST_IN) ==
84*5e126e7cSLuo bin 	       _IMAGE_COLD_SUB_MODULES_MUST_IN) ||
85*5e126e7cSLuo bin 	      collect_section_type == _IMAGE_CFG_SUB_MODULES_MUST_IN))
86*5e126e7cSLuo bin 		return true;
87*5e126e7cSLuo bin 
88*5e126e7cSLuo bin 	if (update_type == FW_UPDATE_HOT &&
89*5e126e7cSLuo bin 	    (collect_section_type & _IMAGE_HOT_SUB_MODULES_MUST_IN) ==
90*5e126e7cSLuo bin 	    _IMAGE_HOT_SUB_MODULES_MUST_IN)
91*5e126e7cSLuo bin 		return true;
92*5e126e7cSLuo bin 
93*5e126e7cSLuo bin 	if (update_type == FW_UPDATE_COLD)
94*5e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Check file integrity failed, valid: 0x%x or 0x%lx, current: 0x%x\n",
95*5e126e7cSLuo bin 			_IMAGE_COLD_SUB_MODULES_MUST_IN,
96*5e126e7cSLuo bin 			_IMAGE_CFG_SUB_MODULES_MUST_IN, collect_section_type);
97*5e126e7cSLuo bin 	else
98*5e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Check file integrity failed, valid:0x%x, current: 0x%x\n",
99*5e126e7cSLuo bin 			_IMAGE_HOT_SUB_MODULES_MUST_IN, collect_section_type);
100*5e126e7cSLuo bin 
101*5e126e7cSLuo bin 	return false;
102*5e126e7cSLuo bin }
103*5e126e7cSLuo bin 
104*5e126e7cSLuo bin static int check_image_device_type(struct hinic_devlink_priv *priv,
105*5e126e7cSLuo bin 				   u32 image_device_type)
106*5e126e7cSLuo bin {
107*5e126e7cSLuo bin 	struct hinic_comm_board_info board_info = {0};
108*5e126e7cSLuo bin 
109*5e126e7cSLuo bin 	if (hinic_get_board_info(priv->hwdev, &board_info)) {
110*5e126e7cSLuo bin 		dev_err(&priv->hwdev->hwif->pdev->dev, "Get board info failed\n");
111*5e126e7cSLuo bin 		return false;
112*5e126e7cSLuo bin 	}
113*5e126e7cSLuo bin 
114*5e126e7cSLuo bin 	if (image_device_type == board_info.info.board_type)
115*5e126e7cSLuo bin 		return true;
116*5e126e7cSLuo bin 
117*5e126e7cSLuo 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");
118*5e126e7cSLuo bin 	dev_err(&priv->hwdev->hwif->pdev->dev, "The image device type: 0x%x, firmware device type: 0x%x\n",
119*5e126e7cSLuo bin 		image_device_type, board_info.info.board_type);
120*5e126e7cSLuo bin 
121*5e126e7cSLuo bin 	return false;
122*5e126e7cSLuo bin }
123*5e126e7cSLuo bin 
124*5e126e7cSLuo bin static int hinic_flash_fw(struct hinic_devlink_priv *priv, const u8 *data,
125*5e126e7cSLuo bin 			  struct host_image_st *host_image)
126*5e126e7cSLuo bin {
127*5e126e7cSLuo bin 	u32 section_remain_send_len, send_fragment_len, send_pos, up_total_len;
128*5e126e7cSLuo bin 	struct hinic_cmd_update_fw *fw_update_msg = NULL;
129*5e126e7cSLuo bin 	u32 section_type, section_crc, section_version;
130*5e126e7cSLuo bin 	u32 i, len, section_len, section_offset;
131*5e126e7cSLuo bin 	u16 out_size = sizeof(*fw_update_msg);
132*5e126e7cSLuo bin 	int total_len_flag = 0;
133*5e126e7cSLuo bin 	int err;
134*5e126e7cSLuo bin 
135*5e126e7cSLuo bin 	fw_update_msg = kzalloc(sizeof(*fw_update_msg), GFP_KERNEL);
136*5e126e7cSLuo bin 	if (!fw_update_msg)
137*5e126e7cSLuo bin 		return -ENOMEM;
138*5e126e7cSLuo bin 
139*5e126e7cSLuo bin 	up_total_len = host_image->image_info.up_total_len;
140*5e126e7cSLuo bin 
141*5e126e7cSLuo bin 	for (i = 0; i < host_image->section_type_num; i++) {
142*5e126e7cSLuo bin 		len = host_image->image_section_info[i].fw_section_len;
143*5e126e7cSLuo bin 		if (host_image->image_section_info[i].fw_section_type ==
144*5e126e7cSLuo bin 		    UP_FW_UPDATE_BOOT) {
145*5e126e7cSLuo bin 			up_total_len = up_total_len - len;
146*5e126e7cSLuo bin 			break;
147*5e126e7cSLuo bin 		}
148*5e126e7cSLuo bin 	}
149*5e126e7cSLuo bin 
150*5e126e7cSLuo bin 	for (i = 0; i < host_image->section_type_num; i++) {
151*5e126e7cSLuo bin 		section_len =
152*5e126e7cSLuo bin 			host_image->image_section_info[i].fw_section_len;
153*5e126e7cSLuo bin 		section_offset =
154*5e126e7cSLuo bin 			host_image->image_section_info[i].fw_section_offset;
155*5e126e7cSLuo bin 		section_remain_send_len = section_len;
156*5e126e7cSLuo bin 		section_type =
157*5e126e7cSLuo bin 			host_image->image_section_info[i].fw_section_type;
158*5e126e7cSLuo bin 		section_crc = host_image->image_section_info[i].fw_section_crc;
159*5e126e7cSLuo bin 		section_version =
160*5e126e7cSLuo bin 			host_image->image_section_info[i].fw_section_version;
161*5e126e7cSLuo bin 
162*5e126e7cSLuo bin 		if (section_type == UP_FW_UPDATE_BOOT)
163*5e126e7cSLuo bin 			continue;
164*5e126e7cSLuo bin 
165*5e126e7cSLuo bin 		send_fragment_len = 0;
166*5e126e7cSLuo bin 		send_pos = 0;
167*5e126e7cSLuo bin 
168*5e126e7cSLuo bin 		while (section_remain_send_len > 0) {
169*5e126e7cSLuo bin 			if (!total_len_flag) {
170*5e126e7cSLuo bin 				fw_update_msg->total_len = up_total_len;
171*5e126e7cSLuo bin 				total_len_flag = 1;
172*5e126e7cSLuo bin 			} else {
173*5e126e7cSLuo bin 				fw_update_msg->total_len = 0;
174*5e126e7cSLuo bin 			}
175*5e126e7cSLuo bin 
176*5e126e7cSLuo bin 			memset(fw_update_msg->data, 0, MAX_FW_FRAGMENT_LEN);
177*5e126e7cSLuo bin 
178*5e126e7cSLuo bin 			fw_update_msg->ctl_info.SF =
179*5e126e7cSLuo bin 				(section_remain_send_len == section_len) ?
180*5e126e7cSLuo bin 				true : false;
181*5e126e7cSLuo bin 			fw_update_msg->section_info.FW_section_CRC = section_crc;
182*5e126e7cSLuo bin 			fw_update_msg->fw_section_version = section_version;
183*5e126e7cSLuo bin 			fw_update_msg->ctl_info.flag = UP_TYPE_A;
184*5e126e7cSLuo bin 
185*5e126e7cSLuo bin 			if (section_type <= UP_FW_UPDATE_UP_DATA_B) {
186*5e126e7cSLuo bin 				fw_update_msg->section_info.FW_section_type =
187*5e126e7cSLuo bin 					(section_type % 2) ?
188*5e126e7cSLuo bin 					UP_FW_UPDATE_UP_DATA :
189*5e126e7cSLuo bin 					UP_FW_UPDATE_UP_TEXT;
190*5e126e7cSLuo bin 
191*5e126e7cSLuo bin 				fw_update_msg->ctl_info.flag = UP_TYPE_B;
192*5e126e7cSLuo bin 				if (section_type <= UP_FW_UPDATE_UP_DATA_A)
193*5e126e7cSLuo bin 					fw_update_msg->ctl_info.flag = UP_TYPE_A;
194*5e126e7cSLuo bin 			} else {
195*5e126e7cSLuo bin 				fw_update_msg->section_info.FW_section_type =
196*5e126e7cSLuo bin 					section_type - 0x2;
197*5e126e7cSLuo bin 			}
198*5e126e7cSLuo bin 
199*5e126e7cSLuo bin 			fw_update_msg->setion_total_len = section_len;
200*5e126e7cSLuo bin 			fw_update_msg->section_offset = send_pos;
201*5e126e7cSLuo bin 
202*5e126e7cSLuo bin 			if (section_remain_send_len <= MAX_FW_FRAGMENT_LEN) {
203*5e126e7cSLuo bin 				fw_update_msg->ctl_info.SL = true;
204*5e126e7cSLuo bin 				fw_update_msg->ctl_info.fragment_len =
205*5e126e7cSLuo bin 					section_remain_send_len;
206*5e126e7cSLuo bin 				send_fragment_len += section_remain_send_len;
207*5e126e7cSLuo bin 			} else {
208*5e126e7cSLuo bin 				fw_update_msg->ctl_info.SL = false;
209*5e126e7cSLuo bin 				fw_update_msg->ctl_info.fragment_len =
210*5e126e7cSLuo bin 					MAX_FW_FRAGMENT_LEN;
211*5e126e7cSLuo bin 				send_fragment_len += MAX_FW_FRAGMENT_LEN;
212*5e126e7cSLuo bin 			}
213*5e126e7cSLuo bin 
214*5e126e7cSLuo bin 			memcpy(fw_update_msg->data,
215*5e126e7cSLuo bin 			       data + UPDATEFW_IMAGE_HEAD_SIZE +
216*5e126e7cSLuo bin 			       section_offset + send_pos,
217*5e126e7cSLuo bin 			       fw_update_msg->ctl_info.fragment_len);
218*5e126e7cSLuo bin 
219*5e126e7cSLuo bin 			err = hinic_port_msg_cmd(priv->hwdev,
220*5e126e7cSLuo bin 						 HINIC_PORT_CMD_UPDATE_FW,
221*5e126e7cSLuo bin 						 fw_update_msg,
222*5e126e7cSLuo bin 						 sizeof(*fw_update_msg),
223*5e126e7cSLuo bin 						 fw_update_msg, &out_size);
224*5e126e7cSLuo bin 			if (err || !out_size || fw_update_msg->status) {
225*5e126e7cSLuo bin 				dev_err(&priv->hwdev->hwif->pdev->dev, "Failed to update firmware, err: %d, status: 0x%x, out size: 0x%x\n",
226*5e126e7cSLuo bin 					err, fw_update_msg->status, out_size);
227*5e126e7cSLuo bin 				err = fw_update_msg->status ?
228*5e126e7cSLuo bin 					fw_update_msg->status : -EIO;
229*5e126e7cSLuo bin 				kfree(fw_update_msg);
230*5e126e7cSLuo bin 				return err;
231*5e126e7cSLuo bin 			}
232*5e126e7cSLuo bin 
233*5e126e7cSLuo bin 			send_pos = send_fragment_len;
234*5e126e7cSLuo bin 			section_remain_send_len = section_len -
235*5e126e7cSLuo bin 						  send_fragment_len;
236*5e126e7cSLuo bin 		}
237*5e126e7cSLuo bin 	}
238*5e126e7cSLuo bin 
239*5e126e7cSLuo bin 	kfree(fw_update_msg);
240*5e126e7cSLuo bin 
241*5e126e7cSLuo bin 	return 0;
242*5e126e7cSLuo bin }
243*5e126e7cSLuo bin 
244*5e126e7cSLuo bin static int hinic_firmware_update(struct hinic_devlink_priv *priv,
245*5e126e7cSLuo bin 				 const struct firmware *fw,
246*5e126e7cSLuo bin 				 struct netlink_ext_ack *extack)
247*5e126e7cSLuo bin {
248*5e126e7cSLuo bin 	struct host_image_st host_image;
249*5e126e7cSLuo bin 	int err;
250*5e126e7cSLuo bin 
251*5e126e7cSLuo bin 	memset(&host_image, 0, sizeof(struct host_image_st));
252*5e126e7cSLuo bin 
253*5e126e7cSLuo bin 	if (!check_image_valid(priv, fw->data, fw->size, &host_image) ||
254*5e126e7cSLuo bin 	    !check_image_integrity(priv, &host_image, FW_UPDATE_COLD) ||
255*5e126e7cSLuo bin 	    !check_image_device_type(priv, host_image.device_id)) {
256*5e126e7cSLuo bin 		NL_SET_ERR_MSG_MOD(extack, "Check image failed");
257*5e126e7cSLuo bin 		return -EINVAL;
258*5e126e7cSLuo bin 	}
259*5e126e7cSLuo bin 
260*5e126e7cSLuo bin 	dev_info(&priv->hwdev->hwif->pdev->dev, "Flash firmware begin\n");
261*5e126e7cSLuo bin 
262*5e126e7cSLuo bin 	err = hinic_flash_fw(priv, fw->data, &host_image);
263*5e126e7cSLuo bin 	if (err) {
264*5e126e7cSLuo bin 		if (err == HINIC_FW_DISMATCH_ERROR) {
265*5e126e7cSLuo bin 			dev_err(&priv->hwdev->hwif->pdev->dev, "Firmware image doesn't match this card, please use newer image, err: %d\n",
266*5e126e7cSLuo bin 				err);
267*5e126e7cSLuo bin 			NL_SET_ERR_MSG_MOD(extack,
268*5e126e7cSLuo bin 					   "Firmware image doesn't match this card, please use newer image");
269*5e126e7cSLuo bin 		} else {
270*5e126e7cSLuo bin 			dev_err(&priv->hwdev->hwif->pdev->dev, "Send firmware image data failed, err: %d\n",
271*5e126e7cSLuo bin 				err);
272*5e126e7cSLuo bin 			NL_SET_ERR_MSG_MOD(extack, "Send firmware image data failed");
273*5e126e7cSLuo bin 		}
274*5e126e7cSLuo bin 
275*5e126e7cSLuo bin 		return err;
276*5e126e7cSLuo bin 	}
277*5e126e7cSLuo bin 
278*5e126e7cSLuo bin 	dev_info(&priv->hwdev->hwif->pdev->dev, "Flash firmware end\n");
279*5e126e7cSLuo bin 
280*5e126e7cSLuo bin 	return 0;
281*5e126e7cSLuo bin }
282*5e126e7cSLuo bin 
283*5e126e7cSLuo bin static int hinic_devlink_flash_update(struct devlink *devlink,
284*5e126e7cSLuo bin 				      const char *file_name,
285*5e126e7cSLuo bin 				      const char *component,
286*5e126e7cSLuo bin 				      struct netlink_ext_ack *extack)
287*5e126e7cSLuo bin {
288*5e126e7cSLuo bin 	struct hinic_devlink_priv *priv = devlink_priv(devlink);
289*5e126e7cSLuo bin 	const struct firmware *fw;
290*5e126e7cSLuo bin 	int err;
291*5e126e7cSLuo bin 
292*5e126e7cSLuo bin 	if (component)
293*5e126e7cSLuo bin 		return -EOPNOTSUPP;
294*5e126e7cSLuo bin 
295*5e126e7cSLuo bin 	err = request_firmware_direct(&fw, file_name,
296*5e126e7cSLuo bin 				      &priv->hwdev->hwif->pdev->dev);
297*5e126e7cSLuo bin 	if (err)
298*5e126e7cSLuo bin 		return err;
299*5e126e7cSLuo bin 
300*5e126e7cSLuo bin 	err = hinic_firmware_update(priv, fw, extack);
301*5e126e7cSLuo bin 	release_firmware(fw);
302*5e126e7cSLuo bin 
303*5e126e7cSLuo bin 	return err;
304*5e126e7cSLuo bin }
305*5e126e7cSLuo bin 
306*5e126e7cSLuo bin static const struct devlink_ops hinic_devlink_ops = {
307*5e126e7cSLuo bin 	.flash_update = hinic_devlink_flash_update,
308*5e126e7cSLuo bin };
309*5e126e7cSLuo bin 
310*5e126e7cSLuo bin struct devlink *hinic_devlink_alloc(void)
311*5e126e7cSLuo bin {
312*5e126e7cSLuo bin 	return devlink_alloc(&hinic_devlink_ops, sizeof(struct hinic_dev));
313*5e126e7cSLuo bin }
314*5e126e7cSLuo bin 
315*5e126e7cSLuo bin void hinic_devlink_free(struct devlink *devlink)
316*5e126e7cSLuo bin {
317*5e126e7cSLuo bin 	devlink_free(devlink);
318*5e126e7cSLuo bin }
319*5e126e7cSLuo bin 
320*5e126e7cSLuo bin int hinic_devlink_register(struct devlink *devlink, struct device *dev)
321*5e126e7cSLuo bin {
322*5e126e7cSLuo bin 	return devlink_register(devlink, dev);
323*5e126e7cSLuo bin }
324*5e126e7cSLuo bin 
325*5e126e7cSLuo bin void hinic_devlink_unregister(struct devlink *devlink)
326*5e126e7cSLuo bin {
327*5e126e7cSLuo bin 	devlink_unregister(devlink);
328*5e126e7cSLuo bin }
329