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