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