Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (C) 2022, Intel Corporation. */
   3
   4#include "ice_vf_lib_private.h"
   5#include "ice.h"
   6#include "ice_lib.h"
   7#include "ice_fltr.h"
   8#include "ice_virtchnl_allowlist.h"
   9
  10/* Public functions which may be accessed by all driver files */
  11
  12/**
  13 * ice_get_vf_by_id - Get pointer to VF by ID
  14 * @pf: the PF private structure
  15 * @vf_id: the VF ID to locate
  16 *
  17 * Locate and return a pointer to the VF structure associated with a given ID.
  18 * Returns NULL if the ID does not have a valid VF structure associated with
  19 * it.
  20 *
  21 * This function takes a reference to the VF, which must be released by
  22 * calling ice_put_vf() once the caller is finished accessing the VF structure
  23 * returned.
  24 */
  25struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
  26{
  27	struct ice_vf *vf;
  28
  29	rcu_read_lock();
  30	hash_for_each_possible_rcu(pf->vfs.table, vf, entry, vf_id) {
  31		if (vf->vf_id == vf_id) {
  32			struct ice_vf *found;
  33
  34			if (kref_get_unless_zero(&vf->refcnt))
  35				found = vf;
  36			else
  37				found = NULL;
  38
  39			rcu_read_unlock();
  40			return found;
  41		}
  42	}
  43	rcu_read_unlock();
  44
  45	return NULL;
  46}
  47
  48/**
  49 * ice_release_vf - Release VF associated with a refcount
  50 * @ref: the kref decremented to zero
  51 *
  52 * Callback function for kref_put to release a VF once its reference count has
  53 * hit zero.
  54 */
  55static void ice_release_vf(struct kref *ref)
  56{
  57	struct ice_vf *vf = container_of(ref, struct ice_vf, refcnt);
  58
  59	pci_dev_put(vf->vfdev);
  60
  61	vf->vf_ops->free(vf);
  62}
  63
  64/**
  65 * ice_put_vf - Release a reference to a VF
  66 * @vf: the VF structure to decrease reference count on
  67 *
  68 * Decrease the reference count for a VF, and free the entry if it is no
  69 * longer in use.
  70 *
  71 * This must be called after ice_get_vf_by_id() once the reference to the VF
  72 * structure is no longer used. Otherwise, the VF structure will never be
  73 * freed.
  74 */
  75void ice_put_vf(struct ice_vf *vf)
  76{
  77	kref_put(&vf->refcnt, ice_release_vf);
  78}
  79
  80/**
  81 * ice_has_vfs - Return true if the PF has any associated VFs
  82 * @pf: the PF private structure
  83 *
  84 * Return whether or not the PF has any allocated VFs.
  85 *
  86 * Note that this function only guarantees that there are no VFs at the point
  87 * of calling it. It does not guarantee that no more VFs will be added.
  88 */
  89bool ice_has_vfs(struct ice_pf *pf)
  90{
  91	/* A simple check that the hash table is not empty does not require
  92	 * the mutex or rcu_read_lock.
  93	 */
  94	return !hash_empty(pf->vfs.table);
  95}
  96
  97/**
  98 * ice_get_num_vfs - Get number of allocated VFs
  99 * @pf: the PF private structure
 100 *
 101 * Return the total number of allocated VFs. NOTE: VF IDs are not guaranteed
 102 * to be contiguous. Do not assume that a VF ID is guaranteed to be less than
 103 * the output of this function.
 104 */
 105u16 ice_get_num_vfs(struct ice_pf *pf)
 106{
 107	struct ice_vf *vf;
 108	unsigned int bkt;
 109	u16 num_vfs = 0;
 110
 111	rcu_read_lock();
 112	ice_for_each_vf_rcu(pf, bkt, vf)
 113		num_vfs++;
 114	rcu_read_unlock();
 115
 116	return num_vfs;
 117}
 118
 119/**
 120 * ice_get_vf_vsi - get VF's VSI based on the stored index
 121 * @vf: VF used to get VSI
 122 */
 123struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
 124{
 125	if (vf->lan_vsi_idx == ICE_NO_VSI)
 126		return NULL;
 127
 128	return vf->pf->vsi[vf->lan_vsi_idx];
 129}
 130
 131/**
 132 * ice_is_vf_disabled
 133 * @vf: pointer to the VF info
 134 *
 135 * If the PF has been disabled, there is no need resetting VF until PF is
 136 * active again. Similarly, if the VF has been disabled, this means something
 137 * else is resetting the VF, so we shouldn't continue.
 138 *
 139 * Returns true if the caller should consider the VF as disabled whether
 140 * because that single VF is explicitly disabled or because the PF is
 141 * currently disabled.
 142 */
 143bool ice_is_vf_disabled(struct ice_vf *vf)
 144{
 145	struct ice_pf *pf = vf->pf;
 146
 147	return (test_bit(ICE_VF_DIS, pf->state) ||
 148		test_bit(ICE_VF_STATE_DIS, vf->vf_states));
 149}
 150
 151/**
 152 * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
 153 * @vf: The VF being resseting
 154 *
 155 * The max poll time is about ~800ms, which is about the maximum time it takes
 156 * for a VF to be reset and/or a VF driver to be removed.
 157 */
 158static void ice_wait_on_vf_reset(struct ice_vf *vf)
 159{
 160	int i;
 161
 162	for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
 163		if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
 164			break;
 165		msleep(ICE_MAX_VF_RESET_SLEEP_MS);
 166	}
 167}
 168
 169/**
 170 * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
 171 * @vf: VF to check if it's ready to be configured/queried
 172 *
 173 * The purpose of this function is to make sure the VF is not in reset, not
 174 * disabled, and initialized so it can be configured and/or queried by a host
 175 * administrator.
 176 */
 177int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
 178{
 179	ice_wait_on_vf_reset(vf);
 180
 181	if (ice_is_vf_disabled(vf))
 182		return -EINVAL;
 183
 184	if (ice_check_vf_init(vf))
 185		return -EBUSY;
 186
 187	return 0;
 188}
 189
 190/**
 191 * ice_trigger_vf_reset - Reset a VF on HW
 192 * @vf: pointer to the VF structure
 193 * @is_vflr: true if VFLR was issued, false if not
 194 * @is_pfr: true if the reset was triggered due to a previous PFR
 195 *
 196 * Trigger hardware to start a reset for a particular VF. Expects the caller
 197 * to wait the proper amount of time to allow hardware to reset the VF before
 198 * it cleans up and restores VF functionality.
 199 */
 200static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
 201{
 202	/* Inform VF that it is no longer active, as a warning */
 203	clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
 204
 205	/* Disable VF's configuration API during reset. The flag is re-enabled
 206	 * when it's safe again to access VF's VSI.
 207	 */
 208	clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
 209
 210	/* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
 211	 * needs to clear them in the case of VFR/VFLR. If this is done for
 212	 * PFR, it can mess up VF resets because the VF driver may already
 213	 * have started cleanup by the time we get here.
 214	 */
 215	if (!is_pfr)
 216		vf->vf_ops->clear_mbx_register(vf);
 217
 218	vf->vf_ops->trigger_reset_register(vf, is_vflr);
 219}
 220
 221static void ice_vf_clear_counters(struct ice_vf *vf)
 222{
 223	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 224
 225	if (vsi)
 226		vsi->num_vlan = 0;
 227
 228	vf->num_mac = 0;
 229	memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
 230	memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
 231}
 232
 233/**
 234 * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
 235 * @vf: VF to perform pre VSI rebuild tasks
 236 *
 237 * These tasks are items that don't need to be amortized since they are most
 238 * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
 239 */
 240static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
 241{
 242	/* Close any IRQ mapping now */
 243	if (vf->vf_ops->irq_close)
 244		vf->vf_ops->irq_close(vf);
 245
 246	ice_vf_clear_counters(vf);
 247	vf->vf_ops->clear_reset_trigger(vf);
 248}
 249
 250/**
 251 * ice_vf_reconfig_vsi - Reconfigure a VF VSI with the device
 252 * @vf: VF to reconfigure the VSI for
 253 *
 254 * This is called when a single VF is being reset (i.e. VVF, VFLR, host VF
 255 * configuration change, etc).
 256 *
 257 * It brings the VSI down and then reconfigures it with the hardware.
 258 */
 259static int ice_vf_reconfig_vsi(struct ice_vf *vf)
 260{
 261	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 262	struct ice_pf *pf = vf->pf;
 263	int err;
 264
 265	if (WARN_ON(!vsi))
 266		return -EINVAL;
 267
 268	vsi->flags = ICE_VSI_FLAG_NO_INIT;
 269
 270	ice_vsi_decfg(vsi);
 271	ice_fltr_remove_all(vsi);
 272
 273	err = ice_vsi_cfg(vsi);
 274	if (err) {
 275		dev_err(ice_pf_to_dev(pf),
 276			"Failed to reconfigure the VF%u's VSI, error %d\n",
 277			vf->vf_id, err);
 278		return err;
 279	}
 280
 281	return 0;
 282}
 283
 284/**
 285 * ice_vf_rebuild_vsi - rebuild the VF's VSI
 286 * @vf: VF to rebuild the VSI for
 287 *
 288 * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
 289 * host, PFR, CORER, etc.).
 290 *
 291 * It reprograms the VSI configuration back into hardware.
 292 */
 293static int ice_vf_rebuild_vsi(struct ice_vf *vf)
 294{
 295	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 296	struct ice_pf *pf = vf->pf;
 297
 298	if (WARN_ON(!vsi))
 299		return -EINVAL;
 300
 301	if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT)) {
 302		dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
 303			vf->vf_id);
 304		return -EIO;
 305	}
 306	/* vsi->idx will remain the same in this case so don't update
 307	 * vf->lan_vsi_idx
 308	 */
 309	vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
 
 310
 311	return 0;
 312}
 313
 314/**
 315 * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
 316 * @vf: VF to add MAC filters for
 317 * @vsi: Pointer to VSI
 318 *
 319 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
 320 * always re-adds either a VLAN 0 or port VLAN based filter after reset.
 321 */
 322static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf, struct ice_vsi *vsi)
 323{
 324	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
 325	struct device *dev = ice_pf_to_dev(vf->pf);
 326	int err;
 327
 328	if (ice_vf_is_port_vlan_ena(vf)) {
 329		err = vlan_ops->set_port_vlan(vsi, &vf->port_vlan_info);
 330		if (err) {
 331			dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
 332				vf->vf_id, err);
 333			return err;
 334		}
 335
 336		err = vlan_ops->add_vlan(vsi, &vf->port_vlan_info);
 337	} else {
 338		/* clear possible previous port vlan config */
 339		err = ice_vsi_clear_port_vlan(vsi);
 340		if (err) {
 341			dev_err(dev, "failed to clear port VLAN via VSI parameters for VF %u, error %d\n",
 342				vf->vf_id, err);
 343			return err;
 344		}
 345		err = ice_vsi_add_vlan_zero(vsi);
 346	}
 347
 348	if (err) {
 349		dev_err(dev, "failed to add VLAN %u filter for VF %u during VF rebuild, error %d\n",
 350			ice_vf_is_port_vlan_ena(vf) ?
 351			ice_vf_get_port_vlan_id(vf) : 0, vf->vf_id, err);
 352		return err;
 353	}
 354
 355	err = vlan_ops->ena_rx_filtering(vsi);
 356	if (err)
 357		dev_warn(dev, "failed to enable Rx VLAN filtering for VF %d VSI %d during VF rebuild, error %d\n",
 358			 vf->vf_id, vsi->idx, err);
 359
 360	return 0;
 361}
 362
 363/**
 364 * ice_vf_rebuild_host_tx_rate_cfg - re-apply the Tx rate limiting configuration
 365 * @vf: VF to re-apply the configuration for
 366 *
 367 * Called after a VF VSI has been re-added/rebuild during reset. The PF driver
 368 * needs to re-apply the host configured Tx rate limiting configuration.
 369 */
 370static int ice_vf_rebuild_host_tx_rate_cfg(struct ice_vf *vf)
 371{
 372	struct device *dev = ice_pf_to_dev(vf->pf);
 373	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 374	int err;
 375
 376	if (WARN_ON(!vsi))
 377		return -EINVAL;
 378
 379	if (vf->min_tx_rate) {
 380		err = ice_set_min_bw_limit(vsi, (u64)vf->min_tx_rate * 1000);
 381		if (err) {
 382			dev_err(dev, "failed to set min Tx rate to %d Mbps for VF %u, error %d\n",
 383				vf->min_tx_rate, vf->vf_id, err);
 384			return err;
 385		}
 386	}
 387
 388	if (vf->max_tx_rate) {
 389		err = ice_set_max_bw_limit(vsi, (u64)vf->max_tx_rate * 1000);
 390		if (err) {
 391			dev_err(dev, "failed to set max Tx rate to %d Mbps for VF %u, error %d\n",
 392				vf->max_tx_rate, vf->vf_id, err);
 393			return err;
 394		}
 395	}
 396
 397	return 0;
 398}
 399
 400/**
 401 * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
 402 * @vf: VF to configure trust setting for
 403 */
 404static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
 405{
 406	assign_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps, vf->trusted);
 407}
 408
 409/**
 410 * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
 411 * @vf: VF to add MAC filters for
 412 *
 413 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
 414 * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
 415 */
 416static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
 417{
 418	struct device *dev = ice_pf_to_dev(vf->pf);
 419	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 420	u8 broadcast[ETH_ALEN];
 421	int status;
 422
 423	if (WARN_ON(!vsi))
 424		return -EINVAL;
 425
 426	if (ice_is_eswitch_mode_switchdev(vf->pf))
 427		return 0;
 428
 429	eth_broadcast_addr(broadcast);
 430	status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
 431	if (status) {
 432		dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %d\n",
 433			vf->vf_id, status);
 434		return status;
 435	}
 436
 437	vf->num_mac++;
 438
 439	if (is_valid_ether_addr(vf->hw_lan_addr)) {
 440		status = ice_fltr_add_mac(vsi, vf->hw_lan_addr,
 441					  ICE_FWD_TO_VSI);
 442		if (status) {
 443			dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %d\n",
 444				&vf->hw_lan_addr[0], vf->vf_id,
 445				status);
 446			return status;
 447		}
 448		vf->num_mac++;
 449
 450		ether_addr_copy(vf->dev_lan_addr, vf->hw_lan_addr);
 451	}
 452
 453	return 0;
 454}
 455
 456/**
 457 * ice_vf_rebuild_aggregator_node_cfg - rebuild aggregator node config
 458 * @vsi: Pointer to VSI
 459 *
 460 * This function moves VSI into corresponding scheduler aggregator node
 461 * based on cached value of "aggregator node info" per VSI
 462 */
 463static void ice_vf_rebuild_aggregator_node_cfg(struct ice_vsi *vsi)
 464{
 465	struct ice_pf *pf = vsi->back;
 466	struct device *dev;
 467	int status;
 468
 469	if (!vsi->agg_node)
 470		return;
 471
 472	dev = ice_pf_to_dev(pf);
 473	if (vsi->agg_node->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
 474		dev_dbg(dev,
 475			"agg_id %u already has reached max_num_vsis %u\n",
 476			vsi->agg_node->agg_id, vsi->agg_node->num_vsis);
 477		return;
 478	}
 479
 480	status = ice_move_vsi_to_agg(pf->hw.port_info, vsi->agg_node->agg_id,
 481				     vsi->idx, vsi->tc_cfg.ena_tc);
 482	if (status)
 483		dev_dbg(dev, "unable to move VSI idx %u into aggregator %u node",
 484			vsi->idx, vsi->agg_node->agg_id);
 485	else
 486		vsi->agg_node->num_vsis++;
 487}
 488
 489/**
 490 * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
 491 * @vf: VF to rebuild host configuration on
 492 */
 493static void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
 494{
 495	struct device *dev = ice_pf_to_dev(vf->pf);
 496	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 497
 498	if (WARN_ON(!vsi))
 499		return;
 500
 501	ice_vf_set_host_trust_cfg(vf);
 502
 503	if (ice_vf_rebuild_host_mac_cfg(vf))
 504		dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
 505			vf->vf_id);
 506
 507	if (ice_vf_rebuild_host_vlan_cfg(vf, vsi))
 508		dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
 509			vf->vf_id);
 510
 511	if (ice_vf_rebuild_host_tx_rate_cfg(vf))
 512		dev_err(dev, "failed to rebuild Tx rate limiting configuration for VF %u\n",
 513			vf->vf_id);
 514
 515	if (ice_vsi_apply_spoofchk(vsi, vf->spoofchk))
 516		dev_err(dev, "failed to rebuild spoofchk configuration for VF %d\n",
 517			vf->vf_id);
 518
 519	/* rebuild aggregator node config for main VF VSI */
 520	ice_vf_rebuild_aggregator_node_cfg(vsi);
 521}
 522
 523/**
 524 * ice_set_vf_state_qs_dis - Set VF queues state to disabled
 525 * @vf: pointer to the VF structure
 526 */
 527static void ice_set_vf_state_qs_dis(struct ice_vf *vf)
 528{
 529	/* Clear Rx/Tx enabled queues flag */
 530	bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
 531	bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
 532	clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
 533}
 534
 535/**
 536 * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
 537 * @vf: VF to set in initialized state
 538 *
 539 * After this function the VF will be ready to receive/handle the
 540 * VIRTCHNL_OP_GET_VF_RESOURCES message
 541 */
 542static void ice_vf_set_initialized(struct ice_vf *vf)
 543{
 544	ice_set_vf_state_qs_dis(vf);
 545	clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
 546	clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
 547	clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
 548	set_bit(ICE_VF_STATE_INIT, vf->vf_states);
 549	memset(&vf->vlan_v2_caps, 0, sizeof(vf->vlan_v2_caps));
 550}
 551
 552/**
 553 * ice_vf_post_vsi_rebuild - Reset tasks that occur after VSI rebuild
 554 * @vf: the VF being reset
 555 *
 556 * Perform reset tasks which must occur after the VSI has been re-created or
 557 * rebuilt during a VF reset.
 558 */
 559static void ice_vf_post_vsi_rebuild(struct ice_vf *vf)
 560{
 561	ice_vf_rebuild_host_cfg(vf);
 562	ice_vf_set_initialized(vf);
 563
 564	vf->vf_ops->post_vsi_rebuild(vf);
 565}
 566
 567/**
 568 * ice_is_any_vf_in_unicast_promisc - check if any VF(s)
 569 * are in unicast promiscuous mode
 570 * @pf: PF structure for accessing VF(s)
 571 *
 572 * Return false if no VF(s) are in unicast promiscuous mode,
 573 * else return true
 574 */
 575bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf)
 576{
 577	bool is_vf_promisc = false;
 578	struct ice_vf *vf;
 579	unsigned int bkt;
 580
 581	rcu_read_lock();
 582	ice_for_each_vf_rcu(pf, bkt, vf) {
 583		/* found a VF that has promiscuous mode configured */
 584		if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states)) {
 585			is_vf_promisc = true;
 586			break;
 587		}
 588	}
 589	rcu_read_unlock();
 590
 591	return is_vf_promisc;
 592}
 593
 594/**
 595 * ice_vf_get_promisc_masks - Calculate masks for promiscuous modes
 596 * @vf: the VF pointer
 597 * @vsi: the VSI to configure
 598 * @ucast_m: promiscuous mask to apply to unicast
 599 * @mcast_m: promiscuous mask to apply to multicast
 600 *
 601 * Decide which mask should be used for unicast and multicast filter,
 602 * based on presence of VLANs
 603 */
 604void
 605ice_vf_get_promisc_masks(struct ice_vf *vf, struct ice_vsi *vsi,
 606			 u8 *ucast_m, u8 *mcast_m)
 607{
 608	if (ice_vf_is_port_vlan_ena(vf) ||
 609	    ice_vsi_has_non_zero_vlans(vsi)) {
 610		*mcast_m = ICE_MCAST_VLAN_PROMISC_BITS;
 611		*ucast_m = ICE_UCAST_VLAN_PROMISC_BITS;
 612	} else {
 613		*mcast_m = ICE_MCAST_PROMISC_BITS;
 614		*ucast_m = ICE_UCAST_PROMISC_BITS;
 615	}
 616}
 617
 618/**
 619 * ice_vf_clear_all_promisc_modes - Clear promisc/allmulticast on VF VSI
 620 * @vf: the VF pointer
 621 * @vsi: the VSI to configure
 622 *
 623 * Clear all promiscuous/allmulticast filters for a VF
 624 */
 625static int
 626ice_vf_clear_all_promisc_modes(struct ice_vf *vf, struct ice_vsi *vsi)
 627{
 628	struct ice_pf *pf = vf->pf;
 629	u8 ucast_m, mcast_m;
 630	int ret = 0;
 631
 632	ice_vf_get_promisc_masks(vf, vsi, &ucast_m, &mcast_m);
 633	if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states)) {
 634		if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
 635			if (ice_is_dflt_vsi_in_use(vsi->port_info))
 636				ret = ice_clear_dflt_vsi(vsi);
 637		} else {
 638			ret = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
 639		}
 640
 641		if (ret) {
 642			dev_err(ice_pf_to_dev(vf->pf), "Disabling promiscuous mode failed\n");
 643		} else {
 644			clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
 645			dev_info(ice_pf_to_dev(vf->pf), "Disabling promiscuous mode succeeded\n");
 646		}
 647	}
 648
 649	if (test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
 650		ret = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
 651		if (ret) {
 652			dev_err(ice_pf_to_dev(vf->pf), "Disabling allmulticast mode failed\n");
 653		} else {
 654			clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
 655			dev_info(ice_pf_to_dev(vf->pf), "Disabling allmulticast mode succeeded\n");
 656		}
 657	}
 658	return ret;
 659}
 660
 661/**
 662 * ice_vf_set_vsi_promisc - Enable promiscuous mode for a VF VSI
 663 * @vf: the VF to configure
 664 * @vsi: the VF's VSI
 665 * @promisc_m: the promiscuous mode to enable
 666 */
 667int
 668ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
 669{
 670	struct ice_hw *hw = &vsi->back->hw;
 671	int status;
 672
 673	if (ice_vf_is_port_vlan_ena(vf))
 674		status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m,
 675						  ice_vf_get_port_vlan_id(vf));
 676	else if (ice_vsi_has_non_zero_vlans(vsi))
 677		status = ice_fltr_set_vlan_vsi_promisc(hw, vsi, promisc_m);
 678	else
 679		status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m, 0);
 680
 681	if (status && status != -EEXIST) {
 682		dev_err(ice_pf_to_dev(vsi->back), "enable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
 683			vf->vf_id, status);
 684		return status;
 685	}
 686
 687	return 0;
 688}
 689
 690/**
 691 * ice_vf_clear_vsi_promisc - Disable promiscuous mode for a VF VSI
 692 * @vf: the VF to configure
 693 * @vsi: the VF's VSI
 694 * @promisc_m: the promiscuous mode to disable
 695 */
 696int
 697ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
 698{
 699	struct ice_hw *hw = &vsi->back->hw;
 700	int status;
 701
 702	if (ice_vf_is_port_vlan_ena(vf))
 703		status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m,
 704						    ice_vf_get_port_vlan_id(vf));
 705	else if (ice_vsi_has_non_zero_vlans(vsi))
 706		status = ice_fltr_clear_vlan_vsi_promisc(hw, vsi, promisc_m);
 707	else
 708		status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m, 0);
 709
 710	if (status && status != -ENOENT) {
 711		dev_err(ice_pf_to_dev(vsi->back), "disable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
 712			vf->vf_id, status);
 713		return status;
 714	}
 715
 716	return 0;
 717}
 718
 719/**
 720 * ice_reset_vf_mbx_cnt - reset VF mailbox message count
 721 * @vf: pointer to the VF structure
 722 *
 723 * This function clears the VF mailbox message count, and should be called on
 724 * VF reset.
 725 */
 726static void ice_reset_vf_mbx_cnt(struct ice_vf *vf)
 727{
 728	struct ice_pf *pf = vf->pf;
 729
 730	if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
 731		ice_mbx_vf_clear_cnt_e830(&pf->hw, vf->vf_id);
 732	else
 733		ice_mbx_clear_malvf(&vf->mbx_info);
 734}
 735
 736/**
 737 * ice_reset_all_vfs - reset all allocated VFs in one go
 738 * @pf: pointer to the PF structure
 739 *
 740 * Reset all VFs at once, in response to a PF or other device reset.
 741 *
 742 * First, tell the hardware to reset each VF, then do all the waiting in one
 743 * chunk, and finally finish restoring each VF after the wait. This is useful
 744 * during PF routines which need to reset all VFs, as otherwise it must perform
 745 * these resets in a serialized fashion.
 746 */
 747void ice_reset_all_vfs(struct ice_pf *pf)
 748{
 749	struct device *dev = ice_pf_to_dev(pf);
 750	struct ice_hw *hw = &pf->hw;
 751	struct ice_vf *vf;
 752	unsigned int bkt;
 753
 754	/* If we don't have any VFs, then there is nothing to reset */
 755	if (!ice_has_vfs(pf))
 756		return;
 757
 758	mutex_lock(&pf->vfs.table_lock);
 759
 760	/* clear all malicious info if the VFs are getting reset */
 761	ice_for_each_vf(pf, bkt, vf)
 762		ice_reset_vf_mbx_cnt(vf);
 
 
 
 763
 764	/* If VFs have been disabled, there is no need to reset */
 765	if (test_and_set_bit(ICE_VF_DIS, pf->state)) {
 766		mutex_unlock(&pf->vfs.table_lock);
 767		return;
 768	}
 769
 770	/* Begin reset on all VFs at once */
 771	ice_for_each_vf(pf, bkt, vf)
 772		ice_trigger_vf_reset(vf, true, true);
 773
 774	/* HW requires some time to make sure it can flush the FIFO for a VF
 775	 * when it resets it. Now that we've triggered all of the VFs, iterate
 776	 * the table again and wait for each VF to complete.
 777	 */
 778	ice_for_each_vf(pf, bkt, vf) {
 779		if (!vf->vf_ops->poll_reset_status(vf)) {
 780			/* Display a warning if at least one VF didn't manage
 781			 * to reset in time, but continue on with the
 782			 * operation.
 783			 */
 784			dev_warn(dev, "VF %u reset check timeout\n", vf->vf_id);
 785			break;
 786		}
 787	}
 788
 789	/* free VF resources to begin resetting the VSI state */
 790	ice_for_each_vf(pf, bkt, vf) {
 791		mutex_lock(&vf->cfg_lock);
 792
 793		ice_eswitch_detach_vf(pf, vf);
 794		vf->driver_caps = 0;
 795		ice_vc_set_default_allowlist(vf);
 796
 797		ice_vf_fdir_exit(vf);
 798		ice_vf_fdir_init(vf);
 799		/* clean VF control VSI when resetting VFs since it should be
 800		 * setup only when VF creates its first FDIR rule.
 801		 */
 802		if (vf->ctrl_vsi_idx != ICE_NO_VSI)
 803			ice_vf_ctrl_invalidate_vsi(vf);
 804
 805		ice_vf_pre_vsi_rebuild(vf);
 806		ice_vf_rebuild_vsi(vf);
 807		ice_vf_post_vsi_rebuild(vf);
 808
 809		ice_eswitch_attach_vf(pf, vf);
 810
 811		mutex_unlock(&vf->cfg_lock);
 812	}
 813
 
 
 
 
 814	ice_flush(hw);
 815	clear_bit(ICE_VF_DIS, pf->state);
 816
 817	mutex_unlock(&pf->vfs.table_lock);
 818}
 819
 820/**
 821 * ice_notify_vf_reset - Notify VF of a reset event
 822 * @vf: pointer to the VF structure
 823 */
 824static void ice_notify_vf_reset(struct ice_vf *vf)
 825{
 826	struct ice_hw *hw = &vf->pf->hw;
 827	struct virtchnl_pf_event pfe;
 828
 829	/* Bail out if VF is in disabled state, neither initialized, nor active
 830	 * state - otherwise proceed with notifications
 831	 */
 832	if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
 833	     !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
 834	    test_bit(ICE_VF_STATE_DIS, vf->vf_states))
 835		return;
 836
 837	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
 838	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
 839	ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
 840			      VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
 841			      NULL);
 842}
 843
 844/**
 845 * ice_reset_vf - Reset a particular VF
 846 * @vf: pointer to the VF structure
 847 * @flags: flags controlling behavior of the reset
 848 *
 849 * Flags:
 850 *   ICE_VF_RESET_VFLR - Indicates a reset is due to VFLR event
 851 *   ICE_VF_RESET_NOTIFY - Send VF a notification prior to reset
 852 *   ICE_VF_RESET_LOCK - Acquire VF cfg_lock before resetting
 853 *
 854 * Returns 0 if the VF is currently in reset, if resets are disabled, or if
 855 * the VF resets successfully. Returns an error code if the VF fails to
 856 * rebuild.
 857 */
 858int ice_reset_vf(struct ice_vf *vf, u32 flags)
 859{
 860	struct ice_pf *pf = vf->pf;
 861	struct ice_lag *lag;
 862	struct ice_vsi *vsi;
 863	u8 act_prt, pri_prt;
 864	struct device *dev;
 
 865	int err = 0;
 866	bool rsd;
 867
 868	dev = ice_pf_to_dev(pf);
 869	act_prt = ICE_LAG_INVALID_PORT;
 870	pri_prt = pf->hw.port_info->lport;
 871
 872	if (flags & ICE_VF_RESET_NOTIFY)
 873		ice_notify_vf_reset(vf);
 874
 875	if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
 876		dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
 877			vf->vf_id);
 878		return 0;
 879	}
 880
 881	if (flags & ICE_VF_RESET_LOCK)
 882		mutex_lock(&vf->cfg_lock);
 883	else
 884		lockdep_assert_held(&vf->cfg_lock);
 885
 886	lag = pf->lag;
 887	mutex_lock(&pf->lag_mutex);
 888	if (lag && lag->bonded && lag->primary) {
 889		act_prt = lag->active_port;
 890		if (act_prt != pri_prt && act_prt != ICE_LAG_INVALID_PORT &&
 891		    lag->upper_netdev)
 892			ice_lag_move_vf_nodes_cfg(lag, act_prt, pri_prt);
 893		else
 894			act_prt = ICE_LAG_INVALID_PORT;
 895	}
 896
 897	if (ice_is_vf_disabled(vf)) {
 898		vsi = ice_get_vf_vsi(vf);
 899		if (!vsi) {
 900			dev_dbg(dev, "VF is already removed\n");
 901			err = -EINVAL;
 902			goto out_unlock;
 903		}
 904		ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
 905
 906		if (ice_vsi_is_rx_queue_active(vsi))
 907			ice_vsi_stop_all_rx_rings(vsi);
 908
 909		dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
 910			vf->vf_id);
 911		goto out_unlock;
 912	}
 913
 
 
 
 
 
 914	/* Set VF disable bit state here, before triggering reset */
 915	set_bit(ICE_VF_STATE_DIS, vf->vf_states);
 916	ice_trigger_vf_reset(vf, flags & ICE_VF_RESET_VFLR, false);
 917
 918	vsi = ice_get_vf_vsi(vf);
 919	if (WARN_ON(!vsi)) {
 920		err = -EIO;
 921		goto out_unlock;
 922	}
 923
 924	ice_dis_vf_qs(vf);
 925
 926	/* Call Disable LAN Tx queue AQ whether or not queues are
 927	 * enabled. This is needed for successful completion of VFR.
 928	 */
 929	ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
 930			NULL, vf->vf_ops->reset_type, vf->vf_id, NULL);
 931
 932	/* poll VPGEN_VFRSTAT reg to make sure
 933	 * that reset is complete
 934	 */
 935	rsd = vf->vf_ops->poll_reset_status(vf);
 936
 937	/* Display a warning if VF didn't manage to reset in time, but need to
 938	 * continue on with the operation.
 939	 */
 940	if (!rsd)
 941		dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
 942
 943	vf->driver_caps = 0;
 944	ice_vc_set_default_allowlist(vf);
 945
 946	/* disable promiscuous modes in case they were enabled
 947	 * ignore any error if disabling process failed
 948	 */
 949	ice_vf_clear_all_promisc_modes(vf, vsi);
 950
 
 
 951	ice_vf_fdir_exit(vf);
 952	ice_vf_fdir_init(vf);
 953	/* clean VF control VSI when resetting VF since it should be setup
 954	 * only when VF creates its first FDIR rule.
 955	 */
 956	if (vf->ctrl_vsi_idx != ICE_NO_VSI)
 957		ice_vf_ctrl_vsi_release(vf);
 958
 959	ice_vf_pre_vsi_rebuild(vf);
 960
 961	if (ice_vf_reconfig_vsi(vf)) {
 962		dev_err(dev, "Failed to release and setup the VF%u's VSI\n",
 963			vf->vf_id);
 964		err = -EFAULT;
 965		goto out_unlock;
 966	}
 967
 968	ice_vf_post_vsi_rebuild(vf);
 969	vsi = ice_get_vf_vsi(vf);
 970	if (WARN_ON(!vsi)) {
 971		err = -EINVAL;
 972		goto out_unlock;
 973	}
 974
 975	ice_eswitch_update_repr(&vf->repr_id, vsi);
 
 976
 977	/* if the VF has been reset allow it to come up again */
 978	ice_reset_vf_mbx_cnt(vf);
 
 
 
 979
 980out_unlock:
 981	if (lag && lag->bonded && lag->primary &&
 982	    act_prt != ICE_LAG_INVALID_PORT)
 983		ice_lag_move_vf_nodes_cfg(lag, pri_prt, act_prt);
 984	mutex_unlock(&pf->lag_mutex);
 985
 986	if (flags & ICE_VF_RESET_LOCK)
 987		mutex_unlock(&vf->cfg_lock);
 988
 989	return err;
 990}
 991
 992/**
 993 * ice_set_vf_state_dis - Set VF state to disabled
 994 * @vf: pointer to the VF structure
 995 */
 996void ice_set_vf_state_dis(struct ice_vf *vf)
 997{
 998	ice_set_vf_state_qs_dis(vf);
 999	vf->vf_ops->clear_reset_state(vf);
 
 
1000}
1001
1002/* Private functions only accessed from other virtualization files */
1003
1004/**
1005 * ice_initialize_vf_entry - Initialize a VF entry
1006 * @vf: pointer to the VF structure
1007 */
1008void ice_initialize_vf_entry(struct ice_vf *vf)
1009{
1010	struct ice_pf *pf = vf->pf;
1011	struct ice_vfs *vfs;
1012
1013	vfs = &pf->vfs;
1014
1015	/* assign default capabilities */
1016	vf->spoofchk = true;
1017	ice_vc_set_default_allowlist(vf);
1018	ice_virtchnl_set_dflt_ops(vf);
1019
1020	/* set default number of MSI-X */
1021	vf->num_msix = vfs->num_msix_per;
1022	vf->num_vf_qs = vfs->num_qps_per;
1023
1024	/* ctrl_vsi_idx will be set to a valid value only when iAVF
1025	 * creates its first fdir rule.
1026	 */
1027	ice_vf_ctrl_invalidate_vsi(vf);
1028	ice_vf_fdir_init(vf);
1029
1030	/* Initialize mailbox info for this VF */
1031	if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
1032		ice_mbx_vf_clear_cnt_e830(&pf->hw, vf->vf_id);
1033	else
1034		ice_mbx_init_vf_info(&pf->hw, &vf->mbx_info);
1035
1036	mutex_init(&vf->cfg_lock);
1037}
1038
1039void ice_deinitialize_vf_entry(struct ice_vf *vf)
1040{
1041	struct ice_pf *pf = vf->pf;
1042
1043	if (!ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
1044		list_del(&vf->mbx_info.list_entry);
1045}
1046
1047/**
1048 * ice_dis_vf_qs - Disable the VF queues
1049 * @vf: pointer to the VF structure
1050 */
1051void ice_dis_vf_qs(struct ice_vf *vf)
1052{
1053	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1054
1055	if (WARN_ON(!vsi))
1056		return;
1057
1058	ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
1059	ice_vsi_stop_all_rx_rings(vsi);
1060	ice_set_vf_state_qs_dis(vf);
1061}
1062
1063/**
1064 * ice_err_to_virt_err - translate errors for VF return code
1065 * @err: error return code
1066 */
1067enum virtchnl_status_code ice_err_to_virt_err(int err)
1068{
1069	switch (err) {
1070	case 0:
1071		return VIRTCHNL_STATUS_SUCCESS;
1072	case -EINVAL:
1073	case -ENODEV:
1074		return VIRTCHNL_STATUS_ERR_PARAM;
1075	case -ENOMEM:
1076		return VIRTCHNL_STATUS_ERR_NO_MEMORY;
1077	case -EALREADY:
1078	case -EBUSY:
1079	case -EIO:
1080	case -ENOSPC:
1081		return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
1082	default:
1083		return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
1084	}
1085}
1086
1087/**
1088 * ice_check_vf_init - helper to check if VF init complete
1089 * @vf: the pointer to the VF to check
1090 */
1091int ice_check_vf_init(struct ice_vf *vf)
1092{
1093	struct ice_pf *pf = vf->pf;
1094
1095	if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
1096		dev_err(ice_pf_to_dev(pf), "VF ID: %u in reset. Try again.\n",
1097			vf->vf_id);
1098		return -EBUSY;
1099	}
1100	return 0;
1101}
1102
1103/**
1104 * ice_vf_get_port_info - Get the VF's port info structure
1105 * @vf: VF used to get the port info structure for
1106 */
1107struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
1108{
1109	return vf->pf->hw.port_info;
1110}
1111
1112/**
1113 * ice_cfg_mac_antispoof - Configure MAC antispoof checking behavior
1114 * @vsi: the VSI to configure
1115 * @enable: whether to enable or disable the spoof checking
1116 *
1117 * Configure a VSI to enable (or disable) spoof checking behavior.
1118 */
1119static int ice_cfg_mac_antispoof(struct ice_vsi *vsi, bool enable)
1120{
1121	struct ice_vsi_ctx *ctx;
1122	int err;
1123
1124	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1125	if (!ctx)
1126		return -ENOMEM;
1127
1128	ctx->info.sec_flags = vsi->info.sec_flags;
1129	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1130
1131	if (enable)
1132		ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
1133	else
1134		ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
1135
1136	err = ice_update_vsi(&vsi->back->hw, vsi->idx, ctx, NULL);
1137	if (err)
1138		dev_err(ice_pf_to_dev(vsi->back), "Failed to configure Tx MAC anti-spoof %s for VSI %d, error %d\n",
1139			enable ? "ON" : "OFF", vsi->vsi_num, err);
1140	else
1141		vsi->info.sec_flags = ctx->info.sec_flags;
1142
1143	kfree(ctx);
1144
1145	return err;
1146}
1147
1148/**
1149 * ice_vsi_ena_spoofchk - enable Tx spoof checking for this VSI
1150 * @vsi: VSI to enable Tx spoof checking for
1151 */
1152static int ice_vsi_ena_spoofchk(struct ice_vsi *vsi)
1153{
1154	struct ice_vsi_vlan_ops *vlan_ops;
1155	int err = 0;
1156
1157	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1158
1159	/* Allow VF with VLAN 0 only to send all tagged traffic */
1160	if (vsi->type != ICE_VSI_VF || ice_vsi_has_non_zero_vlans(vsi)) {
1161		err = vlan_ops->ena_tx_filtering(vsi);
1162		if (err)
1163			return err;
1164	}
1165
1166	return ice_cfg_mac_antispoof(vsi, true);
1167}
1168
1169/**
1170 * ice_vsi_dis_spoofchk - disable Tx spoof checking for this VSI
1171 * @vsi: VSI to disable Tx spoof checking for
1172 */
1173static int ice_vsi_dis_spoofchk(struct ice_vsi *vsi)
1174{
1175	struct ice_vsi_vlan_ops *vlan_ops;
1176	int err;
1177
1178	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1179
1180	err = vlan_ops->dis_tx_filtering(vsi);
1181	if (err)
1182		return err;
1183
1184	return ice_cfg_mac_antispoof(vsi, false);
1185}
1186
1187/**
1188 * ice_vsi_apply_spoofchk - Apply Tx spoof checking setting to a VSI
1189 * @vsi: VSI associated to the VF
1190 * @enable: whether to enable or disable the spoof checking
1191 */
1192int ice_vsi_apply_spoofchk(struct ice_vsi *vsi, bool enable)
1193{
1194	int err;
1195
1196	if (enable)
1197		err = ice_vsi_ena_spoofchk(vsi);
1198	else
1199		err = ice_vsi_dis_spoofchk(vsi);
1200
1201	return err;
1202}
1203
1204/**
1205 * ice_is_vf_trusted
1206 * @vf: pointer to the VF info
1207 */
1208bool ice_is_vf_trusted(struct ice_vf *vf)
1209{
1210	return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1211}
1212
1213/**
1214 * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
1215 * @vf: the VF to check
1216 *
1217 * Returns true if the VF has no Rx and no Tx queues enabled and returns false
1218 * otherwise
1219 */
1220bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
1221{
1222	return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
1223		!bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
1224}
1225
1226/**
1227 * ice_is_vf_link_up - check if the VF's link is up
1228 * @vf: VF to check if link is up
1229 */
1230bool ice_is_vf_link_up(struct ice_vf *vf)
1231{
1232	struct ice_port_info *pi = ice_vf_get_port_info(vf);
1233
1234	if (ice_check_vf_init(vf))
1235		return false;
1236
1237	if (ice_vf_has_no_qs_ena(vf))
1238		return false;
1239	else if (vf->link_forced)
1240		return vf->link_up;
1241	else
1242		return pi->phy.link_info.link_info &
1243			ICE_AQ_LINK_UP;
1244}
1245
1246/**
1247 * ice_vf_ctrl_invalidate_vsi - invalidate ctrl_vsi_idx to remove VSI access
1248 * @vf: VF that control VSI is being invalidated on
1249 */
1250void ice_vf_ctrl_invalidate_vsi(struct ice_vf *vf)
1251{
1252	vf->ctrl_vsi_idx = ICE_NO_VSI;
1253}
1254
1255/**
1256 * ice_vf_ctrl_vsi_release - invalidate the VF's control VSI after freeing it
1257 * @vf: VF that control VSI is being released on
1258 */
1259void ice_vf_ctrl_vsi_release(struct ice_vf *vf)
1260{
1261	ice_vsi_release(vf->pf->vsi[vf->ctrl_vsi_idx]);
1262	ice_vf_ctrl_invalidate_vsi(vf);
 
 
1263}
1264
1265/**
1266 * ice_vf_ctrl_vsi_setup - Set up a VF control VSI
1267 * @vf: VF to setup control VSI for
1268 *
1269 * Returns pointer to the successfully allocated VSI struct on success,
1270 * otherwise returns NULL on failure.
1271 */
1272struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf)
1273{
1274	struct ice_vsi_cfg_params params = {};
1275	struct ice_pf *pf = vf->pf;
1276	struct ice_vsi *vsi;
 
1277
1278	params.type = ICE_VSI_CTRL;
1279	params.port_info = ice_vf_get_port_info(vf);
1280	params.vf = vf;
1281	params.flags = ICE_VSI_FLAG_INIT;
1282
1283	vsi = ice_vsi_setup(pf, &params);
1284	if (!vsi) {
1285		dev_err(ice_pf_to_dev(pf), "Failed to create VF control VSI\n");
1286		ice_vf_ctrl_invalidate_vsi(vf);
 
 
 
 
 
1287	}
1288
1289	return vsi;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1290}
1291
1292/**
1293 * ice_vf_init_host_cfg - Initialize host admin configuration
1294 * @vf: VF to initialize
1295 * @vsi: the VSI created at initialization
1296 *
1297 * Initialize the VF host configuration. Called during VF creation to setup
1298 * VLAN 0, add the VF VSI broadcast filter, and setup spoof checking. It
1299 * should only be called during VF creation.
1300 */
1301int ice_vf_init_host_cfg(struct ice_vf *vf, struct ice_vsi *vsi)
1302{
1303	struct ice_vsi_vlan_ops *vlan_ops;
1304	struct ice_pf *pf = vf->pf;
1305	u8 broadcast[ETH_ALEN];
1306	struct device *dev;
1307	int err;
1308
1309	dev = ice_pf_to_dev(pf);
 
 
 
 
 
 
1310
1311	err = ice_vsi_add_vlan_zero(vsi);
1312	if (err) {
1313		dev_warn(dev, "Failed to add VLAN 0 filter for VF %d\n",
1314			 vf->vf_id);
1315		return err;
1316	}
1317
1318	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
1319	err = vlan_ops->ena_rx_filtering(vsi);
1320	if (err) {
1321		dev_warn(dev, "Failed to enable Rx VLAN filtering for VF %d\n",
1322			 vf->vf_id);
 
1323		return err;
1324	}
1325
1326	eth_broadcast_addr(broadcast);
1327	err = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
1328	if (err) {
1329		dev_err(dev, "Failed to add broadcast MAC filter for VF %d, status %d\n",
1330			vf->vf_id, err);
1331		return err;
1332	}
1333
1334	vf->num_mac = 1;
 
1335
1336	err = ice_vsi_apply_spoofchk(vsi, vf->spoofchk);
1337	if (err) {
1338		dev_warn(dev, "Failed to initialize spoofchk setting for VF %d\n",
1339			 vf->vf_id);
1340		return err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1341	}
1342
1343	return 0;
1344}
1345
1346/**
1347 * ice_vf_invalidate_vsi - invalidate vsi_idx to remove VSI access
1348 * @vf: VF to remove access to VSI for
 
 
 
1349 */
1350void ice_vf_invalidate_vsi(struct ice_vf *vf)
1351{
1352	vf->lan_vsi_idx = ICE_NO_VSI;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1353}
1354
1355/**
1356 * ice_vf_vsi_release - Release the VF VSI and invalidate indexes
1357 * @vf: pointer to the VF structure
1358 *
1359 * Release the VF associated with this VSI and then invalidate the VSI
1360 * indexes.
1361 */
1362void ice_vf_vsi_release(struct ice_vf *vf)
1363{
 
1364	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1365
1366	if (WARN_ON(!vsi))
1367		return;
1368
1369	ice_vsi_release(vsi);
1370	ice_vf_invalidate_vsi(vf);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1371}
1372
1373/**
1374 * ice_get_vf_ctrl_vsi - Get first VF control VSI pointer
1375 * @pf: the PF private structure
1376 * @vsi: pointer to the VSI
1377 *
1378 * Return first found VF control VSI other than the vsi
1379 * passed by parameter. This function is used to determine
1380 * whether new resources have to be allocated for control VSI
1381 * or they can be shared with existing one.
1382 *
1383 * Return found VF control VSI pointer other itself. Return
1384 * NULL Otherwise.
 
 
 
 
 
 
 
 
 
 
1385 *
 
 
1386 */
1387struct ice_vsi *ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi)
1388{
1389	struct ice_vsi *ctrl_vsi = NULL;
1390	struct ice_vf *vf;
1391	unsigned int bkt;
1392
1393	rcu_read_lock();
1394	ice_for_each_vf_rcu(pf, bkt, vf) {
1395		if (vf != vsi->vf && vf->ctrl_vsi_idx != ICE_NO_VSI) {
1396			ctrl_vsi = pf->vsi[vf->ctrl_vsi_idx];
1397			break;
1398		}
1399	}
1400
1401	rcu_read_unlock();
1402	return ctrl_vsi;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1403}
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (C) 2022, Intel Corporation. */
   3
   4#include "ice_vf_lib_private.h"
   5#include "ice.h"
   6#include "ice_lib.h"
   7#include "ice_fltr.h"
   8#include "ice_virtchnl_allowlist.h"
   9
  10/* Public functions which may be accessed by all driver files */
  11
  12/**
  13 * ice_get_vf_by_id - Get pointer to VF by ID
  14 * @pf: the PF private structure
  15 * @vf_id: the VF ID to locate
  16 *
  17 * Locate and return a pointer to the VF structure associated with a given ID.
  18 * Returns NULL if the ID does not have a valid VF structure associated with
  19 * it.
  20 *
  21 * This function takes a reference to the VF, which must be released by
  22 * calling ice_put_vf() once the caller is finished accessing the VF structure
  23 * returned.
  24 */
  25struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
  26{
  27	struct ice_vf *vf;
  28
  29	rcu_read_lock();
  30	hash_for_each_possible_rcu(pf->vfs.table, vf, entry, vf_id) {
  31		if (vf->vf_id == vf_id) {
  32			struct ice_vf *found;
  33
  34			if (kref_get_unless_zero(&vf->refcnt))
  35				found = vf;
  36			else
  37				found = NULL;
  38
  39			rcu_read_unlock();
  40			return found;
  41		}
  42	}
  43	rcu_read_unlock();
  44
  45	return NULL;
  46}
  47
  48/**
  49 * ice_release_vf - Release VF associated with a refcount
  50 * @ref: the kref decremented to zero
  51 *
  52 * Callback function for kref_put to release a VF once its reference count has
  53 * hit zero.
  54 */
  55static void ice_release_vf(struct kref *ref)
  56{
  57	struct ice_vf *vf = container_of(ref, struct ice_vf, refcnt);
  58
 
 
  59	vf->vf_ops->free(vf);
  60}
  61
  62/**
  63 * ice_put_vf - Release a reference to a VF
  64 * @vf: the VF structure to decrease reference count on
  65 *
  66 * Decrease the reference count for a VF, and free the entry if it is no
  67 * longer in use.
  68 *
  69 * This must be called after ice_get_vf_by_id() once the reference to the VF
  70 * structure is no longer used. Otherwise, the VF structure will never be
  71 * freed.
  72 */
  73void ice_put_vf(struct ice_vf *vf)
  74{
  75	kref_put(&vf->refcnt, ice_release_vf);
  76}
  77
  78/**
  79 * ice_has_vfs - Return true if the PF has any associated VFs
  80 * @pf: the PF private structure
  81 *
  82 * Return whether or not the PF has any allocated VFs.
  83 *
  84 * Note that this function only guarantees that there are no VFs at the point
  85 * of calling it. It does not guarantee that no more VFs will be added.
  86 */
  87bool ice_has_vfs(struct ice_pf *pf)
  88{
  89	/* A simple check that the hash table is not empty does not require
  90	 * the mutex or rcu_read_lock.
  91	 */
  92	return !hash_empty(pf->vfs.table);
  93}
  94
  95/**
  96 * ice_get_num_vfs - Get number of allocated VFs
  97 * @pf: the PF private structure
  98 *
  99 * Return the total number of allocated VFs. NOTE: VF IDs are not guaranteed
 100 * to be contiguous. Do not assume that a VF ID is guaranteed to be less than
 101 * the output of this function.
 102 */
 103u16 ice_get_num_vfs(struct ice_pf *pf)
 104{
 105	struct ice_vf *vf;
 106	unsigned int bkt;
 107	u16 num_vfs = 0;
 108
 109	rcu_read_lock();
 110	ice_for_each_vf_rcu(pf, bkt, vf)
 111		num_vfs++;
 112	rcu_read_unlock();
 113
 114	return num_vfs;
 115}
 116
 117/**
 118 * ice_get_vf_vsi - get VF's VSI based on the stored index
 119 * @vf: VF used to get VSI
 120 */
 121struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
 122{
 123	if (vf->lan_vsi_idx == ICE_NO_VSI)
 124		return NULL;
 125
 126	return vf->pf->vsi[vf->lan_vsi_idx];
 127}
 128
 129/**
 130 * ice_is_vf_disabled
 131 * @vf: pointer to the VF info
 132 *
 133 * If the PF has been disabled, there is no need resetting VF until PF is
 134 * active again. Similarly, if the VF has been disabled, this means something
 135 * else is resetting the VF, so we shouldn't continue.
 136 *
 137 * Returns true if the caller should consider the VF as disabled whether
 138 * because that single VF is explicitly disabled or because the PF is
 139 * currently disabled.
 140 */
 141bool ice_is_vf_disabled(struct ice_vf *vf)
 142{
 143	struct ice_pf *pf = vf->pf;
 144
 145	return (test_bit(ICE_VF_DIS, pf->state) ||
 146		test_bit(ICE_VF_STATE_DIS, vf->vf_states));
 147}
 148
 149/**
 150 * ice_wait_on_vf_reset - poll to make sure a given VF is ready after reset
 151 * @vf: The VF being resseting
 152 *
 153 * The max poll time is about ~800ms, which is about the maximum time it takes
 154 * for a VF to be reset and/or a VF driver to be removed.
 155 */
 156static void ice_wait_on_vf_reset(struct ice_vf *vf)
 157{
 158	int i;
 159
 160	for (i = 0; i < ICE_MAX_VF_RESET_TRIES; i++) {
 161		if (test_bit(ICE_VF_STATE_INIT, vf->vf_states))
 162			break;
 163		msleep(ICE_MAX_VF_RESET_SLEEP_MS);
 164	}
 165}
 166
 167/**
 168 * ice_check_vf_ready_for_cfg - check if VF is ready to be configured/queried
 169 * @vf: VF to check if it's ready to be configured/queried
 170 *
 171 * The purpose of this function is to make sure the VF is not in reset, not
 172 * disabled, and initialized so it can be configured and/or queried by a host
 173 * administrator.
 174 */
 175int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
 176{
 177	ice_wait_on_vf_reset(vf);
 178
 179	if (ice_is_vf_disabled(vf))
 180		return -EINVAL;
 181
 182	if (ice_check_vf_init(vf))
 183		return -EBUSY;
 184
 185	return 0;
 186}
 187
 188/**
 189 * ice_trigger_vf_reset - Reset a VF on HW
 190 * @vf: pointer to the VF structure
 191 * @is_vflr: true if VFLR was issued, false if not
 192 * @is_pfr: true if the reset was triggered due to a previous PFR
 193 *
 194 * Trigger hardware to start a reset for a particular VF. Expects the caller
 195 * to wait the proper amount of time to allow hardware to reset the VF before
 196 * it cleans up and restores VF functionality.
 197 */
 198static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr, bool is_pfr)
 199{
 200	/* Inform VF that it is no longer active, as a warning */
 201	clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
 202
 203	/* Disable VF's configuration API during reset. The flag is re-enabled
 204	 * when it's safe again to access VF's VSI.
 205	 */
 206	clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
 207
 208	/* VF_MBX_ARQLEN and VF_MBX_ATQLEN are cleared by PFR, so the driver
 209	 * needs to clear them in the case of VFR/VFLR. If this is done for
 210	 * PFR, it can mess up VF resets because the VF driver may already
 211	 * have started cleanup by the time we get here.
 212	 */
 213	if (!is_pfr)
 214		vf->vf_ops->clear_mbx_register(vf);
 215
 216	vf->vf_ops->trigger_reset_register(vf, is_vflr);
 217}
 218
 219static void ice_vf_clear_counters(struct ice_vf *vf)
 220{
 221	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 222
 223	if (vsi)
 224		vsi->num_vlan = 0;
 225
 226	vf->num_mac = 0;
 227	memset(&vf->mdd_tx_events, 0, sizeof(vf->mdd_tx_events));
 228	memset(&vf->mdd_rx_events, 0, sizeof(vf->mdd_rx_events));
 229}
 230
 231/**
 232 * ice_vf_pre_vsi_rebuild - tasks to be done prior to VSI rebuild
 233 * @vf: VF to perform pre VSI rebuild tasks
 234 *
 235 * These tasks are items that don't need to be amortized since they are most
 236 * likely called in a for loop with all VF(s) in the reset_all_vfs() case.
 237 */
 238static void ice_vf_pre_vsi_rebuild(struct ice_vf *vf)
 239{
 
 
 
 
 240	ice_vf_clear_counters(vf);
 241	vf->vf_ops->clear_reset_trigger(vf);
 242}
 243
 244/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 245 * ice_vf_rebuild_vsi - rebuild the VF's VSI
 246 * @vf: VF to rebuild the VSI for
 247 *
 248 * This is only called when all VF(s) are being reset (i.e. PCIe Reset on the
 249 * host, PFR, CORER, etc.).
 
 
 250 */
 251static int ice_vf_rebuild_vsi(struct ice_vf *vf)
 252{
 253	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 254	struct ice_pf *pf = vf->pf;
 255
 256	if (WARN_ON(!vsi))
 257		return -EINVAL;
 258
 259	if (ice_vsi_rebuild(vsi, true)) {
 260		dev_err(ice_pf_to_dev(pf), "failed to rebuild VF %d VSI\n",
 261			vf->vf_id);
 262		return -EIO;
 263	}
 264	/* vsi->idx will remain the same in this case so don't update
 265	 * vf->lan_vsi_idx
 266	 */
 267	vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
 268	vf->lan_vsi_num = vsi->vsi_num;
 269
 270	return 0;
 271}
 272
 273/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 274 * ice_is_any_vf_in_unicast_promisc - check if any VF(s)
 275 * are in unicast promiscuous mode
 276 * @pf: PF structure for accessing VF(s)
 277 *
 278 * Return false if no VF(s) are in unicast promiscuous mode,
 279 * else return true
 280 */
 281bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf)
 282{
 283	bool is_vf_promisc = false;
 284	struct ice_vf *vf;
 285	unsigned int bkt;
 286
 287	rcu_read_lock();
 288	ice_for_each_vf_rcu(pf, bkt, vf) {
 289		/* found a VF that has promiscuous mode configured */
 290		if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states)) {
 291			is_vf_promisc = true;
 292			break;
 293		}
 294	}
 295	rcu_read_unlock();
 296
 297	return is_vf_promisc;
 298}
 299
 300/**
 301 * ice_vf_get_promisc_masks - Calculate masks for promiscuous modes
 302 * @vf: the VF pointer
 303 * @vsi: the VSI to configure
 304 * @ucast_m: promiscuous mask to apply to unicast
 305 * @mcast_m: promiscuous mask to apply to multicast
 306 *
 307 * Decide which mask should be used for unicast and multicast filter,
 308 * based on presence of VLANs
 309 */
 310void
 311ice_vf_get_promisc_masks(struct ice_vf *vf, struct ice_vsi *vsi,
 312			 u8 *ucast_m, u8 *mcast_m)
 313{
 314	if (ice_vf_is_port_vlan_ena(vf) ||
 315	    ice_vsi_has_non_zero_vlans(vsi)) {
 316		*mcast_m = ICE_MCAST_VLAN_PROMISC_BITS;
 317		*ucast_m = ICE_UCAST_VLAN_PROMISC_BITS;
 318	} else {
 319		*mcast_m = ICE_MCAST_PROMISC_BITS;
 320		*ucast_m = ICE_UCAST_PROMISC_BITS;
 321	}
 322}
 323
 324/**
 325 * ice_vf_clear_all_promisc_modes - Clear promisc/allmulticast on VF VSI
 326 * @vf: the VF pointer
 327 * @vsi: the VSI to configure
 328 *
 329 * Clear all promiscuous/allmulticast filters for a VF
 330 */
 331static int
 332ice_vf_clear_all_promisc_modes(struct ice_vf *vf, struct ice_vsi *vsi)
 333{
 334	struct ice_pf *pf = vf->pf;
 335	u8 ucast_m, mcast_m;
 336	int ret = 0;
 337
 338	ice_vf_get_promisc_masks(vf, vsi, &ucast_m, &mcast_m);
 339	if (test_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states)) {
 340		if (!test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags)) {
 341			if (ice_is_dflt_vsi_in_use(vsi->port_info))
 342				ret = ice_clear_dflt_vsi(vsi);
 343		} else {
 344			ret = ice_vf_clear_vsi_promisc(vf, vsi, ucast_m);
 345		}
 346
 347		if (ret) {
 348			dev_err(ice_pf_to_dev(vf->pf), "Disabling promiscuous mode failed\n");
 349		} else {
 350			clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
 351			dev_info(ice_pf_to_dev(vf->pf), "Disabling promiscuous mode succeeded\n");
 352		}
 353	}
 354
 355	if (test_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states)) {
 356		ret = ice_vf_clear_vsi_promisc(vf, vsi, mcast_m);
 357		if (ret) {
 358			dev_err(ice_pf_to_dev(vf->pf), "Disabling allmulticast mode failed\n");
 359		} else {
 360			clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
 361			dev_info(ice_pf_to_dev(vf->pf), "Disabling allmulticast mode succeeded\n");
 362		}
 363	}
 364	return ret;
 365}
 366
 367/**
 368 * ice_vf_set_vsi_promisc - Enable promiscuous mode for a VF VSI
 369 * @vf: the VF to configure
 370 * @vsi: the VF's VSI
 371 * @promisc_m: the promiscuous mode to enable
 372 */
 373int
 374ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
 375{
 376	struct ice_hw *hw = &vsi->back->hw;
 377	int status;
 378
 379	if (ice_vf_is_port_vlan_ena(vf))
 380		status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m,
 381						  ice_vf_get_port_vlan_id(vf));
 382	else if (ice_vsi_has_non_zero_vlans(vsi))
 383		status = ice_fltr_set_vlan_vsi_promisc(hw, vsi, promisc_m);
 384	else
 385		status = ice_fltr_set_vsi_promisc(hw, vsi->idx, promisc_m, 0);
 386
 387	if (status && status != -EEXIST) {
 388		dev_err(ice_pf_to_dev(vsi->back), "enable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
 389			vf->vf_id, status);
 390		return status;
 391	}
 392
 393	return 0;
 394}
 395
 396/**
 397 * ice_vf_clear_vsi_promisc - Disable promiscuous mode for a VF VSI
 398 * @vf: the VF to configure
 399 * @vsi: the VF's VSI
 400 * @promisc_m: the promiscuous mode to disable
 401 */
 402int
 403ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
 404{
 405	struct ice_hw *hw = &vsi->back->hw;
 406	int status;
 407
 408	if (ice_vf_is_port_vlan_ena(vf))
 409		status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m,
 410						    ice_vf_get_port_vlan_id(vf));
 411	else if (ice_vsi_has_non_zero_vlans(vsi))
 412		status = ice_fltr_clear_vlan_vsi_promisc(hw, vsi, promisc_m);
 413	else
 414		status = ice_fltr_clear_vsi_promisc(hw, vsi->idx, promisc_m, 0);
 415
 416	if (status && status != -ENOENT) {
 417		dev_err(ice_pf_to_dev(vsi->back), "disable Tx/Rx filter promiscuous mode on VF-%u failed, error: %d\n",
 418			vf->vf_id, status);
 419		return status;
 420	}
 421
 422	return 0;
 423}
 424
 425/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 426 * ice_reset_all_vfs - reset all allocated VFs in one go
 427 * @pf: pointer to the PF structure
 428 *
 429 * Reset all VFs at once, in response to a PF or other device reset.
 430 *
 431 * First, tell the hardware to reset each VF, then do all the waiting in one
 432 * chunk, and finally finish restoring each VF after the wait. This is useful
 433 * during PF routines which need to reset all VFs, as otherwise it must perform
 434 * these resets in a serialized fashion.
 435 */
 436void ice_reset_all_vfs(struct ice_pf *pf)
 437{
 438	struct device *dev = ice_pf_to_dev(pf);
 439	struct ice_hw *hw = &pf->hw;
 440	struct ice_vf *vf;
 441	unsigned int bkt;
 442
 443	/* If we don't have any VFs, then there is nothing to reset */
 444	if (!ice_has_vfs(pf))
 445		return;
 446
 447	mutex_lock(&pf->vfs.table_lock);
 448
 449	/* clear all malicious info if the VFs are getting reset */
 450	ice_for_each_vf(pf, bkt, vf)
 451		if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->vfs.malvfs,
 452					ICE_MAX_SRIOV_VFS, vf->vf_id))
 453			dev_dbg(dev, "failed to clear malicious VF state for VF %u\n",
 454				vf->vf_id);
 455
 456	/* If VFs have been disabled, there is no need to reset */
 457	if (test_and_set_bit(ICE_VF_DIS, pf->state)) {
 458		mutex_unlock(&pf->vfs.table_lock);
 459		return;
 460	}
 461
 462	/* Begin reset on all VFs at once */
 463	ice_for_each_vf(pf, bkt, vf)
 464		ice_trigger_vf_reset(vf, true, true);
 465
 466	/* HW requires some time to make sure it can flush the FIFO for a VF
 467	 * when it resets it. Now that we've triggered all of the VFs, iterate
 468	 * the table again and wait for each VF to complete.
 469	 */
 470	ice_for_each_vf(pf, bkt, vf) {
 471		if (!vf->vf_ops->poll_reset_status(vf)) {
 472			/* Display a warning if at least one VF didn't manage
 473			 * to reset in time, but continue on with the
 474			 * operation.
 475			 */
 476			dev_warn(dev, "VF %u reset check timeout\n", vf->vf_id);
 477			break;
 478		}
 479	}
 480
 481	/* free VF resources to begin resetting the VSI state */
 482	ice_for_each_vf(pf, bkt, vf) {
 483		mutex_lock(&vf->cfg_lock);
 484
 
 485		vf->driver_caps = 0;
 486		ice_vc_set_default_allowlist(vf);
 487
 488		ice_vf_fdir_exit(vf);
 489		ice_vf_fdir_init(vf);
 490		/* clean VF control VSI when resetting VFs since it should be
 491		 * setup only when VF creates its first FDIR rule.
 492		 */
 493		if (vf->ctrl_vsi_idx != ICE_NO_VSI)
 494			ice_vf_ctrl_invalidate_vsi(vf);
 495
 496		ice_vf_pre_vsi_rebuild(vf);
 497		ice_vf_rebuild_vsi(vf);
 498		vf->vf_ops->post_vsi_rebuild(vf);
 
 
 499
 500		mutex_unlock(&vf->cfg_lock);
 501	}
 502
 503	if (ice_is_eswitch_mode_switchdev(pf))
 504		if (ice_eswitch_rebuild(pf))
 505			dev_warn(dev, "eswitch rebuild failed\n");
 506
 507	ice_flush(hw);
 508	clear_bit(ICE_VF_DIS, pf->state);
 509
 510	mutex_unlock(&pf->vfs.table_lock);
 511}
 512
 513/**
 514 * ice_notify_vf_reset - Notify VF of a reset event
 515 * @vf: pointer to the VF structure
 516 */
 517static void ice_notify_vf_reset(struct ice_vf *vf)
 518{
 519	struct ice_hw *hw = &vf->pf->hw;
 520	struct virtchnl_pf_event pfe;
 521
 522	/* Bail out if VF is in disabled state, neither initialized, nor active
 523	 * state - otherwise proceed with notifications
 524	 */
 525	if ((!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
 526	     !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) ||
 527	    test_bit(ICE_VF_STATE_DIS, vf->vf_states))
 528		return;
 529
 530	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
 531	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
 532	ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT,
 533			      VIRTCHNL_STATUS_SUCCESS, (u8 *)&pfe, sizeof(pfe),
 534			      NULL);
 535}
 536
 537/**
 538 * ice_reset_vf - Reset a particular VF
 539 * @vf: pointer to the VF structure
 540 * @flags: flags controlling behavior of the reset
 541 *
 542 * Flags:
 543 *   ICE_VF_RESET_VFLR - Indicates a reset is due to VFLR event
 544 *   ICE_VF_RESET_NOTIFY - Send VF a notification prior to reset
 545 *   ICE_VF_RESET_LOCK - Acquire VF cfg_lock before resetting
 546 *
 547 * Returns 0 if the VF is currently in reset, if resets are disabled, or if
 548 * the VF resets successfully. Returns an error code if the VF fails to
 549 * rebuild.
 550 */
 551int ice_reset_vf(struct ice_vf *vf, u32 flags)
 552{
 553	struct ice_pf *pf = vf->pf;
 
 554	struct ice_vsi *vsi;
 
 555	struct device *dev;
 556	struct ice_hw *hw;
 557	int err = 0;
 558	bool rsd;
 559
 560	dev = ice_pf_to_dev(pf);
 561	hw = &pf->hw;
 
 562
 563	if (flags & ICE_VF_RESET_NOTIFY)
 564		ice_notify_vf_reset(vf);
 565
 566	if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
 567		dev_dbg(dev, "Trying to reset VF %d, but all VF resets are disabled\n",
 568			vf->vf_id);
 569		return 0;
 570	}
 571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 572	if (ice_is_vf_disabled(vf)) {
 573		vsi = ice_get_vf_vsi(vf);
 574		if (!vsi) {
 575			dev_dbg(dev, "VF is already removed\n");
 576			return -EINVAL;
 
 577		}
 578		ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
 579
 580		if (ice_vsi_is_rx_queue_active(vsi))
 581			ice_vsi_stop_all_rx_rings(vsi);
 582
 583		dev_dbg(dev, "VF is already disabled, there is no need for resetting it, telling VM, all is fine %d\n",
 584			vf->vf_id);
 585		return 0;
 586	}
 587
 588	if (flags & ICE_VF_RESET_LOCK)
 589		mutex_lock(&vf->cfg_lock);
 590	else
 591		lockdep_assert_held(&vf->cfg_lock);
 592
 593	/* Set VF disable bit state here, before triggering reset */
 594	set_bit(ICE_VF_STATE_DIS, vf->vf_states);
 595	ice_trigger_vf_reset(vf, flags & ICE_VF_RESET_VFLR, false);
 596
 597	vsi = ice_get_vf_vsi(vf);
 598	if (WARN_ON(!vsi)) {
 599		err = -EIO;
 600		goto out_unlock;
 601	}
 602
 603	ice_dis_vf_qs(vf);
 604
 605	/* Call Disable LAN Tx queue AQ whether or not queues are
 606	 * enabled. This is needed for successful completion of VFR.
 607	 */
 608	ice_dis_vsi_txq(vsi->port_info, vsi->idx, 0, 0, NULL, NULL,
 609			NULL, vf->vf_ops->reset_type, vf->vf_id, NULL);
 610
 611	/* poll VPGEN_VFRSTAT reg to make sure
 612	 * that reset is complete
 613	 */
 614	rsd = vf->vf_ops->poll_reset_status(vf);
 615
 616	/* Display a warning if VF didn't manage to reset in time, but need to
 617	 * continue on with the operation.
 618	 */
 619	if (!rsd)
 620		dev_warn(dev, "VF reset check timeout on VF %d\n", vf->vf_id);
 621
 622	vf->driver_caps = 0;
 623	ice_vc_set_default_allowlist(vf);
 624
 625	/* disable promiscuous modes in case they were enabled
 626	 * ignore any error if disabling process failed
 627	 */
 628	ice_vf_clear_all_promisc_modes(vf, vsi);
 629
 630	ice_eswitch_del_vf_mac_rule(vf);
 631
 632	ice_vf_fdir_exit(vf);
 633	ice_vf_fdir_init(vf);
 634	/* clean VF control VSI when resetting VF since it should be setup
 635	 * only when VF creates its first FDIR rule.
 636	 */
 637	if (vf->ctrl_vsi_idx != ICE_NO_VSI)
 638		ice_vf_ctrl_vsi_release(vf);
 639
 640	ice_vf_pre_vsi_rebuild(vf);
 641
 642	if (vf->vf_ops->vsi_rebuild(vf)) {
 643		dev_err(dev, "Failed to release and setup the VF%u's VSI\n",
 644			vf->vf_id);
 645		err = -EFAULT;
 646		goto out_unlock;
 647	}
 648
 649	vf->vf_ops->post_vsi_rebuild(vf);
 650	vsi = ice_get_vf_vsi(vf);
 651	if (WARN_ON(!vsi)) {
 652		err = -EINVAL;
 653		goto out_unlock;
 654	}
 655
 656	ice_eswitch_update_repr(vsi);
 657	ice_eswitch_replay_vf_mac_rule(vf);
 658
 659	/* if the VF has been reset allow it to come up again */
 660	if (ice_mbx_clear_malvf(&hw->mbx_snapshot, pf->vfs.malvfs,
 661				ICE_MAX_SRIOV_VFS, vf->vf_id))
 662		dev_dbg(dev, "failed to clear malicious VF state for VF %u\n",
 663			vf->vf_id);
 664
 665out_unlock:
 
 
 
 
 
 666	if (flags & ICE_VF_RESET_LOCK)
 667		mutex_unlock(&vf->cfg_lock);
 668
 669	return err;
 670}
 671
 672/**
 673 * ice_set_vf_state_qs_dis - Set VF queues state to disabled
 674 * @vf: pointer to the VF structure
 675 */
 676void ice_set_vf_state_qs_dis(struct ice_vf *vf)
 677{
 678	/* Clear Rx/Tx enabled queues flag */
 679	bitmap_zero(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF);
 680	bitmap_zero(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF);
 681	clear_bit(ICE_VF_STATE_QS_ENA, vf->vf_states);
 682}
 683
 684/* Private functions only accessed from other virtualization files */
 685
 686/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 687 * ice_dis_vf_qs - Disable the VF queues
 688 * @vf: pointer to the VF structure
 689 */
 690void ice_dis_vf_qs(struct ice_vf *vf)
 691{
 692	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 693
 694	if (WARN_ON(!vsi))
 695		return;
 696
 697	ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, vf->vf_id);
 698	ice_vsi_stop_all_rx_rings(vsi);
 699	ice_set_vf_state_qs_dis(vf);
 700}
 701
 702/**
 703 * ice_err_to_virt_err - translate errors for VF return code
 704 * @err: error return code
 705 */
 706enum virtchnl_status_code ice_err_to_virt_err(int err)
 707{
 708	switch (err) {
 709	case 0:
 710		return VIRTCHNL_STATUS_SUCCESS;
 711	case -EINVAL:
 712	case -ENODEV:
 713		return VIRTCHNL_STATUS_ERR_PARAM;
 714	case -ENOMEM:
 715		return VIRTCHNL_STATUS_ERR_NO_MEMORY;
 716	case -EALREADY:
 717	case -EBUSY:
 718	case -EIO:
 719	case -ENOSPC:
 720		return VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR;
 721	default:
 722		return VIRTCHNL_STATUS_ERR_NOT_SUPPORTED;
 723	}
 724}
 725
 726/**
 727 * ice_check_vf_init - helper to check if VF init complete
 728 * @vf: the pointer to the VF to check
 729 */
 730int ice_check_vf_init(struct ice_vf *vf)
 731{
 732	struct ice_pf *pf = vf->pf;
 733
 734	if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
 735		dev_err(ice_pf_to_dev(pf), "VF ID: %u in reset. Try again.\n",
 736			vf->vf_id);
 737		return -EBUSY;
 738	}
 739	return 0;
 740}
 741
 742/**
 743 * ice_vf_get_port_info - Get the VF's port info structure
 744 * @vf: VF used to get the port info structure for
 745 */
 746struct ice_port_info *ice_vf_get_port_info(struct ice_vf *vf)
 747{
 748	return vf->pf->hw.port_info;
 749}
 750
 751/**
 752 * ice_cfg_mac_antispoof - Configure MAC antispoof checking behavior
 753 * @vsi: the VSI to configure
 754 * @enable: whether to enable or disable the spoof checking
 755 *
 756 * Configure a VSI to enable (or disable) spoof checking behavior.
 757 */
 758static int ice_cfg_mac_antispoof(struct ice_vsi *vsi, bool enable)
 759{
 760	struct ice_vsi_ctx *ctx;
 761	int err;
 762
 763	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 764	if (!ctx)
 765		return -ENOMEM;
 766
 767	ctx->info.sec_flags = vsi->info.sec_flags;
 768	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
 769
 770	if (enable)
 771		ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
 772	else
 773		ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
 774
 775	err = ice_update_vsi(&vsi->back->hw, vsi->idx, ctx, NULL);
 776	if (err)
 777		dev_err(ice_pf_to_dev(vsi->back), "Failed to configure Tx MAC anti-spoof %s for VSI %d, error %d\n",
 778			enable ? "ON" : "OFF", vsi->vsi_num, err);
 779	else
 780		vsi->info.sec_flags = ctx->info.sec_flags;
 781
 782	kfree(ctx);
 783
 784	return err;
 785}
 786
 787/**
 788 * ice_vsi_ena_spoofchk - enable Tx spoof checking for this VSI
 789 * @vsi: VSI to enable Tx spoof checking for
 790 */
 791static int ice_vsi_ena_spoofchk(struct ice_vsi *vsi)
 792{
 793	struct ice_vsi_vlan_ops *vlan_ops;
 794	int err = 0;
 795
 796	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
 797
 798	/* Allow VF with VLAN 0 only to send all tagged traffic */
 799	if (vsi->type != ICE_VSI_VF || ice_vsi_has_non_zero_vlans(vsi)) {
 800		err = vlan_ops->ena_tx_filtering(vsi);
 801		if (err)
 802			return err;
 803	}
 804
 805	return ice_cfg_mac_antispoof(vsi, true);
 806}
 807
 808/**
 809 * ice_vsi_dis_spoofchk - disable Tx spoof checking for this VSI
 810 * @vsi: VSI to disable Tx spoof checking for
 811 */
 812static int ice_vsi_dis_spoofchk(struct ice_vsi *vsi)
 813{
 814	struct ice_vsi_vlan_ops *vlan_ops;
 815	int err;
 816
 817	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
 818
 819	err = vlan_ops->dis_tx_filtering(vsi);
 820	if (err)
 821		return err;
 822
 823	return ice_cfg_mac_antispoof(vsi, false);
 824}
 825
 826/**
 827 * ice_vsi_apply_spoofchk - Apply Tx spoof checking setting to a VSI
 828 * @vsi: VSI associated to the VF
 829 * @enable: whether to enable or disable the spoof checking
 830 */
 831int ice_vsi_apply_spoofchk(struct ice_vsi *vsi, bool enable)
 832{
 833	int err;
 834
 835	if (enable)
 836		err = ice_vsi_ena_spoofchk(vsi);
 837	else
 838		err = ice_vsi_dis_spoofchk(vsi);
 839
 840	return err;
 841}
 842
 843/**
 844 * ice_is_vf_trusted
 845 * @vf: pointer to the VF info
 846 */
 847bool ice_is_vf_trusted(struct ice_vf *vf)
 848{
 849	return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
 850}
 851
 852/**
 853 * ice_vf_has_no_qs_ena - check if the VF has any Rx or Tx queues enabled
 854 * @vf: the VF to check
 855 *
 856 * Returns true if the VF has no Rx and no Tx queues enabled and returns false
 857 * otherwise
 858 */
 859bool ice_vf_has_no_qs_ena(struct ice_vf *vf)
 860{
 861	return (!bitmap_weight(vf->rxq_ena, ICE_MAX_RSS_QS_PER_VF) &&
 862		!bitmap_weight(vf->txq_ena, ICE_MAX_RSS_QS_PER_VF));
 863}
 864
 865/**
 866 * ice_is_vf_link_up - check if the VF's link is up
 867 * @vf: VF to check if link is up
 868 */
 869bool ice_is_vf_link_up(struct ice_vf *vf)
 870{
 871	struct ice_port_info *pi = ice_vf_get_port_info(vf);
 872
 873	if (ice_check_vf_init(vf))
 874		return false;
 875
 876	if (ice_vf_has_no_qs_ena(vf))
 877		return false;
 878	else if (vf->link_forced)
 879		return vf->link_up;
 880	else
 881		return pi->phy.link_info.link_info &
 882			ICE_AQ_LINK_UP;
 883}
 884
 885/**
 886 * ice_vf_set_host_trust_cfg - set trust setting based on pre-reset value
 887 * @vf: VF to configure trust setting for
 
 
 
 
 
 
 
 
 
 888 */
 889static void ice_vf_set_host_trust_cfg(struct ice_vf *vf)
 890{
 891	if (vf->trusted)
 892		set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
 893	else
 894		clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
 895}
 896
 897/**
 898 * ice_vf_rebuild_host_mac_cfg - add broadcast and the VF's perm_addr/LAA
 899 * @vf: VF to add MAC filters for
 900 *
 901 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
 902 * always re-adds a broadcast filter and the VF's perm_addr/LAA after reset.
 903 */
 904static int ice_vf_rebuild_host_mac_cfg(struct ice_vf *vf)
 905{
 906	struct device *dev = ice_pf_to_dev(vf->pf);
 907	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 908	u8 broadcast[ETH_ALEN];
 909	int status;
 910
 911	if (WARN_ON(!vsi))
 912		return -EINVAL;
 
 
 913
 914	if (ice_is_eswitch_mode_switchdev(vf->pf))
 915		return 0;
 916
 917	eth_broadcast_addr(broadcast);
 918	status = ice_fltr_add_mac(vsi, broadcast, ICE_FWD_TO_VSI);
 919	if (status) {
 920		dev_err(dev, "failed to add broadcast MAC filter for VF %u, error %d\n",
 921			vf->vf_id, status);
 922		return status;
 923	}
 924
 925	vf->num_mac++;
 926
 927	if (is_valid_ether_addr(vf->hw_lan_addr.addr)) {
 928		status = ice_fltr_add_mac(vsi, vf->hw_lan_addr.addr,
 929					  ICE_FWD_TO_VSI);
 930		if (status) {
 931			dev_err(dev, "failed to add default unicast MAC filter %pM for VF %u, error %d\n",
 932				&vf->hw_lan_addr.addr[0], vf->vf_id,
 933				status);
 934			return status;
 935		}
 936		vf->num_mac++;
 937
 938		ether_addr_copy(vf->dev_lan_addr.addr, vf->hw_lan_addr.addr);
 939	}
 940
 941	return 0;
 942}
 943
 944/**
 945 * ice_vf_rebuild_host_vlan_cfg - add VLAN 0 filter or rebuild the Port VLAN
 946 * @vf: VF to add MAC filters for
 947 * @vsi: Pointer to VSI
 948 *
 949 * Called after a VF VSI has been re-added/rebuilt during reset. The PF driver
 950 * always re-adds either a VLAN 0 or port VLAN based filter after reset.
 
 951 */
 952static int ice_vf_rebuild_host_vlan_cfg(struct ice_vf *vf, struct ice_vsi *vsi)
 953{
 954	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
 955	struct device *dev = ice_pf_to_dev(vf->pf);
 
 
 956	int err;
 957
 958	if (ice_vf_is_port_vlan_ena(vf)) {
 959		err = vlan_ops->set_port_vlan(vsi, &vf->port_vlan_info);
 960		if (err) {
 961			dev_err(dev, "failed to configure port VLAN via VSI parameters for VF %u, error %d\n",
 962				vf->vf_id, err);
 963			return err;
 964		}
 965
 966		err = vlan_ops->add_vlan(vsi, &vf->port_vlan_info);
 967	} else {
 968		err = ice_vsi_add_vlan_zero(vsi);
 
 
 969	}
 970
 
 
 971	if (err) {
 972		dev_err(dev, "failed to add VLAN %u filter for VF %u during VF rebuild, error %d\n",
 973			ice_vf_is_port_vlan_ena(vf) ?
 974			ice_vf_get_port_vlan_id(vf) : 0, vf->vf_id, err);
 975		return err;
 976	}
 977
 978	err = vlan_ops->ena_rx_filtering(vsi);
 979	if (err)
 980		dev_warn(dev, "failed to enable Rx VLAN filtering for VF %d VSI %d during VF rebuild, error %d\n",
 981			 vf->vf_id, vsi->idx, err);
 
 
 
 982
 983	return 0;
 984}
 985
 986/**
 987 * ice_vf_rebuild_host_tx_rate_cfg - re-apply the Tx rate limiting configuration
 988 * @vf: VF to re-apply the configuration for
 989 *
 990 * Called after a VF VSI has been re-added/rebuild during reset. The PF driver
 991 * needs to re-apply the host configured Tx rate limiting configuration.
 992 */
 993static int ice_vf_rebuild_host_tx_rate_cfg(struct ice_vf *vf)
 994{
 995	struct device *dev = ice_pf_to_dev(vf->pf);
 996	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
 997	int err;
 998
 999	if (WARN_ON(!vsi))
1000		return -EINVAL;
1001
1002	if (vf->min_tx_rate) {
1003		err = ice_set_min_bw_limit(vsi, (u64)vf->min_tx_rate * 1000);
1004		if (err) {
1005			dev_err(dev, "failed to set min Tx rate to %d Mbps for VF %u, error %d\n",
1006				vf->min_tx_rate, vf->vf_id, err);
1007			return err;
1008		}
1009	}
1010
1011	if (vf->max_tx_rate) {
1012		err = ice_set_max_bw_limit(vsi, (u64)vf->max_tx_rate * 1000);
1013		if (err) {
1014			dev_err(dev, "failed to set max Tx rate to %d Mbps for VF %u, error %d\n",
1015				vf->max_tx_rate, vf->vf_id, err);
1016			return err;
1017		}
1018	}
1019
1020	return 0;
1021}
1022
1023/**
1024 * ice_vf_rebuild_aggregator_node_cfg - rebuild aggregator node config
1025 * @vsi: Pointer to VSI
1026 *
1027 * This function moves VSI into corresponding scheduler aggregator node
1028 * based on cached value of "aggregator node info" per VSI
1029 */
1030static void ice_vf_rebuild_aggregator_node_cfg(struct ice_vsi *vsi)
1031{
1032	struct ice_pf *pf = vsi->back;
1033	struct device *dev;
1034	int status;
1035
1036	if (!vsi->agg_node)
1037		return;
1038
1039	dev = ice_pf_to_dev(pf);
1040	if (vsi->agg_node->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
1041		dev_dbg(dev,
1042			"agg_id %u already has reached max_num_vsis %u\n",
1043			vsi->agg_node->agg_id, vsi->agg_node->num_vsis);
1044		return;
1045	}
1046
1047	status = ice_move_vsi_to_agg(pf->hw.port_info, vsi->agg_node->agg_id,
1048				     vsi->idx, vsi->tc_cfg.ena_tc);
1049	if (status)
1050		dev_dbg(dev, "unable to move VSI idx %u into aggregator %u node",
1051			vsi->idx, vsi->agg_node->agg_id);
1052	else
1053		vsi->agg_node->num_vsis++;
1054}
1055
1056/**
1057 * ice_vf_rebuild_host_cfg - host admin configuration is persistent across reset
1058 * @vf: VF to rebuild host configuration on
 
 
 
1059 */
1060void ice_vf_rebuild_host_cfg(struct ice_vf *vf)
1061{
1062	struct device *dev = ice_pf_to_dev(vf->pf);
1063	struct ice_vsi *vsi = ice_get_vf_vsi(vf);
1064
1065	if (WARN_ON(!vsi))
1066		return;
1067
1068	ice_vf_set_host_trust_cfg(vf);
1069
1070	if (ice_vf_rebuild_host_mac_cfg(vf))
1071		dev_err(dev, "failed to rebuild default MAC configuration for VF %d\n",
1072			vf->vf_id);
1073
1074	if (ice_vf_rebuild_host_vlan_cfg(vf, vsi))
1075		dev_err(dev, "failed to rebuild VLAN configuration for VF %u\n",
1076			vf->vf_id);
1077
1078	if (ice_vf_rebuild_host_tx_rate_cfg(vf))
1079		dev_err(dev, "failed to rebuild Tx rate limiting configuration for VF %u\n",
1080			vf->vf_id);
1081
1082	if (ice_vsi_apply_spoofchk(vsi, vf->spoofchk))
1083		dev_err(dev, "failed to rebuild spoofchk configuration for VF %d\n",
1084			vf->vf_id);
1085
1086	/* rebuild aggregator node config for main VF VSI */
1087	ice_vf_rebuild_aggregator_node_cfg(vsi);
1088}
1089
1090/**
1091 * ice_vf_ctrl_invalidate_vsi - invalidate ctrl_vsi_idx to remove VSI access
1092 * @vf: VF that control VSI is being invalidated on
1093 */
1094void ice_vf_ctrl_invalidate_vsi(struct ice_vf *vf)
1095{
1096	vf->ctrl_vsi_idx = ICE_NO_VSI;
1097}
1098
1099/**
1100 * ice_vf_ctrl_vsi_release - invalidate the VF's control VSI after freeing it
1101 * @vf: VF that control VSI is being released on
1102 */
1103void ice_vf_ctrl_vsi_release(struct ice_vf *vf)
1104{
1105	ice_vsi_release(vf->pf->vsi[vf->ctrl_vsi_idx]);
1106	ice_vf_ctrl_invalidate_vsi(vf);
1107}
1108
1109/**
1110 * ice_vf_ctrl_vsi_setup - Set up a VF control VSI
1111 * @vf: VF to setup control VSI for
1112 *
1113 * Returns pointer to the successfully allocated VSI struct on success,
1114 * otherwise returns NULL on failure.
1115 */
1116struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf)
1117{
1118	struct ice_port_info *pi = ice_vf_get_port_info(vf);
1119	struct ice_pf *pf = vf->pf;
1120	struct ice_vsi *vsi;
1121
1122	vsi = ice_vsi_setup(pf, pi, ICE_VSI_CTRL, vf, NULL);
1123	if (!vsi) {
1124		dev_err(ice_pf_to_dev(pf), "Failed to create VF control VSI\n");
1125		ice_vf_ctrl_invalidate_vsi(vf);
 
 
1126	}
1127
1128	return vsi;
1129}
1130
1131/**
1132 * ice_vf_invalidate_vsi - invalidate vsi_idx/vsi_num to remove VSI access
1133 * @vf: VF to remove access to VSI for
1134 */
1135void ice_vf_invalidate_vsi(struct ice_vf *vf)
1136{
1137	vf->lan_vsi_idx = ICE_NO_VSI;
1138	vf->lan_vsi_num = ICE_NO_VSI;
1139}
1140
1141/**
1142 * ice_vf_set_initialized - VF is ready for VIRTCHNL communication
1143 * @vf: VF to set in initialized state
1144 *
1145 * After this function the VF will be ready to receive/handle the
1146 * VIRTCHNL_OP_GET_VF_RESOURCES message
1147 */
1148void ice_vf_set_initialized(struct ice_vf *vf)
1149{
1150	ice_set_vf_state_qs_dis(vf);
1151	clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
1152	clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
1153	clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
1154	set_bit(ICE_VF_STATE_INIT, vf->vf_states);
1155	memset(&vf->vlan_v2_caps, 0, sizeof(vf->vlan_v2_caps));
1156}