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", hw->api_maj_ver, hw->api_min_ver);
71 static int ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
73 struct ice_hw *hw = &pf->hw;
75 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
80 static int ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
82 struct ice_orom_info *orom = &pf->hw.flash.orom;
84 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", orom->major, orom->build, orom->patch);
90 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
92 struct ice_orom_info *orom = &ctx->pending_orom;
94 if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
95 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
96 orom->major, orom->build, orom->patch);
101 static int ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
103 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
105 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
111 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
113 struct ice_nvm_info *nvm = &ctx->pending_nvm;
115 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
116 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
121 static int ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
123 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
125 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
131 ice_info_pending_eetrack(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
133 struct ice_nvm_info *nvm = &ctx->pending_nvm;
135 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
136 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
141 static int ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
143 struct ice_hw *hw = &pf->hw;
145 snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
150 static int ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
152 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
154 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
160 static int ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
162 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
167 static int ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
169 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
171 /* The netlist version fields are BCD formatted */
172 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
173 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
179 static int ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
181 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
183 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
189 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
191 struct ice_netlist_info *netlist = &ctx->pending_netlist;
193 /* The netlist version fields are BCD formatted */
194 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
195 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
196 netlist->major, netlist->minor,
197 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
204 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf, struct ice_info_ctx *ctx)
206 struct ice_netlist_info *netlist = &ctx->pending_netlist;
208 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
209 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
214 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
215 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
216 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
218 /* The combined() macro inserts both the running entry as well as a stored
219 * entry. The running entry will always report the version from the active
220 * handler. The stored entry will first try the pending handler, and fallback
221 * to the active handler if the pending function does not report a version.
222 * The pending handler should check the status of a pending update for the
223 * relevant flash component. It should only fill in the buffer in the case
224 * where a valid pending version is available. This ensures that the related
225 * stored and running versions remain in sync, and that stored versions are
226 * correctly reported as expected.
228 #define combined(key, active, pending) \
229 running(key, active), \
230 stored(key, pending, active)
232 enum ice_version_type {
238 static const struct ice_devlink_version {
239 enum ice_version_type type;
241 int (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
242 int (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
243 } ice_devlink_versions[] = {
244 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
245 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
246 running("fw.mgmt.api", ice_info_fw_api),
247 running("fw.mgmt.build", ice_info_fw_build),
248 combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
249 combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
250 combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
251 running("fw.app.name", ice_info_ddp_pkg_name),
252 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
253 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
254 combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
255 combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
259 * ice_devlink_info_get - .info_get devlink handler
260 * @devlink: devlink instance structure
261 * @req: the devlink info request
262 * @extack: extended netdev ack structure
264 * Callback for the devlink .info_get operation. Reports information about the
267 * Return: zero on success or an error code on failure.
269 static int ice_devlink_info_get(struct devlink *devlink,
270 struct devlink_info_req *req,
271 struct netlink_ext_ack *extack)
273 struct ice_pf *pf = devlink_priv(devlink);
274 struct device *dev = ice_pf_to_dev(pf);
275 struct ice_hw *hw = &pf->hw;
276 struct ice_info_ctx *ctx;
277 enum ice_status status;
281 err = ice_wait_for_reset(pf, 10 * HZ);
283 NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
287 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
291 /* discover capabilities first */
292 status = ice_discover_dev_caps(hw, &ctx->dev_caps);
294 dev_dbg(dev, "Failed to discover device capabilities, status %s aq_err %s\n",
295 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
296 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
301 if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
302 status = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
304 dev_dbg(dev, "Unable to read inactive Option ROM version data, status %s aq_err %s\n",
305 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
307 /* disable display of pending Option ROM */
308 ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
312 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
313 status = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
315 dev_dbg(dev, "Unable to read inactive NVM version data, status %s aq_err %s\n",
316 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
318 /* disable display of pending Option ROM */
319 ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
323 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
324 status = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
326 dev_dbg(dev, "Unable to read inactive Netlist version data, status %s aq_err %s\n",
327 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
329 /* disable display of pending Option ROM */
330 ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
334 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
336 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
340 ice_info_get_dsn(pf, ctx);
342 err = devlink_info_serial_number_put(req, ctx->buf);
344 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
348 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
349 enum ice_version_type type = ice_devlink_versions[i].type;
350 const char *key = ice_devlink_versions[i].key;
352 memset(ctx->buf, 0, sizeof(ctx->buf));
354 err = ice_devlink_versions[i].getter(pf, ctx);
356 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
360 /* If the default getter doesn't report a version, use the
361 * fallback function. This is primarily useful in the case of
362 * "stored" versions that want to report the same value as the
363 * running version in the normal case of no pending update.
365 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback) {
366 err = ice_devlink_versions[i].fallback(pf, ctx);
368 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
373 /* Do not report missing versions */
374 if (ctx->buf[0] == '\0')
378 case ICE_VERSION_FIXED:
379 err = devlink_info_version_fixed_put(req, key, ctx->buf);
381 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
385 case ICE_VERSION_RUNNING:
386 err = devlink_info_version_running_put(req, key, ctx->buf);
388 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
392 case ICE_VERSION_STORED:
393 err = devlink_info_version_stored_put(req, key, ctx->buf);
395 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
408 * ice_devlink_flash_update - Update firmware stored in flash on the device
409 * @devlink: pointer to devlink associated with device to update
410 * @params: flash update parameters
411 * @extack: netlink extended ACK structure
413 * Perform a device flash update. The bulk of the update logic is contained
414 * within the ice_flash_pldm_image function.
416 * Returns: zero on success, or an error code on failure.
419 ice_devlink_flash_update(struct devlink *devlink,
420 struct devlink_flash_update_params *params,
421 struct netlink_ext_ack *extack)
423 struct ice_pf *pf = devlink_priv(devlink);
424 struct ice_hw *hw = &pf->hw;
428 if (!params->overwrite_mask) {
429 /* preserve all settings and identifiers */
430 preservation = ICE_AQC_NVM_PRESERVE_ALL;
431 } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
432 /* overwrite settings, but preserve the vital device identifiers */
433 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
434 } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
435 DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
436 /* overwrite both settings and identifiers, preserve nothing */
437 preservation = ICE_AQC_NVM_NO_PRESERVATION;
439 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
443 if (!hw->dev_caps.common_cap.nvm_unified_update) {
444 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
448 err = ice_check_for_pending_update(pf, NULL, extack);
452 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
454 return ice_flash_pldm_image(pf, params->fw, preservation, extack);
457 static const struct devlink_ops ice_devlink_ops = {
458 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
459 .info_get = ice_devlink_info_get,
460 .flash_update = ice_devlink_flash_update,
463 static void ice_devlink_free(void *devlink_ptr)
465 devlink_free((struct devlink *)devlink_ptr);
469 * ice_allocate_pf - Allocate devlink and return PF structure pointer
470 * @dev: the device to allocate for
472 * Allocate a devlink instance for this device and return the private area as
473 * the PF structure. The devlink memory is kept track of through devres by
474 * adding an action to remove it when unwinding.
476 struct ice_pf *ice_allocate_pf(struct device *dev)
478 struct devlink *devlink;
480 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
484 /* Add an action to teardown the devlink when unwinding the driver */
485 if (devm_add_action(dev, ice_devlink_free, devlink)) {
486 devlink_free(devlink);
490 return devlink_priv(devlink);
494 * ice_devlink_register - Register devlink interface for this PF
495 * @pf: the PF to register the devlink for.
497 * Register the devlink instance associated with this physical function.
499 * Return: zero on success or an error code on failure.
501 int ice_devlink_register(struct ice_pf *pf)
503 struct devlink *devlink = priv_to_devlink(pf);
504 struct device *dev = ice_pf_to_dev(pf);
507 err = devlink_register(devlink);
509 dev_err(dev, "devlink registration failed: %d\n", err);
517 * ice_devlink_unregister - Unregister devlink resources for this PF.
518 * @pf: the PF structure to cleanup
520 * Releases resources used by devlink and cleans up associated memory.
522 void ice_devlink_unregister(struct ice_pf *pf)
524 devlink_unregister(priv_to_devlink(pf));
528 * ice_devlink_create_port - Create a devlink port for this VSI
529 * @vsi: the VSI to create a port for
531 * Create and register a devlink_port for this VSI.
533 * Return: zero on success or an error code on failure.
535 int ice_devlink_create_port(struct ice_vsi *vsi)
537 struct devlink_port_attrs attrs = {};
538 struct ice_port_info *pi;
539 struct devlink *devlink;
544 /* Currently we only create devlink_port instances for PF VSIs */
545 if (vsi->type != ICE_VSI_PF)
549 devlink = priv_to_devlink(pf);
550 dev = ice_pf_to_dev(pf);
551 pi = pf->hw.port_info;
553 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
554 attrs.phys.port_number = pi->lport;
555 devlink_port_attrs_set(&vsi->devlink_port, &attrs);
556 err = devlink_port_register(devlink, &vsi->devlink_port, vsi->idx);
558 dev_err(dev, "devlink_port_register failed: %d\n", err);
562 vsi->devlink_port_registered = true;
568 * ice_devlink_destroy_port - Destroy the devlink_port for this VSI
569 * @vsi: the VSI to cleanup
571 * Unregisters the devlink_port structure associated with this VSI.
573 void ice_devlink_destroy_port(struct ice_vsi *vsi)
575 if (!vsi->devlink_port_registered)
578 devlink_port_type_clear(&vsi->devlink_port);
579 devlink_port_unregister(&vsi->devlink_port);
581 vsi->devlink_port_registered = false;
585 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
586 * @devlink: the devlink instance
587 * @ops: the devlink region being snapshotted
588 * @extack: extended ACK response structure
589 * @data: on exit points to snapshot data buffer
591 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
592 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
593 * contents. This snapshot can later be viewed via the devlink-region
596 * @returns zero on success, and updates the data pointer. Returns a non-zero
597 * error code on failure.
599 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
600 const struct devlink_region_ops *ops,
601 struct netlink_ext_ack *extack, u8 **data)
603 struct ice_pf *pf = devlink_priv(devlink);
604 struct device *dev = ice_pf_to_dev(pf);
605 struct ice_hw *hw = &pf->hw;
606 enum ice_status status;
610 nvm_size = hw->flash.flash_size;
611 nvm_data = vzalloc(nvm_size);
615 status = ice_acquire_nvm(hw, ICE_RES_READ);
617 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
618 status, hw->adminq.sq_last_status);
619 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
624 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
626 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
627 nvm_size, status, hw->adminq.sq_last_status);
628 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
642 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
643 * @devlink: the devlink instance
644 * @ops: the devlink region being snapshotted
645 * @extack: extended ACK response structure
646 * @data: on exit points to snapshot data buffer
648 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
649 * the device-caps devlink region. It captures a snapshot of the device
650 * capabilities reported by firmware.
652 * @returns zero on success, and updates the data pointer. Returns a non-zero
653 * error code on failure.
656 ice_devlink_devcaps_snapshot(struct devlink *devlink,
657 const struct devlink_region_ops *ops,
658 struct netlink_ext_ack *extack, u8 **data)
660 struct ice_pf *pf = devlink_priv(devlink);
661 struct device *dev = ice_pf_to_dev(pf);
662 struct ice_hw *hw = &pf->hw;
663 enum ice_status status;
666 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
670 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
671 ice_aqc_opc_list_dev_caps, NULL);
673 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
674 status, hw->adminq.sq_last_status);
675 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
680 *data = (u8 *)devcaps;
685 static const struct devlink_region_ops ice_nvm_region_ops = {
688 .snapshot = ice_devlink_nvm_snapshot,
691 static const struct devlink_region_ops ice_devcaps_region_ops = {
692 .name = "device-caps",
694 .snapshot = ice_devlink_devcaps_snapshot,
698 * ice_devlink_init_regions - Initialize devlink regions
699 * @pf: the PF device structure
701 * Create devlink regions used to enable access to dump the contents of the
702 * flash memory on the device.
704 void ice_devlink_init_regions(struct ice_pf *pf)
706 struct devlink *devlink = priv_to_devlink(pf);
707 struct device *dev = ice_pf_to_dev(pf);
710 nvm_size = pf->hw.flash.flash_size;
711 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
713 if (IS_ERR(pf->nvm_region)) {
714 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
715 PTR_ERR(pf->nvm_region));
716 pf->nvm_region = NULL;
719 pf->devcaps_region = devlink_region_create(devlink,
720 &ice_devcaps_region_ops, 10,
722 if (IS_ERR(pf->devcaps_region)) {
723 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
724 PTR_ERR(pf->devcaps_region));
725 pf->devcaps_region = NULL;
730 * ice_devlink_destroy_regions - Destroy devlink regions
731 * @pf: the PF device structure
733 * Remove previously created regions for this PF.
735 void ice_devlink_destroy_regions(struct ice_pf *pf)
738 devlink_region_destroy(pf->nvm_region);
739 if (pf->devcaps_region)
740 devlink_region_destroy(pf->devcaps_region);