xref: /linux/drivers/net/ethernet/intel/i40e/i40e_virtchnl_pf.c (revision ca6c080eef42e4149110f79cf73a48a6ec4e965d)
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 	int i, v;
1634 	u32 reg;
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 (v = 0; v < pf->num_alloc_vfs; v++) {
1646 		vf = &pf->vf[v];
1647 		/* If VF is being reset no need to trigger reset again */
1648 		if (!test_bit(I40E_VF_STATE_RESETTING, &vf->vf_states))
1649 			i40e_trigger_vf_reset(&pf->vf[v], flr);
1650 	}
1651 
1652 	/* HW requires some time to make sure it can flush the FIFO for a VF
1653 	 * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
1654 	 * sequence to make sure that it has completed. We'll keep track of
1655 	 * the VFs using a simple iterator that increments once that VF has
1656 	 * finished resetting.
1657 	 */
1658 	for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
1659 		usleep_range(10000, 20000);
1660 
1661 		/* Check each VF in sequence, beginning with the VF to fail
1662 		 * the previous check.
1663 		 */
1664 		while (v < pf->num_alloc_vfs) {
1665 			vf = &pf->vf[v];
1666 			if (!test_bit(I40E_VF_STATE_RESETTING, &vf->vf_states)) {
1667 				reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
1668 				if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
1669 					break;
1670 			}
1671 
1672 			/* If the current VF has finished resetting, move on
1673 			 * to the next VF in sequence.
1674 			 */
1675 			v++;
1676 		}
1677 	}
1678 
1679 	if (flr)
1680 		usleep_range(10000, 20000);
1681 
1682 	/* Display a warning if at least one VF didn't manage to reset in
1683 	 * time, but continue on with the operation.
1684 	 */
1685 	if (v < pf->num_alloc_vfs)
1686 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
1687 			pf->vf[v].vf_id);
1688 	usleep_range(10000, 20000);
1689 
1690 	/* Begin disabling all the rings associated with VFs, but do not wait
1691 	 * between each VF.
1692 	 */
1693 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1694 		/* On initial reset, we don't have any queues to disable */
1695 		if (pf->vf[v].lan_vsi_idx == 0)
1696 			continue;
1697 
1698 		/* If VF is reset in another thread just continue */
1699 		if (test_bit(I40E_VF_STATE_RESETTING, &vf->vf_states))
1700 			continue;
1701 
1702 		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
1703 	}
1704 
1705 	/* Now that we've notified HW to disable all of the VF rings, wait
1706 	 * until they finish.
1707 	 */
1708 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1709 		/* On initial reset, we don't have any queues to disable */
1710 		if (pf->vf[v].lan_vsi_idx == 0)
1711 			continue;
1712 
1713 		/* If VF is reset in another thread just continue */
1714 		if (test_bit(I40E_VF_STATE_RESETTING, &vf->vf_states))
1715 			continue;
1716 
1717 		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
1718 	}
1719 
1720 	/* Hw may need up to 50ms to finish disabling the RX queues. We
1721 	 * minimize the wait by delaying only once for all VFs.
1722 	 */
1723 	mdelay(50);
1724 
1725 	/* Finish the reset on each VF */
1726 	for (v = 0; v < pf->num_alloc_vfs; v++) {
1727 		/* If VF is reset in another thread just continue */
1728 		if (test_bit(I40E_VF_STATE_RESETTING, &vf->vf_states))
1729 			continue;
1730 
1731 		i40e_cleanup_reset_vf(&pf->vf[v]);
1732 	}
1733 
1734 	i40e_flush(hw);
1735 	usleep_range(20000, 40000);
1736 	clear_bit(__I40E_VF_DISABLE, pf->state);
1737 
1738 	return true;
1739 }
1740 
1741 /**
1742  * i40e_free_vfs
1743  * @pf: pointer to the PF structure
1744  *
1745  * free VF resources
1746  **/
1747 void i40e_free_vfs(struct i40e_pf *pf)
1748 {
1749 	struct i40e_hw *hw = &pf->hw;
1750 	u32 reg_idx, bit_idx;
1751 	int i, tmp, vf_id;
1752 
1753 	if (!pf->vf)
1754 		return;
1755 
1756 	set_bit(__I40E_VFS_RELEASING, pf->state);
1757 	while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
1758 		usleep_range(1000, 2000);
1759 
1760 	i40e_notify_client_of_vf_enable(pf, 0);
1761 
1762 	/* Disable IOV before freeing resources. This lets any VF drivers
1763 	 * running in the host get themselves cleaned up before we yank
1764 	 * the carpet out from underneath their feet.
1765 	 */
1766 	if (!pci_vfs_assigned(pf->pdev))
1767 		pci_disable_sriov(pf->pdev);
1768 	else
1769 		dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1770 
1771 	/* Amortize wait time by stopping all VFs at the same time */
1772 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1773 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1774 			continue;
1775 
1776 		i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
1777 	}
1778 
1779 	for (i = 0; i < pf->num_alloc_vfs; i++) {
1780 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1781 			continue;
1782 
1783 		i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
1784 	}
1785 
1786 	/* free up VF resources */
1787 	tmp = pf->num_alloc_vfs;
1788 	pf->num_alloc_vfs = 0;
1789 	for (i = 0; i < tmp; i++) {
1790 		if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
1791 			i40e_free_vf_res(&pf->vf[i]);
1792 		/* disable qp mappings */
1793 		i40e_disable_vf_mappings(&pf->vf[i]);
1794 	}
1795 
1796 	kfree(pf->vf);
1797 	pf->vf = NULL;
1798 
1799 	/* This check is for when the driver is unloaded while VFs are
1800 	 * assigned. Setting the number of VFs to 0 through sysfs is caught
1801 	 * before this function ever gets called.
1802 	 */
1803 	if (!pci_vfs_assigned(pf->pdev)) {
1804 		/* Acknowledge VFLR for all VFS. Without this, VFs will fail to
1805 		 * work correctly when SR-IOV gets re-enabled.
1806 		 */
1807 		for (vf_id = 0; vf_id < tmp; vf_id++) {
1808 			reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1809 			bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1810 			wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1811 		}
1812 	}
1813 	clear_bit(__I40E_VF_DISABLE, pf->state);
1814 	clear_bit(__I40E_VFS_RELEASING, pf->state);
1815 }
1816 
1817 #ifdef CONFIG_PCI_IOV
1818 /**
1819  * i40e_alloc_vfs
1820  * @pf: pointer to the PF structure
1821  * @num_alloc_vfs: number of VFs to allocate
1822  *
1823  * allocate VF resources
1824  **/
1825 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1826 {
1827 	struct i40e_vf *vfs;
1828 	int i, ret = 0;
1829 
1830 	/* Disable interrupt 0 so we don't try to handle the VFLR. */
1831 	i40e_irq_dynamic_disable_icr0(pf);
1832 
1833 	/* Check to see if we're just allocating resources for extant VFs */
1834 	if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
1835 		ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1836 		if (ret) {
1837 			pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1838 			pf->num_alloc_vfs = 0;
1839 			goto err_iov;
1840 		}
1841 	}
1842 	/* allocate memory */
1843 	vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1844 	if (!vfs) {
1845 		ret = -ENOMEM;
1846 		goto err_alloc;
1847 	}
1848 	pf->vf = vfs;
1849 
1850 	/* apply default profile */
1851 	for (i = 0; i < num_alloc_vfs; i++) {
1852 		vfs[i].pf = pf;
1853 		vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
1854 		vfs[i].vf_id = i;
1855 
1856 		/* assign default capabilities */
1857 		set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1858 		vfs[i].spoofchk = true;
1859 
1860 		set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
1861 
1862 	}
1863 	pf->num_alloc_vfs = num_alloc_vfs;
1864 
1865 	/* VF resources get allocated during reset */
1866 	i40e_reset_all_vfs(pf, false);
1867 
1868 	i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
1869 
1870 err_alloc:
1871 	if (ret)
1872 		i40e_free_vfs(pf);
1873 err_iov:
1874 	/* Re-enable interrupt 0. */
1875 	i40e_irq_dynamic_enable_icr0(pf);
1876 	return ret;
1877 }
1878 
1879 #endif
1880 /**
1881  * i40e_pci_sriov_enable
1882  * @pdev: pointer to a pci_dev structure
1883  * @num_vfs: number of VFs to allocate
1884  *
1885  * Enable or change the number of VFs
1886  **/
1887 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1888 {
1889 #ifdef CONFIG_PCI_IOV
1890 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1891 	int pre_existing_vfs = pci_num_vf(pdev);
1892 	int err = 0;
1893 
1894 	if (test_bit(__I40E_TESTING, pf->state)) {
1895 		dev_warn(&pdev->dev,
1896 			 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
1897 		err = -EPERM;
1898 		goto err_out;
1899 	}
1900 
1901 	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1902 		i40e_free_vfs(pf);
1903 	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1904 		goto out;
1905 
1906 	if (num_vfs > pf->num_req_vfs) {
1907 		dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1908 			 num_vfs, pf->num_req_vfs);
1909 		err = -EPERM;
1910 		goto err_out;
1911 	}
1912 
1913 	dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1914 	err = i40e_alloc_vfs(pf, num_vfs);
1915 	if (err) {
1916 		dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1917 		goto err_out;
1918 	}
1919 
1920 out:
1921 	return num_vfs;
1922 
1923 err_out:
1924 	return err;
1925 #endif
1926 	return 0;
1927 }
1928 
1929 /**
1930  * i40e_pci_sriov_configure
1931  * @pdev: pointer to a pci_dev structure
1932  * @num_vfs: number of VFs to allocate
1933  *
1934  * Enable or change the number of VFs. Called when the user updates the number
1935  * of VFs in sysfs.
1936  **/
1937 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1938 {
1939 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1940 	int ret = 0;
1941 
1942 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
1943 		dev_warn(&pdev->dev, "Unable to configure VFs, other operation is pending.\n");
1944 		return -EAGAIN;
1945 	}
1946 
1947 	if (num_vfs) {
1948 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1949 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1950 			i40e_do_reset_safe(pf, I40E_PF_RESET_AND_REBUILD_FLAG);
1951 		}
1952 		ret = i40e_pci_sriov_enable(pdev, num_vfs);
1953 		goto sriov_configure_out;
1954 	}
1955 
1956 	if (!pci_vfs_assigned(pf->pdev)) {
1957 		i40e_free_vfs(pf);
1958 		pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1959 		i40e_do_reset_safe(pf, I40E_PF_RESET_AND_REBUILD_FLAG);
1960 	} else {
1961 		dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1962 		ret = -EINVAL;
1963 		goto sriov_configure_out;
1964 	}
1965 sriov_configure_out:
1966 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
1967 	return ret;
1968 }
1969 
1970 /***********************virtual channel routines******************/
1971 
1972 /**
1973  * i40e_vc_send_msg_to_vf
1974  * @vf: pointer to the VF info
1975  * @v_opcode: virtual channel opcode
1976  * @v_retval: virtual channel return value
1977  * @msg: pointer to the msg buffer
1978  * @msglen: msg length
1979  *
1980  * send msg to VF
1981  **/
1982 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1983 				  u32 v_retval, u8 *msg, u16 msglen)
1984 {
1985 	struct i40e_pf *pf;
1986 	struct i40e_hw *hw;
1987 	int abs_vf_id;
1988 	int aq_ret;
1989 
1990 	/* validate the request */
1991 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1992 		return -EINVAL;
1993 
1994 	pf = vf->pf;
1995 	hw = &pf->hw;
1996 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1997 
1998 	aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,	v_opcode, v_retval,
1999 					msg, msglen, NULL);
2000 	if (aq_ret) {
2001 		dev_info(&pf->pdev->dev,
2002 			 "Unable to send the message to VF %d aq_err %d\n",
2003 			 vf->vf_id, pf->hw.aq.asq_last_status);
2004 		return -EIO;
2005 	}
2006 
2007 	return 0;
2008 }
2009 
2010 /**
2011  * i40e_vc_send_resp_to_vf
2012  * @vf: pointer to the VF info
2013  * @opcode: operation code
2014  * @retval: return value
2015  *
2016  * send resp msg to VF
2017  **/
2018 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
2019 				   enum virtchnl_ops opcode,
2020 				   int retval)
2021 {
2022 	return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
2023 }
2024 
2025 /**
2026  * i40e_sync_vf_state
2027  * @vf: pointer to the VF info
2028  * @state: VF state
2029  *
2030  * Called from a VF message to synchronize the service with a potential
2031  * VF reset state
2032  **/
2033 static bool i40e_sync_vf_state(struct i40e_vf *vf, enum i40e_vf_states state)
2034 {
2035 	int i;
2036 
2037 	/* When handling some messages, it needs VF state to be set.
2038 	 * It is possible that this flag is cleared during VF reset,
2039 	 * so there is a need to wait until the end of the reset to
2040 	 * handle the request message correctly.
2041 	 */
2042 	for (i = 0; i < I40E_VF_STATE_WAIT_COUNT; i++) {
2043 		if (test_bit(state, &vf->vf_states))
2044 			return true;
2045 		usleep_range(10000, 20000);
2046 	}
2047 
2048 	return test_bit(state, &vf->vf_states);
2049 }
2050 
2051 /**
2052  * i40e_vc_get_version_msg
2053  * @vf: pointer to the VF info
2054  * @msg: pointer to the msg buffer
2055  *
2056  * called from the VF to request the API version used by the PF
2057  **/
2058 static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
2059 {
2060 	struct virtchnl_version_info info = {
2061 		VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
2062 	};
2063 
2064 	vf->vf_ver = *(struct virtchnl_version_info *)msg;
2065 	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
2066 	if (VF_IS_V10(&vf->vf_ver))
2067 		info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
2068 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
2069 				      0, (u8 *)&info,
2070 				      sizeof(struct virtchnl_version_info));
2071 }
2072 
2073 /**
2074  * i40e_del_qch - delete all the additional VSIs created as a part of ADq
2075  * @vf: pointer to VF structure
2076  **/
2077 static void i40e_del_qch(struct i40e_vf *vf)
2078 {
2079 	struct i40e_pf *pf = vf->pf;
2080 	int i;
2081 
2082 	/* first element in the array belongs to primary VF VSI and we shouldn't
2083 	 * delete it. We should however delete the rest of the VSIs created
2084 	 */
2085 	for (i = 1; i < vf->num_tc; i++) {
2086 		if (vf->ch[i].vsi_idx) {
2087 			i40e_vsi_release(pf->vsi[vf->ch[i].vsi_idx]);
2088 			vf->ch[i].vsi_idx = 0;
2089 			vf->ch[i].vsi_id = 0;
2090 		}
2091 	}
2092 }
2093 
2094 /**
2095  * i40e_vc_get_max_frame_size
2096  * @vf: pointer to the VF
2097  *
2098  * Max frame size is determined based on the current port's max frame size and
2099  * whether a port VLAN is configured on this VF. The VF is not aware whether
2100  * it's in a port VLAN so the PF needs to account for this in max frame size
2101  * checks and sending the max frame size to the VF.
2102  **/
2103 static u16 i40e_vc_get_max_frame_size(struct i40e_vf *vf)
2104 {
2105 	u16 max_frame_size = vf->pf->hw.phy.link_info.max_frame_size;
2106 
2107 	if (vf->port_vlan_id)
2108 		max_frame_size -= VLAN_HLEN;
2109 
2110 	return max_frame_size;
2111 }
2112 
2113 /**
2114  * i40e_vc_get_vf_resources_msg
2115  * @vf: pointer to the VF info
2116  * @msg: pointer to the msg buffer
2117  *
2118  * called from the VF to request its resources
2119  **/
2120 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
2121 {
2122 	struct virtchnl_vf_resource *vfres = NULL;
2123 	struct i40e_pf *pf = vf->pf;
2124 	struct i40e_vsi *vsi;
2125 	int num_vsis = 1;
2126 	int aq_ret = 0;
2127 	size_t len = 0;
2128 	int ret;
2129 
2130 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_INIT)) {
2131 		aq_ret = -EINVAL;
2132 		goto err;
2133 	}
2134 
2135 	len = virtchnl_struct_size(vfres, vsi_res, num_vsis);
2136 	vfres = kzalloc(len, GFP_KERNEL);
2137 	if (!vfres) {
2138 		aq_ret = -ENOMEM;
2139 		len = 0;
2140 		goto err;
2141 	}
2142 	if (VF_IS_V11(&vf->vf_ver))
2143 		vf->driver_caps = *(u32 *)msg;
2144 	else
2145 		vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
2146 				  VIRTCHNL_VF_OFFLOAD_RSS_REG |
2147 				  VIRTCHNL_VF_OFFLOAD_VLAN;
2148 
2149 	vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
2150 	vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
2151 	vsi = pf->vsi[vf->lan_vsi_idx];
2152 	if (!vsi->info.pvid)
2153 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
2154 
2155 	if (i40e_vf_client_capable(pf, vf->vf_id) &&
2156 	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RDMA)) {
2157 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RDMA;
2158 		set_bit(I40E_VF_STATE_RDMAENA, &vf->vf_states);
2159 	} else {
2160 		clear_bit(I40E_VF_STATE_RDMAENA, &vf->vf_states);
2161 	}
2162 
2163 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2164 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
2165 	} else {
2166 		if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
2167 		    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
2168 			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
2169 		else
2170 			vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
2171 	}
2172 
2173 	if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
2174 		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
2175 			vfres->vf_cap_flags |=
2176 				VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
2177 	}
2178 
2179 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
2180 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
2181 
2182 	if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
2183 	    (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
2184 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
2185 
2186 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
2187 		if (pf->flags & I40E_FLAG_MFP_ENABLED) {
2188 			dev_err(&pf->pdev->dev,
2189 				"VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
2190 				 vf->vf_id);
2191 			aq_ret = -EINVAL;
2192 			goto err;
2193 		}
2194 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
2195 	}
2196 
2197 	if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
2198 		if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
2199 			vfres->vf_cap_flags |=
2200 					VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
2201 	}
2202 
2203 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
2204 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
2205 
2206 	if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)
2207 		vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADQ;
2208 
2209 	vfres->num_vsis = num_vsis;
2210 	vfres->num_queue_pairs = vf->num_queue_pairs;
2211 	vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
2212 	vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
2213 	vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
2214 	vfres->max_mtu = i40e_vc_get_max_frame_size(vf);
2215 
2216 	if (vf->lan_vsi_idx) {
2217 		vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
2218 		vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
2219 		vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
2220 		/* VFs only use TC 0 */
2221 		vfres->vsi_res[0].qset_handle
2222 					  = le16_to_cpu(vsi->info.qs_handle[0]);
2223 		if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_USO) && !vf->pf_set_mac) {
2224 			i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
2225 			eth_zero_addr(vf->default_lan_addr.addr);
2226 		}
2227 		ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
2228 				vf->default_lan_addr.addr);
2229 	}
2230 	set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
2231 
2232 err:
2233 	/* send the response back to the VF */
2234 	ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
2235 				     aq_ret, (u8 *)vfres, len);
2236 
2237 	kfree(vfres);
2238 	return ret;
2239 }
2240 
2241 /**
2242  * i40e_vc_config_promiscuous_mode_msg
2243  * @vf: pointer to the VF info
2244  * @msg: pointer to the msg buffer
2245  *
2246  * called from the VF to configure the promiscuous mode of
2247  * VF vsis
2248  **/
2249 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf, u8 *msg)
2250 {
2251 	struct virtchnl_promisc_info *info =
2252 	    (struct virtchnl_promisc_info *)msg;
2253 	struct i40e_pf *pf = vf->pf;
2254 	bool allmulti = false;
2255 	bool alluni = false;
2256 	int aq_ret = 0;
2257 
2258 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2259 		aq_ret = -EINVAL;
2260 		goto err_out;
2261 	}
2262 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2263 		dev_err(&pf->pdev->dev,
2264 			"Unprivileged VF %d is attempting to configure promiscuous mode\n",
2265 			vf->vf_id);
2266 
2267 		/* Lie to the VF on purpose, because this is an error we can
2268 		 * ignore. Unprivileged VF is not a virtual channel error.
2269 		 */
2270 		aq_ret = 0;
2271 		goto err_out;
2272 	}
2273 
2274 	if (info->flags > I40E_MAX_VF_PROMISC_FLAGS) {
2275 		aq_ret = -EINVAL;
2276 		goto err_out;
2277 	}
2278 
2279 	if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
2280 		aq_ret = -EINVAL;
2281 		goto err_out;
2282 	}
2283 
2284 	/* Multicast promiscuous handling*/
2285 	if (info->flags & FLAG_VF_MULTICAST_PROMISC)
2286 		allmulti = true;
2287 
2288 	if (info->flags & FLAG_VF_UNICAST_PROMISC)
2289 		alluni = true;
2290 	aq_ret = i40e_config_vf_promiscuous_mode(vf, info->vsi_id, allmulti,
2291 						 alluni);
2292 	if (aq_ret)
2293 		goto err_out;
2294 
2295 	if (allmulti) {
2296 		if (!test_and_set_bit(I40E_VF_STATE_MC_PROMISC,
2297 				      &vf->vf_states))
2298 			dev_info(&pf->pdev->dev,
2299 				 "VF %d successfully set multicast promiscuous mode\n",
2300 				 vf->vf_id);
2301 	} else if (test_and_clear_bit(I40E_VF_STATE_MC_PROMISC,
2302 				      &vf->vf_states))
2303 		dev_info(&pf->pdev->dev,
2304 			 "VF %d successfully unset multicast promiscuous mode\n",
2305 			 vf->vf_id);
2306 
2307 	if (alluni) {
2308 		if (!test_and_set_bit(I40E_VF_STATE_UC_PROMISC,
2309 				      &vf->vf_states))
2310 			dev_info(&pf->pdev->dev,
2311 				 "VF %d successfully set unicast promiscuous mode\n",
2312 				 vf->vf_id);
2313 	} else if (test_and_clear_bit(I40E_VF_STATE_UC_PROMISC,
2314 				      &vf->vf_states))
2315 		dev_info(&pf->pdev->dev,
2316 			 "VF %d successfully unset unicast promiscuous mode\n",
2317 			 vf->vf_id);
2318 
2319 err_out:
2320 	/* send the response to the VF */
2321 	return i40e_vc_send_resp_to_vf(vf,
2322 				       VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
2323 				       aq_ret);
2324 }
2325 
2326 /**
2327  * i40e_vc_config_queues_msg
2328  * @vf: pointer to the VF info
2329  * @msg: pointer to the msg buffer
2330  *
2331  * called from the VF to configure the rx/tx
2332  * queues
2333  **/
2334 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg)
2335 {
2336 	struct virtchnl_vsi_queue_config_info *qci =
2337 	    (struct virtchnl_vsi_queue_config_info *)msg;
2338 	struct virtchnl_queue_pair_info *qpi;
2339 	u16 vsi_id, vsi_queue_id = 0;
2340 	struct i40e_pf *pf = vf->pf;
2341 	int i, j = 0, idx = 0;
2342 	struct i40e_vsi *vsi;
2343 	u16 num_qps_all = 0;
2344 	int aq_ret = 0;
2345 
2346 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2347 		aq_ret = -EINVAL;
2348 		goto error_param;
2349 	}
2350 
2351 	if (!i40e_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
2352 		aq_ret = -EINVAL;
2353 		goto error_param;
2354 	}
2355 
2356 	if (qci->num_queue_pairs > I40E_MAX_VF_QUEUES) {
2357 		aq_ret = -EINVAL;
2358 		goto error_param;
2359 	}
2360 
2361 	if (vf->adq_enabled) {
2362 		for (i = 0; i < vf->num_tc; i++)
2363 			num_qps_all += vf->ch[i].num_qps;
2364 		if (num_qps_all != qci->num_queue_pairs) {
2365 			aq_ret = -EINVAL;
2366 			goto error_param;
2367 		}
2368 	}
2369 
2370 	vsi_id = qci->vsi_id;
2371 
2372 	for (i = 0; i < qci->num_queue_pairs; i++) {
2373 		qpi = &qci->qpair[i];
2374 
2375 		if (!vf->adq_enabled) {
2376 			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
2377 						      qpi->txq.queue_id)) {
2378 				aq_ret = -EINVAL;
2379 				goto error_param;
2380 			}
2381 
2382 			vsi_queue_id = qpi->txq.queue_id;
2383 
2384 			if (qpi->txq.vsi_id != qci->vsi_id ||
2385 			    qpi->rxq.vsi_id != qci->vsi_id ||
2386 			    qpi->rxq.queue_id != vsi_queue_id) {
2387 				aq_ret = -EINVAL;
2388 				goto error_param;
2389 			}
2390 		}
2391 
2392 		if (vf->adq_enabled) {
2393 			if (idx >= ARRAY_SIZE(vf->ch)) {
2394 				aq_ret = -ENODEV;
2395 				goto error_param;
2396 			}
2397 			vsi_id = vf->ch[idx].vsi_id;
2398 		}
2399 
2400 		if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
2401 					     &qpi->rxq) ||
2402 		    i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
2403 					     &qpi->txq)) {
2404 			aq_ret = -EINVAL;
2405 			goto error_param;
2406 		}
2407 
2408 		/* For ADq there can be up to 4 VSIs with max 4 queues each.
2409 		 * VF does not know about these additional VSIs and all
2410 		 * it cares is about its own queues. PF configures these queues
2411 		 * to its appropriate VSIs based on TC mapping
2412 		 */
2413 		if (vf->adq_enabled) {
2414 			if (idx >= ARRAY_SIZE(vf->ch)) {
2415 				aq_ret = -ENODEV;
2416 				goto error_param;
2417 			}
2418 			if (j == (vf->ch[idx].num_qps - 1)) {
2419 				idx++;
2420 				j = 0; /* resetting the queue count */
2421 				vsi_queue_id = 0;
2422 			} else {
2423 				j++;
2424 				vsi_queue_id++;
2425 			}
2426 		}
2427 	}
2428 	/* set vsi num_queue_pairs in use to num configured by VF */
2429 	if (!vf->adq_enabled) {
2430 		pf->vsi[vf->lan_vsi_idx]->num_queue_pairs =
2431 			qci->num_queue_pairs;
2432 	} else {
2433 		for (i = 0; i < vf->num_tc; i++) {
2434 			vsi = pf->vsi[vf->ch[i].vsi_idx];
2435 			vsi->num_queue_pairs = vf->ch[i].num_qps;
2436 
2437 			if (i40e_update_adq_vsi_queues(vsi, i)) {
2438 				aq_ret = -EIO;
2439 				goto error_param;
2440 			}
2441 		}
2442 	}
2443 
2444 error_param:
2445 	/* send the response to the VF */
2446 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
2447 				       aq_ret);
2448 }
2449 
2450 /**
2451  * i40e_validate_queue_map - check queue map is valid
2452  * @vf: the VF structure pointer
2453  * @vsi_id: vsi id
2454  * @queuemap: Tx or Rx queue map
2455  *
2456  * check if Tx or Rx queue map is valid
2457  **/
2458 static int i40e_validate_queue_map(struct i40e_vf *vf, u16 vsi_id,
2459 				   unsigned long queuemap)
2460 {
2461 	u16 vsi_queue_id, queue_id;
2462 
2463 	for_each_set_bit(vsi_queue_id, &queuemap, I40E_MAX_VSI_QP) {
2464 		if (vf->adq_enabled) {
2465 			vsi_id = vf->ch[vsi_queue_id / I40E_MAX_VF_VSI].vsi_id;
2466 			queue_id = (vsi_queue_id % I40E_DEFAULT_QUEUES_PER_VF);
2467 		} else {
2468 			queue_id = vsi_queue_id;
2469 		}
2470 
2471 		if (!i40e_vc_isvalid_queue_id(vf, vsi_id, queue_id))
2472 			return -EINVAL;
2473 	}
2474 
2475 	return 0;
2476 }
2477 
2478 /**
2479  * i40e_vc_config_irq_map_msg
2480  * @vf: pointer to the VF info
2481  * @msg: pointer to the msg buffer
2482  *
2483  * called from the VF to configure the irq to
2484  * queue map
2485  **/
2486 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg)
2487 {
2488 	struct virtchnl_irq_map_info *irqmap_info =
2489 	    (struct virtchnl_irq_map_info *)msg;
2490 	struct virtchnl_vector_map *map;
2491 	int aq_ret = 0;
2492 	u16 vsi_id;
2493 	int i;
2494 
2495 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2496 		aq_ret = -EINVAL;
2497 		goto error_param;
2498 	}
2499 
2500 	if (irqmap_info->num_vectors >
2501 	    vf->pf->hw.func_caps.num_msix_vectors_vf) {
2502 		aq_ret = -EINVAL;
2503 		goto error_param;
2504 	}
2505 
2506 	for (i = 0; i < irqmap_info->num_vectors; i++) {
2507 		map = &irqmap_info->vecmap[i];
2508 		/* validate msg params */
2509 		if (!i40e_vc_isvalid_vector_id(vf, map->vector_id) ||
2510 		    !i40e_vc_isvalid_vsi_id(vf, map->vsi_id)) {
2511 			aq_ret = -EINVAL;
2512 			goto error_param;
2513 		}
2514 		vsi_id = map->vsi_id;
2515 
2516 		if (i40e_validate_queue_map(vf, vsi_id, map->rxq_map)) {
2517 			aq_ret = -EINVAL;
2518 			goto error_param;
2519 		}
2520 
2521 		if (i40e_validate_queue_map(vf, vsi_id, map->txq_map)) {
2522 			aq_ret = -EINVAL;
2523 			goto error_param;
2524 		}
2525 
2526 		i40e_config_irq_link_list(vf, vsi_id, map);
2527 	}
2528 error_param:
2529 	/* send the response to the VF */
2530 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
2531 				       aq_ret);
2532 }
2533 
2534 /**
2535  * i40e_ctrl_vf_tx_rings
2536  * @vsi: the SRIOV VSI being configured
2537  * @q_map: bit map of the queues to be enabled
2538  * @enable: start or stop the queue
2539  **/
2540 static int i40e_ctrl_vf_tx_rings(struct i40e_vsi *vsi, unsigned long q_map,
2541 				 bool enable)
2542 {
2543 	struct i40e_pf *pf = vsi->back;
2544 	int ret = 0;
2545 	u16 q_id;
2546 
2547 	for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
2548 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
2549 					     vsi->base_queue + q_id,
2550 					     false /*is xdp*/, enable);
2551 		if (ret)
2552 			break;
2553 	}
2554 	return ret;
2555 }
2556 
2557 /**
2558  * i40e_ctrl_vf_rx_rings
2559  * @vsi: the SRIOV VSI being configured
2560  * @q_map: bit map of the queues to be enabled
2561  * @enable: start or stop the queue
2562  **/
2563 static int i40e_ctrl_vf_rx_rings(struct i40e_vsi *vsi, unsigned long q_map,
2564 				 bool enable)
2565 {
2566 	struct i40e_pf *pf = vsi->back;
2567 	int ret = 0;
2568 	u16 q_id;
2569 
2570 	for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
2571 		ret = i40e_control_wait_rx_q(pf, vsi->base_queue + q_id,
2572 					     enable);
2573 		if (ret)
2574 			break;
2575 	}
2576 	return ret;
2577 }
2578 
2579 /**
2580  * i40e_vc_validate_vqs_bitmaps - validate Rx/Tx queue bitmaps from VIRTHCHNL
2581  * @vqs: virtchnl_queue_select structure containing bitmaps to validate
2582  *
2583  * Returns true if validation was successful, else false.
2584  */
2585 static bool i40e_vc_validate_vqs_bitmaps(struct virtchnl_queue_select *vqs)
2586 {
2587 	if ((!vqs->rx_queues && !vqs->tx_queues) ||
2588 	    vqs->rx_queues >= BIT(I40E_MAX_VF_QUEUES) ||
2589 	    vqs->tx_queues >= BIT(I40E_MAX_VF_QUEUES))
2590 		return false;
2591 
2592 	return true;
2593 }
2594 
2595 /**
2596  * i40e_vc_enable_queues_msg
2597  * @vf: pointer to the VF info
2598  * @msg: pointer to the msg buffer
2599  *
2600  * called from the VF to enable all or specific queue(s)
2601  **/
2602 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg)
2603 {
2604 	struct virtchnl_queue_select *vqs =
2605 	    (struct virtchnl_queue_select *)msg;
2606 	struct i40e_pf *pf = vf->pf;
2607 	int aq_ret = 0;
2608 	int i;
2609 
2610 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
2611 		aq_ret = -EINVAL;
2612 		goto error_param;
2613 	}
2614 
2615 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2616 		aq_ret = -EINVAL;
2617 		goto error_param;
2618 	}
2619 
2620 	if (!i40e_vc_validate_vqs_bitmaps(vqs)) {
2621 		aq_ret = -EINVAL;
2622 		goto error_param;
2623 	}
2624 
2625 	/* Use the queue bit map sent by the VF */
2626 	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2627 				  true)) {
2628 		aq_ret = -EIO;
2629 		goto error_param;
2630 	}
2631 	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2632 				  true)) {
2633 		aq_ret = -EIO;
2634 		goto error_param;
2635 	}
2636 
2637 	/* need to start the rings for additional ADq VSI's as well */
2638 	if (vf->adq_enabled) {
2639 		/* zero belongs to LAN VSI */
2640 		for (i = 1; i < vf->num_tc; i++) {
2641 			if (i40e_vsi_start_rings(pf->vsi[vf->ch[i].vsi_idx]))
2642 				aq_ret = -EIO;
2643 		}
2644 	}
2645 
2646 error_param:
2647 	/* send the response to the VF */
2648 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
2649 				       aq_ret);
2650 }
2651 
2652 /**
2653  * i40e_vc_disable_queues_msg
2654  * @vf: pointer to the VF info
2655  * @msg: pointer to the msg buffer
2656  *
2657  * called from the VF to disable all or specific
2658  * queue(s)
2659  **/
2660 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg)
2661 {
2662 	struct virtchnl_queue_select *vqs =
2663 	    (struct virtchnl_queue_select *)msg;
2664 	struct i40e_pf *pf = vf->pf;
2665 	int aq_ret = 0;
2666 
2667 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2668 		aq_ret = -EINVAL;
2669 		goto error_param;
2670 	}
2671 
2672 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2673 		aq_ret = -EINVAL;
2674 		goto error_param;
2675 	}
2676 
2677 	if (!i40e_vc_validate_vqs_bitmaps(vqs)) {
2678 		aq_ret = -EINVAL;
2679 		goto error_param;
2680 	}
2681 
2682 	/* Use the queue bit map sent by the VF */
2683 	if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
2684 				  false)) {
2685 		aq_ret = -EIO;
2686 		goto error_param;
2687 	}
2688 	if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
2689 				  false)) {
2690 		aq_ret = -EIO;
2691 		goto error_param;
2692 	}
2693 error_param:
2694 	/* send the response to the VF */
2695 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
2696 				       aq_ret);
2697 }
2698 
2699 /**
2700  * i40e_check_enough_queue - find big enough queue number
2701  * @vf: pointer to the VF info
2702  * @needed: the number of items needed
2703  *
2704  * Returns the base item index of the queue, or negative for error
2705  **/
2706 static int i40e_check_enough_queue(struct i40e_vf *vf, u16 needed)
2707 {
2708 	unsigned int  i, cur_queues, more, pool_size;
2709 	struct i40e_lump_tracking *pile;
2710 	struct i40e_pf *pf = vf->pf;
2711 	struct i40e_vsi *vsi;
2712 
2713 	vsi = pf->vsi[vf->lan_vsi_idx];
2714 	cur_queues = vsi->alloc_queue_pairs;
2715 
2716 	/* if current allocated queues are enough for need */
2717 	if (cur_queues >= needed)
2718 		return vsi->base_queue;
2719 
2720 	pile = pf->qp_pile;
2721 	if (cur_queues > 0) {
2722 		/* if the allocated queues are not zero
2723 		 * just check if there are enough queues for more
2724 		 * behind the allocated queues.
2725 		 */
2726 		more = needed - cur_queues;
2727 		for (i = vsi->base_queue + cur_queues;
2728 			i < pile->num_entries; i++) {
2729 			if (pile->list[i] & I40E_PILE_VALID_BIT)
2730 				break;
2731 
2732 			if (more-- == 1)
2733 				/* there is enough */
2734 				return vsi->base_queue;
2735 		}
2736 	}
2737 
2738 	pool_size = 0;
2739 	for (i = 0; i < pile->num_entries; i++) {
2740 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
2741 			pool_size = 0;
2742 			continue;
2743 		}
2744 		if (needed <= ++pool_size)
2745 			/* there is enough */
2746 			return i;
2747 	}
2748 
2749 	return -ENOMEM;
2750 }
2751 
2752 /**
2753  * i40e_vc_request_queues_msg
2754  * @vf: pointer to the VF info
2755  * @msg: pointer to the msg buffer
2756  *
2757  * VFs get a default number of queues but can use this message to request a
2758  * different number.  If the request is successful, PF will reset the VF and
2759  * return 0.  If unsuccessful, PF will send message informing VF of number of
2760  * available queues and return result of sending VF a message.
2761  **/
2762 static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg)
2763 {
2764 	struct virtchnl_vf_res_request *vfres =
2765 		(struct virtchnl_vf_res_request *)msg;
2766 	u16 req_pairs = vfres->num_queue_pairs;
2767 	u8 cur_pairs = vf->num_queue_pairs;
2768 	struct i40e_pf *pf = vf->pf;
2769 
2770 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE))
2771 		return -EINVAL;
2772 
2773 	if (req_pairs > I40E_MAX_VF_QUEUES) {
2774 		dev_err(&pf->pdev->dev,
2775 			"VF %d tried to request more than %d queues.\n",
2776 			vf->vf_id,
2777 			I40E_MAX_VF_QUEUES);
2778 		vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
2779 	} else if (req_pairs - cur_pairs > pf->queues_left) {
2780 		dev_warn(&pf->pdev->dev,
2781 			 "VF %d requested %d more queues, but only %d left.\n",
2782 			 vf->vf_id,
2783 			 req_pairs - cur_pairs,
2784 			 pf->queues_left);
2785 		vfres->num_queue_pairs = pf->queues_left + cur_pairs;
2786 	} else if (i40e_check_enough_queue(vf, req_pairs) < 0) {
2787 		dev_warn(&pf->pdev->dev,
2788 			 "VF %d requested %d more queues, but there is not enough for it.\n",
2789 			 vf->vf_id,
2790 			 req_pairs - cur_pairs);
2791 		vfres->num_queue_pairs = cur_pairs;
2792 	} else {
2793 		/* successful request */
2794 		vf->num_req_queues = req_pairs;
2795 		i40e_vc_reset_vf(vf, true);
2796 		return 0;
2797 	}
2798 
2799 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
2800 				      (u8 *)vfres, sizeof(*vfres));
2801 }
2802 
2803 /**
2804  * i40e_vc_get_stats_msg
2805  * @vf: pointer to the VF info
2806  * @msg: pointer to the msg buffer
2807  *
2808  * called from the VF to get vsi stats
2809  **/
2810 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg)
2811 {
2812 	struct virtchnl_queue_select *vqs =
2813 	    (struct virtchnl_queue_select *)msg;
2814 	struct i40e_pf *pf = vf->pf;
2815 	struct i40e_eth_stats stats;
2816 	int aq_ret = 0;
2817 	struct i40e_vsi *vsi;
2818 
2819 	memset(&stats, 0, sizeof(struct i40e_eth_stats));
2820 
2821 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
2822 		aq_ret = -EINVAL;
2823 		goto error_param;
2824 	}
2825 
2826 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
2827 		aq_ret = -EINVAL;
2828 		goto error_param;
2829 	}
2830 
2831 	vsi = pf->vsi[vf->lan_vsi_idx];
2832 	if (!vsi) {
2833 		aq_ret = -EINVAL;
2834 		goto error_param;
2835 	}
2836 	i40e_update_eth_stats(vsi);
2837 	stats = vsi->eth_stats;
2838 
2839 error_param:
2840 	/* send the response back to the VF */
2841 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
2842 				      (u8 *)&stats, sizeof(stats));
2843 }
2844 
2845 #define I40E_MAX_MACVLAN_PER_HW 3072
2846 #define I40E_MAX_MACVLAN_PER_PF(num_ports) (I40E_MAX_MACVLAN_PER_HW /	\
2847 	(num_ports))
2848 /* If the VF is not trusted restrict the number of MAC/VLAN it can program
2849  * MAC filters: 16 for multicast, 1 for MAC, 1 for broadcast
2850  */
2851 #define I40E_VC_MAX_MAC_ADDR_PER_VF (16 + 1 + 1)
2852 #define I40E_VC_MAX_VLAN_PER_VF 16
2853 
2854 #define I40E_VC_MAX_MACVLAN_PER_TRUSTED_VF(vf_num, num_ports)		\
2855 ({	typeof(vf_num) vf_num_ = (vf_num);				\
2856 	typeof(num_ports) num_ports_ = (num_ports);			\
2857 	((I40E_MAX_MACVLAN_PER_PF(num_ports_) - vf_num_ *		\
2858 	I40E_VC_MAX_MAC_ADDR_PER_VF) / vf_num_) +			\
2859 	I40E_VC_MAX_MAC_ADDR_PER_VF; })
2860 /**
2861  * i40e_check_vf_permission
2862  * @vf: pointer to the VF info
2863  * @al: MAC address list from virtchnl
2864  *
2865  * Check that the given list of MAC addresses is allowed. Will return -EPERM
2866  * if any address in the list is not valid. Checks the following conditions:
2867  *
2868  * 1) broadcast and zero addresses are never valid
2869  * 2) unicast addresses are not allowed if the VMM has administratively set
2870  *    the VF MAC address, unless the VF is marked as privileged.
2871  * 3) There is enough space to add all the addresses.
2872  *
2873  * Note that to guarantee consistency, it is expected this function be called
2874  * while holding the mac_filter_hash_lock, as otherwise the current number of
2875  * addresses might not be accurate.
2876  **/
2877 static inline int i40e_check_vf_permission(struct i40e_vf *vf,
2878 					   struct virtchnl_ether_addr_list *al)
2879 {
2880 	struct i40e_pf *pf = vf->pf;
2881 	struct i40e_vsi *vsi = pf->vsi[vf->lan_vsi_idx];
2882 	struct i40e_hw *hw = &pf->hw;
2883 	int mac2add_cnt = 0;
2884 	int i;
2885 
2886 	for (i = 0; i < al->num_elements; i++) {
2887 		struct i40e_mac_filter *f;
2888 		u8 *addr = al->list[i].addr;
2889 
2890 		if (is_broadcast_ether_addr(addr) ||
2891 		    is_zero_ether_addr(addr)) {
2892 			dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
2893 				addr);
2894 			return -EINVAL;
2895 		}
2896 
2897 		/* If the host VMM administrator has set the VF MAC address
2898 		 * administratively via the ndo_set_vf_mac command then deny
2899 		 * permission to the VF to add or delete unicast MAC addresses.
2900 		 * Unless the VF is privileged and then it can do whatever.
2901 		 * The VF may request to set the MAC address filter already
2902 		 * assigned to it so do not return an error in that case.
2903 		 */
2904 		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
2905 		    !is_multicast_ether_addr(addr) && vf->pf_set_mac &&
2906 		    !ether_addr_equal(addr, vf->default_lan_addr.addr)) {
2907 			dev_err(&pf->pdev->dev,
2908 				"VF attempting to override administratively set MAC address, bring down and up the VF interface to resume normal operation\n");
2909 			return -EPERM;
2910 		}
2911 
2912 		/*count filters that really will be added*/
2913 		f = i40e_find_mac(vsi, addr);
2914 		if (!f)
2915 			++mac2add_cnt;
2916 	}
2917 
2918 	/* If this VF is not privileged, then we can't add more than a limited
2919 	 * number of addresses. Check to make sure that the additions do not
2920 	 * push us over the limit.
2921 	 */
2922 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2923 		if ((i40e_count_filters(vsi) + mac2add_cnt) >
2924 		    I40E_VC_MAX_MAC_ADDR_PER_VF) {
2925 			dev_err(&pf->pdev->dev,
2926 				"Cannot add more MAC addresses, VF is not trusted, switch the VF to trusted to add more functionality\n");
2927 			return -EPERM;
2928 		}
2929 	/* If this VF is trusted, it can use more resources than untrusted.
2930 	 * However to ensure that every trusted VF has appropriate number of
2931 	 * resources, divide whole pool of resources per port and then across
2932 	 * all VFs.
2933 	 */
2934 	} else {
2935 		if ((i40e_count_filters(vsi) + mac2add_cnt) >
2936 		    I40E_VC_MAX_MACVLAN_PER_TRUSTED_VF(pf->num_alloc_vfs,
2937 						       hw->num_ports)) {
2938 			dev_err(&pf->pdev->dev,
2939 				"Cannot add more MAC addresses, trusted VF exhausted it's resources\n");
2940 			return -EPERM;
2941 		}
2942 	}
2943 	return 0;
2944 }
2945 
2946 /**
2947  * i40e_vc_ether_addr_type - get type of virtchnl_ether_addr
2948  * @vc_ether_addr: used to extract the type
2949  **/
2950 static u8
2951 i40e_vc_ether_addr_type(struct virtchnl_ether_addr *vc_ether_addr)
2952 {
2953 	return vc_ether_addr->type & VIRTCHNL_ETHER_ADDR_TYPE_MASK;
2954 }
2955 
2956 /**
2957  * i40e_is_vc_addr_legacy
2958  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
2959  *
2960  * check if the MAC address is from an older VF
2961  **/
2962 static bool
2963 i40e_is_vc_addr_legacy(struct virtchnl_ether_addr *vc_ether_addr)
2964 {
2965 	return i40e_vc_ether_addr_type(vc_ether_addr) ==
2966 		VIRTCHNL_ETHER_ADDR_LEGACY;
2967 }
2968 
2969 /**
2970  * i40e_is_vc_addr_primary
2971  * @vc_ether_addr: VIRTCHNL structure that contains MAC and type
2972  *
2973  * check if the MAC address is the VF's primary MAC
2974  * This function should only be called when the MAC address in
2975  * virtchnl_ether_addr is a valid unicast MAC
2976  **/
2977 static bool
2978 i40e_is_vc_addr_primary(struct virtchnl_ether_addr *vc_ether_addr)
2979 {
2980 	return i40e_vc_ether_addr_type(vc_ether_addr) ==
2981 		VIRTCHNL_ETHER_ADDR_PRIMARY;
2982 }
2983 
2984 /**
2985  * i40e_update_vf_mac_addr
2986  * @vf: VF to update
2987  * @vc_ether_addr: structure from VIRTCHNL with MAC to add
2988  *
2989  * update the VF's cached hardware MAC if allowed
2990  **/
2991 static void
2992 i40e_update_vf_mac_addr(struct i40e_vf *vf,
2993 			struct virtchnl_ether_addr *vc_ether_addr)
2994 {
2995 	u8 *mac_addr = vc_ether_addr->addr;
2996 
2997 	if (!is_valid_ether_addr(mac_addr))
2998 		return;
2999 
3000 	/* If request to add MAC filter is a primary request update its default
3001 	 * MAC address with the requested one. If it is a legacy request then
3002 	 * check if current default is empty if so update the default MAC
3003 	 */
3004 	if (i40e_is_vc_addr_primary(vc_ether_addr)) {
3005 		ether_addr_copy(vf->default_lan_addr.addr, mac_addr);
3006 	} else if (i40e_is_vc_addr_legacy(vc_ether_addr)) {
3007 		if (is_zero_ether_addr(vf->default_lan_addr.addr))
3008 			ether_addr_copy(vf->default_lan_addr.addr, mac_addr);
3009 	}
3010 }
3011 
3012 /**
3013  * i40e_vc_add_mac_addr_msg
3014  * @vf: pointer to the VF info
3015  * @msg: pointer to the msg buffer
3016  *
3017  * add guest mac address filter
3018  **/
3019 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
3020 {
3021 	struct virtchnl_ether_addr_list *al =
3022 	    (struct virtchnl_ether_addr_list *)msg;
3023 	struct i40e_pf *pf = vf->pf;
3024 	struct i40e_vsi *vsi = NULL;
3025 	int ret = 0;
3026 	int i;
3027 
3028 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3029 	    !i40e_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3030 		ret = -EINVAL;
3031 		goto error_param;
3032 	}
3033 
3034 	vsi = pf->vsi[vf->lan_vsi_idx];
3035 
3036 	/* Lock once, because all function inside for loop accesses VSI's
3037 	 * MAC filter list which needs to be protected using same lock.
3038 	 */
3039 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3040 
3041 	ret = i40e_check_vf_permission(vf, al);
3042 	if (ret) {
3043 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
3044 		goto error_param;
3045 	}
3046 
3047 	/* add new addresses to the list */
3048 	for (i = 0; i < al->num_elements; i++) {
3049 		struct i40e_mac_filter *f;
3050 
3051 		f = i40e_find_mac(vsi, al->list[i].addr);
3052 		if (!f) {
3053 			f = i40e_add_mac_filter(vsi, al->list[i].addr);
3054 
3055 			if (!f) {
3056 				dev_err(&pf->pdev->dev,
3057 					"Unable to add MAC filter %pM for VF %d\n",
3058 					al->list[i].addr, vf->vf_id);
3059 				ret = -EINVAL;
3060 				spin_unlock_bh(&vsi->mac_filter_hash_lock);
3061 				goto error_param;
3062 			}
3063 		}
3064 		i40e_update_vf_mac_addr(vf, &al->list[i]);
3065 	}
3066 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3067 
3068 	/* program the updated filter list */
3069 	ret = i40e_sync_vsi_filters(vsi);
3070 	if (ret)
3071 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
3072 			vf->vf_id, ret);
3073 
3074 error_param:
3075 	/* send the response to the VF */
3076 	return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
3077 				      ret, NULL, 0);
3078 }
3079 
3080 /**
3081  * i40e_vc_del_mac_addr_msg
3082  * @vf: pointer to the VF info
3083  * @msg: pointer to the msg buffer
3084  *
3085  * remove guest mac address filter
3086  **/
3087 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg)
3088 {
3089 	struct virtchnl_ether_addr_list *al =
3090 	    (struct virtchnl_ether_addr_list *)msg;
3091 	bool was_unimac_deleted = false;
3092 	struct i40e_pf *pf = vf->pf;
3093 	struct i40e_vsi *vsi = NULL;
3094 	int ret = 0;
3095 	int i;
3096 
3097 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3098 	    !i40e_vc_isvalid_vsi_id(vf, al->vsi_id)) {
3099 		ret = -EINVAL;
3100 		goto error_param;
3101 	}
3102 
3103 	for (i = 0; i < al->num_elements; i++) {
3104 		if (is_broadcast_ether_addr(al->list[i].addr) ||
3105 		    is_zero_ether_addr(al->list[i].addr)) {
3106 			dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
3107 				al->list[i].addr, vf->vf_id);
3108 			ret = -EINVAL;
3109 			goto error_param;
3110 		}
3111 		if (ether_addr_equal(al->list[i].addr, vf->default_lan_addr.addr))
3112 			was_unimac_deleted = true;
3113 	}
3114 	vsi = pf->vsi[vf->lan_vsi_idx];
3115 
3116 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3117 	/* delete addresses from the list */
3118 	for (i = 0; i < al->num_elements; i++)
3119 		if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
3120 			ret = -EINVAL;
3121 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
3122 			goto error_param;
3123 		}
3124 
3125 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3126 
3127 	if (was_unimac_deleted)
3128 		eth_zero_addr(vf->default_lan_addr.addr);
3129 
3130 	/* program the updated filter list */
3131 	ret = i40e_sync_vsi_filters(vsi);
3132 	if (ret)
3133 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
3134 			vf->vf_id, ret);
3135 
3136 	if (vf->trusted && was_unimac_deleted) {
3137 		struct i40e_mac_filter *f;
3138 		struct hlist_node *h;
3139 		u8 *macaddr = NULL;
3140 		int bkt;
3141 
3142 		/* set last unicast mac address as default */
3143 		spin_lock_bh(&vsi->mac_filter_hash_lock);
3144 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3145 			if (is_valid_ether_addr(f->macaddr))
3146 				macaddr = f->macaddr;
3147 		}
3148 		if (macaddr)
3149 			ether_addr_copy(vf->default_lan_addr.addr, macaddr);
3150 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
3151 	}
3152 error_param:
3153 	/* send the response to the VF */
3154 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR, ret);
3155 }
3156 
3157 /**
3158  * i40e_vc_add_vlan_msg
3159  * @vf: pointer to the VF info
3160  * @msg: pointer to the msg buffer
3161  *
3162  * program guest vlan id
3163  **/
3164 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg)
3165 {
3166 	struct virtchnl_vlan_filter_list *vfl =
3167 	    (struct virtchnl_vlan_filter_list *)msg;
3168 	struct i40e_pf *pf = vf->pf;
3169 	struct i40e_vsi *vsi = NULL;
3170 	int aq_ret = 0;
3171 	int i;
3172 
3173 	if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
3174 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3175 		dev_err(&pf->pdev->dev,
3176 			"VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
3177 		goto error_param;
3178 	}
3179 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
3180 	    !i40e_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
3181 		aq_ret = -EINVAL;
3182 		goto error_param;
3183 	}
3184 
3185 	for (i = 0; i < vfl->num_elements; i++) {
3186 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
3187 			aq_ret = -EINVAL;
3188 			dev_err(&pf->pdev->dev,
3189 				"invalid VF VLAN id %d\n", vfl->vlan_id[i]);
3190 			goto error_param;
3191 		}
3192 	}
3193 	vsi = pf->vsi[vf->lan_vsi_idx];
3194 	if (vsi->info.pvid) {
3195 		aq_ret = -EINVAL;
3196 		goto error_param;
3197 	}
3198 
3199 	i40e_vlan_stripping_enable(vsi);
3200 	for (i = 0; i < vfl->num_elements; i++) {
3201 		/* add new VLAN filter */
3202 		int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
3203 		if (!ret)
3204 			vf->num_vlan++;
3205 
3206 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
3207 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
3208 							   true,
3209 							   vfl->vlan_id[i],
3210 							   NULL);
3211 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
3212 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
3213 							   true,
3214 							   vfl->vlan_id[i],
3215 							   NULL);
3216 
3217 		if (ret)
3218 			dev_err(&pf->pdev->dev,
3219 				"Unable to add VLAN filter %d for VF %d, error %d\n",
3220 				vfl->vlan_id[i], vf->vf_id, ret);
3221 	}
3222 
3223 error_param:
3224 	/* send the response to the VF */
3225 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
3226 }
3227 
3228 /**
3229  * i40e_vc_remove_vlan_msg
3230  * @vf: pointer to the VF info
3231  * @msg: pointer to the msg buffer
3232  *
3233  * remove programmed guest vlan id
3234  **/
3235 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg)
3236 {
3237 	struct virtchnl_vlan_filter_list *vfl =
3238 	    (struct virtchnl_vlan_filter_list *)msg;
3239 	struct i40e_pf *pf = vf->pf;
3240 	struct i40e_vsi *vsi = NULL;
3241 	int aq_ret = 0;
3242 	int i;
3243 
3244 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3245 	    !i40e_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
3246 		aq_ret = -EINVAL;
3247 		goto error_param;
3248 	}
3249 
3250 	for (i = 0; i < vfl->num_elements; i++) {
3251 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
3252 			aq_ret = -EINVAL;
3253 			goto error_param;
3254 		}
3255 	}
3256 
3257 	vsi = pf->vsi[vf->lan_vsi_idx];
3258 	if (vsi->info.pvid) {
3259 		if (vfl->num_elements > 1 || vfl->vlan_id[0])
3260 			aq_ret = -EINVAL;
3261 		goto error_param;
3262 	}
3263 
3264 	for (i = 0; i < vfl->num_elements; i++) {
3265 		i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
3266 		vf->num_vlan--;
3267 
3268 		if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
3269 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
3270 							   false,
3271 							   vfl->vlan_id[i],
3272 							   NULL);
3273 		if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
3274 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
3275 							   false,
3276 							   vfl->vlan_id[i],
3277 							   NULL);
3278 	}
3279 
3280 error_param:
3281 	/* send the response to the VF */
3282 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
3283 }
3284 
3285 /**
3286  * i40e_vc_rdma_msg
3287  * @vf: pointer to the VF info
3288  * @msg: pointer to the msg buffer
3289  * @msglen: msg length
3290  *
3291  * called from the VF for the iwarp msgs
3292  **/
3293 static int i40e_vc_rdma_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
3294 {
3295 	struct i40e_pf *pf = vf->pf;
3296 	int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
3297 	int aq_ret = 0;
3298 
3299 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
3300 	    !test_bit(I40E_VF_STATE_RDMAENA, &vf->vf_states)) {
3301 		aq_ret = -EINVAL;
3302 		goto error_param;
3303 	}
3304 
3305 	i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
3306 				     msg, msglen);
3307 
3308 error_param:
3309 	/* send the response to the VF */
3310 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_RDMA,
3311 				       aq_ret);
3312 }
3313 
3314 /**
3315  * i40e_vc_rdma_qvmap_msg
3316  * @vf: pointer to the VF info
3317  * @msg: pointer to the msg buffer
3318  * @config: config qvmap or release it
3319  *
3320  * called from the VF for the iwarp msgs
3321  **/
3322 static int i40e_vc_rdma_qvmap_msg(struct i40e_vf *vf, u8 *msg, bool config)
3323 {
3324 	struct virtchnl_rdma_qvlist_info *qvlist_info =
3325 				(struct virtchnl_rdma_qvlist_info *)msg;
3326 	int aq_ret = 0;
3327 
3328 	if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
3329 	    !test_bit(I40E_VF_STATE_RDMAENA, &vf->vf_states)) {
3330 		aq_ret = -EINVAL;
3331 		goto error_param;
3332 	}
3333 
3334 	if (config) {
3335 		if (i40e_config_rdma_qvlist(vf, qvlist_info))
3336 			aq_ret = -EINVAL;
3337 	} else {
3338 		i40e_release_rdma_qvlist(vf);
3339 	}
3340 
3341 error_param:
3342 	/* send the response to the VF */
3343 	return i40e_vc_send_resp_to_vf(vf,
3344 			       config ? VIRTCHNL_OP_CONFIG_RDMA_IRQ_MAP :
3345 			       VIRTCHNL_OP_RELEASE_RDMA_IRQ_MAP,
3346 			       aq_ret);
3347 }
3348 
3349 /**
3350  * i40e_vc_config_rss_key
3351  * @vf: pointer to the VF info
3352  * @msg: pointer to the msg buffer
3353  *
3354  * Configure the VF's RSS key
3355  **/
3356 static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg)
3357 {
3358 	struct virtchnl_rss_key *vrk =
3359 		(struct virtchnl_rss_key *)msg;
3360 	struct i40e_pf *pf = vf->pf;
3361 	struct i40e_vsi *vsi = NULL;
3362 	int aq_ret = 0;
3363 
3364 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3365 	    !i40e_vc_isvalid_vsi_id(vf, vrk->vsi_id) ||
3366 	    vrk->key_len != I40E_HKEY_ARRAY_SIZE) {
3367 		aq_ret = -EINVAL;
3368 		goto err;
3369 	}
3370 
3371 	vsi = pf->vsi[vf->lan_vsi_idx];
3372 	aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
3373 err:
3374 	/* send the response to the VF */
3375 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
3376 				       aq_ret);
3377 }
3378 
3379 /**
3380  * i40e_vc_config_rss_lut
3381  * @vf: pointer to the VF info
3382  * @msg: pointer to the msg buffer
3383  *
3384  * Configure the VF's RSS LUT
3385  **/
3386 static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg)
3387 {
3388 	struct virtchnl_rss_lut *vrl =
3389 		(struct virtchnl_rss_lut *)msg;
3390 	struct i40e_pf *pf = vf->pf;
3391 	struct i40e_vsi *vsi = NULL;
3392 	int aq_ret = 0;
3393 	u16 i;
3394 
3395 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE) ||
3396 	    !i40e_vc_isvalid_vsi_id(vf, vrl->vsi_id) ||
3397 	    vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE) {
3398 		aq_ret = -EINVAL;
3399 		goto err;
3400 	}
3401 
3402 	for (i = 0; i < vrl->lut_entries; i++)
3403 		if (vrl->lut[i] >= vf->num_queue_pairs) {
3404 			aq_ret = -EINVAL;
3405 			goto err;
3406 		}
3407 
3408 	vsi = pf->vsi[vf->lan_vsi_idx];
3409 	aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
3410 	/* send the response to the VF */
3411 err:
3412 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
3413 				       aq_ret);
3414 }
3415 
3416 /**
3417  * i40e_vc_get_rss_hena
3418  * @vf: pointer to the VF info
3419  * @msg: pointer to the msg buffer
3420  *
3421  * Return the RSS HENA bits allowed by the hardware
3422  **/
3423 static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg)
3424 {
3425 	struct virtchnl_rss_hena *vrh = NULL;
3426 	struct i40e_pf *pf = vf->pf;
3427 	int aq_ret = 0;
3428 	int len = 0;
3429 
3430 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3431 		aq_ret = -EINVAL;
3432 		goto err;
3433 	}
3434 	len = sizeof(struct virtchnl_rss_hena);
3435 
3436 	vrh = kzalloc(len, GFP_KERNEL);
3437 	if (!vrh) {
3438 		aq_ret = -ENOMEM;
3439 		len = 0;
3440 		goto err;
3441 	}
3442 	vrh->hena = i40e_pf_get_default_rss_hena(pf);
3443 err:
3444 	/* send the response back to the VF */
3445 	aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
3446 					aq_ret, (u8 *)vrh, len);
3447 	kfree(vrh);
3448 	return aq_ret;
3449 }
3450 
3451 /**
3452  * i40e_vc_set_rss_hena
3453  * @vf: pointer to the VF info
3454  * @msg: pointer to the msg buffer
3455  *
3456  * Set the RSS HENA bits for the VF
3457  **/
3458 static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg)
3459 {
3460 	struct virtchnl_rss_hena *vrh =
3461 		(struct virtchnl_rss_hena *)msg;
3462 	struct i40e_pf *pf = vf->pf;
3463 	struct i40e_hw *hw = &pf->hw;
3464 	int aq_ret = 0;
3465 
3466 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3467 		aq_ret = -EINVAL;
3468 		goto err;
3469 	}
3470 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
3471 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
3472 			  (u32)(vrh->hena >> 32));
3473 
3474 	/* send the response to the VF */
3475 err:
3476 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
3477 }
3478 
3479 /**
3480  * i40e_vc_enable_vlan_stripping
3481  * @vf: pointer to the VF info
3482  * @msg: pointer to the msg buffer
3483  *
3484  * Enable vlan header stripping for the VF
3485  **/
3486 static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3487 {
3488 	struct i40e_vsi *vsi;
3489 	int aq_ret = 0;
3490 
3491 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3492 		aq_ret = -EINVAL;
3493 		goto err;
3494 	}
3495 
3496 	vsi = vf->pf->vsi[vf->lan_vsi_idx];
3497 	i40e_vlan_stripping_enable(vsi);
3498 
3499 	/* send the response to the VF */
3500 err:
3501 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
3502 				       aq_ret);
3503 }
3504 
3505 /**
3506  * i40e_vc_disable_vlan_stripping
3507  * @vf: pointer to the VF info
3508  * @msg: pointer to the msg buffer
3509  *
3510  * Disable vlan header stripping for the VF
3511  **/
3512 static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg)
3513 {
3514 	struct i40e_vsi *vsi;
3515 	int aq_ret = 0;
3516 
3517 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3518 		aq_ret = -EINVAL;
3519 		goto err;
3520 	}
3521 
3522 	vsi = vf->pf->vsi[vf->lan_vsi_idx];
3523 	i40e_vlan_stripping_disable(vsi);
3524 
3525 	/* send the response to the VF */
3526 err:
3527 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
3528 				       aq_ret);
3529 }
3530 
3531 /**
3532  * i40e_validate_cloud_filter
3533  * @vf: pointer to VF structure
3534  * @tc_filter: pointer to filter requested
3535  *
3536  * This function validates cloud filter programmed as TC filter for ADq
3537  **/
3538 static int i40e_validate_cloud_filter(struct i40e_vf *vf,
3539 				      struct virtchnl_filter *tc_filter)
3540 {
3541 	struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;
3542 	struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;
3543 	struct i40e_pf *pf = vf->pf;
3544 	struct i40e_vsi *vsi = NULL;
3545 	struct i40e_mac_filter *f;
3546 	struct hlist_node *h;
3547 	bool found = false;
3548 	int bkt;
3549 
3550 	if (tc_filter->action != VIRTCHNL_ACTION_TC_REDIRECT) {
3551 		dev_info(&pf->pdev->dev,
3552 			 "VF %d: ADQ doesn't support this action (%d)\n",
3553 			 vf->vf_id, tc_filter->action);
3554 		goto err;
3555 	}
3556 
3557 	/* action_meta is TC number here to which the filter is applied */
3558 	if (!tc_filter->action_meta ||
3559 	    tc_filter->action_meta > vf->num_tc) {
3560 		dev_info(&pf->pdev->dev, "VF %d: Invalid TC number %u\n",
3561 			 vf->vf_id, tc_filter->action_meta);
3562 		goto err;
3563 	}
3564 
3565 	/* Check filter if it's programmed for advanced mode or basic mode.
3566 	 * There are two ADq modes (for VF only),
3567 	 * 1. Basic mode: intended to allow as many filter options as possible
3568 	 *		  to be added to a VF in Non-trusted mode. Main goal is
3569 	 *		  to add filters to its own MAC and VLAN id.
3570 	 * 2. Advanced mode: is for allowing filters to be applied other than
3571 	 *		  its own MAC or VLAN. This mode requires the VF to be
3572 	 *		  Trusted.
3573 	 */
3574 	if (mask.dst_mac[0] && !mask.dst_ip[0]) {
3575 		vsi = pf->vsi[vf->lan_vsi_idx];
3576 		f = i40e_find_mac(vsi, data.dst_mac);
3577 
3578 		if (!f) {
3579 			dev_info(&pf->pdev->dev,
3580 				 "Destination MAC %pM doesn't belong to VF %d\n",
3581 				 data.dst_mac, vf->vf_id);
3582 			goto err;
3583 		}
3584 
3585 		if (mask.vlan_id) {
3586 			hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
3587 					   hlist) {
3588 				if (f->vlan == ntohs(data.vlan_id)) {
3589 					found = true;
3590 					break;
3591 				}
3592 			}
3593 			if (!found) {
3594 				dev_info(&pf->pdev->dev,
3595 					 "VF %d doesn't have any VLAN id %u\n",
3596 					 vf->vf_id, ntohs(data.vlan_id));
3597 				goto err;
3598 			}
3599 		}
3600 	} else {
3601 		/* Check if VF is trusted */
3602 		if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
3603 			dev_err(&pf->pdev->dev,
3604 				"VF %d not trusted, make VF trusted to add advanced mode ADq cloud filters\n",
3605 				vf->vf_id);
3606 			return -EIO;
3607 		}
3608 	}
3609 
3610 	if (mask.dst_mac[0] & data.dst_mac[0]) {
3611 		if (is_broadcast_ether_addr(data.dst_mac) ||
3612 		    is_zero_ether_addr(data.dst_mac)) {
3613 			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest MAC addr %pM\n",
3614 				 vf->vf_id, data.dst_mac);
3615 			goto err;
3616 		}
3617 	}
3618 
3619 	if (mask.src_mac[0] & data.src_mac[0]) {
3620 		if (is_broadcast_ether_addr(data.src_mac) ||
3621 		    is_zero_ether_addr(data.src_mac)) {
3622 			dev_info(&pf->pdev->dev, "VF %d: Invalid Source MAC addr %pM\n",
3623 				 vf->vf_id, data.src_mac);
3624 			goto err;
3625 		}
3626 	}
3627 
3628 	if (mask.dst_port & data.dst_port) {
3629 		if (!data.dst_port) {
3630 			dev_info(&pf->pdev->dev, "VF %d: Invalid Dest port\n",
3631 				 vf->vf_id);
3632 			goto err;
3633 		}
3634 	}
3635 
3636 	if (mask.src_port & data.src_port) {
3637 		if (!data.src_port) {
3638 			dev_info(&pf->pdev->dev, "VF %d: Invalid Source port\n",
3639 				 vf->vf_id);
3640 			goto err;
3641 		}
3642 	}
3643 
3644 	if (tc_filter->flow_type != VIRTCHNL_TCP_V6_FLOW &&
3645 	    tc_filter->flow_type != VIRTCHNL_TCP_V4_FLOW) {
3646 		dev_info(&pf->pdev->dev, "VF %d: Invalid Flow type\n",
3647 			 vf->vf_id);
3648 		goto err;
3649 	}
3650 
3651 	if (mask.vlan_id & data.vlan_id) {
3652 		if (ntohs(data.vlan_id) > I40E_MAX_VLANID) {
3653 			dev_info(&pf->pdev->dev, "VF %d: invalid VLAN ID\n",
3654 				 vf->vf_id);
3655 			goto err;
3656 		}
3657 	}
3658 
3659 	return 0;
3660 err:
3661 	return -EIO;
3662 }
3663 
3664 /**
3665  * i40e_find_vsi_from_seid - searches for the vsi with the given seid
3666  * @vf: pointer to the VF info
3667  * @seid: seid of the vsi it is searching for
3668  **/
3669 static struct i40e_vsi *i40e_find_vsi_from_seid(struct i40e_vf *vf, u16 seid)
3670 {
3671 	struct i40e_pf *pf = vf->pf;
3672 	struct i40e_vsi *vsi = NULL;
3673 	int i;
3674 
3675 	for (i = 0; i < vf->num_tc ; i++) {
3676 		vsi = i40e_find_vsi_from_id(pf, vf->ch[i].vsi_id);
3677 		if (vsi && vsi->seid == seid)
3678 			return vsi;
3679 	}
3680 	return NULL;
3681 }
3682 
3683 /**
3684  * i40e_del_all_cloud_filters
3685  * @vf: pointer to the VF info
3686  *
3687  * This function deletes all cloud filters
3688  **/
3689 static void i40e_del_all_cloud_filters(struct i40e_vf *vf)
3690 {
3691 	struct i40e_cloud_filter *cfilter = NULL;
3692 	struct i40e_pf *pf = vf->pf;
3693 	struct i40e_vsi *vsi = NULL;
3694 	struct hlist_node *node;
3695 	int ret;
3696 
3697 	hlist_for_each_entry_safe(cfilter, node,
3698 				  &vf->cloud_filter_list, cloud_node) {
3699 		vsi = i40e_find_vsi_from_seid(vf, cfilter->seid);
3700 
3701 		if (!vsi) {
3702 			dev_err(&pf->pdev->dev, "VF %d: no VSI found for matching %u seid, can't delete cloud filter\n",
3703 				vf->vf_id, cfilter->seid);
3704 			continue;
3705 		}
3706 
3707 		if (cfilter->dst_port)
3708 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
3709 								false);
3710 		else
3711 			ret = i40e_add_del_cloud_filter(vsi, cfilter, false);
3712 		if (ret)
3713 			dev_err(&pf->pdev->dev,
3714 				"VF %d: Failed to delete cloud filter, err %pe aq_err %s\n",
3715 				vf->vf_id, ERR_PTR(ret),
3716 				i40e_aq_str(&pf->hw,
3717 					    pf->hw.aq.asq_last_status));
3718 
3719 		hlist_del(&cfilter->cloud_node);
3720 		kfree(cfilter);
3721 		vf->num_cloud_filters--;
3722 	}
3723 }
3724 
3725 /**
3726  * i40e_vc_del_cloud_filter
3727  * @vf: pointer to the VF info
3728  * @msg: pointer to the msg buffer
3729  *
3730  * This function deletes a cloud filter programmed as TC filter for ADq
3731  **/
3732 static int i40e_vc_del_cloud_filter(struct i40e_vf *vf, u8 *msg)
3733 {
3734 	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3735 	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3736 	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3737 	struct i40e_cloud_filter cfilter, *cf = NULL;
3738 	struct i40e_pf *pf = vf->pf;
3739 	struct i40e_vsi *vsi = NULL;
3740 	struct hlist_node *node;
3741 	int aq_ret = 0;
3742 	int i, ret;
3743 
3744 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3745 		aq_ret = -EINVAL;
3746 		goto err;
3747 	}
3748 
3749 	if (!vf->adq_enabled) {
3750 		dev_info(&pf->pdev->dev,
3751 			 "VF %d: ADq not enabled, can't apply cloud filter\n",
3752 			 vf->vf_id);
3753 		aq_ret = -EINVAL;
3754 		goto err;
3755 	}
3756 
3757 	if (i40e_validate_cloud_filter(vf, vcf)) {
3758 		dev_info(&pf->pdev->dev,
3759 			 "VF %d: Invalid input, can't apply cloud filter\n",
3760 			 vf->vf_id);
3761 		aq_ret = -EINVAL;
3762 		goto err;
3763 	}
3764 
3765 	memset(&cfilter, 0, sizeof(cfilter));
3766 	/* parse destination mac address */
3767 	for (i = 0; i < ETH_ALEN; i++)
3768 		cfilter.dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3769 
3770 	/* parse source mac address */
3771 	for (i = 0; i < ETH_ALEN; i++)
3772 		cfilter.src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3773 
3774 	cfilter.vlan_id = mask.vlan_id & tcf.vlan_id;
3775 	cfilter.dst_port = mask.dst_port & tcf.dst_port;
3776 	cfilter.src_port = mask.src_port & tcf.src_port;
3777 
3778 	switch (vcf->flow_type) {
3779 	case VIRTCHNL_TCP_V4_FLOW:
3780 		cfilter.n_proto = ETH_P_IP;
3781 		if (mask.dst_ip[0] & tcf.dst_ip[0])
3782 			memcpy(&cfilter.ip.v4.dst_ip, tcf.dst_ip,
3783 			       ARRAY_SIZE(tcf.dst_ip));
3784 		else if (mask.src_ip[0] & tcf.dst_ip[0])
3785 			memcpy(&cfilter.ip.v4.src_ip, tcf.src_ip,
3786 			       ARRAY_SIZE(tcf.dst_ip));
3787 		break;
3788 	case VIRTCHNL_TCP_V6_FLOW:
3789 		cfilter.n_proto = ETH_P_IPV6;
3790 		if (mask.dst_ip[3] & tcf.dst_ip[3])
3791 			memcpy(&cfilter.ip.v6.dst_ip6, tcf.dst_ip,
3792 			       sizeof(cfilter.ip.v6.dst_ip6));
3793 		if (mask.src_ip[3] & tcf.src_ip[3])
3794 			memcpy(&cfilter.ip.v6.src_ip6, tcf.src_ip,
3795 			       sizeof(cfilter.ip.v6.src_ip6));
3796 		break;
3797 	default:
3798 		/* TC filter can be configured based on different combinations
3799 		 * and in this case IP is not a part of filter config
3800 		 */
3801 		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3802 			 vf->vf_id);
3803 	}
3804 
3805 	/* get the vsi to which the tc belongs to */
3806 	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3807 	cfilter.seid = vsi->seid;
3808 	cfilter.flags = vcf->field_flags;
3809 
3810 	/* Deleting TC filter */
3811 	if (tcf.dst_port)
3812 		ret = i40e_add_del_cloud_filter_big_buf(vsi, &cfilter, false);
3813 	else
3814 		ret = i40e_add_del_cloud_filter(vsi, &cfilter, false);
3815 	if (ret) {
3816 		dev_err(&pf->pdev->dev,
3817 			"VF %d: Failed to delete cloud filter, err %pe aq_err %s\n",
3818 			vf->vf_id, ERR_PTR(ret),
3819 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3820 		goto err;
3821 	}
3822 
3823 	hlist_for_each_entry_safe(cf, node,
3824 				  &vf->cloud_filter_list, cloud_node) {
3825 		if (cf->seid != cfilter.seid)
3826 			continue;
3827 		if (mask.dst_port)
3828 			if (cfilter.dst_port != cf->dst_port)
3829 				continue;
3830 		if (mask.dst_mac[0])
3831 			if (!ether_addr_equal(cf->src_mac, cfilter.src_mac))
3832 				continue;
3833 		/* for ipv4 data to be valid, only first byte of mask is set */
3834 		if (cfilter.n_proto == ETH_P_IP && mask.dst_ip[0])
3835 			if (memcmp(&cfilter.ip.v4.dst_ip, &cf->ip.v4.dst_ip,
3836 				   ARRAY_SIZE(tcf.dst_ip)))
3837 				continue;
3838 		/* for ipv6, mask is set for all sixteen bytes (4 words) */
3839 		if (cfilter.n_proto == ETH_P_IPV6 && mask.dst_ip[3])
3840 			if (memcmp(&cfilter.ip.v6.dst_ip6, &cf->ip.v6.dst_ip6,
3841 				   sizeof(cfilter.ip.v6.src_ip6)))
3842 				continue;
3843 		if (mask.vlan_id)
3844 			if (cfilter.vlan_id != cf->vlan_id)
3845 				continue;
3846 
3847 		hlist_del(&cf->cloud_node);
3848 		kfree(cf);
3849 		vf->num_cloud_filters--;
3850 	}
3851 
3852 err:
3853 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_CLOUD_FILTER,
3854 				       aq_ret);
3855 }
3856 
3857 /**
3858  * i40e_vc_add_cloud_filter
3859  * @vf: pointer to the VF info
3860  * @msg: pointer to the msg buffer
3861  *
3862  * This function adds a cloud filter programmed as TC filter for ADq
3863  **/
3864 static int i40e_vc_add_cloud_filter(struct i40e_vf *vf, u8 *msg)
3865 {
3866 	struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
3867 	struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
3868 	struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
3869 	struct i40e_cloud_filter *cfilter = NULL;
3870 	struct i40e_pf *pf = vf->pf;
3871 	struct i40e_vsi *vsi = NULL;
3872 	int aq_ret = 0;
3873 	int i;
3874 
3875 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3876 		aq_ret = -EINVAL;
3877 		goto err_out;
3878 	}
3879 
3880 	if (!vf->adq_enabled) {
3881 		dev_info(&pf->pdev->dev,
3882 			 "VF %d: ADq is not enabled, can't apply cloud filter\n",
3883 			 vf->vf_id);
3884 		aq_ret = -EINVAL;
3885 		goto err_out;
3886 	}
3887 
3888 	if (i40e_validate_cloud_filter(vf, vcf)) {
3889 		dev_info(&pf->pdev->dev,
3890 			 "VF %d: Invalid input/s, can't apply cloud filter\n",
3891 			 vf->vf_id);
3892 		aq_ret = -EINVAL;
3893 		goto err_out;
3894 	}
3895 
3896 	cfilter = kzalloc(sizeof(*cfilter), GFP_KERNEL);
3897 	if (!cfilter) {
3898 		aq_ret = -ENOMEM;
3899 		goto err_out;
3900 	}
3901 
3902 	/* parse destination mac address */
3903 	for (i = 0; i < ETH_ALEN; i++)
3904 		cfilter->dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
3905 
3906 	/* parse source mac address */
3907 	for (i = 0; i < ETH_ALEN; i++)
3908 		cfilter->src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
3909 
3910 	cfilter->vlan_id = mask.vlan_id & tcf.vlan_id;
3911 	cfilter->dst_port = mask.dst_port & tcf.dst_port;
3912 	cfilter->src_port = mask.src_port & tcf.src_port;
3913 
3914 	switch (vcf->flow_type) {
3915 	case VIRTCHNL_TCP_V4_FLOW:
3916 		cfilter->n_proto = ETH_P_IP;
3917 		if (mask.dst_ip[0] & tcf.dst_ip[0])
3918 			memcpy(&cfilter->ip.v4.dst_ip, tcf.dst_ip,
3919 			       ARRAY_SIZE(tcf.dst_ip));
3920 		else if (mask.src_ip[0] & tcf.dst_ip[0])
3921 			memcpy(&cfilter->ip.v4.src_ip, tcf.src_ip,
3922 			       ARRAY_SIZE(tcf.dst_ip));
3923 		break;
3924 	case VIRTCHNL_TCP_V6_FLOW:
3925 		cfilter->n_proto = ETH_P_IPV6;
3926 		if (mask.dst_ip[3] & tcf.dst_ip[3])
3927 			memcpy(&cfilter->ip.v6.dst_ip6, tcf.dst_ip,
3928 			       sizeof(cfilter->ip.v6.dst_ip6));
3929 		if (mask.src_ip[3] & tcf.src_ip[3])
3930 			memcpy(&cfilter->ip.v6.src_ip6, tcf.src_ip,
3931 			       sizeof(cfilter->ip.v6.src_ip6));
3932 		break;
3933 	default:
3934 		/* TC filter can be configured based on different combinations
3935 		 * and in this case IP is not a part of filter config
3936 		 */
3937 		dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
3938 			 vf->vf_id);
3939 	}
3940 
3941 	/* get the VSI to which the TC belongs to */
3942 	vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
3943 	cfilter->seid = vsi->seid;
3944 	cfilter->flags = vcf->field_flags;
3945 
3946 	/* Adding cloud filter programmed as TC filter */
3947 	if (tcf.dst_port)
3948 		aq_ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, true);
3949 	else
3950 		aq_ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
3951 	if (aq_ret) {
3952 		dev_err(&pf->pdev->dev,
3953 			"VF %d: Failed to add cloud filter, err %pe aq_err %s\n",
3954 			vf->vf_id, ERR_PTR(aq_ret),
3955 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
3956 		goto err_free;
3957 	}
3958 
3959 	INIT_HLIST_NODE(&cfilter->cloud_node);
3960 	hlist_add_head(&cfilter->cloud_node, &vf->cloud_filter_list);
3961 	/* release the pointer passing it to the collection */
3962 	cfilter = NULL;
3963 	vf->num_cloud_filters++;
3964 err_free:
3965 	kfree(cfilter);
3966 err_out:
3967 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_CLOUD_FILTER,
3968 				       aq_ret);
3969 }
3970 
3971 /**
3972  * i40e_vc_add_qch_msg: Add queue channel and enable ADq
3973  * @vf: pointer to the VF info
3974  * @msg: pointer to the msg buffer
3975  **/
3976 static int i40e_vc_add_qch_msg(struct i40e_vf *vf, u8 *msg)
3977 {
3978 	struct virtchnl_tc_info *tci =
3979 		(struct virtchnl_tc_info *)msg;
3980 	struct i40e_pf *pf = vf->pf;
3981 	struct i40e_link_status *ls = &pf->hw.phy.link_info;
3982 	int i, adq_request_qps = 0;
3983 	int aq_ret = 0;
3984 	u64 speed = 0;
3985 
3986 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
3987 		aq_ret = -EINVAL;
3988 		goto err;
3989 	}
3990 
3991 	/* ADq cannot be applied if spoof check is ON */
3992 	if (vf->spoofchk) {
3993 		dev_err(&pf->pdev->dev,
3994 			"Spoof check is ON, turn it OFF to enable ADq\n");
3995 		aq_ret = -EINVAL;
3996 		goto err;
3997 	}
3998 
3999 	if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)) {
4000 		dev_err(&pf->pdev->dev,
4001 			"VF %d attempting to enable ADq, but hasn't properly negotiated that capability\n",
4002 			vf->vf_id);
4003 		aq_ret = -EINVAL;
4004 		goto err;
4005 	}
4006 
4007 	/* max number of traffic classes for VF currently capped at 4 */
4008 	if (!tci->num_tc || tci->num_tc > I40E_MAX_VF_VSI) {
4009 		dev_err(&pf->pdev->dev,
4010 			"VF %d trying to set %u TCs, valid range 1-%u TCs per VF\n",
4011 			vf->vf_id, tci->num_tc, I40E_MAX_VF_VSI);
4012 		aq_ret = -EINVAL;
4013 		goto err;
4014 	}
4015 
4016 	/* validate queues for each TC */
4017 	for (i = 0; i < tci->num_tc; i++)
4018 		if (!tci->list[i].count ||
4019 		    tci->list[i].count > I40E_DEFAULT_QUEUES_PER_VF) {
4020 			dev_err(&pf->pdev->dev,
4021 				"VF %d: TC %d trying to set %u queues, valid range 1-%u queues per TC\n",
4022 				vf->vf_id, i, tci->list[i].count,
4023 				I40E_DEFAULT_QUEUES_PER_VF);
4024 			aq_ret = -EINVAL;
4025 			goto err;
4026 		}
4027 
4028 	/* need Max VF queues but already have default number of queues */
4029 	adq_request_qps = I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF;
4030 
4031 	if (pf->queues_left < adq_request_qps) {
4032 		dev_err(&pf->pdev->dev,
4033 			"No queues left to allocate to VF %d\n",
4034 			vf->vf_id);
4035 		aq_ret = -EINVAL;
4036 		goto err;
4037 	} else {
4038 		/* we need to allocate max VF queues to enable ADq so as to
4039 		 * make sure ADq enabled VF always gets back queues when it
4040 		 * goes through a reset.
4041 		 */
4042 		vf->num_queue_pairs = I40E_MAX_VF_QUEUES;
4043 	}
4044 
4045 	/* get link speed in MB to validate rate limit */
4046 	speed = i40e_vc_link_speed2mbps(ls->link_speed);
4047 	if (speed == SPEED_UNKNOWN) {
4048 		dev_err(&pf->pdev->dev,
4049 			"Cannot detect link speed\n");
4050 		aq_ret = -EINVAL;
4051 		goto err;
4052 	}
4053 
4054 	/* parse data from the queue channel info */
4055 	vf->num_tc = tci->num_tc;
4056 	for (i = 0; i < vf->num_tc; i++) {
4057 		if (tci->list[i].max_tx_rate) {
4058 			if (tci->list[i].max_tx_rate > speed) {
4059 				dev_err(&pf->pdev->dev,
4060 					"Invalid max tx rate %llu specified for VF %d.",
4061 					tci->list[i].max_tx_rate,
4062 					vf->vf_id);
4063 				aq_ret = -EINVAL;
4064 				goto err;
4065 			} else {
4066 				vf->ch[i].max_tx_rate =
4067 					tci->list[i].max_tx_rate;
4068 			}
4069 		}
4070 		vf->ch[i].num_qps = tci->list[i].count;
4071 	}
4072 
4073 	/* set this flag only after making sure all inputs are sane */
4074 	vf->adq_enabled = true;
4075 
4076 	/* reset the VF in order to allocate resources */
4077 	i40e_vc_reset_vf(vf, true);
4078 
4079 	return 0;
4080 
4081 	/* send the response to the VF */
4082 err:
4083 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_CHANNELS,
4084 				       aq_ret);
4085 }
4086 
4087 /**
4088  * i40e_vc_del_qch_msg
4089  * @vf: pointer to the VF info
4090  * @msg: pointer to the msg buffer
4091  **/
4092 static int i40e_vc_del_qch_msg(struct i40e_vf *vf, u8 *msg)
4093 {
4094 	struct i40e_pf *pf = vf->pf;
4095 	int aq_ret = 0;
4096 
4097 	if (!i40e_sync_vf_state(vf, I40E_VF_STATE_ACTIVE)) {
4098 		aq_ret = -EINVAL;
4099 		goto err;
4100 	}
4101 
4102 	if (vf->adq_enabled) {
4103 		i40e_del_all_cloud_filters(vf);
4104 		i40e_del_qch(vf);
4105 		vf->adq_enabled = false;
4106 		vf->num_tc = 0;
4107 		dev_info(&pf->pdev->dev,
4108 			 "Deleting Queue Channels and cloud filters for ADq on VF %d\n",
4109 			 vf->vf_id);
4110 	} else {
4111 		dev_info(&pf->pdev->dev, "VF %d trying to delete queue channels but ADq isn't enabled\n",
4112 			 vf->vf_id);
4113 		aq_ret = -EINVAL;
4114 	}
4115 
4116 	/* reset the VF in order to allocate resources */
4117 	i40e_vc_reset_vf(vf, true);
4118 
4119 	return 0;
4120 
4121 err:
4122 	return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_CHANNELS,
4123 				       aq_ret);
4124 }
4125 
4126 /**
4127  * i40e_vc_process_vf_msg
4128  * @pf: pointer to the PF structure
4129  * @vf_id: source VF id
4130  * @v_opcode: operation code
4131  * @v_retval: unused return value code
4132  * @msg: pointer to the msg buffer
4133  * @msglen: msg length
4134  *
4135  * called from the common aeq/arq handler to
4136  * process request from VF
4137  **/
4138 int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
4139 			   u32 __always_unused v_retval, u8 *msg, u16 msglen)
4140 {
4141 	struct i40e_hw *hw = &pf->hw;
4142 	int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
4143 	struct i40e_vf *vf;
4144 	int ret;
4145 
4146 	pf->vf_aq_requests++;
4147 	if (local_vf_id < 0 || local_vf_id >= pf->num_alloc_vfs)
4148 		return -EINVAL;
4149 	vf = &(pf->vf[local_vf_id]);
4150 
4151 	/* Check if VF is disabled. */
4152 	if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
4153 		return -EINVAL;
4154 
4155 	/* perform basic checks on the msg */
4156 	ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
4157 
4158 	if (ret) {
4159 		i40e_vc_send_resp_to_vf(vf, v_opcode, -EINVAL);
4160 		dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
4161 			local_vf_id, v_opcode, msglen);
4162 		return ret;
4163 	}
4164 
4165 	switch (v_opcode) {
4166 	case VIRTCHNL_OP_VERSION:
4167 		ret = i40e_vc_get_version_msg(vf, msg);
4168 		break;
4169 	case VIRTCHNL_OP_GET_VF_RESOURCES:
4170 		ret = i40e_vc_get_vf_resources_msg(vf, msg);
4171 		i40e_vc_notify_vf_link_state(vf);
4172 		break;
4173 	case VIRTCHNL_OP_RESET_VF:
4174 		i40e_vc_reset_vf(vf, false);
4175 		ret = 0;
4176 		break;
4177 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
4178 		ret = i40e_vc_config_promiscuous_mode_msg(vf, msg);
4179 		break;
4180 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
4181 		ret = i40e_vc_config_queues_msg(vf, msg);
4182 		break;
4183 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
4184 		ret = i40e_vc_config_irq_map_msg(vf, msg);
4185 		break;
4186 	case VIRTCHNL_OP_ENABLE_QUEUES:
4187 		ret = i40e_vc_enable_queues_msg(vf, msg);
4188 		i40e_vc_notify_vf_link_state(vf);
4189 		break;
4190 	case VIRTCHNL_OP_DISABLE_QUEUES:
4191 		ret = i40e_vc_disable_queues_msg(vf, msg);
4192 		break;
4193 	case VIRTCHNL_OP_ADD_ETH_ADDR:
4194 		ret = i40e_vc_add_mac_addr_msg(vf, msg);
4195 		break;
4196 	case VIRTCHNL_OP_DEL_ETH_ADDR:
4197 		ret = i40e_vc_del_mac_addr_msg(vf, msg);
4198 		break;
4199 	case VIRTCHNL_OP_ADD_VLAN:
4200 		ret = i40e_vc_add_vlan_msg(vf, msg);
4201 		break;
4202 	case VIRTCHNL_OP_DEL_VLAN:
4203 		ret = i40e_vc_remove_vlan_msg(vf, msg);
4204 		break;
4205 	case VIRTCHNL_OP_GET_STATS:
4206 		ret = i40e_vc_get_stats_msg(vf, msg);
4207 		break;
4208 	case VIRTCHNL_OP_RDMA:
4209 		ret = i40e_vc_rdma_msg(vf, msg, msglen);
4210 		break;
4211 	case VIRTCHNL_OP_CONFIG_RDMA_IRQ_MAP:
4212 		ret = i40e_vc_rdma_qvmap_msg(vf, msg, true);
4213 		break;
4214 	case VIRTCHNL_OP_RELEASE_RDMA_IRQ_MAP:
4215 		ret = i40e_vc_rdma_qvmap_msg(vf, msg, false);
4216 		break;
4217 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
4218 		ret = i40e_vc_config_rss_key(vf, msg);
4219 		break;
4220 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
4221 		ret = i40e_vc_config_rss_lut(vf, msg);
4222 		break;
4223 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
4224 		ret = i40e_vc_get_rss_hena(vf, msg);
4225 		break;
4226 	case VIRTCHNL_OP_SET_RSS_HENA:
4227 		ret = i40e_vc_set_rss_hena(vf, msg);
4228 		break;
4229 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
4230 		ret = i40e_vc_enable_vlan_stripping(vf, msg);
4231 		break;
4232 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
4233 		ret = i40e_vc_disable_vlan_stripping(vf, msg);
4234 		break;
4235 	case VIRTCHNL_OP_REQUEST_QUEUES:
4236 		ret = i40e_vc_request_queues_msg(vf, msg);
4237 		break;
4238 	case VIRTCHNL_OP_ENABLE_CHANNELS:
4239 		ret = i40e_vc_add_qch_msg(vf, msg);
4240 		break;
4241 	case VIRTCHNL_OP_DISABLE_CHANNELS:
4242 		ret = i40e_vc_del_qch_msg(vf, msg);
4243 		break;
4244 	case VIRTCHNL_OP_ADD_CLOUD_FILTER:
4245 		ret = i40e_vc_add_cloud_filter(vf, msg);
4246 		break;
4247 	case VIRTCHNL_OP_DEL_CLOUD_FILTER:
4248 		ret = i40e_vc_del_cloud_filter(vf, msg);
4249 		break;
4250 	case VIRTCHNL_OP_UNKNOWN:
4251 	default:
4252 		dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
4253 			v_opcode, local_vf_id);
4254 		ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
4255 					      -EOPNOTSUPP);
4256 		break;
4257 	}
4258 
4259 	return ret;
4260 }
4261 
4262 /**
4263  * i40e_vc_process_vflr_event
4264  * @pf: pointer to the PF structure
4265  *
4266  * called from the vlfr irq handler to
4267  * free up VF resources and state variables
4268  **/
4269 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
4270 {
4271 	struct i40e_hw *hw = &pf->hw;
4272 	u32 reg, reg_idx, bit_idx;
4273 	struct i40e_vf *vf;
4274 	int vf_id;
4275 
4276 	if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
4277 		return 0;
4278 
4279 	/* Re-enable the VFLR interrupt cause here, before looking for which
4280 	 * VF got reset. Otherwise, if another VF gets a reset while the
4281 	 * first one is being processed, that interrupt will be lost, and
4282 	 * that VF will be stuck in reset forever.
4283 	 */
4284 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4285 	reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
4286 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4287 	i40e_flush(hw);
4288 
4289 	clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4290 	for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
4291 		reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
4292 		bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
4293 		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
4294 		vf = &pf->vf[vf_id];
4295 		reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
4296 		if (reg & BIT(bit_idx))
4297 			/* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
4298 			i40e_reset_vf(vf, true);
4299 	}
4300 
4301 	return 0;
4302 }
4303 
4304 /**
4305  * i40e_validate_vf
4306  * @pf: the physical function
4307  * @vf_id: VF identifier
4308  *
4309  * Check that the VF is enabled and the VSI exists.
4310  *
4311  * Returns 0 on success, negative on failure
4312  **/
4313 static int i40e_validate_vf(struct i40e_pf *pf, int vf_id)
4314 {
4315 	struct i40e_vsi *vsi;
4316 	struct i40e_vf *vf;
4317 	int ret = 0;
4318 
4319 	if (vf_id >= pf->num_alloc_vfs) {
4320 		dev_err(&pf->pdev->dev,
4321 			"Invalid VF Identifier %d\n", vf_id);
4322 		ret = -EINVAL;
4323 		goto err_out;
4324 	}
4325 	vf = &pf->vf[vf_id];
4326 	vsi = i40e_find_vsi_from_id(pf, vf->lan_vsi_id);
4327 	if (!vsi)
4328 		ret = -EINVAL;
4329 err_out:
4330 	return ret;
4331 }
4332 
4333 /**
4334  * i40e_check_vf_init_timeout
4335  * @vf: the virtual function
4336  *
4337  * Check that the VF's initialization was successfully done and if not
4338  * wait up to 300ms for its finish.
4339  *
4340  * Returns true when VF is initialized, false on timeout
4341  **/
4342 static bool i40e_check_vf_init_timeout(struct i40e_vf *vf)
4343 {
4344 	int i;
4345 
4346 	/* When the VF is resetting wait until it is done.
4347 	 * It can take up to 200 milliseconds, but wait for
4348 	 * up to 300 milliseconds to be safe.
4349 	 */
4350 	for (i = 0; i < 15; i++) {
4351 		if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states))
4352 			return true;
4353 		msleep(20);
4354 	}
4355 
4356 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4357 		dev_err(&vf->pf->pdev->dev,
4358 			"VF %d still in reset. Try again.\n", vf->vf_id);
4359 		return false;
4360 	}
4361 
4362 	return true;
4363 }
4364 
4365 /**
4366  * i40e_ndo_set_vf_mac
4367  * @netdev: network interface device structure
4368  * @vf_id: VF identifier
4369  * @mac: mac address
4370  *
4371  * program VF mac address
4372  **/
4373 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
4374 {
4375 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4376 	struct i40e_vsi *vsi = np->vsi;
4377 	struct i40e_pf *pf = vsi->back;
4378 	struct i40e_mac_filter *f;
4379 	struct i40e_vf *vf;
4380 	int ret = 0;
4381 	struct hlist_node *h;
4382 	int bkt;
4383 
4384 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4385 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4386 		return -EAGAIN;
4387 	}
4388 
4389 	/* validate the request */
4390 	ret = i40e_validate_vf(pf, vf_id);
4391 	if (ret)
4392 		goto error_param;
4393 
4394 	vf = &pf->vf[vf_id];
4395 	if (!i40e_check_vf_init_timeout(vf)) {
4396 		ret = -EAGAIN;
4397 		goto error_param;
4398 	}
4399 	vsi = pf->vsi[vf->lan_vsi_idx];
4400 
4401 	if (is_multicast_ether_addr(mac)) {
4402 		dev_err(&pf->pdev->dev,
4403 			"Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
4404 		ret = -EINVAL;
4405 		goto error_param;
4406 	}
4407 
4408 	/* Lock once because below invoked function add/del_filter requires
4409 	 * mac_filter_hash_lock to be held
4410 	 */
4411 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4412 
4413 	/* delete the temporary mac address */
4414 	if (!is_zero_ether_addr(vf->default_lan_addr.addr))
4415 		i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
4416 
4417 	/* Delete all the filters for this VSI - we're going to kill it
4418 	 * anyway.
4419 	 */
4420 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
4421 		__i40e_del_filter(vsi, f);
4422 
4423 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4424 
4425 	/* program mac filter */
4426 	if (i40e_sync_vsi_filters(vsi)) {
4427 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
4428 		ret = -EIO;
4429 		goto error_param;
4430 	}
4431 	ether_addr_copy(vf->default_lan_addr.addr, mac);
4432 
4433 	if (is_zero_ether_addr(mac)) {
4434 		vf->pf_set_mac = false;
4435 		dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
4436 	} else {
4437 		vf->pf_set_mac = true;
4438 		dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
4439 			 mac, vf_id);
4440 	}
4441 
4442 	/* Force the VF interface down so it has to bring up with new MAC
4443 	 * address
4444 	 */
4445 	i40e_vc_reset_vf(vf, true);
4446 	dev_info(&pf->pdev->dev, "Bring down and up the VF interface to make this change effective.\n");
4447 
4448 error_param:
4449 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4450 	return ret;
4451 }
4452 
4453 /**
4454  * i40e_ndo_set_vf_port_vlan
4455  * @netdev: network interface device structure
4456  * @vf_id: VF identifier
4457  * @vlan_id: mac address
4458  * @qos: priority setting
4459  * @vlan_proto: vlan protocol
4460  *
4461  * program VF vlan id and/or qos
4462  **/
4463 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
4464 			      u16 vlan_id, u8 qos, __be16 vlan_proto)
4465 {
4466 	u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
4467 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4468 	bool allmulti = false, alluni = false;
4469 	struct i40e_pf *pf = np->vsi->back;
4470 	struct i40e_vsi *vsi;
4471 	struct i40e_vf *vf;
4472 	int ret = 0;
4473 
4474 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4475 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4476 		return -EAGAIN;
4477 	}
4478 
4479 	/* validate the request */
4480 	ret = i40e_validate_vf(pf, vf_id);
4481 	if (ret)
4482 		goto error_pvid;
4483 
4484 	if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
4485 		dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
4486 		ret = -EINVAL;
4487 		goto error_pvid;
4488 	}
4489 
4490 	if (vlan_proto != htons(ETH_P_8021Q)) {
4491 		dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
4492 		ret = -EPROTONOSUPPORT;
4493 		goto error_pvid;
4494 	}
4495 
4496 	vf = &pf->vf[vf_id];
4497 	if (!i40e_check_vf_init_timeout(vf)) {
4498 		ret = -EAGAIN;
4499 		goto error_pvid;
4500 	}
4501 	vsi = pf->vsi[vf->lan_vsi_idx];
4502 
4503 	if (le16_to_cpu(vsi->info.pvid) == vlanprio)
4504 		/* duplicate request, so just return success */
4505 		goto error_pvid;
4506 
4507 	i40e_vlan_stripping_enable(vsi);
4508 
4509 	/* Locked once because multiple functions below iterate list */
4510 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4511 
4512 	/* Check for condition where there was already a port VLAN ID
4513 	 * filter set and now it is being deleted by setting it to zero.
4514 	 * Additionally check for the condition where there was a port
4515 	 * VLAN but now there is a new and different port VLAN being set.
4516 	 * Before deleting all the old VLAN filters we must add new ones
4517 	 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
4518 	 * MAC addresses deleted.
4519 	 */
4520 	if ((!(vlan_id || qos) ||
4521 	     vlanprio != le16_to_cpu(vsi->info.pvid)) &&
4522 	    vsi->info.pvid) {
4523 		ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
4524 		if (ret) {
4525 			dev_info(&vsi->back->pdev->dev,
4526 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4527 				 vsi->back->hw.aq.asq_last_status);
4528 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4529 			goto error_pvid;
4530 		}
4531 	}
4532 
4533 	if (vsi->info.pvid) {
4534 		/* remove all filters on the old VLAN */
4535 		i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
4536 					   VLAN_VID_MASK));
4537 	}
4538 
4539 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4540 
4541 	/* disable promisc modes in case they were enabled */
4542 	ret = i40e_config_vf_promiscuous_mode(vf, vf->lan_vsi_id,
4543 					      allmulti, alluni);
4544 	if (ret) {
4545 		dev_err(&pf->pdev->dev, "Unable to config VF promiscuous mode\n");
4546 		goto error_pvid;
4547 	}
4548 
4549 	if (vlan_id || qos)
4550 		ret = i40e_vsi_add_pvid(vsi, vlanprio);
4551 	else
4552 		i40e_vsi_remove_pvid(vsi);
4553 	spin_lock_bh(&vsi->mac_filter_hash_lock);
4554 
4555 	if (vlan_id) {
4556 		dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
4557 			 vlan_id, qos, vf_id);
4558 
4559 		/* add new VLAN filter for each MAC */
4560 		ret = i40e_add_vlan_all_mac(vsi, vlan_id);
4561 		if (ret) {
4562 			dev_info(&vsi->back->pdev->dev,
4563 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
4564 				 vsi->back->hw.aq.asq_last_status);
4565 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
4566 			goto error_pvid;
4567 		}
4568 
4569 		/* remove the previously added non-VLAN MAC filters */
4570 		i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
4571 	}
4572 
4573 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
4574 
4575 	if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
4576 		alluni = true;
4577 
4578 	if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
4579 		allmulti = true;
4580 
4581 	/* Schedule the worker thread to take care of applying changes */
4582 	i40e_service_event_schedule(vsi->back);
4583 
4584 	if (ret) {
4585 		dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
4586 		goto error_pvid;
4587 	}
4588 
4589 	/* The Port VLAN needs to be saved across resets the same as the
4590 	 * default LAN MAC address.
4591 	 */
4592 	vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
4593 
4594 	i40e_vc_reset_vf(vf, true);
4595 	/* During reset the VF got a new VSI, so refresh a pointer. */
4596 	vsi = pf->vsi[vf->lan_vsi_idx];
4597 
4598 	ret = i40e_config_vf_promiscuous_mode(vf, vsi->id, allmulti, alluni);
4599 	if (ret) {
4600 		dev_err(&pf->pdev->dev, "Unable to config vf promiscuous mode\n");
4601 		goto error_pvid;
4602 	}
4603 
4604 	ret = 0;
4605 
4606 error_pvid:
4607 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4608 	return ret;
4609 }
4610 
4611 /**
4612  * i40e_ndo_set_vf_bw
4613  * @netdev: network interface device structure
4614  * @vf_id: VF identifier
4615  * @min_tx_rate: Minimum Tx rate
4616  * @max_tx_rate: Maximum Tx rate
4617  *
4618  * configure VF Tx rate
4619  **/
4620 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
4621 		       int max_tx_rate)
4622 {
4623 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4624 	struct i40e_pf *pf = np->vsi->back;
4625 	struct i40e_vsi *vsi;
4626 	struct i40e_vf *vf;
4627 	int ret = 0;
4628 
4629 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4630 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4631 		return -EAGAIN;
4632 	}
4633 
4634 	/* validate the request */
4635 	ret = i40e_validate_vf(pf, vf_id);
4636 	if (ret)
4637 		goto error;
4638 
4639 	if (min_tx_rate) {
4640 		dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
4641 			min_tx_rate, vf_id);
4642 		ret = -EINVAL;
4643 		goto error;
4644 	}
4645 
4646 	vf = &pf->vf[vf_id];
4647 	if (!i40e_check_vf_init_timeout(vf)) {
4648 		ret = -EAGAIN;
4649 		goto error;
4650 	}
4651 	vsi = pf->vsi[vf->lan_vsi_idx];
4652 
4653 	ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
4654 	if (ret)
4655 		goto error;
4656 
4657 	vf->tx_rate = max_tx_rate;
4658 error:
4659 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4660 	return ret;
4661 }
4662 
4663 /**
4664  * i40e_ndo_get_vf_config
4665  * @netdev: network interface device structure
4666  * @vf_id: VF identifier
4667  * @ivi: VF configuration structure
4668  *
4669  * return VF configuration
4670  **/
4671 int i40e_ndo_get_vf_config(struct net_device *netdev,
4672 			   int vf_id, struct ifla_vf_info *ivi)
4673 {
4674 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4675 	struct i40e_vsi *vsi = np->vsi;
4676 	struct i40e_pf *pf = vsi->back;
4677 	struct i40e_vf *vf;
4678 	int ret = 0;
4679 
4680 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4681 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4682 		return -EAGAIN;
4683 	}
4684 
4685 	/* validate the request */
4686 	ret = i40e_validate_vf(pf, vf_id);
4687 	if (ret)
4688 		goto error_param;
4689 
4690 	vf = &pf->vf[vf_id];
4691 	/* first vsi is always the LAN vsi */
4692 	vsi = pf->vsi[vf->lan_vsi_idx];
4693 	if (!vsi) {
4694 		ret = -ENOENT;
4695 		goto error_param;
4696 	}
4697 
4698 	ivi->vf = vf_id;
4699 
4700 	ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
4701 
4702 	ivi->max_tx_rate = vf->tx_rate;
4703 	ivi->min_tx_rate = 0;
4704 	ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
4705 	ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
4706 		   I40E_VLAN_PRIORITY_SHIFT;
4707 	if (vf->link_forced == false)
4708 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
4709 	else if (vf->link_up == true)
4710 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
4711 	else
4712 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
4713 	ivi->spoofchk = vf->spoofchk;
4714 	ivi->trusted = vf->trusted;
4715 	ret = 0;
4716 
4717 error_param:
4718 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4719 	return ret;
4720 }
4721 
4722 /**
4723  * i40e_ndo_set_vf_link_state
4724  * @netdev: network interface device structure
4725  * @vf_id: VF identifier
4726  * @link: required link state
4727  *
4728  * Set the link state of a specified VF, regardless of physical link state
4729  **/
4730 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
4731 {
4732 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4733 	struct i40e_pf *pf = np->vsi->back;
4734 	struct i40e_link_status *ls = &pf->hw.phy.link_info;
4735 	struct virtchnl_pf_event pfe;
4736 	struct i40e_hw *hw = &pf->hw;
4737 	struct i40e_vf *vf;
4738 	int abs_vf_id;
4739 	int ret = 0;
4740 
4741 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4742 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4743 		return -EAGAIN;
4744 	}
4745 
4746 	/* validate the request */
4747 	if (vf_id >= pf->num_alloc_vfs) {
4748 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4749 		ret = -EINVAL;
4750 		goto error_out;
4751 	}
4752 
4753 	vf = &pf->vf[vf_id];
4754 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
4755 
4756 	pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
4757 	pfe.severity = PF_EVENT_SEVERITY_INFO;
4758 
4759 	switch (link) {
4760 	case IFLA_VF_LINK_STATE_AUTO:
4761 		vf->link_forced = false;
4762 		i40e_set_vf_link_state(vf, &pfe, ls);
4763 		break;
4764 	case IFLA_VF_LINK_STATE_ENABLE:
4765 		vf->link_forced = true;
4766 		vf->link_up = true;
4767 		i40e_set_vf_link_state(vf, &pfe, ls);
4768 		break;
4769 	case IFLA_VF_LINK_STATE_DISABLE:
4770 		vf->link_forced = true;
4771 		vf->link_up = false;
4772 		i40e_set_vf_link_state(vf, &pfe, ls);
4773 		break;
4774 	default:
4775 		ret = -EINVAL;
4776 		goto error_out;
4777 	}
4778 	/* Notify the VF of its new link state */
4779 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
4780 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
4781 
4782 error_out:
4783 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4784 	return ret;
4785 }
4786 
4787 /**
4788  * i40e_ndo_set_vf_spoofchk
4789  * @netdev: network interface device structure
4790  * @vf_id: VF identifier
4791  * @enable: flag to enable or disable feature
4792  *
4793  * Enable or disable VF spoof checking
4794  **/
4795 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
4796 {
4797 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4798 	struct i40e_vsi *vsi = np->vsi;
4799 	struct i40e_pf *pf = vsi->back;
4800 	struct i40e_vsi_context ctxt;
4801 	struct i40e_hw *hw = &pf->hw;
4802 	struct i40e_vf *vf;
4803 	int ret = 0;
4804 
4805 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4806 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4807 		return -EAGAIN;
4808 	}
4809 
4810 	/* validate the request */
4811 	if (vf_id >= pf->num_alloc_vfs) {
4812 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4813 		ret = -EINVAL;
4814 		goto out;
4815 	}
4816 
4817 	vf = &(pf->vf[vf_id]);
4818 	if (!i40e_check_vf_init_timeout(vf)) {
4819 		ret = -EAGAIN;
4820 		goto out;
4821 	}
4822 
4823 	if (enable == vf->spoofchk)
4824 		goto out;
4825 
4826 	vf->spoofchk = enable;
4827 	memset(&ctxt, 0, sizeof(ctxt));
4828 	ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
4829 	ctxt.pf_num = pf->hw.pf_id;
4830 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
4831 	if (enable)
4832 		ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
4833 					I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
4834 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
4835 	if (ret) {
4836 		dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
4837 			ret);
4838 		ret = -EIO;
4839 	}
4840 out:
4841 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4842 	return ret;
4843 }
4844 
4845 /**
4846  * i40e_ndo_set_vf_trust
4847  * @netdev: network interface device structure of the pf
4848  * @vf_id: VF identifier
4849  * @setting: trust setting
4850  *
4851  * Enable or disable VF trust setting
4852  **/
4853 int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
4854 {
4855 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4856 	struct i40e_pf *pf = np->vsi->back;
4857 	struct i40e_vf *vf;
4858 	int ret = 0;
4859 
4860 	if (test_and_set_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state)) {
4861 		dev_warn(&pf->pdev->dev, "Unable to configure VFs, other operation is pending.\n");
4862 		return -EAGAIN;
4863 	}
4864 
4865 	/* validate the request */
4866 	if (vf_id >= pf->num_alloc_vfs) {
4867 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
4868 		ret = -EINVAL;
4869 		goto out;
4870 	}
4871 
4872 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
4873 		dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
4874 		ret = -EINVAL;
4875 		goto out;
4876 	}
4877 
4878 	vf = &pf->vf[vf_id];
4879 
4880 	if (setting == vf->trusted)
4881 		goto out;
4882 
4883 	vf->trusted = setting;
4884 
4885 	/* request PF to sync mac/vlan filters for the VF */
4886 	set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
4887 	pf->vsi[vf->lan_vsi_idx]->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
4888 
4889 	i40e_vc_reset_vf(vf, true);
4890 	dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
4891 		 vf_id, setting ? "" : "un");
4892 
4893 	if (vf->adq_enabled) {
4894 		if (!vf->trusted) {
4895 			dev_info(&pf->pdev->dev,
4896 				 "VF %u no longer Trusted, deleting all cloud filters\n",
4897 				 vf_id);
4898 			i40e_del_all_cloud_filters(vf);
4899 		}
4900 	}
4901 
4902 out:
4903 	clear_bit(__I40E_VIRTCHNL_OP_PENDING, pf->state);
4904 	return ret;
4905 }
4906 
4907 /**
4908  * i40e_get_vf_stats - populate some stats for the VF
4909  * @netdev: the netdev of the PF
4910  * @vf_id: the host OS identifier (0-127)
4911  * @vf_stats: pointer to the OS memory to be initialized
4912  */
4913 int i40e_get_vf_stats(struct net_device *netdev, int vf_id,
4914 		      struct ifla_vf_stats *vf_stats)
4915 {
4916 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4917 	struct i40e_pf *pf = np->vsi->back;
4918 	struct i40e_eth_stats *stats;
4919 	struct i40e_vsi *vsi;
4920 	struct i40e_vf *vf;
4921 
4922 	/* validate the request */
4923 	if (i40e_validate_vf(pf, vf_id))
4924 		return -EINVAL;
4925 
4926 	vf = &pf->vf[vf_id];
4927 	if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
4928 		dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
4929 		return -EBUSY;
4930 	}
4931 
4932 	vsi = pf->vsi[vf->lan_vsi_idx];
4933 	if (!vsi)
4934 		return -EINVAL;
4935 
4936 	i40e_update_eth_stats(vsi);
4937 	stats = &vsi->eth_stats;
4938 
4939 	memset(vf_stats, 0, sizeof(*vf_stats));
4940 
4941 	vf_stats->rx_packets = stats->rx_unicast + stats->rx_broadcast +
4942 		stats->rx_multicast;
4943 	vf_stats->tx_packets = stats->tx_unicast + stats->tx_broadcast +
4944 		stats->tx_multicast;
4945 	vf_stats->rx_bytes   = stats->rx_bytes;
4946 	vf_stats->tx_bytes   = stats->tx_bytes;
4947 	vf_stats->broadcast  = stats->rx_broadcast;
4948 	vf_stats->multicast  = stats->rx_multicast;
4949 	vf_stats->rx_dropped = stats->rx_discards + stats->rx_discards_other;
4950 	vf_stats->tx_dropped = stats->tx_discards;
4951 
4952 	return 0;
4953 }
4954