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"
11 #include "ice_dcb_lib.h"
13 static int ice_active_port_option = -1;
15 /* context for devlink info version reporting */
18 struct ice_orom_info pending_orom;
19 struct ice_nvm_info pending_nvm;
20 struct ice_netlist_info pending_netlist;
21 struct ice_hw_dev_caps dev_caps;
24 /* The following functions are used to format specific strings for various
25 * devlink info versions. The ctx parameter is used to provide the storage
26 * buffer, as well as any ancillary information calculated when the info
29 * If a version does not exist, for example when attempting to get the
30 * inactive version of flash when there is no pending update, the function
31 * should leave the buffer in the ctx structure empty.
34 static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
38 /* Copy the DSN into an array in Big Endian format */
39 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
41 snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
44 static void ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
46 struct ice_hw *hw = &pf->hw;
49 status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
51 /* We failed to locate the PBA, so just skip this entry */
52 dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %d\n",
56 static void ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
58 struct ice_hw *hw = &pf->hw;
60 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
61 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
64 static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
66 struct ice_hw *hw = &pf->hw;
68 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
69 hw->api_min_ver, hw->api_patch);
72 static void 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);
79 static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
81 struct ice_orom_info *orom = &pf->hw.flash.orom;
83 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
84 orom->major, orom->build, orom->patch);
88 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
89 struct ice_info_ctx *ctx)
91 struct ice_orom_info *orom = &ctx->pending_orom;
93 if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
94 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
95 orom->major, orom->build, orom->patch);
98 static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
100 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
102 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
106 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
107 struct ice_info_ctx *ctx)
109 struct ice_nvm_info *nvm = &ctx->pending_nvm;
111 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
112 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x",
113 nvm->major, nvm->minor);
116 static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
118 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
120 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
124 ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
126 struct ice_nvm_info *nvm = &ctx->pending_nvm;
128 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
129 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
132 static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
134 struct ice_hw *hw = &pf->hw;
136 snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
140 ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
142 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
144 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
145 pkg->major, pkg->minor, pkg->update, pkg->draft);
149 ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
151 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
154 static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
156 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
158 /* The netlist version fields are BCD formatted */
159 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
160 netlist->major, netlist->minor,
161 netlist->type >> 16, netlist->type & 0xFFFF,
162 netlist->rev, netlist->cust_ver);
165 static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
167 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
169 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
173 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
174 struct ice_info_ctx *ctx)
176 struct ice_netlist_info *netlist = &ctx->pending_netlist;
178 /* The netlist version fields are BCD formatted */
179 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
180 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
181 netlist->major, netlist->minor,
182 netlist->type >> 16, netlist->type & 0xFFFF,
183 netlist->rev, netlist->cust_ver);
187 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
188 struct ice_info_ctx *ctx)
190 struct ice_netlist_info *netlist = &ctx->pending_netlist;
192 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
193 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
196 static void ice_info_cgu_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
198 u32 id, cfg_ver, fw_ver;
200 if (!ice_is_feature_supported(pf, ICE_F_CGU))
202 if (ice_aq_get_cgu_info(&pf->hw, &id, &cfg_ver, &fw_ver))
204 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", id, cfg_ver, fw_ver);
207 static void ice_info_cgu_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
209 if (!ice_is_feature_supported(pf, ICE_F_CGU))
211 snprintf(ctx->buf, sizeof(ctx->buf), "%u", pf->hw.cgu_part_number);
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 void (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
242 void (*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),
256 fixed("cgu.id", ice_info_cgu_id),
257 running("fw.cgu", ice_info_cgu_fw_build),
261 * ice_devlink_info_get - .info_get devlink handler
262 * @devlink: devlink instance structure
263 * @req: the devlink info request
264 * @extack: extended netdev ack structure
266 * Callback for the devlink .info_get operation. Reports information about the
269 * Return: zero on success or an error code on failure.
271 static int ice_devlink_info_get(struct devlink *devlink,
272 struct devlink_info_req *req,
273 struct netlink_ext_ack *extack)
275 struct ice_pf *pf = devlink_priv(devlink);
276 struct device *dev = ice_pf_to_dev(pf);
277 struct ice_hw *hw = &pf->hw;
278 struct ice_info_ctx *ctx;
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 err = ice_discover_dev_caps(hw, &ctx->dev_caps);
295 dev_dbg(dev, "Failed to discover device capabilities, status %d aq_err %s\n",
296 err, ice_aq_str(hw->adminq.sq_last_status));
297 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
301 if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
302 err = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
304 dev_dbg(dev, "Unable to read inactive Option ROM version data, status %d aq_err %s\n",
305 err, 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 err = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
315 dev_dbg(dev, "Unable to read inactive NVM version data, status %d aq_err %s\n",
316 err, 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 err = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
326 dev_dbg(dev, "Unable to read inactive Netlist version data, status %d aq_err %s\n",
327 err, 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 ice_info_get_dsn(pf, ctx);
336 err = devlink_info_serial_number_put(req, ctx->buf);
338 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
342 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
343 enum ice_version_type type = ice_devlink_versions[i].type;
344 const char *key = ice_devlink_versions[i].key;
346 memset(ctx->buf, 0, sizeof(ctx->buf));
348 ice_devlink_versions[i].getter(pf, ctx);
350 /* If the default getter doesn't report a version, use the
351 * fallback function. This is primarily useful in the case of
352 * "stored" versions that want to report the same value as the
353 * running version in the normal case of no pending update.
355 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
356 ice_devlink_versions[i].fallback(pf, ctx);
358 /* Do not report missing versions */
359 if (ctx->buf[0] == '\0')
363 case ICE_VERSION_FIXED:
364 err = devlink_info_version_fixed_put(req, key, ctx->buf);
366 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
370 case ICE_VERSION_RUNNING:
371 err = devlink_info_version_running_put(req, key, ctx->buf);
373 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
377 case ICE_VERSION_STORED:
378 err = devlink_info_version_stored_put(req, key, ctx->buf);
380 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
393 * ice_devlink_reload_empr_start - Start EMP reset to activate new firmware
394 * @pf: pointer to the pf instance
395 * @extack: netlink extended ACK structure
397 * Allow user to activate new Embedded Management Processor firmware by
398 * issuing device specific EMP reset. Called in response to
399 * a DEVLINK_CMD_RELOAD with the DEVLINK_RELOAD_ACTION_FW_ACTIVATE.
401 * Note that teardown and rebuild of the driver state happens automatically as
402 * part of an interrupt and watchdog task. This is because all physical
403 * functions on the device must be able to reset when an EMP reset occurs from
407 ice_devlink_reload_empr_start(struct ice_pf *pf,
408 struct netlink_ext_ack *extack)
410 struct device *dev = ice_pf_to_dev(pf);
411 struct ice_hw *hw = &pf->hw;
415 err = ice_get_pending_updates(pf, &pending, extack);
419 /* pending is a bitmask of which flash banks have a pending update,
420 * including the main NVM bank, the Option ROM bank, and the netlist
421 * bank. If any of these bits are set, then there is a pending update
422 * waiting to be activated.
425 NL_SET_ERR_MSG_MOD(extack, "No pending firmware update");
429 if (pf->fw_emp_reset_disabled) {
430 NL_SET_ERR_MSG_MOD(extack, "EMP reset is not available. To activate firmware, a reboot or power cycle is needed");
434 dev_dbg(dev, "Issuing device EMP reset to activate firmware\n");
436 err = ice_aq_nvm_update_empr(hw);
438 dev_err(dev, "Failed to trigger EMP device reset to reload firmware, err %d aq_err %s\n",
439 err, ice_aq_str(hw->adminq.sq_last_status));
440 NL_SET_ERR_MSG_MOD(extack, "Failed to trigger EMP device reset to reload firmware");
448 * ice_devlink_reinit_down - unload given PF
449 * @pf: pointer to the PF struct
451 static void ice_devlink_reinit_down(struct ice_pf *pf)
453 /* No need to take devl_lock, it's already taken by devlink API */
456 ice_vsi_decfg(ice_get_main_vsi(pf));
462 * ice_devlink_reload_down - prepare for reload
463 * @devlink: pointer to the devlink instance to reload
464 * @netns_change: if true, the network namespace is changing
465 * @action: the action to perform
466 * @limit: limits on what reload should do, such as not resetting
467 * @extack: netlink extended ACK structure
470 ice_devlink_reload_down(struct devlink *devlink, bool netns_change,
471 enum devlink_reload_action action,
472 enum devlink_reload_limit limit,
473 struct netlink_ext_ack *extack)
475 struct ice_pf *pf = devlink_priv(devlink);
478 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
479 if (ice_is_eswitch_mode_switchdev(pf)) {
480 NL_SET_ERR_MSG_MOD(extack,
481 "Go to legacy mode before doing reinit\n");
484 if (ice_is_adq_active(pf)) {
485 NL_SET_ERR_MSG_MOD(extack,
486 "Turn off ADQ before doing reinit\n");
489 if (ice_has_vfs(pf)) {
490 NL_SET_ERR_MSG_MOD(extack,
491 "Remove all VFs before doing reinit\n");
494 ice_devlink_reinit_down(pf);
496 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
497 return ice_devlink_reload_empr_start(pf, extack);
505 * ice_devlink_reload_empr_finish - Wait for EMP reset to finish
506 * @pf: pointer to the pf instance
507 * @extack: netlink extended ACK structure
509 * Wait for driver to finish rebuilding after EMP reset is completed. This
510 * includes time to wait for both the actual device reset as well as the time
511 * for the driver's rebuild to complete.
514 ice_devlink_reload_empr_finish(struct ice_pf *pf,
515 struct netlink_ext_ack *extack)
519 err = ice_wait_for_reset(pf, 60 * HZ);
521 NL_SET_ERR_MSG_MOD(extack, "Device still resetting after 1 minute");
529 * ice_devlink_port_opt_speed_str - convert speed to a string
530 * @speed: speed value
532 static const char *ice_devlink_port_opt_speed_str(u8 speed)
534 switch (speed & ICE_AQC_PORT_OPT_MAX_LANE_M) {
535 case ICE_AQC_PORT_OPT_MAX_LANE_100M:
537 case ICE_AQC_PORT_OPT_MAX_LANE_1G:
539 case ICE_AQC_PORT_OPT_MAX_LANE_2500M:
541 case ICE_AQC_PORT_OPT_MAX_LANE_5G:
543 case ICE_AQC_PORT_OPT_MAX_LANE_10G:
545 case ICE_AQC_PORT_OPT_MAX_LANE_25G:
547 case ICE_AQC_PORT_OPT_MAX_LANE_50G:
549 case ICE_AQC_PORT_OPT_MAX_LANE_100G:
556 #define ICE_PORT_OPT_DESC_LEN 50
558 * ice_devlink_port_options_print - Print available port split options
559 * @pf: the PF to print split port options
561 * Prints a table with available port split options and max port speeds
563 static void ice_devlink_port_options_print(struct ice_pf *pf)
565 u8 i, j, options_count, cnt, speed, pending_idx, active_idx;
566 struct ice_aqc_get_port_options_elem *options, *opt;
567 struct device *dev = ice_pf_to_dev(pf);
568 bool active_valid, pending_valid;
569 char desc[ICE_PORT_OPT_DESC_LEN];
573 options = kcalloc(ICE_AQC_PORT_OPT_MAX * ICE_MAX_PORT_PER_PCI_DEV,
574 sizeof(*options), GFP_KERNEL);
578 for (i = 0; i < ICE_MAX_PORT_PER_PCI_DEV; i++) {
579 opt = options + i * ICE_AQC_PORT_OPT_MAX;
580 options_count = ICE_AQC_PORT_OPT_MAX;
583 status = ice_aq_get_port_options(&pf->hw, opt, &options_count,
584 i, true, &active_idx,
585 &active_valid, &pending_idx,
588 dev_dbg(dev, "Couldn't read port option for port %d, err %d\n",
594 dev_dbg(dev, "Available port split options and max port speeds (Gbps):\n");
595 dev_dbg(dev, "Status Split Quad 0 Quad 1\n");
596 dev_dbg(dev, " count L0 L1 L2 L3 L4 L5 L6 L7\n");
598 for (i = 0; i < options_count; i++) {
601 if (i == ice_active_port_option)
603 else if ((i == pending_idx) && pending_valid)
608 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
611 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
612 "%-6u", options[i].pmd);
614 for (j = 0; j < ICE_MAX_PORT_PER_PCI_DEV; ++j) {
615 speed = options[i + j * ICE_AQC_PORT_OPT_MAX].max_lane_speed;
616 str = ice_devlink_port_opt_speed_str(speed);
617 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
621 dev_dbg(dev, "%s\n", desc);
629 * ice_devlink_aq_set_port_option - Send set port option admin queue command
630 * @pf: the PF to print split port options
631 * @option_idx: selected port option
632 * @extack: extended netdev ack structure
634 * Sends set port option admin queue command with selected port option and
635 * calls NVM write activate.
638 ice_devlink_aq_set_port_option(struct ice_pf *pf, u8 option_idx,
639 struct netlink_ext_ack *extack)
641 struct device *dev = ice_pf_to_dev(pf);
644 status = ice_aq_set_port_option(&pf->hw, 0, true, option_idx);
646 dev_dbg(dev, "ice_aq_set_port_option, err %d aq_err %d\n",
647 status, pf->hw.adminq.sq_last_status);
648 NL_SET_ERR_MSG_MOD(extack, "Port split request failed");
652 status = ice_acquire_nvm(&pf->hw, ICE_RES_WRITE);
654 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
655 status, pf->hw.adminq.sq_last_status);
656 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
660 status = ice_nvm_write_activate(&pf->hw, ICE_AQC_NVM_ACTIV_REQ_EMPR, NULL);
662 dev_dbg(dev, "ice_nvm_write_activate failed, err %d aq_err %d\n",
663 status, pf->hw.adminq.sq_last_status);
664 NL_SET_ERR_MSG_MOD(extack, "Port split request failed to save data");
665 ice_release_nvm(&pf->hw);
669 ice_release_nvm(&pf->hw);
671 NL_SET_ERR_MSG_MOD(extack, "Reboot required to finish port split");
676 * ice_devlink_port_split - .port_split devlink handler
677 * @devlink: devlink instance structure
678 * @port: devlink port structure
679 * @count: number of ports to split to
680 * @extack: extended netdev ack structure
682 * Callback for the devlink .port_split operation.
684 * Unfortunately, the devlink expression of available options is limited
685 * to just a number, so search for an FW port option which supports
686 * the specified number. As there could be multiple FW port options with
687 * the same port split count, allow switching between them. When the same
688 * port split count request is issued again, switch to the next FW port
689 * option with the same port split count.
691 * Return: zero on success or an error code on failure.
694 ice_devlink_port_split(struct devlink *devlink, struct devlink_port *port,
695 unsigned int count, struct netlink_ext_ack *extack)
697 struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
698 u8 i, j, active_idx, pending_idx, new_option;
699 struct ice_pf *pf = devlink_priv(devlink);
700 u8 option_count = ICE_AQC_PORT_OPT_MAX;
701 struct device *dev = ice_pf_to_dev(pf);
702 bool active_valid, pending_valid;
705 status = ice_aq_get_port_options(&pf->hw, options, &option_count,
706 0, true, &active_idx, &active_valid,
707 &pending_idx, &pending_valid);
709 dev_dbg(dev, "Couldn't read port split options, err = %d\n",
711 NL_SET_ERR_MSG_MOD(extack, "Failed to get available port split options");
715 new_option = ICE_AQC_PORT_OPT_MAX;
716 active_idx = pending_valid ? pending_idx : active_idx;
717 for (i = 1; i <= option_count; i++) {
718 /* In order to allow switching between FW port options with
719 * the same port split count, search for a new option starting
720 * from the active/pending option (with array wrap around).
722 j = (active_idx + i) % option_count;
724 if (count == options[j].pmd) {
730 if (new_option == active_idx) {
731 dev_dbg(dev, "request to split: count: %u is already set and there are no other options\n",
733 NL_SET_ERR_MSG_MOD(extack, "Requested split count is already set");
734 ice_devlink_port_options_print(pf);
738 if (new_option == ICE_AQC_PORT_OPT_MAX) {
739 dev_dbg(dev, "request to split: count: %u not found\n", count);
740 NL_SET_ERR_MSG_MOD(extack, "Port split requested unsupported port config");
741 ice_devlink_port_options_print(pf);
745 status = ice_devlink_aq_set_port_option(pf, new_option, extack);
749 ice_devlink_port_options_print(pf);
755 * ice_devlink_port_unsplit - .port_unsplit devlink handler
756 * @devlink: devlink instance structure
757 * @port: devlink port structure
758 * @extack: extended netdev ack structure
760 * Callback for the devlink .port_unsplit operation.
761 * Calls ice_devlink_port_split with split count set to 1.
762 * There could be no FW option available with split count 1.
764 * Return: zero on success or an error code on failure.
767 ice_devlink_port_unsplit(struct devlink *devlink, struct devlink_port *port,
768 struct netlink_ext_ack *extack)
770 return ice_devlink_port_split(devlink, port, 1, extack);
774 * ice_tear_down_devlink_rate_tree - removes devlink-rate exported tree
777 * This function tears down tree exported during VF's creation.
779 void ice_tear_down_devlink_rate_tree(struct ice_pf *pf)
781 struct devlink *devlink;
785 devlink = priv_to_devlink(pf);
788 mutex_lock(&pf->vfs.table_lock);
789 ice_for_each_vf(pf, bkt, vf) {
790 if (vf->devlink_port.devlink_rate)
791 devl_rate_leaf_destroy(&vf->devlink_port);
793 mutex_unlock(&pf->vfs.table_lock);
795 devl_rate_nodes_destroy(devlink);
796 devl_unlock(devlink);
800 * ice_enable_custom_tx - try to enable custom Tx feature
803 * This function tries to enable custom Tx feature,
804 * it's not possible to enable it, if DCB or ADQ is active.
806 static bool ice_enable_custom_tx(struct ice_pf *pf)
808 struct ice_port_info *pi = ice_get_main_vsi(pf)->port_info;
809 struct device *dev = ice_pf_to_dev(pf);
811 if (pi->is_custom_tx_enabled)
812 /* already enabled, return true */
815 if (ice_is_adq_active(pf)) {
816 dev_err(dev, "ADQ active, can't modify Tx scheduler tree\n");
820 if (ice_is_dcb_active(pf)) {
821 dev_err(dev, "DCB active, can't modify Tx scheduler tree\n");
825 pi->is_custom_tx_enabled = true;
831 * ice_traverse_tx_tree - traverse Tx scheduler tree
832 * @devlink: devlink struct
833 * @node: current node, used for recursion
834 * @tc_node: tc_node struct, that is treated as a root
837 * This function traverses Tx scheduler tree and exports
838 * entire structure to the devlink-rate.
840 static void ice_traverse_tx_tree(struct devlink *devlink, struct ice_sched_node *node,
841 struct ice_sched_node *tc_node, struct ice_pf *pf)
843 struct devlink_rate *rate_node = NULL;
848 /* already added, skip to the next */
849 goto traverse_children;
851 if (node->parent == tc_node) {
852 /* create root node */
853 rate_node = devl_rate_node_create(devlink, node, node->name, NULL);
854 } else if (node->vsi_handle &&
855 pf->vsi[node->vsi_handle]->vf) {
856 vf = pf->vsi[node->vsi_handle]->vf;
857 if (!vf->devlink_port.devlink_rate)
858 /* leaf nodes doesn't have children
859 * so we don't set rate_node
861 devl_rate_leaf_create(&vf->devlink_port, node,
862 node->parent->rate_node);
863 } else if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF &&
864 node->parent->rate_node) {
865 rate_node = devl_rate_node_create(devlink, node, node->name,
866 node->parent->rate_node);
869 if (rate_node && !IS_ERR(rate_node))
870 node->rate_node = rate_node;
873 for (i = 0; i < node->num_children; i++)
874 ice_traverse_tx_tree(devlink, node->children[i], tc_node, pf);
878 * ice_devlink_rate_init_tx_topology - export Tx scheduler tree to devlink rate
879 * @devlink: devlink struct
880 * @vsi: main vsi struct
882 * This function finds a root node, then calls ice_traverse_tx tree, which
883 * traverses the tree and exports it's contents to devlink rate.
885 int ice_devlink_rate_init_tx_topology(struct devlink *devlink, struct ice_vsi *vsi)
887 struct ice_port_info *pi = vsi->port_info;
888 struct ice_sched_node *tc_node;
889 struct ice_pf *pf = vsi->back;
892 tc_node = pi->root->children[0];
893 mutex_lock(&pi->sched_lock);
895 for (i = 0; i < tc_node->num_children; i++)
896 ice_traverse_tx_tree(devlink, tc_node->children[i], tc_node, pf);
897 devl_unlock(devlink);
898 mutex_unlock(&pi->sched_lock);
903 static void ice_clear_rate_nodes(struct ice_sched_node *node)
905 node->rate_node = NULL;
907 for (int i = 0; i < node->num_children; i++)
908 ice_clear_rate_nodes(node->children[i]);
912 * ice_devlink_rate_clear_tx_topology - clear node->rate_node
913 * @vsi: main vsi struct
915 * Clear rate_node to cleanup creation of Tx topology.
918 void ice_devlink_rate_clear_tx_topology(struct ice_vsi *vsi)
920 struct ice_port_info *pi = vsi->port_info;
922 mutex_lock(&pi->sched_lock);
923 ice_clear_rate_nodes(pi->root->children[0]);
924 mutex_unlock(&pi->sched_lock);
928 * ice_set_object_tx_share - sets node scheduling parameter
929 * @pi: devlink struct instance
930 * @node: node struct instance
931 * @bw: bandwidth in bytes per second
932 * @extack: extended netdev ack structure
934 * This function sets ICE_MIN_BW scheduling BW limit.
936 static int ice_set_object_tx_share(struct ice_port_info *pi, struct ice_sched_node *node,
937 u64 bw, struct netlink_ext_ack *extack)
941 mutex_lock(&pi->sched_lock);
942 /* converts bytes per second to kilo bits per second */
943 node->tx_share = div_u64(bw, 125);
944 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW, node->tx_share);
945 mutex_unlock(&pi->sched_lock);
948 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_share");
954 * ice_set_object_tx_max - sets node scheduling parameter
955 * @pi: devlink struct instance
956 * @node: node struct instance
957 * @bw: bandwidth in bytes per second
958 * @extack: extended netdev ack structure
960 * This function sets ICE_MAX_BW scheduling BW limit.
962 static int ice_set_object_tx_max(struct ice_port_info *pi, struct ice_sched_node *node,
963 u64 bw, struct netlink_ext_ack *extack)
967 mutex_lock(&pi->sched_lock);
968 /* converts bytes per second value to kilo bits per second */
969 node->tx_max = div_u64(bw, 125);
970 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW, node->tx_max);
971 mutex_unlock(&pi->sched_lock);
974 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_max");
980 * ice_set_object_tx_priority - sets node scheduling parameter
981 * @pi: devlink struct instance
982 * @node: node struct instance
983 * @priority: value representing priority for strict priority arbitration
984 * @extack: extended netdev ack structure
986 * This function sets priority of node among siblings.
988 static int ice_set_object_tx_priority(struct ice_port_info *pi, struct ice_sched_node *node,
989 u32 priority, struct netlink_ext_ack *extack)
994 NL_SET_ERR_MSG_MOD(extack, "Priority should be less than 8");
998 mutex_lock(&pi->sched_lock);
999 node->tx_priority = priority;
1000 status = ice_sched_set_node_priority(pi, node, node->tx_priority);
1001 mutex_unlock(&pi->sched_lock);
1004 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_priority");
1010 * ice_set_object_tx_weight - sets node scheduling parameter
1011 * @pi: devlink struct instance
1012 * @node: node struct instance
1013 * @weight: value represeting relative weight for WFQ arbitration
1014 * @extack: extended netdev ack structure
1016 * This function sets node weight for WFQ algorithm.
1018 static int ice_set_object_tx_weight(struct ice_port_info *pi, struct ice_sched_node *node,
1019 u32 weight, struct netlink_ext_ack *extack)
1023 if (weight > 200 || weight < 1) {
1024 NL_SET_ERR_MSG_MOD(extack, "Weight must be between 1 and 200");
1028 mutex_lock(&pi->sched_lock);
1029 node->tx_weight = weight;
1030 status = ice_sched_set_node_weight(pi, node, node->tx_weight);
1031 mutex_unlock(&pi->sched_lock);
1034 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_weight");
1040 * ice_get_pi_from_dev_rate - get port info from devlink_rate
1041 * @rate_node: devlink struct instance
1043 * This function returns corresponding port_info struct of devlink_rate
1045 static struct ice_port_info *ice_get_pi_from_dev_rate(struct devlink_rate *rate_node)
1047 struct ice_pf *pf = devlink_priv(rate_node->devlink);
1049 return ice_get_main_vsi(pf)->port_info;
1052 static int ice_devlink_rate_node_new(struct devlink_rate *rate_node, void **priv,
1053 struct netlink_ext_ack *extack)
1055 struct ice_sched_node *node;
1056 struct ice_port_info *pi;
1058 pi = ice_get_pi_from_dev_rate(rate_node);
1060 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1063 /* preallocate memory for ice_sched_node */
1064 node = devm_kzalloc(ice_hw_to_dev(pi->hw), sizeof(*node), GFP_KERNEL);
1070 static int ice_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,
1071 struct netlink_ext_ack *extack)
1073 struct ice_sched_node *node, *tc_node;
1074 struct ice_port_info *pi;
1076 pi = ice_get_pi_from_dev_rate(rate_node);
1077 tc_node = pi->root->children[0];
1080 if (!rate_node->parent || !node || tc_node == node || !extack)
1083 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1086 /* can't allow to delete a node with children */
1087 if (node->num_children)
1090 mutex_lock(&pi->sched_lock);
1091 ice_free_sched_node(pi, node);
1092 mutex_unlock(&pi->sched_lock);
1097 static int ice_devlink_rate_leaf_tx_max_set(struct devlink_rate *rate_leaf, void *priv,
1098 u64 tx_max, struct netlink_ext_ack *extack)
1100 struct ice_sched_node *node = priv;
1102 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1108 return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_leaf),
1109 node, tx_max, extack);
1112 static int ice_devlink_rate_leaf_tx_share_set(struct devlink_rate *rate_leaf, void *priv,
1113 u64 tx_share, struct netlink_ext_ack *extack)
1115 struct ice_sched_node *node = priv;
1117 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1123 return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_leaf), node,
1127 static int ice_devlink_rate_leaf_tx_priority_set(struct devlink_rate *rate_leaf, void *priv,
1128 u32 tx_priority, struct netlink_ext_ack *extack)
1130 struct ice_sched_node *node = priv;
1132 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1138 return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_leaf), node,
1139 tx_priority, extack);
1142 static int ice_devlink_rate_leaf_tx_weight_set(struct devlink_rate *rate_leaf, void *priv,
1143 u32 tx_weight, struct netlink_ext_ack *extack)
1145 struct ice_sched_node *node = priv;
1147 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1153 return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_leaf), node,
1157 static int ice_devlink_rate_node_tx_max_set(struct devlink_rate *rate_node, void *priv,
1158 u64 tx_max, struct netlink_ext_ack *extack)
1160 struct ice_sched_node *node = priv;
1162 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1168 return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_node),
1169 node, tx_max, extack);
1172 static int ice_devlink_rate_node_tx_share_set(struct devlink_rate *rate_node, void *priv,
1173 u64 tx_share, struct netlink_ext_ack *extack)
1175 struct ice_sched_node *node = priv;
1177 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1183 return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_node),
1184 node, tx_share, extack);
1187 static int ice_devlink_rate_node_tx_priority_set(struct devlink_rate *rate_node, void *priv,
1188 u32 tx_priority, struct netlink_ext_ack *extack)
1190 struct ice_sched_node *node = priv;
1192 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1198 return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_node),
1199 node, tx_priority, extack);
1202 static int ice_devlink_rate_node_tx_weight_set(struct devlink_rate *rate_node, void *priv,
1203 u32 tx_weight, struct netlink_ext_ack *extack)
1205 struct ice_sched_node *node = priv;
1207 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1213 return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_node),
1214 node, tx_weight, extack);
1217 static int ice_devlink_set_parent(struct devlink_rate *devlink_rate,
1218 struct devlink_rate *parent,
1219 void *priv, void *parent_priv,
1220 struct netlink_ext_ack *extack)
1222 struct ice_port_info *pi = ice_get_pi_from_dev_rate(devlink_rate);
1223 struct ice_sched_node *tc_node, *node, *parent_node;
1224 u16 num_nodes_added;
1225 u32 first_node_teid;
1229 tc_node = pi->root->children[0];
1235 if (!ice_enable_custom_tx(devlink_priv(devlink_rate->devlink)))
1239 if (!node || tc_node == node || node->num_children)
1242 mutex_lock(&pi->sched_lock);
1243 ice_free_sched_node(pi, node);
1244 mutex_unlock(&pi->sched_lock);
1249 parent_node = parent_priv;
1251 /* if the node doesn't exist, create it */
1252 if (!node->parent) {
1253 mutex_lock(&pi->sched_lock);
1254 status = ice_sched_add_elems(pi, tc_node, parent_node,
1255 parent_node->tx_sched_layer + 1,
1256 1, &num_nodes_added, &first_node_teid,
1258 mutex_unlock(&pi->sched_lock);
1261 NL_SET_ERR_MSG_MOD(extack, "Can't add a new node");
1265 if (devlink_rate->tx_share)
1266 ice_set_object_tx_share(pi, node, devlink_rate->tx_share, extack);
1267 if (devlink_rate->tx_max)
1268 ice_set_object_tx_max(pi, node, devlink_rate->tx_max, extack);
1269 if (devlink_rate->tx_priority)
1270 ice_set_object_tx_priority(pi, node, devlink_rate->tx_priority, extack);
1271 if (devlink_rate->tx_weight)
1272 ice_set_object_tx_weight(pi, node, devlink_rate->tx_weight, extack);
1274 node_teid = le32_to_cpu(node->info.node_teid);
1275 mutex_lock(&pi->sched_lock);
1276 status = ice_sched_move_nodes(pi, parent_node, 1, &node_teid);
1277 mutex_unlock(&pi->sched_lock);
1280 NL_SET_ERR_MSG_MOD(extack, "Can't move existing node to a new parent");
1287 * ice_devlink_reinit_up - do reinit of the given PF
1288 * @pf: pointer to the PF struct
1290 static int ice_devlink_reinit_up(struct ice_pf *pf)
1292 struct ice_vsi *vsi = ice_get_main_vsi(pf);
1293 struct ice_vsi_cfg_params params;
1296 err = ice_init_dev(pf);
1300 params = ice_vsi_to_params(vsi);
1301 params.flags = ICE_VSI_FLAG_INIT;
1304 err = ice_vsi_cfg(vsi, ¶ms);
1309 /* No need to take devl_lock, it's already taken by devlink API */
1326 * ice_devlink_reload_up - do reload up after reinit
1327 * @devlink: pointer to the devlink instance reloading
1328 * @action: the action requested
1329 * @limit: limits imposed by userspace, such as not resetting
1330 * @actions_performed: on return, indicate what actions actually performed
1331 * @extack: netlink extended ACK structure
1334 ice_devlink_reload_up(struct devlink *devlink,
1335 enum devlink_reload_action action,
1336 enum devlink_reload_limit limit,
1337 u32 *actions_performed,
1338 struct netlink_ext_ack *extack)
1340 struct ice_pf *pf = devlink_priv(devlink);
1343 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
1344 *actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
1345 return ice_devlink_reinit_up(pf);
1346 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
1347 *actions_performed = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE);
1348 return ice_devlink_reload_empr_finish(pf, extack);
1355 static const struct devlink_ops ice_devlink_ops = {
1356 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
1357 .reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
1358 BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
1359 .reload_down = ice_devlink_reload_down,
1360 .reload_up = ice_devlink_reload_up,
1361 .eswitch_mode_get = ice_eswitch_mode_get,
1362 .eswitch_mode_set = ice_eswitch_mode_set,
1363 .info_get = ice_devlink_info_get,
1364 .flash_update = ice_devlink_flash_update,
1366 .rate_node_new = ice_devlink_rate_node_new,
1367 .rate_node_del = ice_devlink_rate_node_del,
1369 .rate_leaf_tx_max_set = ice_devlink_rate_leaf_tx_max_set,
1370 .rate_leaf_tx_share_set = ice_devlink_rate_leaf_tx_share_set,
1371 .rate_leaf_tx_priority_set = ice_devlink_rate_leaf_tx_priority_set,
1372 .rate_leaf_tx_weight_set = ice_devlink_rate_leaf_tx_weight_set,
1374 .rate_node_tx_max_set = ice_devlink_rate_node_tx_max_set,
1375 .rate_node_tx_share_set = ice_devlink_rate_node_tx_share_set,
1376 .rate_node_tx_priority_set = ice_devlink_rate_node_tx_priority_set,
1377 .rate_node_tx_weight_set = ice_devlink_rate_node_tx_weight_set,
1379 .rate_leaf_parent_set = ice_devlink_set_parent,
1380 .rate_node_parent_set = ice_devlink_set_parent,
1384 ice_devlink_enable_roce_get(struct devlink *devlink, u32 id,
1385 struct devlink_param_gset_ctx *ctx)
1387 struct ice_pf *pf = devlink_priv(devlink);
1389 ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ? true : false;
1395 ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
1396 struct devlink_param_gset_ctx *ctx)
1398 struct ice_pf *pf = devlink_priv(devlink);
1399 bool roce_ena = ctx->val.vbool;
1403 ice_unplug_aux_dev(pf);
1404 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
1408 pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2;
1409 ret = ice_plug_aux_dev(pf);
1411 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
1417 ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
1418 union devlink_param_value val,
1419 struct netlink_ext_ack *extack)
1421 struct ice_pf *pf = devlink_priv(devlink);
1423 if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
1426 if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) {
1427 NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
1435 ice_devlink_enable_iw_get(struct devlink *devlink, u32 id,
1436 struct devlink_param_gset_ctx *ctx)
1438 struct ice_pf *pf = devlink_priv(devlink);
1440 ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP;
1446 ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
1447 struct devlink_param_gset_ctx *ctx)
1449 struct ice_pf *pf = devlink_priv(devlink);
1450 bool iw_ena = ctx->val.vbool;
1454 ice_unplug_aux_dev(pf);
1455 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
1459 pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP;
1460 ret = ice_plug_aux_dev(pf);
1462 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
1468 ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id,
1469 union devlink_param_value val,
1470 struct netlink_ext_ack *extack)
1472 struct ice_pf *pf = devlink_priv(devlink);
1474 if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
1477 if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) {
1478 NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
1485 static const struct devlink_param ice_devlink_params[] = {
1486 DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1487 ice_devlink_enable_roce_get,
1488 ice_devlink_enable_roce_set,
1489 ice_devlink_enable_roce_validate),
1490 DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1491 ice_devlink_enable_iw_get,
1492 ice_devlink_enable_iw_set,
1493 ice_devlink_enable_iw_validate),
1497 static void ice_devlink_free(void *devlink_ptr)
1499 devlink_free((struct devlink *)devlink_ptr);
1503 * ice_allocate_pf - Allocate devlink and return PF structure pointer
1504 * @dev: the device to allocate for
1506 * Allocate a devlink instance for this device and return the private area as
1507 * the PF structure. The devlink memory is kept track of through devres by
1508 * adding an action to remove it when unwinding.
1510 struct ice_pf *ice_allocate_pf(struct device *dev)
1512 struct devlink *devlink;
1514 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
1518 /* Add an action to teardown the devlink when unwinding the driver */
1519 if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
1522 return devlink_priv(devlink);
1526 * ice_devlink_register - Register devlink interface for this PF
1527 * @pf: the PF to register the devlink for.
1529 * Register the devlink instance associated with this physical function.
1531 * Return: zero on success or an error code on failure.
1533 void ice_devlink_register(struct ice_pf *pf)
1535 struct devlink *devlink = priv_to_devlink(pf);
1537 devlink_register(devlink);
1541 * ice_devlink_unregister - Unregister devlink resources for this PF.
1542 * @pf: the PF structure to cleanup
1544 * Releases resources used by devlink and cleans up associated memory.
1546 void ice_devlink_unregister(struct ice_pf *pf)
1548 devlink_unregister(priv_to_devlink(pf));
1552 * ice_devlink_set_switch_id - Set unique switch id based on pci dsn
1553 * @pf: the PF to create a devlink port for
1554 * @ppid: struct with switch id information
1557 ice_devlink_set_switch_id(struct ice_pf *pf, struct netdev_phys_item_id *ppid)
1559 struct pci_dev *pdev = pf->pdev;
1562 id = pci_get_dsn(pdev);
1564 ppid->id_len = sizeof(id);
1565 put_unaligned_be64(id, &ppid->id);
1568 int ice_devlink_register_params(struct ice_pf *pf)
1570 struct devlink *devlink = priv_to_devlink(pf);
1572 return devlink_params_register(devlink, ice_devlink_params,
1573 ARRAY_SIZE(ice_devlink_params));
1576 void ice_devlink_unregister_params(struct ice_pf *pf)
1578 devlink_params_unregister(priv_to_devlink(pf), ice_devlink_params,
1579 ARRAY_SIZE(ice_devlink_params));
1583 * ice_devlink_set_port_split_options - Set port split options
1584 * @pf: the PF to set port split options
1585 * @attrs: devlink attributes
1587 * Sets devlink port split options based on available FW port options
1590 ice_devlink_set_port_split_options(struct ice_pf *pf,
1591 struct devlink_port_attrs *attrs)
1593 struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
1594 u8 i, active_idx, pending_idx, option_count = ICE_AQC_PORT_OPT_MAX;
1595 bool active_valid, pending_valid;
1598 status = ice_aq_get_port_options(&pf->hw, options, &option_count,
1599 0, true, &active_idx, &active_valid,
1600 &pending_idx, &pending_valid);
1602 dev_dbg(ice_pf_to_dev(pf), "Couldn't read port split options, err = %d\n",
1607 /* find the biggest available port split count */
1608 for (i = 0; i < option_count; i++)
1609 attrs->lanes = max_t(int, attrs->lanes, options[i].pmd);
1611 attrs->splittable = attrs->lanes ? 1 : 0;
1612 ice_active_port_option = active_idx;
1615 static const struct devlink_port_ops ice_devlink_port_ops = {
1616 .port_split = ice_devlink_port_split,
1617 .port_unsplit = ice_devlink_port_unsplit,
1621 * ice_devlink_create_pf_port - Create a devlink port for this PF
1622 * @pf: the PF to create a devlink port for
1624 * Create and register a devlink_port for this PF.
1625 * This function has to be called under devl_lock.
1627 * Return: zero on success or an error code on failure.
1629 int ice_devlink_create_pf_port(struct ice_pf *pf)
1631 struct devlink_port_attrs attrs = {};
1632 struct devlink_port *devlink_port;
1633 struct devlink *devlink;
1634 struct ice_vsi *vsi;
1638 devlink = priv_to_devlink(pf);
1640 dev = ice_pf_to_dev(pf);
1642 devlink_port = &pf->devlink_port;
1644 vsi = ice_get_main_vsi(pf);
1648 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
1649 attrs.phys.port_number = pf->hw.bus.func;
1651 /* As FW supports only port split options for whole device,
1652 * set port split options only for first PF.
1654 if (pf->hw.pf_id == 0)
1655 ice_devlink_set_port_split_options(pf, &attrs);
1657 ice_devlink_set_switch_id(pf, &attrs.switch_id);
1659 devlink_port_attrs_set(devlink_port, &attrs);
1661 err = devl_port_register_with_ops(devlink, devlink_port, vsi->idx,
1662 &ice_devlink_port_ops);
1664 dev_err(dev, "Failed to create devlink port for PF %d, error %d\n",
1673 * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF
1674 * @pf: the PF to cleanup
1676 * Unregisters the devlink_port structure associated with this PF.
1677 * This function has to be called under devl_lock.
1679 void ice_devlink_destroy_pf_port(struct ice_pf *pf)
1681 devl_port_unregister(&pf->devlink_port);
1685 * ice_devlink_create_vf_port - Create a devlink port for this VF
1686 * @vf: the VF to create a port for
1688 * Create and register a devlink_port for this VF.
1690 * Return: zero on success or an error code on failure.
1692 int ice_devlink_create_vf_port(struct ice_vf *vf)
1694 struct devlink_port_attrs attrs = {};
1695 struct devlink_port *devlink_port;
1696 struct devlink *devlink;
1697 struct ice_vsi *vsi;
1703 dev = ice_pf_to_dev(pf);
1704 devlink_port = &vf->devlink_port;
1706 vsi = ice_get_vf_vsi(vf);
1710 attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
1711 attrs.pci_vf.pf = pf->hw.bus.func;
1712 attrs.pci_vf.vf = vf->vf_id;
1714 ice_devlink_set_switch_id(pf, &attrs.switch_id);
1716 devlink_port_attrs_set(devlink_port, &attrs);
1717 devlink = priv_to_devlink(pf);
1719 err = devlink_port_register(devlink, devlink_port, vsi->idx);
1721 dev_err(dev, "Failed to create devlink port for VF %d, error %d\n",
1730 * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF
1731 * @vf: the VF to cleanup
1733 * Unregisters the devlink_port structure associated with this VF.
1735 void ice_devlink_destroy_vf_port(struct ice_vf *vf)
1737 devl_rate_leaf_destroy(&vf->devlink_port);
1738 devlink_port_unregister(&vf->devlink_port);
1741 #define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
1743 static const struct devlink_region_ops ice_nvm_region_ops;
1744 static const struct devlink_region_ops ice_sram_region_ops;
1747 * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
1748 * @devlink: the devlink instance
1749 * @ops: the devlink region to snapshot
1750 * @extack: extended ACK response structure
1751 * @data: on exit points to snapshot data buffer
1753 * This function is called in response to a DEVLINK_CMD_REGION_NEW for either
1754 * the nvm-flash or shadow-ram region.
1756 * It captures a snapshot of the NVM or Shadow RAM flash contents. This
1757 * snapshot can then later be viewed via the DEVLINK_CMD_REGION_READ netlink
1760 * @returns zero on success, and updates the data pointer. Returns a non-zero
1761 * error code on failure.
1763 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
1764 const struct devlink_region_ops *ops,
1765 struct netlink_ext_ack *extack, u8 **data)
1767 struct ice_pf *pf = devlink_priv(devlink);
1768 struct device *dev = ice_pf_to_dev(pf);
1769 struct ice_hw *hw = &pf->hw;
1770 bool read_shadow_ram;
1771 u8 *nvm_data, *tmp, i;
1776 if (ops == &ice_nvm_region_ops) {
1777 read_shadow_ram = false;
1778 nvm_size = hw->flash.flash_size;
1779 } else if (ops == &ice_sram_region_ops) {
1780 read_shadow_ram = true;
1781 nvm_size = hw->flash.sr_words * 2u;
1783 NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1787 nvm_data = vzalloc(nvm_size);
1791 num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
1795 /* Some systems take longer to read the NVM than others which causes the
1796 * FW to reclaim the NVM lock before the entire NVM has been read. Fix
1797 * this by breaking the reads of the NVM into smaller chunks that will
1798 * probably not take as long. This has some overhead since we are
1799 * increasing the number of AQ commands, but it should always work
1801 for (i = 0; i < num_blks; i++) {
1802 u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
1804 status = ice_acquire_nvm(hw, ICE_RES_READ);
1806 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1807 status, hw->adminq.sq_last_status);
1808 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1813 status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
1814 &read_sz, tmp, read_shadow_ram);
1816 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1817 read_sz, status, hw->adminq.sq_last_status);
1818 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1819 ice_release_nvm(hw);
1823 ice_release_nvm(hw);
1835 * ice_devlink_nvm_read - Read a portion of NVM flash contents
1836 * @devlink: the devlink instance
1837 * @ops: the devlink region to snapshot
1838 * @extack: extended ACK response structure
1839 * @offset: the offset to start at
1840 * @size: the amount to read
1841 * @data: the data buffer to read into
1843 * This function is called in response to DEVLINK_CMD_REGION_READ to directly
1844 * read a section of the NVM contents.
1846 * It reads from either the nvm-flash or shadow-ram region contents.
1848 * @returns zero on success, and updates the data pointer. Returns a non-zero
1849 * error code on failure.
1851 static int ice_devlink_nvm_read(struct devlink *devlink,
1852 const struct devlink_region_ops *ops,
1853 struct netlink_ext_ack *extack,
1854 u64 offset, u32 size, u8 *data)
1856 struct ice_pf *pf = devlink_priv(devlink);
1857 struct device *dev = ice_pf_to_dev(pf);
1858 struct ice_hw *hw = &pf->hw;
1859 bool read_shadow_ram;
1863 if (ops == &ice_nvm_region_ops) {
1864 read_shadow_ram = false;
1865 nvm_size = hw->flash.flash_size;
1866 } else if (ops == &ice_sram_region_ops) {
1867 read_shadow_ram = true;
1868 nvm_size = hw->flash.sr_words * 2u;
1870 NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1874 if (offset + size >= nvm_size) {
1875 NL_SET_ERR_MSG_MOD(extack, "Cannot read beyond the region size");
1879 status = ice_acquire_nvm(hw, ICE_RES_READ);
1881 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1882 status, hw->adminq.sq_last_status);
1883 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1887 status = ice_read_flat_nvm(hw, (u32)offset, &size, data,
1890 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1891 size, status, hw->adminq.sq_last_status);
1892 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1893 ice_release_nvm(hw);
1896 ice_release_nvm(hw);
1902 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
1903 * @devlink: the devlink instance
1904 * @ops: the devlink region being snapshotted
1905 * @extack: extended ACK response structure
1906 * @data: on exit points to snapshot data buffer
1908 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
1909 * the device-caps devlink region. It captures a snapshot of the device
1910 * capabilities reported by firmware.
1912 * @returns zero on success, and updates the data pointer. Returns a non-zero
1913 * error code on failure.
1916 ice_devlink_devcaps_snapshot(struct devlink *devlink,
1917 const struct devlink_region_ops *ops,
1918 struct netlink_ext_ack *extack, u8 **data)
1920 struct ice_pf *pf = devlink_priv(devlink);
1921 struct device *dev = ice_pf_to_dev(pf);
1922 struct ice_hw *hw = &pf->hw;
1926 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
1930 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
1931 ice_aqc_opc_list_dev_caps, NULL);
1933 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
1934 status, hw->adminq.sq_last_status);
1935 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
1940 *data = (u8 *)devcaps;
1945 static const struct devlink_region_ops ice_nvm_region_ops = {
1946 .name = "nvm-flash",
1947 .destructor = vfree,
1948 .snapshot = ice_devlink_nvm_snapshot,
1949 .read = ice_devlink_nvm_read,
1952 static const struct devlink_region_ops ice_sram_region_ops = {
1953 .name = "shadow-ram",
1954 .destructor = vfree,
1955 .snapshot = ice_devlink_nvm_snapshot,
1956 .read = ice_devlink_nvm_read,
1959 static const struct devlink_region_ops ice_devcaps_region_ops = {
1960 .name = "device-caps",
1961 .destructor = vfree,
1962 .snapshot = ice_devlink_devcaps_snapshot,
1966 * ice_devlink_init_regions - Initialize devlink regions
1967 * @pf: the PF device structure
1969 * Create devlink regions used to enable access to dump the contents of the
1970 * flash memory on the device.
1972 void ice_devlink_init_regions(struct ice_pf *pf)
1974 struct devlink *devlink = priv_to_devlink(pf);
1975 struct device *dev = ice_pf_to_dev(pf);
1976 u64 nvm_size, sram_size;
1978 nvm_size = pf->hw.flash.flash_size;
1979 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
1981 if (IS_ERR(pf->nvm_region)) {
1982 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
1983 PTR_ERR(pf->nvm_region));
1984 pf->nvm_region = NULL;
1987 sram_size = pf->hw.flash.sr_words * 2u;
1988 pf->sram_region = devlink_region_create(devlink, &ice_sram_region_ops,
1990 if (IS_ERR(pf->sram_region)) {
1991 dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
1992 PTR_ERR(pf->sram_region));
1993 pf->sram_region = NULL;
1996 pf->devcaps_region = devlink_region_create(devlink,
1997 &ice_devcaps_region_ops, 10,
1998 ICE_AQ_MAX_BUF_LEN);
1999 if (IS_ERR(pf->devcaps_region)) {
2000 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
2001 PTR_ERR(pf->devcaps_region));
2002 pf->devcaps_region = NULL;
2007 * ice_devlink_destroy_regions - Destroy devlink regions
2008 * @pf: the PF device structure
2010 * Remove previously created regions for this PF.
2012 void ice_devlink_destroy_regions(struct ice_pf *pf)
2015 devlink_region_destroy(pf->nvm_region);
2017 if (pf->sram_region)
2018 devlink_region_destroy(pf->sram_region);
2020 if (pf->devcaps_region)
2021 devlink_region_destroy(pf->devcaps_region);