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.
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 void 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));
50 static void 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",
55 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
58 static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
60 struct ice_hw *hw = &pf->hw;
62 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u",
63 hw->api_maj_ver, hw->api_min_ver);
66 static void ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
68 struct ice_hw *hw = &pf->hw;
70 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
73 static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
75 struct ice_orom_info *orom = &pf->hw.flash.orom;
77 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
78 orom->major, orom->build, orom->patch);
82 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
83 struct ice_info_ctx *ctx)
85 struct ice_orom_info *orom = &ctx->pending_orom;
87 if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
88 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
89 orom->major, orom->build, orom->patch);
92 static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
94 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
96 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
100 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
101 struct ice_info_ctx *ctx)
103 struct ice_nvm_info *nvm = &ctx->pending_nvm;
105 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
106 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x",
107 nvm->major, nvm->minor);
110 static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
112 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
114 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
118 ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
120 struct ice_nvm_info *nvm = &ctx->pending_nvm;
122 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
123 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
126 static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
128 struct ice_hw *hw = &pf->hw;
130 snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
134 ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
136 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
138 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
139 pkg->major, pkg->minor, pkg->update, pkg->draft);
143 ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
145 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
148 static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
150 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
152 /* The netlist version fields are BCD formatted */
153 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
154 netlist->major, netlist->minor,
155 netlist->type >> 16, netlist->type & 0xFFFF,
156 netlist->rev, netlist->cust_ver);
159 static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
161 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
163 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
167 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
168 struct ice_info_ctx *ctx)
170 struct ice_netlist_info *netlist = &ctx->pending_netlist;
172 /* The netlist version fields are BCD formatted */
173 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
174 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
175 netlist->major, netlist->minor,
176 netlist->type >> 16, netlist->type & 0xFFFF,
177 netlist->rev, netlist->cust_ver);
181 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
182 struct ice_info_ctx *ctx)
184 struct ice_netlist_info *netlist = &ctx->pending_netlist;
186 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
187 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
190 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
191 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
192 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
194 /* The combined() macro inserts both the running entry as well as a stored
195 * entry. The running entry will always report the version from the active
196 * handler. The stored entry will first try the pending handler, and fallback
197 * to the active handler if the pending function does not report a version.
198 * The pending handler should check the status of a pending update for the
199 * relevant flash component. It should only fill in the buffer in the case
200 * where a valid pending version is available. This ensures that the related
201 * stored and running versions remain in sync, and that stored versions are
202 * correctly reported as expected.
204 #define combined(key, active, pending) \
205 running(key, active), \
206 stored(key, pending, active)
208 enum ice_version_type {
214 static const struct ice_devlink_version {
215 enum ice_version_type type;
217 void (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
218 void (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
219 } ice_devlink_versions[] = {
220 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
221 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
222 running("fw.mgmt.api", ice_info_fw_api),
223 running("fw.mgmt.build", ice_info_fw_build),
224 combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
225 combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
226 combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
227 running("fw.app.name", ice_info_ddp_pkg_name),
228 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
229 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
230 combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
231 combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
235 * ice_devlink_info_get - .info_get devlink handler
236 * @devlink: devlink instance structure
237 * @req: the devlink info request
238 * @extack: extended netdev ack structure
240 * Callback for the devlink .info_get operation. Reports information about the
243 * Return: zero on success or an error code on failure.
245 static int ice_devlink_info_get(struct devlink *devlink,
246 struct devlink_info_req *req,
247 struct netlink_ext_ack *extack)
249 struct ice_pf *pf = devlink_priv(devlink);
250 struct device *dev = ice_pf_to_dev(pf);
251 struct ice_hw *hw = &pf->hw;
252 struct ice_info_ctx *ctx;
253 enum ice_status status;
257 err = ice_wait_for_reset(pf, 10 * HZ);
259 NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
263 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
267 /* discover capabilities first */
268 status = ice_discover_dev_caps(hw, &ctx->dev_caps);
270 dev_dbg(dev, "Failed to discover device capabilities, status %s aq_err %s\n",
271 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
272 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
277 if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
278 status = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
280 dev_dbg(dev, "Unable to read inactive Option ROM version data, status %s aq_err %s\n",
281 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
283 /* disable display of pending Option ROM */
284 ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
288 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
289 status = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
291 dev_dbg(dev, "Unable to read inactive NVM version data, status %s aq_err %s\n",
292 ice_stat_str(status), ice_aq_str(hw->adminq.sq_last_status));
294 /* disable display of pending Option ROM */
295 ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
299 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
300 status = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
302 dev_dbg(dev, "Unable to read inactive Netlist 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_netlist = false;
310 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
312 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
316 ice_info_get_dsn(pf, ctx);
318 err = devlink_info_serial_number_put(req, ctx->buf);
320 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
324 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
325 enum ice_version_type type = ice_devlink_versions[i].type;
326 const char *key = ice_devlink_versions[i].key;
328 memset(ctx->buf, 0, sizeof(ctx->buf));
330 ice_devlink_versions[i].getter(pf, ctx);
332 /* If the default getter doesn't report a version, use the
333 * fallback function. This is primarily useful in the case of
334 * "stored" versions that want to report the same value as the
335 * running version in the normal case of no pending update.
337 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
338 ice_devlink_versions[i].fallback(pf, ctx);
340 /* Do not report missing versions */
341 if (ctx->buf[0] == '\0')
345 case ICE_VERSION_FIXED:
346 err = devlink_info_version_fixed_put(req, key, ctx->buf);
348 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
352 case ICE_VERSION_RUNNING:
353 err = devlink_info_version_running_put(req, key, ctx->buf);
355 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
359 case ICE_VERSION_STORED:
360 err = devlink_info_version_stored_put(req, key, ctx->buf);
362 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
375 * ice_devlink_flash_update - Update firmware stored in flash on the device
376 * @devlink: pointer to devlink associated with device to update
377 * @params: flash update parameters
378 * @extack: netlink extended ACK structure
380 * Perform a device flash update. The bulk of the update logic is contained
381 * within the ice_flash_pldm_image function.
383 * Returns: zero on success, or an error code on failure.
386 ice_devlink_flash_update(struct devlink *devlink,
387 struct devlink_flash_update_params *params,
388 struct netlink_ext_ack *extack)
390 struct ice_pf *pf = devlink_priv(devlink);
391 struct ice_hw *hw = &pf->hw;
395 if (!params->overwrite_mask) {
396 /* preserve all settings and identifiers */
397 preservation = ICE_AQC_NVM_PRESERVE_ALL;
398 } else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
399 /* overwrite settings, but preserve the vital device identifiers */
400 preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
401 } else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
402 DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
403 /* overwrite both settings and identifiers, preserve nothing */
404 preservation = ICE_AQC_NVM_NO_PRESERVATION;
406 NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
410 if (!hw->dev_caps.common_cap.nvm_unified_update) {
411 NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
415 err = ice_check_for_pending_update(pf, NULL, extack);
419 devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
421 return ice_flash_pldm_image(pf, params->fw, preservation, extack);
424 static const struct devlink_ops ice_devlink_ops = {
425 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
426 .info_get = ice_devlink_info_get,
427 .flash_update = ice_devlink_flash_update,
430 static void ice_devlink_free(void *devlink_ptr)
432 devlink_free((struct devlink *)devlink_ptr);
436 * ice_allocate_pf - Allocate devlink and return PF structure pointer
437 * @dev: the device to allocate for
439 * Allocate a devlink instance for this device and return the private area as
440 * the PF structure. The devlink memory is kept track of through devres by
441 * adding an action to remove it when unwinding.
443 struct ice_pf *ice_allocate_pf(struct device *dev)
445 struct devlink *devlink;
447 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
451 /* Add an action to teardown the devlink when unwinding the driver */
452 if (devm_add_action(dev, ice_devlink_free, devlink)) {
453 devlink_free(devlink);
457 return devlink_priv(devlink);
461 * ice_devlink_register - Register devlink interface for this PF
462 * @pf: the PF to register the devlink for.
464 * Register the devlink instance associated with this physical function.
466 * Return: zero on success or an error code on failure.
468 void ice_devlink_register(struct ice_pf *pf)
470 struct devlink *devlink = priv_to_devlink(pf);
472 devlink_register(devlink);
476 * ice_devlink_unregister - Unregister devlink resources for this PF.
477 * @pf: the PF structure to cleanup
479 * Releases resources used by devlink and cleans up associated memory.
481 void ice_devlink_unregister(struct ice_pf *pf)
483 devlink_unregister(priv_to_devlink(pf));
487 * ice_devlink_create_port - Create a devlink port for this VSI
488 * @vsi: the VSI to create a port for
490 * Create and register a devlink_port for this VSI.
492 * Return: zero on success or an error code on failure.
494 int ice_devlink_create_port(struct ice_vsi *vsi)
496 struct devlink_port_attrs attrs = {};
497 struct ice_port_info *pi;
498 struct devlink *devlink;
503 /* Currently we only create devlink_port instances for PF VSIs */
504 if (vsi->type != ICE_VSI_PF)
508 devlink = priv_to_devlink(pf);
509 dev = ice_pf_to_dev(pf);
510 pi = pf->hw.port_info;
512 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
513 attrs.phys.port_number = pi->lport;
514 devlink_port_attrs_set(&vsi->devlink_port, &attrs);
515 err = devlink_port_register(devlink, &vsi->devlink_port, vsi->idx);
517 dev_err(dev, "devlink_port_register failed: %d\n", err);
521 vsi->devlink_port_registered = true;
527 * ice_devlink_destroy_port - Destroy the devlink_port for this VSI
528 * @vsi: the VSI to cleanup
530 * Unregisters the devlink_port structure associated with this VSI.
532 void ice_devlink_destroy_port(struct ice_vsi *vsi)
534 if (!vsi->devlink_port_registered)
537 devlink_port_type_clear(&vsi->devlink_port);
538 devlink_port_unregister(&vsi->devlink_port);
540 vsi->devlink_port_registered = false;
544 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
545 * @devlink: the devlink instance
546 * @ops: the devlink region being snapshotted
547 * @extack: extended ACK response structure
548 * @data: on exit points to snapshot data buffer
550 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
551 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
552 * contents. This snapshot can later be viewed via the devlink-region
555 * @returns zero on success, and updates the data pointer. Returns a non-zero
556 * error code on failure.
558 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
559 const struct devlink_region_ops *ops,
560 struct netlink_ext_ack *extack, u8 **data)
562 struct ice_pf *pf = devlink_priv(devlink);
563 struct device *dev = ice_pf_to_dev(pf);
564 struct ice_hw *hw = &pf->hw;
565 enum ice_status status;
569 nvm_size = hw->flash.flash_size;
570 nvm_data = vzalloc(nvm_size);
574 status = ice_acquire_nvm(hw, ICE_RES_READ);
576 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
577 status, hw->adminq.sq_last_status);
578 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
583 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
585 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
586 nvm_size, status, hw->adminq.sq_last_status);
587 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
601 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
602 * @devlink: the devlink instance
603 * @ops: the devlink region being snapshotted
604 * @extack: extended ACK response structure
605 * @data: on exit points to snapshot data buffer
607 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
608 * the device-caps devlink region. It captures a snapshot of the device
609 * capabilities reported by firmware.
611 * @returns zero on success, and updates the data pointer. Returns a non-zero
612 * error code on failure.
615 ice_devlink_devcaps_snapshot(struct devlink *devlink,
616 const struct devlink_region_ops *ops,
617 struct netlink_ext_ack *extack, u8 **data)
619 struct ice_pf *pf = devlink_priv(devlink);
620 struct device *dev = ice_pf_to_dev(pf);
621 struct ice_hw *hw = &pf->hw;
622 enum ice_status status;
625 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
629 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
630 ice_aqc_opc_list_dev_caps, NULL);
632 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
633 status, hw->adminq.sq_last_status);
634 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
639 *data = (u8 *)devcaps;
644 static const struct devlink_region_ops ice_nvm_region_ops = {
647 .snapshot = ice_devlink_nvm_snapshot,
650 static const struct devlink_region_ops ice_devcaps_region_ops = {
651 .name = "device-caps",
653 .snapshot = ice_devlink_devcaps_snapshot,
657 * ice_devlink_init_regions - Initialize devlink regions
658 * @pf: the PF device structure
660 * Create devlink regions used to enable access to dump the contents of the
661 * flash memory on the device.
663 void ice_devlink_init_regions(struct ice_pf *pf)
665 struct devlink *devlink = priv_to_devlink(pf);
666 struct device *dev = ice_pf_to_dev(pf);
669 nvm_size = pf->hw.flash.flash_size;
670 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
672 if (IS_ERR(pf->nvm_region)) {
673 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
674 PTR_ERR(pf->nvm_region));
675 pf->nvm_region = NULL;
678 pf->devcaps_region = devlink_region_create(devlink,
679 &ice_devcaps_region_ops, 10,
681 if (IS_ERR(pf->devcaps_region)) {
682 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
683 PTR_ERR(pf->devcaps_region));
684 pf->devcaps_region = NULL;
689 * ice_devlink_destroy_regions - Destroy devlink regions
690 * @pf: the PF device structure
692 * Remove previously created regions for this PF.
694 void ice_devlink_destroy_regions(struct ice_pf *pf)
697 devlink_region_destroy(pf->nvm_region);
698 if (pf->devcaps_region)
699 devlink_region_destroy(pf->devcaps_region);