Merge tag 'spi-fix-v5.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi
[linux-2.6-microblaze.git] / drivers / net / ethernet / intel / ice / ice_fw_update.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2018-2019, Intel Corporation. */
3
4 #include <asm/unaligned.h>
5 #include <linux/uuid.h>
6 #include <linux/crc32.h>
7 #include <linux/pldmfw.h>
8 #include "ice.h"
9 #include "ice_fw_update.h"
10
11 struct ice_fwu_priv {
12         struct pldmfw context;
13
14         struct ice_pf *pf;
15         struct netlink_ext_ack *extack;
16
17         /* Track which NVM banks to activate at the end of the update */
18         u8 activate_flags;
19 };
20
21 /**
22  * ice_send_package_data - Send record package data to firmware
23  * @context: PLDM fw update structure
24  * @data: pointer to the package data
25  * @length: length of the package data
26  *
27  * Send a copy of the package data associated with the PLDM record matching
28  * this device to the firmware.
29  *
30  * Note that this function sends an AdminQ command that will fail unless the
31  * NVM resource has been acquired.
32  *
33  * Returns: zero on success, or a negative error code on failure.
34  */
35 static int
36 ice_send_package_data(struct pldmfw *context, const u8 *data, u16 length)
37 {
38         struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
39         struct netlink_ext_ack *extack = priv->extack;
40         struct device *dev = context->dev;
41         struct ice_pf *pf = priv->pf;
42         struct ice_hw *hw = &pf->hw;
43         enum ice_status status;
44         u8 *package_data;
45
46         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  */
80 static int
81 ice_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  */
193 static int
194 ice_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  */
267 static int
268 ice_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         err = ice_aq_wait_for_event(pf, ice_aqc_opc_nvm_write, HZ, &event);
293         if (err) {
294                 dev_err(dev, "Timed out waiting for firmware write completion for module 0x%02x, err %d\n",
295                         module, err);
296                 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
297                 return -EIO;
298         }
299
300         completion_module = le16_to_cpu(event.desc.params.nvm.module_typeid);
301         completion_retval = le16_to_cpu(event.desc.retval);
302
303         completion_offset = le16_to_cpu(event.desc.params.nvm.offset_low);
304         completion_offset |= event.desc.params.nvm.offset_high << 16;
305
306         if (completion_module != module) {
307                 dev_err(dev, "Unexpected module_typeid in write completion: got 0x%x, expected 0x%x\n",
308                         completion_module, module);
309                 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
310                 return -EIO;
311         }
312
313         if (completion_offset != offset) {
314                 dev_err(dev, "Unexpected offset in write completion: got %u, expected %u\n",
315                         completion_offset, offset);
316                 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
317                 return -EIO;
318         }
319
320         if (completion_retval) {
321                 dev_err(dev, "Firmware failed to program flash module 0x%02x at offset %u, completion err %s\n",
322                         module, offset,
323                         ice_aq_str((enum ice_aq_err)completion_retval));
324                 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to program flash module");
325                 return -EIO;
326         }
327
328         return 0;
329 }
330
331 /**
332  * ice_write_nvm_module - Write data to an NVM module
333  * @pf: the PF driver structure
334  * @module: the module id to program
335  * @component: the name of the component being updated
336  * @image: buffer of image data to write to the NVM
337  * @length: length of the buffer
338  * @extack: netlink extended ACK structure
339  *
340  * Loop over the data for a given NVM module and program it in 4 Kb
341  * blocks. Notify devlink core of progress after each block is programmed.
342  * Loops over a block of data and programs the NVM in 4k block chunks.
343  *
344  * Note this function assumes the caller has acquired the NVM resource.
345  *
346  * Returns: zero on success, or a negative error code on failure.
347  */
348 static int
349 ice_write_nvm_module(struct ice_pf *pf, u16 module, const char *component,
350                      const u8 *image, u32 length,
351                      struct netlink_ext_ack *extack)
352 {
353         struct devlink *devlink;
354         u32 offset = 0;
355         bool last_cmd;
356         u8 *block;
357         int err;
358
359         devlink = priv_to_devlink(pf);
360
361         devlink_flash_update_status_notify(devlink, "Flashing",
362                                            component, 0, length);
363
364         block = kzalloc(ICE_AQ_MAX_BUF_LEN, GFP_KERNEL);
365         if (!block)
366                 return -ENOMEM;
367
368         do {
369                 u32 block_size;
370
371                 block_size = min_t(u32, ICE_AQ_MAX_BUF_LEN, length - offset);
372                 last_cmd = !(offset + block_size < length);
373
374                 /* ice_aq_update_nvm may copy the firmware response into the
375                  * buffer, so we must make a copy since the source data is
376                  * constant.
377                  */
378                 memcpy(block, image + offset, block_size);
379
380                 err = ice_write_one_nvm_block(pf, module, offset, block_size,
381                                               block, last_cmd, extack);
382                 if (err)
383                         break;
384
385                 offset += block_size;
386
387                 devlink_flash_update_status_notify(devlink, "Flashing",
388                                                    component, offset, length);
389         } while (!last_cmd);
390
391         if (err)
392                 devlink_flash_update_status_notify(devlink, "Flashing failed",
393                                                    component, length, length);
394         else
395                 devlink_flash_update_status_notify(devlink, "Flashing done",
396                                                    component, length, length);
397
398         kfree(block);
399         return err;
400 }
401
402 /**
403  * ice_erase_nvm_module - Erase an NVM module and await firmware completion
404  * @pf: the PF data structure
405  * @module: the module to erase
406  * @component: name of the component being updated
407  * @extack: netlink extended ACK structure
408  *
409  * Erase the inactive NVM bank associated with this module, and await for
410  * a completion response message from firmware.
411  *
412  * Note this function assumes the caller has acquired the NVM resource.
413  *
414  * Returns: zero on success, or a negative error code on failure.
415  */
416 static int
417 ice_erase_nvm_module(struct ice_pf *pf, u16 module, const char *component,
418                      struct netlink_ext_ack *extack)
419 {
420         u16 completion_module, completion_retval;
421         struct device *dev = ice_pf_to_dev(pf);
422         struct ice_rq_event_info event;
423         struct ice_hw *hw = &pf->hw;
424         struct devlink *devlink;
425         enum ice_status status;
426         int err;
427
428         memset(&event, 0, sizeof(event));
429
430         devlink = priv_to_devlink(pf);
431
432         devlink_flash_update_status_notify(devlink, "Erasing", component, 0, 0);
433
434         status = ice_aq_erase_nvm(hw, module, NULL);
435         if (status) {
436                 dev_err(dev, "Failed to erase %s (module 0x%02x), err %s aq_err %s\n",
437                         component, module, ice_stat_str(status),
438                         ice_aq_str(hw->adminq.sq_last_status));
439                 NL_SET_ERR_MSG_MOD(extack, "Failed to erase flash module");
440                 err = -EIO;
441                 goto out_notify_devlink;
442         }
443
444         /* Yes, this really can take minutes to complete */
445         err = ice_aq_wait_for_event(pf, ice_aqc_opc_nvm_erase, 300 * HZ, &event);
446         if (err) {
447                 dev_err(dev, "Timed out waiting for firmware to respond with erase completion for %s (module 0x%02x), err %d\n",
448                         component, module, err);
449                 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
450                 goto out_notify_devlink;
451         }
452
453         completion_module = le16_to_cpu(event.desc.params.nvm.module_typeid);
454         completion_retval = le16_to_cpu(event.desc.retval);
455
456         if (completion_module != module) {
457                 dev_err(dev, "Unexpected module_typeid in erase completion for %s: got 0x%x, expected 0x%x\n",
458                         component, completion_module, module);
459                 NL_SET_ERR_MSG_MOD(extack, "Unexpected firmware response");
460                 err = -EIO;
461                 goto out_notify_devlink;
462         }
463
464         if (completion_retval) {
465                 dev_err(dev, "Firmware failed to erase %s (module 0x02%x), aq_err %s\n",
466                         component, module,
467                         ice_aq_str((enum ice_aq_err)completion_retval));
468                 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to erase flash");
469                 err = -EIO;
470                 goto out_notify_devlink;
471         }
472
473 out_notify_devlink:
474         if (err)
475                 devlink_flash_update_status_notify(devlink, "Erasing failed",
476                                                    component, 0, 0);
477         else
478                 devlink_flash_update_status_notify(devlink, "Erasing done",
479                                                    component, 0, 0);
480
481         return err;
482 }
483
484 /**
485  * ice_switch_flash_banks - Tell firmware to switch NVM banks
486  * @pf: Pointer to the PF data structure
487  * @activate_flags: flags used for the activation command
488  * @extack: netlink extended ACK structure
489  *
490  * Notify firmware to activate the newly written flash banks, and wait for the
491  * firmware response.
492  *
493  * Returns: zero on success or an error code on failure.
494  */
495 static int ice_switch_flash_banks(struct ice_pf *pf, u8 activate_flags,
496                                   struct netlink_ext_ack *extack)
497 {
498         struct device *dev = ice_pf_to_dev(pf);
499         struct ice_rq_event_info event;
500         struct ice_hw *hw = &pf->hw;
501         enum ice_status status;
502         u16 completion_retval;
503         int err;
504
505         memset(&event, 0, sizeof(event));
506
507         status = ice_nvm_write_activate(hw, activate_flags);
508         if (status) {
509                 dev_err(dev, "Failed to switch active flash banks, err %s aq_err %s\n",
510                         ice_stat_str(status),
511                         ice_aq_str(hw->adminq.sq_last_status));
512                 NL_SET_ERR_MSG_MOD(extack, "Failed to switch active flash banks");
513                 return -EIO;
514         }
515
516         err = ice_aq_wait_for_event(pf, ice_aqc_opc_nvm_write_activate, HZ,
517                                     &event);
518         if (err) {
519                 dev_err(dev, "Timed out waiting for firmware to switch active flash banks, err %d\n",
520                         err);
521                 NL_SET_ERR_MSG_MOD(extack, "Timed out waiting for firmware");
522                 return err;
523         }
524
525         completion_retval = le16_to_cpu(event.desc.retval);
526         if (completion_retval) {
527                 dev_err(dev, "Firmware failed to switch active flash banks aq_err %s\n",
528                         ice_aq_str((enum ice_aq_err)completion_retval));
529                 NL_SET_ERR_MSG_MOD(extack, "Firmware failed to switch active flash banks");
530                 return -EIO;
531         }
532
533         return 0;
534 }
535
536 /**
537  * ice_flash_component - Flash a component of the NVM
538  * @context: PLDM fw update structure
539  * @component: the component table to program
540  *
541  * Program the flash contents for a given component. First, determine the
542  * module id. Then, erase the secondary bank for this module. Finally, write
543  * the contents of the component to the NVM.
544  *
545  * Note this function assumes the caller has acquired the NVM resource.
546  *
547  * Returns: zero on success, or a negative error code on failure.
548  */
549 static int
550 ice_flash_component(struct pldmfw *context, struct pldmfw_component *component)
551 {
552         struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
553         struct netlink_ext_ack *extack = priv->extack;
554         struct ice_pf *pf = priv->pf;
555         const char *name;
556         u16 module;
557         u8 flag;
558         int err;
559
560         switch (component->identifier) {
561         case NVM_COMP_ID_OROM:
562                 module = ICE_SR_1ST_OROM_BANK_PTR;
563                 flag = ICE_AQC_NVM_ACTIV_SEL_OROM;
564                 name = "fw.undi";
565                 break;
566         case NVM_COMP_ID_NVM:
567                 module = ICE_SR_1ST_NVM_BANK_PTR;
568                 flag = ICE_AQC_NVM_ACTIV_SEL_NVM;
569                 name = "fw.mgmt";
570                 break;
571         case NVM_COMP_ID_NETLIST:
572                 module = ICE_SR_NETLIST_BANK_PTR;
573                 flag = ICE_AQC_NVM_ACTIV_SEL_NETLIST;
574                 name = "fw.netlist";
575                 break;
576         default:
577                 /* This should not trigger, since we check the id before
578                  * sending the component table to firmware.
579                  */
580                 WARN(1, "Unexpected unknown component identifier 0x%02x",
581                      component->identifier);
582                 return -EINVAL;
583         }
584
585         /* Mark this component for activating at the end */
586         priv->activate_flags |= flag;
587
588         err = ice_erase_nvm_module(pf, module, name, extack);
589         if (err)
590                 return err;
591
592         return ice_write_nvm_module(pf, module, name, component->component_data,
593                                     component->component_size, extack);
594 }
595
596 /**
597  * ice_finalize_update - Perform last steps to complete device update
598  * @context: PLDM fw update structure
599  *
600  * Called as the last step of the update process. Complete the update by
601  * telling the firmware to switch active banks, and perform a reset of
602  * configured.
603  *
604  * Returns: 0 on success, or an error code on failure.
605  */
606 static int ice_finalize_update(struct pldmfw *context)
607 {
608         struct ice_fwu_priv *priv = container_of(context, struct ice_fwu_priv, context);
609         struct netlink_ext_ack *extack = priv->extack;
610         struct ice_pf *pf = priv->pf;
611         int err;
612
613         /* Finally, notify firmware to activate the written NVM banks */
614         err = ice_switch_flash_banks(pf, priv->activate_flags, extack);
615         if (err)
616                 return err;
617
618         return 0;
619 }
620
621 static const struct pldmfw_ops ice_fwu_ops = {
622         .match_record = &pldmfw_op_pci_match_record,
623         .send_package_data = &ice_send_package_data,
624         .send_component_table = &ice_send_component_table,
625         .flash_component = &ice_flash_component,
626         .finalize_update = &ice_finalize_update,
627 };
628
629 /**
630  * ice_flash_pldm_image - Write a PLDM-formatted firmware image to the device
631  * @pf: private device driver structure
632  * @fw: firmware object pointing to the relevant firmware file
633  * @extack: netlink extended ACK structure
634  *
635  * Parse the data for a given firmware file, verifying that it is a valid PLDM
636  * formatted image that matches this device.
637  *
638  * Extract the device record Package Data and Component Tables and send them
639  * to the firmware. Extract and write the flash data for each of the three
640  * main flash components, "fw.mgmt", "fw.undi", and "fw.netlist". Notify
641  * firmware once the data is written to the inactive banks.
642  *
643  * Returns: zero on success or a negative error code on failure.
644  */
645 int ice_flash_pldm_image(struct ice_pf *pf, const struct firmware *fw,
646                          struct netlink_ext_ack *extack)
647 {
648         struct device *dev = ice_pf_to_dev(pf);
649         struct ice_hw *hw = &pf->hw;
650         struct ice_fwu_priv priv;
651         enum ice_status status;
652         int err;
653
654         memset(&priv, 0, sizeof(priv));
655
656         priv.context.ops = &ice_fwu_ops;
657         priv.context.dev = dev;
658         priv.extack = extack;
659         priv.pf = pf;
660         priv.activate_flags = ICE_AQC_NVM_PRESERVE_ALL;
661
662         status = ice_acquire_nvm(hw, ICE_RES_WRITE);
663         if (status) {
664                 dev_err(dev, "Failed to acquire device flash lock, err %s aq_err %s\n",
665                         ice_stat_str(status),
666                         ice_aq_str(hw->adminq.sq_last_status));
667                 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
668                 return -EIO;
669         }
670
671         err = pldmfw_flash_image(&priv.context, fw);
672
673         ice_release_nvm(hw);
674
675         return err;
676 }
677
678 /**
679  * ice_check_for_pending_update - Check for a pending flash update
680  * @pf: the PF driver structure
681  * @component: if not NULL, the name of the component being updated
682  * @extack: Netlink extended ACK structure
683  *
684  * Check whether the device already has a pending flash update. If such an
685  * update is found, cancel it so that the requested update may proceed.
686  *
687  * Returns: zero on success, or a negative error code on failure.
688  */
689 int ice_check_for_pending_update(struct ice_pf *pf, const char *component,
690                                  struct netlink_ext_ack *extack)
691 {
692         struct devlink *devlink = priv_to_devlink(pf);
693         struct device *dev = ice_pf_to_dev(pf);
694         struct ice_hw_dev_caps *dev_caps;
695         struct ice_hw *hw = &pf->hw;
696         enum ice_status status;
697         u8 pending = 0;
698         int err;
699
700         dev_caps = kzalloc(sizeof(*dev_caps), GFP_KERNEL);
701         if (!dev_caps)
702                 return -ENOMEM;
703
704         /* Read the most recent device capabilities from firmware. Do not use
705          * the cached values in hw->dev_caps, because the pending update flag
706          * may have changed, e.g. if an update was previously completed and
707          * the system has not yet rebooted.
708          */
709         status = ice_discover_dev_caps(hw, dev_caps);
710         if (status) {
711                 NL_SET_ERR_MSG_MOD(extack, "Unable to read device capabilities");
712                 kfree(dev_caps);
713                 return -EIO;
714         }
715
716         if (dev_caps->common_cap.nvm_update_pending_nvm) {
717                 dev_info(dev, "The fw.mgmt flash component has a pending update\n");
718                 pending |= ICE_AQC_NVM_ACTIV_SEL_NVM;
719         }
720
721         if (dev_caps->common_cap.nvm_update_pending_orom) {
722                 dev_info(dev, "The fw.undi flash component has a pending update\n");
723                 pending |= ICE_AQC_NVM_ACTIV_SEL_OROM;
724         }
725
726         if (dev_caps->common_cap.nvm_update_pending_netlist) {
727                 dev_info(dev, "The fw.netlist flash component has a pending update\n");
728                 pending |= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
729         }
730
731         kfree(dev_caps);
732
733         /* If the flash_update request is for a specific component, ignore all
734          * of the other components.
735          */
736         if (component) {
737                 if (strcmp(component, "fw.mgmt") == 0)
738                         pending &= ICE_AQC_NVM_ACTIV_SEL_NVM;
739                 else if (strcmp(component, "fw.undi") == 0)
740                         pending &= ICE_AQC_NVM_ACTIV_SEL_OROM;
741                 else if (strcmp(component, "fw.netlist") == 0)
742                         pending &= ICE_AQC_NVM_ACTIV_SEL_NETLIST;
743                 else
744                         WARN(1, "Unexpected flash component %s", component);
745         }
746
747         /* There is no previous pending update, so this request may continue */
748         if (!pending)
749                 return 0;
750
751         /* In order to allow overwriting a previous pending update, notify
752          * firmware to cancel that update by issuing the appropriate command.
753          */
754         devlink_flash_update_status_notify(devlink,
755                                            "Canceling previous pending update",
756                                            component, 0, 0);
757
758         status = ice_acquire_nvm(hw, ICE_RES_WRITE);
759         if (status) {
760                 dev_err(dev, "Failed to acquire device flash lock, err %s aq_err %s\n",
761                         ice_stat_str(status),
762                         ice_aq_str(hw->adminq.sq_last_status));
763                 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire device flash lock");
764                 return -EIO;
765         }
766
767         pending |= ICE_AQC_NVM_REVERT_LAST_ACTIV;
768         err = ice_switch_flash_banks(pf, pending, extack);
769
770         ice_release_nvm(hw);
771
772         return err;
773 }