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", 64 hw->api_maj_ver, hw->api_min_ver); 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(dev, ice_devlink_free, devlink)) { 456 devlink_free(devlink); 457 return NULL; 458 } 459 460 return devlink_priv(devlink); 461 } 462 463 /** 464 * ice_devlink_register - Register devlink interface for this PF 465 * @pf: the PF to register the devlink for. 466 * 467 * Register the devlink instance associated with this physical function. 468 * 469 * Return: zero on success or an error code on failure. 470 */ 471 void ice_devlink_register(struct ice_pf *pf) 472 { 473 struct devlink *devlink = priv_to_devlink(pf); 474 475 devlink_register(devlink); 476 } 477 478 /** 479 * ice_devlink_unregister - Unregister devlink resources for this PF. 480 * @pf: the PF structure to cleanup 481 * 482 * Releases resources used by devlink and cleans up associated memory. 483 */ 484 void ice_devlink_unregister(struct ice_pf *pf) 485 { 486 devlink_unregister(priv_to_devlink(pf)); 487 } 488 489 /** 490 * ice_devlink_create_pf_port - Create a devlink port for this PF 491 * @pf: the PF to create a devlink port for 492 * 493 * Create and register a devlink_port for this PF. 494 * 495 * Return: zero on success or an error code on failure. 496 */ 497 int ice_devlink_create_pf_port(struct ice_pf *pf) 498 { 499 struct devlink_port_attrs attrs = {}; 500 struct devlink_port *devlink_port; 501 struct devlink *devlink; 502 struct ice_vsi *vsi; 503 struct device *dev; 504 int err; 505 506 dev = ice_pf_to_dev(pf); 507 508 devlink_port = &pf->devlink_port; 509 510 vsi = ice_get_main_vsi(pf); 511 if (!vsi) 512 return -EIO; 513 514 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; 515 attrs.phys.port_number = pf->hw.bus.func; 516 devlink_port_attrs_set(devlink_port, &attrs); 517 devlink = priv_to_devlink(pf); 518 519 err = devlink_port_register(devlink, devlink_port, vsi->idx); 520 if (err) { 521 dev_err(dev, "Failed to create devlink port for PF %d, error %d\n", 522 pf->hw.pf_id, err); 523 return err; 524 } 525 526 return 0; 527 } 528 529 /** 530 * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF 531 * @pf: the PF to cleanup 532 * 533 * Unregisters the devlink_port structure associated with this PF. 534 */ 535 void ice_devlink_destroy_pf_port(struct ice_pf *pf) 536 { 537 struct devlink_port *devlink_port; 538 539 devlink_port = &pf->devlink_port; 540 541 devlink_port_type_clear(devlink_port); 542 devlink_port_unregister(devlink_port); 543 } 544 545 /** 546 * ice_devlink_create_vf_port - Create a devlink port for this VF 547 * @vf: the VF to create a port for 548 * 549 * Create and register a devlink_port for this VF. 550 * 551 * Return: zero on success or an error code on failure. 552 */ 553 int ice_devlink_create_vf_port(struct ice_vf *vf) 554 { 555 struct devlink_port_attrs attrs = {}; 556 struct devlink_port *devlink_port; 557 struct devlink *devlink; 558 struct ice_vsi *vsi; 559 struct device *dev; 560 struct ice_pf *pf; 561 int err; 562 563 pf = vf->pf; 564 dev = ice_pf_to_dev(pf); 565 vsi = ice_get_vf_vsi(vf); 566 devlink_port = &vf->devlink_port; 567 568 attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF; 569 attrs.pci_vf.pf = pf->hw.bus.func; 570 attrs.pci_vf.vf = vf->vf_id; 571 572 devlink_port_attrs_set(devlink_port, &attrs); 573 devlink = priv_to_devlink(pf); 574 575 err = devlink_port_register(devlink, devlink_port, vsi->idx); 576 if (err) { 577 dev_err(dev, "Failed to create devlink port for VF %d, error %d\n", 578 vf->vf_id, err); 579 return err; 580 } 581 582 return 0; 583 } 584 585 /** 586 * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF 587 * @vf: the VF to cleanup 588 * 589 * Unregisters the devlink_port structure associated with this VF. 590 */ 591 void ice_devlink_destroy_vf_port(struct ice_vf *vf) 592 { 593 struct devlink_port *devlink_port; 594 595 devlink_port = &vf->devlink_port; 596 597 devlink_port_type_clear(devlink_port); 598 devlink_port_unregister(devlink_port); 599 } 600 601 /** 602 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents 603 * @devlink: the devlink instance 604 * @ops: the devlink region being snapshotted 605 * @extack: extended ACK response structure 606 * @data: on exit points to snapshot data buffer 607 * 608 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for 609 * the shadow-ram devlink region. It captures a snapshot of the shadow ram 610 * contents. This snapshot can later be viewed via the devlink-region 611 * interface. 612 * 613 * @returns zero on success, and updates the data pointer. Returns a non-zero 614 * error code on failure. 615 */ 616 static int ice_devlink_nvm_snapshot(struct devlink *devlink, 617 const struct devlink_region_ops *ops, 618 struct netlink_ext_ack *extack, u8 **data) 619 { 620 struct ice_pf *pf = devlink_priv(devlink); 621 struct device *dev = ice_pf_to_dev(pf); 622 struct ice_hw *hw = &pf->hw; 623 enum ice_status status; 624 void *nvm_data; 625 u32 nvm_size; 626 627 nvm_size = hw->flash.flash_size; 628 nvm_data = vzalloc(nvm_size); 629 if (!nvm_data) 630 return -ENOMEM; 631 632 status = ice_acquire_nvm(hw, ICE_RES_READ); 633 if (status) { 634 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n", 635 status, hw->adminq.sq_last_status); 636 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore"); 637 vfree(nvm_data); 638 return -EIO; 639 } 640 641 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false); 642 if (status) { 643 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n", 644 nvm_size, status, hw->adminq.sq_last_status); 645 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents"); 646 ice_release_nvm(hw); 647 vfree(nvm_data); 648 return -EIO; 649 } 650 651 ice_release_nvm(hw); 652 653 *data = nvm_data; 654 655 return 0; 656 } 657 658 /** 659 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities 660 * @devlink: the devlink instance 661 * @ops: the devlink region being snapshotted 662 * @extack: extended ACK response structure 663 * @data: on exit points to snapshot data buffer 664 * 665 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for 666 * the device-caps devlink region. It captures a snapshot of the device 667 * capabilities reported by firmware. 668 * 669 * @returns zero on success, and updates the data pointer. Returns a non-zero 670 * error code on failure. 671 */ 672 static int 673 ice_devlink_devcaps_snapshot(struct devlink *devlink, 674 const struct devlink_region_ops *ops, 675 struct netlink_ext_ack *extack, u8 **data) 676 { 677 struct ice_pf *pf = devlink_priv(devlink); 678 struct device *dev = ice_pf_to_dev(pf); 679 struct ice_hw *hw = &pf->hw; 680 enum ice_status status; 681 void *devcaps; 682 683 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN); 684 if (!devcaps) 685 return -ENOMEM; 686 687 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL, 688 ice_aqc_opc_list_dev_caps, NULL); 689 if (status) { 690 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n", 691 status, hw->adminq.sq_last_status); 692 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities"); 693 vfree(devcaps); 694 return -EIO; 695 } 696 697 *data = (u8 *)devcaps; 698 699 return 0; 700 } 701 702 static const struct devlink_region_ops ice_nvm_region_ops = { 703 .name = "nvm-flash", 704 .destructor = vfree, 705 .snapshot = ice_devlink_nvm_snapshot, 706 }; 707 708 static const struct devlink_region_ops ice_devcaps_region_ops = { 709 .name = "device-caps", 710 .destructor = vfree, 711 .snapshot = ice_devlink_devcaps_snapshot, 712 }; 713 714 /** 715 * ice_devlink_init_regions - Initialize devlink regions 716 * @pf: the PF device structure 717 * 718 * Create devlink regions used to enable access to dump the contents of the 719 * flash memory on the device. 720 */ 721 void ice_devlink_init_regions(struct ice_pf *pf) 722 { 723 struct devlink *devlink = priv_to_devlink(pf); 724 struct device *dev = ice_pf_to_dev(pf); 725 u64 nvm_size; 726 727 nvm_size = pf->hw.flash.flash_size; 728 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1, 729 nvm_size); 730 if (IS_ERR(pf->nvm_region)) { 731 dev_err(dev, "failed to create NVM devlink region, err %ld\n", 732 PTR_ERR(pf->nvm_region)); 733 pf->nvm_region = NULL; 734 } 735 736 pf->devcaps_region = devlink_region_create(devlink, 737 &ice_devcaps_region_ops, 10, 738 ICE_AQ_MAX_BUF_LEN); 739 if (IS_ERR(pf->devcaps_region)) { 740 dev_err(dev, "failed to create device-caps devlink region, err %ld\n", 741 PTR_ERR(pf->devcaps_region)); 742 pf->devcaps_region = NULL; 743 } 744 } 745 746 /** 747 * ice_devlink_destroy_regions - Destroy devlink regions 748 * @pf: the PF device structure 749 * 750 * Remove previously created regions for this PF. 751 */ 752 void ice_devlink_destroy_regions(struct ice_pf *pf) 753 { 754 if (pf->nvm_region) 755 devlink_region_destroy(pf->nvm_region); 756 if (pf->devcaps_region) 757 devlink_region_destroy(pf->devcaps_region); 758 } 759