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