i40e_virtchnl_pf.c 90.8 KB
Newer Older
1 2 3
/*******************************************************************************
 *
 * Intel Ethernet Controller XL710 Family Linux Driver
4
 * Copyright(c) 2013 - 2016 Intel Corporation.
5 6 7 8 9 10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
G
Greg Rose 已提交
15 16
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26 27 28
 *
 * The full GNU General Public License is included in this distribution in
 * the file called "COPYING".
 *
 * Contact Information:
 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 ******************************************************************************/

#include "i40e.h"

29 30 31 32 33 34 35 36 37 38 39 40 41
/*********************notification routines***********************/

/**
 * i40e_vc_vf_broadcast
 * @pf: pointer to the PF structure
 * @opcode: operation code
 * @retval: return value
 * @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,
42
				 enum virtchnl_ops v_opcode,
43 44 45 46 47 48 49 50
				 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++) {
51
		int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
52
		/* Not all vfs are enabled so skip the ones that are not */
53 54
		if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
		    !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
55 56 57 58 59 60 61 62 63 64 65
			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);
	}
}

/**
66
 * i40e_vc_notify_vf_link_state
67 68 69 70 71 72
 * @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)
{
73
	struct virtchnl_pf_event pfe;
74 75 76
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	struct i40e_link_status *ls = &pf->hw.phy.link_info;
77
	int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
78

79
	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
80
	pfe.severity = PF_EVENT_SEVERITY_INFO;
81 82 83 84 85 86 87
	if (vf->link_forced) {
		pfe.event_data.link_event.link_status = vf->link_up;
		pfe.event_data.link_event.link_speed =
			(vf->link_up ? I40E_LINK_SPEED_40GB : 0);
	} else {
		pfe.event_data.link_event.link_status =
			ls->link_info & I40E_AQ_LINK_UP;
88 89
		pfe.event_data.link_event.link_speed =
			(enum virtchnl_link_speed)ls->link_speed;
90
	}
91
	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
			       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)
{
117
	struct virtchnl_pf_event pfe;
118

119
	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
120
	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
121 122
	i40e_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, 0,
			     (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
123 124 125 126 127 128 129 130 131 132
}

/**
 * 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)
{
133
	struct virtchnl_pf_event pfe;
134 135 136 137 138 139 140
	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 */
141 142
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
	    !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
143 144
		return;

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

147
	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
148
	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
149
	i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, VIRTCHNL_OP_EVENT,
150
			       0, (u8 *)&pfe,
151
			       sizeof(struct virtchnl_pf_event), NULL);
152
}
153 154
/***********************misc routines*****************************/

155 156
/**
 * i40e_vc_disable_vf
157
 * @vf: pointer to the VF info
158
 *
159
 * Disable the VF through a SW reset.
160
 **/
161
static inline void i40e_vc_disable_vf(struct i40e_vf *vf)
162
{
163 164
	int i;

165
	i40e_vc_notify_vf_reset(vf);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

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

183 184
/**
 * i40e_vc_isvalid_vsi_id
185 186
 * @vf: pointer to the VF info
 * @vsi_id: VF relative VSI id
187
 *
188
 * check for the valid VSI id
189
 **/
190
static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
191 192
{
	struct i40e_pf *pf = vf->pf;
193
	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
194

195
	return (vsi && (vsi->vf_id == vf->vf_id));
196 197 198 199
}

/**
 * i40e_vc_isvalid_queue_id
200
 * @vf: pointer to the VF info
201 202 203 204 205
 * @vsi_id: vsi id
 * @qid: vsi relative queue id
 *
 * check for the valid queue id
 **/
206
static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
207 208 209
					    u8 qid)
{
	struct i40e_pf *pf = vf->pf;
210
	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
211

212
	return (vsi && (qid < vsi->alloc_queue_pairs));
213 214 215 216
}

/**
 * i40e_vc_isvalid_vector_id
217 218
 * @vf: pointer to the VF info
 * @vector_id: VF relative vector id
219 220 221 222 223 224 225
 *
 * 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;

226
	return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
227 228 229 230 231 232
}

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

/**
 * i40e_vc_get_pf_queue_id
233
 * @vf: pointer to the VF info
234
 * @vsi_id: id of VSI as provided by the FW
235 236
 * @vsi_queue_id: vsi relative queue id
 *
237
 * return PF relative queue id
238
 **/
239
static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
240 241 242
				   u8 vsi_queue_id)
{
	struct i40e_pf *pf = vf->pf;
243
	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
244 245
	u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;

246 247 248
	if (!vsi)
		return pf_queue_id;

249 250 251 252 253 254 255 256 257 258 259 260 261
	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;
}

/**
 * i40e_config_irq_link_list
262
 * @vf: pointer to the VF info
263
 * @vsi_id: id of VSI as given by the FW
264 265 266 267
 * @vecmap: irq map info
 *
 * configure irq link list from the map
 **/
268
static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
269
				      struct virtchnl_vector_map *vecmap)
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
{
	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;
	u16 next_q, vector_id;
	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(
286 287
		     ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
		     (vector_id - 1));
288 289 290 291 292 293 294

	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;
295
	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
296 297
		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
				    vsi_queue_id));
298 299 300
	}

	tempmap = vecmap->txq_map;
301
	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
302 303
		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
				     vsi_queue_id + 1));
304 305 306 307 308
	}

	next_q = find_first_bit(&linklistmap,
				(I40E_MAX_VSI_QP *
				 I40E_VIRTCHNL_SUPPORTED_QTYPES));
M
Mitch Williams 已提交
309 310
	vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
	qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
311
	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);

	wr32(hw, reg_idx, reg);

	while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
		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;
		}

		next_q = find_next_bit(&linklistmap,
				       (I40E_MAX_VSI_QP *
					I40E_VIRTCHNL_SUPPORTED_QTYPES),
				       next_q + 1);
334 335
		if (next_q <
		    (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
336 337
			vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
			qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
338
			pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
339 340 341 342 343 344 345 346 347 348
							      vsi_queue_id);
		} 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) |
349
		    BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
350 351 352 353
		    (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
		wr32(hw, reg_idx, reg);
	}

354 355 356
	/* if the vf is running in polling mode and using interrupt zero,
	 * need to disable auto-mask on enabling zero interrupt for VFs.
	 */
357
	if ((vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
358 359 360 361 362 363 364 365
	    (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);
		}
	}

366 367 368 369
irq_list_done:
	i40e_flush(hw);
}

370 371 372 373 374 375 376 377
/**
 * 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;
378
	struct virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
379 380 381 382 383 384 385 386
	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++) {
387
		struct virtchnl_iwarp_qv_info *qv_info;
388 389 390 391 392 393 394 395 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
		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,
428
				    struct virtchnl_iwarp_qvlist_info *qvlist_info)
429 430 431
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
432
	struct virtchnl_iwarp_qv_info *qv_info;
433 434 435 436
	u32 v_idx, i, reg_idx, reg;
	u32 next_q_idx, next_q_type;
	u32 msix_vf, size;

437 438
	size = sizeof(struct virtchnl_iwarp_qvlist_info) +
	       (sizeof(struct virtchnl_iwarp_qv_info) *
439 440
						(qvlist_info->num_vectors - 1));
	vf->qvlist_info = kzalloc(size, GFP_KERNEL);
441 442 443
	if (!vf->qvlist_info)
		return -ENOMEM;

444 445 446 447 448 449 450 451 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
	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;
}

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

C
Carolyn Wyborny 已提交
524 525 526 527
	if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
		ret = -ENOENT;
		goto error_context;
	}
528 529
	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 已提交
530 531 532 533
	if (!vsi) {
		ret = -ENOENT;
		goto error_context;
	}
534 535 536 537 538 539 540

	/* 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;
541
	tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
542
	tx_ctx.rdylist_act = 0;
543 544
	tx_ctx.head_wb_ena = info->headwb_enabled;
	tx_ctx.head_wb_addr = info->dma_headwb_addr;
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

	/* 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;
568
	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
569 570 571 572 573 574 575 576 577 578 579 580 581
		    & 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
582
 * @vf: pointer to the VF info
583
 * @vsi_id: id of VSI  as provided by the FW
584 585 586 587 588
 * @vsi_queue_id: vsi relative queue index
 * @info: config. info
 *
 * configure rx queue
 **/
589
static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
590
				    u16 vsi_queue_id,
591
				    struct virtchnl_rxq_info *info)
592 593 594 595 596 597 598
{
	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;

599
	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

	/* 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;

620
		/* set split mode 10b */
M
Mitch Williams 已提交
621
		rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	}

	/* 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 */
	rx_ctx.lrxqthresh = 2;
	rx_ctx.crcstrip = 1;
644
	rx_ctx.prefena = 1;
645
	rx_ctx.l2tsel = 1;
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672

	/* 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
673
 * @vf: pointer to the VF info
674 675
 * @type: type of VSI to allocate
 *
676
 * alloc VF vsi context & resources
677 678 679 680 681 682 683 684 685 686 687 688
 **/
static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
{
	struct i40e_mac_filter *f = NULL;
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi;
	int ret = 0;

	vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);

	if (!vsi) {
		dev_err(&pf->pdev->dev,
689
			"add vsi failed for VF %d, aq_err %d\n",
690 691 692 693 694
			vf->vf_id, pf->hw.aq.asq_last_status);
		ret = -ENOENT;
		goto error_alloc_vsi_res;
	}
	if (type == I40E_VSI_SRIOV) {
M
Mitch Williams 已提交
695
		u64 hena = i40e_pf_get_default_rss_hena(pf);
696
		u8 broadcast[ETH_ALEN];
M
Mitch Williams 已提交
697

698
		vf->lan_vsi_idx = vsi->idx;
699
		vf->lan_vsi_id = vsi->id;
G
Greg Rose 已提交
700 701 702 703 704 705 706 707
		/* 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 已提交
708

709
		spin_lock_bh(&vsi->mac_filter_hash_lock);
M
Mitch Williams 已提交
710
		if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
711 712
			f = i40e_add_mac_filter(vsi,
						vf->default_lan_addr.addr);
M
Mitch Williams 已提交
713 714 715 716 717
			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);
		}
718
		eth_broadcast_addr(broadcast);
719
		f = i40e_add_mac_filter(vsi, broadcast);
720 721 722
		if (!f)
			dev_info(&pf->pdev->dev,
				 "Could not allocate VF broadcast filter\n");
723
		spin_unlock_bh(&vsi->mac_filter_hash_lock);
724 725
		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));
726
	}
727

728
	/* program mac filter */
J
Jesse Brandeburg 已提交
729
	ret = i40e_sync_vsi_filters(vsi);
M
Mitch Williams 已提交
730
	if (ret)
731 732
		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");

733 734 735 736 737 738 739 740 741
	/* Set VF bandwidth if specified */
	if (vf->tx_rate) {
		ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
						  vf->tx_rate / 50, 0, NULL);
		if (ret)
			dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
				vf->vf_id, ret);
	}

742 743 744 745
error_alloc_vsi_res:
	return ret;
}

M
Mitch Williams 已提交
746 747
/**
 * i40e_enable_vf_mappings
748
 * @vf: pointer to the VF info
M
Mitch Williams 已提交
749
 *
750
 * enable VF mappings
M
Mitch Williams 已提交
751 752 753 754 755 756 757 758 759 760 761 762
 **/
static void i40e_enable_vf_mappings(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	u32 reg, total_queue_pairs = 0;
	int j;

	/* 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
	 */
763 764
	i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
			  I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
M
Mitch Williams 已提交
765 766 767 768 769 770

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

	/* map PF queues to VF queues */
771 772
	for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
		u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
J
Jesse Brandeburg 已提交
773

M
Mitch Williams 已提交
774 775 776 777 778 779 780
		reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
		wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
		total_queue_pairs++;
	}

	/* map PF queues to VSI */
	for (j = 0; j < 7; j++) {
781
		if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
M
Mitch Williams 已提交
782 783
			reg = 0x07FF07FF;	/* unused */
		} else {
784
			u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
M
Mitch Williams 已提交
785 786
							  j * 2);
			reg = qid;
787
			qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
M
Mitch Williams 已提交
788 789 790
						      (j * 2) + 1);
			reg |= qid << 16;
		}
791 792
		i40e_write_rx_ctl(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id),
				  reg);
M
Mitch Williams 已提交
793 794 795 796 797 798 799
	}

	i40e_flush(hw);
}

/**
 * i40e_disable_vf_mappings
800
 * @vf: pointer to the VF info
M
Mitch Williams 已提交
801
 *
802
 * disable VF mappings
M
Mitch Williams 已提交
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
 **/
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
820
 * @vf: pointer to the VF info
M
Mitch Williams 已提交
821
 *
822
 * free VF resources
M
Mitch Williams 已提交
823 824 825 826
 **/
static void i40e_free_vf_res(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
M
Mitch Williams 已提交
827 828 829
	struct i40e_hw *hw = &pf->hw;
	u32 reg_idx, reg;
	int i, msix_vf;
M
Mitch Williams 已提交
830

831 832 833
	/* Start by disabling VF's configuration API to prevent the OS from
	 * accessing the VF's VSI after it's freed / invalidated.
	 */
834
	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
835

836 837 838 839 840 841 842 843
	/* 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 已提交
844
	/* free vsi & disconnect it from the parent uplink */
845 846 847
	if (vf->lan_vsi_idx) {
		i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
		vf->lan_vsi_idx = 0;
M
Mitch Williams 已提交
848
		vf->lan_vsi_id = 0;
849
		vf->num_mac = 0;
M
Mitch Williams 已提交
850
	}
851 852
	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;

M
Mitch Williams 已提交
853 854 855 856 857 858 859 860 861 862 863 864
	/* 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 已提交
865

M
Mitch Williams 已提交
866 867 868 869 870 871 872 873 874 875 876 877 878 879
	/* 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);
	}
880
	/* reset some of the state variables keeping track of the resources */
M
Mitch Williams 已提交
881
	vf->num_queue_pairs = 0;
882 883
	clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
	clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
M
Mitch Williams 已提交
884 885 886 887
}

/**
 * i40e_alloc_vf_res
888
 * @vf: pointer to the VF info
M
Mitch Williams 已提交
889
 *
890
 * allocate VF resources
M
Mitch Williams 已提交
891 892 893 894 895 896 897
 **/
static int i40e_alloc_vf_res(struct i40e_vf *vf)
{
	struct i40e_pf *pf = vf->pf;
	int total_queue_pairs = 0;
	int ret;

898 899 900 901 902 903
	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 已提交
904 905 906 907
	/* allocate hw vsi context & associated resources */
	ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
	if (ret)
		goto error_alloc;
908
	total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
909

910 911 912 913 914 915 916 917 918
	/* 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;

919 920 921 922
	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 已提交
923 924

	/* store the total qps number for the runtime
925
	 * VF req validation
M
Mitch Williams 已提交
926 927 928
	 */
	vf->num_queue_pairs = total_queue_pairs;

929
	/* VF is now completely initialized */
930
	set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
M
Mitch Williams 已提交
931 932 933 934 935 936 937 938

error_alloc:
	if (ret)
		i40e_free_vf_res(vf);

	return ret;
}

M
Mitch Williams 已提交
939 940 941 942
#define VF_DEVICE_STATUS 0xAA
#define VF_TRANS_PENDING_MASK 0x20
/**
 * i40e_quiesce_vf_pci
943
 * @vf: pointer to the VF structure
M
Mitch Williams 已提交
944 945 946 947 948 949 950 951 952 953 954
 *
 * 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;

955
	vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
M
Mitch Williams 已提交
956 957 958 959 960 961 962 963 964 965 966 967

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

968
/**
969
 * i40e_trigger_vf_reset
970
 * @vf: pointer to the VF structure
971 972
 * @flr: VFLR was issued or not
 *
973 974 975
 * 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.
976
 **/
977
static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
978 979 980
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
981
	u32 reg, reg_idx, bit_idx;
982

983
	/* warn the VF */
984
	clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
985

986 987 988 989 990 991
	/* 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.
	 */
992
	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
993

M
Mitch Williams 已提交
994 995
	/* 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.
996 997
	 */
	if (!flr) {
998
		/* reset VF using VPGEN_VFRTRIG reg */
M
Mitch Williams 已提交
999 1000
		reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
		reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1001 1002 1003
		wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
		i40e_flush(hw);
	}
1004 1005 1006 1007
	/* 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 已提交
1008
	i40e_flush(hw);
1009

M
Mitch Williams 已提交
1010 1011 1012
	if (i40e_quiesce_vf_pci(vf))
		dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
			vf->vf_id);
1013
}
M
Mitch Williams 已提交
1014

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
/**
 * 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 已提交
1028

1029
	/* free VF resources to begin resetting the VSI state */
M
Mitch Williams 已提交
1030
	i40e_free_vf_res(vf);
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

	/* 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 */
1047
	if (!i40e_alloc_vf_res(vf)) {
1048
		int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1049
		i40e_enable_vf_mappings(vf);
1050 1051
		set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
		clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1052
		/* Do not notify the client during VF init */
1053 1054
		if (test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
				       &vf->vf_states))
1055
			i40e_notify_client_of_vf_reset(pf, abs_vf_id);
1056
		vf->num_vlan = 0;
1057
	}
1058 1059 1060 1061 1062

	/* 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.
	 */
1063
	wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1064 1065 1066 1067 1068 1069 1070
}

/**
 * i40e_reset_vf
 * @vf: pointer to the VF structure
 * @flr: VFLR was issued or not
 *
1071
 * Returns true if the VF is reset, false otherwise.
1072
 **/
1073
bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
1074 1075 1076 1077 1078 1079 1080
{
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	bool rsd = false;
	u32 reg;
	int i;

1081 1082 1083
	/* If the VFs have been disabled, this means something else is
	 * resetting the VF, so we shouldn't continue.
	 */
1084
	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1085
		return false;
1086 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

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

1120
	i40e_flush(hw);
1121
	clear_bit(__I40E_VF_DISABLE, pf->state);
1122 1123

	return true;
1124
}
1125

1126 1127 1128 1129 1130 1131 1132 1133 1134
/**
 * 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.
1135 1136
 *
 * Returns true if any VFs were reset, and false otherwise.
1137
 **/
1138
bool i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
1139 1140 1141 1142 1143 1144 1145 1146
{
	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)
1147
		return false;
1148 1149

	/* If VFs have been disabled, there is no need to reset */
1150
	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1151
		return false;
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224

	/* 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);
1225
	clear_bit(__I40E_VF_DISABLE, pf->state);
1226 1227

	return true;
1228 1229
}

1230 1231
/**
 * i40e_free_vfs
1232
 * @pf: pointer to the PF structure
1233
 *
1234
 * free VF resources
1235 1236 1237
 **/
void i40e_free_vfs(struct i40e_pf *pf)
{
1238 1239 1240
	struct i40e_hw *hw = &pf->hw;
	u32 reg_idx, bit_idx;
	int i, tmp, vf_id;
1241 1242 1243

	if (!pf->vf)
		return;
1244
	while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1245
		usleep_range(1000, 2000);
1246

1247
	i40e_notify_client_of_vf_enable(pf, 0);
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

	/* 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++) {
1258
		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1259 1260 1261 1262
			continue;

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

1264 1265 1266 1267 1268 1269
	/* 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 已提交
1270 1271
	else
		dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1272

1273
	/* free up VF resources */
1274 1275 1276
	tmp = pf->num_alloc_vfs;
	pf->num_alloc_vfs = 0;
	for (i = 0; i < tmp; i++) {
1277
		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1278 1279 1280 1281 1282 1283 1284 1285
			i40e_free_vf_res(&pf->vf[i]);
		/* disable qp mappings */
		i40e_disable_vf_mappings(&pf->vf[i]);
	}

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

1286 1287 1288 1289
	/* 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.
	 */
1290
	if (!pci_vfs_assigned(pf->pdev)) {
1291 1292 1293 1294 1295 1296
		/* 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;
1297
			wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1298
		}
1299
	}
1300
	clear_bit(__I40E_VF_DISABLE, pf->state);
1301 1302 1303 1304 1305
}

#ifdef CONFIG_PCI_IOV
/**
 * i40e_alloc_vfs
1306 1307
 * @pf: pointer to the PF structure
 * @num_alloc_vfs: number of VFs to allocate
1308
 *
1309
 * allocate VF resources
1310
 **/
M
Mitch Williams 已提交
1311
int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1312 1313 1314 1315
{
	struct i40e_vf *vfs;
	int i, ret = 0;

1316
	/* Disable interrupt 0 so we don't try to handle the VFLR. */
1317 1318
	i40e_irq_dynamic_disable_icr0(pf);

M
Mitch Williams 已提交
1319 1320 1321 1322
	/* 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) {
1323
			pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
M
Mitch Williams 已提交
1324 1325 1326
			pf->num_alloc_vfs = 0;
			goto err_iov;
		}
1327 1328
	}
	/* allocate memory */
1329
	vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1330 1331 1332 1333
	if (!vfs) {
		ret = -ENOMEM;
		goto err_alloc;
	}
1334
	pf->vf = vfs;
1335 1336 1337 1338 1339 1340 1341 1342 1343

	/* 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);
1344
		vfs[i].spoofchk = true;
1345 1346

		set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
1347 1348 1349 1350

	}
	pf->num_alloc_vfs = num_alloc_vfs;

1351 1352 1353
	/* VF resources get allocated during reset */
	i40e_reset_all_vfs(pf, false);

1354 1355
	i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);

1356 1357 1358 1359
err_alloc:
	if (ret)
		i40e_free_vfs(pf);
err_iov:
1360
	/* Re-enable interrupt 0. */
1361
	i40e_irq_dynamic_enable_icr0(pf, false);
1362 1363 1364 1365 1366 1367 1368
	return ret;
}

#endif
/**
 * i40e_pci_sriov_enable
 * @pdev: pointer to a pci_dev structure
1369
 * @num_vfs: number of VFs to allocate
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
 *
 * 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;

1380
	if (test_bit(__I40E_TESTING, pf->state)) {
1381 1382 1383 1384 1385 1386
		dev_warn(&pdev->dev,
			 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
		err = -EPERM;
		goto err_out;
	}

1387 1388 1389 1390 1391 1392
	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) {
1393 1394
		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);
1395 1396 1397 1398
		err = -EPERM;
		goto err_out;
	}

1399
	dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
	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
1418
 * @num_vfs: number of VFs to allocate
1419 1420 1421 1422 1423 1424 1425 1426
 *
 * 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);

1427 1428 1429 1430 1431 1432
	if (num_vfs) {
		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
			i40e_do_reset_safe(pf,
					   BIT_ULL(__I40E_PF_RESET_REQUESTED));
		}
1433
		return i40e_pci_sriov_enable(pdev, num_vfs);
1434
	}
1435

1436
	if (!pci_vfs_assigned(pf->pdev)) {
1437
		i40e_free_vfs(pf);
1438 1439
		pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
		i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
1440 1441 1442 1443
	} else {
		dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
		return -EINVAL;
	}
1444 1445 1446 1447 1448 1449 1450
	return 0;
}

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

/**
 * i40e_vc_send_msg_to_vf
1451
 * @vf: pointer to the VF info
1452 1453 1454 1455 1456
 * @v_opcode: virtual channel opcode
 * @v_retval: virtual channel return value
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
1457
 * send msg to VF
1458 1459 1460 1461
 **/
static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
				  u32 v_retval, u8 *msg, u16 msglen)
{
1462 1463 1464
	struct i40e_pf *pf;
	struct i40e_hw *hw;
	int abs_vf_id;
1465 1466
	i40e_status aq_ret;

1467 1468 1469 1470 1471 1472 1473 1474
	/* 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;

1475 1476 1477
	/* single place to detect unsuccessful return values */
	if (v_retval) {
		vf->num_invalid_msgs++;
M
Mitch Williams 已提交
1478 1479
		dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
			 vf->vf_id, v_opcode, v_retval);
1480 1481 1482 1483 1484 1485
		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");
1486
			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1487 1488 1489
		}
	} else {
		vf->num_valid_msgs++;
1490 1491
		/* reset the invalid counter, if a valid message is received. */
		vf->num_invalid_msgs = 0;
1492 1493
	}

1494
	aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,	v_opcode, v_retval,
1495
					msg, msglen, NULL);
1496
	if (aq_ret) {
M
Mitch Williams 已提交
1497 1498 1499
		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);
1500 1501 1502 1503 1504 1505 1506 1507
		return -EIO;
	}

	return 0;
}

/**
 * i40e_vc_send_resp_to_vf
1508
 * @vf: pointer to the VF info
1509 1510 1511
 * @opcode: operation code
 * @retval: return value
 *
1512
 * send resp msg to VF
1513 1514
 **/
static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1515
				   enum virtchnl_ops opcode,
1516 1517 1518 1519 1520 1521 1522
				   i40e_status retval)
{
	return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
}

/**
 * i40e_vc_get_version_msg
1523
 * @vf: pointer to the VF info
1524
 *
1525
 * called from the VF to request the API version used by the PF
1526
 **/
1527
static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
1528
{
1529 1530
	struct virtchnl_version_info info = {
		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1531 1532
	};

1533
	vf->vf_ver = *(struct virtchnl_version_info *)msg;
1534
	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1535
	if (VF_IS_V10(&vf->vf_ver))
1536 1537
		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1538
				      I40E_SUCCESS, (u8 *)&info,
1539
				      sizeof(struct virtchnl_version_info));
1540 1541 1542 1543
}

/**
 * i40e_vc_get_vf_resources_msg
1544
 * @vf: pointer to the VF info
1545 1546 1547
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
1548
 * called from the VF to request its resources
1549
 **/
1550
static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
1551
{
1552
	struct virtchnl_vf_resource *vfres = NULL;
1553 1554 1555 1556
	struct i40e_pf *pf = vf->pf;
	i40e_status aq_ret = 0;
	struct i40e_vsi *vsi;
	int num_vsis = 1;
M
Mitch Williams 已提交
1557
	int len = 0;
1558 1559
	int ret;

1560
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
1561 1562 1563 1564
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}

1565 1566
	len = (sizeof(struct virtchnl_vf_resource) +
	       sizeof(struct virtchnl_vsi_resource) * num_vsis);
1567 1568 1569 1570 1571 1572 1573

	vfres = kzalloc(len, GFP_KERNEL);
	if (!vfres) {
		aq_ret = I40E_ERR_NO_MEMORY;
		len = 0;
		goto err;
	}
1574
	if (VF_IS_V11(&vf->vf_ver))
1575 1576
		vf->driver_caps = *(u32 *)msg;
	else
1577 1578 1579
		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
				  VIRTCHNL_VF_OFFLOAD_RSS_REG |
				  VIRTCHNL_VF_OFFLOAD_VLAN;
1580

1581
	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1582
	vsi = pf->vsi[vf->lan_vsi_idx];
1583
	if (!vsi->info.pvid)
1584
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1585

M
Mitch Williams 已提交
1586
	if (i40e_vf_client_capable(pf, vf->vf_id) &&
1587
	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
1588
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
1589
		set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1590 1591
	} else {
		clear_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1592 1593
	}

1594
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1595
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1596
	} else {
1597
		if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
1598
		    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
1599
			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1600
		else
1601
			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1602
	}
1603

1604
	if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
1605
		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1606
			vfres->vf_cap_flags |=
1607
				VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1608 1609
	}

1610
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1611
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1612

1613
	if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
1614
	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
1615
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1616

1617
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
1618 1619 1620 1621
		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);
1622
			aq_ret = I40E_ERR_PARAM;
1623 1624
			goto err;
		}
1625
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1626
	}
1627

1628
	if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
1629
		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1630
			vfres->vf_cap_flags |=
1631
					VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1632 1633
	}

1634 1635 1636
	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;

1637 1638 1639
	vfres->num_vsis = num_vsis;
	vfres->num_queue_pairs = vf->num_queue_pairs;
	vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1640 1641 1642
	vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
	vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;

1643
	if (vf->lan_vsi_idx) {
M
Mitch Williams 已提交
1644
		vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
1645
		vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
M
Mitch Williams 已提交
1646
		vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
1647
		/* VFs only use TC 0 */
M
Mitch Williams 已提交
1648
		vfres->vsi_res[0].qset_handle
1649
					  = le16_to_cpu(vsi->info.qs_handle[0]);
M
Mitch Williams 已提交
1650
		ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
J
Jesse Brandeburg 已提交
1651
				vf->default_lan_addr.addr);
1652
	}
1653
	set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1654 1655

err:
1656
	/* send the response back to the VF */
1657
	ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
1658 1659 1660 1661 1662 1663 1664 1665
				     aq_ret, (u8 *)vfres, len);

	kfree(vfres);
	return ret;
}

/**
 * i40e_vc_reset_vf_msg
1666
 * @vf: pointer to the VF info
1667 1668 1669
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
1670 1671 1672
 * called from the VF to reset itself,
 * unlike other virtchnl messages, PF driver
 * doesn't send the response back to the VF
1673
 **/
M
Mitch Williams 已提交
1674
static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1675
{
1676
	if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
M
Mitch Williams 已提交
1677
		i40e_reset_vf(vf, false);
1678 1679
}

1680 1681 1682 1683 1684 1685 1686 1687 1688
/**
 * 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;
1689
	int num_vlans = 0, bkt;
1690

1691
	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1692 1693 1694 1695 1696 1697 1698
		if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
			num_vlans++;
	}

	return num_vlans;
}

1699 1700
/**
 * i40e_vc_config_promiscuous_mode_msg
1701
 * @vf: pointer to the VF info
1702 1703 1704
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
1705 1706
 * called from the VF to configure the promiscuous mode of
 * VF vsis
1707 1708 1709 1710
 **/
static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
					       u8 *msg, u16 msglen)
{
1711 1712
	struct virtchnl_promisc_info *info =
	    (struct virtchnl_promisc_info *)msg;
1713 1714
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
1715 1716
	struct i40e_mac_filter *f;
	i40e_status aq_ret = 0;
1717
	bool allmulti = false;
1718 1719 1720
	struct i40e_vsi *vsi;
	bool alluni = false;
	int aq_err = 0;
1721
	int bkt;
1722

1723
	vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
1724
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
C
Carolyn Wyborny 已提交
1725 1726
	    !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
	    !vsi) {
M
Mitch Williams 已提交
1727 1728 1729 1730
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}
	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
1731
		dev_err(&pf->pdev->dev,
M
Mitch Williams 已提交
1732
			"Unprivileged VF %d is attempting to configure promiscuous mode\n",
1733
			vf->vf_id);
M
Mitch Williams 已提交
1734 1735
		/* Lie to the VF on purpose. */
		aq_ret = 0;
1736 1737
		goto error_param;
	}
1738
	/* Multicast promiscuous handling*/
1739
	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
1740
		allmulti = true;
1741 1742 1743 1744 1745 1746 1747

	if (vf->port_vlan_id) {
		aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
							    allmulti,
							    vf->port_vlan_id,
							    NULL);
	} else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1748
		hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1749 1750 1751 1752 1753 1754 1755
			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);
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
			aq_err = pf->hw.aq.asq_last_status;
			if (aq_ret) {
				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));
				break;
			}
		}
	} else {
		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
							       allmulti, NULL);
		aq_err = pf->hw.aq.asq_last_status;
		if (aq_ret) {
			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));
1776
			goto error_param;
1777 1778 1779 1780 1781 1782 1783 1784
		}
	}

	if (!aq_ret) {
		dev_info(&pf->pdev->dev,
			 "VF %d successfully set multicast promiscuous mode\n",
			 vf->vf_id);
		if (allmulti)
1785
			set_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
1786
		else
1787
			clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
1788 1789
	}

1790
	if (info->flags & FLAG_VF_UNICAST_PROMISC)
1791 1792 1793 1794 1795 1796 1797
		alluni = true;
	if (vf->port_vlan_id) {
		aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
							    alluni,
							    vf->port_vlan_id,
							    NULL);
	} else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1798
		hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1799 1800 1801 1802 1803 1804 1805 1806
			if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
				continue;
			aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
								    vsi->seid,
								    alluni,
								    f->vlan,
								    NULL);
			aq_err = pf->hw.aq.asq_last_status;
1807 1808 1809 1810 1811 1812 1813 1814 1815
			if (aq_ret)
				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));
		}
	} else {
		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
1816
							     alluni, NULL,
1817
							     true);
1818
		aq_err = pf->hw.aq.asq_last_status;
1819
		if (aq_ret) {
1820 1821 1822 1823 1824
			dev_err(&pf->pdev->dev,
				"VF %d failed to set unicast promiscuous mode %8.8x err %s aq_err %s\n",
				vf->vf_id, info->flags,
				i40e_stat_str(&pf->hw, aq_ret),
				i40e_aq_str(&pf->hw, aq_err));
1825 1826
			goto error_param;
		}
1827 1828 1829 1830 1831 1832 1833
	}

	if (!aq_ret) {
		dev_info(&pf->pdev->dev,
			 "VF %d successfully set unicast promiscuous mode\n",
			 vf->vf_id);
		if (alluni)
1834
			set_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
1835
		else
1836
			clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
1837
	}
1838 1839

error_param:
1840
	/* send the response to the VF */
1841
	return i40e_vc_send_resp_to_vf(vf,
1842
				       VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1843 1844 1845 1846 1847
				       aq_ret);
}

/**
 * i40e_vc_config_queues_msg
1848
 * @vf: pointer to the VF info
1849 1850 1851
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
1852
 * called from the VF to configure the rx/tx
1853 1854 1855 1856
 * queues
 **/
static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
1857 1858 1859
	struct virtchnl_vsi_queue_config_info *qci =
	    (struct virtchnl_vsi_queue_config_info *)msg;
	struct virtchnl_queue_pair_info *qpi;
1860
	struct i40e_pf *pf = vf->pf;
1861 1862 1863 1864
	u16 vsi_id, vsi_queue_id;
	i40e_status aq_ret = 0;
	int i;

1865
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

	vsi_id = qci->vsi_id;
	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}
	for (i = 0; i < qci->num_queue_pairs; i++) {
		qpi = &qci->qpair[i];
		vsi_queue_id = qpi->txq.queue_id;
		if ((qpi->txq.vsi_id != vsi_id) ||
		    (qpi->rxq.vsi_id != vsi_id) ||
		    (qpi->rxq.queue_id != vsi_queue_id) ||
		    !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
			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;
		}
	}
1894
	/* set vsi num_queue_pairs in use to num configured by VF */
1895
	pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
1896 1897

error_param:
1898
	/* send the response to the VF */
1899
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1900 1901 1902 1903 1904
				       aq_ret);
}

/**
 * i40e_vc_config_irq_map_msg
1905
 * @vf: pointer to the VF info
1906 1907 1908
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
1909
 * called from the VF to configure the irq to
1910 1911 1912 1913
 * queue map
 **/
static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
1914 1915 1916
	struct virtchnl_irq_map_info *irqmap_info =
	    (struct virtchnl_irq_map_info *)msg;
	struct virtchnl_vector_map *map;
1917 1918 1919 1920 1921
	u16 vsi_id, vsi_queue_id, vector_id;
	i40e_status aq_ret = 0;
	unsigned long tempmap;
	int i;

1922
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
		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;
		}

		/* lookout for the invalid queue index */
		tempmap = map->rxq_map;
1941
		for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1942 1943 1944 1945 1946 1947 1948 1949
			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
						      vsi_queue_id)) {
				aq_ret = I40E_ERR_PARAM;
				goto error_param;
			}
		}

		tempmap = map->txq_map;
1950
		for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
						      vsi_queue_id)) {
				aq_ret = I40E_ERR_PARAM;
				goto error_param;
			}
		}

		i40e_config_irq_link_list(vf, vsi_id, map);
	}
error_param:
1961
	/* send the response to the VF */
1962
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
1963 1964 1965 1966 1967
				       aq_ret);
}

/**
 * i40e_vc_enable_queues_msg
1968
 * @vf: pointer to the VF info
1969 1970 1971
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
1972
 * called from the VF to enable all or specific queue(s)
1973 1974 1975
 **/
static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
1976 1977
	struct virtchnl_queue_select *vqs =
	    (struct virtchnl_queue_select *)msg;
1978 1979 1980 1981
	struct i40e_pf *pf = vf->pf;
	u16 vsi_id = vqs->vsi_id;
	i40e_status aq_ret = 0;

1982
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
		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;
	}
1996

F
Filip Sadowski 已提交
1997
	if (i40e_vsi_start_rings(pf->vsi[vf->lan_vsi_idx]))
M
Mitch Williams 已提交
1998
		aq_ret = I40E_ERR_TIMEOUT;
1999
error_param:
2000
	/* send the response to the VF */
2001
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
2002 2003 2004 2005 2006
				       aq_ret);
}

/**
 * i40e_vc_disable_queues_msg
2007
 * @vf: pointer to the VF info
2008 2009 2010
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
2011
 * called from the VF to disable all or specific
2012 2013 2014 2015
 * queue(s)
 **/
static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
2016 2017
	struct virtchnl_queue_select *vqs =
	    (struct virtchnl_queue_select *)msg;
2018 2019 2020
	struct i40e_pf *pf = vf->pf;
	i40e_status aq_ret = 0;

2021
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
		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;
	}
2035

F
Filip Sadowski 已提交
2036
	i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
2037 2038

error_param:
2039
	/* send the response to the VF */
2040
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
2041 2042 2043
				       aq_ret);
}

2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
/**
 * i40e_vc_request_queues_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * VFs get a default number of queues but can use this message to request a
 * different number.  Will respond with either the number requested or the
 * maximum we can support.
 **/
static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg, int msglen)
{
	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 {
		vf->num_req_queues = req_pairs;
	}

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

2090 2091
/**
 * i40e_vc_get_stats_msg
2092
 * @vf: pointer to the VF info
2093 2094 2095
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
2096
 * called from the VF to get vsi stats
2097 2098 2099
 **/
static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
2100 2101
	struct virtchnl_queue_select *vqs =
	    (struct virtchnl_queue_select *)msg;
2102 2103 2104 2105 2106 2107 2108
	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));

2109
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2110 2111 2112 2113 2114 2115 2116 2117 2118
		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;
	}

2119
	vsi = pf->vsi[vf->lan_vsi_idx];
2120 2121 2122 2123 2124
	if (!vsi) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}
	i40e_update_eth_stats(vsi);
2125
	stats = vsi->eth_stats;
2126 2127

error_param:
2128
	/* send the response back to the VF */
2129
	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
2130 2131 2132
				      (u8 *)&stats, sizeof(stats));
}

2133
/* If the VF is not trusted restrict the number of MAC/VLAN it can program */
2134
#define I40E_VC_MAX_MAC_ADDR_PER_VF 12
2135 2136
#define I40E_VC_MAX_VLAN_PER_VF 8

2137 2138
/**
 * i40e_check_vf_permission
2139
 * @vf: pointer to the VF info
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
 * @macaddr: pointer to the MAC Address being checked
 *
 * Check if the VF has permission to add or delete unicast MAC address
 * filters and return error code -EPERM if not.  Then check if the
 * address filter requested is broadcast or zero and if so return
 * an invalid MAC address error code.
 **/
static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
{
	struct i40e_pf *pf = vf->pf;
	int ret = 0;

	if (is_broadcast_ether_addr(macaddr) ||
		   is_zero_ether_addr(macaddr)) {
		dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
		ret = I40E_ERR_INVALID_MAC_ADDR;
2156
	} else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
2157
		   !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2158
		   !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
2159 2160 2161
		/* 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.
2162
		 * Unless the VF is privileged and then it can do whatever.
2163 2164
		 * The VF may request to set the MAC address filter already
		 * assigned to it so do not return an error in that case.
2165 2166
		 */
		dev_err(&pf->pdev->dev,
2167
			"VF attempting to override administratively set MAC address, reload the VF driver to resume normal operation\n");
2168
		ret = -EPERM;
2169 2170 2171 2172 2173
	} else if ((vf->num_mac >= I40E_VC_MAX_MAC_ADDR_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 functionality\n");
		ret = -EPERM;
2174 2175 2176 2177
	}
	return ret;
}

2178 2179
/**
 * i40e_vc_add_mac_addr_msg
2180
 * @vf: pointer to the VF info
2181 2182 2183 2184 2185 2186 2187
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * add guest mac address filter
 **/
static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
2188 2189
	struct virtchnl_ether_addr_list *al =
	    (struct virtchnl_ether_addr_list *)msg;
2190 2191 2192
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = al->vsi_id;
2193
	i40e_status ret = 0;
2194 2195
	int i;

2196
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2197
	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2198
		ret = I40E_ERR_PARAM;
2199 2200 2201 2202
		goto error_param;
	}

	for (i = 0; i < al->num_elements; i++) {
2203 2204
		ret = i40e_check_vf_permission(vf, al->list[i].addr);
		if (ret)
2205 2206
			goto error_param;
	}
2207
	vsi = pf->vsi[vf->lan_vsi_idx];
2208

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

2214 2215 2216 2217
	/* add new addresses to the list */
	for (i = 0; i < al->num_elements; i++) {
		struct i40e_mac_filter *f;

2218
		f = i40e_find_mac(vsi, al->list[i].addr);
2219
		if (!f)
2220
			f = i40e_add_mac_filter(vsi, al->list[i].addr);
2221 2222 2223

		if (!f) {
			dev_err(&pf->pdev->dev,
2224 2225
				"Unable to add MAC filter %pM for VF %d\n",
				 al->list[i].addr, vf->vf_id);
2226
			ret = I40E_ERR_PARAM;
2227
			spin_unlock_bh(&vsi->mac_filter_hash_lock);
2228
			goto error_param;
2229 2230
		} else {
			vf->num_mac++;
2231 2232
		}
	}
2233
	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2234 2235

	/* program the updated filter list */
M
Mitch Williams 已提交
2236 2237 2238 2239
	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);
2240 2241

error_param:
2242
	/* send the response to the VF */
2243
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
2244
				       ret);
2245 2246 2247 2248
}

/**
 * i40e_vc_del_mac_addr_msg
2249
 * @vf: pointer to the VF info
2250 2251 2252 2253 2254 2255 2256
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * remove guest mac address filter
 **/
static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
2257 2258
	struct virtchnl_ether_addr_list *al =
	    (struct virtchnl_ether_addr_list *)msg;
2259 2260 2261
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = al->vsi_id;
2262
	i40e_status ret = 0;
2263 2264
	int i;

2265
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2266
	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2267
		ret = I40E_ERR_PARAM;
2268 2269
		goto error_param;
	}
2270 2271

	for (i = 0; i < al->num_elements; i++) {
2272 2273
		if (is_broadcast_ether_addr(al->list[i].addr) ||
		    is_zero_ether_addr(al->list[i].addr)) {
2274 2275
			dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
				al->list[i].addr, vf->vf_id);
2276
			ret = I40E_ERR_INVALID_MAC_ADDR;
2277
			goto error_param;
2278
		}
2279
	}
2280
	vsi = pf->vsi[vf->lan_vsi_idx];
2281

2282
	spin_lock_bh(&vsi->mac_filter_hash_lock);
2283 2284
	/* delete addresses from the list */
	for (i = 0; i < al->num_elements; i++)
2285
		if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
2286
			ret = I40E_ERR_INVALID_MAC_ADDR;
2287
			spin_unlock_bh(&vsi->mac_filter_hash_lock);
2288
			goto error_param;
2289 2290
		} else {
			vf->num_mac--;
2291 2292
		}

2293
	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2294 2295

	/* program the updated filter list */
M
Mitch Williams 已提交
2296 2297 2298 2299
	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);
2300 2301

error_param:
2302
	/* send the response to the VF */
2303
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
2304
				       ret);
2305 2306 2307 2308
}

/**
 * i40e_vc_add_vlan_msg
2309
 * @vf: pointer to the VF info
2310 2311 2312 2313 2314 2315 2316
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * program guest vlan id
 **/
static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
2317 2318
	struct virtchnl_vlan_filter_list *vfl =
	    (struct virtchnl_vlan_filter_list *)msg;
2319 2320 2321 2322 2323 2324
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = vfl->vsi_id;
	i40e_status aq_ret = 0;
	int i;

2325 2326 2327 2328 2329 2330
	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;
	}
2331
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
	    !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;
		}
	}
2345
	vsi = pf->vsi[vf->lan_vsi_idx];
2346 2347 2348 2349 2350 2351 2352 2353 2354
	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]);
2355 2356
		if (!ret)
			vf->num_vlan++;
J
Jesse Brandeburg 已提交
2357

2358
		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2359 2360 2361 2362
			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
							   true,
							   vfl->vlan_id[i],
							   NULL);
2363
		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2364 2365 2366 2367 2368
			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
							   true,
							   vfl->vlan_id[i],
							   NULL);

2369 2370
		if (ret)
			dev_err(&pf->pdev->dev,
2371 2372
				"Unable to add VLAN filter %d for VF %d, error %d\n",
				vfl->vlan_id[i], vf->vf_id, ret);
2373 2374 2375
	}

error_param:
2376
	/* send the response to the VF */
2377
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
2378 2379 2380 2381
}

/**
 * i40e_vc_remove_vlan_msg
2382
 * @vf: pointer to the VF info
2383 2384 2385 2386 2387 2388 2389
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * remove programmed guest vlan id
 **/
static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
2390 2391
	struct virtchnl_vlan_filter_list *vfl =
	    (struct virtchnl_vlan_filter_list *)msg;
2392 2393 2394 2395 2396 2397
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = vfl->vsi_id;
	i40e_status aq_ret = 0;
	int i;

2398
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
	    !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;
		}
	}

2411
	vsi = pf->vsi[vf->lan_vsi_idx];
2412 2413 2414 2415 2416 2417
	if (vsi->info.pvid) {
		aq_ret = I40E_ERR_PARAM;
		goto error_param;
	}

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

2421
		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2422 2423 2424 2425
			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
							   false,
							   vfl->vlan_id[i],
							   NULL);
2426
		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2427 2428 2429 2430
			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
							   false,
							   vfl->vlan_id[i],
							   NULL);
2431 2432 2433
	}

error_param:
2434
	/* send the response to the VF */
2435
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
2436 2437
}

2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
/**
 * 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;

2452 2453
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2454 2455 2456 2457 2458 2459 2460 2461 2462
		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 */
2463
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
				       aq_ret);
}

/**
 * i40e_vc_iwarp_qvmap_msg
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 * @config: config qvmap or release it
 *
 * called from the VF for the iwarp msgs
 **/
static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, u16 msglen,
				   bool config)
{
2479 2480
	struct virtchnl_iwarp_qvlist_info *qvlist_info =
				(struct virtchnl_iwarp_qvlist_info *)msg;
2481 2482
	i40e_status aq_ret = 0;

2483 2484
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
		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,
2499 2500
			       config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
			       VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
2501 2502 2503
			       aq_ret);
}

2504 2505 2506 2507 2508 2509 2510 2511 2512 2513
/**
 * i40e_vc_config_rss_key
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * Configure the VF's RSS key
 **/
static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
2514 2515
	struct virtchnl_rss_key *vrk =
		(struct virtchnl_rss_key *)msg;
2516 2517 2518 2519 2520
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = vrk->vsi_id;
	i40e_status aq_ret = 0;

2521
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
	    !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 */
2532
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545
				       aq_ret);
}

/**
 * i40e_vc_config_rss_lut
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * Configure the VF's RSS LUT
 **/
static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
2546 2547
	struct virtchnl_rss_lut *vrl =
		(struct virtchnl_rss_lut *)msg;
2548 2549 2550 2551 2552
	struct i40e_pf *pf = vf->pf;
	struct i40e_vsi *vsi = NULL;
	u16 vsi_id = vrl->vsi_id;
	i40e_status aq_ret = 0;

2553
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563
	    !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:
2564
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
				       aq_ret);
}

/**
 * i40e_vc_get_rss_hena
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * Return the RSS HENA bits allowed by the hardware
 **/
static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
2578
	struct virtchnl_rss_hena *vrh = NULL;
2579 2580 2581 2582
	struct i40e_pf *pf = vf->pf;
	i40e_status aq_ret = 0;
	int len = 0;

2583
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2584 2585 2586
		aq_ret = I40E_ERR_PARAM;
		goto err;
	}
2587
	len = sizeof(struct virtchnl_rss_hena);
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597

	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 */
2598
	aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
2599
					aq_ret, (u8 *)vrh, len);
M
Mitch Williams 已提交
2600
	kfree(vrh);
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
	return aq_ret;
}

/**
 * i40e_vc_set_rss_hena
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * Set the RSS HENA bits for the VF
 **/
static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
{
2614 2615
	struct virtchnl_rss_hena *vrh =
		(struct virtchnl_rss_hena *)msg;
2616 2617 2618 2619
	struct i40e_pf *pf = vf->pf;
	struct i40e_hw *hw = &pf->hw;
	i40e_status aq_ret = 0;

2620
	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2621 2622 2623 2624 2625 2626 2627 2628 2629
		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:
2630
	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
2631 2632
}

2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
/**
 * i40e_vc_enable_vlan_stripping
 * @vf: pointer to the VF info
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 *
 * Enable vlan header stripping for the VF
 **/
static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
					 u16 msglen)
{
	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
 * @msglen: msg length
 *
 * Disable vlan header stripping for the VF
 **/
static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
					  u16 msglen)
{
	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);
}

2687 2688
/**
 * i40e_vc_process_vf_msg
2689 2690
 * @pf: pointer to the PF structure
 * @vf_id: source VF id
2691 2692 2693 2694 2695
 * @msg: pointer to the msg buffer
 * @msglen: msg length
 * @msghndl: msg handle
 *
 * called from the common aeq/arq handler to
2696
 * process request from VF
2697
 **/
2698
int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
2699 2700 2701
			   u32 v_retval, u8 *msg, u16 msglen)
{
	struct i40e_hw *hw = &pf->hw;
2702
	int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
2703
	struct i40e_vf *vf;
2704 2705 2706
	int ret;

	pf->vf_aq_requests++;
2707
	if (local_vf_id >= pf->num_alloc_vfs)
2708
		return -EINVAL;
2709
	vf = &(pf->vf[local_vf_id]);
2710 2711 2712 2713 2714

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

2715
	/* perform basic checks on the msg */
2716
	ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
2717

2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
	/* 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;
	}

2731
	if (ret) {
2732
		i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
2733
		dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
2734
			local_vf_id, v_opcode, msglen);
2735 2736 2737 2738 2739 2740
		switch (ret) {
		case VIRTCHNL_ERR_PARAM:
			return -EPERM;
		default:
			return -EINVAL;
		}
2741
	}
2742

2743
	switch (v_opcode) {
2744
	case VIRTCHNL_OP_VERSION:
2745
		ret = i40e_vc_get_version_msg(vf, msg);
2746
		break;
2747
	case VIRTCHNL_OP_GET_VF_RESOURCES:
2748
		ret = i40e_vc_get_vf_resources_msg(vf, msg);
2749
		break;
2750
	case VIRTCHNL_OP_RESET_VF:
M
Mitch Williams 已提交
2751 2752
		i40e_vc_reset_vf_msg(vf);
		ret = 0;
2753
		break;
2754
	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
2755 2756
		ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
		break;
2757
	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2758 2759
		ret = i40e_vc_config_queues_msg(vf, msg, msglen);
		break;
2760
	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
2761 2762
		ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
		break;
2763
	case VIRTCHNL_OP_ENABLE_QUEUES:
2764
		ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
M
Mitch Williams 已提交
2765
		i40e_vc_notify_vf_link_state(vf);
2766
		break;
2767
	case VIRTCHNL_OP_DISABLE_QUEUES:
2768 2769
		ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
		break;
2770
	case VIRTCHNL_OP_ADD_ETH_ADDR:
2771 2772
		ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
		break;
2773
	case VIRTCHNL_OP_DEL_ETH_ADDR:
2774 2775
		ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
		break;
2776
	case VIRTCHNL_OP_ADD_VLAN:
2777 2778
		ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
		break;
2779
	case VIRTCHNL_OP_DEL_VLAN:
2780 2781
		ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
		break;
2782
	case VIRTCHNL_OP_GET_STATS:
2783 2784
		ret = i40e_vc_get_stats_msg(vf, msg, msglen);
		break;
2785
	case VIRTCHNL_OP_IWARP:
2786 2787
		ret = i40e_vc_iwarp_msg(vf, msg, msglen);
		break;
2788
	case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
2789 2790
		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, true);
		break;
2791
	case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
2792 2793
		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, false);
		break;
2794
	case VIRTCHNL_OP_CONFIG_RSS_KEY:
2795 2796
		ret = i40e_vc_config_rss_key(vf, msg, msglen);
		break;
2797
	case VIRTCHNL_OP_CONFIG_RSS_LUT:
2798 2799
		ret = i40e_vc_config_rss_lut(vf, msg, msglen);
		break;
2800
	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
2801 2802
		ret = i40e_vc_get_rss_hena(vf, msg, msglen);
		break;
2803
	case VIRTCHNL_OP_SET_RSS_HENA:
2804 2805
		ret = i40e_vc_set_rss_hena(vf, msg, msglen);
		break;
2806 2807 2808 2809 2810 2811
	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
		ret = i40e_vc_enable_vlan_stripping(vf, msg, msglen);
		break;
	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
		ret = i40e_vc_disable_vlan_stripping(vf, msg, msglen);
		break;
2812 2813 2814
	case VIRTCHNL_OP_REQUEST_QUEUES:
		ret = i40e_vc_request_queues_msg(vf, msg, msglen);
		break;
2815

2816
	case VIRTCHNL_OP_UNKNOWN:
2817
	default:
2818
		dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
2819
			v_opcode, local_vf_id);
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
		ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
					      I40E_ERR_NOT_IMPLEMENTED);
		break;
	}

	return ret;
}

/**
 * i40e_vc_process_vflr_event
2830
 * @pf: pointer to the PF structure
2831 2832
 *
 * called from the vlfr irq handler to
2833
 * free up VF resources and state variables
2834 2835 2836 2837
 **/
int i40e_vc_process_vflr_event(struct i40e_pf *pf)
{
	struct i40e_hw *hw = &pf->hw;
2838
	u32 reg, reg_idx, bit_idx;
2839
	struct i40e_vf *vf;
2840
	int vf_id;
2841

2842
	if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
2843 2844
		return 0;

M
Mitch Williams 已提交
2845 2846 2847 2848 2849
	/* 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.
	 */
2850 2851 2852 2853 2854
	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
	reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
	i40e_flush(hw);

2855
	clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
2856 2857 2858
	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;
2859
		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
2860 2861
		vf = &pf->vf[vf_id];
		reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
2862
		if (reg & BIT(bit_idx))
2863
			/* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
2864
			i40e_reset_vf(vf, true);
2865 2866 2867 2868 2869 2870 2871 2872
	}

	return 0;
}

/**
 * i40e_ndo_set_vf_mac
 * @netdev: network interface device structure
2873
 * @vf_id: VF identifier
2874 2875
 * @mac: mac address
 *
2876
 * program VF mac address
2877 2878 2879 2880 2881 2882 2883 2884 2885
 **/
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;
2886
	int bkt;
2887 2888 2889 2890 2891 2892 2893 2894 2895 2896

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

	vf = &(pf->vf[vf_id]);
2897
	vsi = pf->vsi[vf->lan_vsi_idx];
2898
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
2899 2900 2901
		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
			vf_id);
		ret = -EAGAIN;
2902 2903 2904
		goto error_param;
	}

2905
	if (is_multicast_ether_addr(mac)) {
2906
		dev_err(&pf->pdev->dev,
2907
			"Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
2908 2909 2910 2911
		ret = -EINVAL;
		goto error_param;
	}

K
Kiran Patil 已提交
2912
	/* Lock once because below invoked function add/del_filter requires
2913
	 * mac_filter_hash_lock to be held
K
Kiran Patil 已提交
2914
	 */
2915
	spin_lock_bh(&vsi->mac_filter_hash_lock);
K
Kiran Patil 已提交
2916

2917
	/* delete the temporary mac address */
2918
	if (!is_zero_ether_addr(vf->default_lan_addr.addr))
2919
		i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
2920

2921 2922 2923
	/* Delete all the filters for this VSI - we're going to kill it
	 * anyway.
	 */
2924
	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist)
2925
		__i40e_del_filter(vsi, f);
2926

2927
	spin_unlock_bh(&vsi->mac_filter_hash_lock);
K
Kiran Patil 已提交
2928

2929
	/* program mac filter */
J
Jesse Brandeburg 已提交
2930
	if (i40e_sync_vsi_filters(vsi)) {
2931 2932 2933 2934
		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
		ret = -EIO;
		goto error_param;
	}
2935
	ether_addr_copy(vf->default_lan_addr.addr, mac);
2936 2937 2938 2939 2940 2941 2942 2943 2944 2945

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

2946
	/* Force the VF driver stop so it has to reload with new MAC address */
2947
	i40e_vc_disable_vf(vf);
2948 2949 2950 2951 2952 2953
	dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");

error_param:
	return ret;
}

2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
/**
 * 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;
}

2982 2983 2984
/**
 * i40e_ndo_set_vf_port_vlan
 * @netdev: network interface device structure
2985
 * @vf_id: VF identifier
2986 2987
 * @vlan_id: mac address
 * @qos: priority setting
2988
 * @vlan_proto: vlan protocol
2989
 *
2990
 * program VF vlan id and/or qos
2991
 **/
2992 2993
int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
			      u16 vlan_id, u8 qos, __be16 vlan_proto)
2994
{
2995
	u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
	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;

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

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

3015 3016 3017 3018 3019 3020
	if (vlan_proto != htons(ETH_P_8021Q)) {
		dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
		ret = -EPROTONOSUPPORT;
		goto error_pvid;
	}

3021
	vf = &(pf->vf[vf_id]);
3022
	vsi = pf->vsi[vf->lan_vsi_idx];
3023
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3024 3025 3026
		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
			vf_id);
		ret = -EAGAIN;
3027 3028 3029
		goto error_pvid;
	}

3030
	if (le16_to_cpu(vsi->info.pvid) == vlanprio)
3031 3032 3033
		/* duplicate request, so just return success */
		goto error_pvid;

3034
	if (i40e_vsi_has_vlans(vsi)) {
3035 3036 3037
		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);
3038 3039 3040 3041
		/* Administrator Error - knock the VF offline until he does
		 * the right thing by reconfiguring his network correctly
		 * and then reloading the VF driver.
		 */
3042
		i40e_vc_disable_vf(vf);
M
Mitch Williams 已提交
3043 3044
		/* During reset the VF got a new VSI, so refresh the pointer. */
		vsi = pf->vsi[vf->lan_vsi_idx];
3045
	}
3046

3047 3048 3049
	/* Locked once because multiple functions below iterate list */
	spin_lock_bh(&vsi->mac_filter_hash_lock);

3050 3051
	/* Check for condition where there was already a port VLAN ID
	 * filter set and now it is being deleted by setting it to zero.
3052 3053
	 * Additionally check for the condition where there was a port
	 * VLAN but now there is a new and different port VLAN being set.
3054 3055 3056 3057
	 * 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.
	 */
3058
	if ((!(vlan_id || qos) ||
3059
	    vlanprio != le16_to_cpu(vsi->info.pvid)) &&
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069
	    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;
		}
	}
3070

3071
	if (vsi->info.pvid) {
3072 3073 3074
		/* remove all filters on the old VLAN */
		i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
					   VLAN_VID_MASK));
3075
	}
3076

J
Jia-Ju Bai 已提交
3077
	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3078
	if (vlan_id || qos)
3079
		ret = i40e_vsi_add_pvid(vsi, vlanprio);
3080
	else
G
Greg Rose 已提交
3081
		i40e_vsi_remove_pvid(vsi);
J
Jia-Ju Bai 已提交
3082
	spin_lock_bh(&vsi->mac_filter_hash_lock);
3083 3084 3085 3086 3087

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

3088 3089
		/* add new VLAN filter for each MAC */
		ret = i40e_add_vlan_all_mac(vsi, vlan_id);
3090 3091 3092 3093
		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);
3094
			spin_unlock_bh(&vsi->mac_filter_hash_lock);
3095 3096
			goto error_pvid;
		}
3097 3098 3099

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

3102 3103 3104 3105 3106
	spin_unlock_bh(&vsi->mac_filter_hash_lock);

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

3107 3108 3109 3110
	if (ret) {
		dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
		goto error_pvid;
	}
3111

G
Greg Rose 已提交
3112 3113 3114 3115
	/* 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);
3116 3117 3118 3119 3120 3121
	ret = 0;

error_pvid:
	return ret;
}

3122 3123
#define I40E_BW_CREDIT_DIVISOR 50     /* 50Mbps per BW credit */
#define I40E_MAX_BW_INACTIVE_ACCUM 4  /* device can accumulate 4 credits max */
3124 3125 3126
/**
 * i40e_ndo_set_vf_bw
 * @netdev: network interface device structure
3127 3128
 * @vf_id: VF identifier
 * @tx_rate: Tx rate
3129
 *
3130
 * configure VF Tx rate
3131
 **/
3132 3133
int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
		       int max_tx_rate)
3134
{
3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148
	struct i40e_netdev_priv *np = netdev_priv(netdev);
	struct i40e_pf *pf = np->vsi->back;
	struct i40e_vsi *vsi;
	struct i40e_vf *vf;
	int speed = 0;
	int ret = 0;

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

3149
	if (min_tx_rate) {
3150
		dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
3151 3152 3153 3154
			min_tx_rate, vf_id);
		return -EINVAL;
	}

3155
	vf = &(pf->vf[vf_id]);
3156
	vsi = pf->vsi[vf->lan_vsi_idx];
3157
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3158 3159 3160
		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
			vf_id);
		ret = -EAGAIN;
3161 3162 3163 3164 3165 3166 3167
		goto error;
	}

	switch (pf->hw.phy.link_info.link_speed) {
	case I40E_LINK_SPEED_40GB:
		speed = 40000;
		break;
3168 3169 3170
	case I40E_LINK_SPEED_25GB:
		speed = 25000;
		break;
3171 3172 3173
	case I40E_LINK_SPEED_20GB:
		speed = 20000;
		break;
3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
	case I40E_LINK_SPEED_10GB:
		speed = 10000;
		break;
	case I40E_LINK_SPEED_1GB:
		speed = 1000;
		break;
	default:
		break;
	}

3184
	if (max_tx_rate > speed) {
3185
		dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.\n",
3186
			max_tx_rate, vf->vf_id);
3187 3188 3189 3190
		ret = -EINVAL;
		goto error;
	}

3191 3192 3193 3194 3195
	if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
		dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
		max_tx_rate = 50;
	}

3196
	/* Tx rate credits are in values of 50Mbps, 0 is disabled*/
3197 3198 3199
	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
					  max_tx_rate / I40E_BW_CREDIT_DIVISOR,
					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
3200
	if (ret) {
3201
		dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
3202 3203 3204 3205
			ret);
		ret = -EIO;
		goto error;
	}
3206
	vf->tx_rate = max_tx_rate;
3207 3208
error:
	return ret;
3209 3210 3211 3212 3213
}

/**
 * i40e_ndo_get_vf_config
 * @netdev: network interface device structure
3214 3215
 * @vf_id: VF identifier
 * @ivi: VF configuration structure
3216
 *
3217
 * return VF configuration
3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236
 **/
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;

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

	vf = &(pf->vf[vf_id]);
	/* first vsi is always the LAN vsi */
3237
	vsi = pf->vsi[vf->lan_vsi_idx];
3238
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3239 3240 3241
		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
			vf_id);
		ret = -EAGAIN;
3242 3243 3244 3245 3246
		goto error_param;
	}

	ivi->vf = vf_id;

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

3249 3250
	ivi->max_tx_rate = vf->tx_rate;
	ivi->min_tx_rate = 0;
3251 3252 3253
	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;
3254 3255 3256 3257 3258 3259
	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;
3260
	ivi->spoofchk = vf->spoofchk;
3261
	ivi->trusted = vf->trusted;
3262 3263 3264 3265 3266
	ret = 0;

error_param:
	return ret;
}
M
Mitch Williams 已提交
3267 3268 3269 3270

/**
 * i40e_ndo_set_vf_link_state
 * @netdev: network interface device structure
3271
 * @vf_id: VF identifier
M
Mitch Williams 已提交
3272 3273 3274 3275 3276 3277 3278 3279
 * @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;
3280
	struct virtchnl_pf_event pfe;
M
Mitch Williams 已提交
3281 3282
	struct i40e_hw *hw = &pf->hw;
	struct i40e_vf *vf;
3283
	int abs_vf_id;
M
Mitch Williams 已提交
3284 3285 3286 3287 3288 3289 3290 3291 3292 3293
	int ret = 0;

	/* 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];
3294
	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
M
Mitch Williams 已提交
3295

3296
	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
3297
	pfe.severity = PF_EVENT_SEVERITY_INFO;
M
Mitch Williams 已提交
3298 3299 3300 3301 3302 3303 3304

	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 =
3305
			(enum virtchnl_link_speed)
M
Mitch Williams 已提交
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
			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;
		pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
		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 */
3325
	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
M
Mitch Williams 已提交
3326 3327 3328 3329 3330
			       0, (u8 *)&pfe, sizeof(pfe), NULL);

error_out:
	return ret;
}
3331 3332 3333 3334

/**
 * i40e_ndo_set_vf_spoofchk
 * @netdev: network interface device structure
3335
 * @vf_id: VF identifier
3336 3337 3338 3339
 * @enable: flag to enable or disable feature
 *
 * Enable or disable VF spoof checking
 **/
3340
int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
{
	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;

	/* 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]);
3358
	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3359 3360 3361 3362 3363
		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
			vf_id);
		ret = -EAGAIN;
		goto out;
	}
3364 3365 3366 3367 3368 3369

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

	vf->spoofchk = enable;
	memset(&ctxt, 0, sizeof(ctxt));
3370
	ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
3371 3372 3373
	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 已提交
3374 3375
		ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
					I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
3376 3377 3378 3379 3380 3381 3382 3383 3384
	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:
	return ret;
}
3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417

/**
 * 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;

	/* validate the request */
	if (vf_id >= pf->num_alloc_vfs) {
		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
		return -EINVAL;
	}

	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
		dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
		return -EINVAL;
	}

	vf = &pf->vf[vf_id];

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

	vf->trusted = setting;
3418
	i40e_vc_disable_vf(vf);
3419 3420 3421 3422 3423
	dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
		 vf_id, setting ? "" : "un");
out:
	return ret;
}