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));
50 static int ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
52 struct ice_hw *hw = &pf->hw;
54 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver,
60 static int ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
62 struct ice_hw *hw = &pf->hw;
64 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u", hw->api_maj_ver, hw->api_min_ver);
69 static int ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
71 struct ice_hw *hw = &pf->hw;
73 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
78 static int ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
80 struct ice_orom_info *orom = &pf->hw.flash.orom;
82 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", orom->major, orom->build, orom->patch);
88 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
90 struct ice_orom_info *orom = &ctx->pending_orom;
92 if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
93 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
94 orom->major, orom->build, orom->patch);
99 static int ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
101 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
103 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
109 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
111 struct ice_nvm_info *nvm = &ctx->pending_nvm;
113 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
114 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
119 static int ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
121 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
123 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
129 ice_info_pending_eetrack(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
131 struct ice_nvm_info *nvm = &ctx->pending_nvm;
133 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
134 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
139 static int ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
141 struct ice_hw *hw = &pf->hw;
143 snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
148 static int ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
150 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
152 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
158 static int ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
160 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
165 static int ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
167 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
169 /* The netlist version fields are BCD formatted */
170 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
171 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
177 static int ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
179 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
181 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
187 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
189 struct ice_netlist_info *netlist = &ctx->pending_netlist;
191 /* The netlist version fields are BCD formatted */
192 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
193 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
194 netlist->major, netlist->minor,
195 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
202 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
204 struct ice_netlist_info *netlist = &ctx->pending_netlist;
206 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
207 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
212 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
213 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
214 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
216 /* The combined() macro inserts both the running entry as well as a stored
217 * entry. The running entry will always report the version from the active
218 * handler. The stored entry will first try the pending handler, and fallback
219 * to the active handler if the pending function does not report a version.
220 * The pending handler should check the status of a pending update for the
221 * relevant flash component. It should only fill in the buffer in the case
222 * where a valid pending version is available. This ensures that the related
223 * stored and running versions remain in sync, and that stored versions are
224 * correctly reported as expected.
226 #define combined(key, active, pending) \
227 running(key, active), \
228 stored(key, pending, active)
230 enum ice_version_type {
236 static const struct ice_devlink_version {
237 enum ice_version_type type;
239 int (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
240 int (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
241 } ice_devlink_versions[] = {
242 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
243 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
244 running("fw.mgmt.api", ice_info_fw_api),
245 running("fw.mgmt.build", ice_info_fw_build),
246 combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
247 combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
248 combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
249 running("fw.app.name", ice_info_ddp_pkg_name),
250 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
251 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
252 combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
253 combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
257 * ice_devlink_info_get - .info_get devlink handler
258 * @devlink: devlink instance structure
259 * @req: the devlink info request
260 * @extack: extended netdev ack structure
262 * Callback for the devlink .info_get operation. Reports information about the
265 * Return: zero on success or an error code on failure.
267 static int ice_devlink_info_get(struct devlink *devlink,
268 struct devlink_info_req *req,
269 struct netlink_ext_ack *extack)
271 struct ice_pf *pf = devlink_priv(devlink);
272 struct device *dev = ice_pf_to_dev(pf);
273 struct ice_hw *hw = &pf->hw;
274 struct ice_info_ctx *ctx;
275 enum ice_status status;
279 err = ice_wait_for_reset(pf, 10 * HZ);
281 NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
285 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
289 /* discover capabilities first */
290 status = ice_discover_dev_caps(hw, &ctx->dev_caps);
292 dev_dbg(dev, "Failed to discover device capabilities, status %s aq_err %s\n",
293 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
294 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
299 if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
300 status = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
302 dev_dbg(dev, "Unable to read inactive Option ROM version data, status %s aq_err %s\n",
303 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
305 /* disable display of pending Option ROM */
306 ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
310 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
311 status = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
313 dev_dbg(dev, "Unable to read inactive NVM version data, status %s aq_err %s\n",
314 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
316 /* disable display of pending Option ROM */
317 ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
321 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
322 status = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
324 dev_dbg(dev, "Unable to read inactive Netlist version data, status %s aq_err %s\n",
325 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
327 /* disable display of pending Option ROM */
328 ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
332 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
334 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
338 ice_info_get_dsn(pf, ctx);
340 err = devlink_info_serial_number_put(req, ctx->buf);
342 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
346 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
347 enum ice_version_type type = ice_devlink_versions[i].type;
348 const char *key = ice_devlink_versions[i].key;
350 memset(ctx->buf, 0, sizeof(ctx->buf));
352 err = ice_devlink_versions[i].getter(pf, ctx);
354 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
358 /* If the default getter doesn't report a version, use the
359 * fallback function. This is primarily useful in the case of
360 * "stored" versions that want to report the same value as the
361 * running version in the normal case of no pending update.
363 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback) {
364 err = ice_devlink_versions[i].fallback(pf, ctx);
366 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
371 /* Do not report missing versions */
372 if (ctx->buf[0] == '\0')
376 case ICE_VERSION_FIXED:
377 err = devlink_info_version_fixed_put(req, key, ctx->buf);
379 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
383 case ICE_VERSION_RUNNING:
384 err = devlink_info_version_running_put(req, key, ctx->buf);
386 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
390 case ICE_VERSION_STORED:
391 err = devlink_info_version_stored_put(req, key, ctx->buf);
393 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
406 * ice_devlink_flash_update - Update firmware stored in flash on the device
407 * @devlink: pointer to devlink associated with device to update
408 * @params: flash update parameters
409 * @extack: netlink extended ACK structure
411 * Perform a device flash update. The bulk of the update logic is contained
412 * within the ice_flash_pldm_image function.
414 * Returns: zero on success, or an error code on failure.
417 ice_devlink_flash_update(struct devlink *devlink,
418 struct devlink_flash_update_params *params,
419 struct netlink_ext_ack *extack)
421 struct ice_pf *pf = devlink_priv(devlink);
422 struct ice_hw *hw = &pf->hw;
426 if (!params->overwrite_mask) {
427 /* preserve all settings and identifiers */
428 preservation = ICE_AQC_NVM_PRESERVE_ALL;
429 } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
430 /* overwrite settings, but preserve the vital device identifiers */
431 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
432 } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
433 DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
434 /* overwrite both settings and identifiers, preserve nothing */
435 preservation = ICE_AQC_NVM_NO_PRESERVATION;
437 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
441 if (!hw->dev_caps.common_cap.nvm_unified_update) {
442 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
446 err = ice_check_for_pending_update(pf, NULL, extack);
450 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
452 return ice_flash_pldm_image(pf, params->fw, preservation, extack);
455 static const struct devlink_ops ice_devlink_ops = {
456 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
457 .info_get = ice_devlink_info_get,
458 .flash_update = ice_devlink_flash_update,
461 static void ice_devlink_free(void *devlink_ptr)
463 devlink_free((struct devlink *)devlink_ptr);
467 * ice_allocate_pf - Allocate devlink and return PF structure pointer
468 * @dev: the device to allocate for
470 * Allocate a devlink instance for this device and return the private area as
471 * the PF structure. The devlink memory is kept track of through devres by
472 * adding an action to remove it when unwinding.
474 struct ice_pf *ice_allocate_pf(struct device *dev)
476 struct devlink *devlink;
478 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf));
482 /* Add an action to teardown the devlink when unwinding the driver */
483 if (devm_add_action(dev, ice_devlink_free, devlink)) {
484 devlink_free(devlink);
488 return devlink_priv(devlink);
492 * ice_devlink_register - Register devlink interface for this PF
493 * @pf: the PF to register the devlink for.
495 * Register the devlink instance associated with this physical function.
497 * Return: zero on success or an error code on failure.
499 int ice_devlink_register(struct ice_pf *pf)
501 struct devlink *devlink = priv_to_devlink(pf);
502 struct device *dev = ice_pf_to_dev(pf);
505 err = devlink_register(devlink, dev);
507 dev_err(dev, "devlink registration failed: %d\n", err);
515 * ice_devlink_unregister - Unregister devlink resources for this PF.
516 * @pf: the PF structure to cleanup
518 * Releases resources used by devlink and cleans up associated memory.
520 void ice_devlink_unregister(struct ice_pf *pf)
522 devlink_unregister(priv_to_devlink(pf));
526 * ice_devlink_create_port - Create a devlink port for this VSI
527 * @vsi: the VSI to create a port for
529 * Create and register a devlink_port for this VSI.
531 * Return: zero on success or an error code on failure.
533 int ice_devlink_create_port(struct ice_vsi *vsi)
535 struct devlink_port_attrs attrs = {};
536 struct ice_port_info *pi;
537 struct devlink *devlink;
542 /* Currently we only create devlink_port instances for PF VSIs */
543 if (vsi->type != ICE_VSI_PF)
547 devlink = priv_to_devlink(pf);
548 dev = ice_pf_to_dev(pf);
549 pi = pf->hw.port_info;
551 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
552 attrs.phys.port_number = pi->lport;
553 devlink_port_attrs_set(&vsi->devlink_port, &attrs);
554 err = devlink_port_register(devlink, &vsi->devlink_port, vsi->idx);
556 dev_err(dev, "devlink_port_register failed: %d\n", err);
560 vsi->devlink_port_registered = true;
566 * ice_devlink_destroy_port - Destroy the devlink_port for this VSI
567 * @vsi: the VSI to cleanup
569 * Unregisters the devlink_port structure associated with this VSI.
571 void ice_devlink_destroy_port(struct ice_vsi *vsi)
573 if (!vsi->devlink_port_registered)
576 devlink_port_type_clear(&vsi->devlink_port);
577 devlink_port_unregister(&vsi->devlink_port);
579 vsi->devlink_port_registered = false;
583 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
584 * @devlink: the devlink instance
585 * @ops: the devlink region being snapshotted
586 * @extack: extended ACK response structure
587 * @data: on exit points to snapshot data buffer
589 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
590 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
591 * contents. This snapshot can later be viewed via the devlink-region
594 * @returns zero on success, and updates the data pointer. Returns a non-zero
595 * error code on failure.
597 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
598 const struct devlink_region_ops *ops,
599 struct netlink_ext_ack *extack, u8 **data)
601 struct ice_pf *pf = devlink_priv(devlink);
602 struct device *dev = ice_pf_to_dev(pf);
603 struct ice_hw *hw = &pf->hw;
604 enum ice_status status;
608 nvm_size = hw->flash.flash_size;
609 nvm_data = vzalloc(nvm_size);
613 status = ice_acquire_nvm(hw, ICE_RES_READ);
615 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
616 status, hw->adminq.sq_last_status);
617 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
622 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
624 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
625 nvm_size, status, hw->adminq.sq_last_status);
626 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
640 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
641 * @devlink: the devlink instance
642 * @ops: the devlink region being snapshotted
643 * @extack: extended ACK response structure
644 * @data: on exit points to snapshot data buffer
646 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
647 * the device-caps devlink region. It captures a snapshot of the device
648 * capabilities reported by firmware.
650 * @returns zero on success, and updates the data pointer. Returns a non-zero
651 * error code on failure.
654 ice_devlink_devcaps_snapshot(struct devlink *devlink,
655 const struct devlink_region_ops *ops,
656 struct netlink_ext_ack *extack, u8 **data)
658 struct ice_pf *pf = devlink_priv(devlink);
659 struct device *dev = ice_pf_to_dev(pf);
660 struct ice_hw *hw = &pf->hw;
661 enum ice_status status;
664 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
668 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
669 ice_aqc_opc_list_dev_caps, NULL);
671 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
672 status, hw->adminq.sq_last_status);
673 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
678 *data = (u8 *)devcaps;
683 static const struct devlink_region_ops ice_nvm_region_ops = {
686 .snapshot = ice_devlink_nvm_snapshot,
689 static const struct devlink_region_ops ice_devcaps_region_ops = {
690 .name = "device-caps",
692 .snapshot = ice_devlink_devcaps_snapshot,
696 * ice_devlink_init_regions - Initialize devlink regions
697 * @pf: the PF device structure
699 * Create devlink regions used to enable access to dump the contents of the
700 * flash memory on the device.
702 void ice_devlink_init_regions(struct ice_pf *pf)
704 struct devlink *devlink = priv_to_devlink(pf);
705 struct device *dev = ice_pf_to_dev(pf);
708 nvm_size = pf->hw.flash.flash_size;
709 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
711 if (IS_ERR(pf->nvm_region)) {
712 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
713 PTR_ERR(pf->nvm_region));
714 pf->nvm_region = NULL;
717 pf->devcaps_region = devlink_region_create(devlink,
718 &ice_devcaps_region_ops, 10,
720 if (IS_ERR(pf->devcaps_region)) {
721 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
722 PTR_ERR(pf->devcaps_region));
723 pf->devcaps_region = NULL;
728 * ice_devlink_destroy_regions - Destroy devlink regions
729 * @pf: the PF device structure
731 * Remove previously created regions for this PF.
733 void ice_devlink_destroy_regions(struct ice_pf *pf)
736 devlink_region_destroy(pf->nvm_region);
737 if (pf->devcaps_region)
738 devlink_region_destroy(pf->devcaps_region);