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