net/i40e: parse more VF parameter and configure
[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 i40e_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 i40e_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), I40E_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
171         /* This is not first time to do reset, do cleanup job first */
172         if (vf->vsi) {
173                 /* Disable queues */
174                 memset(&qsel, 0, sizeof(qsel));
175                 for (i = 0; i < vf->vsi->nb_qps; i++)
176                         qsel.rx_queues |= 1 << i;
177                 qsel.tx_queues = qsel.rx_queues;
178                 ret = i40e_pf_host_switch_queues(vf, &qsel, false);
179                 if (ret != I40E_SUCCESS) {
180                         PMD_DRV_LOG(ERR, "Disable VF queues failed");
181                         return -EFAULT;
182                 }
183
184                 /* Disable VF interrupt setting */
185                 vf_msix_num = hw->func_caps.num_msix_vectors_vf;
186                 for (i = 0; i < vf_msix_num; i++) {
187                         if (!i)
188                                 val = I40E_VFINT_DYN_CTL0(vf_id);
189                         else
190                                 val = I40E_VFINT_DYN_CTLN(((vf_msix_num - 1) *
191                                                         (vf_id)) + (i - 1));
192                         I40E_WRITE_REG(hw, val, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
193                 }
194                 I40E_WRITE_FLUSH(hw);
195
196                 /* remove VSI */
197                 ret = i40e_vsi_release(vf->vsi);
198                 if (ret != I40E_SUCCESS) {
199                         PMD_DRV_LOG(ERR, "Release VSI failed");
200                         return -EFAULT;
201                 }
202         }
203
204 #define I40E_VF_PCI_ADDR  0xAA
205 #define I40E_VF_PEND_MASK 0x20
206         /* Check the pending transactions of this VF */
207         /* Use absolute VF id, refer to datasheet for details */
208         I40E_WRITE_REG(hw, I40E_PF_PCI_CIAA, I40E_VF_PCI_ADDR |
209                 (abs_vf_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
210         for (i = 0; i < VFRESET_MAX_WAIT_CNT; i++) {
211                 rte_delay_us(1);
212                 val = I40E_READ_REG(hw, I40E_PF_PCI_CIAD);
213                 if ((val & I40E_VF_PEND_MASK) == 0)
214                         break;
215         }
216
217         if (i >= VFRESET_MAX_WAIT_CNT) {
218                 PMD_DRV_LOG(ERR, "Wait VF PCI transaction end timeout");
219                 return -ETIMEDOUT;
220         }
221
222         /* Reset done, Set COMPLETE flag and clear reset bit */
223         I40E_WRITE_REG(hw, I40E_VFGEN_RSTAT1(vf_id), I40E_VFR_COMPLETED);
224         val = I40E_READ_REG(hw, I40E_VPGEN_VFRTRIG(vf_id));
225         val &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
226         I40E_WRITE_REG(hw, I40E_VPGEN_VFRTRIG(vf_id), val);
227         vf->reset_cnt++;
228         I40E_WRITE_FLUSH(hw);
229
230         /* Allocate resource again */
231         if (pf->floating_veb && pf->floating_veb_list[vf_id]) {
232                 vf->vsi = i40e_vsi_setup(vf->pf, I40E_VSI_SRIOV,
233                                          NULL, vf->vf_idx);
234         } else {
235                 vf->vsi = i40e_vsi_setup(vf->pf, I40E_VSI_SRIOV,
236                                          vf->pf->main_vsi, vf->vf_idx);
237         }
238
239         if (vf->vsi == NULL) {
240                 PMD_DRV_LOG(ERR, "Add vsi failed");
241                 return -EFAULT;
242         }
243
244         ret = i40e_pf_vf_queues_mapping(vf);
245         if (ret != I40E_SUCCESS) {
246                 PMD_DRV_LOG(ERR, "queue mapping error");
247                 i40e_vsi_release(vf->vsi);
248                 return -EFAULT;
249         }
250
251         I40E_WRITE_REG(hw, I40E_VFGEN_RSTAT1(vf_id), I40E_VFR_VFACTIVE);
252
253         return ret;
254 }
255
256 int
257 i40e_pf_host_send_msg_to_vf(struct i40e_pf_vf *vf,
258                             uint32_t opcode,
259                             uint32_t retval,
260                             uint8_t *msg,
261                             uint16_t msglen)
262 {
263         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
264         uint16_t abs_vf_id = hw->func_caps.vf_base_id + vf->vf_idx;
265         int ret;
266
267         ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, opcode, retval,
268                                                 msg, msglen, NULL);
269         if (ret) {
270                 PMD_INIT_LOG(ERR, "Fail to send message to VF, err %u",
271                              hw->aq.asq_last_status);
272         }
273
274         return ret;
275 }
276
277 static void
278 i40e_pf_host_process_cmd_version(struct i40e_pf_vf *vf, bool b_op)
279 {
280         struct i40e_virtchnl_version_info info;
281
282         /* Respond like a Linux PF host in order to support both DPDK VF and
283          * Linux VF driver. The expense is original DPDK host specific feature
284          * like CFG_VLAN_PVID and CONFIG_VSI_QUEUES_EXT will not available.
285          *
286          * DPDK VF also can't identify host driver by version number returned.
287          * It always assume talking with Linux PF.
288          */
289         info.major = I40E_VIRTCHNL_VERSION_MAJOR;
290         info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
291
292         if (b_op)
293                 i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
294                                             I40E_SUCCESS,
295                                             (uint8_t *)&info,
296                                             sizeof(info));
297         else
298                 i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
299                                             I40E_NOT_SUPPORTED,
300                                             (uint8_t *)&info,
301                                             sizeof(info));
302 }
303
304 static int
305 i40e_pf_host_process_cmd_reset_vf(struct i40e_pf_vf *vf)
306 {
307         i40e_pf_host_vf_reset(vf, 1);
308
309         /* No feedback will be sent to VF for VFLR */
310         return I40E_SUCCESS;
311 }
312
313 static int
314 i40e_pf_host_process_cmd_get_vf_resource(struct i40e_pf_vf *vf, bool b_op)
315 {
316         struct i40e_virtchnl_vf_resource *vf_res = NULL;
317         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
318         uint32_t len = 0;
319         int ret = I40E_SUCCESS;
320
321         if (!b_op) {
322                 i40e_pf_host_send_msg_to_vf(vf,
323                                             I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
324                                             I40E_NOT_SUPPORTED, NULL, 0);
325                 return ret;
326         }
327
328         /* only have 1 VSI by default */
329         len =  sizeof(struct i40e_virtchnl_vf_resource) +
330                                 I40E_DEFAULT_VF_VSI_NUM *
331                 sizeof(struct i40e_virtchnl_vsi_resource);
332
333         vf_res = rte_zmalloc("i40e_vf_res", len, 0);
334         if (vf_res == NULL) {
335                 PMD_DRV_LOG(ERR, "failed to allocate mem");
336                 ret = I40E_ERR_NO_MEMORY;
337                 vf_res = NULL;
338                 len = 0;
339                 goto send_msg;
340         }
341
342         vf_res->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
343                                 I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
344         vf_res->max_vectors = hw->func_caps.num_msix_vectors_vf;
345         vf_res->num_queue_pairs = vf->vsi->nb_qps;
346         vf_res->num_vsis = I40E_DEFAULT_VF_VSI_NUM;
347
348         /* Change below setting if PF host can support more VSIs for VF */
349         vf_res->vsi_res[0].vsi_type = I40E_VSI_SRIOV;
350         vf_res->vsi_res[0].vsi_id = vf->vsi->vsi_id;
351         vf_res->vsi_res[0].num_queue_pairs = vf->vsi->nb_qps;
352         ether_addr_copy(&vf->mac_addr,
353                 (struct ether_addr *)vf_res->vsi_res[0].default_mac_addr);
354
355 send_msg:
356         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
357                                         ret, (uint8_t *)vf_res, len);
358         rte_free(vf_res);
359
360         return ret;
361 }
362
363 static int
364 i40e_pf_host_hmc_config_rxq(struct i40e_hw *hw,
365                             struct i40e_pf_vf *vf,
366                             struct i40e_virtchnl_rxq_info *rxq,
367                             uint8_t crcstrip)
368 {
369         int err = I40E_SUCCESS;
370         struct i40e_hmc_obj_rxq rx_ctx;
371         uint16_t abs_queue_id = vf->vsi->base_queue + rxq->queue_id;
372
373         /* Clear the context structure first */
374         memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
375         rx_ctx.dbuff = rxq->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
376         rx_ctx.hbuff = rxq->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
377         rx_ctx.base = rxq->dma_ring_addr / I40E_QUEUE_BASE_ADDR_UNIT;
378         rx_ctx.qlen = rxq->ring_len;
379 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
380         rx_ctx.dsize = 1;
381 #endif
382
383         if (rxq->splithdr_enabled) {
384                 rx_ctx.hsplit_0 = I40E_HEADER_SPLIT_ALL;
385                 rx_ctx.dtype = i40e_header_split_enabled;
386         } else {
387                 rx_ctx.hsplit_0 = I40E_HEADER_SPLIT_NONE;
388                 rx_ctx.dtype = i40e_header_split_none;
389         }
390         rx_ctx.rxmax = rxq->max_pkt_size;
391         rx_ctx.tphrdesc_ena = 1;
392         rx_ctx.tphwdesc_ena = 1;
393         rx_ctx.tphdata_ena = 1;
394         rx_ctx.tphhead_ena = 1;
395         rx_ctx.lrxqthresh = 2;
396         rx_ctx.crcstrip = crcstrip;
397         rx_ctx.l2tsel = 1;
398         rx_ctx.prefena = 1;
399
400         err = i40e_clear_lan_rx_queue_context(hw, abs_queue_id);
401         if (err != I40E_SUCCESS)
402                 return err;
403         err = i40e_set_lan_rx_queue_context(hw, abs_queue_id, &rx_ctx);
404
405         return err;
406 }
407
408 static int
409 i40e_pf_host_hmc_config_txq(struct i40e_hw *hw,
410                             struct i40e_pf_vf *vf,
411                             struct i40e_virtchnl_txq_info *txq)
412 {
413         int err = I40E_SUCCESS;
414         struct i40e_hmc_obj_txq tx_ctx;
415         uint32_t qtx_ctl;
416         uint16_t abs_queue_id = vf->vsi->base_queue + txq->queue_id;
417
418
419         /* clear the context structure first */
420         memset(&tx_ctx, 0, sizeof(tx_ctx));
421         tx_ctx.base = txq->dma_ring_addr / I40E_QUEUE_BASE_ADDR_UNIT;
422         tx_ctx.qlen = txq->ring_len;
423         tx_ctx.rdylist = rte_le_to_cpu_16(vf->vsi->info.qs_handle[0]);
424         tx_ctx.head_wb_ena = txq->headwb_enabled;
425         tx_ctx.head_wb_addr = txq->dma_headwb_addr;
426
427         err = i40e_clear_lan_tx_queue_context(hw, abs_queue_id);
428         if (err != I40E_SUCCESS)
429                 return err;
430
431         err = i40e_set_lan_tx_queue_context(hw, abs_queue_id, &tx_ctx);
432         if (err != I40E_SUCCESS)
433                 return err;
434
435         /* bind queue with VF function, since TX/QX will appear in pair,
436          * so only has QTX_CTL to set.
437          */
438         qtx_ctl = (I40E_QTX_CTL_VF_QUEUE << I40E_QTX_CTL_PFVF_Q_SHIFT) |
439                                 ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
440                                 I40E_QTX_CTL_PF_INDX_MASK) |
441                                 (((vf->vf_idx + hw->func_caps.vf_base_id) <<
442                                 I40E_QTX_CTL_VFVM_INDX_SHIFT) &
443                                 I40E_QTX_CTL_VFVM_INDX_MASK);
444         I40E_WRITE_REG(hw, I40E_QTX_CTL(abs_queue_id), qtx_ctl);
445         I40E_WRITE_FLUSH(hw);
446
447         return I40E_SUCCESS;
448 }
449
450 static int
451 i40e_pf_host_process_cmd_config_vsi_queues(struct i40e_pf_vf *vf,
452                                            uint8_t *msg,
453                                            uint16_t msglen,
454                                            bool b_op)
455 {
456         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
457         struct i40e_vsi *vsi = vf->vsi;
458         struct i40e_virtchnl_vsi_queue_config_info *vc_vqci =
459                 (struct i40e_virtchnl_vsi_queue_config_info *)msg;
460         struct i40e_virtchnl_queue_pair_info *vc_qpi;
461         int i, ret = I40E_SUCCESS;
462
463         if (!b_op) {
464                 i40e_pf_host_send_msg_to_vf(vf,
465                                             I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
466                                             I40E_NOT_SUPPORTED, NULL, 0);
467                 return ret;
468         }
469
470         if (!msg || vc_vqci->num_queue_pairs > vsi->nb_qps ||
471                 vc_vqci->num_queue_pairs > I40E_MAX_VSI_QP ||
472                 msglen < I40E_VIRTCHNL_CONFIG_VSI_QUEUES_SIZE(vc_vqci,
473                                         vc_vqci->num_queue_pairs)) {
474                 PMD_DRV_LOG(ERR, "vsi_queue_config_info argument wrong\n");
475                 ret = I40E_ERR_PARAM;
476                 goto send_msg;
477         }
478
479         vc_qpi = vc_vqci->qpair;
480         for (i = 0; i < vc_vqci->num_queue_pairs; i++) {
481                 if (vc_qpi[i].rxq.queue_id > vsi->nb_qps - 1 ||
482                         vc_qpi[i].txq.queue_id > vsi->nb_qps - 1) {
483                         ret = I40E_ERR_PARAM;
484                         goto send_msg;
485                 }
486
487                 /*
488                  * Apply VF RX queue setting to HMC.
489                  * If the opcode is I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT,
490                  * then the extra information of
491                  * 'struct i40e_virtchnl_queue_pair_extra_info' is needed,
492                  * otherwise set the last parameter to NULL.
493                  */
494                 if (i40e_pf_host_hmc_config_rxq(hw, vf, &vc_qpi[i].rxq,
495                         I40E_CFG_CRCSTRIP_DEFAULT) != I40E_SUCCESS) {
496                         PMD_DRV_LOG(ERR, "Configure RX queue HMC failed");
497                         ret = I40E_ERR_PARAM;
498                         goto send_msg;
499                 }
500
501                 /* Apply VF TX queue setting to HMC */
502                 if (i40e_pf_host_hmc_config_txq(hw, vf,
503                         &vc_qpi[i].txq) != I40E_SUCCESS) {
504                         PMD_DRV_LOG(ERR, "Configure TX queue HMC failed");
505                         ret = I40E_ERR_PARAM;
506                         goto send_msg;
507                 }
508         }
509
510 send_msg:
511         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
512                                                         ret, NULL, 0);
513
514         return ret;
515 }
516
517 static int
518 i40e_pf_host_process_cmd_config_vsi_queues_ext(struct i40e_pf_vf *vf,
519                                                uint8_t *msg,
520                                                uint16_t msglen,
521                                                bool b_op)
522 {
523         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
524         struct i40e_vsi *vsi = vf->vsi;
525         struct i40e_virtchnl_vsi_queue_config_ext_info *vc_vqcei =
526                 (struct i40e_virtchnl_vsi_queue_config_ext_info *)msg;
527         struct i40e_virtchnl_queue_pair_ext_info *vc_qpei;
528         int i, ret = I40E_SUCCESS;
529
530         if (!b_op) {
531                 i40e_pf_host_send_msg_to_vf(
532                         vf,
533                         I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT,
534                         I40E_NOT_SUPPORTED, NULL, 0);
535                 return ret;
536         }
537
538         if (!msg || vc_vqcei->num_queue_pairs > vsi->nb_qps ||
539                 vc_vqcei->num_queue_pairs > I40E_MAX_VSI_QP ||
540                 msglen < I40E_VIRTCHNL_CONFIG_VSI_QUEUES_SIZE(vc_vqcei,
541                                         vc_vqcei->num_queue_pairs)) {
542                 PMD_DRV_LOG(ERR, "vsi_queue_config_ext_info argument wrong\n");
543                 ret = I40E_ERR_PARAM;
544                 goto send_msg;
545         }
546
547         vc_qpei = vc_vqcei->qpair;
548         for (i = 0; i < vc_vqcei->num_queue_pairs; i++) {
549                 if (vc_qpei[i].rxq.queue_id > vsi->nb_qps - 1 ||
550                         vc_qpei[i].txq.queue_id > vsi->nb_qps - 1) {
551                         ret = I40E_ERR_PARAM;
552                         goto send_msg;
553                 }
554                 /*
555                  * Apply VF RX queue setting to HMC.
556                  * If the opcode is I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT,
557                  * then the extra information of
558                  * 'struct i40e_virtchnl_queue_pair_ext_info' is needed,
559                  * otherwise set the last parameter to NULL.
560                  */
561                 if (i40e_pf_host_hmc_config_rxq(hw, vf, &vc_qpei[i].rxq,
562                         vc_qpei[i].rxq_ext.crcstrip) != I40E_SUCCESS) {
563                         PMD_DRV_LOG(ERR, "Configure RX queue HMC failed");
564                         ret = I40E_ERR_PARAM;
565                         goto send_msg;
566                 }
567
568                 /* Apply VF TX queue setting to HMC */
569                 if (i40e_pf_host_hmc_config_txq(hw, vf, &vc_qpei[i].txq) !=
570                                                         I40E_SUCCESS) {
571                         PMD_DRV_LOG(ERR, "Configure TX queue HMC failed");
572                         ret = I40E_ERR_PARAM;
573                         goto send_msg;
574                 }
575         }
576
577 send_msg:
578         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT,
579                                                                 ret, NULL, 0);
580
581         return ret;
582 }
583
584 static int
585 i40e_pf_host_process_cmd_config_irq_map(struct i40e_pf_vf *vf,
586                                         uint8_t *msg, uint16_t msglen,
587                                         bool b_op)
588 {
589         int ret = I40E_SUCCESS;
590         struct i40e_virtchnl_irq_map_info *irqmap =
591             (struct i40e_virtchnl_irq_map_info *)msg;
592
593         if (!b_op) {
594                 i40e_pf_host_send_msg_to_vf(
595                         vf,
596                         I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
597                         I40E_NOT_SUPPORTED, NULL, 0);
598                 return ret;
599         }
600
601         if (msg == NULL || msglen < sizeof(struct i40e_virtchnl_irq_map_info)) {
602                 PMD_DRV_LOG(ERR, "buffer too short");
603                 ret = I40E_ERR_PARAM;
604                 goto send_msg;
605         }
606
607         /* Assume VF only have 1 vector to bind all queues */
608         if (irqmap->num_vectors != 1) {
609                 PMD_DRV_LOG(ERR, "DKDK host only support 1 vector");
610                 ret = I40E_ERR_PARAM;
611                 goto send_msg;
612         }
613
614         /* This MSIX intr store the intr in VF range */
615         vf->vsi->msix_intr = irqmap->vecmap[0].vector_id;
616         vf->vsi->nb_msix = irqmap->num_vectors;
617         vf->vsi->nb_used_qps = vf->vsi->nb_qps;
618
619         /* Don't care how the TX/RX queue mapping with this vector.
620          * Link all VF RX queues together. Only did mapping work.
621          * VF can disable/enable the intr by itself.
622          */
623         i40e_vsi_queues_bind_intr(vf->vsi);
624 send_msg:
625         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
626                                                         ret, NULL, 0);
627
628         return ret;
629 }
630
631 static int
632 i40e_pf_host_switch_queues(struct i40e_pf_vf *vf,
633                            struct i40e_virtchnl_queue_select *qsel,
634                            bool on)
635 {
636         int ret = I40E_SUCCESS;
637         int i;
638         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
639         uint16_t baseq = vf->vsi->base_queue;
640
641         if (qsel->rx_queues + qsel->tx_queues == 0)
642                 return I40E_ERR_PARAM;
643
644         /* always enable RX first and disable last */
645         /* Enable RX if it's enable */
646         if (on) {
647                 for (i = 0; i < I40E_MAX_QP_NUM_PER_VF; i++)
648                         if (qsel->rx_queues & (1 << i)) {
649                                 ret = i40e_switch_rx_queue(hw, baseq + i, on);
650                                 if (ret != I40E_SUCCESS)
651                                         return ret;
652                         }
653         }
654
655         /* Enable/Disable TX */
656         for (i = 0; i < I40E_MAX_QP_NUM_PER_VF; i++)
657                 if (qsel->tx_queues & (1 << i)) {
658                         ret = i40e_switch_tx_queue(hw, baseq + i, on);
659                         if (ret != I40E_SUCCESS)
660                                 return ret;
661                 }
662
663         /* disable RX last if it's disable */
664         if (!on) {
665                 /* disable RX */
666                 for (i = 0; i < I40E_MAX_QP_NUM_PER_VF; i++)
667                         if (qsel->rx_queues & (1 << i)) {
668                                 ret = i40e_switch_rx_queue(hw, baseq + i, on);
669                                 if (ret != I40E_SUCCESS)
670                                         return ret;
671                         }
672         }
673
674         return ret;
675 }
676
677 static int
678 i40e_pf_host_process_cmd_enable_queues(struct i40e_pf_vf *vf,
679                                        uint8_t *msg,
680                                        uint16_t msglen)
681 {
682         int ret = I40E_SUCCESS;
683         struct i40e_virtchnl_queue_select *q_sel =
684                 (struct i40e_virtchnl_queue_select *)msg;
685
686         if (msg == NULL || msglen != sizeof(*q_sel)) {
687                 ret = I40E_ERR_PARAM;
688                 goto send_msg;
689         }
690         ret = i40e_pf_host_switch_queues(vf, q_sel, true);
691
692 send_msg:
693         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
694                                                         ret, NULL, 0);
695
696         return ret;
697 }
698
699 static int
700 i40e_pf_host_process_cmd_disable_queues(struct i40e_pf_vf *vf,
701                                         uint8_t *msg,
702                                         uint16_t msglen,
703                                         bool b_op)
704 {
705         int ret = I40E_SUCCESS;
706         struct i40e_virtchnl_queue_select *q_sel =
707                 (struct i40e_virtchnl_queue_select *)msg;
708
709         if (!b_op) {
710                 i40e_pf_host_send_msg_to_vf(
711                         vf,
712                         I40E_VIRTCHNL_OP_DISABLE_QUEUES,
713                         I40E_NOT_SUPPORTED, NULL, 0);
714                 return ret;
715         }
716
717         if (msg == NULL || msglen != sizeof(*q_sel)) {
718                 ret = I40E_ERR_PARAM;
719                 goto send_msg;
720         }
721         ret = i40e_pf_host_switch_queues(vf, q_sel, false);
722
723 send_msg:
724         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
725                                                         ret, NULL, 0);
726
727         return ret;
728 }
729
730
731 static int
732 i40e_pf_host_process_cmd_add_ether_address(struct i40e_pf_vf *vf,
733                                            uint8_t *msg,
734                                            uint16_t msglen,
735                                            bool b_op)
736 {
737         int ret = I40E_SUCCESS;
738         struct i40e_virtchnl_ether_addr_list *addr_list =
739                         (struct i40e_virtchnl_ether_addr_list *)msg;
740         struct i40e_mac_filter_info filter;
741         int i;
742         struct ether_addr *mac;
743
744         if (!b_op) {
745                 i40e_pf_host_send_msg_to_vf(
746                         vf,
747                         I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
748                         I40E_NOT_SUPPORTED, NULL, 0);
749                 return ret;
750         }
751
752         memset(&filter, 0 , sizeof(struct i40e_mac_filter_info));
753
754         if (msg == NULL || msglen <= sizeof(*addr_list)) {
755                 PMD_DRV_LOG(ERR, "add_ether_address argument too short");
756                 ret = I40E_ERR_PARAM;
757                 goto send_msg;
758         }
759
760         for (i = 0; i < addr_list->num_elements; i++) {
761                 mac = (struct ether_addr *)(addr_list->list[i].addr);
762                 (void)rte_memcpy(&filter.mac_addr, mac, ETHER_ADDR_LEN);
763                 filter.filter_type = RTE_MACVLAN_PERFECT_MATCH;
764                 if (is_zero_ether_addr(mac) ||
765                     i40e_vsi_add_mac(vf->vsi, &filter)) {
766                         ret = I40E_ERR_INVALID_MAC_ADDR;
767                         goto send_msg;
768                 }
769         }
770
771 send_msg:
772         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
773                                                         ret, NULL, 0);
774
775         return ret;
776 }
777
778 static int
779 i40e_pf_host_process_cmd_del_ether_address(struct i40e_pf_vf *vf,
780                                            uint8_t *msg,
781                                            uint16_t msglen,
782                                            bool b_op)
783 {
784         int ret = I40E_SUCCESS;
785         struct i40e_virtchnl_ether_addr_list *addr_list =
786                 (struct i40e_virtchnl_ether_addr_list *)msg;
787         int i;
788         struct ether_addr *mac;
789
790         if (!b_op) {
791                 i40e_pf_host_send_msg_to_vf(
792                         vf,
793                         I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
794                         I40E_NOT_SUPPORTED, NULL, 0);
795                 return ret;
796         }
797
798         if (msg == NULL || msglen <= sizeof(*addr_list)) {
799                 PMD_DRV_LOG(ERR, "delete_ether_address argument too short");
800                 ret = I40E_ERR_PARAM;
801                 goto send_msg;
802         }
803
804         for (i = 0; i < addr_list->num_elements; i++) {
805                 mac = (struct ether_addr *)(addr_list->list[i].addr);
806                 if(!is_valid_assigned_ether_addr(mac) ||
807                         i40e_vsi_delete_mac(vf->vsi, mac)) {
808                         ret = I40E_ERR_INVALID_MAC_ADDR;
809                         goto send_msg;
810                 }
811         }
812
813 send_msg:
814         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
815                                                         ret, NULL, 0);
816
817         return ret;
818 }
819
820 static int
821 i40e_pf_host_process_cmd_add_vlan(struct i40e_pf_vf *vf,
822                                 uint8_t *msg, uint16_t msglen,
823                                 bool b_op)
824 {
825         int ret = I40E_SUCCESS;
826         struct i40e_virtchnl_vlan_filter_list *vlan_filter_list =
827                 (struct i40e_virtchnl_vlan_filter_list *)msg;
828         int i;
829         uint16_t *vid;
830
831         if (!b_op) {
832                 i40e_pf_host_send_msg_to_vf(
833                         vf,
834                         I40E_VIRTCHNL_OP_ADD_VLAN,
835                         I40E_NOT_SUPPORTED, NULL, 0);
836                 return ret;
837         }
838
839         if (msg == NULL || msglen <= sizeof(*vlan_filter_list)) {
840                 PMD_DRV_LOG(ERR, "add_vlan argument too short");
841                 ret = I40E_ERR_PARAM;
842                 goto send_msg;
843         }
844
845         vid = vlan_filter_list->vlan_id;
846
847         for (i = 0; i < vlan_filter_list->num_elements; i++) {
848                 ret = i40e_vsi_add_vlan(vf->vsi, vid[i]);
849                 if(ret != I40E_SUCCESS)
850                         goto send_msg;
851         }
852
853 send_msg:
854         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN,
855                                                 ret, NULL, 0);
856
857         return ret;
858 }
859
860 static int
861 i40e_pf_host_process_cmd_del_vlan(struct i40e_pf_vf *vf,
862                                   uint8_t *msg,
863                                   uint16_t msglen,
864                                   bool b_op)
865 {
866         int ret = I40E_SUCCESS;
867         struct i40e_virtchnl_vlan_filter_list *vlan_filter_list =
868                         (struct i40e_virtchnl_vlan_filter_list *)msg;
869         int i;
870         uint16_t *vid;
871
872         if (!b_op) {
873                 i40e_pf_host_send_msg_to_vf(
874                         vf,
875                         I40E_VIRTCHNL_OP_DEL_VLAN,
876                         I40E_NOT_SUPPORTED, NULL, 0);
877                 return ret;
878         }
879
880         if (msg == NULL || msglen <= sizeof(*vlan_filter_list)) {
881                 PMD_DRV_LOG(ERR, "delete_vlan argument too short");
882                 ret = I40E_ERR_PARAM;
883                 goto send_msg;
884         }
885
886         vid = vlan_filter_list->vlan_id;
887         for (i = 0; i < vlan_filter_list->num_elements; i++) {
888                 ret = i40e_vsi_delete_vlan(vf->vsi, vid[i]);
889                 if(ret != I40E_SUCCESS)
890                         goto send_msg;
891         }
892
893 send_msg:
894         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN,
895                                                 ret, NULL, 0);
896
897         return ret;
898 }
899
900 static int
901 i40e_pf_host_process_cmd_config_promisc_mode(
902                                         struct i40e_pf_vf *vf,
903                                         uint8_t *msg,
904                                         uint16_t msglen,
905                                         bool b_op)
906 {
907         int ret = I40E_SUCCESS;
908         struct i40e_virtchnl_promisc_info *promisc =
909                                 (struct i40e_virtchnl_promisc_info *)msg;
910         struct i40e_hw *hw = I40E_PF_TO_HW(vf->pf);
911         bool unicast = FALSE, multicast = FALSE;
912
913         if (!b_op) {
914                 i40e_pf_host_send_msg_to_vf(
915                         vf,
916                         I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
917                         I40E_NOT_SUPPORTED, NULL, 0);
918                 return ret;
919         }
920
921         if (msg == NULL || msglen != sizeof(*promisc)) {
922                 ret = I40E_ERR_PARAM;
923                 goto send_msg;
924         }
925
926         if (promisc->flags & I40E_FLAG_VF_UNICAST_PROMISC)
927                 unicast = TRUE;
928         ret = i40e_aq_set_vsi_unicast_promiscuous(hw,
929                         vf->vsi->seid, unicast, NULL, true);
930         if (ret != I40E_SUCCESS)
931                 goto send_msg;
932
933         if (promisc->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
934                 multicast = TRUE;
935         ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vf->vsi->seid,
936                                                 multicast, NULL);
937
938 send_msg:
939         i40e_pf_host_send_msg_to_vf(vf,
940                 I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE, ret, NULL, 0);
941
942         return ret;
943 }
944
945 static int
946 i40e_pf_host_process_cmd_get_stats(struct i40e_pf_vf *vf, bool b_op)
947 {
948         i40e_update_vsi_stats(vf->vsi);
949
950         if (b_op)
951                 i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS,
952                                             I40E_SUCCESS,
953                                             (uint8_t *)&vf->vsi->eth_stats,
954                                             sizeof(vf->vsi->eth_stats));
955         else
956                 i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS,
957                                             I40E_NOT_SUPPORTED,
958                                             (uint8_t *)&vf->vsi->eth_stats,
959                                             sizeof(vf->vsi->eth_stats));
960
961         return I40E_SUCCESS;
962 }
963
964 static int
965 i40e_pf_host_process_cmd_cfg_vlan_offload(
966                                         struct i40e_pf_vf *vf,
967                                         uint8_t *msg,
968                                         uint16_t msglen,
969                                         bool b_op)
970 {
971         int ret = I40E_SUCCESS;
972         struct i40e_virtchnl_vlan_offload_info *offload =
973                         (struct i40e_virtchnl_vlan_offload_info *)msg;
974
975         if (!b_op) {
976                 i40e_pf_host_send_msg_to_vf(
977                         vf,
978                         I40E_VIRTCHNL_OP_CFG_VLAN_OFFLOAD,
979                         I40E_NOT_SUPPORTED, NULL, 0);
980                 return ret;
981         }
982
983         if (msg == NULL || msglen != sizeof(*offload)) {
984                 ret = I40E_ERR_PARAM;
985                 goto send_msg;
986         }
987
988         ret = i40e_vsi_config_vlan_stripping(vf->vsi,
989                                                 !!offload->enable_vlan_strip);
990         if (ret != 0)
991                 PMD_DRV_LOG(ERR, "Failed to configure vlan stripping");
992
993 send_msg:
994         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_CFG_VLAN_OFFLOAD,
995                                         ret, NULL, 0);
996
997         return ret;
998 }
999
1000 static int
1001 i40e_pf_host_process_cmd_cfg_pvid(struct i40e_pf_vf *vf,
1002                                         uint8_t *msg,
1003                                         uint16_t msglen,
1004                                         bool b_op)
1005 {
1006         int ret = I40E_SUCCESS;
1007         struct i40e_virtchnl_pvid_info  *tpid_info =
1008                         (struct i40e_virtchnl_pvid_info *)msg;
1009
1010         if (!b_op) {
1011                 i40e_pf_host_send_msg_to_vf(
1012                         vf,
1013                         I40E_VIRTCHNL_OP_CFG_VLAN_PVID,
1014                         I40E_NOT_SUPPORTED, NULL, 0);
1015                 return ret;
1016         }
1017
1018         if (msg == NULL || msglen != sizeof(*tpid_info)) {
1019                 ret = I40E_ERR_PARAM;
1020                 goto send_msg;
1021         }
1022
1023         ret = i40e_vsi_vlan_pvid_set(vf->vsi, &tpid_info->info);
1024
1025 send_msg:
1026         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_CFG_VLAN_PVID,
1027                                         ret, NULL, 0);
1028
1029         return ret;
1030 }
1031
1032 void
1033 i40e_notify_vf_link_status(struct rte_eth_dev *dev, struct i40e_pf_vf *vf)
1034 {
1035         struct i40e_virtchnl_pf_event event;
1036
1037         event.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
1038         event.event_data.link_event.link_status =
1039                 dev->data->dev_link.link_status;
1040         event.event_data.link_event.link_speed =
1041                 (enum i40e_aq_link_speed)dev->data->dev_link.link_speed;
1042         i40e_pf_host_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_EVENT,
1043                 I40E_SUCCESS, (uint8_t *)&event, sizeof(event));
1044 }
1045
1046 void
1047 i40e_pf_host_handle_vf_msg(struct rte_eth_dev *dev,
1048                            uint16_t abs_vf_id, uint32_t opcode,
1049                            __rte_unused uint32_t retval,
1050                            uint8_t *msg,
1051                            uint16_t msglen)
1052 {
1053         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1054         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1055         struct i40e_pf_vf *vf;
1056         /* AdminQ will pass absolute VF id, transfer to internal vf id */
1057         uint16_t vf_id = abs_vf_id - hw->func_caps.vf_base_id;
1058         struct rte_pmd_i40e_mb_event_param cb_param;
1059         bool b_op = TRUE;
1060
1061         if (vf_id > pf->vf_num - 1 || !pf->vfs) {
1062                 PMD_DRV_LOG(ERR, "invalid argument");
1063                 return;
1064         }
1065
1066         vf = &pf->vfs[vf_id];
1067         if (!vf->vsi) {
1068                 PMD_DRV_LOG(ERR, "NO VSI associated with VF found");
1069                 i40e_pf_host_send_msg_to_vf(vf, opcode,
1070                         I40E_ERR_NO_AVAILABLE_VSI, NULL, 0);
1071                 return;
1072         }
1073
1074         /**
1075          * initialise structure to send to user application
1076          * will return response from user in retval field
1077          */
1078         cb_param.retval = RTE_PMD_I40E_MB_EVENT_PROCEED;
1079         cb_param.vfid = vf_id;
1080         cb_param.msg_type = opcode;
1081         cb_param.msg = (void *)msg;
1082         cb_param.msglen = msglen;
1083
1084         /**
1085          * Ask user application if we're allowed to perform those functions.
1086          * If we get cb_param.retval == RTE_PMD_I40E_MB_EVENT_PROCEED,
1087          * then business as usual.
1088          * If RTE_PMD_I40E_MB_EVENT_NOOP_ACK or RTE_PMD_I40E_MB_EVENT_NOOP_NACK,
1089          * do nothing and send not_supported to VF. As PF must send a response
1090          * to VF and ACK/NACK is not defined.
1091          */
1092         _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_VF_MBOX, &cb_param);
1093         if (cb_param.retval != RTE_PMD_I40E_MB_EVENT_PROCEED) {
1094                 PMD_DRV_LOG(WARNING, "VF to PF message(%d) is not permitted!",
1095                             opcode);
1096                 b_op = FALSE;
1097         }
1098
1099         switch (opcode) {
1100         case I40E_VIRTCHNL_OP_VERSION :
1101                 PMD_DRV_LOG(INFO, "OP_VERSION received");
1102                 i40e_pf_host_process_cmd_version(vf, b_op);
1103                 break;
1104         case I40E_VIRTCHNL_OP_RESET_VF :
1105                 PMD_DRV_LOG(INFO, "OP_RESET_VF received");
1106                 i40e_pf_host_process_cmd_reset_vf(vf);
1107                 break;
1108         case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
1109                 PMD_DRV_LOG(INFO, "OP_GET_VF_RESOURCES received");
1110                 i40e_pf_host_process_cmd_get_vf_resource(vf, b_op);
1111                 break;
1112         case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
1113                 PMD_DRV_LOG(INFO, "OP_CONFIG_VSI_QUEUES received");
1114                 i40e_pf_host_process_cmd_config_vsi_queues(vf, msg,
1115                                                            msglen, b_op);
1116                 break;
1117         case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT:
1118                 PMD_DRV_LOG(INFO, "OP_CONFIG_VSI_QUEUES_EXT received");
1119                 i40e_pf_host_process_cmd_config_vsi_queues_ext(vf, msg,
1120                                                                msglen, b_op);
1121                 break;
1122         case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
1123                 PMD_DRV_LOG(INFO, "OP_CONFIG_IRQ_MAP received");
1124                 i40e_pf_host_process_cmd_config_irq_map(vf, msg, msglen, b_op);
1125                 break;
1126         case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
1127                 PMD_DRV_LOG(INFO, "OP_ENABLE_QUEUES received");
1128                 if (b_op) {
1129                         i40e_pf_host_process_cmd_enable_queues(vf, msg, msglen);
1130                         i40e_notify_vf_link_status(dev, vf);
1131                 } else {
1132                         i40e_pf_host_send_msg_to_vf(
1133                                 vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
1134                                 I40E_NOT_SUPPORTED, NULL, 0);
1135                 }
1136                 break;
1137         case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
1138                 PMD_DRV_LOG(INFO, "OP_DISABLE_QUEUE received");
1139                 i40e_pf_host_process_cmd_disable_queues(vf, msg, msglen, b_op);
1140                 break;
1141         case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
1142                 PMD_DRV_LOG(INFO, "OP_ADD_ETHER_ADDRESS received");
1143                 i40e_pf_host_process_cmd_add_ether_address(vf, msg,
1144                                                            msglen, b_op);
1145                 break;
1146         case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
1147                 PMD_DRV_LOG(INFO, "OP_DEL_ETHER_ADDRESS received");
1148                 i40e_pf_host_process_cmd_del_ether_address(vf, msg,
1149                                                            msglen, b_op);
1150                 break;
1151         case I40E_VIRTCHNL_OP_ADD_VLAN:
1152                 PMD_DRV_LOG(INFO, "OP_ADD_VLAN received");
1153                 i40e_pf_host_process_cmd_add_vlan(vf, msg, msglen, b_op);
1154                 break;
1155         case I40E_VIRTCHNL_OP_DEL_VLAN:
1156                 PMD_DRV_LOG(INFO, "OP_DEL_VLAN received");
1157                 i40e_pf_host_process_cmd_del_vlan(vf, msg, msglen, b_op);
1158                 break;
1159         case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
1160                 PMD_DRV_LOG(INFO, "OP_CONFIG_PROMISCUOUS_MODE received");
1161                 i40e_pf_host_process_cmd_config_promisc_mode(vf, msg,
1162                                                              msglen, b_op);
1163                 break;
1164         case I40E_VIRTCHNL_OP_GET_STATS:
1165                 PMD_DRV_LOG(INFO, "OP_GET_STATS received");
1166                 i40e_pf_host_process_cmd_get_stats(vf, b_op);
1167                 break;
1168         case I40E_VIRTCHNL_OP_CFG_VLAN_OFFLOAD:
1169                 PMD_DRV_LOG(INFO, "OP_CFG_VLAN_OFFLOAD received");
1170                 i40e_pf_host_process_cmd_cfg_vlan_offload(vf, msg,
1171                                                           msglen, b_op);
1172                 break;
1173         case I40E_VIRTCHNL_OP_CFG_VLAN_PVID:
1174                 PMD_DRV_LOG(INFO, "OP_CFG_VLAN_PVID received");
1175                 i40e_pf_host_process_cmd_cfg_pvid(vf, msg, msglen, b_op);
1176                 break;
1177         /* Don't add command supported below, which will
1178          * return an error code.
1179          */
1180         default:
1181                 PMD_DRV_LOG(ERR, "%u received, not supported", opcode);
1182                 i40e_pf_host_send_msg_to_vf(vf, opcode, I40E_ERR_PARAM,
1183                                                                 NULL, 0);
1184                 break;
1185         }
1186 }
1187
1188 int
1189 i40e_pf_host_init(struct rte_eth_dev *dev)
1190 {
1191         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1192         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
1193         int ret, i;
1194         uint32_t val;
1195
1196         PMD_INIT_FUNC_TRACE();
1197
1198         /**
1199          * return if SRIOV not enabled, VF number not configured or
1200          * no queue assigned.
1201          */
1202         if(!hw->func_caps.sr_iov_1_1 || pf->vf_num == 0 || pf->vf_nb_qps == 0)
1203                 return I40E_SUCCESS;
1204
1205         /* Allocate memory to store VF structure */
1206         pf->vfs = rte_zmalloc("i40e_pf_vf",sizeof(*pf->vfs) * pf->vf_num, 0);
1207         if(pf->vfs == NULL)
1208                 return -ENOMEM;
1209
1210         /* Disable irq0 for VFR event */
1211         i40e_pf_disable_irq0(hw);
1212
1213         /* Disable VF link status interrupt */
1214         val = I40E_READ_REG(hw, I40E_PFGEN_PORTMDIO_NUM);
1215         val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
1216         I40E_WRITE_REG(hw, I40E_PFGEN_PORTMDIO_NUM, val);
1217         I40E_WRITE_FLUSH(hw);
1218
1219         for (i = 0; i < pf->vf_num; i++) {
1220                 pf->vfs[i].pf = pf;
1221                 pf->vfs[i].state = I40E_VF_INACTIVE;
1222                 pf->vfs[i].vf_idx = i;
1223                 ret = i40e_pf_host_vf_reset(&pf->vfs[i], 0);
1224                 if (ret != I40E_SUCCESS)
1225                         goto fail;
1226                 eth_random_addr(pf->vfs[i].mac_addr.addr_bytes);
1227         }
1228
1229         /* restore irq0 */
1230         i40e_pf_enable_irq0(hw);
1231
1232         return I40E_SUCCESS;
1233
1234 fail:
1235         rte_free(pf->vfs);
1236         i40e_pf_enable_irq0(hw);
1237
1238         return ret;
1239 }
1240
1241 int
1242 i40e_pf_host_uninit(struct rte_eth_dev *dev)
1243 {
1244         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1245         struct i40e_hw *hw = I40E_PF_TO_HW(pf);
1246         uint32_t val;
1247
1248         PMD_INIT_FUNC_TRACE();
1249
1250         /**
1251          * return if SRIOV not enabled, VF number not configured or
1252          * no queue assigned.
1253          */
1254         if ((!hw->func_caps.sr_iov_1_1) ||
1255                 (pf->vf_num == 0) ||
1256                 (pf->vf_nb_qps == 0))
1257                 return I40E_SUCCESS;
1258
1259         /* free memory to store VF structure */
1260         rte_free(pf->vfs);
1261         pf->vfs = NULL;
1262
1263         /* Disable irq0 for VFR event */
1264         i40e_pf_disable_irq0(hw);
1265
1266         /* Disable VF link status interrupt */
1267         val = I40E_READ_REG(hw, I40E_PFGEN_PORTMDIO_NUM);
1268         val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
1269         I40E_WRITE_REG(hw, I40E_PFGEN_PORTMDIO_NUM, val);
1270         I40E_WRITE_FLUSH(hw);
1271
1272         return I40E_SUCCESS;
1273 }