ice: Print the api_patch as part of the fw.mgmt.api
[platform/kernel/linux-starfive.git] / drivers / net / ethernet / intel / ice / ice_devlink.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
3
4 #include "ice.h"
5 #include "ice_lib.h"
6 #include "ice_devlink.h"
7 #include "ice_fw_update.h"
8
9 /* context for devlink info version reporting */
10 struct ice_info_ctx {
11         char buf[128];
12         struct ice_orom_info pending_orom;
13         struct ice_nvm_info pending_nvm;
14         struct ice_netlist_info pending_netlist;
15         struct ice_hw_dev_caps dev_caps;
16 };
17
18 /* The following functions are used to format specific strings for various
19  * devlink info versions. The ctx parameter is used to provide the storage
20  * buffer, as well as any ancillary information calculated when the info
21  * request was made.
22  *
23  * If a version does not exist, for example when attempting to get the
24  * inactive version of flash when there is no pending update, the function
25  * should leave the buffer in the ctx structure empty and return 0.
26  */
27
28 static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
29 {
30         u8 dsn[8];
31
32         /* Copy the DSN into an array in Big Endian format */
33         put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
34
35         snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
36 }
37
38 static int ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
39 {
40         struct ice_hw *hw = &pf->hw;
41         enum ice_status status;
42
43         status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
44         if (status)
45                 /* We failed to locate the PBA, so just skip this entry */
46                 dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %s\n",
47                         ice_stat_str(status));
48
49         return 0;
50 }
51
52 static int ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
53 {
54         struct ice_hw *hw = &pf->hw;
55
56         snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver,
57                  hw->fw_patch);
58
59         return 0;
60 }
61
62 static int ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
63 {
64         struct ice_hw *hw = &pf->hw;
65
66         snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
67                  hw->api_min_ver, hw->api_patch);
68
69         return 0;
70 }
71
72 static int ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
73 {
74         struct ice_hw *hw = &pf->hw;
75
76         snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
77
78         return 0;
79 }
80
81 static int ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
82 {
83         struct ice_orom_info *orom = &pf->hw.flash.orom;
84
85         snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", orom->major, orom->build, orom->patch);
86
87         return 0;
88 }
89
90 static int
91 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
92 {
93         struct ice_orom_info *orom = &ctx->pending_orom;
94
95         if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
96                 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
97                          orom->major, orom->build, orom->patch);
98
99         return 0;
100 }
101
102 static int ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
103 {
104         struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
105
106         snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
107
108         return 0;
109 }
110
111 static int
112 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
113 {
114         struct ice_nvm_info *nvm = &ctx->pending_nvm;
115
116         if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
117                 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
118
119         return 0;
120 }
121
122 static int ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
123 {
124         struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
125
126         snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
127
128         return 0;
129 }
130
131 static int
132 ice_info_pending_eetrack(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
133 {
134         struct ice_nvm_info *nvm = &ctx->pending_nvm;
135
136         if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
137                 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
138
139         return 0;
140 }
141
142 static int ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
143 {
144         struct ice_hw *hw = &pf->hw;
145
146         snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
147
148         return 0;
149 }
150
151 static int ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
152 {
153         struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
154
155         snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
156                  pkg->draft);
157
158         return 0;
159 }
160
161 static int ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
162 {
163         snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
164
165         return 0;
166 }
167
168 static int ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
169 {
170         struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
171
172         /* The netlist version fields are BCD formatted */
173         snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
174                  netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
175                  netlist->cust_ver);
176
177         return 0;
178 }
179
180 static int ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
181 {
182         struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
183
184         snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
185
186         return 0;
187 }
188
189 static int
190 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
191 {
192         struct ice_netlist_info *netlist = &ctx->pending_netlist;
193
194         /* The netlist version fields are BCD formatted */
195         if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
196                 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
197                          netlist->major, netlist->minor,
198                          netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
199                          netlist->cust_ver);
200
201         return 0;
202 }
203
204 static int
205 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
206 {
207         struct ice_netlist_info *netlist = &ctx->pending_netlist;
208
209         if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
210                 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
211
212         return 0;
213 }
214
215 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
216 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
217 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
218
219 /* The combined() macro inserts both the running entry as well as a stored
220  * entry. The running entry will always report the version from the active
221  * handler. The stored entry will first try the pending handler, and fallback
222  * to the active handler if the pending function does not report a version.
223  * The pending handler should check the status of a pending update for the
224  * relevant flash component. It should only fill in the buffer in the case
225  * where a valid pending version is available. This ensures that the related
226  * stored and running versions remain in sync, and that stored versions are
227  * correctly reported as expected.
228  */
229 #define combined(key, active, pending) \
230         running(key, active), \
231         stored(key, pending, active)
232
233 enum ice_version_type {
234         ICE_VERSION_FIXED,
235         ICE_VERSION_RUNNING,
236         ICE_VERSION_STORED,
237 };
238
239 static const struct ice_devlink_version {
240         enum ice_version_type type;
241         const char *key;
242         int (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
243         int (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
244 } ice_devlink_versions[] = {
245         fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
246         running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
247         running("fw.mgmt.api", ice_info_fw_api),
248         running("fw.mgmt.build", ice_info_fw_build),
249         combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
250         combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
251         combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
252         running("fw.app.name", ice_info_ddp_pkg_name),
253         running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
254         running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
255         combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
256         combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
257 };
258
259 /**
260  * ice_devlink_info_get - .info_get devlink handler
261  * @devlink: devlink instance structure
262  * @req: the devlink info request
263  * @extack: extended netdev ack structure
264  *
265  * Callback for the devlink .info_get operation. Reports information about the
266  * device.
267  *
268  * Return: zero on success or an error code on failure.
269  */
270 static int ice_devlink_info_get(struct devlink *devlink,
271                                 struct devlink_info_req *req,
272                                 struct netlink_ext_ack *extack)
273 {
274         struct ice_pf *pf = devlink_priv(devlink);
275         struct device *dev = ice_pf_to_dev(pf);
276         struct ice_hw *hw = &pf->hw;
277         struct ice_info_ctx *ctx;
278         enum ice_status status;
279         size_t i;
280         int err;
281
282         err = ice_wait_for_reset(pf, 10 * HZ);
283         if (err) {
284                 NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
285                 return err;
286         }
287
288         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
289         if (!ctx)
290                 return -ENOMEM;
291
292         /* discover capabilities first */
293         status = ice_discover_dev_caps(hw, &ctx->dev_caps);
294         if (status) {
295                 dev_dbg(dev, "Failed to discover device capabilities, status %s aq_err %s\n",
296                         ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
297                 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
298                 err = -EIO;
299                 goto out_free_ctx;
300         }
301
302         if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
303                 status = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
304                 if (status) {
305                         dev_dbg(dev, "Unable to read inactive Option ROM version data, status %s aq_err %s\n",
306                                 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
307
308                         /* disable display of pending Option ROM */
309                         ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
310                 }
311         }
312
313         if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
314                 status = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
315                 if (status) {
316                         dev_dbg(dev, "Unable to read inactive NVM version data, status %s aq_err %s\n",
317                                 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
318
319                         /* disable display of pending Option ROM */
320                         ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
321                 }
322         }
323
324         if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
325                 status = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
326                 if (status) {
327                         dev_dbg(dev, "Unable to read inactive Netlist version data, status %s aq_err %s\n",
328                                 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
329
330                         /* disable display of pending Option ROM */
331                         ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
332                 }
333         }
334
335         err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
336         if (err) {
337                 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
338                 goto out_free_ctx;
339         }
340
341         ice_info_get_dsn(pf, ctx);
342
343         err = devlink_info_serial_number_put(req, ctx->buf);
344         if (err) {
345                 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
346                 goto out_free_ctx;
347         }
348
349         for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
350                 enum ice_version_type type = ice_devlink_versions[i].type;
351                 const char *key = ice_devlink_versions[i].key;
352
353                 memset(ctx->buf, 0, sizeof(ctx->buf));
354
355                 err = ice_devlink_versions[i].getter(pf, ctx);
356                 if (err) {
357                         NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
358                         goto out_free_ctx;
359                 }
360
361                 /* If the default getter doesn't report a version, use the
362                  * fallback function. This is primarily useful in the case of
363                  * "stored" versions that want to report the same value as the
364                  * running version in the normal case of no pending update.
365                  */
366                 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback) {
367                         err = ice_devlink_versions[i].fallback(pf, ctx);
368                         if (err) {
369                                 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
370                                 goto out_free_ctx;
371                         }
372                 }
373
374                 /* Do not report missing versions */
375                 if (ctx->buf[0] == '\0')
376                         continue;
377
378                 switch (type) {
379                 case ICE_VERSION_FIXED:
380                         err = devlink_info_version_fixed_put(req, key, ctx->buf);
381                         if (err) {
382                                 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
383                                 goto out_free_ctx;
384                         }
385                         break;
386                 case ICE_VERSION_RUNNING:
387                         err = devlink_info_version_running_put(req, key, ctx->buf);
388                         if (err) {
389                                 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
390                                 goto out_free_ctx;
391                         }
392                         break;
393                 case ICE_VERSION_STORED:
394                         err = devlink_info_version_stored_put(req, key, ctx->buf);
395                         if (err) {
396                                 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
397                                 goto out_free_ctx;
398                         }
399                         break;
400                 }
401         }
402
403 out_free_ctx:
404         kfree(ctx);
405         return err;
406 }
407
408 /**
409  * ice_devlink_flash_update - Update firmware stored in flash on the device
410  * @devlink: pointer to devlink associated with device to update
411  * @params: flash update parameters
412  * @extack: netlink extended ACK structure
413  *
414  * Perform a device flash update. The bulk of the update logic is contained
415  * within the ice_flash_pldm_image function.
416  *
417  * Returns: zero on success, or an error code on failure.
418  */
419 static int
420 ice_devlink_flash_update(struct devlink *devlink,
421                          struct devlink_flash_update_params *params,
422                          struct netlink_ext_ack *extack)
423 {
424         struct ice_pf *pf = devlink_priv(devlink);
425         struct ice_hw *hw = &pf->hw;
426         u8 preservation;
427         int err;
428
429         if (!params->overwrite_mask) {
430                 /* preserve all settings and identifiers */
431                 preservation = ICE_AQC_NVM_PRESERVE_ALL;
432         } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
433                 /* overwrite settings, but preserve the vital device identifiers */
434                 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
435         } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
436                                               DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
437                 /* overwrite both settings and identifiers, preserve nothing */
438                 preservation = ICE_AQC_NVM_NO_PRESERVATION;
439         } else {
440                 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
441                 return -EOPNOTSUPP;
442         }
443
444         if (!hw->dev_caps.common_cap.nvm_unified_update) {
445                 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
446                 return -EOPNOTSUPP;
447         }
448
449         err = ice_check_for_pending_update(pf, NULL, extack);
450         if (err)
451                 return err;
452
453         devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
454
455         return ice_flash_pldm_image(pf, params->fw, preservation, extack);
456 }
457
458 static const struct devlink_ops ice_devlink_ops = {
459         .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
460         .info_get = ice_devlink_info_get,
461         .flash_update = ice_devlink_flash_update,
462 };
463
464 static void ice_devlink_free(void *devlink_ptr)
465 {
466         devlink_free((struct devlink *)devlink_ptr);
467 }
468
469 /**
470  * ice_allocate_pf - Allocate devlink and return PF structure pointer
471  * @dev: the device to allocate for
472  *
473  * Allocate a devlink instance for this device and return the private area as
474  * the PF structure. The devlink memory is kept track of through devres by
475  * adding an action to remove it when unwinding.
476  */
477 struct ice_pf *ice_allocate_pf(struct device *dev)
478 {
479         struct devlink *devlink;
480
481         devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
482         if (!devlink)
483                 return NULL;
484
485         /* Add an action to teardown the devlink when unwinding the driver */
486         if (devm_add_action(dev, ice_devlink_free, devlink)) {
487                 devlink_free(devlink);
488                 return NULL;
489         }
490
491         return devlink_priv(devlink);
492 }
493
494 /**
495  * ice_devlink_register - Register devlink interface for this PF
496  * @pf: the PF to register the devlink for.
497  *
498  * Register the devlink instance associated with this physical function.
499  *
500  * Return: zero on success or an error code on failure.
501  */
502 int ice_devlink_register(struct ice_pf *pf)
503 {
504         struct devlink *devlink = priv_to_devlink(pf);
505         struct device *dev = ice_pf_to_dev(pf);
506         int err;
507
508         err = devlink_register(devlink);
509         if (err) {
510                 dev_err(dev, "devlink registration failed: %d\n", err);
511                 return err;
512         }
513
514         return 0;
515 }
516
517 /**
518  * ice_devlink_unregister - Unregister devlink resources for this PF.
519  * @pf: the PF structure to cleanup
520  *
521  * Releases resources used by devlink and cleans up associated memory.
522  */
523 void ice_devlink_unregister(struct ice_pf *pf)
524 {
525         devlink_unregister(priv_to_devlink(pf));
526 }
527
528 /**
529  * ice_devlink_create_port - Create a devlink port for this VSI
530  * @vsi: the VSI to create a port for
531  *
532  * Create and register a devlink_port for this VSI.
533  *
534  * Return: zero on success or an error code on failure.
535  */
536 int ice_devlink_create_port(struct ice_vsi *vsi)
537 {
538         struct devlink_port_attrs attrs = {};
539         struct ice_port_info *pi;
540         struct devlink *devlink;
541         struct device *dev;
542         struct ice_pf *pf;
543         int err;
544
545         /* Currently we only create devlink_port instances for PF VSIs */
546         if (vsi->type != ICE_VSI_PF)
547                 return -EINVAL;
548
549         pf = vsi->back;
550         devlink = priv_to_devlink(pf);
551         dev = ice_pf_to_dev(pf);
552         pi = pf->hw.port_info;
553
554         attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
555         attrs.phys.port_number = pi->lport;
556         devlink_port_attrs_set(&vsi->devlink_port, &attrs);
557         err = devlink_port_register(devlink, &vsi->devlink_port, vsi->idx);
558         if (err) {
559                 dev_err(dev, "devlink_port_register failed: %d\n", err);
560                 return err;
561         }
562
563         vsi->devlink_port_registered = true;
564
565         return 0;
566 }
567
568 /**
569  * ice_devlink_destroy_port - Destroy the devlink_port for this VSI
570  * @vsi: the VSI to cleanup
571  *
572  * Unregisters the devlink_port structure associated with this VSI.
573  */
574 void ice_devlink_destroy_port(struct ice_vsi *vsi)
575 {
576         if (!vsi->devlink_port_registered)
577                 return;
578
579         devlink_port_type_clear(&vsi->devlink_port);
580         devlink_port_unregister(&vsi->devlink_port);
581
582         vsi->devlink_port_registered = false;
583 }
584
585 /**
586  * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
587  * @devlink: the devlink instance
588  * @ops: the devlink region being snapshotted
589  * @extack: extended ACK response structure
590  * @data: on exit points to snapshot data buffer
591  *
592  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
593  * the shadow-ram devlink region. It captures a snapshot of the shadow ram
594  * contents. This snapshot can later be viewed via the devlink-region
595  * interface.
596  *
597  * @returns zero on success, and updates the data pointer. Returns a non-zero
598  * error code on failure.
599  */
600 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
601                                     const struct devlink_region_ops *ops,
602                                     struct netlink_ext_ack *extack, u8 **data)
603 {
604         struct ice_pf *pf = devlink_priv(devlink);
605         struct device *dev = ice_pf_to_dev(pf);
606         struct ice_hw *hw = &pf->hw;
607         enum ice_status status;
608         void *nvm_data;
609         u32 nvm_size;
610
611         nvm_size = hw->flash.flash_size;
612         nvm_data = vzalloc(nvm_size);
613         if (!nvm_data)
614                 return -ENOMEM;
615
616         status = ice_acquire_nvm(hw, ICE_RES_READ);
617         if (status) {
618                 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
619                         status, hw->adminq.sq_last_status);
620                 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
621                 vfree(nvm_data);
622                 return -EIO;
623         }
624
625         status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
626         if (status) {
627                 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
628                         nvm_size, status, hw->adminq.sq_last_status);
629                 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
630                 ice_release_nvm(hw);
631                 vfree(nvm_data);
632                 return -EIO;
633         }
634
635         ice_release_nvm(hw);
636
637         *data = nvm_data;
638
639         return 0;
640 }
641
642 /**
643  * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
644  * @devlink: the devlink instance
645  * @ops: the devlink region being snapshotted
646  * @extack: extended ACK response structure
647  * @data: on exit points to snapshot data buffer
648  *
649  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
650  * the device-caps devlink region. It captures a snapshot of the device
651  * capabilities reported by firmware.
652  *
653  * @returns zero on success, and updates the data pointer. Returns a non-zero
654  * error code on failure.
655  */
656 static int
657 ice_devlink_devcaps_snapshot(struct devlink *devlink,
658                              const struct devlink_region_ops *ops,
659                              struct netlink_ext_ack *extack, u8 **data)
660 {
661         struct ice_pf *pf = devlink_priv(devlink);
662         struct device *dev = ice_pf_to_dev(pf);
663         struct ice_hw *hw = &pf->hw;
664         enum ice_status status;
665         void *devcaps;
666
667         devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
668         if (!devcaps)
669                 return -ENOMEM;
670
671         status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
672                                   ice_aqc_opc_list_dev_caps, NULL);
673         if (status) {
674                 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
675                         status, hw->adminq.sq_last_status);
676                 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
677                 vfree(devcaps);
678                 return -EIO;
679         }
680
681         *data = (u8 *)devcaps;
682
683         return 0;
684 }
685
686 static const struct devlink_region_ops ice_nvm_region_ops = {
687         .name = "nvm-flash",
688         .destructor = vfree,
689         .snapshot = ice_devlink_nvm_snapshot,
690 };
691
692 static const struct devlink_region_ops ice_devcaps_region_ops = {
693         .name = "device-caps",
694         .destructor = vfree,
695         .snapshot = ice_devlink_devcaps_snapshot,
696 };
697
698 /**
699  * ice_devlink_init_regions - Initialize devlink regions
700  * @pf: the PF device structure
701  *
702  * Create devlink regions used to enable access to dump the contents of the
703  * flash memory on the device.
704  */
705 void ice_devlink_init_regions(struct ice_pf *pf)
706 {
707         struct devlink *devlink = priv_to_devlink(pf);
708         struct device *dev = ice_pf_to_dev(pf);
709         u64 nvm_size;
710
711         nvm_size = pf->hw.flash.flash_size;
712         pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
713                                                nvm_size);
714         if (IS_ERR(pf->nvm_region)) {
715                 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
716                         PTR_ERR(pf->nvm_region));
717                 pf->nvm_region = NULL;
718         }
719
720         pf->devcaps_region = devlink_region_create(devlink,
721                                                    &ice_devcaps_region_ops, 10,
722                                                    ICE_AQ_MAX_BUF_LEN);
723         if (IS_ERR(pf->devcaps_region)) {
724                 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
725                         PTR_ERR(pf->devcaps_region));
726                 pf->devcaps_region = NULL;
727         }
728 }
729
730 /**
731  * ice_devlink_destroy_regions - Destroy devlink regions
732  * @pf: the PF device structure
733  *
734  * Remove previously created regions for this PF.
735  */
736 void ice_devlink_destroy_regions(struct ice_pf *pf)
737 {
738         if (pf->nvm_region)
739                 devlink_region_destroy(pf->nvm_region);
740         if (pf->devcaps_region)
741                 devlink_region_destroy(pf->devcaps_region);
742 }