ice_lib.c 74.3 KB
Newer Older
1 2 3 4
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2018, Intel Corporation. */

#include "ice.h"
A
Anirudh Venkataramanan 已提交
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

11 12
/**
 * ice_vsi_type_str - maps VSI type enum to string equivalents
13
 * @vsi_type: VSI type enum
14
 */
15
const char *ice_vsi_type_str(enum ice_vsi_type vsi_type)
16
{
17
	switch (vsi_type) {
18 19 20 21 22 23 24 25 26 27 28
	case ICE_VSI_PF:
		return "ICE_VSI_PF";
	case ICE_VSI_VF:
		return "ICE_VSI_VF";
	case ICE_VSI_LB:
		return "ICE_VSI_LB";
	default:
		return "unknown";
	}
}

29
/**
30
 * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
31 32
 * @vsi: the VSI being configured
 * @ena: start or stop the Rx rings
33 34 35 36 37
 *
 * First enable/disable all of the Rx rings, flush any remaining writes, and
 * then verify that they have all been enabled/disabled successfully. This will
 * let all of the register writes complete when enabling/disabling the Rx rings
 * before waiting for the change in hardware to complete.
38
 */
39
static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
40
{
K
Karol Kolacinski 已提交
41 42
	int ret = 0;
	u16 i;
43

44 45 46 47 48
	for (i = 0; i < vsi->num_rxq; i++)
		ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);

	ice_flush(&vsi->back->hw);

49
	for (i = 0; i < vsi->num_rxq; i++) {
50
		ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
51
		if (ret)
52 53 54 55 56 57
			break;
	}

	return ret;
}

58 59 60 61 62 63 64
/**
 * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
 * @vsi: VSI pointer
 *
 * On error: returns error code (negative)
 * On success: returns 0
 */
65
static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
66 67
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
68 69 70
	struct device *dev;

	dev = ice_pf_to_dev(pf);
71 72

	/* allocate memory for both Tx and Rx ring pointers */
B
Brett Creeley 已提交
73
	vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
74
				     sizeof(*vsi->tx_rings), GFP_KERNEL);
75
	if (!vsi->tx_rings)
76
		return -ENOMEM;
77

B
Brett Creeley 已提交
78
	vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
79
				     sizeof(*vsi->rx_rings), GFP_KERNEL);
80
	if (!vsi->rx_rings)
81 82
		goto err_rings;

M
Maciej Fijalkowski 已提交
83
	/* XDP will have vsi->alloc_txq Tx queues as well, so double the size */
B
Brett Creeley 已提交
84
	vsi->txq_map = devm_kcalloc(dev, (2 * vsi->alloc_txq),
85 86 87 88 89
				    sizeof(*vsi->txq_map), GFP_KERNEL);

	if (!vsi->txq_map)
		goto err_txq_map;

B
Brett Creeley 已提交
90
	vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
91 92 93 94
				    sizeof(*vsi->rxq_map), GFP_KERNEL);
	if (!vsi->rxq_map)
		goto err_rxq_map;

95 96 97 98
	/* There is no need to allocate q_vectors for a loopback VSI. */
	if (vsi->type == ICE_VSI_LB)
		return 0;

99
	/* allocate memory for q_vector pointers */
B
Brett Creeley 已提交
100
	vsi->q_vectors = devm_kcalloc(dev, vsi->num_q_vectors,
101 102 103
				      sizeof(*vsi->q_vectors), GFP_KERNEL);
	if (!vsi->q_vectors)
		goto err_vectors;
104 105 106 107

	return 0;

err_vectors:
B
Brett Creeley 已提交
108
	devm_kfree(dev, vsi->rxq_map);
109
err_rxq_map:
B
Brett Creeley 已提交
110
	devm_kfree(dev, vsi->txq_map);
111
err_txq_map:
B
Brett Creeley 已提交
112
	devm_kfree(dev, vsi->rx_rings);
113
err_rings:
B
Brett Creeley 已提交
114
	devm_kfree(dev, vsi->tx_rings);
115 116 117 118
	return -ENOMEM;
}

/**
119 120 121 122 123 124 125
 * ice_vsi_set_num_desc - Set number of descriptors for queues on this VSI
 * @vsi: the VSI being configured
 */
static void ice_vsi_set_num_desc(struct ice_vsi *vsi)
{
	switch (vsi->type) {
	case ICE_VSI_PF:
126
	case ICE_VSI_LB:
127 128 129 130
		vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
		vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
		break;
	default:
131
		dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
132 133 134 135 136 137 138
			vsi->type);
		break;
	}
}

/**
 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
139
 * @vsi: the VSI being configured
140
 * @vf_id: ID of the VF being configured
141 142 143
 *
 * Return 0 on success and a negative value on error
 */
144
static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
145 146
{
	struct ice_pf *pf = vsi->back;
147 148 149 150 151
	struct ice_vf *vf = NULL;

	if (vsi->type == ICE_VSI_VF)
		vsi->vf_id = vf_id;

152 153
	switch (vsi->type) {
	case ICE_VSI_PF:
154 155
		vsi->alloc_txq = min_t(int, ice_get_avail_txq_count(pf),
				       num_online_cpus());
156 157 158 159
		if (vsi->req_txq) {
			vsi->alloc_txq = vsi->req_txq;
			vsi->num_txq = vsi->req_txq;
		}
160 161 162 163

		pf->num_lan_tx = vsi->alloc_txq;

		/* only 1 Rx queue unless RSS is enabled */
164
		if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
165
			vsi->alloc_rxq = 1;
166
		} else {
167 168
			vsi->alloc_rxq = min_t(int, ice_get_avail_rxq_count(pf),
					       num_online_cpus());
169 170 171 172 173
			if (vsi->req_rxq) {
				vsi->alloc_rxq = vsi->req_rxq;
				vsi->num_rxq = vsi->req_rxq;
			}
		}
174 175 176

		pf->num_lan_rx = vsi->alloc_rxq;

177
		vsi->num_q_vectors = max_t(int, vsi->alloc_rxq, vsi->alloc_txq);
178
		break;
179
	case ICE_VSI_VF:
180 181 182
		vf = &pf->vf[vsi->vf_id];
		vsi->alloc_txq = vf->num_vf_qs;
		vsi->alloc_rxq = vf->num_vf_qs;
183
		/* pf->num_msix_per_vf includes (VF miscellaneous vector +
184
		 * data queue interrupts). Since vsi->num_q_vectors is number
185 186
		 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
		 * original vector count
187
		 */
188
		vsi->num_q_vectors = pf->num_msix_per_vf - ICE_NONQ_VECS_VF;
189
		break;
190 191 192 193
	case ICE_VSI_LB:
		vsi->alloc_txq = 1;
		vsi->alloc_rxq = 1;
		break;
194
	default:
B
Brett Creeley 已提交
195
		dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
196 197
		break;
	}
198 199

	ice_vsi_set_num_desc(vsi);
200 201 202 203 204 205 206 207 208 209 210
}

/**
 * ice_get_free_slot - get the next non-NULL location index in array
 * @array: array to search
 * @size: size of the array
 * @curr: last known occupied index to be used as a search hint
 *
 * void * is being used to keep the functionality generic. This lets us use this
 * function on any array of pointers.
 */
211
static int ice_get_free_slot(void *array, int size, int curr)
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
{
	int **tmp_array = (int **)array;
	int next;

	if (curr < (size - 1) && !tmp_array[curr + 1]) {
		next = curr + 1;
	} else {
		int i = 0;

		while ((i < size) && (tmp_array[i]))
			i++;
		if (i == size)
			next = ICE_NO_VSI;
		else
			next = i;
	}
	return next;
}

231 232 233 234 235 236 237
/**
 * ice_vsi_delete - delete a VSI from the switch
 * @vsi: pointer to VSI being removed
 */
void ice_vsi_delete(struct ice_vsi *vsi)
{
	struct ice_pf *pf = vsi->back;
238
	struct ice_vsi_ctx *ctxt;
239 240
	enum ice_status status;

241
	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
242 243 244
	if (!ctxt)
		return;

245
	if (vsi->type == ICE_VSI_VF)
246 247
		ctxt->vf_num = vsi->vf_id;
	ctxt->vsi_num = vsi->vsi_num;
248

249
	memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
250

251
	status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
252
	if (status)
253 254
		dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %s\n",
			vsi->vsi_num, ice_stat_str(status));
255

256
	kfree(ctxt);
257 258
}

259
/**
260
 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
261 262
 * @vsi: pointer to VSI being cleared
 */
263
static void ice_vsi_free_arrays(struct ice_vsi *vsi)
264 265
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
266 267 268
	struct device *dev;

	dev = ice_pf_to_dev(pf);
269 270

	/* free the ring and vector containers */
271
	if (vsi->q_vectors) {
B
Brett Creeley 已提交
272
		devm_kfree(dev, vsi->q_vectors);
273 274 275
		vsi->q_vectors = NULL;
	}
	if (vsi->tx_rings) {
B
Brett Creeley 已提交
276
		devm_kfree(dev, vsi->tx_rings);
277 278 279
		vsi->tx_rings = NULL;
	}
	if (vsi->rx_rings) {
B
Brett Creeley 已提交
280
		devm_kfree(dev, vsi->rx_rings);
281 282
		vsi->rx_rings = NULL;
	}
283
	if (vsi->txq_map) {
B
Brett Creeley 已提交
284
		devm_kfree(dev, vsi->txq_map);
285 286 287
		vsi->txq_map = NULL;
	}
	if (vsi->rxq_map) {
B
Brett Creeley 已提交
288
		devm_kfree(dev, vsi->rxq_map);
289 290
		vsi->rxq_map = NULL;
	}
291 292 293 294 295 296 297 298 299 300 301 302 303 304
}

/**
 * ice_vsi_clear - clean up and deallocate the provided VSI
 * @vsi: pointer to VSI being cleared
 *
 * This deallocates the VSI's queue resources, removes it from the PF's
 * VSI array if necessary, and deallocates the VSI
 *
 * Returns 0 on success, negative on failure
 */
int ice_vsi_clear(struct ice_vsi *vsi)
{
	struct ice_pf *pf = NULL;
B
Brett Creeley 已提交
305
	struct device *dev;
306 307 308 309 310 311 312 313

	if (!vsi)
		return 0;

	if (!vsi->back)
		return -EINVAL;

	pf = vsi->back;
B
Brett Creeley 已提交
314
	dev = ice_pf_to_dev(pf);
315 316

	if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
B
Brett Creeley 已提交
317
		dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
318 319 320 321 322 323 324 325 326 327
		return -EINVAL;
	}

	mutex_lock(&pf->sw_mutex);
	/* updates the PF for this cleared VSI */

	pf->vsi[vsi->idx] = NULL;
	if (vsi->idx < pf->next_vsi)
		pf->next_vsi = vsi->idx;

328
	ice_vsi_free_arrays(vsi);
329
	mutex_unlock(&pf->sw_mutex);
B
Brett Creeley 已提交
330
	devm_kfree(dev, vsi);
331 332 333 334

	return 0;
}

335 336 337 338 339
/**
 * ice_msix_clean_rings - MSIX mode Interrupt Handler
 * @irq: interrupt number
 * @data: pointer to a q_vector
 */
340
static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
341 342 343 344 345 346 347 348 349 350 351
{
	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;

	if (!q_vector->tx.ring && !q_vector->rx.ring)
		return IRQ_HANDLED;

	napi_schedule(&q_vector->napi);

	return IRQ_HANDLED;
}

352 353 354
/**
 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
 * @pf: board private structure
355
 * @vsi_type: type of VSI
356
 * @vf_id: ID of the VF being configured
357 358 359
 *
 * returns a pointer to a VSI on success, NULL on failure.
 */
360
static struct ice_vsi *
361
ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type, u16 vf_id)
362
{
B
Brett Creeley 已提交
363
	struct device *dev = ice_pf_to_dev(pf);
364 365 366 367 368 369 370 371 372 373
	struct ice_vsi *vsi = NULL;

	/* Need to protect the allocation of the VSIs at the PF level */
	mutex_lock(&pf->sw_mutex);

	/* If we have already allocated our maximum number of VSIs,
	 * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
	 * is available to be populated
	 */
	if (pf->next_vsi == ICE_NO_VSI) {
B
Brett Creeley 已提交
374
		dev_dbg(dev, "out of VSI slots!\n");
375 376 377
		goto unlock_pf;
	}

B
Brett Creeley 已提交
378
	vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
379 380 381
	if (!vsi)
		goto unlock_pf;

382
	vsi->type = vsi_type;
383 384
	vsi->back = pf;
	set_bit(__ICE_DOWN, vsi->state);
385

386 387
	vsi->idx = pf->next_vsi;

388
	if (vsi_type == ICE_VSI_VF)
389 390 391
		ice_vsi_set_num_qs(vsi, vf_id);
	else
		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
392 393 394

	switch (vsi->type) {
	case ICE_VSI_PF:
395
		if (ice_vsi_alloc_arrays(vsi))
396 397 398 399 400
			goto err_rings;

		/* Setup default MSIX irq handler for VSI */
		vsi->irq_handler = ice_msix_clean_rings;
		break;
401
	case ICE_VSI_VF:
402
		if (ice_vsi_alloc_arrays(vsi))
403 404
			goto err_rings;
		break;
405 406 407 408
	case ICE_VSI_LB:
		if (ice_vsi_alloc_arrays(vsi))
			goto err_rings;
		break;
409
	default:
B
Brett Creeley 已提交
410
		dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
411 412 413 414 415 416 417 418 419 420 421 422
		goto unlock_pf;
	}

	/* fill VSI slot in the PF struct */
	pf->vsi[pf->next_vsi] = vsi;

	/* prepare pf->next_vsi for next use */
	pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
					 pf->next_vsi);
	goto unlock_pf;

err_rings:
B
Brett Creeley 已提交
423
	devm_kfree(dev, vsi);
424 425 426 427 428 429
	vsi = NULL;
unlock_pf:
	mutex_unlock(&pf->sw_mutex);
	return vsi;
}

430 431 432 433 434 435
/**
 * ice_vsi_get_qs - Assign queues from PF to VSI
 * @vsi: the VSI to assign queues to
 *
 * Returns 0 on success and a negative value on error
 */
436
static int ice_vsi_get_qs(struct ice_vsi *vsi)
437
{
438 439 440 441
	struct ice_pf *pf = vsi->back;
	struct ice_qs_cfg tx_qs_cfg = {
		.qs_mutex = &pf->avail_q_mutex,
		.pf_map = pf->avail_txqs,
442
		.pf_map_size = pf->max_pf_txqs,
443 444 445 446
		.q_count = vsi->alloc_txq,
		.scatter_count = ICE_MAX_SCATTER_TXQS,
		.vsi_map = vsi->txq_map,
		.vsi_map_offset = 0,
447
		.mapping_mode = ICE_VSI_MAP_CONTIG
448 449 450 451
	};
	struct ice_qs_cfg rx_qs_cfg = {
		.qs_mutex = &pf->avail_q_mutex,
		.pf_map = pf->avail_rxqs,
452
		.pf_map_size = pf->max_pf_rxqs,
453 454 455 456
		.q_count = vsi->alloc_rxq,
		.scatter_count = ICE_MAX_SCATTER_RXQS,
		.vsi_map = vsi->rxq_map,
		.vsi_map_offset = 0,
457
		.mapping_mode = ICE_VSI_MAP_CONTIG
458
	};
459
	int ret;
460

461
	ret = __ice_vsi_get_qs(&tx_qs_cfg);
462 463 464
	if (ret)
		return ret;
	vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
465

466 467 468 469 470 471
	ret = __ice_vsi_get_qs(&rx_qs_cfg);
	if (ret)
		return ret;
	vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;

	return 0;
472 473
}

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
/**
 * ice_vsi_put_qs - Release queues from VSI to PF
 * @vsi: the VSI that is going to release queues
 */
void ice_vsi_put_qs(struct ice_vsi *vsi)
{
	struct ice_pf *pf = vsi->back;
	int i;

	mutex_lock(&pf->avail_q_mutex);

	for (i = 0; i < vsi->alloc_txq; i++) {
		clear_bit(vsi->txq_map[i], pf->avail_txqs);
		vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
	}

	for (i = 0; i < vsi->alloc_rxq; i++) {
		clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
		vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
	}

	mutex_unlock(&pf->avail_q_mutex);
}

T
Tony Nguyen 已提交
498 499 500 501 502 503 504 505 506 507 508
/**
 * ice_is_safe_mode
 * @pf: pointer to the PF struct
 *
 * returns true if driver is in safe mode, false otherwise
 */
bool ice_is_safe_mode(struct ice_pf *pf)
{
	return !test_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
}

509
/**
T
Tony Nguyen 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
 * ice_vsi_clean_rss_flow_fld - Delete RSS configuration
 * @vsi: the VSI being cleaned up
 *
 * This function deletes RSS input set for all flows that were configured
 * for this VSI
 */
static void ice_vsi_clean_rss_flow_fld(struct ice_vsi *vsi)
{
	struct ice_pf *pf = vsi->back;
	enum ice_status status;

	if (ice_is_safe_mode(pf))
		return;

	status = ice_rem_vsi_rss_cfg(&pf->hw, vsi->idx);
	if (status)
526 527
		dev_dbg(ice_pf_to_dev(pf), "ice_rem_vsi_rss_cfg failed for vsi = %d, error = %s\n",
			vsi->vsi_num, ice_stat_str(status));
T
Tony Nguyen 已提交
528 529 530 531
}

/**
 * ice_rss_clean - Delete RSS related VSI structures and configuration
532 533 534 535
 * @vsi: the VSI being removed
 */
static void ice_rss_clean(struct ice_vsi *vsi)
{
B
Brett Creeley 已提交
536 537
	struct ice_pf *pf = vsi->back;
	struct device *dev;
538

B
Brett Creeley 已提交
539
	dev = ice_pf_to_dev(pf);
540 541

	if (vsi->rss_hkey_user)
B
Brett Creeley 已提交
542
		devm_kfree(dev, vsi->rss_hkey_user);
543
	if (vsi->rss_lut_user)
B
Brett Creeley 已提交
544
		devm_kfree(dev, vsi->rss_lut_user);
T
Tony Nguyen 已提交
545 546 547 548 549

	ice_vsi_clean_rss_flow_fld(vsi);
	/* remove RSS replay list */
	if (!ice_is_safe_mode(pf))
		ice_rem_vsi_rss_list(&pf->hw, vsi->idx);
550 551
}

552 553 554 555
/**
 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
 * @vsi: the VSI being configured
 */
556
static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
557 558 559 560 561 562 563 564 565 566 567 568 569
{
	struct ice_hw_common_caps *cap;
	struct ice_pf *pf = vsi->back;

	if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
		vsi->rss_size = 1;
		return;
	}

	cap = &pf->hw.func_caps.common_cap;
	switch (vsi->type) {
	case ICE_VSI_PF:
		/* PF VSI will inherit RSS instance of PF */
K
Karol Kolacinski 已提交
570 571
		vsi->rss_table_size = (u16)cap->rss_table_size;
		vsi->rss_size = min_t(u16, num_online_cpus(),
572 573 574
				      BIT(cap->rss_table_entry_width));
		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
		break;
575
	case ICE_VSI_VF:
M
Mitch Williams 已提交
576 577
		/* VF VSI will get a small RSS table.
		 * For VSI_LUT, LUT size should be set to 64 bytes.
578 579
		 */
		vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
M
Mitch Williams 已提交
580
		vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
581 582
		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
		break;
583 584
	case ICE_VSI_LB:
		break;
585
	default:
B
Brett Creeley 已提交
586
		dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n",
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
			 vsi->type);
		break;
	}
}

/**
 * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
 * @ctxt: the VSI context being set
 *
 * This initializes a default VSI context for all sections except the Queues.
 */
static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
{
	u32 table = 0;

	memset(&ctxt->info, 0, sizeof(ctxt->info));
	/* VSI's should be allocated from shared pool */
	ctxt->alloc_from_pool = true;
	/* Src pruning enabled by default */
	ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
	/* Traffic from VSI can be sent to LAN */
	ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
	/* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
	 * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
	 * packets untagged/tagged.
	 */
	ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
				  ICE_AQ_VSI_VLAN_MODE_M) >>
				 ICE_AQ_VSI_VLAN_MODE_S);
	/* Have 1:1 UP mapping for both ingress/egress tables */
	table |= ICE_UP_TABLE_TRANSLATE(0, 0);
	table |= ICE_UP_TABLE_TRANSLATE(1, 1);
	table |= ICE_UP_TABLE_TRANSLATE(2, 2);
	table |= ICE_UP_TABLE_TRANSLATE(3, 3);
	table |= ICE_UP_TABLE_TRANSLATE(4, 4);
	table |= ICE_UP_TABLE_TRANSLATE(5, 5);
	table |= ICE_UP_TABLE_TRANSLATE(6, 6);
	table |= ICE_UP_TABLE_TRANSLATE(7, 7);
	ctxt->info.ingress_table = cpu_to_le32(table);
	ctxt->info.egress_table = cpu_to_le32(table);
	/* Have 1:1 UP mapping for outer to inner UP table */
	ctxt->info.outer_up_table = cpu_to_le32(table);
	/* No Outer tag support outer_tag_flags remains to zero */
}

/**
 * ice_vsi_setup_q_map - Setup a VSI queue map
 * @vsi: the VSI being configured
 * @ctxt: VSI context structure
 */
static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
{
639
	u16 offset = 0, qmap = 0, tx_count = 0;
640 641
	u16 qcount_tx = vsi->alloc_txq;
	u16 qcount_rx = vsi->alloc_rxq;
642 643
	u16 tx_numq_tc, rx_numq_tc;
	u16 pow = 0, max_rss = 0;
644
	bool ena_tc0 = false;
645
	u8 netdev_tc = 0;
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
	int i;

	/* at least TC0 should be enabled by default */
	if (vsi->tc_cfg.numtc) {
		if (!(vsi->tc_cfg.ena_tc & BIT(0)))
			ena_tc0 = true;
	} else {
		ena_tc0 = true;
	}

	if (ena_tc0) {
		vsi->tc_cfg.numtc++;
		vsi->tc_cfg.ena_tc |= 1;
	}

661 662 663 664 665 666
	rx_numq_tc = qcount_rx / vsi->tc_cfg.numtc;
	if (!rx_numq_tc)
		rx_numq_tc = 1;
	tx_numq_tc = qcount_tx / vsi->tc_cfg.numtc;
	if (!tx_numq_tc)
		tx_numq_tc = 1;
667 668 669 670 671 672 673 674 675 676 677 678 679

	/* TC mapping is a function of the number of Rx queues assigned to the
	 * VSI for each traffic class and the offset of these queues.
	 * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
	 * queues allocated to TC0. No:of queues is a power-of-2.
	 *
	 * If TC is not enabled, the queue offset is set to 0, and allocate one
	 * queue, this way, traffic for the given TC will be sent to the default
	 * queue.
	 *
	 * Setup number and offset of Rx queues for all TCs for the VSI
	 */

680 681
	qcount_rx = rx_numq_tc;

682 683
	/* qcount will change if RSS is enabled */
	if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
684 685 686 687
		if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF) {
			if (vsi->type == ICE_VSI_PF)
				max_rss = ICE_MAX_LG_RSS_QS;
			else
M
Mitch Williams 已提交
688
				max_rss = ICE_MAX_RSS_QS_PER_VF;
K
Karol Kolacinski 已提交
689
			qcount_rx = min_t(u16, rx_numq_tc, max_rss);
690
			if (!vsi->req_rxq)
K
Karol Kolacinski 已提交
691
				qcount_rx = min_t(u16, qcount_rx,
692
						  vsi->rss_size);
693
		}
694 695 696
	}

	/* find the (rounded up) power-of-2 of qcount */
K
Karol Kolacinski 已提交
697
	pow = (u16)order_base_2(qcount_rx);
698

699
	ice_for_each_traffic_class(i) {
700 701 702
		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
			/* TC is not enabled */
			vsi->tc_cfg.tc_info[i].qoffset = 0;
703 704 705
			vsi->tc_cfg.tc_info[i].qcount_rx = 1;
			vsi->tc_cfg.tc_info[i].qcount_tx = 1;
			vsi->tc_cfg.tc_info[i].netdev_tc = 0;
706 707 708 709 710 711
			ctxt->info.tc_mapping[i] = 0;
			continue;
		}

		/* TC is enabled */
		vsi->tc_cfg.tc_info[i].qoffset = offset;
712 713 714
		vsi->tc_cfg.tc_info[i].qcount_rx = qcount_rx;
		vsi->tc_cfg.tc_info[i].qcount_tx = tx_numq_tc;
		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
715 716 717 718 719

		qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
			ICE_AQ_VSI_TC_Q_OFFSET_M) |
			((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
			 ICE_AQ_VSI_TC_Q_NUM_M);
720 721
		offset += qcount_rx;
		tx_count += tx_numq_tc;
722 723
		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
	}
K
Kiran Patil 已提交
724 725 726 727 728 729 730 731 732 733 734 735

	/* if offset is non-zero, means it is calculated correctly based on
	 * enabled TCs for a given VSI otherwise qcount_rx will always
	 * be correct and non-zero because it is based off - VSI's
	 * allocated Rx queues which is at least 1 (hence qcount_tx will be
	 * at least 1)
	 */
	if (offset)
		vsi->num_rxq = offset;
	else
		vsi->num_rxq = qcount_rx;

736
	vsi->num_txq = tx_count;
737

738
	if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
A
Anirudh Venkataramanan 已提交
739
		dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
740 741 742 743 744 745
		/* since there is a chance that num_rxq could have been changed
		 * in the above for loop, make num_txq equal to num_rxq.
		 */
		vsi->num_txq = vsi->num_rxq;
	}

746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
	/* Rx queue mapping */
	ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
	/* q_mapping buffer holds the info for the first queue allocated for
	 * this VSI in the PF space and also the number of queues associated
	 * with this VSI.
	 */
	ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
	ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
}

/**
 * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
 * @ctxt: the VSI context being set
 * @vsi: the VSI being configured
 */
static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
{
	u8 lut_type, hash_type;
B
Brett Creeley 已提交
764
	struct device *dev;
765 766 767
	struct ice_pf *pf;

	pf = vsi->back;
B
Brett Creeley 已提交
768
	dev = ice_pf_to_dev(pf);
769 770 771 772 773 774 775

	switch (vsi->type) {
	case ICE_VSI_PF:
		/* PF VSI will inherit RSS instance of PF */
		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
		break;
776 777 778 779 780
	case ICE_VSI_VF:
		/* VF VSI will gets a small RSS table which is a VSI LUT type */
		lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_VSI;
		hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
		break;
781
	case ICE_VSI_LB:
B
Brett Creeley 已提交
782
		dev_dbg(dev, "Unsupported VSI type %s\n",
783
			ice_vsi_type_str(vsi->type));
784
		return;
785
	default:
B
Brett Creeley 已提交
786
		dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
787 788 789 790 791 792 793 794 795 796 797 798
		return;
	}

	ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
				ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
				((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
				 ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
}

/**
 * ice_vsi_init - Create and initialize a VSI
 * @vsi: the VSI being configured
799
 * @init_vsi: is this call creating a VSI
800 801 802 803
 *
 * This initializes a VSI context depending on the VSI type to be added and
 * passes it down to the add_vsi aq command to create a new VSI.
 */
804
static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
805 806 807
{
	struct ice_pf *pf = vsi->back;
	struct ice_hw *hw = &pf->hw;
808
	struct ice_vsi_ctx *ctxt;
809
	struct device *dev;
810 811
	int ret = 0;

812
	dev = ice_pf_to_dev(pf);
813
	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
814 815 816
	if (!ctxt)
		return -ENOMEM;

817
	ctxt->info = vsi->info;
818
	switch (vsi->type) {
819
	case ICE_VSI_LB:
820
	case ICE_VSI_PF:
821
		ctxt->flags = ICE_AQ_VSI_TYPE_PF;
822
		break;
823
	case ICE_VSI_VF:
824
		ctxt->flags = ICE_AQ_VSI_TYPE_VF;
825
		/* VF number here is the absolute VF number (0-255) */
826
		ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
827
		break;
828
	default:
829 830
		ret = -ENODEV;
		goto out;
831 832
	}

833
	ice_set_dflt_vsi_ctx(ctxt);
834 835
	/* if the switch is in VEB mode, allow VSI loopback */
	if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
836
		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
837 838

	/* Set LUT type and HASH type if RSS is enabled */
839
	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
840
		ice_set_rss_vsi_ctx(ctxt, vsi);
841 842 843 844 845 846 847
		/* if updating VSI context, make sure to set valid_section:
		 * to indicate which section of VSI context being updated
		 */
		if (!init_vsi)
			ctxt->info.valid_sections |=
				cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
	}
848

849 850
	ctxt->info.sw_id = vsi->port_info->sw_id;
	ice_vsi_setup_q_map(vsi, ctxt);
851 852 853 854 855 856
	if (!init_vsi) /* means VSI being updated */
		/* must to indicate which section of VSI context are
		 * being modified
		 */
		ctxt->info.valid_sections |=
			cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
857

B
Brett Creeley 已提交
858 859 860 861
	/* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
	 * respectively
	 */
	if (vsi->type == ICE_VSI_VF) {
862 863
		ctxt->info.valid_sections |=
			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
B
Brett Creeley 已提交
864 865 866 867 868 869 870 871 872 873 874
		if (pf->vf[vsi->vf_id].spoofchk) {
			ctxt->info.sec_flags |=
				ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
				(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
				 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
		} else {
			ctxt->info.sec_flags &=
				~(ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF |
				  (ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
				   ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S));
		}
875 876
	}

877 878 879 880 881 882 883
	/* Allow control frames out of main VSI */
	if (vsi->type == ICE_VSI_PF) {
		ctxt->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
		ctxt->info.valid_sections |=
			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
	}

884 885 886 887 888 889 890 891 892 893 894 895 896 897
	if (init_vsi) {
		ret = ice_add_vsi(hw, vsi->idx, ctxt, NULL);
		if (ret) {
			dev_err(dev, "Add VSI failed, err %d\n", ret);
			ret = -EIO;
			goto out;
		}
	} else {
		ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
		if (ret) {
			dev_err(dev, "Update VSI failed, err %d\n", ret);
			ret = -EIO;
			goto out;
		}
898 899 900
	}

	/* keep context for update VSI operations */
901
	vsi->info = ctxt->info;
902 903

	/* record VSI number returned */
904
	vsi->vsi_num = ctxt->vsi_num;
905

906 907
out:
	kfree(ctxt);
908 909 910
	return ret;
}

911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
/**
 * ice_free_res - free a block of resources
 * @res: pointer to the resource
 * @index: starting index previously returned by ice_get_res
 * @id: identifier to track owner
 *
 * Returns number of resources freed
 */
int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
{
	int count = 0;
	int i;

	if (!res || index >= res->end)
		return -EINVAL;

	id |= ICE_RES_VALID_BIT;
	for (i = index; i < res->end && res->list[i] == id; i++) {
		res->list[i] = 0;
		count++;
	}

	return count;
}

/**
 * ice_search_res - Search the tracker for a block of resources
 * @res: pointer to the resource
 * @needed: size of the block needed
 * @id: identifier to track owner
 *
 * Returns the base item index of the block, or -ENOMEM for error
 */
static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
{
K
Karol Kolacinski 已提交
946
	u16 start = 0, end = 0;
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013

	if (needed > res->end)
		return -ENOMEM;

	id |= ICE_RES_VALID_BIT;

	do {
		/* skip already allocated entries */
		if (res->list[end++] & ICE_RES_VALID_BIT) {
			start = end;
			if ((start + needed) > res->end)
				break;
		}

		if (end == (start + needed)) {
			int i = start;

			/* there was enough, so assign it to the requestor */
			while (i != end)
				res->list[i++] = id;

			return start;
		}
	} while (end < res->end);

	return -ENOMEM;
}

/**
 * ice_get_free_res_count - Get free count from a resource tracker
 * @res: Resource tracker instance
 */
static u16 ice_get_free_res_count(struct ice_res_tracker *res)
{
	u16 i, count = 0;

	for (i = 0; i < res->end; i++)
		if (!(res->list[i] & ICE_RES_VALID_BIT))
			count++;

	return count;
}

/**
 * ice_get_res - get a block of resources
 * @pf: board private structure
 * @res: pointer to the resource
 * @needed: size of the block needed
 * @id: identifier to track owner
 *
 * Returns the base item index of the block, or negative for error
 */
int
ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
{
	if (!res || !pf)
		return -EINVAL;

	if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
		dev_err(ice_pf_to_dev(pf), "param err: needed=%d, num_entries = %d id=0x%04x\n",
			needed, res->num_entries, id);
		return -EINVAL;
	}

	return ice_search_res(res, needed, id);
}

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
/**
 * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
 * @vsi: ptr to the VSI
 *
 * This should only be called after ice_vsi_alloc() which allocates the
 * corresponding SW VSI structure and initializes num_queue_pairs for the
 * newly allocated VSI.
 *
 * Returns 0 on success or negative on failure
 */
1024
static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1025 1026
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
1027
	struct device *dev;
B
Brett Creeley 已提交
1028
	u16 num_q_vectors;
K
Karol Kolacinski 已提交
1029
	int base;
1030

B
Brett Creeley 已提交
1031
	dev = ice_pf_to_dev(pf);
B
Brett Creeley 已提交
1032 1033 1034 1035 1036
	/* SRIOV doesn't grab irq_tracker entries for each VSI */
	if (vsi->type == ICE_VSI_VF)
		return 0;

	if (vsi->base_vector) {
B
Brett Creeley 已提交
1037
		dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
B
Brett Creeley 已提交
1038
			vsi->vsi_num, vsi->base_vector);
1039 1040 1041
		return -EEXIST;
	}

B
Brett Creeley 已提交
1042 1043
	num_q_vectors = vsi->num_q_vectors;
	/* reserve slots from OS requested IRQs */
K
Karol Kolacinski 已提交
1044 1045 1046
	base = ice_get_res(pf, pf->irq_tracker, num_q_vectors, vsi->idx);

	if (base < 0) {
1047 1048 1049
		dev_err(dev, "%d MSI-X interrupts available. %s %d failed to get %d MSI-X vectors\n",
			ice_get_free_res_count(pf->irq_tracker),
			ice_vsi_type_str(vsi->type), vsi->idx, num_q_vectors);
1050 1051
		return -ENOENT;
	}
K
Karol Kolacinski 已提交
1052
	vsi->base_vector = (u16)base;
B
Brett Creeley 已提交
1053
	pf->num_avail_sw_msix -= num_q_vectors;
1054

1055 1056 1057
	return 0;
}

1058 1059 1060 1061
/**
 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
 * @vsi: the VSI having rings deallocated
 */
1062
static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
{
	int i;

	if (vsi->tx_rings) {
		for (i = 0; i < vsi->alloc_txq; i++) {
			if (vsi->tx_rings[i]) {
				kfree_rcu(vsi->tx_rings[i], rcu);
				vsi->tx_rings[i] = NULL;
			}
		}
	}
	if (vsi->rx_rings) {
		for (i = 0; i < vsi->alloc_rxq; i++) {
			if (vsi->rx_rings[i]) {
				kfree_rcu(vsi->rx_rings[i], rcu);
				vsi->rx_rings[i] = NULL;
			}
		}
	}
}

/**
 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
 * @vsi: VSI which is having rings allocated
 */
1088
static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1089 1090
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
1091
	struct device *dev;
K
Karol Kolacinski 已提交
1092
	u16 i;
1093

B
Brett Creeley 已提交
1094
	dev = ice_pf_to_dev(pf);
1095
	/* Allocate Tx rings */
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	for (i = 0; i < vsi->alloc_txq; i++) {
		struct ice_ring *ring;

		/* allocate with kzalloc(), free with kfree_rcu() */
		ring = kzalloc(sizeof(*ring), GFP_KERNEL);

		if (!ring)
			goto err_out;

		ring->q_index = i;
		ring->reg_idx = vsi->txq_map[i];
		ring->ring_active = false;
		ring->vsi = vsi;
B
Brett Creeley 已提交
1109
		ring->dev = dev;
1110
		ring->count = vsi->num_tx_desc;
1111 1112 1113
		vsi->tx_rings[i] = ring;
	}

1114
	/* Allocate Rx rings */
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
	for (i = 0; i < vsi->alloc_rxq; i++) {
		struct ice_ring *ring;

		/* allocate with kzalloc(), free with kfree_rcu() */
		ring = kzalloc(sizeof(*ring), GFP_KERNEL);
		if (!ring)
			goto err_out;

		ring->q_index = i;
		ring->reg_idx = vsi->rxq_map[i];
		ring->ring_active = false;
		ring->vsi = vsi;
		ring->netdev = vsi->netdev;
B
Brett Creeley 已提交
1128
		ring->dev = dev;
1129
		ring->count = vsi->num_rx_desc;
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
		vsi->rx_rings[i] = ring;
	}

	return 0;

err_out:
	ice_vsi_clear_rings(vsi);
	return -ENOMEM;
}

1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
/**
 * ice_vsi_manage_rss_lut - disable/enable RSS
 * @vsi: the VSI being changed
 * @ena: boolean value indicating if this is an enable or disable request
 *
 * In the event of disable request for RSS, this function will zero out RSS
 * LUT, while in the event of enable request for RSS, it will reconfigure RSS
 * LUT.
 */
int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
{
	int err = 0;
	u8 *lut;

1154
	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
	if (!lut)
		return -ENOMEM;

	if (ena) {
		if (vsi->rss_lut_user)
			memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
		else
			ice_fill_rss_lut(lut, vsi->rss_table_size,
					 vsi->rss_size);
	}

	err = ice_set_rss(vsi, NULL, lut, vsi->rss_table_size);
1167
	kfree(lut);
1168 1169 1170
	return err;
}

1171 1172 1173 1174 1175 1176 1177 1178 1179
/**
 * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
 * @vsi: VSI to be configured
 */
static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
{
	struct ice_aqc_get_set_rss_keys *key;
	struct ice_pf *pf = vsi->back;
	enum ice_status status;
B
Brett Creeley 已提交
1180
	struct device *dev;
1181 1182 1183
	int err = 0;
	u8 *lut;

B
Brett Creeley 已提交
1184
	dev = ice_pf_to_dev(pf);
K
Karol Kolacinski 已提交
1185
	vsi->rss_size = min_t(u16, vsi->rss_size, vsi->num_rxq);
1186

1187
	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1188 1189 1190 1191 1192 1193 1194 1195
	if (!lut)
		return -ENOMEM;

	if (vsi->rss_lut_user)
		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
	else
		ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);

1196 1197
	status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
				    vsi->rss_table_size);
1198 1199

	if (status) {
1200 1201
		dev_err(dev, "set_rss_lut failed, error %s\n",
			ice_stat_str(status));
1202 1203 1204 1205
		err = -EIO;
		goto ice_vsi_cfg_rss_exit;
	}

1206
	key = kzalloc(sizeof(*key), GFP_KERNEL);
1207 1208 1209 1210 1211 1212
	if (!key) {
		err = -ENOMEM;
		goto ice_vsi_cfg_rss_exit;
	}

	if (vsi->rss_hkey_user)
1213 1214 1215
		memcpy(key,
		       (struct ice_aqc_get_set_rss_keys *)vsi->rss_hkey_user,
		       ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1216
	else
1217 1218
		netdev_rss_key_fill((void *)key,
				    ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1219

1220
	status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
1221 1222

	if (status) {
1223 1224
		dev_err(dev, "set_rss_key failed, error %s\n",
			ice_stat_str(status));
1225 1226 1227
		err = -EIO;
	}

1228
	kfree(key);
1229
ice_vsi_cfg_rss_exit:
1230
	kfree(lut);
1231 1232 1233
	return err;
}

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
/**
 * ice_vsi_set_vf_rss_flow_fld - Sets VF VSI RSS input set for different flows
 * @vsi: VSI to be configured
 *
 * This function will only be called during the VF VSI setup. Upon successful
 * completion of package download, this function will configure default RSS
 * input sets for VF VSI.
 */
static void ice_vsi_set_vf_rss_flow_fld(struct ice_vsi *vsi)
{
	struct ice_pf *pf = vsi->back;
	enum ice_status status;
	struct device *dev;

	dev = ice_pf_to_dev(pf);
	if (ice_is_safe_mode(pf)) {
		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
			vsi->vsi_num);
		return;
	}

	status = ice_add_avf_rss_cfg(&pf->hw, vsi->idx, ICE_DEFAULT_RSS_HENA);
	if (status)
1257 1258
		dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %s\n",
			vsi->vsi_num, ice_stat_str(status));
1259 1260
}

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
/**
 * ice_vsi_set_rss_flow_fld - Sets RSS input set for different flows
 * @vsi: VSI to be configured
 *
 * This function will only be called after successful download package call
 * during initialization of PF. Since the downloaded package will erase the
 * RSS section, this function will configure RSS input sets for different
 * flow types. The last profile added has the highest priority, therefore 2
 * tuple profiles (i.e. IPv4 src/dst) are added before 4 tuple profiles
 * (i.e. IPv4 src/dst TCP src/dst port).
 */
static void ice_vsi_set_rss_flow_fld(struct ice_vsi *vsi)
{
	u16 vsi_handle = vsi->idx, vsi_num = vsi->vsi_num;
	struct ice_pf *pf = vsi->back;
	struct ice_hw *hw = &pf->hw;
	enum ice_status status;
	struct device *dev;

	dev = ice_pf_to_dev(pf);
	if (ice_is_safe_mode(pf)) {
		dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n",
			vsi_num);
		return;
	}
	/* configure RSS for IPv4 with input set IP src/dst */
	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
				 ICE_FLOW_SEG_HDR_IPV4);
	if (status)
1290 1291
		dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1292 1293 1294 1295 1296

	/* configure RSS for IPv6 with input set IPv6 src/dst */
	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
				 ICE_FLOW_SEG_HDR_IPV6);
	if (status)
1297 1298
		dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1299 1300 1301 1302 1303

	/* configure RSS for tcp4 with input set IP src/dst, TCP src/dst */
	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV4,
				 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4);
	if (status)
1304 1305
		dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1306 1307 1308 1309 1310

	/* configure RSS for udp4 with input set IP src/dst, UDP src/dst */
	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV4,
				 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4);
	if (status)
1311 1312
		dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1313 1314 1315 1316 1317

	/* configure RSS for sctp4 with input set IP src/dst */
	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV4,
				 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4);
	if (status)
1318 1319
		dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1320 1321 1322 1323 1324

	/* configure RSS for tcp6 with input set IPv6 src/dst, TCP src/dst */
	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_TCP_IPV6,
				 ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6);
	if (status)
1325 1326
		dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1327 1328 1329 1330 1331

	/* configure RSS for udp6 with input set IPv6 src/dst, UDP src/dst */
	status = ice_add_rss_cfg(hw, vsi_handle, ICE_HASH_UDP_IPV6,
				 ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6);
	if (status)
1332 1333
		dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1334 1335 1336 1337 1338

	/* configure RSS for sctp6 with input set IPv6 src/dst */
	status = ice_add_rss_cfg(hw, vsi_handle, ICE_FLOW_HASH_IPV6,
				 ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6);
	if (status)
1339 1340
		dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1341 1342
}

1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
/**
 * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
 * @vsi: the VSI to be updated
 */
void ice_update_eth_stats(struct ice_vsi *vsi)
{
	struct ice_eth_stats *prev_es, *cur_es;
	struct ice_hw *hw = &vsi->back->hw;
	u16 vsi_num = vsi->vsi_num;    /* HW absolute index of a VSI */

	prev_es = &vsi->eth_stats_prev;
	cur_es = &vsi->eth_stats;

1356 1357
	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1358

1359 1360
	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1361

1362 1363
	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1364

1365 1366
	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1367 1368 1369 1370

	ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->rx_discards, &cur_es->rx_discards);

1371 1372
	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1373

1374 1375
	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1376

1377 1378
	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1379

1380 1381
	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391

	ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->tx_errors, &cur_es->tx_errors);

	vsi->stat_offsets_loaded = true;
}

/**
 * ice_vsi_add_vlan - Add VSI membership for given VLAN
 * @vsi: the VSI being configured
1392
 * @vid: VLAN ID to be added
1393
 * @action: filter action to be performed on match
1394
 */
1395 1396
int
ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1397 1398
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
1399
	struct device *dev;
1400 1401
	int err = 0;

B
Brett Creeley 已提交
1402
	dev = ice_pf_to_dev(pf);
1403

1404
	if (!ice_fltr_add_vlan(vsi, vid, action)) {
1405 1406
		vsi->num_vlan++;
	} else {
1407
		err = -ENODEV;
B
Brett Creeley 已提交
1408 1409
		dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
			vsi->vsi_num);
1410 1411 1412 1413 1414 1415 1416 1417
	}

	return err;
}

/**
 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
 * @vsi: the VSI being configured
1418
 * @vid: VLAN ID to be removed
1419 1420 1421 1422 1423 1424
 *
 * Returns 0 on success and negative on failure
 */
int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
{
	struct ice_pf *pf = vsi->back;
1425
	enum ice_status status;
B
Brett Creeley 已提交
1426
	struct device *dev;
1427
	int err = 0;
1428

B
Brett Creeley 已提交
1429
	dev = ice_pf_to_dev(pf);
1430

1431
	status = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1432 1433 1434
	if (!status) {
		vsi->num_vlan--;
	} else if (status == ICE_ERR_DOES_NOT_EXIST) {
1435 1436
		dev_dbg(dev, "Failed to remove VLAN %d on VSI %i, it does not exist, status: %s\n",
			vid, vsi->vsi_num, ice_stat_str(status));
1437
	} else {
1438 1439
		dev_err(dev, "Error removing VLAN %d on vsi %i error: %s\n",
			vid, vsi->vsi_num, ice_stat_str(status));
1440
		err = -EIO;
1441 1442
	}

1443
	return err;
1444 1445
}

M
Maciej Fijalkowski 已提交
1446 1447 1448 1449 1450 1451
/**
 * ice_vsi_cfg_frame_size - setup max frame size and Rx buffer length
 * @vsi: VSI
 */
void ice_vsi_cfg_frame_size(struct ice_vsi *vsi)
{
M
Maciej Fijalkowski 已提交
1452 1453 1454 1455
	if (!vsi->netdev || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) {
		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
		vsi->rx_buf_len = ICE_RXBUF_2048;
#if (PAGE_SIZE < 8192)
1456 1457
	} else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
M
Maciej Fijalkowski 已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
		vsi->max_frame = ICE_RXBUF_1536 - NET_IP_ALIGN;
		vsi->rx_buf_len = ICE_RXBUF_1536 - NET_IP_ALIGN;
#endif
	} else {
		vsi->max_frame = ICE_AQ_SET_MAC_FRAME_SIZE_MAX;
#if (PAGE_SIZE < 8192)
		vsi->rx_buf_len = ICE_RXBUF_3072;
#else
		vsi->rx_buf_len = ICE_RXBUF_2048;
#endif
	}
M
Maciej Fijalkowski 已提交
1469 1470
}

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
/**
 * ice_vsi_cfg_rxqs - Configure the VSI for Rx
 * @vsi: the VSI being configured
 *
 * Return 0 on success and a negative value on error
 * Configure the Rx VSI for operation.
 */
int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
{
	u16 i;

1482 1483 1484
	if (vsi->type == ICE_VSI_VF)
		goto setup_rings;

M
Maciej Fijalkowski 已提交
1485
	ice_vsi_cfg_frame_size(vsi);
1486
setup_rings:
1487
	/* set up individual rings */
1488 1489
	for (i = 0; i < vsi->num_rxq; i++) {
		int err;
1490

1491 1492
		err = ice_setup_rx_ctx(vsi->rx_rings[i]);
		if (err) {
1493
			dev_err(ice_pf_to_dev(vsi->back), "ice_setup_rx_ctx failed for RxQ %d, err %d\n",
1494 1495 1496
				i, err);
			return err;
		}
1497
	}
1498 1499

	return 0;
1500 1501 1502 1503 1504
}

/**
 * ice_vsi_cfg_txqs - Configure the VSI for Tx
 * @vsi: the VSI being configured
1505
 * @rings: Tx ring array to be configured
1506 1507 1508 1509
 *
 * Return 0 on success and a negative value on error
 * Configure the Tx VSI for operation.
 */
1510
static int
1511
ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_ring **rings)
1512 1513
{
	struct ice_aqc_add_tx_qgrp *qg_buf;
1514
	u16 q_idx = 0;
1515
	int err = 0;
1516

1517
	qg_buf = kzalloc(sizeof(*qg_buf), GFP_KERNEL);
1518 1519 1520 1521 1522
	if (!qg_buf)
		return -ENOMEM;

	qg_buf->num_txqs = 1;

1523 1524 1525 1526
	for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
		err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
		if (err)
			goto err_cfg_txqs;
1527
	}
1528

1529
err_cfg_txqs:
1530
	kfree(qg_buf);
1531 1532 1533
	return err;
}

1534 1535 1536 1537 1538 1539 1540 1541 1542
/**
 * ice_vsi_cfg_lan_txqs - Configure the VSI for Tx
 * @vsi: the VSI being configured
 *
 * Return 0 on success and a negative value on error
 * Configure the Tx VSI for operation.
 */
int ice_vsi_cfg_lan_txqs(struct ice_vsi *vsi)
{
1543
	return ice_vsi_cfg_txqs(vsi, vsi->tx_rings);
1544 1545
}

M
Maciej Fijalkowski 已提交
1546 1547 1548 1549 1550 1551 1552 1553 1554
/**
 * ice_vsi_cfg_xdp_txqs - Configure Tx queues dedicated for XDP in given VSI
 * @vsi: the VSI being configured
 *
 * Return 0 on success and a negative value on error
 * Configure the Tx queues dedicated for XDP in given VSI for operation.
 */
int ice_vsi_cfg_xdp_txqs(struct ice_vsi *vsi)
{
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
	int ret;
	int i;

	ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings);
	if (ret)
		return ret;

	for (i = 0; i < vsi->num_xdp_txq; i++)
		vsi->xdp_rings[i]->xsk_umem = ice_xsk_umem(vsi->xdp_rings[i]);

	return ret;
M
Maciej Fijalkowski 已提交
1566 1567
}

1568 1569 1570 1571 1572 1573 1574 1575
/**
 * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
 * @intrl: interrupt rate limit in usecs
 * @gran: interrupt rate limit granularity in usecs
 *
 * This function converts a decimal interrupt rate limit in usecs to the format
 * expected by firmware.
 */
1576
u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1577 1578 1579 1580 1581 1582 1583 1584
{
	u32 val = intrl / gran;

	if (val)
		return val | GLINT_RATE_INTRL_ENA_M;
	return 0;
}

1585 1586 1587
/**
 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
 * @vsi: the VSI being configured
1588 1589 1590
 *
 * This configures MSIX mode interrupts for the PF VSI, and should not be used
 * for the VF VSI.
1591 1592 1593 1594 1595
 */
void ice_vsi_cfg_msix(struct ice_vsi *vsi)
{
	struct ice_pf *pf = vsi->back;
	struct ice_hw *hw = &pf->hw;
K
Karol Kolacinski 已提交
1596
	u16 txq = 0, rxq = 0;
1597
	int i, q;
1598

1599
	for (i = 0; i < vsi->num_q_vectors; i++) {
1600
		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1601
		u16 reg_idx = q_vector->reg_idx;
1602

1603
		ice_cfg_itr(hw, q_vector);
1604

1605
		wr32(hw, GLINT_RATE(reg_idx),
1606
		     ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619

		/* Both Transmit Queue Interrupt Cause Control register
		 * and Receive Queue Interrupt Cause control register
		 * expects MSIX_INDX field to be the vector index
		 * within the function space and not the absolute
		 * vector index across PF or across device.
		 * For SR-IOV VF VSIs queue vector index always starts
		 * with 1 since first vector index(0) is used for OICR
		 * in VF space. Since VMDq and other PF VSIs are within
		 * the PF function space, use the vector index that is
		 * tracked for this PF.
		 */
		for (q = 0; q < q_vector->num_ring_tx; q++) {
1620 1621
			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
					      q_vector->tx.itr_idx);
1622 1623 1624 1625
			txq++;
		}

		for (q = 0; q < q_vector->num_ring_rx; q++) {
1626 1627
			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
					      q_vector->rx.itr_idx);
1628 1629 1630 1631 1632
			rxq++;
		}
	}
}

1633 1634 1635 1636 1637 1638 1639
/**
 * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
 * @vsi: the VSI being changed
 */
int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
{
	struct ice_hw *hw = &vsi->back->hw;
1640
	struct ice_vsi_ctx *ctxt;
1641
	enum ice_status status;
1642 1643
	int ret = 0;

1644
	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1645 1646
	if (!ctxt)
		return -ENOMEM;
1647 1648 1649 1650 1651

	/* Here we are configuring the VSI to let the driver add VLAN tags by
	 * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
	 * insertion happens in the Tx hot path, in ice_tx_map.
	 */
1652
	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
1653

1654 1655 1656 1657
	/* Preserve existing VLAN strip setting */
	ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
				  ICE_AQ_VSI_VLAN_EMOD_M);

1658
	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1659

1660
	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1661
	if (status) {
1662 1663 1664
		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %s aq_err %s\n",
			ice_stat_str(status),
			ice_aq_str(hw->adminq.sq_last_status));
1665 1666
		ret = -EIO;
		goto out;
1667 1668
	}

1669 1670
	vsi->info.vlan_flags = ctxt->info.vlan_flags;
out:
1671
	kfree(ctxt);
1672
	return ret;
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
}

/**
 * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
 * @vsi: the VSI being changed
 * @ena: boolean value indicating if this is a enable or disable request
 */
int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
{
	struct ice_hw *hw = &vsi->back->hw;
1683
	struct ice_vsi_ctx *ctxt;
1684
	enum ice_status status;
1685 1686
	int ret = 0;

1687
	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1688 1689
	if (!ctxt)
		return -ENOMEM;
1690 1691 1692 1693 1694

	/* Here we are configuring what the VSI should do with the VLAN tag in
	 * the Rx packet. We can either leave the tag in the packet or put it in
	 * the Rx descriptor.
	 */
1695
	if (ena)
1696
		/* Strip VLAN tag from Rx packet and put it in the desc */
1697 1698
		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
	else
1699
		/* Disable stripping. Leave tag in packet */
1700
		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
1701 1702

	/* Allow all packets untagged/tagged */
1703
	ctxt->info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
1704

1705
	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
1706

1707
	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
1708
	if (status) {
1709 1710 1711
		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %s aq_err %s\n",
			ena, ice_stat_str(status),
			ice_aq_str(hw->adminq.sq_last_status));
1712 1713
		ret = -EIO;
		goto out;
1714 1715
	}

1716 1717
	vsi->info.vlan_flags = ctxt->info.vlan_flags;
out:
1718
	kfree(ctxt);
1719
	return ret;
1720
}
1721 1722

/**
1723 1724
 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
 * @vsi: the VSI whose rings are to be enabled
1725 1726 1727
 *
 * Returns 0 on success and a negative value on error
 */
1728
int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
1729
{
1730
	return ice_vsi_ctrl_all_rx_rings(vsi, true);
1731 1732 1733
}

/**
1734 1735
 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
 * @vsi: the VSI whose rings are to be disabled
1736 1737 1738
 *
 * Returns 0 on success and a negative value on error
 */
1739
int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
1740
{
1741
	return ice_vsi_ctrl_all_rx_rings(vsi, false);
1742 1743
}

1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
/**
 * ice_vsi_stop_tx_rings - Disable Tx rings
 * @vsi: the VSI being configured
 * @rst_src: reset source
 * @rel_vmvf_num: Relative ID of VF/VM
 * @rings: Tx ring array to be stopped
 */
static int
ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
		      u16 rel_vmvf_num, struct ice_ring **rings)
{
1755
	u16 q_idx;
1756 1757 1758

	if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
		return -EINVAL;
1759

1760 1761 1762
	for (q_idx = 0; q_idx < vsi->num_txq; q_idx++) {
		struct ice_txq_meta txq_meta = { };
		int status;
1763

1764 1765
		if (!rings || !rings[q_idx])
			return -EINVAL;
1766

1767 1768 1769
		ice_fill_txq_meta(vsi, rings[q_idx], &txq_meta);
		status = ice_vsi_stop_tx_ring(vsi, rst_src, rel_vmvf_num,
					      rings[q_idx], &txq_meta);
1770

1771 1772
		if (status)
			return status;
1773 1774
	}

1775
	return 0;
1776
}
1777

1778 1779 1780 1781
/**
 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
 * @vsi: the VSI being configured
 * @rst_src: reset source
1782
 * @rel_vmvf_num: Relative ID of VF/VM
1783
 */
1784 1785 1786
int
ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
			  u16 rel_vmvf_num)
1787
{
1788
	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings);
1789 1790
}

M
Maciej Fijalkowski 已提交
1791 1792 1793 1794 1795 1796 1797 1798 1799
/**
 * ice_vsi_stop_xdp_tx_rings - Disable XDP Tx rings
 * @vsi: the VSI being configured
 */
int ice_vsi_stop_xdp_tx_rings(struct ice_vsi *vsi)
{
	return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings);
}

1800 1801 1802 1803
/**
 * ice_vsi_is_vlan_pruning_ena - check if VLAN pruning is enabled or not
 * @vsi: VSI to check whether or not VLAN pruning is enabled.
 *
1804
 * returns true if Rx VLAN pruning is enabled and false otherwise.
1805 1806 1807 1808 1809 1810
 */
bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
{
	if (!vsi)
		return false;

1811
	return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
1812 1813
}

1814 1815 1816 1817
/**
 * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
 * @vsi: VSI to enable or disable VLAN pruning on
 * @ena: set to true to enable VLAN pruning and false to disable it
1818
 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
1819 1820 1821
 *
 * returns 0 if VSI is updated, negative otherwise
 */
1822
int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
1823 1824
{
	struct ice_vsi_ctx *ctxt;
1825
	struct ice_pf *pf;
1826 1827 1828 1829 1830
	int status;

	if (!vsi)
		return -EINVAL;

1831
	pf = vsi->back;
1832
	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1833 1834 1835 1836 1837
	if (!ctxt)
		return -ENOMEM;

	ctxt->info = vsi->info;

B
Brett Creeley 已提交
1838
	if (ena)
1839
		ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
B
Brett Creeley 已提交
1840
	else
1841 1842
		ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;

1843 1844
	if (!vlan_promisc)
		ctxt->info.valid_sections =
B
Brett Creeley 已提交
1845
			cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
1846

1847
	status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
1848
	if (status) {
1849 1850 1851 1852
		netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %s, aq_err = %s\n",
			   ena ? "En" : "Dis", vsi->idx, vsi->vsi_num,
			   ice_stat_str(status),
			   ice_aq_str(pf->hw.adminq.sq_last_status));
1853 1854 1855 1856 1857
		goto err_out;
	}

	vsi->info.sw_flags2 = ctxt->info.sw_flags2;

1858
	kfree(ctxt);
1859 1860 1861
	return 0;

err_out:
1862
	kfree(ctxt);
1863 1864 1865
	return -EIO;
}

1866 1867 1868 1869 1870 1871 1872 1873
static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
{
	struct ice_dcbx_cfg *cfg = &vsi->port_info->local_dcbx_cfg;

	vsi->tc_cfg.ena_tc = ice_dcb_get_ena_tc(cfg);
	vsi->tc_cfg.numtc = ice_dcb_get_num_tc(cfg);
}

1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
/**
 * ice_vsi_set_q_vectors_reg_idx - set the HW register index for all q_vectors
 * @vsi: VSI to set the q_vectors register index on
 */
static int
ice_vsi_set_q_vectors_reg_idx(struct ice_vsi *vsi)
{
	u16 i;

	if (!vsi || !vsi->q_vectors)
		return -EINVAL;

	ice_for_each_q_vector(vsi, i) {
		struct ice_q_vector *q_vector = vsi->q_vectors[i];

		if (!q_vector) {
1890
			dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
1891 1892 1893 1894
				i, vsi->vsi_num);
			goto clear_reg_idx;
		}

B
Brett Creeley 已提交
1895 1896 1897 1898 1899 1900 1901 1902
		if (vsi->type == ICE_VSI_VF) {
			struct ice_vf *vf = &vsi->back->vf[vsi->vf_id];

			q_vector->reg_idx = ice_calc_vf_reg_idx(vf, q_vector);
		} else {
			q_vector->reg_idx =
				q_vector->v_idx + vsi->base_vector;
		}
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
	}

	return 0;

clear_reg_idx:
	ice_for_each_q_vector(vsi, i) {
		struct ice_q_vector *q_vector = vsi->q_vectors[i];

		if (q_vector)
			q_vector->reg_idx = 0;
	}

	return -EINVAL;
}

1918 1919 1920 1921 1922 1923 1924 1925
/**
 * ice_cfg_sw_lldp - Config switch rules for LLDP packet handling
 * @vsi: the VSI being configured
 * @tx: bool to determine Tx or Rx rule
 * @create: bool to determine create or remove Rule
 */
void ice_cfg_sw_lldp(struct ice_vsi *vsi, bool tx, bool create)
{
1926 1927
	enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
				    enum ice_sw_fwd_act_type act);
1928 1929
	struct ice_pf *pf = vsi->back;
	enum ice_status status;
B
Brett Creeley 已提交
1930
	struct device *dev;
1931

B
Brett Creeley 已提交
1932
	dev = ice_pf_to_dev(pf);
1933
	eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
1934

1935 1936 1937
	if (tx)
		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
				  ICE_DROP_PACKET);
1938
	else
1939
		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX, ICE_FWD_TO_VSI);
1940 1941

	if (status)
1942
		dev_err(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
1943
			create ? "adding" : "removing", tx ? "TX" : "RX",
1944
			vsi->vsi_num, ice_stat_str(status));
1945 1946
}

1947 1948 1949 1950
/**
 * ice_vsi_setup - Set up a VSI by a given type
 * @pf: board private structure
 * @pi: pointer to the port_info instance
1951
 * @vsi_type: VSI type
1952
 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
 *         used only for ICE_VSI_VF VSI type. For other VSI types, should
 *         fill-in ICE_INVAL_VFID as input.
 *
 * This allocates the sw VSI structure and its queue resources.
 *
 * Returns pointer to the successfully allocated and configured VSI sw struct on
 * success, NULL on failure.
 */
struct ice_vsi *
ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
1963
	      enum ice_vsi_type vsi_type, u16 vf_id)
1964 1965
{
	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
B
Brett Creeley 已提交
1966
	struct device *dev = ice_pf_to_dev(pf);
1967
	enum ice_status status;
1968 1969 1970
	struct ice_vsi *vsi;
	int ret, i;

1971 1972
	if (vsi_type == ICE_VSI_VF)
		vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
1973
	else
1974
		vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
1975

1976 1977 1978 1979 1980 1981 1982
	if (!vsi) {
		dev_err(dev, "could not allocate VSI\n");
		return NULL;
	}

	vsi->port_info = pi;
	vsi->vsw = pf->first_sw;
1983 1984 1985
	if (vsi->type == ICE_VSI_PF)
		vsi->ethtype = ETH_P_PAUSE;

1986 1987
	if (vsi->type == ICE_VSI_VF)
		vsi->vf_id = vf_id;
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997

	if (ice_vsi_get_qs(vsi)) {
		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
			vsi->idx);
		goto unroll_get_qs;
	}

	/* set RSS capabilities */
	ice_vsi_set_rss_params(vsi);

1998
	/* set TC configuration */
1999 2000
	ice_vsi_set_tc_cfg(vsi);

2001
	/* create the VSI */
2002
	ret = ice_vsi_init(vsi, true);
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
	if (ret)
		goto unroll_get_qs;

	switch (vsi->type) {
	case ICE_VSI_PF:
		ret = ice_vsi_alloc_q_vectors(vsi);
		if (ret)
			goto unroll_vsi_init;

		ret = ice_vsi_setup_vector_base(vsi);
		if (ret)
			goto unroll_alloc_q_vector;

2016 2017 2018 2019
		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
		if (ret)
			goto unroll_vector_base;

2020 2021 2022 2023
		ret = ice_vsi_alloc_rings(vsi);
		if (ret)
			goto unroll_vector_base;

2024 2025 2026 2027 2028 2029 2030
		/* Always add VLAN ID 0 switch rule by default. This is needed
		 * in order to allow all untagged and 0 tagged priority traffic
		 * if Rx VLAN pruning is enabled. Also there are cases where we
		 * don't get the call to add VLAN 0 via ice_vlan_rx_add_vid()
		 * so this handles those cases (i.e. adding the PF to a bridge
		 * without the 8021q module loaded).
		 */
2031
		ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2032 2033 2034
		if (ret)
			goto unroll_clear_rings;

2035 2036 2037 2038 2039 2040
		ice_vsi_map_rings_to_vectors(vsi);

		/* Do not exit if configuring RSS had an issue, at least
		 * receive traffic on first queue. Hence no need to capture
		 * return value
		 */
2041
		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2042
			ice_vsi_cfg_rss_lut_key(vsi);
2043 2044
			ice_vsi_set_rss_flow_fld(vsi);
		}
2045
		break;
2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
	case ICE_VSI_VF:
		/* VF driver will take care of creating netdev for this type and
		 * map queues to vectors through Virtchnl, PF driver only
		 * creates a VSI and corresponding structures for bookkeeping
		 * purpose
		 */
		ret = ice_vsi_alloc_q_vectors(vsi);
		if (ret)
			goto unroll_vsi_init;

		ret = ice_vsi_alloc_rings(vsi);
		if (ret)
			goto unroll_alloc_q_vector;

2060 2061 2062 2063
		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
		if (ret)
			goto unroll_vector_base;

2064 2065 2066 2067
		/* Do not exit if configuring RSS had an issue, at least
		 * receive traffic on first queue. Hence no need to capture
		 * return value
		 */
2068
		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2069
			ice_vsi_cfg_rss_lut_key(vsi);
2070 2071
			ice_vsi_set_vf_rss_flow_fld(vsi);
		}
2072
		break;
2073 2074 2075 2076 2077
	case ICE_VSI_LB:
		ret = ice_vsi_alloc_rings(vsi);
		if (ret)
			goto unroll_vsi_init;
		break;
2078
	default:
2079
		/* clean up the resources and exit */
2080 2081 2082 2083 2084
		goto unroll_vsi_init;
	}

	/* configure VSI nodes based on number of queues and TC's */
	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2085
		max_txqs[i] = vsi->alloc_txq;
2086

2087 2088 2089
	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
				 max_txqs);
	if (status) {
2090 2091
		dev_err(dev, "VSI %d failed lan queue config, error %s\n",
			vsi->vsi_num, ice_stat_str(status));
2092 2093 2094
		goto unroll_vector_base;
	}

2095 2096 2097 2098 2099
	/* Add switch rule to drop all Tx Flow Control Frames, of look up
	 * type ETHERTYPE from VSIs, and restrict malicious VF from sending
	 * out PAUSE or PFC frames. If enabled, FW can still send FC frames.
	 * The rule is added once for PF VSI in order to create appropriate
	 * recipe, since VSI/VSI list is ignored with drop action...
2100 2101 2102
	 * Also add rules to handle LLDP Tx packets.  Tx LLDP packets need to
	 * be dropped so that VFs cannot send LLDP packets to reconfig DCB
	 * settings in the HW.
2103
	 */
2104
	if (!ice_is_safe_mode(pf))
T
Tony Nguyen 已提交
2105
		if (vsi->type == ICE_VSI_PF) {
2106 2107
			ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
					 ICE_DROP_PACKET);
T
Tony Nguyen 已提交
2108 2109
			ice_cfg_sw_lldp(vsi, true, true);
		}
2110

2111 2112
	return vsi;

2113 2114
unroll_clear_rings:
	ice_vsi_clear_rings(vsi);
2115
unroll_vector_base:
2116
	/* reclaim SW interrupts back to the common pool */
B
Brett Creeley 已提交
2117
	ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2118
	pf->num_avail_sw_msix += vsi->num_q_vectors;
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
unroll_alloc_q_vector:
	ice_vsi_free_q_vectors(vsi);
unroll_vsi_init:
	ice_vsi_delete(vsi);
unroll_get_qs:
	ice_vsi_put_qs(vsi);
	ice_vsi_clear(vsi);

	return NULL;
}

2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
/**
 * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
 * @vsi: the VSI being cleaned up
 */
static void ice_vsi_release_msix(struct ice_vsi *vsi)
{
	struct ice_pf *pf = vsi->back;
	struct ice_hw *hw = &pf->hw;
	u32 txq = 0;
	u32 rxq = 0;
	int i, q;

B
Brett Creeley 已提交
2142
	for (i = 0; i < vsi->num_q_vectors; i++) {
2143
		struct ice_q_vector *q_vector = vsi->q_vectors[i];
B
Brett Creeley 已提交
2144
		u16 reg_idx = q_vector->reg_idx;
2145

B
Brett Creeley 已提交
2146 2147
		wr32(hw, GLINT_ITR(ICE_IDX_ITR0, reg_idx), 0);
		wr32(hw, GLINT_ITR(ICE_IDX_ITR1, reg_idx), 0);
2148 2149
		for (q = 0; q < q_vector->num_ring_tx; q++) {
			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
M
Maciej Fijalkowski 已提交
2150 2151 2152 2153 2154
			if (ice_is_xdp_ena_vsi(vsi)) {
				u32 xdp_txq = txq + vsi->num_xdp_txq;

				wr32(hw, QINT_TQCTL(vsi->txq_map[xdp_txq]), 0);
			}
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
			txq++;
		}

		for (q = 0; q < q_vector->num_ring_rx; q++) {
			wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
			rxq++;
		}
	}

	ice_flush(hw);
}

/**
 * ice_vsi_free_irq - Free the IRQ association with the OS
 * @vsi: the VSI being configured
 */
void ice_vsi_free_irq(struct ice_vsi *vsi)
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
2174
	int base = vsi->base_vector;
2175
	int i;
2176

2177 2178
	if (!vsi->q_vectors || !vsi->irqs_ready)
		return;
2179

2180 2181 2182
	ice_vsi_release_msix(vsi);
	if (vsi->type == ICE_VSI_VF)
		return;
2183

2184 2185 2186 2187
	vsi->irqs_ready = false;
	ice_for_each_q_vector(vsi, i) {
		u16 vector = i + base;
		int irq_num;
2188

2189
		irq_num = pf->msix_entries[vector].vector;
2190

2191 2192 2193 2194 2195
		/* free only the irqs that were actually requested */
		if (!vsi->q_vectors[i] ||
		    !(vsi->q_vectors[i]->num_ring_tx ||
		      vsi->q_vectors[i]->num_ring_rx))
			continue;
2196

2197 2198
		/* clear the affinity notifier in the IRQ descriptor */
		irq_set_affinity_notifier(irq_num, NULL);
2199

2200 2201 2202
		/* clear the affinity_mask in the IRQ descriptor */
		irq_set_affinity_hint(irq_num, NULL);
		synchronize_irq(irq_num);
B
Brett Creeley 已提交
2203
		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
	}
}

/**
 * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
 * @vsi: the VSI having resources freed
 */
void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
{
	int i;

	if (!vsi->tx_rings)
		return;

	ice_for_each_txq(vsi, i)
		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
			ice_free_tx_ring(vsi->tx_rings[i]);
}

/**
 * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
 * @vsi: the VSI having resources freed
 */
void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
{
	int i;

	if (!vsi->rx_rings)
		return;

	ice_for_each_rxq(vsi, i)
		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
			ice_free_rx_ring(vsi->rx_rings[i]);
}

2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
/**
 * ice_vsi_close - Shut down a VSI
 * @vsi: the VSI being shut down
 */
void ice_vsi_close(struct ice_vsi *vsi)
{
	if (!test_and_set_bit(__ICE_DOWN, vsi->state))
		ice_down(vsi);

	ice_vsi_free_irq(vsi);
	ice_vsi_free_tx_rings(vsi);
	ice_vsi_free_rx_rings(vsi);
}

2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308
/**
 * ice_ena_vsi - resume a VSI
 * @vsi: the VSI being resume
 * @locked: is the rtnl_lock already held
 */
int ice_ena_vsi(struct ice_vsi *vsi, bool locked)
{
	int err = 0;

	if (!test_bit(__ICE_NEEDS_RESTART, vsi->state))
		return 0;

	clear_bit(__ICE_NEEDS_RESTART, vsi->state);

	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
		if (netif_running(vsi->netdev)) {
			if (!locked)
				rtnl_lock();

			err = ice_open(vsi->netdev);

			if (!locked)
				rtnl_unlock();
		}
	}

	return err;
}

/**
 * ice_dis_vsi - pause a VSI
 * @vsi: the VSI being paused
 * @locked: is the rtnl_lock already held
 */
void ice_dis_vsi(struct ice_vsi *vsi, bool locked)
{
	if (test_bit(__ICE_DOWN, vsi->state))
		return;

	set_bit(__ICE_NEEDS_RESTART, vsi->state);

	if (vsi->type == ICE_VSI_PF && vsi->netdev) {
		if (netif_running(vsi->netdev)) {
			if (!locked)
				rtnl_lock();

			ice_stop(vsi->netdev);

			if (!locked)
				rtnl_unlock();
		} else {
			ice_vsi_close(vsi);
		}
	}
}

2309 2310 2311 2312 2313 2314
/**
 * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
 * @vsi: the VSI being un-configured
 */
void ice_vsi_dis_irq(struct ice_vsi *vsi)
{
B
Brett Creeley 已提交
2315
	int base = vsi->base_vector;
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
	struct ice_pf *pf = vsi->back;
	struct ice_hw *hw = &pf->hw;
	u32 val;
	int i;

	/* disable interrupt causation from each queue */
	if (vsi->tx_rings) {
		ice_for_each_txq(vsi, i) {
			if (vsi->tx_rings[i]) {
				u16 reg;

				reg = vsi->tx_rings[i]->reg_idx;
				val = rd32(hw, QINT_TQCTL(reg));
				val &= ~QINT_TQCTL_CAUSE_ENA_M;
				wr32(hw, QINT_TQCTL(reg), val);
			}
		}
	}

	if (vsi->rx_rings) {
		ice_for_each_rxq(vsi, i) {
			if (vsi->rx_rings[i]) {
				u16 reg;

				reg = vsi->rx_rings[i]->reg_idx;
				val = rd32(hw, QINT_RQCTL(reg));
				val &= ~QINT_RQCTL_CAUSE_ENA_M;
				wr32(hw, QINT_RQCTL(reg), val);
			}
		}
	}

	/* disable each interrupt */
T
Tony Nguyen 已提交
2349 2350 2351
	ice_for_each_q_vector(vsi, i) {
		if (!vsi->q_vectors[i])
			continue;
2352
		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
T
Tony Nguyen 已提交
2353
	}
2354

2355
	ice_flush(hw);
2356

2357 2358 2359 2360
	/* don't call synchronize_irq() for VF's from the host */
	if (vsi->type == ICE_VSI_VF)
		return;

2361 2362
	ice_for_each_q_vector(vsi, i)
		synchronize_irq(pf->msix_entries[i + base].vector);
2363 2364
}

2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379
/**
 * ice_napi_del - Remove NAPI handler for the VSI
 * @vsi: VSI for which NAPI handler is to be removed
 */
void ice_napi_del(struct ice_vsi *vsi)
{
	int v_idx;

	if (!vsi->netdev)
		return;

	ice_for_each_q_vector(vsi, v_idx)
		netif_napi_del(&vsi->q_vectors[v_idx]->napi);
}

2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
/**
 * ice_vsi_release - Delete a VSI and free its resources
 * @vsi: the VSI being removed
 *
 * Returns 0 on success or < 0 on error
 */
int ice_vsi_release(struct ice_vsi *vsi)
{
	struct ice_pf *pf;

	if (!vsi->back)
		return -ENODEV;
	pf = vsi->back;
2393

2394 2395 2396 2397 2398
	/* do not unregister while driver is in the reset recovery pending
	 * state. Since reset/rebuild happens through PF service task workqueue,
	 * it's not a good idea to unregister netdev that is associated to the
	 * PF that is running the work queue items currently. This is done to
	 * avoid check_flush_dependency() warning on this wq
2399
	 */
2400
	if (vsi->netdev && !ice_is_reset_in_progress(pf->state))
2401 2402 2403 2404 2405 2406
		unregister_netdev(vsi->netdev);

	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
		ice_rss_clean(vsi);

	/* Disable VSI and free resources */
2407 2408
	if (vsi->type != ICE_VSI_LB)
		ice_vsi_dis_irq(vsi);
2409 2410
	ice_vsi_close(vsi);

B
Brett Creeley 已提交
2411 2412 2413 2414 2415
	/* SR-IOV determines needed MSIX resources all at once instead of per
	 * VSI since when VFs are spawned we know how many VFs there are and how
	 * many interrupts each VF needs. SR-IOV MSIX resources are also
	 * cleared in the same manner.
	 */
2416 2417
	if (vsi->type != ICE_VSI_VF) {
		/* reclaim SW interrupts back to the common pool */
B
Brett Creeley 已提交
2418
		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2419 2420
		pf->num_avail_sw_msix += vsi->num_q_vectors;
	}
2421

T
Tony Nguyen 已提交
2422 2423
	if (!ice_is_safe_mode(pf)) {
		if (vsi->type == ICE_VSI_PF) {
2424 2425
			ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
					    ICE_DROP_PACKET);
T
Tony Nguyen 已提交
2426 2427 2428 2429 2430 2431 2432
			ice_cfg_sw_lldp(vsi, true, false);
			/* The Rx rule will only exist to remove if the LLDP FW
			 * engine is currently stopped
			 */
			if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags))
				ice_cfg_sw_lldp(vsi, false, false);
		}
2433
	}
2434

2435
	ice_fltr_remove_all(vsi);
2436
	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2437 2438
	ice_vsi_delete(vsi);
	ice_vsi_free_q_vectors(vsi);
2439 2440 2441 2442 2443 2444 2445

	/* make sure unregister_netdev() was called by checking __ICE_DOWN */
	if (vsi->netdev && test_bit(__ICE_DOWN, vsi->state)) {
		free_netdev(vsi->netdev);
		vsi->netdev = NULL;
	}

2446 2447 2448 2449 2450 2451 2452 2453
	ice_vsi_clear_rings(vsi);

	ice_vsi_put_qs(vsi);

	/* retain SW VSI data structure since it is needed to unregister and
	 * free VSI netdev when PF is not in reset recovery pending state,\
	 * for ex: during rmmod.
	 */
2454
	if (!ice_is_reset_in_progress(pf->state))
2455 2456 2457 2458 2459
		ice_vsi_clear(vsi);

	return 0;
}

2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
/**
 * ice_vsi_rebuild_update_coalesce - set coalesce for a q_vector
 * @q_vector: pointer to q_vector which is being updated
 * @coalesce: pointer to array of struct with stored coalesce
 *
 * Set coalesce param in q_vector and update these parameters in HW.
 */
static void
ice_vsi_rebuild_update_coalesce(struct ice_q_vector *q_vector,
				struct ice_coalesce_stored *coalesce)
{
	struct ice_ring_container *rx_rc = &q_vector->rx;
	struct ice_ring_container *tx_rc = &q_vector->tx;
	struct ice_hw *hw = &q_vector->vsi->back->hw;

	tx_rc->itr_setting = coalesce->itr_tx;
	rx_rc->itr_setting = coalesce->itr_rx;

	/* dynamic ITR values will be updated during Tx/Rx */
	if (!ITR_IS_DYNAMIC(tx_rc->itr_setting))
		wr32(hw, GLINT_ITR(tx_rc->itr_idx, q_vector->reg_idx),
		     ITR_REG_ALIGN(tx_rc->itr_setting) >>
		     ICE_ITR_GRAN_S);
	if (!ITR_IS_DYNAMIC(rx_rc->itr_setting))
		wr32(hw, GLINT_ITR(rx_rc->itr_idx, q_vector->reg_idx),
		     ITR_REG_ALIGN(rx_rc->itr_setting) >>
		     ICE_ITR_GRAN_S);

	q_vector->intrl = coalesce->intrl;
	wr32(hw, GLINT_RATE(q_vector->reg_idx),
	     ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
}

/**
 * ice_vsi_rebuild_get_coalesce - get coalesce from all q_vectors
 * @vsi: VSI connected with q_vectors
 * @coalesce: array of struct with stored coalesce
 *
 * Returns array size.
 */
static int
ice_vsi_rebuild_get_coalesce(struct ice_vsi *vsi,
			     struct ice_coalesce_stored *coalesce)
{
	int i;

	ice_for_each_q_vector(vsi, i) {
		struct ice_q_vector *q_vector = vsi->q_vectors[i];

		coalesce[i].itr_tx = q_vector->tx.itr_setting;
		coalesce[i].itr_rx = q_vector->rx.itr_setting;
		coalesce[i].intrl = q_vector->intrl;
	}

	return vsi->num_q_vectors;
}

/**
 * ice_vsi_rebuild_set_coalesce - set coalesce from earlier saved arrays
 * @vsi: VSI connected with q_vectors
 * @coalesce: pointer to array of struct with stored coalesce
 * @size: size of coalesce array
 *
 * Before this function, ice_vsi_rebuild_get_coalesce should be called to save
 * ITR params in arrays. If size is 0 or coalesce wasn't stored set coalesce
 * to default value.
 */
static void
ice_vsi_rebuild_set_coalesce(struct ice_vsi *vsi,
			     struct ice_coalesce_stored *coalesce, int size)
{
	int i;

	if ((size && !coalesce) || !vsi)
		return;

	for (i = 0; i < size && i < vsi->num_q_vectors; i++)
		ice_vsi_rebuild_update_coalesce(vsi->q_vectors[i],
						&coalesce[i]);

	for (; i < vsi->num_q_vectors; i++) {
		struct ice_coalesce_stored coalesce_dflt = {
			.itr_tx = ICE_DFLT_TX_ITR,
			.itr_rx = ICE_DFLT_RX_ITR,
			.intrl = 0
		};
		ice_vsi_rebuild_update_coalesce(vsi->q_vectors[i],
						&coalesce_dflt);
	}
}

2551 2552 2553
/**
 * ice_vsi_rebuild - Rebuild VSI after reset
 * @vsi: VSI to be rebuild
2554
 * @init_vsi: is this an initialization or a reconfigure of the VSI
2555 2556 2557
 *
 * Returns 0 on success and negative value on failure
 */
2558
int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
2559 2560
{
	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2561 2562
	struct ice_coalesce_stored *coalesce;
	int prev_num_q_vectors = 0;
2563
	struct ice_vf *vf = NULL;
2564
	enum ice_status status;
2565
	struct ice_pf *pf;
2566 2567 2568 2569 2570
	int ret, i;

	if (!vsi)
		return -EINVAL;

2571
	pf = vsi->back;
2572 2573 2574
	if (vsi->type == ICE_VSI_VF)
		vf = &pf->vf[vsi->vf_id];

2575 2576 2577 2578 2579
	coalesce = kcalloc(vsi->num_q_vectors,
			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
	if (coalesce)
		prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi,
								  coalesce);
2580
	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
2581
	ice_vsi_free_q_vectors(vsi);
2582

B
Brett Creeley 已提交
2583 2584 2585 2586 2587
	/* SR-IOV determines needed MSIX resources all at once instead of per
	 * VSI since when VFs are spawned we know how many VFs there are and how
	 * many interrupts each VF needs. SR-IOV MSIX resources are also
	 * cleared in the same manner.
	 */
2588 2589
	if (vsi->type != ICE_VSI_VF) {
		/* reclaim SW interrupts back to the common pool */
B
Brett Creeley 已提交
2590
		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2591
		pf->num_avail_sw_msix += vsi->num_q_vectors;
B
Brett Creeley 已提交
2592
		vsi->base_vector = 0;
2593 2594
	}

M
Maciej Fijalkowski 已提交
2595 2596 2597 2598 2599
	if (ice_is_xdp_ena_vsi(vsi))
		/* return value check can be skipped here, it always returns
		 * 0 if reset is in progress
		 */
		ice_destroy_xdp_rings(vsi);
2600
	ice_vsi_put_qs(vsi);
2601
	ice_vsi_clear_rings(vsi);
2602
	ice_vsi_free_arrays(vsi);
2603 2604 2605 2606
	if (vsi->type == ICE_VSI_VF)
		ice_vsi_set_num_qs(vsi, vf->vf_id);
	else
		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
2607 2608 2609 2610 2611 2612

	ret = ice_vsi_alloc_arrays(vsi);
	if (ret < 0)
		goto err_vsi;

	ice_vsi_get_qs(vsi);
2613
	ice_vsi_set_tc_cfg(vsi);
2614 2615

	/* Initialize VSI struct elements and create VSI in FW */
2616
	ret = ice_vsi_init(vsi, init_vsi);
2617 2618 2619 2620 2621 2622 2623 2624 2625
	if (ret < 0)
		goto err_vsi;

	switch (vsi->type) {
	case ICE_VSI_PF:
		ret = ice_vsi_alloc_q_vectors(vsi);
		if (ret)
			goto err_rings;

2626 2627 2628 2629
		ret = ice_vsi_setup_vector_base(vsi);
		if (ret)
			goto err_vectors;

2630 2631 2632 2633
		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
		if (ret)
			goto err_vectors;

2634 2635 2636 2637 2638
		ret = ice_vsi_alloc_rings(vsi);
		if (ret)
			goto err_vectors;

		ice_vsi_map_rings_to_vectors(vsi);
M
Maciej Fijalkowski 已提交
2639 2640 2641 2642 2643 2644
		if (ice_is_xdp_ena_vsi(vsi)) {
			vsi->num_xdp_txq = vsi->alloc_txq;
			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
			if (ret)
				goto err_vectors;
		}
2645 2646 2647 2648
		/* Do not exit if configuring RSS had an issue, at least
		 * receive traffic on first queue. Hence no need to capture
		 * return value
		 */
2649
		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
2650
			ice_vsi_cfg_rss_lut_key(vsi);
2651
		break;
2652 2653 2654 2655 2656
	case ICE_VSI_VF:
		ret = ice_vsi_alloc_q_vectors(vsi);
		if (ret)
			goto err_rings;

2657 2658 2659 2660
		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
		if (ret)
			goto err_vectors;

2661 2662 2663 2664 2665
		ret = ice_vsi_alloc_rings(vsi);
		if (ret)
			goto err_vectors;

		break;
2666 2667 2668 2669 2670
	default:
		break;
	}

	/* configure VSI nodes based on number of queues and TC's */
M
Maciej Fijalkowski 已提交
2671
	for (i = 0; i < vsi->tc_cfg.numtc; i++) {
2672
		max_txqs[i] = vsi->alloc_txq;
2673

M
Maciej Fijalkowski 已提交
2674 2675 2676 2677
		if (ice_is_xdp_ena_vsi(vsi))
			max_txqs[i] += vsi->num_xdp_txq;
	}

2678 2679 2680
	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
				 max_txqs);
	if (status) {
2681 2682
		dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
			vsi->vsi_num, ice_stat_str(status));
2683 2684 2685 2686 2687 2688
		if (init_vsi) {
			ret = -EIO;
			goto err_vectors;
		} else {
			return ice_schedule_reset(pf, ICE_RESET_PFR);
		}
2689
	}
2690 2691 2692
	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
	kfree(coalesce);

2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
	return 0;

err_vectors:
	ice_vsi_free_q_vectors(vsi);
err_rings:
	if (vsi->netdev) {
		vsi->current_netdev_flags = 0;
		unregister_netdev(vsi->netdev);
		free_netdev(vsi->netdev);
		vsi->netdev = NULL;
	}
err_vsi:
	ice_vsi_clear(vsi);
2706
	set_bit(__ICE_RESET_FAILED, pf->state);
2707
	kfree(coalesce);
2708 2709 2710
	return ret;
}

2711
/**
2712
 * ice_is_reset_in_progress - check for a reset in progress
2713
 * @state: PF state field
2714
 */
2715
bool ice_is_reset_in_progress(unsigned long *state)
2716
{
2717
	return test_bit(__ICE_RESET_OICR_RECV, state) ||
D
Dave Ertman 已提交
2718
	       test_bit(__ICE_DCBNL_DEVRESET, state) ||
2719 2720 2721
	       test_bit(__ICE_PFR_REQ, state) ||
	       test_bit(__ICE_CORER_REQ, state) ||
	       test_bit(__ICE_GLOBR_REQ, state);
2722
}
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749

#ifdef CONFIG_DCB
/**
 * ice_vsi_update_q_map - update our copy of the VSI info with new queue map
 * @vsi: VSI being configured
 * @ctx: the context buffer returned from AQ VSI update command
 */
static void ice_vsi_update_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctx)
{
	vsi->info.mapping_flags = ctx->info.mapping_flags;
	memcpy(&vsi->info.q_mapping, &ctx->info.q_mapping,
	       sizeof(vsi->info.q_mapping));
	memcpy(&vsi->info.tc_mapping, ctx->info.tc_mapping,
	       sizeof(vsi->info.tc_mapping));
}

/**
 * ice_vsi_cfg_tc - Configure VSI Tx Sched for given TC map
 * @vsi: VSI to be configured
 * @ena_tc: TC bitmap
 *
 * VSI queues expected to be quiesced before calling this function
 */
int ice_vsi_cfg_tc(struct ice_vsi *vsi, u8 ena_tc)
{
	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
	struct ice_pf *pf = vsi->back;
T
Tony Nguyen 已提交
2750
	struct ice_vsi_ctx *ctx;
2751
	enum ice_status status;
B
Brett Creeley 已提交
2752
	struct device *dev;
2753 2754 2755
	int i, ret = 0;
	u8 num_tc = 0;

B
Brett Creeley 已提交
2756 2757
	dev = ice_pf_to_dev(pf);

2758 2759 2760 2761 2762
	ice_for_each_traffic_class(i) {
		/* build bitmap of enabled TCs */
		if (ena_tc & BIT(i))
			num_tc++;
		/* populate max_txqs per TC */
2763
		max_txqs[i] = vsi->alloc_txq;
2764 2765 2766 2767 2768
	}

	vsi->tc_cfg.ena_tc = ena_tc;
	vsi->tc_cfg.numtc = num_tc;

2769
	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
	if (!ctx)
		return -ENOMEM;

	ctx->vf_num = 0;
	ctx->info = vsi->info;

	ice_vsi_setup_q_map(vsi, ctx);

	/* must to indicate which section of VSI context are being modified */
	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_RXQ_MAP_VALID);
	status = ice_update_vsi(&pf->hw, vsi->idx, ctx, NULL);
	if (status) {
B
Brett Creeley 已提交
2782
		dev_info(dev, "Failed VSI Update\n");
2783 2784 2785 2786 2787 2788 2789 2790
		ret = -EIO;
		goto out;
	}

	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
				 max_txqs);

	if (status) {
2791 2792
		dev_err(dev, "VSI %d failed TC config, error %s\n",
			vsi->vsi_num, ice_stat_str(status));
2793 2794 2795 2796 2797 2798 2799 2800
		ret = -EIO;
		goto out;
	}
	ice_vsi_update_q_map(vsi, ctx);
	vsi->info.valid_sections = 0;

	ice_vsi_cfg_netdev_tc(vsi, ena_tc);
out:
2801
	kfree(ctx);
2802 2803 2804
	return ret;
}
#endif /* CONFIG_DCB */
2805

2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850
/**
 * ice_update_ring_stats - Update ring statistics
 * @ring: ring to update
 * @cont: used to increment per-vector counters
 * @pkts: number of processed packets
 * @bytes: number of processed bytes
 *
 * This function assumes that caller has acquired a u64_stats_sync lock.
 */
static void
ice_update_ring_stats(struct ice_ring *ring, struct ice_ring_container *cont,
		      u64 pkts, u64 bytes)
{
	ring->stats.bytes += bytes;
	ring->stats.pkts += pkts;
	cont->total_bytes += bytes;
	cont->total_pkts += pkts;
}

/**
 * ice_update_tx_ring_stats - Update Tx ring specific counters
 * @tx_ring: ring to update
 * @pkts: number of processed packets
 * @bytes: number of processed bytes
 */
void ice_update_tx_ring_stats(struct ice_ring *tx_ring, u64 pkts, u64 bytes)
{
	u64_stats_update_begin(&tx_ring->syncp);
	ice_update_ring_stats(tx_ring, &tx_ring->q_vector->tx, pkts, bytes);
	u64_stats_update_end(&tx_ring->syncp);
}

/**
 * ice_update_rx_ring_stats - Update Rx ring specific counters
 * @rx_ring: ring to update
 * @pkts: number of processed packets
 * @bytes: number of processed bytes
 */
void ice_update_rx_ring_stats(struct ice_ring *rx_ring, u64 pkts, u64 bytes)
{
	u64_stats_update_begin(&rx_ring->syncp);
	ice_update_ring_stats(rx_ring, &rx_ring->q_vector->rx, pkts, bytes);
	u64_stats_update_end(&rx_ring->syncp);
}

2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908
/**
 * ice_is_dflt_vsi_in_use - check if the default forwarding VSI is being used
 * @sw: switch to check if its default forwarding VSI is free
 *
 * Return true if the default forwarding VSI is already being used, else returns
 * false signalling that it's available to use.
 */
bool ice_is_dflt_vsi_in_use(struct ice_sw *sw)
{
	return (sw->dflt_vsi && sw->dflt_vsi_ena);
}

/**
 * ice_is_vsi_dflt_vsi - check if the VSI passed in is the default VSI
 * @sw: switch for the default forwarding VSI to compare against
 * @vsi: VSI to compare against default forwarding VSI
 *
 * If this VSI passed in is the default forwarding VSI then return true, else
 * return false
 */
bool ice_is_vsi_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
{
	return (sw->dflt_vsi == vsi && sw->dflt_vsi_ena);
}

/**
 * ice_set_dflt_vsi - set the default forwarding VSI
 * @sw: switch used to assign the default forwarding VSI
 * @vsi: VSI getting set as the default forwarding VSI on the switch
 *
 * If the VSI passed in is already the default VSI and it's enabled just return
 * success.
 *
 * If there is already a default VSI on the switch and it's enabled then return
 * -EEXIST since there can only be one default VSI per switch.
 *
 *  Otherwise try to set the VSI passed in as the switch's default VSI and
 *  return the result.
 */
int ice_set_dflt_vsi(struct ice_sw *sw, struct ice_vsi *vsi)
{
	enum ice_status status;
	struct device *dev;

	if (!sw || !vsi)
		return -EINVAL;

	dev = ice_pf_to_dev(vsi->back);

	/* the VSI passed in is already the default VSI */
	if (ice_is_vsi_dflt_vsi(sw, vsi)) {
		dev_dbg(dev, "VSI %d passed in is already the default forwarding VSI, nothing to do\n",
			vsi->vsi_num);
		return 0;
	}

	/* another VSI is already the default VSI for this switch */
	if (ice_is_dflt_vsi_in_use(sw)) {
2909
		dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
2910 2911 2912 2913 2914 2915
			sw->dflt_vsi->vsi_num);
		return -EEXIST;
	}

	status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
	if (status) {
2916 2917
		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
			vsi->vsi_num, ice_stat_str(status));
2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
		return -EIO;
	}

	sw->dflt_vsi = vsi;
	sw->dflt_vsi_ena = true;

	return 0;
}

/**
 * ice_clear_dflt_vsi - clear the default forwarding VSI
 * @sw: switch used to clear the default VSI
 *
 * If the switch has no default VSI or it's not enabled then return error.
 *
 * Otherwise try to clear the default VSI and return the result.
 */
int ice_clear_dflt_vsi(struct ice_sw *sw)
{
	struct ice_vsi *dflt_vsi;
	enum ice_status status;
	struct device *dev;

	if (!sw)
		return -EINVAL;

	dev = ice_pf_to_dev(sw->pf);

	dflt_vsi = sw->dflt_vsi;

	/* there is no default VSI configured */
	if (!ice_is_dflt_vsi_in_use(sw))
		return -ENODEV;

	status = ice_cfg_dflt_vsi(&dflt_vsi->back->hw, dflt_vsi->idx, false,
				  ICE_FLTR_RX);
	if (status) {
2955 2956
		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
			dflt_vsi->vsi_num, ice_stat_str(status));
2957 2958 2959 2960 2961 2962 2963 2964
		return -EIO;
	}

	sw->dflt_vsi = NULL;
	sw->dflt_vsi_ena = false;

	return 0;
}