1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
4 #include <linux/vmalloc.h>
8 #include "ice_devlink.h"
9 #include "ice_eswitch.h"
10 #include "ice_fw_update.h"
12 /* context for devlink info version reporting */
15 struct ice_orom_info pending_orom;
16 struct ice_nvm_info pending_nvm;
17 struct ice_netlist_info pending_netlist;
18 struct ice_hw_dev_caps dev_caps;
21 /* The following functions are used to format specific strings for various
22 * devlink info versions. The ctx parameter is used to provide the storage
23 * buffer, as well as any ancillary information calculated when the info
26 * If a version does not exist, for example when attempting to get the
27 * inactive version of flash when there is no pending update, the function
28 * should leave the buffer in the ctx structure empty.
31 static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
35 /* Copy the DSN into an array in Big Endian format */
36 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
38 snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
41 static void ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
43 struct ice_hw *hw = &pf->hw;
46 status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
48 /* We failed to locate the PBA, so just skip this entry */
49 dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %d\n",
53 static void ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
55 struct ice_hw *hw = &pf->hw;
57 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
58 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
61 static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
63 struct ice_hw *hw = &pf->hw;
65 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
66 hw->api_min_ver, hw->api_patch);
69 static void 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);
76 static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
78 struct ice_orom_info *orom = &pf->hw.flash.orom;
80 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
81 orom->major, orom->build, orom->patch);
85 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
86 struct ice_info_ctx *ctx)
88 struct ice_orom_info *orom = &ctx->pending_orom;
90 if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
91 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
92 orom->major, orom->build, orom->patch);
95 static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
97 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
99 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
103 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
104 struct ice_info_ctx *ctx)
106 struct ice_nvm_info *nvm = &ctx->pending_nvm;
108 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
109 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x",
110 nvm->major, nvm->minor);
113 static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
115 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
117 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
121 ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
123 struct ice_nvm_info *nvm = &ctx->pending_nvm;
125 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
126 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
129 static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
131 struct ice_hw *hw = &pf->hw;
133 snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
137 ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
139 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
141 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
142 pkg->major, pkg->minor, pkg->update, pkg->draft);
146 ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
148 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
151 static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
153 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
155 /* The netlist version fields are BCD formatted */
156 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
157 netlist->major, netlist->minor,
158 netlist->type >> 16, netlist->type & 0xFFFF,
159 netlist->rev, netlist->cust_ver);
162 static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
164 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
166 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
170 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
171 struct ice_info_ctx *ctx)
173 struct ice_netlist_info *netlist = &ctx->pending_netlist;
175 /* The netlist version fields are BCD formatted */
176 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
177 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
178 netlist->major, netlist->minor,
179 netlist->type >> 16, netlist->type & 0xFFFF,
180 netlist->rev, netlist->cust_ver);
184 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
185 struct ice_info_ctx *ctx)
187 struct ice_netlist_info *netlist = &ctx->pending_netlist;
189 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
190 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
193 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
194 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
195 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
197 /* The combined() macro inserts both the running entry as well as a stored
198 * entry. The running entry will always report the version from the active
199 * handler. The stored entry will first try the pending handler, and fallback
200 * to the active handler if the pending function does not report a version.
201 * The pending handler should check the status of a pending update for the
202 * relevant flash component. It should only fill in the buffer in the case
203 * where a valid pending version is available. This ensures that the related
204 * stored and running versions remain in sync, and that stored versions are
205 * correctly reported as expected.
207 #define combined(key, active, pending) \
208 running(key, active), \
209 stored(key, pending, active)
211 enum ice_version_type {
217 static const struct ice_devlink_version {
218 enum ice_version_type type;
220 void (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
221 void (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
222 } ice_devlink_versions[] = {
223 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
224 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
225 running("fw.mgmt.api", ice_info_fw_api),
226 running("fw.mgmt.build", ice_info_fw_build),
227 combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
228 combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
229 combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
230 running("fw.app.name", ice_info_ddp_pkg_name),
231 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
232 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
233 combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
234 combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
238 * ice_devlink_info_get - .info_get devlink handler
239 * @devlink: devlink instance structure
240 * @req: the devlink info request
241 * @extack: extended netdev ack structure
243 * Callback for the devlink .info_get operation. Reports information about the
246 * Return: zero on success or an error code on failure.
248 static int ice_devlink_info_get(struct devlink *devlink,
249 struct devlink_info_req *req,
250 struct netlink_ext_ack *extack)
252 struct ice_pf *pf = devlink_priv(devlink);
253 struct device *dev = ice_pf_to_dev(pf);
254 struct ice_hw *hw = &pf->hw;
255 struct ice_info_ctx *ctx;
259 err = ice_wait_for_reset(pf, 10 * HZ);
261 NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
265 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
269 /* discover capabilities first */
270 err = ice_discover_dev_caps(hw, &ctx->dev_caps);
272 dev_dbg(dev, "Failed to discover device capabilities, status %d aq_err %s\n",
273 err, ice_aq_str(hw->adminq.sq_last_status));
274 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
278 if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
279 err = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
281 dev_dbg(dev, "Unable to read inactive Option ROM version data, status %d aq_err %s\n",
282 err, ice_aq_str(hw->adminq.sq_last_status));
284 /* disable display of pending Option ROM */
285 ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
289 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
290 err = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
292 dev_dbg(dev, "Unable to read inactive NVM version data, status %d aq_err %s\n",
293 err, ice_aq_str(hw->adminq.sq_last_status));
295 /* disable display of pending Option ROM */
296 ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
300 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
301 err = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
303 dev_dbg(dev, "Unable to read inactive Netlist version data, status %d aq_err %s\n",
304 err, ice_aq_str(hw->adminq.sq_last_status));
306 /* disable display of pending Option ROM */
307 ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
311 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
313 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
317 ice_info_get_dsn(pf, ctx);
319 err = devlink_info_serial_number_put(req, ctx->buf);
321 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
325 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
326 enum ice_version_type type = ice_devlink_versions[i].type;
327 const char *key = ice_devlink_versions[i].key;
329 memset(ctx->buf, 0, sizeof(ctx->buf));
331 ice_devlink_versions[i].getter(pf, ctx);
333 /* If the default getter doesn't report a version, use the
334 * fallback function. This is primarily useful in the case of
335 * "stored" versions that want to report the same value as the
336 * running version in the normal case of no pending update.
338 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
339 ice_devlink_versions[i].fallback(pf, ctx);
341 /* Do not report missing versions */
342 if (ctx->buf[0] == '\0')
346 case ICE_VERSION_FIXED:
347 err = devlink_info_version_fixed_put(req, key, ctx->buf);
349 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
353 case ICE_VERSION_RUNNING:
354 err = devlink_info_version_running_put(req, key, ctx->buf);
356 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
360 case ICE_VERSION_STORED:
361 err = devlink_info_version_stored_put(req, key, ctx->buf);
363 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
376 * ice_devlink_reload_empr_start - Start EMP reset to activate new firmware
377 * @devlink: pointer to the devlink instance to reload
378 * @netns_change: if true, the network namespace is changing
379 * @action: the action to perform. Must be DEVLINK_RELOAD_ACTION_FW_ACTIVATE
380 * @limit: limits on what reload should do, such as not resetting
381 * @extack: netlink extended ACK structure
383 * Allow user to activate new Embedded Management Processor firmware by
384 * issuing device specific EMP reset. Called in response to
385 * a DEVLINK_CMD_RELOAD with the DEVLINK_RELOAD_ACTION_FW_ACTIVATE.
387 * Note that teardown and rebuild of the driver state happens automatically as
388 * part of an interrupt and watchdog task. This is because all physical
389 * functions on the device must be able to reset when an EMP reset occurs from
393 ice_devlink_reload_empr_start(struct devlink *devlink, bool netns_change,
394 enum devlink_reload_action action,
395 enum devlink_reload_limit limit,
396 struct netlink_ext_ack *extack)
398 struct ice_pf *pf = devlink_priv(devlink);
399 struct device *dev = ice_pf_to_dev(pf);
400 struct ice_hw *hw = &pf->hw;
404 err = ice_get_pending_updates(pf, &pending, extack);
408 /* pending is a bitmask of which flash banks have a pending update,
409 * including the main NVM bank, the Option ROM bank, and the netlist
410 * bank. If any of these bits are set, then there is a pending update
411 * waiting to be activated.
414 NL_SET_ERR_MSG_MOD(extack, "No pending firmware update");
418 if (pf->fw_emp_reset_disabled) {
419 NL_SET_ERR_MSG_MOD(extack, "EMP reset is not available. To activate firmware, a reboot or power cycle is needed");
423 dev_dbg(dev, "Issuing device EMP reset to activate firmware\n");
425 err = ice_aq_nvm_update_empr(hw);
427 dev_err(dev, "Failed to trigger EMP device reset to reload firmware, err %d aq_err %s\n",
428 err, ice_aq_str(hw->adminq.sq_last_status));
429 NL_SET_ERR_MSG_MOD(extack, "Failed to trigger EMP device reset to reload firmware");
437 * ice_devlink_reload_empr_finish - Wait for EMP reset to finish
438 * @devlink: pointer to the devlink instance reloading
439 * @action: the action requested
440 * @limit: limits imposed by userspace, such as not resetting
441 * @actions_performed: on return, indicate what actions actually performed
442 * @extack: netlink extended ACK structure
444 * Wait for driver to finish rebuilding after EMP reset is completed. This
445 * includes time to wait for both the actual device reset as well as the time
446 * for the driver's rebuild to complete.
449 ice_devlink_reload_empr_finish(struct devlink *devlink,
450 enum devlink_reload_action action,
451 enum devlink_reload_limit limit,
452 u32 *actions_performed,
453 struct netlink_ext_ack *extack)
455 struct ice_pf *pf = devlink_priv(devlink);
458 *actions_performed = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE);
460 err = ice_wait_for_reset(pf, 60 * HZ);
462 NL_SET_ERR_MSG_MOD(extack, "Device still resetting after 1 minute");
469 static const struct devlink_ops ice_devlink_ops = {
470 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
471 .reload_actions = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
472 /* The ice driver currently does not support driver reinit */
473 .reload_down = ice_devlink_reload_empr_start,
474 .reload_up = ice_devlink_reload_empr_finish,
475 .eswitch_mode_get = ice_eswitch_mode_get,
476 .eswitch_mode_set = ice_eswitch_mode_set,
477 .info_get = ice_devlink_info_get,
478 .flash_update = ice_devlink_flash_update,
482 ice_devlink_enable_roce_get(struct devlink *devlink, u32 id,
483 struct devlink_param_gset_ctx *ctx)
485 struct ice_pf *pf = devlink_priv(devlink);
487 ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ? true : false;
493 ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
494 struct devlink_param_gset_ctx *ctx)
496 struct ice_pf *pf = devlink_priv(devlink);
497 bool roce_ena = ctx->val.vbool;
501 ice_unplug_aux_dev(pf);
502 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
506 pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2;
507 ret = ice_plug_aux_dev(pf);
509 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
515 ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
516 union devlink_param_value val,
517 struct netlink_ext_ack *extack)
519 struct ice_pf *pf = devlink_priv(devlink);
521 if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
524 if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) {
525 NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
533 ice_devlink_enable_iw_get(struct devlink *devlink, u32 id,
534 struct devlink_param_gset_ctx *ctx)
536 struct ice_pf *pf = devlink_priv(devlink);
538 ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP;
544 ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
545 struct devlink_param_gset_ctx *ctx)
547 struct ice_pf *pf = devlink_priv(devlink);
548 bool iw_ena = ctx->val.vbool;
552 ice_unplug_aux_dev(pf);
553 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
557 pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP;
558 ret = ice_plug_aux_dev(pf);
560 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
566 ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id,
567 union devlink_param_value val,
568 struct netlink_ext_ack *extack)
570 struct ice_pf *pf = devlink_priv(devlink);
572 if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
575 if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) {
576 NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
583 static const struct devlink_param ice_devlink_params[] = {
584 DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
585 ice_devlink_enable_roce_get,
586 ice_devlink_enable_roce_set,
587 ice_devlink_enable_roce_validate),
588 DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
589 ice_devlink_enable_iw_get,
590 ice_devlink_enable_iw_set,
591 ice_devlink_enable_iw_validate),
595 static void ice_devlink_free(void *devlink_ptr)
597 devlink_free((struct devlink *)devlink_ptr);
601 * ice_allocate_pf - Allocate devlink and return PF structure pointer
602 * @dev: the device to allocate for
604 * Allocate a devlink instance for this device and return the private area as
605 * the PF structure. The devlink memory is kept track of through devres by
606 * adding an action to remove it when unwinding.
608 struct ice_pf *ice_allocate_pf(struct device *dev)
610 struct devlink *devlink;
612 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
616 /* Add an action to teardown the devlink when unwinding the driver */
617 if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
620 return devlink_priv(devlink);
624 * ice_devlink_register - Register devlink interface for this PF
625 * @pf: the PF to register the devlink for.
627 * Register the devlink instance associated with this physical function.
629 * Return: zero on success or an error code on failure.
631 void ice_devlink_register(struct ice_pf *pf)
633 struct devlink *devlink = priv_to_devlink(pf);
635 devlink_set_features(devlink, DEVLINK_F_RELOAD);
636 devlink_register(devlink);
640 * ice_devlink_unregister - Unregister devlink resources for this PF.
641 * @pf: the PF structure to cleanup
643 * Releases resources used by devlink and cleans up associated memory.
645 void ice_devlink_unregister(struct ice_pf *pf)
647 devlink_unregister(priv_to_devlink(pf));
651 * ice_devlink_set_switch_id - Set unique switch id based on pci dsn
652 * @pf: the PF to create a devlink port for
653 * @ppid: struct with switch id information
656 ice_devlink_set_switch_id(struct ice_pf *pf, struct netdev_phys_item_id *ppid)
658 struct pci_dev *pdev = pf->pdev;
661 id = pci_get_dsn(pdev);
663 ppid->id_len = sizeof(id);
664 put_unaligned_be64(id, &ppid->id);
667 int ice_devlink_register_params(struct ice_pf *pf)
669 struct devlink *devlink = priv_to_devlink(pf);
670 union devlink_param_value value;
673 err = devlink_params_register(devlink, ice_devlink_params,
674 ARRAY_SIZE(ice_devlink_params));
679 devlink_param_driverinit_value_set(devlink,
680 DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
683 value.vbool = test_bit(ICE_FLAG_RDMA_ENA, pf->flags) ? true : false;
684 devlink_param_driverinit_value_set(devlink,
685 DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
691 void ice_devlink_unregister_params(struct ice_pf *pf)
693 devlink_params_unregister(priv_to_devlink(pf), ice_devlink_params,
694 ARRAY_SIZE(ice_devlink_params));
698 * ice_devlink_create_pf_port - Create a devlink port for this PF
699 * @pf: the PF to create a devlink port for
701 * Create and register a devlink_port for this PF.
703 * Return: zero on success or an error code on failure.
705 int ice_devlink_create_pf_port(struct ice_pf *pf)
707 struct devlink_port_attrs attrs = {};
708 struct devlink_port *devlink_port;
709 struct devlink *devlink;
714 dev = ice_pf_to_dev(pf);
716 devlink_port = &pf->devlink_port;
718 vsi = ice_get_main_vsi(pf);
722 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
723 attrs.phys.port_number = pf->hw.bus.func;
725 ice_devlink_set_switch_id(pf, &attrs.switch_id);
727 devlink_port_attrs_set(devlink_port, &attrs);
728 devlink = priv_to_devlink(pf);
730 err = devlink_port_register(devlink, devlink_port, vsi->idx);
732 dev_err(dev, "Failed to create devlink port for PF %d, error %d\n",
741 * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF
742 * @pf: the PF to cleanup
744 * Unregisters the devlink_port structure associated with this PF.
746 void ice_devlink_destroy_pf_port(struct ice_pf *pf)
748 struct devlink_port *devlink_port;
750 devlink_port = &pf->devlink_port;
752 devlink_port_type_clear(devlink_port);
753 devlink_port_unregister(devlink_port);
757 * ice_devlink_create_vf_port - Create a devlink port for this VF
758 * @vf: the VF to create a port for
760 * Create and register a devlink_port for this VF.
762 * Return: zero on success or an error code on failure.
764 int ice_devlink_create_vf_port(struct ice_vf *vf)
766 struct devlink_port_attrs attrs = {};
767 struct devlink_port *devlink_port;
768 struct devlink *devlink;
775 dev = ice_pf_to_dev(pf);
776 devlink_port = &vf->devlink_port;
778 vsi = ice_get_vf_vsi(vf);
782 attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
783 attrs.pci_vf.pf = pf->hw.bus.func;
784 attrs.pci_vf.vf = vf->vf_id;
786 ice_devlink_set_switch_id(pf, &attrs.switch_id);
788 devlink_port_attrs_set(devlink_port, &attrs);
789 devlink = priv_to_devlink(pf);
791 err = devlink_port_register(devlink, devlink_port, vsi->idx);
793 dev_err(dev, "Failed to create devlink port for VF %d, error %d\n",
802 * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF
803 * @vf: the VF to cleanup
805 * Unregisters the devlink_port structure associated with this VF.
807 void ice_devlink_destroy_vf_port(struct ice_vf *vf)
809 struct devlink_port *devlink_port;
811 devlink_port = &vf->devlink_port;
813 devlink_port_type_clear(devlink_port);
814 devlink_port_unregister(devlink_port);
817 #define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
820 * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
821 * @devlink: the devlink instance
822 * @ops: the devlink region being snapshotted
823 * @extack: extended ACK response structure
824 * @data: on exit points to snapshot data buffer
826 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
827 * the nvm-flash devlink region. It captures a snapshot of the full NVM flash
828 * contents, including both banks of flash. This snapshot can later be viewed
829 * via the devlink-region interface.
831 * It captures the flash using the FLASH_ONLY bit set when reading via
832 * firmware, so it does not read the current Shadow RAM contents. For that,
833 * use the shadow-ram region.
835 * @returns zero on success, and updates the data pointer. Returns a non-zero
836 * error code on failure.
838 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
839 const struct devlink_region_ops *ops,
840 struct netlink_ext_ack *extack, u8 **data)
842 struct ice_pf *pf = devlink_priv(devlink);
843 struct device *dev = ice_pf_to_dev(pf);
844 struct ice_hw *hw = &pf->hw;
845 u8 *nvm_data, *tmp, i;
850 nvm_size = hw->flash.flash_size;
851 nvm_data = vzalloc(nvm_size);
856 num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
860 /* Some systems take longer to read the NVM than others which causes the
861 * FW to reclaim the NVM lock before the entire NVM has been read. Fix
862 * this by breaking the reads of the NVM into smaller chunks that will
863 * probably not take as long. This has some overhead since we are
864 * increasing the number of AQ commands, but it should always work
866 for (i = 0; i < num_blks; i++) {
867 u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
869 status = ice_acquire_nvm(hw, ICE_RES_READ);
871 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
872 status, hw->adminq.sq_last_status);
873 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
878 status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
879 &read_sz, tmp, false);
881 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
882 read_sz, status, hw->adminq.sq_last_status);
883 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
900 * ice_devlink_sram_snapshot - Capture a snapshot of the Shadow RAM contents
901 * @devlink: the devlink instance
902 * @ops: the devlink region being snapshotted
903 * @extack: extended ACK response structure
904 * @data: on exit points to snapshot data buffer
906 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
907 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
908 * contents. This snapshot can later be viewed via the devlink-region
911 * @returns zero on success, and updates the data pointer. Returns a non-zero
912 * error code on failure.
915 ice_devlink_sram_snapshot(struct devlink *devlink,
916 const struct devlink_region_ops __always_unused *ops,
917 struct netlink_ext_ack *extack, u8 **data)
919 struct ice_pf *pf = devlink_priv(devlink);
920 struct device *dev = ice_pf_to_dev(pf);
921 struct ice_hw *hw = &pf->hw;
926 sram_size = hw->flash.sr_words * 2u;
927 sram_data = vzalloc(sram_size);
931 err = ice_acquire_nvm(hw, ICE_RES_READ);
933 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
934 err, hw->adminq.sq_last_status);
935 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
940 /* Read from the Shadow RAM, rather than directly from NVM */
941 err = ice_read_flat_nvm(hw, 0, &sram_size, sram_data, true);
943 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
944 sram_size, err, hw->adminq.sq_last_status);
945 NL_SET_ERR_MSG_MOD(extack,
946 "Failed to read Shadow RAM contents");
960 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
961 * @devlink: the devlink instance
962 * @ops: the devlink region being snapshotted
963 * @extack: extended ACK response structure
964 * @data: on exit points to snapshot data buffer
966 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
967 * the device-caps devlink region. It captures a snapshot of the device
968 * capabilities reported by firmware.
970 * @returns zero on success, and updates the data pointer. Returns a non-zero
971 * error code on failure.
974 ice_devlink_devcaps_snapshot(struct devlink *devlink,
975 const struct devlink_region_ops *ops,
976 struct netlink_ext_ack *extack, u8 **data)
978 struct ice_pf *pf = devlink_priv(devlink);
979 struct device *dev = ice_pf_to_dev(pf);
980 struct ice_hw *hw = &pf->hw;
984 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
988 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
989 ice_aqc_opc_list_dev_caps, NULL);
991 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
992 status, hw->adminq.sq_last_status);
993 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
998 *data = (u8 *)devcaps;
1003 static const struct devlink_region_ops ice_nvm_region_ops = {
1004 .name = "nvm-flash",
1005 .destructor = vfree,
1006 .snapshot = ice_devlink_nvm_snapshot,
1009 static const struct devlink_region_ops ice_sram_region_ops = {
1010 .name = "shadow-ram",
1011 .destructor = vfree,
1012 .snapshot = ice_devlink_sram_snapshot,
1015 static const struct devlink_region_ops ice_devcaps_region_ops = {
1016 .name = "device-caps",
1017 .destructor = vfree,
1018 .snapshot = ice_devlink_devcaps_snapshot,
1022 * ice_devlink_init_regions - Initialize devlink regions
1023 * @pf: the PF device structure
1025 * Create devlink regions used to enable access to dump the contents of the
1026 * flash memory on the device.
1028 void ice_devlink_init_regions(struct ice_pf *pf)
1030 struct devlink *devlink = priv_to_devlink(pf);
1031 struct device *dev = ice_pf_to_dev(pf);
1032 u64 nvm_size, sram_size;
1034 nvm_size = pf->hw.flash.flash_size;
1035 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
1037 if (IS_ERR(pf->nvm_region)) {
1038 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
1039 PTR_ERR(pf->nvm_region));
1040 pf->nvm_region = NULL;
1043 sram_size = pf->hw.flash.sr_words * 2u;
1044 pf->sram_region = devlink_region_create(devlink, &ice_sram_region_ops,
1046 if (IS_ERR(pf->sram_region)) {
1047 dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
1048 PTR_ERR(pf->sram_region));
1049 pf->sram_region = NULL;
1052 pf->devcaps_region = devlink_region_create(devlink,
1053 &ice_devcaps_region_ops, 10,
1054 ICE_AQ_MAX_BUF_LEN);
1055 if (IS_ERR(pf->devcaps_region)) {
1056 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
1057 PTR_ERR(pf->devcaps_region));
1058 pf->devcaps_region = NULL;
1063 * ice_devlink_destroy_regions - Destroy devlink regions
1064 * @pf: the PF device structure
1066 * Remove previously created regions for this PF.
1068 void ice_devlink_destroy_regions(struct ice_pf *pf)
1071 devlink_region_destroy(pf->nvm_region);
1073 if (pf->sram_region)
1074 devlink_region_destroy(pf->sram_region);
1076 if (pf->devcaps_region)
1077 devlink_region_destroy(pf->devcaps_region);