File Index
Files and directories
Sign in
FILE

ice_fw_update.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/ethernet/intel/ice/ice_fw_update.c C
   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
  12struct 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 */
  48static int
  49ice_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, libie_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 */
  94static int
  95ice_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 */
 211static int
 212ice_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, libie_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 */
 294int 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 libie_aq_desc *desc;
 303	struct ice_aqc_nvm *cmd;
 304	u32 completion_offset;
 305	int err;
 306
 307	dev_dbg(dev, "Writing block of %u bytes for module 0x%02x at offset %u\n",
 308		block_size, module, offset);
 309
 310	ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_write);
 311
 312	err = ice_aq_update_nvm(hw, module, offset, block_size, block,
 313				last_cmd, 0, NULL);
 314	if (err) {
 315		dev_err(dev, "Failed to flash module 0x%02x with block of size %u at offset %u, err %d aq_err %s\n",
 316			module, block_size, offset, err,
 317			libie_aq_str(hw->adminq.sq_last_status));
 318		NL_SET_ERR_MSG_MOD(extack, "Failed to program flash module");
 319		return -EIO;
 320	}
 321
 322	/* In most cases, firmware reports a write completion within a few
 323	 * milliseconds. However, it has been observed that a completion might
 324	 * take more than a second to complete in some cases. The timeout here
 325	 * is conservative and is intended to prevent failure to update when
 326	 * firmware is slow to respond.
 327	 */
 328	err = ice_aq_wait_for_event(pf, &task, 15 * HZ);
 329	if (err) {
 330		dev_err(dev, "Timed out while trying to flash module 0x%02x with block of size %u at offset %u, err %d\n",
 331			module, block_size, offset, err);
 332		NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
 333		return -EIO;
 334	}
 335
 336	desc = &task.event.desc;
 337	cmd = libie_aq_raw(desc);
 338	completion_module = le16_to_cpu(cmd->module_typeid);
 339	completion_retval = le16_to_cpu(desc->retval);
 340
 341	completion_offset = le16_to_cpu(cmd->offset_low);
 342	completion_offset |= cmd->offset_high << 16;
 343
 344	if (completion_module != module) {
 345		dev_err(dev, "Unexpected module_typeid in write completion: got 0x%x, expected 0x%x\n",
 346			completion_module, module);
 347		NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
 348		return -EIO;
 349	}
 350
 351	if (completion_offset != offset) {
 352		dev_err(dev, "Unexpected offset in write completion: got %u, expected %u\n",
 353			completion_offset, offset);
 354		NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
 355		return -EIO;
 356	}
 357
 358	if (completion_retval) {
 359		dev_err(dev, "Firmware failed to flash module 0x%02x with block of size %u at offset %u, err %s\n",
 360			module, block_size, offset,
 361			libie_aq_str((enum libie_aq_err)completion_retval));
 362		NL_SET_ERR_MSG_MOD(extack, "Firmware failed to program flash module");
 363		return -EIO;
 364	}
 365
 366	/* For the last command to write the NVM bank, newer versions of
 367	 * firmware indicate the required level of reset to complete
 368	 * activation of firmware. If the firmware supports this, cache the
 369	 * response for indicating to the user later. Otherwise, assume that
 370	 * a full power cycle is required.
 371	 */
 372	if (reset_level && last_cmd && module == ICE_SR_1ST_NVM_BANK_PTR) {
 373		if (hw->dev_caps.common_cap.pcie_reset_avoidance) {
 374			*reset_level = cmd->cmd_flags &
 375				       ICE_AQC_NVM_RESET_LVL_M;
 376			dev_dbg(dev, "Firmware reported required reset level as %u\n",
 377				*reset_level);
 378		} else {
 379			*reset_level = ICE_AQC_NVM_POR_FLAG;
 380			dev_dbg(dev, "Firmware doesn't support indicating required reset level. Assuming a power cycle is required\n");
 381		}
 382	}
 383
 384	return 0;
 385}
 386
 387/**
 388 * ice_write_nvm_module - Write data to an NVM module
 389 * @pf: the PF driver structure
 390 * @module: the module id to program
 391 * @component: the name of the component being updated
 392 * @image: buffer of image data to write to the NVM
 393 * @length: length of the buffer
 394 * @reset_level: storage for reset level required
 395 * @extack: netlink extended ACK structure
 396 *
 397 * Loop over the data for a given NVM module and program it in 4 Kb
 398 * blocks. Notify devlink core of progress after each block is programmed.
 399 * Loops over a block of data and programs the NVM in 4k block chunks.
 400 *
 401 * Note this function assumes the caller has acquired the NVM resource.
 402 *
 403 * Returns: zero on success, or a negative error code on failure.
 404 */
 405static int
 406ice_write_nvm_module(struct ice_pf *pf, u16 module, const char *component,
 407		     const u8 *image, u32 length, u8 *reset_level,
 408		     struct netlink_ext_ack *extack)
 409{
 410	struct device *dev = ice_pf_to_dev(pf);
 411	struct devlink *devlink;
 412	u32 offset = 0;
 413	bool last_cmd;
 414	u8 *block;
 415	int err;
 416
 417	dev_dbg(dev, "Beginning write of flash component '%s', module 0x%02x\n", component, module);
 418
 419	devlink = priv_to_devlink(pf);
 420
 421	devlink_flash_update_status_notify(devlink, "Flashing",
 422					   component, 0, length);
 423
 424	block = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
 425	if (!block)
 426		return -ENOMEM;
 427
 428	do {
 429		u32 block_size;
 430
 431		block_size = min_t(u32, ICE_AQ_MAX_BUF_LEN, length - offset);
 432		last_cmd = !(offset + block_size < length);
 433
 434		/* ice_aq_update_nvm may copy the firmware response into the
 435		 * buffer, so we must make a copy since the source data is
 436		 * constant.
 437		 */
 438		memcpy(block, image + offset, block_size);
 439
 440		err = ice_write_one_nvm_block(pf, module, offset, block_size,
 441					      block, last_cmd, reset_level,
 442					      extack);
 443		if (err)
 444			break;
 445
 446		offset += block_size;
 447
 448		devlink_flash_update_status_notify(devlink, "Flashing",
 449						   component, offset, length);
 450	} while (!last_cmd);
 451
 452	dev_dbg(dev, "Completed write of flash component '%s', module 0x%02x\n", component, module);
 453
 454	if (err)
 455		devlink_flash_update_status_notify(devlink, "Flashing failed",
 456						   component, length, length);
 457	else
 458		devlink_flash_update_status_notify(devlink, "Flashing done",
 459						   component, length, length);
 460
 461	kfree(block);
 462	return err;
 463}
 464
 465/* Length in seconds to wait before timing out when erasing a flash module.
 466 * Yes, erasing really can take minutes to complete.
 467 */
 468#define ICE_FW_ERASE_TIMEOUT 300
 469
 470/**
 471 * ice_erase_nvm_module - Erase an NVM module and await firmware completion
 472 * @pf: the PF data structure
 473 * @module: the module to erase
 474 * @component: name of the component being updated
 475 * @extack: netlink extended ACK structure
 476 *
 477 * Erase the inactive NVM bank associated with this module, and await for
 478 * a completion response message from firmware.
 479 *
 480 * Note this function assumes the caller has acquired the NVM resource.
 481 *
 482 * Returns: zero on success, or a negative error code on failure.
 483 */
 484static int
 485ice_erase_nvm_module(struct ice_pf *pf, u16 module, const char *component,
 486		     struct netlink_ext_ack *extack)
 487{
 488	u16 completion_module, completion_retval;
 489	struct device *dev = ice_pf_to_dev(pf);
 490	struct ice_aq_task task = {};
 491	struct ice_hw *hw = &pf->hw;
 492	struct libie_aq_desc *desc;
 493	struct ice_aqc_nvm *cmd;
 494	struct devlink *devlink;
 495	int err;
 496
 497	dev_dbg(dev, "Beginning erase of flash component '%s', module 0x%02x\n", component, module);
 498
 499	devlink = priv_to_devlink(pf);
 500
 501	devlink_flash_update_timeout_notify(devlink, "Erasing", component, ICE_FW_ERASE_TIMEOUT);
 502
 503	ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_erase);
 504
 505	err = ice_aq_erase_nvm(hw, module, NULL);
 506	if (err) {
 507		dev_err(dev, "Failed to erase %s (module 0x%02x), err %d aq_err %s\n",
 508			component, module, err,
 509			libie_aq_str(hw->adminq.sq_last_status));
 510		NL_SET_ERR_MSG_MOD(extack, "Failed to erase flash module");
 511		err = -EIO;
 512		goto out_notify_devlink;
 513	}
 514
 515	err = ice_aq_wait_for_event(pf, &task, ICE_FW_ERASE_TIMEOUT * HZ);
 516	if (err) {
 517		dev_err(dev, "Timed out waiting for firmware to respond with erase completion for %s (module 0x%02x), err %d\n",
 518			component, module, err);
 519		NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
 520		goto out_notify_devlink;
 521	}
 522
 523	desc = &task.event.desc;
 524	cmd = libie_aq_raw(desc);
 525	completion_module = le16_to_cpu(cmd->module_typeid);
 526	completion_retval = le16_to_cpu(desc->retval);
 527
 528	if (completion_module != module) {
 529		dev_err(dev, "Unexpected module_typeid in erase completion for %s: got 0x%x, expected 0x%x\n",
 530			component, completion_module, module);
 531		NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
 532		err = -EIO;
 533		goto out_notify_devlink;
 534	}
 535
 536	if (completion_retval) {
 537		dev_err(dev, "Firmware failed to erase %s (module 0x%02x), aq_err %s\n",
 538			component, module,
 539			libie_aq_str((enum libie_aq_err)completion_retval));
 540		NL_SET_ERR_MSG_MOD(extack, "Firmware failed to erase flash");
 541		err = -EIO;
 542		goto out_notify_devlink;
 543	}
 544
 545	dev_dbg(dev, "Completed erase of flash component '%s', module 0x%02x\n", component, module);
 546
 547out_notify_devlink:
 548	if (err)
 549		devlink_flash_update_status_notify(devlink, "Erasing failed",
 550						   component, 0, 0);
 551	else
 552		devlink_flash_update_status_notify(devlink, "Erasing done",
 553						   component, 0, 0);
 554
 555	return err;
 556}
 557
 558/**
 559 * ice_switch_flash_banks - Tell firmware to switch NVM banks
 560 * @pf: Pointer to the PF data structure
 561 * @activate_flags: flags used for the activation command
 562 * @emp_reset_available: on return, indicates if EMP reset is available
 563 * @extack: netlink extended ACK structure
 564 *
 565 * Notify firmware to activate the newly written flash banks, and wait for the
 566 * firmware response.
 567 *
 568 * Returns: zero on success or an error code on failure.
 569 */
 570static int
 571ice_switch_flash_banks(struct ice_pf *pf, u8 activate_flags,
 572		       u8 *emp_reset_available, struct netlink_ext_ack *extack)
 573{
 574	struct device *dev = ice_pf_to_dev(pf);
 575	struct ice_aq_task task = {};
 576	struct ice_hw *hw = &pf->hw;
 577	u16 completion_retval;
 578	u8 response_flags;
 579	int err;
 580
 581	ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_write_activate);
 582
 583	err = ice_nvm_write_activate(hw, activate_flags, &response_flags);
 584	if (err) {
 585		dev_err(dev, "Failed to switch active flash banks, err %d aq_err %s\n",
 586			err, libie_aq_str(hw->adminq.sq_last_status));
 587		NL_SET_ERR_MSG_MOD(extack, "Failed to switch active flash banks");
 588		return -EIO;
 589	}
 590
 591	/* Newer versions of firmware have support to indicate whether an EMP
 592	 * reset to reload firmware is available. For older firmware, EMP
 593	 * reset is always available.
 594	 */
 595	if (emp_reset_available) {
 596		if (hw->dev_caps.common_cap.reset_restrict_support) {
 597			*emp_reset_available = response_flags & ICE_AQC_NVM_EMPR_ENA;
 598			dev_dbg(dev, "Firmware indicated that EMP reset is %s\n",
 599				*emp_reset_available ?
 600				"available" : "not available");
 601		} else {
 602			*emp_reset_available = ICE_AQC_NVM_EMPR_ENA;
 603			dev_dbg(dev, "Firmware does not support restricting EMP reset availability\n");
 604		}
 605	}
 606
 607	err = ice_aq_wait_for_event(pf, &task, 30 * HZ);
 608	if (err) {
 609		dev_err(dev, "Timed out waiting for firmware to switch active flash banks, err %d\n",
 610			err);
 611		NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
 612		return err;
 613	}
 614
 615	completion_retval = le16_to_cpu(task.event.desc.retval);
 616	if (completion_retval) {
 617		dev_err(dev, "Firmware failed to switch active flash banks aq_err %s\n",
 618			libie_aq_str((enum libie_aq_err)completion_retval));
 619		NL_SET_ERR_MSG_MOD(extack, "Firmware failed to switch active flash banks");
 620		return -EIO;
 621	}
 622
 623	return 0;
 624}
 625
 626/**
 627 * ice_flash_component - Flash a component of the NVM
 628 * @context: PLDM fw update structure
 629 * @component: the component table to program
 630 *
 631 * Program the flash contents for a given component. First, determine the
 632 * module id. Then, erase the secondary bank for this module. Finally, write
 633 * the contents of the component to the NVM.
 634 *
 635 * Note this function assumes the caller has acquired the NVM resource.
 636 *
 637 * Returns: zero on success, or a negative error code on failure.
 638 */
 639static int
 640ice_flash_component(struct pldmfw *context, struct pldmfw_component *component)
 641{
 642	struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
 643	struct netlink_ext_ack *extack = priv->extack;
 644	struct ice_pf *pf = priv->pf;
 645	const char *name;
 646	u8 *reset_level;
 647	u16 module;
 648	u8 flag;
 649	int err;
 650
 651	switch (component->identifier) {
 652	case NVM_COMP_ID_OROM:
 653		module = ICE_SR_1ST_OROM_BANK_PTR;
 654		flag = ICE_AQC_NVM_ACTIV_SEL_OROM;
 655		reset_level = NULL;
 656		name = "fw.undi";
 657		break;
 658	case NVM_COMP_ID_NVM:
 659		module = ICE_SR_1ST_NVM_BANK_PTR;
 660		flag = ICE_AQC_NVM_ACTIV_SEL_NVM;
 661		reset_level = &priv->reset_level;
 662		name = "fw.mgmt";
 663		break;
 664	case NVM_COMP_ID_NETLIST:
 665		module = ICE_SR_NETLIST_BANK_PTR;
 666		flag = ICE_AQC_NVM_ACTIV_SEL_NETLIST;
 667		reset_level = NULL;
 668		name = "fw.netlist";
 669		break;
 670	default:
 671		/* This should not trigger, since we check the id before
 672		 * sending the component table to firmware.
 673		 */
 674		WARN(1, "Unexpected unknown component identifier 0x%02x",
 675		     component->identifier);
 676		return -EINVAL;
 677	}
 678
 679	/* Mark this component for activating at the end */
 680	priv->activate_flags |= flag;
 681
 682	err = ice_erase_nvm_module(pf, module, name, extack);
 683	if (err)
 684		return err;
 685
 686	return ice_write_nvm_module(pf, module, name, component->component_data,
 687				    component->component_size, reset_level,
 688				    extack);
 689}
 690
 691/**
 692 * ice_finalize_update - Perform last steps to complete device update
 693 * @context: PLDM fw update structure
 694 *
 695 * Called as the last step of the update process. Complete the update by
 696 * telling the firmware to switch active banks, and perform a reset of
 697 * configured.
 698 *
 699 * Returns: 0 on success, or an error code on failure.
 700 */
 701static int ice_finalize_update(struct pldmfw *context)
 702{
 703	struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
 704	struct netlink_ext_ack *extack = priv->extack;
 705	struct ice_pf *pf = priv->pf;
 706	struct devlink *devlink;
 707	int err;
 708
 709	/* Finally, notify firmware to activate the written NVM banks */
 710	err = ice_switch_flash_banks(pf, priv->activate_flags,
 711				     &priv->emp_reset_available, extack);
 712	if (err)
 713		return err;
 714
 715	devlink = priv_to_devlink(pf);
 716
 717	/* If the required reset is EMPR, but EMPR is disabled, report that
 718	 * a reboot is required instead.
 719	 */
 720	if (priv->reset_level == ICE_AQC_NVM_EMPR_FLAG &&
 721	    !priv->emp_reset_available) {
 722		dev_dbg(ice_pf_to_dev(pf), "Firmware indicated EMP reset as sufficient, but EMP reset is disabled\n");
 723		priv->reset_level = ICE_AQC_NVM_PERST_FLAG;
 724	}
 725
 726	switch (priv->reset_level) {
 727	case ICE_AQC_NVM_EMPR_FLAG:
 728		devlink_flash_update_status_notify(devlink,
 729						   "Activate new firmware by devlink reload",
 730						   NULL, 0, 0);
 731		break;
 732	case ICE_AQC_NVM_PERST_FLAG:
 733		devlink_flash_update_status_notify(devlink,
 734						   "Activate new firmware by rebooting the system",
 735						   NULL, 0, 0);
 736		break;
 737	case ICE_AQC_NVM_POR_FLAG:
 738	default:
 739		devlink_flash_update_status_notify(devlink,
 740						   "Activate new firmware by power cycling the system",
 741						   NULL, 0, 0);
 742		break;
 743	}
 744
 745	pf->fw_emp_reset_disabled = !priv->emp_reset_available;
 746
 747	return 0;
 748}
 749
 750struct ice_pldm_pci_record_id {
 751	u32 vendor;
 752	u32 device;
 753	u32 subsystem_vendor;
 754	u32 subsystem_device;
 755};
 756
 757/**
 758 * ice_op_pci_match_record - Check if a PCI device matches the record
 759 * @context: PLDM fw update structure
 760 * @record: list of records extracted from the PLDM image
 761 *
 762 * Determine if the PCI device associated with this device matches the record
 763 * data provided.
 764 *
 765 * Searches the descriptor TLVs and extracts the relevant descriptor data into
 766 * a pldm_pci_record_id. This is then compared against the PCI device ID
 767 * information.
 768 *
 769 * Returns: true if the device matches the record, false otherwise.
 770 */
 771static bool
 772ice_op_pci_match_record(struct pldmfw *context, struct pldmfw_record *record)
 773{
 774	struct pci_dev *pdev = to_pci_dev(context->dev);
 775	struct ice_pldm_pci_record_id id = {
 776		.vendor = PCI_ANY_ID,
 777		.device = PCI_ANY_ID,
 778		.subsystem_vendor = PCI_ANY_ID,
 779		.subsystem_device = PCI_ANY_ID,
 780	};
 781	struct pldmfw_desc_tlv *desc;
 782
 783	list_for_each_entry(desc, &record->descs, entry) {
 784		u16 value;
 785		int *ptr;
 786
 787		switch (desc->type) {
 788		case PLDM_DESC_ID_PCI_VENDOR_ID:
 789			ptr = &id.vendor;
 790			break;
 791		case PLDM_DESC_ID_PCI_DEVICE_ID:
 792			ptr = &id.device;
 793			break;
 794		case PLDM_DESC_ID_PCI_SUBVENDOR_ID:
 795			ptr = &id.subsystem_vendor;
 796			break;
 797		case PLDM_DESC_ID_PCI_SUBDEV_ID:
 798			ptr = &id.subsystem_device;
 799			break;
 800		default:
 801			/* Skip unrelated TLVs */
 802			continue;
 803		}
 804
 805		value = get_unaligned_le16(desc->data);
 806		/* A value of zero for one of the descriptors is sometimes
 807		 * used when the record should ignore this field when matching
 808		 * device. For example if the record applies to any subsystem
 809		 * device or vendor.
 810		 */
 811		if (value)
 812			*ptr = value;
 813		else
 814			*ptr = PCI_ANY_ID;
 815	}
 816
 817	/* the E822 device can have a generic device ID so check for that */
 818	if ((id.vendor == PCI_ANY_ID || id.vendor == pdev->vendor) &&
 819	    (id.device == PCI_ANY_ID || id.device == pdev->device ||
 820	    id.device == ICE_DEV_ID_E822_SI_DFLT) &&
 821	    (id.subsystem_vendor == PCI_ANY_ID ||
 822	    id.subsystem_vendor == pdev->subsystem_vendor) &&
 823	    (id.subsystem_device == PCI_ANY_ID ||
 824	    id.subsystem_device == pdev->subsystem_device))
 825		return true;
 826
 827	return false;
 828}
 829
 830static const struct pldmfw_ops ice_fwu_ops_e810 = {
 831	.match_record = &pldmfw_op_pci_match_record,
 832	.send_package_data = &ice_send_package_data,
 833	.send_component_table = &ice_send_component_table,
 834	.flash_component = &ice_flash_component,
 835	.finalize_update = &ice_finalize_update,
 836};
 837
 838static const struct pldmfw_ops ice_fwu_ops_e822 = {
 839	.match_record = &ice_op_pci_match_record,
 840	.send_package_data = &ice_send_package_data,
 841	.send_component_table = &ice_send_component_table,
 842	.flash_component = &ice_flash_component,
 843	.finalize_update = &ice_finalize_update,
 844};
 845
 846/**
 847 * ice_get_pending_updates - Check if the component has a pending update
 848 * @pf: the PF driver structure
 849 * @pending: on return, bitmap of updates pending
 850 * @extack: Netlink extended ACK
 851 *
 852 * Check if the device has any pending updates on any flash components.
 853 *
 854 * Returns: zero on success, or a negative error code on failure. Updates
 855 * pending with the bitmap of pending updates.
 856 */
 857int ice_get_pending_updates(struct ice_pf *pf, u8 *pending,
 858			    struct netlink_ext_ack *extack)
 859{
 860	struct device *dev = ice_pf_to_dev(pf);
 861	struct ice_hw_dev_caps *dev_caps;
 862	struct ice_hw *hw = &pf->hw;
 863	int err;
 864
 865	dev_caps = kzalloc(sizeof(*dev_caps), GFP_KERNEL);
 866	if (!dev_caps)
 867		return -ENOMEM;
 868
 869	/* Read the most recent device capabilities from firmware. Do not use
 870	 * the cached values in hw->dev_caps, because the pending update flag
 871	 * may have changed, e.g. if an update was previously completed and
 872	 * the system has not yet rebooted.
 873	 */
 874	err = ice_discover_dev_caps(hw, dev_caps);
 875	if (err) {
 876		NL_SET_ERR_MSG_MOD(extack, "Unable to read device capabilities");
 877		kfree(dev_caps);
 878		return err;
 879	}
 880
 881	*pending = 0;
 882
 883	if (dev_caps->common_cap.nvm_update_pending_nvm) {
 884		dev_info(dev, "The fw.mgmt flash component has a pending update\n");
 885		*pending |= ICE_AQC_NVM_ACTIV_SEL_NVM;
 886	}
 887
 888	if (dev_caps->common_cap.nvm_update_pending_orom) {
 889		dev_info(dev, "The fw.undi flash component has a pending update\n");
 890		*pending |= ICE_AQC_NVM_ACTIV_SEL_OROM;
 891	}
 892
 893	if (dev_caps->common_cap.nvm_update_pending_netlist) {
 894		dev_info(dev, "The fw.netlist flash component has a pending update\n");
 895		*pending |= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
 896	}
 897
 898	kfree(dev_caps);
 899
 900	return 0;
 901}
 902
 903/**
 904 * ice_cancel_pending_update - Cancel any pending update for a component
 905 * @pf: the PF driver structure
 906 * @component: if not NULL, the name of the component being updated
 907 * @extack: Netlink extended ACK structure
 908 *
 909 * Cancel any pending update for the specified component. If component is
 910 * NULL, all device updates will be canceled.
 911 *
 912 * Returns: zero on success, or a negative error code on failure.
 913 */
 914static int
 915ice_cancel_pending_update(struct ice_pf *pf, const char *component,
 916			  struct netlink_ext_ack *extack)
 917{
 918	struct devlink *devlink = priv_to_devlink(pf);
 919	struct device *dev = ice_pf_to_dev(pf);
 920	struct ice_hw *hw = &pf->hw;
 921	u8 pending;
 922	int err;
 923
 924	err = ice_get_pending_updates(pf, &pending, extack);
 925	if (err)
 926		return err;
 927
 928	/* If the flash_update request is for a specific component, ignore all
 929	 * of the other components.
 930	 */
 931	if (component) {
 932		if (strcmp(component, "fw.mgmt") == 0)
 933			pending &= ICE_AQC_NVM_ACTIV_SEL_NVM;
 934		else if (strcmp(component, "fw.undi") == 0)
 935			pending &= ICE_AQC_NVM_ACTIV_SEL_OROM;
 936		else if (strcmp(component, "fw.netlist") == 0)
 937			pending &= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
 938		else
 939			WARN(1, "Unexpected flash component %s", component);
 940	}
 941
 942	/* There is no previous pending update, so this request may continue */
 943	if (!pending)
 944		return 0;
 945
 946	/* In order to allow overwriting a previous pending update, notify
 947	 * firmware to cancel that update by issuing the appropriate command.
 948	 */
 949	devlink_flash_update_status_notify(devlink,
 950					   "Canceling previous pending update",
 951					   component, 0, 0);
 952
 953	err = ice_acquire_nvm(hw, ICE_RES_WRITE);
 954	if (err) {
 955		dev_err(dev, "Failed to acquire device flash lock, err %d aq_err %s\n",
 956			err, libie_aq_str(hw->adminq.sq_last_status));
 957		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
 958		return err;
 959	}
 960
 961	pending |= ICE_AQC_NVM_REVERT_LAST_ACTIV;
 962	err = ice_switch_flash_banks(pf, pending, NULL, extack);
 963
 964	ice_release_nvm(hw);
 965
 966	/* Since we've canceled the pending update, we no longer know if EMP
 967	 * reset is restricted.
 968	 */
 969	pf->fw_emp_reset_disabled = false;
 970
 971	return err;
 972}
 973
 974/**
 975 * ice_devlink_flash_update - Write a firmware image to the device
 976 * @devlink: pointer to devlink associated with the device to update
 977 * @params: devlink flash update parameters
 978 * @extack: netlink extended ACK structure
 979 *
 980 * Parse the data for a given firmware file, verifying that it is a valid PLDM
 981 * formatted image that matches this device.
 982 *
 983 * Extract the device record Package Data and Component Tables and send them
 984 * to the firmware. Extract and write the flash data for each of the three
 985 * main flash components, "fw.mgmt", "fw.undi", and "fw.netlist". Notify
 986 * firmware once the data is written to the inactive banks.
 987 *
 988 * Returns: zero on success or a negative error code on failure.
 989 */
 990int ice_devlink_flash_update(struct devlink *devlink,
 991			     struct devlink_flash_update_params *params,
 992			     struct netlink_ext_ack *extack)
 993{
 994	struct ice_pf *pf = devlink_priv(devlink);
 995	struct device *dev = ice_pf_to_dev(pf);
 996	struct ice_hw *hw = &pf->hw;
 997	struct ice_fwu_priv priv;
 998	u8 preservation;
 999	int err;
1000
1001	if (!params->overwrite_mask) {
1002		/* preserve all settings and identifiers */
1003		preservation = ICE_AQC_NVM_PRESERVE_ALL;
1004	} else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
1005		/* overwrite settings, but preserve the vital device identifiers */
1006		preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
1007	} else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
1008					      DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
1009		/* overwrite both settings and identifiers, preserve nothing */
1010		preservation = ICE_AQC_NVM_NO_PRESERVATION;
1011	} else {
1012		NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
1013		return -EOPNOTSUPP;
1014	}
1015
1016	if (!hw->dev_caps.common_cap.nvm_unified_update && !ice_is_recovery_mode(hw)) {
1017		NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
1018		return -EOPNOTSUPP;
1019	}
1020
1021	memset(&priv, 0, sizeof(priv));
1022
1023	if (params->component && strcmp(params->component, "fw.mgmt") == 0) {
1024		priv.context.mode = PLDMFW_UPDATE_MODE_SINGLE_COMPONENT;
1025		priv.context.component_identifier = NVM_COMP_ID_NVM;
1026	} else if (params->component) {
1027		return -EOPNOTSUPP;
1028	}
1029
1030	/* the E822 device needs a slightly different ops */
1031	if (hw->mac_type == ICE_MAC_GENERIC)
1032		priv.context.ops = &ice_fwu_ops_e822;
1033	else
1034		priv.context.ops = &ice_fwu_ops_e810;
1035	priv.context.dev = dev;
1036	priv.extack = extack;
1037	priv.pf = pf;
1038	priv.activate_flags = preservation;
1039
1040	devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
1041
1042	err = ice_cancel_pending_update(pf, NULL, extack);
1043	if (err)
1044		return err;
1045
1046	err = ice_acquire_nvm(hw, ICE_RES_WRITE);
1047	if (err) {
1048		dev_err(dev, "Failed to acquire device flash lock, err %d aq_err %s\n",
1049			err, libie_aq_str(hw->adminq.sq_last_status));
1050		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
1051		return err;
1052	}
1053
1054	err = pldmfw_flash_image(&priv.context, params->fw);
1055	if (err == -ENOENT) {
1056		dev_err(dev, "Firmware image has no record matching this device\n");
1057		NL_SET_ERR_MSG_MOD(extack, "Firmware image has no record matching this device");
1058	} else if (err) {
1059		/* Do not set a generic extended ACK message here. A more
1060		 * specific message may already have been set by one of our
1061		 * ops.
1062		 */
1063		dev_err(dev, "Failed to flash PLDM image, err %d", err);
1064	}
1065
1066	ice_release_nvm(hw);
1067
1068	return err;
1069}
~/ Read-only file preview.