Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.13.7.
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2020, Intel Corporation. */
   3
   4#include <linux/vmalloc.h>
   5
   6#include "ice.h"
   7#include "ice_lib.h"
   8#include "ice_devlink.h"
   9#include "ice_eswitch.h"
  10#include "ice_fw_update.h"
  11#include "ice_dcb_lib.h"
  12
  13static int ice_active_port_option = -1;
  14
  15/* context for devlink info version reporting */
  16struct ice_info_ctx {
  17	char buf[128];
  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;
  22};
  23
  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
  27 * request was made.
  28 *
  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.
  32 */
  33
  34static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
  35{
  36	u8 dsn[8];
  37
  38	/* Copy the DSN into an array in Big Endian format */
  39	put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
  40
  41	snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
  42}
  43
  44static void ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
  45{
  46	struct ice_hw *hw = &pf->hw;
  47	int status;
  48
  49	status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
  50	if (status)
  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",
  53			status);
  54}
  55
  56static void ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
  57{
  58	struct ice_hw *hw = &pf->hw;
  59
  60	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
  61		 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
  62}
  63
  64static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
  65{
  66	struct ice_hw *hw = &pf->hw;
  67
  68	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
  69		 hw->api_min_ver, hw->api_patch);
  70}
  71
  72static void ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
  73{
  74	struct ice_hw *hw = &pf->hw;
  75
  76	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
  77}
  78
  79static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
  80{
  81	struct ice_orom_info *orom = &pf->hw.flash.orom;
  82
  83	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
  84		 orom->major, orom->build, orom->patch);
  85}
  86
  87static void
  88ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
  89			  struct ice_info_ctx *ctx)
  90{
  91	struct ice_orom_info *orom = &ctx->pending_orom;
  92
  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);
  96}
  97
  98static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
  99{
 100	struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
 101
 102	snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
 103}
 104
 105static void
 106ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
 107			 struct ice_info_ctx *ctx)
 108{
 109	struct ice_nvm_info *nvm = &ctx->pending_nvm;
 110
 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);
 114}
 115
 116static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
 117{
 118	struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
 119
 120	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
 121}
 122
 123static void
 124ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
 125{
 126	struct ice_nvm_info *nvm = &ctx->pending_nvm;
 127
 128	if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
 129		snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
 130}
 131
 132static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
 133{
 134	struct ice_hw *hw = &pf->hw;
 135
 136	snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
 137}
 138
 139static void
 140ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
 141{
 142	struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
 143
 144	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
 145		 pkg->major, pkg->minor, pkg->update, pkg->draft);
 146}
 147
 148static void
 149ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
 150{
 151	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
 152}
 153
 154static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
 155{
 156	struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
 157
 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);
 163}
 164
 165static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
 166{
 167	struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
 168
 169	snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
 170}
 171
 172static void
 173ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
 174			     struct ice_info_ctx *ctx)
 175{
 176	struct ice_netlist_info *netlist = &ctx->pending_netlist;
 177
 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);
 184}
 185
 186static void
 187ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
 188			       struct ice_info_ctx *ctx)
 189{
 190	struct ice_netlist_info *netlist = &ctx->pending_netlist;
 191
 192	if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
 193		snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
 194}
 195
 196static void ice_info_cgu_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
 197{
 198	u32 id, cfg_ver, fw_ver;
 199
 200	if (!ice_is_feature_supported(pf, ICE_F_CGU))
 201		return;
 202	if (ice_aq_get_cgu_info(&pf->hw, &id, &cfg_ver, &fw_ver))
 203		return;
 204	snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", id, cfg_ver, fw_ver);
 205}
 206
 207static void ice_info_cgu_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
 208{
 209	if (!ice_is_feature_supported(pf, ICE_F_CGU))
 210		return;
 211	snprintf(ctx->buf, sizeof(ctx->buf), "%u", pf->hw.cgu_part_number);
 212}
 213
 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 }
 217
 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.
 227 */
 228#define combined(key, active, pending) \
 229	running(key, active), \
 230	stored(key, pending, active)
 231
 232enum ice_version_type {
 233	ICE_VERSION_FIXED,
 234	ICE_VERSION_RUNNING,
 235	ICE_VERSION_STORED,
 236};
 237
 238static const struct ice_devlink_version {
 239	enum ice_version_type type;
 240	const char *key;
 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),
 258};
 259
 260/**
 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
 265 *
 266 * Callback for the devlink .info_get operation. Reports information about the
 267 * device.
 268 *
 269 * Return: zero on success or an error code on failure.
 270 */
 271static int ice_devlink_info_get(struct devlink *devlink,
 272				struct devlink_info_req *req,
 273				struct netlink_ext_ack *extack)
 274{
 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;
 279	size_t i;
 280	int err;
 281
 282	err = ice_wait_for_reset(pf, 10 * HZ);
 283	if (err) {
 284		NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
 285		return err;
 286	}
 287
 288	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 289	if (!ctx)
 290		return -ENOMEM;
 291
 292	/* discover capabilities first */
 293	err = ice_discover_dev_caps(hw, &ctx->dev_caps);
 294	if (err) {
 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");
 298		goto out_free_ctx;
 299	}
 300
 301	if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
 302		err = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
 303		if (err) {
 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));
 306
 307			/* disable display of pending Option ROM */
 308			ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
 309		}
 310	}
 311
 312	if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
 313		err = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
 314		if (err) {
 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));
 317
 318			/* disable display of pending Option ROM */
 319			ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
 320		}
 321	}
 322
 323	if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
 324		err = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
 325		if (err) {
 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));
 328
 329			/* disable display of pending Option ROM */
 330			ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
 331		}
 332	}
 333
 334	ice_info_get_dsn(pf, ctx);
 335
 336	err = devlink_info_serial_number_put(req, ctx->buf);
 337	if (err) {
 338		NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
 339		goto out_free_ctx;
 340	}
 341
 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;
 345
 346		memset(ctx->buf, 0, sizeof(ctx->buf));
 347
 348		ice_devlink_versions[i].getter(pf, ctx);
 349
 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.
 354		 */
 355		if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
 356			ice_devlink_versions[i].fallback(pf, ctx);
 357
 358		/* Do not report missing versions */
 359		if (ctx->buf[0] == '\0')
 360			continue;
 361
 362		switch (type) {
 363		case ICE_VERSION_FIXED:
 364			err = devlink_info_version_fixed_put(req, key, ctx->buf);
 365			if (err) {
 366				NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
 367				goto out_free_ctx;
 368			}
 369			break;
 370		case ICE_VERSION_RUNNING:
 371			err = devlink_info_version_running_put(req, key, ctx->buf);
 372			if (err) {
 373				NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
 374				goto out_free_ctx;
 375			}
 376			break;
 377		case ICE_VERSION_STORED:
 378			err = devlink_info_version_stored_put(req, key, ctx->buf);
 379			if (err) {
 380				NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
 381				goto out_free_ctx;
 382			}
 383			break;
 384		}
 385	}
 386
 387out_free_ctx:
 388	kfree(ctx);
 389	return err;
 390}
 391
 392/**
 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
 396 *
 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.
 400 *
 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
 404 * any source.
 405 */
 406static int
 407ice_devlink_reload_empr_start(struct ice_pf *pf,
 408			      struct netlink_ext_ack *extack)
 409{
 410	struct device *dev = ice_pf_to_dev(pf);
 411	struct ice_hw *hw = &pf->hw;
 412	u8 pending;
 413	int err;
 414
 415	err = ice_get_pending_updates(pf, &pending, extack);
 416	if (err)
 417		return err;
 418
 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.
 423	 */
 424	if (!pending) {
 425		NL_SET_ERR_MSG_MOD(extack, "No pending firmware update");
 426		return -ECANCELED;
 427	}
 428
 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");
 431		return -ECANCELED;
 432	}
 433
 434	dev_dbg(dev, "Issuing device EMP reset to activate firmware\n");
 435
 436	err = ice_aq_nvm_update_empr(hw);
 437	if (err) {
 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");
 441		return err;
 442	}
 443
 444	return 0;
 445}
 446
 447/**
 448 * ice_devlink_reload_down - prepare for reload
 449 * @devlink: pointer to the devlink instance to reload
 450 * @netns_change: if true, the network namespace is changing
 451 * @action: the action to perform
 452 * @limit: limits on what reload should do, such as not resetting
 453 * @extack: netlink extended ACK structure
 454 */
 455static int
 456ice_devlink_reload_down(struct devlink *devlink, bool netns_change,
 457			enum devlink_reload_action action,
 458			enum devlink_reload_limit limit,
 459			struct netlink_ext_ack *extack)
 460{
 461	struct ice_pf *pf = devlink_priv(devlink);
 462
 463	switch (action) {
 464	case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
 465		if (ice_is_eswitch_mode_switchdev(pf)) {
 466			NL_SET_ERR_MSG_MOD(extack,
 467					   "Go to legacy mode before doing reinit\n");
 468			return -EOPNOTSUPP;
 469		}
 470		if (ice_is_adq_active(pf)) {
 471			NL_SET_ERR_MSG_MOD(extack,
 472					   "Turn off ADQ before doing reinit\n");
 473			return -EOPNOTSUPP;
 474		}
 475		if (ice_has_vfs(pf)) {
 476			NL_SET_ERR_MSG_MOD(extack,
 477					   "Remove all VFs before doing reinit\n");
 478			return -EOPNOTSUPP;
 479		}
 480		ice_unload(pf);
 481		return 0;
 482	case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
 483		return ice_devlink_reload_empr_start(pf, extack);
 484	default:
 485		WARN_ON(1);
 486		return -EOPNOTSUPP;
 487	}
 488}
 489
 490/**
 491 * ice_devlink_reload_empr_finish - Wait for EMP reset to finish
 492 * @pf: pointer to the pf instance
 493 * @extack: netlink extended ACK structure
 494 *
 495 * Wait for driver to finish rebuilding after EMP reset is completed. This
 496 * includes time to wait for both the actual device reset as well as the time
 497 * for the driver's rebuild to complete.
 498 */
 499static int
 500ice_devlink_reload_empr_finish(struct ice_pf *pf,
 501			       struct netlink_ext_ack *extack)
 502{
 503	int err;
 504
 505	err = ice_wait_for_reset(pf, 60 * HZ);
 506	if (err) {
 507		NL_SET_ERR_MSG_MOD(extack, "Device still resetting after 1 minute");
 508		return err;
 509	}
 510
 511	return 0;
 512}
 513
 514/**
 515 * ice_devlink_port_opt_speed_str - convert speed to a string
 516 * @speed: speed value
 517 */
 518static const char *ice_devlink_port_opt_speed_str(u8 speed)
 519{
 520	switch (speed & ICE_AQC_PORT_OPT_MAX_LANE_M) {
 521	case ICE_AQC_PORT_OPT_MAX_LANE_100M:
 522		return "0.1";
 523	case ICE_AQC_PORT_OPT_MAX_LANE_1G:
 524		return "1";
 525	case ICE_AQC_PORT_OPT_MAX_LANE_2500M:
 526		return "2.5";
 527	case ICE_AQC_PORT_OPT_MAX_LANE_5G:
 528		return "5";
 529	case ICE_AQC_PORT_OPT_MAX_LANE_10G:
 530		return "10";
 531	case ICE_AQC_PORT_OPT_MAX_LANE_25G:
 532		return "25";
 533	case ICE_AQC_PORT_OPT_MAX_LANE_50G:
 534		return "50";
 535	case ICE_AQC_PORT_OPT_MAX_LANE_100G:
 536		return "100";
 537	}
 538
 539	return "-";
 540}
 541
 542#define ICE_PORT_OPT_DESC_LEN	50
 543/**
 544 * ice_devlink_port_options_print - Print available port split options
 545 * @pf: the PF to print split port options
 546 *
 547 * Prints a table with available port split options and max port speeds
 548 */
 549static void ice_devlink_port_options_print(struct ice_pf *pf)
 550{
 551	u8 i, j, options_count, cnt, speed, pending_idx, active_idx;
 552	struct ice_aqc_get_port_options_elem *options, *opt;
 553	struct device *dev = ice_pf_to_dev(pf);
 554	bool active_valid, pending_valid;
 555	char desc[ICE_PORT_OPT_DESC_LEN];
 556	const char *str;
 557	int status;
 558
 559	options = kcalloc(ICE_AQC_PORT_OPT_MAX * ICE_MAX_PORT_PER_PCI_DEV,
 560			  sizeof(*options), GFP_KERNEL);
 561	if (!options)
 562		return;
 563
 564	for (i = 0; i < ICE_MAX_PORT_PER_PCI_DEV; i++) {
 565		opt = options + i * ICE_AQC_PORT_OPT_MAX;
 566		options_count = ICE_AQC_PORT_OPT_MAX;
 567		active_valid = 0;
 568
 569		status = ice_aq_get_port_options(&pf->hw, opt, &options_count,
 570						 i, true, &active_idx,
 571						 &active_valid, &pending_idx,
 572						 &pending_valid);
 573		if (status) {
 574			dev_dbg(dev, "Couldn't read port option for port %d, err %d\n",
 575				i, status);
 576			goto err;
 577		}
 578	}
 579
 580	dev_dbg(dev, "Available port split options and max port speeds (Gbps):\n");
 581	dev_dbg(dev, "Status  Split      Quad 0          Quad 1\n");
 582	dev_dbg(dev, "        count  L0  L1  L2  L3  L4  L5  L6  L7\n");
 583
 584	for (i = 0; i < options_count; i++) {
 585		cnt = 0;
 586
 587		if (i == ice_active_port_option)
 588			str = "Active";
 589		else if ((i == pending_idx) && pending_valid)
 590			str = "Pending";
 591		else
 592			str = "";
 593
 594		cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
 595				"%-8s", str);
 596
 597		cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
 598				"%-6u", options[i].pmd);
 599
 600		for (j = 0; j < ICE_MAX_PORT_PER_PCI_DEV; ++j) {
 601			speed = options[i + j * ICE_AQC_PORT_OPT_MAX].max_lane_speed;
 602			str = ice_devlink_port_opt_speed_str(speed);
 603			cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
 604					"%3s ", str);
 605		}
 606
 607		dev_dbg(dev, "%s\n", desc);
 608	}
 609
 610err:
 611	kfree(options);
 612}
 613
 614/**
 615 * ice_devlink_aq_set_port_option - Send set port option admin queue command
 616 * @pf: the PF to print split port options
 617 * @option_idx: selected port option
 618 * @extack: extended netdev ack structure
 619 *
 620 * Sends set port option admin queue command with selected port option and
 621 * calls NVM write activate.
 622 */
 623static int
 624ice_devlink_aq_set_port_option(struct ice_pf *pf, u8 option_idx,
 625			       struct netlink_ext_ack *extack)
 626{
 627	struct device *dev = ice_pf_to_dev(pf);
 628	int status;
 629
 630	status = ice_aq_set_port_option(&pf->hw, 0, true, option_idx);
 631	if (status) {
 632		dev_dbg(dev, "ice_aq_set_port_option, err %d aq_err %d\n",
 633			status, pf->hw.adminq.sq_last_status);
 634		NL_SET_ERR_MSG_MOD(extack, "Port split request failed");
 635		return -EIO;
 636	}
 637
 638	status = ice_acquire_nvm(&pf->hw, ICE_RES_WRITE);
 639	if (status) {
 640		dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
 641			status, pf->hw.adminq.sq_last_status);
 642		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
 643		return -EIO;
 644	}
 645
 646	status = ice_nvm_write_activate(&pf->hw, ICE_AQC_NVM_ACTIV_REQ_EMPR, NULL);
 647	if (status) {
 648		dev_dbg(dev, "ice_nvm_write_activate failed, err %d aq_err %d\n",
 649			status, pf->hw.adminq.sq_last_status);
 650		NL_SET_ERR_MSG_MOD(extack, "Port split request failed to save data");
 651		ice_release_nvm(&pf->hw);
 652		return -EIO;
 653	}
 654
 655	ice_release_nvm(&pf->hw);
 656
 657	NL_SET_ERR_MSG_MOD(extack, "Reboot required to finish port split");
 658	return 0;
 659}
 660
 661/**
 662 * ice_devlink_port_split - .port_split devlink handler
 663 * @devlink: devlink instance structure
 664 * @port: devlink port structure
 665 * @count: number of ports to split to
 666 * @extack: extended netdev ack structure
 667 *
 668 * Callback for the devlink .port_split operation.
 669 *
 670 * Unfortunately, the devlink expression of available options is limited
 671 * to just a number, so search for an FW port option which supports
 672 * the specified number. As there could be multiple FW port options with
 673 * the same port split count, allow switching between them. When the same
 674 * port split count request is issued again, switch to the next FW port
 675 * option with the same port split count.
 676 *
 677 * Return: zero on success or an error code on failure.
 678 */
 679static int
 680ice_devlink_port_split(struct devlink *devlink, struct devlink_port *port,
 681		       unsigned int count, struct netlink_ext_ack *extack)
 682{
 683	struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
 684	u8 i, j, active_idx, pending_idx, new_option;
 685	struct ice_pf *pf = devlink_priv(devlink);
 686	u8 option_count = ICE_AQC_PORT_OPT_MAX;
 687	struct device *dev = ice_pf_to_dev(pf);
 688	bool active_valid, pending_valid;
 689	int status;
 690
 691	status = ice_aq_get_port_options(&pf->hw, options, &option_count,
 692					 0, true, &active_idx, &active_valid,
 693					 &pending_idx, &pending_valid);
 694	if (status) {
 695		dev_dbg(dev, "Couldn't read port split options, err = %d\n",
 696			status);
 697		NL_SET_ERR_MSG_MOD(extack, "Failed to get available port split options");
 698		return -EIO;
 699	}
 700
 701	new_option = ICE_AQC_PORT_OPT_MAX;
 702	active_idx = pending_valid ? pending_idx : active_idx;
 703	for (i = 1; i <= option_count; i++) {
 704		/* In order to allow switching between FW port options with
 705		 * the same port split count, search for a new option starting
 706		 * from the active/pending option (with array wrap around).
 707		 */
 708		j = (active_idx + i) % option_count;
 709
 710		if (count == options[j].pmd) {
 711			new_option = j;
 712			break;
 713		}
 714	}
 715
 716	if (new_option == active_idx) {
 717		dev_dbg(dev, "request to split: count: %u is already set and there are no other options\n",
 718			count);
 719		NL_SET_ERR_MSG_MOD(extack, "Requested split count is already set");
 720		ice_devlink_port_options_print(pf);
 721		return -EINVAL;
 722	}
 723
 724	if (new_option == ICE_AQC_PORT_OPT_MAX) {
 725		dev_dbg(dev, "request to split: count: %u not found\n", count);
 726		NL_SET_ERR_MSG_MOD(extack, "Port split requested unsupported port config");
 727		ice_devlink_port_options_print(pf);
 728		return -EINVAL;
 729	}
 730
 731	status = ice_devlink_aq_set_port_option(pf, new_option, extack);
 732	if (status)
 733		return status;
 734
 735	ice_devlink_port_options_print(pf);
 736
 737	return 0;
 738}
 739
 740/**
 741 * ice_devlink_port_unsplit - .port_unsplit devlink handler
 742 * @devlink: devlink instance structure
 743 * @port: devlink port structure
 744 * @extack: extended netdev ack structure
 745 *
 746 * Callback for the devlink .port_unsplit operation.
 747 * Calls ice_devlink_port_split with split count set to 1.
 748 * There could be no FW option available with split count 1.
 749 *
 750 * Return: zero on success or an error code on failure.
 751 */
 752static int
 753ice_devlink_port_unsplit(struct devlink *devlink, struct devlink_port *port,
 754			 struct netlink_ext_ack *extack)
 755{
 756	return ice_devlink_port_split(devlink, port, 1, extack);
 757}
 758
 759/**
 760 * ice_tear_down_devlink_rate_tree - removes devlink-rate exported tree
 761 * @pf: pf struct
 762 *
 763 * This function tears down tree exported during VF's creation.
 764 */
 765void ice_tear_down_devlink_rate_tree(struct ice_pf *pf)
 766{
 767	struct devlink *devlink;
 768	struct ice_vf *vf;
 769	unsigned int bkt;
 770
 771	devlink = priv_to_devlink(pf);
 772
 773	devl_lock(devlink);
 774	mutex_lock(&pf->vfs.table_lock);
 775	ice_for_each_vf(pf, bkt, vf) {
 776		if (vf->devlink_port.devlink_rate)
 777			devl_rate_leaf_destroy(&vf->devlink_port);
 778	}
 779	mutex_unlock(&pf->vfs.table_lock);
 780
 781	devl_rate_nodes_destroy(devlink);
 782	devl_unlock(devlink);
 783}
 784
 785/**
 786 * ice_enable_custom_tx - try to enable custom Tx feature
 787 * @pf: pf struct
 788 *
 789 * This function tries to enable custom Tx feature,
 790 * it's not possible to enable it, if DCB or ADQ is active.
 791 */
 792static bool ice_enable_custom_tx(struct ice_pf *pf)
 793{
 794	struct ice_port_info *pi = ice_get_main_vsi(pf)->port_info;
 795	struct device *dev = ice_pf_to_dev(pf);
 796
 797	if (pi->is_custom_tx_enabled)
 798		/* already enabled, return true */
 799		return true;
 800
 801	if (ice_is_adq_active(pf)) {
 802		dev_err(dev, "ADQ active, can't modify Tx scheduler tree\n");
 803		return false;
 804	}
 805
 806	if (ice_is_dcb_active(pf)) {
 807		dev_err(dev, "DCB active, can't modify Tx scheduler tree\n");
 808		return false;
 809	}
 810
 811	pi->is_custom_tx_enabled = true;
 812
 813	return true;
 814}
 815
 816/**
 817 * ice_traverse_tx_tree - traverse Tx scheduler tree
 818 * @devlink: devlink struct
 819 * @node: current node, used for recursion
 820 * @tc_node: tc_node struct, that is treated as a root
 821 * @pf: pf struct
 822 *
 823 * This function traverses Tx scheduler tree and exports
 824 * entire structure to the devlink-rate.
 825 */
 826static void ice_traverse_tx_tree(struct devlink *devlink, struct ice_sched_node *node,
 827				 struct ice_sched_node *tc_node, struct ice_pf *pf)
 828{
 829	struct devlink_rate *rate_node = NULL;
 830	struct ice_vf *vf;
 831	int i;
 832
 833	if (node->rate_node)
 834		/* already added, skip to the next */
 835		goto traverse_children;
 836
 837	if (node->parent == tc_node) {
 838		/* create root node */
 839		rate_node = devl_rate_node_create(devlink, node, node->name, NULL);
 840	} else if (node->vsi_handle &&
 841		   pf->vsi[node->vsi_handle]->vf) {
 842		vf = pf->vsi[node->vsi_handle]->vf;
 843		if (!vf->devlink_port.devlink_rate)
 844			/* leaf nodes doesn't have children
 845			 * so we don't set rate_node
 846			 */
 847			devl_rate_leaf_create(&vf->devlink_port, node,
 848					      node->parent->rate_node);
 849	} else if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF &&
 850		   node->parent->rate_node) {
 851		rate_node = devl_rate_node_create(devlink, node, node->name,
 852						  node->parent->rate_node);
 853	}
 854
 855	if (rate_node && !IS_ERR(rate_node))
 856		node->rate_node = rate_node;
 857
 858traverse_children:
 859	for (i = 0; i < node->num_children; i++)
 860		ice_traverse_tx_tree(devlink, node->children[i], tc_node, pf);
 861}
 862
 863/**
 864 * ice_devlink_rate_init_tx_topology - export Tx scheduler tree to devlink rate
 865 * @devlink: devlink struct
 866 * @vsi: main vsi struct
 867 *
 868 * This function finds a root node, then calls ice_traverse_tx tree, which
 869 * traverses the tree and exports it's contents to devlink rate.
 870 */
 871int ice_devlink_rate_init_tx_topology(struct devlink *devlink, struct ice_vsi *vsi)
 872{
 873	struct ice_port_info *pi = vsi->port_info;
 874	struct ice_sched_node *tc_node;
 875	struct ice_pf *pf = vsi->back;
 876	int i;
 877
 878	tc_node = pi->root->children[0];
 879	mutex_lock(&pi->sched_lock);
 880	devl_lock(devlink);
 881	for (i = 0; i < tc_node->num_children; i++)
 882		ice_traverse_tx_tree(devlink, tc_node->children[i], tc_node, pf);
 883	devl_unlock(devlink);
 884	mutex_unlock(&pi->sched_lock);
 885
 886	return 0;
 887}
 888
 889static void ice_clear_rate_nodes(struct ice_sched_node *node)
 890{
 891	node->rate_node = NULL;
 892
 893	for (int i = 0; i < node->num_children; i++)
 894		ice_clear_rate_nodes(node->children[i]);
 895}
 896
 897/**
 898 * ice_devlink_rate_clear_tx_topology - clear node->rate_node
 899 * @vsi: main vsi struct
 900 *
 901 * Clear rate_node to cleanup creation of Tx topology.
 902 *
 903 */
 904void ice_devlink_rate_clear_tx_topology(struct ice_vsi *vsi)
 905{
 906	struct ice_port_info *pi = vsi->port_info;
 907
 908	mutex_lock(&pi->sched_lock);
 909	ice_clear_rate_nodes(pi->root->children[0]);
 910	mutex_unlock(&pi->sched_lock);
 911}
 912
 913/**
 914 * ice_set_object_tx_share - sets node scheduling parameter
 915 * @pi: devlink struct instance
 916 * @node: node struct instance
 917 * @bw: bandwidth in bytes per second
 918 * @extack: extended netdev ack structure
 919 *
 920 * This function sets ICE_MIN_BW scheduling BW limit.
 921 */
 922static int ice_set_object_tx_share(struct ice_port_info *pi, struct ice_sched_node *node,
 923				   u64 bw, struct netlink_ext_ack *extack)
 924{
 925	int status;
 926
 927	mutex_lock(&pi->sched_lock);
 928	/* converts bytes per second to kilo bits per second */
 929	node->tx_share = div_u64(bw, 125);
 930	status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW, node->tx_share);
 931	mutex_unlock(&pi->sched_lock);
 932
 933	if (status)
 934		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_share");
 935
 936	return status;
 937}
 938
 939/**
 940 * ice_set_object_tx_max - sets node scheduling parameter
 941 * @pi: devlink struct instance
 942 * @node: node struct instance
 943 * @bw: bandwidth in bytes per second
 944 * @extack: extended netdev ack structure
 945 *
 946 * This function sets ICE_MAX_BW scheduling BW limit.
 947 */
 948static int ice_set_object_tx_max(struct ice_port_info *pi, struct ice_sched_node *node,
 949				 u64 bw, struct netlink_ext_ack *extack)
 950{
 951	int status;
 952
 953	mutex_lock(&pi->sched_lock);
 954	/* converts bytes per second value to kilo bits per second */
 955	node->tx_max = div_u64(bw, 125);
 956	status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW, node->tx_max);
 957	mutex_unlock(&pi->sched_lock);
 958
 959	if (status)
 960		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_max");
 961
 962	return status;
 963}
 964
 965/**
 966 * ice_set_object_tx_priority - sets node scheduling parameter
 967 * @pi: devlink struct instance
 968 * @node: node struct instance
 969 * @priority: value representing priority for strict priority arbitration
 970 * @extack: extended netdev ack structure
 971 *
 972 * This function sets priority of node among siblings.
 973 */
 974static int ice_set_object_tx_priority(struct ice_port_info *pi, struct ice_sched_node *node,
 975				      u32 priority, struct netlink_ext_ack *extack)
 976{
 977	int status;
 978
 979	if (priority >= 8) {
 980		NL_SET_ERR_MSG_MOD(extack, "Priority should be less than 8");
 981		return -EINVAL;
 982	}
 983
 984	mutex_lock(&pi->sched_lock);
 985	node->tx_priority = priority;
 986	status = ice_sched_set_node_priority(pi, node, node->tx_priority);
 987	mutex_unlock(&pi->sched_lock);
 988
 989	if (status)
 990		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_priority");
 991
 992	return status;
 993}
 994
 995/**
 996 * ice_set_object_tx_weight - sets node scheduling parameter
 997 * @pi: devlink struct instance
 998 * @node: node struct instance
 999 * @weight: value represeting relative weight for WFQ arbitration
1000 * @extack: extended netdev ack structure
1001 *
1002 * This function sets node weight for WFQ algorithm.
1003 */
1004static int ice_set_object_tx_weight(struct ice_port_info *pi, struct ice_sched_node *node,
1005				    u32 weight, struct netlink_ext_ack *extack)
1006{
1007	int status;
1008
1009	if (weight > 200 || weight < 1) {
1010		NL_SET_ERR_MSG_MOD(extack, "Weight must be between 1 and 200");
1011		return -EINVAL;
1012	}
1013
1014	mutex_lock(&pi->sched_lock);
1015	node->tx_weight = weight;
1016	status = ice_sched_set_node_weight(pi, node, node->tx_weight);
1017	mutex_unlock(&pi->sched_lock);
1018
1019	if (status)
1020		NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_weight");
1021
1022	return status;
1023}
1024
1025/**
1026 * ice_get_pi_from_dev_rate - get port info from devlink_rate
1027 * @rate_node: devlink struct instance
1028 *
1029 * This function returns corresponding port_info struct of devlink_rate
1030 */
1031static struct ice_port_info *ice_get_pi_from_dev_rate(struct devlink_rate *rate_node)
1032{
1033	struct ice_pf *pf = devlink_priv(rate_node->devlink);
1034
1035	return ice_get_main_vsi(pf)->port_info;
1036}
1037
1038static int ice_devlink_rate_node_new(struct devlink_rate *rate_node, void **priv,
1039				     struct netlink_ext_ack *extack)
1040{
1041	struct ice_sched_node *node;
1042	struct ice_port_info *pi;
1043
1044	pi = ice_get_pi_from_dev_rate(rate_node);
1045
1046	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1047		return -EBUSY;
1048
1049	/* preallocate memory for ice_sched_node */
1050	node = devm_kzalloc(ice_hw_to_dev(pi->hw), sizeof(*node), GFP_KERNEL);
1051	*priv = node;
1052
1053	return 0;
1054}
1055
1056static int ice_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,
1057				     struct netlink_ext_ack *extack)
1058{
1059	struct ice_sched_node *node, *tc_node;
1060	struct ice_port_info *pi;
1061
1062	pi = ice_get_pi_from_dev_rate(rate_node);
1063	tc_node = pi->root->children[0];
1064	node = priv;
1065
1066	if (!rate_node->parent || !node || tc_node == node || !extack)
1067		return 0;
1068
1069	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1070		return -EBUSY;
1071
1072	/* can't allow to delete a node with children */
1073	if (node->num_children)
1074		return -EINVAL;
1075
1076	mutex_lock(&pi->sched_lock);
1077	ice_free_sched_node(pi, node);
1078	mutex_unlock(&pi->sched_lock);
1079
1080	return 0;
1081}
1082
1083static int ice_devlink_rate_leaf_tx_max_set(struct devlink_rate *rate_leaf, void *priv,
1084					    u64 tx_max, struct netlink_ext_ack *extack)
1085{
1086	struct ice_sched_node *node = priv;
1087
1088	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1089		return -EBUSY;
1090
1091	if (!node)
1092		return 0;
1093
1094	return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_leaf),
1095				     node, tx_max, extack);
1096}
1097
1098static int ice_devlink_rate_leaf_tx_share_set(struct devlink_rate *rate_leaf, void *priv,
1099					      u64 tx_share, struct netlink_ext_ack *extack)
1100{
1101	struct ice_sched_node *node = priv;
1102
1103	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1104		return -EBUSY;
1105
1106	if (!node)
1107		return 0;
1108
1109	return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_leaf), node,
1110				       tx_share, extack);
1111}
1112
1113static int ice_devlink_rate_leaf_tx_priority_set(struct devlink_rate *rate_leaf, void *priv,
1114						 u32 tx_priority, struct netlink_ext_ack *extack)
1115{
1116	struct ice_sched_node *node = priv;
1117
1118	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1119		return -EBUSY;
1120
1121	if (!node)
1122		return 0;
1123
1124	return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_leaf), node,
1125					  tx_priority, extack);
1126}
1127
1128static int ice_devlink_rate_leaf_tx_weight_set(struct devlink_rate *rate_leaf, void *priv,
1129					       u32 tx_weight, struct netlink_ext_ack *extack)
1130{
1131	struct ice_sched_node *node = priv;
1132
1133	if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1134		return -EBUSY;
1135
1136	if (!node)
1137		return 0;
1138
1139	return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_leaf), node,
1140					tx_weight, extack);
1141}
1142
1143static int ice_devlink_rate_node_tx_max_set(struct devlink_rate *rate_node, void *priv,
1144					    u64 tx_max, struct netlink_ext_ack *extack)
1145{
1146	struct ice_sched_node *node = priv;
1147
1148	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1149		return -EBUSY;
1150
1151	if (!node)
1152		return 0;
1153
1154	return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_node),
1155				     node, tx_max, extack);
1156}
1157
1158static int ice_devlink_rate_node_tx_share_set(struct devlink_rate *rate_node, void *priv,
1159					      u64 tx_share, struct netlink_ext_ack *extack)
1160{
1161	struct ice_sched_node *node = priv;
1162
1163	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1164		return -EBUSY;
1165
1166	if (!node)
1167		return 0;
1168
1169	return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_node),
1170				       node, tx_share, extack);
1171}
1172
1173static int ice_devlink_rate_node_tx_priority_set(struct devlink_rate *rate_node, void *priv,
1174						 u32 tx_priority, struct netlink_ext_ack *extack)
1175{
1176	struct ice_sched_node *node = priv;
1177
1178	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1179		return -EBUSY;
1180
1181	if (!node)
1182		return 0;
1183
1184	return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_node),
1185					  node, tx_priority, extack);
1186}
1187
1188static int ice_devlink_rate_node_tx_weight_set(struct devlink_rate *rate_node, void *priv,
1189					       u32 tx_weight, struct netlink_ext_ack *extack)
1190{
1191	struct ice_sched_node *node = priv;
1192
1193	if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1194		return -EBUSY;
1195
1196	if (!node)
1197		return 0;
1198
1199	return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_node),
1200					node, tx_weight, extack);
1201}
1202
1203static int ice_devlink_set_parent(struct devlink_rate *devlink_rate,
1204				  struct devlink_rate *parent,
1205				  void *priv, void *parent_priv,
1206				  struct netlink_ext_ack *extack)
1207{
1208	struct ice_port_info *pi = ice_get_pi_from_dev_rate(devlink_rate);
1209	struct ice_sched_node *tc_node, *node, *parent_node;
1210	u16 num_nodes_added;
1211	u32 first_node_teid;
1212	u32 node_teid;
1213	int status;
1214
1215	tc_node = pi->root->children[0];
1216	node = priv;
1217
1218	if (!extack)
1219		return 0;
1220
1221	if (!ice_enable_custom_tx(devlink_priv(devlink_rate->devlink)))
1222		return -EBUSY;
1223
1224	if (!parent) {
1225		if (!node || tc_node == node || node->num_children)
1226			return -EINVAL;
1227
1228		mutex_lock(&pi->sched_lock);
1229		ice_free_sched_node(pi, node);
1230		mutex_unlock(&pi->sched_lock);
1231
1232		return 0;
1233	}
1234
1235	parent_node = parent_priv;
1236
1237	/* if the node doesn't exist, create it */
1238	if (!node->parent) {
1239		mutex_lock(&pi->sched_lock);
1240		status = ice_sched_add_elems(pi, tc_node, parent_node,
1241					     parent_node->tx_sched_layer + 1,
1242					     1, &num_nodes_added, &first_node_teid,
1243					     &node);
1244		mutex_unlock(&pi->sched_lock);
1245
1246		if (status) {
1247			NL_SET_ERR_MSG_MOD(extack, "Can't add a new node");
1248			return status;
1249		}
1250
1251		if (devlink_rate->tx_share)
1252			ice_set_object_tx_share(pi, node, devlink_rate->tx_share, extack);
1253		if (devlink_rate->tx_max)
1254			ice_set_object_tx_max(pi, node, devlink_rate->tx_max, extack);
1255		if (devlink_rate->tx_priority)
1256			ice_set_object_tx_priority(pi, node, devlink_rate->tx_priority, extack);
1257		if (devlink_rate->tx_weight)
1258			ice_set_object_tx_weight(pi, node, devlink_rate->tx_weight, extack);
1259	} else {
1260		node_teid = le32_to_cpu(node->info.node_teid);
1261		mutex_lock(&pi->sched_lock);
1262		status = ice_sched_move_nodes(pi, parent_node, 1, &node_teid);
1263		mutex_unlock(&pi->sched_lock);
1264
1265		if (status)
1266			NL_SET_ERR_MSG_MOD(extack, "Can't move existing node to a new parent");
1267	}
1268
1269	return status;
1270}
1271
1272/**
1273 * ice_devlink_reload_up - do reload up after reinit
1274 * @devlink: pointer to the devlink instance reloading
1275 * @action: the action requested
1276 * @limit: limits imposed by userspace, such as not resetting
1277 * @actions_performed: on return, indicate what actions actually performed
1278 * @extack: netlink extended ACK structure
1279 */
1280static int
1281ice_devlink_reload_up(struct devlink *devlink,
1282		      enum devlink_reload_action action,
1283		      enum devlink_reload_limit limit,
1284		      u32 *actions_performed,
1285		      struct netlink_ext_ack *extack)
1286{
1287	struct ice_pf *pf = devlink_priv(devlink);
1288
1289	switch (action) {
1290	case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
1291		*actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
1292		return ice_load(pf);
1293	case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
1294		*actions_performed = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE);
1295		return ice_devlink_reload_empr_finish(pf, extack);
1296	default:
1297		WARN_ON(1);
1298		return -EOPNOTSUPP;
1299	}
1300}
1301
1302static const struct devlink_ops ice_devlink_ops = {
1303	.supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
1304	.reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
1305			  BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
1306	.reload_down = ice_devlink_reload_down,
1307	.reload_up = ice_devlink_reload_up,
1308	.eswitch_mode_get = ice_eswitch_mode_get,
1309	.eswitch_mode_set = ice_eswitch_mode_set,
1310	.info_get = ice_devlink_info_get,
1311	.flash_update = ice_devlink_flash_update,
1312
1313	.rate_node_new = ice_devlink_rate_node_new,
1314	.rate_node_del = ice_devlink_rate_node_del,
1315
1316	.rate_leaf_tx_max_set = ice_devlink_rate_leaf_tx_max_set,
1317	.rate_leaf_tx_share_set = ice_devlink_rate_leaf_tx_share_set,
1318	.rate_leaf_tx_priority_set = ice_devlink_rate_leaf_tx_priority_set,
1319	.rate_leaf_tx_weight_set = ice_devlink_rate_leaf_tx_weight_set,
1320
1321	.rate_node_tx_max_set = ice_devlink_rate_node_tx_max_set,
1322	.rate_node_tx_share_set = ice_devlink_rate_node_tx_share_set,
1323	.rate_node_tx_priority_set = ice_devlink_rate_node_tx_priority_set,
1324	.rate_node_tx_weight_set = ice_devlink_rate_node_tx_weight_set,
1325
1326	.rate_leaf_parent_set = ice_devlink_set_parent,
1327	.rate_node_parent_set = ice_devlink_set_parent,
1328};
1329
1330static int
1331ice_devlink_enable_roce_get(struct devlink *devlink, u32 id,
1332			    struct devlink_param_gset_ctx *ctx)
1333{
1334	struct ice_pf *pf = devlink_priv(devlink);
1335
1336	ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ? true : false;
1337
1338	return 0;
1339}
1340
1341static int
1342ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
1343			    struct devlink_param_gset_ctx *ctx)
1344{
1345	struct ice_pf *pf = devlink_priv(devlink);
1346	bool roce_ena = ctx->val.vbool;
1347	int ret;
1348
1349	if (!roce_ena) {
1350		ice_unplug_aux_dev(pf);
1351		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
1352		return 0;
1353	}
1354
1355	pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2;
1356	ret = ice_plug_aux_dev(pf);
1357	if (ret)
1358		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
1359
1360	return ret;
1361}
1362
1363static int
1364ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
1365				 union devlink_param_value val,
1366				 struct netlink_ext_ack *extack)
1367{
1368	struct ice_pf *pf = devlink_priv(devlink);
1369
1370	if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
1371		return -EOPNOTSUPP;
1372
1373	if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) {
1374		NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
1375		return -EOPNOTSUPP;
1376	}
1377
1378	return 0;
1379}
1380
1381static int
1382ice_devlink_enable_iw_get(struct devlink *devlink, u32 id,
1383			  struct devlink_param_gset_ctx *ctx)
1384{
1385	struct ice_pf *pf = devlink_priv(devlink);
1386
1387	ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP;
1388
1389	return 0;
1390}
1391
1392static int
1393ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
1394			  struct devlink_param_gset_ctx *ctx)
1395{
1396	struct ice_pf *pf = devlink_priv(devlink);
1397	bool iw_ena = ctx->val.vbool;
1398	int ret;
1399
1400	if (!iw_ena) {
1401		ice_unplug_aux_dev(pf);
1402		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
1403		return 0;
1404	}
1405
1406	pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP;
1407	ret = ice_plug_aux_dev(pf);
1408	if (ret)
1409		pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
1410
1411	return ret;
1412}
1413
1414static int
1415ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id,
1416			       union devlink_param_value val,
1417			       struct netlink_ext_ack *extack)
1418{
1419	struct ice_pf *pf = devlink_priv(devlink);
1420
1421	if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
1422		return -EOPNOTSUPP;
1423
1424	if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) {
1425		NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
1426		return -EOPNOTSUPP;
1427	}
1428
1429	return 0;
1430}
1431
1432static const struct devlink_param ice_devlink_params[] = {
1433	DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1434			      ice_devlink_enable_roce_get,
1435			      ice_devlink_enable_roce_set,
1436			      ice_devlink_enable_roce_validate),
1437	DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1438			      ice_devlink_enable_iw_get,
1439			      ice_devlink_enable_iw_set,
1440			      ice_devlink_enable_iw_validate),
1441
1442};
1443
1444static void ice_devlink_free(void *devlink_ptr)
1445{
1446	devlink_free((struct devlink *)devlink_ptr);
1447}
1448
1449/**
1450 * ice_allocate_pf - Allocate devlink and return PF structure pointer
1451 * @dev: the device to allocate for
1452 *
1453 * Allocate a devlink instance for this device and return the private area as
1454 * the PF structure. The devlink memory is kept track of through devres by
1455 * adding an action to remove it when unwinding.
1456 */
1457struct ice_pf *ice_allocate_pf(struct device *dev)
1458{
1459	struct devlink *devlink;
1460
1461	devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
1462	if (!devlink)
1463		return NULL;
1464
1465	/* Add an action to teardown the devlink when unwinding the driver */
1466	if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
1467		return NULL;
1468
1469	return devlink_priv(devlink);
1470}
1471
1472/**
1473 * ice_devlink_register - Register devlink interface for this PF
1474 * @pf: the PF to register the devlink for.
1475 *
1476 * Register the devlink instance associated with this physical function.
1477 *
1478 * Return: zero on success or an error code on failure.
1479 */
1480void ice_devlink_register(struct ice_pf *pf)
1481{
1482	struct devlink *devlink = priv_to_devlink(pf);
1483
1484	devlink_register(devlink);
1485}
1486
1487/**
1488 * ice_devlink_unregister - Unregister devlink resources for this PF.
1489 * @pf: the PF structure to cleanup
1490 *
1491 * Releases resources used by devlink and cleans up associated memory.
1492 */
1493void ice_devlink_unregister(struct ice_pf *pf)
1494{
1495	devlink_unregister(priv_to_devlink(pf));
1496}
1497
1498/**
1499 * ice_devlink_set_switch_id - Set unique switch id based on pci dsn
1500 * @pf: the PF to create a devlink port for
1501 * @ppid: struct with switch id information
1502 */
1503static void
1504ice_devlink_set_switch_id(struct ice_pf *pf, struct netdev_phys_item_id *ppid)
1505{
1506	struct pci_dev *pdev = pf->pdev;
1507	u64 id;
1508
1509	id = pci_get_dsn(pdev);
1510
1511	ppid->id_len = sizeof(id);
1512	put_unaligned_be64(id, &ppid->id);
1513}
1514
1515int ice_devlink_register_params(struct ice_pf *pf)
1516{
1517	struct devlink *devlink = priv_to_devlink(pf);
1518
1519	return devlink_params_register(devlink, ice_devlink_params,
1520				       ARRAY_SIZE(ice_devlink_params));
1521}
1522
1523void ice_devlink_unregister_params(struct ice_pf *pf)
1524{
1525	devlink_params_unregister(priv_to_devlink(pf), ice_devlink_params,
1526				  ARRAY_SIZE(ice_devlink_params));
1527}
1528
1529/**
1530 * ice_devlink_set_port_split_options - Set port split options
1531 * @pf: the PF to set port split options
1532 * @attrs: devlink attributes
1533 *
1534 * Sets devlink port split options based on available FW port options
1535 */
1536static void
1537ice_devlink_set_port_split_options(struct ice_pf *pf,
1538				   struct devlink_port_attrs *attrs)
1539{
1540	struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
1541	u8 i, active_idx, pending_idx, option_count = ICE_AQC_PORT_OPT_MAX;
1542	bool active_valid, pending_valid;
1543	int status;
1544
1545	status = ice_aq_get_port_options(&pf->hw, options, &option_count,
1546					 0, true, &active_idx, &active_valid,
1547					 &pending_idx, &pending_valid);
1548	if (status) {
1549		dev_dbg(ice_pf_to_dev(pf), "Couldn't read port split options, err = %d\n",
1550			status);
1551		return;
1552	}
1553
1554	/* find the biggest available port split count */
1555	for (i = 0; i < option_count; i++)
1556		attrs->lanes = max_t(int, attrs->lanes, options[i].pmd);
1557
1558	attrs->splittable = attrs->lanes ? 1 : 0;
1559	ice_active_port_option = active_idx;
1560}
1561
1562static const struct devlink_port_ops ice_devlink_port_ops = {
1563	.port_split = ice_devlink_port_split,
1564	.port_unsplit = ice_devlink_port_unsplit,
1565};
1566
1567/**
1568 * ice_devlink_create_pf_port - Create a devlink port for this PF
1569 * @pf: the PF to create a devlink port for
1570 *
1571 * Create and register a devlink_port for this PF.
1572 *
1573 * Return: zero on success or an error code on failure.
1574 */
1575int ice_devlink_create_pf_port(struct ice_pf *pf)
1576{
1577	struct devlink_port_attrs attrs = {};
1578	struct devlink_port *devlink_port;
1579	struct devlink *devlink;
1580	struct ice_vsi *vsi;
1581	struct device *dev;
1582	int err;
1583
1584	dev = ice_pf_to_dev(pf);
1585
1586	devlink_port = &pf->devlink_port;
1587
1588	vsi = ice_get_main_vsi(pf);
1589	if (!vsi)
1590		return -EIO;
1591
1592	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
1593	attrs.phys.port_number = pf->hw.bus.func;
1594
1595	/* As FW supports only port split options for whole device,
1596	 * set port split options only for first PF.
1597	 */
1598	if (pf->hw.pf_id == 0)
1599		ice_devlink_set_port_split_options(pf, &attrs);
1600
1601	ice_devlink_set_switch_id(pf, &attrs.switch_id);
1602
1603	devlink_port_attrs_set(devlink_port, &attrs);
1604	devlink = priv_to_devlink(pf);
1605
1606	err = devlink_port_register_with_ops(devlink, devlink_port, vsi->idx,
1607					     &ice_devlink_port_ops);
1608	if (err) {
1609		dev_err(dev, "Failed to create devlink port for PF %d, error %d\n",
1610			pf->hw.pf_id, err);
1611		return err;
1612	}
1613
1614	return 0;
1615}
1616
1617/**
1618 * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF
1619 * @pf: the PF to cleanup
1620 *
1621 * Unregisters the devlink_port structure associated with this PF.
1622 */
1623void ice_devlink_destroy_pf_port(struct ice_pf *pf)
1624{
1625	devlink_port_unregister(&pf->devlink_port);
1626}
1627
1628/**
1629 * ice_devlink_create_vf_port - Create a devlink port for this VF
1630 * @vf: the VF to create a port for
1631 *
1632 * Create and register a devlink_port for this VF.
1633 *
1634 * Return: zero on success or an error code on failure.
1635 */
1636int ice_devlink_create_vf_port(struct ice_vf *vf)
1637{
1638	struct devlink_port_attrs attrs = {};
1639	struct devlink_port *devlink_port;
1640	struct devlink *devlink;
1641	struct ice_vsi *vsi;
1642	struct device *dev;
1643	struct ice_pf *pf;
1644	int err;
1645
1646	pf = vf->pf;
1647	dev = ice_pf_to_dev(pf);
1648	devlink_port = &vf->devlink_port;
1649
1650	vsi = ice_get_vf_vsi(vf);
1651	if (!vsi)
1652		return -EINVAL;
1653
1654	attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
1655	attrs.pci_vf.pf = pf->hw.bus.func;
1656	attrs.pci_vf.vf = vf->vf_id;
1657
1658	ice_devlink_set_switch_id(pf, &attrs.switch_id);
1659
1660	devlink_port_attrs_set(devlink_port, &attrs);
1661	devlink = priv_to_devlink(pf);
1662
1663	err = devlink_port_register(devlink, devlink_port, vsi->idx);
1664	if (err) {
1665		dev_err(dev, "Failed to create devlink port for VF %d, error %d\n",
1666			vf->vf_id, err);
1667		return err;
1668	}
1669
1670	return 0;
1671}
1672
1673/**
1674 * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF
1675 * @vf: the VF to cleanup
1676 *
1677 * Unregisters the devlink_port structure associated with this VF.
1678 */
1679void ice_devlink_destroy_vf_port(struct ice_vf *vf)
1680{
1681	devl_rate_leaf_destroy(&vf->devlink_port);
1682	devlink_port_unregister(&vf->devlink_port);
1683}
1684
1685#define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
1686
1687static const struct devlink_region_ops ice_nvm_region_ops;
1688static const struct devlink_region_ops ice_sram_region_ops;
1689
1690/**
1691 * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
1692 * @devlink: the devlink instance
1693 * @ops: the devlink region to snapshot
1694 * @extack: extended ACK response structure
1695 * @data: on exit points to snapshot data buffer
1696 *
1697 * This function is called in response to a DEVLINK_CMD_REGION_NEW for either
1698 * the nvm-flash or shadow-ram region.
1699 *
1700 * It captures a snapshot of the NVM or Shadow RAM flash contents. This
1701 * snapshot can then later be viewed via the DEVLINK_CMD_REGION_READ netlink
1702 * interface.
1703 *
1704 * @returns zero on success, and updates the data pointer. Returns a non-zero
1705 * error code on failure.
1706 */
1707static int ice_devlink_nvm_snapshot(struct devlink *devlink,
1708				    const struct devlink_region_ops *ops,
1709				    struct netlink_ext_ack *extack, u8 **data)
1710{
1711	struct ice_pf *pf = devlink_priv(devlink);
1712	struct device *dev = ice_pf_to_dev(pf);
1713	struct ice_hw *hw = &pf->hw;
1714	bool read_shadow_ram;
1715	u8 *nvm_data, *tmp, i;
1716	u32 nvm_size, left;
1717	s8 num_blks;
1718	int status;
1719
1720	if (ops == &ice_nvm_region_ops) {
1721		read_shadow_ram = false;
1722		nvm_size = hw->flash.flash_size;
1723	} else if (ops == &ice_sram_region_ops) {
1724		read_shadow_ram = true;
1725		nvm_size = hw->flash.sr_words * 2u;
1726	} else {
1727		NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1728		return -EOPNOTSUPP;
1729	}
1730
1731	nvm_data = vzalloc(nvm_size);
1732	if (!nvm_data)
1733		return -ENOMEM;
1734
1735	num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
1736	tmp = nvm_data;
1737	left = nvm_size;
1738
1739	/* Some systems take longer to read the NVM than others which causes the
1740	 * FW to reclaim the NVM lock before the entire NVM has been read. Fix
1741	 * this by breaking the reads of the NVM into smaller chunks that will
1742	 * probably not take as long. This has some overhead since we are
1743	 * increasing the number of AQ commands, but it should always work
1744	 */
1745	for (i = 0; i < num_blks; i++) {
1746		u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
1747
1748		status = ice_acquire_nvm(hw, ICE_RES_READ);
1749		if (status) {
1750			dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1751				status, hw->adminq.sq_last_status);
1752			NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1753			vfree(nvm_data);
1754			return -EIO;
1755		}
1756
1757		status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
1758					   &read_sz, tmp, read_shadow_ram);
1759		if (status) {
1760			dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1761				read_sz, status, hw->adminq.sq_last_status);
1762			NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1763			ice_release_nvm(hw);
1764			vfree(nvm_data);
1765			return -EIO;
1766		}
1767		ice_release_nvm(hw);
1768
1769		tmp += read_sz;
1770		left -= read_sz;
1771	}
1772
1773	*data = nvm_data;
1774
1775	return 0;
1776}
1777
1778/**
1779 * ice_devlink_nvm_read - Read a portion of NVM flash contents
1780 * @devlink: the devlink instance
1781 * @ops: the devlink region to snapshot
1782 * @extack: extended ACK response structure
1783 * @offset: the offset to start at
1784 * @size: the amount to read
1785 * @data: the data buffer to read into
1786 *
1787 * This function is called in response to DEVLINK_CMD_REGION_READ to directly
1788 * read a section of the NVM contents.
1789 *
1790 * It reads from either the nvm-flash or shadow-ram region contents.
1791 *
1792 * @returns zero on success, and updates the data pointer. Returns a non-zero
1793 * error code on failure.
1794 */
1795static int ice_devlink_nvm_read(struct devlink *devlink,
1796				const struct devlink_region_ops *ops,
1797				struct netlink_ext_ack *extack,
1798				u64 offset, u32 size, u8 *data)
1799{
1800	struct ice_pf *pf = devlink_priv(devlink);
1801	struct device *dev = ice_pf_to_dev(pf);
1802	struct ice_hw *hw = &pf->hw;
1803	bool read_shadow_ram;
1804	u64 nvm_size;
1805	int status;
1806
1807	if (ops == &ice_nvm_region_ops) {
1808		read_shadow_ram = false;
1809		nvm_size = hw->flash.flash_size;
1810	} else if (ops == &ice_sram_region_ops) {
1811		read_shadow_ram = true;
1812		nvm_size = hw->flash.sr_words * 2u;
1813	} else {
1814		NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1815		return -EOPNOTSUPP;
1816	}
1817
1818	if (offset + size >= nvm_size) {
1819		NL_SET_ERR_MSG_MOD(extack, "Cannot read beyond the region size");
1820		return -ERANGE;
1821	}
1822
1823	status = ice_acquire_nvm(hw, ICE_RES_READ);
1824	if (status) {
1825		dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1826			status, hw->adminq.sq_last_status);
1827		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1828		return -EIO;
1829	}
1830
1831	status = ice_read_flat_nvm(hw, (u32)offset, &size, data,
1832				   read_shadow_ram);
1833	if (status) {
1834		dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1835			size, status, hw->adminq.sq_last_status);
1836		NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1837		ice_release_nvm(hw);
1838		return -EIO;
1839	}
1840	ice_release_nvm(hw);
1841
1842	return 0;
1843}
1844
1845/**
1846 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
1847 * @devlink: the devlink instance
1848 * @ops: the devlink region being snapshotted
1849 * @extack: extended ACK response structure
1850 * @data: on exit points to snapshot data buffer
1851 *
1852 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
1853 * the device-caps devlink region. It captures a snapshot of the device
1854 * capabilities reported by firmware.
1855 *
1856 * @returns zero on success, and updates the data pointer. Returns a non-zero
1857 * error code on failure.
1858 */
1859static int
1860ice_devlink_devcaps_snapshot(struct devlink *devlink,
1861			     const struct devlink_region_ops *ops,
1862			     struct netlink_ext_ack *extack, u8 **data)
1863{
1864	struct ice_pf *pf = devlink_priv(devlink);
1865	struct device *dev = ice_pf_to_dev(pf);
1866	struct ice_hw *hw = &pf->hw;
1867	void *devcaps;
1868	int status;
1869
1870	devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
1871	if (!devcaps)
1872		return -ENOMEM;
1873
1874	status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
1875				  ice_aqc_opc_list_dev_caps, NULL);
1876	if (status) {
1877		dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
1878			status, hw->adminq.sq_last_status);
1879		NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
1880		vfree(devcaps);
1881		return status;
1882	}
1883
1884	*data = (u8 *)devcaps;
1885
1886	return 0;
1887}
1888
1889static const struct devlink_region_ops ice_nvm_region_ops = {
1890	.name = "nvm-flash",
1891	.destructor = vfree,
1892	.snapshot = ice_devlink_nvm_snapshot,
1893	.read = ice_devlink_nvm_read,
1894};
1895
1896static const struct devlink_region_ops ice_sram_region_ops = {
1897	.name = "shadow-ram",
1898	.destructor = vfree,
1899	.snapshot = ice_devlink_nvm_snapshot,
1900	.read = ice_devlink_nvm_read,
1901};
1902
1903static const struct devlink_region_ops ice_devcaps_region_ops = {
1904	.name = "device-caps",
1905	.destructor = vfree,
1906	.snapshot = ice_devlink_devcaps_snapshot,
1907};
1908
1909/**
1910 * ice_devlink_init_regions - Initialize devlink regions
1911 * @pf: the PF device structure
1912 *
1913 * Create devlink regions used to enable access to dump the contents of the
1914 * flash memory on the device.
1915 */
1916void ice_devlink_init_regions(struct ice_pf *pf)
1917{
1918	struct devlink *devlink = priv_to_devlink(pf);
1919	struct device *dev = ice_pf_to_dev(pf);
1920	u64 nvm_size, sram_size;
1921
1922	nvm_size = pf->hw.flash.flash_size;
1923	pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
1924					       nvm_size);
1925	if (IS_ERR(pf->nvm_region)) {
1926		dev_err(dev, "failed to create NVM devlink region, err %ld\n",
1927			PTR_ERR(pf->nvm_region));
1928		pf->nvm_region = NULL;
1929	}
1930
1931	sram_size = pf->hw.flash.sr_words * 2u;
1932	pf->sram_region = devlink_region_create(devlink, &ice_sram_region_ops,
1933						1, sram_size);
1934	if (IS_ERR(pf->sram_region)) {
1935		dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
1936			PTR_ERR(pf->sram_region));
1937		pf->sram_region = NULL;
1938	}
1939
1940	pf->devcaps_region = devlink_region_create(devlink,
1941						   &ice_devcaps_region_ops, 10,
1942						   ICE_AQ_MAX_BUF_LEN);
1943	if (IS_ERR(pf->devcaps_region)) {
1944		dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
1945			PTR_ERR(pf->devcaps_region));
1946		pf->devcaps_region = NULL;
1947	}
1948}
1949
1950/**
1951 * ice_devlink_destroy_regions - Destroy devlink regions
1952 * @pf: the PF device structure
1953 *
1954 * Remove previously created regions for this PF.
1955 */
1956void ice_devlink_destroy_regions(struct ice_pf *pf)
1957{
1958	if (pf->nvm_region)
1959		devlink_region_destroy(pf->nvm_region);
1960
1961	if (pf->sram_region)
1962		devlink_region_destroy(pf->sram_region);
1963
1964	if (pf->devcaps_region)
1965		devlink_region_destroy(pf->devcaps_region);
1966}