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 i40e_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_STAT_INIT, &vf->vf_states) &&
54 		    !test_bit(I40E_VF_STAT_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 i40e_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 = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
80 	pfe.severity = I40E_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 ? I40E_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 = ls->link_speed;
89 	}
90 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
91 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
92 }
93 
94 /**
95  * i40e_vc_notify_link_state
96  * @pf: pointer to the PF structure
97  *
98  * send a link status message to all VFs on a given PF
99  **/
100 void i40e_vc_notify_link_state(struct i40e_pf *pf)
101 {
102 	int i;
103 
104 	for (i = 0; i < pf->num_alloc_vfs; i++)
105 		i40e_vc_notify_vf_link_state(&pf->vf[i]);
106 }
107 
108 /**
109  * i40e_vc_notify_reset
110  * @pf: pointer to the PF structure
111  *
112  * indicate a pending reset to all VFs on a given PF
113  **/
114 void i40e_vc_notify_reset(struct i40e_pf *pf)
115 {
116 	struct i40e_virtchnl_pf_event pfe;
117 
118 	pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
119 	pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
120 	i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, 0,
121 			     (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
122 }
123 
124 /**
125  * i40e_vc_notify_vf_reset
126  * @vf: pointer to the VF structure
127  *
128  * indicate a pending reset to the given VF
129  **/
130 void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
131 {
132 	struct i40e_virtchnl_pf_event pfe;
133 	int abs_vf_id;
134 
135 	/* validate the request */
136 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
137 		return;
138 
139 	/* verify if the VF is in either init or active before proceeding */
140 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
141 	    !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
142 		return;
143 
144 	abs_vf_id = vf->vf_id + (int)vf->pf->hw.func_caps.vf_base_id;
145 
146 	pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
147 	pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
148 	i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
149 			       0, (u8 *)&pfe,
150 			       sizeof(struct i40e_virtchnl_pf_event), NULL);
151 }
152 /***********************misc routines*****************************/
153 
154 /**
155  * i40e_vc_disable_vf
156  * @pf: pointer to the PF info
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_pf *pf, struct i40e_vf *vf)
162 {
163 	i40e_vc_notify_vf_reset(vf);
164 	i40e_reset_vf(vf, false);
165 }
166 
167 /**
168  * i40e_vc_isvalid_vsi_id
169  * @vf: pointer to the VF info
170  * @vsi_id: VF relative VSI id
171  *
172  * check for the valid VSI id
173  **/
174 static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
175 {
176 	struct i40e_pf *pf = vf->pf;
177 	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
178 
179 	return (vsi && (vsi->vf_id == vf->vf_id));
180 }
181 
182 /**
183  * i40e_vc_isvalid_queue_id
184  * @vf: pointer to the VF info
185  * @vsi_id: vsi id
186  * @qid: vsi relative queue id
187  *
188  * check for the valid queue id
189  **/
190 static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
191 					    u8 qid)
192 {
193 	struct i40e_pf *pf = vf->pf;
194 	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
195 
196 	return (vsi && (qid < vsi->alloc_queue_pairs));
197 }
198 
199 /**
200  * i40e_vc_isvalid_vector_id
201  * @vf: pointer to the VF info
202  * @vector_id: VF relative vector id
203  *
204  * check for the valid vector id
205  **/
206 static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
207 {
208 	struct i40e_pf *pf = vf->pf;
209 
210 	return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
211 }
212 
213 /***********************vf resource mgmt routines*****************/
214 
215 /**
216  * i40e_vc_get_pf_queue_id
217  * @vf: pointer to the VF info
218  * @vsi_id: id of VSI as provided by the FW
219  * @vsi_queue_id: vsi relative queue id
220  *
221  * return PF relative queue id
222  **/
223 static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
224 				   u8 vsi_queue_id)
225 {
226 	struct i40e_pf *pf = vf->pf;
227 	struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
228 	u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
229 
230 	if (!vsi)
231 		return pf_queue_id;
232 
233 	if (le16_to_cpu(vsi->info.mapping_flags) &
234 	    I40E_AQ_VSI_QUE_MAP_NONCONTIG)
235 		pf_queue_id =
236 			le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
237 	else
238 		pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
239 			      vsi_queue_id;
240 
241 	return pf_queue_id;
242 }
243 
244 /**
245  * i40e_config_irq_link_list
246  * @vf: pointer to the VF info
247  * @vsi_id: id of VSI as given by the FW
248  * @vecmap: irq map info
249  *
250  * configure irq link list from the map
251  **/
252 static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
253 				      struct i40e_virtchnl_vector_map *vecmap)
254 {
255 	unsigned long linklistmap = 0, tempmap;
256 	struct i40e_pf *pf = vf->pf;
257 	struct i40e_hw *hw = &pf->hw;
258 	u16 vsi_queue_id, pf_queue_id;
259 	enum i40e_queue_type qtype;
260 	u16 next_q, vector_id;
261 	u32 reg, reg_idx;
262 	u16 itr_idx = 0;
263 
264 	vector_id = vecmap->vector_id;
265 	/* setup the head */
266 	if (0 == vector_id)
267 		reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
268 	else
269 		reg_idx = I40E_VPINT_LNKLSTN(
270 		     ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
271 		     (vector_id - 1));
272 
273 	if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
274 		/* Special case - No queues mapped on this vector */
275 		wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
276 		goto irq_list_done;
277 	}
278 	tempmap = vecmap->rxq_map;
279 	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
280 		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
281 				    vsi_queue_id));
282 	}
283 
284 	tempmap = vecmap->txq_map;
285 	for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
286 		linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
287 				     vsi_queue_id + 1));
288 	}
289 
290 	next_q = find_first_bit(&linklistmap,
291 				(I40E_MAX_VSI_QP *
292 				 I40E_VIRTCHNL_SUPPORTED_QTYPES));
293 	vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
294 	qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
295 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
296 	reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
297 
298 	wr32(hw, reg_idx, reg);
299 
300 	while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
301 		switch (qtype) {
302 		case I40E_QUEUE_TYPE_RX:
303 			reg_idx = I40E_QINT_RQCTL(pf_queue_id);
304 			itr_idx = vecmap->rxitr_idx;
305 			break;
306 		case I40E_QUEUE_TYPE_TX:
307 			reg_idx = I40E_QINT_TQCTL(pf_queue_id);
308 			itr_idx = vecmap->txitr_idx;
309 			break;
310 		default:
311 			break;
312 		}
313 
314 		next_q = find_next_bit(&linklistmap,
315 				       (I40E_MAX_VSI_QP *
316 					I40E_VIRTCHNL_SUPPORTED_QTYPES),
317 				       next_q + 1);
318 		if (next_q <
319 		    (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
320 			vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
321 			qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
322 			pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
323 							      vsi_queue_id);
324 		} else {
325 			pf_queue_id = I40E_QUEUE_END_OF_LIST;
326 			qtype = 0;
327 		}
328 
329 		/* format for the RQCTL & TQCTL regs is same */
330 		reg = (vector_id) |
331 		    (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
332 		    (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
333 		    BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
334 		    (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
335 		wr32(hw, reg_idx, reg);
336 	}
337 
338 	/* if the vf is running in polling mode and using interrupt zero,
339 	 * need to disable auto-mask on enabling zero interrupt for VFs.
340 	 */
341 	if ((vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
342 	    (vector_id == 0)) {
343 		reg = rd32(hw, I40E_GLINT_CTL);
344 		if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
345 			reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
346 			wr32(hw, I40E_GLINT_CTL, reg);
347 		}
348 	}
349 
350 irq_list_done:
351 	i40e_flush(hw);
352 }
353 
354 /**
355  * i40e_release_iwarp_qvlist
356  * @vf: pointer to the VF.
357  *
358  **/
359 static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
360 {
361 	struct i40e_pf *pf = vf->pf;
362 	struct i40e_virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
363 	u32 msix_vf;
364 	u32 i;
365 
366 	if (!vf->qvlist_info)
367 		return;
368 
369 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
370 	for (i = 0; i < qvlist_info->num_vectors; i++) {
371 		struct i40e_virtchnl_iwarp_qv_info *qv_info;
372 		u32 next_q_index, next_q_type;
373 		struct i40e_hw *hw = &pf->hw;
374 		u32 v_idx, reg_idx, reg;
375 
376 		qv_info = &qvlist_info->qv_info[i];
377 		if (!qv_info)
378 			continue;
379 		v_idx = qv_info->v_idx;
380 		if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
381 			/* Figure out the queue after CEQ and make that the
382 			 * first queue.
383 			 */
384 			reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
385 			reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
386 			next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
387 					>> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
388 			next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
389 					>> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;
390 
391 			reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
392 			reg = (next_q_index &
393 			       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
394 			       (next_q_type <<
395 			       I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
396 
397 			wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
398 		}
399 	}
400 	kfree(vf->qvlist_info);
401 	vf->qvlist_info = NULL;
402 }
403 
404 /**
405  * i40e_config_iwarp_qvlist
406  * @vf: pointer to the VF info
407  * @qvlist_info: queue and vector list
408  *
409  * Return 0 on success or < 0 on error
410  **/
411 static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
412 				    struct i40e_virtchnl_iwarp_qvlist_info *qvlist_info)
413 {
414 	struct i40e_pf *pf = vf->pf;
415 	struct i40e_hw *hw = &pf->hw;
416 	struct i40e_virtchnl_iwarp_qv_info *qv_info;
417 	u32 v_idx, i, reg_idx, reg;
418 	u32 next_q_idx, next_q_type;
419 	u32 msix_vf, size;
420 
421 	size = sizeof(struct i40e_virtchnl_iwarp_qvlist_info) +
422 	       (sizeof(struct i40e_virtchnl_iwarp_qv_info) *
423 						(qvlist_info->num_vectors - 1));
424 	vf->qvlist_info = kzalloc(size, GFP_KERNEL);
425 	vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
426 
427 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
428 	for (i = 0; i < qvlist_info->num_vectors; i++) {
429 		qv_info = &qvlist_info->qv_info[i];
430 		if (!qv_info)
431 			continue;
432 		v_idx = qv_info->v_idx;
433 
434 		/* Validate vector id belongs to this vf */
435 		if (!i40e_vc_isvalid_vector_id(vf, v_idx))
436 			goto err;
437 
438 		vf->qvlist_info->qv_info[i] = *qv_info;
439 
440 		reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
441 		/* We might be sharing the interrupt, so get the first queue
442 		 * index and type, push it down the list by adding the new
443 		 * queue on top. Also link it with the new queue in CEQCTL.
444 		 */
445 		reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
446 		next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
447 				I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
448 		next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
449 				I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
450 
451 		if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
452 			reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
453 			reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
454 			(v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
455 			(qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
456 			(next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
457 			(next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
458 			wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);
459 
460 			reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
461 			reg = (qv_info->ceq_idx &
462 			       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
463 			       (I40E_QUEUE_TYPE_PE_CEQ <<
464 			       I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
465 			wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
466 		}
467 
468 		if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
469 			reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
470 			(v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
471 			(qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));
472 
473 			wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
474 		}
475 	}
476 
477 	return 0;
478 err:
479 	kfree(vf->qvlist_info);
480 	vf->qvlist_info = NULL;
481 	return -EINVAL;
482 }
483 
484 /**
485  * i40e_config_vsi_tx_queue
486  * @vf: pointer to the VF info
487  * @vsi_id: id of VSI as provided by the FW
488  * @vsi_queue_id: vsi relative queue index
489  * @info: config. info
490  *
491  * configure tx queue
492  **/
493 static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
494 				    u16 vsi_queue_id,
495 				    struct i40e_virtchnl_txq_info *info)
496 {
497 	struct i40e_pf *pf = vf->pf;
498 	struct i40e_hw *hw = &pf->hw;
499 	struct i40e_hmc_obj_txq tx_ctx;
500 	struct i40e_vsi *vsi;
501 	u16 pf_queue_id;
502 	u32 qtx_ctl;
503 	int ret = 0;
504 
505 	if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
506 		ret = -ENOENT;
507 		goto error_context;
508 	}
509 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
510 	vsi = i40e_find_vsi_from_id(pf, vsi_id);
511 	if (!vsi) {
512 		ret = -ENOENT;
513 		goto error_context;
514 	}
515 
516 	/* clear the context structure first */
517 	memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
518 
519 	/* only set the required fields */
520 	tx_ctx.base = info->dma_ring_addr / 128;
521 	tx_ctx.qlen = info->ring_len;
522 	tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
523 	tx_ctx.rdylist_act = 0;
524 	tx_ctx.head_wb_ena = info->headwb_enabled;
525 	tx_ctx.head_wb_addr = info->dma_headwb_addr;
526 
527 	/* clear the context in the HMC */
528 	ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
529 	if (ret) {
530 		dev_err(&pf->pdev->dev,
531 			"Failed to clear VF LAN Tx queue context %d, error: %d\n",
532 			pf_queue_id, ret);
533 		ret = -ENOENT;
534 		goto error_context;
535 	}
536 
537 	/* set the context in the HMC */
538 	ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
539 	if (ret) {
540 		dev_err(&pf->pdev->dev,
541 			"Failed to set VF LAN Tx queue context %d error: %d\n",
542 			pf_queue_id, ret);
543 		ret = -ENOENT;
544 		goto error_context;
545 	}
546 
547 	/* associate this queue with the PCI VF function */
548 	qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
549 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
550 		    & I40E_QTX_CTL_PF_INDX_MASK);
551 	qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
552 		     << I40E_QTX_CTL_VFVM_INDX_SHIFT)
553 		    & I40E_QTX_CTL_VFVM_INDX_MASK);
554 	wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
555 	i40e_flush(hw);
556 
557 error_context:
558 	return ret;
559 }
560 
561 /**
562  * i40e_config_vsi_rx_queue
563  * @vf: pointer to the VF info
564  * @vsi_id: id of VSI  as provided by the FW
565  * @vsi_queue_id: vsi relative queue index
566  * @info: config. info
567  *
568  * configure rx queue
569  **/
570 static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
571 				    u16 vsi_queue_id,
572 				    struct i40e_virtchnl_rxq_info *info)
573 {
574 	struct i40e_pf *pf = vf->pf;
575 	struct i40e_hw *hw = &pf->hw;
576 	struct i40e_hmc_obj_rxq rx_ctx;
577 	u16 pf_queue_id;
578 	int ret = 0;
579 
580 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
581 
582 	/* clear the context structure first */
583 	memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
584 
585 	/* only set the required fields */
586 	rx_ctx.base = info->dma_ring_addr / 128;
587 	rx_ctx.qlen = info->ring_len;
588 
589 	if (info->splithdr_enabled) {
590 		rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2      |
591 				  I40E_RX_SPLIT_IP      |
592 				  I40E_RX_SPLIT_TCP_UDP |
593 				  I40E_RX_SPLIT_SCTP;
594 		/* header length validation */
595 		if (info->hdr_size > ((2 * 1024) - 64)) {
596 			ret = -EINVAL;
597 			goto error_param;
598 		}
599 		rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
600 
601 		/* set split mode 10b */
602 		rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
603 	}
604 
605 	/* databuffer length validation */
606 	if (info->databuffer_size > ((16 * 1024) - 128)) {
607 		ret = -EINVAL;
608 		goto error_param;
609 	}
610 	rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
611 
612 	/* max pkt. length validation */
613 	if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
614 		ret = -EINVAL;
615 		goto error_param;
616 	}
617 	rx_ctx.rxmax = info->max_pkt_size;
618 
619 	/* enable 32bytes desc always */
620 	rx_ctx.dsize = 1;
621 
622 	/* default values */
623 	rx_ctx.lrxqthresh = 2;
624 	rx_ctx.crcstrip = 1;
625 	rx_ctx.prefena = 1;
626 	rx_ctx.l2tsel = 1;
627 
628 	/* clear the context in the HMC */
629 	ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
630 	if (ret) {
631 		dev_err(&pf->pdev->dev,
632 			"Failed to clear VF LAN Rx queue context %d, error: %d\n",
633 			pf_queue_id, ret);
634 		ret = -ENOENT;
635 		goto error_param;
636 	}
637 
638 	/* set the context in the HMC */
639 	ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
640 	if (ret) {
641 		dev_err(&pf->pdev->dev,
642 			"Failed to set VF LAN Rx queue context %d error: %d\n",
643 			pf_queue_id, ret);
644 		ret = -ENOENT;
645 		goto error_param;
646 	}
647 
648 error_param:
649 	return ret;
650 }
651 
652 /**
653  * i40e_alloc_vsi_res
654  * @vf: pointer to the VF info
655  * @type: type of VSI to allocate
656  *
657  * alloc VF vsi context & resources
658  **/
659 static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
660 {
661 	struct i40e_mac_filter *f = NULL;
662 	struct i40e_pf *pf = vf->pf;
663 	struct i40e_vsi *vsi;
664 	int ret = 0;
665 
666 	vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
667 
668 	if (!vsi) {
669 		dev_err(&pf->pdev->dev,
670 			"add vsi failed for VF %d, aq_err %d\n",
671 			vf->vf_id, pf->hw.aq.asq_last_status);
672 		ret = -ENOENT;
673 		goto error_alloc_vsi_res;
674 	}
675 	if (type == I40E_VSI_SRIOV) {
676 		u64 hena = i40e_pf_get_default_rss_hena(pf);
677 		u8 broadcast[ETH_ALEN];
678 
679 		vf->lan_vsi_idx = vsi->idx;
680 		vf->lan_vsi_id = vsi->id;
681 		/* If the port VLAN has been configured and then the
682 		 * VF driver was removed then the VSI port VLAN
683 		 * configuration was destroyed.  Check if there is
684 		 * a port VLAN and restore the VSI configuration if
685 		 * needed.
686 		 */
687 		if (vf->port_vlan_id)
688 			i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
689 
690 		spin_lock_bh(&vsi->mac_filter_hash_lock);
691 		if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
692 			f = i40e_add_mac_filter(vsi,
693 						vf->default_lan_addr.addr);
694 			if (!f)
695 				dev_info(&pf->pdev->dev,
696 					 "Could not add MAC filter %pM for VF %d\n",
697 					vf->default_lan_addr.addr, vf->vf_id);
698 		}
699 		eth_broadcast_addr(broadcast);
700 		f = i40e_add_mac_filter(vsi, broadcast);
701 		if (!f)
702 			dev_info(&pf->pdev->dev,
703 				 "Could not allocate VF broadcast filter\n");
704 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
705 		i40e_write_rx_ctl(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id),
706 				  (u32)hena);
707 		i40e_write_rx_ctl(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id),
708 				  (u32)(hena >> 32));
709 	}
710 
711 	/* program mac filter */
712 	ret = i40e_sync_vsi_filters(vsi);
713 	if (ret)
714 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
715 
716 	/* Set VF bandwidth if specified */
717 	if (vf->tx_rate) {
718 		ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
719 						  vf->tx_rate / 50, 0, NULL);
720 		if (ret)
721 			dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
722 				vf->vf_id, ret);
723 	}
724 
725 error_alloc_vsi_res:
726 	return ret;
727 }
728 
729 /**
730  * i40e_enable_vf_mappings
731  * @vf: pointer to the VF info
732  *
733  * enable VF mappings
734  **/
735 static void i40e_enable_vf_mappings(struct i40e_vf *vf)
736 {
737 	struct i40e_pf *pf = vf->pf;
738 	struct i40e_hw *hw = &pf->hw;
739 	u32 reg, total_queue_pairs = 0;
740 	int j;
741 
742 	/* Tell the hardware we're using noncontiguous mapping. HW requires
743 	 * that VF queues be mapped using this method, even when they are
744 	 * contiguous in real life
745 	 */
746 	i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
747 			  I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
748 
749 	/* enable VF vplan_qtable mappings */
750 	reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
751 	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
752 
753 	/* map PF queues to VF queues */
754 	for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
755 		u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
756 
757 		reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
758 		wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
759 		total_queue_pairs++;
760 	}
761 
762 	/* map PF queues to VSI */
763 	for (j = 0; j < 7; j++) {
764 		if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
765 			reg = 0x07FF07FF;	/* unused */
766 		} else {
767 			u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
768 							  j * 2);
769 			reg = qid;
770 			qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
771 						      (j * 2) + 1);
772 			reg |= qid << 16;
773 		}
774 		i40e_write_rx_ctl(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id),
775 				  reg);
776 	}
777 
778 	i40e_flush(hw);
779 }
780 
781 /**
782  * i40e_disable_vf_mappings
783  * @vf: pointer to the VF info
784  *
785  * disable VF mappings
786  **/
787 static void i40e_disable_vf_mappings(struct i40e_vf *vf)
788 {
789 	struct i40e_pf *pf = vf->pf;
790 	struct i40e_hw *hw = &pf->hw;
791 	int i;
792 
793 	/* disable qp mappings */
794 	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
795 	for (i = 0; i < I40E_MAX_VSI_QP; i++)
796 		wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
797 		     I40E_QUEUE_END_OF_LIST);
798 	i40e_flush(hw);
799 }
800 
801 /**
802  * i40e_free_vf_res
803  * @vf: pointer to the VF info
804  *
805  * free VF resources
806  **/
807 static void i40e_free_vf_res(struct i40e_vf *vf)
808 {
809 	struct i40e_pf *pf = vf->pf;
810 	struct i40e_hw *hw = &pf->hw;
811 	u32 reg_idx, reg;
812 	int i, msix_vf;
813 
814 	/* free vsi & disconnect it from the parent uplink */
815 	if (vf->lan_vsi_idx) {
816 		i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
817 		vf->lan_vsi_idx = 0;
818 		vf->lan_vsi_id = 0;
819 		vf->num_mac = 0;
820 	}
821 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
822 
823 	/* disable interrupts so the VF starts in a known state */
824 	for (i = 0; i < msix_vf; i++) {
825 		/* format is same for both registers */
826 		if (0 == i)
827 			reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
828 		else
829 			reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
830 						      (vf->vf_id))
831 						     + (i - 1));
832 		wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
833 		i40e_flush(hw);
834 	}
835 
836 	/* clear the irq settings */
837 	for (i = 0; i < msix_vf; i++) {
838 		/* format is same for both registers */
839 		if (0 == i)
840 			reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
841 		else
842 			reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
843 						      (vf->vf_id))
844 						     + (i - 1));
845 		reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
846 		       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
847 		wr32(hw, reg_idx, reg);
848 		i40e_flush(hw);
849 	}
850 	/* reset some of the state varibles keeping
851 	 * track of the resources
852 	 */
853 	vf->num_queue_pairs = 0;
854 	vf->vf_states = 0;
855 	clear_bit(I40E_VF_STAT_INIT, &vf->vf_states);
856 }
857 
858 /**
859  * i40e_alloc_vf_res
860  * @vf: pointer to the VF info
861  *
862  * allocate VF resources
863  **/
864 static int i40e_alloc_vf_res(struct i40e_vf *vf)
865 {
866 	struct i40e_pf *pf = vf->pf;
867 	int total_queue_pairs = 0;
868 	int ret;
869 
870 	/* allocate hw vsi context & associated resources */
871 	ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
872 	if (ret)
873 		goto error_alloc;
874 	total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
875 
876 	if (vf->trusted)
877 		set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
878 	else
879 		clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
880 
881 	/* store the total qps number for the runtime
882 	 * VF req validation
883 	 */
884 	vf->num_queue_pairs = total_queue_pairs;
885 
886 	/* VF is now completely initialized */
887 	set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
888 
889 error_alloc:
890 	if (ret)
891 		i40e_free_vf_res(vf);
892 
893 	return ret;
894 }
895 
896 #define VF_DEVICE_STATUS 0xAA
897 #define VF_TRANS_PENDING_MASK 0x20
898 /**
899  * i40e_quiesce_vf_pci
900  * @vf: pointer to the VF structure
901  *
902  * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
903  * if the transactions never clear.
904  **/
905 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
906 {
907 	struct i40e_pf *pf = vf->pf;
908 	struct i40e_hw *hw = &pf->hw;
909 	int vf_abs_id, i;
910 	u32 reg;
911 
912 	vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
913 
914 	wr32(hw, I40E_PF_PCI_CIAA,
915 	     VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
916 	for (i = 0; i < 100; i++) {
917 		reg = rd32(hw, I40E_PF_PCI_CIAD);
918 		if ((reg & VF_TRANS_PENDING_MASK) == 0)
919 			return 0;
920 		udelay(1);
921 	}
922 	return -EIO;
923 }
924 
925 /**
926  * i40e_reset_vf
927  * @vf: pointer to the VF structure
928  * @flr: VFLR was issued or not
929  *
930  * reset the VF
931  **/
932 void i40e_reset_vf(struct i40e_vf *vf, bool flr)
933 {
934 	struct i40e_pf *pf = vf->pf;
935 	struct i40e_hw *hw = &pf->hw;
936 	u32 reg, reg_idx, bit_idx;
937 	bool rsd = false;
938 	int i;
939 
940 	if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
941 		return;
942 
943 	/* warn the VF */
944 	clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
945 
946 	/* In the case of a VFLR, the HW has already reset the VF and we
947 	 * just need to clean up, so don't hit the VFRTRIG register.
948 	 */
949 	if (!flr) {
950 		/* reset VF using VPGEN_VFRTRIG reg */
951 		reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
952 		reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
953 		wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
954 		i40e_flush(hw);
955 	}
956 	/* clear the VFLR bit in GLGEN_VFLRSTAT */
957 	reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
958 	bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
959 	wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
960 	i40e_flush(hw);
961 
962 	if (i40e_quiesce_vf_pci(vf))
963 		dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
964 			vf->vf_id);
965 
966 	/* poll VPGEN_VFRSTAT reg to make sure
967 	 * that reset is complete
968 	 */
969 	for (i = 0; i < 10; i++) {
970 		/* VF reset requires driver to first reset the VF and then
971 		 * poll the status register to make sure that the reset
972 		 * completed successfully. Due to internal HW FIFO flushes,
973 		 * we must wait 10ms before the register will be valid.
974 		 */
975 		usleep_range(10000, 20000);
976 		reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
977 		if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
978 			rsd = true;
979 			break;
980 		}
981 	}
982 
983 	if (flr)
984 		usleep_range(10000, 20000);
985 
986 	if (!rsd)
987 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
988 			vf->vf_id);
989 	wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
990 	/* clear the reset bit in the VPGEN_VFRTRIG reg */
991 	reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
992 	reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
993 	wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
994 
995 	/* On initial reset, we won't have any queues */
996 	if (vf->lan_vsi_idx == 0)
997 		goto complete_reset;
998 
999 	i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
1000 complete_reset:
1001 	/* reallocate VF resources to reset the VSI state */
1002 	i40e_free_vf_res(vf);
1003 	if (!i40e_alloc_vf_res(vf)) {
1004 		int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1005 		i40e_enable_vf_mappings(vf);
1006 		set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1007 		clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1008 		/* Do not notify the client during VF init */
1009 		if (vf->pf->num_alloc_vfs)
1010 			i40e_notify_client_of_vf_reset(pf, abs_vf_id);
1011 		vf->num_vlan = 0;
1012 	}
1013 	/* tell the VF the reset is done */
1014 	wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
1015 
1016 	i40e_flush(hw);
1017 	clear_bit(__I40E_VF_DISABLE, &pf->state);
1018 }
1019 
1020 /**
1021  * i40e_free_vfs
1022  * @pf: pointer to the PF structure
1023  *
1024  * free VF resources
1025  **/
1026 void i40e_free_vfs(struct i40e_pf *pf)
1027 {
1028 	struct i40e_hw *hw = &pf->hw;
1029 	u32 reg_idx, bit_idx;
1030 	int i, tmp, vf_id;
1031 
1032 	if (!pf->vf)
1033 		return;
1034 	while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
1035 		usleep_range(1000, 2000);
1036 
1037 	i40e_notify_client_of_vf_enable(pf, 0);
1038 	for (i = 0; i < pf->num_alloc_vfs; i++)
1039 		if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
1040 			i40e_vsi_stop_rings(pf->vsi[pf->vf[i].lan_vsi_idx]);
1041 
1042 	/* Disable IOV before freeing resources. This lets any VF drivers
1043 	 * running in the host get themselves cleaned up before we yank
1044 	 * the carpet out from underneath their feet.
1045 	 */
1046 	if (!pci_vfs_assigned(pf->pdev))
1047 		pci_disable_sriov(pf->pdev);
1048 	else
1049 		dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
1050 
1051 	msleep(20); /* let any messages in transit get finished up */
1052 
1053 	/* free up VF resources */
1054 	tmp = pf->num_alloc_vfs;
1055 	pf->num_alloc_vfs = 0;
1056 	for (i = 0; i < tmp; i++) {
1057 		if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
1058 			i40e_free_vf_res(&pf->vf[i]);
1059 		/* disable qp mappings */
1060 		i40e_disable_vf_mappings(&pf->vf[i]);
1061 	}
1062 
1063 	kfree(pf->vf);
1064 	pf->vf = NULL;
1065 
1066 	/* This check is for when the driver is unloaded while VFs are
1067 	 * assigned. Setting the number of VFs to 0 through sysfs is caught
1068 	 * before this function ever gets called.
1069 	 */
1070 	if (!pci_vfs_assigned(pf->pdev)) {
1071 		/* Acknowledge VFLR for all VFS. Without this, VFs will fail to
1072 		 * work correctly when SR-IOV gets re-enabled.
1073 		 */
1074 		for (vf_id = 0; vf_id < tmp; vf_id++) {
1075 			reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1076 			bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1077 			wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
1078 		}
1079 	}
1080 	clear_bit(__I40E_VF_DISABLE, &pf->state);
1081 }
1082 
1083 #ifdef CONFIG_PCI_IOV
1084 /**
1085  * i40e_alloc_vfs
1086  * @pf: pointer to the PF structure
1087  * @num_alloc_vfs: number of VFs to allocate
1088  *
1089  * allocate VF resources
1090  **/
1091 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
1092 {
1093 	struct i40e_vf *vfs;
1094 	int i, ret = 0;
1095 
1096 	/* Disable interrupt 0 so we don't try to handle the VFLR. */
1097 	i40e_irq_dynamic_disable_icr0(pf);
1098 
1099 	/* Check to see if we're just allocating resources for extant VFs */
1100 	if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
1101 		ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
1102 		if (ret) {
1103 			pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1104 			pf->num_alloc_vfs = 0;
1105 			goto err_iov;
1106 		}
1107 	}
1108 	/* allocate memory */
1109 	vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
1110 	if (!vfs) {
1111 		ret = -ENOMEM;
1112 		goto err_alloc;
1113 	}
1114 	pf->vf = vfs;
1115 
1116 	/* apply default profile */
1117 	for (i = 0; i < num_alloc_vfs; i++) {
1118 		vfs[i].pf = pf;
1119 		vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
1120 		vfs[i].vf_id = i;
1121 
1122 		/* assign default capabilities */
1123 		set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
1124 		vfs[i].spoofchk = true;
1125 		/* VF resources get allocated during reset */
1126 		i40e_reset_vf(&vfs[i], false);
1127 
1128 	}
1129 	pf->num_alloc_vfs = num_alloc_vfs;
1130 
1131 	i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
1132 
1133 err_alloc:
1134 	if (ret)
1135 		i40e_free_vfs(pf);
1136 err_iov:
1137 	/* Re-enable interrupt 0. */
1138 	i40e_irq_dynamic_enable_icr0(pf, false);
1139 	return ret;
1140 }
1141 
1142 #endif
1143 /**
1144  * i40e_pci_sriov_enable
1145  * @pdev: pointer to a pci_dev structure
1146  * @num_vfs: number of VFs to allocate
1147  *
1148  * Enable or change the number of VFs
1149  **/
1150 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
1151 {
1152 #ifdef CONFIG_PCI_IOV
1153 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1154 	int pre_existing_vfs = pci_num_vf(pdev);
1155 	int err = 0;
1156 
1157 	if (test_bit(__I40E_TESTING, &pf->state)) {
1158 		dev_warn(&pdev->dev,
1159 			 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
1160 		err = -EPERM;
1161 		goto err_out;
1162 	}
1163 
1164 	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
1165 		i40e_free_vfs(pf);
1166 	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1167 		goto out;
1168 
1169 	if (num_vfs > pf->num_req_vfs) {
1170 		dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
1171 			 num_vfs, pf->num_req_vfs);
1172 		err = -EPERM;
1173 		goto err_out;
1174 	}
1175 
1176 	dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
1177 	err = i40e_alloc_vfs(pf, num_vfs);
1178 	if (err) {
1179 		dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1180 		goto err_out;
1181 	}
1182 
1183 out:
1184 	return num_vfs;
1185 
1186 err_out:
1187 	return err;
1188 #endif
1189 	return 0;
1190 }
1191 
1192 /**
1193  * i40e_pci_sriov_configure
1194  * @pdev: pointer to a pci_dev structure
1195  * @num_vfs: number of VFs to allocate
1196  *
1197  * Enable or change the number of VFs. Called when the user updates the number
1198  * of VFs in sysfs.
1199  **/
1200 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1201 {
1202 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1203 
1204 	if (num_vfs) {
1205 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1206 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1207 			i40e_do_reset_safe(pf,
1208 					   BIT_ULL(__I40E_PF_RESET_REQUESTED));
1209 		}
1210 		return i40e_pci_sriov_enable(pdev, num_vfs);
1211 	}
1212 
1213 	if (!pci_vfs_assigned(pf->pdev)) {
1214 		i40e_free_vfs(pf);
1215 		pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1216 		i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
1217 	} else {
1218 		dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1219 		return -EINVAL;
1220 	}
1221 	return 0;
1222 }
1223 
1224 /***********************virtual channel routines******************/
1225 
1226 /**
1227  * i40e_vc_send_msg_to_vf
1228  * @vf: pointer to the VF info
1229  * @v_opcode: virtual channel opcode
1230  * @v_retval: virtual channel return value
1231  * @msg: pointer to the msg buffer
1232  * @msglen: msg length
1233  *
1234  * send msg to VF
1235  **/
1236 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1237 				  u32 v_retval, u8 *msg, u16 msglen)
1238 {
1239 	struct i40e_pf *pf;
1240 	struct i40e_hw *hw;
1241 	int abs_vf_id;
1242 	i40e_status aq_ret;
1243 
1244 	/* validate the request */
1245 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1246 		return -EINVAL;
1247 
1248 	pf = vf->pf;
1249 	hw = &pf->hw;
1250 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1251 
1252 	/* single place to detect unsuccessful return values */
1253 	if (v_retval) {
1254 		vf->num_invalid_msgs++;
1255 		dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
1256 			 vf->vf_id, v_opcode, v_retval);
1257 		if (vf->num_invalid_msgs >
1258 		    I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1259 			dev_err(&pf->pdev->dev,
1260 				"Number of invalid messages exceeded for VF %d\n",
1261 				vf->vf_id);
1262 			dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1263 			set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1264 		}
1265 	} else {
1266 		vf->num_valid_msgs++;
1267 		/* reset the invalid counter, if a valid message is received. */
1268 		vf->num_invalid_msgs = 0;
1269 	}
1270 
1271 	aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,	v_opcode, v_retval,
1272 					msg, msglen, NULL);
1273 	if (aq_ret) {
1274 		dev_info(&pf->pdev->dev,
1275 			 "Unable to send the message to VF %d aq_err %d\n",
1276 			 vf->vf_id, pf->hw.aq.asq_last_status);
1277 		return -EIO;
1278 	}
1279 
1280 	return 0;
1281 }
1282 
1283 /**
1284  * i40e_vc_send_resp_to_vf
1285  * @vf: pointer to the VF info
1286  * @opcode: operation code
1287  * @retval: return value
1288  *
1289  * send resp msg to VF
1290  **/
1291 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1292 				   enum i40e_virtchnl_ops opcode,
1293 				   i40e_status retval)
1294 {
1295 	return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1296 }
1297 
1298 /**
1299  * i40e_vc_get_version_msg
1300  * @vf: pointer to the VF info
1301  *
1302  * called from the VF to request the API version used by the PF
1303  **/
1304 static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
1305 {
1306 	struct i40e_virtchnl_version_info info = {
1307 		I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1308 	};
1309 
1310 	vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
1311 	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1312 	if (VF_IS_V10(vf))
1313 		info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1314 	return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1315 				      I40E_SUCCESS, (u8 *)&info,
1316 				      sizeof(struct
1317 					     i40e_virtchnl_version_info));
1318 }
1319 
1320 /**
1321  * i40e_vc_get_vf_resources_msg
1322  * @vf: pointer to the VF info
1323  * @msg: pointer to the msg buffer
1324  * @msglen: msg length
1325  *
1326  * called from the VF to request its resources
1327  **/
1328 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
1329 {
1330 	struct i40e_virtchnl_vf_resource *vfres = NULL;
1331 	struct i40e_pf *pf = vf->pf;
1332 	i40e_status aq_ret = 0;
1333 	struct i40e_vsi *vsi;
1334 	int num_vsis = 1;
1335 	int len = 0;
1336 	int ret;
1337 
1338 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1339 		aq_ret = I40E_ERR_PARAM;
1340 		goto err;
1341 	}
1342 
1343 	len = (sizeof(struct i40e_virtchnl_vf_resource) +
1344 	       sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1345 
1346 	vfres = kzalloc(len, GFP_KERNEL);
1347 	if (!vfres) {
1348 		aq_ret = I40E_ERR_NO_MEMORY;
1349 		len = 0;
1350 		goto err;
1351 	}
1352 	if (VF_IS_V11(vf))
1353 		vf->driver_caps = *(u32 *)msg;
1354 	else
1355 		vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
1356 				  I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
1357 				  I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1358 
1359 	vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
1360 	vsi = pf->vsi[vf->lan_vsi_idx];
1361 	if (!vsi->info.pvid)
1362 		vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1363 
1364 	if (i40e_vf_client_capable(pf, vf->vf_id, I40E_CLIENT_IWARP) &&
1365 	    (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_IWARP)) {
1366 		vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_IWARP;
1367 		set_bit(I40E_VF_STAT_IWARPENA, &vf->vf_states);
1368 	}
1369 
1370 	if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1371 		vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RSS_PF;
1372 	} else {
1373 		if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
1374 		    (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ))
1375 			vfres->vf_offload_flags |=
1376 					I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1377 		else
1378 			vfres->vf_offload_flags |=
1379 					I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
1380 	}
1381 
1382 	if (pf->flags & I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
1383 		if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
1384 			vfres->vf_offload_flags |=
1385 				I40E_VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
1386 	}
1387 
1388 	if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
1389 		if (pf->flags & I40E_FLAG_MFP_ENABLED) {
1390 			dev_err(&pf->pdev->dev,
1391 				"VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
1392 				 vf->vf_id);
1393 			ret = I40E_ERR_PARAM;
1394 			goto err;
1395 		}
1396 		vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING;
1397 	}
1398 
1399 	if (pf->flags & I40E_FLAG_WB_ON_ITR_CAPABLE) {
1400 		if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1401 			vfres->vf_offload_flags |=
1402 					I40E_VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
1403 	}
1404 
1405 	vfres->num_vsis = num_vsis;
1406 	vfres->num_queue_pairs = vf->num_queue_pairs;
1407 	vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1408 	vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
1409 	vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
1410 
1411 	if (vf->lan_vsi_idx) {
1412 		vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
1413 		vfres->vsi_res[0].vsi_type = I40E_VSI_SRIOV;
1414 		vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
1415 		/* VFs only use TC 0 */
1416 		vfres->vsi_res[0].qset_handle
1417 					  = le16_to_cpu(vsi->info.qs_handle[0]);
1418 		ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
1419 				vf->default_lan_addr.addr);
1420 	}
1421 	set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1422 
1423 err:
1424 	/* send the response back to the VF */
1425 	ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1426 				     aq_ret, (u8 *)vfres, len);
1427 
1428 	kfree(vfres);
1429 	return ret;
1430 }
1431 
1432 /**
1433  * i40e_vc_reset_vf_msg
1434  * @vf: pointer to the VF info
1435  * @msg: pointer to the msg buffer
1436  * @msglen: msg length
1437  *
1438  * called from the VF to reset itself,
1439  * unlike other virtchnl messages, PF driver
1440  * doesn't send the response back to the VF
1441  **/
1442 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1443 {
1444 	if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1445 		i40e_reset_vf(vf, false);
1446 }
1447 
1448 /**
1449  * i40e_getnum_vf_vsi_vlan_filters
1450  * @vsi: pointer to the vsi
1451  *
1452  * called to get the number of VLANs offloaded on this VF
1453  **/
1454 static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
1455 {
1456 	struct i40e_mac_filter *f;
1457 	int num_vlans = 0, bkt;
1458 
1459 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1460 		if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
1461 			num_vlans++;
1462 	}
1463 
1464 	return num_vlans;
1465 }
1466 
1467 /**
1468  * i40e_vc_config_promiscuous_mode_msg
1469  * @vf: pointer to the VF info
1470  * @msg: pointer to the msg buffer
1471  * @msglen: msg length
1472  *
1473  * called from the VF to configure the promiscuous mode of
1474  * VF vsis
1475  **/
1476 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1477 					       u8 *msg, u16 msglen)
1478 {
1479 	struct i40e_virtchnl_promisc_info *info =
1480 	    (struct i40e_virtchnl_promisc_info *)msg;
1481 	struct i40e_pf *pf = vf->pf;
1482 	struct i40e_hw *hw = &pf->hw;
1483 	struct i40e_mac_filter *f;
1484 	i40e_status aq_ret = 0;
1485 	bool allmulti = false;
1486 	struct i40e_vsi *vsi;
1487 	bool alluni = false;
1488 	int aq_err = 0;
1489 	int bkt;
1490 
1491 	vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
1492 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1493 	    !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1494 	    !vsi) {
1495 		aq_ret = I40E_ERR_PARAM;
1496 		goto error_param;
1497 	}
1498 	if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
1499 		dev_err(&pf->pdev->dev,
1500 			"Unprivileged VF %d is attempting to configure promiscuous mode\n",
1501 			vf->vf_id);
1502 		/* Lie to the VF on purpose. */
1503 		aq_ret = 0;
1504 		goto error_param;
1505 	}
1506 	/* Multicast promiscuous handling*/
1507 	if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1508 		allmulti = true;
1509 
1510 	if (vf->port_vlan_id) {
1511 		aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
1512 							    allmulti,
1513 							    vf->port_vlan_id,
1514 							    NULL);
1515 	} else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1516 		hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1517 			if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
1518 				continue;
1519 			aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
1520 								    vsi->seid,
1521 								    allmulti,
1522 								    f->vlan,
1523 								    NULL);
1524 			aq_err = pf->hw.aq.asq_last_status;
1525 			if (aq_ret) {
1526 				dev_err(&pf->pdev->dev,
1527 					"Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
1528 					f->vlan,
1529 					i40e_stat_str(&pf->hw, aq_ret),
1530 					i40e_aq_str(&pf->hw, aq_err));
1531 				break;
1532 			}
1533 		}
1534 	} else {
1535 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
1536 							       allmulti, NULL);
1537 		aq_err = pf->hw.aq.asq_last_status;
1538 		if (aq_ret) {
1539 			dev_err(&pf->pdev->dev,
1540 				"VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
1541 				vf->vf_id,
1542 				i40e_stat_str(&pf->hw, aq_ret),
1543 				i40e_aq_str(&pf->hw, aq_err));
1544 			goto error_param;
1545 		}
1546 	}
1547 
1548 	if (!aq_ret) {
1549 		dev_info(&pf->pdev->dev,
1550 			 "VF %d successfully set multicast promiscuous mode\n",
1551 			 vf->vf_id);
1552 		if (allmulti)
1553 			set_bit(I40E_VF_STAT_MC_PROMISC, &vf->vf_states);
1554 		else
1555 			clear_bit(I40E_VF_STAT_MC_PROMISC, &vf->vf_states);
1556 	}
1557 
1558 	if (info->flags & I40E_FLAG_VF_UNICAST_PROMISC)
1559 		alluni = true;
1560 	if (vf->port_vlan_id) {
1561 		aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
1562 							    alluni,
1563 							    vf->port_vlan_id,
1564 							    NULL);
1565 	} else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
1566 		hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
1567 			aq_ret = 0;
1568 			if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID) {
1569 				aq_ret =
1570 				i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
1571 								   vsi->seid,
1572 								   alluni,
1573 								   f->vlan,
1574 								   NULL);
1575 				aq_err = pf->hw.aq.asq_last_status;
1576 			}
1577 			if (aq_ret)
1578 				dev_err(&pf->pdev->dev,
1579 					"Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
1580 					f->vlan,
1581 					i40e_stat_str(&pf->hw, aq_ret),
1582 					i40e_aq_str(&pf->hw, aq_err));
1583 		}
1584 	} else {
1585 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
1586 							     allmulti, NULL,
1587 							     true);
1588 		aq_err = pf->hw.aq.asq_last_status;
1589 		if (aq_ret) {
1590 			dev_err(&pf->pdev->dev,
1591 				"VF %d failed to set unicast promiscuous mode %8.8x err %s aq_err %s\n",
1592 				vf->vf_id, info->flags,
1593 				i40e_stat_str(&pf->hw, aq_ret),
1594 				i40e_aq_str(&pf->hw, aq_err));
1595 			goto error_param;
1596 		}
1597 	}
1598 
1599 	if (!aq_ret) {
1600 		dev_info(&pf->pdev->dev,
1601 			 "VF %d successfully set unicast promiscuous mode\n",
1602 			 vf->vf_id);
1603 		if (alluni)
1604 			set_bit(I40E_VF_STAT_UC_PROMISC, &vf->vf_states);
1605 		else
1606 			clear_bit(I40E_VF_STAT_UC_PROMISC, &vf->vf_states);
1607 	}
1608 
1609 error_param:
1610 	/* send the response to the VF */
1611 	return i40e_vc_send_resp_to_vf(vf,
1612 				       I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1613 				       aq_ret);
1614 }
1615 
1616 /**
1617  * i40e_vc_config_queues_msg
1618  * @vf: pointer to the VF info
1619  * @msg: pointer to the msg buffer
1620  * @msglen: msg length
1621  *
1622  * called from the VF to configure the rx/tx
1623  * queues
1624  **/
1625 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1626 {
1627 	struct i40e_virtchnl_vsi_queue_config_info *qci =
1628 	    (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1629 	struct i40e_virtchnl_queue_pair_info *qpi;
1630 	struct i40e_pf *pf = vf->pf;
1631 	u16 vsi_id, vsi_queue_id;
1632 	i40e_status aq_ret = 0;
1633 	int i;
1634 
1635 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1636 		aq_ret = I40E_ERR_PARAM;
1637 		goto error_param;
1638 	}
1639 
1640 	vsi_id = qci->vsi_id;
1641 	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1642 		aq_ret = I40E_ERR_PARAM;
1643 		goto error_param;
1644 	}
1645 	for (i = 0; i < qci->num_queue_pairs; i++) {
1646 		qpi = &qci->qpair[i];
1647 		vsi_queue_id = qpi->txq.queue_id;
1648 		if ((qpi->txq.vsi_id != vsi_id) ||
1649 		    (qpi->rxq.vsi_id != vsi_id) ||
1650 		    (qpi->rxq.queue_id != vsi_queue_id) ||
1651 		    !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1652 			aq_ret = I40E_ERR_PARAM;
1653 			goto error_param;
1654 		}
1655 
1656 		if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1657 					     &qpi->rxq) ||
1658 		    i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1659 					     &qpi->txq)) {
1660 			aq_ret = I40E_ERR_PARAM;
1661 			goto error_param;
1662 		}
1663 	}
1664 	/* set vsi num_queue_pairs in use to num configured by VF */
1665 	pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
1666 
1667 error_param:
1668 	/* send the response to the VF */
1669 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1670 				       aq_ret);
1671 }
1672 
1673 /**
1674  * i40e_vc_config_irq_map_msg
1675  * @vf: pointer to the VF info
1676  * @msg: pointer to the msg buffer
1677  * @msglen: msg length
1678  *
1679  * called from the VF to configure the irq to
1680  * queue map
1681  **/
1682 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1683 {
1684 	struct i40e_virtchnl_irq_map_info *irqmap_info =
1685 	    (struct i40e_virtchnl_irq_map_info *)msg;
1686 	struct i40e_virtchnl_vector_map *map;
1687 	u16 vsi_id, vsi_queue_id, vector_id;
1688 	i40e_status aq_ret = 0;
1689 	unsigned long tempmap;
1690 	int i;
1691 
1692 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1693 		aq_ret = I40E_ERR_PARAM;
1694 		goto error_param;
1695 	}
1696 
1697 	for (i = 0; i < irqmap_info->num_vectors; i++) {
1698 		map = &irqmap_info->vecmap[i];
1699 
1700 		vector_id = map->vector_id;
1701 		vsi_id = map->vsi_id;
1702 		/* validate msg params */
1703 		if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1704 		    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1705 			aq_ret = I40E_ERR_PARAM;
1706 			goto error_param;
1707 		}
1708 
1709 		/* lookout for the invalid queue index */
1710 		tempmap = map->rxq_map;
1711 		for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1712 			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1713 						      vsi_queue_id)) {
1714 				aq_ret = I40E_ERR_PARAM;
1715 				goto error_param;
1716 			}
1717 		}
1718 
1719 		tempmap = map->txq_map;
1720 		for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1721 			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1722 						      vsi_queue_id)) {
1723 				aq_ret = I40E_ERR_PARAM;
1724 				goto error_param;
1725 			}
1726 		}
1727 
1728 		i40e_config_irq_link_list(vf, vsi_id, map);
1729 	}
1730 error_param:
1731 	/* send the response to the VF */
1732 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1733 				       aq_ret);
1734 }
1735 
1736 /**
1737  * i40e_vc_enable_queues_msg
1738  * @vf: pointer to the VF info
1739  * @msg: pointer to the msg buffer
1740  * @msglen: msg length
1741  *
1742  * called from the VF to enable all or specific queue(s)
1743  **/
1744 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1745 {
1746 	struct i40e_virtchnl_queue_select *vqs =
1747 	    (struct i40e_virtchnl_queue_select *)msg;
1748 	struct i40e_pf *pf = vf->pf;
1749 	u16 vsi_id = vqs->vsi_id;
1750 	i40e_status aq_ret = 0;
1751 
1752 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1753 		aq_ret = I40E_ERR_PARAM;
1754 		goto error_param;
1755 	}
1756 
1757 	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1758 		aq_ret = I40E_ERR_PARAM;
1759 		goto error_param;
1760 	}
1761 
1762 	if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1763 		aq_ret = I40E_ERR_PARAM;
1764 		goto error_param;
1765 	}
1766 
1767 	if (i40e_vsi_start_rings(pf->vsi[vf->lan_vsi_idx]))
1768 		aq_ret = I40E_ERR_TIMEOUT;
1769 error_param:
1770 	/* send the response to the VF */
1771 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1772 				       aq_ret);
1773 }
1774 
1775 /**
1776  * i40e_vc_disable_queues_msg
1777  * @vf: pointer to the VF info
1778  * @msg: pointer to the msg buffer
1779  * @msglen: msg length
1780  *
1781  * called from the VF to disable all or specific
1782  * queue(s)
1783  **/
1784 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1785 {
1786 	struct i40e_virtchnl_queue_select *vqs =
1787 	    (struct i40e_virtchnl_queue_select *)msg;
1788 	struct i40e_pf *pf = vf->pf;
1789 	i40e_status aq_ret = 0;
1790 
1791 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1792 		aq_ret = I40E_ERR_PARAM;
1793 		goto error_param;
1794 	}
1795 
1796 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1797 		aq_ret = I40E_ERR_PARAM;
1798 		goto error_param;
1799 	}
1800 
1801 	if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1802 		aq_ret = I40E_ERR_PARAM;
1803 		goto error_param;
1804 	}
1805 
1806 	i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
1807 
1808 error_param:
1809 	/* send the response to the VF */
1810 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1811 				       aq_ret);
1812 }
1813 
1814 /**
1815  * i40e_vc_get_stats_msg
1816  * @vf: pointer to the VF info
1817  * @msg: pointer to the msg buffer
1818  * @msglen: msg length
1819  *
1820  * called from the VF to get vsi stats
1821  **/
1822 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1823 {
1824 	struct i40e_virtchnl_queue_select *vqs =
1825 	    (struct i40e_virtchnl_queue_select *)msg;
1826 	struct i40e_pf *pf = vf->pf;
1827 	struct i40e_eth_stats stats;
1828 	i40e_status aq_ret = 0;
1829 	struct i40e_vsi *vsi;
1830 
1831 	memset(&stats, 0, sizeof(struct i40e_eth_stats));
1832 
1833 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1834 		aq_ret = I40E_ERR_PARAM;
1835 		goto error_param;
1836 	}
1837 
1838 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1839 		aq_ret = I40E_ERR_PARAM;
1840 		goto error_param;
1841 	}
1842 
1843 	vsi = pf->vsi[vf->lan_vsi_idx];
1844 	if (!vsi) {
1845 		aq_ret = I40E_ERR_PARAM;
1846 		goto error_param;
1847 	}
1848 	i40e_update_eth_stats(vsi);
1849 	stats = vsi->eth_stats;
1850 
1851 error_param:
1852 	/* send the response back to the VF */
1853 	return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1854 				      (u8 *)&stats, sizeof(stats));
1855 }
1856 
1857 /* If the VF is not trusted restrict the number of MAC/VLAN it can program */
1858 #define I40E_VC_MAX_MAC_ADDR_PER_VF 8
1859 #define I40E_VC_MAX_VLAN_PER_VF 8
1860 
1861 /**
1862  * i40e_check_vf_permission
1863  * @vf: pointer to the VF info
1864  * @macaddr: pointer to the MAC Address being checked
1865  *
1866  * Check if the VF has permission to add or delete unicast MAC address
1867  * filters and return error code -EPERM if not.  Then check if the
1868  * address filter requested is broadcast or zero and if so return
1869  * an invalid MAC address error code.
1870  **/
1871 static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1872 {
1873 	struct i40e_pf *pf = vf->pf;
1874 	int ret = 0;
1875 
1876 	if (is_broadcast_ether_addr(macaddr) ||
1877 		   is_zero_ether_addr(macaddr)) {
1878 		dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1879 		ret = I40E_ERR_INVALID_MAC_ADDR;
1880 	} else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1881 		   !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
1882 		   !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
1883 		/* If the host VMM administrator has set the VF MAC address
1884 		 * administratively via the ndo_set_vf_mac command then deny
1885 		 * permission to the VF to add or delete unicast MAC addresses.
1886 		 * Unless the VF is privileged and then it can do whatever.
1887 		 * The VF may request to set the MAC address filter already
1888 		 * assigned to it so do not return an error in that case.
1889 		 */
1890 		dev_err(&pf->pdev->dev,
1891 			"VF attempting to override administratively set MAC address, reload the VF driver to resume normal operation\n");
1892 		ret = -EPERM;
1893 	} else if ((vf->num_mac >= I40E_VC_MAX_MAC_ADDR_PER_VF) &&
1894 		   !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
1895 		dev_err(&pf->pdev->dev,
1896 			"VF is not trusted, switch the VF to trusted to add more functionality\n");
1897 		ret = -EPERM;
1898 	}
1899 	return ret;
1900 }
1901 
1902 /**
1903  * i40e_vc_add_mac_addr_msg
1904  * @vf: pointer to the VF info
1905  * @msg: pointer to the msg buffer
1906  * @msglen: msg length
1907  *
1908  * add guest mac address filter
1909  **/
1910 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1911 {
1912 	struct i40e_virtchnl_ether_addr_list *al =
1913 	    (struct i40e_virtchnl_ether_addr_list *)msg;
1914 	struct i40e_pf *pf = vf->pf;
1915 	struct i40e_vsi *vsi = NULL;
1916 	u16 vsi_id = al->vsi_id;
1917 	i40e_status ret = 0;
1918 	int i;
1919 
1920 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1921 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1922 		ret = I40E_ERR_PARAM;
1923 		goto error_param;
1924 	}
1925 
1926 	for (i = 0; i < al->num_elements; i++) {
1927 		ret = i40e_check_vf_permission(vf, al->list[i].addr);
1928 		if (ret)
1929 			goto error_param;
1930 	}
1931 	vsi = pf->vsi[vf->lan_vsi_idx];
1932 
1933 	/* Lock once, because all function inside for loop accesses VSI's
1934 	 * MAC filter list which needs to be protected using same lock.
1935 	 */
1936 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1937 
1938 	/* add new addresses to the list */
1939 	for (i = 0; i < al->num_elements; i++) {
1940 		struct i40e_mac_filter *f;
1941 
1942 		f = i40e_find_mac(vsi, al->list[i].addr);
1943 		if (!f)
1944 			f = i40e_add_mac_filter(vsi, al->list[i].addr);
1945 
1946 		if (!f) {
1947 			dev_err(&pf->pdev->dev,
1948 				"Unable to add MAC filter %pM for VF %d\n",
1949 				 al->list[i].addr, vf->vf_id);
1950 			ret = I40E_ERR_PARAM;
1951 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
1952 			goto error_param;
1953 		} else {
1954 			vf->num_mac++;
1955 		}
1956 	}
1957 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1958 
1959 	/* program the updated filter list */
1960 	ret = i40e_sync_vsi_filters(vsi);
1961 	if (ret)
1962 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
1963 			vf->vf_id, ret);
1964 
1965 error_param:
1966 	/* send the response to the VF */
1967 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
1968 				       ret);
1969 }
1970 
1971 /**
1972  * i40e_vc_del_mac_addr_msg
1973  * @vf: pointer to the VF info
1974  * @msg: pointer to the msg buffer
1975  * @msglen: msg length
1976  *
1977  * remove guest mac address filter
1978  **/
1979 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1980 {
1981 	struct i40e_virtchnl_ether_addr_list *al =
1982 	    (struct i40e_virtchnl_ether_addr_list *)msg;
1983 	struct i40e_pf *pf = vf->pf;
1984 	struct i40e_vsi *vsi = NULL;
1985 	u16 vsi_id = al->vsi_id;
1986 	i40e_status ret = 0;
1987 	int i;
1988 
1989 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1990 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1991 		ret = I40E_ERR_PARAM;
1992 		goto error_param;
1993 	}
1994 
1995 	for (i = 0; i < al->num_elements; i++) {
1996 		if (is_broadcast_ether_addr(al->list[i].addr) ||
1997 		    is_zero_ether_addr(al->list[i].addr)) {
1998 			dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
1999 				al->list[i].addr, vf->vf_id);
2000 			ret = I40E_ERR_INVALID_MAC_ADDR;
2001 			goto error_param;
2002 		}
2003 	}
2004 	vsi = pf->vsi[vf->lan_vsi_idx];
2005 
2006 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2007 	/* delete addresses from the list */
2008 	for (i = 0; i < al->num_elements; i++)
2009 		if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
2010 			ret = I40E_ERR_INVALID_MAC_ADDR;
2011 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
2012 			goto error_param;
2013 		} else {
2014 			vf->num_mac--;
2015 		}
2016 
2017 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2018 
2019 	/* program the updated filter list */
2020 	ret = i40e_sync_vsi_filters(vsi);
2021 	if (ret)
2022 		dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
2023 			vf->vf_id, ret);
2024 
2025 error_param:
2026 	/* send the response to the VF */
2027 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
2028 				       ret);
2029 }
2030 
2031 /**
2032  * i40e_vc_add_vlan_msg
2033  * @vf: pointer to the VF info
2034  * @msg: pointer to the msg buffer
2035  * @msglen: msg length
2036  *
2037  * program guest vlan id
2038  **/
2039 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2040 {
2041 	struct i40e_virtchnl_vlan_filter_list *vfl =
2042 	    (struct i40e_virtchnl_vlan_filter_list *)msg;
2043 	struct i40e_pf *pf = vf->pf;
2044 	struct i40e_vsi *vsi = NULL;
2045 	u16 vsi_id = vfl->vsi_id;
2046 	i40e_status aq_ret = 0;
2047 	int i;
2048 
2049 	if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
2050 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
2051 		dev_err(&pf->pdev->dev,
2052 			"VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
2053 		goto error_param;
2054 	}
2055 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2056 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2057 		aq_ret = I40E_ERR_PARAM;
2058 		goto error_param;
2059 	}
2060 
2061 	for (i = 0; i < vfl->num_elements; i++) {
2062 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2063 			aq_ret = I40E_ERR_PARAM;
2064 			dev_err(&pf->pdev->dev,
2065 				"invalid VF VLAN id %d\n", vfl->vlan_id[i]);
2066 			goto error_param;
2067 		}
2068 	}
2069 	vsi = pf->vsi[vf->lan_vsi_idx];
2070 	if (vsi->info.pvid) {
2071 		aq_ret = I40E_ERR_PARAM;
2072 		goto error_param;
2073 	}
2074 
2075 	i40e_vlan_stripping_enable(vsi);
2076 	for (i = 0; i < vfl->num_elements; i++) {
2077 		/* add new VLAN filter */
2078 		int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
2079 		if (!ret)
2080 			vf->num_vlan++;
2081 
2082 		if (test_bit(I40E_VF_STAT_UC_PROMISC, &vf->vf_states))
2083 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2084 							   true,
2085 							   vfl->vlan_id[i],
2086 							   NULL);
2087 		if (test_bit(I40E_VF_STAT_MC_PROMISC, &vf->vf_states))
2088 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2089 							   true,
2090 							   vfl->vlan_id[i],
2091 							   NULL);
2092 
2093 		if (ret)
2094 			dev_err(&pf->pdev->dev,
2095 				"Unable to add VLAN filter %d for VF %d, error %d\n",
2096 				vfl->vlan_id[i], vf->vf_id, ret);
2097 	}
2098 
2099 error_param:
2100 	/* send the response to the VF */
2101 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
2102 }
2103 
2104 /**
2105  * i40e_vc_remove_vlan_msg
2106  * @vf: pointer to the VF info
2107  * @msg: pointer to the msg buffer
2108  * @msglen: msg length
2109  *
2110  * remove programmed guest vlan id
2111  **/
2112 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2113 {
2114 	struct i40e_virtchnl_vlan_filter_list *vfl =
2115 	    (struct i40e_virtchnl_vlan_filter_list *)msg;
2116 	struct i40e_pf *pf = vf->pf;
2117 	struct i40e_vsi *vsi = NULL;
2118 	u16 vsi_id = vfl->vsi_id;
2119 	i40e_status aq_ret = 0;
2120 	int i;
2121 
2122 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2123 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
2124 		aq_ret = I40E_ERR_PARAM;
2125 		goto error_param;
2126 	}
2127 
2128 	for (i = 0; i < vfl->num_elements; i++) {
2129 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
2130 			aq_ret = I40E_ERR_PARAM;
2131 			goto error_param;
2132 		}
2133 	}
2134 
2135 	vsi = pf->vsi[vf->lan_vsi_idx];
2136 	if (vsi->info.pvid) {
2137 		aq_ret = I40E_ERR_PARAM;
2138 		goto error_param;
2139 	}
2140 
2141 	for (i = 0; i < vfl->num_elements; i++) {
2142 		i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
2143 		vf->num_vlan--;
2144 
2145 		if (test_bit(I40E_VF_STAT_UC_PROMISC, &vf->vf_states))
2146 			i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
2147 							   false,
2148 							   vfl->vlan_id[i],
2149 							   NULL);
2150 		if (test_bit(I40E_VF_STAT_MC_PROMISC, &vf->vf_states))
2151 			i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
2152 							   false,
2153 							   vfl->vlan_id[i],
2154 							   NULL);
2155 	}
2156 
2157 error_param:
2158 	/* send the response to the VF */
2159 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
2160 }
2161 
2162 /**
2163  * i40e_vc_iwarp_msg
2164  * @vf: pointer to the VF info
2165  * @msg: pointer to the msg buffer
2166  * @msglen: msg length
2167  *
2168  * called from the VF for the iwarp msgs
2169  **/
2170 static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
2171 {
2172 	struct i40e_pf *pf = vf->pf;
2173 	int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
2174 	i40e_status aq_ret = 0;
2175 
2176 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2177 	    !test_bit(I40E_VF_STAT_IWARPENA, &vf->vf_states)) {
2178 		aq_ret = I40E_ERR_PARAM;
2179 		goto error_param;
2180 	}
2181 
2182 	i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
2183 				     msg, msglen);
2184 
2185 error_param:
2186 	/* send the response to the VF */
2187 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_IWARP,
2188 				       aq_ret);
2189 }
2190 
2191 /**
2192  * i40e_vc_iwarp_qvmap_msg
2193  * @vf: pointer to the VF info
2194  * @msg: pointer to the msg buffer
2195  * @msglen: msg length
2196  * @config: config qvmap or release it
2197  *
2198  * called from the VF for the iwarp msgs
2199  **/
2200 static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, u16 msglen,
2201 				   bool config)
2202 {
2203 	struct i40e_virtchnl_iwarp_qvlist_info *qvlist_info =
2204 				(struct i40e_virtchnl_iwarp_qvlist_info *)msg;
2205 	i40e_status aq_ret = 0;
2206 
2207 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2208 	    !test_bit(I40E_VF_STAT_IWARPENA, &vf->vf_states)) {
2209 		aq_ret = I40E_ERR_PARAM;
2210 		goto error_param;
2211 	}
2212 
2213 	if (config) {
2214 		if (i40e_config_iwarp_qvlist(vf, qvlist_info))
2215 			aq_ret = I40E_ERR_PARAM;
2216 	} else {
2217 		i40e_release_iwarp_qvlist(vf);
2218 	}
2219 
2220 error_param:
2221 	/* send the response to the VF */
2222 	return i40e_vc_send_resp_to_vf(vf,
2223 			       config ? I40E_VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
2224 			       I40E_VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
2225 			       aq_ret);
2226 }
2227 
2228 /**
2229  * i40e_vc_config_rss_key
2230  * @vf: pointer to the VF info
2231  * @msg: pointer to the msg buffer
2232  * @msglen: msg length
2233  *
2234  * Configure the VF's RSS key
2235  **/
2236 static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg, u16 msglen)
2237 {
2238 	struct i40e_virtchnl_rss_key *vrk =
2239 		(struct i40e_virtchnl_rss_key *)msg;
2240 	struct i40e_pf *pf = vf->pf;
2241 	struct i40e_vsi *vsi = NULL;
2242 	u16 vsi_id = vrk->vsi_id;
2243 	i40e_status aq_ret = 0;
2244 
2245 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2246 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
2247 	    (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
2248 		aq_ret = I40E_ERR_PARAM;
2249 		goto err;
2250 	}
2251 
2252 	vsi = pf->vsi[vf->lan_vsi_idx];
2253 	aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
2254 err:
2255 	/* send the response to the VF */
2256 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_RSS_KEY,
2257 				       aq_ret);
2258 }
2259 
2260 /**
2261  * i40e_vc_config_rss_lut
2262  * @vf: pointer to the VF info
2263  * @msg: pointer to the msg buffer
2264  * @msglen: msg length
2265  *
2266  * Configure the VF's RSS LUT
2267  **/
2268 static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg, u16 msglen)
2269 {
2270 	struct i40e_virtchnl_rss_lut *vrl =
2271 		(struct i40e_virtchnl_rss_lut *)msg;
2272 	struct i40e_pf *pf = vf->pf;
2273 	struct i40e_vsi *vsi = NULL;
2274 	u16 vsi_id = vrl->vsi_id;
2275 	i40e_status aq_ret = 0;
2276 
2277 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
2278 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
2279 	    (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
2280 		aq_ret = I40E_ERR_PARAM;
2281 		goto err;
2282 	}
2283 
2284 	vsi = pf->vsi[vf->lan_vsi_idx];
2285 	aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
2286 	/* send the response to the VF */
2287 err:
2288 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_RSS_LUT,
2289 				       aq_ret);
2290 }
2291 
2292 /**
2293  * i40e_vc_get_rss_hena
2294  * @vf: pointer to the VF info
2295  * @msg: pointer to the msg buffer
2296  * @msglen: msg length
2297  *
2298  * Return the RSS HENA bits allowed by the hardware
2299  **/
2300 static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
2301 {
2302 	struct i40e_virtchnl_rss_hena *vrh = NULL;
2303 	struct i40e_pf *pf = vf->pf;
2304 	i40e_status aq_ret = 0;
2305 	int len = 0;
2306 
2307 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
2308 		aq_ret = I40E_ERR_PARAM;
2309 		goto err;
2310 	}
2311 	len = sizeof(struct i40e_virtchnl_rss_hena);
2312 
2313 	vrh = kzalloc(len, GFP_KERNEL);
2314 	if (!vrh) {
2315 		aq_ret = I40E_ERR_NO_MEMORY;
2316 		len = 0;
2317 		goto err;
2318 	}
2319 	vrh->hena = i40e_pf_get_default_rss_hena(pf);
2320 err:
2321 	/* send the response back to the VF */
2322 	aq_ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_RSS_HENA_CAPS,
2323 					aq_ret, (u8 *)vrh, len);
2324 	kfree(vrh);
2325 	return aq_ret;
2326 }
2327 
2328 /**
2329  * i40e_vc_set_rss_hena
2330  * @vf: pointer to the VF info
2331  * @msg: pointer to the msg buffer
2332  * @msglen: msg length
2333  *
2334  * Set the RSS HENA bits for the VF
2335  **/
2336 static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
2337 {
2338 	struct i40e_virtchnl_rss_hena *vrh =
2339 		(struct i40e_virtchnl_rss_hena *)msg;
2340 	struct i40e_pf *pf = vf->pf;
2341 	struct i40e_hw *hw = &pf->hw;
2342 	i40e_status aq_ret = 0;
2343 
2344 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
2345 		aq_ret = I40E_ERR_PARAM;
2346 		goto err;
2347 	}
2348 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
2349 	i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
2350 			  (u32)(vrh->hena >> 32));
2351 
2352 	/* send the response to the VF */
2353 err:
2354 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_SET_RSS_HENA,
2355 				       aq_ret);
2356 }
2357 
2358 /**
2359  * i40e_vc_validate_vf_msg
2360  * @vf: pointer to the VF info
2361  * @msg: pointer to the msg buffer
2362  * @msglen: msg length
2363  * @msghndl: msg handle
2364  *
2365  * validate msg
2366  **/
2367 static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
2368 				   u32 v_retval, u8 *msg, u16 msglen)
2369 {
2370 	bool err_msg_format = false;
2371 	int valid_len = 0;
2372 
2373 	/* Check if VF is disabled. */
2374 	if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
2375 		return I40E_ERR_PARAM;
2376 
2377 	/* Validate message length. */
2378 	switch (v_opcode) {
2379 	case I40E_VIRTCHNL_OP_VERSION:
2380 		valid_len = sizeof(struct i40e_virtchnl_version_info);
2381 		break;
2382 	case I40E_VIRTCHNL_OP_RESET_VF:
2383 		break;
2384 	case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
2385 		if (VF_IS_V11(vf))
2386 			valid_len = sizeof(u32);
2387 		break;
2388 	case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
2389 		valid_len = sizeof(struct i40e_virtchnl_txq_info);
2390 		break;
2391 	case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
2392 		valid_len = sizeof(struct i40e_virtchnl_rxq_info);
2393 		break;
2394 	case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2395 		valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
2396 		if (msglen >= valid_len) {
2397 			struct i40e_virtchnl_vsi_queue_config_info *vqc =
2398 			    (struct i40e_virtchnl_vsi_queue_config_info *)msg;
2399 			valid_len += (vqc->num_queue_pairs *
2400 				      sizeof(struct
2401 					     i40e_virtchnl_queue_pair_info));
2402 			if (vqc->num_queue_pairs == 0)
2403 				err_msg_format = true;
2404 		}
2405 		break;
2406 	case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
2407 		valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
2408 		if (msglen >= valid_len) {
2409 			struct i40e_virtchnl_irq_map_info *vimi =
2410 			    (struct i40e_virtchnl_irq_map_info *)msg;
2411 			valid_len += (vimi->num_vectors *
2412 				      sizeof(struct i40e_virtchnl_vector_map));
2413 			if (vimi->num_vectors == 0)
2414 				err_msg_format = true;
2415 		}
2416 		break;
2417 	case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
2418 	case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
2419 		valid_len = sizeof(struct i40e_virtchnl_queue_select);
2420 		break;
2421 	case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
2422 	case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
2423 		valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
2424 		if (msglen >= valid_len) {
2425 			struct i40e_virtchnl_ether_addr_list *veal =
2426 			    (struct i40e_virtchnl_ether_addr_list *)msg;
2427 			valid_len += veal->num_elements *
2428 			    sizeof(struct i40e_virtchnl_ether_addr);
2429 			if (veal->num_elements == 0)
2430 				err_msg_format = true;
2431 		}
2432 		break;
2433 	case I40E_VIRTCHNL_OP_ADD_VLAN:
2434 	case I40E_VIRTCHNL_OP_DEL_VLAN:
2435 		valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
2436 		if (msglen >= valid_len) {
2437 			struct i40e_virtchnl_vlan_filter_list *vfl =
2438 			    (struct i40e_virtchnl_vlan_filter_list *)msg;
2439 			valid_len += vfl->num_elements * sizeof(u16);
2440 			if (vfl->num_elements == 0)
2441 				err_msg_format = true;
2442 		}
2443 		break;
2444 	case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
2445 		valid_len = sizeof(struct i40e_virtchnl_promisc_info);
2446 		break;
2447 	case I40E_VIRTCHNL_OP_GET_STATS:
2448 		valid_len = sizeof(struct i40e_virtchnl_queue_select);
2449 		break;
2450 	case I40E_VIRTCHNL_OP_IWARP:
2451 		/* These messages are opaque to us and will be validated in
2452 		 * the RDMA client code. We just need to check for nonzero
2453 		 * length. The firmware will enforce max length restrictions.
2454 		 */
2455 		if (msglen)
2456 			valid_len = msglen;
2457 		else
2458 			err_msg_format = true;
2459 		break;
2460 	case I40E_VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
2461 		valid_len = 0;
2462 		break;
2463 	case I40E_VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
2464 		valid_len = sizeof(struct i40e_virtchnl_iwarp_qvlist_info);
2465 		if (msglen >= valid_len) {
2466 			struct i40e_virtchnl_iwarp_qvlist_info *qv =
2467 				(struct i40e_virtchnl_iwarp_qvlist_info *)msg;
2468 			if (qv->num_vectors == 0) {
2469 				err_msg_format = true;
2470 				break;
2471 			}
2472 			valid_len += ((qv->num_vectors - 1) *
2473 				sizeof(struct i40e_virtchnl_iwarp_qv_info));
2474 		}
2475 		break;
2476 	case I40E_VIRTCHNL_OP_CONFIG_RSS_KEY:
2477 		valid_len = sizeof(struct i40e_virtchnl_rss_key);
2478 		if (msglen >= valid_len) {
2479 			struct i40e_virtchnl_rss_key *vrk =
2480 				(struct i40e_virtchnl_rss_key *)msg;
2481 			if (vrk->key_len != I40E_HKEY_ARRAY_SIZE) {
2482 				err_msg_format = true;
2483 				break;
2484 			}
2485 			valid_len += vrk->key_len - 1;
2486 		}
2487 		break;
2488 	case I40E_VIRTCHNL_OP_CONFIG_RSS_LUT:
2489 		valid_len = sizeof(struct i40e_virtchnl_rss_lut);
2490 		if (msglen >= valid_len) {
2491 			struct i40e_virtchnl_rss_lut *vrl =
2492 				(struct i40e_virtchnl_rss_lut *)msg;
2493 			if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE) {
2494 				err_msg_format = true;
2495 				break;
2496 			}
2497 			valid_len += vrl->lut_entries - 1;
2498 		}
2499 		break;
2500 	case I40E_VIRTCHNL_OP_GET_RSS_HENA_CAPS:
2501 		break;
2502 	case I40E_VIRTCHNL_OP_SET_RSS_HENA:
2503 		valid_len = sizeof(struct i40e_virtchnl_rss_hena);
2504 		break;
2505 	/* These are always errors coming from the VF. */
2506 	case I40E_VIRTCHNL_OP_EVENT:
2507 	case I40E_VIRTCHNL_OP_UNKNOWN:
2508 	default:
2509 		return -EPERM;
2510 	}
2511 	/* few more checks */
2512 	if ((valid_len != msglen) || (err_msg_format)) {
2513 		i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
2514 		return -EINVAL;
2515 	} else {
2516 		return 0;
2517 	}
2518 }
2519 
2520 /**
2521  * i40e_vc_process_vf_msg
2522  * @pf: pointer to the PF structure
2523  * @vf_id: source VF id
2524  * @msg: pointer to the msg buffer
2525  * @msglen: msg length
2526  * @msghndl: msg handle
2527  *
2528  * called from the common aeq/arq handler to
2529  * process request from VF
2530  **/
2531 int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
2532 			   u32 v_retval, u8 *msg, u16 msglen)
2533 {
2534 	struct i40e_hw *hw = &pf->hw;
2535 	int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
2536 	struct i40e_vf *vf;
2537 	int ret;
2538 
2539 	pf->vf_aq_requests++;
2540 	if (local_vf_id >= pf->num_alloc_vfs)
2541 		return -EINVAL;
2542 	vf = &(pf->vf[local_vf_id]);
2543 	/* perform basic checks on the msg */
2544 	ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
2545 
2546 	if (ret) {
2547 		dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
2548 			local_vf_id, v_opcode, msglen);
2549 		return ret;
2550 	}
2551 
2552 	switch (v_opcode) {
2553 	case I40E_VIRTCHNL_OP_VERSION:
2554 		ret = i40e_vc_get_version_msg(vf, msg);
2555 		break;
2556 	case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
2557 		ret = i40e_vc_get_vf_resources_msg(vf, msg);
2558 		break;
2559 	case I40E_VIRTCHNL_OP_RESET_VF:
2560 		i40e_vc_reset_vf_msg(vf);
2561 		ret = 0;
2562 		break;
2563 	case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
2564 		ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
2565 		break;
2566 	case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
2567 		ret = i40e_vc_config_queues_msg(vf, msg, msglen);
2568 		break;
2569 	case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
2570 		ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
2571 		break;
2572 	case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
2573 		ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
2574 		i40e_vc_notify_vf_link_state(vf);
2575 		break;
2576 	case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
2577 		ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
2578 		break;
2579 	case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
2580 		ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
2581 		break;
2582 	case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
2583 		ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
2584 		break;
2585 	case I40E_VIRTCHNL_OP_ADD_VLAN:
2586 		ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
2587 		break;
2588 	case I40E_VIRTCHNL_OP_DEL_VLAN:
2589 		ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
2590 		break;
2591 	case I40E_VIRTCHNL_OP_GET_STATS:
2592 		ret = i40e_vc_get_stats_msg(vf, msg, msglen);
2593 		break;
2594 	case I40E_VIRTCHNL_OP_IWARP:
2595 		ret = i40e_vc_iwarp_msg(vf, msg, msglen);
2596 		break;
2597 	case I40E_VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
2598 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, true);
2599 		break;
2600 	case I40E_VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
2601 		ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, false);
2602 		break;
2603 	case I40E_VIRTCHNL_OP_CONFIG_RSS_KEY:
2604 		ret = i40e_vc_config_rss_key(vf, msg, msglen);
2605 		break;
2606 	case I40E_VIRTCHNL_OP_CONFIG_RSS_LUT:
2607 		ret = i40e_vc_config_rss_lut(vf, msg, msglen);
2608 		break;
2609 	case I40E_VIRTCHNL_OP_GET_RSS_HENA_CAPS:
2610 		ret = i40e_vc_get_rss_hena(vf, msg, msglen);
2611 		break;
2612 	case I40E_VIRTCHNL_OP_SET_RSS_HENA:
2613 		ret = i40e_vc_set_rss_hena(vf, msg, msglen);
2614 		break;
2615 
2616 	case I40E_VIRTCHNL_OP_UNKNOWN:
2617 	default:
2618 		dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
2619 			v_opcode, local_vf_id);
2620 		ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
2621 					      I40E_ERR_NOT_IMPLEMENTED);
2622 		break;
2623 	}
2624 
2625 	return ret;
2626 }
2627 
2628 /**
2629  * i40e_vc_process_vflr_event
2630  * @pf: pointer to the PF structure
2631  *
2632  * called from the vlfr irq handler to
2633  * free up VF resources and state variables
2634  **/
2635 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
2636 {
2637 	struct i40e_hw *hw = &pf->hw;
2638 	u32 reg, reg_idx, bit_idx;
2639 	struct i40e_vf *vf;
2640 	int vf_id;
2641 
2642 	if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
2643 		return 0;
2644 
2645 	/* Re-enable the VFLR interrupt cause here, before looking for which
2646 	 * VF got reset. Otherwise, if another VF gets a reset while the
2647 	 * first one is being processed, that interrupt will be lost, and
2648 	 * that VF will be stuck in reset forever.
2649 	 */
2650 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
2651 	reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
2652 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
2653 	i40e_flush(hw);
2654 
2655 	clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2656 	for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
2657 		reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
2658 		bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
2659 		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
2660 		vf = &pf->vf[vf_id];
2661 		reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
2662 		if (reg & BIT(bit_idx))
2663 			/* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
2664 			i40e_reset_vf(vf, true);
2665 	}
2666 
2667 	return 0;
2668 }
2669 
2670 /**
2671  * i40e_ndo_set_vf_mac
2672  * @netdev: network interface device structure
2673  * @vf_id: VF identifier
2674  * @mac: mac address
2675  *
2676  * program VF mac address
2677  **/
2678 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2679 {
2680 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2681 	struct i40e_vsi *vsi = np->vsi;
2682 	struct i40e_pf *pf = vsi->back;
2683 	struct i40e_mac_filter *f;
2684 	struct i40e_vf *vf;
2685 	int ret = 0;
2686 	int bkt;
2687 
2688 	/* validate the request */
2689 	if (vf_id >= pf->num_alloc_vfs) {
2690 		dev_err(&pf->pdev->dev,
2691 			"Invalid VF Identifier %d\n", vf_id);
2692 		ret = -EINVAL;
2693 		goto error_param;
2694 	}
2695 
2696 	vf = &(pf->vf[vf_id]);
2697 	vsi = pf->vsi[vf->lan_vsi_idx];
2698 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2699 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2700 			vf_id);
2701 		ret = -EAGAIN;
2702 		goto error_param;
2703 	}
2704 
2705 	if (is_multicast_ether_addr(mac)) {
2706 		dev_err(&pf->pdev->dev,
2707 			"Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
2708 		ret = -EINVAL;
2709 		goto error_param;
2710 	}
2711 
2712 	/* Lock once because below invoked function add/del_filter requires
2713 	 * mac_filter_hash_lock to be held
2714 	 */
2715 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2716 
2717 	/* delete the temporary mac address */
2718 	if (!is_zero_ether_addr(vf->default_lan_addr.addr))
2719 		i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
2720 
2721 	/* Delete all the filters for this VSI - we're going to kill it
2722 	 * anyway.
2723 	 */
2724 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist)
2725 		__i40e_del_filter(vsi, f);
2726 
2727 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2728 
2729 	dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2730 	/* program mac filter */
2731 	if (i40e_sync_vsi_filters(vsi)) {
2732 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2733 		ret = -EIO;
2734 		goto error_param;
2735 	}
2736 	ether_addr_copy(vf->default_lan_addr.addr, mac);
2737 	vf->pf_set_mac = true;
2738 	/* Force the VF driver stop so it has to reload with new MAC address */
2739 	i40e_vc_disable_vf(pf, vf);
2740 	dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
2741 
2742 error_param:
2743 	return ret;
2744 }
2745 
2746 /**
2747  * i40e_ndo_set_vf_port_vlan
2748  * @netdev: network interface device structure
2749  * @vf_id: VF identifier
2750  * @vlan_id: mac address
2751  * @qos: priority setting
2752  * @vlan_proto: vlan protocol
2753  *
2754  * program VF vlan id and/or qos
2755  **/
2756 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
2757 			      u16 vlan_id, u8 qos, __be16 vlan_proto)
2758 {
2759 	u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
2760 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2761 	struct i40e_pf *pf = np->vsi->back;
2762 	struct i40e_vsi *vsi;
2763 	struct i40e_vf *vf;
2764 	int ret = 0;
2765 
2766 	/* validate the request */
2767 	if (vf_id >= pf->num_alloc_vfs) {
2768 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2769 		ret = -EINVAL;
2770 		goto error_pvid;
2771 	}
2772 
2773 	if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2774 		dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2775 		ret = -EINVAL;
2776 		goto error_pvid;
2777 	}
2778 
2779 	if (vlan_proto != htons(ETH_P_8021Q)) {
2780 		dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
2781 		ret = -EPROTONOSUPPORT;
2782 		goto error_pvid;
2783 	}
2784 
2785 	vf = &(pf->vf[vf_id]);
2786 	vsi = pf->vsi[vf->lan_vsi_idx];
2787 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2788 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2789 			vf_id);
2790 		ret = -EAGAIN;
2791 		goto error_pvid;
2792 	}
2793 
2794 	if (le16_to_cpu(vsi->info.pvid) == vlanprio)
2795 		/* duplicate request, so just return success */
2796 		goto error_pvid;
2797 
2798 	/* Locked once because multiple functions below iterate list */
2799 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2800 
2801 	if (le16_to_cpu(vsi->info.pvid) == 0 && i40e_is_vsi_in_vlan(vsi)) {
2802 		dev_err(&pf->pdev->dev,
2803 			"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",
2804 			vf_id);
2805 		/* Administrator Error - knock the VF offline until he does
2806 		 * the right thing by reconfiguring his network correctly
2807 		 * and then reloading the VF driver.
2808 		 */
2809 		i40e_vc_disable_vf(pf, vf);
2810 		/* During reset the VF got a new VSI, so refresh the pointer. */
2811 		vsi = pf->vsi[vf->lan_vsi_idx];
2812 	}
2813 
2814 	/* Check for condition where there was already a port VLAN ID
2815 	 * filter set and now it is being deleted by setting it to zero.
2816 	 * Additionally check for the condition where there was a port
2817 	 * VLAN but now there is a new and different port VLAN being set.
2818 	 * Before deleting all the old VLAN filters we must add new ones
2819 	 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2820 	 * MAC addresses deleted.
2821 	 */
2822 	if ((!(vlan_id || qos) ||
2823 	    vlanprio != le16_to_cpu(vsi->info.pvid)) &&
2824 	    vsi->info.pvid) {
2825 		ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
2826 		if (ret) {
2827 			dev_info(&vsi->back->pdev->dev,
2828 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2829 				 vsi->back->hw.aq.asq_last_status);
2830 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
2831 			goto error_pvid;
2832 		}
2833 	}
2834 
2835 	if (vsi->info.pvid) {
2836 		/* remove all filters on the old VLAN */
2837 		i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
2838 					   VLAN_VID_MASK));
2839 	}
2840 
2841 	if (vlan_id || qos)
2842 		ret = i40e_vsi_add_pvid(vsi, vlanprio);
2843 	else
2844 		i40e_vsi_remove_pvid(vsi);
2845 
2846 	if (vlan_id) {
2847 		dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2848 			 vlan_id, qos, vf_id);
2849 
2850 		/* add new VLAN filter for each MAC */
2851 		ret = i40e_add_vlan_all_mac(vsi, vlan_id);
2852 		if (ret) {
2853 			dev_info(&vsi->back->pdev->dev,
2854 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2855 				 vsi->back->hw.aq.asq_last_status);
2856 			spin_unlock_bh(&vsi->mac_filter_hash_lock);
2857 			goto error_pvid;
2858 		}
2859 
2860 		/* remove the previously added non-VLAN MAC filters */
2861 		i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
2862 	}
2863 
2864 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2865 
2866 	/* Schedule the worker thread to take care of applying changes */
2867 	i40e_service_event_schedule(vsi->back);
2868 
2869 	if (ret) {
2870 		dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2871 		goto error_pvid;
2872 	}
2873 
2874 	/* The Port VLAN needs to be saved across resets the same as the
2875 	 * default LAN MAC address.
2876 	 */
2877 	vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
2878 	ret = 0;
2879 
2880 error_pvid:
2881 	return ret;
2882 }
2883 
2884 #define I40E_BW_CREDIT_DIVISOR 50     /* 50Mbps per BW credit */
2885 #define I40E_MAX_BW_INACTIVE_ACCUM 4  /* device can accumulate 4 credits max */
2886 /**
2887  * i40e_ndo_set_vf_bw
2888  * @netdev: network interface device structure
2889  * @vf_id: VF identifier
2890  * @tx_rate: Tx rate
2891  *
2892  * configure VF Tx rate
2893  **/
2894 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2895 		       int max_tx_rate)
2896 {
2897 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2898 	struct i40e_pf *pf = np->vsi->back;
2899 	struct i40e_vsi *vsi;
2900 	struct i40e_vf *vf;
2901 	int speed = 0;
2902 	int ret = 0;
2903 
2904 	/* validate the request */
2905 	if (vf_id >= pf->num_alloc_vfs) {
2906 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2907 		ret = -EINVAL;
2908 		goto error;
2909 	}
2910 
2911 	if (min_tx_rate) {
2912 		dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
2913 			min_tx_rate, vf_id);
2914 		return -EINVAL;
2915 	}
2916 
2917 	vf = &(pf->vf[vf_id]);
2918 	vsi = pf->vsi[vf->lan_vsi_idx];
2919 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2920 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
2921 			vf_id);
2922 		ret = -EAGAIN;
2923 		goto error;
2924 	}
2925 
2926 	switch (pf->hw.phy.link_info.link_speed) {
2927 	case I40E_LINK_SPEED_40GB:
2928 		speed = 40000;
2929 		break;
2930 	case I40E_LINK_SPEED_25GB:
2931 		speed = 25000;
2932 		break;
2933 	case I40E_LINK_SPEED_20GB:
2934 		speed = 20000;
2935 		break;
2936 	case I40E_LINK_SPEED_10GB:
2937 		speed = 10000;
2938 		break;
2939 	case I40E_LINK_SPEED_1GB:
2940 		speed = 1000;
2941 		break;
2942 	default:
2943 		break;
2944 	}
2945 
2946 	if (max_tx_rate > speed) {
2947 		dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.\n",
2948 			max_tx_rate, vf->vf_id);
2949 		ret = -EINVAL;
2950 		goto error;
2951 	}
2952 
2953 	if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2954 		dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2955 		max_tx_rate = 50;
2956 	}
2957 
2958 	/* Tx rate credits are in values of 50Mbps, 0 is disabled*/
2959 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2960 					  max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2961 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
2962 	if (ret) {
2963 		dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
2964 			ret);
2965 		ret = -EIO;
2966 		goto error;
2967 	}
2968 	vf->tx_rate = max_tx_rate;
2969 error:
2970 	return ret;
2971 }
2972 
2973 /**
2974  * i40e_ndo_get_vf_config
2975  * @netdev: network interface device structure
2976  * @vf_id: VF identifier
2977  * @ivi: VF configuration structure
2978  *
2979  * return VF configuration
2980  **/
2981 int i40e_ndo_get_vf_config(struct net_device *netdev,
2982 			   int vf_id, struct ifla_vf_info *ivi)
2983 {
2984 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2985 	struct i40e_vsi *vsi = np->vsi;
2986 	struct i40e_pf *pf = vsi->back;
2987 	struct i40e_vf *vf;
2988 	int ret = 0;
2989 
2990 	/* validate the request */
2991 	if (vf_id >= pf->num_alloc_vfs) {
2992 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2993 		ret = -EINVAL;
2994 		goto error_param;
2995 	}
2996 
2997 	vf = &(pf->vf[vf_id]);
2998 	/* first vsi is always the LAN vsi */
2999 	vsi = pf->vsi[vf->lan_vsi_idx];
3000 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
3001 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3002 			vf_id);
3003 		ret = -EAGAIN;
3004 		goto error_param;
3005 	}
3006 
3007 	ivi->vf = vf_id;
3008 
3009 	ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
3010 
3011 	ivi->max_tx_rate = vf->tx_rate;
3012 	ivi->min_tx_rate = 0;
3013 	ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
3014 	ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
3015 		   I40E_VLAN_PRIORITY_SHIFT;
3016 	if (vf->link_forced == false)
3017 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
3018 	else if (vf->link_up == true)
3019 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
3020 	else
3021 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
3022 	ivi->spoofchk = vf->spoofchk;
3023 	ivi->trusted = vf->trusted;
3024 	ret = 0;
3025 
3026 error_param:
3027 	return ret;
3028 }
3029 
3030 /**
3031  * i40e_ndo_set_vf_link_state
3032  * @netdev: network interface device structure
3033  * @vf_id: VF identifier
3034  * @link: required link state
3035  *
3036  * Set the link state of a specified VF, regardless of physical link state
3037  **/
3038 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
3039 {
3040 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3041 	struct i40e_pf *pf = np->vsi->back;
3042 	struct i40e_virtchnl_pf_event pfe;
3043 	struct i40e_hw *hw = &pf->hw;
3044 	struct i40e_vf *vf;
3045 	int abs_vf_id;
3046 	int ret = 0;
3047 
3048 	/* validate the request */
3049 	if (vf_id >= pf->num_alloc_vfs) {
3050 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3051 		ret = -EINVAL;
3052 		goto error_out;
3053 	}
3054 
3055 	vf = &pf->vf[vf_id];
3056 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
3057 
3058 	pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
3059 	pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
3060 
3061 	switch (link) {
3062 	case IFLA_VF_LINK_STATE_AUTO:
3063 		vf->link_forced = false;
3064 		pfe.event_data.link_event.link_status =
3065 			pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
3066 		pfe.event_data.link_event.link_speed =
3067 			pf->hw.phy.link_info.link_speed;
3068 		break;
3069 	case IFLA_VF_LINK_STATE_ENABLE:
3070 		vf->link_forced = true;
3071 		vf->link_up = true;
3072 		pfe.event_data.link_event.link_status = true;
3073 		pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
3074 		break;
3075 	case IFLA_VF_LINK_STATE_DISABLE:
3076 		vf->link_forced = true;
3077 		vf->link_up = false;
3078 		pfe.event_data.link_event.link_status = false;
3079 		pfe.event_data.link_event.link_speed = 0;
3080 		break;
3081 	default:
3082 		ret = -EINVAL;
3083 		goto error_out;
3084 	}
3085 	/* Notify the VF of its new link state */
3086 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
3087 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
3088 
3089 error_out:
3090 	return ret;
3091 }
3092 
3093 /**
3094  * i40e_ndo_set_vf_spoofchk
3095  * @netdev: network interface device structure
3096  * @vf_id: VF identifier
3097  * @enable: flag to enable or disable feature
3098  *
3099  * Enable or disable VF spoof checking
3100  **/
3101 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
3102 {
3103 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3104 	struct i40e_vsi *vsi = np->vsi;
3105 	struct i40e_pf *pf = vsi->back;
3106 	struct i40e_vsi_context ctxt;
3107 	struct i40e_hw *hw = &pf->hw;
3108 	struct i40e_vf *vf;
3109 	int ret = 0;
3110 
3111 	/* validate the request */
3112 	if (vf_id >= pf->num_alloc_vfs) {
3113 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3114 		ret = -EINVAL;
3115 		goto out;
3116 	}
3117 
3118 	vf = &(pf->vf[vf_id]);
3119 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
3120 		dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
3121 			vf_id);
3122 		ret = -EAGAIN;
3123 		goto out;
3124 	}
3125 
3126 	if (enable == vf->spoofchk)
3127 		goto out;
3128 
3129 	vf->spoofchk = enable;
3130 	memset(&ctxt, 0, sizeof(ctxt));
3131 	ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
3132 	ctxt.pf_num = pf->hw.pf_id;
3133 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
3134 	if (enable)
3135 		ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
3136 					I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
3137 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
3138 	if (ret) {
3139 		dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
3140 			ret);
3141 		ret = -EIO;
3142 	}
3143 out:
3144 	return ret;
3145 }
3146 
3147 /**
3148  * i40e_ndo_set_vf_trust
3149  * @netdev: network interface device structure of the pf
3150  * @vf_id: VF identifier
3151  * @setting: trust setting
3152  *
3153  * Enable or disable VF trust setting
3154  **/
3155 int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
3156 {
3157 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3158 	struct i40e_pf *pf = np->vsi->back;
3159 	struct i40e_vf *vf;
3160 	int ret = 0;
3161 
3162 	/* validate the request */
3163 	if (vf_id >= pf->num_alloc_vfs) {
3164 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
3165 		return -EINVAL;
3166 	}
3167 
3168 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3169 		dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
3170 		return -EINVAL;
3171 	}
3172 
3173 	vf = &pf->vf[vf_id];
3174 
3175 	if (!vf)
3176 		return -EINVAL;
3177 	if (setting == vf->trusted)
3178 		goto out;
3179 
3180 	vf->trusted = setting;
3181 	i40e_vc_notify_vf_reset(vf);
3182 	i40e_reset_vf(vf, false);
3183 	dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
3184 		 vf_id, setting ? "" : "un");
3185 out:
3186 	return ret;
3187 }
3188