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