xref: /openbmc/linux/drivers/net/ethernet/intel/i40e/i40e_virtchnl_pf.c (revision 28efb0046512e8a13ed9f9bdf0d68d10bbfbe9cf)
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2016 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26 
27 #include "i40e.h"
28 
29 /*********************notification routines***********************/
30 
31 /**
32  * i40e_vc_vf_broadcast
33  * @pf: pointer to the PF structure
34  * @opcode: operation code
35  * @retval: return value
36  * @msg: pointer to the msg buffer
37  * @msglen: msg length
38  *
39  * send a message to all VFs on a given PF
40  **/
41 static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
42 				 enum virtchnl_ops v_opcode,
43 				 i40e_status v_retval, u8 *msg,
44 				 u16 msglen)
45 {
46 	struct i40e_hw *hw = &pf->hw;
47 	struct i40e_vf *vf = pf->vf;
48 	int i;
49 
50 	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 		if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
54 		    !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
55 			continue;
56 
57 		/* Ignore return value on purpose - a given VF may fail, but
58 		 * we need to keep going and send to all of them
59 		 */
60 		i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
61 				       msg, msglen, NULL);
62 	}
63 }
64 
65 /**
66  * i40e_vc_notify_vf_link_state
67  * @vf: pointer to the VF structure
68  *
69  * send a link status message to a single VF
70  **/
71 static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
72 {
73 	struct virtchnl_pf_event pfe;
74 	struct i40e_pf *pf = vf->pf;
75 	struct i40e_hw *hw = &pf->hw;
76 	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 	if (vf->link_forced) {
82 		pfe.event_data.link_event.link_status = vf->link_up;
83 		pfe.event_data.link_event.link_speed =
84 			(vf->link_up ? I40E_LINK_SPEED_40GB : 0);
85 	} else {
86 		pfe.event_data.link_event.link_status =
87 			ls->link_info & I40E_AQ_LINK_UP;
88 		pfe.event_data.link_event.link_speed =
89 			(enum virtchnl_link_speed)ls->link_speed;
90 	}
91 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
92 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
93 }
94 
95 /**
96  * i40e_vc_notify_link_state
97  * @pf: pointer to the PF structure
98  *
99  * send a link status message to all VFs on a given PF
100  **/
101 void i40e_vc_notify_link_state(struct i40e_pf *pf)
102 {
103 	int i;
104 
105 	for (i = 0; i < pf->num_alloc_vfs; i++)
106 		i40e_vc_notify_vf_link_state(&pf->vf[i]);
107 }
108 
109 /**
110  * i40e_vc_notify_reset
111  * @pf: pointer to the PF structure
112  *
113  * indicate a pending reset to all VFs on a given PF
114  **/
115 void i40e_vc_notify_reset(struct i40e_pf *pf)
116 {
117 	struct virtchnl_pf_event pfe;
118 
119 	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
120 	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
121 	i40e_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, 0,
122 			     (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
123 }
124 
125 /**
126  * i40e_vc_notify_vf_reset
127  * @vf: pointer to the VF structure
128  *
129  * indicate a pending reset to the given VF
130  **/
131 void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
132 {
133 	struct virtchnl_pf_event pfe;
134 	int abs_vf_id;
135 
136 	/* validate the request */
137 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
138 		return;
139 
140 	/* verify if the VF is in either init or active before proceeding */
141 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
142 	    !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
143 		return;
144 
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 /***********************misc routines*****************************/
154 
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 	int i;
164 
165 	i40e_vc_notify_vf_reset(vf);
166 
167 	/* We want to ensure that an actual reset occurs initiated after this
168 	 * function was called. However, we do not want to wait forever, so
169 	 * we'll give a reasonable time and print a message if we failed to
170 	 * ensure a reset.
171 	 */
172 	for (i = 0; i < 20; i++) {
173 		if (i40e_reset_vf(vf, false))
174 			return;
175 		usleep_range(10000, 20000);
176 	}
177 
178 	dev_warn(&vf->pf->pdev->dev,
179 		 "Failed to initiate reset for VF %d after 200 milliseconds\n",
180 		 vf->vf_id);
181 }
182 
183 /**
184  * i40e_vc_isvalid_vsi_id
185  * @vf: pointer to the VF info
186  * @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  * @vsi_id: vsi id
202  * @qid: vsi relative queue id
203  *
204  * check for the valid queue id
205  **/
206 static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
207 					    u8 qid)
208 {
209 	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  * @vf: pointer to the VF info
218  * @vector_id: VF relative vector id
219  *
220  * check for the valid vector id
221  **/
222 static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
223 {
224 	struct i40e_pf *pf = vf->pf;
225 
226 	return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
227 }
228 
229 /***********************vf resource mgmt routines*****************/
230 
231 /**
232  * 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  * @vsi_queue_id: vsi relative queue id
236  *
237  * return PF relative queue id
238  **/
239 static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
240 				   u8 vsi_queue_id)
241 {
242 	struct i40e_pf *pf = vf->pf;
243 	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
244 	u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
245 
246 	if (!vsi)
247 		return pf_queue_id;
248 
249 	if (le16_to_cpu(vsi->info.mapping_flags) &
250 	    I40E_AQ_VSI_QUE_MAP_NONCONTIG)
251 		pf_queue_id =
252 			le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
253 	else
254 		pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
255 			      vsi_queue_id;
256 
257 	return pf_queue_id;
258 }
259 
260 /**
261  * i40e_config_irq_link_list
262  * @vf: pointer to the VF info
263  * @vsi_id: id of VSI as given by the FW
264  * @vecmap: irq map info
265  *
266  * configure irq link list from the map
267  **/
268 static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
269 				      struct virtchnl_vector_map *vecmap)
270 {
271 	unsigned long linklistmap = 0, tempmap;
272 	struct i40e_pf *pf = vf->pf;
273 	struct i40e_hw *hw = &pf->hw;
274 	u16 vsi_queue_id, pf_queue_id;
275 	enum i40e_queue_type qtype;
276 	u16 next_q, vector_id;
277 	u32 reg, reg_idx;
278 	u16 itr_idx = 0;
279 
280 	vector_id = vecmap->vector_id;
281 	/* setup the head */
282 	if (0 == vector_id)
283 		reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
284 	else
285 		reg_idx = I40E_VPINT_LNKLSTN(
286 		     ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
287 		     (vector_id - 1));
288 
289 	if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
290 		/* Special case - No queues mapped on this vector */
291 		wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
292 		goto irq_list_done;
293 	}
294 	tempmap = vecmap->rxq_map;
295 	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
296 		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
297 				    vsi_queue_id));
298 	}
299 
300 	tempmap = vecmap->txq_map;
301 	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
302 		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
303 				     vsi_queue_id + 1));
304 	}
305 
306 	next_q = find_first_bit(&linklistmap,
307 				(I40E_MAX_VSI_QP *
308 				 I40E_VIRTCHNL_SUPPORTED_QTYPES));
309 	vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
310 	qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
311 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
312 	reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
313 
314 	wr32(hw, reg_idx, reg);
315 
316 	while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
317 		switch (qtype) {
318 		case I40E_QUEUE_TYPE_RX:
319 			reg_idx = I40E_QINT_RQCTL(pf_queue_id);
320 			itr_idx = vecmap->rxitr_idx;
321 			break;
322 		case I40E_QUEUE_TYPE_TX:
323 			reg_idx = I40E_QINT_TQCTL(pf_queue_id);
324 			itr_idx = vecmap->txitr_idx;
325 			break;
326 		default:
327 			break;
328 		}
329 
330 		next_q = find_next_bit(&linklistmap,
331 				       (I40E_MAX_VSI_QP *
332 					I40E_VIRTCHNL_SUPPORTED_QTYPES),
333 				       next_q + 1);
334 		if (next_q <
335 		    (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
336 			vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
337 			qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
338 			pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
339 							      vsi_queue_id);
340 		} else {
341 			pf_queue_id = I40E_QUEUE_END_OF_LIST;
342 			qtype = 0;
343 		}
344 
345 		/* format for the RQCTL & TQCTL regs is same */
346 		reg = (vector_id) |
347 		    (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
348 		    (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
349 		    BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
350 		    (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
351 		wr32(hw, reg_idx, reg);
352 	}
353 
354 	/* if the vf is running in polling mode and using interrupt zero,
355 	 * need to disable auto-mask on enabling zero interrupt for VFs.
356 	 */
357 	if ((vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
358 	    (vector_id == 0)) {
359 		reg = rd32(hw, I40E_GLINT_CTL);
360 		if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
361 			reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
362 			wr32(hw, I40E_GLINT_CTL, reg);
363 		}
364 	}
365 
366 irq_list_done:
367 	i40e_flush(hw);
368 }
369 
370 /**
371  * i40e_release_iwarp_qvlist
372  * @vf: pointer to the VF.
373  *
374  **/
375 static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
376 {
377 	struct i40e_pf *pf = vf->pf;
378 	struct virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
379 	u32 msix_vf;
380 	u32 i;
381 
382 	if (!vf->qvlist_info)
383 		return;
384 
385 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
386 	for (i = 0; i < qvlist_info->num_vectors; i++) {
387 		struct virtchnl_iwarp_qv_info *qv_info;
388 		u32 next_q_index, next_q_type;
389 		struct i40e_hw *hw = &pf->hw;
390 		u32 v_idx, reg_idx, reg;
391 
392 		qv_info = &qvlist_info->qv_info[i];
393 		if (!qv_info)
394 			continue;
395 		v_idx = qv_info->v_idx;
396 		if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
397 			/* Figure out the queue after CEQ and make that the
398 			 * first queue.
399 			 */
400 			reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
401 			reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
402 			next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
403 					>> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
404 			next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
405 					>> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;
406 
407 			reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
408 			reg = (next_q_index &
409 			       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
410 			       (next_q_type <<
411 			       I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
412 
413 			wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
414 		}
415 	}
416 	kfree(vf->qvlist_info);
417 	vf->qvlist_info = NULL;
418 }
419 
420 /**
421  * i40e_config_iwarp_qvlist
422  * @vf: pointer to the VF info
423  * @qvlist_info: queue and vector list
424  *
425  * Return 0 on success or < 0 on error
426  **/
427 static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
428 				    struct virtchnl_iwarp_qvlist_info *qvlist_info)
429 {
430 	struct i40e_pf *pf = vf->pf;
431 	struct i40e_hw *hw = &pf->hw;
432 	struct virtchnl_iwarp_qv_info *qv_info;
433 	u32 v_idx, i, reg_idx, reg;
434 	u32 next_q_idx, next_q_type;
435 	u32 msix_vf, size;
436 
437 	size = sizeof(struct virtchnl_iwarp_qvlist_info) +
438 	       (sizeof(struct virtchnl_iwarp_qv_info) *
439 						(qvlist_info->num_vectors - 1));
440 	vf->qvlist_info = kzalloc(size, GFP_KERNEL);
441 	if (!vf->qvlist_info)
442 		return -ENOMEM;
443 
444 	vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
445 
446 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
447 	for (i = 0; i < qvlist_info->num_vectors; i++) {
448 		qv_info = &qvlist_info->qv_info[i];
449 		if (!qv_info)
450 			continue;
451 		v_idx = qv_info->v_idx;
452 
453 		/* Validate vector id belongs to this vf */
454 		if (!i40e_vc_isvalid_vector_id(vf, v_idx))
455 			goto err;
456 
457 		vf->qvlist_info->qv_info[i] = *qv_info;
458 
459 		reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
460 		/* We might be sharing the interrupt, so get the first queue
461 		 * index and type, push it down the list by adding the new
462 		 * queue on top. Also link it with the new queue in CEQCTL.
463 		 */
464 		reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
465 		next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
466 				I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
467 		next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
468 				I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
469 
470 		if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
471 			reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
472 			reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
473 			(v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
474 			(qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
475 			(next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
476 			(next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
477 			wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);
478 
479 			reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
480 			reg = (qv_info->ceq_idx &
481 			       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
482 			       (I40E_QUEUE_TYPE_PE_CEQ <<
483 			       I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
484 			wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
485 		}
486 
487 		if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
488 			reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
489 			(v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
490 			(qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));
491 
492 			wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
493 		}
494 	}
495 
496 	return 0;
497 err:
498 	kfree(vf->qvlist_info);
499 	vf->qvlist_info = NULL;
500 	return -EINVAL;
501 }
502 
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  * @vsi_queue_id: vsi relative queue index
508  * @info: config. info
509  *
510  * configure tx queue
511  **/
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 	struct i40e_pf *pf = vf->pf;
517 	struct i40e_hw *hw = &pf->hw;
518 	struct i40e_hmc_obj_txq tx_ctx;
519 	struct i40e_vsi *vsi;
520 	u16 pf_queue_id;
521 	u32 qtx_ctl;
522 	int ret = 0;
523 
524 	if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
525 		ret = -ENOENT;
526 		goto error_context;
527 	}
528 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
529 	vsi = i40e_find_vsi_from_id(pf, vsi_id);
530 	if (!vsi) {
531 		ret = -ENOENT;
532 		goto error_context;
533 	}
534 
535 	/* clear the context structure first */
536 	memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
537 
538 	/* only set the required fields */
539 	tx_ctx.base = info->dma_ring_addr / 128;
540 	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 	tx_ctx.head_wb_ena = info->headwb_enabled;
544 	tx_ctx.head_wb_addr = info->dma_headwb_addr;
545 
546 	/* clear the context in the HMC */
547 	ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
548 	if (ret) {
549 		dev_err(&pf->pdev->dev,
550 			"Failed to clear VF LAN Tx queue context %d, error: %d\n",
551 			pf_queue_id, ret);
552 		ret = -ENOENT;
553 		goto error_context;
554 	}
555 
556 	/* set the context in the HMC */
557 	ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
558 	if (ret) {
559 		dev_err(&pf->pdev->dev,
560 			"Failed to set VF LAN Tx queue context %d error: %d\n",
561 			pf_queue_id, ret);
562 		ret = -ENOENT;
563 		goto error_context;
564 	}
565 
566 	/* associate this queue with the PCI VF function */
567 	qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
568 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
569 		    & I40E_QTX_CTL_PF_INDX_MASK);
570 	qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
571 		     << I40E_QTX_CTL_VFVM_INDX_SHIFT)
572 		    & I40E_QTX_CTL_VFVM_INDX_MASK);
573 	wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
574 	i40e_flush(hw);
575 
576 error_context:
577 	return ret;
578 }
579 
580 /**
581  * i40e_config_vsi_rx_queue
582  * @vf: pointer to the VF info
583  * @vsi_id: id of VSI  as provided by the FW
584  * @vsi_queue_id: vsi relative queue index
585  * @info: config. info
586  *
587  * configure rx queue
588  **/
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 	struct i40e_pf *pf = vf->pf;
594 	struct i40e_hw *hw = &pf->hw;
595 	struct i40e_hmc_obj_rxq rx_ctx;
596 	u16 pf_queue_id;
597 	int ret = 0;
598 
599 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
600 
601 	/* clear the context structure first */
602 	memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
603 
604 	/* only set the required fields */
605 	rx_ctx.base = info->dma_ring_addr / 128;
606 	rx_ctx.qlen = info->ring_len;
607 
608 	if (info->splithdr_enabled) {
609 		rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2      |
610 				  I40E_RX_SPLIT_IP      |
611 				  I40E_RX_SPLIT_TCP_UDP |
612 				  I40E_RX_SPLIT_SCTP;
613 		/* header length validation */
614 		if (info->hdr_size > ((2 * 1024) - 64)) {
615 			ret = -EINVAL;
616 			goto error_param;
617 		}
618 		rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
619 
620 		/* set split mode 10b */
621 		rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
622 	}
623 
624 	/* databuffer length validation */
625 	if (info->databuffer_size > ((16 * 1024) - 128)) {
626 		ret = -EINVAL;
627 		goto error_param;
628 	}
629 	rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
630 
631 	/* max pkt. length validation */
632 	if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
633 		ret = -EINVAL;
634 		goto error_param;
635 	}
636 	rx_ctx.rxmax = info->max_pkt_size;
637 
638 	/* enable 32bytes desc always */
639 	rx_ctx.dsize = 1;
640 
641 	/* default values */
642 	rx_ctx.lrxqthresh = 2;
643 	rx_ctx.crcstrip = 1;
644 	rx_ctx.prefena = 1;
645 	rx_ctx.l2tsel = 1;
646 
647 	/* clear the context in the HMC */
648 	ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
649 	if (ret) {
650 		dev_err(&pf->pdev->dev,
651 			"Failed to clear VF LAN Rx queue context %d, error: %d\n",
652 			pf_queue_id, ret);
653 		ret = -ENOENT;
654 		goto error_param;
655 	}
656 
657 	/* set the context in the HMC */
658 	ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
659 	if (ret) {
660 		dev_err(&pf->pdev->dev,
661 			"Failed to set VF LAN Rx queue context %d error: %d\n",
662 			pf_queue_id, ret);
663 		ret = -ENOENT;
664 		goto error_param;
665 	}
666 
667 error_param:
668 	return ret;
669 }
670 
671 /**
672  * i40e_alloc_vsi_res
673  * @vf: pointer to the VF info
674  * @type: type of VSI to allocate
675  *
676  * alloc VF vsi context & resources
677  **/
678 static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
679 {
680 	struct i40e_mac_filter *f = NULL;
681 	struct i40e_pf *pf = vf->pf;
682 	struct i40e_vsi *vsi;
683 	int ret = 0;
684 
685 	vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
686 
687 	if (!vsi) {
688 		dev_err(&pf->pdev->dev,
689 			"add vsi failed for VF %d, aq_err %d\n",
690 			vf->vf_id, pf->hw.aq.asq_last_status);
691 		ret = -ENOENT;
692 		goto error_alloc_vsi_res;
693 	}
694 	if (type == I40E_VSI_SRIOV) {
695 		u64 hena = i40e_pf_get_default_rss_hena(pf);
696 		u8 broadcast[ETH_ALEN];
697 
698 		vf->lan_vsi_idx = vsi->idx;
699 		vf->lan_vsi_id = vsi->id;
700 		/* If the port VLAN has been configured and then the
701 		 * VF driver was removed then the VSI port VLAN
702 		 * configuration was destroyed.  Check if there is
703 		 * a port VLAN and restore the VSI configuration if
704 		 * needed.
705 		 */
706 		if (vf->port_vlan_id)
707 			i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
708 
709 		spin_lock_bh(&vsi->mac_filter_hash_lock);
710 		if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
711 			f = i40e_add_mac_filter(vsi,
712 						vf->default_lan_addr.addr);
713 			if (!f)
714 				dev_info(&pf->pdev->dev,
715 					 "Could not add MAC filter %pM for VF %d\n",
716 					vf->default_lan_addr.addr, vf->vf_id);
717 		}
718 		eth_broadcast_addr(broadcast);
719 		f = i40e_add_mac_filter(vsi, broadcast);
720 		if (!f)
721 			dev_info(&pf->pdev->dev,
722 				 "Could not allocate VF broadcast filter\n");
723 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
724 		wr32(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)hena);
725 		wr32(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id), (u32)(hena >> 32));
726 	}
727 
728 	/* program mac filter */
729 	ret = i40e_sync_vsi_filters(vsi);
730 	if (ret)
731 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
732 
733 	/* Set VF bandwidth if specified */
734 	if (vf->tx_rate) {
735 		ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
736 						  vf->tx_rate / 50, 0, NULL);
737 		if (ret)
738 			dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
739 				vf->vf_id, ret);
740 	}
741 
742 error_alloc_vsi_res:
743 	return ret;
744 }
745 
746 /**
747  * i40e_enable_vf_mappings
748  * @vf: pointer to the VF info
749  *
750  * enable VF mappings
751  **/
752 static void i40e_enable_vf_mappings(struct i40e_vf *vf)
753 {
754 	struct i40e_pf *pf = vf->pf;
755 	struct i40e_hw *hw = &pf->hw;
756 	u32 reg, total_queue_pairs = 0;
757 	int j;
758 
759 	/* Tell the hardware we're using noncontiguous mapping. HW requires
760 	 * that VF queues be mapped using this method, even when they are
761 	 * contiguous in real life
762 	 */
763 	i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
764 			  I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
765 
766 	/* enable VF vplan_qtable mappings */
767 	reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
768 	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
769 
770 	/* map PF queues to VF queues */
771 	for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
772 		u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
773 
774 		reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
775 		wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
776 		total_queue_pairs++;
777 	}
778 
779 	/* map PF queues to VSI */
780 	for (j = 0; j < 7; j++) {
781 		if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
782 			reg = 0x07FF07FF;	/* unused */
783 		} else {
784 			u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
785 							  j * 2);
786 			reg = qid;
787 			qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
788 						      (j * 2) + 1);
789 			reg |= qid << 16;
790 		}
791 		i40e_write_rx_ctl(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id),
792 				  reg);
793 	}
794 
795 	i40e_flush(hw);
796 }
797 
798 /**
799  * i40e_disable_vf_mappings
800  * @vf: pointer to the VF info
801  *
802  * disable VF mappings
803  **/
804 static void i40e_disable_vf_mappings(struct i40e_vf *vf)
805 {
806 	struct i40e_pf *pf = vf->pf;
807 	struct i40e_hw *hw = &pf->hw;
808 	int i;
809 
810 	/* disable qp mappings */
811 	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
812 	for (i = 0; i < I40E_MAX_VSI_QP; i++)
813 		wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
814 		     I40E_QUEUE_END_OF_LIST);
815 	i40e_flush(hw);
816 }
817 
818 /**
819  * i40e_free_vf_res
820  * @vf: pointer to the VF info
821  *
822  * free VF resources
823  **/
824 static void i40e_free_vf_res(struct i40e_vf *vf)
825 {
826 	struct i40e_pf *pf = vf->pf;
827 	struct i40e_hw *hw = &pf->hw;
828 	u32 reg_idx, reg;
829 	int i, msix_vf;
830 
831 	/* Start by disabling VF's configuration API to prevent the OS from
832 	 * accessing the VF's VSI after it's freed / invalidated.
833 	 */
834 	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
835 
836 	/* It's possible the VF had requeuested more queues than the default so
837 	 * do the accounting here when we're about to free them.
838 	 */
839 	if (vf->num_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF) {
840 		pf->queues_left += vf->num_queue_pairs -
841 				   I40E_DEFAULT_QUEUES_PER_VF;
842 	}
843 
844 	/* free vsi & disconnect it from the parent uplink */
845 	if (vf->lan_vsi_idx) {
846 		i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
847 		vf->lan_vsi_idx = 0;
848 		vf->lan_vsi_id = 0;
849 		vf->num_mac = 0;
850 	}
851 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
852 
853 	/* disable interrupts so the VF starts in a known state */
854 	for (i = 0; i < msix_vf; i++) {
855 		/* format is same for both registers */
856 		if (0 == i)
857 			reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
858 		else
859 			reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
860 						      (vf->vf_id))
861 						     + (i - 1));
862 		wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
863 		i40e_flush(hw);
864 	}
865 
866 	/* clear the irq settings */
867 	for (i = 0; i < msix_vf; i++) {
868 		/* format is same for both registers */
869 		if (0 == i)
870 			reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
871 		else
872 			reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
873 						      (vf->vf_id))
874 						     + (i - 1));
875 		reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
876 		       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
877 		wr32(hw, reg_idx, reg);
878 		i40e_flush(hw);
879 	}
880 	/* reset some of the state variables keeping track of the resources */
881 	vf->num_queue_pairs = 0;
882 	clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
883 	clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
884 }
885 
886 /**
887  * i40e_alloc_vf_res
888  * @vf: pointer to the VF info
889  *
890  * allocate VF resources
891  **/
892 static int i40e_alloc_vf_res(struct i40e_vf *vf)
893 {
894 	struct i40e_pf *pf = vf->pf;
895 	int total_queue_pairs = 0;
896 	int ret;
897 
898 	if (vf->num_req_queues &&
899 	    vf->num_req_queues <= pf->queues_left + I40E_DEFAULT_QUEUES_PER_VF)
900 		pf->num_vf_qps = vf->num_req_queues;
901 	else
902 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
903 
904 	/* allocate hw vsi context & associated resources */
905 	ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
906 	if (ret)
907 		goto error_alloc;
908 	total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
909 
910 	/* We account for each VF to get a default number of queue pairs.  If
911 	 * the VF has now requested more, we need to account for that to make
912 	 * certain we never request more queues than we actually have left in
913 	 * HW.
914 	 */
915 	if (total_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF)
916 		pf->queues_left -=
917 			total_queue_pairs - I40E_DEFAULT_QUEUES_PER_VF;
918 
919 	if (vf->trusted)
920 		set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
921 	else
922 		clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
923 
924 	/* store the total qps number for the runtime
925 	 * VF req validation
926 	 */
927 	vf->num_queue_pairs = total_queue_pairs;
928 
929 	/* VF is now completely initialized */
930 	set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
931 
932 error_alloc:
933 	if (ret)
934 		i40e_free_vf_res(vf);
935 
936 	return ret;
937 }
938 
939 #define VF_DEVICE_STATUS 0xAA
940 #define VF_TRANS_PENDING_MASK 0x20
941 /**
942  * i40e_quiesce_vf_pci
943  * @vf: pointer to the VF structure
944  *
945  * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
946  * if the transactions never clear.
947  **/
948 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
949 {
950 	struct i40e_pf *pf = vf->pf;
951 	struct i40e_hw *hw = &pf->hw;
952 	int vf_abs_id, i;
953 	u32 reg;
954 
955 	vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
956 
957 	wr32(hw, I40E_PF_PCI_CIAA,
958 	     VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
959 	for (i = 0; i < 100; i++) {
960 		reg = rd32(hw, I40E_PF_PCI_CIAD);
961 		if ((reg & VF_TRANS_PENDING_MASK) == 0)
962 			return 0;
963 		udelay(1);
964 	}
965 	return -EIO;
966 }
967 
968 /**
969  * i40e_trigger_vf_reset
970  * @vf: pointer to the VF structure
971  * @flr: VFLR was issued or not
972  *
973  * Trigger hardware to start a reset for a particular VF. Expects the caller
974  * to wait the proper amount of time to allow hardware to reset the VF before
975  * it cleans up and restores VF functionality.
976  **/
977 static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
978 {
979 	struct i40e_pf *pf = vf->pf;
980 	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 	/* Disable VF's configuration API during reset. The flag is re-enabled
987 	 * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
988 	 * It's normally disabled in i40e_free_vf_res(), but it's safer
989 	 * to do it earlier to give some time to finish to any VF config
990 	 * functions that may still be running at this point.
991 	 */
992 	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
993 
994 	/* In the case of a VFLR, the HW has already reset the VF and we
995 	 * just need to clean up, so don't hit the VFRTRIG register.
996 	 */
997 	if (!flr) {
998 		/* reset VF using VPGEN_VFRTRIG reg */
999 		reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1000 		reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1001 		wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1002 		i40e_flush(hw);
1003 	}
1004 	/* clear the VFLR bit in GLGEN_VFLRSTAT */
1005 	reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
1006 	bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
1007 	wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1008 	i40e_flush(hw);
1009 
1010 	if (i40e_quiesce_vf_pci(vf))
1011 		dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
1012 			vf->vf_id);
1013 }
1014 
1015 /**
1016  * i40e_cleanup_reset_vf
1017  * @vf: pointer to the VF structure
1018  *
1019  * Cleanup a VF after the hardware reset is finished. Expects the caller to
1020  * have verified whether the reset is finished properly, and ensure the
1021  * minimum amount of wait time has passed.
1022  **/
1023 static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
1024 {
1025 	struct i40e_pf *pf = vf->pf;
1026 	struct i40e_hw *hw = &pf->hw;
1027 	u32 reg;
1028 
1029 	/* free VF resources to begin resetting the VSI state */
1030 	i40e_free_vf_res(vf);
1031 
1032 	/* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
1033 	 * By doing this we allow HW to access VF memory at any point. If we
1034 	 * did it any sooner, HW could access memory while it was being freed
1035 	 * in i40e_free_vf_res(), causing an IOMMU fault.
1036 	 *
1037 	 * On the other hand, this needs to be done ASAP, because the VF driver
1038 	 * is waiting for this to happen and may report a timeout. It's
1039 	 * harmless, but it gets logged into Guest OS kernel log, so best avoid
1040 	 * it.
1041 	 */
1042 	reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1043 	reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1044 	wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1045 
1046 	/* 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 		set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1051 		clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1052 		/* Do not notify the client during VF init */
1053 		if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
1054 					&vf->vf_states))
1055 			i40e_notify_client_of_vf_reset(pf, abs_vf_id);
1056 		vf->num_vlan = 0;
1057 	}
1058 
1059 	/* Tell the VF driver the reset is done. This needs to be done only
1060 	 * after VF has been fully initialized, because the VF driver may
1061 	 * request resources immediately after setting this flag.
1062 	 */
1063 	wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1064 }
1065 
1066 /**
1067  * i40e_reset_vf
1068  * @vf: pointer to the VF structure
1069  * @flr: VFLR was issued or not
1070  *
1071  * Returns true if the VF is reset, false otherwise.
1072  **/
1073 bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
1074 {
1075 	struct i40e_pf *pf = vf->pf;
1076 	struct i40e_hw *hw = &pf->hw;
1077 	bool rsd = false;
1078 	u32 reg;
1079 	int i;
1080 
1081 	/* If the VFs have been disabled, this means something else is
1082 	 * resetting the VF, so we shouldn't continue.
1083 	 */
1084 	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1085 		return false;
1086 
1087 	i40e_trigger_vf_reset(vf, flr);
1088 
1089 	/* poll VPGEN_VFRSTAT reg to make sure
1090 	 * that reset is complete
1091 	 */
1092 	for (i = 0; i < 10; i++) {
1093 		/* VF reset requires driver to first reset the VF and then
1094 		 * poll the status register to make sure that the reset
1095 		 * completed successfully. Due to internal HW FIFO flushes,
1096 		 * we must wait 10ms before the register will be valid.
1097 		 */
1098 		usleep_range(10000, 20000);
1099 		reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1100 		if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
1101 			rsd = true;
1102 			break;
1103 		}
1104 	}
1105 
1106 	if (flr)
1107 		usleep_range(10000, 20000);
1108 
1109 	if (!rsd)
1110 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1111 			vf->vf_id);
1112 	usleep_range(10000, 20000);
1113 
1114 	/* On initial reset, we don't have any queues to disable */
1115 	if (vf->lan_vsi_idx != 0)
1116 		i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
1117 
1118 	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  * i40e_reset_all_vfs
1128  * @pf: pointer to the PF structure
1129  * @flr: VFLR was issued or not
1130  *
1131  * Reset all allocated VFs in one go. First, tell the hardware to reset each
1132  * VF, then do all the waiting in one chunk, and finally finish restoring each
1133  * VF after the wait. This is useful during PF routines which need to reset
1134  * 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 	struct i40e_hw *hw = &pf->hw;
1141 	struct i40e_vf *vf;
1142 	int i, v;
1143 	u32 reg;
1144 
1145 	/* If we don't have any VFs, then there is nothing to reset */
1146 	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 	/* Begin reset on all VFs at once */
1154 	for (v = 0; v < pf->num_alloc_vfs; v++)
1155 		i40e_trigger_vf_reset(&pf->vf[v], flr);
1156 
1157 	/* HW requires some time to make sure it can flush the FIFO for a VF
1158 	 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1159 	 * sequence to make sure that it has completed. We'll keep track of
1160 	 * the VFs using a simple iterator that increments once that VF has
1161 	 * finished resetting.
1162 	 */
1163 	for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1164 		usleep_range(10000, 20000);
1165 
1166 		/* Check each VF in sequence, beginning with the VF to fail
1167 		 * the previous check.
1168 		 */
1169 		while (v < pf->num_alloc_vfs) {
1170 			vf = &pf->vf[v];
1171 			reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1172 			if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
1173 				break;
1174 
1175 			/* If the current VF has finished resetting, move on
1176 			 * to the next VF in sequence.
1177 			 */
1178 			v++;
1179 		}
1180 	}
1181 
1182 	if (flr)
1183 		usleep_range(10000, 20000);
1184 
1185 	/* Display a warning if at least one VF didn't manage to reset in
1186 	 * time, but continue on with the operation.
1187 	 */
1188 	if (v < pf->num_alloc_vfs)
1189 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1190 			pf->vf[v].vf_id);
1191 	usleep_range(10000, 20000);
1192 
1193 	/* Begin disabling all the rings associated with VFs, but do not wait
1194 	 * between each VF.
1195 	 */
1196 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1197 		/* On initial reset, we don't have any queues to disable */
1198 		if (pf->vf[v].lan_vsi_idx == 0)
1199 			continue;
1200 
1201 		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
1202 	}
1203 
1204 	/* Now that we've notified HW to disable all of the VF rings, wait
1205 	 * until they finish.
1206 	 */
1207 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1208 		/* On initial reset, we don't have any queues to disable */
1209 		if (pf->vf[v].lan_vsi_idx == 0)
1210 			continue;
1211 
1212 		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
1213 	}
1214 
1215 	/* Hw may need up to 50ms to finish disabling the RX queues. We
1216 	 * minimize the wait by delaying only once for all VFs.
1217 	 */
1218 	mdelay(50);
1219 
1220 	/* Finish the reset on each VF */
1221 	for (v = 0; v < pf->num_alloc_vfs; v++)
1222 		i40e_cleanup_reset_vf(&pf->vf[v]);
1223 
1224 	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 void i40e_free_vfs(struct i40e_pf *pf)
1237 {
1238 	struct i40e_hw *hw = &pf->hw;
1239 	u32 reg_idx, bit_idx;
1240 	int i, tmp, vf_id;
1241 
1242 	if (!pf->vf)
1243 		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 	/* Amortize wait time by stopping all VFs at the same time */
1250 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1251 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1252 			continue;
1253 
1254 		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
1255 	}
1256 
1257 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1258 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1259 			continue;
1260 
1261 		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
1262 	}
1263 
1264 	/* Disable IOV before freeing resources. This lets any VF drivers
1265 	 * running in the host get themselves cleaned up before we yank
1266 	 * the carpet out from underneath their feet.
1267 	 */
1268 	if (!pci_vfs_assigned(pf->pdev))
1269 		pci_disable_sriov(pf->pdev);
1270 	else
1271 		dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1272 
1273 	/* free up VF resources */
1274 	tmp = pf->num_alloc_vfs;
1275 	pf->num_alloc_vfs = 0;
1276 	for (i = 0; i < tmp; i++) {
1277 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1278 			i40e_free_vf_res(&pf->vf[i]);
1279 		/* disable qp mappings */
1280 		i40e_disable_vf_mappings(&pf->vf[i]);
1281 	}
1282 
1283 	kfree(pf->vf);
1284 	pf->vf = NULL;
1285 
1286 	/* This check is for when the driver is unloaded while VFs are
1287 	 * assigned. Setting the number of VFs to 0 through sysfs is caught
1288 	 * before this function ever gets called.
1289 	 */
1290 	if (!pci_vfs_assigned(pf->pdev)) {
1291 		/* Acknowledge VFLR for all VFS. Without this, VFs will fail to
1292 		 * work correctly when SR-IOV gets re-enabled.
1293 		 */
1294 		for (vf_id = 0; vf_id < tmp; vf_id++) {
1295 			reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1296 			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 #ifdef CONFIG_PCI_IOV
1304 /**
1305  * i40e_alloc_vfs
1306  * @pf: pointer to the PF structure
1307  * @num_alloc_vfs: number of VFs to allocate
1308  *
1309  * allocate VF resources
1310  **/
1311 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1312 {
1313 	struct i40e_vf *vfs;
1314 	int i, ret = 0;
1315 
1316 	/* Disable interrupt 0 so we don't try to handle the VFLR. */
1317 	i40e_irq_dynamic_disable_icr0(pf);
1318 
1319 	/* Check to see if we're just allocating resources for extant VFs */
1320 	if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
1321 		ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1322 		if (ret) {
1323 			pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1324 			pf->num_alloc_vfs = 0;
1325 			goto err_iov;
1326 		}
1327 	}
1328 	/* allocate memory */
1329 	vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1330 	if (!vfs) {
1331 		ret = -ENOMEM;
1332 		goto err_alloc;
1333 	}
1334 	pf->vf = vfs;
1335 
1336 	/* apply default profile */
1337 	for (i = 0; i < num_alloc_vfs; i++) {
1338 		vfs[i].pf = pf;
1339 		vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
1340 		vfs[i].vf_id = i;
1341 
1342 		/* assign default capabilities */
1343 		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 	pf->num_alloc_vfs = num_alloc_vfs;
1350 
1351 	/* VF resources get allocated during reset */
1352 	i40e_reset_all_vfs(pf, false);
1353 
1354 	i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
1355 
1356 err_alloc:
1357 	if (ret)
1358 		i40e_free_vfs(pf);
1359 err_iov:
1360 	/* Re-enable interrupt 0. */
1361 	i40e_irq_dynamic_enable_icr0(pf, false);
1362 	return ret;
1363 }
1364 
1365 #endif
1366 /**
1367  * i40e_pci_sriov_enable
1368  * @pdev: pointer to a pci_dev structure
1369  * @num_vfs: number of VFs to allocate
1370  *
1371  * Enable or change the number of VFs
1372  **/
1373 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1374 {
1375 #ifdef CONFIG_PCI_IOV
1376 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1377 	int pre_existing_vfs = pci_num_vf(pdev);
1378 	int err = 0;
1379 
1380 	if (test_bit(__I40E_TESTING, pf->state)) {
1381 		dev_warn(&pdev->dev,
1382 			 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
1383 		err = -EPERM;
1384 		goto err_out;
1385 	}
1386 
1387 	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1388 		i40e_free_vfs(pf);
1389 	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1390 		goto out;
1391 
1392 	if (num_vfs > pf->num_req_vfs) {
1393 		dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1394 			 num_vfs, pf->num_req_vfs);
1395 		err = -EPERM;
1396 		goto err_out;
1397 	}
1398 
1399 	dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1400 	err = i40e_alloc_vfs(pf, num_vfs);
1401 	if (err) {
1402 		dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1403 		goto err_out;
1404 	}
1405 
1406 out:
1407 	return num_vfs;
1408 
1409 err_out:
1410 	return err;
1411 #endif
1412 	return 0;
1413 }
1414 
1415 /**
1416  * i40e_pci_sriov_configure
1417  * @pdev: pointer to a pci_dev structure
1418  * @num_vfs: number of VFs to allocate
1419  *
1420  * Enable or change the number of VFs. Called when the user updates the number
1421  * of VFs in sysfs.
1422  **/
1423 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1424 {
1425 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1426 
1427 	if (num_vfs) {
1428 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1429 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1430 			i40e_do_reset_safe(pf,
1431 					   BIT_ULL(__I40E_PF_RESET_REQUESTED));
1432 		}
1433 		return i40e_pci_sriov_enable(pdev, num_vfs);
1434 	}
1435 
1436 	if (!pci_vfs_assigned(pf->pdev)) {
1437 		i40e_free_vfs(pf);
1438 		pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1439 		i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
1440 	} else {
1441 		dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1442 		return -EINVAL;
1443 	}
1444 	return 0;
1445 }
1446 
1447 /***********************virtual channel routines******************/
1448 
1449 /**
1450  * i40e_vc_send_msg_to_vf
1451  * @vf: pointer to the VF info
1452  * @v_opcode: virtual channel opcode
1453  * @v_retval: virtual channel return value
1454  * @msg: pointer to the msg buffer
1455  * @msglen: msg length
1456  *
1457  * send msg to VF
1458  **/
1459 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1460 				  u32 v_retval, u8 *msg, u16 msglen)
1461 {
1462 	struct i40e_pf *pf;
1463 	struct i40e_hw *hw;
1464 	int abs_vf_id;
1465 	i40e_status aq_ret;
1466 
1467 	/* validate the request */
1468 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1469 		return -EINVAL;
1470 
1471 	pf = vf->pf;
1472 	hw = &pf->hw;
1473 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1474 
1475 	/* single place to detect unsuccessful return values */
1476 	if (v_retval) {
1477 		vf->num_invalid_msgs++;
1478 		dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1479 			 vf->vf_id, v_opcode, v_retval);
1480 		if (vf->num_invalid_msgs >
1481 		    I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1482 			dev_err(&pf->pdev->dev,
1483 				"Number of invalid messages exceeded for VF %d\n",
1484 				vf->vf_id);
1485 			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 	} else {
1489 		vf->num_valid_msgs++;
1490 		/* reset the invalid counter, if a valid message is received. */
1491 		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) {
1497 		dev_info(&pf->pdev->dev,
1498 			 "Unable to send the message to VF %d aq_err %d\n",
1499 			 vf->vf_id, pf->hw.aq.asq_last_status);
1500 		return -EIO;
1501 	}
1502 
1503 	return 0;
1504 }
1505 
1506 /**
1507  * i40e_vc_send_resp_to_vf
1508  * @vf: pointer to the VF info
1509  * @opcode: operation code
1510  * @retval: return value
1511  *
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 				   i40e_status retval)
1517 {
1518 	return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1519 }
1520 
1521 /**
1522  * 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 	struct virtchnl_version_info info = {
1530 		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 		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1537 	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  * @msg: pointer to the msg buffer
1546  * @msglen: msg length
1547  *
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 	struct i40e_pf *pf = vf->pf;
1554 	i40e_status aq_ret = 0;
1555 	struct i40e_vsi *vsi;
1556 	int num_vsis = 1;
1557 	int len = 0;
1558 	int ret;
1559 
1560 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
1561 		aq_ret = I40E_ERR_PARAM;
1562 		goto err;
1563 	}
1564 
1565 	len = (sizeof(struct virtchnl_vf_resource) +
1566 	       sizeof(struct virtchnl_vsi_resource) * num_vsis);
1567 
1568 	vfres = kzalloc(len, GFP_KERNEL);
1569 	if (!vfres) {
1570 		aq_ret = I40E_ERR_NO_MEMORY;
1571 		len = 0;
1572 		goto err;
1573 	}
1574 	if (VF_IS_V11(&vf->vf_ver))
1575 		vf->driver_caps = *(u32 *)msg;
1576 	else
1577 		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1578 				  VIRTCHNL_VF_OFFLOAD_RSS_REG |
1579 				  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 
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 	} else {
1591 		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 		if (pf->flags & I40E_FLAG_MFP_ENABLED) {
1619 			dev_err(&pf->pdev->dev,
1620 				"VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
1621 				 vf->vf_id);
1622 			aq_ret = I40E_ERR_PARAM;
1623 			goto err;
1624 		}
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 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
1635 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
1636 
1637 	vfres->num_vsis = num_vsis;
1638 	vfres->num_queue_pairs = vf->num_queue_pairs;
1639 	vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1640 	vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
1641 	vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
1642 
1643 	if (vf->lan_vsi_idx) {
1644 		vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
1645 		vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
1646 		vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
1647 		/* VFs only use TC 0 */
1648 		vfres->vsi_res[0].qset_handle
1649 					  = le16_to_cpu(vsi->info.qs_handle[0]);
1650 		ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
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 				     aq_ret, (u8 *)vfres, len);
1659 
1660 	kfree(vfres);
1661 	return ret;
1662 }
1663 
1664 /**
1665  * i40e_vc_reset_vf_msg
1666  * @vf: pointer to the VF info
1667  * @msg: pointer to the msg buffer
1668  * @msglen: msg length
1669  *
1670  * called from the VF to reset itself,
1671  * unlike other virtchnl messages, PF driver
1672  * doesn't send the response back to the VF
1673  **/
1674 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1675 {
1676 	if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
1677 		i40e_reset_vf(vf, false);
1678 }
1679 
1680 /**
1681  * i40e_getnum_vf_vsi_vlan_filters
1682  * @vsi: pointer to the vsi
1683  *
1684  * called to get the number of VLANs offloaded on this VF
1685  **/
1686 static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
1687 {
1688 	struct i40e_mac_filter *f;
1689 	int num_vlans = 0, bkt;
1690 
1691 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1692 		if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
1693 			num_vlans++;
1694 	}
1695 
1696 	return num_vlans;
1697 }
1698 
1699 /**
1700  * i40e_vc_config_promiscuous_mode_msg
1701  * @vf: pointer to the VF info
1702  * @msg: pointer to the msg buffer
1703  * @msglen: msg length
1704  *
1705  * called from the VF to configure the promiscuous mode of
1706  * VF vsis
1707  **/
1708 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1709 					       u8 *msg, u16 msglen)
1710 {
1711 	struct virtchnl_promisc_info *info =
1712 	    (struct virtchnl_promisc_info *)msg;
1713 	struct i40e_pf *pf = vf->pf;
1714 	struct i40e_hw *hw = &pf->hw;
1715 	struct i40e_mac_filter *f;
1716 	i40e_status aq_ret = 0;
1717 	bool allmulti = false;
1718 	struct i40e_vsi *vsi;
1719 	bool alluni = false;
1720 	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) ||
1725 	    !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1726 	    !vsi) {
1727 		aq_ret = I40E_ERR_PARAM;
1728 		goto error_param;
1729 	}
1730 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
1731 		dev_err(&pf->pdev->dev,
1732 			"Unprivileged VF %d is attempting to configure promiscuous mode\n",
1733 			vf->vf_id);
1734 		/* Lie to the VF on purpose. */
1735 		aq_ret = 0;
1736 		goto error_param;
1737 	}
1738 	/* Multicast promiscuous handling*/
1739 	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
1740 		allmulti = true;
1741 
1742 	if (vf->port_vlan_id) {
1743 		aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
1744 							    allmulti,
1745 							    vf->port_vlan_id,
1746 							    NULL);
1747 	} else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1748 		hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1749 			if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1750 				continue;
1751 			aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
1752 								    vsi->seid,
1753 								    allmulti,
1754 								    f->vlan,
1755 								    NULL);
1756 			aq_err = pf->hw.aq.asq_last_status;
1757 			if (aq_ret) {
1758 				dev_err(&pf->pdev->dev,
1759 					"Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
1760 					f->vlan,
1761 					i40e_stat_str(&pf->hw, aq_ret),
1762 					i40e_aq_str(&pf->hw, aq_err));
1763 				break;
1764 			}
1765 		}
1766 	} else {
1767 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
1768 							       allmulti, NULL);
1769 		aq_err = pf->hw.aq.asq_last_status;
1770 		if (aq_ret) {
1771 			dev_err(&pf->pdev->dev,
1772 				"VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1773 				vf->vf_id,
1774 				i40e_stat_str(&pf->hw, aq_ret),
1775 				i40e_aq_str(&pf->hw, aq_err));
1776 			goto error_param;
1777 		}
1778 	}
1779 
1780 	if (!aq_ret) {
1781 		dev_info(&pf->pdev->dev,
1782 			 "VF %d successfully set multicast promiscuous mode\n",
1783 			 vf->vf_id);
1784 		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 		alluni = true;
1792 	if (vf->port_vlan_id) {
1793 		aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
1794 							    alluni,
1795 							    vf->port_vlan_id,
1796 							    NULL);
1797 	} else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1798 		hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1799 			if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1800 				continue;
1801 			aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
1802 								    vsi->seid,
1803 								    alluni,
1804 								    f->vlan,
1805 								    NULL);
1806 			aq_err = pf->hw.aq.asq_last_status;
1807 			if (aq_ret)
1808 				dev_err(&pf->pdev->dev,
1809 					"Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
1810 					f->vlan,
1811 					i40e_stat_str(&pf->hw, aq_ret),
1812 					i40e_aq_str(&pf->hw, aq_err));
1813 		}
1814 	} else {
1815 		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 			dev_err(&pf->pdev->dev,
1821 				"VF %d failed to set unicast promiscuous mode %8.8x err %s aq_err %s\n",
1822 				vf->vf_id, info->flags,
1823 				i40e_stat_str(&pf->hw, aq_ret),
1824 				i40e_aq_str(&pf->hw, aq_err));
1825 			goto error_param;
1826 		}
1827 	}
1828 
1829 	if (!aq_ret) {
1830 		dev_info(&pf->pdev->dev,
1831 			 "VF %d successfully set unicast promiscuous mode\n",
1832 			 vf->vf_id);
1833 		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 				       aq_ret);
1844 }
1845 
1846 /**
1847  * i40e_vc_config_queues_msg
1848  * @vf: pointer to the VF info
1849  * @msg: pointer to the msg buffer
1850  * @msglen: msg length
1851  *
1852  * called from the VF to configure the rx/tx
1853  * queues
1854  **/
1855 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1856 {
1857 	struct virtchnl_vsi_queue_config_info *qci =
1858 	    (struct virtchnl_vsi_queue_config_info *)msg;
1859 	struct virtchnl_queue_pair_info *qpi;
1860 	struct i40e_pf *pf = vf->pf;
1861 	u16 vsi_id, vsi_queue_id;
1862 	i40e_status aq_ret = 0;
1863 	int i;
1864 
1865 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
1866 		aq_ret = I40E_ERR_PARAM;
1867 		goto error_param;
1868 	}
1869 
1870 	vsi_id = qci->vsi_id;
1871 	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1872 		aq_ret = I40E_ERR_PARAM;
1873 		goto error_param;
1874 	}
1875 	for (i = 0; i < qci->num_queue_pairs; i++) {
1876 		qpi = &qci->qpair[i];
1877 		vsi_queue_id = qpi->txq.queue_id;
1878 		if ((qpi->txq.vsi_id != vsi_id) ||
1879 		    (qpi->rxq.vsi_id != vsi_id) ||
1880 		    (qpi->rxq.queue_id != vsi_queue_id) ||
1881 		    !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1882 			aq_ret = I40E_ERR_PARAM;
1883 			goto error_param;
1884 		}
1885 
1886 		if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1887 					     &qpi->rxq) ||
1888 		    i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1889 					     &qpi->txq)) {
1890 			aq_ret = I40E_ERR_PARAM;
1891 			goto error_param;
1892 		}
1893 	}
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 				       aq_ret);
1901 }
1902 
1903 /**
1904  * i40e_vc_config_irq_map_msg
1905  * @vf: pointer to the VF info
1906  * @msg: pointer to the msg buffer
1907  * @msglen: msg length
1908  *
1909  * called from the VF to configure the irq to
1910  * queue map
1911  **/
1912 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1913 {
1914 	struct virtchnl_irq_map_info *irqmap_info =
1915 	    (struct virtchnl_irq_map_info *)msg;
1916 	struct virtchnl_vector_map *map;
1917 	u16 vsi_id, vsi_queue_id, vector_id;
1918 	i40e_status aq_ret = 0;
1919 	unsigned long tempmap;
1920 	int i;
1921 
1922 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
1923 		aq_ret = I40E_ERR_PARAM;
1924 		goto error_param;
1925 	}
1926 
1927 	for (i = 0; i < irqmap_info->num_vectors; i++) {
1928 		map = &irqmap_info->vecmap[i];
1929 
1930 		vector_id = map->vector_id;
1931 		vsi_id = map->vsi_id;
1932 		/* validate msg params */
1933 		if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1934 		    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1935 			aq_ret = I40E_ERR_PARAM;
1936 			goto error_param;
1937 		}
1938 
1939 		/* lookout for the invalid queue index */
1940 		tempmap = map->rxq_map;
1941 		for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1942 			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1943 						      vsi_queue_id)) {
1944 				aq_ret = I40E_ERR_PARAM;
1945 				goto error_param;
1946 			}
1947 		}
1948 
1949 		tempmap = map->txq_map;
1950 		for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1951 			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1952 						      vsi_queue_id)) {
1953 				aq_ret = I40E_ERR_PARAM;
1954 				goto error_param;
1955 			}
1956 		}
1957 
1958 		i40e_config_irq_link_list(vf, vsi_id, map);
1959 	}
1960 error_param:
1961 	/* send the response to the VF */
1962 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
1963 				       aq_ret);
1964 }
1965 
1966 /**
1967  * i40e_vc_enable_queues_msg
1968  * @vf: pointer to the VF info
1969  * @msg: pointer to the msg buffer
1970  * @msglen: msg length
1971  *
1972  * called from the VF to enable all or specific queue(s)
1973  **/
1974 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1975 {
1976 	struct virtchnl_queue_select *vqs =
1977 	    (struct virtchnl_queue_select *)msg;
1978 	struct i40e_pf *pf = vf->pf;
1979 	u16 vsi_id = vqs->vsi_id;
1980 	i40e_status aq_ret = 0;
1981 
1982 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
1983 		aq_ret = I40E_ERR_PARAM;
1984 		goto error_param;
1985 	}
1986 
1987 	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1988 		aq_ret = I40E_ERR_PARAM;
1989 		goto error_param;
1990 	}
1991 
1992 	if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1993 		aq_ret = I40E_ERR_PARAM;
1994 		goto error_param;
1995 	}
1996 
1997 	if (i40e_vsi_start_rings(pf->vsi[vf->lan_vsi_idx]))
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 				       aq_ret);
2003 }
2004 
2005 /**
2006  * i40e_vc_disable_queues_msg
2007  * @vf: pointer to the VF info
2008  * @msg: pointer to the msg buffer
2009  * @msglen: msg length
2010  *
2011  * called from the VF to disable all or specific
2012  * queue(s)
2013  **/
2014 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2015 {
2016 	struct virtchnl_queue_select *vqs =
2017 	    (struct virtchnl_queue_select *)msg;
2018 	struct i40e_pf *pf = vf->pf;
2019 	i40e_status aq_ret = 0;
2020 
2021 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2022 		aq_ret = I40E_ERR_PARAM;
2023 		goto error_param;
2024 	}
2025 
2026 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2027 		aq_ret = I40E_ERR_PARAM;
2028 		goto error_param;
2029 	}
2030 
2031 	if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
2032 		aq_ret = I40E_ERR_PARAM;
2033 		goto error_param;
2034 	}
2035 
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 				       aq_ret);
2042 }
2043 
2044 /**
2045  * i40e_vc_request_queues_msg
2046  * @vf: pointer to the VF info
2047  * @msg: pointer to the msg buffer
2048  * @msglen: msg length
2049  *
2050  * VFs get a default number of queues but can use this message to request a
2051  * different number.  Will respond with either the number requested or the
2052  * maximum we can support.
2053  **/
2054 static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg, int msglen)
2055 {
2056 	struct virtchnl_vf_res_request *vfres =
2057 		(struct virtchnl_vf_res_request *)msg;
2058 	int req_pairs = vfres->num_queue_pairs;
2059 	int cur_pairs = vf->num_queue_pairs;
2060 	struct i40e_pf *pf = vf->pf;
2061 
2062 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
2063 		return -EINVAL;
2064 
2065 	if (req_pairs <= 0) {
2066 		dev_err(&pf->pdev->dev,
2067 			"VF %d tried to request %d queues.  Ignoring.\n",
2068 			vf->vf_id, req_pairs);
2069 	} else if (req_pairs > I40E_MAX_VF_QUEUES) {
2070 		dev_err(&pf->pdev->dev,
2071 			"VF %d tried to request more than %d queues.\n",
2072 			vf->vf_id,
2073 			I40E_MAX_VF_QUEUES);
2074 		vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
2075 	} else if (req_pairs - cur_pairs > pf->queues_left) {
2076 		dev_warn(&pf->pdev->dev,
2077 			 "VF %d requested %d more queues, but only %d left.\n",
2078 			 vf->vf_id,
2079 			 req_pairs - cur_pairs,
2080 			 pf->queues_left);
2081 		vfres->num_queue_pairs = pf->queues_left + cur_pairs;
2082 	} else {
2083 		vf->num_req_queues = req_pairs;
2084 	}
2085 
2086 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
2087 				      (u8 *)vfres, sizeof(vfres));
2088 }
2089 
2090 /**
2091  * i40e_vc_get_stats_msg
2092  * @vf: pointer to the VF info
2093  * @msg: pointer to the msg buffer
2094  * @msglen: msg length
2095  *
2096  * called from the VF to get vsi stats
2097  **/
2098 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2099 {
2100 	struct virtchnl_queue_select *vqs =
2101 	    (struct virtchnl_queue_select *)msg;
2102 	struct i40e_pf *pf = vf->pf;
2103 	struct i40e_eth_stats stats;
2104 	i40e_status aq_ret = 0;
2105 	struct i40e_vsi *vsi;
2106 
2107 	memset(&stats, 0, sizeof(struct i40e_eth_stats));
2108 
2109 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2110 		aq_ret = I40E_ERR_PARAM;
2111 		goto error_param;
2112 	}
2113 
2114 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2115 		aq_ret = I40E_ERR_PARAM;
2116 		goto error_param;
2117 	}
2118 
2119 	vsi = pf->vsi[vf->lan_vsi_idx];
2120 	if (!vsi) {
2121 		aq_ret = I40E_ERR_PARAM;
2122 		goto error_param;
2123 	}
2124 	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 				      (u8 *)&stats, sizeof(stats));
2131 }
2132 
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 #define I40E_VC_MAX_VLAN_PER_VF 8
2136 
2137 /**
2138  * i40e_check_vf_permission
2139  * @vf: pointer to the VF info
2140  * @macaddr: pointer to the MAC Address being checked
2141  *
2142  * Check if the VF has permission to add or delete unicast MAC address
2143  * filters and return error code -EPERM if not.  Then check if the
2144  * address filter requested is broadcast or zero and if so return
2145  * an invalid MAC address error code.
2146  **/
2147 static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
2148 {
2149 	struct i40e_pf *pf = vf->pf;
2150 	int ret = 0;
2151 
2152 	if (is_broadcast_ether_addr(macaddr) ||
2153 		   is_zero_ether_addr(macaddr)) {
2154 		dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
2155 		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 		/* If the host VMM administrator has set the VF MAC address
2160 		 * administratively via the ndo_set_vf_mac command then deny
2161 		 * permission to the VF to add or delete unicast MAC addresses.
2162 		 * Unless the VF is privileged and then it can do whatever.
2163 		 * The VF may request to set the MAC address filter already
2164 		 * 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 	} else if ((vf->num_mac >= I40E_VC_MAX_MAC_ADDR_PER_VF) &&
2170 		   !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2171 		dev_err(&pf->pdev->dev,
2172 			"VF is not trusted, switch the VF to trusted to add more functionality\n");
2173 		ret = -EPERM;
2174 	}
2175 	return ret;
2176 }
2177 
2178 /**
2179  * i40e_vc_add_mac_addr_msg
2180  * @vf: pointer to the VF info
2181  * @msg: pointer to the msg buffer
2182  * @msglen: msg length
2183  *
2184  * add guest mac address filter
2185  **/
2186 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2187 {
2188 	struct virtchnl_ether_addr_list *al =
2189 	    (struct virtchnl_ether_addr_list *)msg;
2190 	struct i40e_pf *pf = vf->pf;
2191 	struct i40e_vsi *vsi = NULL;
2192 	u16 vsi_id = al->vsi_id;
2193 	i40e_status ret = 0;
2194 	int i;
2195 
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 		goto error_param;
2200 	}
2201 
2202 	for (i = 0; i < al->num_elements; i++) {
2203 		ret = i40e_check_vf_permission(vf, al->list[i].addr);
2204 		if (ret)
2205 			goto error_param;
2206 	}
2207 	vsi = pf->vsi[vf->lan_vsi_idx];
2208 
2209 	/* Lock once, because all function inside for loop accesses VSI's
2210 	 * MAC filter list which needs to be protected using same lock.
2211 	 */
2212 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2213 
2214 	/* add new addresses to the list */
2215 	for (i = 0; i < al->num_elements; i++) {
2216 		struct i40e_mac_filter *f;
2217 
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 		if (!f) {
2223 			dev_err(&pf->pdev->dev,
2224 				"Unable to add MAC filter %pM for VF %d\n",
2225 				 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 		} else {
2230 			vf->num_mac++;
2231 		}
2232 	}
2233 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2234 
2235 	/* program the updated filter list */
2236 	ret = i40e_sync_vsi_filters(vsi);
2237 	if (ret)
2238 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2239 			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  * @msg: pointer to the msg buffer
2251  * @msglen: msg length
2252  *
2253  * remove guest mac address filter
2254  **/
2255 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2256 {
2257 	struct virtchnl_ether_addr_list *al =
2258 	    (struct virtchnl_ether_addr_list *)msg;
2259 	struct i40e_pf *pf = vf->pf;
2260 	struct i40e_vsi *vsi = NULL;
2261 	u16 vsi_id = al->vsi_id;
2262 	i40e_status ret = 0;
2263 	int i;
2264 
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 		goto error_param;
2269 	}
2270 
2271 	for (i = 0; i < al->num_elements; i++) {
2272 		if (is_broadcast_ether_addr(al->list[i].addr) ||
2273 		    is_zero_ether_addr(al->list[i].addr)) {
2274 			dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
2275 				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 	/* delete addresses from the list */
2284 	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 		} else {
2290 			vf->num_mac--;
2291 		}
2292 
2293 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2294 
2295 	/* program the updated filter list */
2296 	ret = i40e_sync_vsi_filters(vsi);
2297 	if (ret)
2298 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2299 			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  * @msg: pointer to the msg buffer
2311  * @msglen: msg length
2312  *
2313  * program guest vlan id
2314  **/
2315 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2316 {
2317 	struct virtchnl_vlan_filter_list *vfl =
2318 	    (struct virtchnl_vlan_filter_list *)msg;
2319 	struct i40e_pf *pf = vf->pf;
2320 	struct i40e_vsi *vsi = NULL;
2321 	u16 vsi_id = vfl->vsi_id;
2322 	i40e_status aq_ret = 0;
2323 	int i;
2324 
2325 	if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
2326 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2327 		dev_err(&pf->pdev->dev,
2328 			"VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
2329 		goto error_param;
2330 	}
2331 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2332 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2333 		aq_ret = I40E_ERR_PARAM;
2334 		goto error_param;
2335 	}
2336 
2337 	for (i = 0; i < vfl->num_elements; i++) {
2338 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2339 			aq_ret = I40E_ERR_PARAM;
2340 			dev_err(&pf->pdev->dev,
2341 				"invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2342 			goto error_param;
2343 		}
2344 	}
2345 	vsi = pf->vsi[vf->lan_vsi_idx];
2346 	if (vsi->info.pvid) {
2347 		aq_ret = I40E_ERR_PARAM;
2348 		goto error_param;
2349 	}
2350 
2351 	i40e_vlan_stripping_enable(vsi);
2352 	for (i = 0; i < vfl->num_elements; i++) {
2353 		/* add new VLAN filter */
2354 		int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
2355 		if (!ret)
2356 			vf->num_vlan++;
2357 
2358 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2359 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2360 							   true,
2361 							   vfl->vlan_id[i],
2362 							   NULL);
2363 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2364 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2365 							   true,
2366 							   vfl->vlan_id[i],
2367 							   NULL);
2368 
2369 		if (ret)
2370 			dev_err(&pf->pdev->dev,
2371 				"Unable to add VLAN filter %d for VF %d, error %d\n",
2372 				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  * @msg: pointer to the msg buffer
2384  * @msglen: msg length
2385  *
2386  * remove programmed guest vlan id
2387  **/
2388 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2389 {
2390 	struct virtchnl_vlan_filter_list *vfl =
2391 	    (struct virtchnl_vlan_filter_list *)msg;
2392 	struct i40e_pf *pf = vf->pf;
2393 	struct i40e_vsi *vsi = NULL;
2394 	u16 vsi_id = vfl->vsi_id;
2395 	i40e_status aq_ret = 0;
2396 	int i;
2397 
2398 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2399 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2400 		aq_ret = I40E_ERR_PARAM;
2401 		goto error_param;
2402 	}
2403 
2404 	for (i = 0; i < vfl->num_elements; i++) {
2405 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2406 			aq_ret = I40E_ERR_PARAM;
2407 			goto error_param;
2408 		}
2409 	}
2410 
2411 	vsi = pf->vsi[vf->lan_vsi_idx];
2412 	if (vsi->info.pvid) {
2413 		aq_ret = I40E_ERR_PARAM;
2414 		goto error_param;
2415 	}
2416 
2417 	for (i = 0; i < vfl->num_elements; i++) {
2418 		i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
2419 		vf->num_vlan--;
2420 
2421 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2422 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2423 							   false,
2424 							   vfl->vlan_id[i],
2425 							   NULL);
2426 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2427 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2428 							   false,
2429 							   vfl->vlan_id[i],
2430 							   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  * i40e_vc_iwarp_msg
2440  * @vf: pointer to the VF info
2441  * @msg: pointer to the msg buffer
2442  * @msglen: msg length
2443  *
2444  * called from the VF for the iwarp msgs
2445  **/
2446 static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2447 {
2448 	struct i40e_pf *pf = vf->pf;
2449 	int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
2450 	i40e_status aq_ret = 0;
2451 
2452 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2453 	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2454 		aq_ret = I40E_ERR_PARAM;
2455 		goto error_param;
2456 	}
2457 
2458 	i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
2459 				     msg, msglen);
2460 
2461 error_param:
2462 	/* send the response to the VF */
2463 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
2464 				       aq_ret);
2465 }
2466 
2467 /**
2468  * i40e_vc_iwarp_qvmap_msg
2469  * @vf: pointer to the VF info
2470  * @msg: pointer to the msg buffer
2471  * @msglen: msg length
2472  * @config: config qvmap or release it
2473  *
2474  * called from the VF for the iwarp msgs
2475  **/
2476 static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, u16 msglen,
2477 				   bool config)
2478 {
2479 	struct virtchnl_iwarp_qvlist_info *qvlist_info =
2480 				(struct virtchnl_iwarp_qvlist_info *)msg;
2481 	i40e_status aq_ret = 0;
2482 
2483 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2484 	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2485 		aq_ret = I40E_ERR_PARAM;
2486 		goto error_param;
2487 	}
2488 
2489 	if (config) {
2490 		if (i40e_config_iwarp_qvlist(vf, qvlist_info))
2491 			aq_ret = I40E_ERR_PARAM;
2492 	} else {
2493 		i40e_release_iwarp_qvlist(vf);
2494 	}
2495 
2496 error_param:
2497 	/* send the response to the VF */
2498 	return i40e_vc_send_resp_to_vf(vf,
2499 			       config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
2500 			       VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
2501 			       aq_ret);
2502 }
2503 
2504 /**
2505  * i40e_vc_config_rss_key
2506  * @vf: pointer to the VF info
2507  * @msg: pointer to the msg buffer
2508  * @msglen: msg length
2509  *
2510  * Configure the VF's RSS key
2511  **/
2512 static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg, u16 msglen)
2513 {
2514 	struct virtchnl_rss_key *vrk =
2515 		(struct virtchnl_rss_key *)msg;
2516 	struct i40e_pf *pf = vf->pf;
2517 	struct i40e_vsi *vsi = NULL;
2518 	u16 vsi_id = vrk->vsi_id;
2519 	i40e_status aq_ret = 0;
2520 
2521 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2522 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
2523 	    (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
2524 		aq_ret = I40E_ERR_PARAM;
2525 		goto err;
2526 	}
2527 
2528 	vsi = pf->vsi[vf->lan_vsi_idx];
2529 	aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
2530 err:
2531 	/* send the response to the VF */
2532 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
2533 				       aq_ret);
2534 }
2535 
2536 /**
2537  * i40e_vc_config_rss_lut
2538  * @vf: pointer to the VF info
2539  * @msg: pointer to the msg buffer
2540  * @msglen: msg length
2541  *
2542  * Configure the VF's RSS LUT
2543  **/
2544 static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg, u16 msglen)
2545 {
2546 	struct virtchnl_rss_lut *vrl =
2547 		(struct virtchnl_rss_lut *)msg;
2548 	struct i40e_pf *pf = vf->pf;
2549 	struct i40e_vsi *vsi = NULL;
2550 	u16 vsi_id = vrl->vsi_id;
2551 	i40e_status aq_ret = 0;
2552 
2553 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2554 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
2555 	    (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
2556 		aq_ret = I40E_ERR_PARAM;
2557 		goto err;
2558 	}
2559 
2560 	vsi = pf->vsi[vf->lan_vsi_idx];
2561 	aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
2562 	/* send the response to the VF */
2563 err:
2564 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
2565 				       aq_ret);
2566 }
2567 
2568 /**
2569  * i40e_vc_get_rss_hena
2570  * @vf: pointer to the VF info
2571  * @msg: pointer to the msg buffer
2572  * @msglen: msg length
2573  *
2574  * Return the RSS HENA bits allowed by the hardware
2575  **/
2576 static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
2577 {
2578 	struct virtchnl_rss_hena *vrh = NULL;
2579 	struct i40e_pf *pf = vf->pf;
2580 	i40e_status aq_ret = 0;
2581 	int len = 0;
2582 
2583 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2584 		aq_ret = I40E_ERR_PARAM;
2585 		goto err;
2586 	}
2587 	len = sizeof(struct virtchnl_rss_hena);
2588 
2589 	vrh = kzalloc(len, GFP_KERNEL);
2590 	if (!vrh) {
2591 		aq_ret = I40E_ERR_NO_MEMORY;
2592 		len = 0;
2593 		goto err;
2594 	}
2595 	vrh->hena = i40e_pf_get_default_rss_hena(pf);
2596 err:
2597 	/* 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);
2600 	kfree(vrh);
2601 	return aq_ret;
2602 }
2603 
2604 /**
2605  * i40e_vc_set_rss_hena
2606  * @vf: pointer to the VF info
2607  * @msg: pointer to the msg buffer
2608  * @msglen: msg length
2609  *
2610  * Set the RSS HENA bits for the VF
2611  **/
2612 static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
2613 {
2614 	struct virtchnl_rss_hena *vrh =
2615 		(struct virtchnl_rss_hena *)msg;
2616 	struct i40e_pf *pf = vf->pf;
2617 	struct i40e_hw *hw = &pf->hw;
2618 	i40e_status aq_ret = 0;
2619 
2620 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2621 		aq_ret = I40E_ERR_PARAM;
2622 		goto err;
2623 	}
2624 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
2625 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
2626 			  (u32)(vrh->hena >> 32));
2627 
2628 	/* send the response to the VF */
2629 err:
2630 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
2631 }
2632 
2633 /**
2634  * i40e_vc_enable_vlan_stripping
2635  * @vf: pointer to the VF info
2636  * @msg: pointer to the msg buffer
2637  * @msglen: msg length
2638  *
2639  * Enable vlan header stripping for the VF
2640  **/
2641 static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
2642 					 u16 msglen)
2643 {
2644 	struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
2645 	i40e_status aq_ret = 0;
2646 
2647 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2648 		aq_ret = I40E_ERR_PARAM;
2649 		goto err;
2650 	}
2651 
2652 	i40e_vlan_stripping_enable(vsi);
2653 
2654 	/* send the response to the VF */
2655 err:
2656 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
2657 				       aq_ret);
2658 }
2659 
2660 /**
2661  * i40e_vc_disable_vlan_stripping
2662  * @vf: pointer to the VF info
2663  * @msg: pointer to the msg buffer
2664  * @msglen: msg length
2665  *
2666  * Disable vlan header stripping for the VF
2667  **/
2668 static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
2669 					  u16 msglen)
2670 {
2671 	struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
2672 	i40e_status aq_ret = 0;
2673 
2674 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2675 		aq_ret = I40E_ERR_PARAM;
2676 		goto err;
2677 	}
2678 
2679 	i40e_vlan_stripping_disable(vsi);
2680 
2681 	/* send the response to the VF */
2682 err:
2683 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
2684 				       aq_ret);
2685 }
2686 
2687 /**
2688  * i40e_vc_process_vf_msg
2689  * @pf: pointer to the PF structure
2690  * @vf_id: source VF id
2691  * @msg: pointer to the msg buffer
2692  * @msglen: msg length
2693  * @msghndl: msg handle
2694  *
2695  * 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 			   u32 v_retval, u8 *msg, u16 msglen)
2700 {
2701 	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 	int ret;
2705 
2706 	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 	/* Check if VF is disabled. */
2712 	if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
2713 		return I40E_ERR_PARAM;
2714 
2715 	/* perform basic checks on the msg */
2716 	ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
2717 
2718 	/* perform additional checks specific to this driver */
2719 	if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
2720 		struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
2721 
2722 		if (vrk->key_len != I40E_HKEY_ARRAY_SIZE)
2723 			ret = -EINVAL;
2724 	} else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
2725 		struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
2726 
2727 		if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)
2728 			ret = -EINVAL;
2729 	}
2730 
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 		switch (ret) {
2736 		case VIRTCHNL_ERR_PARAM:
2737 			return -EPERM;
2738 		default:
2739 			return -EINVAL;
2740 		}
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:
2751 		i40e_vc_reset_vf_msg(vf);
2752 		ret = 0;
2753 		break;
2754 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
2755 		ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
2756 		break;
2757 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2758 		ret = i40e_vc_config_queues_msg(vf, msg, msglen);
2759 		break;
2760 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
2761 		ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
2762 		break;
2763 	case VIRTCHNL_OP_ENABLE_QUEUES:
2764 		ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
2765 		i40e_vc_notify_vf_link_state(vf);
2766 		break;
2767 	case VIRTCHNL_OP_DISABLE_QUEUES:
2768 		ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
2769 		break;
2770 	case VIRTCHNL_OP_ADD_ETH_ADDR:
2771 		ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
2772 		break;
2773 	case VIRTCHNL_OP_DEL_ETH_ADDR:
2774 		ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
2775 		break;
2776 	case VIRTCHNL_OP_ADD_VLAN:
2777 		ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
2778 		break;
2779 	case VIRTCHNL_OP_DEL_VLAN:
2780 		ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
2781 		break;
2782 	case VIRTCHNL_OP_GET_STATS:
2783 		ret = i40e_vc_get_stats_msg(vf, msg, msglen);
2784 		break;
2785 	case VIRTCHNL_OP_IWARP:
2786 		ret = i40e_vc_iwarp_msg(vf, msg, msglen);
2787 		break;
2788 	case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
2789 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, true);
2790 		break;
2791 	case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
2792 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, false);
2793 		break;
2794 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
2795 		ret = i40e_vc_config_rss_key(vf, msg, msglen);
2796 		break;
2797 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
2798 		ret = i40e_vc_config_rss_lut(vf, msg, msglen);
2799 		break;
2800 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
2801 		ret = i40e_vc_get_rss_hena(vf, msg, msglen);
2802 		break;
2803 	case VIRTCHNL_OP_SET_RSS_HENA:
2804 		ret = i40e_vc_set_rss_hena(vf, msg, msglen);
2805 		break;
2806 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
2807 		ret = i40e_vc_enable_vlan_stripping(vf, msg, msglen);
2808 		break;
2809 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
2810 		ret = i40e_vc_disable_vlan_stripping(vf, msg, msglen);
2811 		break;
2812 	case VIRTCHNL_OP_REQUEST_QUEUES:
2813 		ret = i40e_vc_request_queues_msg(vf, msg, msglen);
2814 		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 		ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
2821 					      I40E_ERR_NOT_IMPLEMENTED);
2822 		break;
2823 	}
2824 
2825 	return ret;
2826 }
2827 
2828 /**
2829  * 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 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
2836 {
2837 	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 		return 0;
2844 
2845 	/* Re-enable the VFLR interrupt cause here, before looking for which
2846 	 * VF got reset. Otherwise, if another VF gets a reset while the
2847 	 * first one is being processed, that interrupt will be lost, and
2848 	 * that VF will be stuck in reset forever.
2849 	 */
2850 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
2851 	reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
2852 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
2853 	i40e_flush(hw);
2854 
2855 	clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
2856 	for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
2857 		reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2858 		bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
2859 		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
2860 		vf = &pf->vf[vf_id];
2861 		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 	return 0;
2868 }
2869 
2870 /**
2871  * i40e_ndo_set_vf_mac
2872  * @netdev: network interface device structure
2873  * @vf_id: VF identifier
2874  * @mac: mac address
2875  *
2876  * program VF mac address
2877  **/
2878 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2879 {
2880 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2881 	struct i40e_vsi *vsi = np->vsi;
2882 	struct i40e_pf *pf = vsi->back;
2883 	struct i40e_mac_filter *f;
2884 	struct i40e_vf *vf;
2885 	int ret = 0;
2886 	int bkt;
2887 
2888 	/* validate the request */
2889 	if (vf_id >= pf->num_alloc_vfs) {
2890 		dev_err(&pf->pdev->dev,
2891 			"Invalid VF Identifier %d\n", vf_id);
2892 		ret = -EINVAL;
2893 		goto error_param;
2894 	}
2895 
2896 	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 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2900 			vf_id);
2901 		ret = -EAGAIN;
2902 		goto error_param;
2903 	}
2904 
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 		ret = -EINVAL;
2909 		goto error_param;
2910 	}
2911 
2912 	/* Lock once because below invoked function add/del_filter requires
2913 	 * mac_filter_hash_lock to be held
2914 	 */
2915 	spin_lock_bh(&vsi->mac_filter_hash_lock);
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 	/* Delete all the filters for this VSI - we're going to kill it
2922 	 * anyway.
2923 	 */
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);
2928 
2929 	/* program mac filter */
2930 	if (i40e_sync_vsi_filters(vsi)) {
2931 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2932 		ret = -EIO;
2933 		goto error_param;
2934 	}
2935 	ether_addr_copy(vf->default_lan_addr.addr, mac);
2936 
2937 	if (is_zero_ether_addr(mac)) {
2938 		vf->pf_set_mac = false;
2939 		dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
2940 	} else {
2941 		vf->pf_set_mac = true;
2942 		dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
2943 			 mac, vf_id);
2944 	}
2945 
2946 	/* Force the VF driver stop so it has to reload with new MAC address */
2947 	i40e_vc_disable_vf(vf);
2948 	dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
2949 
2950 error_param:
2951 	return ret;
2952 }
2953 
2954 /**
2955  * i40e_vsi_has_vlans - True if VSI has configured VLANs
2956  * @vsi: pointer to the vsi
2957  *
2958  * Check if a VSI has configured any VLANs. False if we have a port VLAN or if
2959  * we have no configured VLANs. Do not call while holding the
2960  * mac_filter_hash_lock.
2961  */
2962 static bool i40e_vsi_has_vlans(struct i40e_vsi *vsi)
2963 {
2964 	bool have_vlans;
2965 
2966 	/* If we have a port VLAN, then the VSI cannot have any VLANs
2967 	 * configured, as all MAC/VLAN filters will be assigned to the PVID.
2968 	 */
2969 	if (vsi->info.pvid)
2970 		return false;
2971 
2972 	/* Since we don't have a PVID, we know that if the device is in VLAN
2973 	 * mode it must be because of a VLAN filter configured on this VSI.
2974 	 */
2975 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2976 	have_vlans = i40e_is_vsi_in_vlan(vsi);
2977 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2978 
2979 	return have_vlans;
2980 }
2981 
2982 /**
2983  * i40e_ndo_set_vf_port_vlan
2984  * @netdev: network interface device structure
2985  * @vf_id: VF identifier
2986  * @vlan_id: mac address
2987  * @qos: priority setting
2988  * @vlan_proto: vlan protocol
2989  *
2990  * program VF vlan id and/or qos
2991  **/
2992 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
2993 			      u16 vlan_id, u8 qos, __be16 vlan_proto)
2994 {
2995 	u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
2996 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2997 	struct i40e_pf *pf = np->vsi->back;
2998 	struct i40e_vsi *vsi;
2999 	struct i40e_vf *vf;
3000 	int ret = 0;
3001 
3002 	/* validate the request */
3003 	if (vf_id >= pf->num_alloc_vfs) {
3004 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3005 		ret = -EINVAL;
3006 		goto error_pvid;
3007 	}
3008 
3009 	if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
3010 		dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
3011 		ret = -EINVAL;
3012 		goto error_pvid;
3013 	}
3014 
3015 	if (vlan_proto != htons(ETH_P_8021Q)) {
3016 		dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
3017 		ret = -EPROTONOSUPPORT;
3018 		goto error_pvid;
3019 	}
3020 
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 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3025 			vf_id);
3026 		ret = -EAGAIN;
3027 		goto error_pvid;
3028 	}
3029 
3030 	if (le16_to_cpu(vsi->info.pvid) == vlanprio)
3031 		/* duplicate request, so just return success */
3032 		goto error_pvid;
3033 
3034 	if (i40e_vsi_has_vlans(vsi)) {
3035 		dev_err(&pf->pdev->dev,
3036 			"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",
3037 			vf_id);
3038 		/* Administrator Error - knock the VF offline until he does
3039 		 * the right thing by reconfiguring his network correctly
3040 		 * and then reloading the VF driver.
3041 		 */
3042 		i40e_vc_disable_vf(vf);
3043 		/* During reset the VF got a new VSI, so refresh the pointer. */
3044 		vsi = pf->vsi[vf->lan_vsi_idx];
3045 	}
3046 
3047 	/* Locked once because multiple functions below iterate list */
3048 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3049 
3050 	/* Check for condition where there was already a port VLAN ID
3051 	 * filter set and now it is being deleted by setting it to zero.
3052 	 * Additionally check for the condition where there was a port
3053 	 * VLAN but now there is a new and different port VLAN being set.
3054 	 * Before deleting all the old VLAN filters we must add new ones
3055 	 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
3056 	 * MAC addresses deleted.
3057 	 */
3058 	if ((!(vlan_id || qos) ||
3059 	    vlanprio != le16_to_cpu(vsi->info.pvid)) &&
3060 	    vsi->info.pvid) {
3061 		ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
3062 		if (ret) {
3063 			dev_info(&vsi->back->pdev->dev,
3064 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
3065 				 vsi->back->hw.aq.asq_last_status);
3066 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
3067 			goto error_pvid;
3068 		}
3069 	}
3070 
3071 	if (vsi->info.pvid) {
3072 		/* remove all filters on the old VLAN */
3073 		i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
3074 					   VLAN_VID_MASK));
3075 	}
3076 
3077 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3078 	if (vlan_id || qos)
3079 		ret = i40e_vsi_add_pvid(vsi, vlanprio);
3080 	else
3081 		i40e_vsi_remove_pvid(vsi);
3082 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3083 
3084 	if (vlan_id) {
3085 		dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
3086 			 vlan_id, qos, vf_id);
3087 
3088 		/* add new VLAN filter for each MAC */
3089 		ret = i40e_add_vlan_all_mac(vsi, vlan_id);
3090 		if (ret) {
3091 			dev_info(&vsi->back->pdev->dev,
3092 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
3093 				 vsi->back->hw.aq.asq_last_status);
3094 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
3095 			goto error_pvid;
3096 		}
3097 
3098 		/* remove the previously added non-VLAN MAC filters */
3099 		i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
3100 	}
3101 
3102 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3103 
3104 	/* Schedule the worker thread to take care of applying changes */
3105 	i40e_service_event_schedule(vsi->back);
3106 
3107 	if (ret) {
3108 		dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
3109 		goto error_pvid;
3110 	}
3111 
3112 	/* The Port VLAN needs to be saved across resets the same as the
3113 	 * default LAN MAC address.
3114 	 */
3115 	vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
3116 	ret = 0;
3117 
3118 error_pvid:
3119 	return ret;
3120 }
3121 
3122 #define I40E_BW_CREDIT_DIVISOR 50     /* 50Mbps per BW credit */
3123 #define I40E_MAX_BW_INACTIVE_ACCUM 4  /* device can accumulate 4 credits max */
3124 /**
3125  * i40e_ndo_set_vf_bw
3126  * @netdev: network interface device structure
3127  * @vf_id: VF identifier
3128  * @tx_rate: Tx rate
3129  *
3130  * configure VF Tx rate
3131  **/
3132 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
3133 		       int max_tx_rate)
3134 {
3135 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3136 	struct i40e_pf *pf = np->vsi->back;
3137 	struct i40e_vsi *vsi;
3138 	struct i40e_vf *vf;
3139 	int speed = 0;
3140 	int ret = 0;
3141 
3142 	/* validate the request */
3143 	if (vf_id >= pf->num_alloc_vfs) {
3144 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
3145 		ret = -EINVAL;
3146 		goto error;
3147 	}
3148 
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 			min_tx_rate, vf_id);
3152 		return -EINVAL;
3153 	}
3154 
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 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3159 			vf_id);
3160 		ret = -EAGAIN;
3161 		goto error;
3162 	}
3163 
3164 	switch (pf->hw.phy.link_info.link_speed) {
3165 	case I40E_LINK_SPEED_40GB:
3166 		speed = 40000;
3167 		break;
3168 	case I40E_LINK_SPEED_25GB:
3169 		speed = 25000;
3170 		break;
3171 	case I40E_LINK_SPEED_20GB:
3172 		speed = 20000;
3173 		break;
3174 	case I40E_LINK_SPEED_10GB:
3175 		speed = 10000;
3176 		break;
3177 	case I40E_LINK_SPEED_1GB:
3178 		speed = 1000;
3179 		break;
3180 	default:
3181 		break;
3182 	}
3183 
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 		ret = -EINVAL;
3188 		goto error;
3189 	}
3190 
3191 	if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
3192 		dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
3193 		max_tx_rate = 50;
3194 	}
3195 
3196 	/* Tx rate credits are in values of 50Mbps, 0 is disabled*/
3197 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
3198 					  max_tx_rate / I40E_BW_CREDIT_DIVISOR,
3199 					  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 			ret);
3203 		ret = -EIO;
3204 		goto error;
3205 	}
3206 	vf->tx_rate = max_tx_rate;
3207 error:
3208 	return ret;
3209 }
3210 
3211 /**
3212  * i40e_ndo_get_vf_config
3213  * @netdev: network interface device structure
3214  * @vf_id: VF identifier
3215  * @ivi: VF configuration structure
3216  *
3217  * return VF configuration
3218  **/
3219 int i40e_ndo_get_vf_config(struct net_device *netdev,
3220 			   int vf_id, struct ifla_vf_info *ivi)
3221 {
3222 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3223 	struct i40e_vsi *vsi = np->vsi;
3224 	struct i40e_pf *pf = vsi->back;
3225 	struct i40e_vf *vf;
3226 	int ret = 0;
3227 
3228 	/* validate the request */
3229 	if (vf_id >= pf->num_alloc_vfs) {
3230 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3231 		ret = -EINVAL;
3232 		goto error_param;
3233 	}
3234 
3235 	vf = &(pf->vf[vf_id]);
3236 	/* 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 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3240 			vf_id);
3241 		ret = -EAGAIN;
3242 		goto error_param;
3243 	}
3244 
3245 	ivi->vf = vf_id;
3246 
3247 	ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
3248 
3249 	ivi->max_tx_rate = vf->tx_rate;
3250 	ivi->min_tx_rate = 0;
3251 	ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
3252 	ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
3253 		   I40E_VLAN_PRIORITY_SHIFT;
3254 	if (vf->link_forced == false)
3255 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
3256 	else if (vf->link_up == true)
3257 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
3258 	else
3259 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
3260 	ivi->spoofchk = vf->spoofchk;
3261 	ivi->trusted = vf->trusted;
3262 	ret = 0;
3263 
3264 error_param:
3265 	return ret;
3266 }
3267 
3268 /**
3269  * i40e_ndo_set_vf_link_state
3270  * @netdev: network interface device structure
3271  * @vf_id: VF identifier
3272  * @link: required link state
3273  *
3274  * Set the link state of a specified VF, regardless of physical link state
3275  **/
3276 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
3277 {
3278 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3279 	struct i40e_pf *pf = np->vsi->back;
3280 	struct virtchnl_pf_event pfe;
3281 	struct i40e_hw *hw = &pf->hw;
3282 	struct i40e_vf *vf;
3283 	int abs_vf_id;
3284 	int ret = 0;
3285 
3286 	/* validate the request */
3287 	if (vf_id >= pf->num_alloc_vfs) {
3288 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3289 		ret = -EINVAL;
3290 		goto error_out;
3291 	}
3292 
3293 	vf = &pf->vf[vf_id];
3294 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
3295 
3296 	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
3297 	pfe.severity = PF_EVENT_SEVERITY_INFO;
3298 
3299 	switch (link) {
3300 	case IFLA_VF_LINK_STATE_AUTO:
3301 		vf->link_forced = false;
3302 		pfe.event_data.link_event.link_status =
3303 			pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
3304 		pfe.event_data.link_event.link_speed =
3305 			(enum virtchnl_link_speed)
3306 			pf->hw.phy.link_info.link_speed;
3307 		break;
3308 	case IFLA_VF_LINK_STATE_ENABLE:
3309 		vf->link_forced = true;
3310 		vf->link_up = true;
3311 		pfe.event_data.link_event.link_status = true;
3312 		pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
3313 		break;
3314 	case IFLA_VF_LINK_STATE_DISABLE:
3315 		vf->link_forced = true;
3316 		vf->link_up = false;
3317 		pfe.event_data.link_event.link_status = false;
3318 		pfe.event_data.link_event.link_speed = 0;
3319 		break;
3320 	default:
3321 		ret = -EINVAL;
3322 		goto error_out;
3323 	}
3324 	/* Notify the VF of its new link state */
3325 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
3326 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
3327 
3328 error_out:
3329 	return ret;
3330 }
3331 
3332 /**
3333  * i40e_ndo_set_vf_spoofchk
3334  * @netdev: network interface device structure
3335  * @vf_id: VF identifier
3336  * @enable: flag to enable or disable feature
3337  *
3338  * Enable or disable VF spoof checking
3339  **/
3340 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
3341 {
3342 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3343 	struct i40e_vsi *vsi = np->vsi;
3344 	struct i40e_pf *pf = vsi->back;
3345 	struct i40e_vsi_context ctxt;
3346 	struct i40e_hw *hw = &pf->hw;
3347 	struct i40e_vf *vf;
3348 	int ret = 0;
3349 
3350 	/* validate the request */
3351 	if (vf_id >= pf->num_alloc_vfs) {
3352 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3353 		ret = -EINVAL;
3354 		goto out;
3355 	}
3356 
3357 	vf = &(pf->vf[vf_id]);
3358 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3359 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3360 			vf_id);
3361 		ret = -EAGAIN;
3362 		goto out;
3363 	}
3364 
3365 	if (enable == vf->spoofchk)
3366 		goto out;
3367 
3368 	vf->spoofchk = enable;
3369 	memset(&ctxt, 0, sizeof(ctxt));
3370 	ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
3371 	ctxt.pf_num = pf->hw.pf_id;
3372 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
3373 	if (enable)
3374 		ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
3375 					I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
3376 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
3377 	if (ret) {
3378 		dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
3379 			ret);
3380 		ret = -EIO;
3381 	}
3382 out:
3383 	return ret;
3384 }
3385 
3386 /**
3387  * i40e_ndo_set_vf_trust
3388  * @netdev: network interface device structure of the pf
3389  * @vf_id: VF identifier
3390  * @setting: trust setting
3391  *
3392  * Enable or disable VF trust setting
3393  **/
3394 int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
3395 {
3396 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3397 	struct i40e_pf *pf = np->vsi->back;
3398 	struct i40e_vf *vf;
3399 	int ret = 0;
3400 
3401 	/* validate the request */
3402 	if (vf_id >= pf->num_alloc_vfs) {
3403 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3404 		return -EINVAL;
3405 	}
3406 
3407 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3408 		dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
3409 		return -EINVAL;
3410 	}
3411 
3412 	vf = &pf->vf[vf_id];
3413 
3414 	if (setting == vf->trusted)
3415 		goto out;
3416 
3417 	vf->trusted = setting;
3418 	i40e_vc_disable_vf(vf);
3419 	dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
3420 		 vf_id, setting ? "" : "un");
3421 out:
3422 	return ret;
3423 }
3424