1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2018-2019, Intel Corporation. */
4 #include <linux/unaligned.h>
5 #include <linux/uuid.h>
6 #include <linux/crc32.h>
7 #include <linux/pldmfw.h>
10 #include "ice_fw_update.h"
13 struct pldmfw context;
16 struct netlink_ext_ack *extack;
18 /* Track which NVM banks to activate at the end of the update */
21 /* Track the firmware response of the required reset to complete the
24 * 0 - ICE_AQC_NVM_POR_FLAG - A full power on is required
25 * 1 - ICE_AQC_NVM_PERST_FLAG - A cold PCIe reset is required
26 * 2 - ICE_AQC_NVM_EMPR_FLAG - An EMP reset is required
30 /* Track if EMP reset is available */
31 u8 emp_reset_available;
35 * ice_send_package_data - Send record package data to firmware
36 * @context: PLDM fw update structure
37 * @data: pointer to the package data
38 * @length: length of the package data
40 * Send a copy of the package data associated with the PLDM record matching
41 * this device to the firmware.
43 * Note that this function sends an AdminQ command that will fail unless the
44 * NVM resource has been acquired.
46 * Returns: zero on success, or a negative error code on failure.
49 ice_send_package_data(struct pldmfw *context, const u8 *data, u16 length)
51 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
52 struct netlink_ext_ack *extack = priv->extack;
53 struct device *dev = context->dev;
54 struct ice_pf *pf = priv->pf;
55 struct ice_hw *hw = &pf->hw;
59 dev_dbg(dev, "Sending PLDM record package data to firmware\n");
61 package_data = kmemdup(data, length, GFP_KERNEL);
65 status = ice_nvm_set_pkg_data(hw, false, package_data, length, NULL);
70 dev_err(dev, "Failed to send record package data to firmware, err %d aq_err %s\n",
71 status, ice_aq_str(hw->adminq.sq_last_status));
72 NL_SET_ERR_MSG_MOD(extack, "Failed to record package data to firmware");
80 * ice_check_component_response - Report firmware response to a component
81 * @pf: device private data structure
82 * @id: component id being checked
83 * @response: indicates whether this component can be updated
84 * @code: code indicating reason for response
85 * @extack: netlink extended ACK structure
87 * Check whether firmware indicates if this component can be updated. Report
88 * a suitable error message over the netlink extended ACK if the component
91 * Returns: zero if the component can be updated, or -ECANCELED of the
92 * firmware indicates the component cannot be updated.
95 ice_check_component_response(struct ice_pf *pf, u16 id, u8 response, u8 code,
96 struct netlink_ext_ack *extack)
98 struct device *dev = ice_pf_to_dev(pf);
99 const char *component;
102 case NVM_COMP_ID_OROM:
103 component = "fw.undi";
105 case NVM_COMP_ID_NVM:
106 component = "fw.mgmt";
108 case NVM_COMP_ID_NETLIST:
109 component = "fw.netlist";
112 WARN(1, "Unexpected unknown component identifier 0x%02x", id);
116 dev_dbg(dev, "%s: firmware response 0x%x, code 0x%x\n",
117 component, response, code);
120 case ICE_AQ_NVM_PASS_COMP_CAN_BE_UPDATED:
121 /* firmware indicated this update is good to proceed */
123 case ICE_AQ_NVM_PASS_COMP_CAN_MAY_BE_UPDATEABLE:
124 dev_warn(dev, "firmware recommends not updating %s, as it may result in a downgrade. continuing anyways\n", component);
126 case ICE_AQ_NVM_PASS_COMP_CAN_NOT_BE_UPDATED:
127 dev_info(dev, "firmware has rejected updating %s\n", component);
129 case ICE_AQ_NVM_PASS_COMP_PARTIAL_CHECK:
130 if (ice_is_recovery_mode(&pf->hw))
136 case ICE_AQ_NVM_PASS_COMP_STAMP_IDENTICAL_CODE:
137 dev_err(dev, "Component comparison stamp for %s is identical to the running image\n",
139 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is identical to running image");
141 case ICE_AQ_NVM_PASS_COMP_STAMP_LOWER:
142 dev_err(dev, "Component comparison stamp for %s is lower than the running image\n",
144 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is lower than running image");
146 case ICE_AQ_NVM_PASS_COMP_INVALID_STAMP_CODE:
147 dev_err(dev, "Component comparison stamp for %s is invalid\n",
149 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is invalid");
151 case ICE_AQ_NVM_PASS_COMP_CONFLICT_CODE:
152 dev_err(dev, "%s conflicts with a previous component table\n",
154 NL_SET_ERR_MSG_MOD(extack, "Component table conflict occurred");
156 case ICE_AQ_NVM_PASS_COMP_PRE_REQ_NOT_MET_CODE:
157 dev_err(dev, "Pre-requisites for component %s have not been met\n",
159 NL_SET_ERR_MSG_MOD(extack, "Component pre-requisites not met");
161 case ICE_AQ_NVM_PASS_COMP_NOT_SUPPORTED_CODE:
162 dev_err(dev, "%s is not a supported component\n",
164 NL_SET_ERR_MSG_MOD(extack, "Component not supported");
166 case ICE_AQ_NVM_PASS_COMP_CANNOT_DOWNGRADE_CODE:
167 dev_err(dev, "Security restrictions prevent %s from being downgraded\n",
169 NL_SET_ERR_MSG_MOD(extack, "Component cannot be downgraded");
171 case ICE_AQ_NVM_PASS_COMP_INCOMPLETE_IMAGE_CODE:
172 dev_err(dev, "Received an incomplete component image for %s\n",
174 NL_SET_ERR_MSG_MOD(extack, "Incomplete component image");
176 case ICE_AQ_NVM_PASS_COMP_VER_STR_IDENTICAL_CODE:
177 dev_err(dev, "Component version for %s is identical to the running image\n",
179 NL_SET_ERR_MSG_MOD(extack, "Component version is identical to running image");
181 case ICE_AQ_NVM_PASS_COMP_VER_STR_LOWER_CODE:
182 dev_err(dev, "Component version for %s is lower than the running image\n",
184 NL_SET_ERR_MSG_MOD(extack, "Component version is lower than the running image");
187 dev_err(dev, "Unexpected response code 0x02%x for %s\n",
189 NL_SET_ERR_MSG_MOD(extack, "Received unexpected response code from firmware");
197 * ice_send_component_table - Send PLDM component table to firmware
198 * @context: PLDM fw update structure
199 * @component: the component to process
200 * @transfer_flag: relative transfer order of this component
202 * Read relevant data from the component and forward it to the device
203 * firmware. Check the response to determine if the firmware indicates that
204 * the update can proceed.
206 * This function sends AdminQ commands related to the NVM, and assumes that
207 * the NVM resource has been acquired.
209 * Returns: zero on success, or a negative error code on failure.
212 ice_send_component_table(struct pldmfw *context, struct pldmfw_component *component,
215 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
216 struct netlink_ext_ack *extack = priv->extack;
217 struct ice_aqc_nvm_comp_tbl *comp_tbl;
218 u8 comp_response, comp_response_code;
219 struct device *dev = context->dev;
220 struct ice_pf *pf = priv->pf;
221 struct ice_hw *hw = &pf->hw;
225 switch (component->identifier) {
226 case NVM_COMP_ID_OROM:
227 case NVM_COMP_ID_NVM:
228 case NVM_COMP_ID_NETLIST:
231 dev_err(dev, "Unable to update due to a firmware component with unknown ID %u\n",
232 component->identifier);
233 NL_SET_ERR_MSG_MOD(extack, "Unable to update due to unknown firmware component");
237 length = struct_size(comp_tbl, cvs, component->version_len);
238 comp_tbl = kzalloc(length, GFP_KERNEL);
242 comp_tbl->comp_class = cpu_to_le16(component->classification);
243 comp_tbl->comp_id = cpu_to_le16(component->identifier);
244 comp_tbl->comp_class_idx = FWU_COMP_CLASS_IDX_NOT_USE;
245 comp_tbl->comp_cmp_stamp = cpu_to_le32(component->comparison_stamp);
246 comp_tbl->cvs_type = component->version_type;
247 comp_tbl->cvs_len = component->version_len;
248 memcpy(comp_tbl->cvs, component->version_string, component->version_len);
250 dev_dbg(dev, "Sending component table to firmware:\n");
252 status = ice_nvm_pass_component_tbl(hw, (u8 *)comp_tbl, length,
253 transfer_flag, &comp_response,
254 &comp_response_code, NULL);
259 dev_err(dev, "Failed to transfer component table to firmware, err %d aq_err %s\n",
260 status, ice_aq_str(hw->adminq.sq_last_status));
261 NL_SET_ERR_MSG_MOD(extack, "Failed to transfer component table to firmware");
265 return ice_check_component_response(pf, component->identifier, comp_response,
266 comp_response_code, extack);
270 * ice_write_one_nvm_block - Write an NVM block and await completion response
271 * @pf: the PF data structure
272 * @module: the module to write to
273 * @offset: offset in bytes
274 * @block_size: size of the block to write, up to 4k
275 * @block: pointer to block of data to write
276 * @last_cmd: whether this is the last command
277 * @reset_level: storage for reset level required
278 * @extack: netlink extended ACK structure
280 * Write a block of data to a flash module, and await for the completion
281 * response message from firmware.
283 * Note this function assumes the caller has acquired the NVM resource.
285 * On successful return, reset level indicates the device reset required to
286 * complete the update.
288 * 0 - ICE_AQC_NVM_POR_FLAG - A full power on is required
289 * 1 - ICE_AQC_NVM_PERST_FLAG - A cold PCIe reset is required
290 * 2 - ICE_AQC_NVM_EMPR_FLAG - An EMP reset is required
292 * Returns: zero on success, or a negative error code on failure.
294 int ice_write_one_nvm_block(struct ice_pf *pf, u16 module, u32 offset,
295 u16 block_size, u8 *block, bool last_cmd,
296 u8 *reset_level, struct netlink_ext_ack *extack)
298 u16 completion_module, completion_retval;
299 struct device *dev = ice_pf_to_dev(pf);
300 struct ice_aq_task task = {};
301 struct ice_hw *hw = &pf->hw;
302 struct ice_aq_desc *desc;
303 u32 completion_offset;
306 dev_dbg(dev, "Writing block of %u bytes for module 0x%02x at offset %u\n",
307 block_size, module, offset);
309 ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_write);
311 err = ice_aq_update_nvm(hw, module, offset, block_size, block,
314 dev_err(dev, "Failed to flash module 0x%02x with block of size %u at offset %u, err %d aq_err %s\n",
315 module, block_size, offset, err,
316 ice_aq_str(hw->adminq.sq_last_status));
317 NL_SET_ERR_MSG_MOD(extack, "Failed to program flash module");
321 /* In most cases, firmware reports a write completion within a few
322 * milliseconds. However, it has been observed that a completion might
323 * take more than a second to complete in some cases. The timeout here
324 * is conservative and is intended to prevent failure to update when
325 * firmware is slow to respond.
327 err = ice_aq_wait_for_event(pf, &task, 15 * HZ);
329 dev_err(dev, "Timed out while trying to flash module 0x%02x with block of size %u at offset %u, err %d\n",
330 module, block_size, offset, err);
331 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
335 desc = &task.event.desc;
336 completion_module = le16_to_cpu(desc->params.nvm.module_typeid);
337 completion_retval = le16_to_cpu(desc->retval);
339 completion_offset = le16_to_cpu(desc->params.nvm.offset_low);
340 completion_offset |= desc->params.nvm.offset_high << 16;
342 if (completion_module != module) {
343 dev_err(dev, "Unexpected module_typeid in write completion: got 0x%x, expected 0x%x\n",
344 completion_module, module);
345 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
349 if (completion_offset != offset) {
350 dev_err(dev, "Unexpected offset in write completion: got %u, expected %u\n",
351 completion_offset, offset);
352 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
356 if (completion_retval) {
357 dev_err(dev, "Firmware failed to flash module 0x%02x with block of size %u at offset %u, err %s\n",
358 module, block_size, offset,
359 ice_aq_str((enum ice_aq_err)completion_retval));
360 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to program flash module");
364 /* For the last command to write the NVM bank, newer versions of
365 * firmware indicate the required level of reset to complete
366 * activation of firmware. If the firmware supports this, cache the
367 * response for indicating to the user later. Otherwise, assume that
368 * a full power cycle is required.
370 if (reset_level && last_cmd && module == ICE_SR_1ST_NVM_BANK_PTR) {
371 if (hw->dev_caps.common_cap.pcie_reset_avoidance) {
372 *reset_level = desc->params.nvm.cmd_flags &
373 ICE_AQC_NVM_RESET_LVL_M;
374 dev_dbg(dev, "Firmware reported required reset level as %u\n",
377 *reset_level = ICE_AQC_NVM_POR_FLAG;
378 dev_dbg(dev, "Firmware doesn't support indicating required reset level. Assuming a power cycle is required\n");
386 * ice_write_nvm_module - Write data to an NVM module
387 * @pf: the PF driver structure
388 * @module: the module id to program
389 * @component: the name of the component being updated
390 * @image: buffer of image data to write to the NVM
391 * @length: length of the buffer
392 * @reset_level: storage for reset level required
393 * @extack: netlink extended ACK structure
395 * Loop over the data for a given NVM module and program it in 4 Kb
396 * blocks. Notify devlink core of progress after each block is programmed.
397 * Loops over a block of data and programs the NVM in 4k block chunks.
399 * Note this function assumes the caller has acquired the NVM resource.
401 * Returns: zero on success, or a negative error code on failure.
404 ice_write_nvm_module(struct ice_pf *pf, u16 module, const char *component,
405 const u8 *image, u32 length, u8 *reset_level,
406 struct netlink_ext_ack *extack)
408 struct device *dev = ice_pf_to_dev(pf);
409 struct devlink *devlink;
415 dev_dbg(dev, "Beginning write of flash component '%s', module 0x%02x\n", component, module);
417 devlink = priv_to_devlink(pf);
419 devlink_flash_update_status_notify(devlink, "Flashing",
420 component, 0, length);
422 block = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
429 block_size = min_t(u32, ICE_AQ_MAX_BUF_LEN, length - offset);
430 last_cmd = !(offset + block_size < length);
432 /* ice_aq_update_nvm may copy the firmware response into the
433 * buffer, so we must make a copy since the source data is
436 memcpy(block, image + offset, block_size);
438 err = ice_write_one_nvm_block(pf, module, offset, block_size,
439 block, last_cmd, reset_level,
444 offset += block_size;
446 devlink_flash_update_status_notify(devlink, "Flashing",
447 component, offset, length);
450 dev_dbg(dev, "Completed write of flash component '%s', module 0x%02x\n", component, module);
453 devlink_flash_update_status_notify(devlink, "Flashing failed",
454 component, length, length);
456 devlink_flash_update_status_notify(devlink, "Flashing done",
457 component, length, length);
463 /* Length in seconds to wait before timing out when erasing a flash module.
464 * Yes, erasing really can take minutes to complete.
466 #define ICE_FW_ERASE_TIMEOUT 300
469 * ice_erase_nvm_module - Erase an NVM module and await firmware completion
470 * @pf: the PF data structure
471 * @module: the module to erase
472 * @component: name of the component being updated
473 * @extack: netlink extended ACK structure
475 * Erase the inactive NVM bank associated with this module, and await for
476 * a completion response message from firmware.
478 * Note this function assumes the caller has acquired the NVM resource.
480 * Returns: zero on success, or a negative error code on failure.
483 ice_erase_nvm_module(struct ice_pf *pf, u16 module, const char *component,
484 struct netlink_ext_ack *extack)
486 u16 completion_module, completion_retval;
487 struct device *dev = ice_pf_to_dev(pf);
488 struct ice_aq_task task = {};
489 struct ice_hw *hw = &pf->hw;
490 struct ice_aq_desc *desc;
491 struct devlink *devlink;
494 dev_dbg(dev, "Beginning erase of flash component '%s', module 0x%02x\n", component, module);
496 devlink = priv_to_devlink(pf);
498 devlink_flash_update_timeout_notify(devlink, "Erasing", component, ICE_FW_ERASE_TIMEOUT);
500 ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_erase);
502 err = ice_aq_erase_nvm(hw, module, NULL);
504 dev_err(dev, "Failed to erase %s (module 0x%02x), err %d aq_err %s\n",
505 component, module, err,
506 ice_aq_str(hw->adminq.sq_last_status));
507 NL_SET_ERR_MSG_MOD(extack, "Failed to erase flash module");
509 goto out_notify_devlink;
512 err = ice_aq_wait_for_event(pf, &task, ICE_FW_ERASE_TIMEOUT * HZ);
514 dev_err(dev, "Timed out waiting for firmware to respond with erase completion for %s (module 0x%02x), err %d\n",
515 component, module, err);
516 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
517 goto out_notify_devlink;
520 desc = &task.event.desc;
521 completion_module = le16_to_cpu(desc->params.nvm.module_typeid);
522 completion_retval = le16_to_cpu(desc->retval);
524 if (completion_module != module) {
525 dev_err(dev, "Unexpected module_typeid in erase completion for %s: got 0x%x, expected 0x%x\n",
526 component, completion_module, module);
527 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
529 goto out_notify_devlink;
532 if (completion_retval) {
533 dev_err(dev, "Firmware failed to erase %s (module 0x02%x), aq_err %s\n",
535 ice_aq_str((enum ice_aq_err)completion_retval));
536 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to erase flash");
538 goto out_notify_devlink;
541 dev_dbg(dev, "Completed erase of flash component '%s', module 0x%02x\n", component, module);
545 devlink_flash_update_status_notify(devlink, "Erasing failed",
548 devlink_flash_update_status_notify(devlink, "Erasing done",
555 * ice_switch_flash_banks - Tell firmware to switch NVM banks
556 * @pf: Pointer to the PF data structure
557 * @activate_flags: flags used for the activation command
558 * @emp_reset_available: on return, indicates if EMP reset is available
559 * @extack: netlink extended ACK structure
561 * Notify firmware to activate the newly written flash banks, and wait for the
564 * Returns: zero on success or an error code on failure.
567 ice_switch_flash_banks(struct ice_pf *pf, u8 activate_flags,
568 u8 *emp_reset_available, struct netlink_ext_ack *extack)
570 struct device *dev = ice_pf_to_dev(pf);
571 struct ice_aq_task task = {};
572 struct ice_hw *hw = &pf->hw;
573 u16 completion_retval;
577 ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_write_activate);
579 err = ice_nvm_write_activate(hw, activate_flags, &response_flags);
581 dev_err(dev, "Failed to switch active flash banks, err %d aq_err %s\n",
582 err, ice_aq_str(hw->adminq.sq_last_status));
583 NL_SET_ERR_MSG_MOD(extack, "Failed to switch active flash banks");
587 /* Newer versions of firmware have support to indicate whether an EMP
588 * reset to reload firmware is available. For older firmware, EMP
589 * reset is always available.
591 if (emp_reset_available) {
592 if (hw->dev_caps.common_cap.reset_restrict_support) {
593 *emp_reset_available = response_flags & ICE_AQC_NVM_EMPR_ENA;
594 dev_dbg(dev, "Firmware indicated that EMP reset is %s\n",
595 *emp_reset_available ?
596 "available" : "not available");
598 *emp_reset_available = ICE_AQC_NVM_EMPR_ENA;
599 dev_dbg(dev, "Firmware does not support restricting EMP reset availability\n");
603 err = ice_aq_wait_for_event(pf, &task, 30 * HZ);
605 dev_err(dev, "Timed out waiting for firmware to switch active flash banks, err %d\n",
607 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
611 completion_retval = le16_to_cpu(task.event.desc.retval);
612 if (completion_retval) {
613 dev_err(dev, "Firmware failed to switch active flash banks aq_err %s\n",
614 ice_aq_str((enum ice_aq_err)completion_retval));
615 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to switch active flash banks");
623 * ice_flash_component - Flash a component of the NVM
624 * @context: PLDM fw update structure
625 * @component: the component table to program
627 * Program the flash contents for a given component. First, determine the
628 * module id. Then, erase the secondary bank for this module. Finally, write
629 * the contents of the component to the NVM.
631 * Note this function assumes the caller has acquired the NVM resource.
633 * Returns: zero on success, or a negative error code on failure.
636 ice_flash_component(struct pldmfw *context, struct pldmfw_component *component)
638 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
639 struct netlink_ext_ack *extack = priv->extack;
640 struct ice_pf *pf = priv->pf;
647 switch (component->identifier) {
648 case NVM_COMP_ID_OROM:
649 module = ICE_SR_1ST_OROM_BANK_PTR;
650 flag = ICE_AQC_NVM_ACTIV_SEL_OROM;
654 case NVM_COMP_ID_NVM:
655 module = ICE_SR_1ST_NVM_BANK_PTR;
656 flag = ICE_AQC_NVM_ACTIV_SEL_NVM;
657 reset_level = &priv->reset_level;
660 case NVM_COMP_ID_NETLIST:
661 module = ICE_SR_NETLIST_BANK_PTR;
662 flag = ICE_AQC_NVM_ACTIV_SEL_NETLIST;
667 /* This should not trigger, since we check the id before
668 * sending the component table to firmware.
670 WARN(1, "Unexpected unknown component identifier 0x%02x",
671 component->identifier);
675 /* Mark this component for activating at the end */
676 priv->activate_flags |= flag;
678 err = ice_erase_nvm_module(pf, module, name, extack);
682 return ice_write_nvm_module(pf, module, name, component->component_data,
683 component->component_size, reset_level,
688 * ice_finalize_update - Perform last steps to complete device update
689 * @context: PLDM fw update structure
691 * Called as the last step of the update process. Complete the update by
692 * telling the firmware to switch active banks, and perform a reset of
695 * Returns: 0 on success, or an error code on failure.
697 static int ice_finalize_update(struct pldmfw *context)
699 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
700 struct netlink_ext_ack *extack = priv->extack;
701 struct ice_pf *pf = priv->pf;
702 struct devlink *devlink;
705 /* Finally, notify firmware to activate the written NVM banks */
706 err = ice_switch_flash_banks(pf, priv->activate_flags,
707 &priv->emp_reset_available, extack);
711 devlink = priv_to_devlink(pf);
713 /* If the required reset is EMPR, but EMPR is disabled, report that
714 * a reboot is required instead.
716 if (priv->reset_level == ICE_AQC_NVM_EMPR_FLAG &&
717 !priv->emp_reset_available) {
718 dev_dbg(ice_pf_to_dev(pf), "Firmware indicated EMP reset as sufficient, but EMP reset is disabled\n");
719 priv->reset_level = ICE_AQC_NVM_PERST_FLAG;
722 switch (priv->reset_level) {
723 case ICE_AQC_NVM_EMPR_FLAG:
724 devlink_flash_update_status_notify(devlink,
725 "Activate new firmware by devlink reload",
728 case ICE_AQC_NVM_PERST_FLAG:
729 devlink_flash_update_status_notify(devlink,
730 "Activate new firmware by rebooting the system",
733 case ICE_AQC_NVM_POR_FLAG:
735 devlink_flash_update_status_notify(devlink,
736 "Activate new firmware by power cycling the system",
741 pf->fw_emp_reset_disabled = !priv->emp_reset_available;
746 struct ice_pldm_pci_record_id {
749 u32 subsystem_vendor;
750 u32 subsystem_device;
754 * ice_op_pci_match_record - Check if a PCI device matches the record
755 * @context: PLDM fw update structure
756 * @record: list of records extracted from the PLDM image
758 * Determine if the PCI device associated with this device matches the record
761 * Searches the descriptor TLVs and extracts the relevant descriptor data into
762 * a pldm_pci_record_id. This is then compared against the PCI device ID
765 * Returns: true if the device matches the record, false otherwise.
768 ice_op_pci_match_record(struct pldmfw *context, struct pldmfw_record *record)
770 struct pci_dev *pdev = to_pci_dev(context->dev);
771 struct ice_pldm_pci_record_id id = {
772 .vendor = PCI_ANY_ID,
773 .device = PCI_ANY_ID,
774 .subsystem_vendor = PCI_ANY_ID,
775 .subsystem_device = PCI_ANY_ID,
777 struct pldmfw_desc_tlv *desc;
779 list_for_each_entry(desc, &record->descs, entry) {
783 switch (desc->type) {
784 case PLDM_DESC_ID_PCI_VENDOR_ID:
787 case PLDM_DESC_ID_PCI_DEVICE_ID:
790 case PLDM_DESC_ID_PCI_SUBVENDOR_ID:
791 ptr = &id.subsystem_vendor;
793 case PLDM_DESC_ID_PCI_SUBDEV_ID:
794 ptr = &id.subsystem_device;
797 /* Skip unrelated TLVs */
801 value = get_unaligned_le16(desc->data);
802 /* A value of zero for one of the descriptors is sometimes
803 * used when the record should ignore this field when matching
804 * device. For example if the record applies to any subsystem
813 /* the E822 device can have a generic device ID so check for that */
814 if ((id.vendor == PCI_ANY_ID || id.vendor == pdev->vendor) &&
815 (id.device == PCI_ANY_ID || id.device == pdev->device ||
816 id.device == ICE_DEV_ID_E822_SI_DFLT) &&
817 (id.subsystem_vendor == PCI_ANY_ID ||
818 id.subsystem_vendor == pdev->subsystem_vendor) &&
819 (id.subsystem_device == PCI_ANY_ID ||
820 id.subsystem_device == pdev->subsystem_device))
826 static const struct pldmfw_ops ice_fwu_ops_e810 = {
827 .match_record = &pldmfw_op_pci_match_record,
828 .send_package_data = &ice_send_package_data,
829 .send_component_table = &ice_send_component_table,
830 .flash_component = &ice_flash_component,
831 .finalize_update = &ice_finalize_update,
834 static const struct pldmfw_ops ice_fwu_ops_e822 = {
835 .match_record = &ice_op_pci_match_record,
836 .send_package_data = &ice_send_package_data,
837 .send_component_table = &ice_send_component_table,
838 .flash_component = &ice_flash_component,
839 .finalize_update = &ice_finalize_update,
843 * ice_get_pending_updates - Check if the component has a pending update
844 * @pf: the PF driver structure
845 * @pending: on return, bitmap of updates pending
846 * @extack: Netlink extended ACK
848 * Check if the device has any pending updates on any flash components.
850 * Returns: zero on success, or a negative error code on failure. Updates
851 * pending with the bitmap of pending updates.
853 int ice_get_pending_updates(struct ice_pf *pf, u8 *pending,
854 struct netlink_ext_ack *extack)
856 struct device *dev = ice_pf_to_dev(pf);
857 struct ice_hw_dev_caps *dev_caps;
858 struct ice_hw *hw = &pf->hw;
861 dev_caps = kzalloc(sizeof(*dev_caps), GFP_KERNEL);
865 /* Read the most recent device capabilities from firmware. Do not use
866 * the cached values in hw->dev_caps, because the pending update flag
867 * may have changed, e.g. if an update was previously completed and
868 * the system has not yet rebooted.
870 err = ice_discover_dev_caps(hw, dev_caps);
872 NL_SET_ERR_MSG_MOD(extack, "Unable to read device capabilities");
879 if (dev_caps->common_cap.nvm_update_pending_nvm) {
880 dev_info(dev, "The fw.mgmt flash component has a pending update\n");
881 *pending |= ICE_AQC_NVM_ACTIV_SEL_NVM;
884 if (dev_caps->common_cap.nvm_update_pending_orom) {
885 dev_info(dev, "The fw.undi flash component has a pending update\n");
886 *pending |= ICE_AQC_NVM_ACTIV_SEL_OROM;
889 if (dev_caps->common_cap.nvm_update_pending_netlist) {
890 dev_info(dev, "The fw.netlist flash component has a pending update\n");
891 *pending |= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
900 * ice_cancel_pending_update - Cancel any pending update for a component
901 * @pf: the PF driver structure
902 * @component: if not NULL, the name of the component being updated
903 * @extack: Netlink extended ACK structure
905 * Cancel any pending update for the specified component. If component is
906 * NULL, all device updates will be canceled.
908 * Returns: zero on success, or a negative error code on failure.
911 ice_cancel_pending_update(struct ice_pf *pf, const char *component,
912 struct netlink_ext_ack *extack)
914 struct devlink *devlink = priv_to_devlink(pf);
915 struct device *dev = ice_pf_to_dev(pf);
916 struct ice_hw *hw = &pf->hw;
920 err = ice_get_pending_updates(pf, &pending, extack);
924 /* If the flash_update request is for a specific component, ignore all
925 * of the other components.
928 if (strcmp(component, "fw.mgmt") == 0)
929 pending &= ICE_AQC_NVM_ACTIV_SEL_NVM;
930 else if (strcmp(component, "fw.undi") == 0)
931 pending &= ICE_AQC_NVM_ACTIV_SEL_OROM;
932 else if (strcmp(component, "fw.netlist") == 0)
933 pending &= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
935 WARN(1, "Unexpected flash component %s", component);
938 /* There is no previous pending update, so this request may continue */
942 /* In order to allow overwriting a previous pending update, notify
943 * firmware to cancel that update by issuing the appropriate command.
945 devlink_flash_update_status_notify(devlink,
946 "Canceling previous pending update",
949 err = ice_acquire_nvm(hw, ICE_RES_WRITE);
951 dev_err(dev, "Failed to acquire device flash lock, err %d aq_err %s\n",
952 err, ice_aq_str(hw->adminq.sq_last_status));
953 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
957 pending |= ICE_AQC_NVM_REVERT_LAST_ACTIV;
958 err = ice_switch_flash_banks(pf, pending, NULL, extack);
962 /* Since we've canceled the pending update, we no longer know if EMP
963 * reset is restricted.
965 pf->fw_emp_reset_disabled = false;
971 * ice_devlink_flash_update - Write a firmware image to the device
972 * @devlink: pointer to devlink associated with the device to update
973 * @params: devlink flash update parameters
974 * @extack: netlink extended ACK structure
976 * Parse the data for a given firmware file, verifying that it is a valid PLDM
977 * formatted image that matches this device.
979 * Extract the device record Package Data and Component Tables and send them
980 * to the firmware. Extract and write the flash data for each of the three
981 * main flash components, "fw.mgmt", "fw.undi", and "fw.netlist". Notify
982 * firmware once the data is written to the inactive banks.
984 * Returns: zero on success or a negative error code on failure.
986 int ice_devlink_flash_update(struct devlink *devlink,
987 struct devlink_flash_update_params *params,
988 struct netlink_ext_ack *extack)
990 struct ice_pf *pf = devlink_priv(devlink);
991 struct device *dev = ice_pf_to_dev(pf);
992 struct ice_hw *hw = &pf->hw;
993 struct ice_fwu_priv priv;
997 if (!params->overwrite_mask) {
998 /* preserve all settings and identifiers */
999 preservation = ICE_AQC_NVM_PRESERVE_ALL;
1000 } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
1001 /* overwrite settings, but preserve the vital device identifiers */
1002 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
1003 } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
1004 DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
1005 /* overwrite both settings and identifiers, preserve nothing */
1006 preservation = ICE_AQC_NVM_NO_PRESERVATION;
1008 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
1012 if (!hw->dev_caps.common_cap.nvm_unified_update && !ice_is_recovery_mode(hw)) {
1013 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
1017 memset(&priv, 0, sizeof(priv));
1019 if (params->component && strcmp(params->component, "fw.mgmt") == 0) {
1020 priv.context.mode = PLDMFW_UPDATE_MODE_SINGLE_COMPONENT;
1021 priv.context.component_identifier = NVM_COMP_ID_NVM;
1022 } else if (params->component) {
1026 /* the E822 device needs a slightly different ops */
1027 if (hw->mac_type == ICE_MAC_GENERIC)
1028 priv.context.ops = &ice_fwu_ops_e822;
1030 priv.context.ops = &ice_fwu_ops_e810;
1031 priv.context.dev = dev;
1032 priv.extack = extack;
1034 priv.activate_flags = preservation;
1036 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
1038 err = ice_cancel_pending_update(pf, NULL, extack);
1042 err = ice_acquire_nvm(hw, ICE_RES_WRITE);
1044 dev_err(dev, "Failed to acquire device flash lock, err %d aq_err %s\n",
1045 err, ice_aq_str(hw->adminq.sq_last_status));
1046 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
1050 err = pldmfw_flash_image(&priv.context, params->fw);
1051 if (err == -ENOENT) {
1052 dev_err(dev, "Firmware image has no record matching this device\n");
1053 NL_SET_ERR_MSG_MOD(extack, "Firmware image has no record matching this device");
1055 /* Do not set a generic extended ACK message here. A more
1056 * specific message may already have been set by one of our
1059 dev_err(dev, "Failed to flash PLDM image, err %d", err);
1062 ice_release_nvm(hw);