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