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