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 9static void ice_info_get_dsn(struct ice_pf *pf, char *buf, size_t len) 10{ 11 u8 dsn[8]; 12 13 /* Copy the DSN into an array in Big Endian format */ 14 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn); 15 16 snprintf(buf, len, "%8phD", dsn); 17} 18 19static int ice_info_pba(struct ice_pf *pf, char *buf, size_t len) 20{ 21 struct ice_hw *hw = &pf->hw; 22 enum ice_status status; 23 24 status = ice_read_pba_string(hw, (u8 *)buf, len); 25 if (status) 26 /* We failed to locate the PBA, so just skip this entry */ 27 dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %s\n", 28 ice_stat_str(status)); 29 30 return 0; 31} 32 33static int ice_info_fw_mgmt(struct ice_pf *pf, char *buf, size_t len) 34{ 35 struct ice_hw *hw = &pf->hw; 36 37 snprintf(buf, len, "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver, 38 hw->fw_patch); 39 40 return 0; 41} 42 43static int ice_info_fw_api(struct ice_pf *pf, char *buf, size_t len) 44{ 45 struct ice_hw *hw = &pf->hw; 46 47 snprintf(buf, len, "%u.%u", hw->api_maj_ver, hw->api_min_ver); 48 49 return 0; 50} 51 52static int ice_info_fw_build(struct ice_pf *pf, char *buf, size_t len) 53{ 54 struct ice_hw *hw = &pf->hw; 55 56 snprintf(buf, len, "0x%08x", hw->fw_build); 57 58 return 0; 59} 60 61static int ice_info_orom_ver(struct ice_pf *pf, char *buf, size_t len) 62{ 63 struct ice_orom_info *orom = &pf->hw.nvm.orom; 64 65 snprintf(buf, len, "%u.%u.%u", orom->major, orom->build, orom->patch); 66 67 return 0; 68} 69 70static int ice_info_nvm_ver(struct ice_pf *pf, char *buf, size_t len) 71{ 72 struct ice_nvm_info *nvm = &pf->hw.nvm; 73 74 snprintf(buf, len, "%x.%02x", nvm->major_ver, nvm->minor_ver); 75 76 return 0; 77} 78 79static int ice_info_eetrack(struct ice_pf *pf, char *buf, size_t len) 80{ 81 struct ice_nvm_info *nvm = &pf->hw.nvm; 82 83 snprintf(buf, len, "0x%08x", nvm->eetrack); 84 85 return 0; 86} 87 88static int ice_info_ddp_pkg_name(struct ice_pf *pf, char *buf, size_t len) 89{ 90 struct ice_hw *hw = &pf->hw; 91 92 snprintf(buf, len, "%s", hw->active_pkg_name); 93 94 return 0; 95} 96 97static int ice_info_ddp_pkg_version(struct ice_pf *pf, char *buf, size_t len) 98{ 99 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver; 100 101 snprintf(buf, len, "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update, 102 pkg->draft); 103 104 return 0; 105} 106 107static int ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, char *buf, size_t len) 108{ 109 snprintf(buf, len, "0x%08x", pf->hw.active_track_id); 110 111 return 0; 112} 113 114static int ice_info_netlist_ver(struct ice_pf *pf, char *buf, size_t len) 115{ 116 struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver; 117 118 /* The netlist version fields are BCD formatted */ 119 snprintf(buf, len, "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor, 120 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev, 121 netlist->cust_ver); 122 123 return 0; 124} 125 126static int ice_info_netlist_build(struct ice_pf *pf, char *buf, size_t len) 127{ 128 struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver; 129 130 snprintf(buf, len, "0x%08x", netlist->hash); 131 132 return 0; 133} 134 135#define fixed(key, getter) { ICE_VERSION_FIXED, key, getter } 136#define running(key, getter) { ICE_VERSION_RUNNING, key, getter } 137 138enum ice_version_type { 139 ICE_VERSION_FIXED, 140 ICE_VERSION_RUNNING, 141 ICE_VERSION_STORED, 142}; 143 144static const struct ice_devlink_version { 145 enum ice_version_type type; 146 const char *key; 147 int (*getter)(struct ice_pf *pf, char *buf, size_t len); 148} ice_devlink_versions[] = { 149 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba), 150 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt), 151 running("fw.mgmt.api", ice_info_fw_api), 152 running("fw.mgmt.build", ice_info_fw_build), 153 running(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver), 154 running("fw.psid.api", ice_info_nvm_ver), 155 running(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack), 156 running("fw.app.name", ice_info_ddp_pkg_name), 157 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version), 158 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id), 159 running("fw.netlist", ice_info_netlist_ver), 160 running("fw.netlist.build", ice_info_netlist_build), 161}; 162 163/** 164 * ice_devlink_info_get - .info_get devlink handler 165 * @devlink: devlink instance structure 166 * @req: the devlink info request 167 * @extack: extended netdev ack structure 168 * 169 * Callback for the devlink .info_get operation. Reports information about the 170 * device. 171 * 172 * Return: zero on success or an error code on failure. 173 */ 174static int ice_devlink_info_get(struct devlink *devlink, 175 struct devlink_info_req *req, 176 struct netlink_ext_ack *extack) 177{ 178 struct ice_pf *pf = devlink_priv(devlink); 179 char buf[100]; 180 size_t i; 181 int err; 182 183 err = devlink_info_driver_name_put(req, KBUILD_MODNAME); 184 if (err) { 185 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name"); 186 return err; 187 } 188 189 ice_info_get_dsn(pf, buf, sizeof(buf)); 190 191 err = devlink_info_serial_number_put(req, buf); 192 if (err) { 193 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number"); 194 return err; 195 } 196 197 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) { 198 enum ice_version_type type = ice_devlink_versions[i].type; 199 const char *key = ice_devlink_versions[i].key; 200 201 err = ice_devlink_versions[i].getter(pf, buf, sizeof(buf)); 202 if (err) { 203 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info"); 204 return err; 205 } 206 207 switch (type) { 208 case ICE_VERSION_FIXED: 209 err = devlink_info_version_fixed_put(req, key, buf); 210 if (err) { 211 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version"); 212 return err; 213 } 214 break; 215 case ICE_VERSION_RUNNING: 216 err = devlink_info_version_running_put(req, key, buf); 217 if (err) { 218 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version"); 219 return err; 220 } 221 break; 222 case ICE_VERSION_STORED: 223 err = devlink_info_version_stored_put(req, key, buf); 224 if (err) { 225 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version"); 226 return err; 227 } 228 break; 229 } 230 } 231 232 return 0; 233} 234 235/** 236 * ice_devlink_flash_update - Update firmware stored in flash on the device 237 * @devlink: pointer to devlink associated with device to update 238 * @params: flash update parameters 239 * @extack: netlink extended ACK structure 240 * 241 * Perform a device flash update. The bulk of the update logic is contained 242 * within the ice_flash_pldm_image function. 243 * 244 * Returns: zero on success, or an error code on failure. 245 */ 246static int 247ice_devlink_flash_update(struct devlink *devlink, 248 struct devlink_flash_update_params *params, 249 struct netlink_ext_ack *extack) 250{ 251 struct ice_pf *pf = devlink_priv(devlink); 252 struct device *dev = &pf->pdev->dev; 253 struct ice_hw *hw = &pf->hw; 254 const struct firmware *fw; 255 u8 preservation; 256 int err; 257 258 if (!params->overwrite_mask) { 259 /* preserve all settings and identifiers */ 260 preservation = ICE_AQC_NVM_PRESERVE_ALL; 261 } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) { 262 /* overwrite settings, but preserve the vital device identifiers */ 263 preservation = ICE_AQC_NVM_PRESERVE_SELECTED; 264 } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS | 265 DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) { 266 /* overwrite both settings and identifiers, preserve nothing */ 267 preservation = ICE_AQC_NVM_NO_PRESERVATION; 268 } else { 269 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported"); 270 return -EOPNOTSUPP; 271 } 272 273 if (!hw->dev_caps.common_cap.nvm_unified_update) { 274 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update"); 275 return -EOPNOTSUPP; 276 } 277 278 err = ice_check_for_pending_update(pf, NULL, extack); 279 if (err) 280 return err; 281 282 err = request_firmware(&fw, params->file_name, dev); 283 if (err) { 284 NL_SET_ERR_MSG_MOD(extack, "Unable to read file from disk"); 285 return err; 286 } 287 288 dev_dbg(dev, "Beginning flash update with file '%s'\n", params->file_name); 289 290 devlink_flash_update_begin_notify(devlink); 291 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0); 292 err = ice_flash_pldm_image(pf, fw, preservation, extack); 293 devlink_flash_update_end_notify(devlink); 294 295 release_firmware(fw); 296 297 return err; 298} 299 300static const struct devlink_ops ice_devlink_ops = { 301 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK, 302 .info_get = ice_devlink_info_get, 303 .flash_update = ice_devlink_flash_update, 304}; 305 306static void ice_devlink_free(void *devlink_ptr) 307{ 308 devlink_free((struct devlink *)devlink_ptr); 309} 310 311/** 312 * ice_allocate_pf - Allocate devlink and return PF structure pointer 313 * @dev: the device to allocate for 314 * 315 * Allocate a devlink instance for this device and return the private area as 316 * the PF structure. The devlink memory is kept track of through devres by 317 * adding an action to remove it when unwinding. 318 */ 319struct ice_pf *ice_allocate_pf(struct device *dev) 320{ 321 struct devlink *devlink; 322 323 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf)); 324 if (!devlink) 325 return NULL; 326 327 /* Add an action to teardown the devlink when unwinding the driver */ 328 if (devm_add_action(dev, ice_devlink_free, devlink)) { 329 devlink_free(devlink); 330 return NULL; 331 } 332 333 return devlink_priv(devlink); 334} 335 336/** 337 * ice_devlink_register - Register devlink interface for this PF 338 * @pf: the PF to register the devlink for. 339 * 340 * Register the devlink instance associated with this physical function. 341 * 342 * Return: zero on success or an error code on failure. 343 */ 344int ice_devlink_register(struct ice_pf *pf) 345{ 346 struct devlink *devlink = priv_to_devlink(pf); 347 struct device *dev = ice_pf_to_dev(pf); 348 int err; 349 350 err = devlink_register(devlink, dev); 351 if (err) { 352 dev_err(dev, "devlink registration failed: %d\n", err); 353 return err; 354 } 355 356 return 0; 357} 358 359/** 360 * ice_devlink_unregister - Unregister devlink resources for this PF. 361 * @pf: the PF structure to cleanup 362 * 363 * Releases resources used by devlink and cleans up associated memory. 364 */ 365void ice_devlink_unregister(struct ice_pf *pf) 366{ 367 devlink_unregister(priv_to_devlink(pf)); 368} 369 370/** 371 * ice_devlink_create_port - Create a devlink port for this VSI 372 * @vsi: the VSI to create a port for 373 * 374 * Create and register a devlink_port for this VSI. 375 * 376 * Return: zero on success or an error code on failure. 377 */ 378int ice_devlink_create_port(struct ice_vsi *vsi) 379{ 380 struct devlink_port_attrs attrs = {}; 381 struct ice_port_info *pi; 382 struct devlink *devlink; 383 struct device *dev; 384 struct ice_pf *pf; 385 int err; 386 387 /* Currently we only create devlink_port instances for PF VSIs */ 388 if (vsi->type != ICE_VSI_PF) 389 return -EINVAL; 390 391 pf = vsi->back; 392 devlink = priv_to_devlink(pf); 393 dev = ice_pf_to_dev(pf); 394 pi = pf->hw.port_info; 395 396 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; 397 attrs.phys.port_number = pi->lport; 398 devlink_port_attrs_set(&vsi->devlink_port, &attrs); 399 err = devlink_port_register(devlink, &vsi->devlink_port, vsi->idx); 400 if (err) { 401 dev_err(dev, "devlink_port_register failed: %d\n", err); 402 return err; 403 } 404 405 vsi->devlink_port_registered = true; 406 407 return 0; 408} 409 410/** 411 * ice_devlink_destroy_port - Destroy the devlink_port for this VSI 412 * @vsi: the VSI to cleanup 413 * 414 * Unregisters the devlink_port structure associated with this VSI. 415 */ 416void ice_devlink_destroy_port(struct ice_vsi *vsi) 417{ 418 if (!vsi->devlink_port_registered) 419 return; 420 421 devlink_port_type_clear(&vsi->devlink_port); 422 devlink_port_unregister(&vsi->devlink_port); 423 424 vsi->devlink_port_registered = false; 425} 426 427/** 428 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents 429 * @devlink: the devlink instance 430 * @ops: the devlink region being snapshotted 431 * @extack: extended ACK response structure 432 * @data: on exit points to snapshot data buffer 433 * 434 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for 435 * the shadow-ram devlink region. It captures a snapshot of the shadow ram 436 * contents. This snapshot can later be viewed via the devlink-region 437 * interface. 438 * 439 * @returns zero on success, and updates the data pointer. Returns a non-zero 440 * error code on failure. 441 */ 442static int ice_devlink_nvm_snapshot(struct devlink *devlink, 443 const struct devlink_region_ops *ops, 444 struct netlink_ext_ack *extack, u8 **data) 445{ 446 struct ice_pf *pf = devlink_priv(devlink); 447 struct device *dev = ice_pf_to_dev(pf); 448 struct ice_hw *hw = &pf->hw; 449 enum ice_status status; 450 void *nvm_data; 451 u32 nvm_size; 452 453 nvm_size = hw->nvm.flash_size; 454 nvm_data = vzalloc(nvm_size); 455 if (!nvm_data) 456 return -ENOMEM; 457 458 status = ice_acquire_nvm(hw, ICE_RES_READ); 459 if (status) { 460 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n", 461 status, hw->adminq.sq_last_status); 462 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore"); 463 vfree(nvm_data); 464 return -EIO; 465 } 466 467 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false); 468 if (status) { 469 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n", 470 nvm_size, status, hw->adminq.sq_last_status); 471 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents"); 472 ice_release_nvm(hw); 473 vfree(nvm_data); 474 return -EIO; 475 } 476 477 ice_release_nvm(hw); 478 479 *data = nvm_data; 480 481 return 0; 482} 483 484/** 485 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities 486 * @devlink: the devlink instance 487 * @ops: the devlink region being snapshotted 488 * @extack: extended ACK response structure 489 * @data: on exit points to snapshot data buffer 490 * 491 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for 492 * the device-caps devlink region. It captures a snapshot of the device 493 * capabilities reported by firmware. 494 * 495 * @returns zero on success, and updates the data pointer. Returns a non-zero 496 * error code on failure. 497 */ 498static int 499ice_devlink_devcaps_snapshot(struct devlink *devlink, 500 const struct devlink_region_ops *ops, 501 struct netlink_ext_ack *extack, u8 **data) 502{ 503 struct ice_pf *pf = devlink_priv(devlink); 504 struct device *dev = ice_pf_to_dev(pf); 505 struct ice_hw *hw = &pf->hw; 506 enum ice_status status; 507 void *devcaps; 508 509 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN); 510 if (!devcaps) 511 return -ENOMEM; 512 513 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL, 514 ice_aqc_opc_list_dev_caps, NULL); 515 if (status) { 516 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n", 517 status, hw->adminq.sq_last_status); 518 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities"); 519 vfree(devcaps); 520 return -EIO; 521 } 522 523 *data = (u8 *)devcaps; 524 525 return 0; 526} 527 528static const struct devlink_region_ops ice_nvm_region_ops = { 529 .name = "nvm-flash", 530 .destructor = vfree, 531 .snapshot = ice_devlink_nvm_snapshot, 532}; 533 534static const struct devlink_region_ops ice_devcaps_region_ops = { 535 .name = "device-caps", 536 .destructor = vfree, 537 .snapshot = ice_devlink_devcaps_snapshot, 538}; 539 540/** 541 * ice_devlink_init_regions - Initialize devlink regions 542 * @pf: the PF device structure 543 * 544 * Create devlink regions used to enable access to dump the contents of the 545 * flash memory on the device. 546 */ 547void ice_devlink_init_regions(struct ice_pf *pf) 548{ 549 struct devlink *devlink = priv_to_devlink(pf); 550 struct device *dev = ice_pf_to_dev(pf); 551 u64 nvm_size; 552 553 nvm_size = pf->hw.nvm.flash_size; 554 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1, 555 nvm_size); 556 if (IS_ERR(pf->nvm_region)) { 557 dev_err(dev, "failed to create NVM devlink region, err %ld\n", 558 PTR_ERR(pf->nvm_region)); 559 pf->nvm_region = NULL; 560 } 561 562 pf->devcaps_region = devlink_region_create(devlink, 563 &ice_devcaps_region_ops, 10, 564 ICE_AQ_MAX_BUF_LEN); 565 if (IS_ERR(pf->devcaps_region)) { 566 dev_err(dev, "failed to create device-caps devlink region, err %ld\n", 567 PTR_ERR(pf->devcaps_region)); 568 pf->devcaps_region = NULL; 569 } 570} 571 572/** 573 * ice_devlink_destroy_regions - Destroy devlink regions 574 * @pf: the PF device structure 575 * 576 * Remove previously created regions for this PF. 577 */ 578void ice_devlink_destroy_regions(struct ice_pf *pf) 579{ 580 if (pf->nvm_region) 581 devlink_region_destroy(pf->nvm_region); 582 if (pf->devcaps_region) 583 devlink_region_destroy(pf->devcaps_region); 584} 585