1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2015 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 + 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_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 + 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 + 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_config_vsi_tx_queue
356  * @vf: pointer to the VF info
357  * @vsi_id: id of VSI as provided by the FW
358  * @vsi_queue_id: vsi relative queue index
359  * @info: config. info
360  *
361  * configure tx queue
362  **/
363 static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
364 				    u16 vsi_queue_id,
365 				    struct i40e_virtchnl_txq_info *info)
366 {
367 	struct i40e_pf *pf = vf->pf;
368 	struct i40e_hw *hw = &pf->hw;
369 	struct i40e_hmc_obj_txq tx_ctx;
370 	struct i40e_vsi *vsi;
371 	u16 pf_queue_id;
372 	u32 qtx_ctl;
373 	int ret = 0;
374 
375 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
376 	vsi = i40e_find_vsi_from_id(pf, vsi_id);
377 
378 	/* clear the context structure first */
379 	memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
380 
381 	/* only set the required fields */
382 	tx_ctx.base = info->dma_ring_addr / 128;
383 	tx_ctx.qlen = info->ring_len;
384 	tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
385 	tx_ctx.rdylist_act = 0;
386 	tx_ctx.head_wb_ena = info->headwb_enabled;
387 	tx_ctx.head_wb_addr = info->dma_headwb_addr;
388 
389 	/* clear the context in the HMC */
390 	ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
391 	if (ret) {
392 		dev_err(&pf->pdev->dev,
393 			"Failed to clear VF LAN Tx queue context %d, error: %d\n",
394 			pf_queue_id, ret);
395 		ret = -ENOENT;
396 		goto error_context;
397 	}
398 
399 	/* set the context in the HMC */
400 	ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
401 	if (ret) {
402 		dev_err(&pf->pdev->dev,
403 			"Failed to set VF LAN Tx queue context %d error: %d\n",
404 			pf_queue_id, ret);
405 		ret = -ENOENT;
406 		goto error_context;
407 	}
408 
409 	/* associate this queue with the PCI VF function */
410 	qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
411 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
412 		    & I40E_QTX_CTL_PF_INDX_MASK);
413 	qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
414 		     << I40E_QTX_CTL_VFVM_INDX_SHIFT)
415 		    & I40E_QTX_CTL_VFVM_INDX_MASK);
416 	wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
417 	i40e_flush(hw);
418 
419 error_context:
420 	return ret;
421 }
422 
423 /**
424  * i40e_config_vsi_rx_queue
425  * @vf: pointer to the VF info
426  * @vsi_id: id of VSI  as provided by the FW
427  * @vsi_queue_id: vsi relative queue index
428  * @info: config. info
429  *
430  * configure rx queue
431  **/
432 static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
433 				    u16 vsi_queue_id,
434 				    struct i40e_virtchnl_rxq_info *info)
435 {
436 	struct i40e_pf *pf = vf->pf;
437 	struct i40e_hw *hw = &pf->hw;
438 	struct i40e_hmc_obj_rxq rx_ctx;
439 	u16 pf_queue_id;
440 	int ret = 0;
441 
442 	pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
443 
444 	/* clear the context structure first */
445 	memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
446 
447 	/* only set the required fields */
448 	rx_ctx.base = info->dma_ring_addr / 128;
449 	rx_ctx.qlen = info->ring_len;
450 
451 	if (info->splithdr_enabled) {
452 		rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2      |
453 				  I40E_RX_SPLIT_IP      |
454 				  I40E_RX_SPLIT_TCP_UDP |
455 				  I40E_RX_SPLIT_SCTP;
456 		/* header length validation */
457 		if (info->hdr_size > ((2 * 1024) - 64)) {
458 			ret = -EINVAL;
459 			goto error_param;
460 		}
461 		rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
462 
463 		/* set splitalways mode 10b */
464 		rx_ctx.dtype = 0x2;
465 	}
466 
467 	/* databuffer length validation */
468 	if (info->databuffer_size > ((16 * 1024) - 128)) {
469 		ret = -EINVAL;
470 		goto error_param;
471 	}
472 	rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
473 
474 	/* max pkt. length validation */
475 	if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
476 		ret = -EINVAL;
477 		goto error_param;
478 	}
479 	rx_ctx.rxmax = info->max_pkt_size;
480 
481 	/* enable 32bytes desc always */
482 	rx_ctx.dsize = 1;
483 
484 	/* default values */
485 	rx_ctx.lrxqthresh = 2;
486 	rx_ctx.crcstrip = 1;
487 	rx_ctx.prefena = 1;
488 	rx_ctx.l2tsel = 1;
489 
490 	/* clear the context in the HMC */
491 	ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
492 	if (ret) {
493 		dev_err(&pf->pdev->dev,
494 			"Failed to clear VF LAN Rx queue context %d, error: %d\n",
495 			pf_queue_id, ret);
496 		ret = -ENOENT;
497 		goto error_param;
498 	}
499 
500 	/* set the context in the HMC */
501 	ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
502 	if (ret) {
503 		dev_err(&pf->pdev->dev,
504 			"Failed to set VF LAN Rx queue context %d error: %d\n",
505 			pf_queue_id, ret);
506 		ret = -ENOENT;
507 		goto error_param;
508 	}
509 
510 error_param:
511 	return ret;
512 }
513 
514 /**
515  * i40e_alloc_vsi_res
516  * @vf: pointer to the VF info
517  * @type: type of VSI to allocate
518  *
519  * alloc VF vsi context & resources
520  **/
521 static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
522 {
523 	struct i40e_mac_filter *f = NULL;
524 	struct i40e_pf *pf = vf->pf;
525 	struct i40e_vsi *vsi;
526 	int ret = 0;
527 
528 	vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
529 
530 	if (!vsi) {
531 		dev_err(&pf->pdev->dev,
532 			"add vsi failed for VF %d, aq_err %d\n",
533 			vf->vf_id, pf->hw.aq.asq_last_status);
534 		ret = -ENOENT;
535 		goto error_alloc_vsi_res;
536 	}
537 	if (type == I40E_VSI_SRIOV) {
538 		u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
539 		vf->lan_vsi_idx = vsi->idx;
540 		vf->lan_vsi_id = vsi->id;
541 		/* If the port VLAN has been configured and then the
542 		 * VF driver was removed then the VSI port VLAN
543 		 * configuration was destroyed.  Check if there is
544 		 * a port VLAN and restore the VSI configuration if
545 		 * needed.
546 		 */
547 		if (vf->port_vlan_id)
548 			i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
549 		f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
550 				    vf->port_vlan_id ? vf->port_vlan_id : -1,
551 				    true, false);
552 		if (!f)
553 			dev_info(&pf->pdev->dev,
554 				 "Could not allocate VF MAC addr\n");
555 		f = i40e_add_filter(vsi, brdcast,
556 				    vf->port_vlan_id ? vf->port_vlan_id : -1,
557 				    true, false);
558 		if (!f)
559 			dev_info(&pf->pdev->dev,
560 				 "Could not allocate VF broadcast filter\n");
561 	}
562 
563 	/* program mac filter */
564 	ret = i40e_sync_vsi_filters(vsi);
565 	if (ret)
566 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
567 
568 	/* Set VF bandwidth if specified */
569 	if (vf->tx_rate) {
570 		ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
571 						  vf->tx_rate / 50, 0, NULL);
572 		if (ret)
573 			dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
574 				vf->vf_id, ret);
575 	}
576 
577 error_alloc_vsi_res:
578 	return ret;
579 }
580 
581 /**
582  * i40e_enable_vf_mappings
583  * @vf: pointer to the VF info
584  *
585  * enable VF mappings
586  **/
587 static void i40e_enable_vf_mappings(struct i40e_vf *vf)
588 {
589 	struct i40e_pf *pf = vf->pf;
590 	struct i40e_hw *hw = &pf->hw;
591 	u32 reg, total_queue_pairs = 0;
592 	int j;
593 
594 	/* Tell the hardware we're using noncontiguous mapping. HW requires
595 	 * that VF queues be mapped using this method, even when they are
596 	 * contiguous in real life
597 	 */
598 	wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
599 	     I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
600 
601 	/* enable VF vplan_qtable mappings */
602 	reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
603 	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
604 
605 	/* map PF queues to VF queues */
606 	for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
607 		u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
608 		reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
609 		wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
610 		total_queue_pairs++;
611 	}
612 
613 	/* map PF queues to VSI */
614 	for (j = 0; j < 7; j++) {
615 		if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
616 			reg = 0x07FF07FF;	/* unused */
617 		} else {
618 			u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
619 							  j * 2);
620 			reg = qid;
621 			qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
622 						      (j * 2) + 1);
623 			reg |= qid << 16;
624 		}
625 		wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
626 	}
627 
628 	i40e_flush(hw);
629 }
630 
631 /**
632  * i40e_disable_vf_mappings
633  * @vf: pointer to the VF info
634  *
635  * disable VF mappings
636  **/
637 static void i40e_disable_vf_mappings(struct i40e_vf *vf)
638 {
639 	struct i40e_pf *pf = vf->pf;
640 	struct i40e_hw *hw = &pf->hw;
641 	int i;
642 
643 	/* disable qp mappings */
644 	wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
645 	for (i = 0; i < I40E_MAX_VSI_QP; i++)
646 		wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
647 		     I40E_QUEUE_END_OF_LIST);
648 	i40e_flush(hw);
649 }
650 
651 /**
652  * i40e_free_vf_res
653  * @vf: pointer to the VF info
654  *
655  * free VF resources
656  **/
657 static void i40e_free_vf_res(struct i40e_vf *vf)
658 {
659 	struct i40e_pf *pf = vf->pf;
660 	struct i40e_hw *hw = &pf->hw;
661 	u32 reg_idx, reg;
662 	int i, msix_vf;
663 
664 	/* free vsi & disconnect it from the parent uplink */
665 	if (vf->lan_vsi_idx) {
666 		i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
667 		vf->lan_vsi_idx = 0;
668 		vf->lan_vsi_id = 0;
669 	}
670 	msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
671 
672 	/* disable interrupts so the VF starts in a known state */
673 	for (i = 0; i < msix_vf; i++) {
674 		/* format is same for both registers */
675 		if (0 == i)
676 			reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
677 		else
678 			reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
679 						      (vf->vf_id))
680 						     + (i - 1));
681 		wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
682 		i40e_flush(hw);
683 	}
684 
685 	/* clear the irq settings */
686 	for (i = 0; i < msix_vf; i++) {
687 		/* format is same for both registers */
688 		if (0 == i)
689 			reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
690 		else
691 			reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
692 						      (vf->vf_id))
693 						     + (i - 1));
694 		reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
695 		       I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
696 		wr32(hw, reg_idx, reg);
697 		i40e_flush(hw);
698 	}
699 	/* reset some of the state varibles keeping
700 	 * track of the resources
701 	 */
702 	vf->num_queue_pairs = 0;
703 	vf->vf_states = 0;
704 }
705 
706 /**
707  * i40e_alloc_vf_res
708  * @vf: pointer to the VF info
709  *
710  * allocate VF resources
711  **/
712 static int i40e_alloc_vf_res(struct i40e_vf *vf)
713 {
714 	struct i40e_pf *pf = vf->pf;
715 	int total_queue_pairs = 0;
716 	int ret;
717 
718 	/* allocate hw vsi context & associated resources */
719 	ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
720 	if (ret)
721 		goto error_alloc;
722 	total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
723 	set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
724 
725 	/* store the total qps number for the runtime
726 	 * VF req validation
727 	 */
728 	vf->num_queue_pairs = total_queue_pairs;
729 
730 	/* VF is now completely initialized */
731 	set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
732 
733 error_alloc:
734 	if (ret)
735 		i40e_free_vf_res(vf);
736 
737 	return ret;
738 }
739 
740 #define VF_DEVICE_STATUS 0xAA
741 #define VF_TRANS_PENDING_MASK 0x20
742 /**
743  * i40e_quiesce_vf_pci
744  * @vf: pointer to the VF structure
745  *
746  * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
747  * if the transactions never clear.
748  **/
749 static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
750 {
751 	struct i40e_pf *pf = vf->pf;
752 	struct i40e_hw *hw = &pf->hw;
753 	int vf_abs_id, i;
754 	u32 reg;
755 
756 	vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
757 
758 	wr32(hw, I40E_PF_PCI_CIAA,
759 	     VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
760 	for (i = 0; i < 100; i++) {
761 		reg = rd32(hw, I40E_PF_PCI_CIAD);
762 		if ((reg & VF_TRANS_PENDING_MASK) == 0)
763 			return 0;
764 		udelay(1);
765 	}
766 	return -EIO;
767 }
768 
769 /**
770  * i40e_reset_vf
771  * @vf: pointer to the VF structure
772  * @flr: VFLR was issued or not
773  *
774  * reset the VF
775  **/
776 void i40e_reset_vf(struct i40e_vf *vf, bool flr)
777 {
778 	struct i40e_pf *pf = vf->pf;
779 	struct i40e_hw *hw = &pf->hw;
780 	bool rsd = false;
781 	int i;
782 	u32 reg;
783 
784 	if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
785 		return;
786 
787 	/* warn the VF */
788 	clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
789 
790 	/* In the case of a VFLR, the HW has already reset the VF and we
791 	 * just need to clean up, so don't hit the VFRTRIG register.
792 	 */
793 	if (!flr) {
794 		/* reset VF using VPGEN_VFRTRIG reg */
795 		reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
796 		reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
797 		wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
798 		i40e_flush(hw);
799 	}
800 
801 	if (i40e_quiesce_vf_pci(vf))
802 		dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
803 			vf->vf_id);
804 
805 	/* poll VPGEN_VFRSTAT reg to make sure
806 	 * that reset is complete
807 	 */
808 	for (i = 0; i < 10; i++) {
809 		/* VF reset requires driver to first reset the VF and then
810 		 * poll the status register to make sure that the reset
811 		 * completed successfully. Due to internal HW FIFO flushes,
812 		 * we must wait 10ms before the register will be valid.
813 		 */
814 		usleep_range(10000, 20000);
815 		reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
816 		if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
817 			rsd = true;
818 			break;
819 		}
820 	}
821 
822 	if (flr)
823 		usleep_range(10000, 20000);
824 
825 	if (!rsd)
826 		dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
827 			vf->vf_id);
828 	wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
829 	/* clear the reset bit in the VPGEN_VFRTRIG reg */
830 	reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
831 	reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
832 	wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
833 
834 	/* On initial reset, we won't have any queues */
835 	if (vf->lan_vsi_idx == 0)
836 		goto complete_reset;
837 
838 	i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
839 complete_reset:
840 	/* reallocate VF resources to reset the VSI state */
841 	i40e_free_vf_res(vf);
842 	i40e_alloc_vf_res(vf);
843 	i40e_enable_vf_mappings(vf);
844 	set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
845 	clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
846 
847 	/* tell the VF the reset is done */
848 	wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
849 	i40e_flush(hw);
850 	clear_bit(__I40E_VF_DISABLE, &pf->state);
851 }
852 
853 /**
854  * i40e_free_vfs
855  * @pf: pointer to the PF structure
856  *
857  * free VF resources
858  **/
859 void i40e_free_vfs(struct i40e_pf *pf)
860 {
861 	struct i40e_hw *hw = &pf->hw;
862 	u32 reg_idx, bit_idx;
863 	int i, tmp, vf_id;
864 
865 	if (!pf->vf)
866 		return;
867 	while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
868 		usleep_range(1000, 2000);
869 
870 	for (i = 0; i < pf->num_alloc_vfs; i++)
871 		if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
872 			i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
873 					       false);
874 
875 	/* Disable IOV before freeing resources. This lets any VF drivers
876 	 * running in the host get themselves cleaned up before we yank
877 	 * the carpet out from underneath their feet.
878 	 */
879 	if (!pci_vfs_assigned(pf->pdev))
880 		pci_disable_sriov(pf->pdev);
881 	else
882 		dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
883 
884 	msleep(20); /* let any messages in transit get finished up */
885 
886 	/* free up VF resources */
887 	tmp = pf->num_alloc_vfs;
888 	pf->num_alloc_vfs = 0;
889 	for (i = 0; i < tmp; i++) {
890 		if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
891 			i40e_free_vf_res(&pf->vf[i]);
892 		/* disable qp mappings */
893 		i40e_disable_vf_mappings(&pf->vf[i]);
894 	}
895 
896 	kfree(pf->vf);
897 	pf->vf = NULL;
898 
899 	/* This check is for when the driver is unloaded while VFs are
900 	 * assigned. Setting the number of VFs to 0 through sysfs is caught
901 	 * before this function ever gets called.
902 	 */
903 	if (!pci_vfs_assigned(pf->pdev)) {
904 		/* Acknowledge VFLR for all VFS. Without this, VFs will fail to
905 		 * work correctly when SR-IOV gets re-enabled.
906 		 */
907 		for (vf_id = 0; vf_id < tmp; vf_id++) {
908 			reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
909 			bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
910 			wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
911 		}
912 	}
913 	clear_bit(__I40E_VF_DISABLE, &pf->state);
914 }
915 
916 #ifdef CONFIG_PCI_IOV
917 /**
918  * i40e_alloc_vfs
919  * @pf: pointer to the PF structure
920  * @num_alloc_vfs: number of VFs to allocate
921  *
922  * allocate VF resources
923  **/
924 int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
925 {
926 	struct i40e_vf *vfs;
927 	int i, ret = 0;
928 
929 	/* Disable interrupt 0 so we don't try to handle the VFLR. */
930 	i40e_irq_dynamic_disable_icr0(pf);
931 
932 	/* Check to see if we're just allocating resources for extant VFs */
933 	if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
934 		ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
935 		if (ret) {
936 			pf->num_alloc_vfs = 0;
937 			goto err_iov;
938 		}
939 	}
940 	/* allocate memory */
941 	vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
942 	if (!vfs) {
943 		ret = -ENOMEM;
944 		goto err_alloc;
945 	}
946 	pf->vf = vfs;
947 
948 	/* apply default profile */
949 	for (i = 0; i < num_alloc_vfs; i++) {
950 		vfs[i].pf = pf;
951 		vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
952 		vfs[i].vf_id = i;
953 
954 		/* assign default capabilities */
955 		set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
956 		vfs[i].spoofchk = true;
957 		/* VF resources get allocated during reset */
958 		i40e_reset_vf(&vfs[i], false);
959 
960 		/* enable VF vplan_qtable mappings */
961 		i40e_enable_vf_mappings(&vfs[i]);
962 	}
963 	pf->num_alloc_vfs = num_alloc_vfs;
964 
965 err_alloc:
966 	if (ret)
967 		i40e_free_vfs(pf);
968 err_iov:
969 	/* Re-enable interrupt 0. */
970 	i40e_irq_dynamic_enable_icr0(pf);
971 	return ret;
972 }
973 
974 #endif
975 /**
976  * i40e_pci_sriov_enable
977  * @pdev: pointer to a pci_dev structure
978  * @num_vfs: number of VFs to allocate
979  *
980  * Enable or change the number of VFs
981  **/
982 static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
983 {
984 #ifdef CONFIG_PCI_IOV
985 	struct i40e_pf *pf = pci_get_drvdata(pdev);
986 	int pre_existing_vfs = pci_num_vf(pdev);
987 	int err = 0;
988 
989 	if (pf->state & __I40E_TESTING) {
990 		dev_warn(&pdev->dev,
991 			 "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
992 		err = -EPERM;
993 		goto err_out;
994 	}
995 
996 	dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
997 	if (pre_existing_vfs && pre_existing_vfs != num_vfs)
998 		i40e_free_vfs(pf);
999 	else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
1000 		goto out;
1001 
1002 	if (num_vfs > pf->num_req_vfs) {
1003 		err = -EPERM;
1004 		goto err_out;
1005 	}
1006 
1007 	err = i40e_alloc_vfs(pf, num_vfs);
1008 	if (err) {
1009 		dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
1010 		goto err_out;
1011 	}
1012 
1013 out:
1014 	return num_vfs;
1015 
1016 err_out:
1017 	return err;
1018 #endif
1019 	return 0;
1020 }
1021 
1022 /**
1023  * i40e_pci_sriov_configure
1024  * @pdev: pointer to a pci_dev structure
1025  * @num_vfs: number of VFs to allocate
1026  *
1027  * Enable or change the number of VFs. Called when the user updates the number
1028  * of VFs in sysfs.
1029  **/
1030 int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
1031 {
1032 	struct i40e_pf *pf = pci_get_drvdata(pdev);
1033 
1034 	if (num_vfs) {
1035 		if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
1036 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
1037 			i40e_do_reset_safe(pf,
1038 					   BIT_ULL(__I40E_PF_RESET_REQUESTED));
1039 		}
1040 		return i40e_pci_sriov_enable(pdev, num_vfs);
1041 	}
1042 
1043 	if (!pci_vfs_assigned(pf->pdev)) {
1044 		i40e_free_vfs(pf);
1045 		pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
1046 		i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
1047 	} else {
1048 		dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
1049 		return -EINVAL;
1050 	}
1051 	return 0;
1052 }
1053 
1054 /***********************virtual channel routines******************/
1055 
1056 /**
1057  * i40e_vc_send_msg_to_vf
1058  * @vf: pointer to the VF info
1059  * @v_opcode: virtual channel opcode
1060  * @v_retval: virtual channel return value
1061  * @msg: pointer to the msg buffer
1062  * @msglen: msg length
1063  *
1064  * send msg to VF
1065  **/
1066 static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
1067 				  u32 v_retval, u8 *msg, u16 msglen)
1068 {
1069 	struct i40e_pf *pf;
1070 	struct i40e_hw *hw;
1071 	int abs_vf_id;
1072 	i40e_status aq_ret;
1073 
1074 	/* validate the request */
1075 	if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
1076 		return -EINVAL;
1077 
1078 	pf = vf->pf;
1079 	hw = &pf->hw;
1080 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
1081 
1082 	/* single place to detect unsuccessful return values */
1083 	if (v_retval) {
1084 		vf->num_invalid_msgs++;
1085 		dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
1086 			v_opcode, v_retval);
1087 		if (vf->num_invalid_msgs >
1088 		    I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
1089 			dev_err(&pf->pdev->dev,
1090 				"Number of invalid messages exceeded for VF %d\n",
1091 				vf->vf_id);
1092 			dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
1093 			set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
1094 		}
1095 	} else {
1096 		vf->num_valid_msgs++;
1097 	}
1098 
1099 	aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id,	v_opcode, v_retval,
1100 					msg, msglen, NULL);
1101 	if (aq_ret) {
1102 		dev_err(&pf->pdev->dev,
1103 			"Unable to send the message to VF %d aq_err %d\n",
1104 			vf->vf_id, pf->hw.aq.asq_last_status);
1105 		return -EIO;
1106 	}
1107 
1108 	return 0;
1109 }
1110 
1111 /**
1112  * i40e_vc_send_resp_to_vf
1113  * @vf: pointer to the VF info
1114  * @opcode: operation code
1115  * @retval: return value
1116  *
1117  * send resp msg to VF
1118  **/
1119 static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
1120 				   enum i40e_virtchnl_ops opcode,
1121 				   i40e_status retval)
1122 {
1123 	return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
1124 }
1125 
1126 /**
1127  * i40e_vc_get_version_msg
1128  * @vf: pointer to the VF info
1129  *
1130  * called from the VF to request the API version used by the PF
1131  **/
1132 static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
1133 {
1134 	struct i40e_virtchnl_version_info info = {
1135 		I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
1136 	};
1137 
1138 	vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
1139 	/* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
1140 	if (VF_IS_V10(vf))
1141 		info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
1142 	return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
1143 				      I40E_SUCCESS, (u8 *)&info,
1144 				      sizeof(struct
1145 					     i40e_virtchnl_version_info));
1146 }
1147 
1148 /**
1149  * i40e_vc_get_vf_resources_msg
1150  * @vf: pointer to the VF info
1151  * @msg: pointer to the msg buffer
1152  * @msglen: msg length
1153  *
1154  * called from the VF to request its resources
1155  **/
1156 static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
1157 {
1158 	struct i40e_virtchnl_vf_resource *vfres = NULL;
1159 	struct i40e_pf *pf = vf->pf;
1160 	i40e_status aq_ret = 0;
1161 	struct i40e_vsi *vsi;
1162 	int i = 0, len = 0;
1163 	int num_vsis = 1;
1164 	int ret;
1165 
1166 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
1167 		aq_ret = I40E_ERR_PARAM;
1168 		goto err;
1169 	}
1170 
1171 	len = (sizeof(struct i40e_virtchnl_vf_resource) +
1172 	       sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
1173 
1174 	vfres = kzalloc(len, GFP_KERNEL);
1175 	if (!vfres) {
1176 		aq_ret = I40E_ERR_NO_MEMORY;
1177 		len = 0;
1178 		goto err;
1179 	}
1180 	if (VF_IS_V11(vf))
1181 		vf->driver_caps = *(u32 *)msg;
1182 	else
1183 		vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
1184 				  I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
1185 				  I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1186 
1187 	vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
1188 	vsi = pf->vsi[vf->lan_vsi_idx];
1189 	if (!vsi->info.pvid)
1190 		vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
1191 	if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
1192 		if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ)
1193 			vfres->vf_offload_flags |=
1194 				I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ;
1195 	} else {
1196 		vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
1197 	}
1198 	vfres->num_vsis = num_vsis;
1199 	vfres->num_queue_pairs = vf->num_queue_pairs;
1200 	vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
1201 	if (vf->lan_vsi_idx) {
1202 		vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
1203 		vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
1204 		vfres->vsi_res[i].num_queue_pairs =
1205 		    pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
1206 		memcpy(vfres->vsi_res[i].default_mac_addr,
1207 		       vf->default_lan_addr.addr, ETH_ALEN);
1208 		i++;
1209 	}
1210 	set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
1211 
1212 err:
1213 	/* send the response back to the VF */
1214 	ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
1215 				     aq_ret, (u8 *)vfres, len);
1216 
1217 	kfree(vfres);
1218 	return ret;
1219 }
1220 
1221 /**
1222  * i40e_vc_reset_vf_msg
1223  * @vf: pointer to the VF info
1224  * @msg: pointer to the msg buffer
1225  * @msglen: msg length
1226  *
1227  * called from the VF to reset itself,
1228  * unlike other virtchnl messages, PF driver
1229  * doesn't send the response back to the VF
1230  **/
1231 static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
1232 {
1233 	if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
1234 		i40e_reset_vf(vf, false);
1235 }
1236 
1237 /**
1238  * i40e_vc_config_promiscuous_mode_msg
1239  * @vf: pointer to the VF info
1240  * @msg: pointer to the msg buffer
1241  * @msglen: msg length
1242  *
1243  * called from the VF to configure the promiscuous mode of
1244  * VF vsis
1245  **/
1246 static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
1247 					       u8 *msg, u16 msglen)
1248 {
1249 	struct i40e_virtchnl_promisc_info *info =
1250 	    (struct i40e_virtchnl_promisc_info *)msg;
1251 	struct i40e_pf *pf = vf->pf;
1252 	struct i40e_hw *hw = &pf->hw;
1253 	struct i40e_vsi *vsi;
1254 	bool allmulti = false;
1255 	i40e_status aq_ret;
1256 
1257 	vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
1258 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1259 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1260 	    !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
1261 	    (vsi->type != I40E_VSI_FCOE)) {
1262 		aq_ret = I40E_ERR_PARAM;
1263 		goto error_param;
1264 	}
1265 	if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
1266 		allmulti = true;
1267 	aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
1268 						       allmulti, NULL);
1269 
1270 error_param:
1271 	/* send the response to the VF */
1272 	return i40e_vc_send_resp_to_vf(vf,
1273 				       I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1274 				       aq_ret);
1275 }
1276 
1277 /**
1278  * i40e_vc_config_queues_msg
1279  * @vf: pointer to the VF info
1280  * @msg: pointer to the msg buffer
1281  * @msglen: msg length
1282  *
1283  * called from the VF to configure the rx/tx
1284  * queues
1285  **/
1286 static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1287 {
1288 	struct i40e_virtchnl_vsi_queue_config_info *qci =
1289 	    (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1290 	struct i40e_virtchnl_queue_pair_info *qpi;
1291 	struct i40e_pf *pf = vf->pf;
1292 	u16 vsi_id, vsi_queue_id;
1293 	i40e_status aq_ret = 0;
1294 	int i;
1295 
1296 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1297 		aq_ret = I40E_ERR_PARAM;
1298 		goto error_param;
1299 	}
1300 
1301 	vsi_id = qci->vsi_id;
1302 	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1303 		aq_ret = I40E_ERR_PARAM;
1304 		goto error_param;
1305 	}
1306 	for (i = 0; i < qci->num_queue_pairs; i++) {
1307 		qpi = &qci->qpair[i];
1308 		vsi_queue_id = qpi->txq.queue_id;
1309 		if ((qpi->txq.vsi_id != vsi_id) ||
1310 		    (qpi->rxq.vsi_id != vsi_id) ||
1311 		    (qpi->rxq.queue_id != vsi_queue_id) ||
1312 		    !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
1313 			aq_ret = I40E_ERR_PARAM;
1314 			goto error_param;
1315 		}
1316 
1317 		if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
1318 					     &qpi->rxq) ||
1319 		    i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
1320 					     &qpi->txq)) {
1321 			aq_ret = I40E_ERR_PARAM;
1322 			goto error_param;
1323 		}
1324 	}
1325 	/* set vsi num_queue_pairs in use to num configured by VF */
1326 	pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
1327 
1328 error_param:
1329 	/* send the response to the VF */
1330 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
1331 				       aq_ret);
1332 }
1333 
1334 /**
1335  * i40e_vc_config_irq_map_msg
1336  * @vf: pointer to the VF info
1337  * @msg: pointer to the msg buffer
1338  * @msglen: msg length
1339  *
1340  * called from the VF to configure the irq to
1341  * queue map
1342  **/
1343 static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1344 {
1345 	struct i40e_virtchnl_irq_map_info *irqmap_info =
1346 	    (struct i40e_virtchnl_irq_map_info *)msg;
1347 	struct i40e_virtchnl_vector_map *map;
1348 	u16 vsi_id, vsi_queue_id, vector_id;
1349 	i40e_status aq_ret = 0;
1350 	unsigned long tempmap;
1351 	int i;
1352 
1353 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1354 		aq_ret = I40E_ERR_PARAM;
1355 		goto error_param;
1356 	}
1357 
1358 	for (i = 0; i < irqmap_info->num_vectors; i++) {
1359 		map = &irqmap_info->vecmap[i];
1360 
1361 		vector_id = map->vector_id;
1362 		vsi_id = map->vsi_id;
1363 		/* validate msg params */
1364 		if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
1365 		    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1366 			aq_ret = I40E_ERR_PARAM;
1367 			goto error_param;
1368 		}
1369 
1370 		/* lookout for the invalid queue index */
1371 		tempmap = map->rxq_map;
1372 		for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1373 			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1374 						      vsi_queue_id)) {
1375 				aq_ret = I40E_ERR_PARAM;
1376 				goto error_param;
1377 			}
1378 		}
1379 
1380 		tempmap = map->txq_map;
1381 		for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
1382 			if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
1383 						      vsi_queue_id)) {
1384 				aq_ret = I40E_ERR_PARAM;
1385 				goto error_param;
1386 			}
1387 		}
1388 
1389 		i40e_config_irq_link_list(vf, vsi_id, map);
1390 	}
1391 error_param:
1392 	/* send the response to the VF */
1393 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
1394 				       aq_ret);
1395 }
1396 
1397 /**
1398  * i40e_vc_enable_queues_msg
1399  * @vf: pointer to the VF info
1400  * @msg: pointer to the msg buffer
1401  * @msglen: msg length
1402  *
1403  * called from the VF to enable all or specific queue(s)
1404  **/
1405 static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1406 {
1407 	struct i40e_virtchnl_queue_select *vqs =
1408 	    (struct i40e_virtchnl_queue_select *)msg;
1409 	struct i40e_pf *pf = vf->pf;
1410 	u16 vsi_id = vqs->vsi_id;
1411 	i40e_status aq_ret = 0;
1412 
1413 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1414 		aq_ret = I40E_ERR_PARAM;
1415 		goto error_param;
1416 	}
1417 
1418 	if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1419 		aq_ret = I40E_ERR_PARAM;
1420 		goto error_param;
1421 	}
1422 
1423 	if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1424 		aq_ret = I40E_ERR_PARAM;
1425 		goto error_param;
1426 	}
1427 
1428 	if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
1429 		aq_ret = I40E_ERR_TIMEOUT;
1430 error_param:
1431 	/* send the response to the VF */
1432 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1433 				       aq_ret);
1434 }
1435 
1436 /**
1437  * i40e_vc_disable_queues_msg
1438  * @vf: pointer to the VF info
1439  * @msg: pointer to the msg buffer
1440  * @msglen: msg length
1441  *
1442  * called from the VF to disable all or specific
1443  * queue(s)
1444  **/
1445 static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1446 {
1447 	struct i40e_virtchnl_queue_select *vqs =
1448 	    (struct i40e_virtchnl_queue_select *)msg;
1449 	struct i40e_pf *pf = vf->pf;
1450 	i40e_status aq_ret = 0;
1451 
1452 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1453 		aq_ret = I40E_ERR_PARAM;
1454 		goto error_param;
1455 	}
1456 
1457 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1458 		aq_ret = I40E_ERR_PARAM;
1459 		goto error_param;
1460 	}
1461 
1462 	if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
1463 		aq_ret = I40E_ERR_PARAM;
1464 		goto error_param;
1465 	}
1466 
1467 	if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
1468 		aq_ret = I40E_ERR_TIMEOUT;
1469 
1470 error_param:
1471 	/* send the response to the VF */
1472 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
1473 				       aq_ret);
1474 }
1475 
1476 /**
1477  * i40e_vc_get_stats_msg
1478  * @vf: pointer to the VF info
1479  * @msg: pointer to the msg buffer
1480  * @msglen: msg length
1481  *
1482  * called from the VF to get vsi stats
1483  **/
1484 static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1485 {
1486 	struct i40e_virtchnl_queue_select *vqs =
1487 	    (struct i40e_virtchnl_queue_select *)msg;
1488 	struct i40e_pf *pf = vf->pf;
1489 	struct i40e_eth_stats stats;
1490 	i40e_status aq_ret = 0;
1491 	struct i40e_vsi *vsi;
1492 
1493 	memset(&stats, 0, sizeof(struct i40e_eth_stats));
1494 
1495 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
1496 		aq_ret = I40E_ERR_PARAM;
1497 		goto error_param;
1498 	}
1499 
1500 	if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
1501 		aq_ret = I40E_ERR_PARAM;
1502 		goto error_param;
1503 	}
1504 
1505 	vsi = pf->vsi[vf->lan_vsi_idx];
1506 	if (!vsi) {
1507 		aq_ret = I40E_ERR_PARAM;
1508 		goto error_param;
1509 	}
1510 	i40e_update_eth_stats(vsi);
1511 	stats = vsi->eth_stats;
1512 
1513 error_param:
1514 	/* send the response back to the VF */
1515 	return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
1516 				      (u8 *)&stats, sizeof(stats));
1517 }
1518 
1519 /**
1520  * i40e_check_vf_permission
1521  * @vf: pointer to the VF info
1522  * @macaddr: pointer to the MAC Address being checked
1523  *
1524  * Check if the VF has permission to add or delete unicast MAC address
1525  * filters and return error code -EPERM if not.  Then check if the
1526  * address filter requested is broadcast or zero and if so return
1527  * an invalid MAC address error code.
1528  **/
1529 static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
1530 {
1531 	struct i40e_pf *pf = vf->pf;
1532 	int ret = 0;
1533 
1534 	if (is_broadcast_ether_addr(macaddr) ||
1535 		   is_zero_ether_addr(macaddr)) {
1536 		dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
1537 		ret = I40E_ERR_INVALID_MAC_ADDR;
1538 	} else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
1539 		   !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
1540 		/* If the host VMM administrator has set the VF MAC address
1541 		 * administratively via the ndo_set_vf_mac command then deny
1542 		 * permission to the VF to add or delete unicast MAC addresses.
1543 		 * The VF may request to set the MAC address filter already
1544 		 * assigned to it so do not return an error in that case.
1545 		 */
1546 		dev_err(&pf->pdev->dev,
1547 			"VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
1548 		ret = -EPERM;
1549 	}
1550 	return ret;
1551 }
1552 
1553 /**
1554  * i40e_vc_add_mac_addr_msg
1555  * @vf: pointer to the VF info
1556  * @msg: pointer to the msg buffer
1557  * @msglen: msg length
1558  *
1559  * add guest mac address filter
1560  **/
1561 static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1562 {
1563 	struct i40e_virtchnl_ether_addr_list *al =
1564 	    (struct i40e_virtchnl_ether_addr_list *)msg;
1565 	struct i40e_pf *pf = vf->pf;
1566 	struct i40e_vsi *vsi = NULL;
1567 	u16 vsi_id = al->vsi_id;
1568 	i40e_status ret = 0;
1569 	int i;
1570 
1571 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1572 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1573 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1574 		ret = I40E_ERR_PARAM;
1575 		goto error_param;
1576 	}
1577 
1578 	for (i = 0; i < al->num_elements; i++) {
1579 		ret = i40e_check_vf_permission(vf, al->list[i].addr);
1580 		if (ret)
1581 			goto error_param;
1582 	}
1583 	vsi = pf->vsi[vf->lan_vsi_idx];
1584 
1585 	/* add new addresses to the list */
1586 	for (i = 0; i < al->num_elements; i++) {
1587 		struct i40e_mac_filter *f;
1588 
1589 		f = i40e_find_mac(vsi, al->list[i].addr, true, false);
1590 		if (!f) {
1591 			if (i40e_is_vsi_in_vlan(vsi))
1592 				f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
1593 							 true, false);
1594 			else
1595 				f = i40e_add_filter(vsi, al->list[i].addr, -1,
1596 						    true, false);
1597 		}
1598 
1599 		if (!f) {
1600 			dev_err(&pf->pdev->dev,
1601 				"Unable to add VF MAC filter\n");
1602 			ret = I40E_ERR_PARAM;
1603 			goto error_param;
1604 		}
1605 	}
1606 
1607 	/* program the updated filter list */
1608 	if (i40e_sync_vsi_filters(vsi))
1609 		dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1610 
1611 error_param:
1612 	/* send the response to the VF */
1613 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
1614 				       ret);
1615 }
1616 
1617 /**
1618  * i40e_vc_del_mac_addr_msg
1619  * @vf: pointer to the VF info
1620  * @msg: pointer to the msg buffer
1621  * @msglen: msg length
1622  *
1623  * remove guest mac address filter
1624  **/
1625 static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1626 {
1627 	struct i40e_virtchnl_ether_addr_list *al =
1628 	    (struct i40e_virtchnl_ether_addr_list *)msg;
1629 	struct i40e_pf *pf = vf->pf;
1630 	struct i40e_vsi *vsi = NULL;
1631 	u16 vsi_id = al->vsi_id;
1632 	i40e_status ret = 0;
1633 	int i;
1634 
1635 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1636 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1637 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1638 		ret = I40E_ERR_PARAM;
1639 		goto error_param;
1640 	}
1641 
1642 	for (i = 0; i < al->num_elements; i++) {
1643 		if (is_broadcast_ether_addr(al->list[i].addr) ||
1644 		    is_zero_ether_addr(al->list[i].addr)) {
1645 			dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
1646 				al->list[i].addr);
1647 			ret = I40E_ERR_INVALID_MAC_ADDR;
1648 			goto error_param;
1649 		}
1650 	}
1651 	vsi = pf->vsi[vf->lan_vsi_idx];
1652 
1653 	/* delete addresses from the list */
1654 	for (i = 0; i < al->num_elements; i++)
1655 		i40e_del_filter(vsi, al->list[i].addr,
1656 				I40E_VLAN_ANY, true, false);
1657 
1658 	/* program the updated filter list */
1659 	if (i40e_sync_vsi_filters(vsi))
1660 		dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
1661 
1662 error_param:
1663 	/* send the response to the VF */
1664 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
1665 				       ret);
1666 }
1667 
1668 /**
1669  * i40e_vc_add_vlan_msg
1670  * @vf: pointer to the VF info
1671  * @msg: pointer to the msg buffer
1672  * @msglen: msg length
1673  *
1674  * program guest vlan id
1675  **/
1676 static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1677 {
1678 	struct i40e_virtchnl_vlan_filter_list *vfl =
1679 	    (struct i40e_virtchnl_vlan_filter_list *)msg;
1680 	struct i40e_pf *pf = vf->pf;
1681 	struct i40e_vsi *vsi = NULL;
1682 	u16 vsi_id = vfl->vsi_id;
1683 	i40e_status aq_ret = 0;
1684 	int i;
1685 
1686 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1687 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1688 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1689 		aq_ret = I40E_ERR_PARAM;
1690 		goto error_param;
1691 	}
1692 
1693 	for (i = 0; i < vfl->num_elements; i++) {
1694 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1695 			aq_ret = I40E_ERR_PARAM;
1696 			dev_err(&pf->pdev->dev,
1697 				"invalid VF VLAN id %d\n", vfl->vlan_id[i]);
1698 			goto error_param;
1699 		}
1700 	}
1701 	vsi = pf->vsi[vf->lan_vsi_idx];
1702 	if (vsi->info.pvid) {
1703 		aq_ret = I40E_ERR_PARAM;
1704 		goto error_param;
1705 	}
1706 
1707 	i40e_vlan_stripping_enable(vsi);
1708 	for (i = 0; i < vfl->num_elements; i++) {
1709 		/* add new VLAN filter */
1710 		int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
1711 		if (ret)
1712 			dev_err(&pf->pdev->dev,
1713 				"Unable to add VF vlan filter %d, error %d\n",
1714 				vfl->vlan_id[i], ret);
1715 	}
1716 
1717 error_param:
1718 	/* send the response to the VF */
1719 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
1720 }
1721 
1722 /**
1723  * i40e_vc_remove_vlan_msg
1724  * @vf: pointer to the VF info
1725  * @msg: pointer to the msg buffer
1726  * @msglen: msg length
1727  *
1728  * remove programmed guest vlan id
1729  **/
1730 static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
1731 {
1732 	struct i40e_virtchnl_vlan_filter_list *vfl =
1733 	    (struct i40e_virtchnl_vlan_filter_list *)msg;
1734 	struct i40e_pf *pf = vf->pf;
1735 	struct i40e_vsi *vsi = NULL;
1736 	u16 vsi_id = vfl->vsi_id;
1737 	i40e_status aq_ret = 0;
1738 	int i;
1739 
1740 	if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
1741 	    !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
1742 	    !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
1743 		aq_ret = I40E_ERR_PARAM;
1744 		goto error_param;
1745 	}
1746 
1747 	for (i = 0; i < vfl->num_elements; i++) {
1748 		if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
1749 			aq_ret = I40E_ERR_PARAM;
1750 			goto error_param;
1751 		}
1752 	}
1753 
1754 	vsi = pf->vsi[vf->lan_vsi_idx];
1755 	if (vsi->info.pvid) {
1756 		aq_ret = I40E_ERR_PARAM;
1757 		goto error_param;
1758 	}
1759 
1760 	for (i = 0; i < vfl->num_elements; i++) {
1761 		int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
1762 		if (ret)
1763 			dev_err(&pf->pdev->dev,
1764 				"Unable to delete VF vlan filter %d, error %d\n",
1765 				vfl->vlan_id[i], ret);
1766 	}
1767 
1768 error_param:
1769 	/* send the response to the VF */
1770 	return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
1771 }
1772 
1773 /**
1774  * i40e_vc_validate_vf_msg
1775  * @vf: pointer to the VF info
1776  * @msg: pointer to the msg buffer
1777  * @msglen: msg length
1778  * @msghndl: msg handle
1779  *
1780  * validate msg
1781  **/
1782 static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
1783 				   u32 v_retval, u8 *msg, u16 msglen)
1784 {
1785 	bool err_msg_format = false;
1786 	int valid_len;
1787 
1788 	/* Check if VF is disabled. */
1789 	if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
1790 		return I40E_ERR_PARAM;
1791 
1792 	/* Validate message length. */
1793 	switch (v_opcode) {
1794 	case I40E_VIRTCHNL_OP_VERSION:
1795 		valid_len = sizeof(struct i40e_virtchnl_version_info);
1796 		break;
1797 	case I40E_VIRTCHNL_OP_RESET_VF:
1798 		valid_len = 0;
1799 		break;
1800 	case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1801 		if (VF_IS_V11(vf))
1802 			valid_len = sizeof(u32);
1803 		else
1804 			valid_len = 0;
1805 		break;
1806 	case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
1807 		valid_len = sizeof(struct i40e_virtchnl_txq_info);
1808 		break;
1809 	case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
1810 		valid_len = sizeof(struct i40e_virtchnl_rxq_info);
1811 		break;
1812 	case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1813 		valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
1814 		if (msglen >= valid_len) {
1815 			struct i40e_virtchnl_vsi_queue_config_info *vqc =
1816 			    (struct i40e_virtchnl_vsi_queue_config_info *)msg;
1817 			valid_len += (vqc->num_queue_pairs *
1818 				      sizeof(struct
1819 					     i40e_virtchnl_queue_pair_info));
1820 			if (vqc->num_queue_pairs == 0)
1821 				err_msg_format = true;
1822 		}
1823 		break;
1824 	case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1825 		valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
1826 		if (msglen >= valid_len) {
1827 			struct i40e_virtchnl_irq_map_info *vimi =
1828 			    (struct i40e_virtchnl_irq_map_info *)msg;
1829 			valid_len += (vimi->num_vectors *
1830 				      sizeof(struct i40e_virtchnl_vector_map));
1831 			if (vimi->num_vectors == 0)
1832 				err_msg_format = true;
1833 		}
1834 		break;
1835 	case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1836 	case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1837 		valid_len = sizeof(struct i40e_virtchnl_queue_select);
1838 		break;
1839 	case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1840 	case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1841 		valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
1842 		if (msglen >= valid_len) {
1843 			struct i40e_virtchnl_ether_addr_list *veal =
1844 			    (struct i40e_virtchnl_ether_addr_list *)msg;
1845 			valid_len += veal->num_elements *
1846 			    sizeof(struct i40e_virtchnl_ether_addr);
1847 			if (veal->num_elements == 0)
1848 				err_msg_format = true;
1849 		}
1850 		break;
1851 	case I40E_VIRTCHNL_OP_ADD_VLAN:
1852 	case I40E_VIRTCHNL_OP_DEL_VLAN:
1853 		valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
1854 		if (msglen >= valid_len) {
1855 			struct i40e_virtchnl_vlan_filter_list *vfl =
1856 			    (struct i40e_virtchnl_vlan_filter_list *)msg;
1857 			valid_len += vfl->num_elements * sizeof(u16);
1858 			if (vfl->num_elements == 0)
1859 				err_msg_format = true;
1860 		}
1861 		break;
1862 	case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1863 		valid_len = sizeof(struct i40e_virtchnl_promisc_info);
1864 		break;
1865 	case I40E_VIRTCHNL_OP_GET_STATS:
1866 		valid_len = sizeof(struct i40e_virtchnl_queue_select);
1867 		break;
1868 	/* These are always errors coming from the VF. */
1869 	case I40E_VIRTCHNL_OP_EVENT:
1870 	case I40E_VIRTCHNL_OP_UNKNOWN:
1871 	default:
1872 		return -EPERM;
1873 		break;
1874 	}
1875 	/* few more checks */
1876 	if ((valid_len != msglen) || (err_msg_format)) {
1877 		i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
1878 		return -EINVAL;
1879 	} else {
1880 		return 0;
1881 	}
1882 }
1883 
1884 /**
1885  * i40e_vc_process_vf_msg
1886  * @pf: pointer to the PF structure
1887  * @vf_id: source VF id
1888  * @msg: pointer to the msg buffer
1889  * @msglen: msg length
1890  * @msghndl: msg handle
1891  *
1892  * called from the common aeq/arq handler to
1893  * process request from VF
1894  **/
1895 int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
1896 			   u32 v_retval, u8 *msg, u16 msglen)
1897 {
1898 	struct i40e_hw *hw = &pf->hw;
1899 	unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
1900 	struct i40e_vf *vf;
1901 	int ret;
1902 
1903 	pf->vf_aq_requests++;
1904 	if (local_vf_id >= pf->num_alloc_vfs)
1905 		return -EINVAL;
1906 	vf = &(pf->vf[local_vf_id]);
1907 	/* perform basic checks on the msg */
1908 	ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
1909 
1910 	if (ret) {
1911 		dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
1912 			local_vf_id, v_opcode, msglen);
1913 		return ret;
1914 	}
1915 
1916 	switch (v_opcode) {
1917 	case I40E_VIRTCHNL_OP_VERSION:
1918 		ret = i40e_vc_get_version_msg(vf, msg);
1919 		break;
1920 	case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1921 		ret = i40e_vc_get_vf_resources_msg(vf, msg);
1922 		break;
1923 	case I40E_VIRTCHNL_OP_RESET_VF:
1924 		i40e_vc_reset_vf_msg(vf);
1925 		ret = 0;
1926 		break;
1927 	case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1928 		ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
1929 		break;
1930 	case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1931 		ret = i40e_vc_config_queues_msg(vf, msg, msglen);
1932 		break;
1933 	case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1934 		ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
1935 		break;
1936 	case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1937 		ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
1938 		i40e_vc_notify_vf_link_state(vf);
1939 		break;
1940 	case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1941 		ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
1942 		break;
1943 	case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1944 		ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
1945 		break;
1946 	case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1947 		ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
1948 		break;
1949 	case I40E_VIRTCHNL_OP_ADD_VLAN:
1950 		ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
1951 		break;
1952 	case I40E_VIRTCHNL_OP_DEL_VLAN:
1953 		ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
1954 		break;
1955 	case I40E_VIRTCHNL_OP_GET_STATS:
1956 		ret = i40e_vc_get_stats_msg(vf, msg, msglen);
1957 		break;
1958 	case I40E_VIRTCHNL_OP_UNKNOWN:
1959 	default:
1960 		dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
1961 			v_opcode, local_vf_id);
1962 		ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
1963 					      I40E_ERR_NOT_IMPLEMENTED);
1964 		break;
1965 	}
1966 
1967 	return ret;
1968 }
1969 
1970 /**
1971  * i40e_vc_process_vflr_event
1972  * @pf: pointer to the PF structure
1973  *
1974  * called from the vlfr irq handler to
1975  * free up VF resources and state variables
1976  **/
1977 int i40e_vc_process_vflr_event(struct i40e_pf *pf)
1978 {
1979 	u32 reg, reg_idx, bit_idx, vf_id;
1980 	struct i40e_hw *hw = &pf->hw;
1981 	struct i40e_vf *vf;
1982 
1983 	if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
1984 		return 0;
1985 
1986 	/* re-enable vflr interrupt cause */
1987 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
1988 	reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
1989 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
1990 	i40e_flush(hw);
1991 
1992 	clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
1993 	for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
1994 		reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
1995 		bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
1996 		/* read GLGEN_VFLRSTAT register to find out the flr VFs */
1997 		vf = &pf->vf[vf_id];
1998 		reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
1999 		if (reg & BIT(bit_idx)) {
2000 			/* clear the bit in GLGEN_VFLRSTAT */
2001 			wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
2002 
2003 			if (!test_bit(__I40E_DOWN, &pf->state))
2004 				i40e_reset_vf(vf, true);
2005 		}
2006 	}
2007 
2008 	return 0;
2009 }
2010 
2011 /**
2012  * i40e_ndo_set_vf_mac
2013  * @netdev: network interface device structure
2014  * @vf_id: VF identifier
2015  * @mac: mac address
2016  *
2017  * program VF mac address
2018  **/
2019 int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
2020 {
2021 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2022 	struct i40e_vsi *vsi = np->vsi;
2023 	struct i40e_pf *pf = vsi->back;
2024 	struct i40e_mac_filter *f;
2025 	struct i40e_vf *vf;
2026 	int ret = 0;
2027 
2028 	/* validate the request */
2029 	if (vf_id >= pf->num_alloc_vfs) {
2030 		dev_err(&pf->pdev->dev,
2031 			"Invalid VF Identifier %d\n", vf_id);
2032 		ret = -EINVAL;
2033 		goto error_param;
2034 	}
2035 
2036 	vf = &(pf->vf[vf_id]);
2037 	vsi = pf->vsi[vf->lan_vsi_idx];
2038 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2039 		dev_err(&pf->pdev->dev,
2040 			"Uninitialized VF %d\n", vf_id);
2041 		ret = -EINVAL;
2042 		goto error_param;
2043 	}
2044 
2045 	if (!is_valid_ether_addr(mac)) {
2046 		dev_err(&pf->pdev->dev,
2047 			"Invalid VF ethernet address\n");
2048 		ret = -EINVAL;
2049 		goto error_param;
2050 	}
2051 
2052 	/* delete the temporary mac address */
2053 	i40e_del_filter(vsi, vf->default_lan_addr.addr,
2054 			vf->port_vlan_id ? vf->port_vlan_id : -1,
2055 			true, false);
2056 
2057 	/* Delete all the filters for this VSI - we're going to kill it
2058 	 * anyway.
2059 	 */
2060 	list_for_each_entry(f, &vsi->mac_filter_list, list)
2061 		i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
2062 
2063 	dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
2064 	/* program mac filter */
2065 	if (i40e_sync_vsi_filters(vsi)) {
2066 		dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
2067 		ret = -EIO;
2068 		goto error_param;
2069 	}
2070 	ether_addr_copy(vf->default_lan_addr.addr, mac);
2071 	vf->pf_set_mac = true;
2072 	/* Force the VF driver stop so it has to reload with new MAC address */
2073 	i40e_vc_disable_vf(pf, vf);
2074 	dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
2075 
2076 error_param:
2077 	return ret;
2078 }
2079 
2080 /**
2081  * i40e_ndo_set_vf_port_vlan
2082  * @netdev: network interface device structure
2083  * @vf_id: VF identifier
2084  * @vlan_id: mac address
2085  * @qos: priority setting
2086  *
2087  * program VF vlan id and/or qos
2088  **/
2089 int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
2090 			      int vf_id, u16 vlan_id, u8 qos)
2091 {
2092 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2093 	struct i40e_pf *pf = np->vsi->back;
2094 	struct i40e_vsi *vsi;
2095 	struct i40e_vf *vf;
2096 	int ret = 0;
2097 
2098 	/* validate the request */
2099 	if (vf_id >= pf->num_alloc_vfs) {
2100 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2101 		ret = -EINVAL;
2102 		goto error_pvid;
2103 	}
2104 
2105 	if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
2106 		dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
2107 		ret = -EINVAL;
2108 		goto error_pvid;
2109 	}
2110 
2111 	vf = &(pf->vf[vf_id]);
2112 	vsi = pf->vsi[vf->lan_vsi_idx];
2113 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2114 		dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2115 		ret = -EINVAL;
2116 		goto error_pvid;
2117 	}
2118 
2119 	if (le16_to_cpu(vsi->info.pvid) ==
2120 	    (vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT)))
2121 		/* duplicate request, so just return success */
2122 		goto error_pvid;
2123 
2124 	if (le16_to_cpu(vsi->info.pvid) == 0 && i40e_is_vsi_in_vlan(vsi)) {
2125 		dev_err(&pf->pdev->dev,
2126 			"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",
2127 			vf_id);
2128 		/* Administrator Error - knock the VF offline until he does
2129 		 * the right thing by reconfiguring his network correctly
2130 		 * and then reloading the VF driver.
2131 		 */
2132 		i40e_vc_disable_vf(pf, vf);
2133 	}
2134 
2135 	/* Check for condition where there was already a port VLAN ID
2136 	 * filter set and now it is being deleted by setting it to zero.
2137 	 * Additionally check for the condition where there was a port
2138 	 * VLAN but now there is a new and different port VLAN being set.
2139 	 * Before deleting all the old VLAN filters we must add new ones
2140 	 * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
2141 	 * MAC addresses deleted.
2142 	 */
2143 	if ((!(vlan_id || qos) ||
2144 	    (vlan_id | qos) != le16_to_cpu(vsi->info.pvid)) &&
2145 	    vsi->info.pvid)
2146 		ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
2147 
2148 	if (vsi->info.pvid) {
2149 		/* kill old VLAN */
2150 		ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
2151 					       VLAN_VID_MASK));
2152 		if (ret) {
2153 			dev_info(&vsi->back->pdev->dev,
2154 				 "remove VLAN failed, ret=%d, aq_err=%d\n",
2155 				 ret, pf->hw.aq.asq_last_status);
2156 		}
2157 	}
2158 	if (vlan_id || qos)
2159 		ret = i40e_vsi_add_pvid(vsi,
2160 				vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT));
2161 	else
2162 		i40e_vsi_remove_pvid(vsi);
2163 
2164 	if (vlan_id) {
2165 		dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
2166 			 vlan_id, qos, vf_id);
2167 
2168 		/* add new VLAN filter */
2169 		ret = i40e_vsi_add_vlan(vsi, vlan_id);
2170 		if (ret) {
2171 			dev_info(&vsi->back->pdev->dev,
2172 				 "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
2173 				 vsi->back->hw.aq.asq_last_status);
2174 			goto error_pvid;
2175 		}
2176 		/* Kill non-vlan MAC filters - ignore error return since
2177 		 * there might not be any non-vlan MAC filters.
2178 		 */
2179 		i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
2180 	}
2181 
2182 	if (ret) {
2183 		dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
2184 		goto error_pvid;
2185 	}
2186 	/* The Port VLAN needs to be saved across resets the same as the
2187 	 * default LAN MAC address.
2188 	 */
2189 	vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
2190 	ret = 0;
2191 
2192 error_pvid:
2193 	return ret;
2194 }
2195 
2196 #define I40E_BW_CREDIT_DIVISOR 50     /* 50Mbps per BW credit */
2197 #define I40E_MAX_BW_INACTIVE_ACCUM 4  /* device can accumulate 4 credits max */
2198 /**
2199  * i40e_ndo_set_vf_bw
2200  * @netdev: network interface device structure
2201  * @vf_id: VF identifier
2202  * @tx_rate: Tx rate
2203  *
2204  * configure VF Tx rate
2205  **/
2206 int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
2207 		       int max_tx_rate)
2208 {
2209 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2210 	struct i40e_pf *pf = np->vsi->back;
2211 	struct i40e_vsi *vsi;
2212 	struct i40e_vf *vf;
2213 	int speed = 0;
2214 	int ret = 0;
2215 
2216 	/* validate the request */
2217 	if (vf_id >= pf->num_alloc_vfs) {
2218 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
2219 		ret = -EINVAL;
2220 		goto error;
2221 	}
2222 
2223 	if (min_tx_rate) {
2224 		dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
2225 			min_tx_rate, vf_id);
2226 		return -EINVAL;
2227 	}
2228 
2229 	vf = &(pf->vf[vf_id]);
2230 	vsi = pf->vsi[vf->lan_vsi_idx];
2231 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2232 		dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
2233 		ret = -EINVAL;
2234 		goto error;
2235 	}
2236 
2237 	switch (pf->hw.phy.link_info.link_speed) {
2238 	case I40E_LINK_SPEED_40GB:
2239 		speed = 40000;
2240 		break;
2241 	case I40E_LINK_SPEED_10GB:
2242 		speed = 10000;
2243 		break;
2244 	case I40E_LINK_SPEED_1GB:
2245 		speed = 1000;
2246 		break;
2247 	default:
2248 		break;
2249 	}
2250 
2251 	if (max_tx_rate > speed) {
2252 		dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
2253 			max_tx_rate, vf->vf_id);
2254 		ret = -EINVAL;
2255 		goto error;
2256 	}
2257 
2258 	if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
2259 		dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
2260 		max_tx_rate = 50;
2261 	}
2262 
2263 	/* Tx rate credits are in values of 50Mbps, 0 is disabled*/
2264 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
2265 					  max_tx_rate / I40E_BW_CREDIT_DIVISOR,
2266 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
2267 	if (ret) {
2268 		dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
2269 			ret);
2270 		ret = -EIO;
2271 		goto error;
2272 	}
2273 	vf->tx_rate = max_tx_rate;
2274 error:
2275 	return ret;
2276 }
2277 
2278 /**
2279  * i40e_ndo_get_vf_config
2280  * @netdev: network interface device structure
2281  * @vf_id: VF identifier
2282  * @ivi: VF configuration structure
2283  *
2284  * return VF configuration
2285  **/
2286 int i40e_ndo_get_vf_config(struct net_device *netdev,
2287 			   int vf_id, struct ifla_vf_info *ivi)
2288 {
2289 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2290 	struct i40e_vsi *vsi = np->vsi;
2291 	struct i40e_pf *pf = vsi->back;
2292 	struct i40e_vf *vf;
2293 	int ret = 0;
2294 
2295 	/* validate the request */
2296 	if (vf_id >= pf->num_alloc_vfs) {
2297 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2298 		ret = -EINVAL;
2299 		goto error_param;
2300 	}
2301 
2302 	vf = &(pf->vf[vf_id]);
2303 	/* first vsi is always the LAN vsi */
2304 	vsi = pf->vsi[vf->lan_vsi_idx];
2305 	if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
2306 		dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
2307 		ret = -EINVAL;
2308 		goto error_param;
2309 	}
2310 
2311 	ivi->vf = vf_id;
2312 
2313 	memcpy(&ivi->mac, vf->default_lan_addr.addr, ETH_ALEN);
2314 
2315 	ivi->max_tx_rate = vf->tx_rate;
2316 	ivi->min_tx_rate = 0;
2317 	ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
2318 	ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
2319 		   I40E_VLAN_PRIORITY_SHIFT;
2320 	if (vf->link_forced == false)
2321 		ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
2322 	else if (vf->link_up == true)
2323 		ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
2324 	else
2325 		ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
2326 	ivi->spoofchk = vf->spoofchk;
2327 	ret = 0;
2328 
2329 error_param:
2330 	return ret;
2331 }
2332 
2333 /**
2334  * i40e_ndo_set_vf_link_state
2335  * @netdev: network interface device structure
2336  * @vf_id: VF identifier
2337  * @link: required link state
2338  *
2339  * Set the link state of a specified VF, regardless of physical link state
2340  **/
2341 int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
2342 {
2343 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2344 	struct i40e_pf *pf = np->vsi->back;
2345 	struct i40e_virtchnl_pf_event pfe;
2346 	struct i40e_hw *hw = &pf->hw;
2347 	struct i40e_vf *vf;
2348 	int abs_vf_id;
2349 	int ret = 0;
2350 
2351 	/* validate the request */
2352 	if (vf_id >= pf->num_alloc_vfs) {
2353 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2354 		ret = -EINVAL;
2355 		goto error_out;
2356 	}
2357 
2358 	vf = &pf->vf[vf_id];
2359 	abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
2360 
2361 	pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
2362 	pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
2363 
2364 	switch (link) {
2365 	case IFLA_VF_LINK_STATE_AUTO:
2366 		vf->link_forced = false;
2367 		pfe.event_data.link_event.link_status =
2368 			pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
2369 		pfe.event_data.link_event.link_speed =
2370 			pf->hw.phy.link_info.link_speed;
2371 		break;
2372 	case IFLA_VF_LINK_STATE_ENABLE:
2373 		vf->link_forced = true;
2374 		vf->link_up = true;
2375 		pfe.event_data.link_event.link_status = true;
2376 		pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
2377 		break;
2378 	case IFLA_VF_LINK_STATE_DISABLE:
2379 		vf->link_forced = true;
2380 		vf->link_up = false;
2381 		pfe.event_data.link_event.link_status = false;
2382 		pfe.event_data.link_event.link_speed = 0;
2383 		break;
2384 	default:
2385 		ret = -EINVAL;
2386 		goto error_out;
2387 	}
2388 	/* Notify the VF of its new link state */
2389 	i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
2390 			       0, (u8 *)&pfe, sizeof(pfe), NULL);
2391 
2392 error_out:
2393 	return ret;
2394 }
2395 
2396 /**
2397  * i40e_ndo_set_vf_spoofchk
2398  * @netdev: network interface device structure
2399  * @vf_id: VF identifier
2400  * @enable: flag to enable or disable feature
2401  *
2402  * Enable or disable VF spoof checking
2403  **/
2404 int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
2405 {
2406 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2407 	struct i40e_vsi *vsi = np->vsi;
2408 	struct i40e_pf *pf = vsi->back;
2409 	struct i40e_vsi_context ctxt;
2410 	struct i40e_hw *hw = &pf->hw;
2411 	struct i40e_vf *vf;
2412 	int ret = 0;
2413 
2414 	/* validate the request */
2415 	if (vf_id >= pf->num_alloc_vfs) {
2416 		dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
2417 		ret = -EINVAL;
2418 		goto out;
2419 	}
2420 
2421 	vf = &(pf->vf[vf_id]);
2422 
2423 	if (enable == vf->spoofchk)
2424 		goto out;
2425 
2426 	vf->spoofchk = enable;
2427 	memset(&ctxt, 0, sizeof(ctxt));
2428 	ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
2429 	ctxt.pf_num = pf->hw.pf_id;
2430 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
2431 	if (enable)
2432 		ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
2433 					I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
2434 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
2435 	if (ret) {
2436 		dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
2437 			ret);
2438 		ret = -EIO;
2439 	}
2440 out:
2441 	return ret;
2442 }
2443