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