1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 #include "i40e.h"
5 
6 /*********************notification routines***********************/
7 
8 /**
9  * i40e_vc_vf_broadcast
10  * @pf: pointer to the PF structure
11  * @v_opcode: operation code
12  * @v_retval: return value
13  * @msg: pointer to the msg buffer
14  * @msglen: msg length
15  *
16  * send a message to all VFs on a given PF
17  **/
18 static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
19 				 enum virtchnl_ops v_opcode,
20 				 i40e_status v_retval, u8 *msg,
21 				 u16 msglen)
22 {
23 	struct i40e_hw *hw = &pf->hw;
24 	struct i40e_vf *vf = pf->vf;
25 	int i;
26 
27 	for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
28 		int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
29 		/* Not all vfs are enabled so skip the ones that are not */
30 		if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
31 		    !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
32 			continue;
33 
34 		/* Ignore return value on purpose - a given VF may fail, but
35 		 * we need to keep going and send to all of them
36 		 */
37 		i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
38 				       msg, msglen, NULL);
39 	}
40 }
41 
42 /**
43  * i40e_vc_notify_vf_link_state
44  * @vf: pointer to the VF structure
45  *
46  * send a link status message to a single VF
47  **/
48 static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
49 {
50 	struct virtchnl_pf_event pfe;
51 	struct i40e_pf *pf = vf->pf;
52 	struct i40e_hw *hw = &pf->hw;
53 	struct i40e_link_status *ls = &pf->hw.phy.link_info;
54 	int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
55 
56 	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
57 	pfe.severity = PF_EVENT_SEVERITY_INFO;
58 
59 	/* Always report link is down if the VF queues aren't enabled */
60 	if (!vf->queues_enabled) {
61 		pfe.event_data.link_event.link_status = false;
62 		pfe.event_data.link_event.link_speed = 0;
63 	} else if (vf->link_forced) {
64 		pfe.event_data.link_event.link_status = vf->link_up;
65 		pfe.event_data.link_event.link_speed =
66 			(vf->link_up ? VIRTCHNL_LINK_SPEED_40GB : 0);
67 	} else {
68 		pfe.event_data.link_event.link_status =
69 			ls->link_info & I40E_AQ_LINK_UP;
70 		pfe.event_data.link_event.link_speed =
71 			i40e_virtchnl_link_speed(ls->link_speed);
72 	}
73 
74 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
75 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
76 }
77 
78 /**
79  * i40e_vc_notify_link_state
80  * @pf: pointer to the PF structure
81  *
82  * send a link status message to all VFs on a given PF
83  **/
84 void i40e_vc_notify_link_state(struct i40e_pf *pf)
85 {
86 	int i;
87 
88 	for (i = 0; i < pf->num_alloc_vfs; i++)
89 		i40e_vc_notify_vf_link_state(&pf->vf[i]);
90 }
91 
92 /**
93  * i40e_vc_notify_reset
94  * @pf: pointer to the PF structure
95  *
96  * indicate a pending reset to all VFs on a given PF
97  **/
98 void i40e_vc_notify_reset(struct i40e_pf *pf)
99 {
100 	struct virtchnl_pf_event pfe;
101 
102 	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
103 	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
104 	i40e_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, 0,
105 			     (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
106 }
107 
108 /**
109  * i40e_vc_notify_vf_reset
110  * @vf: pointer to the VF structure
111  *
112  * indicate a pending reset to the given VF
113  **/
114 void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
115 {
116 	struct virtchnl_pf_event pfe;
117 	int abs_vf_id;
118 
119 	/* validate the request */
120 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
121 		return;
122 
123 	/* verify if the VF is in either init or active before proceeding */
124 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
125 	    !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
126 		return;
127 
128 	abs_vf_id = vf->vf_id + (int)vf->pf->hw.func_caps.vf_base_id;
129 
130 	pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
131 	pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
132 	i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, VIRTCHNL_OP_EVENT,
133 			       0, (u8 *)&pfe,
134 			       sizeof(struct virtchnl_pf_event), NULL);
135 }
136 /***********************misc routines*****************************/
137 
138 /**
139  * i40e_vc_disable_vf
140  * @vf: pointer to the VF info
141  *
142  * Disable the VF through a SW reset.
143  **/
144 static inline void i40e_vc_disable_vf(struct i40e_vf *vf)
145 {
146 	int i;
147 
148 	i40e_vc_notify_vf_reset(vf);
149 
150 	/* We want to ensure that an actual reset occurs initiated after this
151 	 * function was called. However, we do not want to wait forever, so
152 	 * we'll give a reasonable time and print a message if we failed to
153 	 * ensure a reset.
154 	 */
155 	for (i = 0; i < 20; i++) {
156 		if (i40e_reset_vf(vf, false))
157 			return;
158 		usleep_range(10000, 20000);
159 	}
160 
161 	dev_warn(&vf->pf->pdev->dev,
162 		 "Failed to initiate reset for VF %d after 200 milliseconds\n",
163 		 vf->vf_id);
164 }
165 
166 /**
167  * i40e_vc_isvalid_vsi_id
168  * @vf: pointer to the VF info
169  * @vsi_id: VF relative VSI id
170  *
171  * check for the valid VSI id
172  **/
173 static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
174 {
175 	struct i40e_pf *pf = vf->pf;
176 	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
177 
178 	return (vsi && (vsi->vf_id == vf->vf_id));
179 }
180 
181 /**
182  * i40e_vc_isvalid_queue_id
183  * @vf: pointer to the VF info
184  * @vsi_id: vsi id
185  * @qid: vsi relative queue id
186  *
187  * check for the valid queue id
188  **/
189 static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
190 					    u16 qid)
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 && (qid < vsi->alloc_queue_pairs));
196 }
197 
198 /**
199  * i40e_vc_isvalid_vector_id
200  * @vf: pointer to the VF info
201  * @vector_id: VF relative vector id
202  *
203  * check for the valid vector id
204  **/
205 static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u32 vector_id)
206 {
207 	struct i40e_pf *pf = vf->pf;
208 
209 	return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
210 }
211 
212 /***********************vf resource mgmt routines*****************/
213 
214 /**
215  * i40e_vc_get_pf_queue_id
216  * @vf: pointer to the VF info
217  * @vsi_id: id of VSI as provided by the FW
218  * @vsi_queue_id: vsi relative queue id
219  *
220  * return PF relative queue id
221  **/
222 static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
223 				   u8 vsi_queue_id)
224 {
225 	struct i40e_pf *pf = vf->pf;
226 	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
227 	u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
228 
229 	if (!vsi)
230 		return pf_queue_id;
231 
232 	if (le16_to_cpu(vsi->info.mapping_flags) &
233 	    I40E_AQ_VSI_QUE_MAP_NONCONTIG)
234 		pf_queue_id =
235 			le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
236 	else
237 		pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
238 			      vsi_queue_id;
239 
240 	return pf_queue_id;
241 }
242 
243 /**
244  * i40e_get_real_pf_qid
245  * @vf: pointer to the VF info
246  * @vsi_id: vsi id
247  * @queue_id: queue number
248  *
249  * wrapper function to get pf_queue_id handling ADq code as well
250  **/
251 static u16 i40e_get_real_pf_qid(struct i40e_vf *vf, u16 vsi_id, u16 queue_id)
252 {
253 	int i;
254 
255 	if (vf->adq_enabled) {
256 		/* Although VF considers all the queues(can be 1 to 16) as its
257 		 * own but they may actually belong to different VSIs(up to 4).
258 		 * We need to find which queues belongs to which VSI.
259 		 */
260 		for (i = 0; i < vf->num_tc; i++) {
261 			if (queue_id < vf->ch[i].num_qps) {
262 				vsi_id = vf->ch[i].vsi_id;
263 				break;
264 			}
265 			/* find right queue id which is relative to a
266 			 * given VSI.
267 			 */
268 			queue_id -= vf->ch[i].num_qps;
269 			}
270 		}
271 
272 	return i40e_vc_get_pf_queue_id(vf, vsi_id, queue_id);
273 }
274 
275 /**
276  * i40e_config_irq_link_list
277  * @vf: pointer to the VF info
278  * @vsi_id: id of VSI as given by the FW
279  * @vecmap: irq map info
280  *
281  * configure irq link list from the map
282  **/
283 static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
284 				      struct virtchnl_vector_map *vecmap)
285 {
286 	unsigned long linklistmap = 0, tempmap;
287 	struct i40e_pf *pf = vf->pf;
288 	struct i40e_hw *hw = &pf->hw;
289 	u16 vsi_queue_id, pf_queue_id;
290 	enum i40e_queue_type qtype;
291 	u16 next_q, vector_id, size;
292 	u32 reg, reg_idx;
293 	u16 itr_idx = 0;
294 
295 	vector_id = vecmap->vector_id;
296 	/* setup the head */
297 	if (0 == vector_id)
298 		reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
299 	else
300 		reg_idx = I40E_VPINT_LNKLSTN(
301 		     ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
302 		     (vector_id - 1));
303 
304 	if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
305 		/* Special case - No queues mapped on this vector */
306 		wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
307 		goto irq_list_done;
308 	}
309 	tempmap = vecmap->rxq_map;
310 	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
311 		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
312 				    vsi_queue_id));
313 	}
314 
315 	tempmap = vecmap->txq_map;
316 	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
317 		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
318 				     vsi_queue_id + 1));
319 	}
320 
321 	size = I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES;
322 	next_q = find_first_bit(&linklistmap, size);
323 	if (unlikely(next_q == size))
324 		goto irq_list_done;
325 
326 	vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
327 	qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
328 	pf_queue_id = i40e_get_real_pf_qid(vf, vsi_id, vsi_queue_id);
329 	reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
330 
331 	wr32(hw, reg_idx, reg);
332 
333 	while (next_q < size) {
334 		switch (qtype) {
335 		case I40E_QUEUE_TYPE_RX:
336 			reg_idx = I40E_QINT_RQCTL(pf_queue_id);
337 			itr_idx = vecmap->rxitr_idx;
338 			break;
339 		case I40E_QUEUE_TYPE_TX:
340 			reg_idx = I40E_QINT_TQCTL(pf_queue_id);
341 			itr_idx = vecmap->txitr_idx;
342 			break;
343 		default:
344 			break;
345 		}
346 
347 		next_q = find_next_bit(&linklistmap, size, next_q + 1);
348 		if (next_q < size) {
349 			vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
350 			qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
351 			pf_queue_id = i40e_get_real_pf_qid(vf,
352 							   vsi_id,
353 							   vsi_queue_id);
354 		} else {
355 			pf_queue_id = I40E_QUEUE_END_OF_LIST;
356 			qtype = 0;
357 		}
358 
359 		/* format for the RQCTL & TQCTL regs is same */
360 		reg = (vector_id) |
361 		    (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
362 		    (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
363 		    BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
364 		    (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
365 		wr32(hw, reg_idx, reg);
366 	}
367 
368 	/* if the vf is running in polling mode and using interrupt zero,
369 	 * need to disable auto-mask on enabling zero interrupt for VFs.
370 	 */
371 	if ((vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
372 	    (vector_id == 0)) {
373 		reg = rd32(hw, I40E_GLINT_CTL);
374 		if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
375 			reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
376 			wr32(hw, I40E_GLINT_CTL, reg);
377 		}
378 	}
379 
380 irq_list_done:
381 	i40e_flush(hw);
382 }
383 
384 /**
385  * i40e_release_iwarp_qvlist
386  * @vf: pointer to the VF.
387  *
388  **/
389 static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
390 {
391 	struct i40e_pf *pf = vf->pf;
392 	struct virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
393 	u32 msix_vf;
394 	u32 i;
395 
396 	if (!vf->qvlist_info)
397 		return;
398 
399 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
400 	for (i = 0; i < qvlist_info->num_vectors; i++) {
401 		struct virtchnl_iwarp_qv_info *qv_info;
402 		u32 next_q_index, next_q_type;
403 		struct i40e_hw *hw = &pf->hw;
404 		u32 v_idx, reg_idx, reg;
405 
406 		qv_info = &qvlist_info->qv_info[i];
407 		if (!qv_info)
408 			continue;
409 		v_idx = qv_info->v_idx;
410 		if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
411 			/* Figure out the queue after CEQ and make that the
412 			 * first queue.
413 			 */
414 			reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
415 			reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
416 			next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
417 					>> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
418 			next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
419 					>> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;
420 
421 			reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
422 			reg = (next_q_index &
423 			       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
424 			       (next_q_type <<
425 			       I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
426 
427 			wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
428 		}
429 	}
430 	kfree(vf->qvlist_info);
431 	vf->qvlist_info = NULL;
432 }
433 
434 /**
435  * i40e_config_iwarp_qvlist
436  * @vf: pointer to the VF info
437  * @qvlist_info: queue and vector list
438  *
439  * Return 0 on success or < 0 on error
440  **/
441 static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
442 				    struct virtchnl_iwarp_qvlist_info *qvlist_info)
443 {
444 	struct i40e_pf *pf = vf->pf;
445 	struct i40e_hw *hw = &pf->hw;
446 	struct virtchnl_iwarp_qv_info *qv_info;
447 	u32 v_idx, i, reg_idx, reg;
448 	u32 next_q_idx, next_q_type;
449 	u32 msix_vf;
450 	int ret = 0;
451 
452 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
453 
454 	if (qvlist_info->num_vectors > msix_vf) {
455 		dev_warn(&pf->pdev->dev,
456 			 "Incorrect number of iwarp vectors %u. Maximum %u allowed.\n",
457 			 qvlist_info->num_vectors,
458 			 msix_vf);
459 		ret = -EINVAL;
460 		goto err_out;
461 	}
462 
463 	kfree(vf->qvlist_info);
464 	vf->qvlist_info = kzalloc(struct_size(vf->qvlist_info, qv_info,
465 					      qvlist_info->num_vectors - 1),
466 				  GFP_KERNEL);
467 	if (!vf->qvlist_info) {
468 		ret = -ENOMEM;
469 		goto err_out;
470 	}
471 	vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
472 
473 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
474 	for (i = 0; i < qvlist_info->num_vectors; i++) {
475 		qv_info = &qvlist_info->qv_info[i];
476 		if (!qv_info)
477 			continue;
478 
479 		/* Validate vector id belongs to this vf */
480 		if (!i40e_vc_isvalid_vector_id(vf, qv_info->v_idx)) {
481 			ret = -EINVAL;
482 			goto err_free;
483 		}
484 
485 		v_idx = qv_info->v_idx;
486 
487 		vf->qvlist_info->qv_info[i] = *qv_info;
488 
489 		reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
490 		/* We might be sharing the interrupt, so get the first queue
491 		 * index and type, push it down the list by adding the new
492 		 * queue on top. Also link it with the new queue in CEQCTL.
493 		 */
494 		reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
495 		next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
496 				I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
497 		next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
498 				I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
499 
500 		if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
501 			reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
502 			reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
503 			(v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
504 			(qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
505 			(next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
506 			(next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
507 			wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);
508 
509 			reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
510 			reg = (qv_info->ceq_idx &
511 			       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
512 			       (I40E_QUEUE_TYPE_PE_CEQ <<
513 			       I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
514 			wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
515 		}
516 
517 		if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
518 			reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
519 			(v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
520 			(qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));
521 
522 			wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
523 		}
524 	}
525 
526 	return 0;
527 err_free:
528 	kfree(vf->qvlist_info);
529 	vf->qvlist_info = NULL;
530 err_out:
531 	return ret;
532 }
533 
534 /**
535  * i40e_config_vsi_tx_queue
536  * @vf: pointer to the VF info
537  * @vsi_id: id of VSI as provided by the FW
538  * @vsi_queue_id: vsi relative queue index
539  * @info: config. info
540  *
541  * configure tx queue
542  **/
543 static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
544 				    u16 vsi_queue_id,
545 				    struct virtchnl_txq_info *info)
546 {
547 	struct i40e_pf *pf = vf->pf;
548 	struct i40e_hw *hw = &pf->hw;
549 	struct i40e_hmc_obj_txq tx_ctx;
550 	struct i40e_vsi *vsi;
551 	u16 pf_queue_id;
552 	u32 qtx_ctl;
553 	int ret = 0;
554 
555 	if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
556 		ret = -ENOENT;
557 		goto error_context;
558 	}
559 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
560 	vsi = i40e_find_vsi_from_id(pf, vsi_id);
561 	if (!vsi) {
562 		ret = -ENOENT;
563 		goto error_context;
564 	}
565 
566 	/* clear the context structure first */
567 	memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
568 
569 	/* only set the required fields */
570 	tx_ctx.base = info->dma_ring_addr / 128;
571 	tx_ctx.qlen = info->ring_len;
572 	tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
573 	tx_ctx.rdylist_act = 0;
574 	tx_ctx.head_wb_ena = info->headwb_enabled;
575 	tx_ctx.head_wb_addr = info->dma_headwb_addr;
576 
577 	/* clear the context in the HMC */
578 	ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
579 	if (ret) {
580 		dev_err(&pf->pdev->dev,
581 			"Failed to clear VF LAN Tx queue context %d, error: %d\n",
582 			pf_queue_id, ret);
583 		ret = -ENOENT;
584 		goto error_context;
585 	}
586 
587 	/* set the context in the HMC */
588 	ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
589 	if (ret) {
590 		dev_err(&pf->pdev->dev,
591 			"Failed to set VF LAN Tx queue context %d error: %d\n",
592 			pf_queue_id, ret);
593 		ret = -ENOENT;
594 		goto error_context;
595 	}
596 
597 	/* associate this queue with the PCI VF function */
598 	qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
599 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
600 		    & I40E_QTX_CTL_PF_INDX_MASK);
601 	qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
602 		     << I40E_QTX_CTL_VFVM_INDX_SHIFT)
603 		    & I40E_QTX_CTL_VFVM_INDX_MASK);
604 	wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
605 	i40e_flush(hw);
606 
607 error_context:
608 	return ret;
609 }
610 
611 /**
612  * i40e_config_vsi_rx_queue
613  * @vf: pointer to the VF info
614  * @vsi_id: id of VSI  as provided by the FW
615  * @vsi_queue_id: vsi relative queue index
616  * @info: config. info
617  *
618  * configure rx queue
619  **/
620 static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
621 				    u16 vsi_queue_id,
622 				    struct virtchnl_rxq_info *info)
623 {
624 	struct i40e_pf *pf = vf->pf;
625 	struct i40e_hw *hw = &pf->hw;
626 	struct i40e_hmc_obj_rxq rx_ctx;
627 	u16 pf_queue_id;
628 	int ret = 0;
629 
630 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
631 
632 	/* clear the context structure first */
633 	memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
634 
635 	/* only set the required fields */
636 	rx_ctx.base = info->dma_ring_addr / 128;
637 	rx_ctx.qlen = info->ring_len;
638 
639 	if (info->splithdr_enabled) {
640 		rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2      |
641 				  I40E_RX_SPLIT_IP      |
642 				  I40E_RX_SPLIT_TCP_UDP |
643 				  I40E_RX_SPLIT_SCTP;
644 		/* header length validation */
645 		if (info->hdr_size > ((2 * 1024) - 64)) {
646 			ret = -EINVAL;
647 			goto error_param;
648 		}
649 		rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
650 
651 		/* set split mode 10b */
652 		rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
653 	}
654 
655 	/* databuffer length validation */
656 	if (info->databuffer_size > ((16 * 1024) - 128)) {
657 		ret = -EINVAL;
658 		goto error_param;
659 	}
660 	rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
661 
662 	/* max pkt. length validation */
663 	if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
664 		ret = -EINVAL;
665 		goto error_param;
666 	}
667 	rx_ctx.rxmax = info->max_pkt_size;
668 
669 	/* enable 32bytes desc always */
670 	rx_ctx.dsize = 1;
671 
672 	/* default values */
673 	rx_ctx.lrxqthresh = 1;
674 	rx_ctx.crcstrip = 1;
675 	rx_ctx.prefena = 1;
676 	rx_ctx.l2tsel = 1;
677 
678 	/* clear the context in the HMC */
679 	ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
680 	if (ret) {
681 		dev_err(&pf->pdev->dev,
682 			"Failed to clear VF LAN Rx queue context %d, error: %d\n",
683 			pf_queue_id, ret);
684 		ret = -ENOENT;
685 		goto error_param;
686 	}
687 
688 	/* set the context in the HMC */
689 	ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
690 	if (ret) {
691 		dev_err(&pf->pdev->dev,
692 			"Failed to set VF LAN Rx queue context %d error: %d\n",
693 			pf_queue_id, ret);
694 		ret = -ENOENT;
695 		goto error_param;
696 	}
697 
698 error_param:
699 	return ret;
700 }
701 
702 /**
703  * i40e_alloc_vsi_res
704  * @vf: pointer to the VF info
705  * @idx: VSI index, applies only for ADq mode, zero otherwise
706  *
707  * alloc VF vsi context & resources
708  **/
709 static int i40e_alloc_vsi_res(struct i40e_vf *vf, u8 idx)
710 {
711 	struct i40e_mac_filter *f = NULL;
712 	struct i40e_pf *pf = vf->pf;
713 	struct i40e_vsi *vsi;
714 	u64 max_tx_rate = 0;
715 	int ret = 0;
716 
717 	vsi = i40e_vsi_setup(pf, I40E_VSI_SRIOV, pf->vsi[pf->lan_vsi]->seid,
718 			     vf->vf_id);
719 
720 	if (!vsi) {
721 		dev_err(&pf->pdev->dev,
722 			"add vsi failed for VF %d, aq_err %d\n",
723 			vf->vf_id, pf->hw.aq.asq_last_status);
724 		ret = -ENOENT;
725 		goto error_alloc_vsi_res;
726 	}
727 
728 	if (!idx) {
729 		u64 hena = i40e_pf_get_default_rss_hena(pf);
730 		u8 broadcast[ETH_ALEN];
731 
732 		vf->lan_vsi_idx = vsi->idx;
733 		vf->lan_vsi_id = vsi->id;
734 		/* If the port VLAN has been configured and then the
735 		 * VF driver was removed then the VSI port VLAN
736 		 * configuration was destroyed.  Check if there is
737 		 * a port VLAN and restore the VSI configuration if
738 		 * needed.
739 		 */
740 		if (vf->port_vlan_id)
741 			i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
742 
743 		spin_lock_bh(&vsi->mac_filter_hash_lock);
744 		if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
745 			f = i40e_add_mac_filter(vsi,
746 						vf->default_lan_addr.addr);
747 			if (!f)
748 				dev_info(&pf->pdev->dev,
749 					 "Could not add MAC filter %pM for VF %d\n",
750 					vf->default_lan_addr.addr, vf->vf_id);
751 		}
752 		eth_broadcast_addr(broadcast);
753 		f = i40e_add_mac_filter(vsi, broadcast);
754 		if (!f)
755 			dev_info(&pf->pdev->dev,
756 				 "Could not allocate VF broadcast filter\n");
757 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
758 		wr32(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)hena);
759 		wr32(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id), (u32)(hena >> 32));
760 		/* program mac filter only for VF VSI */
761 		ret = i40e_sync_vsi_filters(vsi);
762 		if (ret)
763 			dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
764 	}
765 
766 	/* storing VSI index and id for ADq and don't apply the mac filter */
767 	if (vf->adq_enabled) {
768 		vf->ch[idx].vsi_idx = vsi->idx;
769 		vf->ch[idx].vsi_id = vsi->id;
770 	}
771 
772 	/* Set VF bandwidth if specified */
773 	if (vf->tx_rate) {
774 		max_tx_rate = vf->tx_rate;
775 	} else if (vf->ch[idx].max_tx_rate) {
776 		max_tx_rate = vf->ch[idx].max_tx_rate;
777 	}
778 
779 	if (max_tx_rate) {
780 		max_tx_rate = div_u64(max_tx_rate, I40E_BW_CREDIT_DIVISOR);
781 		ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
782 						  max_tx_rate, 0, NULL);
783 		if (ret)
784 			dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
785 				vf->vf_id, ret);
786 	}
787 
788 error_alloc_vsi_res:
789 	return ret;
790 }
791 
792 /**
793  * i40e_map_pf_queues_to_vsi
794  * @vf: pointer to the VF info
795  *
796  * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
797  * function takes care of first part VSILAN_QTABLE, mapping pf queues to VSI.
798  **/
799 static void i40e_map_pf_queues_to_vsi(struct i40e_vf *vf)
800 {
801 	struct i40e_pf *pf = vf->pf;
802 	struct i40e_hw *hw = &pf->hw;
803 	u32 reg, num_tc = 1; /* VF has at least one traffic class */
804 	u16 vsi_id, qps;
805 	int i, j;
806 
807 	if (vf->adq_enabled)
808 		num_tc = vf->num_tc;
809 
810 	for (i = 0; i < num_tc; i++) {
811 		if (vf->adq_enabled) {
812 			qps = vf->ch[i].num_qps;
813 			vsi_id =  vf->ch[i].vsi_id;
814 		} else {
815 			qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
816 			vsi_id = vf->lan_vsi_id;
817 		}
818 
819 		for (j = 0; j < 7; j++) {
820 			if (j * 2 >= qps) {
821 				/* end of list */
822 				reg = 0x07FF07FF;
823 			} else {
824 				u16 qid = i40e_vc_get_pf_queue_id(vf,
825 								  vsi_id,
826 								  j * 2);
827 				reg = qid;
828 				qid = i40e_vc_get_pf_queue_id(vf, vsi_id,
829 							      (j * 2) + 1);
830 				reg |= qid << 16;
831 			}
832 			i40e_write_rx_ctl(hw,
833 					  I40E_VSILAN_QTABLE(j, vsi_id),
834 					  reg);
835 		}
836 	}
837 }
838 
839 /**
840  * i40e_map_pf_to_vf_queues
841  * @vf: pointer to the VF info
842  *
843  * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
844  * function takes care of the second part VPLAN_QTABLE & completes VF mappings.
845  **/
846 static void i40e_map_pf_to_vf_queues(struct i40e_vf *vf)
847 {
848 	struct i40e_pf *pf = vf->pf;
849 	struct i40e_hw *hw = &pf->hw;
850 	u32 reg, total_qps = 0;
851 	u32 qps, num_tc = 1; /* VF has at least one traffic class */
852 	u16 vsi_id, qid;
853 	int i, j;
854 
855 	if (vf->adq_enabled)
856 		num_tc = vf->num_tc;
857 
858 	for (i = 0; i < num_tc; i++) {
859 		if (vf->adq_enabled) {
860 			qps = vf->ch[i].num_qps;
861 			vsi_id =  vf->ch[i].vsi_id;
862 		} else {
863 			qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
864 			vsi_id = vf->lan_vsi_id;
865 		}
866 
867 		for (j = 0; j < qps; j++) {
868 			qid = i40e_vc_get_pf_queue_id(vf, vsi_id, j);
869 
870 			reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
871 			wr32(hw, I40E_VPLAN_QTABLE(total_qps, vf->vf_id),
872 			     reg);
873 			total_qps++;
874 		}
875 	}
876 }
877 
878 /**
879  * i40e_enable_vf_mappings
880  * @vf: pointer to the VF info
881  *
882  * enable VF mappings
883  **/
884 static void i40e_enable_vf_mappings(struct i40e_vf *vf)
885 {
886 	struct i40e_pf *pf = vf->pf;
887 	struct i40e_hw *hw = &pf->hw;
888 	u32 reg;
889 
890 	/* Tell the hardware we're using noncontiguous mapping. HW requires
891 	 * that VF queues be mapped using this method, even when they are
892 	 * contiguous in real life
893 	 */
894 	i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
895 			  I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
896 
897 	/* enable VF vplan_qtable mappings */
898 	reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
899 	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
900 
901 	i40e_map_pf_to_vf_queues(vf);
902 	i40e_map_pf_queues_to_vsi(vf);
903 
904 	i40e_flush(hw);
905 }
906 
907 /**
908  * i40e_disable_vf_mappings
909  * @vf: pointer to the VF info
910  *
911  * disable VF mappings
912  **/
913 static void i40e_disable_vf_mappings(struct i40e_vf *vf)
914 {
915 	struct i40e_pf *pf = vf->pf;
916 	struct i40e_hw *hw = &pf->hw;
917 	int i;
918 
919 	/* disable qp mappings */
920 	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
921 	for (i = 0; i < I40E_MAX_VSI_QP; i++)
922 		wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
923 		     I40E_QUEUE_END_OF_LIST);
924 	i40e_flush(hw);
925 }
926 
927 /**
928  * i40e_free_vf_res
929  * @vf: pointer to the VF info
930  *
931  * free VF resources
932  **/
933 static void i40e_free_vf_res(struct i40e_vf *vf)
934 {
935 	struct i40e_pf *pf = vf->pf;
936 	struct i40e_hw *hw = &pf->hw;
937 	u32 reg_idx, reg;
938 	int i, j, msix_vf;
939 
940 	/* Start by disabling VF's configuration API to prevent the OS from
941 	 * accessing the VF's VSI after it's freed / invalidated.
942 	 */
943 	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
944 
945 	/* It's possible the VF had requeuested more queues than the default so
946 	 * do the accounting here when we're about to free them.
947 	 */
948 	if (vf->num_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF) {
949 		pf->queues_left += vf->num_queue_pairs -
950 				   I40E_DEFAULT_QUEUES_PER_VF;
951 	}
952 
953 	/* free vsi & disconnect it from the parent uplink */
954 	if (vf->lan_vsi_idx) {
955 		i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
956 		vf->lan_vsi_idx = 0;
957 		vf->lan_vsi_id = 0;
958 	}
959 
960 	/* do the accounting and remove additional ADq VSI's */
961 	if (vf->adq_enabled && vf->ch[0].vsi_idx) {
962 		for (j = 0; j < vf->num_tc; j++) {
963 			/* At this point VSI0 is already released so don't
964 			 * release it again and only clear their values in
965 			 * structure variables
966 			 */
967 			if (j)
968 				i40e_vsi_release(pf->vsi[vf->ch[j].vsi_idx]);
969 			vf->ch[j].vsi_idx = 0;
970 			vf->ch[j].vsi_id = 0;
971 		}
972 	}
973 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
974 
975 	/* disable interrupts so the VF starts in a known state */
976 	for (i = 0; i < msix_vf; i++) {
977 		/* format is same for both registers */
978 		if (0 == i)
979 			reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
980 		else
981 			reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
982 						      (vf->vf_id))
983 						     + (i - 1));
984 		wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
985 		i40e_flush(hw);
986 	}
987 
988 	/* clear the irq settings */
989 	for (i = 0; i < msix_vf; i++) {
990 		/* format is same for both registers */
991 		if (0 == i)
992 			reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
993 		else
994 			reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
995 						      (vf->vf_id))
996 						     + (i - 1));
997 		reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
998 		       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
999 		wr32(hw, reg_idx, reg);
1000 		i40e_flush(hw);
1001 	}
1002 	/* reset some of the state variables keeping track of the resources */
1003 	vf->num_queue_pairs = 0;
1004 	clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
1005 	clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
1006 }
1007 
1008 /**
1009  * i40e_alloc_vf_res
1010  * @vf: pointer to the VF info
1011  *
1012  * allocate VF resources
1013  **/
1014 static int i40e_alloc_vf_res(struct i40e_vf *vf)
1015 {
1016 	struct i40e_pf *pf = vf->pf;
1017 	int total_queue_pairs = 0;
1018 	int ret, idx;
1019 
1020 	if (vf->num_req_queues &&
1021 	    vf->num_req_queues <= pf->queues_left + I40E_DEFAULT_QUEUES_PER_VF)
1022 		pf->num_vf_qps = vf->num_req_queues;
1023 	else
1024 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
1025 
1026 	/* allocate hw vsi context & associated resources */
1027 	ret = i40e_alloc_vsi_res(vf, 0);
1028 	if (ret)
1029 		goto error_alloc;
1030 	total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
1031 
1032 	/* allocate additional VSIs based on tc information for ADq */
1033 	if (vf->adq_enabled) {
1034 		if (pf->queues_left >=
1035 		    (I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF)) {
1036 			/* TC 0 always belongs to VF VSI */
1037 			for (idx = 1; idx < vf->num_tc; idx++) {
1038 				ret = i40e_alloc_vsi_res(vf, idx);
1039 				if (ret)
1040 					goto error_alloc;
1041 			}
1042 			/* send correct number of queues */
1043 			total_queue_pairs = I40E_MAX_VF_QUEUES;
1044 		} else {
1045 			dev_info(&pf->pdev->dev, "VF %d: Not enough queues to allocate, disabling ADq\n",
1046 				 vf->vf_id);
1047 			vf->adq_enabled = false;
1048 		}
1049 	}
1050 
1051 	/* We account for each VF to get a default number of queue pairs.  If
1052 	 * the VF has now requested more, we need to account for that to make
1053 	 * certain we never request more queues than we actually have left in
1054 	 * HW.
1055 	 */
1056 	if (total_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF)
1057 		pf->queues_left -=
1058 			total_queue_pairs - I40E_DEFAULT_QUEUES_PER_VF;
1059 
1060 	if (vf->trusted)
1061 		set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1062 	else
1063 		clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
1064 
1065 	/* store the total qps number for the runtime
1066 	 * VF req validation
1067 	 */
1068 	vf->num_queue_pairs = total_queue_pairs;
1069 
1070 	/* VF is now completely initialized */
1071 	set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
1072 
1073 error_alloc:
1074 	if (ret)
1075 		i40e_free_vf_res(vf);
1076 
1077 	return ret;
1078 }
1079 
1080 #define VF_DEVICE_STATUS 0xAA
1081 #define VF_TRANS_PENDING_MASK 0x20
1082 /**
1083  * i40e_quiesce_vf_pci
1084  * @vf: pointer to the VF structure
1085  *
1086  * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
1087  * if the transactions never clear.
1088  **/
1089 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
1090 {
1091 	struct i40e_pf *pf = vf->pf;
1092 	struct i40e_hw *hw = &pf->hw;
1093 	int vf_abs_id, i;
1094 	u32 reg;
1095 
1096 	vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
1097 
1098 	wr32(hw, I40E_PF_PCI_CIAA,
1099 	     VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
1100 	for (i = 0; i < 100; i++) {
1101 		reg = rd32(hw, I40E_PF_PCI_CIAD);
1102 		if ((reg & VF_TRANS_PENDING_MASK) == 0)
1103 			return 0;
1104 		udelay(1);
1105 	}
1106 	return -EIO;
1107 }
1108 
1109 /**
1110  * i40e_getnum_vf_vsi_vlan_filters
1111  * @vsi: pointer to the vsi
1112  *
1113  * called to get the number of VLANs offloaded on this VF
1114  **/
1115 static int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
1116 {
1117 	struct i40e_mac_filter *f;
1118 	int num_vlans = 0, bkt;
1119 
1120 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1121 		if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
1122 			num_vlans++;
1123 	}
1124 
1125 	return num_vlans;
1126 }
1127 
1128 /**
1129  * i40e_get_vlan_list_sync
1130  * @vsi: pointer to the VSI
1131  * @num_vlans: number of VLANs in mac_filter_hash, returned to caller
1132  * @vlan_list: list of VLANs present in mac_filter_hash, returned to caller.
1133  *             This array is allocated here, but has to be freed in caller.
1134  *
1135  * Called to get number of VLANs and VLAN list present in mac_filter_hash.
1136  **/
1137 static void i40e_get_vlan_list_sync(struct i40e_vsi *vsi, int *num_vlans,
1138 					   s16 **vlan_list)
1139 {
1140 	struct i40e_mac_filter *f;
1141 	int i = 0;
1142 	int bkt;
1143 
1144 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1145 	*num_vlans = i40e_getnum_vf_vsi_vlan_filters(vsi);
1146 	*vlan_list = kcalloc(*num_vlans, sizeof(**vlan_list), GFP_ATOMIC);
1147 	if (!(*vlan_list))
1148 		goto err;
1149 
1150 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1151 		if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1152 			continue;
1153 		(*vlan_list)[i++] = f->vlan;
1154 	}
1155 err:
1156 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1157 }
1158 
1159 /**
1160  * i40e_set_vsi_promisc
1161  * @vf: pointer to the VF struct
1162  * @seid: VSI number
1163  * @multi_enable: set MAC L2 layer multicast promiscuous enable/disable
1164  *                for a given VLAN
1165  * @unicast_enable: set MAC L2 layer unicast promiscuous enable/disable
1166  *                  for a given VLAN
1167  * @vl: List of VLANs - apply filter for given VLANs
1168  * @num_vlans: Number of elements in @vl
1169  **/
1170 static i40e_status
1171 i40e_set_vsi_promisc(struct i40e_vf *vf, u16 seid, bool multi_enable,
1172 		     bool unicast_enable, s16 *vl, int num_vlans)
1173 {
1174 	struct i40e_pf *pf = vf->pf;
1175 	struct i40e_hw *hw = &pf->hw;
1176 	i40e_status aq_ret;
1177 	int i;
1178 
1179 	/* No VLAN to set promisc on, set on VSI */
1180 	if (!num_vlans || !vl) {
1181 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, seid,
1182 							       multi_enable,
1183 							       NULL);
1184 		if (aq_ret) {
1185 			int aq_err = pf->hw.aq.asq_last_status;
1186 
1187 			dev_err(&pf->pdev->dev,
1188 				"VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1189 				vf->vf_id,
1190 				i40e_stat_str(&pf->hw, aq_ret),
1191 				i40e_aq_str(&pf->hw, aq_err));
1192 
1193 			return aq_ret;
1194 		}
1195 
1196 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, seid,
1197 							     unicast_enable,
1198 							     NULL, true);
1199 
1200 		if (aq_ret) {
1201 			int aq_err = pf->hw.aq.asq_last_status;
1202 
1203 			dev_err(&pf->pdev->dev,
1204 				"VF %d failed to set unicast promiscuous mode err %s aq_err %s\n",
1205 				vf->vf_id,
1206 				i40e_stat_str(&pf->hw, aq_ret),
1207 				i40e_aq_str(&pf->hw, aq_err));
1208 		}
1209 
1210 		return aq_ret;
1211 	}
1212 
1213 	for (i = 0; i < num_vlans; i++) {
1214 		aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, seid,
1215 							    multi_enable,
1216 							    vl[i], NULL);
1217 		if (aq_ret) {
1218 			int aq_err = pf->hw.aq.asq_last_status;
1219 
1220 			dev_err(&pf->pdev->dev,
1221 				"VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1222 				vf->vf_id,
1223 				i40e_stat_str(&pf->hw, aq_ret),
1224 				i40e_aq_str(&pf->hw, aq_err));
1225 		}
1226 
1227 		aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, seid,
1228 							    unicast_enable,
1229 							    vl[i], NULL);
1230 		if (aq_ret) {
1231 			int aq_err = pf->hw.aq.asq_last_status;
1232 
1233 			dev_err(&pf->pdev->dev,
1234 				"VF %d failed to set unicast promiscuous mode err %s aq_err %s\n",
1235 				vf->vf_id,
1236 				i40e_stat_str(&pf->hw, aq_ret),
1237 				i40e_aq_str(&pf->hw, aq_err));
1238 		}
1239 	}
1240 	return aq_ret;
1241 }
1242 
1243 /**
1244  * i40e_config_vf_promiscuous_mode
1245  * @vf: pointer to the VF info
1246  * @vsi_id: VSI id
1247  * @allmulti: set MAC L2 layer multicast promiscuous enable/disable
1248  * @alluni: set MAC L2 layer unicast promiscuous enable/disable
1249  *
1250  * Called from the VF to configure the promiscuous mode of
1251  * VF vsis and from the VF reset path to reset promiscuous mode.
1252  **/
1253 static i40e_status i40e_config_vf_promiscuous_mode(struct i40e_vf *vf,
1254 						   u16 vsi_id,
1255 						   bool allmulti,
1256 						   bool alluni)
1257 {
1258 	i40e_status aq_ret = I40E_SUCCESS;
1259 	struct i40e_pf *pf = vf->pf;
1260 	struct i40e_vsi *vsi;
1261 	int num_vlans;
1262 	s16 *vl;
1263 
1264 	vsi = i40e_find_vsi_from_id(pf, vsi_id);
1265 	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id) || !vsi)
1266 		return I40E_ERR_PARAM;
1267 
1268 	if (vf->port_vlan_id) {
1269 		aq_ret = i40e_set_vsi_promisc(vf, vsi->seid, allmulti,
1270 					      alluni, &vf->port_vlan_id, 1);
1271 		return aq_ret;
1272 	} else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1273 		i40e_get_vlan_list_sync(vsi, &num_vlans, &vl);
1274 
1275 		if (!vl)
1276 			return I40E_ERR_NO_MEMORY;
1277 
1278 		aq_ret = i40e_set_vsi_promisc(vf, vsi->seid, allmulti, alluni,
1279 					      vl, num_vlans);
1280 		kfree(vl);
1281 		return aq_ret;
1282 	}
1283 
1284 	/* no VLANs to set on, set on VSI */
1285 	aq_ret = i40e_set_vsi_promisc(vf, vsi->seid, allmulti, alluni,
1286 				      NULL, 0);
1287 	return aq_ret;
1288 }
1289 
1290 /**
1291  * i40e_trigger_vf_reset
1292  * @vf: pointer to the VF structure
1293  * @flr: VFLR was issued or not
1294  *
1295  * Trigger hardware to start a reset for a particular VF. Expects the caller
1296  * to wait the proper amount of time to allow hardware to reset the VF before
1297  * it cleans up and restores VF functionality.
1298  **/
1299 static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
1300 {
1301 	struct i40e_pf *pf = vf->pf;
1302 	struct i40e_hw *hw = &pf->hw;
1303 	u32 reg, reg_idx, bit_idx;
1304 
1305 	/* warn the VF */
1306 	clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1307 
1308 	/* Disable VF's configuration API during reset. The flag is re-enabled
1309 	 * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
1310 	 * It's normally disabled in i40e_free_vf_res(), but it's safer
1311 	 * to do it earlier to give some time to finish to any VF config
1312 	 * functions that may still be running at this point.
1313 	 */
1314 	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
1315 
1316 	/* In the case of a VFLR, the HW has already reset the VF and we
1317 	 * just need to clean up, so don't hit the VFRTRIG register.
1318 	 */
1319 	if (!flr) {
1320 		/* reset VF using VPGEN_VFRTRIG reg */
1321 		reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1322 		reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1323 		wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1324 		i40e_flush(hw);
1325 	}
1326 	/* clear the VFLR bit in GLGEN_VFLRSTAT */
1327 	reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
1328 	bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
1329 	wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1330 	i40e_flush(hw);
1331 
1332 	if (i40e_quiesce_vf_pci(vf))
1333 		dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
1334 			vf->vf_id);
1335 }
1336 
1337 /**
1338  * i40e_cleanup_reset_vf
1339  * @vf: pointer to the VF structure
1340  *
1341  * Cleanup a VF after the hardware reset is finished. Expects the caller to
1342  * have verified whether the reset is finished properly, and ensure the
1343  * minimum amount of wait time has passed.
1344  **/
1345 static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
1346 {
1347 	struct i40e_pf *pf = vf->pf;
1348 	struct i40e_hw *hw = &pf->hw;
1349 	u32 reg;
1350 
1351 	/* disable promisc modes in case they were enabled */
1352 	i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id, false, false);
1353 
1354 	/* free VF resources to begin resetting the VSI state */
1355 	i40e_free_vf_res(vf);
1356 
1357 	/* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
1358 	 * By doing this we allow HW to access VF memory at any point. If we
1359 	 * did it any sooner, HW could access memory while it was being freed
1360 	 * in i40e_free_vf_res(), causing an IOMMU fault.
1361 	 *
1362 	 * On the other hand, this needs to be done ASAP, because the VF driver
1363 	 * is waiting for this to happen and may report a timeout. It's
1364 	 * harmless, but it gets logged into Guest OS kernel log, so best avoid
1365 	 * it.
1366 	 */
1367 	reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1368 	reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1369 	wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1370 
1371 	/* reallocate VF resources to finish resetting the VSI state */
1372 	if (!i40e_alloc_vf_res(vf)) {
1373 		int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1374 		i40e_enable_vf_mappings(vf);
1375 		set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1376 		clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1377 		/* Do not notify the client during VF init */
1378 		if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
1379 					&vf->vf_states))
1380 			i40e_notify_client_of_vf_reset(pf, abs_vf_id);
1381 		vf->num_vlan = 0;
1382 	}
1383 
1384 	/* Tell the VF driver the reset is done. This needs to be done only
1385 	 * after VF has been fully initialized, because the VF driver may
1386 	 * request resources immediately after setting this flag.
1387 	 */
1388 	wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1389 }
1390 
1391 /**
1392  * i40e_reset_vf
1393  * @vf: pointer to the VF structure
1394  * @flr: VFLR was issued or not
1395  *
1396  * Returns true if the VF is reset, false otherwise.
1397  **/
1398 bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
1399 {
1400 	struct i40e_pf *pf = vf->pf;
1401 	struct i40e_hw *hw = &pf->hw;
1402 	bool rsd = false;
1403 	u32 reg;
1404 	int i;
1405 
1406 	/* If the VFs have been disabled, this means something else is
1407 	 * resetting the VF, so we shouldn't continue.
1408 	 */
1409 	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1410 		return false;
1411 
1412 	i40e_trigger_vf_reset(vf, flr);
1413 
1414 	/* poll VPGEN_VFRSTAT reg to make sure
1415 	 * that reset is complete
1416 	 */
1417 	for (i = 0; i < 10; i++) {
1418 		/* VF reset requires driver to first reset the VF and then
1419 		 * poll the status register to make sure that the reset
1420 		 * completed successfully. Due to internal HW FIFO flushes,
1421 		 * we must wait 10ms before the register will be valid.
1422 		 */
1423 		usleep_range(10000, 20000);
1424 		reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1425 		if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
1426 			rsd = true;
1427 			break;
1428 		}
1429 	}
1430 
1431 	if (flr)
1432 		usleep_range(10000, 20000);
1433 
1434 	if (!rsd)
1435 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1436 			vf->vf_id);
1437 	usleep_range(10000, 20000);
1438 
1439 	/* On initial reset, we don't have any queues to disable */
1440 	if (vf->lan_vsi_idx != 0)
1441 		i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
1442 
1443 	i40e_cleanup_reset_vf(vf);
1444 
1445 	i40e_flush(hw);
1446 	clear_bit(__I40E_VF_DISABLE, pf->state);
1447 
1448 	return true;
1449 }
1450 
1451 /**
1452  * i40e_reset_all_vfs
1453  * @pf: pointer to the PF structure
1454  * @flr: VFLR was issued or not
1455  *
1456  * Reset all allocated VFs in one go. First, tell the hardware to reset each
1457  * VF, then do all the waiting in one chunk, and finally finish restoring each
1458  * VF after the wait. This is useful during PF routines which need to reset
1459  * all VFs, as otherwise it must perform these resets in a serialized fashion.
1460  *
1461  * Returns true if any VFs were reset, and false otherwise.
1462  **/
1463 bool i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
1464 {
1465 	struct i40e_hw *hw = &pf->hw;
1466 	struct i40e_vf *vf;
1467 	int i, v;
1468 	u32 reg;
1469 
1470 	/* If we don't have any VFs, then there is nothing to reset */
1471 	if (!pf->num_alloc_vfs)
1472 		return false;
1473 
1474 	/* If VFs have been disabled, there is no need to reset */
1475 	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1476 		return false;
1477 
1478 	/* Begin reset on all VFs at once */
1479 	for (v = 0; v < pf->num_alloc_vfs; v++)
1480 		i40e_trigger_vf_reset(&pf->vf[v], flr);
1481 
1482 	/* HW requires some time to make sure it can flush the FIFO for a VF
1483 	 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1484 	 * sequence to make sure that it has completed. We'll keep track of
1485 	 * the VFs using a simple iterator that increments once that VF has
1486 	 * finished resetting.
1487 	 */
1488 	for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1489 		usleep_range(10000, 20000);
1490 
1491 		/* Check each VF in sequence, beginning with the VF to fail
1492 		 * the previous check.
1493 		 */
1494 		while (v < pf->num_alloc_vfs) {
1495 			vf = &pf->vf[v];
1496 			reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1497 			if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
1498 				break;
1499 
1500 			/* If the current VF has finished resetting, move on
1501 			 * to the next VF in sequence.
1502 			 */
1503 			v++;
1504 		}
1505 	}
1506 
1507 	if (flr)
1508 		usleep_range(10000, 20000);
1509 
1510 	/* Display a warning if at least one VF didn't manage to reset in
1511 	 * time, but continue on with the operation.
1512 	 */
1513 	if (v < pf->num_alloc_vfs)
1514 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1515 			pf->vf[v].vf_id);
1516 	usleep_range(10000, 20000);
1517 
1518 	/* Begin disabling all the rings associated with VFs, but do not wait
1519 	 * between each VF.
1520 	 */
1521 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1522 		/* On initial reset, we don't have any queues to disable */
1523 		if (pf->vf[v].lan_vsi_idx == 0)
1524 			continue;
1525 
1526 		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
1527 	}
1528 
1529 	/* Now that we've notified HW to disable all of the VF rings, wait
1530 	 * until they finish.
1531 	 */
1532 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1533 		/* On initial reset, we don't have any queues to disable */
1534 		if (pf->vf[v].lan_vsi_idx == 0)
1535 			continue;
1536 
1537 		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
1538 	}
1539 
1540 	/* Hw may need up to 50ms to finish disabling the RX queues. We
1541 	 * minimize the wait by delaying only once for all VFs.
1542 	 */
1543 	mdelay(50);
1544 
1545 	/* Finish the reset on each VF */
1546 	for (v = 0; v < pf->num_alloc_vfs; v++)
1547 		i40e_cleanup_reset_vf(&pf->vf[v]);
1548 
1549 	i40e_flush(hw);
1550 	clear_bit(__I40E_VF_DISABLE, pf->state);
1551 
1552 	return true;
1553 }
1554 
1555 /**
1556  * i40e_free_vfs
1557  * @pf: pointer to the PF structure
1558  *
1559  * free VF resources
1560  **/
1561 void i40e_free_vfs(struct i40e_pf *pf)
1562 {
1563 	struct i40e_hw *hw = &pf->hw;
1564 	u32 reg_idx, bit_idx;
1565 	int i, tmp, vf_id;
1566 
1567 	if (!pf->vf)
1568 		return;
1569 	while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1570 		usleep_range(1000, 2000);
1571 
1572 	i40e_notify_client_of_vf_enable(pf, 0);
1573 
1574 	/* Amortize wait time by stopping all VFs at the same time */
1575 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1576 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1577 			continue;
1578 
1579 		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
1580 	}
1581 
1582 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1583 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1584 			continue;
1585 
1586 		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
1587 	}
1588 
1589 	/* Disable IOV before freeing resources. This lets any VF drivers
1590 	 * running in the host get themselves cleaned up before we yank
1591 	 * the carpet out from underneath their feet.
1592 	 */
1593 	if (!pci_vfs_assigned(pf->pdev))
1594 		pci_disable_sriov(pf->pdev);
1595 	else
1596 		dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1597 
1598 	/* free up VF resources */
1599 	tmp = pf->num_alloc_vfs;
1600 	pf->num_alloc_vfs = 0;
1601 	for (i = 0; i < tmp; i++) {
1602 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1603 			i40e_free_vf_res(&pf->vf[i]);
1604 		/* disable qp mappings */
1605 		i40e_disable_vf_mappings(&pf->vf[i]);
1606 	}
1607 
1608 	kfree(pf->vf);
1609 	pf->vf = NULL;
1610 
1611 	/* This check is for when the driver is unloaded while VFs are
1612 	 * assigned. Setting the number of VFs to 0 through sysfs is caught
1613 	 * before this function ever gets called.
1614 	 */
1615 	if (!pci_vfs_assigned(pf->pdev)) {
1616 		/* Acknowledge VFLR for all VFS. Without this, VFs will fail to
1617 		 * work correctly when SR-IOV gets re-enabled.
1618 		 */
1619 		for (vf_id = 0; vf_id < tmp; vf_id++) {
1620 			reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1621 			bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1622 			wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1623 		}
1624 	}
1625 	clear_bit(__I40E_VF_DISABLE, pf->state);
1626 }
1627 
1628 #ifdef CONFIG_PCI_IOV
1629 /**
1630  * i40e_alloc_vfs
1631  * @pf: pointer to the PF structure
1632  * @num_alloc_vfs: number of VFs to allocate
1633  *
1634  * allocate VF resources
1635  **/
1636 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1637 {
1638 	struct i40e_vf *vfs;
1639 	int i, ret = 0;
1640 
1641 	/* Disable interrupt 0 so we don't try to handle the VFLR. */
1642 	i40e_irq_dynamic_disable_icr0(pf);
1643 
1644 	/* Check to see if we're just allocating resources for extant VFs */
1645 	if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
1646 		ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1647 		if (ret) {
1648 			pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1649 			pf->num_alloc_vfs = 0;
1650 			goto err_iov;
1651 		}
1652 	}
1653 	/* allocate memory */
1654 	vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1655 	if (!vfs) {
1656 		ret = -ENOMEM;
1657 		goto err_alloc;
1658 	}
1659 	pf->vf = vfs;
1660 
1661 	/* apply default profile */
1662 	for (i = 0; i < num_alloc_vfs; i++) {
1663 		vfs[i].pf = pf;
1664 		vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
1665 		vfs[i].vf_id = i;
1666 
1667 		/* assign default capabilities */
1668 		set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1669 		vfs[i].spoofchk = true;
1670 
1671 		set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
1672 
1673 	}
1674 	pf->num_alloc_vfs = num_alloc_vfs;
1675 
1676 	/* VF resources get allocated during reset */
1677 	i40e_reset_all_vfs(pf, false);
1678 
1679 	i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
1680 
1681 err_alloc:
1682 	if (ret)
1683 		i40e_free_vfs(pf);
1684 err_iov:
1685 	/* Re-enable interrupt 0. */
1686 	i40e_irq_dynamic_enable_icr0(pf);
1687 	return ret;
1688 }
1689 
1690 #endif
1691 /**
1692  * i40e_pci_sriov_enable
1693  * @pdev: pointer to a pci_dev structure
1694  * @num_vfs: number of VFs to allocate
1695  *
1696  * Enable or change the number of VFs
1697  **/
1698 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1699 {
1700 #ifdef CONFIG_PCI_IOV
1701 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1702 	int pre_existing_vfs = pci_num_vf(pdev);
1703 	int err = 0;
1704 
1705 	if (test_bit(__I40E_TESTING, pf->state)) {
1706 		dev_warn(&pdev->dev,
1707 			 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
1708 		err = -EPERM;
1709 		goto err_out;
1710 	}
1711 
1712 	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1713 		i40e_free_vfs(pf);
1714 	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1715 		goto out;
1716 
1717 	if (num_vfs > pf->num_req_vfs) {
1718 		dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1719 			 num_vfs, pf->num_req_vfs);
1720 		err = -EPERM;
1721 		goto err_out;
1722 	}
1723 
1724 	dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1725 	err = i40e_alloc_vfs(pf, num_vfs);
1726 	if (err) {
1727 		dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1728 		goto err_out;
1729 	}
1730 
1731 out:
1732 	return num_vfs;
1733 
1734 err_out:
1735 	return err;
1736 #endif
1737 	return 0;
1738 }
1739 
1740 /**
1741  * i40e_pci_sriov_configure
1742  * @pdev: pointer to a pci_dev structure
1743  * @num_vfs: number of VFs to allocate
1744  *
1745  * Enable or change the number of VFs. Called when the user updates the number
1746  * of VFs in sysfs.
1747  **/
1748 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1749 {
1750 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1751 	int ret = 0;
1752 
1753 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
1754 		dev_warn(&pdev->dev, "Unable to configure VFs, other operation is pending.\n");
1755 		return -EAGAIN;
1756 	}
1757 
1758 	if (num_vfs) {
1759 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1760 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1761 			i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
1762 		}
1763 		ret = i40e_pci_sriov_enable(pdev, num_vfs);
1764 		goto sriov_configure_out;
1765 	}
1766 
1767 	if (!pci_vfs_assigned(pf->pdev)) {
1768 		i40e_free_vfs(pf);
1769 		pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1770 		i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
1771 	} else {
1772 		dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1773 		ret = -EINVAL;
1774 		goto sriov_configure_out;
1775 	}
1776 sriov_configure_out:
1777 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
1778 	return ret;
1779 }
1780 
1781 /***********************virtual channel routines******************/
1782 
1783 /**
1784  * i40e_vc_send_msg_to_vf
1785  * @vf: pointer to the VF info
1786  * @v_opcode: virtual channel opcode
1787  * @v_retval: virtual channel return value
1788  * @msg: pointer to the msg buffer
1789  * @msglen: msg length
1790  *
1791  * send msg to VF
1792  **/
1793 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1794 				  u32 v_retval, u8 *msg, u16 msglen)
1795 {
1796 	struct i40e_pf *pf;
1797 	struct i40e_hw *hw;
1798 	int abs_vf_id;
1799 	i40e_status aq_ret;
1800 
1801 	/* validate the request */
1802 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1803 		return -EINVAL;
1804 
1805 	pf = vf->pf;
1806 	hw = &pf->hw;
1807 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1808 
1809 	/* single place to detect unsuccessful return values */
1810 	if (v_retval) {
1811 		vf->num_invalid_msgs++;
1812 		dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1813 			 vf->vf_id, v_opcode, v_retval);
1814 		if (vf->num_invalid_msgs >
1815 		    I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1816 			dev_err(&pf->pdev->dev,
1817 				"Number of invalid messages exceeded for VF %d\n",
1818 				vf->vf_id);
1819 			dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1820 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1821 		}
1822 	} else {
1823 		vf->num_valid_msgs++;
1824 		/* reset the invalid counter, if a valid message is received. */
1825 		vf->num_invalid_msgs = 0;
1826 	}
1827 
1828 	aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,	v_opcode, v_retval,
1829 					msg, msglen, NULL);
1830 	if (aq_ret) {
1831 		dev_info(&pf->pdev->dev,
1832 			 "Unable to send the message to VF %d aq_err %d\n",
1833 			 vf->vf_id, pf->hw.aq.asq_last_status);
1834 		return -EIO;
1835 	}
1836 
1837 	return 0;
1838 }
1839 
1840 /**
1841  * i40e_vc_send_resp_to_vf
1842  * @vf: pointer to the VF info
1843  * @opcode: operation code
1844  * @retval: return value
1845  *
1846  * send resp msg to VF
1847  **/
1848 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1849 				   enum virtchnl_ops opcode,
1850 				   i40e_status retval)
1851 {
1852 	return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1853 }
1854 
1855 /**
1856  * i40e_vc_get_version_msg
1857  * @vf: pointer to the VF info
1858  * @msg: pointer to the msg buffer
1859  *
1860  * called from the VF to request the API version used by the PF
1861  **/
1862 static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
1863 {
1864 	struct virtchnl_version_info info = {
1865 		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1866 	};
1867 
1868 	vf->vf_ver = *(struct virtchnl_version_info *)msg;
1869 	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1870 	if (VF_IS_V10(&vf->vf_ver))
1871 		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1872 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1873 				      I40E_SUCCESS, (u8 *)&info,
1874 				      sizeof(struct virtchnl_version_info));
1875 }
1876 
1877 /**
1878  * i40e_del_qch - delete all the additional VSIs created as a part of ADq
1879  * @vf: pointer to VF structure
1880  **/
1881 static void i40e_del_qch(struct i40e_vf *vf)
1882 {
1883 	struct i40e_pf *pf = vf->pf;
1884 	int i;
1885 
1886 	/* first element in the array belongs to primary VF VSI and we shouldn't
1887 	 * delete it. We should however delete the rest of the VSIs created
1888 	 */
1889 	for (i = 1; i < vf->num_tc; i++) {
1890 		if (vf->ch[i].vsi_idx) {
1891 			i40e_vsi_release(pf->vsi[vf->ch[i].vsi_idx]);
1892 			vf->ch[i].vsi_idx = 0;
1893 			vf->ch[i].vsi_id = 0;
1894 		}
1895 	}
1896 }
1897 
1898 /**
1899  * i40e_vc_get_vf_resources_msg
1900  * @vf: pointer to the VF info
1901  * @msg: pointer to the msg buffer
1902  *
1903  * called from the VF to request its resources
1904  **/
1905 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
1906 {
1907 	struct virtchnl_vf_resource *vfres = NULL;
1908 	struct i40e_pf *pf = vf->pf;
1909 	i40e_status aq_ret = 0;
1910 	struct i40e_vsi *vsi;
1911 	int num_vsis = 1;
1912 	size_t len = 0;
1913 	int ret;
1914 
1915 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
1916 		aq_ret = I40E_ERR_PARAM;
1917 		goto err;
1918 	}
1919 
1920 	len = struct_size(vfres, vsi_res, num_vsis);
1921 	vfres = kzalloc(len, GFP_KERNEL);
1922 	if (!vfres) {
1923 		aq_ret = I40E_ERR_NO_MEMORY;
1924 		len = 0;
1925 		goto err;
1926 	}
1927 	if (VF_IS_V11(&vf->vf_ver))
1928 		vf->driver_caps = *(u32 *)msg;
1929 	else
1930 		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1931 				  VIRTCHNL_VF_OFFLOAD_RSS_REG |
1932 				  VIRTCHNL_VF_OFFLOAD_VLAN;
1933 
1934 	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1935 	vsi = pf->vsi[vf->lan_vsi_idx];
1936 	if (!vsi->info.pvid)
1937 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1938 
1939 	if (i40e_vf_client_capable(pf, vf->vf_id) &&
1940 	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
1941 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
1942 		set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1943 	} else {
1944 		clear_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1945 	}
1946 
1947 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1948 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1949 	} else {
1950 		if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
1951 		    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
1952 			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1953 		else
1954 			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1955 	}
1956 
1957 	if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
1958 		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1959 			vfres->vf_cap_flags |=
1960 				VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1961 	}
1962 
1963 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1964 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1965 
1966 	if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
1967 	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
1968 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1969 
1970 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
1971 		if (pf->flags & I40E_FLAG_MFP_ENABLED) {
1972 			dev_err(&pf->pdev->dev,
1973 				"VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
1974 				 vf->vf_id);
1975 			aq_ret = I40E_ERR_PARAM;
1976 			goto err;
1977 		}
1978 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1979 	}
1980 
1981 	if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
1982 		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1983 			vfres->vf_cap_flags |=
1984 					VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1985 	}
1986 
1987 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
1988 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
1989 
1990 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)
1991 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADQ;
1992 
1993 	vfres->num_vsis = num_vsis;
1994 	vfres->num_queue_pairs = vf->num_queue_pairs;
1995 	vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1996 	vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
1997 	vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
1998 
1999 	if (vf->lan_vsi_idx) {
2000 		vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
2001 		vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
2002 		vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
2003 		/* VFs only use TC 0 */
2004 		vfres->vsi_res[0].qset_handle
2005 					  = le16_to_cpu(vsi->info.qs_handle[0]);
2006 		ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
2007 				vf->default_lan_addr.addr);
2008 	}
2009 	set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
2010 
2011 err:
2012 	/* send the response back to the VF */
2013 	ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
2014 				     aq_ret, (u8 *)vfres, len);
2015 
2016 	kfree(vfres);
2017 	return ret;
2018 }
2019 
2020 /**
2021  * i40e_vc_reset_vf_msg
2022  * @vf: pointer to the VF info
2023  *
2024  * called from the VF to reset itself,
2025  * unlike other virtchnl messages, PF driver
2026  * doesn't send the response back to the VF
2027  **/
2028 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
2029 {
2030 	if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
2031 		i40e_reset_vf(vf, false);
2032 }
2033 
2034 /**
2035  * i40e_vc_config_promiscuous_mode_msg
2036  * @vf: pointer to the VF info
2037  * @msg: pointer to the msg buffer
2038  *
2039  * called from the VF to configure the promiscuous mode of
2040  * VF vsis
2041  **/
2042 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf, u8 *msg)
2043 {
2044 	struct virtchnl_promisc_info *info =
2045 	    (struct virtchnl_promisc_info *)msg;
2046 	struct i40e_pf *pf = vf->pf;
2047 	i40e_status aq_ret = 0;
2048 	bool allmulti = false;
2049 	bool alluni = false;
2050 
2051 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2052 		aq_ret = I40E_ERR_PARAM;
2053 		goto err_out;
2054 	}
2055 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2056 		dev_err(&pf->pdev->dev,
2057 			"Unprivileged VF %d is attempting to configure promiscuous mode\n",
2058 			vf->vf_id);
2059 
2060 		/* Lie to the VF on purpose, because this is an error we can
2061 		 * ignore. Unprivileged VF is not a virtual channel error.
2062 		 */
2063 		aq_ret = 0;
2064 		goto err_out;
2065 	}
2066 
2067 	if (info->flags > I40E_MAX_VF_PROMISC_FLAGS) {
2068 		aq_ret = I40E_ERR_PARAM;
2069 		goto err_out;
2070 	}
2071 
2072 	if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
2073 		aq_ret = I40E_ERR_PARAM;
2074 		goto err_out;
2075 	}
2076 
2077 	/* Multicast promiscuous handling*/
2078 	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
2079 		allmulti = true;
2080 
2081 	if (info->flags & FLAG_VF_UNICAST_PROMISC)
2082 		alluni = true;
2083 	aq_ret = i40e_config_vf_promiscuous_mode(vf, info->vsi_id, allmulti,
2084 						 alluni);
2085 	if (aq_ret)
2086 		goto err_out;
2087 
2088 	if (allmulti) {
2089 		if (!test_and_set_bit(I40E_VF_STATE_MC_PROMISC,
2090 				      &vf->vf_states))
2091 			dev_info(&pf->pdev->dev,
2092 				 "VF %d successfully set multicast promiscuous mode\n",
2093 				 vf->vf_id);
2094 	} else if (test_and_clear_bit(I40E_VF_STATE_MC_PROMISC,
2095 				      &vf->vf_states))
2096 		dev_info(&pf->pdev->dev,
2097 			 "VF %d successfully unset multicast promiscuous mode\n",
2098 			 vf->vf_id);
2099 
2100 	if (alluni) {
2101 		if (!test_and_set_bit(I40E_VF_STATE_UC_PROMISC,
2102 				      &vf->vf_states))
2103 			dev_info(&pf->pdev->dev,
2104 				 "VF %d successfully set unicast promiscuous mode\n",
2105 				 vf->vf_id);
2106 	} else if (test_and_clear_bit(I40E_VF_STATE_UC_PROMISC,
2107 				      &vf->vf_states))
2108 		dev_info(&pf->pdev->dev,
2109 			 "VF %d successfully unset unicast promiscuous mode\n",
2110 			 vf->vf_id);
2111 
2112 err_out:
2113 	/* send the response to the VF */
2114 	return i40e_vc_send_resp_to_vf(vf,
2115 				       VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
2116 				       aq_ret);
2117 }
2118 
2119 /**
2120  * i40e_vc_config_queues_msg
2121  * @vf: pointer to the VF info
2122  * @msg: pointer to the msg buffer
2123  *
2124  * called from the VF to configure the rx/tx
2125  * queues
2126  **/
2127 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg)
2128 {
2129 	struct virtchnl_vsi_queue_config_info *qci =
2130 	    (struct virtchnl_vsi_queue_config_info *)msg;
2131 	struct virtchnl_queue_pair_info *qpi;
2132 	struct i40e_pf *pf = vf->pf;
2133 	u16 vsi_id, vsi_queue_id = 0;
2134 	u16 num_qps_all = 0;
2135 	i40e_status aq_ret = 0;
2136 	int i, j = 0, idx = 0;
2137 
2138 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2139 		aq_ret = I40E_ERR_PARAM;
2140 		goto error_param;
2141 	}
2142 
2143 	if (!i40e_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
2144 		aq_ret = I40E_ERR_PARAM;
2145 		goto error_param;
2146 	}
2147 
2148 	if (qci->num_queue_pairs > I40E_MAX_VF_QUEUES) {
2149 		aq_ret = I40E_ERR_PARAM;
2150 		goto error_param;
2151 	}
2152 
2153 	if (vf->adq_enabled) {
2154 		for (i = 0; i < I40E_MAX_VF_VSI; i++)
2155 			num_qps_all += vf->ch[i].num_qps;
2156 		if (num_qps_all != qci->num_queue_pairs) {
2157 			aq_ret = I40E_ERR_PARAM;
2158 			goto error_param;
2159 		}
2160 	}
2161 
2162 	vsi_id = qci->vsi_id;
2163 
2164 	for (i = 0; i < qci->num_queue_pairs; i++) {
2165 		qpi = &qci->qpair[i];
2166 
2167 		if (!vf->adq_enabled) {
2168 			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
2169 						      qpi->txq.queue_id)) {
2170 				aq_ret = I40E_ERR_PARAM;
2171 				goto error_param;
2172 			}
2173 
2174 			vsi_queue_id = qpi->txq.queue_id;
2175 
2176 			if (qpi->txq.vsi_id != qci->vsi_id ||
2177 			    qpi->rxq.vsi_id != qci->vsi_id ||
2178 			    qpi->rxq.queue_id != vsi_queue_id) {
2179 				aq_ret = I40E_ERR_PARAM;
2180 				goto error_param;
2181 			}
2182 		}
2183 
2184 		if (vf->adq_enabled) {
2185 			if (idx >= ARRAY_SIZE(vf->ch)) {
2186 				aq_ret = I40E_ERR_NO_AVAILABLE_VSI;
2187 				goto error_param;
2188 			}
2189 			vsi_id = vf->ch[idx].vsi_id;
2190 		}
2191 
2192 		if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
2193 					     &qpi->rxq) ||
2194 		    i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
2195 					     &qpi->txq)) {
2196 			aq_ret = I40E_ERR_PARAM;
2197 			goto error_param;
2198 		}
2199 
2200 		/* For ADq there can be up to 4 VSIs with max 4 queues each.
2201 		 * VF does not know about these additional VSIs and all
2202 		 * it cares is about its own queues. PF configures these queues
2203 		 * to its appropriate VSIs based on TC mapping
2204 		 */
2205 		if (vf->adq_enabled) {
2206 			if (idx >= ARRAY_SIZE(vf->ch)) {
2207 				aq_ret = I40E_ERR_NO_AVAILABLE_VSI;
2208 				goto error_param;
2209 			}
2210 			if (j == (vf->ch[idx].num_qps - 1)) {
2211 				idx++;
2212 				j = 0; /* resetting the queue count */
2213 				vsi_queue_id = 0;
2214 			} else {
2215 				j++;
2216 				vsi_queue_id++;
2217 			}
2218 		}
2219 	}
2220 	/* set vsi num_queue_pairs in use to num configured by VF */
2221 	if (!vf->adq_enabled) {
2222 		pf->vsi[vf->lan_vsi_idx]->num_queue_pairs =
2223 			qci->num_queue_pairs;
2224 	} else {
2225 		for (i = 0; i < vf->num_tc; i++)
2226 			pf->vsi[vf->ch[i].vsi_idx]->num_queue_pairs =
2227 			       vf->ch[i].num_qps;
2228 	}
2229 
2230 error_param:
2231 	/* send the response to the VF */
2232 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2233 				       aq_ret);
2234 }
2235 
2236 /**
2237  * i40e_validate_queue_map
2238  * @vsi_id: vsi id
2239  * @queuemap: Tx or Rx queue map
2240  *
2241  * check if Tx or Rx queue map is valid
2242  **/
2243 static int i40e_validate_queue_map(struct i40e_vf *vf, u16 vsi_id,
2244 				   unsigned long queuemap)
2245 {
2246 	u16 vsi_queue_id, queue_id;
2247 
2248 	for_each_set_bit(vsi_queue_id, &queuemap, I40E_MAX_VSI_QP) {
2249 		if (vf->adq_enabled) {
2250 			vsi_id = vf->ch[vsi_queue_id / I40E_MAX_VF_VSI].vsi_id;
2251 			queue_id = (vsi_queue_id % I40E_DEFAULT_QUEUES_PER_VF);
2252 		} else {
2253 			queue_id = vsi_queue_id;
2254 		}
2255 
2256 		if (!i40e_vc_isvalid_queue_id(vf, vsi_id, queue_id))
2257 			return -EINVAL;
2258 	}
2259 
2260 	return 0;
2261 }
2262 
2263 /**
2264  * i40e_vc_config_irq_map_msg
2265  * @vf: pointer to the VF info
2266  * @msg: pointer to the msg buffer
2267  *
2268  * called from the VF to configure the irq to
2269  * queue map
2270  **/
2271 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg)
2272 {
2273 	struct virtchnl_irq_map_info *irqmap_info =
2274 	    (struct virtchnl_irq_map_info *)msg;
2275 	struct virtchnl_vector_map *map;
2276 	u16 vsi_id;
2277 	i40e_status aq_ret = 0;
2278 	int i;
2279 
2280 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2281 		aq_ret = I40E_ERR_PARAM;
2282 		goto error_param;
2283 	}
2284 
2285 	if (irqmap_info->num_vectors >
2286 	    vf->pf->hw.func_caps.num_msix_vectors_vf) {
2287 		aq_ret = I40E_ERR_PARAM;
2288 		goto error_param;
2289 	}
2290 
2291 	for (i = 0; i < irqmap_info->num_vectors; i++) {
2292 		map = &irqmap_info->vecmap[i];
2293 		/* validate msg params */
2294 		if (!i40e_vc_isvalid_vector_id(vf, map->vector_id) ||
2295 		    !i40e_vc_isvalid_vsi_id(vf, map->vsi_id)) {
2296 			aq_ret = I40E_ERR_PARAM;
2297 			goto error_param;
2298 		}
2299 		vsi_id = map->vsi_id;
2300 
2301 		if (i40e_validate_queue_map(vf, vsi_id, map->rxq_map)) {
2302 			aq_ret = I40E_ERR_PARAM;
2303 			goto error_param;
2304 		}
2305 
2306 		if (i40e_validate_queue_map(vf, vsi_id, map->txq_map)) {
2307 			aq_ret = I40E_ERR_PARAM;
2308 			goto error_param;
2309 		}
2310 
2311 		i40e_config_irq_link_list(vf, vsi_id, map);
2312 	}
2313 error_param:
2314 	/* send the response to the VF */
2315 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
2316 				       aq_ret);
2317 }
2318 
2319 /**
2320  * i40e_ctrl_vf_tx_rings
2321  * @vsi: the SRIOV VSI being configured
2322  * @q_map: bit map of the queues to be enabled
2323  * @enable: start or stop the queue
2324  **/
2325 static int i40e_ctrl_vf_tx_rings(struct i40e_vsi *vsi, unsigned long q_map,
2326 				 bool enable)
2327 {
2328 	struct i40e_pf *pf = vsi->back;
2329 	int ret = 0;
2330 	u16 q_id;
2331 
2332 	for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
2333 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
2334 					     vsi->base_queue + q_id,
2335 					     false /*is xdp*/, enable);
2336 		if (ret)
2337 			break;
2338 	}
2339 	return ret;
2340 }
2341 
2342 /**
2343  * i40e_ctrl_vf_rx_rings
2344  * @vsi: the SRIOV VSI being configured
2345  * @q_map: bit map of the queues to be enabled
2346  * @enable: start or stop the queue
2347  **/
2348 static int i40e_ctrl_vf_rx_rings(struct i40e_vsi *vsi, unsigned long q_map,
2349 				 bool enable)
2350 {
2351 	struct i40e_pf *pf = vsi->back;
2352 	int ret = 0;
2353 	u16 q_id;
2354 
2355 	for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
2356 		ret = i40e_control_wait_rx_q(pf, vsi->base_queue + q_id,
2357 					     enable);
2358 		if (ret)
2359 			break;
2360 	}
2361 	return ret;
2362 }
2363 
2364 /**
2365  * i40e_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTHCHNL
2366  * @vqs: virtchnl_queue_select structure containing bitmaps to validate
2367  *
2368  * Returns true if validation was successful, else false.
2369  */
2370 static bool i40e_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
2371 {
2372 	if ((!vqs->rx_queues && !vqs->tx_queues) ||
2373 	    vqs->rx_queues >= BIT(I40E_MAX_VF_QUEUES) ||
2374 	    vqs->tx_queues >= BIT(I40E_MAX_VF_QUEUES))
2375 		return false;
2376 
2377 	return true;
2378 }
2379 
2380 /**
2381  * i40e_vc_enable_queues_msg
2382  * @vf: pointer to the VF info
2383  * @msg: pointer to the msg buffer
2384  *
2385  * called from the VF to enable all or specific queue(s)
2386  **/
2387 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg)
2388 {
2389 	struct virtchnl_queue_select *vqs =
2390 	    (struct virtchnl_queue_select *)msg;
2391 	struct i40e_pf *pf = vf->pf;
2392 	i40e_status aq_ret = 0;
2393 	int i;
2394 
2395 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2396 		aq_ret = I40E_ERR_PARAM;
2397 		goto error_param;
2398 	}
2399 
2400 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2401 		aq_ret = I40E_ERR_PARAM;
2402 		goto error_param;
2403 	}
2404 
2405 	if (!i40e_vc_validate_vqs_bitmaps(vqs)) {
2406 		aq_ret = I40E_ERR_PARAM;
2407 		goto error_param;
2408 	}
2409 
2410 	/* Use the queue bit map sent by the VF */
2411 	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2412 				  true)) {
2413 		aq_ret = I40E_ERR_TIMEOUT;
2414 		goto error_param;
2415 	}
2416 	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2417 				  true)) {
2418 		aq_ret = I40E_ERR_TIMEOUT;
2419 		goto error_param;
2420 	}
2421 
2422 	/* need to start the rings for additional ADq VSI's as well */
2423 	if (vf->adq_enabled) {
2424 		/* zero belongs to LAN VSI */
2425 		for (i = 1; i < vf->num_tc; i++) {
2426 			if (i40e_vsi_start_rings(pf->vsi[vf->ch[i].vsi_idx]))
2427 				aq_ret = I40E_ERR_TIMEOUT;
2428 		}
2429 	}
2430 
2431 	vf->queues_enabled = true;
2432 
2433 error_param:
2434 	/* send the response to the VF */
2435 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
2436 				       aq_ret);
2437 }
2438 
2439 /**
2440  * i40e_vc_disable_queues_msg
2441  * @vf: pointer to the VF info
2442  * @msg: pointer to the msg buffer
2443  *
2444  * called from the VF to disable all or specific
2445  * queue(s)
2446  **/
2447 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg)
2448 {
2449 	struct virtchnl_queue_select *vqs =
2450 	    (struct virtchnl_queue_select *)msg;
2451 	struct i40e_pf *pf = vf->pf;
2452 	i40e_status aq_ret = 0;
2453 
2454 	/* Immediately mark queues as disabled */
2455 	vf->queues_enabled = false;
2456 
2457 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2458 		aq_ret = I40E_ERR_PARAM;
2459 		goto error_param;
2460 	}
2461 
2462 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2463 		aq_ret = I40E_ERR_PARAM;
2464 		goto error_param;
2465 	}
2466 
2467 	if (!i40e_vc_validate_vqs_bitmaps(vqs)) {
2468 		aq_ret = I40E_ERR_PARAM;
2469 		goto error_param;
2470 	}
2471 
2472 	/* Use the queue bit map sent by the VF */
2473 	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2474 				  false)) {
2475 		aq_ret = I40E_ERR_TIMEOUT;
2476 		goto error_param;
2477 	}
2478 	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2479 				  false)) {
2480 		aq_ret = I40E_ERR_TIMEOUT;
2481 		goto error_param;
2482 	}
2483 error_param:
2484 	/* send the response to the VF */
2485 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
2486 				       aq_ret);
2487 }
2488 
2489 /**
2490  * i40e_vc_request_queues_msg
2491  * @vf: pointer to the VF info
2492  * @msg: pointer to the msg buffer
2493  *
2494  * VFs get a default number of queues but can use this message to request a
2495  * different number.  If the request is successful, PF will reset the VF and
2496  * return 0.  If unsuccessful, PF will send message informing VF of number of
2497  * available queues and return result of sending VF a message.
2498  **/
2499 static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg)
2500 {
2501 	struct virtchnl_vf_res_request *vfres =
2502 		(struct virtchnl_vf_res_request *)msg;
2503 	u16 req_pairs = vfres->num_queue_pairs;
2504 	u8 cur_pairs = vf->num_queue_pairs;
2505 	struct i40e_pf *pf = vf->pf;
2506 
2507 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
2508 		return -EINVAL;
2509 
2510 	if (req_pairs > I40E_MAX_VF_QUEUES) {
2511 		dev_err(&pf->pdev->dev,
2512 			"VF %d tried to request more than %d queues.\n",
2513 			vf->vf_id,
2514 			I40E_MAX_VF_QUEUES);
2515 		vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
2516 	} else if (req_pairs - cur_pairs > pf->queues_left) {
2517 		dev_warn(&pf->pdev->dev,
2518 			 "VF %d requested %d more queues, but only %d left.\n",
2519 			 vf->vf_id,
2520 			 req_pairs - cur_pairs,
2521 			 pf->queues_left);
2522 		vfres->num_queue_pairs = pf->queues_left + cur_pairs;
2523 	} else {
2524 		/* successful request */
2525 		vf->num_req_queues = req_pairs;
2526 		i40e_vc_notify_vf_reset(vf);
2527 		i40e_reset_vf(vf, false);
2528 		return 0;
2529 	}
2530 
2531 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
2532 				      (u8 *)vfres, sizeof(*vfres));
2533 }
2534 
2535 /**
2536  * i40e_vc_get_stats_msg
2537  * @vf: pointer to the VF info
2538  * @msg: pointer to the msg buffer
2539  *
2540  * called from the VF to get vsi stats
2541  **/
2542 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg)
2543 {
2544 	struct virtchnl_queue_select *vqs =
2545 	    (struct virtchnl_queue_select *)msg;
2546 	struct i40e_pf *pf = vf->pf;
2547 	struct i40e_eth_stats stats;
2548 	i40e_status aq_ret = 0;
2549 	struct i40e_vsi *vsi;
2550 
2551 	memset(&stats, 0, sizeof(struct i40e_eth_stats));
2552 
2553 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2554 		aq_ret = I40E_ERR_PARAM;
2555 		goto error_param;
2556 	}
2557 
2558 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2559 		aq_ret = I40E_ERR_PARAM;
2560 		goto error_param;
2561 	}
2562 
2563 	vsi = pf->vsi[vf->lan_vsi_idx];
2564 	if (!vsi) {
2565 		aq_ret = I40E_ERR_PARAM;
2566 		goto error_param;
2567 	}
2568 	i40e_update_eth_stats(vsi);
2569 	stats = vsi->eth_stats;
2570 
2571 error_param:
2572 	/* send the response back to the VF */
2573 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
2574 				      (u8 *)&stats, sizeof(stats));
2575 }
2576 
2577 /* If the VF is not trusted restrict the number of MAC/VLAN it can program
2578  * MAC filters: 16 for multicast, 1 for MAC, 1 for broadcast
2579  */
2580 #define I40E_VC_MAX_MAC_ADDR_PER_VF (16 + 1 + 1)
2581 #define I40E_VC_MAX_VLAN_PER_VF 16
2582 
2583 /**
2584  * i40e_check_vf_permission
2585  * @vf: pointer to the VF info
2586  * @al: MAC address list from virtchnl
2587  *
2588  * Check that the given list of MAC addresses is allowed. Will return -EPERM
2589  * if any address in the list is not valid. Checks the following conditions:
2590  *
2591  * 1) broadcast and zero addresses are never valid
2592  * 2) unicast addresses are not allowed if the VMM has administratively set
2593  *    the VF MAC address, unless the VF is marked as privileged.
2594  * 3) There is enough space to add all the addresses.
2595  *
2596  * Note that to guarantee consistency, it is expected this function be called
2597  * while holding the mac_filter_hash_lock, as otherwise the current number of
2598  * addresses might not be accurate.
2599  **/
2600 static inline int i40e_check_vf_permission(struct i40e_vf *vf,
2601 					   struct virtchnl_ether_addr_list *al)
2602 {
2603 	struct i40e_pf *pf = vf->pf;
2604 	struct i40e_vsi *vsi = pf->vsi[vf->lan_vsi_idx];
2605 	int mac2add_cnt = 0;
2606 	int i;
2607 
2608 	for (i = 0; i < al->num_elements; i++) {
2609 		struct i40e_mac_filter *f;
2610 		u8 *addr = al->list[i].addr;
2611 
2612 		if (is_broadcast_ether_addr(addr) ||
2613 		    is_zero_ether_addr(addr)) {
2614 			dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
2615 				addr);
2616 			return I40E_ERR_INVALID_MAC_ADDR;
2617 		}
2618 
2619 		/* If the host VMM administrator has set the VF MAC address
2620 		 * administratively via the ndo_set_vf_mac command then deny
2621 		 * permission to the VF to add or delete unicast MAC addresses.
2622 		 * Unless the VF is privileged and then it can do whatever.
2623 		 * The VF may request to set the MAC address filter already
2624 		 * assigned to it so do not return an error in that case.
2625 		 */
2626 		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2627 		    !is_multicast_ether_addr(addr) && vf->pf_set_mac &&
2628 		    !ether_addr_equal(addr, vf->default_lan_addr.addr)) {
2629 			dev_err(&pf->pdev->dev,
2630 				"VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
2631 			return -EPERM;
2632 		}
2633 
2634 		/*count filters that really will be added*/
2635 		f = i40e_find_mac(vsi, addr);
2636 		if (!f)
2637 			++mac2add_cnt;
2638 	}
2639 
2640 	/* If this VF is not privileged, then we can't add more than a limited
2641 	 * number of addresses. Check to make sure that the additions do not
2642 	 * push us over the limit.
2643 	 */
2644 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2645 	    (i40e_count_filters(vsi) + mac2add_cnt) >
2646 		    I40E_VC_MAX_MAC_ADDR_PER_VF) {
2647 		dev_err(&pf->pdev->dev,
2648 			"Cannot add more MAC addresses, VF is not trusted, switch the VF to trusted to add more functionality\n");
2649 		return -EPERM;
2650 	}
2651 	return 0;
2652 }
2653 
2654 /**
2655  * i40e_vc_add_mac_addr_msg
2656  * @vf: pointer to the VF info
2657  * @msg: pointer to the msg buffer
2658  *
2659  * add guest mac address filter
2660  **/
2661 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
2662 {
2663 	struct virtchnl_ether_addr_list *al =
2664 	    (struct virtchnl_ether_addr_list *)msg;
2665 	struct i40e_pf *pf = vf->pf;
2666 	struct i40e_vsi *vsi = NULL;
2667 	i40e_status ret = 0;
2668 	int i;
2669 
2670 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2671 	    !i40e_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2672 		ret = I40E_ERR_PARAM;
2673 		goto error_param;
2674 	}
2675 
2676 	vsi = pf->vsi[vf->lan_vsi_idx];
2677 
2678 	/* Lock once, because all function inside for loop accesses VSI's
2679 	 * MAC filter list which needs to be protected using same lock.
2680 	 */
2681 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2682 
2683 	ret = i40e_check_vf_permission(vf, al);
2684 	if (ret) {
2685 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2686 		goto error_param;
2687 	}
2688 
2689 	/* add new addresses to the list */
2690 	for (i = 0; i < al->num_elements; i++) {
2691 		struct i40e_mac_filter *f;
2692 
2693 		f = i40e_find_mac(vsi, al->list[i].addr);
2694 		if (!f) {
2695 			f = i40e_add_mac_filter(vsi, al->list[i].addr);
2696 
2697 			if (!f) {
2698 				dev_err(&pf->pdev->dev,
2699 					"Unable to add MAC filter %pM for VF %d\n",
2700 					al->list[i].addr, vf->vf_id);
2701 				ret = I40E_ERR_PARAM;
2702 				spin_unlock_bh(&vsi->mac_filter_hash_lock);
2703 				goto error_param;
2704 			}
2705 		}
2706 	}
2707 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2708 
2709 	/* program the updated filter list */
2710 	ret = i40e_sync_vsi_filters(vsi);
2711 	if (ret)
2712 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2713 			vf->vf_id, ret);
2714 
2715 error_param:
2716 	/* send the response to the VF */
2717 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
2718 				       ret);
2719 }
2720 
2721 /**
2722  * i40e_vc_del_mac_addr_msg
2723  * @vf: pointer to the VF info
2724  * @msg: pointer to the msg buffer
2725  *
2726  * remove guest mac address filter
2727  **/
2728 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
2729 {
2730 	struct virtchnl_ether_addr_list *al =
2731 	    (struct virtchnl_ether_addr_list *)msg;
2732 	struct i40e_pf *pf = vf->pf;
2733 	struct i40e_vsi *vsi = NULL;
2734 	i40e_status ret = 0;
2735 	int i;
2736 
2737 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2738 	    !i40e_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2739 		ret = I40E_ERR_PARAM;
2740 		goto error_param;
2741 	}
2742 
2743 	for (i = 0; i < al->num_elements; i++) {
2744 		if (is_broadcast_ether_addr(al->list[i].addr) ||
2745 		    is_zero_ether_addr(al->list[i].addr)) {
2746 			dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
2747 				al->list[i].addr, vf->vf_id);
2748 			ret = I40E_ERR_INVALID_MAC_ADDR;
2749 			goto error_param;
2750 		}
2751 	}
2752 	vsi = pf->vsi[vf->lan_vsi_idx];
2753 
2754 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2755 	/* delete addresses from the list */
2756 	for (i = 0; i < al->num_elements; i++)
2757 		if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
2758 			ret = I40E_ERR_INVALID_MAC_ADDR;
2759 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
2760 			goto error_param;
2761 		}
2762 
2763 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2764 
2765 	/* program the updated filter list */
2766 	ret = i40e_sync_vsi_filters(vsi);
2767 	if (ret)
2768 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2769 			vf->vf_id, ret);
2770 
2771 error_param:
2772 	/* send the response to the VF */
2773 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
2774 				       ret);
2775 }
2776 
2777 /**
2778  * i40e_vc_add_vlan_msg
2779  * @vf: pointer to the VF info
2780  * @msg: pointer to the msg buffer
2781  *
2782  * program guest vlan id
2783  **/
2784 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg)
2785 {
2786 	struct virtchnl_vlan_filter_list *vfl =
2787 	    (struct virtchnl_vlan_filter_list *)msg;
2788 	struct i40e_pf *pf = vf->pf;
2789 	struct i40e_vsi *vsi = NULL;
2790 	i40e_status aq_ret = 0;
2791 	int i;
2792 
2793 	if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
2794 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2795 		dev_err(&pf->pdev->dev,
2796 			"VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
2797 		goto error_param;
2798 	}
2799 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2800 	    !i40e_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2801 		aq_ret = I40E_ERR_PARAM;
2802 		goto error_param;
2803 	}
2804 
2805 	for (i = 0; i < vfl->num_elements; i++) {
2806 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2807 			aq_ret = I40E_ERR_PARAM;
2808 			dev_err(&pf->pdev->dev,
2809 				"invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2810 			goto error_param;
2811 		}
2812 	}
2813 	vsi = pf->vsi[vf->lan_vsi_idx];
2814 	if (vsi->info.pvid) {
2815 		aq_ret = I40E_ERR_PARAM;
2816 		goto error_param;
2817 	}
2818 
2819 	i40e_vlan_stripping_enable(vsi);
2820 	for (i = 0; i < vfl->num_elements; i++) {
2821 		/* add new VLAN filter */
2822 		int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
2823 		if (!ret)
2824 			vf->num_vlan++;
2825 
2826 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2827 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2828 							   true,
2829 							   vfl->vlan_id[i],
2830 							   NULL);
2831 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2832 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2833 							   true,
2834 							   vfl->vlan_id[i],
2835 							   NULL);
2836 
2837 		if (ret)
2838 			dev_err(&pf->pdev->dev,
2839 				"Unable to add VLAN filter %d for VF %d, error %d\n",
2840 				vfl->vlan_id[i], vf->vf_id, ret);
2841 	}
2842 
2843 error_param:
2844 	/* send the response to the VF */
2845 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
2846 }
2847 
2848 /**
2849  * i40e_vc_remove_vlan_msg
2850  * @vf: pointer to the VF info
2851  * @msg: pointer to the msg buffer
2852  *
2853  * remove programmed guest vlan id
2854  **/
2855 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg)
2856 {
2857 	struct virtchnl_vlan_filter_list *vfl =
2858 	    (struct virtchnl_vlan_filter_list *)msg;
2859 	struct i40e_pf *pf = vf->pf;
2860 	struct i40e_vsi *vsi = NULL;
2861 	i40e_status aq_ret = 0;
2862 	int i;
2863 
2864 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2865 	    !i40e_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2866 		aq_ret = I40E_ERR_PARAM;
2867 		goto error_param;
2868 	}
2869 
2870 	for (i = 0; i < vfl->num_elements; i++) {
2871 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2872 			aq_ret = I40E_ERR_PARAM;
2873 			goto error_param;
2874 		}
2875 	}
2876 
2877 	vsi = pf->vsi[vf->lan_vsi_idx];
2878 	if (vsi->info.pvid) {
2879 		if (vfl->num_elements > 1 || vfl->vlan_id[0])
2880 			aq_ret = I40E_ERR_PARAM;
2881 		goto error_param;
2882 	}
2883 
2884 	for (i = 0; i < vfl->num_elements; i++) {
2885 		i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
2886 		vf->num_vlan--;
2887 
2888 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2889 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2890 							   false,
2891 							   vfl->vlan_id[i],
2892 							   NULL);
2893 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2894 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2895 							   false,
2896 							   vfl->vlan_id[i],
2897 							   NULL);
2898 	}
2899 
2900 error_param:
2901 	/* send the response to the VF */
2902 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
2903 }
2904 
2905 /**
2906  * i40e_vc_iwarp_msg
2907  * @vf: pointer to the VF info
2908  * @msg: pointer to the msg buffer
2909  * @msglen: msg length
2910  *
2911  * called from the VF for the iwarp msgs
2912  **/
2913 static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2914 {
2915 	struct i40e_pf *pf = vf->pf;
2916 	int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
2917 	i40e_status aq_ret = 0;
2918 
2919 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2920 	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2921 		aq_ret = I40E_ERR_PARAM;
2922 		goto error_param;
2923 	}
2924 
2925 	i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
2926 				     msg, msglen);
2927 
2928 error_param:
2929 	/* send the response to the VF */
2930 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
2931 				       aq_ret);
2932 }
2933 
2934 /**
2935  * i40e_vc_iwarp_qvmap_msg
2936  * @vf: pointer to the VF info
2937  * @msg: pointer to the msg buffer
2938  * @config: config qvmap or release it
2939  *
2940  * called from the VF for the iwarp msgs
2941  **/
2942 static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, bool config)
2943 {
2944 	struct virtchnl_iwarp_qvlist_info *qvlist_info =
2945 				(struct virtchnl_iwarp_qvlist_info *)msg;
2946 	i40e_status aq_ret = 0;
2947 
2948 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2949 	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2950 		aq_ret = I40E_ERR_PARAM;
2951 		goto error_param;
2952 	}
2953 
2954 	if (config) {
2955 		if (i40e_config_iwarp_qvlist(vf, qvlist_info))
2956 			aq_ret = I40E_ERR_PARAM;
2957 	} else {
2958 		i40e_release_iwarp_qvlist(vf);
2959 	}
2960 
2961 error_param:
2962 	/* send the response to the VF */
2963 	return i40e_vc_send_resp_to_vf(vf,
2964 			       config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
2965 			       VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
2966 			       aq_ret);
2967 }
2968 
2969 /**
2970  * i40e_vc_config_rss_key
2971  * @vf: pointer to the VF info
2972  * @msg: pointer to the msg buffer
2973  *
2974  * Configure the VF's RSS key
2975  **/
2976 static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg)
2977 {
2978 	struct virtchnl_rss_key *vrk =
2979 		(struct virtchnl_rss_key *)msg;
2980 	struct i40e_pf *pf = vf->pf;
2981 	struct i40e_vsi *vsi = NULL;
2982 	i40e_status aq_ret = 0;
2983 
2984 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2985 	    !i40e_vc_isvalid_vsi_id(vf, vrk->vsi_id) ||
2986 	    (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
2987 		aq_ret = I40E_ERR_PARAM;
2988 		goto err;
2989 	}
2990 
2991 	vsi = pf->vsi[vf->lan_vsi_idx];
2992 	aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
2993 err:
2994 	/* send the response to the VF */
2995 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
2996 				       aq_ret);
2997 }
2998 
2999 /**
3000  * i40e_vc_config_rss_lut
3001  * @vf: pointer to the VF info
3002  * @msg: pointer to the msg buffer
3003  *
3004  * Configure the VF's RSS LUT
3005  **/
3006 static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg)
3007 {
3008 	struct virtchnl_rss_lut *vrl =
3009 		(struct virtchnl_rss_lut *)msg;
3010 	struct i40e_pf *pf = vf->pf;
3011 	struct i40e_vsi *vsi = NULL;
3012 	i40e_status aq_ret = 0;
3013 	u16 i;
3014 
3015 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
3016 	    !i40e_vc_isvalid_vsi_id(vf, vrl->vsi_id) ||
3017 	    (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
3018 		aq_ret = I40E_ERR_PARAM;
3019 		goto err;
3020 	}
3021 
3022 	for (i = 0; i < vrl->lut_entries; i++)
3023 		if (vrl->lut[i] >= vf->num_queue_pairs) {
3024 			aq_ret = I40E_ERR_PARAM;
3025 			goto err;
3026 		}
3027 
3028 	vsi = pf->vsi[vf->lan_vsi_idx];
3029 	aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
3030 	/* send the response to the VF */
3031 err:
3032 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
3033 				       aq_ret);
3034 }
3035 
3036 /**
3037  * i40e_vc_get_rss_hena
3038  * @vf: pointer to the VF info
3039  * @msg: pointer to the msg buffer
3040  *
3041  * Return the RSS HENA bits allowed by the hardware
3042  **/
3043 static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg)
3044 {
3045 	struct virtchnl_rss_hena *vrh = NULL;
3046 	struct i40e_pf *pf = vf->pf;
3047 	i40e_status aq_ret = 0;
3048 	int len = 0;
3049 
3050 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3051 		aq_ret = I40E_ERR_PARAM;
3052 		goto err;
3053 	}
3054 	len = sizeof(struct virtchnl_rss_hena);
3055 
3056 	vrh = kzalloc(len, GFP_KERNEL);
3057 	if (!vrh) {
3058 		aq_ret = I40E_ERR_NO_MEMORY;
3059 		len = 0;
3060 		goto err;
3061 	}
3062 	vrh->hena = i40e_pf_get_default_rss_hena(pf);
3063 err:
3064 	/* send the response back to the VF */
3065 	aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
3066 					aq_ret, (u8 *)vrh, len);
3067 	kfree(vrh);
3068 	return aq_ret;
3069 }
3070 
3071 /**
3072  * i40e_vc_set_rss_hena
3073  * @vf: pointer to the VF info
3074  * @msg: pointer to the msg buffer
3075  *
3076  * Set the RSS HENA bits for the VF
3077  **/
3078 static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg)
3079 {
3080 	struct virtchnl_rss_hena *vrh =
3081 		(struct virtchnl_rss_hena *)msg;
3082 	struct i40e_pf *pf = vf->pf;
3083 	struct i40e_hw *hw = &pf->hw;
3084 	i40e_status aq_ret = 0;
3085 
3086 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3087 		aq_ret = I40E_ERR_PARAM;
3088 		goto err;
3089 	}
3090 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
3091 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
3092 			  (u32)(vrh->hena >> 32));
3093 
3094 	/* send the response to the VF */
3095 err:
3096 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
3097 }
3098 
3099 /**
3100  * i40e_vc_enable_vlan_stripping
3101  * @vf: pointer to the VF info
3102  * @msg: pointer to the msg buffer
3103  *
3104  * Enable vlan header stripping for the VF
3105  **/
3106 static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3107 {
3108 	i40e_status aq_ret = 0;
3109 	struct i40e_vsi *vsi;
3110 
3111 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3112 		aq_ret = I40E_ERR_PARAM;
3113 		goto err;
3114 	}
3115 
3116 	vsi = vf->pf->vsi[vf->lan_vsi_idx];
3117 	i40e_vlan_stripping_enable(vsi);
3118 
3119 	/* send the response to the VF */
3120 err:
3121 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
3122 				       aq_ret);
3123 }
3124 
3125 /**
3126  * i40e_vc_disable_vlan_stripping
3127  * @vf: pointer to the VF info
3128  * @msg: pointer to the msg buffer
3129  *
3130  * Disable vlan header stripping for the VF
3131  **/
3132 static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3133 {
3134 	i40e_status aq_ret = 0;
3135 	struct i40e_vsi *vsi;
3136 
3137 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3138 		aq_ret = I40E_ERR_PARAM;
3139 		goto err;
3140 	}
3141 
3142 	vsi = vf->pf->vsi[vf->lan_vsi_idx];
3143 	i40e_vlan_stripping_disable(vsi);
3144 
3145 	/* send the response to the VF */
3146 err:
3147 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
3148 				       aq_ret);
3149 }
3150 
3151 /**
3152  * i40e_validate_cloud_filter
3153  * @mask: mask for TC filter
3154  * @data: data for TC filter
3155  *
3156  * This function validates cloud filter programmed as TC filter for ADq
3157  **/
3158 static int i40e_validate_cloud_filter(struct i40e_vf *vf,
3159 				      struct virtchnl_filter *tc_filter)
3160 {
3161 	struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;
3162 	struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;
3163 	struct i40e_pf *pf = vf->pf;
3164 	struct i40e_vsi *vsi = NULL;
3165 	struct i40e_mac_filter *f;
3166 	struct hlist_node *h;
3167 	bool found = false;
3168 	int bkt;
3169 
3170 	if (!tc_filter->action) {
3171 		dev_info(&pf->pdev->dev,
3172 			 "VF %d: Currently ADq doesn't support Drop Action\n",
3173 			 vf->vf_id);
3174 		goto err;
3175 	}
3176 
3177 	/* action_meta is TC number here to which the filter is applied */
3178 	if (!tc_filter->action_meta ||
3179 	    tc_filter->action_meta > I40E_MAX_VF_VSI) {
3180 		dev_info(&pf->pdev->dev, "VF %d: Invalid TC number %u\n",
3181 			 vf->vf_id, tc_filter->action_meta);
3182 		goto err;
3183 	}
3184 
3185 	/* Check filter if it's programmed for advanced mode or basic mode.
3186 	 * There are two ADq modes (for VF only),
3187 	 * 1. Basic mode: intended to allow as many filter options as possible
3188 	 *		  to be added to a VF in Non-trusted mode. Main goal is
3189 	 *		  to add filters to its own MAC and VLAN id.
3190 	 * 2. Advanced mode: is for allowing filters to be applied other than
3191 	 *		  its own MAC or VLAN. This mode requires the VF to be
3192 	 *		  Trusted.
3193 	 */
3194 	if (mask.dst_mac[0] && !mask.dst_ip[0]) {
3195 		vsi = pf->vsi[vf->lan_vsi_idx];
3196 		f = i40e_find_mac(vsi, data.dst_mac);
3197 
3198 		if (!f) {
3199 			dev_info(&pf->pdev->dev,
3200 				 "Destination MAC %pM doesn't belong to VF %d\n",
3201 				 data.dst_mac, vf->vf_id);
3202 			goto err;
3203 		}
3204 
3205 		if (mask.vlan_id) {
3206 			hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
3207 					   hlist) {
3208 				if (f->vlan == ntohs(data.vlan_id)) {
3209 					found = true;
3210 					break;
3211 				}
3212 			}
3213 			if (!found) {
3214 				dev_info(&pf->pdev->dev,
3215 					 "VF %d doesn't have any VLAN id %u\n",
3216 					 vf->vf_id, ntohs(data.vlan_id));
3217 				goto err;
3218 			}
3219 		}
3220 	} else {
3221 		/* Check if VF is trusted */
3222 		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3223 			dev_err(&pf->pdev->dev,
3224 				"VF %d not trusted, make VF trusted to add advanced mode ADq cloud filters\n",
3225 				vf->vf_id);
3226 			return I40E_ERR_CONFIG;
3227 		}
3228 	}
3229 
3230 	if (mask.dst_mac[0] & data.dst_mac[0]) {
3231 		if (is_broadcast_ether_addr(data.dst_mac) ||
3232 		    is_zero_ether_addr(data.dst_mac)) {
3233 			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest MAC addr %pM\n",
3234 				 vf->vf_id, data.dst_mac);
3235 			goto err;
3236 		}
3237 	}
3238 
3239 	if (mask.src_mac[0] & data.src_mac[0]) {
3240 		if (is_broadcast_ether_addr(data.src_mac) ||
3241 		    is_zero_ether_addr(data.src_mac)) {
3242 			dev_info(&pf->pdev->dev, "VF %d: Invalid Source MAC addr %pM\n",
3243 				 vf->vf_id, data.src_mac);
3244 			goto err;
3245 		}
3246 	}
3247 
3248 	if (mask.dst_port & data.dst_port) {
3249 		if (!data.dst_port) {
3250 			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest port\n",
3251 				 vf->vf_id);
3252 			goto err;
3253 		}
3254 	}
3255 
3256 	if (mask.src_port & data.src_port) {
3257 		if (!data.src_port) {
3258 			dev_info(&pf->pdev->dev, "VF %d: Invalid Source port\n",
3259 				 vf->vf_id);
3260 			goto err;
3261 		}
3262 	}
3263 
3264 	if (tc_filter->flow_type != VIRTCHNL_TCP_V6_FLOW &&
3265 	    tc_filter->flow_type != VIRTCHNL_TCP_V4_FLOW) {
3266 		dev_info(&pf->pdev->dev, "VF %d: Invalid Flow type\n",
3267 			 vf->vf_id);
3268 		goto err;
3269 	}
3270 
3271 	if (mask.vlan_id & data.vlan_id) {
3272 		if (ntohs(data.vlan_id) > I40E_MAX_VLANID) {
3273 			dev_info(&pf->pdev->dev, "VF %d: invalid VLAN ID\n",
3274 				 vf->vf_id);
3275 			goto err;
3276 		}
3277 	}
3278 
3279 	return I40E_SUCCESS;
3280 err:
3281 	return I40E_ERR_CONFIG;
3282 }
3283 
3284 /**
3285  * i40e_find_vsi_from_seid - searches for the vsi with the given seid
3286  * @vf: pointer to the VF info
3287  * @seid - seid of the vsi it is searching for
3288  **/
3289 static struct i40e_vsi *i40e_find_vsi_from_seid(struct i40e_vf *vf, u16 seid)
3290 {
3291 	struct i40e_pf *pf = vf->pf;
3292 	struct i40e_vsi *vsi = NULL;
3293 	int i;
3294 
3295 	for (i = 0; i < vf->num_tc ; i++) {
3296 		vsi = i40e_find_vsi_from_id(pf, vf->ch[i].vsi_id);
3297 		if (vsi && vsi->seid == seid)
3298 			return vsi;
3299 	}
3300 	return NULL;
3301 }
3302 
3303 /**
3304  * i40e_del_all_cloud_filters
3305  * @vf: pointer to the VF info
3306  *
3307  * This function deletes all cloud filters
3308  **/
3309 static void i40e_del_all_cloud_filters(struct i40e_vf *vf)
3310 {
3311 	struct i40e_cloud_filter *cfilter = NULL;
3312 	struct i40e_pf *pf = vf->pf;
3313 	struct i40e_vsi *vsi = NULL;
3314 	struct hlist_node *node;
3315 	int ret;
3316 
3317 	hlist_for_each_entry_safe(cfilter, node,
3318 				  &vf->cloud_filter_list, cloud_node) {
3319 		vsi = i40e_find_vsi_from_seid(vf, cfilter->seid);
3320 
3321 		if (!vsi) {
3322 			dev_err(&pf->pdev->dev, "VF %d: no VSI found for matching %u seid, can't delete cloud filter\n",
3323 				vf->vf_id, cfilter->seid);
3324 			continue;
3325 		}
3326 
3327 		if (cfilter->dst_port)
3328 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
3329 								false);
3330 		else
3331 			ret = i40e_add_del_cloud_filter(vsi, cfilter, false);
3332 		if (ret)
3333 			dev_err(&pf->pdev->dev,
3334 				"VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
3335 				vf->vf_id, i40e_stat_str(&pf->hw, ret),
3336 				i40e_aq_str(&pf->hw,
3337 					    pf->hw.aq.asq_last_status));
3338 
3339 		hlist_del(&cfilter->cloud_node);
3340 		kfree(cfilter);
3341 		vf->num_cloud_filters--;
3342 	}
3343 }
3344 
3345 /**
3346  * i40e_vc_del_cloud_filter
3347  * @vf: pointer to the VF info
3348  * @msg: pointer to the msg buffer
3349  *
3350  * This function deletes a cloud filter programmed as TC filter for ADq
3351  **/
3352 static int i40e_vc_del_cloud_filter(struct i40e_vf *vf, u8 *msg)
3353 {
3354 	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3355 	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3356 	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3357 	struct i40e_cloud_filter cfilter, *cf = NULL;
3358 	struct i40e_pf *pf = vf->pf;
3359 	struct i40e_vsi *vsi = NULL;
3360 	struct hlist_node *node;
3361 	i40e_status aq_ret = 0;
3362 	int i, ret;
3363 
3364 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3365 		aq_ret = I40E_ERR_PARAM;
3366 		goto err;
3367 	}
3368 
3369 	if (!vf->adq_enabled) {
3370 		dev_info(&pf->pdev->dev,
3371 			 "VF %d: ADq not enabled, can't apply cloud filter\n",
3372 			 vf->vf_id);
3373 		aq_ret = I40E_ERR_PARAM;
3374 		goto err;
3375 	}
3376 
3377 	if (i40e_validate_cloud_filter(vf, vcf)) {
3378 		dev_info(&pf->pdev->dev,
3379 			 "VF %d: Invalid input, can't apply cloud filter\n",
3380 			 vf->vf_id);
3381 		aq_ret = I40E_ERR_PARAM;
3382 		goto err;
3383 	}
3384 
3385 	memset(&cfilter, 0, sizeof(cfilter));
3386 	/* parse destination mac address */
3387 	for (i = 0; i < ETH_ALEN; i++)
3388 		cfilter.dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3389 
3390 	/* parse source mac address */
3391 	for (i = 0; i < ETH_ALEN; i++)
3392 		cfilter.src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3393 
3394 	cfilter.vlan_id = mask.vlan_id & tcf.vlan_id;
3395 	cfilter.dst_port = mask.dst_port & tcf.dst_port;
3396 	cfilter.src_port = mask.src_port & tcf.src_port;
3397 
3398 	switch (vcf->flow_type) {
3399 	case VIRTCHNL_TCP_V4_FLOW:
3400 		cfilter.n_proto = ETH_P_IP;
3401 		if (mask.dst_ip[0] & tcf.dst_ip[0])
3402 			memcpy(&cfilter.ip.v4.dst_ip, tcf.dst_ip,
3403 			       ARRAY_SIZE(tcf.dst_ip));
3404 		else if (mask.src_ip[0] & tcf.dst_ip[0])
3405 			memcpy(&cfilter.ip.v4.src_ip, tcf.src_ip,
3406 			       ARRAY_SIZE(tcf.dst_ip));
3407 		break;
3408 	case VIRTCHNL_TCP_V6_FLOW:
3409 		cfilter.n_proto = ETH_P_IPV6;
3410 		if (mask.dst_ip[3] & tcf.dst_ip[3])
3411 			memcpy(&cfilter.ip.v6.dst_ip6, tcf.dst_ip,
3412 			       sizeof(cfilter.ip.v6.dst_ip6));
3413 		if (mask.src_ip[3] & tcf.src_ip[3])
3414 			memcpy(&cfilter.ip.v6.src_ip6, tcf.src_ip,
3415 			       sizeof(cfilter.ip.v6.src_ip6));
3416 		break;
3417 	default:
3418 		/* TC filter can be configured based on different combinations
3419 		 * and in this case IP is not a part of filter config
3420 		 */
3421 		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3422 			 vf->vf_id);
3423 	}
3424 
3425 	/* get the vsi to which the tc belongs to */
3426 	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3427 	cfilter.seid = vsi->seid;
3428 	cfilter.flags = vcf->field_flags;
3429 
3430 	/* Deleting TC filter */
3431 	if (tcf.dst_port)
3432 		ret = i40e_add_del_cloud_filter_big_buf(vsi, &cfilter, false);
3433 	else
3434 		ret = i40e_add_del_cloud_filter(vsi, &cfilter, false);
3435 	if (ret) {
3436 		dev_err(&pf->pdev->dev,
3437 			"VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
3438 			vf->vf_id, i40e_stat_str(&pf->hw, ret),
3439 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3440 		goto err;
3441 	}
3442 
3443 	hlist_for_each_entry_safe(cf, node,
3444 				  &vf->cloud_filter_list, cloud_node) {
3445 		if (cf->seid != cfilter.seid)
3446 			continue;
3447 		if (mask.dst_port)
3448 			if (cfilter.dst_port != cf->dst_port)
3449 				continue;
3450 		if (mask.dst_mac[0])
3451 			if (!ether_addr_equal(cf->src_mac, cfilter.src_mac))
3452 				continue;
3453 		/* for ipv4 data to be valid, only first byte of mask is set */
3454 		if (cfilter.n_proto == ETH_P_IP && mask.dst_ip[0])
3455 			if (memcmp(&cfilter.ip.v4.dst_ip, &cf->ip.v4.dst_ip,
3456 				   ARRAY_SIZE(tcf.dst_ip)))
3457 				continue;
3458 		/* for ipv6, mask is set for all sixteen bytes (4 words) */
3459 		if (cfilter.n_proto == ETH_P_IPV6 && mask.dst_ip[3])
3460 			if (memcmp(&cfilter.ip.v6.dst_ip6, &cf->ip.v6.dst_ip6,
3461 				   sizeof(cfilter.ip.v6.src_ip6)))
3462 				continue;
3463 		if (mask.vlan_id)
3464 			if (cfilter.vlan_id != cf->vlan_id)
3465 				continue;
3466 
3467 		hlist_del(&cf->cloud_node);
3468 		kfree(cf);
3469 		vf->num_cloud_filters--;
3470 	}
3471 
3472 err:
3473 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_CLOUD_FILTER,
3474 				       aq_ret);
3475 }
3476 
3477 /**
3478  * i40e_vc_add_cloud_filter
3479  * @vf: pointer to the VF info
3480  * @msg: pointer to the msg buffer
3481  *
3482  * This function adds a cloud filter programmed as TC filter for ADq
3483  **/
3484 static int i40e_vc_add_cloud_filter(struct i40e_vf *vf, u8 *msg)
3485 {
3486 	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3487 	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3488 	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3489 	struct i40e_cloud_filter *cfilter = NULL;
3490 	struct i40e_pf *pf = vf->pf;
3491 	struct i40e_vsi *vsi = NULL;
3492 	i40e_status aq_ret = 0;
3493 	int i, ret;
3494 
3495 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3496 		aq_ret = I40E_ERR_PARAM;
3497 		goto err_out;
3498 	}
3499 
3500 	if (!vf->adq_enabled) {
3501 		dev_info(&pf->pdev->dev,
3502 			 "VF %d: ADq is not enabled, can't apply cloud filter\n",
3503 			 vf->vf_id);
3504 		aq_ret = I40E_ERR_PARAM;
3505 		goto err_out;
3506 	}
3507 
3508 	if (i40e_validate_cloud_filter(vf, vcf)) {
3509 		dev_info(&pf->pdev->dev,
3510 			 "VF %d: Invalid input/s, can't apply cloud filter\n",
3511 			 vf->vf_id);
3512 		aq_ret = I40E_ERR_PARAM;
3513 		goto err_out;
3514 	}
3515 
3516 	cfilter = kzalloc(sizeof(*cfilter), GFP_KERNEL);
3517 	if (!cfilter)
3518 		return -ENOMEM;
3519 
3520 	/* parse destination mac address */
3521 	for (i = 0; i < ETH_ALEN; i++)
3522 		cfilter->dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3523 
3524 	/* parse source mac address */
3525 	for (i = 0; i < ETH_ALEN; i++)
3526 		cfilter->src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3527 
3528 	cfilter->vlan_id = mask.vlan_id & tcf.vlan_id;
3529 	cfilter->dst_port = mask.dst_port & tcf.dst_port;
3530 	cfilter->src_port = mask.src_port & tcf.src_port;
3531 
3532 	switch (vcf->flow_type) {
3533 	case VIRTCHNL_TCP_V4_FLOW:
3534 		cfilter->n_proto = ETH_P_IP;
3535 		if (mask.dst_ip[0] & tcf.dst_ip[0])
3536 			memcpy(&cfilter->ip.v4.dst_ip, tcf.dst_ip,
3537 			       ARRAY_SIZE(tcf.dst_ip));
3538 		else if (mask.src_ip[0] & tcf.dst_ip[0])
3539 			memcpy(&cfilter->ip.v4.src_ip, tcf.src_ip,
3540 			       ARRAY_SIZE(tcf.dst_ip));
3541 		break;
3542 	case VIRTCHNL_TCP_V6_FLOW:
3543 		cfilter->n_proto = ETH_P_IPV6;
3544 		if (mask.dst_ip[3] & tcf.dst_ip[3])
3545 			memcpy(&cfilter->ip.v6.dst_ip6, tcf.dst_ip,
3546 			       sizeof(cfilter->ip.v6.dst_ip6));
3547 		if (mask.src_ip[3] & tcf.src_ip[3])
3548 			memcpy(&cfilter->ip.v6.src_ip6, tcf.src_ip,
3549 			       sizeof(cfilter->ip.v6.src_ip6));
3550 		break;
3551 	default:
3552 		/* TC filter can be configured based on different combinations
3553 		 * and in this case IP is not a part of filter config
3554 		 */
3555 		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3556 			 vf->vf_id);
3557 	}
3558 
3559 	/* get the VSI to which the TC belongs to */
3560 	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3561 	cfilter->seid = vsi->seid;
3562 	cfilter->flags = vcf->field_flags;
3563 
3564 	/* Adding cloud filter programmed as TC filter */
3565 	if (tcf.dst_port)
3566 		ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, true);
3567 	else
3568 		ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
3569 	if (ret) {
3570 		dev_err(&pf->pdev->dev,
3571 			"VF %d: Failed to add cloud filter, err %s aq_err %s\n",
3572 			vf->vf_id, i40e_stat_str(&pf->hw, ret),
3573 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3574 		goto err_free;
3575 	}
3576 
3577 	INIT_HLIST_NODE(&cfilter->cloud_node);
3578 	hlist_add_head(&cfilter->cloud_node, &vf->cloud_filter_list);
3579 	/* release the pointer passing it to the collection */
3580 	cfilter = NULL;
3581 	vf->num_cloud_filters++;
3582 err_free:
3583 	kfree(cfilter);
3584 err_out:
3585 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_CLOUD_FILTER,
3586 				       aq_ret);
3587 }
3588 
3589 /**
3590  * i40e_vc_add_qch_msg: Add queue channel and enable ADq
3591  * @vf: pointer to the VF info
3592  * @msg: pointer to the msg buffer
3593  **/
3594 static int i40e_vc_add_qch_msg(struct i40e_vf *vf, u8 *msg)
3595 {
3596 	struct virtchnl_tc_info *tci =
3597 		(struct virtchnl_tc_info *)msg;
3598 	struct i40e_pf *pf = vf->pf;
3599 	struct i40e_link_status *ls = &pf->hw.phy.link_info;
3600 	int i, adq_request_qps = 0;
3601 	i40e_status aq_ret = 0;
3602 	u64 speed = 0;
3603 
3604 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3605 		aq_ret = I40E_ERR_PARAM;
3606 		goto err;
3607 	}
3608 
3609 	/* ADq cannot be applied if spoof check is ON */
3610 	if (vf->spoofchk) {
3611 		dev_err(&pf->pdev->dev,
3612 			"Spoof check is ON, turn it OFF to enable ADq\n");
3613 		aq_ret = I40E_ERR_PARAM;
3614 		goto err;
3615 	}
3616 
3617 	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3618 		dev_err(&pf->pdev->dev,
3619 			"VF %d attempting to enable ADq, but hasn't properly negotiated that capability\n",
3620 			vf->vf_id);
3621 		aq_ret = I40E_ERR_PARAM;
3622 		goto err;
3623 	}
3624 
3625 	/* max number of traffic classes for VF currently capped at 4 */
3626 	if (!tci->num_tc || tci->num_tc > I40E_MAX_VF_VSI) {
3627 		dev_err(&pf->pdev->dev,
3628 			"VF %d trying to set %u TCs, valid range 1-%u TCs per VF\n",
3629 			vf->vf_id, tci->num_tc, I40E_MAX_VF_VSI);
3630 		aq_ret = I40E_ERR_PARAM;
3631 		goto err;
3632 	}
3633 
3634 	/* validate queues for each TC */
3635 	for (i = 0; i < tci->num_tc; i++)
3636 		if (!tci->list[i].count ||
3637 		    tci->list[i].count > I40E_DEFAULT_QUEUES_PER_VF) {
3638 			dev_err(&pf->pdev->dev,
3639 				"VF %d: TC %d trying to set %u queues, valid range 1-%u queues per TC\n",
3640 				vf->vf_id, i, tci->list[i].count,
3641 				I40E_DEFAULT_QUEUES_PER_VF);
3642 			aq_ret = I40E_ERR_PARAM;
3643 			goto err;
3644 		}
3645 
3646 	/* need Max VF queues but already have default number of queues */
3647 	adq_request_qps = I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF;
3648 
3649 	if (pf->queues_left < adq_request_qps) {
3650 		dev_err(&pf->pdev->dev,
3651 			"No queues left to allocate to VF %d\n",
3652 			vf->vf_id);
3653 		aq_ret = I40E_ERR_PARAM;
3654 		goto err;
3655 	} else {
3656 		/* we need to allocate max VF queues to enable ADq so as to
3657 		 * make sure ADq enabled VF always gets back queues when it
3658 		 * goes through a reset.
3659 		 */
3660 		vf->num_queue_pairs = I40E_MAX_VF_QUEUES;
3661 	}
3662 
3663 	/* get link speed in MB to validate rate limit */
3664 	switch (ls->link_speed) {
3665 	case VIRTCHNL_LINK_SPEED_100MB:
3666 		speed = SPEED_100;
3667 		break;
3668 	case VIRTCHNL_LINK_SPEED_1GB:
3669 		speed = SPEED_1000;
3670 		break;
3671 	case VIRTCHNL_LINK_SPEED_10GB:
3672 		speed = SPEED_10000;
3673 		break;
3674 	case VIRTCHNL_LINK_SPEED_20GB:
3675 		speed = SPEED_20000;
3676 		break;
3677 	case VIRTCHNL_LINK_SPEED_25GB:
3678 		speed = SPEED_25000;
3679 		break;
3680 	case VIRTCHNL_LINK_SPEED_40GB:
3681 		speed = SPEED_40000;
3682 		break;
3683 	default:
3684 		dev_err(&pf->pdev->dev,
3685 			"Cannot detect link speed\n");
3686 		aq_ret = I40E_ERR_PARAM;
3687 		goto err;
3688 	}
3689 
3690 	/* parse data from the queue channel info */
3691 	vf->num_tc = tci->num_tc;
3692 	for (i = 0; i < vf->num_tc; i++) {
3693 		if (tci->list[i].max_tx_rate) {
3694 			if (tci->list[i].max_tx_rate > speed) {
3695 				dev_err(&pf->pdev->dev,
3696 					"Invalid max tx rate %llu specified for VF %d.",
3697 					tci->list[i].max_tx_rate,
3698 					vf->vf_id);
3699 				aq_ret = I40E_ERR_PARAM;
3700 				goto err;
3701 			} else {
3702 				vf->ch[i].max_tx_rate =
3703 					tci->list[i].max_tx_rate;
3704 			}
3705 		}
3706 		vf->ch[i].num_qps = tci->list[i].count;
3707 	}
3708 
3709 	/* set this flag only after making sure all inputs are sane */
3710 	vf->adq_enabled = true;
3711 	/* num_req_queues is set when user changes number of queues via ethtool
3712 	 * and this causes issue for default VSI(which depends on this variable)
3713 	 * when ADq is enabled, hence reset it.
3714 	 */
3715 	vf->num_req_queues = 0;
3716 
3717 	/* reset the VF in order to allocate resources */
3718 	i40e_vc_notify_vf_reset(vf);
3719 	i40e_reset_vf(vf, false);
3720 
3721 	return I40E_SUCCESS;
3722 
3723 	/* send the response to the VF */
3724 err:
3725 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_CHANNELS,
3726 				       aq_ret);
3727 }
3728 
3729 /**
3730  * i40e_vc_del_qch_msg
3731  * @vf: pointer to the VF info
3732  * @msg: pointer to the msg buffer
3733  **/
3734 static int i40e_vc_del_qch_msg(struct i40e_vf *vf, u8 *msg)
3735 {
3736 	struct i40e_pf *pf = vf->pf;
3737 	i40e_status aq_ret = 0;
3738 
3739 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3740 		aq_ret = I40E_ERR_PARAM;
3741 		goto err;
3742 	}
3743 
3744 	if (vf->adq_enabled) {
3745 		i40e_del_all_cloud_filters(vf);
3746 		i40e_del_qch(vf);
3747 		vf->adq_enabled = false;
3748 		vf->num_tc = 0;
3749 		dev_info(&pf->pdev->dev,
3750 			 "Deleting Queue Channels and cloud filters for ADq on VF %d\n",
3751 			 vf->vf_id);
3752 	} else {
3753 		dev_info(&pf->pdev->dev, "VF %d trying to delete queue channels but ADq isn't enabled\n",
3754 			 vf->vf_id);
3755 		aq_ret = I40E_ERR_PARAM;
3756 	}
3757 
3758 	/* reset the VF in order to allocate resources */
3759 	i40e_vc_notify_vf_reset(vf);
3760 	i40e_reset_vf(vf, false);
3761 
3762 	return I40E_SUCCESS;
3763 
3764 err:
3765 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_CHANNELS,
3766 				       aq_ret);
3767 }
3768 
3769 /**
3770  * i40e_vc_process_vf_msg
3771  * @pf: pointer to the PF structure
3772  * @vf_id: source VF id
3773  * @v_opcode: operation code
3774  * @v_retval: unused return value code
3775  * @msg: pointer to the msg buffer
3776  * @msglen: msg length
3777  *
3778  * called from the common aeq/arq handler to
3779  * process request from VF
3780  **/
3781 int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
3782 			   u32 __always_unused v_retval, u8 *msg, u16 msglen)
3783 {
3784 	struct i40e_hw *hw = &pf->hw;
3785 	int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
3786 	struct i40e_vf *vf;
3787 	int ret;
3788 
3789 	pf->vf_aq_requests++;
3790 	if (local_vf_id < 0 || local_vf_id >= pf->num_alloc_vfs)
3791 		return -EINVAL;
3792 	vf = &(pf->vf[local_vf_id]);
3793 
3794 	/* Check if VF is disabled. */
3795 	if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
3796 		return I40E_ERR_PARAM;
3797 
3798 	/* perform basic checks on the msg */
3799 	ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
3800 
3801 	if (ret) {
3802 		i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
3803 		dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
3804 			local_vf_id, v_opcode, msglen);
3805 		switch (ret) {
3806 		case VIRTCHNL_STATUS_ERR_PARAM:
3807 			return -EPERM;
3808 		default:
3809 			return -EINVAL;
3810 		}
3811 	}
3812 
3813 	switch (v_opcode) {
3814 	case VIRTCHNL_OP_VERSION:
3815 		ret = i40e_vc_get_version_msg(vf, msg);
3816 		break;
3817 	case VIRTCHNL_OP_GET_VF_RESOURCES:
3818 		ret = i40e_vc_get_vf_resources_msg(vf, msg);
3819 		i40e_vc_notify_vf_link_state(vf);
3820 		break;
3821 	case VIRTCHNL_OP_RESET_VF:
3822 		i40e_vc_reset_vf_msg(vf);
3823 		ret = 0;
3824 		break;
3825 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
3826 		ret = i40e_vc_config_promiscuous_mode_msg(vf, msg);
3827 		break;
3828 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
3829 		ret = i40e_vc_config_queues_msg(vf, msg);
3830 		break;
3831 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
3832 		ret = i40e_vc_config_irq_map_msg(vf, msg);
3833 		break;
3834 	case VIRTCHNL_OP_ENABLE_QUEUES:
3835 		ret = i40e_vc_enable_queues_msg(vf, msg);
3836 		i40e_vc_notify_vf_link_state(vf);
3837 		break;
3838 	case VIRTCHNL_OP_DISABLE_QUEUES:
3839 		ret = i40e_vc_disable_queues_msg(vf, msg);
3840 		break;
3841 	case VIRTCHNL_OP_ADD_ETH_ADDR:
3842 		ret = i40e_vc_add_mac_addr_msg(vf, msg);
3843 		break;
3844 	case VIRTCHNL_OP_DEL_ETH_ADDR:
3845 		ret = i40e_vc_del_mac_addr_msg(vf, msg);
3846 		break;
3847 	case VIRTCHNL_OP_ADD_VLAN:
3848 		ret = i40e_vc_add_vlan_msg(vf, msg);
3849 		break;
3850 	case VIRTCHNL_OP_DEL_VLAN:
3851 		ret = i40e_vc_remove_vlan_msg(vf, msg);
3852 		break;
3853 	case VIRTCHNL_OP_GET_STATS:
3854 		ret = i40e_vc_get_stats_msg(vf, msg);
3855 		break;
3856 	case VIRTCHNL_OP_IWARP:
3857 		ret = i40e_vc_iwarp_msg(vf, msg, msglen);
3858 		break;
3859 	case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
3860 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, true);
3861 		break;
3862 	case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
3863 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, false);
3864 		break;
3865 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
3866 		ret = i40e_vc_config_rss_key(vf, msg);
3867 		break;
3868 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
3869 		ret = i40e_vc_config_rss_lut(vf, msg);
3870 		break;
3871 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
3872 		ret = i40e_vc_get_rss_hena(vf, msg);
3873 		break;
3874 	case VIRTCHNL_OP_SET_RSS_HENA:
3875 		ret = i40e_vc_set_rss_hena(vf, msg);
3876 		break;
3877 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
3878 		ret = i40e_vc_enable_vlan_stripping(vf, msg);
3879 		break;
3880 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
3881 		ret = i40e_vc_disable_vlan_stripping(vf, msg);
3882 		break;
3883 	case VIRTCHNL_OP_REQUEST_QUEUES:
3884 		ret = i40e_vc_request_queues_msg(vf, msg);
3885 		break;
3886 	case VIRTCHNL_OP_ENABLE_CHANNELS:
3887 		ret = i40e_vc_add_qch_msg(vf, msg);
3888 		break;
3889 	case VIRTCHNL_OP_DISABLE_CHANNELS:
3890 		ret = i40e_vc_del_qch_msg(vf, msg);
3891 		break;
3892 	case VIRTCHNL_OP_ADD_CLOUD_FILTER:
3893 		ret = i40e_vc_add_cloud_filter(vf, msg);
3894 		break;
3895 	case VIRTCHNL_OP_DEL_CLOUD_FILTER:
3896 		ret = i40e_vc_del_cloud_filter(vf, msg);
3897 		break;
3898 	case VIRTCHNL_OP_UNKNOWN:
3899 	default:
3900 		dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
3901 			v_opcode, local_vf_id);
3902 		ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
3903 					      I40E_ERR_NOT_IMPLEMENTED);
3904 		break;
3905 	}
3906 
3907 	return ret;
3908 }
3909 
3910 /**
3911  * i40e_vc_process_vflr_event
3912  * @pf: pointer to the PF structure
3913  *
3914  * called from the vlfr irq handler to
3915  * free up VF resources and state variables
3916  **/
3917 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
3918 {
3919 	struct i40e_hw *hw = &pf->hw;
3920 	u32 reg, reg_idx, bit_idx;
3921 	struct i40e_vf *vf;
3922 	int vf_id;
3923 
3924 	if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
3925 		return 0;
3926 
3927 	/* Re-enable the VFLR interrupt cause here, before looking for which
3928 	 * VF got reset. Otherwise, if another VF gets a reset while the
3929 	 * first one is being processed, that interrupt will be lost, and
3930 	 * that VF will be stuck in reset forever.
3931 	 */
3932 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
3933 	reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
3934 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
3935 	i40e_flush(hw);
3936 
3937 	clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3938 	for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
3939 		reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
3940 		bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
3941 		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
3942 		vf = &pf->vf[vf_id];
3943 		reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
3944 		if (reg & BIT(bit_idx))
3945 			/* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
3946 			i40e_reset_vf(vf, true);
3947 	}
3948 
3949 	return 0;
3950 }
3951 
3952 /**
3953  * i40e_validate_vf
3954  * @pf: the physical function
3955  * @vf_id: VF identifier
3956  *
3957  * Check that the VF is enabled and the VSI exists.
3958  *
3959  * Returns 0 on success, negative on failure
3960  **/
3961 static int i40e_validate_vf(struct i40e_pf *pf, int vf_id)
3962 {
3963 	struct i40e_vsi *vsi;
3964 	struct i40e_vf *vf;
3965 	int ret = 0;
3966 
3967 	if (vf_id >= pf->num_alloc_vfs) {
3968 		dev_err(&pf->pdev->dev,
3969 			"Invalid VF Identifier %d\n", vf_id);
3970 		ret = -EINVAL;
3971 		goto err_out;
3972 	}
3973 	vf = &pf->vf[vf_id];
3974 	vsi = i40e_find_vsi_from_id(pf, vf->lan_vsi_id);
3975 	if (!vsi)
3976 		ret = -EINVAL;
3977 err_out:
3978 	return ret;
3979 }
3980 
3981 /**
3982  * i40e_ndo_set_vf_mac
3983  * @netdev: network interface device structure
3984  * @vf_id: VF identifier
3985  * @mac: mac address
3986  *
3987  * program VF mac address
3988  **/
3989 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
3990 {
3991 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3992 	struct i40e_vsi *vsi = np->vsi;
3993 	struct i40e_pf *pf = vsi->back;
3994 	struct i40e_mac_filter *f;
3995 	struct i40e_vf *vf;
3996 	int ret = 0;
3997 	struct hlist_node *h;
3998 	int bkt;
3999 	u8 i;
4000 
4001 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4002 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4003 		return -EAGAIN;
4004 	}
4005 
4006 	/* validate the request */
4007 	ret = i40e_validate_vf(pf, vf_id);
4008 	if (ret)
4009 		goto error_param;
4010 
4011 	vf = &pf->vf[vf_id];
4012 	vsi = pf->vsi[vf->lan_vsi_idx];
4013 
4014 	/* When the VF is resetting wait until it is done.
4015 	 * It can take up to 200 milliseconds,
4016 	 * but wait for up to 300 milliseconds to be safe.
4017 	 * If the VF is indeed in reset, the vsi pointer has
4018 	 * to show on the newly loaded vsi under pf->vsi[id].
4019 	 */
4020 	for (i = 0; i < 15; i++) {
4021 		if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4022 			if (i > 0)
4023 				vsi = pf->vsi[vf->lan_vsi_idx];
4024 			break;
4025 		}
4026 		msleep(20);
4027 	}
4028 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4029 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4030 			vf_id);
4031 		ret = -EAGAIN;
4032 		goto error_param;
4033 	}
4034 
4035 	if (is_multicast_ether_addr(mac)) {
4036 		dev_err(&pf->pdev->dev,
4037 			"Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
4038 		ret = -EINVAL;
4039 		goto error_param;
4040 	}
4041 
4042 	/* Lock once because below invoked function add/del_filter requires
4043 	 * mac_filter_hash_lock to be held
4044 	 */
4045 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4046 
4047 	/* delete the temporary mac address */
4048 	if (!is_zero_ether_addr(vf->default_lan_addr.addr))
4049 		i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
4050 
4051 	/* Delete all the filters for this VSI - we're going to kill it
4052 	 * anyway.
4053 	 */
4054 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
4055 		__i40e_del_filter(vsi, f);
4056 
4057 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4058 
4059 	/* program mac filter */
4060 	if (i40e_sync_vsi_filters(vsi)) {
4061 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
4062 		ret = -EIO;
4063 		goto error_param;
4064 	}
4065 	ether_addr_copy(vf->default_lan_addr.addr, mac);
4066 
4067 	if (is_zero_ether_addr(mac)) {
4068 		vf->pf_set_mac = false;
4069 		dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
4070 	} else {
4071 		vf->pf_set_mac = true;
4072 		dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
4073 			 mac, vf_id);
4074 	}
4075 
4076 	/* Force the VF interface down so it has to bring up with new MAC
4077 	 * address
4078 	 */
4079 	i40e_vc_disable_vf(vf);
4080 	dev_info(&pf->pdev->dev, "Bring down and up the VF interface to make this change effective.\n");
4081 
4082 error_param:
4083 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4084 	return ret;
4085 }
4086 
4087 /**
4088  * i40e_vsi_has_vlans - True if VSI has configured VLANs
4089  * @vsi: pointer to the vsi
4090  *
4091  * Check if a VSI has configured any VLANs. False if we have a port VLAN or if
4092  * we have no configured VLANs. Do not call while holding the
4093  * mac_filter_hash_lock.
4094  */
4095 static bool i40e_vsi_has_vlans(struct i40e_vsi *vsi)
4096 {
4097 	bool have_vlans;
4098 
4099 	/* If we have a port VLAN, then the VSI cannot have any VLANs
4100 	 * configured, as all MAC/VLAN filters will be assigned to the PVID.
4101 	 */
4102 	if (vsi->info.pvid)
4103 		return false;
4104 
4105 	/* Since we don't have a PVID, we know that if the device is in VLAN
4106 	 * mode it must be because of a VLAN filter configured on this VSI.
4107 	 */
4108 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4109 	have_vlans = i40e_is_vsi_in_vlan(vsi);
4110 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4111 
4112 	return have_vlans;
4113 }
4114 
4115 /**
4116  * i40e_ndo_set_vf_port_vlan
4117  * @netdev: network interface device structure
4118  * @vf_id: VF identifier
4119  * @vlan_id: mac address
4120  * @qos: priority setting
4121  * @vlan_proto: vlan protocol
4122  *
4123  * program VF vlan id and/or qos
4124  **/
4125 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
4126 			      u16 vlan_id, u8 qos, __be16 vlan_proto)
4127 {
4128 	u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
4129 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4130 	bool allmulti = false, alluni = false;
4131 	struct i40e_pf *pf = np->vsi->back;
4132 	struct i40e_vsi *vsi;
4133 	struct i40e_vf *vf;
4134 	int ret = 0;
4135 
4136 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4137 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4138 		return -EAGAIN;
4139 	}
4140 
4141 	/* validate the request */
4142 	ret = i40e_validate_vf(pf, vf_id);
4143 	if (ret)
4144 		goto error_pvid;
4145 
4146 	if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
4147 		dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
4148 		ret = -EINVAL;
4149 		goto error_pvid;
4150 	}
4151 
4152 	if (vlan_proto != htons(ETH_P_8021Q)) {
4153 		dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
4154 		ret = -EPROTONOSUPPORT;
4155 		goto error_pvid;
4156 	}
4157 
4158 	vf = &pf->vf[vf_id];
4159 	vsi = pf->vsi[vf->lan_vsi_idx];
4160 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4161 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4162 			vf_id);
4163 		ret = -EAGAIN;
4164 		goto error_pvid;
4165 	}
4166 
4167 	if (le16_to_cpu(vsi->info.pvid) == vlanprio)
4168 		/* duplicate request, so just return success */
4169 		goto error_pvid;
4170 
4171 	if (i40e_vsi_has_vlans(vsi)) {
4172 		dev_err(&pf->pdev->dev,
4173 			"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",
4174 			vf_id);
4175 		/* Administrator Error - knock the VF offline until he does
4176 		 * the right thing by reconfiguring his network correctly
4177 		 * and then reloading the VF driver.
4178 		 */
4179 		i40e_vc_disable_vf(vf);
4180 		/* During reset the VF got a new VSI, so refresh the pointer. */
4181 		vsi = pf->vsi[vf->lan_vsi_idx];
4182 	}
4183 
4184 	/* Locked once because multiple functions below iterate list */
4185 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4186 
4187 	/* Check for condition where there was already a port VLAN ID
4188 	 * filter set and now it is being deleted by setting it to zero.
4189 	 * Additionally check for the condition where there was a port
4190 	 * VLAN but now there is a new and different port VLAN being set.
4191 	 * Before deleting all the old VLAN filters we must add new ones
4192 	 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
4193 	 * MAC addresses deleted.
4194 	 */
4195 	if ((!(vlan_id || qos) ||
4196 	    vlanprio != le16_to_cpu(vsi->info.pvid)) &&
4197 	    vsi->info.pvid) {
4198 		ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
4199 		if (ret) {
4200 			dev_info(&vsi->back->pdev->dev,
4201 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4202 				 vsi->back->hw.aq.asq_last_status);
4203 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4204 			goto error_pvid;
4205 		}
4206 	}
4207 
4208 	if (vsi->info.pvid) {
4209 		/* remove all filters on the old VLAN */
4210 		i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
4211 					   VLAN_VID_MASK));
4212 	}
4213 
4214 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4215 
4216 	/* disable promisc modes in case they were enabled */
4217 	ret = i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id,
4218 					      allmulti, alluni);
4219 	if (ret) {
4220 		dev_err(&pf->pdev->dev, "Unable to config VF promiscuous mode\n");
4221 		goto error_pvid;
4222 	}
4223 
4224 	if (vlan_id || qos)
4225 		ret = i40e_vsi_add_pvid(vsi, vlanprio);
4226 	else
4227 		i40e_vsi_remove_pvid(vsi);
4228 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4229 
4230 	if (vlan_id) {
4231 		dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
4232 			 vlan_id, qos, vf_id);
4233 
4234 		/* add new VLAN filter for each MAC */
4235 		ret = i40e_add_vlan_all_mac(vsi, vlan_id);
4236 		if (ret) {
4237 			dev_info(&vsi->back->pdev->dev,
4238 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4239 				 vsi->back->hw.aq.asq_last_status);
4240 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4241 			goto error_pvid;
4242 		}
4243 
4244 		/* remove the previously added non-VLAN MAC filters */
4245 		i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
4246 	}
4247 
4248 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4249 
4250 	if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
4251 		alluni = true;
4252 
4253 	if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
4254 		allmulti = true;
4255 
4256 	/* Schedule the worker thread to take care of applying changes */
4257 	i40e_service_event_schedule(vsi->back);
4258 
4259 	if (ret) {
4260 		dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
4261 		goto error_pvid;
4262 	}
4263 
4264 	/* The Port VLAN needs to be saved across resets the same as the
4265 	 * default LAN MAC address.
4266 	 */
4267 	vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
4268 
4269 	ret = i40e_config_vf_promiscuous_mode(vf, vsi->id, allmulti, alluni);
4270 	if (ret) {
4271 		dev_err(&pf->pdev->dev, "Unable to config vf promiscuous mode\n");
4272 		goto error_pvid;
4273 	}
4274 
4275 	ret = 0;
4276 
4277 error_pvid:
4278 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4279 	return ret;
4280 }
4281 
4282 /**
4283  * i40e_ndo_set_vf_bw
4284  * @netdev: network interface device structure
4285  * @vf_id: VF identifier
4286  * @min_tx_rate: Minimum Tx rate
4287  * @max_tx_rate: Maximum Tx rate
4288  *
4289  * configure VF Tx rate
4290  **/
4291 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
4292 		       int max_tx_rate)
4293 {
4294 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4295 	struct i40e_pf *pf = np->vsi->back;
4296 	struct i40e_vsi *vsi;
4297 	struct i40e_vf *vf;
4298 	int ret = 0;
4299 
4300 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4301 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4302 		return -EAGAIN;
4303 	}
4304 
4305 	/* validate the request */
4306 	ret = i40e_validate_vf(pf, vf_id);
4307 	if (ret)
4308 		goto error;
4309 
4310 	if (min_tx_rate) {
4311 		dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
4312 			min_tx_rate, vf_id);
4313 		ret = -EINVAL;
4314 		goto error;
4315 	}
4316 
4317 	vf = &pf->vf[vf_id];
4318 	vsi = pf->vsi[vf->lan_vsi_idx];
4319 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4320 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4321 			vf_id);
4322 		ret = -EAGAIN;
4323 		goto error;
4324 	}
4325 
4326 	ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
4327 	if (ret)
4328 		goto error;
4329 
4330 	vf->tx_rate = max_tx_rate;
4331 error:
4332 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4333 	return ret;
4334 }
4335 
4336 /**
4337  * i40e_ndo_get_vf_config
4338  * @netdev: network interface device structure
4339  * @vf_id: VF identifier
4340  * @ivi: VF configuration structure
4341  *
4342  * return VF configuration
4343  **/
4344 int i40e_ndo_get_vf_config(struct net_device *netdev,
4345 			   int vf_id, struct ifla_vf_info *ivi)
4346 {
4347 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4348 	struct i40e_vsi *vsi = np->vsi;
4349 	struct i40e_pf *pf = vsi->back;
4350 	struct i40e_vf *vf;
4351 	int ret = 0;
4352 
4353 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4354 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4355 		return -EAGAIN;
4356 	}
4357 
4358 	/* validate the request */
4359 	ret = i40e_validate_vf(pf, vf_id);
4360 	if (ret)
4361 		goto error_param;
4362 
4363 	vf = &pf->vf[vf_id];
4364 	/* first vsi is always the LAN vsi */
4365 	vsi = pf->vsi[vf->lan_vsi_idx];
4366 	if (!vsi) {
4367 		ret = -ENOENT;
4368 		goto error_param;
4369 	}
4370 
4371 	ivi->vf = vf_id;
4372 
4373 	ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
4374 
4375 	ivi->max_tx_rate = vf->tx_rate;
4376 	ivi->min_tx_rate = 0;
4377 	ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
4378 	ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
4379 		   I40E_VLAN_PRIORITY_SHIFT;
4380 	if (vf->link_forced == false)
4381 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4382 	else if (vf->link_up == true)
4383 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4384 	else
4385 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4386 	ivi->spoofchk = vf->spoofchk;
4387 	ivi->trusted = vf->trusted;
4388 	ret = 0;
4389 
4390 error_param:
4391 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4392 	return ret;
4393 }
4394 
4395 /**
4396  * i40e_ndo_set_vf_link_state
4397  * @netdev: network interface device structure
4398  * @vf_id: VF identifier
4399  * @link: required link state
4400  *
4401  * Set the link state of a specified VF, regardless of physical link state
4402  **/
4403 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
4404 {
4405 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4406 	struct i40e_pf *pf = np->vsi->back;
4407 	struct virtchnl_pf_event pfe;
4408 	struct i40e_hw *hw = &pf->hw;
4409 	struct i40e_vf *vf;
4410 	int abs_vf_id;
4411 	int ret = 0;
4412 
4413 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4414 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4415 		return -EAGAIN;
4416 	}
4417 
4418 	/* validate the request */
4419 	if (vf_id >= pf->num_alloc_vfs) {
4420 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4421 		ret = -EINVAL;
4422 		goto error_out;
4423 	}
4424 
4425 	vf = &pf->vf[vf_id];
4426 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
4427 
4428 	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
4429 	pfe.severity = PF_EVENT_SEVERITY_INFO;
4430 
4431 	switch (link) {
4432 	case IFLA_VF_LINK_STATE_AUTO:
4433 		vf->link_forced = false;
4434 		pfe.event_data.link_event.link_status =
4435 			pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
4436 		pfe.event_data.link_event.link_speed =
4437 			(enum virtchnl_link_speed)
4438 			pf->hw.phy.link_info.link_speed;
4439 		break;
4440 	case IFLA_VF_LINK_STATE_ENABLE:
4441 		vf->link_forced = true;
4442 		vf->link_up = true;
4443 		pfe.event_data.link_event.link_status = true;
4444 		pfe.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_40GB;
4445 		break;
4446 	case IFLA_VF_LINK_STATE_DISABLE:
4447 		vf->link_forced = true;
4448 		vf->link_up = false;
4449 		pfe.event_data.link_event.link_status = false;
4450 		pfe.event_data.link_event.link_speed = 0;
4451 		break;
4452 	default:
4453 		ret = -EINVAL;
4454 		goto error_out;
4455 	}
4456 	/* Notify the VF of its new link state */
4457 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
4458 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
4459 
4460 error_out:
4461 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4462 	return ret;
4463 }
4464 
4465 /**
4466  * i40e_ndo_set_vf_spoofchk
4467  * @netdev: network interface device structure
4468  * @vf_id: VF identifier
4469  * @enable: flag to enable or disable feature
4470  *
4471  * Enable or disable VF spoof checking
4472  **/
4473 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
4474 {
4475 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4476 	struct i40e_vsi *vsi = np->vsi;
4477 	struct i40e_pf *pf = vsi->back;
4478 	struct i40e_vsi_context ctxt;
4479 	struct i40e_hw *hw = &pf->hw;
4480 	struct i40e_vf *vf;
4481 	int ret = 0;
4482 
4483 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4484 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4485 		return -EAGAIN;
4486 	}
4487 
4488 	/* validate the request */
4489 	if (vf_id >= pf->num_alloc_vfs) {
4490 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4491 		ret = -EINVAL;
4492 		goto out;
4493 	}
4494 
4495 	vf = &(pf->vf[vf_id]);
4496 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4497 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4498 			vf_id);
4499 		ret = -EAGAIN;
4500 		goto out;
4501 	}
4502 
4503 	if (enable == vf->spoofchk)
4504 		goto out;
4505 
4506 	vf->spoofchk = enable;
4507 	memset(&ctxt, 0, sizeof(ctxt));
4508 	ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
4509 	ctxt.pf_num = pf->hw.pf_id;
4510 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
4511 	if (enable)
4512 		ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
4513 					I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
4514 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
4515 	if (ret) {
4516 		dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
4517 			ret);
4518 		ret = -EIO;
4519 	}
4520 out:
4521 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4522 	return ret;
4523 }
4524 
4525 /**
4526  * i40e_ndo_set_vf_trust
4527  * @netdev: network interface device structure of the pf
4528  * @vf_id: VF identifier
4529  * @setting: trust setting
4530  *
4531  * Enable or disable VF trust setting
4532  **/
4533 int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
4534 {
4535 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4536 	struct i40e_pf *pf = np->vsi->back;
4537 	struct i40e_vf *vf;
4538 	int ret = 0;
4539 
4540 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4541 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4542 		return -EAGAIN;
4543 	}
4544 
4545 	/* validate the request */
4546 	if (vf_id >= pf->num_alloc_vfs) {
4547 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4548 		ret = -EINVAL;
4549 		goto out;
4550 	}
4551 
4552 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4553 		dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
4554 		ret = -EINVAL;
4555 		goto out;
4556 	}
4557 
4558 	vf = &pf->vf[vf_id];
4559 
4560 	if (setting == vf->trusted)
4561 		goto out;
4562 
4563 	vf->trusted = setting;
4564 	i40e_vc_disable_vf(vf);
4565 	dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
4566 		 vf_id, setting ? "" : "un");
4567 
4568 	if (vf->adq_enabled) {
4569 		if (!vf->trusted) {
4570 			dev_info(&pf->pdev->dev,
4571 				 "VF %u no longer Trusted, deleting all cloud filters\n",
4572 				 vf_id);
4573 			i40e_del_all_cloud_filters(vf);
4574 		}
4575 	}
4576 
4577 out:
4578 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4579 	return ret;
4580 }
4581 
4582 /**
4583  * i40e_get_vf_stats - populate some stats for the VF
4584  * @netdev: the netdev of the PF
4585  * @vf_id: the host OS identifier (0-127)
4586  * @vf_stats: pointer to the OS memory to be initialized
4587  */
4588 int i40e_get_vf_stats(struct net_device *netdev, int vf_id,
4589 		      struct ifla_vf_stats *vf_stats)
4590 {
4591 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4592 	struct i40e_pf *pf = np->vsi->back;
4593 	struct i40e_eth_stats *stats;
4594 	struct i40e_vsi *vsi;
4595 	struct i40e_vf *vf;
4596 
4597 	/* validate the request */
4598 	if (i40e_validate_vf(pf, vf_id))
4599 		return -EINVAL;
4600 
4601 	vf = &pf->vf[vf_id];
4602 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4603 		dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
4604 		return -EBUSY;
4605 	}
4606 
4607 	vsi = pf->vsi[vf->lan_vsi_idx];
4608 	if (!vsi)
4609 		return -EINVAL;
4610 
4611 	i40e_update_eth_stats(vsi);
4612 	stats = &vsi->eth_stats;
4613 
4614 	memset(vf_stats, 0, sizeof(*vf_stats));
4615 
4616 	vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
4617 		stats->rx_multicast;
4618 	vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
4619 		stats->tx_multicast;
4620 	vf_stats->rx_bytes   = stats->rx_bytes;
4621 	vf_stats->tx_bytes   = stats->tx_bytes;
4622 	vf_stats->broadcast  = stats->rx_broadcast;
4623 	vf_stats->multicast  = stats->rx_multicast;
4624 	vf_stats->rx_dropped = stats->rx_discards;
4625 	vf_stats->tx_dropped = stats->tx_discards;
4626 
4627 	return 0;
4628 }
4629