ice_lib.c 101.5 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
#include "ice_devlink.h"
11

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

34
/**
35
 * ice_vsi_ctrl_all_rx_rings - Start or stop a VSI's Rx rings
36 37
 * @vsi: the VSI being configured
 * @ena: start or stop the Rx rings
38 39 40 41 42
 *
 * 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.
43
 */
44
static int ice_vsi_ctrl_all_rx_rings(struct ice_vsi *vsi, bool ena)
45
{
K
Karol Kolacinski 已提交
46 47
	int ret = 0;
	u16 i;
48

49
	ice_for_each_rxq(vsi, i)
50 51 52 53
		ice_vsi_ctrl_one_rx_ring(vsi, ena, i, false);

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

54
	ice_for_each_rxq(vsi, i) {
55
		ret = ice_vsi_wait_one_rx_ring(vsi, ena, i);
56
		if (ret)
57 58 59 60 61 62
			break;
	}

	return ret;
}

63 64 65 66 67 68 69
/**
 * 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
 */
70
static int ice_vsi_alloc_arrays(struct ice_vsi *vsi)
71 72
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
73 74 75
	struct device *dev;

	dev = ice_pf_to_dev(pf);
76 77

	/* allocate memory for both Tx and Rx ring pointers */
B
Brett Creeley 已提交
78
	vsi->tx_rings = devm_kcalloc(dev, vsi->alloc_txq,
79
				     sizeof(*vsi->tx_rings), GFP_KERNEL);
80
	if (!vsi->tx_rings)
81
		return -ENOMEM;
82

B
Brett Creeley 已提交
83
	vsi->rx_rings = devm_kcalloc(dev, vsi->alloc_rxq,
84
				     sizeof(*vsi->rx_rings), GFP_KERNEL);
85
	if (!vsi->rx_rings)
86 87
		goto err_rings;

M
Maciej Fijalkowski 已提交
88
	/* XDP will have vsi->alloc_txq Tx queues as well, so double the size */
B
Brett Creeley 已提交
89
	vsi->txq_map = devm_kcalloc(dev, (2 * vsi->alloc_txq),
90 91 92 93 94
				    sizeof(*vsi->txq_map), GFP_KERNEL);

	if (!vsi->txq_map)
		goto err_txq_map;

B
Brett Creeley 已提交
95
	vsi->rxq_map = devm_kcalloc(dev, vsi->alloc_rxq,
96 97 98 99
				    sizeof(*vsi->rxq_map), GFP_KERNEL);
	if (!vsi->rxq_map)
		goto err_rxq_map;

100 101 102 103
	/* There is no need to allocate q_vectors for a loopback VSI. */
	if (vsi->type == ICE_VSI_LB)
		return 0;

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

110 111 112 113
	vsi->af_xdp_zc_qps = bitmap_zalloc(max_t(int, vsi->alloc_txq, vsi->alloc_rxq), GFP_KERNEL);
	if (!vsi->af_xdp_zc_qps)
		goto err_zc_qps;

114 115
	return 0;

116 117
err_zc_qps:
	devm_kfree(dev, vsi->q_vectors);
118
err_vectors:
B
Brett Creeley 已提交
119
	devm_kfree(dev, vsi->rxq_map);
120
err_rxq_map:
B
Brett Creeley 已提交
121
	devm_kfree(dev, vsi->txq_map);
122
err_txq_map:
B
Brett Creeley 已提交
123
	devm_kfree(dev, vsi->rx_rings);
124
err_rings:
B
Brett Creeley 已提交
125
	devm_kfree(dev, vsi->tx_rings);
126 127 128 129
	return -ENOMEM;
}

/**
130 131 132 133 134 135 136
 * 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:
137
	case ICE_VSI_SWITCHDEV_CTRL:
138
	case ICE_VSI_CTRL:
139
	case ICE_VSI_LB:
140 141 142 143 144 145 146 147
		/* a user could change the values of num_[tr]x_desc using
		 * ethtool -G so we should keep those values instead of
		 * overwriting them with the defaults.
		 */
		if (!vsi->num_rx_desc)
			vsi->num_rx_desc = ICE_DFLT_NUM_RX_DESC;
		if (!vsi->num_tx_desc)
			vsi->num_tx_desc = ICE_DFLT_NUM_TX_DESC;
148 149
		break;
	default:
150
		dev_dbg(ice_pf_to_dev(vsi->back), "Not setting number of Tx/Rx descriptors for VSI type %d\n",
151 152 153 154 155 156 157
			vsi->type);
		break;
	}
}

/**
 * ice_vsi_set_num_qs - Set number of queues, descriptors and vectors for a VSI
158
 * @vsi: the VSI being configured
159
 * @vf_id: ID of the VF being configured
160 161 162
 *
 * Return 0 on success and a negative value on error
 */
163
static void ice_vsi_set_num_qs(struct ice_vsi *vsi, u16 vf_id)
164 165
{
	struct ice_pf *pf = vsi->back;
166 167 168 169
	struct ice_vf *vf = NULL;

	if (vsi->type == ICE_VSI_VF)
		vsi->vf_id = vf_id;
170 171
	else
		vsi->vf_id = ICE_INVAL_VFID;
172

173 174
	switch (vsi->type) {
	case ICE_VSI_PF:
175 176 177
		if (vsi->req_txq) {
			vsi->alloc_txq = vsi->req_txq;
			vsi->num_txq = vsi->req_txq;
178 179 180 181
		} else {
			vsi->alloc_txq = min3(pf->num_lan_msix,
					      ice_get_avail_txq_count(pf),
					      (u16)num_online_cpus());
182
		}
183 184 185 186

		pf->num_lan_tx = vsi->alloc_txq;

		/* only 1 Rx queue unless RSS is enabled */
187
		if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
188
			vsi->alloc_rxq = 1;
189 190 191 192
		} else {
			if (vsi->req_rxq) {
				vsi->alloc_rxq = vsi->req_rxq;
				vsi->num_rxq = vsi->req_rxq;
193 194 195 196
			} else {
				vsi->alloc_rxq = min3(pf->num_lan_msix,
						      ice_get_avail_rxq_count(pf),
						      (u16)num_online_cpus());
197 198
			}
		}
199 200 201

		pf->num_lan_rx = vsi->alloc_rxq;

202 203 204
		vsi->num_q_vectors = min_t(int, pf->num_lan_msix,
					   max_t(int, vsi->alloc_rxq,
						 vsi->alloc_txq));
205
		break;
206 207 208 209 210 211 212 213
	case ICE_VSI_SWITCHDEV_CTRL:
		/* The number of queues for ctrl VSI is equal to number of VFs.
		 * Each ring is associated to the corresponding VF_PR netdev.
		 */
		vsi->alloc_txq = pf->num_alloc_vfs;
		vsi->alloc_rxq = pf->num_alloc_vfs;
		vsi->num_q_vectors = 1;
		break;
214
	case ICE_VSI_VF:
215
		vf = &pf->vf[vsi->vf_id];
216 217
		if (vf->num_req_qs)
			vf->num_vf_qs = vf->num_req_qs;
218 219
		vsi->alloc_txq = vf->num_vf_qs;
		vsi->alloc_rxq = vf->num_vf_qs;
220
		/* pf->num_msix_per_vf includes (VF miscellaneous vector +
221
		 * data queue interrupts). Since vsi->num_q_vectors is number
222 223
		 * of queues vectors, subtract 1 (ICE_NONQ_VECS_VF) from the
		 * original vector count
224
		 */
225
		vsi->num_q_vectors = pf->num_msix_per_vf - ICE_NONQ_VECS_VF;
226
		break;
227 228 229 230 231
	case ICE_VSI_CTRL:
		vsi->alloc_txq = 1;
		vsi->alloc_rxq = 1;
		vsi->num_q_vectors = 1;
		break;
232 233 234 235
	case ICE_VSI_LB:
		vsi->alloc_txq = 1;
		vsi->alloc_rxq = 1;
		break;
236
	default:
B
Brett Creeley 已提交
237
		dev_warn(ice_pf_to_dev(pf), "Unknown VSI type %d\n", vsi->type);
238 239
		break;
	}
240 241

	ice_vsi_set_num_desc(vsi);
242 243 244 245 246 247 248 249 250 251 252
}

/**
 * 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.
 */
253
static int ice_get_free_slot(void *array, int size, int curr)
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
{
	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;
}

273 274 275 276
/**
 * ice_vsi_delete - delete a VSI from the switch
 * @vsi: pointer to VSI being removed
 */
277
static void ice_vsi_delete(struct ice_vsi *vsi)
278 279
{
	struct ice_pf *pf = vsi->back;
280
	struct ice_vsi_ctx *ctxt;
281 282
	enum ice_status status;

283
	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
284 285 286
	if (!ctxt)
		return;

287
	if (vsi->type == ICE_VSI_VF)
288 289
		ctxt->vf_num = vsi->vf_id;
	ctxt->vsi_num = vsi->vsi_num;
290

291
	memcpy(&ctxt->info, &vsi->info, sizeof(ctxt->info));
292

293
	status = ice_free_vsi(&pf->hw, vsi->idx, ctxt, false, NULL);
294
	if (status)
295 296
		dev_err(ice_pf_to_dev(pf), "Failed to delete VSI %i in FW - error: %s\n",
			vsi->vsi_num, ice_stat_str(status));
297

298
	kfree(ctxt);
299 300
}

301
/**
302
 * ice_vsi_free_arrays - De-allocate queue and vector pointer arrays for the VSI
303 304
 * @vsi: pointer to VSI being cleared
 */
305
static void ice_vsi_free_arrays(struct ice_vsi *vsi)
306 307
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
308 309 310
	struct device *dev;

	dev = ice_pf_to_dev(pf);
311

312 313 314 315
	if (vsi->af_xdp_zc_qps) {
		bitmap_free(vsi->af_xdp_zc_qps);
		vsi->af_xdp_zc_qps = NULL;
	}
316
	/* free the ring and vector containers */
317
	if (vsi->q_vectors) {
B
Brett Creeley 已提交
318
		devm_kfree(dev, vsi->q_vectors);
319 320 321
		vsi->q_vectors = NULL;
	}
	if (vsi->tx_rings) {
B
Brett Creeley 已提交
322
		devm_kfree(dev, vsi->tx_rings);
323 324 325
		vsi->tx_rings = NULL;
	}
	if (vsi->rx_rings) {
B
Brett Creeley 已提交
326
		devm_kfree(dev, vsi->rx_rings);
327 328
		vsi->rx_rings = NULL;
	}
329
	if (vsi->txq_map) {
B
Brett Creeley 已提交
330
		devm_kfree(dev, vsi->txq_map);
331 332 333
		vsi->txq_map = NULL;
	}
	if (vsi->rxq_map) {
B
Brett Creeley 已提交
334
		devm_kfree(dev, vsi->rxq_map);
335 336
		vsi->rxq_map = NULL;
	}
337 338 339 340 341 342 343 344 345 346 347
}

/**
 * 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
 */
348
static int ice_vsi_clear(struct ice_vsi *vsi)
349 350
{
	struct ice_pf *pf = NULL;
B
Brett Creeley 已提交
351
	struct device *dev;
352 353 354 355 356 357 358 359

	if (!vsi)
		return 0;

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

	pf = vsi->back;
B
Brett Creeley 已提交
360
	dev = ice_pf_to_dev(pf);
361 362

	if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
B
Brett Creeley 已提交
363
		dev_dbg(dev, "vsi does not exist at pf->vsi[%d]\n", vsi->idx);
364 365 366 367 368 369 370
		return -EINVAL;
	}

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

	pf->vsi[vsi->idx] = NULL;
371
	if (vsi->idx < pf->next_vsi && vsi->type != ICE_VSI_CTRL)
372
		pf->next_vsi = vsi->idx;
373 374 375
	if (vsi->idx < pf->next_vsi && vsi->type == ICE_VSI_CTRL &&
	    vsi->vf_id != ICE_INVAL_VFID)
		pf->next_vsi = vsi->idx;
376

377
	ice_vsi_free_arrays(vsi);
378
	mutex_unlock(&pf->sw_mutex);
B
Brett Creeley 已提交
379
	devm_kfree(dev, vsi);
380 381 382 383

	return 0;
}

384 385 386 387 388 389 390 391 392
/**
 * ice_msix_clean_ctrl_vsi - MSIX mode interrupt handler for ctrl VSI
 * @irq: interrupt number
 * @data: pointer to a q_vector
 */
static irqreturn_t ice_msix_clean_ctrl_vsi(int __always_unused irq, void *data)
{
	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;

393
	if (!q_vector->tx.tx_ring)
394 395 396
		return IRQ_HANDLED;

#define FDIR_RX_DESC_CLEAN_BUDGET 64
397 398
	ice_clean_rx_irq(q_vector->rx.rx_ring, FDIR_RX_DESC_CLEAN_BUDGET);
	ice_clean_ctrl_tx_irq(q_vector->tx.tx_ring);
399 400 401 402

	return IRQ_HANDLED;
}

403 404 405 406 407
/**
 * ice_msix_clean_rings - MSIX mode Interrupt Handler
 * @irq: interrupt number
 * @data: pointer to a q_vector
 */
408
static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
409 410 411
{
	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;

412
	if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
413 414
		return IRQ_HANDLED;

415 416
	q_vector->total_events++;

417 418 419 420 421
	napi_schedule(&q_vector->napi);

	return IRQ_HANDLED;
}

422 423 424 425 426 427
static irqreturn_t ice_eswitch_msix_clean_rings(int __always_unused irq, void *data)
{
	struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
	struct ice_pf *pf = q_vector->vsi->back;
	int i;

428
	if (!q_vector->tx.tx_ring && !q_vector->rx.rx_ring)
429 430 431 432 433 434 435 436
		return IRQ_HANDLED;

	ice_for_each_vf(pf, i)
		napi_schedule(&pf->vf[i].repr->q_vector->napi);

	return IRQ_HANDLED;
}

437 438 439
/**
 * ice_vsi_alloc - Allocates the next available struct VSI in the PF
 * @pf: board private structure
440
 * @vsi_type: type of VSI
441
 * @vf_id: ID of the VF being configured
442 443 444
 *
 * returns a pointer to a VSI on success, NULL on failure.
 */
445
static struct ice_vsi *
446
ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type, u16 vf_id)
447
{
B
Brett Creeley 已提交
448
	struct device *dev = ice_pf_to_dev(pf);
449 450 451 452 453 454 455 456 457 458
	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 已提交
459
		dev_dbg(dev, "out of VSI slots!\n");
460 461 462
		goto unlock_pf;
	}

B
Brett Creeley 已提交
463
	vsi = devm_kzalloc(dev, sizeof(*vsi), GFP_KERNEL);
464 465 466
	if (!vsi)
		goto unlock_pf;

467
	vsi->type = vsi_type;
468
	vsi->back = pf;
469
	set_bit(ICE_VSI_DOWN, vsi->state);
470

471
	if (vsi_type == ICE_VSI_VF)
472 473 474
		ice_vsi_set_num_qs(vsi, vf_id);
	else
		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
475 476

	switch (vsi->type) {
477 478 479 480 481 482 483
	case ICE_VSI_SWITCHDEV_CTRL:
		if (ice_vsi_alloc_arrays(vsi))
			goto err_rings;

		/* Setup eswitch MSIX irq handler for VSI */
		vsi->irq_handler = ice_eswitch_msix_clean_rings;
		break;
484
	case ICE_VSI_PF:
485
		if (ice_vsi_alloc_arrays(vsi))
486 487 488 489 490
			goto err_rings;

		/* Setup default MSIX irq handler for VSI */
		vsi->irq_handler = ice_msix_clean_rings;
		break;
491 492 493 494 495 496 497
	case ICE_VSI_CTRL:
		if (ice_vsi_alloc_arrays(vsi))
			goto err_rings;

		/* Setup ctrl VSI MSIX irq handler */
		vsi->irq_handler = ice_msix_clean_ctrl_vsi;
		break;
498
	case ICE_VSI_VF:
499
		if (ice_vsi_alloc_arrays(vsi))
500 501
			goto err_rings;
		break;
502 503 504 505
	case ICE_VSI_LB:
		if (ice_vsi_alloc_arrays(vsi))
			goto err_rings;
		break;
506
	default:
B
Brett Creeley 已提交
507
		dev_warn(dev, "Unknown VSI type %d\n", vsi->type);
508 509 510
		goto unlock_pf;
	}

511 512
	if (vsi->type == ICE_VSI_CTRL && vf_id == ICE_INVAL_VFID) {
		/* Use the last VSI slot as the index for PF control VSI */
513 514 515 516 517 518 519
		vsi->idx = pf->num_alloc_vsi - 1;
		pf->ctrl_vsi_idx = vsi->idx;
		pf->vsi[vsi->idx] = vsi;
	} else {
		/* fill slot and make note of the index */
		vsi->idx = pf->next_vsi;
		pf->vsi[pf->next_vsi] = vsi;
520

521 522 523 524
		/* prepare pf->next_vsi for next use */
		pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
						 pf->next_vsi);
	}
525 526 527

	if (vsi->type == ICE_VSI_CTRL && vf_id != ICE_INVAL_VFID)
		pf->vf[vf_id].ctrl_vsi_idx = vsi->idx;
528 529 530
	goto unlock_pf;

err_rings:
B
Brett Creeley 已提交
531
	devm_kfree(dev, vsi);
532 533 534 535 536 537
	vsi = NULL;
unlock_pf:
	mutex_unlock(&pf->sw_mutex);
	return vsi;
}

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
/**
 * ice_alloc_fd_res - Allocate FD resource for a VSI
 * @vsi: pointer to the ice_vsi
 *
 * This allocates the FD resources
 *
 * Returns 0 on success, -EPERM on no-op or -EIO on failure
 */
static int ice_alloc_fd_res(struct ice_vsi *vsi)
{
	struct ice_pf *pf = vsi->back;
	u32 g_val, b_val;

	/* Flow Director filters are only allocated/assigned to the PF VSI which
	 * passes the traffic. The CTRL VSI is only used to add/delete filters
	 * so we don't allocate resources to it
	 */

	/* FD filters from guaranteed pool per VSI */
	g_val = pf->hw.func_caps.fd_fltr_guar;
	if (!g_val)
		return -EPERM;

	/* FD filters from best effort pool */
	b_val = pf->hw.func_caps.fd_fltr_best_effort;
	if (!b_val)
		return -EPERM;

566
	if (!(vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_VF))
567 568 569 570 571 572 573 574 575 576
		return -EPERM;

	if (!test_bit(ICE_FLAG_FD_ENA, pf->flags))
		return -EPERM;

	vsi->num_gfltr = g_val / pf->num_alloc_vsi;

	/* each VSI gets same "best_effort" quota */
	vsi->num_bfltr = b_val;

577 578 579 580 581 582 583
	if (vsi->type == ICE_VSI_VF) {
		vsi->num_gfltr = 0;

		/* each VSI gets same "best_effort" quota */
		vsi->num_bfltr = b_val;
	}

584 585 586
	return 0;
}

587 588 589 590 591 592
/**
 * 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
 */
593
static int ice_vsi_get_qs(struct ice_vsi *vsi)
594
{
595 596 597 598
	struct ice_pf *pf = vsi->back;
	struct ice_qs_cfg tx_qs_cfg = {
		.qs_mutex = &pf->avail_q_mutex,
		.pf_map = pf->avail_txqs,
599
		.pf_map_size = pf->max_pf_txqs,
600 601 602 603
		.q_count = vsi->alloc_txq,
		.scatter_count = ICE_MAX_SCATTER_TXQS,
		.vsi_map = vsi->txq_map,
		.vsi_map_offset = 0,
604
		.mapping_mode = ICE_VSI_MAP_CONTIG
605 606 607 608
	};
	struct ice_qs_cfg rx_qs_cfg = {
		.qs_mutex = &pf->avail_q_mutex,
		.pf_map = pf->avail_rxqs,
609
		.pf_map_size = pf->max_pf_rxqs,
610 611 612 613
		.q_count = vsi->alloc_rxq,
		.scatter_count = ICE_MAX_SCATTER_RXQS,
		.vsi_map = vsi->rxq_map,
		.vsi_map_offset = 0,
614
		.mapping_mode = ICE_VSI_MAP_CONTIG
615
	};
616
	int ret;
617

618
	ret = __ice_vsi_get_qs(&tx_qs_cfg);
619 620 621
	if (ret)
		return ret;
	vsi->tx_mapping_mode = tx_qs_cfg.mapping_mode;
622

623 624 625 626 627 628
	ret = __ice_vsi_get_qs(&rx_qs_cfg);
	if (ret)
		return ret;
	vsi->rx_mapping_mode = rx_qs_cfg.mapping_mode;

	return 0;
629 630
}

631 632 633 634
/**
 * ice_vsi_put_qs - Release queues from VSI to PF
 * @vsi: the VSI that is going to release queues
 */
635
static void ice_vsi_put_qs(struct ice_vsi *vsi)
636 637 638 639 640 641
{
	struct ice_pf *pf = vsi->back;
	int i;

	mutex_lock(&pf->avail_q_mutex);

642
	ice_for_each_alloc_txq(vsi, i) {
643 644 645 646
		clear_bit(vsi->txq_map[i], pf->avail_txqs);
		vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
	}

647
	ice_for_each_alloc_rxq(vsi, i) {
648 649 650 651 652 653 654
		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 已提交
655 656 657 658 659 660 661 662 663 664 665
/**
 * 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);
}

D
Dave Ertman 已提交
666 667 668 669 670 671 672 673 674 675 676
/**
 * ice_is_aux_ena
 * @pf: pointer to the PF struct
 *
 * returns true if AUX devices/drivers are supported, false otherwise
 */
bool ice_is_aux_ena(struct ice_pf *pf)
{
	return test_bit(ICE_FLAG_AUX_ENA, pf->flags);
}

677
/**
T
Tony Nguyen 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
 * 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)
694 695
		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 已提交
696 697 698 699
}

/**
 * ice_rss_clean - Delete RSS related VSI structures and configuration
700 701 702 703
 * @vsi: the VSI being removed
 */
static void ice_rss_clean(struct ice_vsi *vsi)
{
B
Brett Creeley 已提交
704 705
	struct ice_pf *pf = vsi->back;
	struct device *dev;
706

B
Brett Creeley 已提交
707
	dev = ice_pf_to_dev(pf);
708 709

	if (vsi->rss_hkey_user)
B
Brett Creeley 已提交
710
		devm_kfree(dev, vsi->rss_hkey_user);
711
	if (vsi->rss_lut_user)
B
Brett Creeley 已提交
712
		devm_kfree(dev, vsi->rss_lut_user);
T
Tony Nguyen 已提交
713 714 715 716 717

	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);
718 719
}

720 721 722 723
/**
 * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
 * @vsi: the VSI being configured
 */
724
static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
725 726 727 728 729 730 731 732 733 734 735 736 737
{
	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 已提交
738 739
		vsi->rss_table_size = (u16)cap->rss_table_size;
		vsi->rss_size = min_t(u16, num_online_cpus(),
740 741 742
				      BIT(cap->rss_table_entry_width));
		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
		break;
743 744 745 746 747 748
	case ICE_VSI_SWITCHDEV_CTRL:
		vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
		vsi->rss_size = min_t(u16, num_online_cpus(),
				      BIT(cap->rss_table_entry_width));
		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
		break;
749
	case ICE_VSI_VF:
M
Mitch Williams 已提交
750 751
		/* VF VSI will get a small RSS table.
		 * For VSI_LUT, LUT size should be set to 64 bytes.
752 753
		 */
		vsi->rss_table_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
M
Mitch Williams 已提交
754
		vsi->rss_size = ICE_MAX_RSS_QS_PER_VF;
755 756
		vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_VSI;
		break;
757 758
	case ICE_VSI_LB:
		break;
759
	default:
760 761
		dev_dbg(ice_pf_to_dev(pf), "Unsupported VSI type %s\n",
			ice_vsi_type_str(vsi->type));
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
		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)
{
813 814
	u16 offset = 0, qmap = 0, tx_count = 0, pow = 0;
	u16 num_txq_per_tc, num_rxq_per_tc;
815 816 817
	u16 qcount_tx = vsi->alloc_txq;
	u16 qcount_rx = vsi->alloc_rxq;
	bool ena_tc0 = false;
818
	u8 netdev_tc = 0;
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
	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;
	}

834 835 836 837 838 839 840 841 842
	num_rxq_per_tc = min_t(u16, qcount_rx / vsi->tc_cfg.numtc, ICE_MAX_RXQS_PER_TC);
	if (!num_rxq_per_tc)
		num_rxq_per_tc = 1;
	num_txq_per_tc = qcount_tx / vsi->tc_cfg.numtc;
	if (!num_txq_per_tc)
		num_txq_per_tc = 1;

	/* find the (rounded up) power-of-2 of qcount */
	pow = (u16)order_base_2(num_rxq_per_tc);
843 844 845 846 847 848 849 850 851 852 853 854

	/* 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
	 */
855
	ice_for_each_traffic_class(i) {
856 857 858
		if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
			/* TC is not enabled */
			vsi->tc_cfg.tc_info[i].qoffset = 0;
859 860 861
			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;
862 863 864 865 866 867
			ctxt->info.tc_mapping[i] = 0;
			continue;
		}

		/* TC is enabled */
		vsi->tc_cfg.tc_info[i].qoffset = offset;
868 869
		vsi->tc_cfg.tc_info[i].qcount_rx = num_rxq_per_tc;
		vsi->tc_cfg.tc_info[i].qcount_tx = num_txq_per_tc;
870
		vsi->tc_cfg.tc_info[i].netdev_tc = netdev_tc++;
871 872 873 874 875

		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);
876 877
		offset += num_rxq_per_tc;
		tx_count += num_txq_per_tc;
878 879
		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
	}
K
Kiran Patil 已提交
880 881 882 883 884 885 886 887 888 889

	/* 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
890
		vsi->num_rxq = num_rxq_per_tc;
K
Kiran Patil 已提交
891

892
	vsi->num_txq = tx_count;
893

894
	if (vsi->type == ICE_VSI_VF && vsi->num_txq != vsi->num_rxq) {
A
Anirudh Venkataramanan 已提交
895
		dev_dbg(ice_pf_to_dev(vsi->back), "VF VSI should have same number of Tx and Rx queues. Hence making them equal\n");
896 897 898 899 900 901
		/* 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;
	}

902 903 904 905 906 907 908 909 910 911
	/* 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);
}

912 913 914 915 916 917 918 919 920 921
/**
 * ice_set_fd_vsi_ctx - Set FD VSI context before adding a VSI
 * @ctxt: the VSI context being set
 * @vsi: the VSI being configured
 */
static void ice_set_fd_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
{
	u8 dflt_q_group, dflt_q_prio;
	u16 dflt_q, report_q, val;

922 923
	if (vsi->type != ICE_VSI_PF && vsi->type != ICE_VSI_CTRL &&
	    vsi->type != ICE_VSI_VF)
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
		return;

	val = ICE_AQ_VSI_PROP_FLOW_DIR_VALID;
	ctxt->info.valid_sections |= cpu_to_le16(val);
	dflt_q = 0;
	dflt_q_group = 0;
	report_q = 0;
	dflt_q_prio = 0;

	/* enable flow director filtering/programming */
	val = ICE_AQ_VSI_FD_ENABLE | ICE_AQ_VSI_FD_PROG_ENABLE;
	ctxt->info.fd_options = cpu_to_le16(val);
	/* max of allocated flow director filters */
	ctxt->info.max_fd_fltr_dedicated =
			cpu_to_le16(vsi->num_gfltr);
	/* max of shared flow director filters any VSI may program */
	ctxt->info.max_fd_fltr_shared =
			cpu_to_le16(vsi->num_bfltr);
	/* default queue index within the VSI of the default FD */
	val = ((dflt_q << ICE_AQ_VSI_FD_DEF_Q_S) &
	       ICE_AQ_VSI_FD_DEF_Q_M);
	/* target queue or queue group to the FD filter */
	val |= ((dflt_q_group << ICE_AQ_VSI_FD_DEF_GRP_S) &
		ICE_AQ_VSI_FD_DEF_GRP_M);
	ctxt->info.fd_def_q = cpu_to_le16(val);
	/* queue index on which FD filter completion is reported */
	val = ((report_q << ICE_AQ_VSI_FD_REPORT_Q_S) &
	       ICE_AQ_VSI_FD_REPORT_Q_M);
	/* priority of the default qindex action */
	val |= ((dflt_q_prio << ICE_AQ_VSI_FD_DEF_PRIORITY_S) &
		ICE_AQ_VSI_FD_DEF_PRIORITY_M);
	ctxt->info.fd_report_opt = cpu_to_le16(val);
}

958 959 960 961 962 963 964 965
/**
 * 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 已提交
966
	struct device *dev;
967 968 969
	struct ice_pf *pf;

	pf = vsi->back;
B
Brett Creeley 已提交
970
	dev = ice_pf_to_dev(pf);
971 972 973 974 975 976 977

	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;
978 979 980 981 982
	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;
983
	default:
B
Brett Creeley 已提交
984
		dev_dbg(dev, "Unsupported VSI type %s\n",
985
			ice_vsi_type_str(vsi->type));
986
		return;
987 988 989 990 991 992 993 994 995 996 997
	}

	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
998
 * @init_vsi: is this call creating a VSI
999 1000 1001 1002
 *
 * 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.
 */
1003
static int ice_vsi_init(struct ice_vsi *vsi, bool init_vsi)
1004 1005 1006
{
	struct ice_pf *pf = vsi->back;
	struct ice_hw *hw = &pf->hw;
1007
	struct ice_vsi_ctx *ctxt;
1008
	struct device *dev;
1009 1010
	int ret = 0;

1011
	dev = ice_pf_to_dev(pf);
1012
	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1013 1014 1015
	if (!ctxt)
		return -ENOMEM;

1016
	switch (vsi->type) {
1017
	case ICE_VSI_CTRL:
1018
	case ICE_VSI_LB:
1019
	case ICE_VSI_PF:
1020
		ctxt->flags = ICE_AQ_VSI_TYPE_PF;
1021
		break;
1022 1023 1024
	case ICE_VSI_SWITCHDEV_CTRL:
		ctxt->flags = ICE_AQ_VSI_TYPE_VMDQ2;
		break;
1025
	case ICE_VSI_VF:
1026
		ctxt->flags = ICE_AQ_VSI_TYPE_VF;
1027
		/* VF number here is the absolute VF number (0-255) */
1028
		ctxt->vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
1029
		break;
1030
	default:
1031 1032
		ret = -ENODEV;
		goto out;
1033 1034
	}

1035
	ice_set_dflt_vsi_ctx(ctxt);
1036 1037
	if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
		ice_set_fd_vsi_ctx(ctxt, vsi);
1038 1039
	/* if the switch is in VEB mode, allow VSI loopback */
	if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
1040
		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
1041 1042

	/* Set LUT type and HASH type if RSS is enabled */
1043 1044
	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags) &&
	    vsi->type != ICE_VSI_CTRL) {
1045
		ice_set_rss_vsi_ctx(ctxt, vsi);
1046 1047 1048 1049 1050 1051 1052
		/* 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);
	}
1053

1054 1055
	ctxt->info.sw_id = vsi->port_info->sw_id;
	ice_vsi_setup_q_map(vsi, ctxt);
1056 1057 1058 1059 1060 1061
	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);
1062

B
Brett Creeley 已提交
1063 1064 1065 1066
	/* enable/disable MAC and VLAN anti-spoof when spoofchk is on/off
	 * respectively
	 */
	if (vsi->type == ICE_VSI_VF) {
1067 1068
		ctxt->info.valid_sections |=
			cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
B
Brett Creeley 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
		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));
		}
1080 1081
	}

1082 1083 1084 1085 1086 1087 1088
	/* 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);
	}

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	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;
		}
1103 1104 1105
	}

	/* keep context for update VSI operations */
1106
	vsi->info = ctxt->info;
1107 1108

	/* record VSI number returned */
1109
	vsi->vsi_num = ctxt->vsi_num;
1110

1111 1112
out:
	kfree(ctxt);
1113 1114 1115
	return ret;
}

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
/**
 * 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 已提交
1151
	u16 start = 0, end = 0;
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

	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);
}

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
/**
 * 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
 */
1229
static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
1230 1231
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
1232
	struct device *dev;
B
Brett Creeley 已提交
1233
	u16 num_q_vectors;
K
Karol Kolacinski 已提交
1234
	int base;
1235

B
Brett Creeley 已提交
1236
	dev = ice_pf_to_dev(pf);
B
Brett Creeley 已提交
1237 1238 1239 1240 1241
	/* 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 已提交
1242
		dev_dbg(dev, "VSI %d has non-zero base vector %d\n",
B
Brett Creeley 已提交
1243
			vsi->vsi_num, vsi->base_vector);
1244 1245 1246
		return -EEXIST;
	}

B
Brett Creeley 已提交
1247 1248
	num_q_vectors = vsi->num_q_vectors;
	/* reserve slots from OS requested IRQs */
1249 1250 1251 1252
	if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
		int i;

		ice_for_each_vf(pf, i) {
1253 1254
			struct ice_vf *vf = &pf->vf[i];

1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
			if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI) {
				base = pf->vsi[vf->ctrl_vsi_idx]->base_vector;
				break;
			}
		}
		if (i == pf->num_alloc_vfs)
			base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
					   ICE_RES_VF_CTRL_VEC_ID);
	} else {
		base = ice_get_res(pf, pf->irq_tracker, num_q_vectors,
				   vsi->idx);
	}
K
Karol Kolacinski 已提交
1267 1268

	if (base < 0) {
1269 1270 1271
		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);
1272 1273
		return -ENOENT;
	}
K
Karol Kolacinski 已提交
1274
	vsi->base_vector = (u16)base;
B
Brett Creeley 已提交
1275
	pf->num_avail_sw_msix -= num_q_vectors;
1276

1277 1278 1279
	return 0;
}

1280 1281 1282 1283
/**
 * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
 * @vsi: the VSI having rings deallocated
 */
1284
static void ice_vsi_clear_rings(struct ice_vsi *vsi)
1285 1286 1287
{
	int i;

1288 1289 1290 1291 1292 1293
	/* Avoid stale references by clearing map from vector to ring */
	if (vsi->q_vectors) {
		ice_for_each_q_vector(vsi, i) {
			struct ice_q_vector *q_vector = vsi->q_vectors[i];

			if (q_vector) {
1294 1295
				q_vector->tx.tx_ring = NULL;
				q_vector->rx.rx_ring = NULL;
1296 1297 1298 1299
			}
		}
	}

1300
	if (vsi->tx_rings) {
1301
		ice_for_each_alloc_txq(vsi, i) {
1302 1303
			if (vsi->tx_rings[i]) {
				kfree_rcu(vsi->tx_rings[i], rcu);
1304
				WRITE_ONCE(vsi->tx_rings[i], NULL);
1305 1306 1307 1308
			}
		}
	}
	if (vsi->rx_rings) {
1309
		ice_for_each_alloc_rxq(vsi, i) {
1310 1311
			if (vsi->rx_rings[i]) {
				kfree_rcu(vsi->rx_rings[i], rcu);
1312
				WRITE_ONCE(vsi->rx_rings[i], NULL);
1313 1314 1315 1316 1317 1318 1319 1320 1321
			}
		}
	}
}

/**
 * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
 * @vsi: VSI which is having rings allocated
 */
1322
static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
1323 1324
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
1325
	struct device *dev;
K
Karol Kolacinski 已提交
1326
	u16 i;
1327

B
Brett Creeley 已提交
1328
	dev = ice_pf_to_dev(pf);
1329
	/* Allocate Tx rings */
1330
	ice_for_each_alloc_txq(vsi, i) {
1331
		struct ice_tx_ring *ring;
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341

		/* 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->vsi = vsi;
1342
		ring->tx_tstamps = &pf->ptp.port.tx;
B
Brett Creeley 已提交
1343
		ring->dev = dev;
1344
		ring->count = vsi->num_tx_desc;
1345
		WRITE_ONCE(vsi->tx_rings[i], ring);
1346 1347
	}

1348
	/* Allocate Rx rings */
1349
	ice_for_each_alloc_rxq(vsi, i) {
1350
		struct ice_rx_ring *ring;
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

		/* 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->vsi = vsi;
		ring->netdev = vsi->netdev;
B
Brett Creeley 已提交
1361
		ring->dev = dev;
1362
		ring->count = vsi->num_rx_desc;
1363
		WRITE_ONCE(vsi->rx_rings[i], ring);
1364 1365 1366 1367 1368 1369 1370 1371 1372
	}

	return 0;

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

1373 1374 1375 1376 1377 1378 1379 1380 1381
/**
 * 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.
 */
1382
void ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
1383 1384 1385
{
	u8 *lut;

1386
	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1387
	if (!lut)
1388
		return;
1389 1390 1391 1392 1393 1394 1395 1396 1397

	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);
	}

1398
	ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
1399
	kfree(lut);
1400 1401
}

1402 1403 1404 1405 1406 1407 1408
/**
 * 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_pf *pf = vsi->back;
B
Brett Creeley 已提交
1409
	struct device *dev;
1410 1411
	u8 *lut, *key;
	int err;
1412

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

1416
	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1417 1418 1419 1420 1421 1422 1423 1424
	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);

1425 1426 1427
	err = ice_set_rss_lut(vsi, lut, vsi->rss_table_size);
	if (err) {
		dev_err(dev, "set_rss_lut failed, error %d\n", err);
1428 1429 1430
		goto ice_vsi_cfg_rss_exit;
	}

1431
	key = kzalloc(ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE, GFP_KERNEL);
1432 1433 1434 1435 1436 1437
	if (!key) {
		err = -ENOMEM;
		goto ice_vsi_cfg_rss_exit;
	}

	if (vsi->rss_hkey_user)
1438
		memcpy(key, vsi->rss_hkey_user, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1439
	else
1440
		netdev_rss_key_fill((void *)key, ICE_GET_SET_RSS_KEY_EXTEND_KEY_SIZE);
1441

1442 1443 1444
	err = ice_set_rss_key(vsi, key);
	if (err)
		dev_err(dev, "set_rss_key failed, error %d\n", err);
1445

1446
	kfree(key);
1447
ice_vsi_cfg_rss_exit:
1448
	kfree(lut);
1449 1450 1451
	return err;
}

1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
/**
 * 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)
1475 1476
		dev_dbg(dev, "ice_add_avf_rss_cfg failed for vsi = %d, error = %s\n",
			vsi->vsi_num, ice_stat_str(status));
1477 1478
}

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
/**
 * 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)
1508 1509
		dev_dbg(dev, "ice_add_rss_cfg failed for ipv4 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1510 1511 1512 1513 1514

	/* 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)
1515 1516
		dev_dbg(dev, "ice_add_rss_cfg failed for ipv6 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1517 1518 1519 1520 1521

	/* 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)
1522 1523
		dev_dbg(dev, "ice_add_rss_cfg failed for tcp4 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1524 1525 1526 1527 1528

	/* 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)
1529 1530
		dev_dbg(dev, "ice_add_rss_cfg failed for udp4 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1531 1532 1533 1534 1535

	/* 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)
1536 1537
		dev_dbg(dev, "ice_add_rss_cfg failed for sctp4 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1538 1539 1540 1541 1542

	/* 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)
1543 1544
		dev_dbg(dev, "ice_add_rss_cfg failed for tcp6 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1545 1546 1547 1548 1549

	/* 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)
1550 1551
		dev_dbg(dev, "ice_add_rss_cfg failed for udp6 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1552 1553 1554 1555 1556

	/* 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)
1557 1558
		dev_dbg(dev, "ice_add_rss_cfg failed for sctp6 flow, vsi = %d, error = %s\n",
			vsi_num, ice_stat_str(status));
1559 1560
}

1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
/**
 * ice_pf_state_is_nominal - checks the PF for nominal state
 * @pf: pointer to PF to check
 *
 * Check the PF's state for a collection of bits that would indicate
 * the PF is in a state that would inhibit normal operation for
 * driver functionality.
 *
 * Returns true if PF is in a nominal state, false otherwise
 */
bool ice_pf_state_is_nominal(struct ice_pf *pf)
{
1573
	DECLARE_BITMAP(check_bits, ICE_STATE_NBITS) = { 0 };
1574 1575 1576 1577

	if (!pf)
		return false;

1578 1579
	bitmap_set(check_bits, 0, ICE_STATE_NOMINAL_CHECK_BITS);
	if (bitmap_intersects(pf->state, check_bits, ICE_STATE_NBITS))
1580 1581 1582 1583 1584
		return false;

	return true;
}

1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
/**
 * 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;

1598 1599
	ice_stat_update40(hw, GLV_GORCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->rx_bytes, &cur_es->rx_bytes);
1600

1601 1602
	ice_stat_update40(hw, GLV_UPRCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->rx_unicast, &cur_es->rx_unicast);
1603

1604 1605
	ice_stat_update40(hw, GLV_MPRCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->rx_multicast, &cur_es->rx_multicast);
1606

1607 1608
	ice_stat_update40(hw, GLV_BPRCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->rx_broadcast, &cur_es->rx_broadcast);
1609 1610 1611 1612

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

1613 1614
	ice_stat_update40(hw, GLV_GOTCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->tx_bytes, &cur_es->tx_bytes);
1615

1616 1617
	ice_stat_update40(hw, GLV_UPTCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->tx_unicast, &cur_es->tx_unicast);
1618

1619 1620
	ice_stat_update40(hw, GLV_MPTCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->tx_multicast, &cur_es->tx_multicast);
1621

1622 1623
	ice_stat_update40(hw, GLV_BPTCL(vsi_num), vsi->stat_offsets_loaded,
			  &prev_es->tx_broadcast, &cur_es->tx_broadcast);
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633

	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
1634
 * @vid: VLAN ID to be added
1635
 * @action: filter action to be performed on match
1636
 */
1637 1638
int
ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid, enum ice_sw_fwd_act_type action)
1639 1640
{
	struct ice_pf *pf = vsi->back;
B
Brett Creeley 已提交
1641
	struct device *dev;
1642 1643
	int err = 0;

B
Brett Creeley 已提交
1644
	dev = ice_pf_to_dev(pf);
1645

1646
	if (!ice_fltr_add_vlan(vsi, vid, action)) {
1647 1648
		vsi->num_vlan++;
	} else {
1649
		err = -ENODEV;
B
Brett Creeley 已提交
1650 1651
		dev_err(dev, "Failure Adding VLAN %d on VSI %i\n", vid,
			vsi->vsi_num);
1652 1653 1654 1655 1656 1657 1658 1659
	}

	return err;
}

/**
 * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
 * @vsi: the VSI being configured
1660
 * @vid: VLAN ID to be removed
1661 1662 1663 1664 1665 1666
 *
 * 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;
1667
	enum ice_status status;
B
Brett Creeley 已提交
1668
	struct device *dev;
1669
	int err = 0;
1670

B
Brett Creeley 已提交
1671
	dev = ice_pf_to_dev(pf);
1672

1673
	status = ice_fltr_remove_vlan(vsi, vid, ICE_FWD_TO_VSI);
1674 1675 1676
	if (!status) {
		vsi->num_vlan--;
	} else if (status == ICE_ERR_DOES_NOT_EXIST) {
1677 1678
		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));
1679
	} else {
1680 1681
		dev_err(dev, "Error removing VLAN %d on vsi %i error: %s\n",
			vid, vsi->vsi_num, ice_stat_str(status));
1682
		err = -EIO;
1683 1684
	}

1685
	return err;
1686 1687
}

M
Maciej Fijalkowski 已提交
1688 1689 1690 1691 1692 1693
/**
 * 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 已提交
1694 1695 1696 1697
	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)
1698 1699
	} else if (!ICE_2K_TOO_SMALL_WITH_PADDING &&
		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
M
Maciej Fijalkowski 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
		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 已提交
1711 1712
}

1713 1714 1715 1716 1717 1718
/**
 * ice_write_qrxflxp_cntxt - write/configure QRXFLXP_CNTXT register
 * @hw: HW pointer
 * @pf_q: index of the Rx queue in the PF's queue space
 * @rxdid: flexible descriptor RXDID
 * @prio: priority for the RXDID for this queue
1719
 * @ena_ts: true to enable timestamp and false to disable timestamp
1720 1721
 */
void
1722 1723
ice_write_qrxflxp_cntxt(struct ice_hw *hw, u16 pf_q, u32 rxdid, u32 prio,
			bool ena_ts)
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
{
	int regval = rd32(hw, QRXFLXP_CNTXT(pf_q));

	/* clear any previous values */
	regval &= ~(QRXFLXP_CNTXT_RXDID_IDX_M |
		    QRXFLXP_CNTXT_RXDID_PRIO_M |
		    QRXFLXP_CNTXT_TS_M);

	regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
		QRXFLXP_CNTXT_RXDID_IDX_M;

	regval |= (prio << QRXFLXP_CNTXT_RXDID_PRIO_S) &
		QRXFLXP_CNTXT_RXDID_PRIO_M;

1738 1739 1740 1741
	if (ena_ts)
		/* Enable TimeSync on this queue */
		regval |= QRXFLXP_CNTXT_TS_M;

1742 1743 1744
	wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
}

1745 1746 1747 1748 1749 1750 1751 1752
int ice_vsi_cfg_single_rxq(struct ice_vsi *vsi, u16 q_idx)
{
	if (q_idx >= vsi->num_rxq)
		return -EINVAL;

	return ice_vsi_cfg_rxq(vsi->rx_rings[q_idx]);
}

1753
int ice_vsi_cfg_single_txq(struct ice_vsi *vsi, struct ice_tx_ring **tx_rings, u16 q_idx)
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
{
	struct ice_aqc_add_tx_qgrp *qg_buf;
	int err;

	if (q_idx >= vsi->alloc_txq || !tx_rings || !tx_rings[q_idx])
		return -EINVAL;

	qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
	if (!qg_buf)
		return -ENOMEM;

	qg_buf->num_txqs = 1;

	err = ice_vsi_cfg_txq(vsi, tx_rings[q_idx], qg_buf);
	kfree(qg_buf);
	return err;
}

1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
/**
 * 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;

1783 1784 1785
	if (vsi->type == ICE_VSI_VF)
		goto setup_rings;

M
Maciej Fijalkowski 已提交
1786
	ice_vsi_cfg_frame_size(vsi);
1787
setup_rings:
1788
	/* set up individual rings */
1789 1790
	ice_for_each_rxq(vsi, i) {
		int err = ice_vsi_cfg_rxq(vsi->rx_rings[i]);
1791

1792
		if (err)
1793
			return err;
1794
	}
1795 1796

	return 0;
1797 1798 1799 1800 1801
}

/**
 * ice_vsi_cfg_txqs - Configure the VSI for Tx
 * @vsi: the VSI being configured
1802
 * @rings: Tx ring array to be configured
1803
 * @count: number of Tx ring array elements
1804 1805 1806 1807
 *
 * Return 0 on success and a negative value on error
 * Configure the Tx VSI for operation.
 */
1808
static int
1809
ice_vsi_cfg_txqs(struct ice_vsi *vsi, struct ice_tx_ring **rings, u16 count)
1810 1811
{
	struct ice_aqc_add_tx_qgrp *qg_buf;
1812
	u16 q_idx = 0;
1813
	int err = 0;
1814

1815
	qg_buf = kzalloc(struct_size(qg_buf, txqs, 1), GFP_KERNEL);
1816 1817 1818 1819 1820
	if (!qg_buf)
		return -ENOMEM;

	qg_buf->num_txqs = 1;

1821
	for (q_idx = 0; q_idx < count; q_idx++) {
1822 1823 1824
		err = ice_vsi_cfg_txq(vsi, rings[q_idx], qg_buf);
		if (err)
			goto err_cfg_txqs;
1825
	}
1826

1827
err_cfg_txqs:
1828
	kfree(qg_buf);
1829 1830 1831
	return err;
}

1832 1833 1834 1835 1836 1837 1838 1839 1840
/**
 * 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)
{
1841
	return ice_vsi_cfg_txqs(vsi, vsi->tx_rings, vsi->num_txq);
1842 1843
}

M
Maciej Fijalkowski 已提交
1844 1845 1846 1847 1848 1849 1850 1851 1852
/**
 * 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)
{
1853 1854 1855
	int ret;
	int i;

1856
	ret = ice_vsi_cfg_txqs(vsi, vsi->xdp_rings, vsi->num_xdp_txq);
1857 1858 1859
	if (ret)
		return ret;

1860
	ice_for_each_xdp_txq(vsi, i)
1861
		vsi->xdp_rings[i]->xsk_pool = ice_tx_xsk_pool(vsi->xdp_rings[i]);
1862 1863

	return ret;
M
Maciej Fijalkowski 已提交
1864 1865
}

1866 1867 1868 1869 1870 1871 1872 1873
/**
 * 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.
 */
1874
static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
1875 1876 1877 1878 1879 1880 1881 1882
{
	u32 val = intrl / gran;

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

1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895
/**
 * ice_write_intrl - write throttle rate limit to interrupt specific register
 * @q_vector: pointer to interrupt specific structure
 * @intrl: throttle rate limit in microseconds to write
 */
void ice_write_intrl(struct ice_q_vector *q_vector, u8 intrl)
{
	struct ice_hw *hw = &q_vector->vsi->back->hw;

	wr32(hw, GLINT_RATE(q_vector->reg_idx),
	     ice_intrl_usec_to_reg(intrl, ICE_INTRL_GRAN_ABOVE_25));
}

1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
static struct ice_q_vector *ice_pull_qvec_from_rc(struct ice_ring_container *rc)
{
	switch (rc->type) {
	case ICE_RX_CONTAINER:
		if (rc->rx_ring)
			return rc->rx_ring->q_vector;
		break;
	case ICE_TX_CONTAINER:
		if (rc->tx_ring)
			return rc->tx_ring->q_vector;
	default:
		break;
	}

	return NULL;
}

1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
/**
 * __ice_write_itr - write throttle rate to register
 * @q_vector: pointer to interrupt data structure
 * @rc: pointer to ring container
 * @itr: throttle rate in microseconds to write
 */
static void __ice_write_itr(struct ice_q_vector *q_vector,
			    struct ice_ring_container *rc, u16 itr)
{
	struct ice_hw *hw = &q_vector->vsi->back->hw;

	wr32(hw, GLINT_ITR(rc->itr_idx, q_vector->reg_idx),
	     ITR_REG_ALIGN(itr) >> ICE_ITR_GRAN_S);
}

/**
 * ice_write_itr - write throttle rate to queue specific register
 * @rc: pointer to ring container
 * @itr: throttle rate in microseconds to write
 */
void ice_write_itr(struct ice_ring_container *rc, u16 itr)
{
	struct ice_q_vector *q_vector;

1937 1938
	q_vector = ice_pull_qvec_from_rc(rc);
	if (!q_vector)
1939 1940 1941 1942 1943
		return;

	__ice_write_itr(q_vector, rc, itr);
}

1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
/**
 * ice_set_q_vector_intrl - set up interrupt rate limiting
 * @q_vector: the vector to be configured
 *
 * Interrupt rate limiting is local to the vector, not per-queue so we must
 * detect if either ring container has dynamic moderation enabled to decide
 * what to set the interrupt rate limit to via INTRL settings. In the case that
 * dynamic moderation is disabled on both, write the value with the cached
 * setting to make sure INTRL register matches the user visible value.
 */
void ice_set_q_vector_intrl(struct ice_q_vector *q_vector)
{
	if (ITR_IS_DYNAMIC(&q_vector->tx) || ITR_IS_DYNAMIC(&q_vector->rx)) {
		/* in the case of dynamic enabled, cap each vector to no more
		 * than (4 us) 250,000 ints/sec, which allows low latency
		 * but still less than 500,000 interrupts per second, which
		 * reduces CPU a bit in the case of the lowest latency
		 * setting. The 4 here is a value in microseconds.
		 */
		ice_write_intrl(q_vector, 4);
	} else {
		ice_write_intrl(q_vector, q_vector->intrl);
	}
}

1969 1970 1971
/**
 * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
 * @vsi: the VSI being configured
1972 1973 1974
 *
 * This configures MSIX mode interrupts for the PF VSI, and should not be used
 * for the VF VSI.
1975 1976 1977 1978 1979
 */
void ice_vsi_cfg_msix(struct ice_vsi *vsi)
{
	struct ice_pf *pf = vsi->back;
	struct ice_hw *hw = &pf->hw;
K
Karol Kolacinski 已提交
1980
	u16 txq = 0, rxq = 0;
1981
	int i, q;
1982

1983
	ice_for_each_q_vector(vsi, i) {
1984
		struct ice_q_vector *q_vector = vsi->q_vectors[i];
1985
		u16 reg_idx = q_vector->reg_idx;
1986

1987
		ice_cfg_itr(hw, q_vector);
1988

1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
		/* 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++) {
2001 2002
			ice_cfg_txq_interrupt(vsi, txq, reg_idx,
					      q_vector->tx.itr_idx);
2003 2004 2005 2006
			txq++;
		}

		for (q = 0; q < q_vector->num_ring_rx; q++) {
2007 2008
			ice_cfg_rxq_interrupt(vsi, rxq, reg_idx,
					      q_vector->rx.itr_idx);
2009 2010 2011 2012 2013
			rxq++;
		}
	}
}

2014 2015 2016 2017 2018 2019 2020
/**
 * 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;
2021
	struct ice_vsi_ctx *ctxt;
2022
	enum ice_status status;
2023 2024
	int ret = 0;

2025
	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2026 2027
	if (!ctxt)
		return -ENOMEM;
2028 2029 2030 2031 2032

	/* 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.
	 */
2033
	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
2034

2035 2036 2037 2038
	/* Preserve existing VLAN strip setting */
	ctxt->info.vlan_flags |= (vsi->info.vlan_flags &
				  ICE_AQ_VSI_VLAN_EMOD_M);

2039
	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
2040

2041
	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
2042
	if (status) {
2043 2044 2045
		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));
2046 2047
		ret = -EIO;
		goto out;
2048 2049
	}

2050 2051
	vsi->info.vlan_flags = ctxt->info.vlan_flags;
out:
2052
	kfree(ctxt);
2053
	return ret;
2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
}

/**
 * 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;
2064
	struct ice_vsi_ctx *ctxt;
2065
	enum ice_status status;
2066 2067
	int ret = 0;

2068 2069 2070 2071 2072 2073
	/* do not allow modifying VLAN stripping when a port VLAN is configured
	 * on this VSI
	 */
	if (vsi->info.pvid)
		return 0;

2074
	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2075 2076
	if (!ctxt)
		return -ENOMEM;
2077 2078 2079 2080 2081

	/* 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.
	 */
2082
	if (ena)
2083
		/* Strip VLAN tag from Rx packet and put it in the desc */
2084 2085
		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
	else
2086
		/* Disable stripping. Leave tag in packet */
2087
		ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
2088 2089

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

2092
	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
2093

2094
	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
2095
	if (status) {
2096 2097 2098
		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));
2099 2100
		ret = -EIO;
		goto out;
2101 2102
	}

2103 2104
	vsi->info.vlan_flags = ctxt->info.vlan_flags;
out:
2105
	kfree(ctxt);
2106
	return ret;
2107
}
2108 2109

/**
2110 2111
 * ice_vsi_start_all_rx_rings - start/enable all of a VSI's Rx rings
 * @vsi: the VSI whose rings are to be enabled
2112 2113 2114
 *
 * Returns 0 on success and a negative value on error
 */
2115
int ice_vsi_start_all_rx_rings(struct ice_vsi *vsi)
2116
{
2117
	return ice_vsi_ctrl_all_rx_rings(vsi, true);
2118 2119 2120
}

/**
2121 2122
 * ice_vsi_stop_all_rx_rings - stop/disable all of a VSI's Rx rings
 * @vsi: the VSI whose rings are to be disabled
2123 2124 2125
 *
 * Returns 0 on success and a negative value on error
 */
2126
int ice_vsi_stop_all_rx_rings(struct ice_vsi *vsi)
2127
{
2128
	return ice_vsi_ctrl_all_rx_rings(vsi, false);
2129 2130
}

2131 2132 2133 2134 2135 2136
/**
 * 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
2137
 * @count: number of Tx ring array elements
2138 2139 2140
 */
static int
ice_vsi_stop_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
2141
		      u16 rel_vmvf_num, struct ice_tx_ring **rings, u16 count)
2142
{
2143
	u16 q_idx;
2144 2145 2146

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

2148
	for (q_idx = 0; q_idx < count; q_idx++) {
2149 2150
		struct ice_txq_meta txq_meta = { };
		int status;
2151

2152 2153
		if (!rings || !rings[q_idx])
			return -EINVAL;
2154

2155 2156 2157
		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);
2158

2159 2160
		if (status)
			return status;
2161 2162
	}

2163
	return 0;
2164
}
2165

2166 2167 2168 2169
/**
 * ice_vsi_stop_lan_tx_rings - Disable LAN Tx rings
 * @vsi: the VSI being configured
 * @rst_src: reset source
2170
 * @rel_vmvf_num: Relative ID of VF/VM
2171
 */
2172 2173 2174
int
ice_vsi_stop_lan_tx_rings(struct ice_vsi *vsi, enum ice_disq_rst_src rst_src,
			  u16 rel_vmvf_num)
2175
{
2176
	return ice_vsi_stop_tx_rings(vsi, rst_src, rel_vmvf_num, vsi->tx_rings, vsi->num_txq);
2177 2178
}

M
Maciej Fijalkowski 已提交
2179 2180 2181 2182 2183 2184
/**
 * 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)
{
2185
	return ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, 0, vsi->xdp_rings, vsi->num_xdp_txq);
M
Maciej Fijalkowski 已提交
2186 2187
}

2188 2189 2190 2191
/**
 * 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.
 *
2192
 * returns true if Rx VLAN pruning is enabled and false otherwise.
2193 2194 2195 2196 2197 2198
 */
bool ice_vsi_is_vlan_pruning_ena(struct ice_vsi *vsi)
{
	if (!vsi)
		return false;

2199
	return (vsi->info.sw_flags2 & ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA);
2200 2201
}

2202 2203 2204 2205
/**
 * 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
2206
 * @vlan_promisc: enable valid security flags if not in VLAN promiscuous mode
2207 2208 2209
 *
 * returns 0 if VSI is updated, negative otherwise
 */
2210
int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena, bool vlan_promisc)
2211 2212
{
	struct ice_vsi_ctx *ctxt;
2213
	struct ice_pf *pf;
2214 2215 2216 2217 2218
	int status;

	if (!vsi)
		return -EINVAL;

2219 2220 2221 2222 2223 2224 2225
	/* Don't enable VLAN pruning if the netdev is currently in promiscuous
	 * mode. VLAN pruning will be enabled when the interface exits
	 * promiscuous mode if any VLAN filters are active.
	 */
	if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
		return 0;

2226
	pf = vsi->back;
2227
	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
2228 2229 2230 2231 2232
	if (!ctxt)
		return -ENOMEM;

	ctxt->info = vsi->info;

B
Brett Creeley 已提交
2233
	if (ena)
2234
		ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
B
Brett Creeley 已提交
2235
	else
2236 2237
		ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;

2238 2239
	if (!vlan_promisc)
		ctxt->info.valid_sections =
B
Brett Creeley 已提交
2240
			cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
2241

2242
	status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
2243
	if (status) {
2244 2245 2246 2247
		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));
2248 2249 2250 2251 2252
		goto err_out;
	}

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

2253
	kfree(ctxt);
2254 2255 2256
	return 0;

err_out:
2257
	kfree(ctxt);
2258 2259 2260
	return -EIO;
}

2261 2262
static void ice_vsi_set_tc_cfg(struct ice_vsi *vsi)
{
2263
	struct ice_dcbx_cfg *cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
2264 2265 2266 2267 2268

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

2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
/**
 * 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) {
2285
			dev_err(ice_pf_to_dev(vsi->back), "Failed to set reg_idx on q_vector %d VSI %d\n",
2286 2287 2288 2289
				i, vsi->vsi_num);
			goto clear_reg_idx;
		}

B
Brett Creeley 已提交
2290 2291 2292 2293 2294 2295 2296 2297
		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;
		}
2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
	}

	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;
}

2313 2314 2315 2316 2317 2318 2319 2320
/**
 * 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)
{
2321 2322
	enum ice_status (*eth_fltr)(struct ice_vsi *v, u16 type, u16 flag,
				    enum ice_sw_fwd_act_type act);
2323 2324
	struct ice_pf *pf = vsi->back;
	enum ice_status status;
B
Brett Creeley 已提交
2325
	struct device *dev;
2326

B
Brett Creeley 已提交
2327
	dev = ice_pf_to_dev(pf);
2328
	eth_fltr = create ? ice_fltr_add_eth : ice_fltr_remove_eth;
2329

2330
	if (tx) {
2331 2332
		status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_TX,
				  ICE_DROP_PACKET);
2333 2334 2335 2336 2337 2338 2339 2340 2341
	} else {
		if (ice_fw_supports_lldp_fltr_ctrl(&pf->hw)) {
			status = ice_lldp_fltr_add_remove(&pf->hw, vsi->vsi_num,
							  create);
		} else {
			status = eth_fltr(vsi, ETH_P_LLDP, ICE_FLTR_RX,
					  ICE_FWD_TO_VSI);
		}
	}
2342 2343

	if (status)
2344
		dev_dbg(dev, "Fail %s %s LLDP rule on VSI %i error: %s\n",
2345
			create ? "adding" : "removing", tx ? "TX" : "RX",
2346
			vsi->vsi_num, ice_stat_str(status));
2347 2348
}

2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
/**
 * ice_set_agg_vsi - sets up scheduler aggregator node and move VSI into it
 * @vsi: pointer to the VSI
 *
 * This function will allocate new scheduler aggregator now if needed and will
 * move specified VSI into it.
 */
static void ice_set_agg_vsi(struct ice_vsi *vsi)
{
	struct device *dev = ice_pf_to_dev(vsi->back);
	struct ice_agg_node *agg_node_iter = NULL;
	u32 agg_id = ICE_INVALID_AGG_NODE_ID;
	struct ice_agg_node *agg_node = NULL;
	int node_offset, max_agg_nodes = 0;
	struct ice_port_info *port_info;
	struct ice_pf *pf = vsi->back;
	u32 agg_node_id_start = 0;
	enum ice_status status;

	/* create (as needed) scheduler aggregator node and move VSI into
	 * corresponding aggregator node
	 * - PF aggregator node to contains VSIs of type _PF and _CTRL
	 * - VF aggregator nodes will contain VF VSI
	 */
	port_info = pf->hw.port_info;
	if (!port_info)
		return;

	switch (vsi->type) {
	case ICE_VSI_CTRL:
	case ICE_VSI_LB:
	case ICE_VSI_PF:
2381
	case ICE_VSI_SWITCHDEV_CTRL:
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
		max_agg_nodes = ICE_MAX_PF_AGG_NODES;
		agg_node_id_start = ICE_PF_AGG_NODE_ID_START;
		agg_node_iter = &pf->pf_agg_node[0];
		break;
	case ICE_VSI_VF:
		/* user can create 'n' VFs on a given PF, but since max children
		 * per aggregator node can be only 64. Following code handles
		 * aggregator(s) for VF VSIs, either selects a agg_node which
		 * was already created provided num_vsis < 64, otherwise
		 * select next available node, which will be created
		 */
		max_agg_nodes = ICE_MAX_VF_AGG_NODES;
		agg_node_id_start = ICE_VF_AGG_NODE_ID_START;
		agg_node_iter = &pf->vf_agg_node[0];
		break;
	default:
		/* other VSI type, handle later if needed */
		dev_dbg(dev, "unexpected VSI type %s\n",
			ice_vsi_type_str(vsi->type));
		return;
	}

	/* find the appropriate aggregator node */
	for (node_offset = 0; node_offset < max_agg_nodes; node_offset++) {
		/* see if we can find space in previously created
		 * node if num_vsis < 64, otherwise skip
		 */
		if (agg_node_iter->num_vsis &&
		    agg_node_iter->num_vsis == ICE_MAX_VSIS_IN_AGG_NODE) {
			agg_node_iter++;
			continue;
		}

		if (agg_node_iter->valid &&
		    agg_node_iter->agg_id != ICE_INVALID_AGG_NODE_ID) {
			agg_id = agg_node_iter->agg_id;
			agg_node = agg_node_iter;
			break;
		}

		/* find unclaimed agg_id */
		if (agg_node_iter->agg_id == ICE_INVALID_AGG_NODE_ID) {
			agg_id = node_offset + agg_node_id_start;
			agg_node = agg_node_iter;
			break;
		}
		/* move to next agg_node */
		agg_node_iter++;
	}

	if (!agg_node)
		return;

	/* if selected aggregator node was not created, create it */
	if (!agg_node->valid) {
		status = ice_cfg_agg(port_info, agg_id, ICE_AGG_TYPE_AGG,
				     (u8)vsi->tc_cfg.ena_tc);
		if (status) {
			dev_err(dev, "unable to create aggregator node with agg_id %u\n",
				agg_id);
			return;
		}
		/* aggregator node is created, store the neeeded info */
		agg_node->valid = true;
		agg_node->agg_id = agg_id;
	}

	/* move VSI to corresponding aggregator node */
	status = ice_move_vsi_to_agg(port_info, agg_id, vsi->idx,
				     (u8)vsi->tc_cfg.ena_tc);
	if (status) {
		dev_err(dev, "unable to move VSI idx %u into aggregator %u node",
			vsi->idx, agg_id);
		return;
	}

	/* keep active children count for aggregator node */
	agg_node->num_vsis++;

	/* cache the 'agg_id' in VSI, so that after reset - VSI will be moved
	 * to aggregator node
	 */
	vsi->agg_node = agg_node;
	dev_dbg(dev, "successfully moved VSI idx %u tc_bitmap 0x%x) into aggregator node %d which has num_vsis %u\n",
		vsi->idx, vsi->tc_cfg.ena_tc, vsi->agg_node->agg_id,
		vsi->agg_node->num_vsis);
}

2470 2471 2472 2473
/**
 * ice_vsi_setup - Set up a VSI by a given type
 * @pf: board private structure
 * @pi: pointer to the port_info instance
2474
 * @vsi_type: VSI type
2475
 * @vf_id: defines VF ID to which this VSI connects. This field is meant to be
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
 *         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,
2486
	      enum ice_vsi_type vsi_type, u16 vf_id)
2487 2488
{
	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
B
Brett Creeley 已提交
2489
	struct device *dev = ice_pf_to_dev(pf);
2490
	enum ice_status status;
2491 2492 2493
	struct ice_vsi *vsi;
	int ret, i;

2494
	if (vsi_type == ICE_VSI_VF || vsi_type == ICE_VSI_CTRL)
2495
		vsi = ice_vsi_alloc(pf, vsi_type, vf_id);
2496
	else
2497
		vsi = ice_vsi_alloc(pf, vsi_type, ICE_INVAL_VFID);
2498

2499 2500 2501 2502 2503 2504 2505
	if (!vsi) {
		dev_err(dev, "could not allocate VSI\n");
		return NULL;
	}

	vsi->port_info = pi;
	vsi->vsw = pf->first_sw;
2506 2507 2508
	if (vsi->type == ICE_VSI_PF)
		vsi->ethtype = ETH_P_PAUSE;

2509
	if (vsi->type == ICE_VSI_VF || vsi->type == ICE_VSI_CTRL)
2510
		vsi->vf_id = vf_id;
2511

2512 2513
	ice_alloc_fd_res(vsi);

2514 2515 2516
	if (ice_vsi_get_qs(vsi)) {
		dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
			vsi->idx);
2517
		goto unroll_vsi_alloc;
2518 2519 2520 2521 2522
	}

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

2523
	/* set TC configuration */
2524 2525
	ice_vsi_set_tc_cfg(vsi);

2526
	/* create the VSI */
2527
	ret = ice_vsi_init(vsi, true);
2528 2529 2530 2531
	if (ret)
		goto unroll_get_qs;

	switch (vsi->type) {
2532
	case ICE_VSI_CTRL:
2533
	case ICE_VSI_SWITCHDEV_CTRL:
2534 2535 2536 2537 2538 2539 2540 2541 2542
	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;

2543 2544 2545 2546
		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
		if (ret)
			goto unroll_vector_base;

2547 2548 2549 2550
		ret = ice_vsi_alloc_rings(vsi);
		if (ret)
			goto unroll_vector_base;

2551 2552 2553 2554 2555 2556 2557
		/* 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).
		 */
2558
		ret = ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI);
2559 2560 2561
		if (ret)
			goto unroll_clear_rings;

2562 2563
		ice_vsi_map_rings_to_vectors(vsi);

2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
		if (vsi->type != ICE_VSI_CTRL)
			/* Do not exit if configuring RSS had an issue, at
			 * least receive traffic on first queue. Hence no
			 * need to capture return value
			 */
			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
				ice_vsi_cfg_rss_lut_key(vsi);
				ice_vsi_set_rss_flow_fld(vsi);
			}
B
Brett Creeley 已提交
2574
		ice_init_arfs(vsi);
2575
		break;
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589
	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;

2590 2591 2592 2593
		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
		if (ret)
			goto unroll_vector_base;

2594 2595 2596 2597
		/* Do not exit if configuring RSS had an issue, at least
		 * receive traffic on first queue. Hence no need to capture
		 * return value
		 */
2598
		if (test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
2599
			ice_vsi_cfg_rss_lut_key(vsi);
2600 2601
			ice_vsi_set_vf_rss_flow_fld(vsi);
		}
2602
		break;
2603 2604 2605 2606 2607
	case ICE_VSI_LB:
		ret = ice_vsi_alloc_rings(vsi);
		if (ret)
			goto unroll_vsi_init;
		break;
2608
	default:
2609
		/* clean up the resources and exit */
2610 2611 2612 2613 2614
		goto unroll_vsi_init;
	}

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

2617 2618 2619
	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
				 max_txqs);
	if (status) {
2620 2621
		dev_err(dev, "VSI %d failed lan queue config, error %s\n",
			vsi->vsi_num, ice_stat_str(status));
2622
		goto unroll_clear_rings;
2623 2624
	}

2625 2626 2627 2628 2629
	/* 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...
2630 2631 2632
	 * 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.
2633
	 */
2634
	if (!ice_is_safe_mode(pf))
T
Tony Nguyen 已提交
2635
		if (vsi->type == ICE_VSI_PF) {
2636 2637
			ice_fltr_add_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
					 ICE_DROP_PACKET);
T
Tony Nguyen 已提交
2638 2639
			ice_cfg_sw_lldp(vsi, true, true);
		}
2640

2641 2642
	if (!vsi->agg_node)
		ice_set_agg_vsi(vsi);
2643 2644
	return vsi;

2645 2646
unroll_clear_rings:
	ice_vsi_clear_rings(vsi);
2647
unroll_vector_base:
2648
	/* reclaim SW interrupts back to the common pool */
B
Brett Creeley 已提交
2649
	ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2650
	pf->num_avail_sw_msix += vsi->num_q_vectors;
2651 2652 2653 2654 2655 2656
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);
2657
unroll_vsi_alloc:
2658 2659
	if (vsi_type == ICE_VSI_VF)
		ice_enable_lag(pf->lag);
2660 2661 2662 2663 2664
	ice_vsi_clear(vsi);

	return NULL;
}

2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
/**
 * 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;

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

2680
		ice_write_intrl(q_vector, 0);
2681
		for (q = 0; q < q_vector->num_ring_tx; q++) {
2682
			ice_write_itr(&q_vector->tx, 0);
2683
			wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
M
Maciej Fijalkowski 已提交
2684 2685 2686 2687 2688
			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);
			}
2689 2690 2691 2692
			txq++;
		}

		for (q = 0; q < q_vector->num_ring_rx; q++) {
2693
			ice_write_itr(&q_vector->rx, 0);
2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
			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 已提交
2709
	int base = vsi->base_vector;
2710
	int i;
2711

2712 2713
	if (!vsi->q_vectors || !vsi->irqs_ready)
		return;
2714

2715 2716 2717
	ice_vsi_release_msix(vsi);
	if (vsi->type == ICE_VSI_VF)
		return;
2718

2719 2720 2721 2722
	vsi->irqs_ready = false;
	ice_for_each_q_vector(vsi, i) {
		u16 vector = i + base;
		int irq_num;
2723

2724
		irq_num = pf->msix_entries[vector].vector;
2725

2726 2727 2728 2729 2730
		/* 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;
2731

2732 2733
		/* clear the affinity notifier in the IRQ descriptor */
		irq_set_affinity_notifier(irq_num, NULL);
2734

2735 2736 2737
		/* clear the affinity_mask in the IRQ descriptor */
		irq_set_affinity_hint(irq_num, NULL);
		synchronize_irq(irq_num);
B
Brett Creeley 已提交
2738
		devm_free_irq(ice_pf_to_dev(pf), irq_num, vsi->q_vectors[i]);
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773
	}
}

/**
 * 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]);
}

2774 2775 2776 2777 2778 2779
/**
 * ice_vsi_close - Shut down a VSI
 * @vsi: the VSI being shut down
 */
void ice_vsi_close(struct ice_vsi *vsi)
{
2780
	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state))
2781 2782 2783 2784 2785 2786 2787
		ice_down(vsi);

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

2788 2789 2790 2791 2792 2793 2794 2795 2796
/**
 * 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;

2797
	if (!test_bit(ICE_VSI_NEEDS_RESTART, vsi->state))
2798 2799
		return 0;

2800
	clear_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2801 2802 2803 2804 2805 2806

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

2807
			err = ice_open_internal(vsi->netdev);
2808 2809 2810 2811

			if (!locked)
				rtnl_unlock();
		}
2812 2813
	} else if (vsi->type == ICE_VSI_CTRL) {
		err = ice_vsi_open_ctrl(vsi);
2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
	}

	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)
{
2826
	if (test_bit(ICE_VSI_DOWN, vsi->state))
2827 2828
		return;

2829
	set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
2830 2831 2832 2833 2834 2835

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

2836
			ice_vsi_close(vsi);
2837 2838 2839 2840 2841 2842

			if (!locked)
				rtnl_unlock();
		} else {
			ice_vsi_close(vsi);
		}
2843 2844
	} else if (vsi->type == ICE_VSI_CTRL ||
		   vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
2845
		ice_vsi_close(vsi);
2846 2847 2848
	}
}

2849 2850 2851 2852 2853 2854
/**
 * 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 已提交
2855
	int base = vsi->base_vector;
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
	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 已提交
2889 2890 2891
	ice_for_each_q_vector(vsi, i) {
		if (!vsi->q_vectors[i])
			continue;
2892
		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
T
Tony Nguyen 已提交
2893
	}
2894

2895
	ice_flush(hw);
2896

2897 2898 2899 2900
	/* don't call synchronize_irq() for VF's from the host */
	if (vsi->type == ICE_VSI_VF)
		return;

2901 2902
	ice_for_each_q_vector(vsi, i)
		synchronize_irq(pf->msix_entries[i + base].vector);
2903 2904
}

2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
/**
 * 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);
}

2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
/**
 * 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;
2933

2934 2935 2936 2937 2938
	/* 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
2939
	 */
2940 2941
	if (vsi->netdev && !ice_is_reset_in_progress(pf->state) &&
	    (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state))) {
2942
		unregister_netdev(vsi->netdev);
2943
		clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
2944
	}
2945

2946 2947
	if (vsi->type == ICE_VSI_PF)
		ice_devlink_destroy_pf_port(pf);
2948

2949 2950 2951 2952
	if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
		ice_rss_clean(vsi);

	/* Disable VSI and free resources */
2953 2954
	if (vsi->type != ICE_VSI_LB)
		ice_vsi_dis_irq(vsi);
2955 2956
	ice_vsi_close(vsi);

B
Brett Creeley 已提交
2957 2958 2959 2960 2961
	/* 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.
	 */
2962 2963 2964 2965
	if (vsi->type == ICE_VSI_CTRL && vsi->vf_id != ICE_INVAL_VFID) {
		int i;

		ice_for_each_vf(pf, i) {
2966 2967
			struct ice_vf *vf = &pf->vf[i];

2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
			if (i != vsi->vf_id && vf->ctrl_vsi_idx != ICE_NO_VSI)
				break;
		}
		if (i == pf->num_alloc_vfs) {
			/* No other VFs left that have control VSI, reclaim SW
			 * interrupts back to the common pool
			 */
			ice_free_res(pf->irq_tracker, vsi->base_vector,
				     ICE_RES_VF_CTRL_VEC_ID);
			pf->num_avail_sw_msix += vsi->num_q_vectors;
		}
	} else if (vsi->type != ICE_VSI_VF) {
2980
		/* reclaim SW interrupts back to the common pool */
B
Brett Creeley 已提交
2981
		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
2982 2983
		pf->num_avail_sw_msix += vsi->num_q_vectors;
	}
2984

T
Tony Nguyen 已提交
2985 2986
	if (!ice_is_safe_mode(pf)) {
		if (vsi->type == ICE_VSI_PF) {
2987 2988
			ice_fltr_remove_eth(vsi, ETH_P_PAUSE, ICE_FLTR_TX,
					    ICE_DROP_PACKET);
T
Tony Nguyen 已提交
2989 2990 2991 2992 2993 2994 2995
			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);
		}
2996
	}
2997

2998
	ice_fltr_remove_all(vsi);
2999
	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3000 3001
	ice_vsi_delete(vsi);
	ice_vsi_free_q_vectors(vsi);
3002

3003 3004 3005 3006 3007 3008 3009 3010 3011 3012
	if (vsi->netdev) {
		if (test_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state)) {
			unregister_netdev(vsi->netdev);
			clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
		}
		if (test_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state)) {
			free_netdev(vsi->netdev);
			vsi->netdev = NULL;
			clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
		}
3013 3014
	}

3015 3016 3017
	if (vsi->type == ICE_VSI_VF &&
	    vsi->agg_node && vsi->agg_node->valid)
		vsi->agg_node->num_vsis--;
3018 3019 3020 3021 3022 3023 3024 3025
	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.
	 */
3026
	if (!ice_is_reset_in_progress(pf->state))
3027 3028 3029 3030 3031
		ice_vsi_clear(vsi);

	return 0;
}

3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050
/**
 * 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;
3051 3052 3053 3054 3055

		if (i < vsi->num_txq)
			coalesce[i].tx_valid = true;
		if (i < vsi->num_rxq)
			coalesce[i].rx_valid = true;
3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074
	}

	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)
{
3075
	struct ice_ring_container *rc;
3076 3077 3078 3079 3080
	int i;

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

3081 3082 3083 3084 3085
	/* There are a couple of cases that have to be handled here:
	 *   1. The case where the number of queue vectors stays the same, but
	 *      the number of Tx or Rx rings changes (the first for loop)
	 *   2. The case where the number of queue vectors increased (the
	 *      second for loop)
3086
	 */
3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
	for (i = 0; i < size && i < vsi->num_q_vectors; i++) {
		/* There are 2 cases to handle here and they are the same for
		 * both Tx and Rx:
		 *   if the entry was valid previously (coalesce[i].[tr]x_valid
		 *   and the loop variable is less than the number of rings
		 *   allocated, then write the previous values
		 *
		 *   if the entry was not valid previously, but the number of
		 *   rings is less than are allocated (this means the number of
		 *   rings increased from previously), then write out the
		 *   values in the first element
3098 3099 3100 3101
		 *
		 *   Also, always write the ITR, even if in ITR_IS_DYNAMIC
		 *   as there is no harm because the dynamic algorithm
		 *   will just overwrite.
3102
		 */
3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124
		if (i < vsi->alloc_rxq && coalesce[i].rx_valid) {
			rc = &vsi->q_vectors[i]->rx;
			rc->itr_setting = coalesce[i].itr_rx;
			ice_write_itr(rc, rc->itr_setting);
		} else if (i < vsi->alloc_rxq) {
			rc = &vsi->q_vectors[i]->rx;
			rc->itr_setting = coalesce[0].itr_rx;
			ice_write_itr(rc, rc->itr_setting);
		}

		if (i < vsi->alloc_txq && coalesce[i].tx_valid) {
			rc = &vsi->q_vectors[i]->tx;
			rc->itr_setting = coalesce[i].itr_tx;
			ice_write_itr(rc, rc->itr_setting);
		} else if (i < vsi->alloc_txq) {
			rc = &vsi->q_vectors[i]->tx;
			rc->itr_setting = coalesce[0].itr_tx;
			ice_write_itr(rc, rc->itr_setting);
		}

		vsi->q_vectors[i]->intrl = coalesce[i].intrl;
		ice_write_intrl(vsi->q_vectors[i], coalesce[i].intrl);
3125 3126 3127 3128 3129 3130
	}

	/* the number of queue vectors increased so write whatever is in
	 * the first element
	 */
	for (; i < vsi->num_q_vectors; i++) {
3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142
		/* transmit */
		rc = &vsi->q_vectors[i]->tx;
		rc->itr_setting = coalesce[0].itr_tx;
		ice_write_itr(rc, rc->itr_setting);

		/* receive */
		rc = &vsi->q_vectors[i]->rx;
		rc->itr_setting = coalesce[0].itr_rx;
		ice_write_itr(rc, rc->itr_setting);

		vsi->q_vectors[i]->intrl = coalesce[0].intrl;
		ice_write_intrl(vsi->q_vectors[i], coalesce[0].intrl);
3143
	}
3144 3145
}

3146 3147 3148
/**
 * ice_vsi_rebuild - Rebuild VSI after reset
 * @vsi: VSI to be rebuild
3149
 * @init_vsi: is this an initialization or a reconfigure of the VSI
3150 3151 3152
 *
 * Returns 0 on success and negative value on failure
 */
3153
int ice_vsi_rebuild(struct ice_vsi *vsi, bool init_vsi)
3154 3155
{
	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
3156 3157
	struct ice_coalesce_stored *coalesce;
	int prev_num_q_vectors = 0;
3158
	struct ice_vf *vf = NULL;
3159
	enum ice_vsi_type vtype;
3160
	enum ice_status status;
3161
	struct ice_pf *pf;
3162 3163 3164 3165 3166
	int ret, i;

	if (!vsi)
		return -EINVAL;

3167
	pf = vsi->back;
3168 3169
	vtype = vsi->type;
	if (vtype == ICE_VSI_VF)
3170 3171
		vf = &pf->vf[vsi->vf_id];

3172 3173
	coalesce = kcalloc(vsi->num_q_vectors,
			   sizeof(struct ice_coalesce_stored), GFP_KERNEL);
3174 3175 3176 3177 3178
	if (!coalesce)
		return -ENOMEM;

	prev_num_q_vectors = ice_vsi_rebuild_get_coalesce(vsi, coalesce);

3179
	ice_rm_vsi_lan_cfg(vsi->port_info, vsi->idx);
3180
	ice_vsi_free_q_vectors(vsi);
3181

B
Brett Creeley 已提交
3182 3183 3184 3185 3186
	/* 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.
	 */
3187
	if (vtype != ICE_VSI_VF) {
3188
		/* reclaim SW interrupts back to the common pool */
B
Brett Creeley 已提交
3189
		ice_free_res(pf->irq_tracker, vsi->base_vector, vsi->idx);
3190
		pf->num_avail_sw_msix += vsi->num_q_vectors;
B
Brett Creeley 已提交
3191
		vsi->base_vector = 0;
3192 3193
	}

M
Maciej Fijalkowski 已提交
3194 3195 3196 3197 3198
	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);
3199
	ice_vsi_put_qs(vsi);
3200
	ice_vsi_clear_rings(vsi);
3201
	ice_vsi_free_arrays(vsi);
3202
	if (vtype == ICE_VSI_VF)
3203 3204 3205
		ice_vsi_set_num_qs(vsi, vf->vf_id);
	else
		ice_vsi_set_num_qs(vsi, ICE_INVAL_VFID);
3206 3207 3208 3209 3210 3211

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

	ice_vsi_get_qs(vsi);
3212 3213

	ice_alloc_fd_res(vsi);
3214
	ice_vsi_set_tc_cfg(vsi);
3215 3216

	/* Initialize VSI struct elements and create VSI in FW */
3217
	ret = ice_vsi_init(vsi, init_vsi);
3218 3219 3220
	if (ret < 0)
		goto err_vsi;

3221
	switch (vtype) {
3222
	case ICE_VSI_CTRL:
3223
	case ICE_VSI_SWITCHDEV_CTRL:
3224 3225 3226 3227 3228
	case ICE_VSI_PF:
		ret = ice_vsi_alloc_q_vectors(vsi);
		if (ret)
			goto err_rings;

3229 3230 3231 3232
		ret = ice_vsi_setup_vector_base(vsi);
		if (ret)
			goto err_vectors;

3233 3234 3235 3236
		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
		if (ret)
			goto err_vectors;

3237 3238 3239 3240 3241
		ret = ice_vsi_alloc_rings(vsi);
		if (ret)
			goto err_vectors;

		ice_vsi_map_rings_to_vectors(vsi);
M
Maciej Fijalkowski 已提交
3242
		if (ice_is_xdp_ena_vsi(vsi)) {
3243 3244 3245
			ret = ice_vsi_determine_xdp_res(vsi);
			if (ret)
				goto err_vectors;
M
Maciej Fijalkowski 已提交
3246 3247 3248 3249
			ret = ice_prepare_xdp_rings(vsi, vsi->xdp_prog);
			if (ret)
				goto err_vectors;
		}
3250
		/* ICE_VSI_CTRL does not need RSS so skip RSS processing */
3251
		if (vtype != ICE_VSI_CTRL)
3252 3253 3254 3255 3256 3257
			/* Do not exit if configuring RSS had an issue, at
			 * least receive traffic on first queue. Hence no
			 * need to capture return value
			 */
			if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
				ice_vsi_cfg_rss_lut_key(vsi);
3258
		break;
3259 3260 3261 3262 3263
	case ICE_VSI_VF:
		ret = ice_vsi_alloc_q_vectors(vsi);
		if (ret)
			goto err_rings;

3264 3265 3266 3267
		ret = ice_vsi_set_q_vectors_reg_idx(vsi);
		if (ret)
			goto err_vectors;

3268 3269 3270 3271 3272
		ret = ice_vsi_alloc_rings(vsi);
		if (ret)
			goto err_vectors;

		break;
3273 3274 3275 3276 3277
	default:
		break;
	}

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

M
Maciej Fijalkowski 已提交
3281 3282 3283 3284
		if (ice_is_xdp_ena_vsi(vsi))
			max_txqs[i] += vsi->num_xdp_txq;
	}

3285 3286 3287
	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
				 max_txqs);
	if (status) {
3288 3289
		dev_err(ice_pf_to_dev(pf), "VSI %d failed lan queue config, error %s\n",
			vsi->vsi_num, ice_stat_str(status));
3290 3291 3292 3293 3294 3295
		if (init_vsi) {
			ret = -EIO;
			goto err_vectors;
		} else {
			return ice_schedule_reset(pf, ICE_RESET_PFR);
		}
3296
	}
3297 3298 3299
	ice_vsi_rebuild_set_coalesce(vsi, coalesce, prev_num_q_vectors);
	kfree(coalesce);

3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312
	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);
3313
	set_bit(ICE_RESET_FAILED, pf->state);
3314
	kfree(coalesce);
3315 3316 3317
	return ret;
}

3318
/**
3319
 * ice_is_reset_in_progress - check for a reset in progress
3320
 * @state: PF state field
3321
 */
3322
bool ice_is_reset_in_progress(unsigned long *state)
3323
{
3324 3325 3326 3327
	return test_bit(ICE_RESET_OICR_RECV, state) ||
	       test_bit(ICE_PFR_REQ, state) ||
	       test_bit(ICE_CORER_REQ, state) ||
	       test_bit(ICE_GLOBR_REQ, state);
3328
}
3329

3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
/**
 * ice_wait_for_reset - Wait for driver to finish reset and rebuild
 * @pf: pointer to the PF structure
 * @timeout: length of time to wait, in jiffies
 *
 * Wait (sleep) for a short time until the driver finishes cleaning up from
 * a device reset. The caller must be able to sleep. Use this to delay
 * operations that could fail while the driver is cleaning up after a device
 * reset.
 *
 * Returns 0 on success, -EBUSY if the reset is not finished within the
 * timeout, and -ERESTARTSYS if the thread was interrupted.
 */
int ice_wait_for_reset(struct ice_pf *pf, unsigned long timeout)
{
	long ret;

	ret = wait_event_interruptible_timeout(pf->reset_wait_queue,
					       !ice_is_reset_in_progress(pf->state),
					       timeout);
	if (ret < 0)
		return ret;
	else if (!ret)
		return -EBUSY;
	else
		return 0;
}

3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383
#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 已提交
3384
	struct ice_vsi_ctx *ctx;
3385
	enum ice_status status;
B
Brett Creeley 已提交
3386
	struct device *dev;
3387 3388 3389
	int i, ret = 0;
	u8 num_tc = 0;

B
Brett Creeley 已提交
3390 3391
	dev = ice_pf_to_dev(pf);

3392 3393 3394 3395 3396
	ice_for_each_traffic_class(i) {
		/* build bitmap of enabled TCs */
		if (ena_tc & BIT(i))
			num_tc++;
		/* populate max_txqs per TC */
3397
		max_txqs[i] = vsi->alloc_txq;
3398 3399 3400 3401 3402
	}

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

3403
	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415
	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 已提交
3416
		dev_info(dev, "Failed VSI Update\n");
3417 3418 3419 3420 3421 3422 3423 3424
		ret = -EIO;
		goto out;
	}

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

	if (status) {
3425 3426
		dev_err(dev, "VSI %d failed TC config, error %s\n",
			vsi->vsi_num, ice_stat_str(status));
3427 3428 3429 3430 3431 3432 3433 3434
		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:
3435
	kfree(ctx);
3436 3437 3438
	return ret;
}
#endif /* CONFIG_DCB */
3439

3440 3441
/**
 * ice_update_ring_stats - Update ring statistics
3442
 * @stats: stats to be updated
3443 3444 3445 3446 3447
 * @pkts: number of processed packets
 * @bytes: number of processed bytes
 *
 * This function assumes that caller has acquired a u64_stats_sync lock.
 */
3448
static void ice_update_ring_stats(struct ice_q_stats *stats, u64 pkts, u64 bytes)
3449
{
3450 3451
	stats->bytes += bytes;
	stats->pkts += pkts;
3452 3453 3454 3455 3456 3457 3458 3459
}

/**
 * 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
 */
3460
void ice_update_tx_ring_stats(struct ice_tx_ring *tx_ring, u64 pkts, u64 bytes)
3461 3462
{
	u64_stats_update_begin(&tx_ring->syncp);
3463
	ice_update_ring_stats(&tx_ring->stats, pkts, bytes);
3464 3465 3466 3467 3468 3469 3470 3471 3472
	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
 */
3473
void ice_update_rx_ring_stats(struct ice_rx_ring *rx_ring, u64 pkts, u64 bytes)
3474 3475
{
	u64_stats_update_begin(&rx_ring->syncp);
3476
	ice_update_ring_stats(&rx_ring->stats, pkts, bytes);
3477 3478 3479
	u64_stats_update_end(&rx_ring->syncp);
}

3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491
/**
 * ice_status_to_errno - convert from enum ice_status to Linux errno
 * @err: ice_status value to convert
 */
int ice_status_to_errno(enum ice_status err)
{
	switch (err) {
	case ICE_SUCCESS:
		return 0;
	case ICE_ERR_DOES_NOT_EXIST:
		return -ENOENT;
	case ICE_ERR_OUT_OF_RANGE:
3492 3493 3494 3495 3496
	case ICE_ERR_AQ_ERROR:
	case ICE_ERR_AQ_TIMEOUT:
	case ICE_ERR_AQ_EMPTY:
	case ICE_ERR_AQ_FW_CRITICAL:
		return -EIO;
3497
	case ICE_ERR_PARAM:
3498
	case ICE_ERR_INVAL_SIZE:
3499 3500 3501 3502 3503
		return -EINVAL;
	case ICE_ERR_NO_MEMORY:
		return -ENOMEM;
	case ICE_ERR_MAX_LIMIT:
		return -EAGAIN;
3504 3505 3506 3507
	case ICE_ERR_RESET_ONGOING:
		return -EBUSY;
	case ICE_ERR_AQ_FULL:
		return -ENOSPC;
3508 3509 3510 3511 3512
	default:
		return -EINVAL;
	}
}

3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570
/**
 * 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)) {
3571
		dev_err(dev, "Default forwarding VSI %d already in use, disable it and try again\n",
3572 3573 3574 3575 3576 3577
			sw->dflt_vsi->vsi_num);
		return -EEXIST;
	}

	status = ice_cfg_dflt_vsi(&vsi->back->hw, vsi->idx, true, ICE_FLTR_RX);
	if (status) {
3578 3579
		dev_err(dev, "Failed to set VSI %d as the default forwarding VSI, error %s\n",
			vsi->vsi_num, ice_stat_str(status));
3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616
		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) {
3617 3618
		dev_err(dev, "Failed to clear the default forwarding VSI %d, error %s\n",
			dflt_vsi->vsi_num, ice_stat_str(status));
3619 3620 3621 3622 3623 3624 3625 3626
		return -EIO;
	}

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

	return 0;
}
3627

3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801
/**
 * ice_get_link_speed_mbps - get link speed in Mbps
 * @vsi: the VSI whose link speed is being queried
 *
 * Return current VSI link speed and 0 if the speed is unknown.
 */
int ice_get_link_speed_mbps(struct ice_vsi *vsi)
{
	switch (vsi->port_info->phy.link_info.link_speed) {
	case ICE_AQ_LINK_SPEED_100GB:
		return SPEED_100000;
	case ICE_AQ_LINK_SPEED_50GB:
		return SPEED_50000;
	case ICE_AQ_LINK_SPEED_40GB:
		return SPEED_40000;
	case ICE_AQ_LINK_SPEED_25GB:
		return SPEED_25000;
	case ICE_AQ_LINK_SPEED_20GB:
		return SPEED_20000;
	case ICE_AQ_LINK_SPEED_10GB:
		return SPEED_10000;
	case ICE_AQ_LINK_SPEED_5GB:
		return SPEED_5000;
	case ICE_AQ_LINK_SPEED_2500MB:
		return SPEED_2500;
	case ICE_AQ_LINK_SPEED_1000MB:
		return SPEED_1000;
	case ICE_AQ_LINK_SPEED_100MB:
		return SPEED_100;
	case ICE_AQ_LINK_SPEED_10MB:
		return SPEED_10;
	case ICE_AQ_LINK_SPEED_UNKNOWN:
	default:
		return 0;
	}
}

/**
 * ice_get_link_speed_kbps - get link speed in Kbps
 * @vsi: the VSI whose link speed is being queried
 *
 * Return current VSI link speed and 0 if the speed is unknown.
 */
static int ice_get_link_speed_kbps(struct ice_vsi *vsi)
{
	int speed_mbps;

	speed_mbps = ice_get_link_speed_mbps(vsi);

	return speed_mbps * 1000;
}

/**
 * ice_set_min_bw_limit - setup minimum BW limit for Tx based on min_tx_rate
 * @vsi: VSI to be configured
 * @min_tx_rate: min Tx rate in Kbps to be configured as BW limit
 *
 * If the min_tx_rate is specified as 0 that means to clear the minimum BW limit
 * profile, otherwise a non-zero value will force a minimum BW limit for the VSI
 * on TC 0.
 */
int ice_set_min_bw_limit(struct ice_vsi *vsi, u64 min_tx_rate)
{
	struct ice_pf *pf = vsi->back;
	enum ice_status status;
	struct device *dev;
	int speed;

	dev = ice_pf_to_dev(pf);
	if (!vsi->port_info) {
		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
			vsi->idx, vsi->type);
		return -EINVAL;
	}

	speed = ice_get_link_speed_kbps(vsi);
	if (min_tx_rate > (u64)speed) {
		dev_err(dev, "invalid min Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
			min_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
			speed);
		return -EINVAL;
	}

	/* Configure min BW for VSI limit */
	if (min_tx_rate) {
		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
						   ICE_MIN_BW, min_tx_rate);
		if (status) {
			dev_err(dev, "failed to set min Tx rate(%llu Kbps) for %s %d\n",
				min_tx_rate, ice_vsi_type_str(vsi->type),
				vsi->idx);
			return -EIO;
		}

		dev_dbg(dev, "set min Tx rate(%llu Kbps) for %s\n",
			min_tx_rate, ice_vsi_type_str(vsi->type));
	} else {
		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
							vsi->idx, 0,
							ICE_MIN_BW);
		if (status) {
			dev_err(dev, "failed to clear min Tx rate configuration for %s %d\n",
				ice_vsi_type_str(vsi->type), vsi->idx);
			return -EIO;
		}

		dev_dbg(dev, "cleared min Tx rate configuration for %s %d\n",
			ice_vsi_type_str(vsi->type), vsi->idx);
	}

	return 0;
}

/**
 * ice_set_max_bw_limit - setup maximum BW limit for Tx based on max_tx_rate
 * @vsi: VSI to be configured
 * @max_tx_rate: max Tx rate in Kbps to be configured as BW limit
 *
 * If the max_tx_rate is specified as 0 that means to clear the maximum BW limit
 * profile, otherwise a non-zero value will force a maximum BW limit for the VSI
 * on TC 0.
 */
int ice_set_max_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate)
{
	struct ice_pf *pf = vsi->back;
	enum ice_status status;
	struct device *dev;
	int speed;

	dev = ice_pf_to_dev(pf);
	if (!vsi->port_info) {
		dev_dbg(dev, "VSI %d, type %u specified doesn't have valid port_info\n",
			vsi->idx, vsi->type);
		return -EINVAL;
	}

	speed = ice_get_link_speed_kbps(vsi);
	if (max_tx_rate > (u64)speed) {
		dev_err(dev, "invalid max Tx rate %llu Kbps specified for %s %d is greater than current link speed %u Kbps\n",
			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx,
			speed);
		return -EINVAL;
	}

	/* Configure max BW for VSI limit */
	if (max_tx_rate) {
		status = ice_cfg_vsi_bw_lmt_per_tc(vsi->port_info, vsi->idx, 0,
						   ICE_MAX_BW, max_tx_rate);
		if (status) {
			dev_err(dev, "failed setting max Tx rate(%llu Kbps) for %s %d\n",
				max_tx_rate, ice_vsi_type_str(vsi->type),
				vsi->idx);
			return -EIO;
		}

		dev_dbg(dev, "set max Tx rate(%llu Kbps) for %s %d\n",
			max_tx_rate, ice_vsi_type_str(vsi->type), vsi->idx);
	} else {
		status = ice_cfg_vsi_bw_dflt_lmt_per_tc(vsi->port_info,
							vsi->idx, 0,
							ICE_MAX_BW);
		if (status) {
			dev_err(dev, "failed clearing max Tx rate configuration for %s %d\n",
				ice_vsi_type_str(vsi->type), vsi->idx);
			return -EIO;
		}

		dev_dbg(dev, "cleared max Tx rate configuration for %s %d\n",
			ice_vsi_type_str(vsi->type), vsi->idx);
	}

	return 0;
}

3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837
/**
 * ice_set_link - turn on/off physical link
 * @vsi: VSI to modify physical link on
 * @ena: turn on/off physical link
 */
int ice_set_link(struct ice_vsi *vsi, bool ena)
{
	struct device *dev = ice_pf_to_dev(vsi->back);
	struct ice_port_info *pi = vsi->port_info;
	struct ice_hw *hw = pi->hw;
	enum ice_status status;

	if (vsi->type != ICE_VSI_PF)
		return -EINVAL;

	status = ice_aq_set_link_restart_an(pi, ena, NULL);

	/* if link is owned by manageability, FW will return ICE_AQ_RC_EMODE.
	 * this is not a fatal error, so print a warning message and return
	 * a success code. Return an error if FW returns an error code other
	 * than ICE_AQ_RC_EMODE
	 */
	if (status == ICE_ERR_AQ_ERROR) {
		if (hw->adminq.sq_last_status == ICE_AQ_RC_EMODE)
			dev_warn(dev, "can't set link to %s, err %s aq_err %s. not fatal, continuing\n",
				 (ena ? "ON" : "OFF"), ice_stat_str(status),
				 ice_aq_str(hw->adminq.sq_last_status));
	} else if (status) {
		dev_err(dev, "can't set link to %s, err %s aq_err %s\n",
			(ena ? "ON" : "OFF"), ice_stat_str(status),
			ice_aq_str(hw->adminq.sq_last_status));
		return -EIO;
	}

	return 0;
}
3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866

/**
 * ice_is_feature_supported
 * @pf: pointer to the struct ice_pf instance
 * @f: feature enum to be checked
 *
 * returns true if feature is supported, false otherwise
 */
bool ice_is_feature_supported(struct ice_pf *pf, enum ice_feature f)
{
	if (f < 0 || f >= ICE_F_MAX)
		return false;

	return test_bit(f, pf->features);
}

/**
 * ice_set_feature_support
 * @pf: pointer to the struct ice_pf instance
 * @f: feature enum to set
 */
static void ice_set_feature_support(struct ice_pf *pf, enum ice_feature f)
{
	if (f < 0 || f >= ICE_F_MAX)
		return;

	set_bit(f, pf->features);
}

3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879
/**
 * ice_clear_feature_support
 * @pf: pointer to the struct ice_pf instance
 * @f: feature enum to clear
 */
void ice_clear_feature_support(struct ice_pf *pf, enum ice_feature f)
{
	if (f < 0 || f >= ICE_F_MAX)
		return;

	clear_bit(f, pf->features);
}

3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892
/**
 * ice_init_feature_support
 * @pf: pointer to the struct ice_pf instance
 *
 * called during init to setup supported feature
 */
void ice_init_feature_support(struct ice_pf *pf)
{
	switch (pf->hw.device_id) {
	case ICE_DEV_ID_E810C_BACKPLANE:
	case ICE_DEV_ID_E810C_QSFP:
	case ICE_DEV_ID_E810C_SFP:
		ice_set_feature_support(pf, ICE_F_DSCP);
3893 3894
		if (ice_is_e810t(&pf->hw))
			ice_set_feature_support(pf, ICE_F_SMA_CTRL);
3895 3896 3897 3898 3899
		break;
	default:
		break;
	}
}
3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960

/**
 * ice_vsi_update_security - update security block in VSI
 * @vsi: pointer to VSI structure
 * @fill: function pointer to fill ctx
 */
int
ice_vsi_update_security(struct ice_vsi *vsi, void (*fill)(struct ice_vsi_ctx *))
{
	struct ice_vsi_ctx ctx = { 0 };

	ctx.info = vsi->info;
	ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
	fill(&ctx);

	if (ice_update_vsi(&vsi->back->hw, vsi->idx, &ctx, NULL))
		return -ENODEV;

	vsi->info = ctx.info;
	return 0;
}

/**
 * ice_vsi_ctx_set_antispoof - set antispoof function in VSI ctx
 * @ctx: pointer to VSI ctx structure
 */
void ice_vsi_ctx_set_antispoof(struct ice_vsi_ctx *ctx)
{
	ctx->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);
}

/**
 * ice_vsi_ctx_clear_antispoof - clear antispoof function in VSI ctx
 * @ctx: pointer to VSI ctx structure
 */
void ice_vsi_ctx_clear_antispoof(struct ice_vsi_ctx *ctx)
{
	ctx->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);
}

/**
 * ice_vsi_ctx_set_allow_override - allow destination override on VSI
 * @ctx: pointer to VSI ctx structure
 */
void ice_vsi_ctx_set_allow_override(struct ice_vsi_ctx *ctx)
{
	ctx->info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
}

/**
 * ice_vsi_ctx_clear_allow_override - turn off destination override on VSI
 * @ctx: pointer to VSI ctx structure
 */
void ice_vsi_ctx_clear_allow_override(struct ice_vsi_ctx *ctx)
{
	ctx->info.sec_flags &= ~ICE_AQ_VSI_SEC_FLAG_ALLOW_DEST_OVRD;
}