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 static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi);
1110 
1111 /**
1112  * i40e_config_vf_promiscuous_mode
1113  * @vf: pointer to the VF info
1114  * @vsi_id: VSI id
1115  * @allmulti: set MAC L2 layer multicast promiscuous enable/disable
1116  * @alluni: set MAC L2 layer unicast promiscuous enable/disable
1117  *
1118  * Called from the VF to configure the promiscuous mode of
1119  * VF vsis and from the VF reset path to reset promiscuous mode.
1120  **/
1121 static i40e_status i40e_config_vf_promiscuous_mode(struct i40e_vf *vf,
1122 						   u16 vsi_id,
1123 						   bool allmulti,
1124 						   bool alluni)
1125 {
1126 	struct i40e_pf *pf = vf->pf;
1127 	struct i40e_hw *hw = &pf->hw;
1128 	struct i40e_mac_filter *f;
1129 	i40e_status aq_ret = 0;
1130 	struct i40e_vsi *vsi;
1131 	int bkt;
1132 
1133 	vsi = i40e_find_vsi_from_id(pf, vsi_id);
1134 	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id) || !vsi)
1135 		return I40E_ERR_PARAM;
1136 
1137 	if (vf->port_vlan_id) {
1138 		aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
1139 							    allmulti,
1140 							    vf->port_vlan_id,
1141 							    NULL);
1142 		if (aq_ret) {
1143 			int aq_err = pf->hw.aq.asq_last_status;
1144 
1145 			dev_err(&pf->pdev->dev,
1146 				"VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1147 				vf->vf_id,
1148 				i40e_stat_str(&pf->hw, aq_ret),
1149 				i40e_aq_str(&pf->hw, aq_err));
1150 			return aq_ret;
1151 		}
1152 
1153 		aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
1154 							    alluni,
1155 							    vf->port_vlan_id,
1156 							    NULL);
1157 		if (aq_ret) {
1158 			int aq_err = pf->hw.aq.asq_last_status;
1159 
1160 			dev_err(&pf->pdev->dev,
1161 				"VF %d failed to set unicast promiscuous mode err %s aq_err %s\n",
1162 				vf->vf_id,
1163 				i40e_stat_str(&pf->hw, aq_ret),
1164 				i40e_aq_str(&pf->hw, aq_err));
1165 		}
1166 		return aq_ret;
1167 	} else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1168 		hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1169 			if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1170 				continue;
1171 			aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
1172 								    vsi->seid,
1173 								    allmulti,
1174 								    f->vlan,
1175 								    NULL);
1176 			if (aq_ret) {
1177 				int aq_err = pf->hw.aq.asq_last_status;
1178 
1179 				dev_err(&pf->pdev->dev,
1180 					"Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
1181 					f->vlan,
1182 					i40e_stat_str(&pf->hw, aq_ret),
1183 					i40e_aq_str(&pf->hw, aq_err));
1184 			}
1185 
1186 			aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
1187 								    vsi->seid,
1188 								    alluni,
1189 								    f->vlan,
1190 								    NULL);
1191 			if (aq_ret) {
1192 				int aq_err = pf->hw.aq.asq_last_status;
1193 
1194 				dev_err(&pf->pdev->dev,
1195 					"Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
1196 					f->vlan,
1197 					i40e_stat_str(&pf->hw, aq_ret),
1198 					i40e_aq_str(&pf->hw, aq_err));
1199 			}
1200 		}
1201 		return aq_ret;
1202 	}
1203 	aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid, allmulti,
1204 						       NULL);
1205 	if (aq_ret) {
1206 		int aq_err = pf->hw.aq.asq_last_status;
1207 
1208 		dev_err(&pf->pdev->dev,
1209 			"VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1210 			vf->vf_id,
1211 			i40e_stat_str(&pf->hw, aq_ret),
1212 			i40e_aq_str(&pf->hw, aq_err));
1213 		return aq_ret;
1214 	}
1215 
1216 	aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid, alluni,
1217 						     NULL, true);
1218 	if (aq_ret) {
1219 		int aq_err = pf->hw.aq.asq_last_status;
1220 
1221 		dev_err(&pf->pdev->dev,
1222 			"VF %d failed to set unicast promiscuous mode err %s aq_err %s\n",
1223 			vf->vf_id,
1224 			i40e_stat_str(&pf->hw, aq_ret),
1225 			i40e_aq_str(&pf->hw, aq_err));
1226 	}
1227 
1228 	return aq_ret;
1229 }
1230 
1231 /**
1232  * i40e_trigger_vf_reset
1233  * @vf: pointer to the VF structure
1234  * @flr: VFLR was issued or not
1235  *
1236  * Trigger hardware to start a reset for a particular VF. Expects the caller
1237  * to wait the proper amount of time to allow hardware to reset the VF before
1238  * it cleans up and restores VF functionality.
1239  **/
1240 static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
1241 {
1242 	struct i40e_pf *pf = vf->pf;
1243 	struct i40e_hw *hw = &pf->hw;
1244 	u32 reg, reg_idx, bit_idx;
1245 
1246 	/* warn the VF */
1247 	clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1248 
1249 	/* Disable VF's configuration API during reset. The flag is re-enabled
1250 	 * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
1251 	 * It's normally disabled in i40e_free_vf_res(), but it's safer
1252 	 * to do it earlier to give some time to finish to any VF config
1253 	 * functions that may still be running at this point.
1254 	 */
1255 	clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
1256 
1257 	/* In the case of a VFLR, the HW has already reset the VF and we
1258 	 * just need to clean up, so don't hit the VFRTRIG register.
1259 	 */
1260 	if (!flr) {
1261 		/* reset VF using VPGEN_VFRTRIG reg */
1262 		reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1263 		reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1264 		wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1265 		i40e_flush(hw);
1266 	}
1267 	/* clear the VFLR bit in GLGEN_VFLRSTAT */
1268 	reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
1269 	bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
1270 	wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1271 	i40e_flush(hw);
1272 
1273 	if (i40e_quiesce_vf_pci(vf))
1274 		dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
1275 			vf->vf_id);
1276 }
1277 
1278 /**
1279  * i40e_cleanup_reset_vf
1280  * @vf: pointer to the VF structure
1281  *
1282  * Cleanup a VF after the hardware reset is finished. Expects the caller to
1283  * have verified whether the reset is finished properly, and ensure the
1284  * minimum amount of wait time has passed.
1285  **/
1286 static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
1287 {
1288 	struct i40e_pf *pf = vf->pf;
1289 	struct i40e_hw *hw = &pf->hw;
1290 	u32 reg;
1291 
1292 	/* disable promisc modes in case they were enabled */
1293 	i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id, false, false);
1294 
1295 	/* free VF resources to begin resetting the VSI state */
1296 	i40e_free_vf_res(vf);
1297 
1298 	/* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
1299 	 * By doing this we allow HW to access VF memory at any point. If we
1300 	 * did it any sooner, HW could access memory while it was being freed
1301 	 * in i40e_free_vf_res(), causing an IOMMU fault.
1302 	 *
1303 	 * On the other hand, this needs to be done ASAP, because the VF driver
1304 	 * is waiting for this to happen and may report a timeout. It's
1305 	 * harmless, but it gets logged into Guest OS kernel log, so best avoid
1306 	 * it.
1307 	 */
1308 	reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
1309 	reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
1310 	wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
1311 
1312 	/* reallocate VF resources to finish resetting the VSI state */
1313 	if (!i40e_alloc_vf_res(vf)) {
1314 		int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1315 		i40e_enable_vf_mappings(vf);
1316 		set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1317 		clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1318 		/* Do not notify the client during VF init */
1319 		if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
1320 					&vf->vf_states))
1321 			i40e_notify_client_of_vf_reset(pf, abs_vf_id);
1322 		vf->num_vlan = 0;
1323 	}
1324 
1325 	/* Tell the VF driver the reset is done. This needs to be done only
1326 	 * after VF has been fully initialized, because the VF driver may
1327 	 * request resources immediately after setting this flag.
1328 	 */
1329 	wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
1330 }
1331 
1332 /**
1333  * i40e_reset_vf
1334  * @vf: pointer to the VF structure
1335  * @flr: VFLR was issued or not
1336  *
1337  * Returns true if the VF is reset, false otherwise.
1338  **/
1339 bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
1340 {
1341 	struct i40e_pf *pf = vf->pf;
1342 	struct i40e_hw *hw = &pf->hw;
1343 	bool rsd = false;
1344 	u32 reg;
1345 	int i;
1346 
1347 	/* If the VFs have been disabled, this means something else is
1348 	 * resetting the VF, so we shouldn't continue.
1349 	 */
1350 	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1351 		return false;
1352 
1353 	i40e_trigger_vf_reset(vf, flr);
1354 
1355 	/* poll VPGEN_VFRSTAT reg to make sure
1356 	 * that reset is complete
1357 	 */
1358 	for (i = 0; i < 10; i++) {
1359 		/* VF reset requires driver to first reset the VF and then
1360 		 * poll the status register to make sure that the reset
1361 		 * completed successfully. Due to internal HW FIFO flushes,
1362 		 * we must wait 10ms before the register will be valid.
1363 		 */
1364 		usleep_range(10000, 20000);
1365 		reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1366 		if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
1367 			rsd = true;
1368 			break;
1369 		}
1370 	}
1371 
1372 	if (flr)
1373 		usleep_range(10000, 20000);
1374 
1375 	if (!rsd)
1376 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1377 			vf->vf_id);
1378 	usleep_range(10000, 20000);
1379 
1380 	/* On initial reset, we don't have any queues to disable */
1381 	if (vf->lan_vsi_idx != 0)
1382 		i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
1383 
1384 	i40e_cleanup_reset_vf(vf);
1385 
1386 	i40e_flush(hw);
1387 	clear_bit(__I40E_VF_DISABLE, pf->state);
1388 
1389 	return true;
1390 }
1391 
1392 /**
1393  * i40e_reset_all_vfs
1394  * @pf: pointer to the PF structure
1395  * @flr: VFLR was issued or not
1396  *
1397  * Reset all allocated VFs in one go. First, tell the hardware to reset each
1398  * VF, then do all the waiting in one chunk, and finally finish restoring each
1399  * VF after the wait. This is useful during PF routines which need to reset
1400  * all VFs, as otherwise it must perform these resets in a serialized fashion.
1401  *
1402  * Returns true if any VFs were reset, and false otherwise.
1403  **/
1404 bool i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
1405 {
1406 	struct i40e_hw *hw = &pf->hw;
1407 	struct i40e_vf *vf;
1408 	int i, v;
1409 	u32 reg;
1410 
1411 	/* If we don't have any VFs, then there is nothing to reset */
1412 	if (!pf->num_alloc_vfs)
1413 		return false;
1414 
1415 	/* If VFs have been disabled, there is no need to reset */
1416 	if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1417 		return false;
1418 
1419 	/* Begin reset on all VFs at once */
1420 	for (v = 0; v < pf->num_alloc_vfs; v++)
1421 		i40e_trigger_vf_reset(&pf->vf[v], flr);
1422 
1423 	/* HW requires some time to make sure it can flush the FIFO for a VF
1424 	 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1425 	 * sequence to make sure that it has completed. We'll keep track of
1426 	 * the VFs using a simple iterator that increments once that VF has
1427 	 * finished resetting.
1428 	 */
1429 	for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1430 		usleep_range(10000, 20000);
1431 
1432 		/* Check each VF in sequence, beginning with the VF to fail
1433 		 * the previous check.
1434 		 */
1435 		while (v < pf->num_alloc_vfs) {
1436 			vf = &pf->vf[v];
1437 			reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1438 			if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
1439 				break;
1440 
1441 			/* If the current VF has finished resetting, move on
1442 			 * to the next VF in sequence.
1443 			 */
1444 			v++;
1445 		}
1446 	}
1447 
1448 	if (flr)
1449 		usleep_range(10000, 20000);
1450 
1451 	/* Display a warning if at least one VF didn't manage to reset in
1452 	 * time, but continue on with the operation.
1453 	 */
1454 	if (v < pf->num_alloc_vfs)
1455 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1456 			pf->vf[v].vf_id);
1457 	usleep_range(10000, 20000);
1458 
1459 	/* Begin disabling all the rings associated with VFs, but do not wait
1460 	 * between each VF.
1461 	 */
1462 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1463 		/* On initial reset, we don't have any queues to disable */
1464 		if (pf->vf[v].lan_vsi_idx == 0)
1465 			continue;
1466 
1467 		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
1468 	}
1469 
1470 	/* Now that we've notified HW to disable all of the VF rings, wait
1471 	 * until they finish.
1472 	 */
1473 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1474 		/* On initial reset, we don't have any queues to disable */
1475 		if (pf->vf[v].lan_vsi_idx == 0)
1476 			continue;
1477 
1478 		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
1479 	}
1480 
1481 	/* Hw may need up to 50ms to finish disabling the RX queues. We
1482 	 * minimize the wait by delaying only once for all VFs.
1483 	 */
1484 	mdelay(50);
1485 
1486 	/* Finish the reset on each VF */
1487 	for (v = 0; v < pf->num_alloc_vfs; v++)
1488 		i40e_cleanup_reset_vf(&pf->vf[v]);
1489 
1490 	i40e_flush(hw);
1491 	clear_bit(__I40E_VF_DISABLE, pf->state);
1492 
1493 	return true;
1494 }
1495 
1496 /**
1497  * i40e_free_vfs
1498  * @pf: pointer to the PF structure
1499  *
1500  * free VF resources
1501  **/
1502 void i40e_free_vfs(struct i40e_pf *pf)
1503 {
1504 	struct i40e_hw *hw = &pf->hw;
1505 	u32 reg_idx, bit_idx;
1506 	int i, tmp, vf_id;
1507 
1508 	if (!pf->vf)
1509 		return;
1510 	while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1511 		usleep_range(1000, 2000);
1512 
1513 	i40e_notify_client_of_vf_enable(pf, 0);
1514 
1515 	/* Amortize wait time by stopping all VFs at the same time */
1516 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1517 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1518 			continue;
1519 
1520 		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
1521 	}
1522 
1523 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1524 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1525 			continue;
1526 
1527 		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
1528 	}
1529 
1530 	/* Disable IOV before freeing resources. This lets any VF drivers
1531 	 * running in the host get themselves cleaned up before we yank
1532 	 * the carpet out from underneath their feet.
1533 	 */
1534 	if (!pci_vfs_assigned(pf->pdev))
1535 		pci_disable_sriov(pf->pdev);
1536 	else
1537 		dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1538 
1539 	/* free up VF resources */
1540 	tmp = pf->num_alloc_vfs;
1541 	pf->num_alloc_vfs = 0;
1542 	for (i = 0; i < tmp; i++) {
1543 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1544 			i40e_free_vf_res(&pf->vf[i]);
1545 		/* disable qp mappings */
1546 		i40e_disable_vf_mappings(&pf->vf[i]);
1547 	}
1548 
1549 	kfree(pf->vf);
1550 	pf->vf = NULL;
1551 
1552 	/* This check is for when the driver is unloaded while VFs are
1553 	 * assigned. Setting the number of VFs to 0 through sysfs is caught
1554 	 * before this function ever gets called.
1555 	 */
1556 	if (!pci_vfs_assigned(pf->pdev)) {
1557 		/* Acknowledge VFLR for all VFS. Without this, VFs will fail to
1558 		 * work correctly when SR-IOV gets re-enabled.
1559 		 */
1560 		for (vf_id = 0; vf_id < tmp; vf_id++) {
1561 			reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1562 			bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1563 			wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1564 		}
1565 	}
1566 	clear_bit(__I40E_VF_DISABLE, pf->state);
1567 }
1568 
1569 #ifdef CONFIG_PCI_IOV
1570 /**
1571  * i40e_alloc_vfs
1572  * @pf: pointer to the PF structure
1573  * @num_alloc_vfs: number of VFs to allocate
1574  *
1575  * allocate VF resources
1576  **/
1577 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1578 {
1579 	struct i40e_vf *vfs;
1580 	int i, ret = 0;
1581 
1582 	/* Disable interrupt 0 so we don't try to handle the VFLR. */
1583 	i40e_irq_dynamic_disable_icr0(pf);
1584 
1585 	/* Check to see if we're just allocating resources for extant VFs */
1586 	if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
1587 		ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1588 		if (ret) {
1589 			pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1590 			pf->num_alloc_vfs = 0;
1591 			goto err_iov;
1592 		}
1593 	}
1594 	/* allocate memory */
1595 	vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1596 	if (!vfs) {
1597 		ret = -ENOMEM;
1598 		goto err_alloc;
1599 	}
1600 	pf->vf = vfs;
1601 
1602 	/* apply default profile */
1603 	for (i = 0; i < num_alloc_vfs; i++) {
1604 		vfs[i].pf = pf;
1605 		vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
1606 		vfs[i].vf_id = i;
1607 
1608 		/* assign default capabilities */
1609 		set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1610 		vfs[i].spoofchk = true;
1611 
1612 		set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
1613 
1614 	}
1615 	pf->num_alloc_vfs = num_alloc_vfs;
1616 
1617 	/* VF resources get allocated during reset */
1618 	i40e_reset_all_vfs(pf, false);
1619 
1620 	i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
1621 
1622 err_alloc:
1623 	if (ret)
1624 		i40e_free_vfs(pf);
1625 err_iov:
1626 	/* Re-enable interrupt 0. */
1627 	i40e_irq_dynamic_enable_icr0(pf);
1628 	return ret;
1629 }
1630 
1631 #endif
1632 /**
1633  * i40e_pci_sriov_enable
1634  * @pdev: pointer to a pci_dev structure
1635  * @num_vfs: number of VFs to allocate
1636  *
1637  * Enable or change the number of VFs
1638  **/
1639 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1640 {
1641 #ifdef CONFIG_PCI_IOV
1642 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1643 	int pre_existing_vfs = pci_num_vf(pdev);
1644 	int err = 0;
1645 
1646 	if (test_bit(__I40E_TESTING, pf->state)) {
1647 		dev_warn(&pdev->dev,
1648 			 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
1649 		err = -EPERM;
1650 		goto err_out;
1651 	}
1652 
1653 	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1654 		i40e_free_vfs(pf);
1655 	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1656 		goto out;
1657 
1658 	if (num_vfs > pf->num_req_vfs) {
1659 		dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1660 			 num_vfs, pf->num_req_vfs);
1661 		err = -EPERM;
1662 		goto err_out;
1663 	}
1664 
1665 	dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1666 	err = i40e_alloc_vfs(pf, num_vfs);
1667 	if (err) {
1668 		dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1669 		goto err_out;
1670 	}
1671 
1672 out:
1673 	return num_vfs;
1674 
1675 err_out:
1676 	return err;
1677 #endif
1678 	return 0;
1679 }
1680 
1681 /**
1682  * i40e_pci_sriov_configure
1683  * @pdev: pointer to a pci_dev structure
1684  * @num_vfs: number of VFs to allocate
1685  *
1686  * Enable or change the number of VFs. Called when the user updates the number
1687  * of VFs in sysfs.
1688  **/
1689 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1690 {
1691 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1692 	int ret = 0;
1693 
1694 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
1695 		dev_warn(&pdev->dev, "Unable to configure VFs, other operation is pending.\n");
1696 		return -EAGAIN;
1697 	}
1698 
1699 	if (num_vfs) {
1700 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1701 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1702 			i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
1703 		}
1704 		ret = i40e_pci_sriov_enable(pdev, num_vfs);
1705 		goto sriov_configure_out;
1706 	}
1707 
1708 	if (!pci_vfs_assigned(pf->pdev)) {
1709 		i40e_free_vfs(pf);
1710 		pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1711 		i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
1712 	} else {
1713 		dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1714 		ret = -EINVAL;
1715 		goto sriov_configure_out;
1716 	}
1717 sriov_configure_out:
1718 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
1719 	return ret;
1720 }
1721 
1722 /***********************virtual channel routines******************/
1723 
1724 /**
1725  * i40e_vc_send_msg_to_vf
1726  * @vf: pointer to the VF info
1727  * @v_opcode: virtual channel opcode
1728  * @v_retval: virtual channel return value
1729  * @msg: pointer to the msg buffer
1730  * @msglen: msg length
1731  *
1732  * send msg to VF
1733  **/
1734 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1735 				  u32 v_retval, u8 *msg, u16 msglen)
1736 {
1737 	struct i40e_pf *pf;
1738 	struct i40e_hw *hw;
1739 	int abs_vf_id;
1740 	i40e_status aq_ret;
1741 
1742 	/* validate the request */
1743 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1744 		return -EINVAL;
1745 
1746 	pf = vf->pf;
1747 	hw = &pf->hw;
1748 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1749 
1750 	/* single place to detect unsuccessful return values */
1751 	if (v_retval) {
1752 		vf->num_invalid_msgs++;
1753 		dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1754 			 vf->vf_id, v_opcode, v_retval);
1755 		if (vf->num_invalid_msgs >
1756 		    I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1757 			dev_err(&pf->pdev->dev,
1758 				"Number of invalid messages exceeded for VF %d\n",
1759 				vf->vf_id);
1760 			dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1761 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
1762 		}
1763 	} else {
1764 		vf->num_valid_msgs++;
1765 		/* reset the invalid counter, if a valid message is received. */
1766 		vf->num_invalid_msgs = 0;
1767 	}
1768 
1769 	aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,	v_opcode, v_retval,
1770 					msg, msglen, NULL);
1771 	if (aq_ret) {
1772 		dev_info(&pf->pdev->dev,
1773 			 "Unable to send the message to VF %d aq_err %d\n",
1774 			 vf->vf_id, pf->hw.aq.asq_last_status);
1775 		return -EIO;
1776 	}
1777 
1778 	return 0;
1779 }
1780 
1781 /**
1782  * i40e_vc_send_resp_to_vf
1783  * @vf: pointer to the VF info
1784  * @opcode: operation code
1785  * @retval: return value
1786  *
1787  * send resp msg to VF
1788  **/
1789 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1790 				   enum virtchnl_ops opcode,
1791 				   i40e_status retval)
1792 {
1793 	return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1794 }
1795 
1796 /**
1797  * i40e_vc_get_version_msg
1798  * @vf: pointer to the VF info
1799  * @msg: pointer to the msg buffer
1800  *
1801  * called from the VF to request the API version used by the PF
1802  **/
1803 static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
1804 {
1805 	struct virtchnl_version_info info = {
1806 		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
1807 	};
1808 
1809 	vf->vf_ver = *(struct virtchnl_version_info *)msg;
1810 	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1811 	if (VF_IS_V10(&vf->vf_ver))
1812 		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1813 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
1814 				      I40E_SUCCESS, (u8 *)&info,
1815 				      sizeof(struct virtchnl_version_info));
1816 }
1817 
1818 /**
1819  * i40e_del_qch - delete all the additional VSIs created as a part of ADq
1820  * @vf: pointer to VF structure
1821  **/
1822 static void i40e_del_qch(struct i40e_vf *vf)
1823 {
1824 	struct i40e_pf *pf = vf->pf;
1825 	int i;
1826 
1827 	/* first element in the array belongs to primary VF VSI and we shouldn't
1828 	 * delete it. We should however delete the rest of the VSIs created
1829 	 */
1830 	for (i = 1; i < vf->num_tc; i++) {
1831 		if (vf->ch[i].vsi_idx) {
1832 			i40e_vsi_release(pf->vsi[vf->ch[i].vsi_idx]);
1833 			vf->ch[i].vsi_idx = 0;
1834 			vf->ch[i].vsi_id = 0;
1835 		}
1836 	}
1837 }
1838 
1839 /**
1840  * i40e_vc_get_vf_resources_msg
1841  * @vf: pointer to the VF info
1842  * @msg: pointer to the msg buffer
1843  *
1844  * called from the VF to request its resources
1845  **/
1846 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
1847 {
1848 	struct virtchnl_vf_resource *vfres = NULL;
1849 	struct i40e_pf *pf = vf->pf;
1850 	i40e_status aq_ret = 0;
1851 	struct i40e_vsi *vsi;
1852 	int num_vsis = 1;
1853 	size_t len = 0;
1854 	int ret;
1855 
1856 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
1857 		aq_ret = I40E_ERR_PARAM;
1858 		goto err;
1859 	}
1860 
1861 	len = struct_size(vfres, vsi_res, num_vsis);
1862 	vfres = kzalloc(len, GFP_KERNEL);
1863 	if (!vfres) {
1864 		aq_ret = I40E_ERR_NO_MEMORY;
1865 		len = 0;
1866 		goto err;
1867 	}
1868 	if (VF_IS_V11(&vf->vf_ver))
1869 		vf->driver_caps = *(u32 *)msg;
1870 	else
1871 		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
1872 				  VIRTCHNL_VF_OFFLOAD_RSS_REG |
1873 				  VIRTCHNL_VF_OFFLOAD_VLAN;
1874 
1875 	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
1876 	vsi = pf->vsi[vf->lan_vsi_idx];
1877 	if (!vsi->info.pvid)
1878 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
1879 
1880 	if (i40e_vf_client_capable(pf, vf->vf_id) &&
1881 	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
1882 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
1883 		set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1884 	} else {
1885 		clear_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
1886 	}
1887 
1888 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1889 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
1890 	} else {
1891 		if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
1892 		    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
1893 			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1894 		else
1895 			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
1896 	}
1897 
1898 	if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
1899 		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1900 			vfres->vf_cap_flags |=
1901 				VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1902 	}
1903 
1904 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
1905 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
1906 
1907 	if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
1908 	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
1909 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
1910 
1911 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
1912 		if (pf->flags & I40E_FLAG_MFP_ENABLED) {
1913 			dev_err(&pf->pdev->dev,
1914 				"VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
1915 				 vf->vf_id);
1916 			aq_ret = I40E_ERR_PARAM;
1917 			goto err;
1918 		}
1919 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1920 	}
1921 
1922 	if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
1923 		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1924 			vfres->vf_cap_flags |=
1925 					VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1926 	}
1927 
1928 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
1929 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
1930 
1931 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)
1932 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADQ;
1933 
1934 	vfres->num_vsis = num_vsis;
1935 	vfres->num_queue_pairs = vf->num_queue_pairs;
1936 	vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1937 	vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
1938 	vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
1939 
1940 	if (vf->lan_vsi_idx) {
1941 		vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
1942 		vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
1943 		vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
1944 		/* VFs only use TC 0 */
1945 		vfres->vsi_res[0].qset_handle
1946 					  = le16_to_cpu(vsi->info.qs_handle[0]);
1947 		ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1948 				vf->default_lan_addr.addr);
1949 	}
1950 	set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
1951 
1952 err:
1953 	/* send the response back to the VF */
1954 	ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
1955 				     aq_ret, (u8 *)vfres, len);
1956 
1957 	kfree(vfres);
1958 	return ret;
1959 }
1960 
1961 /**
1962  * i40e_vc_reset_vf_msg
1963  * @vf: pointer to the VF info
1964  *
1965  * called from the VF to reset itself,
1966  * unlike other virtchnl messages, PF driver
1967  * doesn't send the response back to the VF
1968  **/
1969 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1970 {
1971 	if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
1972 		i40e_reset_vf(vf, false);
1973 }
1974 
1975 /**
1976  * i40e_getnum_vf_vsi_vlan_filters
1977  * @vsi: pointer to the vsi
1978  *
1979  * called to get the number of VLANs offloaded on this VF
1980  **/
1981 static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
1982 {
1983 	struct i40e_mac_filter *f;
1984 	int num_vlans = 0, bkt;
1985 
1986 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1987 		if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
1988 			num_vlans++;
1989 	}
1990 
1991 	return num_vlans;
1992 }
1993 
1994 /**
1995  * i40e_vc_config_promiscuous_mode_msg
1996  * @vf: pointer to the VF info
1997  * @msg: pointer to the msg buffer
1998  *
1999  * called from the VF to configure the promiscuous mode of
2000  * VF vsis
2001  **/
2002 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf, u8 *msg)
2003 {
2004 	struct virtchnl_promisc_info *info =
2005 	    (struct virtchnl_promisc_info *)msg;
2006 	struct i40e_pf *pf = vf->pf;
2007 	i40e_status aq_ret = 0;
2008 	bool allmulti = false;
2009 	bool alluni = false;
2010 
2011 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2012 		aq_ret = I40E_ERR_PARAM;
2013 		goto err_out;
2014 	}
2015 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2016 		dev_err(&pf->pdev->dev,
2017 			"Unprivileged VF %d is attempting to configure promiscuous mode\n",
2018 			vf->vf_id);
2019 
2020 		/* Lie to the VF on purpose, because this is an error we can
2021 		 * ignore. Unprivileged VF is not a virtual channel error.
2022 		 */
2023 		aq_ret = 0;
2024 		goto err_out;
2025 	}
2026 
2027 	if (info->flags > I40E_MAX_VF_PROMISC_FLAGS) {
2028 		aq_ret = I40E_ERR_PARAM;
2029 		goto err_out;
2030 	}
2031 
2032 	if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
2033 		aq_ret = I40E_ERR_PARAM;
2034 		goto err_out;
2035 	}
2036 
2037 	/* Multicast promiscuous handling*/
2038 	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
2039 		allmulti = true;
2040 
2041 	if (info->flags & FLAG_VF_UNICAST_PROMISC)
2042 		alluni = true;
2043 	aq_ret = i40e_config_vf_promiscuous_mode(vf, info->vsi_id, allmulti,
2044 						 alluni);
2045 	if (aq_ret)
2046 		goto err_out;
2047 
2048 	if (allmulti) {
2049 		if (!test_and_set_bit(I40E_VF_STATE_MC_PROMISC,
2050 				      &vf->vf_states))
2051 			dev_info(&pf->pdev->dev,
2052 				 "VF %d successfully set multicast promiscuous mode\n",
2053 				 vf->vf_id);
2054 	} else if (test_and_clear_bit(I40E_VF_STATE_MC_PROMISC,
2055 				      &vf->vf_states))
2056 		dev_info(&pf->pdev->dev,
2057 			 "VF %d successfully unset multicast promiscuous mode\n",
2058 			 vf->vf_id);
2059 
2060 	if (alluni) {
2061 		if (!test_and_set_bit(I40E_VF_STATE_UC_PROMISC,
2062 				      &vf->vf_states))
2063 			dev_info(&pf->pdev->dev,
2064 				 "VF %d successfully set unicast promiscuous mode\n",
2065 				 vf->vf_id);
2066 	} else if (test_and_clear_bit(I40E_VF_STATE_UC_PROMISC,
2067 				      &vf->vf_states))
2068 		dev_info(&pf->pdev->dev,
2069 			 "VF %d successfully unset unicast promiscuous mode\n",
2070 			 vf->vf_id);
2071 
2072 err_out:
2073 	/* send the response to the VF */
2074 	return i40e_vc_send_resp_to_vf(vf,
2075 				       VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
2076 				       aq_ret);
2077 }
2078 
2079 /**
2080  * i40e_vc_config_queues_msg
2081  * @vf: pointer to the VF info
2082  * @msg: pointer to the msg buffer
2083  *
2084  * called from the VF to configure the rx/tx
2085  * queues
2086  **/
2087 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg)
2088 {
2089 	struct virtchnl_vsi_queue_config_info *qci =
2090 	    (struct virtchnl_vsi_queue_config_info *)msg;
2091 	struct virtchnl_queue_pair_info *qpi;
2092 	struct i40e_pf *pf = vf->pf;
2093 	u16 vsi_id, vsi_queue_id = 0;
2094 	u16 num_qps_all = 0;
2095 	i40e_status aq_ret = 0;
2096 	int i, j = 0, idx = 0;
2097 
2098 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2099 		aq_ret = I40E_ERR_PARAM;
2100 		goto error_param;
2101 	}
2102 
2103 	if (!i40e_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
2104 		aq_ret = I40E_ERR_PARAM;
2105 		goto error_param;
2106 	}
2107 
2108 	if (qci->num_queue_pairs > I40E_MAX_VF_QUEUES) {
2109 		aq_ret = I40E_ERR_PARAM;
2110 		goto error_param;
2111 	}
2112 
2113 	if (vf->adq_enabled) {
2114 		for (i = 0; i < I40E_MAX_VF_VSI; i++)
2115 			num_qps_all += vf->ch[i].num_qps;
2116 		if (num_qps_all != qci->num_queue_pairs) {
2117 			aq_ret = I40E_ERR_PARAM;
2118 			goto error_param;
2119 		}
2120 	}
2121 
2122 	vsi_id = qci->vsi_id;
2123 
2124 	for (i = 0; i < qci->num_queue_pairs; i++) {
2125 		qpi = &qci->qpair[i];
2126 
2127 		if (!vf->adq_enabled) {
2128 			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
2129 						      qpi->txq.queue_id)) {
2130 				aq_ret = I40E_ERR_PARAM;
2131 				goto error_param;
2132 			}
2133 
2134 			vsi_queue_id = qpi->txq.queue_id;
2135 
2136 			if (qpi->txq.vsi_id != qci->vsi_id ||
2137 			    qpi->rxq.vsi_id != qci->vsi_id ||
2138 			    qpi->rxq.queue_id != vsi_queue_id) {
2139 				aq_ret = I40E_ERR_PARAM;
2140 				goto error_param;
2141 			}
2142 		}
2143 
2144 		if (vf->adq_enabled) {
2145 			if (idx >= ARRAY_SIZE(vf->ch)) {
2146 				aq_ret = I40E_ERR_NO_AVAILABLE_VSI;
2147 				goto error_param;
2148 			}
2149 			vsi_id = vf->ch[idx].vsi_id;
2150 		}
2151 
2152 		if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
2153 					     &qpi->rxq) ||
2154 		    i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
2155 					     &qpi->txq)) {
2156 			aq_ret = I40E_ERR_PARAM;
2157 			goto error_param;
2158 		}
2159 
2160 		/* For ADq there can be up to 4 VSIs with max 4 queues each.
2161 		 * VF does not know about these additional VSIs and all
2162 		 * it cares is about its own queues. PF configures these queues
2163 		 * to its appropriate VSIs based on TC mapping
2164 		 */
2165 		if (vf->adq_enabled) {
2166 			if (idx >= ARRAY_SIZE(vf->ch)) {
2167 				aq_ret = I40E_ERR_NO_AVAILABLE_VSI;
2168 				goto error_param;
2169 			}
2170 			if (j == (vf->ch[idx].num_qps - 1)) {
2171 				idx++;
2172 				j = 0; /* resetting the queue count */
2173 				vsi_queue_id = 0;
2174 			} else {
2175 				j++;
2176 				vsi_queue_id++;
2177 			}
2178 		}
2179 	}
2180 	/* set vsi num_queue_pairs in use to num configured by VF */
2181 	if (!vf->adq_enabled) {
2182 		pf->vsi[vf->lan_vsi_idx]->num_queue_pairs =
2183 			qci->num_queue_pairs;
2184 	} else {
2185 		for (i = 0; i < vf->num_tc; i++)
2186 			pf->vsi[vf->ch[i].vsi_idx]->num_queue_pairs =
2187 			       vf->ch[i].num_qps;
2188 	}
2189 
2190 error_param:
2191 	/* send the response to the VF */
2192 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2193 				       aq_ret);
2194 }
2195 
2196 /**
2197  * i40e_validate_queue_map
2198  * @vsi_id: vsi id
2199  * @queuemap: Tx or Rx queue map
2200  *
2201  * check if Tx or Rx queue map is valid
2202  **/
2203 static int i40e_validate_queue_map(struct i40e_vf *vf, u16 vsi_id,
2204 				   unsigned long queuemap)
2205 {
2206 	u16 vsi_queue_id, queue_id;
2207 
2208 	for_each_set_bit(vsi_queue_id, &queuemap, I40E_MAX_VSI_QP) {
2209 		if (vf->adq_enabled) {
2210 			vsi_id = vf->ch[vsi_queue_id / I40E_MAX_VF_VSI].vsi_id;
2211 			queue_id = (vsi_queue_id % I40E_DEFAULT_QUEUES_PER_VF);
2212 		} else {
2213 			queue_id = vsi_queue_id;
2214 		}
2215 
2216 		if (!i40e_vc_isvalid_queue_id(vf, vsi_id, queue_id))
2217 			return -EINVAL;
2218 	}
2219 
2220 	return 0;
2221 }
2222 
2223 /**
2224  * i40e_vc_config_irq_map_msg
2225  * @vf: pointer to the VF info
2226  * @msg: pointer to the msg buffer
2227  *
2228  * called from the VF to configure the irq to
2229  * queue map
2230  **/
2231 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg)
2232 {
2233 	struct virtchnl_irq_map_info *irqmap_info =
2234 	    (struct virtchnl_irq_map_info *)msg;
2235 	struct virtchnl_vector_map *map;
2236 	u16 vsi_id;
2237 	i40e_status aq_ret = 0;
2238 	int i;
2239 
2240 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2241 		aq_ret = I40E_ERR_PARAM;
2242 		goto error_param;
2243 	}
2244 
2245 	if (irqmap_info->num_vectors >
2246 	    vf->pf->hw.func_caps.num_msix_vectors_vf) {
2247 		aq_ret = I40E_ERR_PARAM;
2248 		goto error_param;
2249 	}
2250 
2251 	for (i = 0; i < irqmap_info->num_vectors; i++) {
2252 		map = &irqmap_info->vecmap[i];
2253 		/* validate msg params */
2254 		if (!i40e_vc_isvalid_vector_id(vf, map->vector_id) ||
2255 		    !i40e_vc_isvalid_vsi_id(vf, map->vsi_id)) {
2256 			aq_ret = I40E_ERR_PARAM;
2257 			goto error_param;
2258 		}
2259 		vsi_id = map->vsi_id;
2260 
2261 		if (i40e_validate_queue_map(vf, vsi_id, map->rxq_map)) {
2262 			aq_ret = I40E_ERR_PARAM;
2263 			goto error_param;
2264 		}
2265 
2266 		if (i40e_validate_queue_map(vf, vsi_id, map->txq_map)) {
2267 			aq_ret = I40E_ERR_PARAM;
2268 			goto error_param;
2269 		}
2270 
2271 		i40e_config_irq_link_list(vf, vsi_id, map);
2272 	}
2273 error_param:
2274 	/* send the response to the VF */
2275 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
2276 				       aq_ret);
2277 }
2278 
2279 /**
2280  * i40e_ctrl_vf_tx_rings
2281  * @vsi: the SRIOV VSI being configured
2282  * @q_map: bit map of the queues to be enabled
2283  * @enable: start or stop the queue
2284  **/
2285 static int i40e_ctrl_vf_tx_rings(struct i40e_vsi *vsi, unsigned long q_map,
2286 				 bool enable)
2287 {
2288 	struct i40e_pf *pf = vsi->back;
2289 	int ret = 0;
2290 	u16 q_id;
2291 
2292 	for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
2293 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
2294 					     vsi->base_queue + q_id,
2295 					     false /*is xdp*/, enable);
2296 		if (ret)
2297 			break;
2298 	}
2299 	return ret;
2300 }
2301 
2302 /**
2303  * i40e_ctrl_vf_rx_rings
2304  * @vsi: the SRIOV VSI being configured
2305  * @q_map: bit map of the queues to be enabled
2306  * @enable: start or stop the queue
2307  **/
2308 static int i40e_ctrl_vf_rx_rings(struct i40e_vsi *vsi, unsigned long q_map,
2309 				 bool enable)
2310 {
2311 	struct i40e_pf *pf = vsi->back;
2312 	int ret = 0;
2313 	u16 q_id;
2314 
2315 	for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
2316 		ret = i40e_control_wait_rx_q(pf, vsi->base_queue + q_id,
2317 					     enable);
2318 		if (ret)
2319 			break;
2320 	}
2321 	return ret;
2322 }
2323 
2324 /**
2325  * i40e_vc_enable_queues_msg
2326  * @vf: pointer to the VF info
2327  * @msg: pointer to the msg buffer
2328  *
2329  * called from the VF to enable all or specific queue(s)
2330  **/
2331 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg)
2332 {
2333 	struct virtchnl_queue_select *vqs =
2334 	    (struct virtchnl_queue_select *)msg;
2335 	struct i40e_pf *pf = vf->pf;
2336 	i40e_status aq_ret = 0;
2337 	int i;
2338 
2339 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2340 		aq_ret = I40E_ERR_PARAM;
2341 		goto error_param;
2342 	}
2343 
2344 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2345 		aq_ret = I40E_ERR_PARAM;
2346 		goto error_param;
2347 	}
2348 
2349 	if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
2350 		aq_ret = I40E_ERR_PARAM;
2351 		goto error_param;
2352 	}
2353 
2354 	/* Use the queue bit map sent by the VF */
2355 	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2356 				  true)) {
2357 		aq_ret = I40E_ERR_TIMEOUT;
2358 		goto error_param;
2359 	}
2360 	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2361 				  true)) {
2362 		aq_ret = I40E_ERR_TIMEOUT;
2363 		goto error_param;
2364 	}
2365 
2366 	/* need to start the rings for additional ADq VSI's as well */
2367 	if (vf->adq_enabled) {
2368 		/* zero belongs to LAN VSI */
2369 		for (i = 1; i < vf->num_tc; i++) {
2370 			if (i40e_vsi_start_rings(pf->vsi[vf->ch[i].vsi_idx]))
2371 				aq_ret = I40E_ERR_TIMEOUT;
2372 		}
2373 	}
2374 
2375 	vf->queues_enabled = true;
2376 
2377 error_param:
2378 	/* send the response to the VF */
2379 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
2380 				       aq_ret);
2381 }
2382 
2383 /**
2384  * i40e_vc_disable_queues_msg
2385  * @vf: pointer to the VF info
2386  * @msg: pointer to the msg buffer
2387  *
2388  * called from the VF to disable all or specific
2389  * queue(s)
2390  **/
2391 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg)
2392 {
2393 	struct virtchnl_queue_select *vqs =
2394 	    (struct virtchnl_queue_select *)msg;
2395 	struct i40e_pf *pf = vf->pf;
2396 	i40e_status aq_ret = 0;
2397 
2398 	/* Immediately mark queues as disabled */
2399 	vf->queues_enabled = false;
2400 
2401 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2402 		aq_ret = I40E_ERR_PARAM;
2403 		goto error_param;
2404 	}
2405 
2406 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2407 		aq_ret = I40E_ERR_PARAM;
2408 		goto error_param;
2409 	}
2410 
2411 	if ((vqs->rx_queues == 0 && vqs->tx_queues == 0) ||
2412 	    vqs->rx_queues > I40E_MAX_VF_QUEUES ||
2413 	    vqs->tx_queues > I40E_MAX_VF_QUEUES) {
2414 		aq_ret = I40E_ERR_PARAM;
2415 		goto error_param;
2416 	}
2417 
2418 	/* Use the queue bit map sent by the VF */
2419 	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2420 				  false)) {
2421 		aq_ret = I40E_ERR_TIMEOUT;
2422 		goto error_param;
2423 	}
2424 	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2425 				  false)) {
2426 		aq_ret = I40E_ERR_TIMEOUT;
2427 		goto error_param;
2428 	}
2429 error_param:
2430 	/* send the response to the VF */
2431 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
2432 				       aq_ret);
2433 }
2434 
2435 /**
2436  * i40e_vc_request_queues_msg
2437  * @vf: pointer to the VF info
2438  * @msg: pointer to the msg buffer
2439  *
2440  * VFs get a default number of queues but can use this message to request a
2441  * different number.  If the request is successful, PF will reset the VF and
2442  * return 0.  If unsuccessful, PF will send message informing VF of number of
2443  * available queues and return result of sending VF a message.
2444  **/
2445 static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg)
2446 {
2447 	struct virtchnl_vf_res_request *vfres =
2448 		(struct virtchnl_vf_res_request *)msg;
2449 	u16 req_pairs = vfres->num_queue_pairs;
2450 	u8 cur_pairs = vf->num_queue_pairs;
2451 	struct i40e_pf *pf = vf->pf;
2452 
2453 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
2454 		return -EINVAL;
2455 
2456 	if (req_pairs > I40E_MAX_VF_QUEUES) {
2457 		dev_err(&pf->pdev->dev,
2458 			"VF %d tried to request more than %d queues.\n",
2459 			vf->vf_id,
2460 			I40E_MAX_VF_QUEUES);
2461 		vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
2462 	} else if (req_pairs - cur_pairs > pf->queues_left) {
2463 		dev_warn(&pf->pdev->dev,
2464 			 "VF %d requested %d more queues, but only %d left.\n",
2465 			 vf->vf_id,
2466 			 req_pairs - cur_pairs,
2467 			 pf->queues_left);
2468 		vfres->num_queue_pairs = pf->queues_left + cur_pairs;
2469 	} else {
2470 		/* successful request */
2471 		vf->num_req_queues = req_pairs;
2472 		i40e_vc_notify_vf_reset(vf);
2473 		i40e_reset_vf(vf, false);
2474 		return 0;
2475 	}
2476 
2477 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
2478 				      (u8 *)vfres, sizeof(*vfres));
2479 }
2480 
2481 /**
2482  * i40e_vc_get_stats_msg
2483  * @vf: pointer to the VF info
2484  * @msg: pointer to the msg buffer
2485  *
2486  * called from the VF to get vsi stats
2487  **/
2488 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg)
2489 {
2490 	struct virtchnl_queue_select *vqs =
2491 	    (struct virtchnl_queue_select *)msg;
2492 	struct i40e_pf *pf = vf->pf;
2493 	struct i40e_eth_stats stats;
2494 	i40e_status aq_ret = 0;
2495 	struct i40e_vsi *vsi;
2496 
2497 	memset(&stats, 0, sizeof(struct i40e_eth_stats));
2498 
2499 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2500 		aq_ret = I40E_ERR_PARAM;
2501 		goto error_param;
2502 	}
2503 
2504 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2505 		aq_ret = I40E_ERR_PARAM;
2506 		goto error_param;
2507 	}
2508 
2509 	vsi = pf->vsi[vf->lan_vsi_idx];
2510 	if (!vsi) {
2511 		aq_ret = I40E_ERR_PARAM;
2512 		goto error_param;
2513 	}
2514 	i40e_update_eth_stats(vsi);
2515 	stats = vsi->eth_stats;
2516 
2517 error_param:
2518 	/* send the response back to the VF */
2519 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
2520 				      (u8 *)&stats, sizeof(stats));
2521 }
2522 
2523 /* If the VF is not trusted restrict the number of MAC/VLAN it can program
2524  * MAC filters: 16 for multicast, 1 for MAC, 1 for broadcast
2525  */
2526 #define I40E_VC_MAX_MAC_ADDR_PER_VF (16 + 1 + 1)
2527 #define I40E_VC_MAX_VLAN_PER_VF 16
2528 
2529 /**
2530  * i40e_check_vf_permission
2531  * @vf: pointer to the VF info
2532  * @al: MAC address list from virtchnl
2533  *
2534  * Check that the given list of MAC addresses is allowed. Will return -EPERM
2535  * if any address in the list is not valid. Checks the following conditions:
2536  *
2537  * 1) broadcast and zero addresses are never valid
2538  * 2) unicast addresses are not allowed if the VMM has administratively set
2539  *    the VF MAC address, unless the VF is marked as privileged.
2540  * 3) There is enough space to add all the addresses.
2541  *
2542  * Note that to guarantee consistency, it is expected this function be called
2543  * while holding the mac_filter_hash_lock, as otherwise the current number of
2544  * addresses might not be accurate.
2545  **/
2546 static inline int i40e_check_vf_permission(struct i40e_vf *vf,
2547 					   struct virtchnl_ether_addr_list *al)
2548 {
2549 	struct i40e_pf *pf = vf->pf;
2550 	struct i40e_vsi *vsi = pf->vsi[vf->lan_vsi_idx];
2551 	int mac2add_cnt = 0;
2552 	int i;
2553 
2554 	for (i = 0; i < al->num_elements; i++) {
2555 		struct i40e_mac_filter *f;
2556 		u8 *addr = al->list[i].addr;
2557 
2558 		if (is_broadcast_ether_addr(addr) ||
2559 		    is_zero_ether_addr(addr)) {
2560 			dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
2561 				addr);
2562 			return I40E_ERR_INVALID_MAC_ADDR;
2563 		}
2564 
2565 		/* If the host VMM administrator has set the VF MAC address
2566 		 * administratively via the ndo_set_vf_mac command then deny
2567 		 * permission to the VF to add or delete unicast MAC addresses.
2568 		 * Unless the VF is privileged and then it can do whatever.
2569 		 * The VF may request to set the MAC address filter already
2570 		 * assigned to it so do not return an error in that case.
2571 		 */
2572 		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2573 		    !is_multicast_ether_addr(addr) && vf->pf_set_mac &&
2574 		    !ether_addr_equal(addr, vf->default_lan_addr.addr)) {
2575 			dev_err(&pf->pdev->dev,
2576 				"VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
2577 			return -EPERM;
2578 		}
2579 
2580 		/*count filters that really will be added*/
2581 		f = i40e_find_mac(vsi, addr);
2582 		if (!f)
2583 			++mac2add_cnt;
2584 	}
2585 
2586 	/* If this VF is not privileged, then we can't add more than a limited
2587 	 * number of addresses. Check to make sure that the additions do not
2588 	 * push us over the limit.
2589 	 */
2590 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2591 	    (i40e_count_filters(vsi) + mac2add_cnt) >
2592 		    I40E_VC_MAX_MAC_ADDR_PER_VF) {
2593 		dev_err(&pf->pdev->dev,
2594 			"Cannot add more MAC addresses, VF is not trusted, switch the VF to trusted to add more functionality\n");
2595 		return -EPERM;
2596 	}
2597 	return 0;
2598 }
2599 
2600 /**
2601  * i40e_vc_add_mac_addr_msg
2602  * @vf: pointer to the VF info
2603  * @msg: pointer to the msg buffer
2604  *
2605  * add guest mac address filter
2606  **/
2607 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
2608 {
2609 	struct virtchnl_ether_addr_list *al =
2610 	    (struct virtchnl_ether_addr_list *)msg;
2611 	struct i40e_pf *pf = vf->pf;
2612 	struct i40e_vsi *vsi = NULL;
2613 	i40e_status ret = 0;
2614 	int i;
2615 
2616 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2617 	    !i40e_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2618 		ret = I40E_ERR_PARAM;
2619 		goto error_param;
2620 	}
2621 
2622 	vsi = pf->vsi[vf->lan_vsi_idx];
2623 
2624 	/* Lock once, because all function inside for loop accesses VSI's
2625 	 * MAC filter list which needs to be protected using same lock.
2626 	 */
2627 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2628 
2629 	ret = i40e_check_vf_permission(vf, al);
2630 	if (ret) {
2631 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2632 		goto error_param;
2633 	}
2634 
2635 	/* add new addresses to the list */
2636 	for (i = 0; i < al->num_elements; i++) {
2637 		struct i40e_mac_filter *f;
2638 
2639 		f = i40e_find_mac(vsi, al->list[i].addr);
2640 		if (!f) {
2641 			f = i40e_add_mac_filter(vsi, al->list[i].addr);
2642 
2643 			if (!f) {
2644 				dev_err(&pf->pdev->dev,
2645 					"Unable to add MAC filter %pM for VF %d\n",
2646 					al->list[i].addr, vf->vf_id);
2647 				ret = I40E_ERR_PARAM;
2648 				spin_unlock_bh(&vsi->mac_filter_hash_lock);
2649 				goto error_param;
2650 			}
2651 		}
2652 	}
2653 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2654 
2655 	/* program the updated filter list */
2656 	ret = i40e_sync_vsi_filters(vsi);
2657 	if (ret)
2658 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2659 			vf->vf_id, ret);
2660 
2661 error_param:
2662 	/* send the response to the VF */
2663 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
2664 				       ret);
2665 }
2666 
2667 /**
2668  * i40e_vc_del_mac_addr_msg
2669  * @vf: pointer to the VF info
2670  * @msg: pointer to the msg buffer
2671  *
2672  * remove guest mac address filter
2673  **/
2674 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
2675 {
2676 	struct virtchnl_ether_addr_list *al =
2677 	    (struct virtchnl_ether_addr_list *)msg;
2678 	struct i40e_pf *pf = vf->pf;
2679 	struct i40e_vsi *vsi = NULL;
2680 	i40e_status ret = 0;
2681 	int i;
2682 
2683 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2684 	    !i40e_vc_isvalid_vsi_id(vf, al->vsi_id)) {
2685 		ret = I40E_ERR_PARAM;
2686 		goto error_param;
2687 	}
2688 
2689 	for (i = 0; i < al->num_elements; i++) {
2690 		if (is_broadcast_ether_addr(al->list[i].addr) ||
2691 		    is_zero_ether_addr(al->list[i].addr)) {
2692 			dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
2693 				al->list[i].addr, vf->vf_id);
2694 			ret = I40E_ERR_INVALID_MAC_ADDR;
2695 			goto error_param;
2696 		}
2697 	}
2698 	vsi = pf->vsi[vf->lan_vsi_idx];
2699 
2700 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2701 	/* delete addresses from the list */
2702 	for (i = 0; i < al->num_elements; i++)
2703 		if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
2704 			ret = I40E_ERR_INVALID_MAC_ADDR;
2705 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
2706 			goto error_param;
2707 		}
2708 
2709 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2710 
2711 	/* program the updated filter list */
2712 	ret = i40e_sync_vsi_filters(vsi);
2713 	if (ret)
2714 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2715 			vf->vf_id, ret);
2716 
2717 error_param:
2718 	/* send the response to the VF */
2719 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
2720 				       ret);
2721 }
2722 
2723 /**
2724  * i40e_vc_add_vlan_msg
2725  * @vf: pointer to the VF info
2726  * @msg: pointer to the msg buffer
2727  *
2728  * program guest vlan id
2729  **/
2730 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg)
2731 {
2732 	struct virtchnl_vlan_filter_list *vfl =
2733 	    (struct virtchnl_vlan_filter_list *)msg;
2734 	struct i40e_pf *pf = vf->pf;
2735 	struct i40e_vsi *vsi = NULL;
2736 	i40e_status aq_ret = 0;
2737 	int i;
2738 
2739 	if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
2740 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2741 		dev_err(&pf->pdev->dev,
2742 			"VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
2743 		goto error_param;
2744 	}
2745 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2746 	    !i40e_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2747 		aq_ret = I40E_ERR_PARAM;
2748 		goto error_param;
2749 	}
2750 
2751 	for (i = 0; i < vfl->num_elements; i++) {
2752 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2753 			aq_ret = I40E_ERR_PARAM;
2754 			dev_err(&pf->pdev->dev,
2755 				"invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2756 			goto error_param;
2757 		}
2758 	}
2759 	vsi = pf->vsi[vf->lan_vsi_idx];
2760 	if (vsi->info.pvid) {
2761 		aq_ret = I40E_ERR_PARAM;
2762 		goto error_param;
2763 	}
2764 
2765 	i40e_vlan_stripping_enable(vsi);
2766 	for (i = 0; i < vfl->num_elements; i++) {
2767 		/* add new VLAN filter */
2768 		int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
2769 		if (!ret)
2770 			vf->num_vlan++;
2771 
2772 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2773 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2774 							   true,
2775 							   vfl->vlan_id[i],
2776 							   NULL);
2777 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2778 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2779 							   true,
2780 							   vfl->vlan_id[i],
2781 							   NULL);
2782 
2783 		if (ret)
2784 			dev_err(&pf->pdev->dev,
2785 				"Unable to add VLAN filter %d for VF %d, error %d\n",
2786 				vfl->vlan_id[i], vf->vf_id, ret);
2787 	}
2788 
2789 error_param:
2790 	/* send the response to the VF */
2791 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
2792 }
2793 
2794 /**
2795  * i40e_vc_remove_vlan_msg
2796  * @vf: pointer to the VF info
2797  * @msg: pointer to the msg buffer
2798  *
2799  * remove programmed guest vlan id
2800  **/
2801 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg)
2802 {
2803 	struct virtchnl_vlan_filter_list *vfl =
2804 	    (struct virtchnl_vlan_filter_list *)msg;
2805 	struct i40e_pf *pf = vf->pf;
2806 	struct i40e_vsi *vsi = NULL;
2807 	i40e_status aq_ret = 0;
2808 	int i;
2809 
2810 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2811 	    !i40e_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
2812 		aq_ret = I40E_ERR_PARAM;
2813 		goto error_param;
2814 	}
2815 
2816 	for (i = 0; i < vfl->num_elements; i++) {
2817 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2818 			aq_ret = I40E_ERR_PARAM;
2819 			goto error_param;
2820 		}
2821 	}
2822 
2823 	vsi = pf->vsi[vf->lan_vsi_idx];
2824 	if (vsi->info.pvid) {
2825 		if (vfl->num_elements > 1 || vfl->vlan_id[0])
2826 			aq_ret = I40E_ERR_PARAM;
2827 		goto error_param;
2828 	}
2829 
2830 	for (i = 0; i < vfl->num_elements; i++) {
2831 		i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
2832 		vf->num_vlan--;
2833 
2834 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
2835 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2836 							   false,
2837 							   vfl->vlan_id[i],
2838 							   NULL);
2839 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
2840 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2841 							   false,
2842 							   vfl->vlan_id[i],
2843 							   NULL);
2844 	}
2845 
2846 error_param:
2847 	/* send the response to the VF */
2848 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
2849 }
2850 
2851 /**
2852  * i40e_vc_iwarp_msg
2853  * @vf: pointer to the VF info
2854  * @msg: pointer to the msg buffer
2855  * @msglen: msg length
2856  *
2857  * called from the VF for the iwarp msgs
2858  **/
2859 static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2860 {
2861 	struct i40e_pf *pf = vf->pf;
2862 	int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
2863 	i40e_status aq_ret = 0;
2864 
2865 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2866 	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2867 		aq_ret = I40E_ERR_PARAM;
2868 		goto error_param;
2869 	}
2870 
2871 	i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
2872 				     msg, msglen);
2873 
2874 error_param:
2875 	/* send the response to the VF */
2876 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
2877 				       aq_ret);
2878 }
2879 
2880 /**
2881  * i40e_vc_iwarp_qvmap_msg
2882  * @vf: pointer to the VF info
2883  * @msg: pointer to the msg buffer
2884  * @config: config qvmap or release it
2885  *
2886  * called from the VF for the iwarp msgs
2887  **/
2888 static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, bool config)
2889 {
2890 	struct virtchnl_iwarp_qvlist_info *qvlist_info =
2891 				(struct virtchnl_iwarp_qvlist_info *)msg;
2892 	i40e_status aq_ret = 0;
2893 
2894 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2895 	    !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
2896 		aq_ret = I40E_ERR_PARAM;
2897 		goto error_param;
2898 	}
2899 
2900 	if (config) {
2901 		if (i40e_config_iwarp_qvlist(vf, qvlist_info))
2902 			aq_ret = I40E_ERR_PARAM;
2903 	} else {
2904 		i40e_release_iwarp_qvlist(vf);
2905 	}
2906 
2907 error_param:
2908 	/* send the response to the VF */
2909 	return i40e_vc_send_resp_to_vf(vf,
2910 			       config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
2911 			       VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
2912 			       aq_ret);
2913 }
2914 
2915 /**
2916  * i40e_vc_config_rss_key
2917  * @vf: pointer to the VF info
2918  * @msg: pointer to the msg buffer
2919  *
2920  * Configure the VF's RSS key
2921  **/
2922 static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg)
2923 {
2924 	struct virtchnl_rss_key *vrk =
2925 		(struct virtchnl_rss_key *)msg;
2926 	struct i40e_pf *pf = vf->pf;
2927 	struct i40e_vsi *vsi = NULL;
2928 	i40e_status aq_ret = 0;
2929 
2930 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2931 	    !i40e_vc_isvalid_vsi_id(vf, vrk->vsi_id) ||
2932 	    (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
2933 		aq_ret = I40E_ERR_PARAM;
2934 		goto err;
2935 	}
2936 
2937 	vsi = pf->vsi[vf->lan_vsi_idx];
2938 	aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
2939 err:
2940 	/* send the response to the VF */
2941 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
2942 				       aq_ret);
2943 }
2944 
2945 /**
2946  * i40e_vc_config_rss_lut
2947  * @vf: pointer to the VF info
2948  * @msg: pointer to the msg buffer
2949  *
2950  * Configure the VF's RSS LUT
2951  **/
2952 static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg)
2953 {
2954 	struct virtchnl_rss_lut *vrl =
2955 		(struct virtchnl_rss_lut *)msg;
2956 	struct i40e_pf *pf = vf->pf;
2957 	struct i40e_vsi *vsi = NULL;
2958 	i40e_status aq_ret = 0;
2959 	u16 i;
2960 
2961 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
2962 	    !i40e_vc_isvalid_vsi_id(vf, vrl->vsi_id) ||
2963 	    (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
2964 		aq_ret = I40E_ERR_PARAM;
2965 		goto err;
2966 	}
2967 
2968 	for (i = 0; i < vrl->lut_entries; i++)
2969 		if (vrl->lut[i] >= vf->num_queue_pairs) {
2970 			aq_ret = I40E_ERR_PARAM;
2971 			goto err;
2972 		}
2973 
2974 	vsi = pf->vsi[vf->lan_vsi_idx];
2975 	aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
2976 	/* send the response to the VF */
2977 err:
2978 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
2979 				       aq_ret);
2980 }
2981 
2982 /**
2983  * i40e_vc_get_rss_hena
2984  * @vf: pointer to the VF info
2985  * @msg: pointer to the msg buffer
2986  *
2987  * Return the RSS HENA bits allowed by the hardware
2988  **/
2989 static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg)
2990 {
2991 	struct virtchnl_rss_hena *vrh = NULL;
2992 	struct i40e_pf *pf = vf->pf;
2993 	i40e_status aq_ret = 0;
2994 	int len = 0;
2995 
2996 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2997 		aq_ret = I40E_ERR_PARAM;
2998 		goto err;
2999 	}
3000 	len = sizeof(struct virtchnl_rss_hena);
3001 
3002 	vrh = kzalloc(len, GFP_KERNEL);
3003 	if (!vrh) {
3004 		aq_ret = I40E_ERR_NO_MEMORY;
3005 		len = 0;
3006 		goto err;
3007 	}
3008 	vrh->hena = i40e_pf_get_default_rss_hena(pf);
3009 err:
3010 	/* send the response back to the VF */
3011 	aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
3012 					aq_ret, (u8 *)vrh, len);
3013 	kfree(vrh);
3014 	return aq_ret;
3015 }
3016 
3017 /**
3018  * i40e_vc_set_rss_hena
3019  * @vf: pointer to the VF info
3020  * @msg: pointer to the msg buffer
3021  *
3022  * Set the RSS HENA bits for the VF
3023  **/
3024 static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg)
3025 {
3026 	struct virtchnl_rss_hena *vrh =
3027 		(struct virtchnl_rss_hena *)msg;
3028 	struct i40e_pf *pf = vf->pf;
3029 	struct i40e_hw *hw = &pf->hw;
3030 	i40e_status aq_ret = 0;
3031 
3032 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3033 		aq_ret = I40E_ERR_PARAM;
3034 		goto err;
3035 	}
3036 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
3037 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
3038 			  (u32)(vrh->hena >> 32));
3039 
3040 	/* send the response to the VF */
3041 err:
3042 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
3043 }
3044 
3045 /**
3046  * i40e_vc_enable_vlan_stripping
3047  * @vf: pointer to the VF info
3048  * @msg: pointer to the msg buffer
3049  *
3050  * Enable vlan header stripping for the VF
3051  **/
3052 static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3053 {
3054 	i40e_status aq_ret = 0;
3055 	struct i40e_vsi *vsi;
3056 
3057 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3058 		aq_ret = I40E_ERR_PARAM;
3059 		goto err;
3060 	}
3061 
3062 	vsi = vf->pf->vsi[vf->lan_vsi_idx];
3063 	i40e_vlan_stripping_enable(vsi);
3064 
3065 	/* send the response to the VF */
3066 err:
3067 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
3068 				       aq_ret);
3069 }
3070 
3071 /**
3072  * i40e_vc_disable_vlan_stripping
3073  * @vf: pointer to the VF info
3074  * @msg: pointer to the msg buffer
3075  *
3076  * Disable vlan header stripping for the VF
3077  **/
3078 static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3079 {
3080 	i40e_status aq_ret = 0;
3081 	struct i40e_vsi *vsi;
3082 
3083 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3084 		aq_ret = I40E_ERR_PARAM;
3085 		goto err;
3086 	}
3087 
3088 	vsi = vf->pf->vsi[vf->lan_vsi_idx];
3089 	i40e_vlan_stripping_disable(vsi);
3090 
3091 	/* send the response to the VF */
3092 err:
3093 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
3094 				       aq_ret);
3095 }
3096 
3097 /**
3098  * i40e_validate_cloud_filter
3099  * @mask: mask for TC filter
3100  * @data: data for TC filter
3101  *
3102  * This function validates cloud filter programmed as TC filter for ADq
3103  **/
3104 static int i40e_validate_cloud_filter(struct i40e_vf *vf,
3105 				      struct virtchnl_filter *tc_filter)
3106 {
3107 	struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;
3108 	struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;
3109 	struct i40e_pf *pf = vf->pf;
3110 	struct i40e_vsi *vsi = NULL;
3111 	struct i40e_mac_filter *f;
3112 	struct hlist_node *h;
3113 	bool found = false;
3114 	int bkt;
3115 
3116 	if (!tc_filter->action) {
3117 		dev_info(&pf->pdev->dev,
3118 			 "VF %d: Currently ADq doesn't support Drop Action\n",
3119 			 vf->vf_id);
3120 		goto err;
3121 	}
3122 
3123 	/* action_meta is TC number here to which the filter is applied */
3124 	if (!tc_filter->action_meta ||
3125 	    tc_filter->action_meta > I40E_MAX_VF_VSI) {
3126 		dev_info(&pf->pdev->dev, "VF %d: Invalid TC number %u\n",
3127 			 vf->vf_id, tc_filter->action_meta);
3128 		goto err;
3129 	}
3130 
3131 	/* Check filter if it's programmed for advanced mode or basic mode.
3132 	 * There are two ADq modes (for VF only),
3133 	 * 1. Basic mode: intended to allow as many filter options as possible
3134 	 *		  to be added to a VF in Non-trusted mode. Main goal is
3135 	 *		  to add filters to its own MAC and VLAN id.
3136 	 * 2. Advanced mode: is for allowing filters to be applied other than
3137 	 *		  its own MAC or VLAN. This mode requires the VF to be
3138 	 *		  Trusted.
3139 	 */
3140 	if (mask.dst_mac[0] && !mask.dst_ip[0]) {
3141 		vsi = pf->vsi[vf->lan_vsi_idx];
3142 		f = i40e_find_mac(vsi, data.dst_mac);
3143 
3144 		if (!f) {
3145 			dev_info(&pf->pdev->dev,
3146 				 "Destination MAC %pM doesn't belong to VF %d\n",
3147 				 data.dst_mac, vf->vf_id);
3148 			goto err;
3149 		}
3150 
3151 		if (mask.vlan_id) {
3152 			hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
3153 					   hlist) {
3154 				if (f->vlan == ntohs(data.vlan_id)) {
3155 					found = true;
3156 					break;
3157 				}
3158 			}
3159 			if (!found) {
3160 				dev_info(&pf->pdev->dev,
3161 					 "VF %d doesn't have any VLAN id %u\n",
3162 					 vf->vf_id, ntohs(data.vlan_id));
3163 				goto err;
3164 			}
3165 		}
3166 	} else {
3167 		/* Check if VF is trusted */
3168 		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3169 			dev_err(&pf->pdev->dev,
3170 				"VF %d not trusted, make VF trusted to add advanced mode ADq cloud filters\n",
3171 				vf->vf_id);
3172 			return I40E_ERR_CONFIG;
3173 		}
3174 	}
3175 
3176 	if (mask.dst_mac[0] & data.dst_mac[0]) {
3177 		if (is_broadcast_ether_addr(data.dst_mac) ||
3178 		    is_zero_ether_addr(data.dst_mac)) {
3179 			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest MAC addr %pM\n",
3180 				 vf->vf_id, data.dst_mac);
3181 			goto err;
3182 		}
3183 	}
3184 
3185 	if (mask.src_mac[0] & data.src_mac[0]) {
3186 		if (is_broadcast_ether_addr(data.src_mac) ||
3187 		    is_zero_ether_addr(data.src_mac)) {
3188 			dev_info(&pf->pdev->dev, "VF %d: Invalid Source MAC addr %pM\n",
3189 				 vf->vf_id, data.src_mac);
3190 			goto err;
3191 		}
3192 	}
3193 
3194 	if (mask.dst_port & data.dst_port) {
3195 		if (!data.dst_port) {
3196 			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest port\n",
3197 				 vf->vf_id);
3198 			goto err;
3199 		}
3200 	}
3201 
3202 	if (mask.src_port & data.src_port) {
3203 		if (!data.src_port) {
3204 			dev_info(&pf->pdev->dev, "VF %d: Invalid Source port\n",
3205 				 vf->vf_id);
3206 			goto err;
3207 		}
3208 	}
3209 
3210 	if (tc_filter->flow_type != VIRTCHNL_TCP_V6_FLOW &&
3211 	    tc_filter->flow_type != VIRTCHNL_TCP_V4_FLOW) {
3212 		dev_info(&pf->pdev->dev, "VF %d: Invalid Flow type\n",
3213 			 vf->vf_id);
3214 		goto err;
3215 	}
3216 
3217 	if (mask.vlan_id & data.vlan_id) {
3218 		if (ntohs(data.vlan_id) > I40E_MAX_VLANID) {
3219 			dev_info(&pf->pdev->dev, "VF %d: invalid VLAN ID\n",
3220 				 vf->vf_id);
3221 			goto err;
3222 		}
3223 	}
3224 
3225 	return I40E_SUCCESS;
3226 err:
3227 	return I40E_ERR_CONFIG;
3228 }
3229 
3230 /**
3231  * i40e_find_vsi_from_seid - searches for the vsi with the given seid
3232  * @vf: pointer to the VF info
3233  * @seid - seid of the vsi it is searching for
3234  **/
3235 static struct i40e_vsi *i40e_find_vsi_from_seid(struct i40e_vf *vf, u16 seid)
3236 {
3237 	struct i40e_pf *pf = vf->pf;
3238 	struct i40e_vsi *vsi = NULL;
3239 	int i;
3240 
3241 	for (i = 0; i < vf->num_tc ; i++) {
3242 		vsi = i40e_find_vsi_from_id(pf, vf->ch[i].vsi_id);
3243 		if (vsi && vsi->seid == seid)
3244 			return vsi;
3245 	}
3246 	return NULL;
3247 }
3248 
3249 /**
3250  * i40e_del_all_cloud_filters
3251  * @vf: pointer to the VF info
3252  *
3253  * This function deletes all cloud filters
3254  **/
3255 static void i40e_del_all_cloud_filters(struct i40e_vf *vf)
3256 {
3257 	struct i40e_cloud_filter *cfilter = NULL;
3258 	struct i40e_pf *pf = vf->pf;
3259 	struct i40e_vsi *vsi = NULL;
3260 	struct hlist_node *node;
3261 	int ret;
3262 
3263 	hlist_for_each_entry_safe(cfilter, node,
3264 				  &vf->cloud_filter_list, cloud_node) {
3265 		vsi = i40e_find_vsi_from_seid(vf, cfilter->seid);
3266 
3267 		if (!vsi) {
3268 			dev_err(&pf->pdev->dev, "VF %d: no VSI found for matching %u seid, can't delete cloud filter\n",
3269 				vf->vf_id, cfilter->seid);
3270 			continue;
3271 		}
3272 
3273 		if (cfilter->dst_port)
3274 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
3275 								false);
3276 		else
3277 			ret = i40e_add_del_cloud_filter(vsi, cfilter, false);
3278 		if (ret)
3279 			dev_err(&pf->pdev->dev,
3280 				"VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
3281 				vf->vf_id, i40e_stat_str(&pf->hw, ret),
3282 				i40e_aq_str(&pf->hw,
3283 					    pf->hw.aq.asq_last_status));
3284 
3285 		hlist_del(&cfilter->cloud_node);
3286 		kfree(cfilter);
3287 		vf->num_cloud_filters--;
3288 	}
3289 }
3290 
3291 /**
3292  * i40e_vc_del_cloud_filter
3293  * @vf: pointer to the VF info
3294  * @msg: pointer to the msg buffer
3295  *
3296  * This function deletes a cloud filter programmed as TC filter for ADq
3297  **/
3298 static int i40e_vc_del_cloud_filter(struct i40e_vf *vf, u8 *msg)
3299 {
3300 	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3301 	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3302 	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3303 	struct i40e_cloud_filter cfilter, *cf = NULL;
3304 	struct i40e_pf *pf = vf->pf;
3305 	struct i40e_vsi *vsi = NULL;
3306 	struct hlist_node *node;
3307 	i40e_status aq_ret = 0;
3308 	int i, ret;
3309 
3310 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3311 		aq_ret = I40E_ERR_PARAM;
3312 		goto err;
3313 	}
3314 
3315 	if (!vf->adq_enabled) {
3316 		dev_info(&pf->pdev->dev,
3317 			 "VF %d: ADq not enabled, can't apply cloud filter\n",
3318 			 vf->vf_id);
3319 		aq_ret = I40E_ERR_PARAM;
3320 		goto err;
3321 	}
3322 
3323 	if (i40e_validate_cloud_filter(vf, vcf)) {
3324 		dev_info(&pf->pdev->dev,
3325 			 "VF %d: Invalid input, can't apply cloud filter\n",
3326 			 vf->vf_id);
3327 		aq_ret = I40E_ERR_PARAM;
3328 		goto err;
3329 	}
3330 
3331 	memset(&cfilter, 0, sizeof(cfilter));
3332 	/* parse destination mac address */
3333 	for (i = 0; i < ETH_ALEN; i++)
3334 		cfilter.dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3335 
3336 	/* parse source mac address */
3337 	for (i = 0; i < ETH_ALEN; i++)
3338 		cfilter.src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3339 
3340 	cfilter.vlan_id = mask.vlan_id & tcf.vlan_id;
3341 	cfilter.dst_port = mask.dst_port & tcf.dst_port;
3342 	cfilter.src_port = mask.src_port & tcf.src_port;
3343 
3344 	switch (vcf->flow_type) {
3345 	case VIRTCHNL_TCP_V4_FLOW:
3346 		cfilter.n_proto = ETH_P_IP;
3347 		if (mask.dst_ip[0] & tcf.dst_ip[0])
3348 			memcpy(&cfilter.ip.v4.dst_ip, tcf.dst_ip,
3349 			       ARRAY_SIZE(tcf.dst_ip));
3350 		else if (mask.src_ip[0] & tcf.dst_ip[0])
3351 			memcpy(&cfilter.ip.v4.src_ip, tcf.src_ip,
3352 			       ARRAY_SIZE(tcf.dst_ip));
3353 		break;
3354 	case VIRTCHNL_TCP_V6_FLOW:
3355 		cfilter.n_proto = ETH_P_IPV6;
3356 		if (mask.dst_ip[3] & tcf.dst_ip[3])
3357 			memcpy(&cfilter.ip.v6.dst_ip6, tcf.dst_ip,
3358 			       sizeof(cfilter.ip.v6.dst_ip6));
3359 		if (mask.src_ip[3] & tcf.src_ip[3])
3360 			memcpy(&cfilter.ip.v6.src_ip6, tcf.src_ip,
3361 			       sizeof(cfilter.ip.v6.src_ip6));
3362 		break;
3363 	default:
3364 		/* TC filter can be configured based on different combinations
3365 		 * and in this case IP is not a part of filter config
3366 		 */
3367 		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3368 			 vf->vf_id);
3369 	}
3370 
3371 	/* get the vsi to which the tc belongs to */
3372 	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3373 	cfilter.seid = vsi->seid;
3374 	cfilter.flags = vcf->field_flags;
3375 
3376 	/* Deleting TC filter */
3377 	if (tcf.dst_port)
3378 		ret = i40e_add_del_cloud_filter_big_buf(vsi, &cfilter, false);
3379 	else
3380 		ret = i40e_add_del_cloud_filter(vsi, &cfilter, false);
3381 	if (ret) {
3382 		dev_err(&pf->pdev->dev,
3383 			"VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
3384 			vf->vf_id, i40e_stat_str(&pf->hw, ret),
3385 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3386 		goto err;
3387 	}
3388 
3389 	hlist_for_each_entry_safe(cf, node,
3390 				  &vf->cloud_filter_list, cloud_node) {
3391 		if (cf->seid != cfilter.seid)
3392 			continue;
3393 		if (mask.dst_port)
3394 			if (cfilter.dst_port != cf->dst_port)
3395 				continue;
3396 		if (mask.dst_mac[0])
3397 			if (!ether_addr_equal(cf->src_mac, cfilter.src_mac))
3398 				continue;
3399 		/* for ipv4 data to be valid, only first byte of mask is set */
3400 		if (cfilter.n_proto == ETH_P_IP && mask.dst_ip[0])
3401 			if (memcmp(&cfilter.ip.v4.dst_ip, &cf->ip.v4.dst_ip,
3402 				   ARRAY_SIZE(tcf.dst_ip)))
3403 				continue;
3404 		/* for ipv6, mask is set for all sixteen bytes (4 words) */
3405 		if (cfilter.n_proto == ETH_P_IPV6 && mask.dst_ip[3])
3406 			if (memcmp(&cfilter.ip.v6.dst_ip6, &cf->ip.v6.dst_ip6,
3407 				   sizeof(cfilter.ip.v6.src_ip6)))
3408 				continue;
3409 		if (mask.vlan_id)
3410 			if (cfilter.vlan_id != cf->vlan_id)
3411 				continue;
3412 
3413 		hlist_del(&cf->cloud_node);
3414 		kfree(cf);
3415 		vf->num_cloud_filters--;
3416 	}
3417 
3418 err:
3419 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_CLOUD_FILTER,
3420 				       aq_ret);
3421 }
3422 
3423 /**
3424  * i40e_vc_add_cloud_filter
3425  * @vf: pointer to the VF info
3426  * @msg: pointer to the msg buffer
3427  *
3428  * This function adds a cloud filter programmed as TC filter for ADq
3429  **/
3430 static int i40e_vc_add_cloud_filter(struct i40e_vf *vf, u8 *msg)
3431 {
3432 	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3433 	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3434 	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3435 	struct i40e_cloud_filter *cfilter = NULL;
3436 	struct i40e_pf *pf = vf->pf;
3437 	struct i40e_vsi *vsi = NULL;
3438 	i40e_status aq_ret = 0;
3439 	int i, ret;
3440 
3441 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3442 		aq_ret = I40E_ERR_PARAM;
3443 		goto err_out;
3444 	}
3445 
3446 	if (!vf->adq_enabled) {
3447 		dev_info(&pf->pdev->dev,
3448 			 "VF %d: ADq is not enabled, can't apply cloud filter\n",
3449 			 vf->vf_id);
3450 		aq_ret = I40E_ERR_PARAM;
3451 		goto err_out;
3452 	}
3453 
3454 	if (i40e_validate_cloud_filter(vf, vcf)) {
3455 		dev_info(&pf->pdev->dev,
3456 			 "VF %d: Invalid input/s, can't apply cloud filter\n",
3457 			 vf->vf_id);
3458 		aq_ret = I40E_ERR_PARAM;
3459 		goto err_out;
3460 	}
3461 
3462 	cfilter = kzalloc(sizeof(*cfilter), GFP_KERNEL);
3463 	if (!cfilter)
3464 		return -ENOMEM;
3465 
3466 	/* parse destination mac address */
3467 	for (i = 0; i < ETH_ALEN; i++)
3468 		cfilter->dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3469 
3470 	/* parse source mac address */
3471 	for (i = 0; i < ETH_ALEN; i++)
3472 		cfilter->src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3473 
3474 	cfilter->vlan_id = mask.vlan_id & tcf.vlan_id;
3475 	cfilter->dst_port = mask.dst_port & tcf.dst_port;
3476 	cfilter->src_port = mask.src_port & tcf.src_port;
3477 
3478 	switch (vcf->flow_type) {
3479 	case VIRTCHNL_TCP_V4_FLOW:
3480 		cfilter->n_proto = ETH_P_IP;
3481 		if (mask.dst_ip[0] & tcf.dst_ip[0])
3482 			memcpy(&cfilter->ip.v4.dst_ip, tcf.dst_ip,
3483 			       ARRAY_SIZE(tcf.dst_ip));
3484 		else if (mask.src_ip[0] & tcf.dst_ip[0])
3485 			memcpy(&cfilter->ip.v4.src_ip, tcf.src_ip,
3486 			       ARRAY_SIZE(tcf.dst_ip));
3487 		break;
3488 	case VIRTCHNL_TCP_V6_FLOW:
3489 		cfilter->n_proto = ETH_P_IPV6;
3490 		if (mask.dst_ip[3] & tcf.dst_ip[3])
3491 			memcpy(&cfilter->ip.v6.dst_ip6, tcf.dst_ip,
3492 			       sizeof(cfilter->ip.v6.dst_ip6));
3493 		if (mask.src_ip[3] & tcf.src_ip[3])
3494 			memcpy(&cfilter->ip.v6.src_ip6, tcf.src_ip,
3495 			       sizeof(cfilter->ip.v6.src_ip6));
3496 		break;
3497 	default:
3498 		/* TC filter can be configured based on different combinations
3499 		 * and in this case IP is not a part of filter config
3500 		 */
3501 		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3502 			 vf->vf_id);
3503 	}
3504 
3505 	/* get the VSI to which the TC belongs to */
3506 	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3507 	cfilter->seid = vsi->seid;
3508 	cfilter->flags = vcf->field_flags;
3509 
3510 	/* Adding cloud filter programmed as TC filter */
3511 	if (tcf.dst_port)
3512 		ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, true);
3513 	else
3514 		ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
3515 	if (ret) {
3516 		dev_err(&pf->pdev->dev,
3517 			"VF %d: Failed to add cloud filter, err %s aq_err %s\n",
3518 			vf->vf_id, i40e_stat_str(&pf->hw, ret),
3519 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3520 		goto err_free;
3521 	}
3522 
3523 	INIT_HLIST_NODE(&cfilter->cloud_node);
3524 	hlist_add_head(&cfilter->cloud_node, &vf->cloud_filter_list);
3525 	/* release the pointer passing it to the collection */
3526 	cfilter = NULL;
3527 	vf->num_cloud_filters++;
3528 err_free:
3529 	kfree(cfilter);
3530 err_out:
3531 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_CLOUD_FILTER,
3532 				       aq_ret);
3533 }
3534 
3535 /**
3536  * i40e_vc_add_qch_msg: Add queue channel and enable ADq
3537  * @vf: pointer to the VF info
3538  * @msg: pointer to the msg buffer
3539  **/
3540 static int i40e_vc_add_qch_msg(struct i40e_vf *vf, u8 *msg)
3541 {
3542 	struct virtchnl_tc_info *tci =
3543 		(struct virtchnl_tc_info *)msg;
3544 	struct i40e_pf *pf = vf->pf;
3545 	struct i40e_link_status *ls = &pf->hw.phy.link_info;
3546 	int i, adq_request_qps = 0;
3547 	i40e_status aq_ret = 0;
3548 	u64 speed = 0;
3549 
3550 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3551 		aq_ret = I40E_ERR_PARAM;
3552 		goto err;
3553 	}
3554 
3555 	/* ADq cannot be applied if spoof check is ON */
3556 	if (vf->spoofchk) {
3557 		dev_err(&pf->pdev->dev,
3558 			"Spoof check is ON, turn it OFF to enable ADq\n");
3559 		aq_ret = I40E_ERR_PARAM;
3560 		goto err;
3561 	}
3562 
3563 	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)) {
3564 		dev_err(&pf->pdev->dev,
3565 			"VF %d attempting to enable ADq, but hasn't properly negotiated that capability\n",
3566 			vf->vf_id);
3567 		aq_ret = I40E_ERR_PARAM;
3568 		goto err;
3569 	}
3570 
3571 	/* max number of traffic classes for VF currently capped at 4 */
3572 	if (!tci->num_tc || tci->num_tc > I40E_MAX_VF_VSI) {
3573 		dev_err(&pf->pdev->dev,
3574 			"VF %d trying to set %u TCs, valid range 1-%u TCs per VF\n",
3575 			vf->vf_id, tci->num_tc, I40E_MAX_VF_VSI);
3576 		aq_ret = I40E_ERR_PARAM;
3577 		goto err;
3578 	}
3579 
3580 	/* validate queues for each TC */
3581 	for (i = 0; i < tci->num_tc; i++)
3582 		if (!tci->list[i].count ||
3583 		    tci->list[i].count > I40E_DEFAULT_QUEUES_PER_VF) {
3584 			dev_err(&pf->pdev->dev,
3585 				"VF %d: TC %d trying to set %u queues, valid range 1-%u queues per TC\n",
3586 				vf->vf_id, i, tci->list[i].count,
3587 				I40E_DEFAULT_QUEUES_PER_VF);
3588 			aq_ret = I40E_ERR_PARAM;
3589 			goto err;
3590 		}
3591 
3592 	/* need Max VF queues but already have default number of queues */
3593 	adq_request_qps = I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF;
3594 
3595 	if (pf->queues_left < adq_request_qps) {
3596 		dev_err(&pf->pdev->dev,
3597 			"No queues left to allocate to VF %d\n",
3598 			vf->vf_id);
3599 		aq_ret = I40E_ERR_PARAM;
3600 		goto err;
3601 	} else {
3602 		/* we need to allocate max VF queues to enable ADq so as to
3603 		 * make sure ADq enabled VF always gets back queues when it
3604 		 * goes through a reset.
3605 		 */
3606 		vf->num_queue_pairs = I40E_MAX_VF_QUEUES;
3607 	}
3608 
3609 	/* get link speed in MB to validate rate limit */
3610 	switch (ls->link_speed) {
3611 	case VIRTCHNL_LINK_SPEED_100MB:
3612 		speed = SPEED_100;
3613 		break;
3614 	case VIRTCHNL_LINK_SPEED_1GB:
3615 		speed = SPEED_1000;
3616 		break;
3617 	case VIRTCHNL_LINK_SPEED_10GB:
3618 		speed = SPEED_10000;
3619 		break;
3620 	case VIRTCHNL_LINK_SPEED_20GB:
3621 		speed = SPEED_20000;
3622 		break;
3623 	case VIRTCHNL_LINK_SPEED_25GB:
3624 		speed = SPEED_25000;
3625 		break;
3626 	case VIRTCHNL_LINK_SPEED_40GB:
3627 		speed = SPEED_40000;
3628 		break;
3629 	default:
3630 		dev_err(&pf->pdev->dev,
3631 			"Cannot detect link speed\n");
3632 		aq_ret = I40E_ERR_PARAM;
3633 		goto err;
3634 	}
3635 
3636 	/* parse data from the queue channel info */
3637 	vf->num_tc = tci->num_tc;
3638 	for (i = 0; i < vf->num_tc; i++) {
3639 		if (tci->list[i].max_tx_rate) {
3640 			if (tci->list[i].max_tx_rate > speed) {
3641 				dev_err(&pf->pdev->dev,
3642 					"Invalid max tx rate %llu specified for VF %d.",
3643 					tci->list[i].max_tx_rate,
3644 					vf->vf_id);
3645 				aq_ret = I40E_ERR_PARAM;
3646 				goto err;
3647 			} else {
3648 				vf->ch[i].max_tx_rate =
3649 					tci->list[i].max_tx_rate;
3650 			}
3651 		}
3652 		vf->ch[i].num_qps = tci->list[i].count;
3653 	}
3654 
3655 	/* set this flag only after making sure all inputs are sane */
3656 	vf->adq_enabled = true;
3657 	/* num_req_queues is set when user changes number of queues via ethtool
3658 	 * and this causes issue for default VSI(which depends on this variable)
3659 	 * when ADq is enabled, hence reset it.
3660 	 */
3661 	vf->num_req_queues = 0;
3662 
3663 	/* reset the VF in order to allocate resources */
3664 	i40e_vc_notify_vf_reset(vf);
3665 	i40e_reset_vf(vf, false);
3666 
3667 	return I40E_SUCCESS;
3668 
3669 	/* send the response to the VF */
3670 err:
3671 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_CHANNELS,
3672 				       aq_ret);
3673 }
3674 
3675 /**
3676  * i40e_vc_del_qch_msg
3677  * @vf: pointer to the VF info
3678  * @msg: pointer to the msg buffer
3679  **/
3680 static int i40e_vc_del_qch_msg(struct i40e_vf *vf, u8 *msg)
3681 {
3682 	struct i40e_pf *pf = vf->pf;
3683 	i40e_status aq_ret = 0;
3684 
3685 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
3686 		aq_ret = I40E_ERR_PARAM;
3687 		goto err;
3688 	}
3689 
3690 	if (vf->adq_enabled) {
3691 		i40e_del_all_cloud_filters(vf);
3692 		i40e_del_qch(vf);
3693 		vf->adq_enabled = false;
3694 		vf->num_tc = 0;
3695 		dev_info(&pf->pdev->dev,
3696 			 "Deleting Queue Channels and cloud filters for ADq on VF %d\n",
3697 			 vf->vf_id);
3698 	} else {
3699 		dev_info(&pf->pdev->dev, "VF %d trying to delete queue channels but ADq isn't enabled\n",
3700 			 vf->vf_id);
3701 		aq_ret = I40E_ERR_PARAM;
3702 	}
3703 
3704 	/* reset the VF in order to allocate resources */
3705 	i40e_vc_notify_vf_reset(vf);
3706 	i40e_reset_vf(vf, false);
3707 
3708 	return I40E_SUCCESS;
3709 
3710 err:
3711 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_CHANNELS,
3712 				       aq_ret);
3713 }
3714 
3715 /**
3716  * i40e_vc_process_vf_msg
3717  * @pf: pointer to the PF structure
3718  * @vf_id: source VF id
3719  * @v_opcode: operation code
3720  * @v_retval: unused return value code
3721  * @msg: pointer to the msg buffer
3722  * @msglen: msg length
3723  *
3724  * called from the common aeq/arq handler to
3725  * process request from VF
3726  **/
3727 int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
3728 			   u32 __always_unused v_retval, u8 *msg, u16 msglen)
3729 {
3730 	struct i40e_hw *hw = &pf->hw;
3731 	int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
3732 	struct i40e_vf *vf;
3733 	int ret;
3734 
3735 	pf->vf_aq_requests++;
3736 	if (local_vf_id < 0 || local_vf_id >= pf->num_alloc_vfs)
3737 		return -EINVAL;
3738 	vf = &(pf->vf[local_vf_id]);
3739 
3740 	/* Check if VF is disabled. */
3741 	if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
3742 		return I40E_ERR_PARAM;
3743 
3744 	/* perform basic checks on the msg */
3745 	ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
3746 
3747 	if (ret) {
3748 		i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
3749 		dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
3750 			local_vf_id, v_opcode, msglen);
3751 		switch (ret) {
3752 		case VIRTCHNL_STATUS_ERR_PARAM:
3753 			return -EPERM;
3754 		default:
3755 			return -EINVAL;
3756 		}
3757 	}
3758 
3759 	switch (v_opcode) {
3760 	case VIRTCHNL_OP_VERSION:
3761 		ret = i40e_vc_get_version_msg(vf, msg);
3762 		break;
3763 	case VIRTCHNL_OP_GET_VF_RESOURCES:
3764 		ret = i40e_vc_get_vf_resources_msg(vf, msg);
3765 		i40e_vc_notify_vf_link_state(vf);
3766 		break;
3767 	case VIRTCHNL_OP_RESET_VF:
3768 		i40e_vc_reset_vf_msg(vf);
3769 		ret = 0;
3770 		break;
3771 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
3772 		ret = i40e_vc_config_promiscuous_mode_msg(vf, msg);
3773 		break;
3774 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
3775 		ret = i40e_vc_config_queues_msg(vf, msg);
3776 		break;
3777 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
3778 		ret = i40e_vc_config_irq_map_msg(vf, msg);
3779 		break;
3780 	case VIRTCHNL_OP_ENABLE_QUEUES:
3781 		ret = i40e_vc_enable_queues_msg(vf, msg);
3782 		i40e_vc_notify_vf_link_state(vf);
3783 		break;
3784 	case VIRTCHNL_OP_DISABLE_QUEUES:
3785 		ret = i40e_vc_disable_queues_msg(vf, msg);
3786 		break;
3787 	case VIRTCHNL_OP_ADD_ETH_ADDR:
3788 		ret = i40e_vc_add_mac_addr_msg(vf, msg);
3789 		break;
3790 	case VIRTCHNL_OP_DEL_ETH_ADDR:
3791 		ret = i40e_vc_del_mac_addr_msg(vf, msg);
3792 		break;
3793 	case VIRTCHNL_OP_ADD_VLAN:
3794 		ret = i40e_vc_add_vlan_msg(vf, msg);
3795 		break;
3796 	case VIRTCHNL_OP_DEL_VLAN:
3797 		ret = i40e_vc_remove_vlan_msg(vf, msg);
3798 		break;
3799 	case VIRTCHNL_OP_GET_STATS:
3800 		ret = i40e_vc_get_stats_msg(vf, msg);
3801 		break;
3802 	case VIRTCHNL_OP_IWARP:
3803 		ret = i40e_vc_iwarp_msg(vf, msg, msglen);
3804 		break;
3805 	case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
3806 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, true);
3807 		break;
3808 	case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
3809 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, false);
3810 		break;
3811 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
3812 		ret = i40e_vc_config_rss_key(vf, msg);
3813 		break;
3814 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
3815 		ret = i40e_vc_config_rss_lut(vf, msg);
3816 		break;
3817 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
3818 		ret = i40e_vc_get_rss_hena(vf, msg);
3819 		break;
3820 	case VIRTCHNL_OP_SET_RSS_HENA:
3821 		ret = i40e_vc_set_rss_hena(vf, msg);
3822 		break;
3823 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
3824 		ret = i40e_vc_enable_vlan_stripping(vf, msg);
3825 		break;
3826 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
3827 		ret = i40e_vc_disable_vlan_stripping(vf, msg);
3828 		break;
3829 	case VIRTCHNL_OP_REQUEST_QUEUES:
3830 		ret = i40e_vc_request_queues_msg(vf, msg);
3831 		break;
3832 	case VIRTCHNL_OP_ENABLE_CHANNELS:
3833 		ret = i40e_vc_add_qch_msg(vf, msg);
3834 		break;
3835 	case VIRTCHNL_OP_DISABLE_CHANNELS:
3836 		ret = i40e_vc_del_qch_msg(vf, msg);
3837 		break;
3838 	case VIRTCHNL_OP_ADD_CLOUD_FILTER:
3839 		ret = i40e_vc_add_cloud_filter(vf, msg);
3840 		break;
3841 	case VIRTCHNL_OP_DEL_CLOUD_FILTER:
3842 		ret = i40e_vc_del_cloud_filter(vf, msg);
3843 		break;
3844 	case VIRTCHNL_OP_UNKNOWN:
3845 	default:
3846 		dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
3847 			v_opcode, local_vf_id);
3848 		ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
3849 					      I40E_ERR_NOT_IMPLEMENTED);
3850 		break;
3851 	}
3852 
3853 	return ret;
3854 }
3855 
3856 /**
3857  * i40e_vc_process_vflr_event
3858  * @pf: pointer to the PF structure
3859  *
3860  * called from the vlfr irq handler to
3861  * free up VF resources and state variables
3862  **/
3863 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
3864 {
3865 	struct i40e_hw *hw = &pf->hw;
3866 	u32 reg, reg_idx, bit_idx;
3867 	struct i40e_vf *vf;
3868 	int vf_id;
3869 
3870 	if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
3871 		return 0;
3872 
3873 	/* Re-enable the VFLR interrupt cause here, before looking for which
3874 	 * VF got reset. Otherwise, if another VF gets a reset while the
3875 	 * first one is being processed, that interrupt will be lost, and
3876 	 * that VF will be stuck in reset forever.
3877 	 */
3878 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
3879 	reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
3880 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
3881 	i40e_flush(hw);
3882 
3883 	clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
3884 	for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
3885 		reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
3886 		bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
3887 		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
3888 		vf = &pf->vf[vf_id];
3889 		reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
3890 		if (reg & BIT(bit_idx))
3891 			/* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
3892 			i40e_reset_vf(vf, true);
3893 	}
3894 
3895 	return 0;
3896 }
3897 
3898 /**
3899  * i40e_validate_vf
3900  * @pf: the physical function
3901  * @vf_id: VF identifier
3902  *
3903  * Check that the VF is enabled and the VSI exists.
3904  *
3905  * Returns 0 on success, negative on failure
3906  **/
3907 static int i40e_validate_vf(struct i40e_pf *pf, int vf_id)
3908 {
3909 	struct i40e_vsi *vsi;
3910 	struct i40e_vf *vf;
3911 	int ret = 0;
3912 
3913 	if (vf_id >= pf->num_alloc_vfs) {
3914 		dev_err(&pf->pdev->dev,
3915 			"Invalid VF Identifier %d\n", vf_id);
3916 		ret = -EINVAL;
3917 		goto err_out;
3918 	}
3919 	vf = &pf->vf[vf_id];
3920 	vsi = i40e_find_vsi_from_id(pf, vf->lan_vsi_id);
3921 	if (!vsi)
3922 		ret = -EINVAL;
3923 err_out:
3924 	return ret;
3925 }
3926 
3927 /**
3928  * i40e_ndo_set_vf_mac
3929  * @netdev: network interface device structure
3930  * @vf_id: VF identifier
3931  * @mac: mac address
3932  *
3933  * program VF mac address
3934  **/
3935 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
3936 {
3937 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3938 	struct i40e_vsi *vsi = np->vsi;
3939 	struct i40e_pf *pf = vsi->back;
3940 	struct i40e_mac_filter *f;
3941 	struct i40e_vf *vf;
3942 	int ret = 0;
3943 	struct hlist_node *h;
3944 	int bkt;
3945 	u8 i;
3946 
3947 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
3948 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
3949 		return -EAGAIN;
3950 	}
3951 
3952 	/* validate the request */
3953 	ret = i40e_validate_vf(pf, vf_id);
3954 	if (ret)
3955 		goto error_param;
3956 
3957 	vf = &pf->vf[vf_id];
3958 	vsi = pf->vsi[vf->lan_vsi_idx];
3959 
3960 	/* When the VF is resetting wait until it is done.
3961 	 * It can take up to 200 milliseconds,
3962 	 * but wait for up to 300 milliseconds to be safe.
3963 	 * If the VF is indeed in reset, the vsi pointer has
3964 	 * to show on the newly loaded vsi under pf->vsi[id].
3965 	 */
3966 	for (i = 0; i < 15; i++) {
3967 		if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3968 			if (i > 0)
3969 				vsi = pf->vsi[vf->lan_vsi_idx];
3970 			break;
3971 		}
3972 		msleep(20);
3973 	}
3974 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
3975 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3976 			vf_id);
3977 		ret = -EAGAIN;
3978 		goto error_param;
3979 	}
3980 
3981 	if (is_multicast_ether_addr(mac)) {
3982 		dev_err(&pf->pdev->dev,
3983 			"Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
3984 		ret = -EINVAL;
3985 		goto error_param;
3986 	}
3987 
3988 	/* Lock once because below invoked function add/del_filter requires
3989 	 * mac_filter_hash_lock to be held
3990 	 */
3991 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3992 
3993 	/* delete the temporary mac address */
3994 	if (!is_zero_ether_addr(vf->default_lan_addr.addr))
3995 		i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
3996 
3997 	/* Delete all the filters for this VSI - we're going to kill it
3998 	 * anyway.
3999 	 */
4000 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
4001 		__i40e_del_filter(vsi, f);
4002 
4003 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4004 
4005 	/* program mac filter */
4006 	if (i40e_sync_vsi_filters(vsi)) {
4007 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
4008 		ret = -EIO;
4009 		goto error_param;
4010 	}
4011 	ether_addr_copy(vf->default_lan_addr.addr, mac);
4012 
4013 	if (is_zero_ether_addr(mac)) {
4014 		vf->pf_set_mac = false;
4015 		dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
4016 	} else {
4017 		vf->pf_set_mac = true;
4018 		dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
4019 			 mac, vf_id);
4020 	}
4021 
4022 	/* Force the VF interface down so it has to bring up with new MAC
4023 	 * address
4024 	 */
4025 	i40e_vc_disable_vf(vf);
4026 	dev_info(&pf->pdev->dev, "Bring down and up the VF interface to make this change effective.\n");
4027 
4028 error_param:
4029 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4030 	return ret;
4031 }
4032 
4033 /**
4034  * i40e_vsi_has_vlans - True if VSI has configured VLANs
4035  * @vsi: pointer to the vsi
4036  *
4037  * Check if a VSI has configured any VLANs. False if we have a port VLAN or if
4038  * we have no configured VLANs. Do not call while holding the
4039  * mac_filter_hash_lock.
4040  */
4041 static bool i40e_vsi_has_vlans(struct i40e_vsi *vsi)
4042 {
4043 	bool have_vlans;
4044 
4045 	/* If we have a port VLAN, then the VSI cannot have any VLANs
4046 	 * configured, as all MAC/VLAN filters will be assigned to the PVID.
4047 	 */
4048 	if (vsi->info.pvid)
4049 		return false;
4050 
4051 	/* Since we don't have a PVID, we know that if the device is in VLAN
4052 	 * mode it must be because of a VLAN filter configured on this VSI.
4053 	 */
4054 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4055 	have_vlans = i40e_is_vsi_in_vlan(vsi);
4056 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4057 
4058 	return have_vlans;
4059 }
4060 
4061 /**
4062  * i40e_ndo_set_vf_port_vlan
4063  * @netdev: network interface device structure
4064  * @vf_id: VF identifier
4065  * @vlan_id: mac address
4066  * @qos: priority setting
4067  * @vlan_proto: vlan protocol
4068  *
4069  * program VF vlan id and/or qos
4070  **/
4071 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
4072 			      u16 vlan_id, u8 qos, __be16 vlan_proto)
4073 {
4074 	u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
4075 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4076 	bool allmulti = false, alluni = false;
4077 	struct i40e_pf *pf = np->vsi->back;
4078 	struct i40e_vsi *vsi;
4079 	struct i40e_vf *vf;
4080 	int ret = 0;
4081 
4082 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4083 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4084 		return -EAGAIN;
4085 	}
4086 
4087 	/* validate the request */
4088 	ret = i40e_validate_vf(pf, vf_id);
4089 	if (ret)
4090 		goto error_pvid;
4091 
4092 	if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
4093 		dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
4094 		ret = -EINVAL;
4095 		goto error_pvid;
4096 	}
4097 
4098 	if (vlan_proto != htons(ETH_P_8021Q)) {
4099 		dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
4100 		ret = -EPROTONOSUPPORT;
4101 		goto error_pvid;
4102 	}
4103 
4104 	vf = &pf->vf[vf_id];
4105 	vsi = pf->vsi[vf->lan_vsi_idx];
4106 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4107 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4108 			vf_id);
4109 		ret = -EAGAIN;
4110 		goto error_pvid;
4111 	}
4112 
4113 	if (le16_to_cpu(vsi->info.pvid) == vlanprio)
4114 		/* duplicate request, so just return success */
4115 		goto error_pvid;
4116 
4117 	if (i40e_vsi_has_vlans(vsi)) {
4118 		dev_err(&pf->pdev->dev,
4119 			"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",
4120 			vf_id);
4121 		/* Administrator Error - knock the VF offline until he does
4122 		 * the right thing by reconfiguring his network correctly
4123 		 * and then reloading the VF driver.
4124 		 */
4125 		i40e_vc_disable_vf(vf);
4126 		/* During reset the VF got a new VSI, so refresh the pointer. */
4127 		vsi = pf->vsi[vf->lan_vsi_idx];
4128 	}
4129 
4130 	/* Locked once because multiple functions below iterate list */
4131 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4132 
4133 	/* Check for condition where there was already a port VLAN ID
4134 	 * filter set and now it is being deleted by setting it to zero.
4135 	 * Additionally check for the condition where there was a port
4136 	 * VLAN but now there is a new and different port VLAN being set.
4137 	 * Before deleting all the old VLAN filters we must add new ones
4138 	 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
4139 	 * MAC addresses deleted.
4140 	 */
4141 	if ((!(vlan_id || qos) ||
4142 	    vlanprio != le16_to_cpu(vsi->info.pvid)) &&
4143 	    vsi->info.pvid) {
4144 		ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
4145 		if (ret) {
4146 			dev_info(&vsi->back->pdev->dev,
4147 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4148 				 vsi->back->hw.aq.asq_last_status);
4149 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4150 			goto error_pvid;
4151 		}
4152 	}
4153 
4154 	if (vsi->info.pvid) {
4155 		/* remove all filters on the old VLAN */
4156 		i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
4157 					   VLAN_VID_MASK));
4158 	}
4159 
4160 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4161 
4162 	/* disable promisc modes in case they were enabled */
4163 	ret = i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id,
4164 					      allmulti, alluni);
4165 	if (ret) {
4166 		dev_err(&pf->pdev->dev, "Unable to config VF promiscuous mode\n");
4167 		goto error_pvid;
4168 	}
4169 
4170 	if (vlan_id || qos)
4171 		ret = i40e_vsi_add_pvid(vsi, vlanprio);
4172 	else
4173 		i40e_vsi_remove_pvid(vsi);
4174 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4175 
4176 	if (vlan_id) {
4177 		dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
4178 			 vlan_id, qos, vf_id);
4179 
4180 		/* add new VLAN filter for each MAC */
4181 		ret = i40e_add_vlan_all_mac(vsi, vlan_id);
4182 		if (ret) {
4183 			dev_info(&vsi->back->pdev->dev,
4184 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4185 				 vsi->back->hw.aq.asq_last_status);
4186 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4187 			goto error_pvid;
4188 		}
4189 
4190 		/* remove the previously added non-VLAN MAC filters */
4191 		i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
4192 	}
4193 
4194 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4195 
4196 	if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
4197 		alluni = true;
4198 
4199 	if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
4200 		allmulti = true;
4201 
4202 	/* Schedule the worker thread to take care of applying changes */
4203 	i40e_service_event_schedule(vsi->back);
4204 
4205 	if (ret) {
4206 		dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
4207 		goto error_pvid;
4208 	}
4209 
4210 	/* The Port VLAN needs to be saved across resets the same as the
4211 	 * default LAN MAC address.
4212 	 */
4213 	vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
4214 
4215 	ret = i40e_config_vf_promiscuous_mode(vf, vsi->id, allmulti, alluni);
4216 	if (ret) {
4217 		dev_err(&pf->pdev->dev, "Unable to config vf promiscuous mode\n");
4218 		goto error_pvid;
4219 	}
4220 
4221 	ret = 0;
4222 
4223 error_pvid:
4224 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4225 	return ret;
4226 }
4227 
4228 /**
4229  * i40e_ndo_set_vf_bw
4230  * @netdev: network interface device structure
4231  * @vf_id: VF identifier
4232  * @min_tx_rate: Minimum Tx rate
4233  * @max_tx_rate: Maximum Tx rate
4234  *
4235  * configure VF Tx rate
4236  **/
4237 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
4238 		       int max_tx_rate)
4239 {
4240 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4241 	struct i40e_pf *pf = np->vsi->back;
4242 	struct i40e_vsi *vsi;
4243 	struct i40e_vf *vf;
4244 	int ret = 0;
4245 
4246 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4247 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4248 		return -EAGAIN;
4249 	}
4250 
4251 	/* validate the request */
4252 	ret = i40e_validate_vf(pf, vf_id);
4253 	if (ret)
4254 		goto error;
4255 
4256 	if (min_tx_rate) {
4257 		dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
4258 			min_tx_rate, vf_id);
4259 		ret = -EINVAL;
4260 		goto error;
4261 	}
4262 
4263 	vf = &pf->vf[vf_id];
4264 	vsi = pf->vsi[vf->lan_vsi_idx];
4265 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4266 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4267 			vf_id);
4268 		ret = -EAGAIN;
4269 		goto error;
4270 	}
4271 
4272 	ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
4273 	if (ret)
4274 		goto error;
4275 
4276 	vf->tx_rate = max_tx_rate;
4277 error:
4278 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4279 	return ret;
4280 }
4281 
4282 /**
4283  * i40e_ndo_get_vf_config
4284  * @netdev: network interface device structure
4285  * @vf_id: VF identifier
4286  * @ivi: VF configuration structure
4287  *
4288  * return VF configuration
4289  **/
4290 int i40e_ndo_get_vf_config(struct net_device *netdev,
4291 			   int vf_id, struct ifla_vf_info *ivi)
4292 {
4293 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4294 	struct i40e_vsi *vsi = np->vsi;
4295 	struct i40e_pf *pf = vsi->back;
4296 	struct i40e_vf *vf;
4297 	int ret = 0;
4298 
4299 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4300 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4301 		return -EAGAIN;
4302 	}
4303 
4304 	/* validate the request */
4305 	ret = i40e_validate_vf(pf, vf_id);
4306 	if (ret)
4307 		goto error_param;
4308 
4309 	vf = &pf->vf[vf_id];
4310 	/* first vsi is always the LAN vsi */
4311 	vsi = pf->vsi[vf->lan_vsi_idx];
4312 	if (!vsi) {
4313 		ret = -ENOENT;
4314 		goto error_param;
4315 	}
4316 
4317 	ivi->vf = vf_id;
4318 
4319 	ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
4320 
4321 	ivi->max_tx_rate = vf->tx_rate;
4322 	ivi->min_tx_rate = 0;
4323 	ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
4324 	ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
4325 		   I40E_VLAN_PRIORITY_SHIFT;
4326 	if (vf->link_forced == false)
4327 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4328 	else if (vf->link_up == true)
4329 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4330 	else
4331 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4332 	ivi->spoofchk = vf->spoofchk;
4333 	ivi->trusted = vf->trusted;
4334 	ret = 0;
4335 
4336 error_param:
4337 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4338 	return ret;
4339 }
4340 
4341 /**
4342  * i40e_ndo_set_vf_link_state
4343  * @netdev: network interface device structure
4344  * @vf_id: VF identifier
4345  * @link: required link state
4346  *
4347  * Set the link state of a specified VF, regardless of physical link state
4348  **/
4349 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
4350 {
4351 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4352 	struct i40e_pf *pf = np->vsi->back;
4353 	struct virtchnl_pf_event pfe;
4354 	struct i40e_hw *hw = &pf->hw;
4355 	struct i40e_vf *vf;
4356 	int abs_vf_id;
4357 	int ret = 0;
4358 
4359 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4360 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4361 		return -EAGAIN;
4362 	}
4363 
4364 	/* validate the request */
4365 	if (vf_id >= pf->num_alloc_vfs) {
4366 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4367 		ret = -EINVAL;
4368 		goto error_out;
4369 	}
4370 
4371 	vf = &pf->vf[vf_id];
4372 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
4373 
4374 	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
4375 	pfe.severity = PF_EVENT_SEVERITY_INFO;
4376 
4377 	switch (link) {
4378 	case IFLA_VF_LINK_STATE_AUTO:
4379 		vf->link_forced = false;
4380 		pfe.event_data.link_event.link_status =
4381 			pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
4382 		pfe.event_data.link_event.link_speed =
4383 			(enum virtchnl_link_speed)
4384 			pf->hw.phy.link_info.link_speed;
4385 		break;
4386 	case IFLA_VF_LINK_STATE_ENABLE:
4387 		vf->link_forced = true;
4388 		vf->link_up = true;
4389 		pfe.event_data.link_event.link_status = true;
4390 		pfe.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_40GB;
4391 		break;
4392 	case IFLA_VF_LINK_STATE_DISABLE:
4393 		vf->link_forced = true;
4394 		vf->link_up = false;
4395 		pfe.event_data.link_event.link_status = false;
4396 		pfe.event_data.link_event.link_speed = 0;
4397 		break;
4398 	default:
4399 		ret = -EINVAL;
4400 		goto error_out;
4401 	}
4402 	/* Notify the VF of its new link state */
4403 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
4404 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
4405 
4406 error_out:
4407 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4408 	return ret;
4409 }
4410 
4411 /**
4412  * i40e_ndo_set_vf_spoofchk
4413  * @netdev: network interface device structure
4414  * @vf_id: VF identifier
4415  * @enable: flag to enable or disable feature
4416  *
4417  * Enable or disable VF spoof checking
4418  **/
4419 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
4420 {
4421 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4422 	struct i40e_vsi *vsi = np->vsi;
4423 	struct i40e_pf *pf = vsi->back;
4424 	struct i40e_vsi_context ctxt;
4425 	struct i40e_hw *hw = &pf->hw;
4426 	struct i40e_vf *vf;
4427 	int ret = 0;
4428 
4429 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4430 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4431 		return -EAGAIN;
4432 	}
4433 
4434 	/* validate the request */
4435 	if (vf_id >= pf->num_alloc_vfs) {
4436 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4437 		ret = -EINVAL;
4438 		goto out;
4439 	}
4440 
4441 	vf = &(pf->vf[vf_id]);
4442 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4443 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
4444 			vf_id);
4445 		ret = -EAGAIN;
4446 		goto out;
4447 	}
4448 
4449 	if (enable == vf->spoofchk)
4450 		goto out;
4451 
4452 	vf->spoofchk = enable;
4453 	memset(&ctxt, 0, sizeof(ctxt));
4454 	ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
4455 	ctxt.pf_num = pf->hw.pf_id;
4456 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
4457 	if (enable)
4458 		ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
4459 					I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
4460 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
4461 	if (ret) {
4462 		dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
4463 			ret);
4464 		ret = -EIO;
4465 	}
4466 out:
4467 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4468 	return ret;
4469 }
4470 
4471 /**
4472  * i40e_ndo_set_vf_trust
4473  * @netdev: network interface device structure of the pf
4474  * @vf_id: VF identifier
4475  * @setting: trust setting
4476  *
4477  * Enable or disable VF trust setting
4478  **/
4479 int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
4480 {
4481 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4482 	struct i40e_pf *pf = np->vsi->back;
4483 	struct i40e_vf *vf;
4484 	int ret = 0;
4485 
4486 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4487 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4488 		return -EAGAIN;
4489 	}
4490 
4491 	/* validate the request */
4492 	if (vf_id >= pf->num_alloc_vfs) {
4493 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4494 		ret = -EINVAL;
4495 		goto out;
4496 	}
4497 
4498 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4499 		dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
4500 		ret = -EINVAL;
4501 		goto out;
4502 	}
4503 
4504 	vf = &pf->vf[vf_id];
4505 
4506 	if (setting == vf->trusted)
4507 		goto out;
4508 
4509 	vf->trusted = setting;
4510 	i40e_vc_disable_vf(vf);
4511 	dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
4512 		 vf_id, setting ? "" : "un");
4513 
4514 	if (vf->adq_enabled) {
4515 		if (!vf->trusted) {
4516 			dev_info(&pf->pdev->dev,
4517 				 "VF %u no longer Trusted, deleting all cloud filters\n",
4518 				 vf_id);
4519 			i40e_del_all_cloud_filters(vf);
4520 		}
4521 	}
4522 
4523 out:
4524 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4525 	return ret;
4526 }
4527 
4528 /**
4529  * i40e_get_vf_stats - populate some stats for the VF
4530  * @netdev: the netdev of the PF
4531  * @vf_id: the host OS identifier (0-127)
4532  * @vf_stats: pointer to the OS memory to be initialized
4533  */
4534 int i40e_get_vf_stats(struct net_device *netdev, int vf_id,
4535 		      struct ifla_vf_stats *vf_stats)
4536 {
4537 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4538 	struct i40e_pf *pf = np->vsi->back;
4539 	struct i40e_eth_stats *stats;
4540 	struct i40e_vsi *vsi;
4541 	struct i40e_vf *vf;
4542 
4543 	/* validate the request */
4544 	if (i40e_validate_vf(pf, vf_id))
4545 		return -EINVAL;
4546 
4547 	vf = &pf->vf[vf_id];
4548 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4549 		dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
4550 		return -EBUSY;
4551 	}
4552 
4553 	vsi = pf->vsi[vf->lan_vsi_idx];
4554 	if (!vsi)
4555 		return -EINVAL;
4556 
4557 	i40e_update_eth_stats(vsi);
4558 	stats = &vsi->eth_stats;
4559 
4560 	memset(vf_stats, 0, sizeof(*vf_stats));
4561 
4562 	vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
4563 		stats->rx_multicast;
4564 	vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
4565 		stats->tx_multicast;
4566 	vf_stats->rx_bytes   = stats->rx_bytes;
4567 	vf_stats->tx_bytes   = stats->tx_bytes;
4568 	vf_stats->broadcast  = stats->rx_broadcast;
4569 	vf_stats->multicast  = stats->rx_multicast;
4570 	vf_stats->rx_dropped = stats->rx_discards;
4571 	vf_stats->tx_dropped = stats->tx_discards;
4572 
4573 	return 0;
4574 }
4575