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