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