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