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