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