1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) 2018-2019, Intel Corporation. */ 3 4 #include <linux/unaligned.h> 5 #include <linux/uuid.h> 6 #include <linux/crc32.h> 7 #include <linux/pldmfw.h> 8 #include "ice.h" 9 #include "ice_lib.h" 10 #include "ice_fw_update.h" 11 12 struct ice_fwu_priv { 13 struct pldmfw context; 14 15 struct ice_pf *pf; 16 struct netlink_ext_ack *extack; 17 18 /* Track which NVM banks to activate at the end of the update */ 19 u8 activate_flags; 20 21 /* Track the firmware response of the required reset to complete the 22 * flash update. 23 * 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 27 */ 28 u8 reset_level; 29 30 /* Track if EMP reset is available */ 31 u8 emp_reset_available; 32 }; 33 34 /** 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 39 * 40 * Send a copy of the package data associated with the PLDM record matching 41 * this device to the firmware. 42 * 43 * Note that this function sends an AdminQ command that will fail unless the 44 * NVM resource has been acquired. 45 * 46 * Returns: zero on success, or a negative error code on failure. 47 */ 48 static int 49 ice_send_package_data(struct pldmfw *context, const u8 *data, u16 length) 50 { 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; 56 u8 *package_data; 57 int status; 58 59 dev_dbg(dev, "Sending PLDM record package data to firmware\n"); 60 61 package_data = kmemdup(data, length, GFP_KERNEL); 62 if (!package_data) 63 return -ENOMEM; 64 65 status = ice_nvm_set_pkg_data(hw, false, package_data, length, NULL); 66 67 kfree(package_data); 68 69 if (status) { 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"); 73 return -EIO; 74 } 75 76 return 0; 77 } 78 79 /** 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 86 * 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 89 * cannot be updated. 90 * 91 * Returns: zero if the component can be updated, or -ECANCELED of the 92 * firmware indicates the component cannot be updated. 93 */ 94 static int 95 ice_check_component_response(struct ice_pf *pf, u16 id, u8 response, u8 code, 96 struct netlink_ext_ack *extack) 97 { 98 struct device *dev = ice_pf_to_dev(pf); 99 const char *component; 100 101 switch (id) { 102 case NVM_COMP_ID_OROM: 103 component = "fw.undi"; 104 break; 105 case NVM_COMP_ID_NVM: 106 component = "fw.mgmt"; 107 break; 108 case NVM_COMP_ID_NETLIST: 109 component = "fw.netlist"; 110 break; 111 default: 112 WARN(1, "Unexpected unknown component identifier 0x%02x", id); 113 return -EINVAL; 114 } 115 116 dev_dbg(dev, "%s: firmware response 0x%x, code 0x%x\n", 117 component, response, code); 118 119 switch (response) { 120 case ICE_AQ_NVM_PASS_COMP_CAN_BE_UPDATED: 121 /* firmware indicated this update is good to proceed */ 122 return 0; 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); 125 return 0; 126 case ICE_AQ_NVM_PASS_COMP_CAN_NOT_BE_UPDATED: 127 dev_info(dev, "firmware has rejected updating %s\n", component); 128 break; 129 case ICE_AQ_NVM_PASS_COMP_PARTIAL_CHECK: 130 if (ice_is_recovery_mode(&pf->hw)) 131 return 0; 132 break; 133 } 134 135 switch (code) { 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", 138 component); 139 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is identical to running image"); 140 break; 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", 143 component); 144 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is lower than running image"); 145 break; 146 case ICE_AQ_NVM_PASS_COMP_INVALID_STAMP_CODE: 147 dev_err(dev, "Component comparison stamp for %s is invalid\n", 148 component); 149 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is invalid"); 150 break; 151 case ICE_AQ_NVM_PASS_COMP_CONFLICT_CODE: 152 dev_err(dev, "%s conflicts with a previous component table\n", 153 component); 154 NL_SET_ERR_MSG_MOD(extack, "Component table conflict occurred"); 155 break; 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", 158 component); 159 NL_SET_ERR_MSG_MOD(extack, "Component pre-requisites not met"); 160 break; 161 case ICE_AQ_NVM_PASS_COMP_NOT_SUPPORTED_CODE: 162 dev_err(dev, "%s is not a supported component\n", 163 component); 164 NL_SET_ERR_MSG_MOD(extack, "Component not supported"); 165 break; 166 case ICE_AQ_NVM_PASS_COMP_CANNOT_DOWNGRADE_CODE: 167 dev_err(dev, "Security restrictions prevent %s from being downgraded\n", 168 component); 169 NL_SET_ERR_MSG_MOD(extack, "Component cannot be downgraded"); 170 break; 171 case ICE_AQ_NVM_PASS_COMP_INCOMPLETE_IMAGE_CODE: 172 dev_err(dev, "Received an incomplete component image for %s\n", 173 component); 174 NL_SET_ERR_MSG_MOD(extack, "Incomplete component image"); 175 break; 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", 178 component); 179 NL_SET_ERR_MSG_MOD(extack, "Component version is identical to running image"); 180 break; 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", 183 component); 184 NL_SET_ERR_MSG_MOD(extack, "Component version is lower than the running image"); 185 break; 186 default: 187 dev_err(dev, "Unexpected response code 0x02%x for %s\n", 188 code, component); 189 NL_SET_ERR_MSG_MOD(extack, "Received unexpected response code from firmware"); 190 break; 191 } 192 193 return -ECANCELED; 194 } 195 196 /** 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 201 * 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. 205 * 206 * This function sends AdminQ commands related to the NVM, and assumes that 207 * the NVM resource has been acquired. 208 * 209 * Returns: zero on success, or a negative error code on failure. 210 */ 211 static int 212 ice_send_component_table(struct pldmfw *context, struct pldmfw_component *component, 213 u8 transfer_flag) 214 { 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; 222 size_t length; 223 int status; 224 225 switch (component->identifier) { 226 case NVM_COMP_ID_OROM: 227 case NVM_COMP_ID_NVM: 228 case NVM_COMP_ID_NETLIST: 229 break; 230 default: 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"); 234 return -EOPNOTSUPP; 235 } 236 237 length = struct_size(comp_tbl, cvs, component->version_len); 238 comp_tbl = kzalloc(length, GFP_KERNEL); 239 if (!comp_tbl) 240 return -ENOMEM; 241 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); 249 250 dev_dbg(dev, "Sending component table to firmware:\n"); 251 252 status = ice_nvm_pass_component_tbl(hw, (u8 *)comp_tbl, length, 253 transfer_flag, &comp_response, 254 &comp_response_code, NULL); 255 256 kfree(comp_tbl); 257 258 if (status) { 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"); 262 return -EIO; 263 } 264 265 return ice_check_component_response(pf, component->identifier, comp_response, 266 comp_response_code, extack); 267 } 268 269 /** 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 279 * 280 * Write a block of data to a flash module, and await for the completion 281 * response message from firmware. 282 * 283 * Note this function assumes the caller has acquired the NVM resource. 284 * 285 * On successful return, reset level indicates the device reset required to 286 * complete the update. 287 * 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 291 * 292 * Returns: zero on success, or a negative error code on failure. 293 */ 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) 297 { 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; 304 int err; 305 306 dev_dbg(dev, "Writing block of %u bytes for module 0x%02x at offset %u\n", 307 block_size, module, offset); 308 309 ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_write); 310 311 err = ice_aq_update_nvm(hw, module, offset, block_size, block, 312 last_cmd, 0, NULL); 313 if (err) { 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"); 318 return -EIO; 319 } 320 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. 326 */ 327 err = ice_aq_wait_for_event(pf, &task, 15 * HZ); 328 if (err) { 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"); 332 return -EIO; 333 } 334 335 desc = &task.event.desc; 336 completion_module = le16_to_cpu(desc->params.nvm.module_typeid); 337 completion_retval = le16_to_cpu(desc->retval); 338 339 completion_offset = le16_to_cpu(desc->params.nvm.offset_low); 340 completion_offset |= desc->params.nvm.offset_high << 16; 341 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"); 346 return -EIO; 347 } 348 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"); 353 return -EIO; 354 } 355 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"); 361 return -EIO; 362 } 363 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. 369 */ 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", 375 *reset_level); 376 } else { 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"); 379 } 380 } 381 382 return 0; 383 } 384 385 /** 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 394 * 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. 398 * 399 * Note this function assumes the caller has acquired the NVM resource. 400 * 401 * Returns: zero on success, or a negative error code on failure. 402 */ 403 static int 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) 407 { 408 struct device *dev = ice_pf_to_dev(pf); 409 struct devlink *devlink; 410 u32 offset = 0; 411 bool last_cmd; 412 u8 *block; 413 int err; 414 415 dev_dbg(dev, "Beginning write of flash component '%s', module 0x%02x\n", component, module); 416 417 devlink = priv_to_devlink(pf); 418 419 devlink_flash_update_status_notify(devlink, "Flashing", 420 component, 0, length); 421 422 block = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL); 423 if (!block) 424 return -ENOMEM; 425 426 do { 427 u32 block_size; 428 429 block_size = min_t(u32, ICE_AQ_MAX_BUF_LEN, length - offset); 430 last_cmd = !(offset + block_size < length); 431 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 434 * constant. 435 */ 436 memcpy(block, image + offset, block_size); 437 438 err = ice_write_one_nvm_block(pf, module, offset, block_size, 439 block, last_cmd, reset_level, 440 extack); 441 if (err) 442 break; 443 444 offset += block_size; 445 446 devlink_flash_update_status_notify(devlink, "Flashing", 447 component, offset, length); 448 } while (!last_cmd); 449 450 dev_dbg(dev, "Completed write of flash component '%s', module 0x%02x\n", component, module); 451 452 if (err) 453 devlink_flash_update_status_notify(devlink, "Flashing failed", 454 component, length, length); 455 else 456 devlink_flash_update_status_notify(devlink, "Flashing done", 457 component, length, length); 458 459 kfree(block); 460 return err; 461 } 462 463 /* Length in seconds to wait before timing out when erasing a flash module. 464 * Yes, erasing really can take minutes to complete. 465 */ 466 #define ICE_FW_ERASE_TIMEOUT 300 467 468 /** 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 474 * 475 * Erase the inactive NVM bank associated with this module, and await for 476 * a completion response message from firmware. 477 * 478 * Note this function assumes the caller has acquired the NVM resource. 479 * 480 * Returns: zero on success, or a negative error code on failure. 481 */ 482 static int 483 ice_erase_nvm_module(struct ice_pf *pf, u16 module, const char *component, 484 struct netlink_ext_ack *extack) 485 { 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; 492 int err; 493 494 dev_dbg(dev, "Beginning erase of flash component '%s', module 0x%02x\n", component, module); 495 496 devlink = priv_to_devlink(pf); 497 498 devlink_flash_update_timeout_notify(devlink, "Erasing", component, ICE_FW_ERASE_TIMEOUT); 499 500 ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_erase); 501 502 err = ice_aq_erase_nvm(hw, module, NULL); 503 if (err) { 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"); 508 err = -EIO; 509 goto out_notify_devlink; 510 } 511 512 err = ice_aq_wait_for_event(pf, &task, ICE_FW_ERASE_TIMEOUT * HZ); 513 if (err) { 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; 518 } 519 520 desc = &task.event.desc; 521 completion_module = le16_to_cpu(desc->params.nvm.module_typeid); 522 completion_retval = le16_to_cpu(desc->retval); 523 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"); 528 err = -EIO; 529 goto out_notify_devlink; 530 } 531 532 if (completion_retval) { 533 dev_err(dev, "Firmware failed to erase %s (module 0x02%x), aq_err %s\n", 534 component, module, 535 ice_aq_str((enum ice_aq_err)completion_retval)); 536 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to erase flash"); 537 err = -EIO; 538 goto out_notify_devlink; 539 } 540 541 dev_dbg(dev, "Completed erase of flash component '%s', module 0x%02x\n", component, module); 542 543 out_notify_devlink: 544 if (err) 545 devlink_flash_update_status_notify(devlink, "Erasing failed", 546 component, 0, 0); 547 else 548 devlink_flash_update_status_notify(devlink, "Erasing done", 549 component, 0, 0); 550 551 return err; 552 } 553 554 /** 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 560 * 561 * Notify firmware to activate the newly written flash banks, and wait for the 562 * firmware response. 563 * 564 * Returns: zero on success or an error code on failure. 565 */ 566 static int 567 ice_switch_flash_banks(struct ice_pf *pf, u8 activate_flags, 568 u8 *emp_reset_available, struct netlink_ext_ack *extack) 569 { 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; 574 u8 response_flags; 575 int err; 576 577 ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_write_activate); 578 579 err = ice_nvm_write_activate(hw, activate_flags, &response_flags); 580 if (err) { 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"); 584 return -EIO; 585 } 586 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. 590 */ 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"); 597 } else { 598 *emp_reset_available = ICE_AQC_NVM_EMPR_ENA; 599 dev_dbg(dev, "Firmware does not support restricting EMP reset availability\n"); 600 } 601 } 602 603 err = ice_aq_wait_for_event(pf, &task, 30 * HZ); 604 if (err) { 605 dev_err(dev, "Timed out waiting for firmware to switch active flash banks, err %d\n", 606 err); 607 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware"); 608 return err; 609 } 610 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"); 616 return -EIO; 617 } 618 619 return 0; 620 } 621 622 /** 623 * ice_flash_component - Flash a component of the NVM 624 * @context: PLDM fw update structure 625 * @component: the component table to program 626 * 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. 630 * 631 * Note this function assumes the caller has acquired the NVM resource. 632 * 633 * Returns: zero on success, or a negative error code on failure. 634 */ 635 static int 636 ice_flash_component(struct pldmfw *context, struct pldmfw_component *component) 637 { 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; 641 const char *name; 642 u8 *reset_level; 643 u16 module; 644 u8 flag; 645 int err; 646 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; 651 reset_level = NULL; 652 name = "fw.undi"; 653 break; 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; 658 name = "fw.mgmt"; 659 break; 660 case NVM_COMP_ID_NETLIST: 661 module = ICE_SR_NETLIST_BANK_PTR; 662 flag = ICE_AQC_NVM_ACTIV_SEL_NETLIST; 663 reset_level = NULL; 664 name = "fw.netlist"; 665 break; 666 default: 667 /* This should not trigger, since we check the id before 668 * sending the component table to firmware. 669 */ 670 WARN(1, "Unexpected unknown component identifier 0x%02x", 671 component->identifier); 672 return -EINVAL; 673 } 674 675 /* Mark this component for activating at the end */ 676 priv->activate_flags |= flag; 677 678 err = ice_erase_nvm_module(pf, module, name, extack); 679 if (err) 680 return err; 681 682 return ice_write_nvm_module(pf, module, name, component->component_data, 683 component->component_size, reset_level, 684 extack); 685 } 686 687 /** 688 * ice_finalize_update - Perform last steps to complete device update 689 * @context: PLDM fw update structure 690 * 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 693 * configured. 694 * 695 * Returns: 0 on success, or an error code on failure. 696 */ 697 static int ice_finalize_update(struct pldmfw *context) 698 { 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; 703 int err; 704 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); 708 if (err) 709 return err; 710 711 devlink = priv_to_devlink(pf); 712 713 /* If the required reset is EMPR, but EMPR is disabled, report that 714 * a reboot is required instead. 715 */ 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; 720 } 721 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", 726 NULL, 0, 0); 727 break; 728 case ICE_AQC_NVM_PERST_FLAG: 729 devlink_flash_update_status_notify(devlink, 730 "Activate new firmware by rebooting the system", 731 NULL, 0, 0); 732 break; 733 case ICE_AQC_NVM_POR_FLAG: 734 default: 735 devlink_flash_update_status_notify(devlink, 736 "Activate new firmware by power cycling the system", 737 NULL, 0, 0); 738 break; 739 } 740 741 pf->fw_emp_reset_disabled = !priv->emp_reset_available; 742 743 return 0; 744 } 745 746 struct ice_pldm_pci_record_id { 747 u32 vendor; 748 u32 device; 749 u32 subsystem_vendor; 750 u32 subsystem_device; 751 }; 752 753 /** 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 757 * 758 * Determine if the PCI device associated with this device matches the record 759 * data provided. 760 * 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 763 * information. 764 * 765 * Returns: true if the device matches the record, false otherwise. 766 */ 767 static bool 768 ice_op_pci_match_record(struct pldmfw *context, struct pldmfw_record *record) 769 { 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, 776 }; 777 struct pldmfw_desc_tlv *desc; 778 779 list_for_each_entry(desc, &record->descs, entry) { 780 u16 value; 781 int *ptr; 782 783 switch (desc->type) { 784 case PLDM_DESC_ID_PCI_VENDOR_ID: 785 ptr = &id.vendor; 786 break; 787 case PLDM_DESC_ID_PCI_DEVICE_ID: 788 ptr = &id.device; 789 break; 790 case PLDM_DESC_ID_PCI_SUBVENDOR_ID: 791 ptr = &id.subsystem_vendor; 792 break; 793 case PLDM_DESC_ID_PCI_SUBDEV_ID: 794 ptr = &id.subsystem_device; 795 break; 796 default: 797 /* Skip unrelated TLVs */ 798 continue; 799 } 800 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 805 * device or vendor. 806 */ 807 if (value) 808 *ptr = value; 809 else 810 *ptr = PCI_ANY_ID; 811 } 812 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)) 821 return true; 822 823 return false; 824 } 825 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, 832 }; 833 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, 840 }; 841 842 /** 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 847 * 848 * Check if the device has any pending updates on any flash components. 849 * 850 * Returns: zero on success, or a negative error code on failure. Updates 851 * pending with the bitmap of pending updates. 852 */ 853 int ice_get_pending_updates(struct ice_pf *pf, u8 *pending, 854 struct netlink_ext_ack *extack) 855 { 856 struct device *dev = ice_pf_to_dev(pf); 857 struct ice_hw_dev_caps *dev_caps; 858 struct ice_hw *hw = &pf->hw; 859 int err; 860 861 dev_caps = kzalloc(sizeof(*dev_caps), GFP_KERNEL); 862 if (!dev_caps) 863 return -ENOMEM; 864 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. 869 */ 870 err = ice_discover_dev_caps(hw, dev_caps); 871 if (err) { 872 NL_SET_ERR_MSG_MOD(extack, "Unable to read device capabilities"); 873 kfree(dev_caps); 874 return err; 875 } 876 877 *pending = 0; 878 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; 882 } 883 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; 887 } 888 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; 892 } 893 894 kfree(dev_caps); 895 896 return 0; 897 } 898 899 /** 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 904 * 905 * Cancel any pending update for the specified component. If component is 906 * NULL, all device updates will be canceled. 907 * 908 * Returns: zero on success, or a negative error code on failure. 909 */ 910 static int 911 ice_cancel_pending_update(struct ice_pf *pf, const char *component, 912 struct netlink_ext_ack *extack) 913 { 914 struct devlink *devlink = priv_to_devlink(pf); 915 struct device *dev = ice_pf_to_dev(pf); 916 struct ice_hw *hw = &pf->hw; 917 u8 pending; 918 int err; 919 920 err = ice_get_pending_updates(pf, &pending, extack); 921 if (err) 922 return err; 923 924 /* If the flash_update request is for a specific component, ignore all 925 * of the other components. 926 */ 927 if (component) { 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; 934 else 935 WARN(1, "Unexpected flash component %s", component); 936 } 937 938 /* There is no previous pending update, so this request may continue */ 939 if (!pending) 940 return 0; 941 942 /* In order to allow overwriting a previous pending update, notify 943 * firmware to cancel that update by issuing the appropriate command. 944 */ 945 devlink_flash_update_status_notify(devlink, 946 "Canceling previous pending update", 947 component, 0, 0); 948 949 err = ice_acquire_nvm(hw, ICE_RES_WRITE); 950 if (err) { 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"); 954 return err; 955 } 956 957 pending |= ICE_AQC_NVM_REVERT_LAST_ACTIV; 958 err = ice_switch_flash_banks(pf, pending, NULL, extack); 959 960 ice_release_nvm(hw); 961 962 /* Since we've canceled the pending update, we no longer know if EMP 963 * reset is restricted. 964 */ 965 pf->fw_emp_reset_disabled = false; 966 967 return err; 968 } 969 970 /** 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 975 * 976 * Parse the data for a given firmware file, verifying that it is a valid PLDM 977 * formatted image that matches this device. 978 * 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. 983 * 984 * Returns: zero on success or a negative error code on failure. 985 */ 986 int ice_devlink_flash_update(struct devlink *devlink, 987 struct devlink_flash_update_params *params, 988 struct netlink_ext_ack *extack) 989 { 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; 994 u8 preservation; 995 int err; 996 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; 1007 } else { 1008 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported"); 1009 return -EOPNOTSUPP; 1010 } 1011 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"); 1014 return -EOPNOTSUPP; 1015 } 1016 1017 memset(&priv, 0, sizeof(priv)); 1018 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) { 1023 return -EOPNOTSUPP; 1024 } 1025 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; 1029 else 1030 priv.context.ops = &ice_fwu_ops_e810; 1031 priv.context.dev = dev; 1032 priv.extack = extack; 1033 priv.pf = pf; 1034 priv.activate_flags = preservation; 1035 1036 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0); 1037 1038 err = ice_cancel_pending_update(pf, NULL, extack); 1039 if (err) 1040 return err; 1041 1042 err = ice_acquire_nvm(hw, ICE_RES_WRITE); 1043 if (err) { 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"); 1047 return err; 1048 } 1049 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"); 1054 } else if (err) { 1055 /* Do not set a generic extended ACK message here. A more 1056 * specific message may already have been set by one of our 1057 * ops. 1058 */ 1059 dev_err(dev, "Failed to flash PLDM image, err %d", err); 1060 } 1061 1062 ice_release_nvm(hw); 1063 1064 return err; 1065 } 1066