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