New upstream version 17.08
[deb_dpdk.git] / drivers / net / i40e / i40e_pf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/queue.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdarg.h>
41 #include <inttypes.h>
42
43 #include <rte_string_fns.h>
44 #include <rte_pci.h>
45 #include <rte_ether.h>
46 #include <rte_ethdev.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
49 #include <rte_memcpy.h>
50
51 #include "i40e_logs.h"
52 #include "base/i40e_prototype.h"
53 #include "base/i40e_adminq_cmd.h"
54 #include "base/i40e_type.h"
55 #include "i40e_ethdev.h"
56 #include "i40e_rxtx.h"
57 #include "i40e_pf.h"
58 #include "rte_pmd_i40e.h"
59
60 #define I40E_CFG_CRCSTRIP_DEFAULT 1
61
62 static int
63 i40e_pf_host_switch_queues(struct i40e_pf_vf *vf,
64                            struct virtchnl_queue_select *qsel,
65                            bool on);
66
67 /**
68  * Bind PF queues with VSI and VF.
69  **/
70 static int
71 i40e_pf_vf_queues_mapping(struct i40e_pf_vf *vf)
72 {
73         int i;
74         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
75         uint16_t vsi_id = vf->vsi->vsi_id;
76         uint16_t vf_id  = vf->vf_idx;
77         uint16_t nb_qps = vf->vsi->nb_qps;
78         uint16_t qbase  = vf->vsi->base_queue;
79         uint16_t q1, q2;
80         uint32_t val;
81
82         /*
83          * VF should use scatter range queues. So, it needn't
84          * to set QBASE in this register.
85          */
86         i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vsi_id),
87                           I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
88
89         /* Set to enable VFLAN_QTABLE[] registers valid */
90         I40E_WRITE_REG(hw, I40E_VPLAN_MAPENA(vf_id),
91                 I40E_VPLAN_MAPENA_TXRX_ENA_MASK);
92
93         /* map PF queues to VF */
94         for (i = 0; i < nb_qps; i++) {
95                 val = ((qbase + i) & I40E_VPLAN_QTABLE_QINDEX_MASK);
96                 I40E_WRITE_REG(hw, I40E_VPLAN_QTABLE(i, vf_id), val);
97         }
98
99         /* map PF queues to VSI */
100         for (i = 0; i < I40E_MAX_QP_NUM_PER_VF / 2; i++) {
101                 if (2 * i > nb_qps - 1)
102                         q1 = I40E_VSILAN_QTABLE_QINDEX_0_MASK;
103                 else
104                         q1 = qbase + 2 * i;
105
106                 if (2 * i + 1 > nb_qps - 1)
107                         q2 = I40E_VSILAN_QTABLE_QINDEX_0_MASK;
108                 else
109                         q2 = qbase + 2 * i + 1;
110
111                 val = (q2 << I40E_VSILAN_QTABLE_QINDEX_1_SHIFT) + q1;
112                 i40e_write_rx_ctl(hw, I40E_VSILAN_QTABLE(i, vsi_id), val);
113         }
114         I40E_WRITE_FLUSH(hw);
115
116         return I40E_SUCCESS;
117 }
118
119
120 /**
121  * Proceed VF reset operation.
122  */
123 int
124 i40e_pf_host_vf_reset(struct i40e_pf_vf *vf, bool do_hw_reset)
125 {
126         uint32_t val, i;
127         struct i40e_hw *hw;
128         struct i40e_pf *pf;
129         uint16_t vf_id, abs_vf_id, vf_msix_num;
130         int ret;
131         struct virtchnl_queue_select qsel;
132
133         if (vf == NULL)
134                 return -EINVAL;
135
136         pf = vf->pf;
137         hw = I40E_PF_TO_HW(vf->pf);
138         vf_id = vf->vf_idx;
139         abs_vf_id = vf_id + hw->func_caps.vf_base_id;
140
141         /* Notify VF that we are in VFR progress */
142         I40E_WRITE_REG(hw, I40E_VFGEN_RSTAT1(vf_id), VIRTCHNL_VFR_INPROGRESS);
143
144         /*
145          * If require a SW VF reset, a VFLR interrupt will be generated,
146          * this function will be called again. To avoid it,
147          * disable interrupt first.
148          */
149         if (do_hw_reset) {
150                 vf->state = I40E_VF_INRESET;
151                 val = I40E_READ_REG(hw, I40E_VPGEN_VFRTRIG(vf_id));
152                 val |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
153                 I40E_WRITE_REG(hw, I40E_VPGEN_VFRTRIG(vf_id), val);
154                 I40E_WRITE_FLUSH(hw);
155         }
156
157 #define VFRESET_MAX_WAIT_CNT 100
158         /* Wait until VF reset is done */
159         for (i = 0; i < VFRESET_MAX_WAIT_CNT; i++) {
160                 rte_delay_us(10);
161                 val = I40E_READ_REG(hw, I40E_VPGEN_VFRSTAT(vf_id));
162                 if (val & I40E_VPGEN_VFRSTAT_VFRD_MASK)
163                         break;
164         }
165
166         if (i >= VFRESET_MAX_WAIT_CNT) {
167                 PMD_DRV_LOG(ERR, "VF reset timeout");
168                 return -ETIMEDOUT;
169         }
170         /* This is not first time to do reset, do cleanup job first */
171         if (vf->vsi) {
172                 /* Disable queues */
173                 memset(&qsel, 0, sizeof(qsel));
174                 for (i = 0; i < vf->vsi->nb_qps; i++)
175                         qsel.rx_queues |= 1 << i;
176                 qsel.tx_queues = qsel.rx_queues;
177                 ret = i40e_pf_host_switch_queues(vf, &qsel, false);
178                 if (ret != I40E_SUCCESS) {
179                         PMD_DRV_LOG(ERR, "Disable VF queues failed");
180                         return -EFAULT;
181                 }
182
183                 /* Disable VF interrupt setting */
184                 vf_msix_num = hw->func_caps.num_msix_vectors_vf;
185                 for (i = 0; i < vf_msix_num; i++) {
186                         if (!i)
187                                 val = I40E_VFINT_DYN_CTL0(vf_id);
188                         else
189                                 val = I40E_VFINT_DYN_CTLN(((vf_msix_num - 1) *
190                                                         (vf_id)) + (i - 1));
191                         I40E_WRITE_REG(hw, val, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
192                 }
193                 I40E_WRITE_FLUSH(hw);
194
195                 /* remove VSI */
196                 ret = i40e_vsi_release(vf->vsi);
197                 if (ret != I40E_SUCCESS) {
198                         PMD_DRV_LOG(ERR, "Release VSI failed");
199                         return -EFAULT;
200                 }
201         }
202
203 #define I40E_VF_PCI_ADDR  0xAA
204 #define I40E_VF_PEND_MASK 0x20
205         /* Check the pending transactions of this VF */
206         /* Use absolute VF id, refer to datasheet for details */
207         I40E_WRITE_REG(hw, I40E_PF_PCI_CIAA, I40E_VF_PCI_ADDR |
208                 (abs_vf_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
209         for (i = 0; i < VFRESET_MAX_WAIT_CNT; i++) {
210                 rte_delay_us(1);
211                 val = I40E_READ_REG(hw, I40E_PF_PCI_CIAD);
212                 if ((val & I40E_VF_PEND_MASK) == 0)
213                         break;
214         }
215
216         if (i >= VFRESET_MAX_WAIT_CNT) {
217                 PMD_DRV_LOG(ERR, "Wait VF PCI transaction end timeout");
218                 return -ETIMEDOUT;
219         }
220
221         /* Reset done, Set COMPLETE flag and clear reset bit */
222         I40E_WRITE_REG(hw, I40E_VFGEN_RSTAT1(vf_id), VIRTCHNL_VFR_COMPLETED);
223         val = I40E_READ_REG(hw, I40E_VPGEN_VFRTRIG(vf_id));
224         val &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
225         I40E_WRITE_REG(hw, I40E_VPGEN_VFRTRIG(vf_id), val);
226         vf->reset_cnt++;
227         I40E_WRITE_FLUSH(hw);
228
229         /* Allocate resource again */
230         if (pf->floating_veb && pf->floating_veb_list[vf_id]) {
231                 vf->vsi = i40e_vsi_setup(vf->pf, I40E_VSI_SRIOV,
232                                          NULL, vf->vf_idx);
233         } else {
234                 vf->vsi = i40e_vsi_setup(vf->pf, I40E_VSI_SRIOV,
235                                          vf->pf->main_vsi, vf->vf_idx);
236         }
237
238         if (vf->vsi == NULL) {
239                 PMD_DRV_LOG(ERR, "Add vsi failed");
240                 return -EFAULT;
241         }
242
243         ret = i40e_pf_vf_queues_mapping(vf);
244         if (ret != I40E_SUCCESS) {
245                 PMD_DRV_LOG(ERR, "queue mapping error");
246                 i40e_vsi_release(vf->vsi);
247                 return -EFAULT;
248         }
249
250         I40E_WRITE_REG(hw, I40E_VFGEN_RSTAT1(vf_id), VIRTCHNL_VFR_VFACTIVE);
251
252         return ret;
253 }
254
255 int
256 i40e_pf_host_send_msg_to_vf(struct i40e_pf_vf *vf,
257                             uint32_t opcode,
258                             uint32_t retval,
259                             uint8_t *msg,
260                             uint16_t msglen)
261 {
262         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
263         uint16_t abs_vf_id = hw->func_caps.vf_base_id + vf->vf_idx;
264         int ret;
265
266         ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, opcode, retval,
267                                                 msg, msglen, NULL);
268         if (ret) {
269                 PMD_INIT_LOG(ERR, "Fail to send message to VF, err %u",
270                              hw->aq.asq_last_status);
271         }
272
273         return ret;
274 }
275
276 static void
277 i40e_pf_host_process_cmd_version(struct i40e_pf_vf *vf, bool b_op)
278 {
279         struct virtchnl_version_info info;
280
281         /* Respond like a Linux PF host in order to support both DPDK VF and
282          * Linux VF driver. The expense is original DPDK host specific feature
283          * like CFG_VLAN_PVID and CONFIG_VSI_QUEUES_EXT will not available.
284          *
285          * DPDK VF also can't identify host driver by version number returned.
286          * It always assume talking with Linux PF.
287          */
288         info.major = VIRTCHNL_VERSION_MAJOR;
289         info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
290
291         if (b_op)
292                 i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
293                                             I40E_SUCCESS,
294                                             (uint8_t *)&info,
295                                             sizeof(info));
296         else
297                 i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
298                                             I40E_NOT_SUPPORTED,
299                                             (uint8_t *)&info,
300                                             sizeof(info));
301 }
302
303 static int
304 i40e_pf_host_process_cmd_reset_vf(struct i40e_pf_vf *vf)
305 {
306         i40e_pf_host_vf_reset(vf, 1);
307
308         /* No feedback will be sent to VF for VFLR */
309         return I40E_SUCCESS;
310 }
311
312 static int
313 i40e_pf_host_process_cmd_get_vf_resource(struct i40e_pf_vf *vf, bool b_op)
314 {
315         struct virtchnl_vf_resource *vf_res = NULL;
316         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
317         uint32_t len = 0;
318         int ret = I40E_SUCCESS;
319
320         if (!b_op) {
321                 i40e_pf_host_send_msg_to_vf(vf,
322                                             VIRTCHNL_OP_GET_VF_RESOURCES,
323                                             I40E_NOT_SUPPORTED, NULL, 0);
324                 return ret;
325         }
326
327         /* only have 1 VSI by default */
328         len =  sizeof(struct virtchnl_vf_resource) +
329                                 I40E_DEFAULT_VF_VSI_NUM *
330                 sizeof(struct virtchnl_vsi_resource);
331
332         vf_res = rte_zmalloc("i40e_vf_res", len, 0);
333         if (vf_res == NULL) {
334                 PMD_DRV_LOG(ERR, "failed to allocate mem");
335                 ret = I40E_ERR_NO_MEMORY;
336                 vf_res = NULL;
337                 len = 0;
338                 goto send_msg;
339         }
340
341         vf_res->vf_offload_flags = VIRTCHNL_VF_OFFLOAD_L2 |
342                                 VIRTCHNL_VF_OFFLOAD_VLAN;
343         vf_res->max_vectors = hw->func_caps.num_msix_vectors_vf;
344         vf_res->num_queue_pairs = vf->vsi->nb_qps;
345         vf_res->num_vsis = I40E_DEFAULT_VF_VSI_NUM;
346
347         /* Change below setting if PF host can support more VSIs for VF */
348         vf_res->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
349         vf_res->vsi_res[0].vsi_id = vf->vsi->vsi_id;
350         vf_res->vsi_res[0].num_queue_pairs = vf->vsi->nb_qps;
351         ether_addr_copy(&vf->mac_addr,
352                 (struct ether_addr *)vf_res->vsi_res[0].default_mac_addr);
353
354 send_msg:
355         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
356                                         ret, (uint8_t *)vf_res, len);
357         rte_free(vf_res);
358
359         return ret;
360 }
361
362 static int
363 i40e_pf_host_hmc_config_rxq(struct i40e_hw *hw,
364                             struct i40e_pf_vf *vf,
365                             struct virtchnl_rxq_info *rxq,
366                             uint8_t crcstrip)
367 {
368         int err = I40E_SUCCESS;
369         struct i40e_hmc_obj_rxq rx_ctx;
370         uint16_t abs_queue_id = vf->vsi->base_queue + rxq->queue_id;
371
372         /* Clear the context structure first */
373         memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
374         rx_ctx.dbuff = rxq->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
375         rx_ctx.hbuff = rxq->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
376         rx_ctx.base = rxq->dma_ring_addr / I40E_QUEUE_BASE_ADDR_UNIT;
377         rx_ctx.qlen = rxq->ring_len;
378 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
379         rx_ctx.dsize = 1;
380 #endif
381
382         if (rxq->splithdr_enabled) {
383                 rx_ctx.hsplit_0 = I40E_HEADER_SPLIT_ALL;
384                 rx_ctx.dtype = i40e_header_split_enabled;
385         } else {
386                 rx_ctx.hsplit_0 = I40E_HEADER_SPLIT_NONE;
387                 rx_ctx.dtype = i40e_header_split_none;
388         }
389         rx_ctx.rxmax = rxq->max_pkt_size;
390         rx_ctx.tphrdesc_ena = 1;
391         rx_ctx.tphwdesc_ena = 1;
392         rx_ctx.tphdata_ena = 1;
393         rx_ctx.tphhead_ena = 1;
394         rx_ctx.lrxqthresh = 2;
395         rx_ctx.crcstrip = crcstrip;
396         rx_ctx.l2tsel = 1;
397         rx_ctx.prefena = 1;
398
399         err = i40e_clear_lan_rx_queue_context(hw, abs_queue_id);
400         if (err != I40E_SUCCESS)
401                 return err;
402         err = i40e_set_lan_rx_queue_context(hw, abs_queue_id, &rx_ctx);
403
404         return err;
405 }
406
407 static inline uint8_t
408 i40e_vsi_get_tc_of_queue(struct i40e_vsi *vsi,
409                 uint16_t queue_id)
410 {
411         struct i40e_aqc_vsi_properties_data *info = &vsi->info;
412         uint16_t bsf, qp_idx;
413         uint8_t i;
414
415         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
416                 if (vsi->enabled_tc & (1 << i)) {
417                         qp_idx = rte_le_to_cpu_16((info->tc_mapping[i] &
418                                 I40E_AQ_VSI_TC_QUE_OFFSET_MASK) >>
419                                 I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT);
420                         bsf = rte_le_to_cpu_16((info->tc_mapping[i] &
421                                 I40E_AQ_VSI_TC_QUE_NUMBER_MASK) >>
422                                 I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
423                         if (queue_id >= qp_idx && queue_id < qp_idx + (1 << bsf))
424                                 return i;
425                 }
426         }
427         return 0;
428 }
429
430 static int
431 i40e_pf_host_hmc_config_txq(struct i40e_hw *hw,
432                             struct i40e_pf_vf *vf,
433                             struct virtchnl_txq_info *txq)
434 {
435         int err = I40E_SUCCESS;
436         struct i40e_hmc_obj_txq tx_ctx;
437         struct i40e_vsi *vsi = vf->vsi;
438         uint32_t qtx_ctl;
439         uint16_t abs_queue_id = vsi->base_queue + txq->queue_id;
440         uint8_t dcb_tc;
441
442         /* clear the context structure first */
443         memset(&tx_ctx, 0, sizeof(tx_ctx));
444         tx_ctx.base = txq->dma_ring_addr / I40E_QUEUE_BASE_ADDR_UNIT;
445         tx_ctx.qlen = txq->ring_len;
446         dcb_tc = i40e_vsi_get_tc_of_queue(vsi, txq->queue_id);
447         tx_ctx.rdylist = rte_le_to_cpu_16(vsi->info.qs_handle[dcb_tc]);
448         tx_ctx.head_wb_ena = txq->headwb_enabled;
449         tx_ctx.head_wb_addr = txq->dma_headwb_addr;
450
451         err = i40e_clear_lan_tx_queue_context(hw, abs_queue_id);
452         if (err != I40E_SUCCESS)
453                 return err;
454
455         err = i40e_set_lan_tx_queue_context(hw, abs_queue_id, &tx_ctx);
456         if (err != I40E_SUCCESS)
457                 return err;
458
459         /* bind queue with VF function, since TX/QX will appear in pair,
460          * so only has QTX_CTL to set.
461          */
462         qtx_ctl = (I40E_QTX_CTL_VF_QUEUE << I40E_QTX_CTL_PFVF_Q_SHIFT) |
463                                 ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
464                                 I40E_QTX_CTL_PF_INDX_MASK) |
465                                 (((vf->vf_idx + hw->func_caps.vf_base_id) <<
466                                 I40E_QTX_CTL_VFVM_INDX_SHIFT) &
467                                 I40E_QTX_CTL_VFVM_INDX_MASK);
468         I40E_WRITE_REG(hw, I40E_QTX_CTL(abs_queue_id), qtx_ctl);
469         I40E_WRITE_FLUSH(hw);
470
471         return I40E_SUCCESS;
472 }
473
474 static int
475 i40e_pf_host_process_cmd_config_vsi_queues(struct i40e_pf_vf *vf,
476                                            uint8_t *msg,
477                                            uint16_t msglen,
478                                            bool b_op)
479 {
480         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
481         struct i40e_vsi *vsi = vf->vsi;
482         struct virtchnl_vsi_queue_config_info *vc_vqci =
483                 (struct virtchnl_vsi_queue_config_info *)msg;
484         struct virtchnl_queue_pair_info *vc_qpi;
485         int i, ret = I40E_SUCCESS;
486
487         if (!b_op) {
488                 i40e_pf_host_send_msg_to_vf(vf,
489                                             VIRTCHNL_OP_CONFIG_VSI_QUEUES,
490                                             I40E_NOT_SUPPORTED, NULL, 0);
491                 return ret;
492         }
493
494         if (!msg || vc_vqci->num_queue_pairs > vsi->nb_qps ||
495                 vc_vqci->num_queue_pairs > I40E_MAX_VSI_QP ||
496                 msglen < I40E_VIRTCHNL_CONFIG_VSI_QUEUES_SIZE(vc_vqci,
497                                         vc_vqci->num_queue_pairs)) {
498                 PMD_DRV_LOG(ERR, "vsi_queue_config_info argument wrong");
499                 ret = I40E_ERR_PARAM;
500                 goto send_msg;
501         }
502
503         vc_qpi = vc_vqci->qpair;
504         for (i = 0; i < vc_vqci->num_queue_pairs; i++) {
505                 if (vc_qpi[i].rxq.queue_id > vsi->nb_qps - 1 ||
506                         vc_qpi[i].txq.queue_id > vsi->nb_qps - 1) {
507                         ret = I40E_ERR_PARAM;
508                         goto send_msg;
509                 }
510
511                 /*
512                  * Apply VF RX queue setting to HMC.
513                  * If the opcode is VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT,
514                  * then the extra information of
515                  * 'struct virtchnl_queue_pair_extra_info' is needed,
516                  * otherwise set the last parameter to NULL.
517                  */
518                 if (i40e_pf_host_hmc_config_rxq(hw, vf, &vc_qpi[i].rxq,
519                         I40E_CFG_CRCSTRIP_DEFAULT) != I40E_SUCCESS) {
520                         PMD_DRV_LOG(ERR, "Configure RX queue HMC failed");
521                         ret = I40E_ERR_PARAM;
522                         goto send_msg;
523                 }
524
525                 /* Apply VF TX queue setting to HMC */
526                 if (i40e_pf_host_hmc_config_txq(hw, vf,
527                         &vc_qpi[i].txq) != I40E_SUCCESS) {
528                         PMD_DRV_LOG(ERR, "Configure TX queue HMC failed");
529                         ret = I40E_ERR_PARAM;
530                         goto send_msg;
531                 }
532         }
533
534 send_msg:
535         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
536                                                         ret, NULL, 0);
537
538         return ret;
539 }
540
541 static int
542 i40e_pf_host_process_cmd_config_vsi_queues_ext(struct i40e_pf_vf *vf,
543                                                uint8_t *msg,
544                                                uint16_t msglen,
545                                                bool b_op)
546 {
547         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
548         struct i40e_vsi *vsi = vf->vsi;
549         struct virtchnl_vsi_queue_config_ext_info *vc_vqcei =
550                 (struct virtchnl_vsi_queue_config_ext_info *)msg;
551         struct virtchnl_queue_pair_ext_info *vc_qpei;
552         int i, ret = I40E_SUCCESS;
553
554         if (!b_op) {
555                 i40e_pf_host_send_msg_to_vf(
556                         vf,
557                         VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT,
558                         I40E_NOT_SUPPORTED, NULL, 0);
559                 return ret;
560         }
561
562         if (!msg || vc_vqcei->num_queue_pairs > vsi->nb_qps ||
563                 vc_vqcei->num_queue_pairs > I40E_MAX_VSI_QP ||
564                 msglen < I40E_VIRTCHNL_CONFIG_VSI_QUEUES_SIZE(vc_vqcei,
565                                         vc_vqcei->num_queue_pairs)) {
566                 PMD_DRV_LOG(ERR, "vsi_queue_config_ext_info argument wrong");
567                 ret = I40E_ERR_PARAM;
568                 goto send_msg;
569         }
570
571         vc_qpei = vc_vqcei->qpair;
572         for (i = 0; i < vc_vqcei->num_queue_pairs; i++) {
573                 if (vc_qpei[i].rxq.queue_id > vsi->nb_qps - 1 ||
574                         vc_qpei[i].txq.queue_id > vsi->nb_qps - 1) {
575                         ret = I40E_ERR_PARAM;
576                         goto send_msg;
577                 }
578                 /*
579                  * Apply VF RX queue setting to HMC.
580                  * If the opcode is VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT,
581                  * then the extra information of
582                  * 'struct virtchnl_queue_pair_ext_info' is needed,
583                  * otherwise set the last parameter to NULL.
584                  */
585                 if (i40e_pf_host_hmc_config_rxq(hw, vf, &vc_qpei[i].rxq,
586                         vc_qpei[i].rxq_ext.crcstrip) != I40E_SUCCESS) {
587                         PMD_DRV_LOG(ERR, "Configure RX queue HMC failed");
588                         ret = I40E_ERR_PARAM;
589                         goto send_msg;
590                 }
591
592                 /* Apply VF TX queue setting to HMC */
593                 if (i40e_pf_host_hmc_config_txq(hw, vf, &vc_qpei[i].txq) !=
594                                                         I40E_SUCCESS) {
595                         PMD_DRV_LOG(ERR, "Configure TX queue HMC failed");
596                         ret = I40E_ERR_PARAM;
597                         goto send_msg;
598                 }
599         }
600
601 send_msg:
602         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT,
603                                                                 ret, NULL, 0);
604
605         return ret;
606 }
607
608 static void
609 i40e_pf_config_irq_link_list(struct i40e_pf_vf *vf,
610                               struct virtchnl_vector_map *vvm)
611 {
612 #define BITS_PER_CHAR 8
613         uint64_t linklistmap = 0, tempmap;
614         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
615         uint16_t qid;
616         bool b_first_q = true;
617         enum i40e_queue_type qtype;
618         uint16_t vector_id;
619         uint32_t reg, reg_idx;
620         uint16_t itr_idx = 0, i;
621
622         vector_id = vvm->vector_id;
623         /* setup the head */
624         if (!vector_id)
625                 reg_idx = I40E_VPINT_LNKLST0(vf->vf_idx);
626         else
627                 reg_idx = I40E_VPINT_LNKLSTN(
628                 ((hw->func_caps.num_msix_vectors_vf - 1) * vf->vf_idx)
629                 + (vector_id - 1));
630
631         if (vvm->rxq_map == 0 && vvm->txq_map == 0) {
632                 I40E_WRITE_REG(hw, reg_idx,
633                         I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
634                 goto cfg_irq_done;
635         }
636
637         /* sort all rx and tx queues */
638         tempmap = vvm->rxq_map;
639         for (i = 0; i < sizeof(vvm->rxq_map) * BITS_PER_CHAR; i++) {
640                 if (tempmap & 0x1)
641                         linklistmap |= (1 << (2 * i));
642                 tempmap >>= 1;
643         }
644
645         tempmap = vvm->txq_map;
646         for (i = 0; i < sizeof(vvm->txq_map) * BITS_PER_CHAR; i++) {
647                 if (tempmap & 0x1)
648                         linklistmap |= (1 << (2 * i + 1));
649                 tempmap >>= 1;
650         }
651
652         /* Link all rx and tx queues into a chained list */
653         tempmap = linklistmap;
654         i = 0;
655         b_first_q = true;
656         do {
657                 if (tempmap & 0x1) {
658                         qtype = (enum i40e_queue_type)(i % 2);
659                         qid = vf->vsi->base_queue + i / 2;
660                         if (b_first_q) {
661                                 /* This is header */
662                                 b_first_q = false;
663                                 reg = ((qtype <<
664                                 I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT)
665                                 | qid);
666                         } else {
667                                 /* element in the link list */
668                                 reg = (vector_id) |
669                                 (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
670                                 (qid << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
671                                 BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
672                                 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
673                         }
674                         I40E_WRITE_REG(hw, reg_idx, reg);
675                         /* find next register to program */
676                         switch (qtype) {
677                         case I40E_QUEUE_TYPE_RX:
678                                 reg_idx = I40E_QINT_RQCTL(qid);
679                                 itr_idx = vvm->rxitr_idx;
680                                 break;
681                         case I40E_QUEUE_TYPE_TX:
682                                 reg_idx = I40E_QINT_TQCTL(qid);
683                                 itr_idx = vvm->txitr_idx;
684                                 break;
685                         default:
686                                 break;
687                         }
688                 }
689                 i++;
690                 tempmap >>= 1;
691         } while (tempmap);
692
693         /* Terminate the link list */
694         reg = (vector_id) |
695                 (0 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
696                 (0x7FF << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
697                 BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
698                 (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
699         I40E_WRITE_REG(hw, reg_idx, reg);
700
701 cfg_irq_done:
702         I40E_WRITE_FLUSH(hw);
703 }
704
705 static int
706 i40e_pf_host_process_cmd_config_irq_map(struct i40e_pf_vf *vf,
707                                         uint8_t *msg, uint16_t msglen,
708                                         bool b_op)
709 {
710         int ret = I40E_SUCCESS;
711         struct i40e_pf *pf = vf->pf;
712         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
713         struct virtchnl_irq_map_info *irqmap =
714             (struct virtchnl_irq_map_info *)msg;
715         struct virtchnl_vector_map *map;
716         int i;
717         uint16_t vector_id;
718         unsigned long qbit_max;
719
720         if (!b_op) {
721                 i40e_pf_host_send_msg_to_vf(
722                         vf,
723                         VIRTCHNL_OP_CONFIG_IRQ_MAP,
724                         I40E_NOT_SUPPORTED, NULL, 0);
725                 return ret;
726         }
727
728         if (msg == NULL || msglen < sizeof(struct virtchnl_irq_map_info)) {
729                 PMD_DRV_LOG(ERR, "buffer too short");
730                 ret = I40E_ERR_PARAM;
731                 goto send_msg;
732         }
733
734         /* PF host will support both DPDK VF or Linux VF driver, identify by
735          * number of vectors requested.
736          */
737
738         /* DPDK VF only requires single vector */
739         if (irqmap->num_vectors == 1) {
740                 /* This MSIX intr store the intr in VF range */
741                 vf->vsi->msix_intr = irqmap->vecmap[0].vector_id;
742                 vf->vsi->nb_msix = irqmap->num_vectors;
743                 vf->vsi->nb_used_qps = vf->vsi->nb_qps;
744
745                 /* Don't care how the TX/RX queue mapping with this vector.
746                  * Link all VF RX queues together. Only did mapping work.
747                  * VF can disable/enable the intr by itself.
748                  */
749                 i40e_vsi_queues_bind_intr(vf->vsi);
750                 goto send_msg;
751         }
752
753         /* Then, it's Linux VF driver */
754         qbit_max = 1 << pf->vf_nb_qp_max;
755         for (i = 0; i < irqmap->num_vectors; i++) {
756                 map = &irqmap->vecmap[i];
757
758                 vector_id = map->vector_id;
759                 /* validate msg params */
760                 if (vector_id >= hw->func_caps.num_msix_vectors_vf) {
761                         ret = I40E_ERR_PARAM;
762                         goto send_msg;
763                 }
764
765                 if ((map->rxq_map < qbit_max) && (map->txq_map < qbit_max)) {
766                         i40e_pf_config_irq_link_list(vf, map);
767                 } else {
768                         /* configured queue size excceed limit */
769                         ret = I40E_ERR_PARAM;
770                         goto send_msg;
771                 }
772         }
773
774 send_msg:
775         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
776                                                         ret, NULL, 0);
777
778         return ret;
779 }
780
781 static int
782 i40e_pf_host_switch_queues(struct i40e_pf_vf *vf,
783                            struct virtchnl_queue_select *qsel,
784                            bool on)
785 {
786         int ret = I40E_SUCCESS;
787         int i;
788         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
789         uint16_t baseq = vf->vsi->base_queue;
790
791         if (qsel->rx_queues + qsel->tx_queues == 0)
792                 return I40E_ERR_PARAM;
793
794         /* always enable RX first and disable last */
795         /* Enable RX if it's enable */
796         if (on) {
797                 for (i = 0; i < I40E_MAX_QP_NUM_PER_VF; i++)
798                         if (qsel->rx_queues & (1 << i)) {
799                                 ret = i40e_switch_rx_queue(hw, baseq + i, on);
800                                 if (ret != I40E_SUCCESS)
801                                         return ret;
802                         }
803         }
804
805         /* Enable/Disable TX */
806         for (i = 0; i < I40E_MAX_QP_NUM_PER_VF; i++)
807                 if (qsel->tx_queues & (1 << i)) {
808                         ret = i40e_switch_tx_queue(hw, baseq + i, on);
809                         if (ret != I40E_SUCCESS)
810                                 return ret;
811                 }
812
813         /* disable RX last if it's disable */
814         if (!on) {
815                 /* disable RX */
816                 for (i = 0; i < I40E_MAX_QP_NUM_PER_VF; i++)
817                         if (qsel->rx_queues & (1 << i)) {
818                                 ret = i40e_switch_rx_queue(hw, baseq + i, on);
819                                 if (ret != I40E_SUCCESS)
820                                         return ret;
821                         }
822         }
823
824         return ret;
825 }
826
827 static int
828 i40e_pf_host_process_cmd_enable_queues(struct i40e_pf_vf *vf,
829                                        uint8_t *msg,
830                                        uint16_t msglen)
831 {
832         int ret = I40E_SUCCESS;
833         struct virtchnl_queue_select *q_sel =
834                 (struct virtchnl_queue_select *)msg;
835
836         if (msg == NULL || msglen != sizeof(*q_sel)) {
837                 ret = I40E_ERR_PARAM;
838                 goto send_msg;
839         }
840         ret = i40e_pf_host_switch_queues(vf, q_sel, true);
841
842 send_msg:
843         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
844                                                         ret, NULL, 0);
845
846         return ret;
847 }
848
849 static int
850 i40e_pf_host_process_cmd_disable_queues(struct i40e_pf_vf *vf,
851                                         uint8_t *msg,
852                                         uint16_t msglen,
853                                         bool b_op)
854 {
855         int ret = I40E_SUCCESS;
856         struct virtchnl_queue_select *q_sel =
857                 (struct virtchnl_queue_select *)msg;
858
859         if (!b_op) {
860                 i40e_pf_host_send_msg_to_vf(
861                         vf,
862                         VIRTCHNL_OP_DISABLE_QUEUES,
863                         I40E_NOT_SUPPORTED, NULL, 0);
864                 return ret;
865         }
866
867         if (msg == NULL || msglen != sizeof(*q_sel)) {
868                 ret = I40E_ERR_PARAM;
869                 goto send_msg;
870         }
871         ret = i40e_pf_host_switch_queues(vf, q_sel, false);
872
873 send_msg:
874         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
875                                                         ret, NULL, 0);
876
877         return ret;
878 }
879
880
881 static int
882 i40e_pf_host_process_cmd_add_ether_address(struct i40e_pf_vf *vf,
883                                            uint8_t *msg,
884                                            uint16_t msglen,
885                                            bool b_op)
886 {
887         int ret = I40E_SUCCESS;
888         struct virtchnl_ether_addr_list *addr_list =
889                         (struct virtchnl_ether_addr_list *)msg;
890         struct i40e_mac_filter_info filter;
891         int i;
892         struct ether_addr *mac;
893
894         if (!b_op) {
895                 i40e_pf_host_send_msg_to_vf(
896                         vf,
897                         VIRTCHNL_OP_ADD_ETH_ADDR,
898                         I40E_NOT_SUPPORTED, NULL, 0);
899                 return ret;
900         }
901
902         memset(&filter, 0 , sizeof(struct i40e_mac_filter_info));
903
904         if (msg == NULL || msglen <= sizeof(*addr_list)) {
905                 PMD_DRV_LOG(ERR, "add_ether_address argument too short");
906                 ret = I40E_ERR_PARAM;
907                 goto send_msg;
908         }
909
910         for (i = 0; i < addr_list->num_elements; i++) {
911                 mac = (struct ether_addr *)(addr_list->list[i].addr);
912                 (void)rte_memcpy(&filter.mac_addr, mac, ETHER_ADDR_LEN);
913                 filter.filter_type = RTE_MACVLAN_PERFECT_MATCH;
914                 if (is_zero_ether_addr(mac) ||
915                     i40e_vsi_add_mac(vf->vsi, &filter)) {
916                         ret = I40E_ERR_INVALID_MAC_ADDR;
917                         goto send_msg;
918                 }
919         }
920
921 send_msg:
922         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
923                                                         ret, NULL, 0);
924
925         return ret;
926 }
927
928 static int
929 i40e_pf_host_process_cmd_del_ether_address(struct i40e_pf_vf *vf,
930                                            uint8_t *msg,
931                                            uint16_t msglen,
932                                            bool b_op)
933 {
934         int ret = I40E_SUCCESS;
935         struct virtchnl_ether_addr_list *addr_list =
936                 (struct virtchnl_ether_addr_list *)msg;
937         int i;
938         struct ether_addr *mac;
939
940         if (!b_op) {
941                 i40e_pf_host_send_msg_to_vf(
942                         vf,
943                         VIRTCHNL_OP_DEL_ETH_ADDR,
944                         I40E_NOT_SUPPORTED, NULL, 0);
945                 return ret;
946         }
947
948         if (msg == NULL || msglen <= sizeof(*addr_list)) {
949                 PMD_DRV_LOG(ERR, "delete_ether_address argument too short");
950                 ret = I40E_ERR_PARAM;
951                 goto send_msg;
952         }
953
954         for (i = 0; i < addr_list->num_elements; i++) {
955                 mac = (struct ether_addr *)(addr_list->list[i].addr);
956                 if(is_zero_ether_addr(mac) ||
957                         i40e_vsi_delete_mac(vf->vsi, mac)) {
958                         ret = I40E_ERR_INVALID_MAC_ADDR;
959                         goto send_msg;
960                 }
961         }
962
963 send_msg:
964         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
965                                                         ret, NULL, 0);
966
967         return ret;
968 }
969
970 static int
971 i40e_pf_host_process_cmd_add_vlan(struct i40e_pf_vf *vf,
972                                 uint8_t *msg, uint16_t msglen,
973                                 bool b_op)
974 {
975         int ret = I40E_SUCCESS;
976         struct virtchnl_vlan_filter_list *vlan_filter_list =
977                 (struct virtchnl_vlan_filter_list *)msg;
978         int i;
979         uint16_t *vid;
980
981         if (!b_op) {
982                 i40e_pf_host_send_msg_to_vf(
983                         vf,
984                         VIRTCHNL_OP_ADD_VLAN,
985                         I40E_NOT_SUPPORTED, NULL, 0);
986                 return ret;
987         }
988
989         if (msg == NULL || msglen <= sizeof(*vlan_filter_list)) {
990                 PMD_DRV_LOG(ERR, "add_vlan argument too short");
991                 ret = I40E_ERR_PARAM;
992                 goto send_msg;
993         }
994
995         vid = vlan_filter_list->vlan_id;
996
997         for (i = 0; i < vlan_filter_list->num_elements; i++) {
998                 ret = i40e_vsi_add_vlan(vf->vsi, vid[i]);
999                 if(ret != I40E_SUCCESS)
1000                         goto send_msg;
1001         }
1002
1003 send_msg:
1004         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN,
1005                                                 ret, NULL, 0);
1006
1007         return ret;
1008 }
1009
1010 static int
1011 i40e_pf_host_process_cmd_del_vlan(struct i40e_pf_vf *vf,
1012                                   uint8_t *msg,
1013                                   uint16_t msglen,
1014                                   bool b_op)
1015 {
1016         int ret = I40E_SUCCESS;
1017         struct virtchnl_vlan_filter_list *vlan_filter_list =
1018                         (struct virtchnl_vlan_filter_list *)msg;
1019         int i;
1020         uint16_t *vid;
1021
1022         if (!b_op) {
1023                 i40e_pf_host_send_msg_to_vf(
1024                         vf,
1025                         VIRTCHNL_OP_DEL_VLAN,
1026                         I40E_NOT_SUPPORTED, NULL, 0);
1027                 return ret;
1028         }
1029
1030         if (msg == NULL || msglen <= sizeof(*vlan_filter_list)) {
1031                 PMD_DRV_LOG(ERR, "delete_vlan argument too short");
1032                 ret = I40E_ERR_PARAM;
1033                 goto send_msg;
1034         }
1035
1036         vid = vlan_filter_list->vlan_id;
1037         for (i = 0; i < vlan_filter_list->num_elements; i++) {
1038                 ret = i40e_vsi_delete_vlan(vf->vsi, vid[i]);
1039                 if(ret != I40E_SUCCESS)
1040                         goto send_msg;
1041         }
1042
1043 send_msg:
1044         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN,
1045                                                 ret, NULL, 0);
1046
1047         return ret;
1048 }
1049
1050 static int
1051 i40e_pf_host_process_cmd_config_promisc_mode(
1052                                         struct i40e_pf_vf *vf,
1053                                         uint8_t *msg,
1054                                         uint16_t msglen,
1055                                         bool b_op)
1056 {
1057         int ret = I40E_SUCCESS;
1058         struct virtchnl_promisc_info *promisc =
1059                                 (struct virtchnl_promisc_info *)msg;
1060         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
1061         bool unicast = FALSE, multicast = FALSE;
1062
1063         if (!b_op) {
1064                 i40e_pf_host_send_msg_to_vf(
1065                         vf,
1066                         VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
1067                         I40E_NOT_SUPPORTED, NULL, 0);
1068                 return ret;
1069         }
1070
1071         if (msg == NULL || msglen != sizeof(*promisc)) {
1072                 ret = I40E_ERR_PARAM;
1073                 goto send_msg;
1074         }
1075
1076         if (promisc->flags & FLAG_VF_UNICAST_PROMISC)
1077                 unicast = TRUE;
1078         ret = i40e_aq_set_vsi_unicast_promiscuous(hw,
1079                         vf->vsi->seid, unicast, NULL, true);
1080         if (ret != I40E_SUCCESS)
1081                 goto send_msg;
1082
1083         if (promisc->flags & FLAG_VF_MULTICAST_PROMISC)
1084                 multicast = TRUE;
1085         ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vf->vsi->seid,
1086                                                 multicast, NULL);
1087
1088 send_msg:
1089         i40e_pf_host_send_msg_to_vf(vf,
1090                 VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE, ret, NULL, 0);
1091
1092         return ret;
1093 }
1094
1095 static int
1096 i40e_pf_host_process_cmd_get_stats(struct i40e_pf_vf *vf, bool b_op)
1097 {
1098         i40e_update_vsi_stats(vf->vsi);
1099
1100         if (b_op)
1101                 i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS,
1102                                             I40E_SUCCESS,
1103                                             (uint8_t *)&vf->vsi->eth_stats,
1104                                             sizeof(vf->vsi->eth_stats));
1105         else
1106                 i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS,
1107                                             I40E_NOT_SUPPORTED,
1108                                             (uint8_t *)&vf->vsi->eth_stats,
1109                                             sizeof(vf->vsi->eth_stats));
1110
1111         return I40E_SUCCESS;
1112 }
1113
1114 static int
1115 i40e_pf_host_process_cmd_enable_vlan_strip(struct i40e_pf_vf *vf, bool b_op)
1116 {
1117         int ret = I40E_SUCCESS;
1118
1119         if (!b_op) {
1120                 i40e_pf_host_send_msg_to_vf(
1121                         vf,
1122                         VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
1123                         I40E_NOT_SUPPORTED, NULL, 0);
1124                 return ret;
1125         }
1126
1127         ret = i40e_vsi_config_vlan_stripping(vf->vsi, TRUE);
1128         if (ret != 0)
1129                 PMD_DRV_LOG(ERR, "Failed to enable vlan stripping");
1130
1131         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
1132                                     ret, NULL, 0);
1133
1134         return ret;
1135 }
1136
1137 static int
1138 i40e_pf_host_process_cmd_disable_vlan_strip(struct i40e_pf_vf *vf, bool b_op)
1139 {
1140         int ret = I40E_SUCCESS;
1141
1142         if (!b_op) {
1143                 i40e_pf_host_send_msg_to_vf(
1144                         vf,
1145                         VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
1146                         I40E_NOT_SUPPORTED, NULL, 0);
1147                 return ret;
1148         }
1149
1150         ret = i40e_vsi_config_vlan_stripping(vf->vsi, FALSE);
1151         if (ret != 0)
1152                 PMD_DRV_LOG(ERR, "Failed to disable vlan stripping");
1153
1154         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
1155                                     ret, NULL, 0);
1156
1157         return ret;
1158 }
1159
1160 static int
1161 i40e_pf_host_process_cmd_cfg_pvid(struct i40e_pf_vf *vf,
1162                                         uint8_t *msg,
1163                                         uint16_t msglen,
1164                                         bool b_op)
1165 {
1166         int ret = I40E_SUCCESS;
1167         struct virtchnl_pvid_info  *tpid_info =
1168                         (struct virtchnl_pvid_info *)msg;
1169
1170         if (!b_op) {
1171                 i40e_pf_host_send_msg_to_vf(
1172                         vf,
1173                         I40E_VIRTCHNL_OP_CFG_VLAN_PVID,
1174                         I40E_NOT_SUPPORTED, NULL, 0);
1175                 return ret;
1176         }
1177
1178         if (msg == NULL || msglen != sizeof(*tpid_info)) {
1179                 ret = I40E_ERR_PARAM;
1180                 goto send_msg;
1181         }
1182
1183         ret = i40e_vsi_vlan_pvid_set(vf->vsi, &tpid_info->info);
1184
1185 send_msg:
1186         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_CFG_VLAN_PVID,
1187                                         ret, NULL, 0);
1188
1189         return ret;
1190 }
1191
1192 void
1193 i40e_notify_vf_link_status(struct rte_eth_dev *dev, struct i40e_pf_vf *vf)
1194 {
1195         struct virtchnl_pf_event event;
1196
1197         event.event = VIRTCHNL_EVENT_LINK_CHANGE;
1198         event.event_data.link_event.link_status =
1199                 dev->data->dev_link.link_status;
1200
1201         /* need to convert the ETH_SPEED_xxx into VIRTCHNL_LINK_SPEED_xxx */
1202         switch (dev->data->dev_link.link_speed) {
1203         case ETH_SPEED_NUM_100M:
1204                 event.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_100MB;
1205                 break;
1206         case ETH_SPEED_NUM_1G:
1207                 event.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_1GB;
1208                 break;
1209         case ETH_SPEED_NUM_10G:
1210                 event.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_10GB;
1211                 break;
1212         case ETH_SPEED_NUM_20G:
1213                 event.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_20GB;
1214                 break;
1215         case ETH_SPEED_NUM_25G:
1216                 event.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_25GB;
1217                 break;
1218         case ETH_SPEED_NUM_40G:
1219                 event.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_40GB;
1220                 break;
1221         default:
1222                 event.event_data.link_event.link_speed =
1223                         VIRTCHNL_LINK_SPEED_UNKNOWN;
1224                 break;
1225         }
1226
1227         i40e_pf_host_send_msg_to_vf(vf, VIRTCHNL_OP_EVENT,
1228                 I40E_SUCCESS, (uint8_t *)&event, sizeof(event));
1229 }
1230
1231 void
1232 i40e_pf_host_handle_vf_msg(struct rte_eth_dev *dev,
1233                            uint16_t abs_vf_id, uint32_t opcode,
1234                            __rte_unused uint32_t retval,
1235                            uint8_t *msg,
1236                            uint16_t msglen)
1237 {
1238         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1239         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1240         struct i40e_pf_vf *vf;
1241         /* AdminQ will pass absolute VF id, transfer to internal vf id */
1242         uint16_t vf_id = abs_vf_id - hw->func_caps.vf_base_id;
1243         struct rte_pmd_i40e_mb_event_param ret_param;
1244         bool b_op = TRUE;
1245
1246         if (vf_id > pf->vf_num - 1 || !pf->vfs) {
1247                 PMD_DRV_LOG(ERR, "invalid argument");
1248                 return;
1249         }
1250
1251         vf = &pf->vfs[vf_id];
1252         if (!vf->vsi) {
1253                 PMD_DRV_LOG(ERR, "NO VSI associated with VF found");
1254                 i40e_pf_host_send_msg_to_vf(vf, opcode,
1255                         I40E_ERR_NO_AVAILABLE_VSI, NULL, 0);
1256                 return;
1257         }
1258
1259         /**
1260          * initialise structure to send to user application
1261          * will return response from user in retval field
1262          */
1263         ret_param.retval = RTE_PMD_I40E_MB_EVENT_PROCEED;
1264         ret_param.vfid = vf_id;
1265         ret_param.msg_type = opcode;
1266         ret_param.msg = (void *)msg;
1267         ret_param.msglen = msglen;
1268
1269         /**
1270          * Ask user application if we're allowed to perform those functions.
1271          * If we get ret_param.retval == RTE_PMD_I40E_MB_EVENT_PROCEED,
1272          * then business as usual.
1273          * If RTE_PMD_I40E_MB_EVENT_NOOP_ACK or RTE_PMD_I40E_MB_EVENT_NOOP_NACK,
1274          * do nothing and send not_supported to VF. As PF must send a response
1275          * to VF and ACK/NACK is not defined.
1276          */
1277         _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_VF_MBOX,
1278                                       NULL, &ret_param);
1279         if (ret_param.retval != RTE_PMD_I40E_MB_EVENT_PROCEED) {
1280                 PMD_DRV_LOG(WARNING, "VF to PF message(%d) is not permitted!",
1281                             opcode);
1282                 b_op = FALSE;
1283         }
1284
1285         switch (opcode) {
1286         case VIRTCHNL_OP_VERSION:
1287                 PMD_DRV_LOG(INFO, "OP_VERSION received");
1288                 i40e_pf_host_process_cmd_version(vf, b_op);
1289                 break;
1290         case VIRTCHNL_OP_RESET_VF:
1291                 PMD_DRV_LOG(INFO, "OP_RESET_VF received");
1292                 i40e_pf_host_process_cmd_reset_vf(vf);
1293                 break;
1294         case VIRTCHNL_OP_GET_VF_RESOURCES:
1295                 PMD_DRV_LOG(INFO, "OP_GET_VF_RESOURCES received");
1296                 i40e_pf_host_process_cmd_get_vf_resource(vf, b_op);
1297                 break;
1298         case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1299                 PMD_DRV_LOG(INFO, "OP_CONFIG_VSI_QUEUES received");
1300                 i40e_pf_host_process_cmd_config_vsi_queues(vf, msg,
1301                                                            msglen, b_op);
1302                 break;
1303         case VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT:
1304                 PMD_DRV_LOG(INFO, "OP_CONFIG_VSI_QUEUES_EXT received");
1305                 i40e_pf_host_process_cmd_config_vsi_queues_ext(vf, msg,
1306                                                                msglen, b_op);
1307                 break;
1308         case VIRTCHNL_OP_CONFIG_IRQ_MAP:
1309                 PMD_DRV_LOG(INFO, "OP_CONFIG_IRQ_MAP received");
1310                 i40e_pf_host_process_cmd_config_irq_map(vf, msg, msglen, b_op);
1311                 break;
1312         case VIRTCHNL_OP_ENABLE_QUEUES:
1313                 PMD_DRV_LOG(INFO, "OP_ENABLE_QUEUES received");
1314                 if (b_op) {
1315                         i40e_pf_host_process_cmd_enable_queues(vf, msg, msglen);
1316                         i40e_notify_vf_link_status(dev, vf);
1317                 } else {
1318                         i40e_pf_host_send_msg_to_vf(
1319                                 vf, VIRTCHNL_OP_ENABLE_QUEUES,
1320                                 I40E_NOT_SUPPORTED, NULL, 0);
1321                 }
1322                 break;
1323         case VIRTCHNL_OP_DISABLE_QUEUES:
1324                 PMD_DRV_LOG(INFO, "OP_DISABLE_QUEUE received");
1325                 i40e_pf_host_process_cmd_disable_queues(vf, msg, msglen, b_op);
1326                 break;
1327         case VIRTCHNL_OP_ADD_ETH_ADDR:
1328                 PMD_DRV_LOG(INFO, "OP_ADD_ETHER_ADDRESS received");
1329                 i40e_pf_host_process_cmd_add_ether_address(vf, msg,
1330                                                            msglen, b_op);
1331                 break;
1332         case VIRTCHNL_OP_DEL_ETH_ADDR:
1333                 PMD_DRV_LOG(INFO, "OP_DEL_ETHER_ADDRESS received");
1334                 i40e_pf_host_process_cmd_del_ether_address(vf, msg,
1335                                                            msglen, b_op);
1336                 break;
1337         case VIRTCHNL_OP_ADD_VLAN:
1338                 PMD_DRV_LOG(INFO, "OP_ADD_VLAN received");
1339                 i40e_pf_host_process_cmd_add_vlan(vf, msg, msglen, b_op);
1340                 break;
1341         case VIRTCHNL_OP_DEL_VLAN:
1342                 PMD_DRV_LOG(INFO, "OP_DEL_VLAN received");
1343                 i40e_pf_host_process_cmd_del_vlan(vf, msg, msglen, b_op);
1344                 break;
1345         case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1346                 PMD_DRV_LOG(INFO, "OP_CONFIG_PROMISCUOUS_MODE received");
1347                 i40e_pf_host_process_cmd_config_promisc_mode(vf, msg,
1348                                                              msglen, b_op);
1349                 break;
1350         case VIRTCHNL_OP_GET_STATS:
1351                 PMD_DRV_LOG(INFO, "OP_GET_STATS received");
1352                 i40e_pf_host_process_cmd_get_stats(vf, b_op);
1353                 break;
1354         case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
1355                 PMD_DRV_LOG(INFO, "OP_ENABLE_VLAN_STRIPPING received");
1356                 i40e_pf_host_process_cmd_enable_vlan_strip(vf, b_op);
1357                 break;
1358         case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
1359                 PMD_DRV_LOG(INFO, "OP_DISABLE_VLAN_STRIPPING received");
1360                 i40e_pf_host_process_cmd_disable_vlan_strip(vf, b_op);
1361                 break;
1362         case I40E_VIRTCHNL_OP_CFG_VLAN_PVID:
1363                 PMD_DRV_LOG(INFO, "OP_CFG_VLAN_PVID received");
1364                 i40e_pf_host_process_cmd_cfg_pvid(vf, msg, msglen, b_op);
1365                 break;
1366         /* Don't add command supported below, which will
1367          * return an error code.
1368          */
1369         default:
1370                 PMD_DRV_LOG(ERR, "%u received, not supported", opcode);
1371                 i40e_pf_host_send_msg_to_vf(vf, opcode, I40E_ERR_PARAM,
1372                                                                 NULL, 0);
1373                 break;
1374         }
1375 }
1376
1377 int
1378 i40e_pf_host_init(struct rte_eth_dev *dev)
1379 {
1380         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1381         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
1382         int ret, i;
1383         uint32_t val;
1384
1385         PMD_INIT_FUNC_TRACE();
1386
1387         /**
1388          * return if SRIOV not enabled, VF number not configured or
1389          * no queue assigned.
1390          */
1391         if(!hw->func_caps.sr_iov_1_1 || pf->vf_num == 0 || pf->vf_nb_qps == 0)
1392                 return I40E_SUCCESS;
1393
1394         /* Allocate memory to store VF structure */
1395         pf->vfs = rte_zmalloc("i40e_pf_vf",sizeof(*pf->vfs) * pf->vf_num, 0);
1396         if(pf->vfs == NULL)
1397                 return -ENOMEM;
1398
1399         /* Disable irq0 for VFR event */
1400         i40e_pf_disable_irq0(hw);
1401
1402         /* Disable VF link status interrupt */
1403         val = I40E_READ_REG(hw, I40E_PFGEN_PORTMDIO_NUM);
1404         val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
1405         I40E_WRITE_REG(hw, I40E_PFGEN_PORTMDIO_NUM, val);
1406         I40E_WRITE_FLUSH(hw);
1407
1408         for (i = 0; i < pf->vf_num; i++) {
1409                 pf->vfs[i].pf = pf;
1410                 pf->vfs[i].state = I40E_VF_INACTIVE;
1411                 pf->vfs[i].vf_idx = i;
1412                 ret = i40e_pf_host_vf_reset(&pf->vfs[i], 0);
1413                 if (ret != I40E_SUCCESS)
1414                         goto fail;
1415         }
1416
1417         RTE_ETH_DEV_SRIOV(dev).active = pf->vf_num;
1418         /* restore irq0 */
1419         i40e_pf_enable_irq0(hw);
1420
1421         return I40E_SUCCESS;
1422
1423 fail:
1424         rte_free(pf->vfs);
1425         i40e_pf_enable_irq0(hw);
1426
1427         return ret;
1428 }
1429
1430 int
1431 i40e_pf_host_uninit(struct rte_eth_dev *dev)
1432 {
1433         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1434         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
1435         uint32_t val;
1436
1437         PMD_INIT_FUNC_TRACE();
1438
1439         /**
1440          * return if SRIOV not enabled, VF number not configured or
1441          * no queue assigned.
1442          */
1443         if ((!hw->func_caps.sr_iov_1_1) ||
1444                 (pf->vf_num == 0) ||
1445                 (pf->vf_nb_qps == 0))
1446                 return I40E_SUCCESS;
1447
1448         /* free memory to store VF structure */
1449         rte_free(pf->vfs);
1450         pf->vfs = NULL;
1451
1452         /* Disable irq0 for VFR event */
1453         i40e_pf_disable_irq0(hw);
1454
1455         /* Disable VF link status interrupt */
1456         val = I40E_READ_REG(hw, I40E_PFGEN_PORTMDIO_NUM);
1457         val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
1458         I40E_WRITE_REG(hw, I40E_PFGEN_PORTMDIO_NUM, val);
1459         I40E_WRITE_FLUSH(hw);
1460
1461         return I40E_SUCCESS;
1462 }