1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2018-2019, Intel Corporation. */
3 
4 #include <asm/unaligned.h>
5 #include <linux/uuid.h>
6 #include <linux/crc32.h>
7 #include <linux/pldmfw.h>
8 #include "ice.h"
9 #include "ice_fw_update.h"
10 
11 struct ice_fwu_priv {
12 	struct pldmfw context;
13 
14 	struct ice_pf *pf;
15 	struct netlink_ext_ack *extack;
16 
17 	/* Track which NVM banks to activate at the end of the update */
18 	u8 activate_flags;
19 };
20 
21 /**
22  * ice_send_package_data - Send record package data to firmware
23  * @context: PLDM fw update structure
24  * @data: pointer to the package data
25  * @length: length of the package data
26  *
27  * Send a copy of the package data associated with the PLDM record matching
28  * this device to the firmware.
29  *
30  * Note that this function sends an AdminQ command that will fail unless the
31  * NVM resource has been acquired.
32  *
33  * Returns: zero on success, or a negative error code on failure.
34  */
35 static int
36 ice_send_package_data(struct pldmfw *context, const u8 *data, u16 length)
37 {
38 	struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
39 	struct netlink_ext_ack *extack = priv->extack;
40 	struct device *dev = context->dev;
41 	struct ice_pf *pf = priv->pf;
42 	struct ice_hw *hw = &pf->hw;
43 	enum ice_status status;
44 	u8 *package_data;
45 
46 	dev_dbg(dev, "Sending PLDM record package data to firmware\n");
47 
48 	package_data = kmemdup(data, length, GFP_KERNEL);
49 	if (!package_data)
50 		return -ENOMEM;
51 
52 	status = ice_nvm_set_pkg_data(hw, false, package_data, length, NULL);
53 
54 	kfree(package_data);
55 
56 	if (status) {
57 		dev_err(dev, "Failed to send record package data to firmware, err %s aq_err %s\n",
58 			ice_stat_str(status),
59 			ice_aq_str(hw->adminq.sq_last_status));
60 		NL_SET_ERR_MSG_MOD(extack, "Failed to record package data to firmware");
61 		return -EIO;
62 	}
63 
64 	return 0;
65 }
66 
67 /**
68  * ice_check_component_response - Report firmware response to a component
69  * @pf: device private data structure
70  * @id: component id being checked
71  * @response: indicates whether this component can be updated
72  * @code: code indicating reason for response
73  * @extack: netlink extended ACK structure
74  *
75  * Check whether firmware indicates if this component can be updated. Report
76  * a suitable error message over the netlink extended ACK if the component
77  * cannot be updated.
78  *
79  * Returns: zero if the component can be updated, or -ECANCELED of the
80  * firmware indicates the component cannot be updated.
81  */
82 static int
83 ice_check_component_response(struct ice_pf *pf, u16 id, u8 response, u8 code,
84 			     struct netlink_ext_ack *extack)
85 {
86 	struct device *dev = ice_pf_to_dev(pf);
87 	const char *component;
88 
89 	switch (id) {
90 	case NVM_COMP_ID_OROM:
91 		component = "fw.undi";
92 		break;
93 	case NVM_COMP_ID_NVM:
94 		component = "fw.mgmt";
95 		break;
96 	case NVM_COMP_ID_NETLIST:
97 		component = "fw.netlist";
98 		break;
99 	default:
100 		WARN(1, "Unexpected unknown component identifier 0x%02x", id);
101 		return -EINVAL;
102 	}
103 
104 	dev_dbg(dev, "%s: firmware response 0x%x, code 0x%x\n",
105 		component, response, code);
106 
107 	switch (response) {
108 	case ICE_AQ_NVM_PASS_COMP_CAN_BE_UPDATED:
109 		/* firmware indicated this update is good to proceed */
110 		return 0;
111 	case ICE_AQ_NVM_PASS_COMP_CAN_MAY_BE_UPDATEABLE:
112 		dev_warn(dev, "firmware recommends not updating %s, as it may result in a downgrade. continuing anyways\n", component);
113 		return 0;
114 	case ICE_AQ_NVM_PASS_COMP_CAN_NOT_BE_UPDATED:
115 		dev_info(dev, "firmware has rejected updating %s\n", component);
116 		break;
117 	}
118 
119 	switch (code) {
120 	case ICE_AQ_NVM_PASS_COMP_STAMP_IDENTICAL_CODE:
121 		dev_err(dev, "Component comparison stamp for %s is identical to the running image\n",
122 			component);
123 		NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is identical to running image");
124 		break;
125 	case ICE_AQ_NVM_PASS_COMP_STAMP_LOWER:
126 		dev_err(dev, "Component comparison stamp for %s is lower than the running image\n",
127 			component);
128 		NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is lower than running image");
129 		break;
130 	case ICE_AQ_NVM_PASS_COMP_INVALID_STAMP_CODE:
131 		dev_err(dev, "Component comparison stamp for %s is invalid\n",
132 			component);
133 		NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is invalid");
134 		break;
135 	case ICE_AQ_NVM_PASS_COMP_CONFLICT_CODE:
136 		dev_err(dev, "%s conflicts with a previous component table\n",
137 			component);
138 		NL_SET_ERR_MSG_MOD(extack, "Component table conflict occurred");
139 		break;
140 	case ICE_AQ_NVM_PASS_COMP_PRE_REQ_NOT_MET_CODE:
141 		dev_err(dev, "Pre-requisites for component %s have not been met\n",
142 			component);
143 		NL_SET_ERR_MSG_MOD(extack, "Component pre-requisites not met");
144 		break;
145 	case ICE_AQ_NVM_PASS_COMP_NOT_SUPPORTED_CODE:
146 		dev_err(dev, "%s is not a supported component\n",
147 			component);
148 		NL_SET_ERR_MSG_MOD(extack, "Component not supported");
149 		break;
150 	case ICE_AQ_NVM_PASS_COMP_CANNOT_DOWNGRADE_CODE:
151 		dev_err(dev, "Security restrictions prevent %s from being downgraded\n",
152 			component);
153 		NL_SET_ERR_MSG_MOD(extack, "Component cannot be downgraded");
154 		break;
155 	case ICE_AQ_NVM_PASS_COMP_INCOMPLETE_IMAGE_CODE:
156 		dev_err(dev, "Received an incomplete component image for %s\n",
157 			component);
158 		NL_SET_ERR_MSG_MOD(extack, "Incomplete component image");
159 		break;
160 	case ICE_AQ_NVM_PASS_COMP_VER_STR_IDENTICAL_CODE:
161 		dev_err(dev, "Component version for %s is identical to the running image\n",
162 			component);
163 		NL_SET_ERR_MSG_MOD(extack, "Component version is identical to running image");
164 		break;
165 	case ICE_AQ_NVM_PASS_COMP_VER_STR_LOWER_CODE:
166 		dev_err(dev, "Component version for %s is lower than the running image\n",
167 			component);
168 		NL_SET_ERR_MSG_MOD(extack, "Component version is lower than the running image");
169 		break;
170 	default:
171 		dev_err(dev, "Unexpected response code 0x02%x for %s\n",
172 			code, component);
173 		NL_SET_ERR_MSG_MOD(extack, "Received unexpected response code from firmware");
174 		break;
175 	}
176 
177 	return -ECANCELED;
178 }
179 
180 /**
181  * ice_send_component_table - Send PLDM component table to firmware
182  * @context: PLDM fw update structure
183  * @component: the component to process
184  * @transfer_flag: relative transfer order of this component
185  *
186  * Read relevant data from the component and forward it to the device
187  * firmware. Check the response to determine if the firmware indicates that
188  * the update can proceed.
189  *
190  * This function sends AdminQ commands related to the NVM, and assumes that
191  * the NVM resource has been acquired.
192  *
193  * Returns: zero on success, or a negative error code on failure.
194  */
195 static int
196 ice_send_component_table(struct pldmfw *context, struct pldmfw_component *component,
197 			 u8 transfer_flag)
198 {
199 	struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
200 	struct netlink_ext_ack *extack = priv->extack;
201 	struct ice_aqc_nvm_comp_tbl *comp_tbl;
202 	u8 comp_response, comp_response_code;
203 	struct device *dev = context->dev;
204 	struct ice_pf *pf = priv->pf;
205 	struct ice_hw *hw = &pf->hw;
206 	enum ice_status status;
207 	size_t length;
208 
209 	switch (component->identifier) {
210 	case NVM_COMP_ID_OROM:
211 	case NVM_COMP_ID_NVM:
212 	case NVM_COMP_ID_NETLIST:
213 		break;
214 	default:
215 		dev_err(dev, "Unable to update due to a firmware component with unknown ID %u\n",
216 			component->identifier);
217 		NL_SET_ERR_MSG_MOD(extack, "Unable to update due to unknown firmware component");
218 		return -EOPNOTSUPP;
219 	}
220 
221 	length = struct_size(comp_tbl, cvs, component->version_len);
222 	comp_tbl = kzalloc(length, GFP_KERNEL);
223 	if (!comp_tbl)
224 		return -ENOMEM;
225 
226 	comp_tbl->comp_class = cpu_to_le16(component->classification);
227 	comp_tbl->comp_id = cpu_to_le16(component->identifier);
228 	comp_tbl->comp_class_idx = FWU_COMP_CLASS_IDX_NOT_USE;
229 	comp_tbl->comp_cmp_stamp = cpu_to_le32(component->comparison_stamp);
230 	comp_tbl->cvs_type = component->version_type;
231 	comp_tbl->cvs_len = component->version_len;
232 	memcpy(comp_tbl->cvs, component->version_string, component->version_len);
233 
234 	dev_dbg(dev, "Sending component table to firmware:\n");
235 
236 	status = ice_nvm_pass_component_tbl(hw, (u8 *)comp_tbl, length,
237 					    transfer_flag, &comp_response,
238 					    &comp_response_code, NULL);
239 
240 	kfree(comp_tbl);
241 
242 	if (status) {
243 		dev_err(dev, "Failed to transfer component table to firmware, err %s aq_err %s\n",
244 			ice_stat_str(status),
245 			ice_aq_str(hw->adminq.sq_last_status));
246 		NL_SET_ERR_MSG_MOD(extack, "Failed to transfer component table to firmware");
247 		return -EIO;
248 	}
249 
250 	return ice_check_component_response(pf, component->identifier, comp_response,
251 					    comp_response_code, extack);
252 }
253 
254 /**
255  * ice_write_one_nvm_block - Write an NVM block and await completion response
256  * @pf: the PF data structure
257  * @module: the module to write to
258  * @offset: offset in bytes
259  * @block_size: size of the block to write, up to 4k
260  * @block: pointer to block of data to write
261  * @last_cmd: whether this is the last command
262  * @extack: netlink extended ACK structure
263  *
264  * Write a block of data to a flash module, and await for the completion
265  * response message from firmware.
266  *
267  * Note this function assumes the caller has acquired the NVM resource.
268  *
269  * Returns: zero on success, or a negative error code on failure.
270  */
271 static int
272 ice_write_one_nvm_block(struct ice_pf *pf, u16 module, u32 offset,
273 			u16 block_size, u8 *block, bool last_cmd,
274 			struct netlink_ext_ack *extack)
275 {
276 	u16 completion_module, completion_retval;
277 	struct device *dev = ice_pf_to_dev(pf);
278 	struct ice_rq_event_info event;
279 	struct ice_hw *hw = &pf->hw;
280 	enum ice_status status;
281 	u32 completion_offset;
282 	int err;
283 
284 	memset(&event, 0, sizeof(event));
285 
286 	dev_dbg(dev, "Writing block of %u bytes for module 0x%02x at offset %u\n",
287 		block_size, module, offset);
288 
289 	status = ice_aq_update_nvm(hw, module, offset, block_size, block,
290 				   last_cmd, 0, NULL);
291 	if (status) {
292 		dev_err(dev, "Failed to flash module 0x%02x with block of size %u at offset %u, err %s aq_err %s\n",
293 			module, block_size, offset, ice_stat_str(status),
294 			ice_aq_str(hw->adminq.sq_last_status));
295 		NL_SET_ERR_MSG_MOD(extack, "Failed to program flash module");
296 		return -EIO;
297 	}
298 
299 	/* In most cases, firmware reports a write completion within a few
300 	 * milliseconds. However, it has been observed that a completion might
301 	 * take more than a second to complete in some cases. The timeout here
302 	 * is conservative and is intended to prevent failure to update when
303 	 * firmware is slow to respond.
304 	 */
305 	err = ice_aq_wait_for_event(pf, ice_aqc_opc_nvm_write, 15 * HZ, &event);
306 	if (err) {
307 		dev_err(dev, "Timed out while trying to flash module 0x%02x with block of size %u at offset %u, err %d\n",
308 			module, block_size, offset, err);
309 		NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
310 		return -EIO;
311 	}
312 
313 	completion_module = le16_to_cpu(event.desc.params.nvm.module_typeid);
314 	completion_retval = le16_to_cpu(event.desc.retval);
315 
316 	completion_offset = le16_to_cpu(event.desc.params.nvm.offset_low);
317 	completion_offset |= event.desc.params.nvm.offset_high << 16;
318 
319 	if (completion_module != module) {
320 		dev_err(dev, "Unexpected module_typeid in write completion: got 0x%x, expected 0x%x\n",
321 			completion_module, module);
322 		NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
323 		return -EIO;
324 	}
325 
326 	if (completion_offset != offset) {
327 		dev_err(dev, "Unexpected offset in write completion: got %u, expected %u\n",
328 			completion_offset, offset);
329 		NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
330 		return -EIO;
331 	}
332 
333 	if (completion_retval) {
334 		dev_err(dev, "Firmware failed to flash module 0x%02x with block of size %u at offset %u, err %s\n",
335 			module, block_size, offset,
336 			ice_aq_str((enum ice_aq_err)completion_retval));
337 		NL_SET_ERR_MSG_MOD(extack, "Firmware failed to program flash module");
338 		return -EIO;
339 	}
340 
341 	return 0;
342 }
343 
344 /**
345  * ice_write_nvm_module - Write data to an NVM module
346  * @pf: the PF driver structure
347  * @module: the module id to program
348  * @component: the name of the component being updated
349  * @image: buffer of image data to write to the NVM
350  * @length: length of the buffer
351  * @extack: netlink extended ACK structure
352  *
353  * Loop over the data for a given NVM module and program it in 4 Kb
354  * blocks. Notify devlink core of progress after each block is programmed.
355  * Loops over a block of data and programs the NVM in 4k block chunks.
356  *
357  * Note this function assumes the caller has acquired the NVM resource.
358  *
359  * Returns: zero on success, or a negative error code on failure.
360  */
361 static int
362 ice_write_nvm_module(struct ice_pf *pf, u16 module, const char *component,
363 		     const u8 *image, u32 length,
364 		     struct netlink_ext_ack *extack)
365 {
366 	struct device *dev = ice_pf_to_dev(pf);
367 	struct devlink *devlink;
368 	u32 offset = 0;
369 	bool last_cmd;
370 	u8 *block;
371 	int err;
372 
373 	dev_dbg(dev, "Beginning write of flash component '%s', module 0x%02x\n", component, module);
374 
375 	devlink = priv_to_devlink(pf);
376 
377 	devlink_flash_update_status_notify(devlink, "Flashing",
378 					   component, 0, length);
379 
380 	block = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
381 	if (!block)
382 		return -ENOMEM;
383 
384 	do {
385 		u32 block_size;
386 
387 		block_size = min_t(u32, ICE_AQ_MAX_BUF_LEN, length - offset);
388 		last_cmd = !(offset + block_size < length);
389 
390 		/* ice_aq_update_nvm may copy the firmware response into the
391 		 * buffer, so we must make a copy since the source data is
392 		 * constant.
393 		 */
394 		memcpy(block, image + offset, block_size);
395 
396 		err = ice_write_one_nvm_block(pf, module, offset, block_size,
397 					      block, last_cmd, extack);
398 		if (err)
399 			break;
400 
401 		offset += block_size;
402 
403 		devlink_flash_update_status_notify(devlink, "Flashing",
404 						   component, offset, length);
405 	} while (!last_cmd);
406 
407 	dev_dbg(dev, "Completed write of flash component '%s', module 0x%02x\n", component, module);
408 
409 	if (err)
410 		devlink_flash_update_status_notify(devlink, "Flashing failed",
411 						   component, length, length);
412 	else
413 		devlink_flash_update_status_notify(devlink, "Flashing done",
414 						   component, length, length);
415 
416 	kfree(block);
417 	return err;
418 }
419 
420 /**
421  * ice_erase_nvm_module - Erase an NVM module and await firmware completion
422  * @pf: the PF data structure
423  * @module: the module to erase
424  * @component: name of the component being updated
425  * @extack: netlink extended ACK structure
426  *
427  * Erase the inactive NVM bank associated with this module, and await for
428  * a completion response message from firmware.
429  *
430  * Note this function assumes the caller has acquired the NVM resource.
431  *
432  * Returns: zero on success, or a negative error code on failure.
433  */
434 static int
435 ice_erase_nvm_module(struct ice_pf *pf, u16 module, const char *component,
436 		     struct netlink_ext_ack *extack)
437 {
438 	u16 completion_module, completion_retval;
439 	struct device *dev = ice_pf_to_dev(pf);
440 	struct ice_rq_event_info event;
441 	struct ice_hw *hw = &pf->hw;
442 	struct devlink *devlink;
443 	enum ice_status status;
444 	int err;
445 
446 	dev_dbg(dev, "Beginning erase of flash component '%s', module 0x%02x\n", component, module);
447 
448 	memset(&event, 0, sizeof(event));
449 
450 	devlink = priv_to_devlink(pf);
451 
452 	devlink_flash_update_status_notify(devlink, "Erasing", component, 0, 0);
453 
454 	status = ice_aq_erase_nvm(hw, module, NULL);
455 	if (status) {
456 		dev_err(dev, "Failed to erase %s (module 0x%02x), err %s aq_err %s\n",
457 			component, module, ice_stat_str(status),
458 			ice_aq_str(hw->adminq.sq_last_status));
459 		NL_SET_ERR_MSG_MOD(extack, "Failed to erase flash module");
460 		err = -EIO;
461 		goto out_notify_devlink;
462 	}
463 
464 	/* Yes, this really can take minutes to complete */
465 	err = ice_aq_wait_for_event(pf, ice_aqc_opc_nvm_erase, 300 * HZ, &event);
466 	if (err) {
467 		dev_err(dev, "Timed out waiting for firmware to respond with erase completion for %s (module 0x%02x), err %d\n",
468 			component, module, err);
469 		NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
470 		goto out_notify_devlink;
471 	}
472 
473 	completion_module = le16_to_cpu(event.desc.params.nvm.module_typeid);
474 	completion_retval = le16_to_cpu(event.desc.retval);
475 
476 	if (completion_module != module) {
477 		dev_err(dev, "Unexpected module_typeid in erase completion for %s: got 0x%x, expected 0x%x\n",
478 			component, completion_module, module);
479 		NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
480 		err = -EIO;
481 		goto out_notify_devlink;
482 	}
483 
484 	if (completion_retval) {
485 		dev_err(dev, "Firmware failed to erase %s (module 0x02%x), aq_err %s\n",
486 			component, module,
487 			ice_aq_str((enum ice_aq_err)completion_retval));
488 		NL_SET_ERR_MSG_MOD(extack, "Firmware failed to erase flash");
489 		err = -EIO;
490 		goto out_notify_devlink;
491 	}
492 
493 	dev_dbg(dev, "Completed erase of flash component '%s', module 0x%02x\n", component, module);
494 
495 out_notify_devlink:
496 	if (err)
497 		devlink_flash_update_status_notify(devlink, "Erasing failed",
498 						   component, 0, 0);
499 	else
500 		devlink_flash_update_status_notify(devlink, "Erasing done",
501 						   component, 0, 0);
502 
503 	return err;
504 }
505 
506 /**
507  * ice_switch_flash_banks - Tell firmware to switch NVM banks
508  * @pf: Pointer to the PF data structure
509  * @activate_flags: flags used for the activation command
510  * @extack: netlink extended ACK structure
511  *
512  * Notify firmware to activate the newly written flash banks, and wait for the
513  * firmware response.
514  *
515  * Returns: zero on success or an error code on failure.
516  */
517 static int ice_switch_flash_banks(struct ice_pf *pf, u8 activate_flags,
518 				  struct netlink_ext_ack *extack)
519 {
520 	struct device *dev = ice_pf_to_dev(pf);
521 	struct ice_rq_event_info event;
522 	struct ice_hw *hw = &pf->hw;
523 	enum ice_status status;
524 	u16 completion_retval;
525 	int err;
526 
527 	memset(&event, 0, sizeof(event));
528 
529 	status = ice_nvm_write_activate(hw, activate_flags);
530 	if (status) {
531 		dev_err(dev, "Failed to switch active flash banks, err %s aq_err %s\n",
532 			ice_stat_str(status),
533 			ice_aq_str(hw->adminq.sq_last_status));
534 		NL_SET_ERR_MSG_MOD(extack, "Failed to switch active flash banks");
535 		return -EIO;
536 	}
537 
538 	err = ice_aq_wait_for_event(pf, ice_aqc_opc_nvm_write_activate, 30 * HZ,
539 				    &event);
540 	if (err) {
541 		dev_err(dev, "Timed out waiting for firmware to switch active flash banks, err %d\n",
542 			err);
543 		NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
544 		return err;
545 	}
546 
547 	completion_retval = le16_to_cpu(event.desc.retval);
548 	if (completion_retval) {
549 		dev_err(dev, "Firmware failed to switch active flash banks aq_err %s\n",
550 			ice_aq_str((enum ice_aq_err)completion_retval));
551 		NL_SET_ERR_MSG_MOD(extack, "Firmware failed to switch active flash banks");
552 		return -EIO;
553 	}
554 
555 	return 0;
556 }
557 
558 /**
559  * ice_flash_component - Flash a component of the NVM
560  * @context: PLDM fw update structure
561  * @component: the component table to program
562  *
563  * Program the flash contents for a given component. First, determine the
564  * module id. Then, erase the secondary bank for this module. Finally, write
565  * the contents of the component to the NVM.
566  *
567  * Note this function assumes the caller has acquired the NVM resource.
568  *
569  * Returns: zero on success, or a negative error code on failure.
570  */
571 static int
572 ice_flash_component(struct pldmfw *context, struct pldmfw_component *component)
573 {
574 	struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
575 	struct netlink_ext_ack *extack = priv->extack;
576 	struct ice_pf *pf = priv->pf;
577 	const char *name;
578 	u16 module;
579 	u8 flag;
580 	int err;
581 
582 	switch (component->identifier) {
583 	case NVM_COMP_ID_OROM:
584 		module = ICE_SR_1ST_OROM_BANK_PTR;
585 		flag = ICE_AQC_NVM_ACTIV_SEL_OROM;
586 		name = "fw.undi";
587 		break;
588 	case NVM_COMP_ID_NVM:
589 		module = ICE_SR_1ST_NVM_BANK_PTR;
590 		flag = ICE_AQC_NVM_ACTIV_SEL_NVM;
591 		name = "fw.mgmt";
592 		break;
593 	case NVM_COMP_ID_NETLIST:
594 		module = ICE_SR_NETLIST_BANK_PTR;
595 		flag = ICE_AQC_NVM_ACTIV_SEL_NETLIST;
596 		name = "fw.netlist";
597 		break;
598 	default:
599 		/* This should not trigger, since we check the id before
600 		 * sending the component table to firmware.
601 		 */
602 		WARN(1, "Unexpected unknown component identifier 0x%02x",
603 		     component->identifier);
604 		return -EINVAL;
605 	}
606 
607 	/* Mark this component for activating at the end */
608 	priv->activate_flags |= flag;
609 
610 	err = ice_erase_nvm_module(pf, module, name, extack);
611 	if (err)
612 		return err;
613 
614 	return ice_write_nvm_module(pf, module, name, component->component_data,
615 				    component->component_size, extack);
616 }
617 
618 /**
619  * ice_finalize_update - Perform last steps to complete device update
620  * @context: PLDM fw update structure
621  *
622  * Called as the last step of the update process. Complete the update by
623  * telling the firmware to switch active banks, and perform a reset of
624  * configured.
625  *
626  * Returns: 0 on success, or an error code on failure.
627  */
628 static int ice_finalize_update(struct pldmfw *context)
629 {
630 	struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
631 	struct netlink_ext_ack *extack = priv->extack;
632 	struct ice_pf *pf = priv->pf;
633 
634 	/* Finally, notify firmware to activate the written NVM banks */
635 	return ice_switch_flash_banks(pf, priv->activate_flags, extack);
636 }
637 
638 static const struct pldmfw_ops ice_fwu_ops = {
639 	.match_record = &pldmfw_op_pci_match_record,
640 	.send_package_data = &ice_send_package_data,
641 	.send_component_table = &ice_send_component_table,
642 	.flash_component = &ice_flash_component,
643 	.finalize_update = &ice_finalize_update,
644 };
645 
646 /**
647  * ice_flash_pldm_image - Write a PLDM-formatted firmware image to the device
648  * @pf: private device driver structure
649  * @fw: firmware object pointing to the relevant firmware file
650  * @preservation: preservation level to request from firmware
651  * @extack: netlink extended ACK structure
652  *
653  * Parse the data for a given firmware file, verifying that it is a valid PLDM
654  * formatted image that matches this device.
655  *
656  * Extract the device record Package Data and Component Tables and send them
657  * to the firmware. Extract and write the flash data for each of the three
658  * main flash components, "fw.mgmt", "fw.undi", and "fw.netlist". Notify
659  * firmware once the data is written to the inactive banks.
660  *
661  * Returns: zero on success or a negative error code on failure.
662  */
663 int ice_flash_pldm_image(struct ice_pf *pf, const struct firmware *fw,
664 			 u8 preservation, struct netlink_ext_ack *extack)
665 {
666 	struct device *dev = ice_pf_to_dev(pf);
667 	struct ice_hw *hw = &pf->hw;
668 	struct ice_fwu_priv priv;
669 	enum ice_status status;
670 	int err;
671 
672 	switch (preservation) {
673 	case ICE_AQC_NVM_PRESERVE_ALL:
674 	case ICE_AQC_NVM_PRESERVE_SELECTED:
675 	case ICE_AQC_NVM_NO_PRESERVATION:
676 	case ICE_AQC_NVM_FACTORY_DEFAULT:
677 		break;
678 	default:
679 		WARN(1, "Unexpected preservation level request %u", preservation);
680 		return -EINVAL;
681 	}
682 
683 	memset(&priv, 0, sizeof(priv));
684 
685 	priv.context.ops = &ice_fwu_ops;
686 	priv.context.dev = dev;
687 	priv.extack = extack;
688 	priv.pf = pf;
689 	priv.activate_flags = preservation;
690 
691 	status = ice_acquire_nvm(hw, ICE_RES_WRITE);
692 	if (status) {
693 		dev_err(dev, "Failed to acquire device flash lock, err %s aq_err %s\n",
694 			ice_stat_str(status),
695 			ice_aq_str(hw->adminq.sq_last_status));
696 		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
697 		return -EIO;
698 	}
699 
700 	err = pldmfw_flash_image(&priv.context, fw);
701 
702 	ice_release_nvm(hw);
703 
704 	return err;
705 }
706 
707 /**
708  * ice_check_for_pending_update - Check for a pending flash update
709  * @pf: the PF driver structure
710  * @component: if not NULL, the name of the component being updated
711  * @extack: Netlink extended ACK structure
712  *
713  * Check whether the device already has a pending flash update. If such an
714  * update is found, cancel it so that the requested update may proceed.
715  *
716  * Returns: zero on success, or a negative error code on failure.
717  */
718 int ice_check_for_pending_update(struct ice_pf *pf, const char *component,
719 				 struct netlink_ext_ack *extack)
720 {
721 	struct devlink *devlink = priv_to_devlink(pf);
722 	struct device *dev = ice_pf_to_dev(pf);
723 	struct ice_hw_dev_caps *dev_caps;
724 	struct ice_hw *hw = &pf->hw;
725 	enum ice_status status;
726 	u8 pending = 0;
727 	int err;
728 
729 	dev_caps = kzalloc(sizeof(*dev_caps), GFP_KERNEL);
730 	if (!dev_caps)
731 		return -ENOMEM;
732 
733 	/* Read the most recent device capabilities from firmware. Do not use
734 	 * the cached values in hw->dev_caps, because the pending update flag
735 	 * may have changed, e.g. if an update was previously completed and
736 	 * the system has not yet rebooted.
737 	 */
738 	status = ice_discover_dev_caps(hw, dev_caps);
739 	if (status) {
740 		NL_SET_ERR_MSG_MOD(extack, "Unable to read device capabilities");
741 		kfree(dev_caps);
742 		return -EIO;
743 	}
744 
745 	if (dev_caps->common_cap.nvm_update_pending_nvm) {
746 		dev_info(dev, "The fw.mgmt flash component has a pending update\n");
747 		pending |= ICE_AQC_NVM_ACTIV_SEL_NVM;
748 	}
749 
750 	if (dev_caps->common_cap.nvm_update_pending_orom) {
751 		dev_info(dev, "The fw.undi flash component has a pending update\n");
752 		pending |= ICE_AQC_NVM_ACTIV_SEL_OROM;
753 	}
754 
755 	if (dev_caps->common_cap.nvm_update_pending_netlist) {
756 		dev_info(dev, "The fw.netlist flash component has a pending update\n");
757 		pending |= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
758 	}
759 
760 	kfree(dev_caps);
761 
762 	/* If the flash_update request is for a specific component, ignore all
763 	 * of the other components.
764 	 */
765 	if (component) {
766 		if (strcmp(component, "fw.mgmt") == 0)
767 			pending &= ICE_AQC_NVM_ACTIV_SEL_NVM;
768 		else if (strcmp(component, "fw.undi") == 0)
769 			pending &= ICE_AQC_NVM_ACTIV_SEL_OROM;
770 		else if (strcmp(component, "fw.netlist") == 0)
771 			pending &= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
772 		else
773 			WARN(1, "Unexpected flash component %s", component);
774 	}
775 
776 	/* There is no previous pending update, so this request may continue */
777 	if (!pending)
778 		return 0;
779 
780 	/* In order to allow overwriting a previous pending update, notify
781 	 * firmware to cancel that update by issuing the appropriate command.
782 	 */
783 	devlink_flash_update_status_notify(devlink,
784 					   "Canceling previous pending update",
785 					   component, 0, 0);
786 
787 	status = ice_acquire_nvm(hw, ICE_RES_WRITE);
788 	if (status) {
789 		dev_err(dev, "Failed to acquire device flash lock, err %s aq_err %s\n",
790 			ice_stat_str(status),
791 			ice_aq_str(hw->adminq.sq_last_status));
792 		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
793 		return -EIO;
794 	}
795 
796 	pending |= ICE_AQC_NVM_REVERT_LAST_ACTIV;
797 	err = ice_switch_flash_banks(pf, pending, extack);
798 
799 	ice_release_nvm(hw);
800 
801 	return err;
802 }
803