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