Loading...
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_fw_update.h"
10
11struct ice_fwu_priv {
12 struct pldmfw context;
13
14 struct ice_pf *pf;
15 struct netlink_ext_ack *extack;
16
17 /* Track which NVM banks to activate at the end of the update */
18 u8 activate_flags;
19
20 /* Track the firmware response of the required reset to complete the
21 * flash update.
22 *
23 * 0 - ICE_AQC_NVM_POR_FLAG - A full power on is required
24 * 1 - ICE_AQC_NVM_PERST_FLAG - A cold PCIe reset is required
25 * 2 - ICE_AQC_NVM_EMPR_FLAG - An EMP reset is required
26 */
27 u8 reset_level;
28
29 /* Track if EMP reset is available */
30 u8 emp_reset_available;
31};
32
33/**
34 * ice_send_package_data - Send record package data to firmware
35 * @context: PLDM fw update structure
36 * @data: pointer to the package data
37 * @length: length of the package data
38 *
39 * Send a copy of the package data associated with the PLDM record matching
40 * this device to the firmware.
41 *
42 * Note that this function sends an AdminQ command that will fail unless the
43 * NVM resource has been acquired.
44 *
45 * Returns: zero on success, or a negative error code on failure.
46 */
47static int
48ice_send_package_data(struct pldmfw *context, const u8 *data, u16 length)
49{
50 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
51 struct netlink_ext_ack *extack = priv->extack;
52 struct device *dev = context->dev;
53 struct ice_pf *pf = priv->pf;
54 struct ice_hw *hw = &pf->hw;
55 u8 *package_data;
56 int status;
57
58 dev_dbg(dev, "Sending PLDM record package data to firmware\n");
59
60 package_data = kmemdup(data, length, GFP_KERNEL);
61 if (!package_data)
62 return -ENOMEM;
63
64 status = ice_nvm_set_pkg_data(hw, false, package_data, length, NULL);
65
66 kfree(package_data);
67
68 if (status) {
69 dev_err(dev, "Failed to send record package data to firmware, err %d aq_err %s\n",
70 status, ice_aq_str(hw->adminq.sq_last_status));
71 NL_SET_ERR_MSG_MOD(extack, "Failed to record package data to firmware");
72 return -EIO;
73 }
74
75 return 0;
76}
77
78/**
79 * ice_check_component_response - Report firmware response to a component
80 * @pf: device private data structure
81 * @id: component id being checked
82 * @response: indicates whether this component can be updated
83 * @code: code indicating reason for response
84 * @extack: netlink extended ACK structure
85 *
86 * Check whether firmware indicates if this component can be updated. Report
87 * a suitable error message over the netlink extended ACK if the component
88 * cannot be updated.
89 *
90 * Returns: zero if the component can be updated, or -ECANCELED of the
91 * firmware indicates the component cannot be updated.
92 */
93static int
94ice_check_component_response(struct ice_pf *pf, u16 id, u8 response, u8 code,
95 struct netlink_ext_ack *extack)
96{
97 struct device *dev = ice_pf_to_dev(pf);
98 const char *component;
99
100 switch (id) {
101 case NVM_COMP_ID_OROM:
102 component = "fw.undi";
103 break;
104 case NVM_COMP_ID_NVM:
105 component = "fw.mgmt";
106 break;
107 case NVM_COMP_ID_NETLIST:
108 component = "fw.netlist";
109 break;
110 default:
111 WARN(1, "Unexpected unknown component identifier 0x%02x", id);
112 return -EINVAL;
113 }
114
115 dev_dbg(dev, "%s: firmware response 0x%x, code 0x%x\n",
116 component, response, code);
117
118 switch (response) {
119 case ICE_AQ_NVM_PASS_COMP_CAN_BE_UPDATED:
120 /* firmware indicated this update is good to proceed */
121 return 0;
122 case ICE_AQ_NVM_PASS_COMP_CAN_MAY_BE_UPDATEABLE:
123 dev_warn(dev, "firmware recommends not updating %s, as it may result in a downgrade. continuing anyways\n", component);
124 return 0;
125 case ICE_AQ_NVM_PASS_COMP_CAN_NOT_BE_UPDATED:
126 dev_info(dev, "firmware has rejected updating %s\n", component);
127 break;
128 }
129
130 switch (code) {
131 case ICE_AQ_NVM_PASS_COMP_STAMP_IDENTICAL_CODE:
132 dev_err(dev, "Component comparison stamp for %s is identical to the running image\n",
133 component);
134 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is identical to running image");
135 break;
136 case ICE_AQ_NVM_PASS_COMP_STAMP_LOWER:
137 dev_err(dev, "Component comparison stamp for %s is lower than the running image\n",
138 component);
139 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is lower than running image");
140 break;
141 case ICE_AQ_NVM_PASS_COMP_INVALID_STAMP_CODE:
142 dev_err(dev, "Component comparison stamp for %s is invalid\n",
143 component);
144 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is invalid");
145 break;
146 case ICE_AQ_NVM_PASS_COMP_CONFLICT_CODE:
147 dev_err(dev, "%s conflicts with a previous component table\n",
148 component);
149 NL_SET_ERR_MSG_MOD(extack, "Component table conflict occurred");
150 break;
151 case ICE_AQ_NVM_PASS_COMP_PRE_REQ_NOT_MET_CODE:
152 dev_err(dev, "Pre-requisites for component %s have not been met\n",
153 component);
154 NL_SET_ERR_MSG_MOD(extack, "Component pre-requisites not met");
155 break;
156 case ICE_AQ_NVM_PASS_COMP_NOT_SUPPORTED_CODE:
157 dev_err(dev, "%s is not a supported component\n",
158 component);
159 NL_SET_ERR_MSG_MOD(extack, "Component not supported");
160 break;
161 case ICE_AQ_NVM_PASS_COMP_CANNOT_DOWNGRADE_CODE:
162 dev_err(dev, "Security restrictions prevent %s from being downgraded\n",
163 component);
164 NL_SET_ERR_MSG_MOD(extack, "Component cannot be downgraded");
165 break;
166 case ICE_AQ_NVM_PASS_COMP_INCOMPLETE_IMAGE_CODE:
167 dev_err(dev, "Received an incomplete component image for %s\n",
168 component);
169 NL_SET_ERR_MSG_MOD(extack, "Incomplete component image");
170 break;
171 case ICE_AQ_NVM_PASS_COMP_VER_STR_IDENTICAL_CODE:
172 dev_err(dev, "Component version for %s is identical to the running image\n",
173 component);
174 NL_SET_ERR_MSG_MOD(extack, "Component version is identical to running image");
175 break;
176 case ICE_AQ_NVM_PASS_COMP_VER_STR_LOWER_CODE:
177 dev_err(dev, "Component version for %s is lower than the running image\n",
178 component);
179 NL_SET_ERR_MSG_MOD(extack, "Component version is lower than the running image");
180 break;
181 default:
182 dev_err(dev, "Unexpected response code 0x02%x for %s\n",
183 code, component);
184 NL_SET_ERR_MSG_MOD(extack, "Received unexpected response code from firmware");
185 break;
186 }
187
188 return -ECANCELED;
189}
190
191/**
192 * ice_send_component_table - Send PLDM component table to firmware
193 * @context: PLDM fw update structure
194 * @component: the component to process
195 * @transfer_flag: relative transfer order of this component
196 *
197 * Read relevant data from the component and forward it to the device
198 * firmware. Check the response to determine if the firmware indicates that
199 * the update can proceed.
200 *
201 * This function sends AdminQ commands related to the NVM, and assumes that
202 * the NVM resource has been acquired.
203 *
204 * Returns: zero on success, or a negative error code on failure.
205 */
206static int
207ice_send_component_table(struct pldmfw *context, struct pldmfw_component *component,
208 u8 transfer_flag)
209{
210 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
211 struct netlink_ext_ack *extack = priv->extack;
212 struct ice_aqc_nvm_comp_tbl *comp_tbl;
213 u8 comp_response, comp_response_code;
214 struct device *dev = context->dev;
215 struct ice_pf *pf = priv->pf;
216 struct ice_hw *hw = &pf->hw;
217 size_t length;
218 int status;
219
220 switch (component->identifier) {
221 case NVM_COMP_ID_OROM:
222 case NVM_COMP_ID_NVM:
223 case NVM_COMP_ID_NETLIST:
224 break;
225 default:
226 dev_err(dev, "Unable to update due to a firmware component with unknown ID %u\n",
227 component->identifier);
228 NL_SET_ERR_MSG_MOD(extack, "Unable to update due to unknown firmware component");
229 return -EOPNOTSUPP;
230 }
231
232 length = struct_size(comp_tbl, cvs, component->version_len);
233 comp_tbl = kzalloc(length, GFP_KERNEL);
234 if (!comp_tbl)
235 return -ENOMEM;
236
237 comp_tbl->comp_class = cpu_to_le16(component->classification);
238 comp_tbl->comp_id = cpu_to_le16(component->identifier);
239 comp_tbl->comp_class_idx = FWU_COMP_CLASS_IDX_NOT_USE;
240 comp_tbl->comp_cmp_stamp = cpu_to_le32(component->comparison_stamp);
241 comp_tbl->cvs_type = component->version_type;
242 comp_tbl->cvs_len = component->version_len;
243 memcpy(comp_tbl->cvs, component->version_string, component->version_len);
244
245 dev_dbg(dev, "Sending component table to firmware:\n");
246
247 status = ice_nvm_pass_component_tbl(hw, (u8 *)comp_tbl, length,
248 transfer_flag, &comp_response,
249 &comp_response_code, NULL);
250
251 kfree(comp_tbl);
252
253 if (status) {
254 dev_err(dev, "Failed to transfer component table to firmware, err %d aq_err %s\n",
255 status, ice_aq_str(hw->adminq.sq_last_status));
256 NL_SET_ERR_MSG_MOD(extack, "Failed to transfer component table to firmware");
257 return -EIO;
258 }
259
260 return ice_check_component_response(pf, component->identifier, comp_response,
261 comp_response_code, extack);
262}
263
264/**
265 * ice_write_one_nvm_block - Write an NVM block and await completion response
266 * @pf: the PF data structure
267 * @module: the module to write to
268 * @offset: offset in bytes
269 * @block_size: size of the block to write, up to 4k
270 * @block: pointer to block of data to write
271 * @last_cmd: whether this is the last command
272 * @reset_level: storage for reset level required
273 * @extack: netlink extended ACK structure
274 *
275 * Write a block of data to a flash module, and await for the completion
276 * response message from firmware.
277 *
278 * Note this function assumes the caller has acquired the NVM resource.
279 *
280 * On successful return, reset level indicates the device reset required to
281 * complete the update.
282 *
283 * 0 - ICE_AQC_NVM_POR_FLAG - A full power on is required
284 * 1 - ICE_AQC_NVM_PERST_FLAG - A cold PCIe reset is required
285 * 2 - ICE_AQC_NVM_EMPR_FLAG - An EMP reset is required
286 *
287 * Returns: zero on success, or a negative error code on failure.
288 */
289int ice_write_one_nvm_block(struct ice_pf *pf, u16 module, u32 offset,
290 u16 block_size, u8 *block, bool last_cmd,
291 u8 *reset_level, struct netlink_ext_ack *extack)
292{
293 u16 completion_module, completion_retval;
294 struct device *dev = ice_pf_to_dev(pf);
295 struct ice_aq_task task = {};
296 struct ice_hw *hw = &pf->hw;
297 struct ice_aq_desc *desc;
298 u32 completion_offset;
299 int err;
300
301 dev_dbg(dev, "Writing block of %u bytes for module 0x%02x at offset %u\n",
302 block_size, module, offset);
303
304 ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_write);
305
306 err = ice_aq_update_nvm(hw, module, offset, block_size, block,
307 last_cmd, 0, NULL);
308 if (err) {
309 dev_err(dev, "Failed to flash module 0x%02x with block of size %u at offset %u, err %d aq_err %s\n",
310 module, block_size, offset, err,
311 ice_aq_str(hw->adminq.sq_last_status));
312 NL_SET_ERR_MSG_MOD(extack, "Failed to program flash module");
313 return -EIO;
314 }
315
316 /* In most cases, firmware reports a write completion within a few
317 * milliseconds. However, it has been observed that a completion might
318 * take more than a second to complete in some cases. The timeout here
319 * is conservative and is intended to prevent failure to update when
320 * firmware is slow to respond.
321 */
322 err = ice_aq_wait_for_event(pf, &task, 15 * HZ);
323 if (err) {
324 dev_err(dev, "Timed out while trying to flash module 0x%02x with block of size %u at offset %u, err %d\n",
325 module, block_size, offset, err);
326 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
327 return -EIO;
328 }
329
330 desc = &task.event.desc;
331 completion_module = le16_to_cpu(desc->params.nvm.module_typeid);
332 completion_retval = le16_to_cpu(desc->retval);
333
334 completion_offset = le16_to_cpu(desc->params.nvm.offset_low);
335 completion_offset |= desc->params.nvm.offset_high << 16;
336
337 if (completion_module != module) {
338 dev_err(dev, "Unexpected module_typeid in write completion: got 0x%x, expected 0x%x\n",
339 completion_module, module);
340 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
341 return -EIO;
342 }
343
344 if (completion_offset != offset) {
345 dev_err(dev, "Unexpected offset in write completion: got %u, expected %u\n",
346 completion_offset, offset);
347 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
348 return -EIO;
349 }
350
351 if (completion_retval) {
352 dev_err(dev, "Firmware failed to flash module 0x%02x with block of size %u at offset %u, err %s\n",
353 module, block_size, offset,
354 ice_aq_str((enum ice_aq_err)completion_retval));
355 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to program flash module");
356 return -EIO;
357 }
358
359 /* For the last command to write the NVM bank, newer versions of
360 * firmware indicate the required level of reset to complete
361 * activation of firmware. If the firmware supports this, cache the
362 * response for indicating to the user later. Otherwise, assume that
363 * a full power cycle is required.
364 */
365 if (reset_level && last_cmd && module == ICE_SR_1ST_NVM_BANK_PTR) {
366 if (hw->dev_caps.common_cap.pcie_reset_avoidance) {
367 *reset_level = desc->params.nvm.cmd_flags &
368 ICE_AQC_NVM_RESET_LVL_M;
369 dev_dbg(dev, "Firmware reported required reset level as %u\n",
370 *reset_level);
371 } else {
372 *reset_level = ICE_AQC_NVM_POR_FLAG;
373 dev_dbg(dev, "Firmware doesn't support indicating required reset level. Assuming a power cycle is required\n");
374 }
375 }
376
377 return 0;
378}
379
380/**
381 * ice_write_nvm_module - Write data to an NVM module
382 * @pf: the PF driver structure
383 * @module: the module id to program
384 * @component: the name of the component being updated
385 * @image: buffer of image data to write to the NVM
386 * @length: length of the buffer
387 * @reset_level: storage for reset level required
388 * @extack: netlink extended ACK structure
389 *
390 * Loop over the data for a given NVM module and program it in 4 Kb
391 * blocks. Notify devlink core of progress after each block is programmed.
392 * Loops over a block of data and programs the NVM in 4k block chunks.
393 *
394 * Note this function assumes the caller has acquired the NVM resource.
395 *
396 * Returns: zero on success, or a negative error code on failure.
397 */
398static int
399ice_write_nvm_module(struct ice_pf *pf, u16 module, const char *component,
400 const u8 *image, u32 length, u8 *reset_level,
401 struct netlink_ext_ack *extack)
402{
403 struct device *dev = ice_pf_to_dev(pf);
404 struct devlink *devlink;
405 u32 offset = 0;
406 bool last_cmd;
407 u8 *block;
408 int err;
409
410 dev_dbg(dev, "Beginning write of flash component '%s', module 0x%02x\n", component, module);
411
412 devlink = priv_to_devlink(pf);
413
414 devlink_flash_update_status_notify(devlink, "Flashing",
415 component, 0, length);
416
417 block = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
418 if (!block)
419 return -ENOMEM;
420
421 do {
422 u32 block_size;
423
424 block_size = min_t(u32, ICE_AQ_MAX_BUF_LEN, length - offset);
425 last_cmd = !(offset + block_size < length);
426
427 /* ice_aq_update_nvm may copy the firmware response into the
428 * buffer, so we must make a copy since the source data is
429 * constant.
430 */
431 memcpy(block, image + offset, block_size);
432
433 err = ice_write_one_nvm_block(pf, module, offset, block_size,
434 block, last_cmd, reset_level,
435 extack);
436 if (err)
437 break;
438
439 offset += block_size;
440
441 devlink_flash_update_status_notify(devlink, "Flashing",
442 component, offset, length);
443 } while (!last_cmd);
444
445 dev_dbg(dev, "Completed write of flash component '%s', module 0x%02x\n", component, module);
446
447 if (err)
448 devlink_flash_update_status_notify(devlink, "Flashing failed",
449 component, length, length);
450 else
451 devlink_flash_update_status_notify(devlink, "Flashing done",
452 component, length, length);
453
454 kfree(block);
455 return err;
456}
457
458/* Length in seconds to wait before timing out when erasing a flash module.
459 * Yes, erasing really can take minutes to complete.
460 */
461#define ICE_FW_ERASE_TIMEOUT 300
462
463/**
464 * ice_erase_nvm_module - Erase an NVM module and await firmware completion
465 * @pf: the PF data structure
466 * @module: the module to erase
467 * @component: name of the component being updated
468 * @extack: netlink extended ACK structure
469 *
470 * Erase the inactive NVM bank associated with this module, and await for
471 * a completion response message from firmware.
472 *
473 * Note this function assumes the caller has acquired the NVM resource.
474 *
475 * Returns: zero on success, or a negative error code on failure.
476 */
477static int
478ice_erase_nvm_module(struct ice_pf *pf, u16 module, const char *component,
479 struct netlink_ext_ack *extack)
480{
481 u16 completion_module, completion_retval;
482 struct device *dev = ice_pf_to_dev(pf);
483 struct ice_aq_task task = {};
484 struct ice_hw *hw = &pf->hw;
485 struct ice_aq_desc *desc;
486 struct devlink *devlink;
487 int err;
488
489 dev_dbg(dev, "Beginning erase of flash component '%s', module 0x%02x\n", component, module);
490
491 devlink = priv_to_devlink(pf);
492
493 devlink_flash_update_timeout_notify(devlink, "Erasing", component, ICE_FW_ERASE_TIMEOUT);
494
495 ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_erase);
496
497 err = ice_aq_erase_nvm(hw, module, NULL);
498 if (err) {
499 dev_err(dev, "Failed to erase %s (module 0x%02x), err %d aq_err %s\n",
500 component, module, err,
501 ice_aq_str(hw->adminq.sq_last_status));
502 NL_SET_ERR_MSG_MOD(extack, "Failed to erase flash module");
503 err = -EIO;
504 goto out_notify_devlink;
505 }
506
507 err = ice_aq_wait_for_event(pf, &task, ICE_FW_ERASE_TIMEOUT * HZ);
508 if (err) {
509 dev_err(dev, "Timed out waiting for firmware to respond with erase completion for %s (module 0x%02x), err %d\n",
510 component, module, err);
511 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
512 goto out_notify_devlink;
513 }
514
515 desc = &task.event.desc;
516 completion_module = le16_to_cpu(desc->params.nvm.module_typeid);
517 completion_retval = le16_to_cpu(desc->retval);
518
519 if (completion_module != module) {
520 dev_err(dev, "Unexpected module_typeid in erase completion for %s: got 0x%x, expected 0x%x\n",
521 component, completion_module, module);
522 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
523 err = -EIO;
524 goto out_notify_devlink;
525 }
526
527 if (completion_retval) {
528 dev_err(dev, "Firmware failed to erase %s (module 0x02%x), aq_err %s\n",
529 component, module,
530 ice_aq_str((enum ice_aq_err)completion_retval));
531 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to erase flash");
532 err = -EIO;
533 goto out_notify_devlink;
534 }
535
536 dev_dbg(dev, "Completed erase of flash component '%s', module 0x%02x\n", component, module);
537
538out_notify_devlink:
539 if (err)
540 devlink_flash_update_status_notify(devlink, "Erasing failed",
541 component, 0, 0);
542 else
543 devlink_flash_update_status_notify(devlink, "Erasing done",
544 component, 0, 0);
545
546 return err;
547}
548
549/**
550 * ice_switch_flash_banks - Tell firmware to switch NVM banks
551 * @pf: Pointer to the PF data structure
552 * @activate_flags: flags used for the activation command
553 * @emp_reset_available: on return, indicates if EMP reset is available
554 * @extack: netlink extended ACK structure
555 *
556 * Notify firmware to activate the newly written flash banks, and wait for the
557 * firmware response.
558 *
559 * Returns: zero on success or an error code on failure.
560 */
561static int
562ice_switch_flash_banks(struct ice_pf *pf, u8 activate_flags,
563 u8 *emp_reset_available, struct netlink_ext_ack *extack)
564{
565 struct device *dev = ice_pf_to_dev(pf);
566 struct ice_aq_task task = {};
567 struct ice_hw *hw = &pf->hw;
568 u16 completion_retval;
569 u8 response_flags;
570 int err;
571
572 ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_write_activate);
573
574 err = ice_nvm_write_activate(hw, activate_flags, &response_flags);
575 if (err) {
576 dev_err(dev, "Failed to switch active flash banks, err %d aq_err %s\n",
577 err, ice_aq_str(hw->adminq.sq_last_status));
578 NL_SET_ERR_MSG_MOD(extack, "Failed to switch active flash banks");
579 return -EIO;
580 }
581
582 /* Newer versions of firmware have support to indicate whether an EMP
583 * reset to reload firmware is available. For older firmware, EMP
584 * reset is always available.
585 */
586 if (emp_reset_available) {
587 if (hw->dev_caps.common_cap.reset_restrict_support) {
588 *emp_reset_available = response_flags & ICE_AQC_NVM_EMPR_ENA;
589 dev_dbg(dev, "Firmware indicated that EMP reset is %s\n",
590 *emp_reset_available ?
591 "available" : "not available");
592 } else {
593 *emp_reset_available = ICE_AQC_NVM_EMPR_ENA;
594 dev_dbg(dev, "Firmware does not support restricting EMP reset availability\n");
595 }
596 }
597
598 err = ice_aq_wait_for_event(pf, &task, 30 * HZ);
599 if (err) {
600 dev_err(dev, "Timed out waiting for firmware to switch active flash banks, err %d\n",
601 err);
602 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
603 return err;
604 }
605
606 completion_retval = le16_to_cpu(task.event.desc.retval);
607 if (completion_retval) {
608 dev_err(dev, "Firmware failed to switch active flash banks aq_err %s\n",
609 ice_aq_str((enum ice_aq_err)completion_retval));
610 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to switch active flash banks");
611 return -EIO;
612 }
613
614 return 0;
615}
616
617/**
618 * ice_flash_component - Flash a component of the NVM
619 * @context: PLDM fw update structure
620 * @component: the component table to program
621 *
622 * Program the flash contents for a given component. First, determine the
623 * module id. Then, erase the secondary bank for this module. Finally, write
624 * the contents of the component to the NVM.
625 *
626 * Note this function assumes the caller has acquired the NVM resource.
627 *
628 * Returns: zero on success, or a negative error code on failure.
629 */
630static int
631ice_flash_component(struct pldmfw *context, struct pldmfw_component *component)
632{
633 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
634 struct netlink_ext_ack *extack = priv->extack;
635 struct ice_pf *pf = priv->pf;
636 const char *name;
637 u8 *reset_level;
638 u16 module;
639 u8 flag;
640 int err;
641
642 switch (component->identifier) {
643 case NVM_COMP_ID_OROM:
644 module = ICE_SR_1ST_OROM_BANK_PTR;
645 flag = ICE_AQC_NVM_ACTIV_SEL_OROM;
646 reset_level = NULL;
647 name = "fw.undi";
648 break;
649 case NVM_COMP_ID_NVM:
650 module = ICE_SR_1ST_NVM_BANK_PTR;
651 flag = ICE_AQC_NVM_ACTIV_SEL_NVM;
652 reset_level = &priv->reset_level;
653 name = "fw.mgmt";
654 break;
655 case NVM_COMP_ID_NETLIST:
656 module = ICE_SR_NETLIST_BANK_PTR;
657 flag = ICE_AQC_NVM_ACTIV_SEL_NETLIST;
658 reset_level = NULL;
659 name = "fw.netlist";
660 break;
661 default:
662 /* This should not trigger, since we check the id before
663 * sending the component table to firmware.
664 */
665 WARN(1, "Unexpected unknown component identifier 0x%02x",
666 component->identifier);
667 return -EINVAL;
668 }
669
670 /* Mark this component for activating at the end */
671 priv->activate_flags |= flag;
672
673 err = ice_erase_nvm_module(pf, module, name, extack);
674 if (err)
675 return err;
676
677 return ice_write_nvm_module(pf, module, name, component->component_data,
678 component->component_size, reset_level,
679 extack);
680}
681
682/**
683 * ice_finalize_update - Perform last steps to complete device update
684 * @context: PLDM fw update structure
685 *
686 * Called as the last step of the update process. Complete the update by
687 * telling the firmware to switch active banks, and perform a reset of
688 * configured.
689 *
690 * Returns: 0 on success, or an error code on failure.
691 */
692static int ice_finalize_update(struct pldmfw *context)
693{
694 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
695 struct netlink_ext_ack *extack = priv->extack;
696 struct ice_pf *pf = priv->pf;
697 struct devlink *devlink;
698 int err;
699
700 /* Finally, notify firmware to activate the written NVM banks */
701 err = ice_switch_flash_banks(pf, priv->activate_flags,
702 &priv->emp_reset_available, extack);
703 if (err)
704 return err;
705
706 devlink = priv_to_devlink(pf);
707
708 /* If the required reset is EMPR, but EMPR is disabled, report that
709 * a reboot is required instead.
710 */
711 if (priv->reset_level == ICE_AQC_NVM_EMPR_FLAG &&
712 !priv->emp_reset_available) {
713 dev_dbg(ice_pf_to_dev(pf), "Firmware indicated EMP reset as sufficient, but EMP reset is disabled\n");
714 priv->reset_level = ICE_AQC_NVM_PERST_FLAG;
715 }
716
717 switch (priv->reset_level) {
718 case ICE_AQC_NVM_EMPR_FLAG:
719 devlink_flash_update_status_notify(devlink,
720 "Activate new firmware by devlink reload",
721 NULL, 0, 0);
722 break;
723 case ICE_AQC_NVM_PERST_FLAG:
724 devlink_flash_update_status_notify(devlink,
725 "Activate new firmware by rebooting the system",
726 NULL, 0, 0);
727 break;
728 case ICE_AQC_NVM_POR_FLAG:
729 default:
730 devlink_flash_update_status_notify(devlink,
731 "Activate new firmware by power cycling the system",
732 NULL, 0, 0);
733 break;
734 }
735
736 pf->fw_emp_reset_disabled = !priv->emp_reset_available;
737
738 return 0;
739}
740
741struct ice_pldm_pci_record_id {
742 u32 vendor;
743 u32 device;
744 u32 subsystem_vendor;
745 u32 subsystem_device;
746};
747
748/**
749 * ice_op_pci_match_record - Check if a PCI device matches the record
750 * @context: PLDM fw update structure
751 * @record: list of records extracted from the PLDM image
752 *
753 * Determine if the PCI device associated with this device matches the record
754 * data provided.
755 *
756 * Searches the descriptor TLVs and extracts the relevant descriptor data into
757 * a pldm_pci_record_id. This is then compared against the PCI device ID
758 * information.
759 *
760 * Returns: true if the device matches the record, false otherwise.
761 */
762static bool
763ice_op_pci_match_record(struct pldmfw *context, struct pldmfw_record *record)
764{
765 struct pci_dev *pdev = to_pci_dev(context->dev);
766 struct ice_pldm_pci_record_id id = {
767 .vendor = PCI_ANY_ID,
768 .device = PCI_ANY_ID,
769 .subsystem_vendor = PCI_ANY_ID,
770 .subsystem_device = PCI_ANY_ID,
771 };
772 struct pldmfw_desc_tlv *desc;
773
774 list_for_each_entry(desc, &record->descs, entry) {
775 u16 value;
776 int *ptr;
777
778 switch (desc->type) {
779 case PLDM_DESC_ID_PCI_VENDOR_ID:
780 ptr = &id.vendor;
781 break;
782 case PLDM_DESC_ID_PCI_DEVICE_ID:
783 ptr = &id.device;
784 break;
785 case PLDM_DESC_ID_PCI_SUBVENDOR_ID:
786 ptr = &id.subsystem_vendor;
787 break;
788 case PLDM_DESC_ID_PCI_SUBDEV_ID:
789 ptr = &id.subsystem_device;
790 break;
791 default:
792 /* Skip unrelated TLVs */
793 continue;
794 }
795
796 value = get_unaligned_le16(desc->data);
797 /* A value of zero for one of the descriptors is sometimes
798 * used when the record should ignore this field when matching
799 * device. For example if the record applies to any subsystem
800 * device or vendor.
801 */
802 if (value)
803 *ptr = value;
804 else
805 *ptr = PCI_ANY_ID;
806 }
807
808 /* the E822 device can have a generic device ID so check for that */
809 if ((id.vendor == PCI_ANY_ID || id.vendor == pdev->vendor) &&
810 (id.device == PCI_ANY_ID || id.device == pdev->device ||
811 id.device == ICE_DEV_ID_E822_SI_DFLT) &&
812 (id.subsystem_vendor == PCI_ANY_ID ||
813 id.subsystem_vendor == pdev->subsystem_vendor) &&
814 (id.subsystem_device == PCI_ANY_ID ||
815 id.subsystem_device == pdev->subsystem_device))
816 return true;
817
818 return false;
819}
820
821static const struct pldmfw_ops ice_fwu_ops_e810 = {
822 .match_record = &pldmfw_op_pci_match_record,
823 .send_package_data = &ice_send_package_data,
824 .send_component_table = &ice_send_component_table,
825 .flash_component = &ice_flash_component,
826 .finalize_update = &ice_finalize_update,
827};
828
829static const struct pldmfw_ops ice_fwu_ops_e822 = {
830 .match_record = &ice_op_pci_match_record,
831 .send_package_data = &ice_send_package_data,
832 .send_component_table = &ice_send_component_table,
833 .flash_component = &ice_flash_component,
834 .finalize_update = &ice_finalize_update,
835};
836
837/**
838 * ice_get_pending_updates - Check if the component has a pending update
839 * @pf: the PF driver structure
840 * @pending: on return, bitmap of updates pending
841 * @extack: Netlink extended ACK
842 *
843 * Check if the device has any pending updates on any flash components.
844 *
845 * Returns: zero on success, or a negative error code on failure. Updates
846 * pending with the bitmap of pending updates.
847 */
848int ice_get_pending_updates(struct ice_pf *pf, u8 *pending,
849 struct netlink_ext_ack *extack)
850{
851 struct device *dev = ice_pf_to_dev(pf);
852 struct ice_hw_dev_caps *dev_caps;
853 struct ice_hw *hw = &pf->hw;
854 int err;
855
856 dev_caps = kzalloc(sizeof(*dev_caps), GFP_KERNEL);
857 if (!dev_caps)
858 return -ENOMEM;
859
860 /* Read the most recent device capabilities from firmware. Do not use
861 * the cached values in hw->dev_caps, because the pending update flag
862 * may have changed, e.g. if an update was previously completed and
863 * the system has not yet rebooted.
864 */
865 err = ice_discover_dev_caps(hw, dev_caps);
866 if (err) {
867 NL_SET_ERR_MSG_MOD(extack, "Unable to read device capabilities");
868 kfree(dev_caps);
869 return err;
870 }
871
872 *pending = 0;
873
874 if (dev_caps->common_cap.nvm_update_pending_nvm) {
875 dev_info(dev, "The fw.mgmt flash component has a pending update\n");
876 *pending |= ICE_AQC_NVM_ACTIV_SEL_NVM;
877 }
878
879 if (dev_caps->common_cap.nvm_update_pending_orom) {
880 dev_info(dev, "The fw.undi flash component has a pending update\n");
881 *pending |= ICE_AQC_NVM_ACTIV_SEL_OROM;
882 }
883
884 if (dev_caps->common_cap.nvm_update_pending_netlist) {
885 dev_info(dev, "The fw.netlist flash component has a pending update\n");
886 *pending |= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
887 }
888
889 kfree(dev_caps);
890
891 return 0;
892}
893
894/**
895 * ice_cancel_pending_update - Cancel any pending update for a component
896 * @pf: the PF driver structure
897 * @component: if not NULL, the name of the component being updated
898 * @extack: Netlink extended ACK structure
899 *
900 * Cancel any pending update for the specified component. If component is
901 * NULL, all device updates will be canceled.
902 *
903 * Returns: zero on success, or a negative error code on failure.
904 */
905static int
906ice_cancel_pending_update(struct ice_pf *pf, const char *component,
907 struct netlink_ext_ack *extack)
908{
909 struct devlink *devlink = priv_to_devlink(pf);
910 struct device *dev = ice_pf_to_dev(pf);
911 struct ice_hw *hw = &pf->hw;
912 u8 pending;
913 int err;
914
915 err = ice_get_pending_updates(pf, &pending, extack);
916 if (err)
917 return err;
918
919 /* If the flash_update request is for a specific component, ignore all
920 * of the other components.
921 */
922 if (component) {
923 if (strcmp(component, "fw.mgmt") == 0)
924 pending &= ICE_AQC_NVM_ACTIV_SEL_NVM;
925 else if (strcmp(component, "fw.undi") == 0)
926 pending &= ICE_AQC_NVM_ACTIV_SEL_OROM;
927 else if (strcmp(component, "fw.netlist") == 0)
928 pending &= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
929 else
930 WARN(1, "Unexpected flash component %s", component);
931 }
932
933 /* There is no previous pending update, so this request may continue */
934 if (!pending)
935 return 0;
936
937 /* In order to allow overwriting a previous pending update, notify
938 * firmware to cancel that update by issuing the appropriate command.
939 */
940 devlink_flash_update_status_notify(devlink,
941 "Canceling previous pending update",
942 component, 0, 0);
943
944 err = ice_acquire_nvm(hw, ICE_RES_WRITE);
945 if (err) {
946 dev_err(dev, "Failed to acquire device flash lock, err %d aq_err %s\n",
947 err, ice_aq_str(hw->adminq.sq_last_status));
948 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
949 return err;
950 }
951
952 pending |= ICE_AQC_NVM_REVERT_LAST_ACTIV;
953 err = ice_switch_flash_banks(pf, pending, NULL, extack);
954
955 ice_release_nvm(hw);
956
957 /* Since we've canceled the pending update, we no longer know if EMP
958 * reset is restricted.
959 */
960 pf->fw_emp_reset_disabled = false;
961
962 return err;
963}
964
965/**
966 * ice_devlink_flash_update - Write a firmware image to the device
967 * @devlink: pointer to devlink associated with the device to update
968 * @params: devlink flash update parameters
969 * @extack: netlink extended ACK structure
970 *
971 * Parse the data for a given firmware file, verifying that it is a valid PLDM
972 * formatted image that matches this device.
973 *
974 * Extract the device record Package Data and Component Tables and send them
975 * to the firmware. Extract and write the flash data for each of the three
976 * main flash components, "fw.mgmt", "fw.undi", and "fw.netlist". Notify
977 * firmware once the data is written to the inactive banks.
978 *
979 * Returns: zero on success or a negative error code on failure.
980 */
981int ice_devlink_flash_update(struct devlink *devlink,
982 struct devlink_flash_update_params *params,
983 struct netlink_ext_ack *extack)
984{
985 struct ice_pf *pf = devlink_priv(devlink);
986 struct device *dev = ice_pf_to_dev(pf);
987 struct ice_hw *hw = &pf->hw;
988 struct ice_fwu_priv priv;
989 u8 preservation;
990 int err;
991
992 if (!params->overwrite_mask) {
993 /* preserve all settings and identifiers */
994 preservation = ICE_AQC_NVM_PRESERVE_ALL;
995 } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
996 /* overwrite settings, but preserve the vital device identifiers */
997 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
998 } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
999 DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
1000 /* overwrite both settings and identifiers, preserve nothing */
1001 preservation = ICE_AQC_NVM_NO_PRESERVATION;
1002 } else {
1003 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
1004 return -EOPNOTSUPP;
1005 }
1006
1007 if (!hw->dev_caps.common_cap.nvm_unified_update) {
1008 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
1009 return -EOPNOTSUPP;
1010 }
1011
1012 memset(&priv, 0, sizeof(priv));
1013
1014 /* the E822 device needs a slightly different ops */
1015 if (hw->mac_type == ICE_MAC_GENERIC)
1016 priv.context.ops = &ice_fwu_ops_e822;
1017 else
1018 priv.context.ops = &ice_fwu_ops_e810;
1019 priv.context.dev = dev;
1020 priv.extack = extack;
1021 priv.pf = pf;
1022 priv.activate_flags = preservation;
1023
1024 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
1025
1026 err = ice_cancel_pending_update(pf, NULL, extack);
1027 if (err)
1028 return err;
1029
1030 err = ice_acquire_nvm(hw, ICE_RES_WRITE);
1031 if (err) {
1032 dev_err(dev, "Failed to acquire device flash lock, err %d aq_err %s\n",
1033 err, ice_aq_str(hw->adminq.sq_last_status));
1034 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
1035 return err;
1036 }
1037
1038 err = pldmfw_flash_image(&priv.context, params->fw);
1039 if (err == -ENOENT) {
1040 dev_err(dev, "Firmware image has no record matching this device\n");
1041 NL_SET_ERR_MSG_MOD(extack, "Firmware image has no record matching this device");
1042 } else if (err) {
1043 /* Do not set a generic extended ACK message here. A more
1044 * specific message may already have been set by one of our
1045 * ops.
1046 */
1047 dev_err(dev, "Failed to flash PLDM image, err %d", err);
1048 }
1049
1050 ice_release_nvm(hw);
1051
1052 return err;
1053}
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2018-2019, Intel Corporation. */
3
4#include <asm/unaligned.h>
5#include <linux/uuid.h>
6#include <linux/crc32.h>
7#include <linux/pldmfw.h>
8#include "ice.h"
9#include "ice_fw_update.h"
10
11struct ice_fwu_priv {
12 struct pldmfw context;
13
14 struct ice_pf *pf;
15 struct netlink_ext_ack *extack;
16
17 /* Track which NVM banks to activate at the end of the update */
18 u8 activate_flags;
19};
20
21/**
22 * ice_send_package_data - Send record package data to firmware
23 * @context: PLDM fw update structure
24 * @data: pointer to the package data
25 * @length: length of the package data
26 *
27 * Send a copy of the package data associated with the PLDM record matching
28 * this device to the firmware.
29 *
30 * Note that this function sends an AdminQ command that will fail unless the
31 * NVM resource has been acquired.
32 *
33 * Returns: zero on success, or a negative error code on failure.
34 */
35static int
36ice_send_package_data(struct pldmfw *context, const u8 *data, u16 length)
37{
38 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
39 struct netlink_ext_ack *extack = priv->extack;
40 struct device *dev = context->dev;
41 struct ice_pf *pf = priv->pf;
42 struct ice_hw *hw = &pf->hw;
43 enum ice_status status;
44 u8 *package_data;
45
46 package_data = kmemdup(data, length, GFP_KERNEL);
47 if (!package_data)
48 return -ENOMEM;
49
50 status = ice_nvm_set_pkg_data(hw, false, package_data, length, NULL);
51
52 kfree(package_data);
53
54 if (status) {
55 dev_err(dev, "Failed to send record package data to firmware, err %s aq_err %s\n",
56 ice_stat_str(status),
57 ice_aq_str(hw->adminq.sq_last_status));
58 NL_SET_ERR_MSG_MOD(extack, "Failed to record package data to firmware");
59 return -EIO;
60 }
61
62 return 0;
63}
64
65/**
66 * ice_check_component_response - Report firmware response to a component
67 * @pf: device private data structure
68 * @id: component id being checked
69 * @response: indicates whether this component can be updated
70 * @code: code indicating reason for response
71 * @extack: netlink extended ACK structure
72 *
73 * Check whether firmware indicates if this component can be updated. Report
74 * a suitable error message over the netlink extended ACK if the component
75 * cannot be updated.
76 *
77 * Returns: zero if the component can be updated, or -ECANCELED of the
78 * firmware indicates the component cannot be updated.
79 */
80static int
81ice_check_component_response(struct ice_pf *pf, u16 id, u8 response, u8 code,
82 struct netlink_ext_ack *extack)
83{
84 struct device *dev = ice_pf_to_dev(pf);
85 const char *component;
86
87 switch (id) {
88 case NVM_COMP_ID_OROM:
89 component = "fw.undi";
90 break;
91 case NVM_COMP_ID_NVM:
92 component = "fw.mgmt";
93 break;
94 case NVM_COMP_ID_NETLIST:
95 component = "fw.netlist";
96 break;
97 default:
98 WARN(1, "Unexpected unknown component identifier 0x%02x", id);
99 return -EINVAL;
100 }
101
102 dev_dbg(dev, "%s: firmware response 0x%x, code 0x%x\n",
103 component, response, code);
104
105 switch (response) {
106 case ICE_AQ_NVM_PASS_COMP_CAN_BE_UPDATED:
107 /* firmware indicated this update is good to proceed */
108 return 0;
109 case ICE_AQ_NVM_PASS_COMP_CAN_MAY_BE_UPDATEABLE:
110 dev_warn(dev, "firmware recommends not updating %s, as it may result in a downgrade. continuing anyways\n", component);
111 return 0;
112 case ICE_AQ_NVM_PASS_COMP_CAN_NOT_BE_UPDATED:
113 dev_info(dev, "firmware has rejected updating %s\n", component);
114 break;
115 }
116
117 switch (code) {
118 case ICE_AQ_NVM_PASS_COMP_STAMP_IDENTICAL_CODE:
119 dev_err(dev, "Component comparison stamp for %s is identical to the running image\n",
120 component);
121 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is identical to running image");
122 break;
123 case ICE_AQ_NVM_PASS_COMP_STAMP_LOWER:
124 dev_err(dev, "Component comparison stamp for %s is lower than the running image\n",
125 component);
126 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is lower than running image");
127 break;
128 case ICE_AQ_NVM_PASS_COMP_INVALID_STAMP_CODE:
129 dev_err(dev, "Component comparison stamp for %s is invalid\n",
130 component);
131 NL_SET_ERR_MSG_MOD(extack, "Component comparison stamp is invalid");
132 break;
133 case ICE_AQ_NVM_PASS_COMP_CONFLICT_CODE:
134 dev_err(dev, "%s conflicts with a previous component table\n",
135 component);
136 NL_SET_ERR_MSG_MOD(extack, "Component table conflict occurred");
137 break;
138 case ICE_AQ_NVM_PASS_COMP_PRE_REQ_NOT_MET_CODE:
139 dev_err(dev, "Pre-requisites for component %s have not been met\n",
140 component);
141 NL_SET_ERR_MSG_MOD(extack, "Component pre-requisites not met");
142 break;
143 case ICE_AQ_NVM_PASS_COMP_NOT_SUPPORTED_CODE:
144 dev_err(dev, "%s is not a supported component\n",
145 component);
146 NL_SET_ERR_MSG_MOD(extack, "Component not supported");
147 break;
148 case ICE_AQ_NVM_PASS_COMP_CANNOT_DOWNGRADE_CODE:
149 dev_err(dev, "Security restrictions prevent %s from being downgraded\n",
150 component);
151 NL_SET_ERR_MSG_MOD(extack, "Component cannot be downgraded");
152 break;
153 case ICE_AQ_NVM_PASS_COMP_INCOMPLETE_IMAGE_CODE:
154 dev_err(dev, "Received an incomplete component image for %s\n",
155 component);
156 NL_SET_ERR_MSG_MOD(extack, "Incomplete component image");
157 break;
158 case ICE_AQ_NVM_PASS_COMP_VER_STR_IDENTICAL_CODE:
159 dev_err(dev, "Component version for %s is identical to the running image\n",
160 component);
161 NL_SET_ERR_MSG_MOD(extack, "Component version is identical to running image");
162 break;
163 case ICE_AQ_NVM_PASS_COMP_VER_STR_LOWER_CODE:
164 dev_err(dev, "Component version for %s is lower than the running image\n",
165 component);
166 NL_SET_ERR_MSG_MOD(extack, "Component version is lower than the running image");
167 break;
168 default:
169 dev_err(dev, "Unexpected response code 0x02%x for %s\n",
170 code, component);
171 NL_SET_ERR_MSG_MOD(extack, "Received unexpected response code from firmware");
172 break;
173 }
174
175 return -ECANCELED;
176}
177
178/**
179 * ice_send_component_table - Send PLDM component table to firmware
180 * @context: PLDM fw update structure
181 * @component: the component to process
182 * @transfer_flag: relative transfer order of this component
183 *
184 * Read relevant data from the component and forward it to the device
185 * firmware. Check the response to determine if the firmware indicates that
186 * the update can proceed.
187 *
188 * This function sends AdminQ commands related to the NVM, and assumes that
189 * the NVM resource has been acquired.
190 *
191 * Returns: zero on success, or a negative error code on failure.
192 */
193static int
194ice_send_component_table(struct pldmfw *context, struct pldmfw_component *component,
195 u8 transfer_flag)
196{
197 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
198 struct netlink_ext_ack *extack = priv->extack;
199 struct ice_aqc_nvm_comp_tbl *comp_tbl;
200 u8 comp_response, comp_response_code;
201 struct device *dev = context->dev;
202 struct ice_pf *pf = priv->pf;
203 struct ice_hw *hw = &pf->hw;
204 enum ice_status status;
205 size_t length;
206
207 switch (component->identifier) {
208 case NVM_COMP_ID_OROM:
209 case NVM_COMP_ID_NVM:
210 case NVM_COMP_ID_NETLIST:
211 break;
212 default:
213 dev_err(dev, "Unable to update due to a firmware component with unknown ID %u\n",
214 component->identifier);
215 NL_SET_ERR_MSG_MOD(extack, "Unable to update due to unknown firmware component");
216 return -EOPNOTSUPP;
217 }
218
219 length = struct_size(comp_tbl, cvs, component->version_len);
220 comp_tbl = kzalloc(length, GFP_KERNEL);
221 if (!comp_tbl)
222 return -ENOMEM;
223
224 comp_tbl->comp_class = cpu_to_le16(component->classification);
225 comp_tbl->comp_id = cpu_to_le16(component->identifier);
226 comp_tbl->comp_class_idx = FWU_COMP_CLASS_IDX_NOT_USE;
227 comp_tbl->comp_cmp_stamp = cpu_to_le32(component->comparison_stamp);
228 comp_tbl->cvs_type = component->version_type;
229 comp_tbl->cvs_len = component->version_len;
230 memcpy(comp_tbl->cvs, component->version_string, component->version_len);
231
232 status = ice_nvm_pass_component_tbl(hw, (u8 *)comp_tbl, length,
233 transfer_flag, &comp_response,
234 &comp_response_code, NULL);
235
236 kfree(comp_tbl);
237
238 if (status) {
239 dev_err(dev, "Failed to transfer component table to firmware, err %s aq_err %s\n",
240 ice_stat_str(status),
241 ice_aq_str(hw->adminq.sq_last_status));
242 NL_SET_ERR_MSG_MOD(extack, "Failed to transfer component table to firmware");
243 return -EIO;
244 }
245
246 return ice_check_component_response(pf, component->identifier, comp_response,
247 comp_response_code, extack);
248}
249
250/**
251 * ice_write_one_nvm_block - Write an NVM block and await completion response
252 * @pf: the PF data structure
253 * @module: the module to write to
254 * @offset: offset in bytes
255 * @block_size: size of the block to write, up to 4k
256 * @block: pointer to block of data to write
257 * @last_cmd: whether this is the last command
258 * @extack: netlink extended ACK structure
259 *
260 * Write a block of data to a flash module, and await for the completion
261 * response message from firmware.
262 *
263 * Note this function assumes the caller has acquired the NVM resource.
264 *
265 * Returns: zero on success, or a negative error code on failure.
266 */
267static int
268ice_write_one_nvm_block(struct ice_pf *pf, u16 module, u32 offset,
269 u16 block_size, u8 *block, bool last_cmd,
270 struct netlink_ext_ack *extack)
271{
272 u16 completion_module, completion_retval;
273 struct device *dev = ice_pf_to_dev(pf);
274 struct ice_rq_event_info event;
275 struct ice_hw *hw = &pf->hw;
276 enum ice_status status;
277 u32 completion_offset;
278 int err;
279
280 memset(&event, 0, sizeof(event));
281
282 status = ice_aq_update_nvm(hw, module, offset, block_size, block,
283 last_cmd, 0, NULL);
284 if (status) {
285 dev_err(dev, "Failed to program flash module 0x%02x at offset %u, err %s aq_err %s\n",
286 module, offset, ice_stat_str(status),
287 ice_aq_str(hw->adminq.sq_last_status));
288 NL_SET_ERR_MSG_MOD(extack, "Failed to program flash module");
289 return -EIO;
290 }
291
292 /* In most cases, firmware reports a write completion within a few
293 * milliseconds. However, it has been observed that a completion might
294 * take more than a second to complete in some cases. The timeout here
295 * is conservative and is intended to prevent failure to update when
296 * firmware is slow to respond.
297 */
298 err = ice_aq_wait_for_event(pf, ice_aqc_opc_nvm_write, 15 * HZ, &event);
299 if (err) {
300 dev_err(dev, "Timed out waiting for firmware write completion for module 0x%02x, err %d\n",
301 module, err);
302 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
303 return -EIO;
304 }
305
306 completion_module = le16_to_cpu(event.desc.params.nvm.module_typeid);
307 completion_retval = le16_to_cpu(event.desc.retval);
308
309 completion_offset = le16_to_cpu(event.desc.params.nvm.offset_low);
310 completion_offset |= event.desc.params.nvm.offset_high << 16;
311
312 if (completion_module != module) {
313 dev_err(dev, "Unexpected module_typeid in write completion: got 0x%x, expected 0x%x\n",
314 completion_module, module);
315 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
316 return -EIO;
317 }
318
319 if (completion_offset != offset) {
320 dev_err(dev, "Unexpected offset in write completion: got %u, expected %u\n",
321 completion_offset, offset);
322 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
323 return -EIO;
324 }
325
326 if (completion_retval) {
327 dev_err(dev, "Firmware failed to program flash module 0x%02x at offset %u, completion err %s\n",
328 module, offset,
329 ice_aq_str((enum ice_aq_err)completion_retval));
330 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to program flash module");
331 return -EIO;
332 }
333
334 return 0;
335}
336
337/**
338 * ice_write_nvm_module - Write data to an NVM module
339 * @pf: the PF driver structure
340 * @module: the module id to program
341 * @component: the name of the component being updated
342 * @image: buffer of image data to write to the NVM
343 * @length: length of the buffer
344 * @extack: netlink extended ACK structure
345 *
346 * Loop over the data for a given NVM module and program it in 4 Kb
347 * blocks. Notify devlink core of progress after each block is programmed.
348 * Loops over a block of data and programs the NVM in 4k block chunks.
349 *
350 * Note this function assumes the caller has acquired the NVM resource.
351 *
352 * Returns: zero on success, or a negative error code on failure.
353 */
354static int
355ice_write_nvm_module(struct ice_pf *pf, u16 module, const char *component,
356 const u8 *image, u32 length,
357 struct netlink_ext_ack *extack)
358{
359 struct devlink *devlink;
360 u32 offset = 0;
361 bool last_cmd;
362 u8 *block;
363 int err;
364
365 devlink = priv_to_devlink(pf);
366
367 devlink_flash_update_status_notify(devlink, "Flashing",
368 component, 0, length);
369
370 block = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
371 if (!block)
372 return -ENOMEM;
373
374 do {
375 u32 block_size;
376
377 block_size = min_t(u32, ICE_AQ_MAX_BUF_LEN, length - offset);
378 last_cmd = !(offset + block_size < length);
379
380 /* ice_aq_update_nvm may copy the firmware response into the
381 * buffer, so we must make a copy since the source data is
382 * constant.
383 */
384 memcpy(block, image + offset, block_size);
385
386 err = ice_write_one_nvm_block(pf, module, offset, block_size,
387 block, last_cmd, extack);
388 if (err)
389 break;
390
391 offset += block_size;
392
393 devlink_flash_update_status_notify(devlink, "Flashing",
394 component, offset, length);
395 } while (!last_cmd);
396
397 if (err)
398 devlink_flash_update_status_notify(devlink, "Flashing failed",
399 component, length, length);
400 else
401 devlink_flash_update_status_notify(devlink, "Flashing done",
402 component, length, length);
403
404 kfree(block);
405 return err;
406}
407
408/**
409 * ice_erase_nvm_module - Erase an NVM module and await firmware completion
410 * @pf: the PF data structure
411 * @module: the module to erase
412 * @component: name of the component being updated
413 * @extack: netlink extended ACK structure
414 *
415 * Erase the inactive NVM bank associated with this module, and await for
416 * a completion response message from firmware.
417 *
418 * Note this function assumes the caller has acquired the NVM resource.
419 *
420 * Returns: zero on success, or a negative error code on failure.
421 */
422static int
423ice_erase_nvm_module(struct ice_pf *pf, u16 module, const char *component,
424 struct netlink_ext_ack *extack)
425{
426 u16 completion_module, completion_retval;
427 struct device *dev = ice_pf_to_dev(pf);
428 struct ice_rq_event_info event;
429 struct ice_hw *hw = &pf->hw;
430 struct devlink *devlink;
431 enum ice_status status;
432 int err;
433
434 memset(&event, 0, sizeof(event));
435
436 devlink = priv_to_devlink(pf);
437
438 devlink_flash_update_status_notify(devlink, "Erasing", component, 0, 0);
439
440 status = ice_aq_erase_nvm(hw, module, NULL);
441 if (status) {
442 dev_err(dev, "Failed to erase %s (module 0x%02x), err %s aq_err %s\n",
443 component, module, ice_stat_str(status),
444 ice_aq_str(hw->adminq.sq_last_status));
445 NL_SET_ERR_MSG_MOD(extack, "Failed to erase flash module");
446 err = -EIO;
447 goto out_notify_devlink;
448 }
449
450 /* Yes, this really can take minutes to complete */
451 err = ice_aq_wait_for_event(pf, ice_aqc_opc_nvm_erase, 300 * HZ, &event);
452 if (err) {
453 dev_err(dev, "Timed out waiting for firmware to respond with erase completion for %s (module 0x%02x), err %d\n",
454 component, module, err);
455 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
456 goto out_notify_devlink;
457 }
458
459 completion_module = le16_to_cpu(event.desc.params.nvm.module_typeid);
460 completion_retval = le16_to_cpu(event.desc.retval);
461
462 if (completion_module != module) {
463 dev_err(dev, "Unexpected module_typeid in erase completion for %s: got 0x%x, expected 0x%x\n",
464 component, completion_module, module);
465 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
466 err = -EIO;
467 goto out_notify_devlink;
468 }
469
470 if (completion_retval) {
471 dev_err(dev, "Firmware failed to erase %s (module 0x02%x), aq_err %s\n",
472 component, module,
473 ice_aq_str((enum ice_aq_err)completion_retval));
474 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to erase flash");
475 err = -EIO;
476 goto out_notify_devlink;
477 }
478
479out_notify_devlink:
480 if (err)
481 devlink_flash_update_status_notify(devlink, "Erasing failed",
482 component, 0, 0);
483 else
484 devlink_flash_update_status_notify(devlink, "Erasing done",
485 component, 0, 0);
486
487 return err;
488}
489
490/**
491 * ice_switch_flash_banks - Tell firmware to switch NVM banks
492 * @pf: Pointer to the PF data structure
493 * @activate_flags: flags used for the activation command
494 * @extack: netlink extended ACK structure
495 *
496 * Notify firmware to activate the newly written flash banks, and wait for the
497 * firmware response.
498 *
499 * Returns: zero on success or an error code on failure.
500 */
501static int ice_switch_flash_banks(struct ice_pf *pf, u8 activate_flags,
502 struct netlink_ext_ack *extack)
503{
504 struct device *dev = ice_pf_to_dev(pf);
505 struct ice_rq_event_info event;
506 struct ice_hw *hw = &pf->hw;
507 enum ice_status status;
508 u16 completion_retval;
509 int err;
510
511 memset(&event, 0, sizeof(event));
512
513 status = ice_nvm_write_activate(hw, activate_flags);
514 if (status) {
515 dev_err(dev, "Failed to switch active flash banks, err %s aq_err %s\n",
516 ice_stat_str(status),
517 ice_aq_str(hw->adminq.sq_last_status));
518 NL_SET_ERR_MSG_MOD(extack, "Failed to switch active flash banks");
519 return -EIO;
520 }
521
522 err = ice_aq_wait_for_event(pf, ice_aqc_opc_nvm_write_activate, 30 * HZ,
523 &event);
524 if (err) {
525 dev_err(dev, "Timed out waiting for firmware to switch active flash banks, err %d\n",
526 err);
527 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
528 return err;
529 }
530
531 completion_retval = le16_to_cpu(event.desc.retval);
532 if (completion_retval) {
533 dev_err(dev, "Firmware failed to switch active flash banks aq_err %s\n",
534 ice_aq_str((enum ice_aq_err)completion_retval));
535 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to switch active flash banks");
536 return -EIO;
537 }
538
539 return 0;
540}
541
542/**
543 * ice_flash_component - Flash a component of the NVM
544 * @context: PLDM fw update structure
545 * @component: the component table to program
546 *
547 * Program the flash contents for a given component. First, determine the
548 * module id. Then, erase the secondary bank for this module. Finally, write
549 * the contents of the component to the NVM.
550 *
551 * Note this function assumes the caller has acquired the NVM resource.
552 *
553 * Returns: zero on success, or a negative error code on failure.
554 */
555static int
556ice_flash_component(struct pldmfw *context, struct pldmfw_component *component)
557{
558 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
559 struct netlink_ext_ack *extack = priv->extack;
560 struct ice_pf *pf = priv->pf;
561 const char *name;
562 u16 module;
563 u8 flag;
564 int err;
565
566 switch (component->identifier) {
567 case NVM_COMP_ID_OROM:
568 module = ICE_SR_1ST_OROM_BANK_PTR;
569 flag = ICE_AQC_NVM_ACTIV_SEL_OROM;
570 name = "fw.undi";
571 break;
572 case NVM_COMP_ID_NVM:
573 module = ICE_SR_1ST_NVM_BANK_PTR;
574 flag = ICE_AQC_NVM_ACTIV_SEL_NVM;
575 name = "fw.mgmt";
576 break;
577 case NVM_COMP_ID_NETLIST:
578 module = ICE_SR_NETLIST_BANK_PTR;
579 flag = ICE_AQC_NVM_ACTIV_SEL_NETLIST;
580 name = "fw.netlist";
581 break;
582 default:
583 /* This should not trigger, since we check the id before
584 * sending the component table to firmware.
585 */
586 WARN(1, "Unexpected unknown component identifier 0x%02x",
587 component->identifier);
588 return -EINVAL;
589 }
590
591 /* Mark this component for activating at the end */
592 priv->activate_flags |= flag;
593
594 err = ice_erase_nvm_module(pf, module, name, extack);
595 if (err)
596 return err;
597
598 return ice_write_nvm_module(pf, module, name, component->component_data,
599 component->component_size, extack);
600}
601
602/**
603 * ice_finalize_update - Perform last steps to complete device update
604 * @context: PLDM fw update structure
605 *
606 * Called as the last step of the update process. Complete the update by
607 * telling the firmware to switch active banks, and perform a reset of
608 * configured.
609 *
610 * Returns: 0 on success, or an error code on failure.
611 */
612static int ice_finalize_update(struct pldmfw *context)
613{
614 struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
615 struct netlink_ext_ack *extack = priv->extack;
616 struct ice_pf *pf = priv->pf;
617 int err;
618
619 /* Finally, notify firmware to activate the written NVM banks */
620 err = ice_switch_flash_banks(pf, priv->activate_flags, extack);
621 if (err)
622 return err;
623
624 return 0;
625}
626
627static const struct pldmfw_ops ice_fwu_ops = {
628 .match_record = &pldmfw_op_pci_match_record,
629 .send_package_data = &ice_send_package_data,
630 .send_component_table = &ice_send_component_table,
631 .flash_component = &ice_flash_component,
632 .finalize_update = &ice_finalize_update,
633};
634
635/**
636 * ice_flash_pldm_image - Write a PLDM-formatted firmware image to the device
637 * @pf: private device driver structure
638 * @fw: firmware object pointing to the relevant firmware file
639 * @extack: netlink extended ACK structure
640 *
641 * Parse the data for a given firmware file, verifying that it is a valid PLDM
642 * formatted image that matches this device.
643 *
644 * Extract the device record Package Data and Component Tables and send them
645 * to the firmware. Extract and write the flash data for each of the three
646 * main flash components, "fw.mgmt", "fw.undi", and "fw.netlist". Notify
647 * firmware once the data is written to the inactive banks.
648 *
649 * Returns: zero on success or a negative error code on failure.
650 */
651int ice_flash_pldm_image(struct ice_pf *pf, const struct firmware *fw,
652 struct netlink_ext_ack *extack)
653{
654 struct device *dev = ice_pf_to_dev(pf);
655 struct ice_hw *hw = &pf->hw;
656 struct ice_fwu_priv priv;
657 enum ice_status status;
658 int err;
659
660 memset(&priv, 0, sizeof(priv));
661
662 priv.context.ops = &ice_fwu_ops;
663 priv.context.dev = dev;
664 priv.extack = extack;
665 priv.pf = pf;
666 priv.activate_flags = ICE_AQC_NVM_PRESERVE_ALL;
667
668 status = ice_acquire_nvm(hw, ICE_RES_WRITE);
669 if (status) {
670 dev_err(dev, "Failed to acquire device flash lock, err %s aq_err %s\n",
671 ice_stat_str(status),
672 ice_aq_str(hw->adminq.sq_last_status));
673 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
674 return -EIO;
675 }
676
677 err = pldmfw_flash_image(&priv.context, fw);
678
679 ice_release_nvm(hw);
680
681 return err;
682}
683
684/**
685 * ice_check_for_pending_update - Check for a pending flash update
686 * @pf: the PF driver structure
687 * @component: if not NULL, the name of the component being updated
688 * @extack: Netlink extended ACK structure
689 *
690 * Check whether the device already has a pending flash update. If such an
691 * update is found, cancel it so that the requested update may proceed.
692 *
693 * Returns: zero on success, or a negative error code on failure.
694 */
695int ice_check_for_pending_update(struct ice_pf *pf, const char *component,
696 struct netlink_ext_ack *extack)
697{
698 struct devlink *devlink = priv_to_devlink(pf);
699 struct device *dev = ice_pf_to_dev(pf);
700 struct ice_hw_dev_caps *dev_caps;
701 struct ice_hw *hw = &pf->hw;
702 enum ice_status status;
703 u8 pending = 0;
704 int err;
705
706 dev_caps = kzalloc(sizeof(*dev_caps), GFP_KERNEL);
707 if (!dev_caps)
708 return -ENOMEM;
709
710 /* Read the most recent device capabilities from firmware. Do not use
711 * the cached values in hw->dev_caps, because the pending update flag
712 * may have changed, e.g. if an update was previously completed and
713 * the system has not yet rebooted.
714 */
715 status = ice_discover_dev_caps(hw, dev_caps);
716 if (status) {
717 NL_SET_ERR_MSG_MOD(extack, "Unable to read device capabilities");
718 kfree(dev_caps);
719 return -EIO;
720 }
721
722 if (dev_caps->common_cap.nvm_update_pending_nvm) {
723 dev_info(dev, "The fw.mgmt flash component has a pending update\n");
724 pending |= ICE_AQC_NVM_ACTIV_SEL_NVM;
725 }
726
727 if (dev_caps->common_cap.nvm_update_pending_orom) {
728 dev_info(dev, "The fw.undi flash component has a pending update\n");
729 pending |= ICE_AQC_NVM_ACTIV_SEL_OROM;
730 }
731
732 if (dev_caps->common_cap.nvm_update_pending_netlist) {
733 dev_info(dev, "The fw.netlist flash component has a pending update\n");
734 pending |= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
735 }
736
737 kfree(dev_caps);
738
739 /* If the flash_update request is for a specific component, ignore all
740 * of the other components.
741 */
742 if (component) {
743 if (strcmp(component, "fw.mgmt") == 0)
744 pending &= ICE_AQC_NVM_ACTIV_SEL_NVM;
745 else if (strcmp(component, "fw.undi") == 0)
746 pending &= ICE_AQC_NVM_ACTIV_SEL_OROM;
747 else if (strcmp(component, "fw.netlist") == 0)
748 pending &= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
749 else
750 WARN(1, "Unexpected flash component %s", component);
751 }
752
753 /* There is no previous pending update, so this request may continue */
754 if (!pending)
755 return 0;
756
757 /* In order to allow overwriting a previous pending update, notify
758 * firmware to cancel that update by issuing the appropriate command.
759 */
760 devlink_flash_update_status_notify(devlink,
761 "Canceling previous pending update",
762 component, 0, 0);
763
764 status = ice_acquire_nvm(hw, ICE_RES_WRITE);
765 if (status) {
766 dev_err(dev, "Failed to acquire device flash lock, err %s aq_err %s\n",
767 ice_stat_str(status),
768 ice_aq_str(hw->adminq.sq_last_status));
769 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
770 return -EIO;
771 }
772
773 pending |= ICE_AQC_NVM_REVERT_LAST_ACTIV;
774 err = ice_switch_flash_banks(pf, pending, extack);
775
776 ice_release_nvm(hw);
777
778 return err;
779}