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