Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2018, Intel Corporation. */
   3
   4#include "ice.h"
   5#include "ice_base.h"
   6#include "ice_flow.h"
   7#include "ice_lib.h"
   8#include "ice_fltr.h"
   9#include "ice_dcb_lib.h"
  10#include "ice_type.h"
  11#include "ice_vsi_vlan_ops.h"
  12
  13/**
  14 * ice_vsi_type_str - maps VSI type enum to string equivalents
  15 * @vsi_type: VSI type enum
  16 */
  17const char *ice_vsi_type_str(enum ice_vsi_type vsi_type)
  18{
  19	switch (vsi_type) {
  20	case ICE_VSI_PF:
  21		return "ICE_VSI_PF";
  22	case ICE_VSI_VF:
  23		return "ICE_VSI_VF";
  24	case ICE_VSI_SF:
  25		return "ICE_VSI_SF";
  26	case ICE_VSI_CTRL:
  27		return "ICE_VSI_CTRL";
  28	case ICE_VSI_CHNL:
  29		return "ICE_VSI_CHNL";
  30	case ICE_VSI_LB:
  31		return "ICE_VSI_LB";
  32	default:
  33		return "unknown";
  34	}
  35}
  36
  37/**
  38 * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
  39 * @vsi: the VSI being configured
  40 * @ena: start or stop the Rx rings
  41 *
  42 * First enable/disable all of the Rx rings, flush any remaining writes, and
  43 * then verify that they have all been enabled/disabled successfully. This will
  44 * let all of the register writes complete when enabling/disabling the Rx rings
  45 * before waiting for the change in hardware to complete.
  46 */
  47static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
  48{
  49	int ret = 0;
  50	u16 i;
  51
  52	ice_for_each_rxq(vsi, i)
  53		ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);
  54
  55	ice_flush(&vsi->back->hw);
  56
  57	ice_for_each_rxq(vsi, i) {
  58		ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
  59		if (ret)
  60			break;
  61	}
  62
  63	return ret;
  64}
  65
  66/**
  67 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
  68 * @vsi: VSI pointer
  69 *
  70 * On error: returns error code (negative)
  71 * On success: returns 0
  72 */
  73static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
  74{
  75	struct ice_pf *pf = vsi->back;
  76	struct device *dev;
  77
  78	dev = ice_pf_to_dev(pf);
  79	if (vsi->type == ICE_VSI_CHNL)
  80		return 0;
  81
  82	/* allocate memory for both Tx and Rx ring pointers */
  83	vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
  84				     sizeof(*vsi->tx_rings), GFP_KERNEL);
  85	if (!vsi->tx_rings)
  86		return -ENOMEM;
  87
  88	vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
  89				     sizeof(*vsi->rx_rings), GFP_KERNEL);
  90	if (!vsi->rx_rings)
  91		goto err_rings;
  92
  93	/* txq_map needs to have enough space to track both Tx (stack) rings
  94	 * and XDP rings; at this point vsi->num_xdp_txq might not be set,
  95	 * so use num_possible_cpus() as we want to always provide XDP ring
  96	 * per CPU, regardless of queue count settings from user that might
  97	 * have come from ethtool's set_channels() callback;
  98	 */
  99	vsi->txq_map = devm_kcalloc(dev, (vsi->alloc_txq + num_possible_cpus()),
 100				    sizeof(*vsi->txq_map), GFP_KERNEL);
 101
 102	if (!vsi->txq_map)
 103		goto err_txq_map;
 104
 105	vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
 106				    sizeof(*vsi->rxq_map), GFP_KERNEL);
 107	if (!vsi->rxq_map)
 108		goto err_rxq_map;
 109
 110	/* There is no need to allocate q_vectors for a loopback VSI. */
 111	if (vsi->type == ICE_VSI_LB)
 112		return 0;
 113
 114	/* allocate memory for q_vector pointers */
 115	vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
 116				      sizeof(*vsi->q_vectors), GFP_KERNEL);
 117	if (!vsi->q_vectors)
 118		goto err_vectors;
 119
 
 
 
 
 120	return 0;
 121
 
 
 122err_vectors:
 123	devm_kfree(dev, vsi->rxq_map);
 124err_rxq_map:
 125	devm_kfree(dev, vsi->txq_map);
 126err_txq_map:
 127	devm_kfree(dev, vsi->rx_rings);
 128err_rings:
 129	devm_kfree(dev, vsi->tx_rings);
 130	return -ENOMEM;
 131}
 132
 133/**
 134 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
 135 * @vsi: the VSI being configured
 136 */
 137static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
 138{
 139	switch (vsi->type) {
 140	case ICE_VSI_PF:
 141	case ICE_VSI_SF:
 142	case ICE_VSI_CTRL:
 143	case ICE_VSI_LB:
 144		/* a user could change the values of num_[tr]x_desc using
 145		 * ethtool -G so we should keep those values instead of
 146		 * overwriting them with the defaults.
 147		 */
 148		if (!vsi->num_rx_desc)
 149			vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
 150		if (!vsi->num_tx_desc)
 151			vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
 152		break;
 153	default:
 154		dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
 155			vsi->type);
 156		break;
 157	}
 158}
 159
 160/**
 161 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
 162 * @vsi: the VSI being configured
 
 163 *
 164 * Return 0 on success and a negative value on error
 165 */
 166static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
 167{
 168	enum ice_vsi_type vsi_type = vsi->type;
 169	struct ice_pf *pf = vsi->back;
 170	struct ice_vf *vf = vsi->vf;
 171
 172	if (WARN_ON(vsi_type == ICE_VSI_VF && !vf))
 173		return;
 
 
 174
 175	switch (vsi_type) {
 176	case ICE_VSI_PF:
 177		if (vsi->req_txq) {
 178			vsi->alloc_txq = vsi->req_txq;
 179			vsi->num_txq = vsi->req_txq;
 180		} else {
 181			vsi->alloc_txq = min3(pf->num_lan_msix,
 182					      ice_get_avail_txq_count(pf),
 183					      (u16)num_online_cpus());
 184		}
 185
 186		pf->num_lan_tx = vsi->alloc_txq;
 187
 188		/* only 1 Rx queue unless RSS is enabled */
 189		if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
 190			vsi->alloc_rxq = 1;
 191		} else {
 192			if (vsi->req_rxq) {
 193				vsi->alloc_rxq = vsi->req_rxq;
 194				vsi->num_rxq = vsi->req_rxq;
 195			} else {
 196				vsi->alloc_rxq = min3(pf->num_lan_msix,
 197						      ice_get_avail_rxq_count(pf),
 198						      (u16)num_online_cpus());
 199			}
 200		}
 201
 202		pf->num_lan_rx = vsi->alloc_rxq;
 203
 204		vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
 205					   max_t(int, vsi->alloc_rxq,
 206						 vsi->alloc_txq));
 207		break;
 208	case ICE_VSI_SF:
 209		vsi->alloc_txq = 1;
 210		vsi->alloc_rxq = 1;
 211		vsi->num_q_vectors = 1;
 212		vsi->irq_dyn_alloc = true;
 213		break;
 214	case ICE_VSI_VF:
 
 215		if (vf->num_req_qs)
 216			vf->num_vf_qs = vf->num_req_qs;
 217		vsi->alloc_txq = vf->num_vf_qs;
 218		vsi->alloc_rxq = vf->num_vf_qs;
 219		/* pf->vfs.num_msix_per includes (VF miscellaneous vector +
 220		 * data queue interrupts). Since vsi->num_q_vectors is number
 221		 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
 222		 * original vector count
 223		 */
 224		vsi->num_q_vectors = vf->num_msix - ICE_NONQ_VECS_VF;
 225		break;
 226	case ICE_VSI_CTRL:
 227		vsi->alloc_txq = 1;
 228		vsi->alloc_rxq = 1;
 229		vsi->num_q_vectors = 1;
 230		break;
 231	case ICE_VSI_CHNL:
 232		vsi->alloc_txq = 0;
 233		vsi->alloc_rxq = 0;
 234		break;
 235	case ICE_VSI_LB:
 236		vsi->alloc_txq = 1;
 237		vsi->alloc_rxq = 1;
 238		break;
 239	default:
 240		dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi_type);
 241		break;
 242	}
 243
 244	ice_vsi_set_num_desc(vsi);
 245}
 246
 247/**
 248 * ice_get_free_slot - get the next non-NULL location index in array
 249 * @array: array to search
 250 * @size: size of the array
 251 * @curr: last known occupied index to be used as a search hint
 252 *
 253 * void * is being used to keep the functionality generic. This lets us use this
 254 * function on any array of pointers.
 255 */
 256static int ice_get_free_slot(void *array, int size, int curr)
 257{
 258	int **tmp_array = (int **)array;
 259	int next;
 260
 261	if (curr < (size - 1) && !tmp_array[curr + 1]) {
 262		next = curr + 1;
 263	} else {
 264		int i = 0;
 265
 266		while ((i < size) && (tmp_array[i]))
 267			i++;
 268		if (i == size)
 269			next = ICE_NO_VSI;
 270		else
 271			next = i;
 272	}
 273	return next;
 274}
 275
 276/**
 277 * ice_vsi_delete_from_hw - delete a VSI from the switch
 278 * @vsi: pointer to VSI being removed
 279 */
 280static void ice_vsi_delete_from_hw(struct ice_vsi *vsi)
 281{
 282	struct ice_pf *pf = vsi->back;
 283	struct ice_vsi_ctx *ctxt;
 284	int status;
 285
 286	ice_fltr_remove_all(vsi);
 287	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
 288	if (!ctxt)
 289		return;
 290
 291	if (vsi->type == ICE_VSI_VF)
 292		ctxt->vf_num = vsi->vf->vf_id;
 293	ctxt->vsi_num = vsi->vsi_num;
 294
 295	memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
 296
 297	status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
 298	if (status)
 299		dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %d\n",
 300			vsi->vsi_num, status);
 301
 302	kfree(ctxt);
 303}
 304
 305/**
 306 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
 307 * @vsi: pointer to VSI being cleared
 308 */
 309static void ice_vsi_free_arrays(struct ice_vsi *vsi)
 310{
 311	struct ice_pf *pf = vsi->back;
 312	struct device *dev;
 313
 314	dev = ice_pf_to_dev(pf);
 315
 
 
 
 
 316	/* free the ring and vector containers */
 317	devm_kfree(dev, vsi->q_vectors);
 318	vsi->q_vectors = NULL;
 319	devm_kfree(dev, vsi->tx_rings);
 320	vsi->tx_rings = NULL;
 321	devm_kfree(dev, vsi->rx_rings);
 322	vsi->rx_rings = NULL;
 323	devm_kfree(dev, vsi->txq_map);
 324	vsi->txq_map = NULL;
 325	devm_kfree(dev, vsi->rxq_map);
 326	vsi->rxq_map = NULL;
 327}
 328
 329/**
 330 * ice_vsi_free_stats - Free the ring statistics structures
 331 * @vsi: VSI pointer
 332 */
 333static void ice_vsi_free_stats(struct ice_vsi *vsi)
 334{
 335	struct ice_vsi_stats *vsi_stat;
 336	struct ice_pf *pf = vsi->back;
 337	int i;
 338
 339	if (vsi->type == ICE_VSI_CHNL)
 340		return;
 341	if (!pf->vsi_stats)
 342		return;
 343
 344	vsi_stat = pf->vsi_stats[vsi->idx];
 345	if (!vsi_stat)
 346		return;
 347
 348	ice_for_each_alloc_txq(vsi, i) {
 349		if (vsi_stat->tx_ring_stats[i]) {
 350			kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
 351			WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
 352		}
 353	}
 354
 355	ice_for_each_alloc_rxq(vsi, i) {
 356		if (vsi_stat->rx_ring_stats[i]) {
 357			kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
 358			WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
 359		}
 360	}
 361
 362	kfree(vsi_stat->tx_ring_stats);
 363	kfree(vsi_stat->rx_ring_stats);
 364	kfree(vsi_stat);
 365	pf->vsi_stats[vsi->idx] = NULL;
 366}
 367
 368/**
 369 * ice_vsi_alloc_ring_stats - Allocates Tx and Rx ring stats for the VSI
 370 * @vsi: VSI which is having stats allocated
 371 */
 372static int ice_vsi_alloc_ring_stats(struct ice_vsi *vsi)
 373{
 374	struct ice_ring_stats **tx_ring_stats;
 375	struct ice_ring_stats **rx_ring_stats;
 376	struct ice_vsi_stats *vsi_stats;
 377	struct ice_pf *pf = vsi->back;
 378	u16 i;
 379
 380	vsi_stats = pf->vsi_stats[vsi->idx];
 381	tx_ring_stats = vsi_stats->tx_ring_stats;
 382	rx_ring_stats = vsi_stats->rx_ring_stats;
 383
 384	/* Allocate Tx ring stats */
 385	ice_for_each_alloc_txq(vsi, i) {
 386		struct ice_ring_stats *ring_stats;
 387		struct ice_tx_ring *ring;
 388
 389		ring = vsi->tx_rings[i];
 390		ring_stats = tx_ring_stats[i];
 391
 392		if (!ring_stats) {
 393			ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
 394			if (!ring_stats)
 395				goto err_out;
 396
 397			WRITE_ONCE(tx_ring_stats[i], ring_stats);
 398		}
 399
 400		ring->ring_stats = ring_stats;
 401	}
 402
 403	/* Allocate Rx ring stats */
 404	ice_for_each_alloc_rxq(vsi, i) {
 405		struct ice_ring_stats *ring_stats;
 406		struct ice_rx_ring *ring;
 407
 408		ring = vsi->rx_rings[i];
 409		ring_stats = rx_ring_stats[i];
 410
 411		if (!ring_stats) {
 412			ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
 413			if (!ring_stats)
 414				goto err_out;
 415
 416			WRITE_ONCE(rx_ring_stats[i], ring_stats);
 417		}
 418
 419		ring->ring_stats = ring_stats;
 420	}
 421
 422	return 0;
 423
 424err_out:
 425	ice_vsi_free_stats(vsi);
 426	return -ENOMEM;
 427}
 428
 429/**
 430 * ice_vsi_free - clean up and deallocate the provided VSI
 431 * @vsi: pointer to VSI being cleared
 432 *
 433 * This deallocates the VSI's queue resources, removes it from the PF's
 434 * VSI array if necessary, and deallocates the VSI
 
 
 435 */
 436void ice_vsi_free(struct ice_vsi *vsi)
 437{
 438	struct ice_pf *pf = NULL;
 439	struct device *dev;
 440
 441	if (!vsi || !vsi->back)
 442		return;
 
 
 
 443
 444	pf = vsi->back;
 445	dev = ice_pf_to_dev(pf);
 446
 447	if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
 448		dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
 449		return;
 450	}
 451
 452	mutex_lock(&pf->sw_mutex);
 453	/* updates the PF for this cleared VSI */
 454
 455	pf->vsi[vsi->idx] = NULL;
 456	pf->next_vsi = vsi->idx;
 
 
 
 
 457
 458	ice_vsi_free_stats(vsi);
 459	ice_vsi_free_arrays(vsi);
 460	mutex_destroy(&vsi->xdp_state_lock);
 461	mutex_unlock(&pf->sw_mutex);
 462	devm_kfree(dev, vsi);
 463}
 464
 465void ice_vsi_delete(struct ice_vsi *vsi)
 466{
 467	ice_vsi_delete_from_hw(vsi);
 468	ice_vsi_free(vsi);
 469}
 470
 471/**
 472 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
 473 * @irq: interrupt number
 474 * @data: pointer to a q_vector
 475 */
 476static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
 477{
 478	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
 479
 480	if (!q_vector->tx.tx_ring)
 481		return IRQ_HANDLED;
 482
 483#define FDIR_RX_DESC_CLEAN_BUDGET 64
 484	ice_clean_rx_irq(q_vector->rx.rx_ring, FDIR_RX_DESC_CLEAN_BUDGET);
 485	ice_clean_ctrl_tx_irq(q_vector->tx.tx_ring);
 486
 487	return IRQ_HANDLED;
 488}
 489
 490/**
 491 * ice_msix_clean_rings - MSIX mode Interrupt Handler
 492 * @irq: interrupt number
 493 * @data: pointer to a q_vector
 494 */
 495static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
 496{
 497	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
 498
 499	if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
 500		return IRQ_HANDLED;
 501
 502	q_vector->total_events++;
 503
 504	napi_schedule(&q_vector->napi);
 505
 506	return IRQ_HANDLED;
 507}
 508
 509/**
 510 * ice_vsi_alloc_stat_arrays - Allocate statistics arrays
 511 * @vsi: VSI pointer
 512 */
 513static int ice_vsi_alloc_stat_arrays(struct ice_vsi *vsi)
 514{
 515	struct ice_vsi_stats *vsi_stat;
 516	struct ice_pf *pf = vsi->back;
 517
 518	if (vsi->type == ICE_VSI_CHNL)
 519		return 0;
 520	if (!pf->vsi_stats)
 521		return -ENOENT;
 522
 523	if (pf->vsi_stats[vsi->idx])
 524	/* realloc will happen in rebuild path */
 525		return 0;
 526
 527	vsi_stat = kzalloc(sizeof(*vsi_stat), GFP_KERNEL);
 528	if (!vsi_stat)
 529		return -ENOMEM;
 530
 531	vsi_stat->tx_ring_stats =
 532		kcalloc(vsi->alloc_txq, sizeof(*vsi_stat->tx_ring_stats),
 533			GFP_KERNEL);
 534	if (!vsi_stat->tx_ring_stats)
 535		goto err_alloc_tx;
 536
 537	vsi_stat->rx_ring_stats =
 538		kcalloc(vsi->alloc_rxq, sizeof(*vsi_stat->rx_ring_stats),
 539			GFP_KERNEL);
 540	if (!vsi_stat->rx_ring_stats)
 541		goto err_alloc_rx;
 542
 543	pf->vsi_stats[vsi->idx] = vsi_stat;
 544
 545	return 0;
 546
 547err_alloc_rx:
 548	kfree(vsi_stat->rx_ring_stats);
 549err_alloc_tx:
 550	kfree(vsi_stat->tx_ring_stats);
 551	kfree(vsi_stat);
 552	pf->vsi_stats[vsi->idx] = NULL;
 553	return -ENOMEM;
 554}
 555
 556/**
 557 * ice_vsi_alloc_def - set default values for already allocated VSI
 558 * @vsi: ptr to VSI
 559 * @ch: ptr to channel
 560 */
 561static int
 562ice_vsi_alloc_def(struct ice_vsi *vsi, struct ice_channel *ch)
 563{
 564	if (vsi->type != ICE_VSI_CHNL) {
 565		ice_vsi_set_num_qs(vsi);
 566		if (ice_vsi_alloc_arrays(vsi))
 567			return -ENOMEM;
 568	}
 569
 570	switch (vsi->type) {
 571	case ICE_VSI_PF:
 572	case ICE_VSI_SF:
 573		/* Setup default MSIX irq handler for VSI */
 574		vsi->irq_handler = ice_msix_clean_rings;
 575		break;
 576	case ICE_VSI_CTRL:
 577		/* Setup ctrl VSI MSIX irq handler */
 578		vsi->irq_handler = ice_msix_clean_ctrl_vsi;
 579		break;
 580	case ICE_VSI_CHNL:
 581		if (!ch)
 582			return -EINVAL;
 583
 584		vsi->num_rxq = ch->num_rxq;
 585		vsi->num_txq = ch->num_txq;
 586		vsi->next_base_q = ch->base_q;
 587		break;
 588	case ICE_VSI_VF:
 589	case ICE_VSI_LB:
 590		break;
 591	default:
 592		ice_vsi_free_arrays(vsi);
 593		return -EINVAL;
 594	}
 595
 596	return 0;
 597}
 598
 599/**
 600 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
 601 * @pf: board private structure
 602 *
 603 * Reserves a VSI index from the PF and allocates an empty VSI structure
 604 * without a type. The VSI structure must later be initialized by calling
 605 * ice_vsi_cfg().
 606 *
 607 * returns a pointer to a VSI on success, NULL on failure.
 608 */
 609struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf)
 
 610{
 611	struct device *dev = ice_pf_to_dev(pf);
 612	struct ice_vsi *vsi = NULL;
 613
 614	/* Need to protect the allocation of the VSIs at the PF level */
 615	mutex_lock(&pf->sw_mutex);
 616
 617	/* If we have already allocated our maximum number of VSIs,
 618	 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
 619	 * is available to be populated
 620	 */
 621	if (pf->next_vsi == ICE_NO_VSI) {
 622		dev_dbg(dev, "out of VSI slots!\n");
 623		goto unlock_pf;
 624	}
 625
 626	vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
 627	if (!vsi)
 628		goto unlock_pf;
 629
 
 630	vsi->back = pf;
 631	set_bit(ICE_VSI_DOWN, vsi->state);
 632
 633	/* fill slot and make note of the index */
 634	vsi->idx = pf->next_vsi;
 635	pf->vsi[pf->next_vsi] = vsi;
 636
 637	/* prepare pf->next_vsi for next use */
 638	pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
 639					 pf->next_vsi);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 640
 641	mutex_init(&vsi->xdp_state_lock);
 
 
 642
 
 
 
 643unlock_pf:
 644	mutex_unlock(&pf->sw_mutex);
 645	return vsi;
 646}
 647
 648/**
 649 * ice_alloc_fd_res - Allocate FD resource for a VSI
 650 * @vsi: pointer to the ice_vsi
 651 *
 652 * This allocates the FD resources
 653 *
 654 * Returns 0 on success, -EPERM on no-op or -EIO on failure
 655 */
 656static int ice_alloc_fd_res(struct ice_vsi *vsi)
 657{
 658	struct ice_pf *pf = vsi->back;
 659	u32 g_val, b_val;
 660
 661	/* Flow Director filters are only allocated/assigned to the PF VSI or
 662	 * CHNL VSI which passes the traffic. The CTRL VSI is only used to
 663	 * add/delete filters so resources are not allocated to it
 664	 */
 665	if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
 666		return -EPERM;
 667
 668	if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF ||
 669	      vsi->type == ICE_VSI_CHNL))
 670		return -EPERM;
 671
 672	/* FD filters from guaranteed pool per VSI */
 673	g_val = pf->hw.func_caps.fd_fltr_guar;
 674	if (!g_val)
 675		return -EPERM;
 676
 677	/* FD filters from best effort pool */
 678	b_val = pf->hw.func_caps.fd_fltr_best_effort;
 679	if (!b_val)
 680		return -EPERM;
 681
 682	/* PF main VSI gets only 64 FD resources from guaranteed pool
 683	 * when ADQ is configured.
 684	 */
 685#define ICE_PF_VSI_GFLTR	64
 686
 687	/* determine FD filter resources per VSI from shared(best effort) and
 688	 * dedicated pool
 689	 */
 690	if (vsi->type == ICE_VSI_PF) {
 691		vsi->num_gfltr = g_val;
 692		/* if MQPRIO is configured, main VSI doesn't get all FD
 693		 * resources from guaranteed pool. PF VSI gets 64 FD resources
 694		 */
 695		if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
 696			if (g_val < ICE_PF_VSI_GFLTR)
 697				return -EPERM;
 698			/* allow bare minimum entries for PF VSI */
 699			vsi->num_gfltr = ICE_PF_VSI_GFLTR;
 700		}
 701
 702		/* each VSI gets same "best_effort" quota */
 703		vsi->num_bfltr = b_val;
 704	} else if (vsi->type == ICE_VSI_VF) {
 705		vsi->num_gfltr = 0;
 706
 707		/* each VSI gets same "best_effort" quota */
 708		vsi->num_bfltr = b_val;
 709	} else {
 710		struct ice_vsi *main_vsi;
 711		int numtc;
 712
 713		main_vsi = ice_get_main_vsi(pf);
 714		if (!main_vsi)
 715			return -EPERM;
 716
 717		if (!main_vsi->all_numtc)
 718			return -EINVAL;
 719
 720		/* figure out ADQ numtc */
 721		numtc = main_vsi->all_numtc - ICE_CHNL_START_TC;
 722
 723		/* only one TC but still asking resources for channels,
 724		 * invalid config
 725		 */
 726		if (numtc < ICE_CHNL_START_TC)
 727			return -EPERM;
 728
 729		g_val -= ICE_PF_VSI_GFLTR;
 730		/* channel VSIs gets equal share from guaranteed pool */
 731		vsi->num_gfltr = g_val / numtc;
 732
 733		/* each VSI gets same "best_effort" quota */
 734		vsi->num_bfltr = b_val;
 735	}
 736
 737	return 0;
 738}
 739
 740/**
 741 * ice_vsi_get_qs - Assign queues from PF to VSI
 742 * @vsi: the VSI to assign queues to
 743 *
 744 * Returns 0 on success and a negative value on error
 745 */
 746static int ice_vsi_get_qs(struct ice_vsi *vsi)
 747{
 748	struct ice_pf *pf = vsi->back;
 749	struct ice_qs_cfg tx_qs_cfg = {
 750		.qs_mutex = &pf->avail_q_mutex,
 751		.pf_map = pf->avail_txqs,
 752		.pf_map_size = pf->max_pf_txqs,
 753		.q_count = vsi->alloc_txq,
 754		.scatter_count = ICE_MAX_SCATTER_TXQS,
 755		.vsi_map = vsi->txq_map,
 756		.vsi_map_offset = 0,
 757		.mapping_mode = ICE_VSI_MAP_CONTIG
 758	};
 759	struct ice_qs_cfg rx_qs_cfg = {
 760		.qs_mutex = &pf->avail_q_mutex,
 761		.pf_map = pf->avail_rxqs,
 762		.pf_map_size = pf->max_pf_rxqs,
 763		.q_count = vsi->alloc_rxq,
 764		.scatter_count = ICE_MAX_SCATTER_RXQS,
 765		.vsi_map = vsi->rxq_map,
 766		.vsi_map_offset = 0,
 767		.mapping_mode = ICE_VSI_MAP_CONTIG
 768	};
 769	int ret;
 770
 771	if (vsi->type == ICE_VSI_CHNL)
 772		return 0;
 773
 774	ret = __ice_vsi_get_qs(&tx_qs_cfg);
 775	if (ret)
 776		return ret;
 777	vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
 778
 779	ret = __ice_vsi_get_qs(&rx_qs_cfg);
 780	if (ret)
 781		return ret;
 782	vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
 783
 784	return 0;
 785}
 786
 787/**
 788 * ice_vsi_put_qs - Release queues from VSI to PF
 789 * @vsi: the VSI that is going to release queues
 790 */
 791static void ice_vsi_put_qs(struct ice_vsi *vsi)
 792{
 793	struct ice_pf *pf = vsi->back;
 794	int i;
 795
 796	mutex_lock(&pf->avail_q_mutex);
 797
 798	ice_for_each_alloc_txq(vsi, i) {
 799		clear_bit(vsi->txq_map[i], pf->avail_txqs);
 800		vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
 801	}
 802
 803	ice_for_each_alloc_rxq(vsi, i) {
 804		clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
 805		vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
 806	}
 807
 808	mutex_unlock(&pf->avail_q_mutex);
 809}
 810
 811/**
 812 * ice_is_safe_mode
 813 * @pf: pointer to the PF struct
 814 *
 815 * returns true if driver is in safe mode, false otherwise
 816 */
 817bool ice_is_safe_mode(struct ice_pf *pf)
 818{
 819	return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
 820}
 821
 822/**
 823 * ice_is_rdma_ena
 824 * @pf: pointer to the PF struct
 825 *
 826 * returns true if RDMA is currently supported, false otherwise
 827 */
 828bool ice_is_rdma_ena(struct ice_pf *pf)
 829{
 830	return test_bit(ICE_FLAG_RDMA_ENA, pf->flags);
 831}
 832
 833/**
 834 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
 835 * @vsi: the VSI being cleaned up
 836 *
 837 * This function deletes RSS input set for all flows that were configured
 838 * for this VSI
 839 */
 840static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
 841{
 842	struct ice_pf *pf = vsi->back;
 843	int status;
 844
 845	if (ice_is_safe_mode(pf))
 846		return;
 847
 848	status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
 849	if (status)
 850		dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %d\n",
 851			vsi->vsi_num, status);
 852}
 853
 854/**
 855 * ice_rss_clean - Delete RSS related VSI structures and configuration
 856 * @vsi: the VSI being removed
 857 */
 858static void ice_rss_clean(struct ice_vsi *vsi)
 859{
 860	struct ice_pf *pf = vsi->back;
 861	struct device *dev;
 862
 863	dev = ice_pf_to_dev(pf);
 864
 865	devm_kfree(dev, vsi->rss_hkey_user);
 866	devm_kfree(dev, vsi->rss_lut_user);
 
 
 867
 868	ice_vsi_clean_rss_flow_fld(vsi);
 869	/* remove RSS replay list */
 870	if (!ice_is_safe_mode(pf))
 871		ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
 872}
 873
 874/**
 875 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
 876 * @vsi: the VSI being configured
 877 */
 878static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
 879{
 880	struct ice_hw_common_caps *cap;
 881	struct ice_pf *pf = vsi->back;
 882	u16 max_rss_size;
 883
 884	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
 885		vsi->rss_size = 1;
 886		return;
 887	}
 888
 889	cap = &pf->hw.func_caps.common_cap;
 890	max_rss_size = BIT(cap->rss_table_entry_width);
 891	switch (vsi->type) {
 892	case ICE_VSI_CHNL:
 893	case ICE_VSI_PF:
 894		/* PF VSI will inherit RSS instance of PF */
 895		vsi->rss_table_size = (u16)cap->rss_table_size;
 896		if (vsi->type == ICE_VSI_CHNL)
 897			vsi->rss_size = min_t(u16, vsi->num_rxq, max_rss_size);
 898		else
 899			vsi->rss_size = min_t(u16, num_online_cpus(),
 900					      max_rss_size);
 901		vsi->rss_lut_type = ICE_LUT_PF;
 902		break;
 903	case ICE_VSI_SF:
 904		vsi->rss_table_size = ICE_LUT_VSI_SIZE;
 905		vsi->rss_size = min_t(u16, num_online_cpus(), max_rss_size);
 906		vsi->rss_lut_type = ICE_LUT_VSI;
 907		break;
 908	case ICE_VSI_VF:
 909		/* VF VSI will get a small RSS table.
 910		 * For VSI_LUT, LUT size should be set to 64 bytes.
 911		 */
 912		vsi->rss_table_size = ICE_LUT_VSI_SIZE;
 913		vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
 914		vsi->rss_lut_type = ICE_LUT_VSI;
 915		break;
 916	case ICE_VSI_LB:
 917		break;
 918	default:
 919		dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
 920			ice_vsi_type_str(vsi->type));
 921		break;
 922	}
 923}
 924
 925/**
 926 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
 927 * @hw: HW structure used to determine the VLAN mode of the device
 928 * @ctxt: the VSI context being set
 929 *
 930 * This initializes a default VSI context for all sections except the Queues.
 931 */
 932static void ice_set_dflt_vsi_ctx(struct ice_hw *hw, struct ice_vsi_ctx *ctxt)
 933{
 934	u32 table = 0;
 935
 936	memset(&ctxt->info, 0, sizeof(ctxt->info));
 937	/* VSI's should be allocated from shared pool */
 938	ctxt->alloc_from_pool = true;
 939	/* Src pruning enabled by default */
 940	ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
 941	/* Traffic from VSI can be sent to LAN */
 942	ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
 943	/* allow all untagged/tagged packets by default on Tx */
 944	ctxt->info.inner_vlan_flags = FIELD_PREP(ICE_AQ_VSI_INNER_VLAN_TX_MODE_M,
 945						 ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL);
 946	/* SVM - by default bits 3 and 4 in inner_vlan_flags are 0's which
 947	 * results in legacy behavior (show VLAN, DEI, and UP) in descriptor.
 948	 *
 949	 * DVM - leave inner VLAN in packet by default
 950	 */
 951	if (ice_is_dvm_ena(hw)) {
 952		ctxt->info.inner_vlan_flags |=
 953			FIELD_PREP(ICE_AQ_VSI_INNER_VLAN_EMODE_M,
 954				   ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING);
 955		ctxt->info.outer_vlan_flags =
 956			FIELD_PREP(ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M,
 957				   ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ALL);
 958		ctxt->info.outer_vlan_flags |=
 959			FIELD_PREP(ICE_AQ_VSI_OUTER_TAG_TYPE_M,
 960				   ICE_AQ_VSI_OUTER_TAG_VLAN_8100);
 961		ctxt->info.outer_vlan_flags |=
 962			FIELD_PREP(ICE_AQ_VSI_OUTER_VLAN_EMODE_M,
 963				   ICE_AQ_VSI_OUTER_VLAN_EMODE_NOTHING);
 964	}
 965	/* Have 1:1 UP mapping for both ingress/egress tables */
 966	table |= ICE_UP_TABLE_TRANSLATE(0, 0);
 967	table |= ICE_UP_TABLE_TRANSLATE(1, 1);
 968	table |= ICE_UP_TABLE_TRANSLATE(2, 2);
 969	table |= ICE_UP_TABLE_TRANSLATE(3, 3);
 970	table |= ICE_UP_TABLE_TRANSLATE(4, 4);
 971	table |= ICE_UP_TABLE_TRANSLATE(5, 5);
 972	table |= ICE_UP_TABLE_TRANSLATE(6, 6);
 973	table |= ICE_UP_TABLE_TRANSLATE(7, 7);
 974	ctxt->info.ingress_table = cpu_to_le32(table);
 975	ctxt->info.egress_table = cpu_to_le32(table);
 976	/* Have 1:1 UP mapping for outer to inner UP table */
 977	ctxt->info.outer_up_table = cpu_to_le32(table);
 978	/* No Outer tag support outer_tag_flags remains to zero */
 979}
 980
 981/**
 982 * ice_vsi_setup_q_map - Setup a VSI queue map
 983 * @vsi: the VSI being configured
 984 * @ctxt: VSI context structure
 985 */
 986static int ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
 987{
 988	u16 offset = 0, qmap = 0, tx_count = 0, rx_count = 0, pow = 0;
 989	u16 num_txq_per_tc, num_rxq_per_tc;
 990	u16 qcount_tx = vsi->alloc_txq;
 991	u16 qcount_rx = vsi->alloc_rxq;
 
 992	u8 netdev_tc = 0;
 993	int i;
 994
 995	if (!vsi->tc_cfg.numtc) {
 996		/* at least TC0 should be enabled by default */
 997		vsi->tc_cfg.numtc = 1;
 998		vsi->tc_cfg.ena_tc = 1;
 
 
 
 
 
 
 
 999	}
1000
1001	num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
1002	if (!num_rxq_per_tc)
1003		num_rxq_per_tc = 1;
1004	num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
1005	if (!num_txq_per_tc)
1006		num_txq_per_tc = 1;
1007
1008	/* find the (rounded up) power-of-2 of qcount */
1009	pow = (u16)order_base_2(num_rxq_per_tc);
1010
1011	/* TC mapping is a function of the number of Rx queues assigned to the
1012	 * VSI for each traffic class and the offset of these queues.
1013	 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
1014	 * queues allocated to TC0. No:of queues is a power-of-2.
1015	 *
1016	 * If TC is not enabled, the queue offset is set to 0, and allocate one
1017	 * queue, this way, traffic for the given TC will be sent to the default
1018	 * queue.
1019	 *
1020	 * Setup number and offset of Rx queues for all TCs for the VSI
1021	 */
1022	ice_for_each_traffic_class(i) {
1023		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
1024			/* TC is not enabled */
1025			vsi->tc_cfg.tc_info[i].qoffset = 0;
1026			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
1027			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
1028			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
1029			ctxt->info.tc_mapping[i] = 0;
1030			continue;
1031		}
1032
1033		/* TC is enabled */
1034		vsi->tc_cfg.tc_info[i].qoffset = offset;
1035		vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
1036		vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
1037		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
1038
1039		qmap = FIELD_PREP(ICE_AQ_VSI_TC_Q_OFFSET_M, offset);
1040		qmap |= FIELD_PREP(ICE_AQ_VSI_TC_Q_NUM_M, pow);
 
 
1041		offset += num_rxq_per_tc;
1042		tx_count += num_txq_per_tc;
1043		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1044	}
1045
1046	/* if offset is non-zero, means it is calculated correctly based on
1047	 * enabled TCs for a given VSI otherwise qcount_rx will always
1048	 * be correct and non-zero because it is based off - VSI's
1049	 * allocated Rx queues which is at least 1 (hence qcount_tx will be
1050	 * at least 1)
1051	 */
1052	if (offset)
1053		rx_count = offset;
1054	else
1055		rx_count = num_rxq_per_tc;
1056
1057	if (rx_count > vsi->alloc_rxq) {
1058		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
1059			rx_count, vsi->alloc_rxq);
1060		return -EINVAL;
1061	}
1062
1063	if (tx_count > vsi->alloc_txq) {
1064		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
1065			tx_count, vsi->alloc_txq);
1066		return -EINVAL;
1067	}
1068
1069	vsi->num_txq = tx_count;
1070	vsi->num_rxq = rx_count;
1071
1072	if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
1073		dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
1074		/* since there is a chance that num_rxq could have been changed
1075		 * in the above for loop, make num_txq equal to num_rxq.
1076		 */
1077		vsi->num_txq = vsi->num_rxq;
1078	}
1079
1080	/* Rx queue mapping */
1081	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1082	/* q_mapping buffer holds the info for the first queue allocated for
1083	 * this VSI in the PF space and also the number of queues associated
1084	 * with this VSI.
1085	 */
1086	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
1087	ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
1088
1089	return 0;
1090}
1091
1092/**
1093 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
1094 * @ctxt: the VSI context being set
1095 * @vsi: the VSI being configured
1096 */
1097static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1098{
1099	u8 dflt_q_group, dflt_q_prio;
1100	u16 dflt_q, report_q, val;
1101
1102	if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
1103	    vsi->type != ICE_VSI_VF && vsi->type != ICE_VSI_CHNL)
1104		return;
1105
1106	val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
1107	ctxt->info.valid_sections |= cpu_to_le16(val);
1108	dflt_q = 0;
1109	dflt_q_group = 0;
1110	report_q = 0;
1111	dflt_q_prio = 0;
1112
1113	/* enable flow director filtering/programming */
1114	val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
1115	ctxt->info.fd_options = cpu_to_le16(val);
1116	/* max of allocated flow director filters */
1117	ctxt->info.max_fd_fltr_dedicated =
1118			cpu_to_le16(vsi->num_gfltr);
1119	/* max of shared flow director filters any VSI may program */
1120	ctxt->info.max_fd_fltr_shared =
1121			cpu_to_le16(vsi->num_bfltr);
1122	/* default queue index within the VSI of the default FD */
1123	val = FIELD_PREP(ICE_AQ_VSI_FD_DEF_Q_M, dflt_q);
 
1124	/* target queue or queue group to the FD filter */
1125	val |= FIELD_PREP(ICE_AQ_VSI_FD_DEF_GRP_M, dflt_q_group);
 
1126	ctxt->info.fd_def_q = cpu_to_le16(val);
1127	/* queue index on which FD filter completion is reported */
1128	val = FIELD_PREP(ICE_AQ_VSI_FD_REPORT_Q_M, report_q);
 
1129	/* priority of the default qindex action */
1130	val |= FIELD_PREP(ICE_AQ_VSI_FD_DEF_PRIORITY_M, dflt_q_prio);
 
1131	ctxt->info.fd_report_opt = cpu_to_le16(val);
1132}
1133
1134/**
1135 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
1136 * @ctxt: the VSI context being set
1137 * @vsi: the VSI being configured
1138 */
1139static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
1140{
1141	u8 lut_type, hash_type;
1142	struct device *dev;
1143	struct ice_pf *pf;
1144
1145	pf = vsi->back;
1146	dev = ice_pf_to_dev(pf);
1147
1148	switch (vsi->type) {
1149	case ICE_VSI_CHNL:
1150	case ICE_VSI_PF:
1151		/* PF VSI will inherit RSS instance of PF */
1152		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
 
1153		break;
1154	case ICE_VSI_VF:
1155	case ICE_VSI_SF:
1156		/* VF VSI will gets a small RSS table which is a VSI LUT type */
1157		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
 
1158		break;
1159	default:
1160		dev_dbg(dev, "Unsupported VSI type %s\n",
1161			ice_vsi_type_str(vsi->type));
1162		return;
1163	}
1164
1165	hash_type = ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ;
1166	vsi->rss_hfunc = hash_type;
1167
1168	ctxt->info.q_opt_rss =
1169		FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_LUT_M, lut_type) |
1170		FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hash_type);
1171}
1172
1173static void
1174ice_chnl_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
1175{
1176	struct ice_pf *pf = vsi->back;
1177	u16 qcount, qmap;
1178	u8 offset = 0;
1179	int pow;
1180
1181	qcount = min_t(int, vsi->num_rxq, pf->num_lan_msix);
1182
1183	pow = order_base_2(qcount);
1184	qmap = FIELD_PREP(ICE_AQ_VSI_TC_Q_OFFSET_M, offset);
1185	qmap |= FIELD_PREP(ICE_AQ_VSI_TC_Q_NUM_M, pow);
1186
1187	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1188	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
1189	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->next_base_q);
1190	ctxt->info.q_mapping[1] = cpu_to_le16(qcount);
1191}
1192
1193/**
1194 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
1195 * @vsi: VSI to check whether or not VLAN pruning is enabled.
1196 *
1197 * returns true if Rx VLAN pruning is enabled and false otherwise.
1198 */
1199static bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
1200{
1201	return vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1202}
1203
1204/**
1205 * ice_vsi_init - Create and initialize a VSI
1206 * @vsi: the VSI being configured
1207 * @vsi_flags: VSI configuration flags
1208 *
1209 * Set ICE_FLAG_VSI_INIT to initialize a new VSI context, clear it to
1210 * reconfigure an existing context.
1211 *
1212 * This initializes a VSI context depending on the VSI type to be added and
1213 * passes it down to the add_vsi aq command to create a new VSI.
1214 */
1215static int ice_vsi_init(struct ice_vsi *vsi, u32 vsi_flags)
1216{
1217	struct ice_pf *pf = vsi->back;
1218	struct ice_hw *hw = &pf->hw;
1219	struct ice_vsi_ctx *ctxt;
1220	struct device *dev;
1221	int ret = 0;
1222
1223	dev = ice_pf_to_dev(pf);
1224	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1225	if (!ctxt)
1226		return -ENOMEM;
1227
1228	switch (vsi->type) {
1229	case ICE_VSI_CTRL:
1230	case ICE_VSI_LB:
1231	case ICE_VSI_PF:
1232		ctxt->flags = ICE_AQ_VSI_TYPE_PF;
1233		break;
1234	case ICE_VSI_SF:
1235	case ICE_VSI_CHNL:
1236		ctxt->flags = ICE_AQ_VSI_TYPE_VMDQ2;
1237		break;
1238	case ICE_VSI_VF:
1239		ctxt->flags = ICE_AQ_VSI_TYPE_VF;
1240		/* VF number here is the absolute VF number (0-255) */
1241		ctxt->vf_num = vsi->vf->vf_id + hw->func_caps.vf_base_id;
1242		break;
1243	default:
1244		ret = -ENODEV;
1245		goto out;
1246	}
1247
1248	/* Handle VLAN pruning for channel VSI if main VSI has VLAN
1249	 * prune enabled
1250	 */
1251	if (vsi->type == ICE_VSI_CHNL) {
1252		struct ice_vsi *main_vsi;
1253
1254		main_vsi = ice_get_main_vsi(pf);
1255		if (main_vsi && ice_vsi_is_vlan_pruning_ena(main_vsi))
1256			ctxt->info.sw_flags2 |=
1257				ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1258		else
1259			ctxt->info.sw_flags2 &=
1260				~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
1261	}
1262
1263	ice_set_dflt_vsi_ctx(hw, ctxt);
1264	if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
1265		ice_set_fd_vsi_ctx(ctxt, vsi);
1266	/* if the switch is in VEB mode, allow VSI loopback */
1267	if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1268		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1269
1270	/* Set LUT type and HASH type if RSS is enabled */
1271	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
1272	    vsi->type != ICE_VSI_CTRL) {
1273		ice_set_rss_vsi_ctx(ctxt, vsi);
1274		/* if updating VSI context, make sure to set valid_section:
1275		 * to indicate which section of VSI context being updated
1276		 */
1277		if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1278			ctxt->info.valid_sections |=
1279				cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1280	}
1281
1282	ctxt->info.sw_id = vsi->port_info->sw_id;
1283	if (vsi->type == ICE_VSI_CHNL) {
1284		ice_chnl_vsi_setup_q_map(vsi, ctxt);
1285	} else {
1286		ret = ice_vsi_setup_q_map(vsi, ctxt);
1287		if (ret)
1288			goto out;
 
1289
1290		if (!(vsi_flags & ICE_VSI_FLAG_INIT))
1291			/* means VSI being updated */
1292			/* must to indicate which section of VSI context are
1293			 * being modified
1294			 */
1295			ctxt->info.valid_sections |=
1296				cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
 
 
 
 
 
 
 
 
 
 
1297	}
1298
1299	/* Allow control frames out of main VSI */
1300	if (vsi->type == ICE_VSI_PF) {
1301		ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1302		ctxt->info.valid_sections |=
1303			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1304	}
1305
1306	if (vsi_flags & ICE_VSI_FLAG_INIT) {
1307		ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1308		if (ret) {
1309			dev_err(dev, "Add VSI failed, err %d\n", ret);
1310			ret = -EIO;
1311			goto out;
1312		}
1313	} else {
1314		ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1315		if (ret) {
1316			dev_err(dev, "Update VSI failed, err %d\n", ret);
1317			ret = -EIO;
1318			goto out;
1319		}
1320	}
1321
1322	/* keep context for update VSI operations */
1323	vsi->info = ctxt->info;
1324
1325	/* record VSI number returned */
1326	vsi->vsi_num = ctxt->vsi_num;
1327
1328out:
1329	kfree(ctxt);
1330	return ret;
1331}
1332
1333/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1334 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1335 * @vsi: the VSI having rings deallocated
1336 */
1337static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1338{
1339	int i;
1340
1341	/* Avoid stale references by clearing map from vector to ring */
1342	if (vsi->q_vectors) {
1343		ice_for_each_q_vector(vsi, i) {
1344			struct ice_q_vector *q_vector = vsi->q_vectors[i];
1345
1346			if (q_vector) {
1347				q_vector->tx.tx_ring = NULL;
1348				q_vector->rx.rx_ring = NULL;
1349			}
1350		}
1351	}
1352
1353	if (vsi->tx_rings) {
1354		ice_for_each_alloc_txq(vsi, i) {
1355			if (vsi->tx_rings[i]) {
1356				kfree_rcu(vsi->tx_rings[i], rcu);
1357				WRITE_ONCE(vsi->tx_rings[i], NULL);
1358			}
1359		}
1360	}
1361	if (vsi->rx_rings) {
1362		ice_for_each_alloc_rxq(vsi, i) {
1363			if (vsi->rx_rings[i]) {
1364				kfree_rcu(vsi->rx_rings[i], rcu);
1365				WRITE_ONCE(vsi->rx_rings[i], NULL);
1366			}
1367		}
1368	}
1369}
1370
1371/**
1372 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1373 * @vsi: VSI which is having rings allocated
1374 */
1375static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1376{
1377	bool dvm_ena = ice_is_dvm_ena(&vsi->back->hw);
1378	struct ice_pf *pf = vsi->back;
1379	struct device *dev;
1380	u16 i;
1381
1382	dev = ice_pf_to_dev(pf);
1383	/* Allocate Tx rings */
1384	ice_for_each_alloc_txq(vsi, i) {
1385		struct ice_tx_ring *ring;
1386
1387		/* allocate with kzalloc(), free with kfree_rcu() */
1388		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1389
1390		if (!ring)
1391			goto err_out;
1392
1393		ring->q_index = i;
1394		ring->reg_idx = vsi->txq_map[i];
 
1395		ring->vsi = vsi;
1396		ring->tx_tstamps = &pf->ptp.port.tx;
1397		ring->dev = dev;
1398		ring->count = vsi->num_tx_desc;
1399		ring->txq_teid = ICE_INVAL_TEID;
1400		if (dvm_ena)
1401			ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG2;
1402		else
1403			ring->flags |= ICE_TX_FLAGS_RING_VLAN_L2TAG1;
1404		WRITE_ONCE(vsi->tx_rings[i], ring);
1405	}
1406
1407	/* Allocate Rx rings */
1408	ice_for_each_alloc_rxq(vsi, i) {
1409		struct ice_rx_ring *ring;
1410
1411		/* allocate with kzalloc(), free with kfree_rcu() */
1412		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1413		if (!ring)
1414			goto err_out;
1415
1416		ring->q_index = i;
1417		ring->reg_idx = vsi->rxq_map[i];
 
1418		ring->vsi = vsi;
1419		ring->netdev = vsi->netdev;
1420		ring->dev = dev;
1421		ring->count = vsi->num_rx_desc;
1422		ring->cached_phctime = pf->ptp.cached_phc_time;
1423		WRITE_ONCE(vsi->rx_rings[i], ring);
1424	}
1425
1426	return 0;
1427
1428err_out:
1429	ice_vsi_clear_rings(vsi);
1430	return -ENOMEM;
1431}
1432
1433/**
1434 * ice_vsi_manage_rss_lut - disable/enable RSS
1435 * @vsi: the VSI being changed
1436 * @ena: boolean value indicating if this is an enable or disable request
1437 *
1438 * In the event of disable request for RSS, this function will zero out RSS
1439 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1440 * LUT.
1441 */
1442void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1443{
1444	u8 *lut;
1445
1446	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1447	if (!lut)
1448		return;
1449
1450	if (ena) {
1451		if (vsi->rss_lut_user)
1452			memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1453		else
1454			ice_fill_rss_lut(lut, vsi->rss_table_size,
1455					 vsi->rss_size);
1456	}
1457
1458	ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1459	kfree(lut);
1460}
1461
1462/**
1463 * ice_vsi_cfg_crc_strip - Configure CRC stripping for a VSI
1464 * @vsi: VSI to be configured
1465 * @disable: set to true to have FCS / CRC in the frame data
1466 */
1467void ice_vsi_cfg_crc_strip(struct ice_vsi *vsi, bool disable)
1468{
1469	int i;
1470
1471	ice_for_each_rxq(vsi, i)
1472		if (disable)
1473			vsi->rx_rings[i]->flags |= ICE_RX_FLAGS_CRC_STRIP_DIS;
1474		else
1475			vsi->rx_rings[i]->flags &= ~ICE_RX_FLAGS_CRC_STRIP_DIS;
1476}
1477
1478/**
1479 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1480 * @vsi: VSI to be configured
1481 */
1482int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1483{
1484	struct ice_pf *pf = vsi->back;
1485	struct device *dev;
1486	u8 *lut, *key;
1487	int err;
1488
1489	dev = ice_pf_to_dev(pf);
1490	if (vsi->type == ICE_VSI_PF && vsi->ch_rss_size &&
1491	    (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))) {
1492		vsi->rss_size = min_t(u16, vsi->rss_size, vsi->ch_rss_size);
1493	} else {
1494		vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1495
1496		/* If orig_rss_size is valid and it is less than determined
1497		 * main VSI's rss_size, update main VSI's rss_size to be
1498		 * orig_rss_size so that when tc-qdisc is deleted, main VSI
1499		 * RSS table gets programmed to be correct (whatever it was
1500		 * to begin with (prior to setup-tc for ADQ config)
1501		 */
1502		if (vsi->orig_rss_size && vsi->rss_size < vsi->orig_rss_size &&
1503		    vsi->orig_rss_size <= vsi->num_rxq) {
1504			vsi->rss_size = vsi->orig_rss_size;
1505			/* now orig_rss_size is used, reset it to zero */
1506			vsi->orig_rss_size = 0;
1507		}
1508	}
1509
1510	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1511	if (!lut)
1512		return -ENOMEM;
1513
1514	if (vsi->rss_lut_user)
1515		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1516	else
1517		ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1518
1519	err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1520	if (err) {
1521		dev_err(dev, "set_rss_lut failed, error %d\n", err);
1522		goto ice_vsi_cfg_rss_exit;
1523	}
1524
1525	key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1526	if (!key) {
1527		err = -ENOMEM;
1528		goto ice_vsi_cfg_rss_exit;
1529	}
1530
1531	if (vsi->rss_hkey_user)
1532		memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1533	else
1534		netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1535
1536	err = ice_set_rss_key(vsi, key);
1537	if (err)
1538		dev_err(dev, "set_rss_key failed, error %d\n", err);
1539
1540	kfree(key);
1541ice_vsi_cfg_rss_exit:
1542	kfree(lut);
1543	return err;
1544}
1545
1546/**
1547 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1548 * @vsi: VSI to be configured
1549 *
1550 * This function will only be called during the VF VSI setup. Upon successful
1551 * completion of package download, this function will configure default RSS
1552 * input sets for VF VSI.
1553 */
1554static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1555{
1556	struct ice_pf *pf = vsi->back;
 
1557	struct device *dev;
1558	int status;
1559
1560	dev = ice_pf_to_dev(pf);
1561	if (ice_is_safe_mode(pf)) {
1562		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1563			vsi->vsi_num);
1564		return;
1565	}
1566
1567	status = ice_add_avf_rss_cfg(&pf->hw, vsi, ICE_DEFAULT_RSS_HENA);
1568	if (status)
1569		dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %d\n",
1570			vsi->vsi_num, status);
1571}
1572
1573static const struct ice_rss_hash_cfg default_rss_cfgs[] = {
1574	/* configure RSS for IPv4 with input set IP src/dst */
1575	{ICE_FLOW_SEG_HDR_IPV4, ICE_FLOW_HASH_IPV4, ICE_RSS_ANY_HEADERS, false},
1576	/* configure RSS for IPv6 with input set IPv6 src/dst */
1577	{ICE_FLOW_SEG_HDR_IPV6, ICE_FLOW_HASH_IPV6, ICE_RSS_ANY_HEADERS, false},
1578	/* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1579	{ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4,
1580				ICE_HASH_TCP_IPV4,  ICE_RSS_ANY_HEADERS, false},
1581	/* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1582	{ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4,
1583				ICE_HASH_UDP_IPV4,  ICE_RSS_ANY_HEADERS, false},
1584	/* configure RSS for sctp4 with input set IP src/dst - only support
1585	 * RSS on SCTPv4 on outer headers (non-tunneled)
1586	 */
1587	{ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4,
1588		ICE_HASH_SCTP_IPV4, ICE_RSS_OUTER_HEADERS, false},
1589	/* configure RSS for gtpc4 with input set IPv4 src/dst */
1590	{ICE_FLOW_SEG_HDR_GTPC | ICE_FLOW_SEG_HDR_IPV4,
1591		ICE_FLOW_HASH_IPV4, ICE_RSS_OUTER_HEADERS, false},
1592	/* configure RSS for gtpc4t with input set IPv4 src/dst */
1593	{ICE_FLOW_SEG_HDR_GTPC_TEID | ICE_FLOW_SEG_HDR_IPV4,
1594		ICE_FLOW_HASH_GTP_C_IPV4_TEID, ICE_RSS_OUTER_HEADERS, false},
1595	/* configure RSS for gtpu4 with input set IPv4 src/dst */
1596	{ICE_FLOW_SEG_HDR_GTPU_IP | ICE_FLOW_SEG_HDR_IPV4,
1597		ICE_FLOW_HASH_GTP_U_IPV4_TEID, ICE_RSS_OUTER_HEADERS, false},
1598	/* configure RSS for gtpu4e with input set IPv4 src/dst */
1599	{ICE_FLOW_SEG_HDR_GTPU_EH | ICE_FLOW_SEG_HDR_IPV4,
1600		ICE_FLOW_HASH_GTP_U_IPV4_EH, ICE_RSS_OUTER_HEADERS, false},
1601	/* configure RSS for gtpu4u with input set IPv4 src/dst */
1602	{ ICE_FLOW_SEG_HDR_GTPU_UP | ICE_FLOW_SEG_HDR_IPV4,
1603		ICE_FLOW_HASH_GTP_U_IPV4_UP, ICE_RSS_OUTER_HEADERS, false},
1604	/* configure RSS for gtpu4d with input set IPv4 src/dst */
1605	{ICE_FLOW_SEG_HDR_GTPU_DWN | ICE_FLOW_SEG_HDR_IPV4,
1606		ICE_FLOW_HASH_GTP_U_IPV4_DWN, ICE_RSS_OUTER_HEADERS, false},
1607
1608	/* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1609	{ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6,
1610				ICE_HASH_TCP_IPV6,  ICE_RSS_ANY_HEADERS, false},
1611	/* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1612	{ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6,
1613				ICE_HASH_UDP_IPV6,  ICE_RSS_ANY_HEADERS, false},
1614	/* configure RSS for sctp6 with input set IPv6 src/dst - only support
1615	 * RSS on SCTPv6 on outer headers (non-tunneled)
1616	 */
1617	{ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6,
1618		ICE_HASH_SCTP_IPV6, ICE_RSS_OUTER_HEADERS, false},
1619	/* configure RSS for IPSEC ESP SPI with input set MAC_IPV4_SPI */
1620	{ICE_FLOW_SEG_HDR_ESP,
1621		ICE_FLOW_HASH_ESP_SPI, ICE_RSS_OUTER_HEADERS, false},
1622	/* configure RSS for gtpc6 with input set IPv6 src/dst */
1623	{ICE_FLOW_SEG_HDR_GTPC | ICE_FLOW_SEG_HDR_IPV6,
1624		ICE_FLOW_HASH_IPV6, ICE_RSS_OUTER_HEADERS, false},
1625	/* configure RSS for gtpc6t with input set IPv6 src/dst */
1626	{ICE_FLOW_SEG_HDR_GTPC_TEID | ICE_FLOW_SEG_HDR_IPV6,
1627		ICE_FLOW_HASH_GTP_C_IPV6_TEID, ICE_RSS_OUTER_HEADERS, false},
1628	/* configure RSS for gtpu6 with input set IPv6 src/dst */
1629	{ICE_FLOW_SEG_HDR_GTPU_IP | ICE_FLOW_SEG_HDR_IPV6,
1630		ICE_FLOW_HASH_GTP_U_IPV6_TEID, ICE_RSS_OUTER_HEADERS, false},
1631	/* configure RSS for gtpu6e with input set IPv6 src/dst */
1632	{ICE_FLOW_SEG_HDR_GTPU_EH | ICE_FLOW_SEG_HDR_IPV6,
1633		ICE_FLOW_HASH_GTP_U_IPV6_EH, ICE_RSS_OUTER_HEADERS, false},
1634	/* configure RSS for gtpu6u with input set IPv6 src/dst */
1635	{ ICE_FLOW_SEG_HDR_GTPU_UP | ICE_FLOW_SEG_HDR_IPV6,
1636		ICE_FLOW_HASH_GTP_U_IPV6_UP, ICE_RSS_OUTER_HEADERS, false},
1637	/* configure RSS for gtpu6d with input set IPv6 src/dst */
1638	{ICE_FLOW_SEG_HDR_GTPU_DWN | ICE_FLOW_SEG_HDR_IPV6,
1639		ICE_FLOW_HASH_GTP_U_IPV6_DWN, ICE_RSS_OUTER_HEADERS, false},
1640};
1641
1642/**
1643 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1644 * @vsi: VSI to be configured
1645 *
1646 * This function will only be called after successful download package call
1647 * during initialization of PF. Since the downloaded package will erase the
1648 * RSS section, this function will configure RSS input sets for different
1649 * flow types. The last profile added has the highest priority, therefore 2
1650 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1651 * (i.e. IPv4 src/dst TCP src/dst port).
1652 */
1653static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1654{
1655	u16 vsi_num = vsi->vsi_num;
1656	struct ice_pf *pf = vsi->back;
1657	struct ice_hw *hw = &pf->hw;
 
1658	struct device *dev;
1659	int status;
1660	u32 i;
1661
1662	dev = ice_pf_to_dev(pf);
1663	if (ice_is_safe_mode(pf)) {
1664		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1665			vsi_num);
1666		return;
1667	}
1668	for (i = 0; i < ARRAY_SIZE(default_rss_cfgs); i++) {
1669		const struct ice_rss_hash_cfg *cfg = &default_rss_cfgs[i];
 
 
 
 
1670
1671		status = ice_add_rss_cfg(hw, vsi, cfg);
1672		if (status)
1673			dev_dbg(dev, "ice_add_rss_cfg failed, addl_hdrs = %x, hash_flds = %llx, hdr_type = %d, symm = %d\n",
1674				cfg->addl_hdrs, cfg->hash_flds,
1675				cfg->hdr_type, cfg->symm);
1676	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1677}
1678
1679/**
1680 * ice_pf_state_is_nominal - checks the PF for nominal state
1681 * @pf: pointer to PF to check
1682 *
1683 * Check the PF's state for a collection of bits that would indicate
1684 * the PF is in a state that would inhibit normal operation for
1685 * driver functionality.
1686 *
1687 * Returns true if PF is in a nominal state, false otherwise
1688 */
1689bool ice_pf_state_is_nominal(struct ice_pf *pf)
1690{
1691	DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1692
1693	if (!pf)
1694		return false;
1695
1696	bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1697	if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1698		return false;
1699
1700	return true;
1701}
1702
1703/**
1704 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1705 * @vsi: the VSI to be updated
1706 */
1707void ice_update_eth_stats(struct ice_vsi *vsi)
1708{
1709	struct ice_eth_stats *prev_es, *cur_es;
1710	struct ice_hw *hw = &vsi->back->hw;
1711	struct ice_pf *pf = vsi->back;
1712	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1713
1714	prev_es = &vsi->eth_stats_prev;
1715	cur_es = &vsi->eth_stats;
1716
1717	if (ice_is_reset_in_progress(pf->state))
1718		vsi->stat_offsets_loaded = false;
1719
1720	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1721			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1722
1723	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1724			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1725
1726	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1727			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1728
1729	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1730			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1731
1732	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1733			  &prev_es->rx_discards, &cur_es->rx_discards);
1734
1735	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1736			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1737
1738	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1739			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1740
1741	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1742			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1743
1744	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1745			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1746
1747	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1748			  &prev_es->tx_errors, &cur_es->tx_errors);
1749
1750	vsi->stat_offsets_loaded = true;
1751}
1752
1753/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1754 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1755 * @hw: HW pointer
1756 * @pf_q: index of the Rx queue in the PF's queue space
1757 * @rxdid: flexible descriptor RXDID
1758 * @prio: priority for the RXDID for this queue
1759 * @ena_ts: true to enable timestamp and false to disable timestamp
1760 */
1761void
1762ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1763			bool ena_ts)
1764{
1765	int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1766
1767	/* clear any previous values */
1768	regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1769		    QRXFLXP_CNTXT_RXDID_PRIO_M |
1770		    QRXFLXP_CNTXT_TS_M);
1771
1772	regval |= FIELD_PREP(QRXFLXP_CNTXT_RXDID_IDX_M, rxdid);
1773	regval |= FIELD_PREP(QRXFLXP_CNTXT_RXDID_PRIO_M, prio);
 
 
 
1774
1775	if (ena_ts)
1776		/* Enable TimeSync on this queue */
1777		regval |= QRXFLXP_CNTXT_TS_M;
1778
1779	wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1780}
1781
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1782/**
1783 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1784 * @intrl: interrupt rate limit in usecs
1785 * @gran: interrupt rate limit granularity in usecs
1786 *
1787 * This function converts a decimal interrupt rate limit in usecs to the format
1788 * expected by firmware.
1789 */
1790static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1791{
1792	u32 val = intrl / gran;
1793
1794	if (val)
1795		return val | GLINT_RATE_INTRL_ENA_M;
1796	return 0;
1797}
1798
1799/**
1800 * ice_write_intrl - write throttle rate limit to interrupt specific register
1801 * @q_vector: pointer to interrupt specific structure
1802 * @intrl: throttle rate limit in microseconds to write
1803 */
1804void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1805{
1806	struct ice_hw *hw = &q_vector->vsi->back->hw;
1807
1808	wr32(hw, GLINT_RATE(q_vector->reg_idx),
1809	     ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1810}
1811
1812static struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc)
1813{
1814	switch (rc->type) {
1815	case ICE_RX_CONTAINER:
1816		if (rc->rx_ring)
1817			return rc->rx_ring->q_vector;
1818		break;
1819	case ICE_TX_CONTAINER:
1820		if (rc->tx_ring)
1821			return rc->tx_ring->q_vector;
1822		break;
1823	default:
1824		break;
1825	}
1826
1827	return NULL;
1828}
1829
1830/**
1831 * __ice_write_itr - write throttle rate to register
1832 * @q_vector: pointer to interrupt data structure
1833 * @rc: pointer to ring container
1834 * @itr: throttle rate in microseconds to write
1835 */
1836static void __ice_write_itr(struct ice_q_vector *q_vector,
1837			    struct ice_ring_container *rc, u16 itr)
1838{
1839	struct ice_hw *hw = &q_vector->vsi->back->hw;
1840
1841	wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1842	     ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1843}
1844
1845/**
1846 * ice_write_itr - write throttle rate to queue specific register
1847 * @rc: pointer to ring container
1848 * @itr: throttle rate in microseconds to write
1849 */
1850void ice_write_itr(struct ice_ring_container *rc, u16 itr)
1851{
1852	struct ice_q_vector *q_vector;
1853
1854	q_vector = ice_pull_qvec_from_rc(rc);
1855	if (!q_vector)
1856		return;
1857
1858	__ice_write_itr(q_vector, rc, itr);
1859}
1860
1861/**
1862 * ice_set_q_vector_intrl - set up interrupt rate limiting
1863 * @q_vector: the vector to be configured
1864 *
1865 * Interrupt rate limiting is local to the vector, not per-queue so we must
1866 * detect if either ring container has dynamic moderation enabled to decide
1867 * what to set the interrupt rate limit to via INTRL settings. In the case that
1868 * dynamic moderation is disabled on both, write the value with the cached
1869 * setting to make sure INTRL register matches the user visible value.
1870 */
1871void ice_set_q_vector_intrl(struct ice_q_vector *q_vector)
1872{
1873	if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) {
1874		/* in the case of dynamic enabled, cap each vector to no more
1875		 * than (4 us) 250,000 ints/sec, which allows low latency
1876		 * but still less than 500,000 interrupts per second, which
1877		 * reduces CPU a bit in the case of the lowest latency
1878		 * setting. The 4 here is a value in microseconds.
1879		 */
1880		ice_write_intrl(q_vector, 4);
1881	} else {
1882		ice_write_intrl(q_vector, q_vector->intrl);
1883	}
1884}
1885
1886/**
1887 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1888 * @vsi: the VSI being configured
1889 *
1890 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1891 * for the VF VSI.
1892 */
1893void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1894{
1895	struct ice_pf *pf = vsi->back;
1896	struct ice_hw *hw = &pf->hw;
1897	u16 txq = 0, rxq = 0;
1898	int i, q;
1899
1900	ice_for_each_q_vector(vsi, i) {
1901		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1902		u16 reg_idx = q_vector->reg_idx;
1903
1904		ice_cfg_itr(hw, q_vector);
1905
1906		/* Both Transmit Queue Interrupt Cause Control register
1907		 * and Receive Queue Interrupt Cause control register
1908		 * expects MSIX_INDX field to be the vector index
1909		 * within the function space and not the absolute
1910		 * vector index across PF or across device.
1911		 * For SR-IOV VF VSIs queue vector index always starts
1912		 * with 1 since first vector index(0) is used for OICR
1913		 * in VF space. Since VMDq and other PF VSIs are within
1914		 * the PF function space, use the vector index that is
1915		 * tracked for this PF.
1916		 */
1917		for (q = 0; q < q_vector->num_ring_tx; q++) {
1918			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1919					      q_vector->tx.itr_idx);
1920			txq++;
1921		}
1922
1923		for (q = 0; q < q_vector->num_ring_rx; q++) {
1924			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1925					      q_vector->rx.itr_idx);
1926			rxq++;
1927		}
1928	}
1929}
1930
1931/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1932 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
1933 * @vsi: the VSI whose rings are to be enabled
1934 *
1935 * Returns 0 on success and a negative value on error
1936 */
1937int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
1938{
1939	return ice_vsi_ctrl_all_rx_rings(vsi, true);
1940}
1941
1942/**
1943 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
1944 * @vsi: the VSI whose rings are to be disabled
1945 *
1946 * Returns 0 on success and a negative value on error
1947 */
1948int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
1949{
1950	return ice_vsi_ctrl_all_rx_rings(vsi, false);
1951}
1952
1953/**
1954 * ice_vsi_stop_tx_rings - Disable Tx rings
1955 * @vsi: the VSI being configured
1956 * @rst_src: reset source
1957 * @rel_vmvf_num: Relative ID of VF/VM
1958 * @rings: Tx ring array to be stopped
1959 * @count: number of Tx ring array elements
1960 */
1961static int
1962ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1963		      u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count)
1964{
1965	u16 q_idx;
1966
1967	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
1968		return -EINVAL;
1969
1970	for (q_idx = 0; q_idx < count; q_idx++) {
1971		struct ice_txq_meta txq_meta = { };
1972		int status;
1973
1974		if (!rings || !rings[q_idx])
1975			return -EINVAL;
1976
1977		ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
1978		status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
1979					      rings[q_idx], &txq_meta);
1980
1981		if (status)
1982			return status;
1983	}
1984
1985	return 0;
1986}
1987
1988/**
1989 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
1990 * @vsi: the VSI being configured
1991 * @rst_src: reset source
1992 * @rel_vmvf_num: Relative ID of VF/VM
1993 */
1994int
1995ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
1996			  u16 rel_vmvf_num)
1997{
1998	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
1999}
2000
2001/**
2002 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2003 * @vsi: the VSI being configured
2004 */
2005int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2006{
2007	return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2008}
2009
2010/**
2011 * ice_vsi_is_rx_queue_active
2012 * @vsi: the VSI being configured
2013 *
2014 * Return true if at least one queue is active.
2015 */
2016bool ice_vsi_is_rx_queue_active(struct ice_vsi *vsi)
2017{
2018	struct ice_pf *pf = vsi->back;
2019	struct ice_hw *hw = &pf->hw;
2020	int i;
2021
2022	ice_for_each_rxq(vsi, i) {
2023		u32 rx_reg;
2024		int pf_q;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2025
2026		pf_q = vsi->rxq_map[i];
2027		rx_reg = rd32(hw, QRX_CTRL(pf_q));
2028		if (rx_reg & QRX_CTRL_QENA_STAT_M)
2029			return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2030	}
2031
2032	return false;
 
 
 
 
 
 
 
2033}
2034
2035static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2036{
2037	if (!test_bit(ICE_FLAG_DCB_ENA, vsi->back->flags)) {
2038		vsi->tc_cfg.ena_tc = ICE_DFLT_TRAFFIC_CLASS;
2039		vsi->tc_cfg.numtc = 1;
2040		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2041	}
2042
2043	/* set VSI TC information based on DCB config */
2044	ice_vsi_set_dcb_tc_cfg(vsi);
2045}
2046
2047/**
2048 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2049 * @vsi: the VSI being configured
2050 * @tx: bool to determine Tx or Rx rule
2051 * @create: bool to determine create or remove Rule
2052 */
2053void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2054{
2055	int (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2056			enum ice_sw_fwd_act_type act);
2057	struct ice_pf *pf = vsi->back;
 
2058	struct device *dev;
2059	int status;
2060
2061	dev = ice_pf_to_dev(pf);
2062	eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2063
2064	if (tx) {
2065		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2066				  ICE_DROP_PACKET);
2067	} else {
2068		if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2069			status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2070							  create);
2071		} else {
2072			status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2073					  ICE_FWD_TO_VSI);
2074		}
2075	}
2076
2077	if (status)
2078		dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %d\n",
2079			create ? "adding" : "removing", tx ? "TX" : "RX",
2080			vsi->vsi_num, status);
2081}
2082
2083/**
2084 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2085 * @vsi: pointer to the VSI
2086 *
2087 * This function will allocate new scheduler aggregator now if needed and will
2088 * move specified VSI into it.
2089 */
2090static void ice_set_agg_vsi(struct ice_vsi *vsi)
2091{
2092	struct device *dev = ice_pf_to_dev(vsi->back);
2093	struct ice_agg_node *agg_node_iter = NULL;
2094	u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2095	struct ice_agg_node *agg_node = NULL;
2096	int node_offset, max_agg_nodes = 0;
2097	struct ice_port_info *port_info;
2098	struct ice_pf *pf = vsi->back;
2099	u32 agg_node_id_start = 0;
2100	int status;
2101
2102	/* create (as needed) scheduler aggregator node and move VSI into
2103	 * corresponding aggregator node
2104	 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2105	 * - VF aggregator nodes will contain VF VSI
2106	 */
2107	port_info = pf->hw.port_info;
2108	if (!port_info)
2109		return;
2110
2111	switch (vsi->type) {
2112	case ICE_VSI_CTRL:
2113	case ICE_VSI_CHNL:
2114	case ICE_VSI_LB:
2115	case ICE_VSI_PF:
2116	case ICE_VSI_SF:
2117		max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2118		agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2119		agg_node_iter = &pf->pf_agg_node[0];
2120		break;
2121	case ICE_VSI_VF:
2122		/* user can create 'n' VFs on a given PF, but since max children
2123		 * per aggregator node can be only 64. Following code handles
2124		 * aggregator(s) for VF VSIs, either selects a agg_node which
2125		 * was already created provided num_vsis < 64, otherwise
2126		 * select next available node, which will be created
2127		 */
2128		max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2129		agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2130		agg_node_iter = &pf->vf_agg_node[0];
2131		break;
2132	default:
2133		/* other VSI type, handle later if needed */
2134		dev_dbg(dev, "unexpected VSI type %s\n",
2135			ice_vsi_type_str(vsi->type));
2136		return;
2137	}
2138
2139	/* find the appropriate aggregator node */
2140	for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2141		/* see if we can find space in previously created
2142		 * node if num_vsis < 64, otherwise skip
2143		 */
2144		if (agg_node_iter->num_vsis &&
2145		    agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2146			agg_node_iter++;
2147			continue;
2148		}
2149
2150		if (agg_node_iter->valid &&
2151		    agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2152			agg_id = agg_node_iter->agg_id;
2153			agg_node = agg_node_iter;
2154			break;
2155		}
2156
2157		/* find unclaimed agg_id */
2158		if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2159			agg_id = node_offset + agg_node_id_start;
2160			agg_node = agg_node_iter;
2161			break;
2162		}
2163		/* move to next agg_node */
2164		agg_node_iter++;
2165	}
2166
2167	if (!agg_node)
2168		return;
2169
2170	/* if selected aggregator node was not created, create it */
2171	if (!agg_node->valid) {
2172		status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2173				     (u8)vsi->tc_cfg.ena_tc);
2174		if (status) {
2175			dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2176				agg_id);
2177			return;
2178		}
2179		/* aggregator node is created, store the needed info */
2180		agg_node->valid = true;
2181		agg_node->agg_id = agg_id;
2182	}
2183
2184	/* move VSI to corresponding aggregator node */
2185	status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2186				     (u8)vsi->tc_cfg.ena_tc);
2187	if (status) {
2188		dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2189			vsi->idx, agg_id);
2190		return;
2191	}
2192
2193	/* keep active children count for aggregator node */
2194	agg_node->num_vsis++;
2195
2196	/* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2197	 * to aggregator node
2198	 */
2199	vsi->agg_node = agg_node;
2200	dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2201		vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2202		vsi->agg_node->num_vsis);
2203}
2204
2205static int ice_vsi_cfg_tc_lan(struct ice_pf *pf, struct ice_vsi *vsi)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2206{
2207	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2208	struct device *dev = ice_pf_to_dev(pf);
 
 
2209	int ret, i;
2210
2211	/* configure VSI nodes based on number of queues and TC's */
2212	ice_for_each_traffic_class(i) {
2213		if (!(vsi->tc_cfg.ena_tc & BIT(i)))
2214			continue;
2215
2216		if (vsi->type == ICE_VSI_CHNL) {
2217			if (!vsi->alloc_txq && vsi->num_txq)
2218				max_txqs[i] = vsi->num_txq;
2219			else
2220				max_txqs[i] = pf->num_lan_tx;
2221		} else {
2222			max_txqs[i] = vsi->alloc_txq;
2223		}
2224
2225		if (vsi->type == ICE_VSI_PF)
2226			max_txqs[i] += vsi->num_xdp_txq;
2227	}
2228
2229	dev_dbg(dev, "vsi->tc_cfg.ena_tc = %d\n", vsi->tc_cfg.ena_tc);
2230	ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2231			      max_txqs);
2232	if (ret) {
2233		dev_err(dev, "VSI %d failed lan queue config, error %d\n",
2234			vsi->vsi_num, ret);
2235		return ret;
2236	}
2237
2238	return 0;
2239}
2240
2241/**
2242 * ice_vsi_cfg_def - configure default VSI based on the type
2243 * @vsi: pointer to VSI
2244 */
2245static int ice_vsi_cfg_def(struct ice_vsi *vsi)
2246{
2247	struct device *dev = ice_pf_to_dev(vsi->back);
2248	struct ice_pf *pf = vsi->back;
2249	int ret;
2250
2251	vsi->vsw = pf->first_sw;
 
 
2252
2253	ret = ice_vsi_alloc_def(vsi, vsi->ch);
2254	if (ret)
2255		return ret;
2256
2257	/* allocate memory for Tx/Rx ring stat pointers */
2258	ret = ice_vsi_alloc_stat_arrays(vsi);
2259	if (ret)
2260		goto unroll_vsi_alloc;
2261
2262	ice_alloc_fd_res(vsi);
2263
2264	ret = ice_vsi_get_qs(vsi);
2265	if (ret) {
2266		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2267			vsi->idx);
2268		goto unroll_vsi_alloc_stat;
2269	}
2270
2271	/* set RSS capabilities */
2272	ice_vsi_set_rss_params(vsi);
2273
2274	/* set TC configuration */
2275	ice_vsi_set_tc_cfg(vsi);
2276
2277	/* create the VSI */
2278	ret = ice_vsi_init(vsi, vsi->flags);
2279	if (ret)
2280		goto unroll_get_qs;
2281
2282	ice_vsi_init_vlan_ops(vsi);
2283
2284	switch (vsi->type) {
2285	case ICE_VSI_CTRL:
2286	case ICE_VSI_SF:
2287	case ICE_VSI_PF:
2288		ret = ice_vsi_alloc_q_vectors(vsi);
2289		if (ret)
2290			goto unroll_vsi_init;
2291
2292		ret = ice_vsi_alloc_rings(vsi);
 
 
 
 
2293		if (ret)
2294			goto unroll_vector_base;
2295
2296		ret = ice_vsi_alloc_ring_stats(vsi);
2297		if (ret)
2298			goto unroll_vector_base;
2299
2300		if (ice_is_xdp_ena_vsi(vsi)) {
2301			ret = ice_vsi_determine_xdp_res(vsi);
2302			if (ret)
2303				goto unroll_vector_base;
2304			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog,
2305						    ICE_XDP_CFG_PART);
2306			if (ret)
2307				goto unroll_vector_base;
2308		}
 
2309
2310		ice_vsi_map_rings_to_vectors(vsi);
2311
2312		vsi->stat_offsets_loaded = false;
2313
2314		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2315		if (vsi->type != ICE_VSI_CTRL)
2316			/* Do not exit if configuring RSS had an issue, at
2317			 * least receive traffic on first queue. Hence no
2318			 * need to capture return value
2319			 */
2320			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2321				ice_vsi_cfg_rss_lut_key(vsi);
2322				ice_vsi_set_rss_flow_fld(vsi);
2323			}
2324		ice_init_arfs(vsi);
2325		break;
2326	case ICE_VSI_CHNL:
2327		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2328			ice_vsi_cfg_rss_lut_key(vsi);
2329			ice_vsi_set_rss_flow_fld(vsi);
2330		}
2331		break;
2332	case ICE_VSI_VF:
2333		/* VF driver will take care of creating netdev for this type and
2334		 * map queues to vectors through Virtchnl, PF driver only
2335		 * creates a VSI and corresponding structures for bookkeeping
2336		 * purpose
2337		 */
2338		ret = ice_vsi_alloc_q_vectors(vsi);
2339		if (ret)
2340			goto unroll_vsi_init;
2341
2342		ret = ice_vsi_alloc_rings(vsi);
2343		if (ret)
2344			goto unroll_alloc_q_vector;
2345
2346		ret = ice_vsi_alloc_ring_stats(vsi);
2347		if (ret)
2348			goto unroll_vector_base;
2349
2350		vsi->stat_offsets_loaded = false;
2351
2352		/* Do not exit if configuring RSS had an issue, at least
2353		 * receive traffic on first queue. Hence no need to capture
2354		 * return value
2355		 */
2356		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2357			ice_vsi_cfg_rss_lut_key(vsi);
2358			ice_vsi_set_vf_rss_flow_fld(vsi);
2359		}
2360		break;
2361	case ICE_VSI_LB:
2362		ret = ice_vsi_alloc_rings(vsi);
2363		if (ret)
2364			goto unroll_vsi_init;
2365
2366		ret = ice_vsi_alloc_ring_stats(vsi);
2367		if (ret)
2368			goto unroll_vector_base;
2369
2370		break;
2371	default:
2372		/* clean up the resources and exit */
2373		ret = -EINVAL;
2374		goto unroll_vsi_init;
2375	}
2376
2377	return 0;
2378
2379unroll_vector_base:
2380	/* reclaim SW interrupts back to the common pool */
2381unroll_alloc_q_vector:
2382	ice_vsi_free_q_vectors(vsi);
2383unroll_vsi_init:
2384	ice_vsi_delete_from_hw(vsi);
2385unroll_get_qs:
2386	ice_vsi_put_qs(vsi);
2387unroll_vsi_alloc_stat:
2388	ice_vsi_free_stats(vsi);
2389unroll_vsi_alloc:
2390	ice_vsi_free_arrays(vsi);
2391	return ret;
2392}
2393
2394/**
2395 * ice_vsi_cfg - configure a previously allocated VSI
2396 * @vsi: pointer to VSI
2397 */
2398int ice_vsi_cfg(struct ice_vsi *vsi)
2399{
2400	struct ice_pf *pf = vsi->back;
2401	int ret;
2402
2403	if (WARN_ON(vsi->type == ICE_VSI_VF && !vsi->vf))
2404		return -EINVAL;
2405
2406	ret = ice_vsi_cfg_def(vsi);
2407	if (ret)
2408		return ret;
2409
2410	ret = ice_vsi_cfg_tc_lan(vsi->back, vsi);
2411	if (ret)
2412		ice_vsi_decfg(vsi);
2413
2414	if (vsi->type == ICE_VSI_CTRL) {
2415		if (vsi->vf) {
2416			WARN_ON(vsi->vf->ctrl_vsi_idx != ICE_NO_VSI);
2417			vsi->vf->ctrl_vsi_idx = vsi->idx;
2418		} else {
2419			WARN_ON(pf->ctrl_vsi_idx != ICE_NO_VSI);
2420			pf->ctrl_vsi_idx = vsi->idx;
2421		}
2422	}
2423
2424	return ret;
2425}
2426
2427/**
2428 * ice_vsi_decfg - remove all VSI configuration
2429 * @vsi: pointer to VSI
2430 */
2431void ice_vsi_decfg(struct ice_vsi *vsi)
2432{
2433	struct ice_pf *pf = vsi->back;
2434	int err;
2435
2436	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2437	err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
2438	if (err)
2439		dev_err(ice_pf_to_dev(pf), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
2440			vsi->vsi_num, err);
2441
2442	if (vsi->xdp_rings)
2443		/* return value check can be skipped here, it always returns
2444		 * 0 if reset is in progress
2445		 */
2446		ice_destroy_xdp_rings(vsi, ICE_XDP_CFG_PART);
2447
2448	ice_vsi_clear_rings(vsi);
2449	ice_vsi_free_q_vectors(vsi);
2450	ice_vsi_put_qs(vsi);
2451	ice_vsi_free_arrays(vsi);
2452
2453	/* SR-IOV determines needed MSIX resources all at once instead of per
2454	 * VSI since when VFs are spawned we know how many VFs there are and how
2455	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2456	 * cleared in the same manner.
2457	 */
2458
2459	if (vsi->type == ICE_VSI_VF &&
2460	    vsi->agg_node && vsi->agg_node->valid)
2461		vsi->agg_node->num_vsis--;
2462}
2463
2464/**
2465 * ice_vsi_setup - Set up a VSI by a given type
2466 * @pf: board private structure
2467 * @params: parameters to use when creating the VSI
2468 *
2469 * This allocates the sw VSI structure and its queue resources.
2470 *
2471 * Returns pointer to the successfully allocated and configured VSI sw struct on
2472 * success, NULL on failure.
2473 */
2474struct ice_vsi *
2475ice_vsi_setup(struct ice_pf *pf, struct ice_vsi_cfg_params *params)
2476{
2477	struct device *dev = ice_pf_to_dev(pf);
2478	struct ice_vsi *vsi;
2479	int ret;
2480
2481	/* ice_vsi_setup can only initialize a new VSI, and we must have
2482	 * a port_info structure for it.
2483	 */
2484	if (WARN_ON(!(params->flags & ICE_VSI_FLAG_INIT)) ||
2485	    WARN_ON(!params->port_info))
2486		return NULL;
2487
2488	vsi = ice_vsi_alloc(pf);
2489	if (!vsi) {
2490		dev_err(dev, "could not allocate VSI\n");
2491		return NULL;
 
 
2492	}
2493
2494	vsi->params = *params;
2495	ret = ice_vsi_cfg(vsi);
2496	if (ret)
2497		goto err_vsi_cfg;
2498
2499	/* Add switch rule to drop all Tx Flow Control Frames, of look up
2500	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2501	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2502	 * The rule is added once for PF VSI in order to create appropriate
2503	 * recipe, since VSI/VSI list is ignored with drop action...
2504	 * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
2505	 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2506	 * settings in the HW.
2507	 */
2508	if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF) {
2509		ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2510				 ICE_DROP_PACKET);
2511		ice_cfg_sw_lldp(vsi, true, true);
2512	}
 
2513
2514	if (!vsi->agg_node)
2515		ice_set_agg_vsi(vsi);
2516
2517	return vsi;
2518
2519err_vsi_cfg:
2520	ice_vsi_free(vsi);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2521
2522	return NULL;
2523}
2524
2525/**
2526 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2527 * @vsi: the VSI being cleaned up
2528 */
2529static void ice_vsi_release_msix(struct ice_vsi *vsi)
2530{
2531	struct ice_pf *pf = vsi->back;
2532	struct ice_hw *hw = &pf->hw;
2533	u32 txq = 0;
2534	u32 rxq = 0;
2535	int i, q;
2536
2537	ice_for_each_q_vector(vsi, i) {
2538		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2539
2540		ice_write_intrl(q_vector, 0);
2541		for (q = 0; q < q_vector->num_ring_tx; q++) {
2542			ice_write_itr(&q_vector->tx, 0);
2543			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2544			if (vsi->xdp_rings) {
2545				u32 xdp_txq = txq + vsi->num_xdp_txq;
2546
2547				wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2548			}
2549			txq++;
2550		}
2551
2552		for (q = 0; q < q_vector->num_ring_rx; q++) {
2553			ice_write_itr(&q_vector->rx, 0);
2554			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2555			rxq++;
2556		}
2557	}
2558
2559	ice_flush(hw);
2560}
2561
2562/**
2563 * ice_vsi_free_irq - Free the IRQ association with the OS
2564 * @vsi: the VSI being configured
2565 */
2566void ice_vsi_free_irq(struct ice_vsi *vsi)
2567{
2568	struct ice_pf *pf = vsi->back;
 
2569	int i;
2570
2571	if (!vsi->q_vectors || !vsi->irqs_ready)
2572		return;
2573
2574	ice_vsi_release_msix(vsi);
2575	if (vsi->type == ICE_VSI_VF)
2576		return;
2577
2578	vsi->irqs_ready = false;
2579	ice_free_cpu_rx_rmap(vsi);
2580
2581	ice_for_each_q_vector(vsi, i) {
 
2582		int irq_num;
2583
2584		irq_num = vsi->q_vectors[i]->irq.virq;
2585
2586		/* free only the irqs that were actually requested */
2587		if (!vsi->q_vectors[i] ||
2588		    !(vsi->q_vectors[i]->num_ring_tx ||
2589		      vsi->q_vectors[i]->num_ring_rx))
2590			continue;
2591
2592		/* clear the affinity notifier in the IRQ descriptor */
2593		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2594			irq_set_affinity_notifier(irq_num, NULL);
2595
2596		/* clear the affinity_hint in the IRQ descriptor */
2597		irq_update_affinity_hint(irq_num, NULL);
2598		synchronize_irq(irq_num);
2599		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2600	}
2601}
2602
2603/**
2604 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2605 * @vsi: the VSI having resources freed
2606 */
2607void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2608{
2609	int i;
2610
2611	if (!vsi->tx_rings)
2612		return;
2613
2614	ice_for_each_txq(vsi, i)
2615		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2616			ice_free_tx_ring(vsi->tx_rings[i]);
2617}
2618
2619/**
2620 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2621 * @vsi: the VSI having resources freed
2622 */
2623void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2624{
2625	int i;
2626
2627	if (!vsi->rx_rings)
2628		return;
2629
2630	ice_for_each_rxq(vsi, i)
2631		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2632			ice_free_rx_ring(vsi->rx_rings[i]);
2633}
2634
2635/**
2636 * ice_vsi_close - Shut down a VSI
2637 * @vsi: the VSI being shut down
2638 */
2639void ice_vsi_close(struct ice_vsi *vsi)
2640{
2641	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2642		ice_down(vsi);
2643
2644	ice_vsi_clear_napi_queues(vsi);
2645	ice_vsi_free_irq(vsi);
2646	ice_vsi_free_tx_rings(vsi);
2647	ice_vsi_free_rx_rings(vsi);
2648}
2649
2650/**
2651 * ice_ena_vsi - resume a VSI
2652 * @vsi: the VSI being resume
2653 * @locked: is the rtnl_lock already held
2654 */
2655int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2656{
2657	int err = 0;
2658
2659	if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2660		return 0;
2661
2662	clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2663
2664	if (vsi->netdev && (vsi->type == ICE_VSI_PF ||
2665			    vsi->type == ICE_VSI_SF)) {
2666		if (netif_running(vsi->netdev)) {
2667			if (!locked)
2668				rtnl_lock();
2669
2670			err = ice_open_internal(vsi->netdev);
2671
2672			if (!locked)
2673				rtnl_unlock();
2674		}
2675	} else if (vsi->type == ICE_VSI_CTRL) {
2676		err = ice_vsi_open_ctrl(vsi);
2677	}
2678
2679	return err;
2680}
2681
2682/**
2683 * ice_dis_vsi - pause a VSI
2684 * @vsi: the VSI being paused
2685 * @locked: is the rtnl_lock already held
2686 */
2687void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2688{
2689	bool already_down = test_bit(ICE_VSI_DOWN, vsi->state);
 
2690
2691	set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2692
2693	if (vsi->netdev && (vsi->type == ICE_VSI_PF ||
2694			    vsi->type == ICE_VSI_SF)) {
2695		if (netif_running(vsi->netdev)) {
2696			if (!locked)
2697				rtnl_lock();
2698			already_down = test_bit(ICE_VSI_DOWN, vsi->state);
2699			if (!already_down)
2700				ice_vsi_close(vsi);
2701
2702			if (!locked)
2703				rtnl_unlock();
2704		} else if (!already_down) {
2705			ice_vsi_close(vsi);
2706		}
2707	} else if (vsi->type == ICE_VSI_CTRL && !already_down) {
2708		ice_vsi_close(vsi);
2709	}
2710}
2711
2712/**
2713 * ice_vsi_set_napi_queues - associate netdev queues with napi
2714 * @vsi: VSI pointer
2715 *
2716 * Associate queue[s] with napi for all vectors.
2717 * The caller must hold rtnl_lock.
2718 */
2719void ice_vsi_set_napi_queues(struct ice_vsi *vsi)
2720{
2721	struct net_device *netdev = vsi->netdev;
2722	int q_idx, v_idx;
 
 
 
2723
2724	if (!netdev)
2725		return;
 
 
 
2726
2727	ice_for_each_rxq(vsi, q_idx)
2728		netif_queue_set_napi(netdev, q_idx, NETDEV_QUEUE_TYPE_RX,
2729				     &vsi->rx_rings[q_idx]->q_vector->napi);
 
 
 
 
2730
2731	ice_for_each_txq(vsi, q_idx)
2732		netif_queue_set_napi(netdev, q_idx, NETDEV_QUEUE_TYPE_TX,
2733				     &vsi->tx_rings[q_idx]->q_vector->napi);
2734	/* Also set the interrupt number for the NAPI */
2735	ice_for_each_q_vector(vsi, v_idx) {
2736		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2737
2738		netif_napi_set_irq(&q_vector->napi, q_vector->irq.virq);
 
 
 
 
 
2739	}
2740}
2741
2742/**
2743 * ice_vsi_clear_napi_queues - dissociate netdev queues from napi
2744 * @vsi: VSI pointer
2745 *
2746 * Clear the association between all VSI queues queue[s] and napi.
2747 * The caller must hold rtnl_lock.
2748 */
2749void ice_vsi_clear_napi_queues(struct ice_vsi *vsi)
2750{
2751	struct net_device *netdev = vsi->netdev;
2752	int q_idx;
2753
2754	if (!netdev)
2755		return;
2756
2757	ice_for_each_txq(vsi, q_idx)
2758		netif_queue_set_napi(netdev, q_idx, NETDEV_QUEUE_TYPE_TX, NULL);
 
2759
2760	ice_for_each_rxq(vsi, q_idx)
2761		netif_queue_set_napi(netdev, q_idx, NETDEV_QUEUE_TYPE_RX, NULL);
2762}
2763
2764/**
2765 * ice_napi_add - register NAPI handler for the VSI
2766 * @vsi: VSI for which NAPI handler is to be registered
2767 *
2768 * This function is only called in the driver's load path. Registering the NAPI
2769 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
2770 * reset/rebuild, etc.)
2771 */
2772void ice_napi_add(struct ice_vsi *vsi)
2773{
2774	int v_idx;
2775
2776	if (!vsi->netdev)
2777		return;
2778
2779	ice_for_each_q_vector(vsi, v_idx)
2780		netif_napi_add_config(vsi->netdev,
2781				      &vsi->q_vectors[v_idx]->napi,
2782				      ice_napi_poll,
2783				      v_idx);
2784}
2785
2786/**
2787 * ice_vsi_release - Delete a VSI and free its resources
2788 * @vsi: the VSI being removed
2789 *
2790 * Returns 0 on success or < 0 on error
2791 */
2792int ice_vsi_release(struct ice_vsi *vsi)
2793{
 
2794	struct ice_pf *pf;
2795
2796	if (!vsi->back)
2797		return -ENODEV;
2798	pf = vsi->back;
2799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2800	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2801		ice_rss_clean(vsi);
2802
 
 
 
2803	ice_vsi_close(vsi);
2804
2805	/* The Rx rule will only exist to remove if the LLDP FW
2806	 * engine is currently stopped
 
 
2807	 */
2808	if (!ice_is_safe_mode(pf) && vsi->type == ICE_VSI_PF &&
2809	    !test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2810		ice_cfg_sw_lldp(vsi, false, false);
 
 
2811
2812	ice_vsi_decfg(vsi);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2813
2814	/* retain SW VSI data structure since it is needed to unregister and
2815	 * free VSI netdev when PF is not in reset recovery pending state,\
2816	 * for ex: during rmmod.
2817	 */
2818	if (!ice_is_reset_in_progress(pf->state))
2819		ice_vsi_delete(vsi);
2820
2821	return 0;
2822}
2823
2824/**
2825 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2826 * @vsi: VSI connected with q_vectors
2827 * @coalesce: array of struct with stored coalesce
2828 *
2829 * Returns array size.
2830 */
2831static int
2832ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2833			     struct ice_coalesce_stored *coalesce)
2834{
2835	int i;
2836
2837	ice_for_each_q_vector(vsi, i) {
2838		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2839
2840		coalesce[i].itr_tx = q_vector->tx.itr_settings;
2841		coalesce[i].itr_rx = q_vector->rx.itr_settings;
2842		coalesce[i].intrl = q_vector->intrl;
2843
2844		if (i < vsi->num_txq)
2845			coalesce[i].tx_valid = true;
2846		if (i < vsi->num_rxq)
2847			coalesce[i].rx_valid = true;
2848	}
2849
2850	return vsi->num_q_vectors;
2851}
2852
2853/**
2854 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2855 * @vsi: VSI connected with q_vectors
2856 * @coalesce: pointer to array of struct with stored coalesce
2857 * @size: size of coalesce array
2858 *
2859 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2860 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2861 * to default value.
2862 */
2863static void
2864ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2865			     struct ice_coalesce_stored *coalesce, int size)
2866{
2867	struct ice_ring_container *rc;
2868	int i;
2869
2870	if ((size && !coalesce) || !vsi)
2871		return;
2872
2873	/* There are a couple of cases that have to be handled here:
2874	 *   1. The case where the number of queue vectors stays the same, but
2875	 *      the number of Tx or Rx rings changes (the first for loop)
2876	 *   2. The case where the number of queue vectors increased (the
2877	 *      second for loop)
2878	 */
2879	for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
2880		/* There are 2 cases to handle here and they are the same for
2881		 * both Tx and Rx:
2882		 *   if the entry was valid previously (coalesce[i].[tr]x_valid
2883		 *   and the loop variable is less than the number of rings
2884		 *   allocated, then write the previous values
2885		 *
2886		 *   if the entry was not valid previously, but the number of
2887		 *   rings is less than are allocated (this means the number of
2888		 *   rings increased from previously), then write out the
2889		 *   values in the first element
2890		 *
2891		 *   Also, always write the ITR, even if in ITR_IS_DYNAMIC
2892		 *   as there is no harm because the dynamic algorithm
2893		 *   will just overwrite.
2894		 */
2895		if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
2896			rc = &vsi->q_vectors[i]->rx;
2897			rc->itr_settings = coalesce[i].itr_rx;
2898			ice_write_itr(rc, rc->itr_setting);
2899		} else if (i < vsi->alloc_rxq) {
2900			rc = &vsi->q_vectors[i]->rx;
2901			rc->itr_settings = coalesce[0].itr_rx;
2902			ice_write_itr(rc, rc->itr_setting);
2903		}
2904
2905		if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
2906			rc = &vsi->q_vectors[i]->tx;
2907			rc->itr_settings = coalesce[i].itr_tx;
2908			ice_write_itr(rc, rc->itr_setting);
2909		} else if (i < vsi->alloc_txq) {
2910			rc = &vsi->q_vectors[i]->tx;
2911			rc->itr_settings = coalesce[0].itr_tx;
2912			ice_write_itr(rc, rc->itr_setting);
2913		}
2914
2915		vsi->q_vectors[i]->intrl = coalesce[i].intrl;
2916		ice_set_q_vector_intrl(vsi->q_vectors[i]);
2917	}
2918
2919	/* the number of queue vectors increased so write whatever is in
2920	 * the first element
2921	 */
2922	for (; i < vsi->num_q_vectors; i++) {
2923		/* transmit */
2924		rc = &vsi->q_vectors[i]->tx;
2925		rc->itr_settings = coalesce[0].itr_tx;
2926		ice_write_itr(rc, rc->itr_setting);
2927
2928		/* receive */
2929		rc = &vsi->q_vectors[i]->rx;
2930		rc->itr_settings = coalesce[0].itr_rx;
2931		ice_write_itr(rc, rc->itr_setting);
2932
2933		vsi->q_vectors[i]->intrl = coalesce[0].intrl;
2934		ice_set_q_vector_intrl(vsi->q_vectors[i]);
2935	}
2936}
2937
2938/**
2939 * ice_vsi_realloc_stat_arrays - Frees unused stat structures or alloc new ones
2940 * @vsi: VSI pointer
2941 */
2942static int
2943ice_vsi_realloc_stat_arrays(struct ice_vsi *vsi)
2944{
2945	u16 req_txq = vsi->req_txq ? vsi->req_txq : vsi->alloc_txq;
2946	u16 req_rxq = vsi->req_rxq ? vsi->req_rxq : vsi->alloc_rxq;
2947	struct ice_ring_stats **tx_ring_stats;
2948	struct ice_ring_stats **rx_ring_stats;
2949	struct ice_vsi_stats *vsi_stat;
2950	struct ice_pf *pf = vsi->back;
2951	u16 prev_txq = vsi->alloc_txq;
2952	u16 prev_rxq = vsi->alloc_rxq;
2953	int i;
2954
2955	vsi_stat = pf->vsi_stats[vsi->idx];
2956
2957	if (req_txq < prev_txq) {
2958		for (i = req_txq; i < prev_txq; i++) {
2959			if (vsi_stat->tx_ring_stats[i]) {
2960				kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
2961				WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
2962			}
2963		}
2964	}
2965
2966	tx_ring_stats = vsi_stat->tx_ring_stats;
2967	vsi_stat->tx_ring_stats =
2968		krealloc_array(vsi_stat->tx_ring_stats, req_txq,
2969			       sizeof(*vsi_stat->tx_ring_stats),
2970			       GFP_KERNEL | __GFP_ZERO);
2971	if (!vsi_stat->tx_ring_stats) {
2972		vsi_stat->tx_ring_stats = tx_ring_stats;
2973		return -ENOMEM;
2974	}
2975
2976	if (req_rxq < prev_rxq) {
2977		for (i = req_rxq; i < prev_rxq; i++) {
2978			if (vsi_stat->rx_ring_stats[i]) {
2979				kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
2980				WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
2981			}
2982		}
2983	}
2984
2985	rx_ring_stats = vsi_stat->rx_ring_stats;
2986	vsi_stat->rx_ring_stats =
2987		krealloc_array(vsi_stat->rx_ring_stats, req_rxq,
2988			       sizeof(*vsi_stat->rx_ring_stats),
2989			       GFP_KERNEL | __GFP_ZERO);
2990	if (!vsi_stat->rx_ring_stats) {
2991		vsi_stat->rx_ring_stats = rx_ring_stats;
2992		return -ENOMEM;
2993	}
2994
2995	return 0;
2996}
2997
2998/**
2999 * ice_vsi_rebuild - Rebuild VSI after reset
3000 * @vsi: VSI to be rebuild
3001 * @vsi_flags: flags used for VSI rebuild flow
3002 *
3003 * Set vsi_flags to ICE_VSI_FLAG_INIT to initialize a new VSI, or
3004 * ICE_VSI_FLAG_NO_INIT to rebuild an existing VSI in hardware.
3005 *
3006 * Returns 0 on success and negative value on failure
3007 */
3008int ice_vsi_rebuild(struct ice_vsi *vsi, u32 vsi_flags)
3009{
 
3010	struct ice_coalesce_stored *coalesce;
3011	int prev_num_q_vectors;
 
 
 
3012	struct ice_pf *pf;
3013	int ret;
3014
3015	if (!vsi)
3016		return -EINVAL;
3017
3018	vsi->flags = vsi_flags;
3019	pf = vsi->back;
3020	if (WARN_ON(vsi->type == ICE_VSI_VF && !vsi->vf))
3021		return -EINVAL;
 
3022
3023	mutex_lock(&vsi->xdp_state_lock);
 
 
 
3024
3025	ret = ice_vsi_realloc_stat_arrays(vsi);
3026	if (ret)
3027		goto unlock;
3028
3029	ice_vsi_decfg(vsi);
3030	ret = ice_vsi_cfg_def(vsi);
3031	if (ret)
3032		goto unlock;
 
 
3033
3034	coalesce = kcalloc(vsi->num_q_vectors,
3035			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3036	if (!coalesce) {
3037		ret = -ENOMEM;
3038		goto decfg;
 
 
 
 
 
3039	}
3040
3041	prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
 
 
 
 
 
 
 
 
 
 
 
3042
3043	ret = ice_vsi_cfg_tc_lan(pf, vsi);
3044	if (ret) {
3045		if (vsi_flags & ICE_VSI_FLAG_INIT) {
3046			ret = -EIO;
3047			goto free_coalesce;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3048		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3049
3050		ret = ice_schedule_reset(pf, ICE_RESET_PFR);
3051		goto free_coalesce;
3052	}
3053
 
 
 
 
 
 
 
 
 
 
 
 
3054	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3055	clear_bit(ICE_VSI_REBUILD_PENDING, vsi->state);
3056
3057free_coalesce:
 
 
 
 
 
 
 
 
 
 
 
 
 
3058	kfree(coalesce);
3059decfg:
3060	if (ret)
3061		ice_vsi_decfg(vsi);
3062unlock:
3063	mutex_unlock(&vsi->xdp_state_lock);
3064	return ret;
3065}
3066
3067/**
3068 * ice_is_reset_in_progress - check for a reset in progress
3069 * @state: PF state field
3070 */
3071bool ice_is_reset_in_progress(unsigned long *state)
3072{
3073	return test_bit(ICE_RESET_OICR_RECV, state) ||
3074	       test_bit(ICE_PFR_REQ, state) ||
3075	       test_bit(ICE_CORER_REQ, state) ||
3076	       test_bit(ICE_GLOBR_REQ, state);
3077}
3078
3079/**
3080 * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3081 * @pf: pointer to the PF structure
3082 * @timeout: length of time to wait, in jiffies
3083 *
3084 * Wait (sleep) for a short time until the driver finishes cleaning up from
3085 * a device reset. The caller must be able to sleep. Use this to delay
3086 * operations that could fail while the driver is cleaning up after a device
3087 * reset.
3088 *
3089 * Returns 0 on success, -EBUSY if the reset is not finished within the
3090 * timeout, and -ERESTARTSYS if the thread was interrupted.
3091 */
3092int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3093{
3094	long ret;
3095
3096	ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3097					       !ice_is_reset_in_progress(pf->state),
3098					       timeout);
3099	if (ret < 0)
3100		return ret;
3101	else if (!ret)
3102		return -EBUSY;
3103	else
3104		return 0;
3105}
3106
 
3107/**
3108 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3109 * @vsi: VSI being configured
3110 * @ctx: the context buffer returned from AQ VSI update command
3111 */
3112static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3113{
3114	vsi->info.mapping_flags = ctx->info.mapping_flags;
3115	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3116	       sizeof(vsi->info.q_mapping));
3117	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3118	       sizeof(vsi->info.tc_mapping));
3119}
3120
3121/**
3122 * ice_vsi_cfg_netdev_tc - Setup the netdev TC configuration
3123 * @vsi: the VSI being configured
3124 * @ena_tc: TC map to be enabled
3125 */
3126void ice_vsi_cfg_netdev_tc(struct ice_vsi *vsi, u8 ena_tc)
3127{
3128	struct net_device *netdev = vsi->netdev;
3129	struct ice_pf *pf = vsi->back;
3130	int numtc = vsi->tc_cfg.numtc;
3131	struct ice_dcbx_cfg *dcbcfg;
3132	u8 netdev_tc;
3133	int i;
3134
3135	if (!netdev)
3136		return;
3137
3138	/* CHNL VSI doesn't have it's own netdev, hence, no netdev_tc */
3139	if (vsi->type == ICE_VSI_CHNL)
3140		return;
3141
3142	if (!ena_tc) {
3143		netdev_reset_tc(netdev);
3144		return;
3145	}
3146
3147	if (vsi->type == ICE_VSI_PF && ice_is_adq_active(pf))
3148		numtc = vsi->all_numtc;
3149
3150	if (netdev_set_num_tc(netdev, numtc))
3151		return;
3152
3153	dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg;
3154
3155	ice_for_each_traffic_class(i)
3156		if (vsi->tc_cfg.ena_tc & BIT(i))
3157			netdev_set_tc_queue(netdev,
3158					    vsi->tc_cfg.tc_info[i].netdev_tc,
3159					    vsi->tc_cfg.tc_info[i].qcount_tx,
3160					    vsi->tc_cfg.tc_info[i].qoffset);
3161	/* setup TC queue map for CHNL TCs */
3162	ice_for_each_chnl_tc(i) {
3163		if (!(vsi->all_enatc & BIT(i)))
3164			break;
3165		if (!vsi->mqprio_qopt.qopt.count[i])
3166			break;
3167		netdev_set_tc_queue(netdev, i,
3168				    vsi->mqprio_qopt.qopt.count[i],
3169				    vsi->mqprio_qopt.qopt.offset[i]);
3170	}
3171
3172	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3173		return;
3174
3175	for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) {
3176		u8 ets_tc = dcbcfg->etscfg.prio_table[i];
3177
3178		/* Get the mapped netdev TC# for the UP */
3179		netdev_tc = vsi->tc_cfg.tc_info[ets_tc].netdev_tc;
3180		netdev_set_prio_tc_map(netdev, i, netdev_tc);
3181	}
3182}
3183
3184/**
3185 * ice_vsi_setup_q_map_mqprio - Prepares mqprio based tc_config
3186 * @vsi: the VSI being configured,
3187 * @ctxt: VSI context structure
3188 * @ena_tc: number of traffic classes to enable
3189 *
3190 * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
3191 */
3192static int
3193ice_vsi_setup_q_map_mqprio(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt,
3194			   u8 ena_tc)
3195{
3196	u16 pow, offset = 0, qcount_tx = 0, qcount_rx = 0, qmap;
3197	u16 tc0_offset = vsi->mqprio_qopt.qopt.offset[0];
3198	int tc0_qcount = vsi->mqprio_qopt.qopt.count[0];
3199	u16 new_txq, new_rxq;
3200	u8 netdev_tc = 0;
3201	int i;
3202
3203	vsi->tc_cfg.ena_tc = ena_tc ? ena_tc : 1;
3204
3205	pow = order_base_2(tc0_qcount);
3206	qmap = FIELD_PREP(ICE_AQ_VSI_TC_Q_OFFSET_M, tc0_offset);
3207	qmap |= FIELD_PREP(ICE_AQ_VSI_TC_Q_NUM_M, pow);
3208
3209	ice_for_each_traffic_class(i) {
3210		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
3211			/* TC is not enabled */
3212			vsi->tc_cfg.tc_info[i].qoffset = 0;
3213			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
3214			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
3215			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
3216			ctxt->info.tc_mapping[i] = 0;
3217			continue;
3218		}
3219
3220		offset = vsi->mqprio_qopt.qopt.offset[i];
3221		qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3222		qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3223		vsi->tc_cfg.tc_info[i].qoffset = offset;
3224		vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
3225		vsi->tc_cfg.tc_info[i].qcount_tx = qcount_tx;
3226		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
3227	}
3228
3229	if (vsi->all_numtc && vsi->all_numtc != vsi->tc_cfg.numtc) {
3230		ice_for_each_chnl_tc(i) {
3231			if (!(vsi->all_enatc & BIT(i)))
3232				continue;
3233			offset = vsi->mqprio_qopt.qopt.offset[i];
3234			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
3235			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
3236		}
3237	}
3238
3239	new_txq = offset + qcount_tx;
3240	if (new_txq > vsi->alloc_txq) {
3241		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Tx queues (%u), than were allocated (%u)!\n",
3242			new_txq, vsi->alloc_txq);
3243		return -EINVAL;
3244	}
3245
3246	new_rxq = offset + qcount_rx;
3247	if (new_rxq > vsi->alloc_rxq) {
3248		dev_err(ice_pf_to_dev(vsi->back), "Trying to use more Rx queues (%u), than were allocated (%u)!\n",
3249			new_rxq, vsi->alloc_rxq);
3250		return -EINVAL;
3251	}
3252
3253	/* Set actual Tx/Rx queue pairs */
3254	vsi->num_txq = new_txq;
3255	vsi->num_rxq = new_rxq;
3256
3257	/* Setup queue TC[0].qmap for given VSI context */
3258	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
3259	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
3260	ctxt->info.q_mapping[1] = cpu_to_le16(tc0_qcount);
3261
3262	/* Find queue count available for channel VSIs and starting offset
3263	 * for channel VSIs
3264	 */
3265	if (tc0_qcount && tc0_qcount < vsi->num_rxq) {
3266		vsi->cnt_q_avail = vsi->num_rxq - tc0_qcount;
3267		vsi->next_base_q = tc0_qcount;
3268	}
3269	dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_txq = %d\n",  vsi->num_txq);
3270	dev_dbg(ice_pf_to_dev(vsi->back), "vsi->num_rxq = %d\n",  vsi->num_rxq);
3271	dev_dbg(ice_pf_to_dev(vsi->back), "all_numtc %u, all_enatc: 0x%04x, tc_cfg.numtc %u\n",
3272		vsi->all_numtc, vsi->all_enatc, vsi->tc_cfg.numtc);
3273
3274	return 0;
3275}
3276
3277/**
3278 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3279 * @vsi: VSI to be configured
3280 * @ena_tc: TC bitmap
3281 *
3282 * VSI queues expected to be quiesced before calling this function
3283 */
3284int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3285{
3286	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3287	struct ice_pf *pf = vsi->back;
3288	struct ice_tc_cfg old_tc_cfg;
3289	struct ice_vsi_ctx *ctx;
 
3290	struct device *dev;
3291	int i, ret = 0;
3292	u8 num_tc = 0;
3293
3294	dev = ice_pf_to_dev(pf);
3295	if (vsi->tc_cfg.ena_tc == ena_tc &&
3296	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
3297		return 0;
3298
3299	ice_for_each_traffic_class(i) {
3300		/* build bitmap of enabled TCs */
3301		if (ena_tc & BIT(i))
3302			num_tc++;
3303		/* populate max_txqs per TC */
3304		max_txqs[i] = vsi->alloc_txq;
3305		/* Update max_txqs if it is CHNL VSI, because alloc_t[r]xq are
3306		 * zero for CHNL VSI, hence use num_txq instead as max_txqs
3307		 */
3308		if (vsi->type == ICE_VSI_CHNL &&
3309		    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3310			max_txqs[i] = vsi->num_txq;
3311	}
3312
3313	memcpy(&old_tc_cfg, &vsi->tc_cfg, sizeof(old_tc_cfg));
3314	vsi->tc_cfg.ena_tc = ena_tc;
3315	vsi->tc_cfg.numtc = num_tc;
3316
3317	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3318	if (!ctx)
3319		return -ENOMEM;
3320
3321	ctx->vf_num = 0;
3322	ctx->info = vsi->info;
3323
3324	if (vsi->type == ICE_VSI_PF &&
3325	    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3326		ret = ice_vsi_setup_q_map_mqprio(vsi, ctx, ena_tc);
3327	else
3328		ret = ice_vsi_setup_q_map(vsi, ctx);
3329
3330	if (ret) {
3331		memcpy(&vsi->tc_cfg, &old_tc_cfg, sizeof(vsi->tc_cfg));
3332		goto out;
3333	}
3334
3335	/* must to indicate which section of VSI context are being modified */
3336	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3337	ret = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3338	if (ret) {
3339		dev_info(dev, "Failed VSI Update\n");
 
3340		goto out;
3341	}
3342
3343	if (vsi->type == ICE_VSI_PF &&
3344	    test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
3345		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, 1, max_txqs);
3346	else
3347		ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx,
3348				      vsi->tc_cfg.ena_tc, max_txqs);
3349
3350	if (ret) {
3351		dev_err(dev, "VSI %d failed TC config, error %d\n",
3352			vsi->vsi_num, ret);
 
3353		goto out;
3354	}
3355	ice_vsi_update_q_map(vsi, ctx);
3356	vsi->info.valid_sections = 0;
3357
3358	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3359out:
3360	kfree(ctx);
3361	return ret;
3362}
 
3363
3364/**
3365 * ice_update_ring_stats - Update ring statistics
3366 * @stats: stats to be updated
3367 * @pkts: number of processed packets
3368 * @bytes: number of processed bytes
3369 *
3370 * This function assumes that caller has acquired a u64_stats_sync lock.
3371 */
3372static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3373{
3374	stats->bytes += bytes;
3375	stats->pkts += pkts;
3376}
3377
3378/**
3379 * ice_update_tx_ring_stats - Update Tx ring specific counters
3380 * @tx_ring: ring to update
3381 * @pkts: number of processed packets
3382 * @bytes: number of processed bytes
3383 */
3384void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3385{
3386	u64_stats_update_begin(&tx_ring->ring_stats->syncp);
3387	ice_update_ring_stats(&tx_ring->ring_stats->stats, pkts, bytes);
3388	u64_stats_update_end(&tx_ring->ring_stats->syncp);
3389}
3390
3391/**
3392 * ice_update_rx_ring_stats - Update Rx ring specific counters
3393 * @rx_ring: ring to update
3394 * @pkts: number of processed packets
3395 * @bytes: number of processed bytes
3396 */
3397void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3398{
3399	u64_stats_update_begin(&rx_ring->ring_stats->syncp);
3400	ice_update_ring_stats(&rx_ring->ring_stats->stats, pkts, bytes);
3401	u64_stats_update_end(&rx_ring->ring_stats->syncp);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3402}
3403
3404/**
3405 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3406 * @pi: port info of the switch with default VSI
3407 *
3408 * Return true if the there is a single VSI in default forwarding VSI list
 
3409 */
3410bool ice_is_dflt_vsi_in_use(struct ice_port_info *pi)
3411{
3412	bool exists = false;
3413
3414	ice_check_if_dflt_vsi(pi, 0, &exists);
3415	return exists;
3416}
3417
3418/**
3419 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
 
3420 * @vsi: VSI to compare against default forwarding VSI
3421 *
3422 * If this VSI passed in is the default forwarding VSI then return true, else
3423 * return false
3424 */
3425bool ice_is_vsi_dflt_vsi(struct ice_vsi *vsi)
3426{
3427	return ice_check_if_dflt_vsi(vsi->port_info, vsi->idx, NULL);
3428}
3429
3430/**
3431 * ice_set_dflt_vsi - set the default forwarding VSI
 
3432 * @vsi: VSI getting set as the default forwarding VSI on the switch
3433 *
3434 * If the VSI passed in is already the default VSI and it's enabled just return
3435 * success.
3436 *
3437 * Otherwise try to set the VSI passed in as the switch's default VSI and
3438 * return the result.
 
 
 
3439 */
3440int ice_set_dflt_vsi(struct ice_vsi *vsi)
3441{
 
3442	struct device *dev;
3443	int status;
3444
3445	if (!vsi)
3446		return -EINVAL;
3447
3448	dev = ice_pf_to_dev(vsi->back);
3449
3450	if (ice_lag_is_switchdev_running(vsi->back)) {
3451		dev_dbg(dev, "VSI %d passed is a part of LAG containing interfaces in switchdev mode, nothing to do\n",
3452			vsi->vsi_num);
3453		return 0;
3454	}
3455
3456	/* the VSI passed in is already the default VSI */
3457	if (ice_is_vsi_dflt_vsi(vsi)) {
3458		dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3459			vsi->vsi_num);
3460		return 0;
3461	}
3462
3463	status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, true, ICE_FLTR_RX);
 
 
 
 
 
 
 
3464	if (status) {
3465		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %d\n",
3466			vsi->vsi_num, status);
3467		return status;
3468	}
3469
 
 
 
3470	return 0;
3471}
3472
3473/**
3474 * ice_clear_dflt_vsi - clear the default forwarding VSI
3475 * @vsi: VSI to remove from filter list
3476 *
3477 * If the switch has no default VSI or it's not enabled then return error.
3478 *
3479 * Otherwise try to clear the default VSI and return the result.
3480 */
3481int ice_clear_dflt_vsi(struct ice_vsi *vsi)
3482{
 
 
3483	struct device *dev;
3484	int status;
3485
3486	if (!vsi)
3487		return -EINVAL;
3488
3489	dev = ice_pf_to_dev(vsi->back);
 
 
3490
3491	/* there is no default VSI configured */
3492	if (!ice_is_dflt_vsi_in_use(vsi->port_info))
3493		return -ENODEV;
3494
3495	status = ice_cfg_dflt_vsi(vsi->port_info, vsi->idx, false,
3496				  ICE_FLTR_RX);
3497	if (status) {
3498		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %d\n",
3499			vsi->vsi_num, status);
3500		return -EIO;
3501	}
3502
3503	return 0;
3504}
3505
3506/**
3507 * ice_get_link_speed_mbps - get link speed in Mbps
3508 * @vsi: the VSI whose link speed is being queried
3509 *
3510 * Return current VSI link speed and 0 if the speed is unknown.
3511 */
3512int ice_get_link_speed_mbps(struct ice_vsi *vsi)
3513{
3514	unsigned int link_speed;
3515
3516	link_speed = vsi->port_info->phy.link_info.link_speed;
3517
3518	return (int)ice_get_link_speed(fls(link_speed) - 1);
3519}
3520
3521/**
3522 * ice_get_link_speed_kbps - get link speed in Kbps
3523 * @vsi: the VSI whose link speed is being queried
3524 *
3525 * Return current VSI link speed and 0 if the speed is unknown.
3526 */
3527int ice_get_link_speed_kbps(struct ice_vsi *vsi)
3528{
3529	int speed_mbps;
3530
3531	speed_mbps = ice_get_link_speed_mbps(vsi);
3532
3533	return speed_mbps * 1000;
3534}
3535
3536/**
3537 * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
3538 * @vsi: VSI to be configured
3539 * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
3540 *
3541 * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
3542 * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
3543 * on TC 0.
3544 */
3545int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
3546{
3547	struct ice_pf *pf = vsi->back;
3548	struct device *dev;
3549	int status;
3550	int speed;
3551
3552	dev = ice_pf_to_dev(pf);
3553	if (!vsi->port_info) {
3554		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3555			vsi->idx, vsi->type);
3556		return -EINVAL;
3557	}
3558
3559	speed = ice_get_link_speed_kbps(vsi);
3560	if (min_tx_rate > (u64)speed) {
3561		dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3562			min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3563			speed);
3564		return -EINVAL;
3565	}
3566
3567	/* Configure min BW for VSI limit */
3568	if (min_tx_rate) {
3569		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3570						   ICE_MIN_BW, min_tx_rate);
3571		if (status) {
3572			dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
3573				min_tx_rate, ice_vsi_type_str(vsi->type),
3574				vsi->idx);
3575			return status;
3576		}
3577
3578		dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
3579			min_tx_rate, ice_vsi_type_str(vsi->type));
3580	} else {
3581		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3582							vsi->idx, 0,
3583							ICE_MIN_BW);
3584		if (status) {
3585			dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
3586				ice_vsi_type_str(vsi->type), vsi->idx);
3587			return status;
3588		}
3589
3590		dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
3591			ice_vsi_type_str(vsi->type), vsi->idx);
3592	}
3593
3594	return 0;
3595}
3596
3597/**
3598 * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
3599 * @vsi: VSI to be configured
3600 * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
3601 *
3602 * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
3603 * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
3604 * on TC 0.
3605 */
3606int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
3607{
3608	struct ice_pf *pf = vsi->back;
3609	struct device *dev;
3610	int status;
3611	int speed;
3612
3613	dev = ice_pf_to_dev(pf);
3614	if (!vsi->port_info) {
3615		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
3616			vsi->idx, vsi->type);
3617		return -EINVAL;
3618	}
3619
3620	speed = ice_get_link_speed_kbps(vsi);
3621	if (max_tx_rate > (u64)speed) {
3622		dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
3623			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
3624			speed);
3625		return -EINVAL;
3626	}
3627
3628	/* Configure max BW for VSI limit */
3629	if (max_tx_rate) {
3630		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
3631						   ICE_MAX_BW, max_tx_rate);
3632		if (status) {
3633			dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
3634				max_tx_rate, ice_vsi_type_str(vsi->type),
3635				vsi->idx);
3636			return status;
3637		}
3638
3639		dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
3640			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
3641	} else {
3642		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
3643							vsi->idx, 0,
3644							ICE_MAX_BW);
3645		if (status) {
3646			dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
3647				ice_vsi_type_str(vsi->type), vsi->idx);
3648			return status;
3649		}
3650
3651		dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
3652			ice_vsi_type_str(vsi->type), vsi->idx);
3653	}
3654
3655	return 0;
3656}
3657
3658/**
3659 * ice_set_link - turn on/off physical link
3660 * @vsi: VSI to modify physical link on
3661 * @ena: turn on/off physical link
3662 */
3663int ice_set_link(struct ice_vsi *vsi, bool ena)
3664{
3665	struct device *dev = ice_pf_to_dev(vsi->back);
3666	struct ice_port_info *pi = vsi->port_info;
3667	struct ice_hw *hw = pi->hw;
3668	int status;
3669
3670	if (vsi->type != ICE_VSI_PF)
3671		return -EINVAL;
3672
3673	status = ice_aq_set_link_restart_an(pi, ena, NULL);
3674
3675	/* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3676	 * this is not a fatal error, so print a warning message and return
3677	 * a success code. Return an error if FW returns an error code other
3678	 * than ICE_AQ_RC_EMODE
3679	 */
3680	if (status == -EIO) {
3681		if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3682			dev_dbg(dev, "can't set link to %s, err %d aq_err %s. not fatal, continuing\n",
3683				(ena ? "ON" : "OFF"), status,
3684				ice_aq_str(hw->adminq.sq_last_status));
3685	} else if (status) {
3686		dev_err(dev, "can't set link to %s, err %d aq_err %s\n",
3687			(ena ? "ON" : "OFF"), status,
3688			ice_aq_str(hw->adminq.sq_last_status));
3689		return status;
3690	}
3691
3692	return 0;
3693}
3694
3695/**
3696 * ice_vsi_add_vlan_zero - add VLAN 0 filter(s) for this VSI
3697 * @vsi: VSI used to add VLAN filters
3698 *
3699 * In Single VLAN Mode (SVM), single VLAN filters via ICE_SW_LKUP_VLAN are based
3700 * on the inner VLAN ID, so the VLAN TPID (i.e. 0x8100 or 0x888a8) doesn't
3701 * matter. In Double VLAN Mode (DVM), outer/single VLAN filters via
3702 * ICE_SW_LKUP_VLAN are based on the outer/single VLAN ID + VLAN TPID.
3703 *
3704 * For both modes add a VLAN 0 + no VLAN TPID filter to handle untagged traffic
3705 * when VLAN pruning is enabled. Also, this handles VLAN 0 priority tagged
3706 * traffic in SVM, since the VLAN TPID isn't part of filtering.
3707 *
3708 * If DVM is enabled then an explicit VLAN 0 + VLAN TPID filter needs to be
3709 * added to allow VLAN 0 priority tagged traffic in DVM, since the VLAN TPID is
3710 * part of filtering.
3711 */
3712int ice_vsi_add_vlan_zero(struct ice_vsi *vsi)
3713{
3714	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3715	struct ice_vlan vlan;
3716	int err;
3717
3718	vlan = ICE_VLAN(0, 0, 0);
3719	err = vlan_ops->add_vlan(vsi, &vlan);
3720	if (err && err != -EEXIST)
3721		return err;
3722
3723	/* in SVM both VLAN 0 filters are identical */
3724	if (!ice_is_dvm_ena(&vsi->back->hw))
3725		return 0;
3726
3727	vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3728	err = vlan_ops->add_vlan(vsi, &vlan);
3729	if (err && err != -EEXIST)
3730		return err;
3731
3732	return 0;
3733}
3734
3735/**
3736 * ice_vsi_del_vlan_zero - delete VLAN 0 filter(s) for this VSI
3737 * @vsi: VSI used to add VLAN filters
3738 *
3739 * Delete the VLAN 0 filters in the same manner that they were added in
3740 * ice_vsi_add_vlan_zero.
3741 */
3742int ice_vsi_del_vlan_zero(struct ice_vsi *vsi)
3743{
3744	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3745	struct ice_vlan vlan;
3746	int err;
3747
3748	vlan = ICE_VLAN(0, 0, 0);
3749	err = vlan_ops->del_vlan(vsi, &vlan);
3750	if (err && err != -EEXIST)
3751		return err;
3752
3753	/* in SVM both VLAN 0 filters are identical */
3754	if (!ice_is_dvm_ena(&vsi->back->hw))
3755		return 0;
3756
3757	vlan = ICE_VLAN(ETH_P_8021Q, 0, 0);
3758	err = vlan_ops->del_vlan(vsi, &vlan);
3759	if (err && err != -EEXIST)
3760		return err;
3761
3762	/* when deleting the last VLAN filter, make sure to disable the VLAN
3763	 * promisc mode so the filter isn't left by accident
3764	 */
3765	return ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3766				    ICE_MCAST_VLAN_PROMISC_BITS, 0);
3767}
3768
3769/**
3770 * ice_vsi_num_zero_vlans - get number of VLAN 0 filters based on VLAN mode
3771 * @vsi: VSI used to get the VLAN mode
3772 *
3773 * If DVM is enabled then 2 VLAN 0 filters are added, else if SVM is enabled
3774 * then 1 VLAN 0 filter is added. See ice_vsi_add_vlan_zero for more details.
3775 */
3776static u16 ice_vsi_num_zero_vlans(struct ice_vsi *vsi)
3777{
3778#define ICE_DVM_NUM_ZERO_VLAN_FLTRS	2
3779#define ICE_SVM_NUM_ZERO_VLAN_FLTRS	1
3780	/* no VLAN 0 filter is created when a port VLAN is active */
3781	if (vsi->type == ICE_VSI_VF) {
3782		if (WARN_ON(!vsi->vf))
3783			return 0;
3784
3785		if (ice_vf_is_port_vlan_ena(vsi->vf))
3786			return 0;
3787	}
3788
3789	if (ice_is_dvm_ena(&vsi->back->hw))
3790		return ICE_DVM_NUM_ZERO_VLAN_FLTRS;
3791	else
3792		return ICE_SVM_NUM_ZERO_VLAN_FLTRS;
3793}
3794
3795/**
3796 * ice_vsi_has_non_zero_vlans - check if VSI has any non-zero VLANs
3797 * @vsi: VSI used to determine if any non-zero VLANs have been added
3798 */
3799bool ice_vsi_has_non_zero_vlans(struct ice_vsi *vsi)
3800{
3801	return (vsi->num_vlan > ice_vsi_num_zero_vlans(vsi));
3802}
3803
3804/**
3805 * ice_vsi_num_non_zero_vlans - get the number of non-zero VLANs for this VSI
3806 * @vsi: VSI used to get the number of non-zero VLANs added
3807 */
3808u16 ice_vsi_num_non_zero_vlans(struct ice_vsi *vsi)
3809{
3810	return (vsi->num_vlan - ice_vsi_num_zero_vlans(vsi));
3811}
3812
3813/**
3814 * ice_is_feature_supported
3815 * @pf: pointer to the struct ice_pf instance
3816 * @f: feature enum to be checked
3817 *
3818 * returns true if feature is supported, false otherwise
3819 */
3820bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
3821{
3822	if (f < 0 || f >= ICE_F_MAX)
3823		return false;
3824
3825	return test_bit(f, pf->features);
3826}
3827
3828/**
3829 * ice_set_feature_support
3830 * @pf: pointer to the struct ice_pf instance
3831 * @f: feature enum to set
3832 */
3833void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
3834{
3835	if (f < 0 || f >= ICE_F_MAX)
3836		return;
3837
3838	set_bit(f, pf->features);
3839}
3840
3841/**
3842 * ice_clear_feature_support
3843 * @pf: pointer to the struct ice_pf instance
3844 * @f: feature enum to clear
3845 */
3846void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
3847{
3848	if (f < 0 || f >= ICE_F_MAX)
3849		return;
3850
3851	clear_bit(f, pf->features);
3852}
3853
3854/**
3855 * ice_init_feature_support
3856 * @pf: pointer to the struct ice_pf instance
3857 *
3858 * called during init to setup supported feature
3859 */
3860void ice_init_feature_support(struct ice_pf *pf)
3861{
3862	switch (pf->hw.device_id) {
3863	case ICE_DEV_ID_E810C_BACKPLANE:
3864	case ICE_DEV_ID_E810C_QSFP:
3865	case ICE_DEV_ID_E810C_SFP:
3866	case ICE_DEV_ID_E810_XXV_BACKPLANE:
3867	case ICE_DEV_ID_E810_XXV_QSFP:
3868	case ICE_DEV_ID_E810_XXV_SFP:
3869		ice_set_feature_support(pf, ICE_F_DSCP);
3870		if (ice_is_phy_rclk_in_netlist(&pf->hw))
3871			ice_set_feature_support(pf, ICE_F_PHY_RCLK);
3872		/* If we don't own the timer - don't enable other caps */
3873		if (!ice_pf_src_tmr_owned(pf))
3874			break;
3875		if (ice_is_cgu_in_netlist(&pf->hw))
3876			ice_set_feature_support(pf, ICE_F_CGU);
3877		if (ice_is_clock_mux_in_netlist(&pf->hw))
3878			ice_set_feature_support(pf, ICE_F_SMA_CTRL);
3879		if (ice_gnss_is_gps_present(&pf->hw))
3880			ice_set_feature_support(pf, ICE_F_GNSS);
3881		break;
3882	default:
3883		break;
3884	}
3885
3886	if (pf->hw.mac_type == ICE_MAC_E830)
3887		ice_set_feature_support(pf, ICE_F_MBX_LIMIT);
3888}
3889
3890/**
3891 * ice_vsi_update_security - update security block in VSI
3892 * @vsi: pointer to VSI structure
3893 * @fill: function pointer to fill ctx
3894 */
3895int
3896ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
3897{
3898	struct ice_vsi_ctx ctx = { 0 };
3899
3900	ctx.info = vsi->info;
3901	ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
3902	fill(&ctx);
3903
3904	if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
3905		return -ENODEV;
3906
3907	vsi->info = ctx.info;
3908	return 0;
3909}
3910
3911/**
3912 * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
3913 * @ctx: pointer to VSI ctx structure
3914 */
3915void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
3916{
3917	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
3918			       (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3919				ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3920}
3921
3922/**
3923 * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
3924 * @ctx: pointer to VSI ctx structure
3925 */
3926void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
3927{
3928	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF &
3929			       ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3930				 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3931}
3932
3933/**
3934 * ice_vsi_ctx_set_allow_override - allow destination override on VSI
3935 * @ctx: pointer to VSI ctx structure
3936 */
3937void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
3938{
3939	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
3940}
3941
3942/**
3943 * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
3944 * @ctx: pointer to VSI ctx structure
3945 */
3946void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
3947{
3948	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
3949}
3950
3951/**
3952 * ice_vsi_update_local_lb - update sw block in VSI with local loopback bit
3953 * @vsi: pointer to VSI structure
3954 * @set: set or unset the bit
3955 */
3956int
3957ice_vsi_update_local_lb(struct ice_vsi *vsi, bool set)
3958{
3959	struct ice_vsi_ctx ctx = {
3960		.info	= vsi->info,
3961	};
3962
3963	ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
3964	if (set)
3965		ctx.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_LOCAL_LB;
3966	else
3967		ctx.info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_LOCAL_LB;
3968
3969	if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
3970		return -ENODEV;
3971
3972	vsi->info = ctx.info;
3973	return 0;
3974}
v5.14.15
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2018, Intel Corporation. */
   3
   4#include "ice.h"
   5#include "ice_base.h"
   6#include "ice_flow.h"
   7#include "ice_lib.h"
   8#include "ice_fltr.h"
   9#include "ice_dcb_lib.h"
  10#include "ice_devlink.h"
 
  11
  12/**
  13 * ice_vsi_type_str - maps VSI type enum to string equivalents
  14 * @vsi_type: VSI type enum
  15 */
  16const char *ice_vsi_type_str(enum ice_vsi_type vsi_type)
  17{
  18	switch (vsi_type) {
  19	case ICE_VSI_PF:
  20		return "ICE_VSI_PF";
  21	case ICE_VSI_VF:
  22		return "ICE_VSI_VF";
 
 
  23	case ICE_VSI_CTRL:
  24		return "ICE_VSI_CTRL";
 
 
  25	case ICE_VSI_LB:
  26		return "ICE_VSI_LB";
  27	default:
  28		return "unknown";
  29	}
  30}
  31
  32/**
  33 * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
  34 * @vsi: the VSI being configured
  35 * @ena: start or stop the Rx rings
  36 *
  37 * First enable/disable all of the Rx rings, flush any remaining writes, and
  38 * then verify that they have all been enabled/disabled successfully. This will
  39 * let all of the register writes complete when enabling/disabling the Rx rings
  40 * before waiting for the change in hardware to complete.
  41 */
  42static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
  43{
  44	int ret = 0;
  45	u16 i;
  46
  47	for (i = 0; i < vsi->num_rxq; i++)
  48		ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);
  49
  50	ice_flush(&vsi->back->hw);
  51
  52	for (i = 0; i < vsi->num_rxq; i++) {
  53		ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
  54		if (ret)
  55			break;
  56	}
  57
  58	return ret;
  59}
  60
  61/**
  62 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
  63 * @vsi: VSI pointer
  64 *
  65 * On error: returns error code (negative)
  66 * On success: returns 0
  67 */
  68static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
  69{
  70	struct ice_pf *pf = vsi->back;
  71	struct device *dev;
  72
  73	dev = ice_pf_to_dev(pf);
 
 
  74
  75	/* allocate memory for both Tx and Rx ring pointers */
  76	vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
  77				     sizeof(*vsi->tx_rings), GFP_KERNEL);
  78	if (!vsi->tx_rings)
  79		return -ENOMEM;
  80
  81	vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
  82				     sizeof(*vsi->rx_rings), GFP_KERNEL);
  83	if (!vsi->rx_rings)
  84		goto err_rings;
  85
  86	/* XDP will have vsi->alloc_txq Tx queues as well, so double the size */
  87	vsi->txq_map = devm_kcalloc(dev, (2 * vsi->alloc_txq),
 
 
 
 
 
  88				    sizeof(*vsi->txq_map), GFP_KERNEL);
  89
  90	if (!vsi->txq_map)
  91		goto err_txq_map;
  92
  93	vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
  94				    sizeof(*vsi->rxq_map), GFP_KERNEL);
  95	if (!vsi->rxq_map)
  96		goto err_rxq_map;
  97
  98	/* There is no need to allocate q_vectors for a loopback VSI. */
  99	if (vsi->type == ICE_VSI_LB)
 100		return 0;
 101
 102	/* allocate memory for q_vector pointers */
 103	vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
 104				      sizeof(*vsi->q_vectors), GFP_KERNEL);
 105	if (!vsi->q_vectors)
 106		goto err_vectors;
 107
 108	vsi->af_xdp_zc_qps = bitmap_zalloc(max_t(int, vsi->alloc_txq, vsi->alloc_rxq), GFP_KERNEL);
 109	if (!vsi->af_xdp_zc_qps)
 110		goto err_zc_qps;
 111
 112	return 0;
 113
 114err_zc_qps:
 115	devm_kfree(dev, vsi->q_vectors);
 116err_vectors:
 117	devm_kfree(dev, vsi->rxq_map);
 118err_rxq_map:
 119	devm_kfree(dev, vsi->txq_map);
 120err_txq_map:
 121	devm_kfree(dev, vsi->rx_rings);
 122err_rings:
 123	devm_kfree(dev, vsi->tx_rings);
 124	return -ENOMEM;
 125}
 126
 127/**
 128 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
 129 * @vsi: the VSI being configured
 130 */
 131static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
 132{
 133	switch (vsi->type) {
 134	case ICE_VSI_PF:
 
 135	case ICE_VSI_CTRL:
 136	case ICE_VSI_LB:
 137		/* a user could change the values of num_[tr]x_desc using
 138		 * ethtool -G so we should keep those values instead of
 139		 * overwriting them with the defaults.
 140		 */
 141		if (!vsi->num_rx_desc)
 142			vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
 143		if (!vsi->num_tx_desc)
 144			vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
 145		break;
 146	default:
 147		dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
 148			vsi->type);
 149		break;
 150	}
 151}
 152
 153/**
 154 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
 155 * @vsi: the VSI being configured
 156 * @vf_id: ID of the VF being configured
 157 *
 158 * Return 0 on success and a negative value on error
 159 */
 160static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
 161{
 
 162	struct ice_pf *pf = vsi->back;
 163	struct ice_vf *vf = NULL;
 164
 165	if (vsi->type == ICE_VSI_VF)
 166		vsi->vf_id = vf_id;
 167	else
 168		vsi->vf_id = ICE_INVAL_VFID;
 169
 170	switch (vsi->type) {
 171	case ICE_VSI_PF:
 172		if (vsi->req_txq) {
 173			vsi->alloc_txq = vsi->req_txq;
 174			vsi->num_txq = vsi->req_txq;
 175		} else {
 176			vsi->alloc_txq = min3(pf->num_lan_msix,
 177					      ice_get_avail_txq_count(pf),
 178					      (u16)num_online_cpus());
 179		}
 180
 181		pf->num_lan_tx = vsi->alloc_txq;
 182
 183		/* only 1 Rx queue unless RSS is enabled */
 184		if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
 185			vsi->alloc_rxq = 1;
 186		} else {
 187			if (vsi->req_rxq) {
 188				vsi->alloc_rxq = vsi->req_rxq;
 189				vsi->num_rxq = vsi->req_rxq;
 190			} else {
 191				vsi->alloc_rxq = min3(pf->num_lan_msix,
 192						      ice_get_avail_rxq_count(pf),
 193						      (u16)num_online_cpus());
 194			}
 195		}
 196
 197		pf->num_lan_rx = vsi->alloc_rxq;
 198
 199		vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
 200					   max_t(int, vsi->alloc_rxq,
 201						 vsi->alloc_txq));
 202		break;
 
 
 
 
 
 
 203	case ICE_VSI_VF:
 204		vf = &pf->vf[vsi->vf_id];
 205		if (vf->num_req_qs)
 206			vf->num_vf_qs = vf->num_req_qs;
 207		vsi->alloc_txq = vf->num_vf_qs;
 208		vsi->alloc_rxq = vf->num_vf_qs;
 209		/* pf->num_msix_per_vf includes (VF miscellaneous vector +
 210		 * data queue interrupts). Since vsi->num_q_vectors is number
 211		 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
 212		 * original vector count
 213		 */
 214		vsi->num_q_vectors = pf->num_msix_per_vf - ICE_NONQ_VECS_VF;
 215		break;
 216	case ICE_VSI_CTRL:
 217		vsi->alloc_txq = 1;
 218		vsi->alloc_rxq = 1;
 219		vsi->num_q_vectors = 1;
 220		break;
 
 
 
 
 221	case ICE_VSI_LB:
 222		vsi->alloc_txq = 1;
 223		vsi->alloc_rxq = 1;
 224		break;
 225	default:
 226		dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
 227		break;
 228	}
 229
 230	ice_vsi_set_num_desc(vsi);
 231}
 232
 233/**
 234 * ice_get_free_slot - get the next non-NULL location index in array
 235 * @array: array to search
 236 * @size: size of the array
 237 * @curr: last known occupied index to be used as a search hint
 238 *
 239 * void * is being used to keep the functionality generic. This lets us use this
 240 * function on any array of pointers.
 241 */
 242static int ice_get_free_slot(void *array, int size, int curr)
 243{
 244	int **tmp_array = (int **)array;
 245	int next;
 246
 247	if (curr < (size - 1) && !tmp_array[curr + 1]) {
 248		next = curr + 1;
 249	} else {
 250		int i = 0;
 251
 252		while ((i < size) && (tmp_array[i]))
 253			i++;
 254		if (i == size)
 255			next = ICE_NO_VSI;
 256		else
 257			next = i;
 258	}
 259	return next;
 260}
 261
 262/**
 263 * ice_vsi_delete - delete a VSI from the switch
 264 * @vsi: pointer to VSI being removed
 265 */
 266static void ice_vsi_delete(struct ice_vsi *vsi)
 267{
 268	struct ice_pf *pf = vsi->back;
 269	struct ice_vsi_ctx *ctxt;
 270	enum ice_status status;
 271
 
 272	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
 273	if (!ctxt)
 274		return;
 275
 276	if (vsi->type == ICE_VSI_VF)
 277		ctxt->vf_num = vsi->vf_id;
 278	ctxt->vsi_num = vsi->vsi_num;
 279
 280	memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
 281
 282	status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
 283	if (status)
 284		dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %s\n",
 285			vsi->vsi_num, ice_stat_str(status));
 286
 287	kfree(ctxt);
 288}
 289
 290/**
 291 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
 292 * @vsi: pointer to VSI being cleared
 293 */
 294static void ice_vsi_free_arrays(struct ice_vsi *vsi)
 295{
 296	struct ice_pf *pf = vsi->back;
 297	struct device *dev;
 298
 299	dev = ice_pf_to_dev(pf);
 300
 301	if (vsi->af_xdp_zc_qps) {
 302		bitmap_free(vsi->af_xdp_zc_qps);
 303		vsi->af_xdp_zc_qps = NULL;
 304	}
 305	/* free the ring and vector containers */
 306	if (vsi->q_vectors) {
 307		devm_kfree(dev, vsi->q_vectors);
 308		vsi->q_vectors = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 309	}
 310	if (vsi->tx_rings) {
 311		devm_kfree(dev, vsi->tx_rings);
 312		vsi->tx_rings = NULL;
 
 
 
 313	}
 314	if (vsi->rx_rings) {
 315		devm_kfree(dev, vsi->rx_rings);
 316		vsi->rx_rings = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 317	}
 318	if (vsi->txq_map) {
 319		devm_kfree(dev, vsi->txq_map);
 320		vsi->txq_map = NULL;
 321	}
 322	if (vsi->rxq_map) {
 323		devm_kfree(dev, vsi->rxq_map);
 324		vsi->rxq_map = NULL;
 
 
 
 
 
 
 
 
 
 
 
 325	}
 
 
 
 
 
 
 326}
 327
 328/**
 329 * ice_vsi_clear - clean up and deallocate the provided VSI
 330 * @vsi: pointer to VSI being cleared
 331 *
 332 * This deallocates the VSI's queue resources, removes it from the PF's
 333 * VSI array if necessary, and deallocates the VSI
 334 *
 335 * Returns 0 on success, negative on failure
 336 */
 337static int ice_vsi_clear(struct ice_vsi *vsi)
 338{
 339	struct ice_pf *pf = NULL;
 340	struct device *dev;
 341
 342	if (!vsi)
 343		return 0;
 344
 345	if (!vsi->back)
 346		return -EINVAL;
 347
 348	pf = vsi->back;
 349	dev = ice_pf_to_dev(pf);
 350
 351	if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
 352		dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
 353		return -EINVAL;
 354	}
 355
 356	mutex_lock(&pf->sw_mutex);
 357	/* updates the PF for this cleared VSI */
 358
 359	pf->vsi[vsi->idx] = NULL;
 360	if (vsi->idx < pf->next_vsi && vsi->type != ICE_VSI_CTRL)
 361		pf->next_vsi = vsi->idx;
 362	if (vsi->idx < pf->next_vsi && vsi->type == ICE_VSI_CTRL &&
 363	    vsi->vf_id != ICE_INVAL_VFID)
 364		pf->next_vsi = vsi->idx;
 365
 
 366	ice_vsi_free_arrays(vsi);
 
 367	mutex_unlock(&pf->sw_mutex);
 368	devm_kfree(dev, vsi);
 
 369
 370	return 0;
 
 
 
 371}
 372
 373/**
 374 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
 375 * @irq: interrupt number
 376 * @data: pointer to a q_vector
 377 */
 378static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
 379{
 380	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
 381
 382	if (!q_vector->tx.ring)
 383		return IRQ_HANDLED;
 384
 385#define FDIR_RX_DESC_CLEAN_BUDGET 64
 386	ice_clean_rx_irq(q_vector->rx.ring, FDIR_RX_DESC_CLEAN_BUDGET);
 387	ice_clean_ctrl_tx_irq(q_vector->tx.ring);
 388
 389	return IRQ_HANDLED;
 390}
 391
 392/**
 393 * ice_msix_clean_rings - MSIX mode Interrupt Handler
 394 * @irq: interrupt number
 395 * @data: pointer to a q_vector
 396 */
 397static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
 398{
 399	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
 400
 401	if (!q_vector->tx.ring && !q_vector->rx.ring)
 402		return IRQ_HANDLED;
 403
 404	q_vector->total_events++;
 405
 406	napi_schedule(&q_vector->napi);
 407
 408	return IRQ_HANDLED;
 409}
 410
 411/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 412 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
 413 * @pf: board private structure
 414 * @vsi_type: type of VSI
 415 * @vf_id: ID of the VF being configured
 
 
 416 *
 417 * returns a pointer to a VSI on success, NULL on failure.
 418 */
 419static struct ice_vsi *
 420ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type, u16 vf_id)
 421{
 422	struct device *dev = ice_pf_to_dev(pf);
 423	struct ice_vsi *vsi = NULL;
 424
 425	/* Need to protect the allocation of the VSIs at the PF level */
 426	mutex_lock(&pf->sw_mutex);
 427
 428	/* If we have already allocated our maximum number of VSIs,
 429	 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
 430	 * is available to be populated
 431	 */
 432	if (pf->next_vsi == ICE_NO_VSI) {
 433		dev_dbg(dev, "out of VSI slots!\n");
 434		goto unlock_pf;
 435	}
 436
 437	vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
 438	if (!vsi)
 439		goto unlock_pf;
 440
 441	vsi->type = vsi_type;
 442	vsi->back = pf;
 443	set_bit(ICE_VSI_DOWN, vsi->state);
 444
 445	if (vsi_type == ICE_VSI_VF)
 446		ice_vsi_set_num_qs(vsi, vf_id);
 447	else
 448		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
 449
 450	switch (vsi->type) {
 451	case ICE_VSI_PF:
 452		if (ice_vsi_alloc_arrays(vsi))
 453			goto err_rings;
 454
 455		/* Setup default MSIX irq handler for VSI */
 456		vsi->irq_handler = ice_msix_clean_rings;
 457		break;
 458	case ICE_VSI_CTRL:
 459		if (ice_vsi_alloc_arrays(vsi))
 460			goto err_rings;
 461
 462		/* Setup ctrl VSI MSIX irq handler */
 463		vsi->irq_handler = ice_msix_clean_ctrl_vsi;
 464		break;
 465	case ICE_VSI_VF:
 466		if (ice_vsi_alloc_arrays(vsi))
 467			goto err_rings;
 468		break;
 469	case ICE_VSI_LB:
 470		if (ice_vsi_alloc_arrays(vsi))
 471			goto err_rings;
 472		break;
 473	default:
 474		dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
 475		goto unlock_pf;
 476	}
 477
 478	if (vsi->type == ICE_VSI_CTRL && vf_id == ICE_INVAL_VFID) {
 479		/* Use the last VSI slot as the index for PF control VSI */
 480		vsi->idx = pf->num_alloc_vsi - 1;
 481		pf->ctrl_vsi_idx = vsi->idx;
 482		pf->vsi[vsi->idx] = vsi;
 483	} else {
 484		/* fill slot and make note of the index */
 485		vsi->idx = pf->next_vsi;
 486		pf->vsi[pf->next_vsi] = vsi;
 487
 488		/* prepare pf->next_vsi for next use */
 489		pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
 490						 pf->next_vsi);
 491	}
 492
 493	if (vsi->type == ICE_VSI_CTRL && vf_id != ICE_INVAL_VFID)
 494		pf->vf[vf_id].ctrl_vsi_idx = vsi->idx;
 495	goto unlock_pf;
 496
 497err_rings:
 498	devm_kfree(dev, vsi);
 499	vsi = NULL;
 500unlock_pf:
 501	mutex_unlock(&pf->sw_mutex);
 502	return vsi;
 503}
 504
 505/**
 506 * ice_alloc_fd_res - Allocate FD resource for a VSI
 507 * @vsi: pointer to the ice_vsi
 508 *
 509 * This allocates the FD resources
 510 *
 511 * Returns 0 on success, -EPERM on no-op or -EIO on failure
 512 */
 513static int ice_alloc_fd_res(struct ice_vsi *vsi)
 514{
 515	struct ice_pf *pf = vsi->back;
 516	u32 g_val, b_val;
 517
 518	/* Flow Director filters are only allocated/assigned to the PF VSI which
 519	 * passes the traffic. The CTRL VSI is only used to add/delete filters
 520	 * so we don't allocate resources to it
 521	 */
 
 
 
 
 
 
 522
 523	/* FD filters from guaranteed pool per VSI */
 524	g_val = pf->hw.func_caps.fd_fltr_guar;
 525	if (!g_val)
 526		return -EPERM;
 527
 528	/* FD filters from best effort pool */
 529	b_val = pf->hw.func_caps.fd_fltr_best_effort;
 530	if (!b_val)
 531		return -EPERM;
 532
 533	if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF))
 534		return -EPERM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 535
 536	if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
 537		return -EPERM;
 538
 539	vsi->num_gfltr = g_val / pf->num_alloc_vsi;
 
 540
 541	/* each VSI gets same "best_effort" quota */
 542	vsi->num_bfltr = b_val;
 
 
 
 543
 544	if (vsi->type == ICE_VSI_VF) {
 545		vsi->num_gfltr = 0;
 
 546
 547		/* each VSI gets same "best_effort" quota */
 548		vsi->num_bfltr = b_val;
 549	}
 550
 551	return 0;
 552}
 553
 554/**
 555 * ice_vsi_get_qs - Assign queues from PF to VSI
 556 * @vsi: the VSI to assign queues to
 557 *
 558 * Returns 0 on success and a negative value on error
 559 */
 560static int ice_vsi_get_qs(struct ice_vsi *vsi)
 561{
 562	struct ice_pf *pf = vsi->back;
 563	struct ice_qs_cfg tx_qs_cfg = {
 564		.qs_mutex = &pf->avail_q_mutex,
 565		.pf_map = pf->avail_txqs,
 566		.pf_map_size = pf->max_pf_txqs,
 567		.q_count = vsi->alloc_txq,
 568		.scatter_count = ICE_MAX_SCATTER_TXQS,
 569		.vsi_map = vsi->txq_map,
 570		.vsi_map_offset = 0,
 571		.mapping_mode = ICE_VSI_MAP_CONTIG
 572	};
 573	struct ice_qs_cfg rx_qs_cfg = {
 574		.qs_mutex = &pf->avail_q_mutex,
 575		.pf_map = pf->avail_rxqs,
 576		.pf_map_size = pf->max_pf_rxqs,
 577		.q_count = vsi->alloc_rxq,
 578		.scatter_count = ICE_MAX_SCATTER_RXQS,
 579		.vsi_map = vsi->rxq_map,
 580		.vsi_map_offset = 0,
 581		.mapping_mode = ICE_VSI_MAP_CONTIG
 582	};
 583	int ret;
 584
 
 
 
 585	ret = __ice_vsi_get_qs(&tx_qs_cfg);
 586	if (ret)
 587		return ret;
 588	vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
 589
 590	ret = __ice_vsi_get_qs(&rx_qs_cfg);
 591	if (ret)
 592		return ret;
 593	vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;
 594
 595	return 0;
 596}
 597
 598/**
 599 * ice_vsi_put_qs - Release queues from VSI to PF
 600 * @vsi: the VSI that is going to release queues
 601 */
 602static void ice_vsi_put_qs(struct ice_vsi *vsi)
 603{
 604	struct ice_pf *pf = vsi->back;
 605	int i;
 606
 607	mutex_lock(&pf->avail_q_mutex);
 608
 609	for (i = 0; i < vsi->alloc_txq; i++) {
 610		clear_bit(vsi->txq_map[i], pf->avail_txqs);
 611		vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
 612	}
 613
 614	for (i = 0; i < vsi->alloc_rxq; i++) {
 615		clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
 616		vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
 617	}
 618
 619	mutex_unlock(&pf->avail_q_mutex);
 620}
 621
 622/**
 623 * ice_is_safe_mode
 624 * @pf: pointer to the PF struct
 625 *
 626 * returns true if driver is in safe mode, false otherwise
 627 */
 628bool ice_is_safe_mode(struct ice_pf *pf)
 629{
 630	return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
 631}
 632
 633/**
 634 * ice_is_aux_ena
 635 * @pf: pointer to the PF struct
 636 *
 637 * returns true if AUX devices/drivers are supported, false otherwise
 638 */
 639bool ice_is_aux_ena(struct ice_pf *pf)
 640{
 641	return test_bit(ICE_FLAG_AUX_ENA, pf->flags);
 642}
 643
 644/**
 645 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
 646 * @vsi: the VSI being cleaned up
 647 *
 648 * This function deletes RSS input set for all flows that were configured
 649 * for this VSI
 650 */
 651static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
 652{
 653	struct ice_pf *pf = vsi->back;
 654	enum ice_status status;
 655
 656	if (ice_is_safe_mode(pf))
 657		return;
 658
 659	status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
 660	if (status)
 661		dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %s\n",
 662			vsi->vsi_num, ice_stat_str(status));
 663}
 664
 665/**
 666 * ice_rss_clean - Delete RSS related VSI structures and configuration
 667 * @vsi: the VSI being removed
 668 */
 669static void ice_rss_clean(struct ice_vsi *vsi)
 670{
 671	struct ice_pf *pf = vsi->back;
 672	struct device *dev;
 673
 674	dev = ice_pf_to_dev(pf);
 675
 676	if (vsi->rss_hkey_user)
 677		devm_kfree(dev, vsi->rss_hkey_user);
 678	if (vsi->rss_lut_user)
 679		devm_kfree(dev, vsi->rss_lut_user);
 680
 681	ice_vsi_clean_rss_flow_fld(vsi);
 682	/* remove RSS replay list */
 683	if (!ice_is_safe_mode(pf))
 684		ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
 685}
 686
 687/**
 688 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
 689 * @vsi: the VSI being configured
 690 */
 691static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
 692{
 693	struct ice_hw_common_caps *cap;
 694	struct ice_pf *pf = vsi->back;
 
 695
 696	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
 697		vsi->rss_size = 1;
 698		return;
 699	}
 700
 701	cap = &pf->hw.func_caps.common_cap;
 
 702	switch (vsi->type) {
 
 703	case ICE_VSI_PF:
 704		/* PF VSI will inherit RSS instance of PF */
 705		vsi->rss_table_size = (u16)cap->rss_table_size;
 706		vsi->rss_size = min_t(u16, num_online_cpus(),
 707				      BIT(cap->rss_table_entry_width));
 708		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
 
 
 
 
 
 
 
 
 709		break;
 710	case ICE_VSI_VF:
 711		/* VF VSI will get a small RSS table.
 712		 * For VSI_LUT, LUT size should be set to 64 bytes.
 713		 */
 714		vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
 715		vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
 716		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
 717		break;
 718	case ICE_VSI_LB:
 719		break;
 720	default:
 721		dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
 722			ice_vsi_type_str(vsi->type));
 723		break;
 724	}
 725}
 726
 727/**
 728 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
 
 729 * @ctxt: the VSI context being set
 730 *
 731 * This initializes a default VSI context for all sections except the Queues.
 732 */
 733static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
 734{
 735	u32 table = 0;
 736
 737	memset(&ctxt->info, 0, sizeof(ctxt->info));
 738	/* VSI's should be allocated from shared pool */
 739	ctxt->alloc_from_pool = true;
 740	/* Src pruning enabled by default */
 741	ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
 742	/* Traffic from VSI can be sent to LAN */
 743	ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
 744	/* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
 745	 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
 746	 * packets untagged/tagged.
 747	 */
 748	ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
 749				  ICE_AQ_VSI_VLAN_MODE_M) >>
 750				 ICE_AQ_VSI_VLAN_MODE_S);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 751	/* Have 1:1 UP mapping for both ingress/egress tables */
 752	table |= ICE_UP_TABLE_TRANSLATE(0, 0);
 753	table |= ICE_UP_TABLE_TRANSLATE(1, 1);
 754	table |= ICE_UP_TABLE_TRANSLATE(2, 2);
 755	table |= ICE_UP_TABLE_TRANSLATE(3, 3);
 756	table |= ICE_UP_TABLE_TRANSLATE(4, 4);
 757	table |= ICE_UP_TABLE_TRANSLATE(5, 5);
 758	table |= ICE_UP_TABLE_TRANSLATE(6, 6);
 759	table |= ICE_UP_TABLE_TRANSLATE(7, 7);
 760	ctxt->info.ingress_table = cpu_to_le32(table);
 761	ctxt->info.egress_table = cpu_to_le32(table);
 762	/* Have 1:1 UP mapping for outer to inner UP table */
 763	ctxt->info.outer_up_table = cpu_to_le32(table);
 764	/* No Outer tag support outer_tag_flags remains to zero */
 765}
 766
 767/**
 768 * ice_vsi_setup_q_map - Setup a VSI queue map
 769 * @vsi: the VSI being configured
 770 * @ctxt: VSI context structure
 771 */
 772static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
 773{
 774	u16 offset = 0, qmap = 0, tx_count = 0, pow = 0;
 775	u16 num_txq_per_tc, num_rxq_per_tc;
 776	u16 qcount_tx = vsi->alloc_txq;
 777	u16 qcount_rx = vsi->alloc_rxq;
 778	bool ena_tc0 = false;
 779	u8 netdev_tc = 0;
 780	int i;
 781
 782	/* at least TC0 should be enabled by default */
 783	if (vsi->tc_cfg.numtc) {
 784		if (!(vsi->tc_cfg.ena_tc & BIT(0)))
 785			ena_tc0 = true;
 786	} else {
 787		ena_tc0 = true;
 788	}
 789
 790	if (ena_tc0) {
 791		vsi->tc_cfg.numtc++;
 792		vsi->tc_cfg.ena_tc |= 1;
 793	}
 794
 795	num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
 796	if (!num_rxq_per_tc)
 797		num_rxq_per_tc = 1;
 798	num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
 799	if (!num_txq_per_tc)
 800		num_txq_per_tc = 1;
 801
 802	/* find the (rounded up) power-of-2 of qcount */
 803	pow = (u16)order_base_2(num_rxq_per_tc);
 804
 805	/* TC mapping is a function of the number of Rx queues assigned to the
 806	 * VSI for each traffic class and the offset of these queues.
 807	 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
 808	 * queues allocated to TC0. No:of queues is a power-of-2.
 809	 *
 810	 * If TC is not enabled, the queue offset is set to 0, and allocate one
 811	 * queue, this way, traffic for the given TC will be sent to the default
 812	 * queue.
 813	 *
 814	 * Setup number and offset of Rx queues for all TCs for the VSI
 815	 */
 816	ice_for_each_traffic_class(i) {
 817		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
 818			/* TC is not enabled */
 819			vsi->tc_cfg.tc_info[i].qoffset = 0;
 820			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
 821			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
 822			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
 823			ctxt->info.tc_mapping[i] = 0;
 824			continue;
 825		}
 826
 827		/* TC is enabled */
 828		vsi->tc_cfg.tc_info[i].qoffset = offset;
 829		vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
 830		vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
 831		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
 832
 833		qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
 834			ICE_AQ_VSI_TC_Q_OFFSET_M) |
 835			((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
 836			 ICE_AQ_VSI_TC_Q_NUM_M);
 837		offset += num_rxq_per_tc;
 838		tx_count += num_txq_per_tc;
 839		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
 840	}
 841
 842	/* if offset is non-zero, means it is calculated correctly based on
 843	 * enabled TCs for a given VSI otherwise qcount_rx will always
 844	 * be correct and non-zero because it is based off - VSI's
 845	 * allocated Rx queues which is at least 1 (hence qcount_tx will be
 846	 * at least 1)
 847	 */
 848	if (offset)
 849		vsi->num_rxq = offset;
 850	else
 851		vsi->num_rxq = num_rxq_per_tc;
 
 
 
 
 
 
 
 
 
 
 
 
 852
 853	vsi->num_txq = tx_count;
 
 854
 855	if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
 856		dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
 857		/* since there is a chance that num_rxq could have been changed
 858		 * in the above for loop, make num_txq equal to num_rxq.
 859		 */
 860		vsi->num_txq = vsi->num_rxq;
 861	}
 862
 863	/* Rx queue mapping */
 864	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
 865	/* q_mapping buffer holds the info for the first queue allocated for
 866	 * this VSI in the PF space and also the number of queues associated
 867	 * with this VSI.
 868	 */
 869	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
 870	ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
 
 
 871}
 872
 873/**
 874 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
 875 * @ctxt: the VSI context being set
 876 * @vsi: the VSI being configured
 877 */
 878static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
 879{
 880	u8 dflt_q_group, dflt_q_prio;
 881	u16 dflt_q, report_q, val;
 882
 883	if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
 884	    vsi->type != ICE_VSI_VF)
 885		return;
 886
 887	val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
 888	ctxt->info.valid_sections |= cpu_to_le16(val);
 889	dflt_q = 0;
 890	dflt_q_group = 0;
 891	report_q = 0;
 892	dflt_q_prio = 0;
 893
 894	/* enable flow director filtering/programming */
 895	val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
 896	ctxt->info.fd_options = cpu_to_le16(val);
 897	/* max of allocated flow director filters */
 898	ctxt->info.max_fd_fltr_dedicated =
 899			cpu_to_le16(vsi->num_gfltr);
 900	/* max of shared flow director filters any VSI may program */
 901	ctxt->info.max_fd_fltr_shared =
 902			cpu_to_le16(vsi->num_bfltr);
 903	/* default queue index within the VSI of the default FD */
 904	val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
 905	       ICE_AQ_VSI_FD_DEF_Q_M);
 906	/* target queue or queue group to the FD filter */
 907	val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
 908		ICE_AQ_VSI_FD_DEF_GRP_M);
 909	ctxt->info.fd_def_q = cpu_to_le16(val);
 910	/* queue index on which FD filter completion is reported */
 911	val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
 912	       ICE_AQ_VSI_FD_REPORT_Q_M);
 913	/* priority of the default qindex action */
 914	val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
 915		ICE_AQ_VSI_FD_DEF_PRIORITY_M);
 916	ctxt->info.fd_report_opt = cpu_to_le16(val);
 917}
 918
 919/**
 920 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
 921 * @ctxt: the VSI context being set
 922 * @vsi: the VSI being configured
 923 */
 924static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
 925{
 926	u8 lut_type, hash_type;
 927	struct device *dev;
 928	struct ice_pf *pf;
 929
 930	pf = vsi->back;
 931	dev = ice_pf_to_dev(pf);
 932
 933	switch (vsi->type) {
 
 934	case ICE_VSI_PF:
 935		/* PF VSI will inherit RSS instance of PF */
 936		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
 937		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
 938		break;
 939	case ICE_VSI_VF:
 
 940		/* VF VSI will gets a small RSS table which is a VSI LUT type */
 941		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
 942		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
 943		break;
 944	default:
 945		dev_dbg(dev, "Unsupported VSI type %s\n",
 946			ice_vsi_type_str(vsi->type));
 947		return;
 948	}
 949
 950	ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
 951				ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
 952				((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
 953				 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 954}
 955
 956/**
 957 * ice_vsi_init - Create and initialize a VSI
 958 * @vsi: the VSI being configured
 959 * @init_vsi: is this call creating a VSI
 
 
 
 960 *
 961 * This initializes a VSI context depending on the VSI type to be added and
 962 * passes it down to the add_vsi aq command to create a new VSI.
 963 */
 964static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
 965{
 966	struct ice_pf *pf = vsi->back;
 967	struct ice_hw *hw = &pf->hw;
 968	struct ice_vsi_ctx *ctxt;
 969	struct device *dev;
 970	int ret = 0;
 971
 972	dev = ice_pf_to_dev(pf);
 973	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
 974	if (!ctxt)
 975		return -ENOMEM;
 976
 977	switch (vsi->type) {
 978	case ICE_VSI_CTRL:
 979	case ICE_VSI_LB:
 980	case ICE_VSI_PF:
 981		ctxt->flags = ICE_AQ_VSI_TYPE_PF;
 982		break;
 
 
 
 
 983	case ICE_VSI_VF:
 984		ctxt->flags = ICE_AQ_VSI_TYPE_VF;
 985		/* VF number here is the absolute VF number (0-255) */
 986		ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
 987		break;
 988	default:
 989		ret = -ENODEV;
 990		goto out;
 991	}
 992
 993	ice_set_dflt_vsi_ctx(ctxt);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 994	if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
 995		ice_set_fd_vsi_ctx(ctxt, vsi);
 996	/* if the switch is in VEB mode, allow VSI loopback */
 997	if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
 998		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
 999
1000	/* Set LUT type and HASH type if RSS is enabled */
1001	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
1002	    vsi->type != ICE_VSI_CTRL) {
1003		ice_set_rss_vsi_ctx(ctxt, vsi);
1004		/* if updating VSI context, make sure to set valid_section:
1005		 * to indicate which section of VSI context being updated
1006		 */
1007		if (!init_vsi)
1008			ctxt->info.valid_sections |=
1009				cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
1010	}
1011
1012	ctxt->info.sw_id = vsi->port_info->sw_id;
1013	ice_vsi_setup_q_map(vsi, ctxt);
1014	if (!init_vsi) /* means VSI being updated */
1015		/* must to indicate which section of VSI context are
1016		 * being modified
1017		 */
1018		ctxt->info.valid_sections |=
1019			cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
1020
1021	/* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
1022	 * respectively
1023	 */
1024	if (vsi->type == ICE_VSI_VF) {
1025		ctxt->info.valid_sections |=
1026			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1027		if (pf->vf[vsi->vf_id].spoofchk) {
1028			ctxt->info.sec_flags |=
1029				ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1030				(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1031				 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
1032		} else {
1033			ctxt->info.sec_flags &=
1034				~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
1035				  (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
1036				   ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
1037		}
1038	}
1039
1040	/* Allow control frames out of main VSI */
1041	if (vsi->type == ICE_VSI_PF) {
1042		ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
1043		ctxt->info.valid_sections |=
1044			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
1045	}
1046
1047	if (init_vsi) {
1048		ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
1049		if (ret) {
1050			dev_err(dev, "Add VSI failed, err %d\n", ret);
1051			ret = -EIO;
1052			goto out;
1053		}
1054	} else {
1055		ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1056		if (ret) {
1057			dev_err(dev, "Update VSI failed, err %d\n", ret);
1058			ret = -EIO;
1059			goto out;
1060		}
1061	}
1062
1063	/* keep context for update VSI operations */
1064	vsi->info = ctxt->info;
1065
1066	/* record VSI number returned */
1067	vsi->vsi_num = ctxt->vsi_num;
1068
1069out:
1070	kfree(ctxt);
1071	return ret;
1072}
1073
1074/**
1075 * ice_free_res - free a block of resources
1076 * @res: pointer to the resource
1077 * @index: starting index previously returned by ice_get_res
1078 * @id: identifier to track owner
1079 *
1080 * Returns number of resources freed
1081 */
1082int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
1083{
1084	int count = 0;
1085	int i;
1086
1087	if (!res || index >= res->end)
1088		return -EINVAL;
1089
1090	id |= ICE_RES_VALID_BIT;
1091	for (i = index; i < res->end && res->list[i] == id; i++) {
1092		res->list[i] = 0;
1093		count++;
1094	}
1095
1096	return count;
1097}
1098
1099/**
1100 * ice_search_res - Search the tracker for a block of resources
1101 * @res: pointer to the resource
1102 * @needed: size of the block needed
1103 * @id: identifier to track owner
1104 *
1105 * Returns the base item index of the block, or -ENOMEM for error
1106 */
1107static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
1108{
1109	u16 start = 0, end = 0;
1110
1111	if (needed > res->end)
1112		return -ENOMEM;
1113
1114	id |= ICE_RES_VALID_BIT;
1115
1116	do {
1117		/* skip already allocated entries */
1118		if (res->list[end++] & ICE_RES_VALID_BIT) {
1119			start = end;
1120			if ((start + needed) > res->end)
1121				break;
1122		}
1123
1124		if (end == (start + needed)) {
1125			int i = start;
1126
1127			/* there was enough, so assign it to the requestor */
1128			while (i != end)
1129				res->list[i++] = id;
1130
1131			return start;
1132		}
1133	} while (end < res->end);
1134
1135	return -ENOMEM;
1136}
1137
1138/**
1139 * ice_get_free_res_count - Get free count from a resource tracker
1140 * @res: Resource tracker instance
1141 */
1142static u16 ice_get_free_res_count(struct ice_res_tracker *res)
1143{
1144	u16 i, count = 0;
1145
1146	for (i = 0; i < res->end; i++)
1147		if (!(res->list[i] & ICE_RES_VALID_BIT))
1148			count++;
1149
1150	return count;
1151}
1152
1153/**
1154 * ice_get_res - get a block of resources
1155 * @pf: board private structure
1156 * @res: pointer to the resource
1157 * @needed: size of the block needed
1158 * @id: identifier to track owner
1159 *
1160 * Returns the base item index of the block, or negative for error
1161 */
1162int
1163ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
1164{
1165	if (!res || !pf)
1166		return -EINVAL;
1167
1168	if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
1169		dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
1170			needed, res->num_entries, id);
1171		return -EINVAL;
1172	}
1173
1174	return ice_search_res(res, needed, id);
1175}
1176
1177/**
1178 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
1179 * @vsi: ptr to the VSI
1180 *
1181 * This should only be called after ice_vsi_alloc() which allocates the
1182 * corresponding SW VSI structure and initializes num_queue_pairs for the
1183 * newly allocated VSI.
1184 *
1185 * Returns 0 on success or negative on failure
1186 */
1187static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1188{
1189	struct ice_pf *pf = vsi->back;
1190	struct device *dev;
1191	u16 num_q_vectors;
1192	int base;
1193
1194	dev = ice_pf_to_dev(pf);
1195	/* SRIOV doesn't grab irq_tracker entries for each VSI */
1196	if (vsi->type == ICE_VSI_VF)
1197		return 0;
1198
1199	if (vsi->base_vector) {
1200		dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
1201			vsi->vsi_num, vsi->base_vector);
1202		return -EEXIST;
1203	}
1204
1205	num_q_vectors = vsi->num_q_vectors;
1206	/* reserve slots from OS requested IRQs */
1207	if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
1208		int i;
1209
1210		ice_for_each_vf(pf, i) {
1211			struct ice_vf *vf = &pf->vf[i];
1212
1213			if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI) {
1214				base = pf->vsi[vf->ctrl_vsi_idx]->base_vector;
1215				break;
1216			}
1217		}
1218		if (i == pf->num_alloc_vfs)
1219			base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1220					   ICE_RES_VF_CTRL_VEC_ID);
1221	} else {
1222		base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
1223				   vsi->idx);
1224	}
1225
1226	if (base < 0) {
1227		dev_err(dev, "%d MSI-X interrupts available. %s %d failed to get %d MSI-X vectors\n",
1228			ice_get_free_res_count(pf->irq_tracker),
1229			ice_vsi_type_str(vsi->type), vsi->idx, num_q_vectors);
1230		return -ENOENT;
1231	}
1232	vsi->base_vector = (u16)base;
1233	pf->num_avail_sw_msix -= num_q_vectors;
1234
1235	return 0;
1236}
1237
1238/**
1239 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
1240 * @vsi: the VSI having rings deallocated
1241 */
1242static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1243{
1244	int i;
1245
1246	/* Avoid stale references by clearing map from vector to ring */
1247	if (vsi->q_vectors) {
1248		ice_for_each_q_vector(vsi, i) {
1249			struct ice_q_vector *q_vector = vsi->q_vectors[i];
1250
1251			if (q_vector) {
1252				q_vector->tx.ring = NULL;
1253				q_vector->rx.ring = NULL;
1254			}
1255		}
1256	}
1257
1258	if (vsi->tx_rings) {
1259		for (i = 0; i < vsi->alloc_txq; i++) {
1260			if (vsi->tx_rings[i]) {
1261				kfree_rcu(vsi->tx_rings[i], rcu);
1262				WRITE_ONCE(vsi->tx_rings[i], NULL);
1263			}
1264		}
1265	}
1266	if (vsi->rx_rings) {
1267		for (i = 0; i < vsi->alloc_rxq; i++) {
1268			if (vsi->rx_rings[i]) {
1269				kfree_rcu(vsi->rx_rings[i], rcu);
1270				WRITE_ONCE(vsi->rx_rings[i], NULL);
1271			}
1272		}
1273	}
1274}
1275
1276/**
1277 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
1278 * @vsi: VSI which is having rings allocated
1279 */
1280static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1281{
 
1282	struct ice_pf *pf = vsi->back;
1283	struct device *dev;
1284	u16 i;
1285
1286	dev = ice_pf_to_dev(pf);
1287	/* Allocate Tx rings */
1288	for (i = 0; i < vsi->alloc_txq; i++) {
1289		struct ice_ring *ring;
1290
1291		/* allocate with kzalloc(), free with kfree_rcu() */
1292		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1293
1294		if (!ring)
1295			goto err_out;
1296
1297		ring->q_index = i;
1298		ring->reg_idx = vsi->txq_map[i];
1299		ring->ring_active = false;
1300		ring->vsi = vsi;
1301		ring->tx_tstamps = &pf->ptp.port.tx;
1302		ring->dev = dev;
1303		ring->count = vsi->num_tx_desc;
 
 
 
 
 
1304		WRITE_ONCE(vsi->tx_rings[i], ring);
1305	}
1306
1307	/* Allocate Rx rings */
1308	for (i = 0; i < vsi->alloc_rxq; i++) {
1309		struct ice_ring *ring;
1310
1311		/* allocate with kzalloc(), free with kfree_rcu() */
1312		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
1313		if (!ring)
1314			goto err_out;
1315
1316		ring->q_index = i;
1317		ring->reg_idx = vsi->rxq_map[i];
1318		ring->ring_active = false;
1319		ring->vsi = vsi;
1320		ring->netdev = vsi->netdev;
1321		ring->dev = dev;
1322		ring->count = vsi->num_rx_desc;
 
1323		WRITE_ONCE(vsi->rx_rings[i], ring);
1324	}
1325
1326	return 0;
1327
1328err_out:
1329	ice_vsi_clear_rings(vsi);
1330	return -ENOMEM;
1331}
1332
1333/**
1334 * ice_vsi_manage_rss_lut - disable/enable RSS
1335 * @vsi: the VSI being changed
1336 * @ena: boolean value indicating if this is an enable or disable request
1337 *
1338 * In the event of disable request for RSS, this function will zero out RSS
1339 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
1340 * LUT.
1341 */
1342void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1343{
1344	u8 *lut;
1345
1346	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1347	if (!lut)
1348		return;
1349
1350	if (ena) {
1351		if (vsi->rss_lut_user)
1352			memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1353		else
1354			ice_fill_rss_lut(lut, vsi->rss_table_size,
1355					 vsi->rss_size);
1356	}
1357
1358	ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1359	kfree(lut);
1360}
1361
1362/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1363 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
1364 * @vsi: VSI to be configured
1365 */
1366static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
1367{
1368	struct ice_pf *pf = vsi->back;
1369	struct device *dev;
1370	u8 *lut, *key;
1371	int err;
1372
1373	dev = ice_pf_to_dev(pf);
1374	vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1375
1376	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1377	if (!lut)
1378		return -ENOMEM;
1379
1380	if (vsi->rss_lut_user)
1381		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1382	else
1383		ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
1384
1385	err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1386	if (err) {
1387		dev_err(dev, "set_rss_lut failed, error %d\n", err);
1388		goto ice_vsi_cfg_rss_exit;
1389	}
1390
1391	key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1392	if (!key) {
1393		err = -ENOMEM;
1394		goto ice_vsi_cfg_rss_exit;
1395	}
1396
1397	if (vsi->rss_hkey_user)
1398		memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1399	else
1400		netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1401
1402	err = ice_set_rss_key(vsi, key);
1403	if (err)
1404		dev_err(dev, "set_rss_key failed, error %d\n", err);
1405
1406	kfree(key);
1407ice_vsi_cfg_rss_exit:
1408	kfree(lut);
1409	return err;
1410}
1411
1412/**
1413 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
1414 * @vsi: VSI to be configured
1415 *
1416 * This function will only be called during the VF VSI setup. Upon successful
1417 * completion of package download, this function will configure default RSS
1418 * input sets for VF VSI.
1419 */
1420static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
1421{
1422	struct ice_pf *pf = vsi->back;
1423	enum ice_status status;
1424	struct device *dev;
 
1425
1426	dev = ice_pf_to_dev(pf);
1427	if (ice_is_safe_mode(pf)) {
1428		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1429			vsi->vsi_num);
1430		return;
1431	}
1432
1433	status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
1434	if (status)
1435		dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %s\n",
1436			vsi->vsi_num, ice_stat_str(status));
1437}
1438
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1439/**
1440 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
1441 * @vsi: VSI to be configured
1442 *
1443 * This function will only be called after successful download package call
1444 * during initialization of PF. Since the downloaded package will erase the
1445 * RSS section, this function will configure RSS input sets for different
1446 * flow types. The last profile added has the highest priority, therefore 2
1447 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
1448 * (i.e. IPv4 src/dst TCP src/dst port).
1449 */
1450static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
1451{
1452	u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
1453	struct ice_pf *pf = vsi->back;
1454	struct ice_hw *hw = &pf->hw;
1455	enum ice_status status;
1456	struct device *dev;
 
 
1457
1458	dev = ice_pf_to_dev(pf);
1459	if (ice_is_safe_mode(pf)) {
1460		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
1461			vsi_num);
1462		return;
1463	}
1464	/* configure RSS for IPv4 with input set IP src/dst */
1465	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1466				 ICE_FLOW_SEG_HDR_IPV4);
1467	if (status)
1468		dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %s\n",
1469			vsi_num, ice_stat_str(status));
1470
1471	/* configure RSS for IPv6 with input set IPv6 src/dst */
1472	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1473				 ICE_FLOW_SEG_HDR_IPV6);
1474	if (status)
1475		dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %s\n",
1476			vsi_num, ice_stat_str(status));
1477
1478	/* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
1479	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
1480				 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
1481	if (status)
1482		dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %s\n",
1483			vsi_num, ice_stat_str(status));
1484
1485	/* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
1486	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
1487				 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
1488	if (status)
1489		dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %s\n",
1490			vsi_num, ice_stat_str(status));
1491
1492	/* configure RSS for sctp4 with input set IP src/dst */
1493	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
1494				 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
1495	if (status)
1496		dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %s\n",
1497			vsi_num, ice_stat_str(status));
1498
1499	/* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
1500	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
1501				 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
1502	if (status)
1503		dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %s\n",
1504			vsi_num, ice_stat_str(status));
1505
1506	/* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
1507	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
1508				 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
1509	if (status)
1510		dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %s\n",
1511			vsi_num, ice_stat_str(status));
1512
1513	/* configure RSS for sctp6 with input set IPv6 src/dst */
1514	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
1515				 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
1516	if (status)
1517		dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %s\n",
1518			vsi_num, ice_stat_str(status));
1519}
1520
1521/**
1522 * ice_pf_state_is_nominal - checks the PF for nominal state
1523 * @pf: pointer to PF to check
1524 *
1525 * Check the PF's state for a collection of bits that would indicate
1526 * the PF is in a state that would inhibit normal operation for
1527 * driver functionality.
1528 *
1529 * Returns true if PF is in a nominal state, false otherwise
1530 */
1531bool ice_pf_state_is_nominal(struct ice_pf *pf)
1532{
1533	DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1534
1535	if (!pf)
1536		return false;
1537
1538	bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
1539	if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1540		return false;
1541
1542	return true;
1543}
1544
1545/**
1546 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
1547 * @vsi: the VSI to be updated
1548 */
1549void ice_update_eth_stats(struct ice_vsi *vsi)
1550{
1551	struct ice_eth_stats *prev_es, *cur_es;
1552	struct ice_hw *hw = &vsi->back->hw;
 
1553	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */
1554
1555	prev_es = &vsi->eth_stats_prev;
1556	cur_es = &vsi->eth_stats;
1557
 
 
 
1558	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
1559			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1560
1561	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
1562			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1563
1564	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
1565			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1566
1567	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
1568			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1569
1570	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
1571			  &prev_es->rx_discards, &cur_es->rx_discards);
1572
1573	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
1574			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1575
1576	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
1577			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1578
1579	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
1580			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1581
1582	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
1583			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1584
1585	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
1586			  &prev_es->tx_errors, &cur_es->tx_errors);
1587
1588	vsi->stat_offsets_loaded = true;
1589}
1590
1591/**
1592 * ice_vsi_add_vlan - Add VSI membership for given VLAN
1593 * @vsi: the VSI being configured
1594 * @vid: VLAN ID to be added
1595 * @action: filter action to be performed on match
1596 */
1597int
1598ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1599{
1600	struct ice_pf *pf = vsi->back;
1601	struct device *dev;
1602	int err = 0;
1603
1604	dev = ice_pf_to_dev(pf);
1605
1606	if (!ice_fltr_add_vlan(vsi, vid, action)) {
1607		vsi->num_vlan++;
1608	} else {
1609		err = -ENODEV;
1610		dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
1611			vsi->vsi_num);
1612	}
1613
1614	return err;
1615}
1616
1617/**
1618 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
1619 * @vsi: the VSI being configured
1620 * @vid: VLAN ID to be removed
1621 *
1622 * Returns 0 on success and negative on failure
1623 */
1624int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
1625{
1626	struct ice_pf *pf = vsi->back;
1627	enum ice_status status;
1628	struct device *dev;
1629	int err = 0;
1630
1631	dev = ice_pf_to_dev(pf);
1632
1633	status = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1634	if (!status) {
1635		vsi->num_vlan--;
1636	} else if (status == ICE_ERR_DOES_NOT_EXIST) {
1637		dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, status: %s\n",
1638			vid, vsi->vsi_num, ice_stat_str(status));
1639	} else {
1640		dev_err(dev, "Error removing VLAN %d on vsi %i error: %s\n",
1641			vid, vsi->vsi_num, ice_stat_str(status));
1642		err = -EIO;
1643	}
1644
1645	return err;
1646}
1647
1648/**
1649 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
1650 * @vsi: VSI
1651 */
1652void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
1653{
1654	if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
1655		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1656		vsi->rx_buf_len = ICE_RXBUF_2048;
1657#if (PAGE_SIZE < 8192)
1658	} else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
1659		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
1660		vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
1661		vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
1662#endif
1663	} else {
1664		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
1665#if (PAGE_SIZE < 8192)
1666		vsi->rx_buf_len = ICE_RXBUF_3072;
1667#else
1668		vsi->rx_buf_len = ICE_RXBUF_2048;
1669#endif
1670	}
1671}
1672
1673/**
1674 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
1675 * @hw: HW pointer
1676 * @pf_q: index of the Rx queue in the PF's queue space
1677 * @rxdid: flexible descriptor RXDID
1678 * @prio: priority for the RXDID for this queue
1679 * @ena_ts: true to enable timestamp and false to disable timestamp
1680 */
1681void
1682ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
1683			bool ena_ts)
1684{
1685	int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
1686
1687	/* clear any previous values */
1688	regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
1689		    QRXFLXP_CNTXT_RXDID_PRIO_M |
1690		    QRXFLXP_CNTXT_TS_M);
1691
1692	regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
1693		QRXFLXP_CNTXT_RXDID_IDX_M;
1694
1695	regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
1696		QRXFLXP_CNTXT_RXDID_PRIO_M;
1697
1698	if (ena_ts)
1699		/* Enable TimeSync on this queue */
1700		regval |= QRXFLXP_CNTXT_TS_M;
1701
1702	wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
1703}
1704
1705int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx)
1706{
1707	if (q_idx >= vsi->num_rxq)
1708		return -EINVAL;
1709
1710	return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]);
1711}
1712
1713int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_ring **tx_rings, u16 q_idx)
1714{
1715	struct ice_aqc_add_tx_qgrp *qg_buf;
1716	int err;
1717
1718	if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx])
1719		return -EINVAL;
1720
1721	qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1722	if (!qg_buf)
1723		return -ENOMEM;
1724
1725	qg_buf->num_txqs = 1;
1726
1727	err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf);
1728	kfree(qg_buf);
1729	return err;
1730}
1731
1732/**
1733 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
1734 * @vsi: the VSI being configured
1735 *
1736 * Return 0 on success and a negative value on error
1737 * Configure the Rx VSI for operation.
1738 */
1739int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
1740{
1741	u16 i;
1742
1743	if (vsi->type == ICE_VSI_VF)
1744		goto setup_rings;
1745
1746	ice_vsi_cfg_frame_size(vsi);
1747setup_rings:
1748	/* set up individual rings */
1749	ice_for_each_rxq(vsi, i) {
1750		int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]);
1751
1752		if (err)
1753			return err;
1754	}
1755
1756	return 0;
1757}
1758
1759/**
1760 * ice_vsi_cfg_txqs - Configure the VSI for Tx
1761 * @vsi: the VSI being configured
1762 * @rings: Tx ring array to be configured
1763 * @count: number of Tx ring array elements
1764 *
1765 * Return 0 on success and a negative value on error
1766 * Configure the Tx VSI for operation.
1767 */
1768static int
1769ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings, u16 count)
1770{
1771	struct ice_aqc_add_tx_qgrp *qg_buf;
1772	u16 q_idx = 0;
1773	int err = 0;
1774
1775	qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1776	if (!qg_buf)
1777		return -ENOMEM;
1778
1779	qg_buf->num_txqs = 1;
1780
1781	for (q_idx = 0; q_idx < count; q_idx++) {
1782		err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
1783		if (err)
1784			goto err_cfg_txqs;
1785	}
1786
1787err_cfg_txqs:
1788	kfree(qg_buf);
1789	return err;
1790}
1791
1792/**
1793 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
1794 * @vsi: the VSI being configured
1795 *
1796 * Return 0 on success and a negative value on error
1797 * Configure the Tx VSI for operation.
1798 */
1799int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
1800{
1801	return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1802}
1803
1804/**
1805 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
1806 * @vsi: the VSI being configured
1807 *
1808 * Return 0 on success and a negative value on error
1809 * Configure the Tx queues dedicated for XDP in given VSI for operation.
1810 */
1811int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
1812{
1813	int ret;
1814	int i;
1815
1816	ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1817	if (ret)
1818		return ret;
1819
1820	for (i = 0; i < vsi->num_xdp_txq; i++)
1821		vsi->xdp_rings[i]->xsk_pool = ice_xsk_pool(vsi->xdp_rings[i]);
1822
1823	return ret;
1824}
1825
1826/**
1827 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
1828 * @intrl: interrupt rate limit in usecs
1829 * @gran: interrupt rate limit granularity in usecs
1830 *
1831 * This function converts a decimal interrupt rate limit in usecs to the format
1832 * expected by firmware.
1833 */
1834static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1835{
1836	u32 val = intrl / gran;
1837
1838	if (val)
1839		return val | GLINT_RATE_INTRL_ENA_M;
1840	return 0;
1841}
1842
1843/**
1844 * ice_write_intrl - write throttle rate limit to interrupt specific register
1845 * @q_vector: pointer to interrupt specific structure
1846 * @intrl: throttle rate limit in microseconds to write
1847 */
1848void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
1849{
1850	struct ice_hw *hw = &q_vector->vsi->back->hw;
1851
1852	wr32(hw, GLINT_RATE(q_vector->reg_idx),
1853	     ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
1854}
1855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1856/**
1857 * __ice_write_itr - write throttle rate to register
1858 * @q_vector: pointer to interrupt data structure
1859 * @rc: pointer to ring container
1860 * @itr: throttle rate in microseconds to write
1861 */
1862static void __ice_write_itr(struct ice_q_vector *q_vector,
1863			    struct ice_ring_container *rc, u16 itr)
1864{
1865	struct ice_hw *hw = &q_vector->vsi->back->hw;
1866
1867	wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
1868	     ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
1869}
1870
1871/**
1872 * ice_write_itr - write throttle rate to queue specific register
1873 * @rc: pointer to ring container
1874 * @itr: throttle rate in microseconds to write
1875 */
1876void ice_write_itr(struct ice_ring_container *rc, u16 itr)
1877{
1878	struct ice_q_vector *q_vector;
1879
1880	if (!rc->ring)
 
1881		return;
1882
1883	q_vector = rc->ring->q_vector;
 
1884
1885	__ice_write_itr(q_vector, rc, itr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1886}
1887
1888/**
1889 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
1890 * @vsi: the VSI being configured
1891 *
1892 * This configures MSIX mode interrupts for the PF VSI, and should not be used
1893 * for the VF VSI.
1894 */
1895void ice_vsi_cfg_msix(struct ice_vsi *vsi)
1896{
1897	struct ice_pf *pf = vsi->back;
1898	struct ice_hw *hw = &pf->hw;
1899	u16 txq = 0, rxq = 0;
1900	int i, q;
1901
1902	for (i = 0; i < vsi->num_q_vectors; i++) {
1903		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1904		u16 reg_idx = q_vector->reg_idx;
1905
1906		ice_cfg_itr(hw, q_vector);
1907
1908		/* Both Transmit Queue Interrupt Cause Control register
1909		 * and Receive Queue Interrupt Cause control register
1910		 * expects MSIX_INDX field to be the vector index
1911		 * within the function space and not the absolute
1912		 * vector index across PF or across device.
1913		 * For SR-IOV VF VSIs queue vector index always starts
1914		 * with 1 since first vector index(0) is used for OICR
1915		 * in VF space. Since VMDq and other PF VSIs are within
1916		 * the PF function space, use the vector index that is
1917		 * tracked for this PF.
1918		 */
1919		for (q = 0; q < q_vector->num_ring_tx; q++) {
1920			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
1921					      q_vector->tx.itr_idx);
1922			txq++;
1923		}
1924
1925		for (q = 0; q < q_vector->num_ring_rx; q++) {
1926			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
1927					      q_vector->rx.itr_idx);
1928			rxq++;
1929		}
1930	}
1931}
1932
1933/**
1934 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
1935 * @vsi: the VSI being changed
1936 */
1937int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
1938{
1939	struct ice_hw *hw = &vsi->back->hw;
1940	struct ice_vsi_ctx *ctxt;
1941	enum ice_status status;
1942	int ret = 0;
1943
1944	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1945	if (!ctxt)
1946		return -ENOMEM;
1947
1948	/* Here we are configuring the VSI to let the driver add VLAN tags by
1949	 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
1950	 * insertion happens in the Tx hot path, in ice_tx_map.
1951	 */
1952	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1953
1954	/* Preserve existing VLAN strip setting */
1955	ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
1956				  ICE_AQ_VSI_VLAN_EMOD_M);
1957
1958	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1959
1960	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1961	if (status) {
1962		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %s aq_err %s\n",
1963			ice_stat_str(status),
1964			ice_aq_str(hw->adminq.sq_last_status));
1965		ret = -EIO;
1966		goto out;
1967	}
1968
1969	vsi->info.vlan_flags = ctxt->info.vlan_flags;
1970out:
1971	kfree(ctxt);
1972	return ret;
1973}
1974
1975/**
1976 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
1977 * @vsi: the VSI being changed
1978 * @ena: boolean value indicating if this is a enable or disable request
1979 */
1980int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
1981{
1982	struct ice_hw *hw = &vsi->back->hw;
1983	struct ice_vsi_ctx *ctxt;
1984	enum ice_status status;
1985	int ret = 0;
1986
1987	/* do not allow modifying VLAN stripping when a port VLAN is configured
1988	 * on this VSI
1989	 */
1990	if (vsi->info.pvid)
1991		return 0;
1992
1993	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1994	if (!ctxt)
1995		return -ENOMEM;
1996
1997	/* Here we are configuring what the VSI should do with the VLAN tag in
1998	 * the Rx packet. We can either leave the tag in the packet or put it in
1999	 * the Rx descriptor.
2000	 */
2001	if (ena)
2002		/* Strip VLAN tag from Rx packet and put it in the desc */
2003		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
2004	else
2005		/* Disable stripping. Leave tag in packet */
2006		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
2007
2008	/* Allow all packets untagged/tagged */
2009	ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
2010
2011	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
2012
2013	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
2014	if (status) {
2015		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %s aq_err %s\n",
2016			ena, ice_stat_str(status),
2017			ice_aq_str(hw->adminq.sq_last_status));
2018		ret = -EIO;
2019		goto out;
2020	}
2021
2022	vsi->info.vlan_flags = ctxt->info.vlan_flags;
2023out:
2024	kfree(ctxt);
2025	return ret;
2026}
2027
2028/**
2029 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
2030 * @vsi: the VSI whose rings are to be enabled
2031 *
2032 * Returns 0 on success and a negative value on error
2033 */
2034int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
2035{
2036	return ice_vsi_ctrl_all_rx_rings(vsi, true);
2037}
2038
2039/**
2040 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
2041 * @vsi: the VSI whose rings are to be disabled
2042 *
2043 * Returns 0 on success and a negative value on error
2044 */
2045int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2046{
2047	return ice_vsi_ctrl_all_rx_rings(vsi, false);
2048}
2049
2050/**
2051 * ice_vsi_stop_tx_rings - Disable Tx rings
2052 * @vsi: the VSI being configured
2053 * @rst_src: reset source
2054 * @rel_vmvf_num: Relative ID of VF/VM
2055 * @rings: Tx ring array to be stopped
2056 * @count: number of Tx ring array elements
2057 */
2058static int
2059ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2060		      u16 rel_vmvf_num, struct ice_ring **rings, u16 count)
2061{
2062	u16 q_idx;
2063
2064	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
2065		return -EINVAL;
2066
2067	for (q_idx = 0; q_idx < count; q_idx++) {
2068		struct ice_txq_meta txq_meta = { };
2069		int status;
2070
2071		if (!rings || !rings[q_idx])
2072			return -EINVAL;
2073
2074		ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
2075		status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
2076					      rings[q_idx], &txq_meta);
2077
2078		if (status)
2079			return status;
2080	}
2081
2082	return 0;
2083}
2084
2085/**
2086 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
2087 * @vsi: the VSI being configured
2088 * @rst_src: reset source
2089 * @rel_vmvf_num: Relative ID of VF/VM
2090 */
2091int
2092ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2093			  u16 rel_vmvf_num)
2094{
2095	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2096}
2097
2098/**
2099 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
2100 * @vsi: the VSI being configured
2101 */
2102int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
2103{
2104	return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
2105}
2106
2107/**
2108 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
2109 * @vsi: VSI to check whether or not VLAN pruning is enabled.
2110 *
2111 * returns true if Rx VLAN pruning is enabled and false otherwise.
2112 */
2113bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
2114{
2115	if (!vsi)
2116		return false;
 
2117
2118	return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2119}
2120
2121/**
2122 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
2123 * @vsi: VSI to enable or disable VLAN pruning on
2124 * @ena: set to true to enable VLAN pruning and false to disable it
2125 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
2126 *
2127 * returns 0 if VSI is updated, negative otherwise
2128 */
2129int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
2130{
2131	struct ice_vsi_ctx *ctxt;
2132	struct ice_pf *pf;
2133	int status;
2134
2135	if (!vsi)
2136		return -EINVAL;
2137
2138	/* Don't enable VLAN pruning if the netdev is currently in promiscuous
2139	 * mode. VLAN pruning will be enabled when the interface exits
2140	 * promiscuous mode if any VLAN filters are active.
2141	 */
2142	if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
2143		return 0;
2144
2145	pf = vsi->back;
2146	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2147	if (!ctxt)
2148		return -ENOMEM;
2149
2150	ctxt->info = vsi->info;
2151
2152	if (ena)
2153		ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2154	else
2155		ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
2156
2157	if (!vlan_promisc)
2158		ctxt->info.valid_sections =
2159			cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2160
2161	status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2162	if (status) {
2163		netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %s, aq_err = %s\n",
2164			   ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
2165			   ice_stat_str(status),
2166			   ice_aq_str(pf->hw.adminq.sq_last_status));
2167		goto err_out;
2168	}
2169
2170	vsi->info.sw_flags2 = ctxt->info.sw_flags2;
2171
2172	kfree(ctxt);
2173	return 0;
2174
2175err_out:
2176	kfree(ctxt);
2177	return -EIO;
2178}
2179
2180static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
2181{
2182	struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
2183
2184	vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
2185	vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
2186}
2187
2188/**
2189 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
2190 * @vsi: VSI to set the q_vectors register index on
2191 */
2192static int
2193ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
2194{
2195	u16 i;
2196
2197	if (!vsi || !vsi->q_vectors)
2198		return -EINVAL;
2199
2200	ice_for_each_q_vector(vsi, i) {
2201		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2202
2203		if (!q_vector) {
2204			dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2205				i, vsi->vsi_num);
2206			goto clear_reg_idx;
2207		}
2208
2209		if (vsi->type == ICE_VSI_VF) {
2210			struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];
2211
2212			q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
2213		} else {
2214			q_vector->reg_idx =
2215				q_vector->v_idx + vsi->base_vector;
2216		}
2217	}
2218
2219	return 0;
2220
2221clear_reg_idx:
2222	ice_for_each_q_vector(vsi, i) {
2223		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2224
2225		if (q_vector)
2226			q_vector->reg_idx = 0;
2227	}
2228
2229	return -EINVAL;
 
2230}
2231
2232/**
2233 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
2234 * @vsi: the VSI being configured
2235 * @tx: bool to determine Tx or Rx rule
2236 * @create: bool to determine create or remove Rule
2237 */
2238void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
2239{
2240	enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
2241				    enum ice_sw_fwd_act_type act);
2242	struct ice_pf *pf = vsi->back;
2243	enum ice_status status;
2244	struct device *dev;
 
2245
2246	dev = ice_pf_to_dev(pf);
2247	eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2248
2249	if (tx) {
2250		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
2251				  ICE_DROP_PACKET);
2252	} else {
2253		if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
2254			status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
2255							  create);
2256		} else {
2257			status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
2258					  ICE_FWD_TO_VSI);
2259		}
2260	}
2261
2262	if (status)
2263		dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
2264			create ? "adding" : "removing", tx ? "TX" : "RX",
2265			vsi->vsi_num, ice_stat_str(status));
2266}
2267
2268/**
2269 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
2270 * @vsi: pointer to the VSI
2271 *
2272 * This function will allocate new scheduler aggregator now if needed and will
2273 * move specified VSI into it.
2274 */
2275static void ice_set_agg_vsi(struct ice_vsi *vsi)
2276{
2277	struct device *dev = ice_pf_to_dev(vsi->back);
2278	struct ice_agg_node *agg_node_iter = NULL;
2279	u32 agg_id = ICE_INVALID_AGG_NODE_ID;
2280	struct ice_agg_node *agg_node = NULL;
2281	int node_offset, max_agg_nodes = 0;
2282	struct ice_port_info *port_info;
2283	struct ice_pf *pf = vsi->back;
2284	u32 agg_node_id_start = 0;
2285	enum ice_status status;
2286
2287	/* create (as needed) scheduler aggregator node and move VSI into
2288	 * corresponding aggregator node
2289	 * - PF aggregator node to contains VSIs of type _PF and _CTRL
2290	 * - VF aggregator nodes will contain VF VSI
2291	 */
2292	port_info = pf->hw.port_info;
2293	if (!port_info)
2294		return;
2295
2296	switch (vsi->type) {
2297	case ICE_VSI_CTRL:
 
2298	case ICE_VSI_LB:
2299	case ICE_VSI_PF:
 
2300		max_agg_nodes = ICE_MAX_PF_AGG_NODES;
2301		agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
2302		agg_node_iter = &pf->pf_agg_node[0];
2303		break;
2304	case ICE_VSI_VF:
2305		/* user can create 'n' VFs on a given PF, but since max children
2306		 * per aggregator node can be only 64. Following code handles
2307		 * aggregator(s) for VF VSIs, either selects a agg_node which
2308		 * was already created provided num_vsis < 64, otherwise
2309		 * select next available node, which will be created
2310		 */
2311		max_agg_nodes = ICE_MAX_VF_AGG_NODES;
2312		agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
2313		agg_node_iter = &pf->vf_agg_node[0];
2314		break;
2315	default:
2316		/* other VSI type, handle later if needed */
2317		dev_dbg(dev, "unexpected VSI type %s\n",
2318			ice_vsi_type_str(vsi->type));
2319		return;
2320	}
2321
2322	/* find the appropriate aggregator node */
2323	for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
2324		/* see if we can find space in previously created
2325		 * node if num_vsis < 64, otherwise skip
2326		 */
2327		if (agg_node_iter->num_vsis &&
2328		    agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
2329			agg_node_iter++;
2330			continue;
2331		}
2332
2333		if (agg_node_iter->valid &&
2334		    agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
2335			agg_id = agg_node_iter->agg_id;
2336			agg_node = agg_node_iter;
2337			break;
2338		}
2339
2340		/* find unclaimed agg_id */
2341		if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
2342			agg_id = node_offset + agg_node_id_start;
2343			agg_node = agg_node_iter;
2344			break;
2345		}
2346		/* move to next agg_node */
2347		agg_node_iter++;
2348	}
2349
2350	if (!agg_node)
2351		return;
2352
2353	/* if selected aggregator node was not created, create it */
2354	if (!agg_node->valid) {
2355		status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
2356				     (u8)vsi->tc_cfg.ena_tc);
2357		if (status) {
2358			dev_err(dev, "unable to create aggregator node with agg_id %u\n",
2359				agg_id);
2360			return;
2361		}
2362		/* aggregator node is created, store the neeeded info */
2363		agg_node->valid = true;
2364		agg_node->agg_id = agg_id;
2365	}
2366
2367	/* move VSI to corresponding aggregator node */
2368	status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
2369				     (u8)vsi->tc_cfg.ena_tc);
2370	if (status) {
2371		dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
2372			vsi->idx, agg_id);
2373		return;
2374	}
2375
2376	/* keep active children count for aggregator node */
2377	agg_node->num_vsis++;
2378
2379	/* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
2380	 * to aggregator node
2381	 */
2382	vsi->agg_node = agg_node;
2383	dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
2384		vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
2385		vsi->agg_node->num_vsis);
2386}
2387
2388/**
2389 * ice_vsi_setup - Set up a VSI by a given type
2390 * @pf: board private structure
2391 * @pi: pointer to the port_info instance
2392 * @vsi_type: VSI type
2393 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2394 *         used only for ICE_VSI_VF VSI type. For other VSI types, should
2395 *         fill-in ICE_INVAL_VFID as input.
2396 *
2397 * This allocates the sw VSI structure and its queue resources.
2398 *
2399 * Returns pointer to the successfully allocated and configured VSI sw struct on
2400 * success, NULL on failure.
2401 */
2402struct ice_vsi *
2403ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
2404	      enum ice_vsi_type vsi_type, u16 vf_id)
2405{
2406	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2407	struct device *dev = ice_pf_to_dev(pf);
2408	enum ice_status status;
2409	struct ice_vsi *vsi;
2410	int ret, i;
2411
2412	if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2413		vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
2414	else
2415		vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
 
 
 
 
 
 
 
 
 
 
 
 
 
2416
2417	if (!vsi) {
2418		dev_err(dev, "could not allocate VSI\n");
2419		return NULL;
 
 
 
 
2420	}
2421
2422	vsi->port_info = pi;
 
 
 
 
 
 
 
 
 
 
 
 
2423	vsi->vsw = pf->first_sw;
2424	if (vsi->type == ICE_VSI_PF)
2425		vsi->ethtype = ETH_P_PAUSE;
2426
2427	if (vsi->type == ICE_VSI_VF || vsi->type == ICE_VSI_CTRL)
2428		vsi->vf_id = vf_id;
 
 
 
 
 
 
2429
2430	ice_alloc_fd_res(vsi);
2431
2432	if (ice_vsi_get_qs(vsi)) {
 
2433		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
2434			vsi->idx);
2435		goto unroll_vsi_alloc;
2436	}
2437
2438	/* set RSS capabilities */
2439	ice_vsi_set_rss_params(vsi);
2440
2441	/* set TC configuration */
2442	ice_vsi_set_tc_cfg(vsi);
2443
2444	/* create the VSI */
2445	ret = ice_vsi_init(vsi, true);
2446	if (ret)
2447		goto unroll_get_qs;
2448
 
 
2449	switch (vsi->type) {
2450	case ICE_VSI_CTRL:
 
2451	case ICE_VSI_PF:
2452		ret = ice_vsi_alloc_q_vectors(vsi);
2453		if (ret)
2454			goto unroll_vsi_init;
2455
2456		ret = ice_vsi_setup_vector_base(vsi);
2457		if (ret)
2458			goto unroll_alloc_q_vector;
2459
2460		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2461		if (ret)
2462			goto unroll_vector_base;
2463
2464		ret = ice_vsi_alloc_rings(vsi);
2465		if (ret)
2466			goto unroll_vector_base;
2467
2468		/* Always add VLAN ID 0 switch rule by default. This is needed
2469		 * in order to allow all untagged and 0 tagged priority traffic
2470		 * if Rx VLAN pruning is enabled. Also there are cases where we
2471		 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
2472		 * so this handles those cases (i.e. adding the PF to a bridge
2473		 * without the 8021q module loaded).
2474		 */
2475		ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2476		if (ret)
2477			goto unroll_clear_rings;
2478
2479		ice_vsi_map_rings_to_vectors(vsi);
2480
 
 
2481		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
2482		if (vsi->type != ICE_VSI_CTRL)
2483			/* Do not exit if configuring RSS had an issue, at
2484			 * least receive traffic on first queue. Hence no
2485			 * need to capture return value
2486			 */
2487			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2488				ice_vsi_cfg_rss_lut_key(vsi);
2489				ice_vsi_set_rss_flow_fld(vsi);
2490			}
2491		ice_init_arfs(vsi);
2492		break;
 
 
 
 
 
 
2493	case ICE_VSI_VF:
2494		/* VF driver will take care of creating netdev for this type and
2495		 * map queues to vectors through Virtchnl, PF driver only
2496		 * creates a VSI and corresponding structures for bookkeeping
2497		 * purpose
2498		 */
2499		ret = ice_vsi_alloc_q_vectors(vsi);
2500		if (ret)
2501			goto unroll_vsi_init;
2502
2503		ret = ice_vsi_alloc_rings(vsi);
2504		if (ret)
2505			goto unroll_alloc_q_vector;
2506
2507		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
2508		if (ret)
2509			goto unroll_vector_base;
2510
 
 
2511		/* Do not exit if configuring RSS had an issue, at least
2512		 * receive traffic on first queue. Hence no need to capture
2513		 * return value
2514		 */
2515		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2516			ice_vsi_cfg_rss_lut_key(vsi);
2517			ice_vsi_set_vf_rss_flow_fld(vsi);
2518		}
2519		break;
2520	case ICE_VSI_LB:
2521		ret = ice_vsi_alloc_rings(vsi);
2522		if (ret)
2523			goto unroll_vsi_init;
 
 
 
 
 
2524		break;
2525	default:
2526		/* clean up the resources and exit */
 
2527		goto unroll_vsi_init;
2528	}
2529
2530	/* configure VSI nodes based on number of queues and TC's */
2531	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2532		max_txqs[i] = vsi->alloc_txq;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2533
2534	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2535				 max_txqs);
2536	if (status) {
2537		dev_err(dev, "VSI %d failed lan queue config, error %s\n",
2538			vsi->vsi_num, ice_stat_str(status));
2539		goto unroll_clear_rings;
2540	}
2541
 
 
 
 
 
2542	/* Add switch rule to drop all Tx Flow Control Frames, of look up
2543	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
2544	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
2545	 * The rule is added once for PF VSI in order to create appropriate
2546	 * recipe, since VSI/VSI list is ignored with drop action...
2547	 * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
2548	 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
2549	 * settings in the HW.
2550	 */
2551	if (!ice_is_safe_mode(pf))
2552		if (vsi->type == ICE_VSI_PF) {
2553			ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2554					 ICE_DROP_PACKET);
2555			ice_cfg_sw_lldp(vsi, true, true);
2556		}
2557
2558	if (!vsi->agg_node)
2559		ice_set_agg_vsi(vsi);
 
2560	return vsi;
2561
2562unroll_clear_rings:
2563	ice_vsi_clear_rings(vsi);
2564unroll_vector_base:
2565	/* reclaim SW interrupts back to the common pool */
2566	ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2567	pf->num_avail_sw_msix += vsi->num_q_vectors;
2568unroll_alloc_q_vector:
2569	ice_vsi_free_q_vectors(vsi);
2570unroll_vsi_init:
2571	ice_vsi_delete(vsi);
2572unroll_get_qs:
2573	ice_vsi_put_qs(vsi);
2574unroll_vsi_alloc:
2575	if (vsi_type == ICE_VSI_VF)
2576		ice_enable_lag(pf->lag);
2577	ice_vsi_clear(vsi);
2578
2579	return NULL;
2580}
2581
2582/**
2583 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
2584 * @vsi: the VSI being cleaned up
2585 */
2586static void ice_vsi_release_msix(struct ice_vsi *vsi)
2587{
2588	struct ice_pf *pf = vsi->back;
2589	struct ice_hw *hw = &pf->hw;
2590	u32 txq = 0;
2591	u32 rxq = 0;
2592	int i, q;
2593
2594	for (i = 0; i < vsi->num_q_vectors; i++) {
2595		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2596
2597		ice_write_intrl(q_vector, 0);
2598		for (q = 0; q < q_vector->num_ring_tx; q++) {
2599			ice_write_itr(&q_vector->tx, 0);
2600			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
2601			if (ice_is_xdp_ena_vsi(vsi)) {
2602				u32 xdp_txq = txq + vsi->num_xdp_txq;
2603
2604				wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
2605			}
2606			txq++;
2607		}
2608
2609		for (q = 0; q < q_vector->num_ring_rx; q++) {
2610			ice_write_itr(&q_vector->rx, 0);
2611			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
2612			rxq++;
2613		}
2614	}
2615
2616	ice_flush(hw);
2617}
2618
2619/**
2620 * ice_vsi_free_irq - Free the IRQ association with the OS
2621 * @vsi: the VSI being configured
2622 */
2623void ice_vsi_free_irq(struct ice_vsi *vsi)
2624{
2625	struct ice_pf *pf = vsi->back;
2626	int base = vsi->base_vector;
2627	int i;
2628
2629	if (!vsi->q_vectors || !vsi->irqs_ready)
2630		return;
2631
2632	ice_vsi_release_msix(vsi);
2633	if (vsi->type == ICE_VSI_VF)
2634		return;
2635
2636	vsi->irqs_ready = false;
 
 
2637	ice_for_each_q_vector(vsi, i) {
2638		u16 vector = i + base;
2639		int irq_num;
2640
2641		irq_num = pf->msix_entries[vector].vector;
2642
2643		/* free only the irqs that were actually requested */
2644		if (!vsi->q_vectors[i] ||
2645		    !(vsi->q_vectors[i]->num_ring_tx ||
2646		      vsi->q_vectors[i]->num_ring_rx))
2647			continue;
2648
2649		/* clear the affinity notifier in the IRQ descriptor */
2650		irq_set_affinity_notifier(irq_num, NULL);
 
2651
2652		/* clear the affinity_mask in the IRQ descriptor */
2653		irq_set_affinity_hint(irq_num, NULL);
2654		synchronize_irq(irq_num);
2655		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2656	}
2657}
2658
2659/**
2660 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
2661 * @vsi: the VSI having resources freed
2662 */
2663void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
2664{
2665	int i;
2666
2667	if (!vsi->tx_rings)
2668		return;
2669
2670	ice_for_each_txq(vsi, i)
2671		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2672			ice_free_tx_ring(vsi->tx_rings[i]);
2673}
2674
2675/**
2676 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
2677 * @vsi: the VSI having resources freed
2678 */
2679void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
2680{
2681	int i;
2682
2683	if (!vsi->rx_rings)
2684		return;
2685
2686	ice_for_each_rxq(vsi, i)
2687		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2688			ice_free_rx_ring(vsi->rx_rings[i]);
2689}
2690
2691/**
2692 * ice_vsi_close - Shut down a VSI
2693 * @vsi: the VSI being shut down
2694 */
2695void ice_vsi_close(struct ice_vsi *vsi)
2696{
2697	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2698		ice_down(vsi);
2699
 
2700	ice_vsi_free_irq(vsi);
2701	ice_vsi_free_tx_rings(vsi);
2702	ice_vsi_free_rx_rings(vsi);
2703}
2704
2705/**
2706 * ice_ena_vsi - resume a VSI
2707 * @vsi: the VSI being resume
2708 * @locked: is the rtnl_lock already held
2709 */
2710int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
2711{
2712	int err = 0;
2713
2714	if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2715		return 0;
2716
2717	clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2718
2719	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
 
2720		if (netif_running(vsi->netdev)) {
2721			if (!locked)
2722				rtnl_lock();
2723
2724			err = ice_open_internal(vsi->netdev);
2725
2726			if (!locked)
2727				rtnl_unlock();
2728		}
2729	} else if (vsi->type == ICE_VSI_CTRL) {
2730		err = ice_vsi_open_ctrl(vsi);
2731	}
2732
2733	return err;
2734}
2735
2736/**
2737 * ice_dis_vsi - pause a VSI
2738 * @vsi: the VSI being paused
2739 * @locked: is the rtnl_lock already held
2740 */
2741void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
2742{
2743	if (test_bit(ICE_VSI_DOWN, vsi->state))
2744		return;
2745
2746	set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2747
2748	if (vsi->type == ICE_VSI_PF && vsi->netdev) {
 
2749		if (netif_running(vsi->netdev)) {
2750			if (!locked)
2751				rtnl_lock();
2752
2753			ice_vsi_close(vsi);
 
2754
2755			if (!locked)
2756				rtnl_unlock();
2757		} else {
2758			ice_vsi_close(vsi);
2759		}
2760	} else if (vsi->type == ICE_VSI_CTRL) {
2761		ice_vsi_close(vsi);
2762	}
2763}
2764
2765/**
2766 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
2767 * @vsi: the VSI being un-configured
 
 
 
2768 */
2769void ice_vsi_dis_irq(struct ice_vsi *vsi)
2770{
2771	int base = vsi->base_vector;
2772	struct ice_pf *pf = vsi->back;
2773	struct ice_hw *hw = &pf->hw;
2774	u32 val;
2775	int i;
2776
2777	/* disable interrupt causation from each queue */
2778	if (vsi->tx_rings) {
2779		ice_for_each_txq(vsi, i) {
2780			if (vsi->tx_rings[i]) {
2781				u16 reg;
2782
2783				reg = vsi->tx_rings[i]->reg_idx;
2784				val = rd32(hw, QINT_TQCTL(reg));
2785				val &= ~QINT_TQCTL_CAUSE_ENA_M;
2786				wr32(hw, QINT_TQCTL(reg), val);
2787			}
2788		}
2789	}
2790
2791	if (vsi->rx_rings) {
2792		ice_for_each_rxq(vsi, i) {
2793			if (vsi->rx_rings[i]) {
2794				u16 reg;
 
 
2795
2796				reg = vsi->rx_rings[i]->reg_idx;
2797				val = rd32(hw, QINT_RQCTL(reg));
2798				val &= ~QINT_RQCTL_CAUSE_ENA_M;
2799				wr32(hw, QINT_RQCTL(reg), val);
2800			}
2801		}
2802	}
 
2803
2804	/* disable each interrupt */
2805	ice_for_each_q_vector(vsi, i) {
2806		if (!vsi->q_vectors[i])
2807			continue;
2808		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
2809	}
 
 
 
 
 
2810
2811	ice_flush(hw);
 
2812
2813	/* don't call synchronize_irq() for VF's from the host */
2814	if (vsi->type == ICE_VSI_VF)
2815		return;
2816
2817	ice_for_each_q_vector(vsi, i)
2818		synchronize_irq(pf->msix_entries[i + base].vector);
2819}
2820
2821/**
2822 * ice_napi_del - Remove NAPI handler for the VSI
2823 * @vsi: VSI for which NAPI handler is to be removed
 
 
 
 
2824 */
2825void ice_napi_del(struct ice_vsi *vsi)
2826{
2827	int v_idx;
2828
2829	if (!vsi->netdev)
2830		return;
2831
2832	ice_for_each_q_vector(vsi, v_idx)
2833		netif_napi_del(&vsi->q_vectors[v_idx]->napi);
 
 
 
2834}
2835
2836/**
2837 * ice_vsi_release - Delete a VSI and free its resources
2838 * @vsi: the VSI being removed
2839 *
2840 * Returns 0 on success or < 0 on error
2841 */
2842int ice_vsi_release(struct ice_vsi *vsi)
2843{
2844	enum ice_status err;
2845	struct ice_pf *pf;
2846
2847	if (!vsi->back)
2848		return -ENODEV;
2849	pf = vsi->back;
2850
2851	/* do not unregister while driver is in the reset recovery pending
2852	 * state. Since reset/rebuild happens through PF service task workqueue,
2853	 * it's not a good idea to unregister netdev that is associated to the
2854	 * PF that is running the work queue items currently. This is done to
2855	 * avoid check_flush_dependency() warning on this wq
2856	 */
2857	if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
2858	    (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2859		unregister_netdev(vsi->netdev);
2860		clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2861	}
2862
2863	ice_devlink_destroy_port(vsi);
2864
2865	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2866		ice_rss_clean(vsi);
2867
2868	/* Disable VSI and free resources */
2869	if (vsi->type != ICE_VSI_LB)
2870		ice_vsi_dis_irq(vsi);
2871	ice_vsi_close(vsi);
2872
2873	/* SR-IOV determines needed MSIX resources all at once instead of per
2874	 * VSI since when VFs are spawned we know how many VFs there are and how
2875	 * many interrupts each VF needs. SR-IOV MSIX resources are also
2876	 * cleared in the same manner.
2877	 */
2878	if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
2879		int i;
2880
2881		ice_for_each_vf(pf, i) {
2882			struct ice_vf *vf = &pf->vf[i];
2883
2884			if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI)
2885				break;
2886		}
2887		if (i == pf->num_alloc_vfs) {
2888			/* No other VFs left that have control VSI, reclaim SW
2889			 * interrupts back to the common pool
2890			 */
2891			ice_free_res(pf->irq_tracker, vsi->base_vector,
2892				     ICE_RES_VF_CTRL_VEC_ID);
2893			pf->num_avail_sw_msix += vsi->num_q_vectors;
2894		}
2895	} else if (vsi->type != ICE_VSI_VF) {
2896		/* reclaim SW interrupts back to the common pool */
2897		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2898		pf->num_avail_sw_msix += vsi->num_q_vectors;
2899	}
2900
2901	if (!ice_is_safe_mode(pf)) {
2902		if (vsi->type == ICE_VSI_PF) {
2903			ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
2904					    ICE_DROP_PACKET);
2905			ice_cfg_sw_lldp(vsi, true, false);
2906			/* The Rx rule will only exist to remove if the LLDP FW
2907			 * engine is currently stopped
2908			 */
2909			if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
2910				ice_cfg_sw_lldp(vsi, false, false);
2911		}
2912	}
2913
2914	ice_fltr_remove_all(vsi);
2915	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2916	err = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
2917	if (err)
2918		dev_err(ice_pf_to_dev(vsi->back), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
2919			vsi->vsi_num, err);
2920	ice_vsi_delete(vsi);
2921	ice_vsi_free_q_vectors(vsi);
2922
2923	if (vsi->netdev) {
2924		if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
2925			unregister_netdev(vsi->netdev);
2926			clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2927		}
2928		if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
2929			free_netdev(vsi->netdev);
2930			vsi->netdev = NULL;
2931			clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
2932		}
2933	}
2934
2935	if (vsi->type == ICE_VSI_VF &&
2936	    vsi->agg_node && vsi->agg_node->valid)
2937		vsi->agg_node->num_vsis--;
2938	ice_vsi_clear_rings(vsi);
2939
2940	ice_vsi_put_qs(vsi);
2941
2942	/* retain SW VSI data structure since it is needed to unregister and
2943	 * free VSI netdev when PF is not in reset recovery pending state,\
2944	 * for ex: during rmmod.
2945	 */
2946	if (!ice_is_reset_in_progress(pf->state))
2947		ice_vsi_clear(vsi);
2948
2949	return 0;
2950}
2951
2952/**
2953 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
2954 * @vsi: VSI connected with q_vectors
2955 * @coalesce: array of struct with stored coalesce
2956 *
2957 * Returns array size.
2958 */
2959static int
2960ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
2961			     struct ice_coalesce_stored *coalesce)
2962{
2963	int i;
2964
2965	ice_for_each_q_vector(vsi, i) {
2966		struct ice_q_vector *q_vector = vsi->q_vectors[i];
2967
2968		coalesce[i].itr_tx = q_vector->tx.itr_setting;
2969		coalesce[i].itr_rx = q_vector->rx.itr_setting;
2970		coalesce[i].intrl = q_vector->intrl;
2971
2972		if (i < vsi->num_txq)
2973			coalesce[i].tx_valid = true;
2974		if (i < vsi->num_rxq)
2975			coalesce[i].rx_valid = true;
2976	}
2977
2978	return vsi->num_q_vectors;
2979}
2980
2981/**
2982 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
2983 * @vsi: VSI connected with q_vectors
2984 * @coalesce: pointer to array of struct with stored coalesce
2985 * @size: size of coalesce array
2986 *
2987 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
2988 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
2989 * to default value.
2990 */
2991static void
2992ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
2993			     struct ice_coalesce_stored *coalesce, int size)
2994{
2995	struct ice_ring_container *rc;
2996	int i;
2997
2998	if ((size && !coalesce) || !vsi)
2999		return;
3000
3001	/* There are a couple of cases that have to be handled here:
3002	 *   1. The case where the number of queue vectors stays the same, but
3003	 *      the number of Tx or Rx rings changes (the first for loop)
3004	 *   2. The case where the number of queue vectors increased (the
3005	 *      second for loop)
3006	 */
3007	for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
3008		/* There are 2 cases to handle here and they are the same for
3009		 * both Tx and Rx:
3010		 *   if the entry was valid previously (coalesce[i].[tr]x_valid
3011		 *   and the loop variable is less than the number of rings
3012		 *   allocated, then write the previous values
3013		 *
3014		 *   if the entry was not valid previously, but the number of
3015		 *   rings is less than are allocated (this means the number of
3016		 *   rings increased from previously), then write out the
3017		 *   values in the first element
3018		 *
3019		 *   Also, always write the ITR, even if in ITR_IS_DYNAMIC
3020		 *   as there is no harm because the dynamic algorithm
3021		 *   will just overwrite.
3022		 */
3023		if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
3024			rc = &vsi->q_vectors[i]->rx;
3025			rc->itr_setting = coalesce[i].itr_rx;
3026			ice_write_itr(rc, rc->itr_setting);
3027		} else if (i < vsi->alloc_rxq) {
3028			rc = &vsi->q_vectors[i]->rx;
3029			rc->itr_setting = coalesce[0].itr_rx;
3030			ice_write_itr(rc, rc->itr_setting);
3031		}
3032
3033		if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
3034			rc = &vsi->q_vectors[i]->tx;
3035			rc->itr_setting = coalesce[i].itr_tx;
3036			ice_write_itr(rc, rc->itr_setting);
3037		} else if (i < vsi->alloc_txq) {
3038			rc = &vsi->q_vectors[i]->tx;
3039			rc->itr_setting = coalesce[0].itr_tx;
3040			ice_write_itr(rc, rc->itr_setting);
3041		}
3042
3043		vsi->q_vectors[i]->intrl = coalesce[i].intrl;
3044		ice_write_intrl(vsi->q_vectors[i], coalesce[i].intrl);
3045	}
3046
3047	/* the number of queue vectors increased so write whatever is in
3048	 * the first element
3049	 */
3050	for (; i < vsi->num_q_vectors; i++) {
3051		/* transmit */
3052		rc = &vsi->q_vectors[i]->tx;
3053		rc->itr_setting = coalesce[0].itr_tx;
3054		ice_write_itr(rc, rc->itr_setting);
3055
3056		/* receive */
3057		rc = &vsi->q_vectors[i]->rx;
3058		rc->itr_setting = coalesce[0].itr_rx;
3059		ice_write_itr(rc, rc->itr_setting);
3060
3061		vsi->q_vectors[i]->intrl = coalesce[0].intrl;
3062		ice_write_intrl(vsi->q_vectors[i], coalesce[0].intrl);
3063	}
3064}
3065
3066/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3067 * ice_vsi_rebuild - Rebuild VSI after reset
3068 * @vsi: VSI to be rebuild
3069 * @init_vsi: is this an initialization or a reconfigure of the VSI
 
 
 
3070 *
3071 * Returns 0 on success and negative value on failure
3072 */
3073int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3074{
3075	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3076	struct ice_coalesce_stored *coalesce;
3077	int prev_num_q_vectors = 0;
3078	struct ice_vf *vf = NULL;
3079	enum ice_vsi_type vtype;
3080	enum ice_status status;
3081	struct ice_pf *pf;
3082	int ret, i;
3083
3084	if (!vsi)
3085		return -EINVAL;
3086
 
3087	pf = vsi->back;
3088	vtype = vsi->type;
3089	if (vtype == ICE_VSI_VF)
3090		vf = &pf->vf[vsi->vf_id];
3091
3092	coalesce = kcalloc(vsi->num_q_vectors,
3093			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3094	if (!coalesce)
3095		return -ENOMEM;
3096
3097	prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);
 
 
3098
3099	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3100	ret = ice_rm_vsi_rdma_cfg(vsi->port_info, vsi->idx);
3101	if (ret)
3102		dev_err(ice_pf_to_dev(vsi->back), "Failed to remove RDMA scheduler config for VSI %u, err %d\n",
3103			vsi->vsi_num, ret);
3104	ice_vsi_free_q_vectors(vsi);
3105
3106	/* SR-IOV determines needed MSIX resources all at once instead of per
3107	 * VSI since when VFs are spawned we know how many VFs there are and how
3108	 * many interrupts each VF needs. SR-IOV MSIX resources are also
3109	 * cleared in the same manner.
3110	 */
3111	if (vtype != ICE_VSI_VF) {
3112		/* reclaim SW interrupts back to the common pool */
3113		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3114		pf->num_avail_sw_msix += vsi->num_q_vectors;
3115		vsi->base_vector = 0;
3116	}
3117
3118	if (ice_is_xdp_ena_vsi(vsi))
3119		/* return value check can be skipped here, it always returns
3120		 * 0 if reset is in progress
3121		 */
3122		ice_destroy_xdp_rings(vsi);
3123	ice_vsi_put_qs(vsi);
3124	ice_vsi_clear_rings(vsi);
3125	ice_vsi_free_arrays(vsi);
3126	if (vtype == ICE_VSI_VF)
3127		ice_vsi_set_num_qs(vsi, vf->vf_id);
3128	else
3129		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3130
3131	ret = ice_vsi_alloc_arrays(vsi);
3132	if (ret < 0)
3133		goto err_vsi;
3134
3135	ice_vsi_get_qs(vsi);
3136
3137	ice_alloc_fd_res(vsi);
3138	ice_vsi_set_tc_cfg(vsi);
3139
3140	/* Initialize VSI struct elements and create VSI in FW */
3141	ret = ice_vsi_init(vsi, init_vsi);
3142	if (ret < 0)
3143		goto err_vsi;
3144
3145	switch (vtype) {
3146	case ICE_VSI_CTRL:
3147	case ICE_VSI_PF:
3148		ret = ice_vsi_alloc_q_vectors(vsi);
3149		if (ret)
3150			goto err_rings;
3151
3152		ret = ice_vsi_setup_vector_base(vsi);
3153		if (ret)
3154			goto err_vectors;
3155
3156		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3157		if (ret)
3158			goto err_vectors;
3159
3160		ret = ice_vsi_alloc_rings(vsi);
3161		if (ret)
3162			goto err_vectors;
3163
3164		ice_vsi_map_rings_to_vectors(vsi);
3165		if (ice_is_xdp_ena_vsi(vsi)) {
3166			vsi->num_xdp_txq = vsi->alloc_rxq;
3167			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
3168			if (ret)
3169				goto err_vectors;
3170		}
3171		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3172		if (vtype != ICE_VSI_CTRL)
3173			/* Do not exit if configuring RSS had an issue, at
3174			 * least receive traffic on first queue. Hence no
3175			 * need to capture return value
3176			 */
3177			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
3178				ice_vsi_cfg_rss_lut_key(vsi);
3179		break;
3180	case ICE_VSI_VF:
3181		ret = ice_vsi_alloc_q_vectors(vsi);
3182		if (ret)
3183			goto err_rings;
3184
3185		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
3186		if (ret)
3187			goto err_vectors;
3188
3189		ret = ice_vsi_alloc_rings(vsi);
3190		if (ret)
3191			goto err_vectors;
3192
3193		break;
3194	default:
3195		break;
3196	}
3197
3198	/* configure VSI nodes based on number of queues and TC's */
3199	for (i = 0; i < vsi->tc_cfg.numtc; i++) {
3200		max_txqs[i] = vsi->alloc_txq;
3201
3202		if (ice_is_xdp_ena_vsi(vsi))
3203			max_txqs[i] += vsi->num_xdp_txq;
3204	}
3205
3206	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3207				 max_txqs);
3208	if (status) {
3209		dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
3210			vsi->vsi_num, ice_stat_str(status));
3211		if (init_vsi) {
3212			ret = -EIO;
3213			goto err_vectors;
3214		} else {
3215			return ice_schedule_reset(pf, ICE_RESET_PFR);
3216		}
3217	}
3218	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
3219	kfree(coalesce);
3220
3221	return 0;
3222
3223err_vectors:
3224	ice_vsi_free_q_vectors(vsi);
3225err_rings:
3226	if (vsi->netdev) {
3227		vsi->current_netdev_flags = 0;
3228		unregister_netdev(vsi->netdev);
3229		free_netdev(vsi->netdev);
3230		vsi->netdev = NULL;
3231	}
3232err_vsi:
3233	ice_vsi_clear(vsi);
3234	set_bit(ICE_RESET_FAILED, pf->state);
3235	kfree(coalesce);
 
 
 
 
 
3236	return ret;
3237}
3238
3239/**
3240 * ice_is_reset_in_progress - check for a reset in progress
3241 * @state: PF state field
3242 */
3243bool ice_is_reset_in_progress(unsigned long *state)
3244{
3245	return test_bit(ICE_RESET_OICR_RECV, state) ||
3246	       test_bit(ICE_PFR_REQ, state) ||
3247	       test_bit(ICE_CORER_REQ, state) ||
3248	       test_bit(ICE_GLOBR_REQ, state);
3249}
3250
3251/**
3252 * ice_wait_for_reset - Wait for driver to finish reset and rebuild
3253 * @pf: pointer to the PF structure
3254 * @timeout: length of time to wait, in jiffies
3255 *
3256 * Wait (sleep) for a short time until the driver finishes cleaning up from
3257 * a device reset. The caller must be able to sleep. Use this to delay
3258 * operations that could fail while the driver is cleaning up after a device
3259 * reset.
3260 *
3261 * Returns 0 on success, -EBUSY if the reset is not finished within the
3262 * timeout, and -ERESTARTSYS if the thread was interrupted.
3263 */
3264int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
3265{
3266	long ret;
3267
3268	ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
3269					       !ice_is_reset_in_progress(pf->state),
3270					       timeout);
3271	if (ret < 0)
3272		return ret;
3273	else if (!ret)
3274		return -EBUSY;
3275	else
3276		return 0;
3277}
3278
3279#ifdef CONFIG_DCB
3280/**
3281 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
3282 * @vsi: VSI being configured
3283 * @ctx: the context buffer returned from AQ VSI update command
3284 */
3285static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
3286{
3287	vsi->info.mapping_flags = ctx->info.mapping_flags;
3288	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
3289	       sizeof(vsi->info.q_mapping));
3290	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
3291	       sizeof(vsi->info.tc_mapping));
3292}
3293
3294/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3295 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
3296 * @vsi: VSI to be configured
3297 * @ena_tc: TC bitmap
3298 *
3299 * VSI queues expected to be quiesced before calling this function
3300 */
3301int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
3302{
3303	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3304	struct ice_pf *pf = vsi->back;
 
3305	struct ice_vsi_ctx *ctx;
3306	enum ice_status status;
3307	struct device *dev;
3308	int i, ret = 0;
3309	u8 num_tc = 0;
3310
3311	dev = ice_pf_to_dev(pf);
 
 
 
3312
3313	ice_for_each_traffic_class(i) {
3314		/* build bitmap of enabled TCs */
3315		if (ena_tc & BIT(i))
3316			num_tc++;
3317		/* populate max_txqs per TC */
3318		max_txqs[i] = vsi->alloc_txq;
 
 
 
 
 
 
3319	}
3320
 
3321	vsi->tc_cfg.ena_tc = ena_tc;
3322	vsi->tc_cfg.numtc = num_tc;
3323
3324	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3325	if (!ctx)
3326		return -ENOMEM;
3327
3328	ctx->vf_num = 0;
3329	ctx->info = vsi->info;
3330
3331	ice_vsi_setup_q_map(vsi, ctx);
 
 
 
 
 
 
 
 
 
3332
3333	/* must to indicate which section of VSI context are being modified */
3334	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
3335	status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
3336	if (status) {
3337		dev_info(dev, "Failed VSI Update\n");
3338		ret = -EIO;
3339		goto out;
3340	}
3341
3342	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
3343				 max_txqs);
 
 
 
 
3344
3345	if (status) {
3346		dev_err(dev, "VSI %d failed TC config, error %s\n",
3347			vsi->vsi_num, ice_stat_str(status));
3348		ret = -EIO;
3349		goto out;
3350	}
3351	ice_vsi_update_q_map(vsi, ctx);
3352	vsi->info.valid_sections = 0;
3353
3354	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
3355out:
3356	kfree(ctx);
3357	return ret;
3358}
3359#endif /* CONFIG_DCB */
3360
3361/**
3362 * ice_update_ring_stats - Update ring statistics
3363 * @ring: ring to update
3364 * @pkts: number of processed packets
3365 * @bytes: number of processed bytes
3366 *
3367 * This function assumes that caller has acquired a u64_stats_sync lock.
3368 */
3369static void ice_update_ring_stats(struct ice_ring *ring, u64 pkts, u64 bytes)
3370{
3371	ring->stats.bytes += bytes;
3372	ring->stats.pkts += pkts;
3373}
3374
3375/**
3376 * ice_update_tx_ring_stats - Update Tx ring specific counters
3377 * @tx_ring: ring to update
3378 * @pkts: number of processed packets
3379 * @bytes: number of processed bytes
3380 */
3381void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
3382{
3383	u64_stats_update_begin(&tx_ring->syncp);
3384	ice_update_ring_stats(tx_ring, pkts, bytes);
3385	u64_stats_update_end(&tx_ring->syncp);
3386}
3387
3388/**
3389 * ice_update_rx_ring_stats - Update Rx ring specific counters
3390 * @rx_ring: ring to update
3391 * @pkts: number of processed packets
3392 * @bytes: number of processed bytes
3393 */
3394void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
3395{
3396	u64_stats_update_begin(&rx_ring->syncp);
3397	ice_update_ring_stats(rx_ring, pkts, bytes);
3398	u64_stats_update_end(&rx_ring->syncp);
3399}
3400
3401/**
3402 * ice_status_to_errno - convert from enum ice_status to Linux errno
3403 * @err: ice_status value to convert
3404 */
3405int ice_status_to_errno(enum ice_status err)
3406{
3407	switch (err) {
3408	case ICE_SUCCESS:
3409		return 0;
3410	case ICE_ERR_DOES_NOT_EXIST:
3411		return -ENOENT;
3412	case ICE_ERR_OUT_OF_RANGE:
3413	case ICE_ERR_AQ_ERROR:
3414	case ICE_ERR_AQ_TIMEOUT:
3415	case ICE_ERR_AQ_EMPTY:
3416	case ICE_ERR_AQ_FW_CRITICAL:
3417		return -EIO;
3418	case ICE_ERR_PARAM:
3419	case ICE_ERR_INVAL_SIZE:
3420		return -EINVAL;
3421	case ICE_ERR_NO_MEMORY:
3422		return -ENOMEM;
3423	case ICE_ERR_MAX_LIMIT:
3424		return -EAGAIN;
3425	case ICE_ERR_RESET_ONGOING:
3426		return -EBUSY;
3427	case ICE_ERR_AQ_FULL:
3428		return -ENOSPC;
3429	default:
3430		return -EINVAL;
3431	}
3432}
3433
3434/**
3435 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
3436 * @sw: switch to check if its default forwarding VSI is free
3437 *
3438 * Return true if the default forwarding VSI is already being used, else returns
3439 * false signalling that it's available to use.
3440 */
3441bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
3442{
3443	return (sw->dflt_vsi && sw->dflt_vsi_ena);
 
 
 
3444}
3445
3446/**
3447 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
3448 * @sw: switch for the default forwarding VSI to compare against
3449 * @vsi: VSI to compare against default forwarding VSI
3450 *
3451 * If this VSI passed in is the default forwarding VSI then return true, else
3452 * return false
3453 */
3454bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3455{
3456	return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
3457}
3458
3459/**
3460 * ice_set_dflt_vsi - set the default forwarding VSI
3461 * @sw: switch used to assign the default forwarding VSI
3462 * @vsi: VSI getting set as the default forwarding VSI on the switch
3463 *
3464 * If the VSI passed in is already the default VSI and it's enabled just return
3465 * success.
3466 *
3467 * If there is already a default VSI on the switch and it's enabled then return
3468 * -EEXIST since there can only be one default VSI per switch.
3469 *
3470 *  Otherwise try to set the VSI passed in as the switch's default VSI and
3471 *  return the result.
3472 */
3473int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
3474{
3475	enum ice_status status;
3476	struct device *dev;
 
3477
3478	if (!sw || !vsi)
3479		return -EINVAL;
3480
3481	dev = ice_pf_to_dev(vsi->back);
3482
 
 
 
 
 
 
3483	/* the VSI passed in is already the default VSI */
3484	if (ice_is_vsi_dflt_vsi(sw, vsi)) {
3485		dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
3486			vsi->vsi_num);
3487		return 0;
3488	}
3489
3490	/* another VSI is already the default VSI for this switch */
3491	if (ice_is_dflt_vsi_in_use(sw)) {
3492		dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3493			sw->dflt_vsi->vsi_num);
3494		return -EEXIST;
3495	}
3496
3497	status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
3498	if (status) {
3499		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
3500			vsi->vsi_num, ice_stat_str(status));
3501		return -EIO;
3502	}
3503
3504	sw->dflt_vsi = vsi;
3505	sw->dflt_vsi_ena = true;
3506
3507	return 0;
3508}
3509
3510/**
3511 * ice_clear_dflt_vsi - clear the default forwarding VSI
3512 * @sw: switch used to clear the default VSI
3513 *
3514 * If the switch has no default VSI or it's not enabled then return error.
3515 *
3516 * Otherwise try to clear the default VSI and return the result.
3517 */
3518int ice_clear_dflt_vsi(struct ice_sw *sw)
3519{
3520	struct ice_vsi *dflt_vsi;
3521	enum ice_status status;
3522	struct device *dev;
 
3523
3524	if (!sw)
3525		return -EINVAL;
3526
3527	dev = ice_pf_to_dev(sw->pf);
3528
3529	dflt_vsi = sw->dflt_vsi;
3530
3531	/* there is no default VSI configured */
3532	if (!ice_is_dflt_vsi_in_use(sw))
3533		return -ENODEV;
3534
3535	status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
3536				  ICE_FLTR_RX);
3537	if (status) {
3538		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
3539			dflt_vsi->vsi_num, ice_stat_str(status));
3540		return -EIO;
3541	}
3542
3543	sw->dflt_vsi = NULL;
3544	sw->dflt_vsi_ena = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3545
3546	return 0;
3547}
3548
3549/**
3550 * ice_set_link - turn on/off physical link
3551 * @vsi: VSI to modify physical link on
3552 * @ena: turn on/off physical link
3553 */
3554int ice_set_link(struct ice_vsi *vsi, bool ena)
3555{
3556	struct device *dev = ice_pf_to_dev(vsi->back);
3557	struct ice_port_info *pi = vsi->port_info;
3558	struct ice_hw *hw = pi->hw;
3559	enum ice_status status;
3560
3561	if (vsi->type != ICE_VSI_PF)
3562		return -EINVAL;
3563
3564	status = ice_aq_set_link_restart_an(pi, ena, NULL);
3565
3566	/* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
3567	 * this is not a fatal error, so print a warning message and return
3568	 * a success code. Return an error if FW returns an error code other
3569	 * than ICE_AQ_RC_EMODE
3570	 */
3571	if (status == ICE_ERR_AQ_ERROR) {
3572		if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
3573			dev_warn(dev, "can't set link to %s, err %s aq_err %s. not fatal, continuing\n",
3574				 (ena ? "ON" : "OFF"), ice_stat_str(status),
3575				 ice_aq_str(hw->adminq.sq_last_status));
3576	} else if (status) {
3577		dev_err(dev, "can't set link to %s, err %s aq_err %s\n",
3578			(ena ? "ON" : "OFF"), ice_stat_str(status),
3579			ice_aq_str(hw->adminq.sq_last_status));
3580		return -EIO;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3581	}
3582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3583	return 0;
3584}