1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2019 Mellanox Technologies. All rights reserved */
3 
4 #define pr_fmt(fmt) "mlxfw: " fmt
5 
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/delay.h>
9 
10 #include "mlxfw.h"
11 #include "mlxfw_mfa2.h"
12 
13 #define MLXFW_FSM_STATE_WAIT_CYCLE_MS 200
14 #define MLXFW_FSM_STATE_WAIT_TIMEOUT_MS 30000
15 #define MLXFW_FSM_STATE_WAIT_ROUNDS \
16 	(MLXFW_FSM_STATE_WAIT_TIMEOUT_MS / MLXFW_FSM_STATE_WAIT_CYCLE_MS)
17 #define MLXFW_FSM_MAX_COMPONENT_SIZE (10 * (1 << 20))
18 
19 static const char * const mlxfw_fsm_state_err_str[] = {
20 	[MLXFW_FSM_STATE_ERR_ERROR] =
21 		"general error",
22 	[MLXFW_FSM_STATE_ERR_REJECTED_DIGEST_ERR] =
23 		"component hash mismatch",
24 	[MLXFW_FSM_STATE_ERR_REJECTED_NOT_APPLICABLE] =
25 		"component not applicable",
26 	[MLXFW_FSM_STATE_ERR_REJECTED_UNKNOWN_KEY] =
27 		"unknown key",
28 	[MLXFW_FSM_STATE_ERR_REJECTED_AUTH_FAILED] =
29 		"authentication failed",
30 	[MLXFW_FSM_STATE_ERR_REJECTED_UNSIGNED] =
31 		"component was not signed",
32 	[MLXFW_FSM_STATE_ERR_REJECTED_KEY_NOT_APPLICABLE] =
33 		"key not applicable",
34 	[MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT] =
35 		"bad format",
36 	[MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET] =
37 		"pending reset",
38 	[MLXFW_FSM_STATE_ERR_MAX] =
39 		"unknown error"
40 };
41 
42 static void mlxfw_status_notify(struct mlxfw_dev *mlxfw_dev,
43 				const char *msg, const char *comp_name,
44 				u32 done_bytes, u32 total_bytes)
45 {
46 	if (!mlxfw_dev->ops->status_notify)
47 		return;
48 	mlxfw_dev->ops->status_notify(mlxfw_dev, msg, comp_name,
49 				      done_bytes, total_bytes);
50 }
51 
52 static int mlxfw_fsm_state_wait(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
53 				enum mlxfw_fsm_state fsm_state,
54 				struct netlink_ext_ack *extack)
55 {
56 	enum mlxfw_fsm_state_err fsm_state_err;
57 	enum mlxfw_fsm_state curr_fsm_state;
58 	int times;
59 	int err;
60 
61 	times = MLXFW_FSM_STATE_WAIT_ROUNDS;
62 retry:
63 	err = mlxfw_dev->ops->fsm_query_state(mlxfw_dev, fwhandle,
64 					      &curr_fsm_state, &fsm_state_err);
65 	if (err)
66 		return err;
67 
68 	if (fsm_state_err != MLXFW_FSM_STATE_ERR_OK) {
69 		pr_err("Firmware flash failed: %s\n",
70 		       mlxfw_fsm_state_err_str[fsm_state_err]);
71 		NL_SET_ERR_MSG_MOD(extack, "Firmware flash failed");
72 		return -EINVAL;
73 	}
74 	if (curr_fsm_state != fsm_state) {
75 		if (--times == 0) {
76 			pr_err("Timeout reached on FSM state change");
77 			NL_SET_ERR_MSG_MOD(extack, "Timeout reached on FSM state change");
78 			return -ETIMEDOUT;
79 		}
80 		msleep(MLXFW_FSM_STATE_WAIT_CYCLE_MS);
81 		goto retry;
82 	}
83 	return 0;
84 }
85 
86 #define MLXFW_ALIGN_DOWN(x, align_bits) ((x) & ~((1 << (align_bits)) - 1))
87 #define MLXFW_ALIGN_UP(x, align_bits) \
88 		MLXFW_ALIGN_DOWN((x) + ((1 << (align_bits)) - 1), (align_bits))
89 
90 static int mlxfw_flash_component(struct mlxfw_dev *mlxfw_dev,
91 				 u32 fwhandle,
92 				 struct mlxfw_mfa2_component *comp,
93 				 struct netlink_ext_ack *extack)
94 {
95 	u16 comp_max_write_size;
96 	u8 comp_align_bits;
97 	u32 comp_max_size;
98 	char comp_name[8];
99 	u16 block_size;
100 	u8 *block_ptr;
101 	u32 offset;
102 	int err;
103 
104 	sprintf(comp_name, "%u", comp->index);
105 
106 	err = mlxfw_dev->ops->component_query(mlxfw_dev, comp->index,
107 					      &comp_max_size, &comp_align_bits,
108 					      &comp_max_write_size);
109 	if (err)
110 		return err;
111 
112 	comp_max_size = min_t(u32, comp_max_size, MLXFW_FSM_MAX_COMPONENT_SIZE);
113 	if (comp->data_size > comp_max_size) {
114 		pr_err("Component %d is of size %d which is bigger than limit %d\n",
115 		       comp->index, comp->data_size, comp_max_size);
116 		NL_SET_ERR_MSG_MOD(extack, "Component is bigger than limit");
117 		return -EINVAL;
118 	}
119 
120 	comp_max_write_size = MLXFW_ALIGN_DOWN(comp_max_write_size,
121 					       comp_align_bits);
122 
123 	pr_debug("Component update\n");
124 	mlxfw_status_notify(mlxfw_dev, "Updating component", comp_name, 0, 0);
125 	err = mlxfw_dev->ops->fsm_component_update(mlxfw_dev, fwhandle,
126 						   comp->index,
127 						   comp->data_size);
128 	if (err)
129 		return err;
130 
131 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
132 				   MLXFW_FSM_STATE_DOWNLOAD, extack);
133 	if (err)
134 		goto err_out;
135 
136 	pr_debug("Component download\n");
137 	mlxfw_status_notify(mlxfw_dev, "Downloading component",
138 			    comp_name, 0, comp->data_size);
139 	for (offset = 0;
140 	     offset < MLXFW_ALIGN_UP(comp->data_size, comp_align_bits);
141 	     offset += comp_max_write_size) {
142 		block_ptr = comp->data + offset;
143 		block_size = (u16) min_t(u32, comp->data_size - offset,
144 					 comp_max_write_size);
145 		err = mlxfw_dev->ops->fsm_block_download(mlxfw_dev, fwhandle,
146 							 block_ptr, block_size,
147 							 offset);
148 		if (err)
149 			goto err_out;
150 		mlxfw_status_notify(mlxfw_dev, "Downloading component",
151 				    comp_name, offset + block_size,
152 				    comp->data_size);
153 	}
154 
155 	pr_debug("Component verify\n");
156 	mlxfw_status_notify(mlxfw_dev, "Verifying component", comp_name, 0, 0);
157 	err = mlxfw_dev->ops->fsm_component_verify(mlxfw_dev, fwhandle,
158 						   comp->index);
159 	if (err)
160 		goto err_out;
161 
162 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
163 				   MLXFW_FSM_STATE_LOCKED, extack);
164 	if (err)
165 		goto err_out;
166 	return 0;
167 
168 err_out:
169 	mlxfw_dev->ops->fsm_cancel(mlxfw_dev, fwhandle);
170 	return err;
171 }
172 
173 static int mlxfw_flash_components(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
174 				  struct mlxfw_mfa2_file *mfa2_file,
175 				  struct netlink_ext_ack *extack)
176 {
177 	u32 component_count;
178 	int err;
179 	int i;
180 
181 	err = mlxfw_mfa2_file_component_count(mfa2_file, mlxfw_dev->psid,
182 					      mlxfw_dev->psid_size,
183 					      &component_count);
184 	if (err) {
185 		pr_err("Could not find device PSID in MFA2 file\n");
186 		NL_SET_ERR_MSG_MOD(extack, "Could not find device PSID in MFA2 file");
187 		return err;
188 	}
189 
190 	for (i = 0; i < component_count; i++) {
191 		struct mlxfw_mfa2_component *comp;
192 
193 		comp = mlxfw_mfa2_file_component_get(mfa2_file, mlxfw_dev->psid,
194 						     mlxfw_dev->psid_size, i);
195 		if (IS_ERR(comp))
196 			return PTR_ERR(comp);
197 
198 		pr_info("Flashing component type %d\n", comp->index);
199 		err = mlxfw_flash_component(mlxfw_dev, fwhandle, comp, extack);
200 		mlxfw_mfa2_file_component_put(comp);
201 		if (err)
202 			return err;
203 	}
204 	return 0;
205 }
206 
207 int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
208 			 const struct firmware *firmware,
209 			 struct netlink_ext_ack *extack)
210 {
211 	struct mlxfw_mfa2_file *mfa2_file;
212 	u32 fwhandle;
213 	int err;
214 
215 	if (!mlxfw_mfa2_check(firmware)) {
216 		pr_err("Firmware file is not MFA2\n");
217 		NL_SET_ERR_MSG_MOD(extack, "Firmware file is not MFA2");
218 		return -EINVAL;
219 	}
220 
221 	mfa2_file = mlxfw_mfa2_file_init(firmware);
222 	if (IS_ERR(mfa2_file))
223 		return PTR_ERR(mfa2_file);
224 
225 	pr_info("Initialize firmware flash process\n");
226 	mlxfw_status_notify(mlxfw_dev, "Initializing firmware flash process",
227 			    NULL, 0, 0);
228 	err = mlxfw_dev->ops->fsm_lock(mlxfw_dev, &fwhandle);
229 	if (err) {
230 		pr_err("Could not lock the firmware FSM\n");
231 		NL_SET_ERR_MSG_MOD(extack, "Could not lock the firmware FSM");
232 		goto err_fsm_lock;
233 	}
234 
235 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
236 				   MLXFW_FSM_STATE_LOCKED, extack);
237 	if (err)
238 		goto err_state_wait_idle_to_locked;
239 
240 	err = mlxfw_flash_components(mlxfw_dev, fwhandle, mfa2_file, extack);
241 	if (err)
242 		goto err_flash_components;
243 
244 	pr_debug("Activate image\n");
245 	mlxfw_status_notify(mlxfw_dev, "Activating image", NULL, 0, 0);
246 	err = mlxfw_dev->ops->fsm_activate(mlxfw_dev, fwhandle);
247 	if (err) {
248 		pr_err("Could not activate the downloaded image\n");
249 		NL_SET_ERR_MSG_MOD(extack, "Could not activate the downloaded image");
250 		goto err_fsm_activate;
251 	}
252 
253 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
254 				   MLXFW_FSM_STATE_LOCKED, extack);
255 	if (err)
256 		goto err_state_wait_activate_to_locked;
257 
258 	pr_debug("Handle release\n");
259 	mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
260 
261 	pr_info("Firmware flash done.\n");
262 	mlxfw_status_notify(mlxfw_dev, "Firmware flash done", NULL, 0, 0);
263 	mlxfw_mfa2_file_fini(mfa2_file);
264 	return 0;
265 
266 err_state_wait_activate_to_locked:
267 err_fsm_activate:
268 err_flash_components:
269 err_state_wait_idle_to_locked:
270 	mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
271 err_fsm_lock:
272 	mlxfw_mfa2_file_fini(mfa2_file);
273 	return err;
274 }
275 EXPORT_SYMBOL(mlxfw_firmware_flash);
276 
277 MODULE_LICENSE("Dual BSD/GPL");
278 MODULE_AUTHOR("Yotam Gigi <yotamg@mellanox.com>");
279 MODULE_DESCRIPTION("Mellanox firmware flash lib");
280