i40e_virtchnl_pf.c 117.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/* Copyright(c) 2013 - 2018 Intel Corporation. */
3 4 5

#include "i40e.h"

6 7 8 9 10
/*********************notification routines***********************/

/**
 * i40e_vc_vf_broadcast
 * @pf: pointer to the PF structure
11 12
 * @v_opcode: operation code
 * @v_retval: return value
13 14 15 16 17 18
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * send a message to all VFs on a given PF
 **/
static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
19
				 enum virtchnl_ops v_opcode,
20 21 22 23 24 25 26 27
				 i40e_status v_retval, u8 *msg,
				 u16 msglen)
{
	struct i40e_hw *hw = &pf->hw;
	struct i40e_vf *vf = pf->vf;
	int i;

	for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
28
		int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
29
		/* Not all vfs are enabled so skip the ones that are not */
30 31
		if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
		    !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
32 33 34 35 36 37 38 39 40 41 42
			continue;

		/* Ignore return value on purpose - a given VF may fail, but
		 * we need to keep going and send to all of them
		 */
		i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
				       msg, msglen, NULL);
	}
}

/**
43
 * i40e_vc_notify_vf_link_state
44 45 46 47 48 49
 * @vf: pointer to the VF structure
 *
 * send a link status message to a single VF
 **/
static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
{
50
	struct virtchnl_pf_event pfe;
51 52 53
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	struct i40e_link_status *ls = &pf->hw.phy.link_info;
54
	int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
55

56
	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
57
	pfe.severity = PF_EVENT_SEVERITY_INFO;
58 59 60
	if (vf->link_forced) {
		pfe.event_data.link_event.link_status = vf->link_up;
		pfe.event_data.link_event.link_speed =
61
			(vf->link_up ? VIRTCHNL_LINK_SPEED_40GB : 0);
62 63 64
	} else {
		pfe.event_data.link_event.link_status =
			ls->link_info & I40E_AQ_LINK_UP;
65
		pfe.event_data.link_event.link_speed =
66
			i40e_virtchnl_link_speed(ls->link_speed);
67
	}
68
	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
			       0, (u8 *)&pfe, sizeof(pfe), NULL);
}

/**
 * i40e_vc_notify_link_state
 * @pf: pointer to the PF structure
 *
 * send a link status message to all VFs on a given PF
 **/
void i40e_vc_notify_link_state(struct i40e_pf *pf)
{
	int i;

	for (i = 0; i < pf->num_alloc_vfs; i++)
		i40e_vc_notify_vf_link_state(&pf->vf[i]);
}

/**
 * i40e_vc_notify_reset
 * @pf: pointer to the PF structure
 *
 * indicate a pending reset to all VFs on a given PF
 **/
void i40e_vc_notify_reset(struct i40e_pf *pf)
{
94
	struct virtchnl_pf_event pfe;
95

96
	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
97
	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
98 99
	i40e_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, 0,
			     (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
100 101 102 103 104 105 106 107 108 109
}

/**
 * i40e_vc_notify_vf_reset
 * @vf: pointer to the VF structure
 *
 * indicate a pending reset to the given VF
 **/
void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
{
110
	struct virtchnl_pf_event pfe;
111 112 113 114 115 116 117
	int abs_vf_id;

	/* validate the request */
	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
		return;

	/* verify if the VF is in either init or active before proceeding */
118 119
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
	    !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
120 121
		return;

122
	abs_vf_id = vf->vf_id + (int)vf->pf->hw.func_caps.vf_base_id;
123

124
	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
125
	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
126
	i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, VIRTCHNL_OP_EVENT,
127
			       0, (u8 *)&pfe,
128
			       sizeof(struct virtchnl_pf_event), NULL);
129
}
130 131
/***********************misc routines*****************************/

132 133
/**
 * i40e_vc_disable_vf
134
 * @vf: pointer to the VF info
135
 *
136
 * Disable the VF through a SW reset.
137
 **/
138
static inline void i40e_vc_disable_vf(struct i40e_vf *vf)
139
{
140 141
	int i;

142
	i40e_vc_notify_vf_reset(vf);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

	/* We want to ensure that an actual reset occurs initiated after this
	 * function was called. However, we do not want to wait forever, so
	 * we'll give a reasonable time and print a message if we failed to
	 * ensure a reset.
	 */
	for (i = 0; i < 20; i++) {
		if (i40e_reset_vf(vf, false))
			return;
		usleep_range(10000, 20000);
	}

	dev_warn(&vf->pf->pdev->dev,
		 "Failed to initiate reset for VF %d after 200 milliseconds\n",
		 vf->vf_id);
158 159
}

160 161
/**
 * i40e_vc_isvalid_vsi_id
162 163
 * @vf: pointer to the VF info
 * @vsi_id: VF relative VSI id
164
 *
165
 * check for the valid VSI id
166
 **/
167
static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
168 169
{
	struct i40e_pf *pf = vf->pf;
170
	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
171

172
	return (vsi && (vsi->vf_id == vf->vf_id));
173 174 175 176
}

/**
 * i40e_vc_isvalid_queue_id
177
 * @vf: pointer to the VF info
178 179 180 181 182
 * @vsi_id: vsi id
 * @qid: vsi relative queue id
 *
 * check for the valid queue id
 **/
183
static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
184 185 186
					    u8 qid)
{
	struct i40e_pf *pf = vf->pf;
187
	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
188

189
	return (vsi && (qid < vsi->alloc_queue_pairs));
190 191 192 193
}

/**
 * i40e_vc_isvalid_vector_id
194 195
 * @vf: pointer to the VF info
 * @vector_id: VF relative vector id
196 197 198 199 200 201 202
 *
 * check for the valid vector id
 **/
static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
{
	struct i40e_pf *pf = vf->pf;

203
	return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
204 205 206 207 208 209
}

/***********************vf resource mgmt routines*****************/

/**
 * i40e_vc_get_pf_queue_id
210
 * @vf: pointer to the VF info
211
 * @vsi_id: id of VSI as provided by the FW
212 213
 * @vsi_queue_id: vsi relative queue id
 *
214
 * return PF relative queue id
215
 **/
216
static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
217 218 219
				   u8 vsi_queue_id)
{
	struct i40e_pf *pf = vf->pf;
220
	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
221 222
	u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;

223 224 225
	if (!vsi)
		return pf_queue_id;

226 227 228 229 230 231 232 233 234 235 236
	if (le16_to_cpu(vsi->info.mapping_flags) &
	    I40E_AQ_VSI_QUE_MAP_NONCONTIG)
		pf_queue_id =
			le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
	else
		pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
			      vsi_queue_id;

	return pf_queue_id;
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/**
 * i40e_get_real_pf_qid
 * @vf: pointer to the VF info
 * @vsi_id: vsi id
 * @queue_id: queue number
 *
 * wrapper function to get pf_queue_id handling ADq code as well
 **/
static u16 i40e_get_real_pf_qid(struct i40e_vf *vf, u16 vsi_id, u16 queue_id)
{
	int i;

	if (vf->adq_enabled) {
		/* Although VF considers all the queues(can be 1 to 16) as its
		 * own but they may actually belong to different VSIs(up to 4).
		 * We need to find which queues belongs to which VSI.
		 */
		for (i = 0; i < vf->num_tc; i++) {
			if (queue_id < vf->ch[i].num_qps) {
				vsi_id = vf->ch[i].vsi_id;
				break;
			}
			/* find right queue id which is relative to a
			 * given VSI.
			 */
			queue_id -= vf->ch[i].num_qps;
			}
		}

	return i40e_vc_get_pf_queue_id(vf, vsi_id, queue_id);
}

269 270
/**
 * i40e_config_irq_link_list
271
 * @vf: pointer to the VF info
272
 * @vsi_id: id of VSI as given by the FW
273 274 275 276
 * @vecmap: irq map info
 *
 * configure irq link list from the map
 **/
277
static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
278
				      struct virtchnl_vector_map *vecmap)
279 280 281 282 283 284
{
	unsigned long linklistmap = 0, tempmap;
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	u16 vsi_queue_id, pf_queue_id;
	enum i40e_queue_type qtype;
285
	u16 next_q, vector_id, size;
286 287 288 289 290 291 292 293 294
	u32 reg, reg_idx;
	u16 itr_idx = 0;

	vector_id = vecmap->vector_id;
	/* setup the head */
	if (0 == vector_id)
		reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
	else
		reg_idx = I40E_VPINT_LNKLSTN(
295 296
		     ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
		     (vector_id - 1));
297 298 299 300 301 302 303

	if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
		/* Special case - No queues mapped on this vector */
		wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
		goto irq_list_done;
	}
	tempmap = vecmap->rxq_map;
304
	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
305 306
		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
				    vsi_queue_id));
307 308 309
	}

	tempmap = vecmap->txq_map;
310
	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
311 312
		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
				     vsi_queue_id + 1));
313 314
	}

315 316 317
	size = I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES;
	next_q = find_first_bit(&linklistmap, size);
	if (unlikely(next_q == size))
318 319
		goto irq_list_done;

M
Mitch Williams 已提交
320 321
	vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
	qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
322
	pf_queue_id = i40e_get_real_pf_qid(vf, vsi_id, vsi_queue_id);
323 324 325 326
	reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);

	wr32(hw, reg_idx, reg);

327
	while (next_q < size) {
328 329 330 331 332 333 334 335 336 337 338 339 340
		switch (qtype) {
		case I40E_QUEUE_TYPE_RX:
			reg_idx = I40E_QINT_RQCTL(pf_queue_id);
			itr_idx = vecmap->rxitr_idx;
			break;
		case I40E_QUEUE_TYPE_TX:
			reg_idx = I40E_QINT_TQCTL(pf_queue_id);
			itr_idx = vecmap->txitr_idx;
			break;
		default:
			break;
		}

341 342
		next_q = find_next_bit(&linklistmap, size, next_q + 1);
		if (next_q < size) {
343 344
			vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
			qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
345 346 347
			pf_queue_id = i40e_get_real_pf_qid(vf,
							   vsi_id,
							   vsi_queue_id);
348 349 350 351 352 353 354 355 356
		} else {
			pf_queue_id = I40E_QUEUE_END_OF_LIST;
			qtype = 0;
		}

		/* format for the RQCTL & TQCTL regs is same */
		reg = (vector_id) |
		    (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
		    (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
357
		    BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
358 359 360 361
		    (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
		wr32(hw, reg_idx, reg);
	}

362 363 364
	/* if the vf is running in polling mode and using interrupt zero,
	 * need to disable auto-mask on enabling zero interrupt for VFs.
	 */
365
	if ((vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
366 367 368 369 370 371 372 373
	    (vector_id == 0)) {
		reg = rd32(hw, I40E_GLINT_CTL);
		if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
			reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
			wr32(hw, I40E_GLINT_CTL, reg);
		}
	}

374 375 376 377
irq_list_done:
	i40e_flush(hw);
}

378 379 380 381 382 383 384 385
/**
 * i40e_release_iwarp_qvlist
 * @vf: pointer to the VF.
 *
 **/
static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
386
	struct virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
387 388 389 390 391 392 393 394
	u32 msix_vf;
	u32 i;

	if (!vf->qvlist_info)
		return;

	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
	for (i = 0; i < qvlist_info->num_vectors; i++) {
395
		struct virtchnl_iwarp_qv_info *qv_info;
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
		u32 next_q_index, next_q_type;
		struct i40e_hw *hw = &pf->hw;
		u32 v_idx, reg_idx, reg;

		qv_info = &qvlist_info->qv_info[i];
		if (!qv_info)
			continue;
		v_idx = qv_info->v_idx;
		if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
			/* Figure out the queue after CEQ and make that the
			 * first queue.
			 */
			reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
			reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
			next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
					>> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
			next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
					>> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;

			reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
			reg = (next_q_index &
			       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
			       (next_q_type <<
			       I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);

			wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
		}
	}
	kfree(vf->qvlist_info);
	vf->qvlist_info = NULL;
}

/**
 * i40e_config_iwarp_qvlist
 * @vf: pointer to the VF info
 * @qvlist_info: queue and vector list
 *
 * Return 0 on success or < 0 on error
 **/
static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
436
				    struct virtchnl_iwarp_qvlist_info *qvlist_info)
437 438 439
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
440
	struct virtchnl_iwarp_qv_info *qv_info;
441 442 443 444
	u32 v_idx, i, reg_idx, reg;
	u32 next_q_idx, next_q_type;
	u32 msix_vf, size;

445 446
	size = sizeof(struct virtchnl_iwarp_qvlist_info) +
	       (sizeof(struct virtchnl_iwarp_qv_info) *
447 448
						(qvlist_info->num_vectors - 1));
	vf->qvlist_info = kzalloc(size, GFP_KERNEL);
449 450 451
	if (!vf->qvlist_info)
		return -ENOMEM;

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	vf->qvlist_info->num_vectors = qvlist_info->num_vectors;

	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
	for (i = 0; i < qvlist_info->num_vectors; i++) {
		qv_info = &qvlist_info->qv_info[i];
		if (!qv_info)
			continue;
		v_idx = qv_info->v_idx;

		/* Validate vector id belongs to this vf */
		if (!i40e_vc_isvalid_vector_id(vf, v_idx))
			goto err;

		vf->qvlist_info->qv_info[i] = *qv_info;

		reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
		/* We might be sharing the interrupt, so get the first queue
		 * index and type, push it down the list by adding the new
		 * queue on top. Also link it with the new queue in CEQCTL.
		 */
		reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
		next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
				I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
		next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
				I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);

		if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
			reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
			reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
			(v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
			(qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
			(next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
			(next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
			wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);

			reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
			reg = (qv_info->ceq_idx &
			       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
			       (I40E_QUEUE_TYPE_PE_CEQ <<
			       I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
			wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
		}

		if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
			reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
			(v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
			(qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));

			wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
		}
	}

	return 0;
err:
	kfree(vf->qvlist_info);
	vf->qvlist_info = NULL;
	return -EINVAL;
}

511 512
/**
 * i40e_config_vsi_tx_queue
513
 * @vf: pointer to the VF info
514
 * @vsi_id: id of VSI as provided by the FW
515 516 517 518 519
 * @vsi_queue_id: vsi relative queue index
 * @info: config. info
 *
 * configure tx queue
 **/
520
static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
521
				    u16 vsi_queue_id,
522
				    struct virtchnl_txq_info *info)
523 524 525 526
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	struct i40e_hmc_obj_txq tx_ctx;
527
	struct i40e_vsi *vsi;
528 529 530 531
	u16 pf_queue_id;
	u32 qtx_ctl;
	int ret = 0;

C
Carolyn Wyborny 已提交
532 533 534 535
	if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
		ret = -ENOENT;
		goto error_context;
	}
536 537
	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
	vsi = i40e_find_vsi_from_id(pf, vsi_id);
C
Carolyn Wyborny 已提交
538 539 540 541
	if (!vsi) {
		ret = -ENOENT;
		goto error_context;
	}
542 543 544 545 546 547 548

	/* clear the context structure first */
	memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));

	/* only set the required fields */
	tx_ctx.base = info->dma_ring_addr / 128;
	tx_ctx.qlen = info->ring_len;
549
	tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
550
	tx_ctx.rdylist_act = 0;
551 552
	tx_ctx.head_wb_ena = info->headwb_enabled;
	tx_ctx.head_wb_addr = info->dma_headwb_addr;
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

	/* clear the context in the HMC */
	ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
	if (ret) {
		dev_err(&pf->pdev->dev,
			"Failed to clear VF LAN Tx queue context %d, error: %d\n",
			pf_queue_id, ret);
		ret = -ENOENT;
		goto error_context;
	}

	/* set the context in the HMC */
	ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
	if (ret) {
		dev_err(&pf->pdev->dev,
			"Failed to set VF LAN Tx queue context %d error: %d\n",
			pf_queue_id, ret);
		ret = -ENOENT;
		goto error_context;
	}

	/* associate this queue with the PCI VF function */
	qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
576
	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
577 578 579 580 581 582 583 584 585 586 587 588 589
		    & I40E_QTX_CTL_PF_INDX_MASK);
	qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
		     << I40E_QTX_CTL_VFVM_INDX_SHIFT)
		    & I40E_QTX_CTL_VFVM_INDX_MASK);
	wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
	i40e_flush(hw);

error_context:
	return ret;
}

/**
 * i40e_config_vsi_rx_queue
590
 * @vf: pointer to the VF info
591
 * @vsi_id: id of VSI  as provided by the FW
592 593 594 595 596
 * @vsi_queue_id: vsi relative queue index
 * @info: config. info
 *
 * configure rx queue
 **/
597
static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
598
				    u16 vsi_queue_id,
599
				    struct virtchnl_rxq_info *info)
600 601 602 603 604 605 606
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	struct i40e_hmc_obj_rxq rx_ctx;
	u16 pf_queue_id;
	int ret = 0;

607
	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

	/* clear the context structure first */
	memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));

	/* only set the required fields */
	rx_ctx.base = info->dma_ring_addr / 128;
	rx_ctx.qlen = info->ring_len;

	if (info->splithdr_enabled) {
		rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2      |
				  I40E_RX_SPLIT_IP      |
				  I40E_RX_SPLIT_TCP_UDP |
				  I40E_RX_SPLIT_SCTP;
		/* header length validation */
		if (info->hdr_size > ((2 * 1024) - 64)) {
			ret = -EINVAL;
			goto error_param;
		}
		rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;

628
		/* set split mode 10b */
M
Mitch Williams 已提交
629
		rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
	}

	/* databuffer length validation */
	if (info->databuffer_size > ((16 * 1024) - 128)) {
		ret = -EINVAL;
		goto error_param;
	}
	rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;

	/* max pkt. length validation */
	if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
		ret = -EINVAL;
		goto error_param;
	}
	rx_ctx.rxmax = info->max_pkt_size;

	/* enable 32bytes desc always */
	rx_ctx.dsize = 1;

	/* default values */
J
Jacob Keller 已提交
650
	rx_ctx.lrxqthresh = 1;
651
	rx_ctx.crcstrip = 1;
652
	rx_ctx.prefena = 1;
653
	rx_ctx.l2tsel = 1;
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

	/* clear the context in the HMC */
	ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
	if (ret) {
		dev_err(&pf->pdev->dev,
			"Failed to clear VF LAN Rx queue context %d, error: %d\n",
			pf_queue_id, ret);
		ret = -ENOENT;
		goto error_param;
	}

	/* set the context in the HMC */
	ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
	if (ret) {
		dev_err(&pf->pdev->dev,
			"Failed to set VF LAN Rx queue context %d error: %d\n",
			pf_queue_id, ret);
		ret = -ENOENT;
		goto error_param;
	}

error_param:
	return ret;
}

/**
 * i40e_alloc_vsi_res
681
 * @vf: pointer to the VF info
682
 * @idx: VSI index, applies only for ADq mode, zero otherwise
683
 *
684
 * alloc VF vsi context & resources
685
 **/
686
static int i40e_alloc_vsi_res(struct i40e_vf *vf, u8 idx)
687 688 689 690
{
	struct i40e_mac_filter *f = NULL;
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi;
691
	u64 max_tx_rate = 0;
692 693
	int ret = 0;

694 695
	vsi = i40e_vsi_setup(pf, I40E_VSI_SRIOV, pf->vsi[pf->lan_vsi]->seid,
			     vf->vf_id);
696 697 698

	if (!vsi) {
		dev_err(&pf->pdev->dev,
699
			"add vsi failed for VF %d, aq_err %d\n",
700 701 702 703
			vf->vf_id, pf->hw.aq.asq_last_status);
		ret = -ENOENT;
		goto error_alloc_vsi_res;
	}
704 705

	if (!idx) {
M
Mitch Williams 已提交
706
		u64 hena = i40e_pf_get_default_rss_hena(pf);
707
		u8 broadcast[ETH_ALEN];
M
Mitch Williams 已提交
708

709
		vf->lan_vsi_idx = vsi->idx;
710
		vf->lan_vsi_id = vsi->id;
G
Greg Rose 已提交
711 712 713 714 715 716 717 718
		/* If the port VLAN has been configured and then the
		 * VF driver was removed then the VSI port VLAN
		 * configuration was destroyed.  Check if there is
		 * a port VLAN and restore the VSI configuration if
		 * needed.
		 */
		if (vf->port_vlan_id)
			i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
K
Kiran Patil 已提交
719

720
		spin_lock_bh(&vsi->mac_filter_hash_lock);
M
Mitch Williams 已提交
721
		if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
722 723
			f = i40e_add_mac_filter(vsi,
						vf->default_lan_addr.addr);
M
Mitch Williams 已提交
724 725 726 727 728
			if (!f)
				dev_info(&pf->pdev->dev,
					 "Could not add MAC filter %pM for VF %d\n",
					vf->default_lan_addr.addr, vf->vf_id);
		}
729
		eth_broadcast_addr(broadcast);
730
		f = i40e_add_mac_filter(vsi, broadcast);
731 732 733
		if (!f)
			dev_info(&pf->pdev->dev,
				 "Could not allocate VF broadcast filter\n");
734
		spin_unlock_bh(&vsi->mac_filter_hash_lock);
735 736
		wr32(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)hena);
		wr32(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id), (u32)(hena >> 32));
737 738 739 740
		/* program mac filter only for VF VSI */
		ret = i40e_sync_vsi_filters(vsi);
		if (ret)
			dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
741
	}
742

743 744 745 746 747
	/* storing VSI index and id for ADq and don't apply the mac filter */
	if (vf->adq_enabled) {
		vf->ch[idx].vsi_idx = vsi->idx;
		vf->ch[idx].vsi_id = vsi->id;
	}
748

749 750
	/* Set VF bandwidth if specified */
	if (vf->tx_rate) {
751 752 753 754 755 756 757
		max_tx_rate = vf->tx_rate;
	} else if (vf->ch[idx].max_tx_rate) {
		max_tx_rate = vf->ch[idx].max_tx_rate;
	}

	if (max_tx_rate) {
		max_tx_rate = div_u64(max_tx_rate, I40E_BW_CREDIT_DIVISOR);
758
		ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
759
						  max_tx_rate, 0, NULL);
760 761 762 763 764
		if (ret)
			dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
				vf->vf_id, ret);
	}

765 766 767 768
error_alloc_vsi_res:
	return ret;
}

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 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
/**
 * i40e_map_pf_queues_to_vsi
 * @vf: pointer to the VF info
 *
 * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
 * function takes care of first part VSILAN_QTABLE, mapping pf queues to VSI.
 **/
static void i40e_map_pf_queues_to_vsi(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	u32 reg, num_tc = 1; /* VF has at least one traffic class */
	u16 vsi_id, qps;
	int i, j;

	if (vf->adq_enabled)
		num_tc = vf->num_tc;

	for (i = 0; i < num_tc; i++) {
		if (vf->adq_enabled) {
			qps = vf->ch[i].num_qps;
			vsi_id =  vf->ch[i].vsi_id;
		} else {
			qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
			vsi_id = vf->lan_vsi_id;
		}

		for (j = 0; j < 7; j++) {
			if (j * 2 >= qps) {
				/* end of list */
				reg = 0x07FF07FF;
			} else {
				u16 qid = i40e_vc_get_pf_queue_id(vf,
								  vsi_id,
								  j * 2);
				reg = qid;
				qid = i40e_vc_get_pf_queue_id(vf, vsi_id,
							      (j * 2) + 1);
				reg |= qid << 16;
			}
			i40e_write_rx_ctl(hw,
					  I40E_VSILAN_QTABLE(j, vsi_id),
					  reg);
		}
	}
}

/**
 * i40e_map_pf_to_vf_queues
 * @vf: pointer to the VF info
 *
 * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
 * function takes care of the second part VPLAN_QTABLE & completes VF mappings.
 **/
static void i40e_map_pf_to_vf_queues(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	u32 reg, total_qps = 0;
	u32 qps, num_tc = 1; /* VF has at least one traffic class */
	u16 vsi_id, qid;
	int i, j;

	if (vf->adq_enabled)
		num_tc = vf->num_tc;

	for (i = 0; i < num_tc; i++) {
		if (vf->adq_enabled) {
			qps = vf->ch[i].num_qps;
			vsi_id =  vf->ch[i].vsi_id;
		} else {
			qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
			vsi_id = vf->lan_vsi_id;
		}

		for (j = 0; j < qps; j++) {
			qid = i40e_vc_get_pf_queue_id(vf, vsi_id, j);

			reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
			wr32(hw, I40E_VPLAN_QTABLE(total_qps, vf->vf_id),
			     reg);
			total_qps++;
		}
	}
}

M
Mitch Williams 已提交
855 856
/**
 * i40e_enable_vf_mappings
857
 * @vf: pointer to the VF info
M
Mitch Williams 已提交
858
 *
859
 * enable VF mappings
M
Mitch Williams 已提交
860 861 862 863 864
 **/
static void i40e_enable_vf_mappings(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
865
	u32 reg;
M
Mitch Williams 已提交
866 867 868 869 870

	/* Tell the hardware we're using noncontiguous mapping. HW requires
	 * that VF queues be mapped using this method, even when they are
	 * contiguous in real life
	 */
871 872
	i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
			  I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
M
Mitch Williams 已提交
873 874 875 876 877

	/* enable VF vplan_qtable mappings */
	reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);

878 879
	i40e_map_pf_to_vf_queues(vf);
	i40e_map_pf_queues_to_vsi(vf);
M
Mitch Williams 已提交
880 881 882 883 884 885

	i40e_flush(hw);
}

/**
 * i40e_disable_vf_mappings
886
 * @vf: pointer to the VF info
M
Mitch Williams 已提交
887
 *
888
 * disable VF mappings
M
Mitch Williams 已提交
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
 **/
static void i40e_disable_vf_mappings(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	int i;

	/* disable qp mappings */
	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
	for (i = 0; i < I40E_MAX_VSI_QP; i++)
		wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
		     I40E_QUEUE_END_OF_LIST);
	i40e_flush(hw);
}

/**
 * i40e_free_vf_res
906
 * @vf: pointer to the VF info
M
Mitch Williams 已提交
907
 *
908
 * free VF resources
M
Mitch Williams 已提交
909 910 911 912
 **/
static void i40e_free_vf_res(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
M
Mitch Williams 已提交
913 914
	struct i40e_hw *hw = &pf->hw;
	u32 reg_idx, reg;
915
	int i, j, msix_vf;
M
Mitch Williams 已提交
916

917 918 919
	/* Start by disabling VF's configuration API to prevent the OS from
	 * accessing the VF's VSI after it's freed / invalidated.
	 */
920
	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
921

922 923 924 925 926 927 928 929
	/* It's possible the VF had requeuested more queues than the default so
	 * do the accounting here when we're about to free them.
	 */
	if (vf->num_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF) {
		pf->queues_left += vf->num_queue_pairs -
				   I40E_DEFAULT_QUEUES_PER_VF;
	}

M
Mitch Williams 已提交
930
	/* free vsi & disconnect it from the parent uplink */
931 932 933
	if (vf->lan_vsi_idx) {
		i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
		vf->lan_vsi_idx = 0;
M
Mitch Williams 已提交
934
		vf->lan_vsi_id = 0;
935
		vf->num_mac = 0;
M
Mitch Williams 已提交
936
	}
937 938 939 940 941 942 943 944 945 946 947 948 949 950

	/* do the accounting and remove additional ADq VSI's */
	if (vf->adq_enabled && vf->ch[0].vsi_idx) {
		for (j = 0; j < vf->num_tc; j++) {
			/* At this point VSI0 is already released so don't
			 * release it again and only clear their values in
			 * structure variables
			 */
			if (j)
				i40e_vsi_release(pf->vsi[vf->ch[j].vsi_idx]);
			vf->ch[j].vsi_idx = 0;
			vf->ch[j].vsi_id = 0;
		}
	}
951 952
	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;

M
Mitch Williams 已提交
953 954 955 956 957 958 959 960 961 962 963 964
	/* disable interrupts so the VF starts in a known state */
	for (i = 0; i < msix_vf; i++) {
		/* format is same for both registers */
		if (0 == i)
			reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
		else
			reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
						      (vf->vf_id))
						     + (i - 1));
		wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
		i40e_flush(hw);
	}
M
Mitch Williams 已提交
965

M
Mitch Williams 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979
	/* clear the irq settings */
	for (i = 0; i < msix_vf; i++) {
		/* format is same for both registers */
		if (0 == i)
			reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
		else
			reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
						      (vf->vf_id))
						     + (i - 1));
		reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
		       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
		wr32(hw, reg_idx, reg);
		i40e_flush(hw);
	}
980
	/* reset some of the state variables keeping track of the resources */
M
Mitch Williams 已提交
981
	vf->num_queue_pairs = 0;
982 983
	clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
	clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
M
Mitch Williams 已提交
984 985 986 987
}

/**
 * i40e_alloc_vf_res
988
 * @vf: pointer to the VF info
M
Mitch Williams 已提交
989
 *
990
 * allocate VF resources
M
Mitch Williams 已提交
991 992 993 994 995
 **/
static int i40e_alloc_vf_res(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
	int total_queue_pairs = 0;
996
	int ret, idx;
M
Mitch Williams 已提交
997

998 999 1000 1001 1002 1003
	if (vf->num_req_queues &&
	    vf->num_req_queues <= pf->queues_left + I40E_DEFAULT_QUEUES_PER_VF)
		pf->num_vf_qps = vf->num_req_queues;
	else
		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;

M
Mitch Williams 已提交
1004
	/* allocate hw vsi context & associated resources */
1005
	ret = i40e_alloc_vsi_res(vf, 0);
M
Mitch Williams 已提交
1006 1007
	if (ret)
		goto error_alloc;
1008
	total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
1009

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	/* allocate additional VSIs based on tc information for ADq */
	if (vf->adq_enabled) {
		if (pf->queues_left >=
		    (I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF)) {
			/* TC 0 always belongs to VF VSI */
			for (idx = 1; idx < vf->num_tc; idx++) {
				ret = i40e_alloc_vsi_res(vf, idx);
				if (ret)
					goto error_alloc;
			}
			/* send correct number of queues */
			total_queue_pairs = I40E_MAX_VF_QUEUES;
		} else {
			dev_info(&pf->pdev->dev, "VF %d: Not enough queues to allocate, disabling ADq\n",
				 vf->vf_id);
			vf->adq_enabled = false;
		}
	}

1029 1030 1031 1032 1033 1034 1035 1036 1037
	/* We account for each VF to get a default number of queue pairs.  If
	 * the VF has now requested more, we need to account for that to make
	 * certain we never request more queues than we actually have left in
	 * HW.
	 */
	if (total_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF)
		pf->queues_left -=
			total_queue_pairs - I40E_DEFAULT_QUEUES_PER_VF;

1038 1039 1040 1041
	if (vf->trusted)
		set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
	else
		clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
M
Mitch Williams 已提交
1042 1043

	/* store the total qps number for the runtime
1044
	 * VF req validation
M
Mitch Williams 已提交
1045 1046 1047
	 */
	vf->num_queue_pairs = total_queue_pairs;

1048
	/* VF is now completely initialized */
1049
	set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
M
Mitch Williams 已提交
1050 1051 1052 1053 1054 1055 1056 1057

error_alloc:
	if (ret)
		i40e_free_vf_res(vf);

	return ret;
}

M
Mitch Williams 已提交
1058 1059 1060 1061
#define VF_DEVICE_STATUS 0xAA
#define VF_TRANS_PENDING_MASK 0x20
/**
 * i40e_quiesce_vf_pci
1062
 * @vf: pointer to the VF structure
M
Mitch Williams 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
 *
 * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
 * if the transactions never clear.
 **/
static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	int vf_abs_id, i;
	u32 reg;

1074
	vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
M
Mitch Williams 已提交
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

	wr32(hw, I40E_PF_PCI_CIAA,
	     VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
	for (i = 0; i < 100; i++) {
		reg = rd32(hw, I40E_PF_PCI_CIAD);
		if ((reg & VF_TRANS_PENDING_MASK) == 0)
			return 0;
		udelay(1);
	}
	return -EIO;
}

1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 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 1151 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
static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi);

/**
 * i40e_config_vf_promiscuous_mode
 * @vf: pointer to the VF info
 * @vsi_id: VSI id
 * @allmulti: set MAC L2 layer multicast promiscuous enable/disable
 * @alluni: set MAC L2 layer unicast promiscuous enable/disable
 *
 * Called from the VF to configure the promiscuous mode of
 * VF vsis and from the VF reset path to reset promiscuous mode.
 **/
static i40e_status i40e_config_vf_promiscuous_mode(struct i40e_vf *vf,
						   u16 vsi_id,
						   bool allmulti,
						   bool alluni)
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	struct i40e_mac_filter *f;
	i40e_status aq_ret = 0;
	struct i40e_vsi *vsi;
	int bkt;

	vsi = i40e_find_vsi_from_id(pf, vsi_id);
	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id) || !vsi)
		return I40E_ERR_PARAM;

	if (vf->port_vlan_id) {
		aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
							    allmulti,
							    vf->port_vlan_id,
							    NULL);
		if (aq_ret) {
			int aq_err = pf->hw.aq.asq_last_status;

			dev_err(&pf->pdev->dev,
				"VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
				vf->vf_id,
				i40e_stat_str(&pf->hw, aq_ret),
				i40e_aq_str(&pf->hw, aq_err));
			return aq_ret;
		}

		aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
							    alluni,
							    vf->port_vlan_id,
							    NULL);
		if (aq_ret) {
			int aq_err = pf->hw.aq.asq_last_status;

			dev_err(&pf->pdev->dev,
				"VF %d failed to set unicast promiscuous mode err %s aq_err %s\n",
				vf->vf_id,
				i40e_stat_str(&pf->hw, aq_ret),
				i40e_aq_str(&pf->hw, aq_err));
		}
		return aq_ret;
	} else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
		hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
			if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
				continue;
			aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
								    vsi->seid,
								    allmulti,
								    f->vlan,
								    NULL);
			if (aq_ret) {
				int aq_err = pf->hw.aq.asq_last_status;

				dev_err(&pf->pdev->dev,
					"Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
					f->vlan,
					i40e_stat_str(&pf->hw, aq_ret),
					i40e_aq_str(&pf->hw, aq_err));
			}

			aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
								    vsi->seid,
								    alluni,
								    f->vlan,
								    NULL);
			if (aq_ret) {
				int aq_err = pf->hw.aq.asq_last_status;

				dev_err(&pf->pdev->dev,
					"Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
					f->vlan,
					i40e_stat_str(&pf->hw, aq_ret),
					i40e_aq_str(&pf->hw, aq_err));
			}
		}
		return aq_ret;
	}
	aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid, allmulti,
						       NULL);
	if (aq_ret) {
		int aq_err = pf->hw.aq.asq_last_status;

		dev_err(&pf->pdev->dev,
			"VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
			vf->vf_id,
			i40e_stat_str(&pf->hw, aq_ret),
			i40e_aq_str(&pf->hw, aq_err));
		return aq_ret;
	}

	aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid, alluni,
						     NULL, true);
	if (aq_ret) {
		int aq_err = pf->hw.aq.asq_last_status;

		dev_err(&pf->pdev->dev,
			"VF %d failed to set unicast promiscuous mode err %s aq_err %s\n",
			vf->vf_id,
			i40e_stat_str(&pf->hw, aq_ret),
			i40e_aq_str(&pf->hw, aq_err));
	}

	return aq_ret;
}

1209
/**
1210
 * i40e_trigger_vf_reset
1211
 * @vf: pointer to the VF structure
1212 1213
 * @flr: VFLR was issued or not
 *
1214 1215 1216
 * Trigger hardware to start a reset for a particular VF. Expects the caller
 * to wait the proper amount of time to allow hardware to reset the VF before
 * it cleans up and restores VF functionality.
1217
 **/
1218
static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
1219 1220 1221
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
1222
	u32 reg, reg_idx, bit_idx;
1223

1224
	/* warn the VF */
1225
	clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1226

1227 1228 1229 1230 1231 1232
	/* Disable VF's configuration API during reset. The flag is re-enabled
	 * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
	 * It's normally disabled in i40e_free_vf_res(), but it's safer
	 * to do it earlier to give some time to finish to any VF config
	 * functions that may still be running at this point.
	 */
1233
	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
1234

M
Mitch Williams 已提交
1235 1236
	/* In the case of a VFLR, the HW has already reset the VF and we
	 * just need to clean up, so don't hit the VFRTRIG register.
1237 1238
	 */
	if (!flr) {
1239
		/* reset VF using VPGEN_VFRTRIG reg */
M
Mitch Williams 已提交
1240 1241
		reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
		reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1242 1243 1244
		wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
		i40e_flush(hw);
	}
1245 1246 1247 1248
	/* clear the VFLR bit in GLGEN_VFLRSTAT */
	reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
	bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
	wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
A
Akeem G Abodunrin 已提交
1249
	i40e_flush(hw);
1250

M
Mitch Williams 已提交
1251 1252 1253
	if (i40e_quiesce_vf_pci(vf))
		dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
			vf->vf_id);
1254
}
M
Mitch Williams 已提交
1255

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
/**
 * i40e_cleanup_reset_vf
 * @vf: pointer to the VF structure
 *
 * Cleanup a VF after the hardware reset is finished. Expects the caller to
 * have verified whether the reset is finished properly, and ensure the
 * minimum amount of wait time has passed.
 **/
static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	u32 reg;
M
Mitch Williams 已提交
1269

1270 1271 1272
	/* disable promisc modes in case they were enabled */
	i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id, false, false);

1273
	/* free VF resources to begin resetting the VSI state */
M
Mitch Williams 已提交
1274
	i40e_free_vf_res(vf);
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290

	/* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
	 * By doing this we allow HW to access VF memory at any point. If we
	 * did it any sooner, HW could access memory while it was being freed
	 * in i40e_free_vf_res(), causing an IOMMU fault.
	 *
	 * On the other hand, this needs to be done ASAP, because the VF driver
	 * is waiting for this to happen and may report a timeout. It's
	 * harmless, but it gets logged into Guest OS kernel log, so best avoid
	 * it.
	 */
	reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
	reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
	wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);

	/* reallocate VF resources to finish resetting the VSI state */
1291
	if (!i40e_alloc_vf_res(vf)) {
1292
		int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1293
		i40e_enable_vf_mappings(vf);
1294 1295
		set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
		clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1296
		/* Do not notify the client during VF init */
A
Alan Brady 已提交
1297 1298
		if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
					&vf->vf_states))
1299
			i40e_notify_client_of_vf_reset(pf, abs_vf_id);
1300
		vf->num_vlan = 0;
1301
	}
1302 1303 1304 1305 1306

	/* Tell the VF driver the reset is done. This needs to be done only
	 * after VF has been fully initialized, because the VF driver may
	 * request resources immediately after setting this flag.
	 */
1307
	wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1308 1309 1310 1311 1312 1313 1314
}

/**
 * i40e_reset_vf
 * @vf: pointer to the VF structure
 * @flr: VFLR was issued or not
 *
1315
 * Returns true if the VF is reset, false otherwise.
1316
 **/
1317
bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
1318 1319 1320 1321 1322 1323 1324
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	bool rsd = false;
	u32 reg;
	int i;

1325 1326 1327
	/* If the VFs have been disabled, this means something else is
	 * resetting the VF, so we shouldn't continue.
	 */
1328
	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1329
		return false;
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362

	i40e_trigger_vf_reset(vf, flr);

	/* poll VPGEN_VFRSTAT reg to make sure
	 * that reset is complete
	 */
	for (i = 0; i < 10; i++) {
		/* VF reset requires driver to first reset the VF and then
		 * poll the status register to make sure that the reset
		 * completed successfully. Due to internal HW FIFO flushes,
		 * we must wait 10ms before the register will be valid.
		 */
		usleep_range(10000, 20000);
		reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
		if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
			rsd = true;
			break;
		}
	}

	if (flr)
		usleep_range(10000, 20000);

	if (!rsd)
		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
			vf->vf_id);
	usleep_range(10000, 20000);

	/* On initial reset, we don't have any queues to disable */
	if (vf->lan_vsi_idx != 0)
		i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);

	i40e_cleanup_reset_vf(vf);
1363

1364
	i40e_flush(hw);
1365
	clear_bit(__I40E_VF_DISABLE, pf->state);
1366 1367

	return true;
1368
}
1369

1370 1371 1372 1373 1374 1375 1376 1377 1378
/**
 * i40e_reset_all_vfs
 * @pf: pointer to the PF structure
 * @flr: VFLR was issued or not
 *
 * Reset all allocated VFs in one go. First, tell the hardware to reset each
 * VF, then do all the waiting in one chunk, and finally finish restoring each
 * VF after the wait. This is useful during PF routines which need to reset
 * all VFs, as otherwise it must perform these resets in a serialized fashion.
1379 1380
 *
 * Returns true if any VFs were reset, and false otherwise.
1381
 **/
1382
bool i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
1383 1384 1385 1386 1387 1388 1389 1390
{
	struct i40e_hw *hw = &pf->hw;
	struct i40e_vf *vf;
	int i, v;
	u32 reg;

	/* If we don't have any VFs, then there is nothing to reset */
	if (!pf->num_alloc_vfs)
1391
		return false;
1392 1393

	/* If VFs have been disabled, there is no need to reset */
1394
	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1395
		return false;
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468

	/* Begin reset on all VFs at once */
	for (v = 0; v < pf->num_alloc_vfs; v++)
		i40e_trigger_vf_reset(&pf->vf[v], flr);

	/* HW requires some time to make sure it can flush the FIFO for a VF
	 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
	 * sequence to make sure that it has completed. We'll keep track of
	 * the VFs using a simple iterator that increments once that VF has
	 * finished resetting.
	 */
	for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
		usleep_range(10000, 20000);

		/* Check each VF in sequence, beginning with the VF to fail
		 * the previous check.
		 */
		while (v < pf->num_alloc_vfs) {
			vf = &pf->vf[v];
			reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
			if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
				break;

			/* If the current VF has finished resetting, move on
			 * to the next VF in sequence.
			 */
			v++;
		}
	}

	if (flr)
		usleep_range(10000, 20000);

	/* Display a warning if at least one VF didn't manage to reset in
	 * time, but continue on with the operation.
	 */
	if (v < pf->num_alloc_vfs)
		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
			pf->vf[v].vf_id);
	usleep_range(10000, 20000);

	/* Begin disabling all the rings associated with VFs, but do not wait
	 * between each VF.
	 */
	for (v = 0; v < pf->num_alloc_vfs; v++) {
		/* On initial reset, we don't have any queues to disable */
		if (pf->vf[v].lan_vsi_idx == 0)
			continue;

		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
	}

	/* Now that we've notified HW to disable all of the VF rings, wait
	 * until they finish.
	 */
	for (v = 0; v < pf->num_alloc_vfs; v++) {
		/* On initial reset, we don't have any queues to disable */
		if (pf->vf[v].lan_vsi_idx == 0)
			continue;

		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
	}

	/* Hw may need up to 50ms to finish disabling the RX queues. We
	 * minimize the wait by delaying only once for all VFs.
	 */
	mdelay(50);

	/* Finish the reset on each VF */
	for (v = 0; v < pf->num_alloc_vfs; v++)
		i40e_cleanup_reset_vf(&pf->vf[v]);

	i40e_flush(hw);
1469
	clear_bit(__I40E_VF_DISABLE, pf->state);
1470 1471

	return true;
1472 1473
}

1474 1475
/**
 * i40e_free_vfs
1476
 * @pf: pointer to the PF structure
1477
 *
1478
 * free VF resources
1479 1480 1481
 **/
void i40e_free_vfs(struct i40e_pf *pf)
{
1482 1483 1484
	struct i40e_hw *hw = &pf->hw;
	u32 reg_idx, bit_idx;
	int i, tmp, vf_id;
1485 1486 1487

	if (!pf->vf)
		return;
1488
	while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1489
		usleep_range(1000, 2000);
1490

1491
	i40e_notify_client_of_vf_enable(pf, 0);
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501

	/* Amortize wait time by stopping all VFs at the same time */
	for (i = 0; i < pf->num_alloc_vfs; i++) {
		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
			continue;

		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
	}

	for (i = 0; i < pf->num_alloc_vfs; i++) {
1502
		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1503 1504 1505 1506
			continue;

		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
	}
M
Mitch Williams 已提交
1507

1508 1509 1510 1511 1512 1513
	/* Disable IOV before freeing resources. This lets any VF drivers
	 * running in the host get themselves cleaned up before we yank
	 * the carpet out from underneath their feet.
	 */
	if (!pci_vfs_assigned(pf->pdev))
		pci_disable_sriov(pf->pdev);
M
Mitch Williams 已提交
1514 1515
	else
		dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1516

1517
	/* free up VF resources */
1518 1519 1520
	tmp = pf->num_alloc_vfs;
	pf->num_alloc_vfs = 0;
	for (i = 0; i < tmp; i++) {
1521
		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1522 1523 1524 1525 1526 1527 1528 1529
			i40e_free_vf_res(&pf->vf[i]);
		/* disable qp mappings */
		i40e_disable_vf_mappings(&pf->vf[i]);
	}

	kfree(pf->vf);
	pf->vf = NULL;

1530 1531 1532 1533
	/* This check is for when the driver is unloaded while VFs are
	 * assigned. Setting the number of VFs to 0 through sysfs is caught
	 * before this function ever gets called.
	 */
1534
	if (!pci_vfs_assigned(pf->pdev)) {
1535 1536 1537 1538 1539 1540
		/* Acknowledge VFLR for all VFS. Without this, VFs will fail to
		 * work correctly when SR-IOV gets re-enabled.
		 */
		for (vf_id = 0; vf_id < tmp; vf_id++) {
			reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
			bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1541
			wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1542
		}
1543
	}
1544
	clear_bit(__I40E_VF_DISABLE, pf->state);
1545 1546 1547 1548 1549
}

#ifdef CONFIG_PCI_IOV
/**
 * i40e_alloc_vfs
1550 1551
 * @pf: pointer to the PF structure
 * @num_alloc_vfs: number of VFs to allocate
1552
 *
1553
 * allocate VF resources
1554
 **/
M
Mitch Williams 已提交
1555
int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1556 1557 1558 1559
{
	struct i40e_vf *vfs;
	int i, ret = 0;

1560
	/* Disable interrupt 0 so we don't try to handle the VFLR. */
1561 1562
	i40e_irq_dynamic_disable_icr0(pf);

M
Mitch Williams 已提交
1563 1564 1565 1566
	/* Check to see if we're just allocating resources for extant VFs */
	if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
		ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
		if (ret) {
1567
			pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
M
Mitch Williams 已提交
1568 1569 1570
			pf->num_alloc_vfs = 0;
			goto err_iov;
		}
1571 1572
	}
	/* allocate memory */
1573
	vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1574 1575 1576 1577
	if (!vfs) {
		ret = -ENOMEM;
		goto err_alloc;
	}
1578
	pf->vf = vfs;
1579 1580 1581 1582 1583 1584 1585 1586 1587

	/* apply default profile */
	for (i = 0; i < num_alloc_vfs; i++) {
		vfs[i].pf = pf;
		vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
		vfs[i].vf_id = i;

		/* assign default capabilities */
		set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1588
		vfs[i].spoofchk = true;
1589 1590

		set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
1591 1592 1593 1594

	}
	pf->num_alloc_vfs = num_alloc_vfs;

1595 1596 1597
	/* VF resources get allocated during reset */
	i40e_reset_all_vfs(pf, false);

1598 1599
	i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);

1600 1601 1602 1603
err_alloc:
	if (ret)
		i40e_free_vfs(pf);
err_iov:
1604
	/* Re-enable interrupt 0. */
1605
	i40e_irq_dynamic_enable_icr0(pf);
1606 1607 1608 1609 1610 1611 1612
	return ret;
}

#endif
/**
 * i40e_pci_sriov_enable
 * @pdev: pointer to a pci_dev structure
1613
 * @num_vfs: number of VFs to allocate
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
 *
 * Enable or change the number of VFs
 **/
static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
{
#ifdef CONFIG_PCI_IOV
	struct i40e_pf *pf = pci_get_drvdata(pdev);
	int pre_existing_vfs = pci_num_vf(pdev);
	int err = 0;

1624
	if (test_bit(__I40E_TESTING, pf->state)) {
1625 1626 1627 1628 1629 1630
		dev_warn(&pdev->dev,
			 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
		err = -EPERM;
		goto err_out;
	}

1631 1632 1633 1634 1635 1636
	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
		i40e_free_vfs(pf);
	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
		goto out;

	if (num_vfs > pf->num_req_vfs) {
1637 1638
		dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
			 num_vfs, pf->num_req_vfs);
1639 1640 1641 1642
		err = -EPERM;
		goto err_out;
	}

1643
	dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
	err = i40e_alloc_vfs(pf, num_vfs);
	if (err) {
		dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
		goto err_out;
	}

out:
	return num_vfs;

err_out:
	return err;
#endif
	return 0;
}

/**
 * i40e_pci_sriov_configure
 * @pdev: pointer to a pci_dev structure
1662
 * @num_vfs: number of VFs to allocate
1663 1664 1665 1666 1667 1668 1669
 *
 * Enable or change the number of VFs. Called when the user updates the number
 * of VFs in sysfs.
 **/
int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
{
	struct i40e_pf *pf = pci_get_drvdata(pdev);
1670 1671 1672 1673 1674 1675
	int ret = 0;

	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
		dev_warn(&pdev->dev, "Unable to configure VFs, other operation is pending.\n");
		return -EAGAIN;
	}
1676

1677 1678 1679
	if (num_vfs) {
		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
A
Amritha Nambiar 已提交
1680
			i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
1681
		}
1682 1683
		ret = i40e_pci_sriov_enable(pdev, num_vfs);
		goto sriov_configure_out;
1684
	}
1685

1686
	if (!pci_vfs_assigned(pf->pdev)) {
1687
		i40e_free_vfs(pf);
1688
		pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
A
Amritha Nambiar 已提交
1689
		i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
1690 1691
	} else {
		dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1692 1693
		ret = -EINVAL;
		goto sriov_configure_out;
1694
	}
1695 1696 1697
sriov_configure_out:
	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
	return ret;
1698 1699 1700 1701 1702 1703
}

/***********************virtual channel routines******************/

/**
 * i40e_vc_send_msg_to_vf
1704
 * @vf: pointer to the VF info
1705 1706 1707 1708 1709
 * @v_opcode: virtual channel opcode
 * @v_retval: virtual channel return value
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
1710
 * send msg to VF
1711 1712 1713 1714
 **/
static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
				  u32 v_retval, u8 *msg, u16 msglen)
{
1715 1716 1717
	struct i40e_pf *pf;
	struct i40e_hw *hw;
	int abs_vf_id;
1718 1719
	i40e_status aq_ret;

1720 1721 1722 1723 1724 1725 1726 1727
	/* validate the request */
	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
		return -EINVAL;

	pf = vf->pf;
	hw = &pf->hw;
	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;

1728 1729 1730
	/* single place to detect unsuccessful return values */
	if (v_retval) {
		vf->num_invalid_msgs++;
M
Mitch Williams 已提交
1731 1732
		dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
			 vf->vf_id, v_opcode, v_retval);
1733 1734 1735 1736 1737 1738
		if (vf->num_invalid_msgs >
		    I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
			dev_err(&pf->pdev->dev,
				"Number of invalid messages exceeded for VF %d\n",
				vf->vf_id);
			dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1739
			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1740 1741 1742
		}
	} else {
		vf->num_valid_msgs++;
1743 1744
		/* reset the invalid counter, if a valid message is received. */
		vf->num_invalid_msgs = 0;
1745 1746
	}

1747
	aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,	v_opcode, v_retval,
1748
					msg, msglen, NULL);
1749
	if (aq_ret) {
M
Mitch Williams 已提交
1750 1751 1752
		dev_info(&pf->pdev->dev,
			 "Unable to send the message to VF %d aq_err %d\n",
			 vf->vf_id, pf->hw.aq.asq_last_status);
1753 1754 1755 1756 1757 1758 1759 1760
		return -EIO;
	}

	return 0;
}

/**
 * i40e_vc_send_resp_to_vf
1761
 * @vf: pointer to the VF info
1762 1763 1764
 * @opcode: operation code
 * @retval: return value
 *
1765
 * send resp msg to VF
1766 1767
 **/
static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1768
				   enum virtchnl_ops opcode,
1769 1770 1771 1772 1773 1774 1775
				   i40e_status retval)
{
	return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
}

/**
 * i40e_vc_get_version_msg
1776
 * @vf: pointer to the VF info
1777
 * @msg: pointer to the msg buffer
1778
 *
1779
 * called from the VF to request the API version used by the PF
1780
 **/
1781
static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
1782
{
1783 1784
	struct virtchnl_version_info info = {
		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1785 1786
	};

1787
	vf->vf_ver = *(struct virtchnl_version_info *)msg;
1788
	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1789
	if (VF_IS_V10(&vf->vf_ver))
1790 1791
		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1792
				      I40E_SUCCESS, (u8 *)&info,
1793
				      sizeof(struct virtchnl_version_info));
1794 1795
}

1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
/**
 * i40e_del_qch - delete all the additional VSIs created as a part of ADq
 * @vf: pointer to VF structure
 **/
static void i40e_del_qch(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
	int i;

	/* first element in the array belongs to primary VF VSI and we shouldn't
	 * delete it. We should however delete the rest of the VSIs created
	 */
	for (i = 1; i < vf->num_tc; i++) {
		if (vf->ch[i].vsi_idx) {
			i40e_vsi_release(pf->vsi[vf->ch[i].vsi_idx]);
			vf->ch[i].vsi_idx = 0;
			vf->ch[i].vsi_id = 0;
		}
	}
}

1817 1818
/**
 * i40e_vc_get_vf_resources_msg
1819
 * @vf: pointer to the VF info
1820 1821
 * @msg: pointer to the msg buffer
 *
1822
 * called from the VF to request its resources
1823
 **/
1824
static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
1825
{
1826
	struct virtchnl_vf_resource *vfres = NULL;
1827 1828 1829 1830
	struct i40e_pf *pf = vf->pf;
	i40e_status aq_ret = 0;
	struct i40e_vsi *vsi;
	int num_vsis = 1;
M
Mitch Williams 已提交
1831
	int len = 0;
1832 1833
	int ret;

1834
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
1835 1836 1837 1838
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

1839 1840
	len = (sizeof(struct virtchnl_vf_resource) +
	       sizeof(struct virtchnl_vsi_resource) * num_vsis);
1841 1842 1843 1844 1845 1846 1847

	vfres = kzalloc(len, GFP_KERNEL);
	if (!vfres) {
		aq_ret = I40E_ERR_NO_MEMORY;
		len = 0;
		goto err;
	}
1848
	if (VF_IS_V11(&vf->vf_ver))
1849 1850
		vf->driver_caps = *(u32 *)msg;
	else
1851 1852 1853
		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
				  VIRTCHNL_VF_OFFLOAD_RSS_REG |
				  VIRTCHNL_VF_OFFLOAD_VLAN;
1854

1855
	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1856
	vsi = pf->vsi[vf->lan_vsi_idx];
1857
	if (!vsi->info.pvid)
1858
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1859

M
Mitch Williams 已提交
1860
	if (i40e_vf_client_capable(pf, vf->vf_id) &&
1861
	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
1862
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
1863
		set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1864 1865
	} else {
		clear_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1866 1867
	}

1868
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1869
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1870
	} else {
1871
		if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
1872
		    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
1873
			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1874
		else
1875
			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1876
	}
1877

1878
	if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
1879
		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1880
			vfres->vf_cap_flags |=
1881
				VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1882 1883
	}

1884
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1885
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1886

1887
	if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
1888
	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
1889
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1890

1891
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
1892 1893 1894 1895
		if (pf->flags & I40E_FLAG_MFP_ENABLED) {
			dev_err(&pf->pdev->dev,
				"VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
				 vf->vf_id);
1896
			aq_ret = I40E_ERR_PARAM;
1897 1898
			goto err;
		}
1899
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1900
	}
1901

1902
	if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
1903
		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1904
			vfres->vf_cap_flags |=
1905
					VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1906 1907
	}

1908 1909 1910
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;

1911 1912 1913
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADQ;

1914 1915 1916
	vfres->num_vsis = num_vsis;
	vfres->num_queue_pairs = vf->num_queue_pairs;
	vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1917 1918 1919
	vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
	vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;

1920
	if (vf->lan_vsi_idx) {
M
Mitch Williams 已提交
1921
		vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
1922
		vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
M
Mitch Williams 已提交
1923
		vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
1924
		/* VFs only use TC 0 */
M
Mitch Williams 已提交
1925
		vfres->vsi_res[0].qset_handle
1926
					  = le16_to_cpu(vsi->info.qs_handle[0]);
M
Mitch Williams 已提交
1927
		ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
J
Jesse Brandeburg 已提交
1928
				vf->default_lan_addr.addr);
1929
	}
1930
	set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1931 1932

err:
1933
	/* send the response back to the VF */
1934
	ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
1935 1936 1937 1938 1939 1940 1941 1942
				     aq_ret, (u8 *)vfres, len);

	kfree(vfres);
	return ret;
}

/**
 * i40e_vc_reset_vf_msg
1943
 * @vf: pointer to the VF info
1944
 *
1945 1946 1947
 * called from the VF to reset itself,
 * unlike other virtchnl messages, PF driver
 * doesn't send the response back to the VF
1948
 **/
M
Mitch Williams 已提交
1949
static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1950
{
1951
	if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
M
Mitch Williams 已提交
1952
		i40e_reset_vf(vf, false);
1953 1954
}

1955 1956 1957 1958 1959 1960 1961 1962 1963
/**
 * i40e_getnum_vf_vsi_vlan_filters
 * @vsi: pointer to the vsi
 *
 * called to get the number of VLANs offloaded on this VF
 **/
static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
{
	struct i40e_mac_filter *f;
1964
	int num_vlans = 0, bkt;
1965

1966
	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1967 1968 1969 1970 1971 1972 1973
		if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
			num_vlans++;
	}

	return num_vlans;
}

1974 1975
/**
 * i40e_vc_config_promiscuous_mode_msg
1976
 * @vf: pointer to the VF info
1977 1978
 * @msg: pointer to the msg buffer
 *
1979 1980
 * called from the VF to configure the promiscuous mode of
 * VF vsis
1981
 **/
1982
static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf, u8 *msg)
1983
{
1984 1985
	struct virtchnl_promisc_info *info =
	    (struct virtchnl_promisc_info *)msg;
1986
	struct i40e_pf *pf = vf->pf;
1987
	i40e_status aq_ret = 0;
1988
	bool allmulti = false;
1989
	bool alluni = false;
1990

1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
		aq_ret = I40E_ERR_PARAM;
		goto err_out;
	}
	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
		dev_err(&pf->pdev->dev,
			"Unprivileged VF %d is attempting to configure promiscuous mode\n",
			vf->vf_id);

		/* Lie to the VF on purpose, because this is an error we can
		 * ignore. Unprivileged VF is not a virtual channel error.
		 */
		aq_ret = 0;
		goto err_out;
	}
2006

2007
	/* Multicast promiscuous handling*/
2008
	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
2009
		allmulti = true;
2010

2011 2012 2013 2014
	if (info->flags & FLAG_VF_UNICAST_PROMISC)
		alluni = true;
	aq_ret = i40e_config_vf_promiscuous_mode(vf, info->vsi_id, allmulti,
						 alluni);
2015
	if (!aq_ret) {
2016 2017 2018 2019
		if (allmulti) {
			dev_info(&pf->pdev->dev,
				 "VF %d successfully set multicast promiscuous mode\n",
				 vf->vf_id);
2020
			set_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
2021 2022 2023 2024
		} else {
			dev_info(&pf->pdev->dev,
				 "VF %d successfully unset multicast promiscuous mode\n",
				 vf->vf_id);
2025
			clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
2026
		}
2027 2028 2029 2030
		if (alluni) {
			dev_info(&pf->pdev->dev,
				 "VF %d successfully set unicast promiscuous mode\n",
				 vf->vf_id);
2031
			set_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
2032 2033 2034 2035
		} else {
			dev_info(&pf->pdev->dev,
				 "VF %d successfully unset unicast promiscuous mode\n",
				 vf->vf_id);
2036
			clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
2037
		}
2038
	}
2039
err_out:
2040
	/* send the response to the VF */
2041
	return i40e_vc_send_resp_to_vf(vf,
2042
				       VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
2043 2044 2045 2046 2047
				       aq_ret);
}

/**
 * i40e_vc_config_queues_msg
2048
 * @vf: pointer to the VF info
2049 2050
 * @msg: pointer to the msg buffer
 *
2051
 * called from the VF to configure the rx/tx
2052 2053
 * queues
 **/
2054
static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg)
2055
{
2056 2057 2058
	struct virtchnl_vsi_queue_config_info *qci =
	    (struct virtchnl_vsi_queue_config_info *)msg;
	struct virtchnl_queue_pair_info *qpi;
2059
	struct i40e_pf *pf = vf->pf;
2060
	u16 vsi_id, vsi_queue_id = 0;
2061
	i40e_status aq_ret = 0;
2062 2063 2064
	int i, j = 0, idx = 0;

	vsi_id = qci->vsi_id;
2065

2066
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2067 2068 2069 2070 2071 2072 2073 2074
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}
2075

2076 2077 2078 2079 2080
	if (qci->num_queue_pairs > I40E_MAX_VF_QUEUES) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

2081 2082
	for (i = 0; i < qci->num_queue_pairs; i++) {
		qpi = &qci->qpair[i];
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095

		if (!vf->adq_enabled) {
			vsi_queue_id = qpi->txq.queue_id;

			if (qpi->txq.vsi_id != qci->vsi_id ||
			    qpi->rxq.vsi_id != qci->vsi_id ||
			    qpi->rxq.queue_id != vsi_queue_id) {
				aq_ret = I40E_ERR_PARAM;
				goto error_param;
			}
		}

		if (!i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
			aq_ret = I40E_ERR_PARAM;
			goto error_param;
		}

		if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
					     &qpi->rxq) ||
		    i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
					     &qpi->txq)) {
			aq_ret = I40E_ERR_PARAM;
			goto error_param;
		}
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123

		/* For ADq there can be up to 4 VSIs with max 4 queues each.
		 * VF does not know about these additional VSIs and all
		 * it cares is about its own queues. PF configures these queues
		 * to its appropriate VSIs based on TC mapping
		 **/
		if (vf->adq_enabled) {
			if (j == (vf->ch[idx].num_qps - 1)) {
				idx++;
				j = 0; /* resetting the queue count */
				vsi_queue_id = 0;
			} else {
				j++;
				vsi_queue_id++;
			}
			vsi_id = vf->ch[idx].vsi_id;
		}
2124
	}
2125
	/* set vsi num_queue_pairs in use to num configured by VF */
2126 2127 2128 2129 2130 2131 2132 2133
	if (!vf->adq_enabled) {
		pf->vsi[vf->lan_vsi_idx]->num_queue_pairs =
			qci->num_queue_pairs;
	} else {
		for (i = 0; i < vf->num_tc; i++)
			pf->vsi[vf->ch[i].vsi_idx]->num_queue_pairs =
			       vf->ch[i].num_qps;
	}
2134 2135

error_param:
2136
	/* send the response to the VF */
2137
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2138 2139 2140
				       aq_ret);
}

2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
/**
 * i40e_validate_queue_map
 * @vsi_id: vsi id
 * @queuemap: Tx or Rx queue map
 *
 * check if Tx or Rx queue map is valid
 **/
static int i40e_validate_queue_map(struct i40e_vf *vf, u16 vsi_id,
				   unsigned long queuemap)
{
	u16 vsi_queue_id, queue_id;

	for_each_set_bit(vsi_queue_id, &queuemap, I40E_MAX_VSI_QP) {
		if (vf->adq_enabled) {
			vsi_id = vf->ch[vsi_queue_id / I40E_MAX_VF_VSI].vsi_id;
			queue_id = (vsi_queue_id % I40E_DEFAULT_QUEUES_PER_VF);
		} else {
			queue_id = vsi_queue_id;
		}

		if (!i40e_vc_isvalid_queue_id(vf, vsi_id, queue_id))
			return -EINVAL;
	}

	return 0;
}

2168 2169
/**
 * i40e_vc_config_irq_map_msg
2170
 * @vf: pointer to the VF info
2171 2172
 * @msg: pointer to the msg buffer
 *
2173
 * called from the VF to configure the irq to
2174 2175
 * queue map
 **/
2176
static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg)
2177
{
2178 2179 2180
	struct virtchnl_irq_map_info *irqmap_info =
	    (struct virtchnl_irq_map_info *)msg;
	struct virtchnl_vector_map *map;
2181
	u16 vsi_id, vector_id;
2182 2183 2184
	i40e_status aq_ret = 0;
	int i;

2185
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	for (i = 0; i < irqmap_info->num_vectors; i++) {
		map = &irqmap_info->vecmap[i];
		vector_id = map->vector_id;
		vsi_id = map->vsi_id;
		/* validate msg params */
		if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
		    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
			aq_ret = I40E_ERR_PARAM;
			goto error_param;
		}

2201 2202 2203
		if (i40e_validate_queue_map(vf, vsi_id, map->rxq_map)) {
			aq_ret = I40E_ERR_PARAM;
			goto error_param;
2204 2205
		}

2206 2207 2208
		if (i40e_validate_queue_map(vf, vsi_id, map->txq_map)) {
			aq_ret = I40E_ERR_PARAM;
			goto error_param;
2209 2210 2211 2212 2213
		}

		i40e_config_irq_link_list(vf, vsi_id, map);
	}
error_param:
2214
	/* send the response to the VF */
2215
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
2216 2217 2218
				       aq_ret);
}

2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
/**
 * i40e_ctrl_vf_tx_rings
 * @vsi: the SRIOV VSI being configured
 * @q_map: bit map of the queues to be enabled
 * @enable: start or stop the queue
 **/
static int i40e_ctrl_vf_tx_rings(struct i40e_vsi *vsi, unsigned long q_map,
				 bool enable)
{
	struct i40e_pf *pf = vsi->back;
	int ret = 0;
	u16 q_id;

	for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
		ret = i40e_control_wait_tx_q(vsi->seid, pf,
					     vsi->base_queue + q_id,
					     false /*is xdp*/, enable);
		if (ret)
			break;
	}
	return ret;
}

/**
 * i40e_ctrl_vf_rx_rings
 * @vsi: the SRIOV VSI being configured
 * @q_map: bit map of the queues to be enabled
 * @enable: start or stop the queue
 **/
static int i40e_ctrl_vf_rx_rings(struct i40e_vsi *vsi, unsigned long q_map,
				 bool enable)
{
	struct i40e_pf *pf = vsi->back;
	int ret = 0;
	u16 q_id;

	for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
		ret = i40e_control_wait_rx_q(pf, vsi->base_queue + q_id,
					     enable);
		if (ret)
			break;
	}
	return ret;
}

2264 2265
/**
 * i40e_vc_enable_queues_msg
2266
 * @vf: pointer to the VF info
2267 2268
 * @msg: pointer to the msg buffer
 *
2269
 * called from the VF to enable all or specific queue(s)
2270
 **/
2271
static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg)
2272
{
2273 2274
	struct virtchnl_queue_select *vqs =
	    (struct virtchnl_queue_select *)msg;
2275 2276 2277
	struct i40e_pf *pf = vf->pf;
	u16 vsi_id = vqs->vsi_id;
	i40e_status aq_ret = 0;
2278
	int i;
2279

2280
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}
2294

2295 2296 2297
	/* Use the queue bit map sent by the VF */
	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
				  true)) {
M
Mitch Williams 已提交
2298
		aq_ret = I40E_ERR_TIMEOUT;
2299 2300 2301 2302 2303 2304 2305
		goto error_param;
	}
	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
				  true)) {
		aq_ret = I40E_ERR_TIMEOUT;
		goto error_param;
	}
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315

	/* need to start the rings for additional ADq VSI's as well */
	if (vf->adq_enabled) {
		/* zero belongs to LAN VSI */
		for (i = 1; i < vf->num_tc; i++) {
			if (i40e_vsi_start_rings(pf->vsi[vf->ch[i].vsi_idx]))
				aq_ret = I40E_ERR_TIMEOUT;
		}
	}

2316
error_param:
2317
	/* send the response to the VF */
2318
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
2319 2320 2321 2322 2323
				       aq_ret);
}

/**
 * i40e_vc_disable_queues_msg
2324
 * @vf: pointer to the VF info
2325 2326
 * @msg: pointer to the msg buffer
 *
2327
 * called from the VF to disable all or specific
2328 2329
 * queue(s)
 **/
2330
static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg)
2331
{
2332 2333
	struct virtchnl_queue_select *vqs =
	    (struct virtchnl_queue_select *)msg;
2334 2335 2336
	struct i40e_pf *pf = vf->pf;
	i40e_status aq_ret = 0;

2337
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}
2351

2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
	/* Use the queue bit map sent by the VF */
	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
				  false)) {
		aq_ret = I40E_ERR_TIMEOUT;
		goto error_param;
	}
	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
				  false)) {
		aq_ret = I40E_ERR_TIMEOUT;
		goto error_param;
	}
2363
error_param:
2364
	/* send the response to the VF */
2365
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
2366 2367 2368
				       aq_ret);
}

2369 2370 2371 2372 2373 2374
/**
 * i40e_vc_request_queues_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * VFs get a default number of queues but can use this message to request a
2375 2376 2377
 * different number.  If the request is successful, PF will reset the VF and
 * return 0.  If unsuccessful, PF will send message informing VF of number of
 * available queues and return result of sending VF a message.
2378
 **/
2379
static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg)
2380 2381 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
{
	struct virtchnl_vf_res_request *vfres =
		(struct virtchnl_vf_res_request *)msg;
	int req_pairs = vfres->num_queue_pairs;
	int cur_pairs = vf->num_queue_pairs;
	struct i40e_pf *pf = vf->pf;

	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
		return -EINVAL;

	if (req_pairs <= 0) {
		dev_err(&pf->pdev->dev,
			"VF %d tried to request %d queues.  Ignoring.\n",
			vf->vf_id, req_pairs);
	} else if (req_pairs > I40E_MAX_VF_QUEUES) {
		dev_err(&pf->pdev->dev,
			"VF %d tried to request more than %d queues.\n",
			vf->vf_id,
			I40E_MAX_VF_QUEUES);
		vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
	} else if (req_pairs - cur_pairs > pf->queues_left) {
		dev_warn(&pf->pdev->dev,
			 "VF %d requested %d more queues, but only %d left.\n",
			 vf->vf_id,
			 req_pairs - cur_pairs,
			 pf->queues_left);
		vfres->num_queue_pairs = pf->queues_left + cur_pairs;
	} else {
2408
		/* successful request */
2409
		vf->num_req_queues = req_pairs;
2410 2411 2412
		i40e_vc_notify_vf_reset(vf);
		i40e_reset_vf(vf, false);
		return 0;
2413 2414 2415
	}

	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
2416
				      (u8 *)vfres, sizeof(*vfres));
2417 2418
}

2419 2420
/**
 * i40e_vc_get_stats_msg
2421
 * @vf: pointer to the VF info
2422 2423
 * @msg: pointer to the msg buffer
 *
2424
 * called from the VF to get vsi stats
2425
 **/
2426
static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg)
2427
{
2428 2429
	struct virtchnl_queue_select *vqs =
	    (struct virtchnl_queue_select *)msg;
2430 2431 2432 2433 2434 2435 2436
	struct i40e_pf *pf = vf->pf;
	struct i40e_eth_stats stats;
	i40e_status aq_ret = 0;
	struct i40e_vsi *vsi;

	memset(&stats, 0, sizeof(struct i40e_eth_stats));

2437
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2438 2439 2440 2441 2442 2443 2444 2445 2446
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

2447
	vsi = pf->vsi[vf->lan_vsi_idx];
2448 2449 2450 2451 2452
	if (!vsi) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}
	i40e_update_eth_stats(vsi);
2453
	stats = vsi->eth_stats;
2454 2455

error_param:
2456
	/* send the response back to the VF */
2457
	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
2458 2459 2460
				      (u8 *)&stats, sizeof(stats));
}

2461 2462 2463 2464
/* If the VF is not trusted restrict the number of MAC/VLAN it can program
 * MAC filters: 16 for multicast, 1 for MAC, 1 for broadcast
 */
#define I40E_VC_MAX_MAC_ADDR_PER_VF (16 + 1 + 1)
2465 2466
#define I40E_VC_MAX_VLAN_PER_VF 8

2467 2468
/**
 * i40e_check_vf_permission
2469
 * @vf: pointer to the VF info
2470
 * @al: MAC address list from virtchnl
2471
 *
2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482
 * Check that the given list of MAC addresses is allowed. Will return -EPERM
 * if any address in the list is not valid. Checks the following conditions:
 *
 * 1) broadcast and zero addresses are never valid
 * 2) unicast addresses are not allowed if the VMM has administratively set
 *    the VF MAC address, unless the VF is marked as privileged.
 * 3) There is enough space to add all the addresses.
 *
 * Note that to guarantee consistency, it is expected this function be called
 * while holding the mac_filter_hash_lock, as otherwise the current number of
 * addresses might not be accurate.
2483
 **/
2484 2485
static inline int i40e_check_vf_permission(struct i40e_vf *vf,
					   struct virtchnl_ether_addr_list *al)
2486 2487
{
	struct i40e_pf *pf = vf->pf;
2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
	int i;

	/* If this VF is not privileged, then we can't add more than a limited
	 * number of addresses. Check to make sure that the additions do not
	 * push us over the limit.
	 */
	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
	    (vf->num_mac + al->num_elements) > I40E_VC_MAX_MAC_ADDR_PER_VF) {
		dev_err(&pf->pdev->dev,
			"Cannot add more MAC addresses, VF is not trusted, switch the VF to trusted to add more functionality\n");
		return -EPERM;
	}

	for (i = 0; i < al->num_elements; i++) {
		u8 *addr = al->list[i].addr;

		if (is_broadcast_ether_addr(addr) ||
		    is_zero_ether_addr(addr)) {
			dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
				addr);
			return I40E_ERR_INVALID_MAC_ADDR;
		}
2510 2511 2512 2513

		/* If the host VMM administrator has set the VF MAC address
		 * administratively via the ndo_set_vf_mac command then deny
		 * permission to the VF to add or delete unicast MAC addresses.
2514
		 * Unless the VF is privileged and then it can do whatever.
2515 2516
		 * The VF may request to set the MAC address filter already
		 * assigned to it so do not return an error in that case.
2517
		 */
2518 2519 2520 2521
		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
		    !is_multicast_ether_addr(addr) && vf->pf_set_mac &&
		    !ether_addr_equal(addr, vf->default_lan_addr.addr)) {
			dev_err(&pf->pdev->dev,
2522
				"VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
2523 2524
			return -EPERM;
		}
2525
	}
2526 2527

	return 0;
2528 2529
}

2530 2531
/**
 * i40e_vc_add_mac_addr_msg
2532
 * @vf: pointer to the VF info
2533 2534 2535 2536
 * @msg: pointer to the msg buffer
 *
 * add guest mac address filter
 **/
2537
static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
2538
{
2539 2540
	struct virtchnl_ether_addr_list *al =
	    (struct virtchnl_ether_addr_list *)msg;
2541 2542 2543
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = al->vsi_id;
2544
	i40e_status ret = 0;
2545 2546
	int i;

2547
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2548
	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2549
		ret = I40E_ERR_PARAM;
2550 2551 2552
		goto error_param;
	}

2553
	vsi = pf->vsi[vf->lan_vsi_idx];
2554

K
Kiran Patil 已提交
2555 2556 2557
	/* Lock once, because all function inside for loop accesses VSI's
	 * MAC filter list which needs to be protected using same lock.
	 */
2558
	spin_lock_bh(&vsi->mac_filter_hash_lock);
K
Kiran Patil 已提交
2559

2560 2561 2562 2563 2564 2565
	ret = i40e_check_vf_permission(vf, al);
	if (ret) {
		spin_unlock_bh(&vsi->mac_filter_hash_lock);
		goto error_param;
	}

2566 2567 2568 2569
	/* add new addresses to the list */
	for (i = 0; i < al->num_elements; i++) {
		struct i40e_mac_filter *f;

2570
		f = i40e_find_mac(vsi, al->list[i].addr);
2571
		if (!f) {
2572
			f = i40e_add_mac_filter(vsi, al->list[i].addr);
2573

2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
			if (!f) {
				dev_err(&pf->pdev->dev,
					"Unable to add MAC filter %pM for VF %d\n",
					al->list[i].addr, vf->vf_id);
				ret = I40E_ERR_PARAM;
				spin_unlock_bh(&vsi->mac_filter_hash_lock);
				goto error_param;
			} else {
				vf->num_mac++;
			}
2584 2585
		}
	}
2586
	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2587 2588

	/* program the updated filter list */
M
Mitch Williams 已提交
2589 2590 2591 2592
	ret = i40e_sync_vsi_filters(vsi);
	if (ret)
		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
			vf->vf_id, ret);
2593 2594

error_param:
2595
	/* send the response to the VF */
2596
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
2597
				       ret);
2598 2599 2600 2601
}

/**
 * i40e_vc_del_mac_addr_msg
2602
 * @vf: pointer to the VF info
2603 2604 2605 2606
 * @msg: pointer to the msg buffer
 *
 * remove guest mac address filter
 **/
2607
static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
2608
{
2609 2610
	struct virtchnl_ether_addr_list *al =
	    (struct virtchnl_ether_addr_list *)msg;
2611 2612 2613
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = al->vsi_id;
2614
	i40e_status ret = 0;
2615 2616
	int i;

2617
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2618
	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2619
		ret = I40E_ERR_PARAM;
2620 2621
		goto error_param;
	}
2622 2623

	for (i = 0; i < al->num_elements; i++) {
2624 2625
		if (is_broadcast_ether_addr(al->list[i].addr) ||
		    is_zero_ether_addr(al->list[i].addr)) {
2626 2627
			dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
				al->list[i].addr, vf->vf_id);
2628
			ret = I40E_ERR_INVALID_MAC_ADDR;
2629
			goto error_param;
2630
		}
2631 2632 2633 2634 2635 2636 2637 2638 2639 2640

		if (vf->pf_set_mac &&
		    ether_addr_equal(al->list[i].addr,
				     vf->default_lan_addr.addr)) {
			dev_err(&pf->pdev->dev,
				"MAC addr %pM has been set by PF, cannot delete it for VF %d, reset VF to change MAC addr\n",
				vf->default_lan_addr.addr, vf->vf_id);
			ret = I40E_ERR_PARAM;
			goto error_param;
		}
2641
	}
2642
	vsi = pf->vsi[vf->lan_vsi_idx];
2643

2644
	spin_lock_bh(&vsi->mac_filter_hash_lock);
2645 2646
	/* delete addresses from the list */
	for (i = 0; i < al->num_elements; i++)
2647
		if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
2648
			ret = I40E_ERR_INVALID_MAC_ADDR;
2649
			spin_unlock_bh(&vsi->mac_filter_hash_lock);
2650
			goto error_param;
2651 2652
		} else {
			vf->num_mac--;
2653 2654
		}

2655
	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2656 2657

	/* program the updated filter list */
M
Mitch Williams 已提交
2658 2659 2660 2661
	ret = i40e_sync_vsi_filters(vsi);
	if (ret)
		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
			vf->vf_id, ret);
2662 2663

error_param:
2664
	/* send the response to the VF */
2665
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
2666
				       ret);
2667 2668 2669 2670
}

/**
 * i40e_vc_add_vlan_msg
2671
 * @vf: pointer to the VF info
2672 2673 2674 2675
 * @msg: pointer to the msg buffer
 *
 * program guest vlan id
 **/
2676
static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg)
2677
{
2678 2679
	struct virtchnl_vlan_filter_list *vfl =
	    (struct virtchnl_vlan_filter_list *)msg;
2680 2681 2682 2683 2684 2685
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = vfl->vsi_id;
	i40e_status aq_ret = 0;
	int i;

2686 2687 2688 2689 2690 2691
	if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
		dev_err(&pf->pdev->dev,
			"VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
		goto error_param;
	}
2692
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	for (i = 0; i < vfl->num_elements; i++) {
		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
			aq_ret = I40E_ERR_PARAM;
			dev_err(&pf->pdev->dev,
				"invalid VF VLAN id %d\n", vfl->vlan_id[i]);
			goto error_param;
		}
	}
2706
	vsi = pf->vsi[vf->lan_vsi_idx];
2707 2708 2709 2710 2711 2712 2713 2714 2715
	if (vsi->info.pvid) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	i40e_vlan_stripping_enable(vsi);
	for (i = 0; i < vfl->num_elements; i++) {
		/* add new VLAN filter */
		int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
2716 2717
		if (!ret)
			vf->num_vlan++;
J
Jesse Brandeburg 已提交
2718

2719
		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2720 2721 2722 2723
			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
							   true,
							   vfl->vlan_id[i],
							   NULL);
2724
		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2725 2726 2727 2728 2729
			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
							   true,
							   vfl->vlan_id[i],
							   NULL);

2730 2731
		if (ret)
			dev_err(&pf->pdev->dev,
2732 2733
				"Unable to add VLAN filter %d for VF %d, error %d\n",
				vfl->vlan_id[i], vf->vf_id, ret);
2734 2735 2736
	}

error_param:
2737
	/* send the response to the VF */
2738
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
2739 2740 2741 2742
}

/**
 * i40e_vc_remove_vlan_msg
2743
 * @vf: pointer to the VF info
2744 2745 2746 2747
 * @msg: pointer to the msg buffer
 *
 * remove programmed guest vlan id
 **/
2748
static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg)
2749
{
2750 2751
	struct virtchnl_vlan_filter_list *vfl =
	    (struct virtchnl_vlan_filter_list *)msg;
2752 2753 2754 2755 2756 2757
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = vfl->vsi_id;
	i40e_status aq_ret = 0;
	int i;

2758
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	for (i = 0; i < vfl->num_elements; i++) {
		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
			aq_ret = I40E_ERR_PARAM;
			goto error_param;
		}
	}

2771
	vsi = pf->vsi[vf->lan_vsi_idx];
2772
	if (vsi->info.pvid) {
2773 2774
		if (vfl->num_elements > 1 || vfl->vlan_id[0])
			aq_ret = I40E_ERR_PARAM;
2775 2776 2777 2778
		goto error_param;
	}

	for (i = 0; i < vfl->num_elements; i++) {
F
Filip Sadowski 已提交
2779 2780
		i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
		vf->num_vlan--;
J
Jesse Brandeburg 已提交
2781

2782
		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2783 2784 2785 2786
			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
							   false,
							   vfl->vlan_id[i],
							   NULL);
2787
		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2788 2789 2790 2791
			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
							   false,
							   vfl->vlan_id[i],
							   NULL);
2792 2793 2794
	}

error_param:
2795
	/* send the response to the VF */
2796
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
2797 2798
}

2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812
/**
 * i40e_vc_iwarp_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * called from the VF for the iwarp msgs
 **/
static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
	struct i40e_pf *pf = vf->pf;
	int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
	i40e_status aq_ret = 0;

2813 2814
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2815 2816 2817 2818 2819 2820 2821 2822 2823
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
				     msg, msglen);

error_param:
	/* send the response to the VF */
2824
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835
				       aq_ret);
}

/**
 * i40e_vc_iwarp_qvmap_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 * @config: config qvmap or release it
 *
 * called from the VF for the iwarp msgs
 **/
2836
static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, bool config)
2837
{
2838 2839
	struct virtchnl_iwarp_qvlist_info *qvlist_info =
				(struct virtchnl_iwarp_qvlist_info *)msg;
2840 2841
	i40e_status aq_ret = 0;

2842 2843
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	if (config) {
		if (i40e_config_iwarp_qvlist(vf, qvlist_info))
			aq_ret = I40E_ERR_PARAM;
	} else {
		i40e_release_iwarp_qvlist(vf);
	}

error_param:
	/* send the response to the VF */
	return i40e_vc_send_resp_to_vf(vf,
2858 2859
			       config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
			       VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
2860 2861 2862
			       aq_ret);
}

2863 2864 2865 2866 2867 2868 2869
/**
 * i40e_vc_config_rss_key
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * Configure the VF's RSS key
 **/
2870
static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg)
2871
{
2872 2873
	struct virtchnl_rss_key *vrk =
		(struct virtchnl_rss_key *)msg;
2874 2875 2876 2877 2878
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = vrk->vsi_id;
	i40e_status aq_ret = 0;

2879
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
	    !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
	    (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	vsi = pf->vsi[vf->lan_vsi_idx];
	aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
err:
	/* send the response to the VF */
2890
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900
				       aq_ret);
}

/**
 * i40e_vc_config_rss_lut
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * Configure the VF's RSS LUT
 **/
2901
static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg)
2902
{
2903 2904
	struct virtchnl_rss_lut *vrl =
		(struct virtchnl_rss_lut *)msg;
2905 2906 2907 2908 2909
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = vrl->vsi_id;
	i40e_status aq_ret = 0;

2910
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2911 2912 2913 2914 2915 2916 2917 2918 2919 2920
	    !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
	    (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	vsi = pf->vsi[vf->lan_vsi_idx];
	aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
	/* send the response to the VF */
err:
2921
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
				       aq_ret);
}

/**
 * i40e_vc_get_rss_hena
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * Return the RSS HENA bits allowed by the hardware
 **/
2932
static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg)
2933
{
2934
	struct virtchnl_rss_hena *vrh = NULL;
2935 2936 2937 2938
	struct i40e_pf *pf = vf->pf;
	i40e_status aq_ret = 0;
	int len = 0;

2939
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2940 2941 2942
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}
2943
	len = sizeof(struct virtchnl_rss_hena);
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953

	vrh = kzalloc(len, GFP_KERNEL);
	if (!vrh) {
		aq_ret = I40E_ERR_NO_MEMORY;
		len = 0;
		goto err;
	}
	vrh->hena = i40e_pf_get_default_rss_hena(pf);
err:
	/* send the response back to the VF */
2954
	aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
2955
					aq_ret, (u8 *)vrh, len);
M
Mitch Williams 已提交
2956
	kfree(vrh);
2957 2958 2959 2960 2961 2962 2963 2964 2965 2966
	return aq_ret;
}

/**
 * i40e_vc_set_rss_hena
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * Set the RSS HENA bits for the VF
 **/
2967
static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg)
2968
{
2969 2970
	struct virtchnl_rss_hena *vrh =
		(struct virtchnl_rss_hena *)msg;
2971 2972 2973 2974
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	i40e_status aq_ret = 0;

2975
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2976 2977 2978 2979 2980 2981 2982 2983 2984
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}
	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
			  (u32)(vrh->hena >> 32));

	/* send the response to the VF */
err:
2985
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
2986 2987
}

2988 2989 2990 2991 2992 2993 2994
/**
 * i40e_vc_enable_vlan_stripping
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * Enable vlan header stripping for the VF
 **/
2995
static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019
{
	struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
	i40e_status aq_ret = 0;

	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	i40e_vlan_stripping_enable(vsi);

	/* send the response to the VF */
err:
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
				       aq_ret);
}

/**
 * i40e_vc_disable_vlan_stripping
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * Disable vlan header stripping for the VF
 **/
3020
static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037
{
	struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
	i40e_status aq_ret = 0;

	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	i40e_vlan_stripping_disable(vsi);

	/* send the response to the VF */
err:
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
				       aq_ret);
}

3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135
/**
 * i40e_validate_cloud_filter
 * @mask: mask for TC filter
 * @data: data for TC filter
 *
 * This function validates cloud filter programmed as TC filter for ADq
 **/
static int i40e_validate_cloud_filter(struct i40e_vf *vf,
				      struct virtchnl_filter *tc_filter)
{
	struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;
	struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	struct i40e_mac_filter *f;
	struct hlist_node *h;
	bool found = false;
	int bkt;

	if (!tc_filter->action) {
		dev_info(&pf->pdev->dev,
			 "VF %d: Currently ADq doesn't support Drop Action\n",
			 vf->vf_id);
		goto err;
	}

	/* action_meta is TC number here to which the filter is applied */
	if (!tc_filter->action_meta ||
	    tc_filter->action_meta > I40E_MAX_VF_VSI) {
		dev_info(&pf->pdev->dev, "VF %d: Invalid TC number %u\n",
			 vf->vf_id, tc_filter->action_meta);
		goto err;
	}

	/* Check filter if it's programmed for advanced mode or basic mode.
	 * There are two ADq modes (for VF only),
	 * 1. Basic mode: intended to allow as many filter options as possible
	 *		  to be added to a VF in Non-trusted mode. Main goal is
	 *		  to add filters to its own MAC and VLAN id.
	 * 2. Advanced mode: is for allowing filters to be applied other than
	 *		  its own MAC or VLAN. This mode requires the VF to be
	 *		  Trusted.
	 */
	if (mask.dst_mac[0] && !mask.dst_ip[0]) {
		vsi = pf->vsi[vf->lan_vsi_idx];
		f = i40e_find_mac(vsi, data.dst_mac);

		if (!f) {
			dev_info(&pf->pdev->dev,
				 "Destination MAC %pM doesn't belong to VF %d\n",
				 data.dst_mac, vf->vf_id);
			goto err;
		}

		if (mask.vlan_id) {
			hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
					   hlist) {
				if (f->vlan == ntohs(data.vlan_id)) {
					found = true;
					break;
				}
			}
			if (!found) {
				dev_info(&pf->pdev->dev,
					 "VF %d doesn't have any VLAN id %u\n",
					 vf->vf_id, ntohs(data.vlan_id));
				goto err;
			}
		}
	} else {
		/* Check if VF is trusted */
		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
			dev_err(&pf->pdev->dev,
				"VF %d not trusted, make VF trusted to add advanced mode ADq cloud filters\n",
				vf->vf_id);
			return I40E_ERR_CONFIG;
		}
	}

	if (mask.dst_mac[0] & data.dst_mac[0]) {
		if (is_broadcast_ether_addr(data.dst_mac) ||
		    is_zero_ether_addr(data.dst_mac)) {
			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest MAC addr %pM\n",
				 vf->vf_id, data.dst_mac);
			goto err;
		}
	}

	if (mask.src_mac[0] & data.src_mac[0]) {
		if (is_broadcast_ether_addr(data.src_mac) ||
		    is_zero_ether_addr(data.src_mac)) {
			dev_info(&pf->pdev->dev, "VF %d: Invalid Source MAC addr %pM\n",
				 vf->vf_id, data.src_mac);
			goto err;
		}
	}

	if (mask.dst_port & data.dst_port) {
3136
		if (!data.dst_port) {
3137 3138 3139 3140 3141 3142 3143
			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest port\n",
				 vf->vf_id);
			goto err;
		}
	}

	if (mask.src_port & data.src_port) {
3144
		if (!data.src_port) {
3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
			dev_info(&pf->pdev->dev, "VF %d: Invalid Source port\n",
				 vf->vf_id);
			goto err;
		}
	}

	if (tc_filter->flow_type != VIRTCHNL_TCP_V6_FLOW &&
	    tc_filter->flow_type != VIRTCHNL_TCP_V4_FLOW) {
		dev_info(&pf->pdev->dev, "VF %d: Invalid Flow type\n",
			 vf->vf_id);
		goto err;
	}

	if (mask.vlan_id & data.vlan_id) {
		if (ntohs(data.vlan_id) > I40E_MAX_VLANID) {
			dev_info(&pf->pdev->dev, "VF %d: invalid VLAN ID\n",
				 vf->vf_id);
			goto err;
		}
	}

	return I40E_SUCCESS;
err:
	return I40E_ERR_CONFIG;
}

/**
 * i40e_find_vsi_from_seid - searches for the vsi with the given seid
 * @vf: pointer to the VF info
 * @seid - seid of the vsi it is searching for
 **/
static struct i40e_vsi *i40e_find_vsi_from_seid(struct i40e_vf *vf, u16 seid)
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	int i;

	for (i = 0; i < vf->num_tc ; i++) {
		vsi = i40e_find_vsi_from_id(pf, vf->ch[i].vsi_id);
3184
		if (vsi && vsi->seid == seid)
3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267
			return vsi;
	}
	return NULL;
}

/**
 * i40e_del_all_cloud_filters
 * @vf: pointer to the VF info
 *
 * This function deletes all cloud filters
 **/
static void i40e_del_all_cloud_filters(struct i40e_vf *vf)
{
	struct i40e_cloud_filter *cfilter = NULL;
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	struct hlist_node *node;
	int ret;

	hlist_for_each_entry_safe(cfilter, node,
				  &vf->cloud_filter_list, cloud_node) {
		vsi = i40e_find_vsi_from_seid(vf, cfilter->seid);

		if (!vsi) {
			dev_err(&pf->pdev->dev, "VF %d: no VSI found for matching %u seid, can't delete cloud filter\n",
				vf->vf_id, cfilter->seid);
			continue;
		}

		if (cfilter->dst_port)
			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
								false);
		else
			ret = i40e_add_del_cloud_filter(vsi, cfilter, false);
		if (ret)
			dev_err(&pf->pdev->dev,
				"VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
				vf->vf_id, i40e_stat_str(&pf->hw, ret),
				i40e_aq_str(&pf->hw,
					    pf->hw.aq.asq_last_status));

		hlist_del(&cfilter->cloud_node);
		kfree(cfilter);
		vf->num_cloud_filters--;
	}
}

/**
 * i40e_vc_del_cloud_filter
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * This function deletes a cloud filter programmed as TC filter for ADq
 **/
static int i40e_vc_del_cloud_filter(struct i40e_vf *vf, u8 *msg)
{
	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
	struct i40e_cloud_filter cfilter, *cf = NULL;
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	struct hlist_node *node;
	i40e_status aq_ret = 0;
	int i, ret;

	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	if (!vf->adq_enabled) {
		dev_info(&pf->pdev->dev,
			 "VF %d: ADq not enabled, can't apply cloud filter\n",
			 vf->vf_id);
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	if (i40e_validate_cloud_filter(vf, vcf)) {
		dev_info(&pf->pdev->dev,
			 "VF %d: Invalid input, can't apply cloud filter\n",
			 vf->vf_id);
3268 3269
		aq_ret = I40E_ERR_PARAM;
		goto err;
3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 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 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 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398
	}

	memset(&cfilter, 0, sizeof(cfilter));
	/* parse destination mac address */
	for (i = 0; i < ETH_ALEN; i++)
		cfilter.dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];

	/* parse source mac address */
	for (i = 0; i < ETH_ALEN; i++)
		cfilter.src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];

	cfilter.vlan_id = mask.vlan_id & tcf.vlan_id;
	cfilter.dst_port = mask.dst_port & tcf.dst_port;
	cfilter.src_port = mask.src_port & tcf.src_port;

	switch (vcf->flow_type) {
	case VIRTCHNL_TCP_V4_FLOW:
		cfilter.n_proto = ETH_P_IP;
		if (mask.dst_ip[0] & tcf.dst_ip[0])
			memcpy(&cfilter.ip.v4.dst_ip, tcf.dst_ip,
			       ARRAY_SIZE(tcf.dst_ip));
		else if (mask.src_ip[0] & tcf.dst_ip[0])
			memcpy(&cfilter.ip.v4.src_ip, tcf.src_ip,
			       ARRAY_SIZE(tcf.dst_ip));
		break;
	case VIRTCHNL_TCP_V6_FLOW:
		cfilter.n_proto = ETH_P_IPV6;
		if (mask.dst_ip[3] & tcf.dst_ip[3])
			memcpy(&cfilter.ip.v6.dst_ip6, tcf.dst_ip,
			       sizeof(cfilter.ip.v6.dst_ip6));
		if (mask.src_ip[3] & tcf.src_ip[3])
			memcpy(&cfilter.ip.v6.src_ip6, tcf.src_ip,
			       sizeof(cfilter.ip.v6.src_ip6));
		break;
	default:
		/* TC filter can be configured based on different combinations
		 * and in this case IP is not a part of filter config
		 */
		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
			 vf->vf_id);
	}

	/* get the vsi to which the tc belongs to */
	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
	cfilter.seid = vsi->seid;
	cfilter.flags = vcf->field_flags;

	/* Deleting TC filter */
	if (tcf.dst_port)
		ret = i40e_add_del_cloud_filter_big_buf(vsi, &cfilter, false);
	else
		ret = i40e_add_del_cloud_filter(vsi, &cfilter, false);
	if (ret) {
		dev_err(&pf->pdev->dev,
			"VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
			vf->vf_id, i40e_stat_str(&pf->hw, ret),
			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
		goto err;
	}

	hlist_for_each_entry_safe(cf, node,
				  &vf->cloud_filter_list, cloud_node) {
		if (cf->seid != cfilter.seid)
			continue;
		if (mask.dst_port)
			if (cfilter.dst_port != cf->dst_port)
				continue;
		if (mask.dst_mac[0])
			if (!ether_addr_equal(cf->src_mac, cfilter.src_mac))
				continue;
		/* for ipv4 data to be valid, only first byte of mask is set */
		if (cfilter.n_proto == ETH_P_IP && mask.dst_ip[0])
			if (memcmp(&cfilter.ip.v4.dst_ip, &cf->ip.v4.dst_ip,
				   ARRAY_SIZE(tcf.dst_ip)))
				continue;
		/* for ipv6, mask is set for all sixteen bytes (4 words) */
		if (cfilter.n_proto == ETH_P_IPV6 && mask.dst_ip[3])
			if (memcmp(&cfilter.ip.v6.dst_ip6, &cf->ip.v6.dst_ip6,
				   sizeof(cfilter.ip.v6.src_ip6)))
				continue;
		if (mask.vlan_id)
			if (cfilter.vlan_id != cf->vlan_id)
				continue;

		hlist_del(&cf->cloud_node);
		kfree(cf);
		vf->num_cloud_filters--;
	}

err:
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_CLOUD_FILTER,
				       aq_ret);
}

/**
 * i40e_vc_add_cloud_filter
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 *
 * This function adds a cloud filter programmed as TC filter for ADq
 **/
static int i40e_vc_add_cloud_filter(struct i40e_vf *vf, u8 *msg)
{
	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
	struct i40e_cloud_filter *cfilter = NULL;
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	i40e_status aq_ret = 0;
	int i, ret;

	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	if (!vf->adq_enabled) {
		dev_info(&pf->pdev->dev,
			 "VF %d: ADq is not enabled, can't apply cloud filter\n",
			 vf->vf_id);
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	if (i40e_validate_cloud_filter(vf, vcf)) {
		dev_info(&pf->pdev->dev,
			 "VF %d: Invalid input/s, can't apply cloud filter\n",
			 vf->vf_id);
3399 3400
		aq_ret = I40E_ERR_PARAM;
		goto err;
3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
	}

	cfilter = kzalloc(sizeof(*cfilter), GFP_KERNEL);
	if (!cfilter)
		return -ENOMEM;

	/* parse destination mac address */
	for (i = 0; i < ETH_ALEN; i++)
		cfilter->dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];

	/* parse source mac address */
	for (i = 0; i < ETH_ALEN; i++)
		cfilter->src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];

	cfilter->vlan_id = mask.vlan_id & tcf.vlan_id;
	cfilter->dst_port = mask.dst_port & tcf.dst_port;
	cfilter->src_port = mask.src_port & tcf.src_port;

	switch (vcf->flow_type) {
	case VIRTCHNL_TCP_V4_FLOW:
		cfilter->n_proto = ETH_P_IP;
		if (mask.dst_ip[0] & tcf.dst_ip[0])
			memcpy(&cfilter->ip.v4.dst_ip, tcf.dst_ip,
			       ARRAY_SIZE(tcf.dst_ip));
		else if (mask.src_ip[0] & tcf.dst_ip[0])
			memcpy(&cfilter->ip.v4.src_ip, tcf.src_ip,
			       ARRAY_SIZE(tcf.dst_ip));
		break;
	case VIRTCHNL_TCP_V6_FLOW:
		cfilter->n_proto = ETH_P_IPV6;
		if (mask.dst_ip[3] & tcf.dst_ip[3])
			memcpy(&cfilter->ip.v6.dst_ip6, tcf.dst_ip,
			       sizeof(cfilter->ip.v6.dst_ip6));
		if (mask.src_ip[3] & tcf.src_ip[3])
			memcpy(&cfilter->ip.v6.src_ip6, tcf.src_ip,
			       sizeof(cfilter->ip.v6.src_ip6));
		break;
	default:
		/* TC filter can be configured based on different combinations
		 * and in this case IP is not a part of filter config
		 */
		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
			 vf->vf_id);
	}

	/* get the VSI to which the TC belongs to */
	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
	cfilter->seid = vsi->seid;
	cfilter->flags = vcf->field_flags;

	/* Adding cloud filter programmed as TC filter */
	if (tcf.dst_port)
		ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, true);
	else
		ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
	if (ret) {
		dev_err(&pf->pdev->dev,
			"VF %d: Failed to add cloud filter, err %s aq_err %s\n",
			vf->vf_id, i40e_stat_str(&pf->hw, ret),
			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
		goto err;
	}

	INIT_HLIST_NODE(&cfilter->cloud_node);
	hlist_add_head(&cfilter->cloud_node, &vf->cloud_filter_list);
	vf->num_cloud_filters++;
err:
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_CLOUD_FILTER,
				       aq_ret);
}

3472 3473 3474 3475 3476 3477 3478 3479 3480 3481
/**
 * i40e_vc_add_qch_msg: Add queue channel and enable ADq
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 **/
static int i40e_vc_add_qch_msg(struct i40e_vf *vf, u8 *msg)
{
	struct virtchnl_tc_info *tci =
		(struct virtchnl_tc_info *)msg;
	struct i40e_pf *pf = vf->pf;
3482 3483
	struct i40e_link_status *ls = &pf->hw.phy.link_info;
	int i, adq_request_qps = 0, speed = 0;
3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 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
	i40e_status aq_ret = 0;

	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	/* ADq cannot be applied if spoof check is ON */
	if (vf->spoofchk) {
		dev_err(&pf->pdev->dev,
			"Spoof check is ON, turn it OFF to enable ADq\n");
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)) {
		dev_err(&pf->pdev->dev,
			"VF %d attempting to enable ADq, but hasn't properly negotiated that capability\n",
			vf->vf_id);
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	/* max number of traffic classes for VF currently capped at 4 */
	if (!tci->num_tc || tci->num_tc > I40E_MAX_VF_VSI) {
		dev_err(&pf->pdev->dev,
			"VF %d trying to set %u TCs, valid range 1-4 TCs per VF\n",
			vf->vf_id, tci->num_tc);
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	/* validate queues for each TC */
	for (i = 0; i < tci->num_tc; i++)
		if (!tci->list[i].count ||
		    tci->list[i].count > I40E_DEFAULT_QUEUES_PER_VF) {
			dev_err(&pf->pdev->dev,
				"VF %d: TC %d trying to set %u queues, valid range 1-4 queues per TC\n",
				vf->vf_id, i, tci->list[i].count);
			aq_ret = I40E_ERR_PARAM;
			goto err;
		}

	/* need Max VF queues but already have default number of queues */
	adq_request_qps = I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF;

	if (pf->queues_left < adq_request_qps) {
		dev_err(&pf->pdev->dev,
			"No queues left to allocate to VF %d\n",
			vf->vf_id);
		aq_ret = I40E_ERR_PARAM;
		goto err;
	} else {
		/* we need to allocate max VF queues to enable ADq so as to
		 * make sure ADq enabled VF always gets back queues when it
		 * goes through a reset.
		 */
		vf->num_queue_pairs = I40E_MAX_VF_QUEUES;
	}

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
	/* get link speed in MB to validate rate limit */
	switch (ls->link_speed) {
	case VIRTCHNL_LINK_SPEED_100MB:
		speed = SPEED_100;
		break;
	case VIRTCHNL_LINK_SPEED_1GB:
		speed = SPEED_1000;
		break;
	case VIRTCHNL_LINK_SPEED_10GB:
		speed = SPEED_10000;
		break;
	case VIRTCHNL_LINK_SPEED_20GB:
		speed = SPEED_20000;
		break;
	case VIRTCHNL_LINK_SPEED_25GB:
		speed = SPEED_25000;
		break;
	case VIRTCHNL_LINK_SPEED_40GB:
		speed = SPEED_40000;
		break;
	default:
		dev_err(&pf->pdev->dev,
			"Cannot detect link speed\n");
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

3571 3572
	/* parse data from the queue channel info */
	vf->num_tc = tci->num_tc;
3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586
	for (i = 0; i < vf->num_tc; i++) {
		if (tci->list[i].max_tx_rate) {
			if (tci->list[i].max_tx_rate > speed) {
				dev_err(&pf->pdev->dev,
					"Invalid max tx rate %llu specified for VF %d.",
					tci->list[i].max_tx_rate,
					vf->vf_id);
				aq_ret = I40E_ERR_PARAM;
				goto err;
			} else {
				vf->ch[i].max_tx_rate =
					tci->list[i].max_tx_rate;
			}
		}
3587
		vf->ch[i].num_qps = tci->list[i].count;
3588
	}
3589 3590 3591

	/* set this flag only after making sure all inputs are sane */
	vf->adq_enabled = true;
3592 3593 3594 3595 3596
	/* num_req_queues is set when user changes number of queues via ethtool
	 * and this causes issue for default VSI(which depends on this variable)
	 * when ADq is enabled, hence reset it.
	 */
	vf->num_req_queues = 0;
3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609

	/* reset the VF in order to allocate resources */
	i40e_vc_notify_vf_reset(vf);
	i40e_reset_vf(vf, false);

	return I40E_SUCCESS;

	/* send the response to the VF */
err:
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_CHANNELS,
				       aq_ret);
}

3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625
/**
 * i40e_vc_del_qch_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 **/
static int i40e_vc_del_qch_msg(struct i40e_vf *vf, u8 *msg)
{
	struct i40e_pf *pf = vf->pf;
	i40e_status aq_ret = 0;

	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

	if (vf->adq_enabled) {
3626
		i40e_del_all_cloud_filters(vf);
3627 3628 3629 3630
		i40e_del_qch(vf);
		vf->adq_enabled = false;
		vf->num_tc = 0;
		dev_info(&pf->pdev->dev,
3631
			 "Deleting Queue Channels and cloud filters for ADq on VF %d\n",
3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649
			 vf->vf_id);
	} else {
		dev_info(&pf->pdev->dev, "VF %d trying to delete queue channels but ADq isn't enabled\n",
			 vf->vf_id);
		aq_ret = I40E_ERR_PARAM;
	}

	/* reset the VF in order to allocate resources */
	i40e_vc_notify_vf_reset(vf);
	i40e_reset_vf(vf, false);

	return I40E_SUCCESS;

err:
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_CHANNELS,
				       aq_ret);
}

3650 3651
/**
 * i40e_vc_process_vf_msg
3652 3653
 * @pf: pointer to the PF structure
 * @vf_id: source VF id
3654 3655
 * @v_opcode: operation code
 * @v_retval: unused return value code
3656 3657 3658 3659
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * called from the common aeq/arq handler to
3660
 * process request from VF
3661
 **/
3662
int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
3663
			   u32 __always_unused v_retval, u8 *msg, u16 msglen)
3664 3665
{
	struct i40e_hw *hw = &pf->hw;
3666
	int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
3667
	struct i40e_vf *vf;
3668 3669 3670
	int ret;

	pf->vf_aq_requests++;
3671
	if (local_vf_id < 0 || local_vf_id >= pf->num_alloc_vfs)
3672
		return -EINVAL;
3673
	vf = &(pf->vf[local_vf_id]);
3674 3675 3676 3677 3678

	/* Check if VF is disabled. */
	if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
		return I40E_ERR_PARAM;

3679
	/* perform basic checks on the msg */
3680
	ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
3681

3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694
	/* perform additional checks specific to this driver */
	if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
		struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;

		if (vrk->key_len != I40E_HKEY_ARRAY_SIZE)
			ret = -EINVAL;
	} else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
		struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;

		if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)
			ret = -EINVAL;
	}

3695
	if (ret) {
3696
		i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
3697
		dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
3698
			local_vf_id, v_opcode, msglen);
3699
		switch (ret) {
M
Mitch Williams 已提交
3700
		case VIRTCHNL_STATUS_ERR_PARAM:
3701 3702 3703 3704
			return -EPERM;
		default:
			return -EINVAL;
		}
3705
	}
3706

3707
	switch (v_opcode) {
3708
	case VIRTCHNL_OP_VERSION:
3709
		ret = i40e_vc_get_version_msg(vf, msg);
3710
		break;
3711
	case VIRTCHNL_OP_GET_VF_RESOURCES:
3712
		ret = i40e_vc_get_vf_resources_msg(vf, msg);
3713
		i40e_vc_notify_vf_link_state(vf);
3714
		break;
3715
	case VIRTCHNL_OP_RESET_VF:
M
Mitch Williams 已提交
3716 3717
		i40e_vc_reset_vf_msg(vf);
		ret = 0;
3718
		break;
3719
	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
3720
		ret = i40e_vc_config_promiscuous_mode_msg(vf, msg);
3721
		break;
3722
	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
3723
		ret = i40e_vc_config_queues_msg(vf, msg);
3724
		break;
3725
	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
3726
		ret = i40e_vc_config_irq_map_msg(vf, msg);
3727
		break;
3728
	case VIRTCHNL_OP_ENABLE_QUEUES:
3729
		ret = i40e_vc_enable_queues_msg(vf, msg);
M
Mitch Williams 已提交
3730
		i40e_vc_notify_vf_link_state(vf);
3731
		break;
3732
	case VIRTCHNL_OP_DISABLE_QUEUES:
3733
		ret = i40e_vc_disable_queues_msg(vf, msg);
3734
		break;
3735
	case VIRTCHNL_OP_ADD_ETH_ADDR:
3736
		ret = i40e_vc_add_mac_addr_msg(vf, msg);
3737
		break;
3738
	case VIRTCHNL_OP_DEL_ETH_ADDR:
3739
		ret = i40e_vc_del_mac_addr_msg(vf, msg);
3740
		break;
3741
	case VIRTCHNL_OP_ADD_VLAN:
3742
		ret = i40e_vc_add_vlan_msg(vf, msg);
3743
		break;
3744
	case VIRTCHNL_OP_DEL_VLAN:
3745
		ret = i40e_vc_remove_vlan_msg(vf, msg);
3746
		break;
3747
	case VIRTCHNL_OP_GET_STATS:
3748
		ret = i40e_vc_get_stats_msg(vf, msg);
3749
		break;
3750
	case VIRTCHNL_OP_IWARP:
3751 3752
		ret = i40e_vc_iwarp_msg(vf, msg, msglen);
		break;
3753
	case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
3754
		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, true);
3755
		break;
3756
	case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
3757
		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, false);
3758
		break;
3759
	case VIRTCHNL_OP_CONFIG_RSS_KEY:
3760
		ret = i40e_vc_config_rss_key(vf, msg);
3761
		break;
3762
	case VIRTCHNL_OP_CONFIG_RSS_LUT:
3763
		ret = i40e_vc_config_rss_lut(vf, msg);
3764
		break;
3765
	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
3766
		ret = i40e_vc_get_rss_hena(vf, msg);
3767
		break;
3768
	case VIRTCHNL_OP_SET_RSS_HENA:
3769
		ret = i40e_vc_set_rss_hena(vf, msg);
3770
		break;
3771
	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
3772
		ret = i40e_vc_enable_vlan_stripping(vf, msg);
3773 3774
		break;
	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
3775
		ret = i40e_vc_disable_vlan_stripping(vf, msg);
3776
		break;
3777
	case VIRTCHNL_OP_REQUEST_QUEUES:
3778
		ret = i40e_vc_request_queues_msg(vf, msg);
3779
		break;
3780 3781 3782
	case VIRTCHNL_OP_ENABLE_CHANNELS:
		ret = i40e_vc_add_qch_msg(vf, msg);
		break;
3783 3784 3785
	case VIRTCHNL_OP_DISABLE_CHANNELS:
		ret = i40e_vc_del_qch_msg(vf, msg);
		break;
3786 3787 3788 3789 3790 3791
	case VIRTCHNL_OP_ADD_CLOUD_FILTER:
		ret = i40e_vc_add_cloud_filter(vf, msg);
		break;
	case VIRTCHNL_OP_DEL_CLOUD_FILTER:
		ret = i40e_vc_del_cloud_filter(vf, msg);
		break;
3792
	case VIRTCHNL_OP_UNKNOWN:
3793
	default:
3794
		dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
3795
			v_opcode, local_vf_id);
3796 3797 3798 3799 3800 3801 3802 3803 3804 3805
		ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
					      I40E_ERR_NOT_IMPLEMENTED);
		break;
	}

	return ret;
}

/**
 * i40e_vc_process_vflr_event
3806
 * @pf: pointer to the PF structure
3807 3808
 *
 * called from the vlfr irq handler to
3809
 * free up VF resources and state variables
3810 3811 3812 3813
 **/
int i40e_vc_process_vflr_event(struct i40e_pf *pf)
{
	struct i40e_hw *hw = &pf->hw;
3814
	u32 reg, reg_idx, bit_idx;
3815
	struct i40e_vf *vf;
3816
	int vf_id;
3817

3818
	if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
3819 3820
		return 0;

M
Mitch Williams 已提交
3821 3822 3823 3824 3825
	/* Re-enable the VFLR interrupt cause here, before looking for which
	 * VF got reset. Otherwise, if another VF gets a reset while the
	 * first one is being processed, that interrupt will be lost, and
	 * that VF will be stuck in reset forever.
	 */
3826 3827 3828 3829 3830
	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
	reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
	i40e_flush(hw);

3831
	clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3832 3833 3834
	for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
		reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
		bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
3835
		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
3836 3837
		vf = &pf->vf[vf_id];
		reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
3838
		if (reg & BIT(bit_idx))
3839
			/* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
3840
			i40e_reset_vf(vf, true);
3841 3842 3843 3844 3845
	}

	return 0;
}

3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874
/**
 * i40e_validate_vf
 * @pf: the physical function
 * @vf_id: VF identifier
 *
 * Check that the VF is enabled and the VSI exists.
 *
 * Returns 0 on success, negative on failure
 **/
static int i40e_validate_vf(struct i40e_pf *pf, int vf_id)
{
	struct i40e_vsi *vsi;
	struct i40e_vf *vf;
	int ret = 0;

	if (vf_id >= pf->num_alloc_vfs) {
		dev_err(&pf->pdev->dev,
			"Invalid VF Identifier %d\n", vf_id);
		ret = -EINVAL;
		goto err_out;
	}
	vf = &pf->vf[vf_id];
	vsi = i40e_find_vsi_from_id(pf, vf->lan_vsi_id);
	if (!vsi)
		ret = -EINVAL;
err_out:
	return ret;
}

3875 3876 3877
/**
 * i40e_ndo_set_vf_mac
 * @netdev: network interface device structure
3878
 * @vf_id: VF identifier
3879 3880
 * @mac: mac address
 *
3881
 * program VF mac address
3882 3883 3884 3885 3886 3887 3888 3889 3890
 **/
int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
{
	struct i40e_netdev_priv *np = netdev_priv(netdev);
	struct i40e_vsi *vsi = np->vsi;
	struct i40e_pf *pf = vsi->back;
	struct i40e_mac_filter *f;
	struct i40e_vf *vf;
	int ret = 0;
3891
	struct hlist_node *h;
3892
	int bkt;
3893
	u8 i;
3894 3895

	/* validate the request */
3896 3897
	ret = i40e_validate_vf(pf, vf_id);
	if (ret)
3898 3899
		goto error_param;

3900
	vf = &pf->vf[vf_id];
3901
	vsi = pf->vsi[vf->lan_vsi_idx];
3902 3903 3904 3905 3906 3907 3908 3909 3910 3911

	/* When the VF is resetting wait until it is done.
	 * It can take up to 200 milliseconds,
	 * but wait for up to 300 milliseconds to be safe.
	 */
	for (i = 0; i < 15; i++) {
		if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states))
			break;
		msleep(20);
	}
3912
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3913 3914 3915
		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
			vf_id);
		ret = -EAGAIN;
3916 3917 3918
		goto error_param;
	}

3919 3920 3921 3922 3923
	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
		return -EAGAIN;
	}

3924
	if (is_multicast_ether_addr(mac)) {
3925
		dev_err(&pf->pdev->dev,
3926
			"Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
3927 3928 3929 3930
		ret = -EINVAL;
		goto error_param;
	}

K
Kiran Patil 已提交
3931
	/* Lock once because below invoked function add/del_filter requires
3932
	 * mac_filter_hash_lock to be held
K
Kiran Patil 已提交
3933
	 */
3934
	spin_lock_bh(&vsi->mac_filter_hash_lock);
K
Kiran Patil 已提交
3935

3936
	/* delete the temporary mac address */
3937
	if (!is_zero_ether_addr(vf->default_lan_addr.addr))
3938
		i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
3939

3940 3941 3942
	/* Delete all the filters for this VSI - we're going to kill it
	 * anyway.
	 */
3943
	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
3944
		__i40e_del_filter(vsi, f);
3945

3946
	spin_unlock_bh(&vsi->mac_filter_hash_lock);
K
Kiran Patil 已提交
3947

3948
	/* program mac filter */
J
Jesse Brandeburg 已提交
3949
	if (i40e_sync_vsi_filters(vsi)) {
3950 3951 3952 3953
		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
		ret = -EIO;
		goto error_param;
	}
3954
	ether_addr_copy(vf->default_lan_addr.addr, mac);
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964

	if (is_zero_ether_addr(mac)) {
		vf->pf_set_mac = false;
		dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
	} else {
		vf->pf_set_mac = true;
		dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
			 mac, vf_id);
	}

3965 3966 3967
	/* Force the VF interface down so it has to bring up with new MAC
	 * address
	 */
3968
	i40e_vc_disable_vf(vf);
3969
	dev_info(&pf->pdev->dev, "Bring down and up the VF interface to make this change effective.\n");
3970 3971

error_param:
3972
	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
3973 3974 3975
	return ret;
}

3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003
/**
 * i40e_vsi_has_vlans - True if VSI has configured VLANs
 * @vsi: pointer to the vsi
 *
 * Check if a VSI has configured any VLANs. False if we have a port VLAN or if
 * we have no configured VLANs. Do not call while holding the
 * mac_filter_hash_lock.
 */
static bool i40e_vsi_has_vlans(struct i40e_vsi *vsi)
{
	bool have_vlans;

	/* If we have a port VLAN, then the VSI cannot have any VLANs
	 * configured, as all MAC/VLAN filters will be assigned to the PVID.
	 */
	if (vsi->info.pvid)
		return false;

	/* Since we don't have a PVID, we know that if the device is in VLAN
	 * mode it must be because of a VLAN filter configured on this VSI.
	 */
	spin_lock_bh(&vsi->mac_filter_hash_lock);
	have_vlans = i40e_is_vsi_in_vlan(vsi);
	spin_unlock_bh(&vsi->mac_filter_hash_lock);

	return have_vlans;
}

4004 4005 4006
/**
 * i40e_ndo_set_vf_port_vlan
 * @netdev: network interface device structure
4007
 * @vf_id: VF identifier
4008 4009
 * @vlan_id: mac address
 * @qos: priority setting
4010
 * @vlan_proto: vlan protocol
4011
 *
4012
 * program VF vlan id and/or qos
4013
 **/
4014 4015
int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
			      u16 vlan_id, u8 qos, __be16 vlan_proto)
4016
{
4017
	u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
4018
	struct i40e_netdev_priv *np = netdev_priv(netdev);
4019
	bool allmulti = false, alluni = false;
4020 4021 4022 4023 4024
	struct i40e_pf *pf = np->vsi->back;
	struct i40e_vsi *vsi;
	struct i40e_vf *vf;
	int ret = 0;

4025 4026 4027 4028 4029
	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
		return -EAGAIN;
	}

4030
	/* validate the request */
4031 4032
	ret = i40e_validate_vf(pf, vf_id);
	if (ret)
4033 4034 4035 4036 4037 4038 4039 4040
		goto error_pvid;

	if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
		dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
		ret = -EINVAL;
		goto error_pvid;
	}

4041 4042 4043 4044 4045 4046
	if (vlan_proto != htons(ETH_P_8021Q)) {
		dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
		ret = -EPROTONOSUPPORT;
		goto error_pvid;
	}

4047
	vf = &pf->vf[vf_id];
4048
	vsi = pf->vsi[vf->lan_vsi_idx];
4049
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4050 4051 4052
		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
			vf_id);
		ret = -EAGAIN;
4053 4054 4055
		goto error_pvid;
	}

4056
	if (le16_to_cpu(vsi->info.pvid) == vlanprio)
4057 4058 4059
		/* duplicate request, so just return success */
		goto error_pvid;

4060
	if (i40e_vsi_has_vlans(vsi)) {
4061 4062 4063
		dev_err(&pf->pdev->dev,
			"VF %d has already configured VLAN filters and the administrator is requesting a port VLAN override.\nPlease unload and reload the VF driver for this change to take effect.\n",
			vf_id);
4064 4065 4066 4067
		/* Administrator Error - knock the VF offline until he does
		 * the right thing by reconfiguring his network correctly
		 * and then reloading the VF driver.
		 */
4068
		i40e_vc_disable_vf(vf);
M
Mitch Williams 已提交
4069 4070
		/* During reset the VF got a new VSI, so refresh the pointer. */
		vsi = pf->vsi[vf->lan_vsi_idx];
4071
	}
4072

4073 4074 4075
	/* Locked once because multiple functions below iterate list */
	spin_lock_bh(&vsi->mac_filter_hash_lock);

4076 4077
	/* Check for condition where there was already a port VLAN ID
	 * filter set and now it is being deleted by setting it to zero.
4078 4079
	 * Additionally check for the condition where there was a port
	 * VLAN but now there is a new and different port VLAN being set.
4080 4081 4082 4083
	 * Before deleting all the old VLAN filters we must add new ones
	 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
	 * MAC addresses deleted.
	 */
4084
	if ((!(vlan_id || qos) ||
4085
	    vlanprio != le16_to_cpu(vsi->info.pvid)) &&
4086 4087 4088 4089 4090 4091 4092 4093 4094 4095
	    vsi->info.pvid) {
		ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
		if (ret) {
			dev_info(&vsi->back->pdev->dev,
				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
				 vsi->back->hw.aq.asq_last_status);
			spin_unlock_bh(&vsi->mac_filter_hash_lock);
			goto error_pvid;
		}
	}
4096

4097
	if (vsi->info.pvid) {
4098 4099 4100
		/* remove all filters on the old VLAN */
		i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
					   VLAN_VID_MASK));
4101
	}
4102

J
Jia-Ju Bai 已提交
4103
	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4104 4105 4106 4107 4108 4109 4110 4111 4112

	/* disable promisc modes in case they were enabled */
	ret = i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id,
					      allmulti, alluni);
	if (ret) {
		dev_err(&pf->pdev->dev, "Unable to config VF promiscuous mode\n");
		goto error_pvid;
	}

4113
	if (vlan_id || qos)
4114
		ret = i40e_vsi_add_pvid(vsi, vlanprio);
4115
	else
G
Greg Rose 已提交
4116
		i40e_vsi_remove_pvid(vsi);
J
Jia-Ju Bai 已提交
4117
	spin_lock_bh(&vsi->mac_filter_hash_lock);
4118 4119 4120 4121 4122

	if (vlan_id) {
		dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
			 vlan_id, qos, vf_id);

4123 4124
		/* add new VLAN filter for each MAC */
		ret = i40e_add_vlan_all_mac(vsi, vlan_id);
4125 4126 4127 4128
		if (ret) {
			dev_info(&vsi->back->pdev->dev,
				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
				 vsi->back->hw.aq.asq_last_status);
4129
			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4130 4131
			goto error_pvid;
		}
4132 4133 4134

		/* remove the previously added non-VLAN MAC filters */
		i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
4135 4136
	}

4137 4138
	spin_unlock_bh(&vsi->mac_filter_hash_lock);

4139 4140 4141 4142 4143 4144
	if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
		alluni = true;

	if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
		allmulti = true;

4145 4146 4147
	/* Schedule the worker thread to take care of applying changes */
	i40e_service_event_schedule(vsi->back);

4148 4149 4150 4151
	if (ret) {
		dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
		goto error_pvid;
	}
4152

G
Greg Rose 已提交
4153 4154 4155 4156
	/* The Port VLAN needs to be saved across resets the same as the
	 * default LAN MAC address.
	 */
	vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
4157 4158 4159 4160 4161 4162 4163

	ret = i40e_config_vf_promiscuous_mode(vf, vsi->id, allmulti, alluni);
	if (ret) {
		dev_err(&pf->pdev->dev, "Unable to config vf promiscuous mode\n");
		goto error_pvid;
	}

4164 4165 4166
	ret = 0;

error_pvid:
4167
	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4168 4169 4170 4171 4172 4173
	return ret;
}

/**
 * i40e_ndo_set_vf_bw
 * @netdev: network interface device structure
4174
 * @vf_id: VF identifier
4175 4176
 * @min_tx_rate: Minimum Tx rate
 * @max_tx_rate: Maximum Tx rate
4177
 *
4178
 * configure VF Tx rate
4179
 **/
4180 4181
int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
		       int max_tx_rate)
4182
{
4183 4184 4185 4186 4187 4188
	struct i40e_netdev_priv *np = netdev_priv(netdev);
	struct i40e_pf *pf = np->vsi->back;
	struct i40e_vsi *vsi;
	struct i40e_vf *vf;
	int ret = 0;

4189 4190 4191 4192 4193
	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
		return -EAGAIN;
	}

4194
	/* validate the request */
4195 4196
	ret = i40e_validate_vf(pf, vf_id);
	if (ret)
4197 4198
		goto error;

4199
	if (min_tx_rate) {
4200
		dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
4201 4202 4203 4204
			min_tx_rate, vf_id);
		return -EINVAL;
	}

4205
	vf = &pf->vf[vf_id];
4206
	vsi = pf->vsi[vf->lan_vsi_idx];
4207
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4208 4209 4210
		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
			vf_id);
		ret = -EAGAIN;
4211 4212 4213
		goto error;
	}

4214 4215
	ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
	if (ret)
4216
		goto error;
4217

4218
	vf->tx_rate = max_tx_rate;
4219
error:
4220
	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4221
	return ret;
4222 4223 4224 4225 4226
}

/**
 * i40e_ndo_get_vf_config
 * @netdev: network interface device structure
4227 4228
 * @vf_id: VF identifier
 * @ivi: VF configuration structure
4229
 *
4230
 * return VF configuration
4231 4232 4233 4234 4235 4236 4237 4238 4239 4240
 **/
int i40e_ndo_get_vf_config(struct net_device *netdev,
			   int vf_id, struct ifla_vf_info *ivi)
{
	struct i40e_netdev_priv *np = netdev_priv(netdev);
	struct i40e_vsi *vsi = np->vsi;
	struct i40e_pf *pf = vsi->back;
	struct i40e_vf *vf;
	int ret = 0;

4241 4242 4243 4244 4245
	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
		return -EAGAIN;
	}

4246
	/* validate the request */
4247 4248
	ret = i40e_validate_vf(pf, vf_id);
	if (ret)
4249 4250
		goto error_param;

4251
	vf = &pf->vf[vf_id];
4252
	/* first vsi is always the LAN vsi */
4253
	vsi = pf->vsi[vf->lan_vsi_idx];
4254
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4255 4256 4257
		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
			vf_id);
		ret = -EAGAIN;
4258 4259 4260 4261 4262
		goto error_param;
	}

	ivi->vf = vf_id;

J
Jesse Brandeburg 已提交
4263
	ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
4264

4265 4266
	ivi->max_tx_rate = vf->tx_rate;
	ivi->min_tx_rate = 0;
4267 4268 4269
	ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
	ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
		   I40E_VLAN_PRIORITY_SHIFT;
4270 4271 4272 4273 4274 4275
	if (vf->link_forced == false)
		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
	else if (vf->link_up == true)
		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
	else
		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4276
	ivi->spoofchk = vf->spoofchk;
4277
	ivi->trusted = vf->trusted;
4278 4279 4280
	ret = 0;

error_param:
4281
	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4282 4283
	return ret;
}
M
Mitch Williams 已提交
4284 4285 4286 4287

/**
 * i40e_ndo_set_vf_link_state
 * @netdev: network interface device structure
4288
 * @vf_id: VF identifier
M
Mitch Williams 已提交
4289 4290 4291 4292 4293 4294 4295 4296
 * @link: required link state
 *
 * Set the link state of a specified VF, regardless of physical link state
 **/
int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
{
	struct i40e_netdev_priv *np = netdev_priv(netdev);
	struct i40e_pf *pf = np->vsi->back;
4297
	struct virtchnl_pf_event pfe;
M
Mitch Williams 已提交
4298 4299
	struct i40e_hw *hw = &pf->hw;
	struct i40e_vf *vf;
4300
	int abs_vf_id;
M
Mitch Williams 已提交
4301 4302
	int ret = 0;

4303 4304 4305 4306 4307
	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
		return -EAGAIN;
	}

M
Mitch Williams 已提交
4308 4309 4310 4311 4312 4313 4314 4315
	/* validate the request */
	if (vf_id >= pf->num_alloc_vfs) {
		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
		ret = -EINVAL;
		goto error_out;
	}

	vf = &pf->vf[vf_id];
4316
	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
M
Mitch Williams 已提交
4317

4318
	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
4319
	pfe.severity = PF_EVENT_SEVERITY_INFO;
M
Mitch Williams 已提交
4320 4321 4322 4323 4324 4325 4326

	switch (link) {
	case IFLA_VF_LINK_STATE_AUTO:
		vf->link_forced = false;
		pfe.event_data.link_event.link_status =
			pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
		pfe.event_data.link_event.link_speed =
4327
			(enum virtchnl_link_speed)
M
Mitch Williams 已提交
4328 4329 4330 4331 4332 4333
			pf->hw.phy.link_info.link_speed;
		break;
	case IFLA_VF_LINK_STATE_ENABLE:
		vf->link_forced = true;
		vf->link_up = true;
		pfe.event_data.link_event.link_status = true;
4334
		pfe.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_40GB;
M
Mitch Williams 已提交
4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346
		break;
	case IFLA_VF_LINK_STATE_DISABLE:
		vf->link_forced = true;
		vf->link_up = false;
		pfe.event_data.link_event.link_status = false;
		pfe.event_data.link_event.link_speed = 0;
		break;
	default:
		ret = -EINVAL;
		goto error_out;
	}
	/* Notify the VF of its new link state */
4347
	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
M
Mitch Williams 已提交
4348 4349 4350
			       0, (u8 *)&pfe, sizeof(pfe), NULL);

error_out:
4351
	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
M
Mitch Williams 已提交
4352 4353
	return ret;
}
4354 4355 4356 4357

/**
 * i40e_ndo_set_vf_spoofchk
 * @netdev: network interface device structure
4358
 * @vf_id: VF identifier
4359 4360 4361 4362
 * @enable: flag to enable or disable feature
 *
 * Enable or disable VF spoof checking
 **/
4363
int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
4364 4365 4366 4367 4368 4369 4370 4371 4372
{
	struct i40e_netdev_priv *np = netdev_priv(netdev);
	struct i40e_vsi *vsi = np->vsi;
	struct i40e_pf *pf = vsi->back;
	struct i40e_vsi_context ctxt;
	struct i40e_hw *hw = &pf->hw;
	struct i40e_vf *vf;
	int ret = 0;

4373 4374 4375 4376 4377
	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
		return -EAGAIN;
	}

4378 4379 4380 4381 4382 4383 4384 4385
	/* validate the request */
	if (vf_id >= pf->num_alloc_vfs) {
		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
		ret = -EINVAL;
		goto out;
	}

	vf = &(pf->vf[vf_id]);
4386
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4387 4388 4389 4390 4391
		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
			vf_id);
		ret = -EAGAIN;
		goto out;
	}
4392 4393 4394 4395 4396 4397

	if (enable == vf->spoofchk)
		goto out;

	vf->spoofchk = enable;
	memset(&ctxt, 0, sizeof(ctxt));
4398
	ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
4399 4400 4401
	ctxt.pf_num = pf->hw.pf_id;
	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
	if (enable)
G
Greg Rose 已提交
4402 4403
		ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
					I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
4404 4405 4406 4407 4408 4409 4410
	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
	if (ret) {
		dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
			ret);
		ret = -EIO;
	}
out:
4411
	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4412 4413
	return ret;
}
4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429

/**
 * i40e_ndo_set_vf_trust
 * @netdev: network interface device structure of the pf
 * @vf_id: VF identifier
 * @setting: trust setting
 *
 * Enable or disable VF trust setting
 **/
int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
{
	struct i40e_netdev_priv *np = netdev_priv(netdev);
	struct i40e_pf *pf = np->vsi->back;
	struct i40e_vf *vf;
	int ret = 0;

4430 4431 4432 4433 4434
	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
		return -EAGAIN;
	}

4435 4436 4437
	/* validate the request */
	if (vf_id >= pf->num_alloc_vfs) {
		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4438 4439
		ret = -EINVAL;
		goto out;
4440 4441 4442 4443
	}

	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
		dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
4444 4445
		ret = -EINVAL;
		goto out;
4446 4447 4448 4449 4450 4451 4452 4453
	}

	vf = &pf->vf[vf_id];

	if (setting == vf->trusted)
		goto out;

	vf->trusted = setting;
4454
	i40e_vc_disable_vf(vf);
4455 4456
	dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
		 vf_id, setting ? "" : "un");
4457 4458 4459 4460 4461 4462 4463 4464 4465 4466

	if (vf->adq_enabled) {
		if (!vf->trusted) {
			dev_info(&pf->pdev->dev,
				 "VF %u no longer Trusted, deleting all cloud filters\n",
				 vf_id);
			i40e_del_all_cloud_filters(vf);
		}
	}

4467
out:
4468
	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4469 4470
	return ret;
}