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