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