1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
6 #include "ice_devlink.h"
7 #include "ice_fw_update.h"
9 /* context for devlink info version reporting */
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;
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
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.
28 static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
32 /* Copy the DSN into an array in Big Endian format */
33 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
35 snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
38 static int ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
40 struct ice_hw *hw = &pf->hw;
41 enum ice_status status;
43 status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
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));
52 static int ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
54 struct ice_hw *hw = &pf->hw;
56 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver,
62 static int ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
64 struct ice_hw *hw = &pf->hw;
66 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
67 hw->api_min_ver, hw->api_patch);
72 static int ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
74 struct ice_hw *hw = &pf->hw;
76 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
81 static int ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
83 struct ice_orom_info *orom = &pf->hw.flash.orom;
85 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", orom->major, orom->build, orom->patch);
91 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
93 struct ice_orom_info *orom = &ctx->pending_orom;
95 if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
96 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
97 orom->major, orom->build, orom->patch);
102 static int ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
104 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
106 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
112 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
114 struct ice_nvm_info *nvm = &ctx->pending_nvm;
116 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
117 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
122 static int ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
124 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
126 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
132 ice_info_pending_eetrack(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
134 struct ice_nvm_info *nvm = &ctx->pending_nvm;
136 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
137 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
142 static int ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
144 struct ice_hw *hw = &pf->hw;
146 snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
151 static int ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
153 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
155 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
161 static int ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
163 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
168 static int ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
170 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
172 /* The netlist version fields are BCD formatted */
173 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
174 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
180 static int ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
182 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
184 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
190 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
192 struct ice_netlist_info *netlist = &ctx->pending_netlist;
194 /* The netlist version fields are BCD formatted */
195 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
196 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
197 netlist->major, netlist->minor,
198 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
205 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
207 struct ice_netlist_info *netlist = &ctx->pending_netlist;
209 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
210 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
215 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
216 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
217 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
219 /* The combined() macro inserts both the running entry as well as a stored
220 * entry. The running entry will always report the version from the active
221 * handler. The stored entry will first try the pending handler, and fallback
222 * to the active handler if the pending function does not report a version.
223 * The pending handler should check the status of a pending update for the
224 * relevant flash component. It should only fill in the buffer in the case
225 * where a valid pending version is available. This ensures that the related
226 * stored and running versions remain in sync, and that stored versions are
227 * correctly reported as expected.
229 #define combined(key, active, pending) \
230 running(key, active), \
231 stored(key, pending, active)
233 enum ice_version_type {
239 static const struct ice_devlink_version {
240 enum ice_version_type type;
242 int (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
243 int (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
244 } ice_devlink_versions[] = {
245 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
246 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
247 running("fw.mgmt.api", ice_info_fw_api),
248 running("fw.mgmt.build", ice_info_fw_build),
249 combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
250 combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
251 combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
252 running("fw.app.name", ice_info_ddp_pkg_name),
253 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
254 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
255 combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
256 combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
260 * ice_devlink_info_get - .info_get devlink handler
261 * @devlink: devlink instance structure
262 * @req: the devlink info request
263 * @extack: extended netdev ack structure
265 * Callback for the devlink .info_get operation. Reports information about the
268 * Return: zero on success or an error code on failure.
270 static int ice_devlink_info_get(struct devlink *devlink,
271 struct devlink_info_req *req,
272 struct netlink_ext_ack *extack)
274 struct ice_pf *pf = devlink_priv(devlink);
275 struct device *dev = ice_pf_to_dev(pf);
276 struct ice_hw *hw = &pf->hw;
277 struct ice_info_ctx *ctx;
278 enum ice_status status;
282 err = ice_wait_for_reset(pf, 10 * HZ);
284 NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
288 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
292 /* discover capabilities first */
293 status = ice_discover_dev_caps(hw, &ctx->dev_caps);
295 dev_dbg(dev, "Failed to discover device capabilities, status %s aq_err %s\n",
296 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
297 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
302 if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
303 status = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
305 dev_dbg(dev, "Unable to read inactive Option ROM version data, status %s aq_err %s\n",
306 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
308 /* disable display of pending Option ROM */
309 ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
313 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
314 status = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
316 dev_dbg(dev, "Unable to read inactive NVM version data, status %s aq_err %s\n",
317 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
319 /* disable display of pending Option ROM */
320 ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
324 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
325 status = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
327 dev_dbg(dev, "Unable to read inactive Netlist version data, status %s aq_err %s\n",
328 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
330 /* disable display of pending Option ROM */
331 ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
335 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
337 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
341 ice_info_get_dsn(pf, ctx);
343 err = devlink_info_serial_number_put(req, ctx->buf);
345 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
349 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
350 enum ice_version_type type = ice_devlink_versions[i].type;
351 const char *key = ice_devlink_versions[i].key;
353 memset(ctx->buf, 0, sizeof(ctx->buf));
355 err = ice_devlink_versions[i].getter(pf, ctx);
357 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
361 /* If the default getter doesn't report a version, use the
362 * fallback function. This is primarily useful in the case of
363 * "stored" versions that want to report the same value as the
364 * running version in the normal case of no pending update.
366 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback) {
367 err = ice_devlink_versions[i].fallback(pf, ctx);
369 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
374 /* Do not report missing versions */
375 if (ctx->buf[0] == '\0')
379 case ICE_VERSION_FIXED:
380 err = devlink_info_version_fixed_put(req, key, ctx->buf);
382 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
386 case ICE_VERSION_RUNNING:
387 err = devlink_info_version_running_put(req, key, ctx->buf);
389 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
393 case ICE_VERSION_STORED:
394 err = devlink_info_version_stored_put(req, key, ctx->buf);
396 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
409 * ice_devlink_flash_update - Update firmware stored in flash on the device
410 * @devlink: pointer to devlink associated with device to update
411 * @params: flash update parameters
412 * @extack: netlink extended ACK structure
414 * Perform a device flash update. The bulk of the update logic is contained
415 * within the ice_flash_pldm_image function.
417 * Returns: zero on success, or an error code on failure.
420 ice_devlink_flash_update(struct devlink *devlink,
421 struct devlink_flash_update_params *params,
422 struct netlink_ext_ack *extack)
424 struct ice_pf *pf = devlink_priv(devlink);
425 struct ice_hw *hw = &pf->hw;
429 if (!params->overwrite_mask) {
430 /* preserve all settings and identifiers */
431 preservation = ICE_AQC_NVM_PRESERVE_ALL;
432 } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
433 /* overwrite settings, but preserve the vital device identifiers */
434 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
435 } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
436 DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
437 /* overwrite both settings and identifiers, preserve nothing */
438 preservation = ICE_AQC_NVM_NO_PRESERVATION;
440 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
444 if (!hw->dev_caps.common_cap.nvm_unified_update) {
445 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
449 err = ice_check_for_pending_update(pf, NULL, extack);
453 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
455 return ice_flash_pldm_image(pf, params->fw, preservation, extack);
458 static const struct devlink_ops ice_devlink_ops = {
459 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
460 .info_get = ice_devlink_info_get,
461 .flash_update = ice_devlink_flash_update,
464 static void ice_devlink_free(void *devlink_ptr)
466 devlink_free((struct devlink *)devlink_ptr);
470 * ice_allocate_pf - Allocate devlink and return PF structure pointer
471 * @dev: the device to allocate for
473 * Allocate a devlink instance for this device and return the private area as
474 * the PF structure. The devlink memory is kept track of through devres by
475 * adding an action to remove it when unwinding.
477 struct ice_pf *ice_allocate_pf(struct device *dev)
479 struct devlink *devlink;
481 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
485 /* Add an action to teardown the devlink when unwinding the driver */
486 if (devm_add_action(dev, ice_devlink_free, devlink)) {
487 devlink_free(devlink);
491 return devlink_priv(devlink);
495 * ice_devlink_register - Register devlink interface for this PF
496 * @pf: the PF to register the devlink for.
498 * Register the devlink instance associated with this physical function.
500 * Return: zero on success or an error code on failure.
502 int ice_devlink_register(struct ice_pf *pf)
504 struct devlink *devlink = priv_to_devlink(pf);
505 struct device *dev = ice_pf_to_dev(pf);
508 err = devlink_register(devlink);
510 dev_err(dev, "devlink registration failed: %d\n", err);
518 * ice_devlink_unregister - Unregister devlink resources for this PF.
519 * @pf: the PF structure to cleanup
521 * Releases resources used by devlink and cleans up associated memory.
523 void ice_devlink_unregister(struct ice_pf *pf)
525 devlink_unregister(priv_to_devlink(pf));
529 * ice_devlink_create_port - Create a devlink port for this VSI
530 * @vsi: the VSI to create a port for
532 * Create and register a devlink_port for this VSI.
534 * Return: zero on success or an error code on failure.
536 int ice_devlink_create_port(struct ice_vsi *vsi)
538 struct devlink_port_attrs attrs = {};
539 struct ice_port_info *pi;
540 struct devlink *devlink;
545 /* Currently we only create devlink_port instances for PF VSIs */
546 if (vsi->type != ICE_VSI_PF)
550 devlink = priv_to_devlink(pf);
551 dev = ice_pf_to_dev(pf);
552 pi = pf->hw.port_info;
554 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
555 attrs.phys.port_number = pi->lport;
556 devlink_port_attrs_set(&vsi->devlink_port, &attrs);
557 err = devlink_port_register(devlink, &vsi->devlink_port, vsi->idx);
559 dev_err(dev, "devlink_port_register failed: %d\n", err);
563 vsi->devlink_port_registered = true;
569 * ice_devlink_destroy_port - Destroy the devlink_port for this VSI
570 * @vsi: the VSI to cleanup
572 * Unregisters the devlink_port structure associated with this VSI.
574 void ice_devlink_destroy_port(struct ice_vsi *vsi)
576 if (!vsi->devlink_port_registered)
579 devlink_port_type_clear(&vsi->devlink_port);
580 devlink_port_unregister(&vsi->devlink_port);
582 vsi->devlink_port_registered = false;
586 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
587 * @devlink: the devlink instance
588 * @ops: the devlink region being snapshotted
589 * @extack: extended ACK response structure
590 * @data: on exit points to snapshot data buffer
592 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
593 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
594 * contents. This snapshot can later be viewed via the devlink-region
597 * @returns zero on success, and updates the data pointer. Returns a non-zero
598 * error code on failure.
600 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
601 const struct devlink_region_ops *ops,
602 struct netlink_ext_ack *extack, u8 **data)
604 struct ice_pf *pf = devlink_priv(devlink);
605 struct device *dev = ice_pf_to_dev(pf);
606 struct ice_hw *hw = &pf->hw;
607 enum ice_status status;
611 nvm_size = hw->flash.flash_size;
612 nvm_data = vzalloc(nvm_size);
616 status = ice_acquire_nvm(hw, ICE_RES_READ);
618 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
619 status, hw->adminq.sq_last_status);
620 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
625 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
627 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
628 nvm_size, status, hw->adminq.sq_last_status);
629 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
643 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
644 * @devlink: the devlink instance
645 * @ops: the devlink region being snapshotted
646 * @extack: extended ACK response structure
647 * @data: on exit points to snapshot data buffer
649 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
650 * the device-caps devlink region. It captures a snapshot of the device
651 * capabilities reported by firmware.
653 * @returns zero on success, and updates the data pointer. Returns a non-zero
654 * error code on failure.
657 ice_devlink_devcaps_snapshot(struct devlink *devlink,
658 const struct devlink_region_ops *ops,
659 struct netlink_ext_ack *extack, u8 **data)
661 struct ice_pf *pf = devlink_priv(devlink);
662 struct device *dev = ice_pf_to_dev(pf);
663 struct ice_hw *hw = &pf->hw;
664 enum ice_status status;
667 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
671 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
672 ice_aqc_opc_list_dev_caps, NULL);
674 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
675 status, hw->adminq.sq_last_status);
676 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
681 *data = (u8 *)devcaps;
686 static const struct devlink_region_ops ice_nvm_region_ops = {
689 .snapshot = ice_devlink_nvm_snapshot,
692 static const struct devlink_region_ops ice_devcaps_region_ops = {
693 .name = "device-caps",
695 .snapshot = ice_devlink_devcaps_snapshot,
699 * ice_devlink_init_regions - Initialize devlink regions
700 * @pf: the PF device structure
702 * Create devlink regions used to enable access to dump the contents of the
703 * flash memory on the device.
705 void ice_devlink_init_regions(struct ice_pf *pf)
707 struct devlink *devlink = priv_to_devlink(pf);
708 struct device *dev = ice_pf_to_dev(pf);
711 nvm_size = pf->hw.flash.flash_size;
712 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
714 if (IS_ERR(pf->nvm_region)) {
715 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
716 PTR_ERR(pf->nvm_region));
717 pf->nvm_region = NULL;
720 pf->devcaps_region = devlink_region_create(devlink,
721 &ice_devcaps_region_ops, 10,
723 if (IS_ERR(pf->devcaps_region)) {
724 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
725 PTR_ERR(pf->devcaps_region));
726 pf->devcaps_region = NULL;
731 * ice_devlink_destroy_regions - Destroy devlink regions
732 * @pf: the PF device structure
734 * Remove previously created regions for this PF.
736 void ice_devlink_destroy_regions(struct ice_pf *pf)
739 devlink_region_destroy(pf->nvm_region);
740 if (pf->devcaps_region)
741 devlink_region_destroy(pf->devcaps_region);