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