i40e: clean log messages
[dpdk.git] / lib / librte_pmd_i40e / i40e_ethdev_vf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <rte_byteorder.h>
43 #include <rte_common.h>
44 #include <rte_cycles.h>
45
46 #include <rte_interrupts.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_pci.h>
50 #include <rte_atomic.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_memory.h>
53 #include <rte_memzone.h>
54 #include <rte_tailq.h>
55 #include <rte_eal.h>
56 #include <rte_alarm.h>
57 #include <rte_ether.h>
58 #include <rte_ethdev.h>
59 #include <rte_atomic.h>
60 #include <rte_malloc.h>
61 #include <rte_dev.h>
62
63 #include "i40e_logs.h"
64 #include "i40e/i40e_prototype.h"
65 #include "i40e/i40e_adminq_cmd.h"
66 #include "i40e/i40e_type.h"
67
68 #include "i40e_rxtx.h"
69 #include "i40e_ethdev.h"
70 #include "i40e_pf.h"
71 #define I40EVF_VSI_DEFAULT_MSIX_INTR 1
72
73 /* busy wait delay in msec */
74 #define I40EVF_BUSY_WAIT_DELAY 10
75 #define I40EVF_BUSY_WAIT_COUNT 50
76 #define MAX_RESET_WAIT_CNT     20
77
78 struct i40evf_arq_msg_info {
79         enum i40e_virtchnl_ops ops;
80         enum i40e_status_code result;
81         uint16_t msg_len;
82         uint8_t *msg;
83 };
84
85 struct vf_cmd_info {
86         enum i40e_virtchnl_ops ops;
87         uint8_t *in_args;
88         uint32_t in_args_size;
89         uint8_t *out_buffer;
90         /* Input & output type. pass in buffer size and pass out
91          * actual return result
92          */
93         uint32_t out_size;
94 };
95
96 enum i40evf_aq_result {
97         I40EVF_MSG_ERR = -1, /* Meet error when accessing admin queue */
98         I40EVF_MSG_NON,      /* Read nothing from admin queue */
99         I40EVF_MSG_SYS,      /* Read system msg from admin queue */
100         I40EVF_MSG_CMD,      /* Read async command result */
101 };
102
103 /* A share buffer to store the command result from PF driver */
104 static uint8_t cmd_result_buffer[I40E_AQ_BUF_SZ];
105
106 static int i40evf_dev_configure(struct rte_eth_dev *dev);
107 static int i40evf_dev_start(struct rte_eth_dev *dev);
108 static void i40evf_dev_stop(struct rte_eth_dev *dev);
109 static void i40evf_dev_info_get(struct rte_eth_dev *dev,
110                                 struct rte_eth_dev_info *dev_info);
111 static int i40evf_dev_link_update(struct rte_eth_dev *dev,
112                                   __rte_unused int wait_to_complete);
113 static void i40evf_dev_stats_get(struct rte_eth_dev *dev,
114                                 struct rte_eth_stats *stats);
115 static int i40evf_vlan_filter_set(struct rte_eth_dev *dev,
116                                   uint16_t vlan_id, int on);
117 static void i40evf_vlan_offload_set(struct rte_eth_dev *dev, int mask);
118 static int i40evf_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid,
119                                 int on);
120 static void i40evf_dev_close(struct rte_eth_dev *dev);
121 static void i40evf_dev_promiscuous_enable(struct rte_eth_dev *dev);
122 static void i40evf_dev_promiscuous_disable(struct rte_eth_dev *dev);
123 static void i40evf_dev_allmulticast_enable(struct rte_eth_dev *dev);
124 static void i40evf_dev_allmulticast_disable(struct rte_eth_dev *dev);
125 static int i40evf_get_link_status(struct rte_eth_dev *dev,
126                                   struct rte_eth_link *link);
127 static int i40evf_init_vlan(struct rte_eth_dev *dev);
128 static int i40evf_dev_rx_queue_start(struct rte_eth_dev *, uint16_t);
129 static int i40evf_dev_rx_queue_stop(struct rte_eth_dev *, uint16_t);
130 static int i40evf_dev_tx_queue_start(struct rte_eth_dev *, uint16_t);
131 static int i40evf_dev_tx_queue_stop(struct rte_eth_dev *, uint16_t);
132 static struct eth_dev_ops i40evf_eth_dev_ops = {
133         .dev_configure        = i40evf_dev_configure,
134         .dev_start            = i40evf_dev_start,
135         .dev_stop             = i40evf_dev_stop,
136         .promiscuous_enable   = i40evf_dev_promiscuous_enable,
137         .promiscuous_disable  = i40evf_dev_promiscuous_disable,
138         .allmulticast_enable  = i40evf_dev_allmulticast_enable,
139         .allmulticast_disable = i40evf_dev_allmulticast_disable,
140         .link_update          = i40evf_dev_link_update,
141         .stats_get            = i40evf_dev_stats_get,
142         .dev_close            = i40evf_dev_close,
143         .dev_infos_get        = i40evf_dev_info_get,
144         .vlan_filter_set      = i40evf_vlan_filter_set,
145         .vlan_offload_set     = i40evf_vlan_offload_set,
146         .vlan_pvid_set        = i40evf_vlan_pvid_set,
147         .rx_queue_start       = i40evf_dev_rx_queue_start,
148         .rx_queue_stop        = i40evf_dev_rx_queue_stop,
149         .tx_queue_start       = i40evf_dev_tx_queue_start,
150         .tx_queue_stop        = i40evf_dev_tx_queue_stop,
151         .rx_queue_setup       = i40e_dev_rx_queue_setup,
152         .rx_queue_release     = i40e_dev_rx_queue_release,
153         .tx_queue_setup       = i40e_dev_tx_queue_setup,
154         .tx_queue_release     = i40e_dev_tx_queue_release,
155 };
156
157 static int
158 i40evf_set_mac_type(struct i40e_hw *hw)
159 {
160         int status = I40E_ERR_DEVICE_NOT_SUPPORTED;
161
162         if (hw->vendor_id == I40E_INTEL_VENDOR_ID) {
163                 switch (hw->device_id) {
164                 case I40E_DEV_ID_VF:
165                 case I40E_DEV_ID_VF_HV:
166                         hw->mac.type = I40E_MAC_VF;
167                         status = I40E_SUCCESS;
168                         break;
169                 default:
170                         ;
171                 }
172         }
173
174         return status;
175 }
176
177 /*
178  * Parse admin queue message.
179  *
180  * return value:
181  *  < 0: meet error
182  *  0: read sys msg
183  *  > 0: read cmd result
184  */
185 static enum i40evf_aq_result
186 i40evf_parse_pfmsg(struct i40e_vf *vf,
187                    struct i40e_arq_event_info *event,
188                    struct i40evf_arq_msg_info *data)
189 {
190         enum i40e_virtchnl_ops opcode = (enum i40e_virtchnl_ops)\
191                         rte_le_to_cpu_32(event->desc.cookie_high);
192         enum i40e_status_code retval = (enum i40e_status_code)\
193                         rte_le_to_cpu_32(event->desc.cookie_low);
194         enum i40evf_aq_result ret = I40EVF_MSG_CMD;
195
196         /* pf sys event */
197         if (opcode == I40E_VIRTCHNL_OP_EVENT) {
198                 struct i40e_virtchnl_pf_event *vpe =
199                         (struct i40e_virtchnl_pf_event *)event->msg_buf;
200
201                 /* Initialize ret to sys event */
202                 ret = I40EVF_MSG_SYS;
203                 switch (vpe->event) {
204                 case I40E_VIRTCHNL_EVENT_LINK_CHANGE:
205                         vf->link_up =
206                                 vpe->event_data.link_event.link_status;
207                         vf->pend_msg |= PFMSG_LINK_CHANGE;
208                         PMD_DRV_LOG(INFO, "Link status update:%s",
209                                     vf->link_up ? "up" : "down");
210                         break;
211                 case I40E_VIRTCHNL_EVENT_RESET_IMPENDING:
212                         vf->vf_reset = true;
213                         vf->pend_msg |= PFMSG_RESET_IMPENDING;
214                         PMD_DRV_LOG(INFO, "vf is reseting");
215                         break;
216                 case I40E_VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
217                         vf->dev_closed = true;
218                         vf->pend_msg |= PFMSG_DRIVER_CLOSE;
219                         PMD_DRV_LOG(INFO, "PF driver closed");
220                         break;
221                 default:
222                         PMD_DRV_LOG(ERR, "%s: Unknown event %d from pf",
223                                     __func__, vpe->event);
224                 }
225         } else {
226                 /* async reply msg on command issued by vf previously */
227                 ret = I40EVF_MSG_CMD;
228                 /* Actual buffer length read from PF */
229                 data->msg_len = event->msg_size;
230         }
231         /* fill the ops and result to notify VF */
232         data->result = retval;
233         data->ops = opcode;
234
235         return ret;
236 }
237
238 /*
239  * Read data in admin queue to get msg from pf driver
240  */
241 static enum i40evf_aq_result
242 i40evf_read_pfmsg(struct rte_eth_dev *dev, struct i40evf_arq_msg_info *data)
243 {
244         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
245         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
246         struct i40e_arq_event_info event;
247         int ret;
248         enum i40evf_aq_result result = I40EVF_MSG_NON;
249
250         event.msg_size = data->msg_len;
251         event.msg_buf = data->msg;
252         ret = i40e_clean_arq_element(hw, &event, NULL);
253         /* Can't read any msg from adminQ */
254         if (ret) {
255                 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
256                         result = I40EVF_MSG_NON;
257                 else
258                         result = I40EVF_MSG_ERR;
259                 return result;
260         }
261
262         /* Parse the event */
263         result = i40evf_parse_pfmsg(vf, &event, data);
264
265         return result;
266 }
267
268 /*
269  * Polling read until command result return from pf driver or meet error.
270  */
271 static int
272 i40evf_wait_cmd_done(struct rte_eth_dev *dev,
273                      struct i40evf_arq_msg_info *data)
274 {
275         int i = 0;
276         enum i40evf_aq_result ret;
277
278 #define MAX_TRY_TIMES 10
279 #define ASQ_DELAY_MS  50
280         do {
281                 /* Delay some time first */
282                 rte_delay_ms(ASQ_DELAY_MS);
283                 ret = i40evf_read_pfmsg(dev, data);
284
285                 if (ret == I40EVF_MSG_CMD)
286                         return 0;
287                 else if (ret == I40EVF_MSG_ERR)
288                         return -1;
289
290                 /* If don't read msg or read sys event, continue */
291         } while(i++ < MAX_TRY_TIMES);
292
293         return -1;
294 }
295
296 /**
297  * clear current command. Only call in case execute
298  * _atomic_set_cmd successfully.
299  */
300 static inline void
301 _clear_cmd(struct i40e_vf *vf)
302 {
303         rte_wmb();
304         vf->pend_cmd = I40E_VIRTCHNL_OP_UNKNOWN;
305 }
306
307 /*
308  * Check there is pending cmd in execution. If none, set new command.
309  */
310 static inline int
311 _atomic_set_cmd(struct i40e_vf *vf, enum i40e_virtchnl_ops ops)
312 {
313         int ret = rte_atomic32_cmpset(&vf->pend_cmd,
314                         I40E_VIRTCHNL_OP_UNKNOWN, ops);
315
316         if (!ret)
317                 PMD_DRV_LOG(ERR, "There is incomplete cmd %d", vf->pend_cmd);
318
319         return !ret;
320 }
321
322 static int
323 i40evf_execute_vf_cmd(struct rte_eth_dev *dev, struct vf_cmd_info *args)
324 {
325         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
326         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
327         int err = -1;
328         struct i40evf_arq_msg_info info;
329
330         if (_atomic_set_cmd(vf, args->ops))
331                 return -1;
332
333         info.msg = args->out_buffer;
334         info.msg_len = args->out_size;
335         info.ops = I40E_VIRTCHNL_OP_UNKNOWN;
336         info.result = I40E_SUCCESS;
337
338         err = i40e_aq_send_msg_to_pf(hw, args->ops, I40E_SUCCESS,
339                      args->in_args, args->in_args_size, NULL);
340         if (err) {
341                 PMD_DRV_LOG(ERR, "fail to send cmd %d", args->ops);
342                 return err;
343         }
344
345         err = i40evf_wait_cmd_done(dev, &info);
346         /* read message and it's expected one */
347         if (!err && args->ops == info.ops)
348                 _clear_cmd(vf);
349         else if (err)
350                 PMD_DRV_LOG(ERR, "Failed to read message from AdminQ");
351         else if (args->ops != info.ops)
352                 PMD_DRV_LOG(ERR, "command mismatch, expect %u, get %u",
353                             args->ops, info.ops);
354
355         return (err | info.result);
356 }
357
358 /*
359  * Check API version with sync wait until version read or fail from admin queue
360  */
361 static int
362 i40evf_check_api_version(struct rte_eth_dev *dev)
363 {
364         struct i40e_virtchnl_version_info version, *pver;
365         int err;
366         struct vf_cmd_info args;
367         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
368
369         version.major = I40E_VIRTCHNL_VERSION_MAJOR;
370         version.minor = I40E_VIRTCHNL_VERSION_MINOR;
371
372         args.ops = I40E_VIRTCHNL_OP_VERSION;
373         args.in_args = (uint8_t *)&version;
374         args.in_args_size = sizeof(version);
375         args.out_buffer = cmd_result_buffer;
376         args.out_size = I40E_AQ_BUF_SZ;
377
378         err = i40evf_execute_vf_cmd(dev, &args);
379         if (err) {
380                 PMD_INIT_LOG(ERR, "fail to execute command OP_VERSION");
381                 return err;
382         }
383
384         pver = (struct i40e_virtchnl_version_info *)args.out_buffer;
385         /* We are talking with DPDK host */
386         if (pver->major == I40E_DPDK_VERSION_MAJOR) {
387                 vf->host_is_dpdk = TRUE;
388                 PMD_DRV_LOG(INFO, "Detect PF host is DPDK app");
389         }
390         /* It's linux host driver */
391         else if ((pver->major != version.major) ||
392             (pver->minor != version.minor)) {
393                 PMD_INIT_LOG(ERR, "pf/vf API version mismatch. "
394                              "(%u.%u)-(%u.%u)", pver->major, pver->minor,
395                              version.major, version.minor);
396                 return -1;
397         }
398
399         return 0;
400 }
401
402 static int
403 i40evf_get_vf_resource(struct rte_eth_dev *dev)
404 {
405         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
406         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
407         int err;
408         struct vf_cmd_info args;
409         uint32_t len;
410
411         args.ops = I40E_VIRTCHNL_OP_GET_VF_RESOURCES;
412         args.in_args = NULL;
413         args.in_args_size = 0;
414         args.out_buffer = cmd_result_buffer;
415         args.out_size = I40E_AQ_BUF_SZ;
416
417         err = i40evf_execute_vf_cmd(dev, &args);
418
419         if (err) {
420                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_VF_RESOURCE");
421                 return err;
422         }
423
424         len =  sizeof(struct i40e_virtchnl_vf_resource) +
425                 I40E_MAX_VF_VSI * sizeof(struct i40e_virtchnl_vsi_resource);
426
427         (void)rte_memcpy(vf->vf_res, args.out_buffer,
428                         RTE_MIN(args.out_size, len));
429         i40e_vf_parse_hw_config(hw, vf->vf_res);
430
431         return 0;
432 }
433
434 static int
435 i40evf_config_promisc(struct rte_eth_dev *dev,
436                       bool enable_unicast,
437                       bool enable_multicast)
438 {
439         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
440         int err;
441         struct vf_cmd_info args;
442         struct i40e_virtchnl_promisc_info promisc;
443
444         promisc.flags = 0;
445         promisc.vsi_id = vf->vsi_res->vsi_id;
446
447         if (enable_unicast)
448                 promisc.flags |= I40E_FLAG_VF_UNICAST_PROMISC;
449
450         if (enable_multicast)
451                 promisc.flags |= I40E_FLAG_VF_MULTICAST_PROMISC;
452
453         args.ops = I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
454         args.in_args = (uint8_t *)&promisc;
455         args.in_args_size = sizeof(promisc);
456         args.out_buffer = cmd_result_buffer;
457         args.out_size = I40E_AQ_BUF_SZ;
458
459         err = i40evf_execute_vf_cmd(dev, &args);
460
461         if (err)
462                 PMD_DRV_LOG(ERR, "fail to execute command "
463                             "CONFIG_PROMISCUOUS_MODE");
464         return err;
465 }
466
467 /* Configure vlan and double vlan offload. Use flag to specify which part to configure */
468 static int
469 i40evf_config_vlan_offload(struct rte_eth_dev *dev,
470                                 bool enable_vlan_strip)
471 {
472         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
473         int err;
474         struct vf_cmd_info args;
475         struct i40e_virtchnl_vlan_offload_info offload;
476
477         offload.vsi_id = vf->vsi_res->vsi_id;
478         offload.enable_vlan_strip = enable_vlan_strip;
479
480         args.ops = (enum i40e_virtchnl_ops)I40E_VIRTCHNL_OP_CFG_VLAN_OFFLOAD;
481         args.in_args = (uint8_t *)&offload;
482         args.in_args_size = sizeof(offload);
483         args.out_buffer = cmd_result_buffer;
484         args.out_size = I40E_AQ_BUF_SZ;
485
486         err = i40evf_execute_vf_cmd(dev, &args);
487         if (err)
488                 PMD_DRV_LOG(ERR, "fail to execute command CFG_VLAN_OFFLOAD");
489
490         return err;
491 }
492
493 static int
494 i40evf_config_vlan_pvid(struct rte_eth_dev *dev,
495                                 struct i40e_vsi_vlan_pvid_info *info)
496 {
497         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
498         int err;
499         struct vf_cmd_info args;
500         struct i40e_virtchnl_pvid_info tpid_info;
501
502         if (dev == NULL || info == NULL) {
503                 PMD_DRV_LOG(ERR, "invalid parameters");
504                 return I40E_ERR_PARAM;
505         }
506
507         memset(&tpid_info, 0, sizeof(tpid_info));
508         tpid_info.vsi_id = vf->vsi_res->vsi_id;
509         (void)rte_memcpy(&tpid_info.info, info, sizeof(*info));
510
511         args.ops = (enum i40e_virtchnl_ops)I40E_VIRTCHNL_OP_CFG_VLAN_PVID;
512         args.in_args = (uint8_t *)&tpid_info;
513         args.in_args_size = sizeof(tpid_info);
514         args.out_buffer = cmd_result_buffer;
515         args.out_size = I40E_AQ_BUF_SZ;
516
517         err = i40evf_execute_vf_cmd(dev, &args);
518         if (err)
519                 PMD_DRV_LOG(ERR, "fail to execute command CFG_VLAN_PVID");
520
521         return err;
522 }
523
524 static int
525 i40evf_configure_queues(struct rte_eth_dev *dev)
526 {
527         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
528         struct i40e_virtchnl_vsi_queue_config_info *queue_info;
529         struct i40e_virtchnl_queue_pair_info *queue_cfg;
530         struct i40e_rx_queue **rxq =
531                 (struct i40e_rx_queue **)dev->data->rx_queues;
532         struct i40e_tx_queue **txq =
533                 (struct i40e_tx_queue **)dev->data->tx_queues;
534         int i, len, nb_qpairs, num_rxq, num_txq;
535         int err;
536         struct vf_cmd_info args;
537         struct rte_pktmbuf_pool_private *mbp_priv;
538
539         nb_qpairs = vf->num_queue_pairs;
540         len = sizeof(*queue_info) + sizeof(*queue_cfg) * nb_qpairs;
541         queue_info = rte_zmalloc("queue_info", len, 0);
542         if (queue_info == NULL) {
543                 PMD_INIT_LOG(ERR, "failed alloc memory for queue_info");
544                 return -1;
545         }
546         queue_info->vsi_id = vf->vsi_res->vsi_id;
547         queue_info->num_queue_pairs = nb_qpairs;
548         queue_cfg = queue_info->qpair;
549
550         num_rxq = dev->data->nb_rx_queues;
551         num_txq = dev->data->nb_tx_queues;
552         /*
553          * PF host driver required to configure queues in pairs, which means
554          * rxq_num should equals to txq_num. The actual usage won't always
555          * work that way. The solution is fills 0 with HW ring option in case
556          * they are not equal.
557          */
558         for (i = 0; i < nb_qpairs; i++) {
559                 /*Fill TX info */
560                 queue_cfg->txq.vsi_id = queue_info->vsi_id;
561                 queue_cfg->txq.queue_id = i;
562                 if (i < num_txq) {
563                         queue_cfg->txq.ring_len = txq[i]->nb_tx_desc;
564                         queue_cfg->txq.dma_ring_addr = txq[i]->tx_ring_phys_addr;
565                 } else {
566                         queue_cfg->txq.ring_len = 0;
567                         queue_cfg->txq.dma_ring_addr = 0;
568                 }
569
570                 /* Fill RX info */
571                 queue_cfg->rxq.vsi_id = queue_info->vsi_id;
572                 queue_cfg->rxq.queue_id = i;
573                 queue_cfg->rxq.max_pkt_size = vf->max_pkt_len;
574                 if (i < num_rxq) {
575                         mbp_priv = rte_mempool_get_priv(rxq[i]->mp);
576                         queue_cfg->rxq.databuffer_size = mbp_priv->mbuf_data_room_size -
577                                                    RTE_PKTMBUF_HEADROOM;;
578                         queue_cfg->rxq.ring_len = rxq[i]->nb_rx_desc;
579                         queue_cfg->rxq.dma_ring_addr = rxq[i]->rx_ring_phys_addr;;
580                 } else {
581                         queue_cfg->rxq.ring_len = 0;
582                         queue_cfg->rxq.dma_ring_addr = 0;
583                         queue_cfg->rxq.databuffer_size = 0;
584                 }
585                 queue_cfg++;
586         }
587
588         args.ops = I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES;
589         args.in_args = (u8 *)queue_info;
590         args.in_args_size = len;
591         args.out_buffer = cmd_result_buffer;
592         args.out_size = I40E_AQ_BUF_SZ;
593         err = i40evf_execute_vf_cmd(dev, &args);
594         if (err)
595                 PMD_DRV_LOG(ERR, "fail to execute command "
596                             "OP_CONFIG_VSI_QUEUES");
597         rte_free(queue_info);
598
599         return err;
600 }
601
602 static int
603 i40evf_config_irq_map(struct rte_eth_dev *dev)
604 {
605         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
606         struct vf_cmd_info args;
607         uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_irq_map_info) + \
608                 sizeof(struct i40e_virtchnl_vector_map)];
609         struct i40e_virtchnl_irq_map_info *map_info;
610         int i, err;
611         map_info = (struct i40e_virtchnl_irq_map_info *)cmd_buffer;
612         map_info->num_vectors = 1;
613         map_info->vecmap[0].rxitr_idx = RTE_LIBRTE_I40E_ITR_INTERVAL / 2;
614         map_info->vecmap[0].txitr_idx = RTE_LIBRTE_I40E_ITR_INTERVAL / 2;
615         map_info->vecmap[0].vsi_id = vf->vsi_res->vsi_id;
616         /* Alway use default dynamic MSIX interrupt */
617         map_info->vecmap[0].vector_id = I40EVF_VSI_DEFAULT_MSIX_INTR;
618         /* Don't map any tx queue */
619         map_info->vecmap[0].txq_map = 0;
620         map_info->vecmap[0].rxq_map = 0;
621         for (i = 0; i < dev->data->nb_rx_queues; i++)
622                 map_info->vecmap[0].rxq_map |= 1 << i;
623
624         args.ops = I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP;
625         args.in_args = (u8 *)cmd_buffer;
626         args.in_args_size = sizeof(cmd_buffer);
627         args.out_buffer = cmd_result_buffer;
628         args.out_size = I40E_AQ_BUF_SZ;
629         err = i40evf_execute_vf_cmd(dev, &args);
630         if (err)
631                 PMD_DRV_LOG(ERR, "fail to execute command OP_ENABLE_QUEUES");
632
633         return err;
634 }
635
636 static int
637 i40evf_switch_queue(struct rte_eth_dev *dev, bool isrx, uint16_t qid,
638                                 bool on)
639 {
640         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
641         struct i40e_virtchnl_queue_select queue_select;
642         int err;
643         struct vf_cmd_info args;
644         memset(&queue_select, 0, sizeof(queue_select));
645         queue_select.vsi_id = vf->vsi_res->vsi_id;
646
647         if (isrx)
648                 queue_select.rx_queues |= 1 << qid;
649         else
650                 queue_select.tx_queues |= 1 << qid;
651
652         if (on)
653                 args.ops = I40E_VIRTCHNL_OP_ENABLE_QUEUES;
654         else
655                 args.ops = I40E_VIRTCHNL_OP_DISABLE_QUEUES;
656         args.in_args = (u8 *)&queue_select;
657         args.in_args_size = sizeof(queue_select);
658         args.out_buffer = cmd_result_buffer;
659         args.out_size = I40E_AQ_BUF_SZ;
660         err = i40evf_execute_vf_cmd(dev, &args);
661         if (err)
662                 PMD_DRV_LOG(ERR, "fail to switch %s %u %s",
663                             isrx ? "RX" : "TX", qid, on ? "on" : "off");
664
665         return err;
666 }
667
668 static int
669 i40evf_start_queues(struct rte_eth_dev *dev)
670 {
671         struct rte_eth_dev_data *dev_data = dev->data;
672         int i;
673         struct i40e_rx_queue *rxq;
674         struct i40e_tx_queue *txq;
675
676         for (i = 0; i < dev->data->nb_rx_queues; i++) {
677                 rxq = dev_data->rx_queues[i];
678                 if (rxq->start_rx_per_q)
679                         continue;
680                 if (i40evf_dev_rx_queue_start(dev, i) != 0) {
681                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
682                         return -1;
683                 }
684         }
685
686         for (i = 0; i < dev->data->nb_tx_queues; i++) {
687                 txq = dev_data->tx_queues[i];
688                 if (txq->start_tx_per_q)
689                         continue;
690                 if (i40evf_dev_tx_queue_start(dev, i) != 0) {
691                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
692                         return -1;
693                 }
694         }
695
696         return 0;
697 }
698
699 static int
700 i40evf_stop_queues(struct rte_eth_dev *dev)
701 {
702         int i;
703
704         /* Stop TX queues first */
705         for (i = 0; i < dev->data->nb_tx_queues; i++) {
706                 if (i40evf_dev_tx_queue_stop(dev, i) != 0) {
707                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
708                         return -1;
709                 }
710         }
711
712         /* Then stop RX queues */
713         for (i = 0; i < dev->data->nb_rx_queues; i++) {
714                 if (i40evf_dev_rx_queue_stop(dev, i) != 0) {
715                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
716                         return -1;
717                 }
718         }
719
720         return 0;
721 }
722
723 static int
724 i40evf_add_mac_addr(struct rte_eth_dev *dev, struct ether_addr *addr)
725 {
726         struct i40e_virtchnl_ether_addr_list *list;
727         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
728         uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_ether_addr_list) + \
729                         sizeof(struct i40e_virtchnl_ether_addr)];
730         int err;
731         struct vf_cmd_info args;
732
733         if (i40e_validate_mac_addr(addr->addr_bytes) != I40E_SUCCESS) {
734                 PMD_DRV_LOG(ERR, "Invalid mac:%x:%x:%x:%x:%x:%x",
735                             addr->addr_bytes[0], addr->addr_bytes[1],
736                             addr->addr_bytes[2], addr->addr_bytes[3],
737                             addr->addr_bytes[4], addr->addr_bytes[5]);
738                 return -1;
739         }
740
741         list = (struct i40e_virtchnl_ether_addr_list *)cmd_buffer;
742         list->vsi_id = vf->vsi_res->vsi_id;
743         list->num_elements = 1;
744         (void)rte_memcpy(list->list[0].addr, addr->addr_bytes,
745                                         sizeof(addr->addr_bytes));
746
747         args.ops = I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS;
748         args.in_args = cmd_buffer;
749         args.in_args_size = sizeof(cmd_buffer);
750         args.out_buffer = cmd_result_buffer;
751         args.out_size = I40E_AQ_BUF_SZ;
752         err = i40evf_execute_vf_cmd(dev, &args);
753         if (err)
754                 PMD_DRV_LOG(ERR, "fail to execute command "
755                             "OP_ADD_ETHER_ADDRESS");
756
757         return err;
758 }
759
760 static int
761 i40evf_del_mac_addr(struct rte_eth_dev *dev, struct ether_addr *addr)
762 {
763         struct i40e_virtchnl_ether_addr_list *list;
764         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
765         uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_ether_addr_list) + \
766                         sizeof(struct i40e_virtchnl_ether_addr)];
767         int err;
768         struct vf_cmd_info args;
769
770         if (i40e_validate_mac_addr(addr->addr_bytes) != I40E_SUCCESS) {
771                 PMD_DRV_LOG(ERR, "Invalid mac:%x-%x-%x-%x-%x-%x",
772                             addr->addr_bytes[0], addr->addr_bytes[1],
773                             addr->addr_bytes[2], addr->addr_bytes[3],
774                             addr->addr_bytes[4], addr->addr_bytes[5]);
775                 return -1;
776         }
777
778         list = (struct i40e_virtchnl_ether_addr_list *)cmd_buffer;
779         list->vsi_id = vf->vsi_res->vsi_id;
780         list->num_elements = 1;
781         (void)rte_memcpy(list->list[0].addr, addr->addr_bytes,
782                         sizeof(addr->addr_bytes));
783
784         args.ops = I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS;
785         args.in_args = cmd_buffer;
786         args.in_args_size = sizeof(cmd_buffer);
787         args.out_buffer = cmd_result_buffer;
788         args.out_size = I40E_AQ_BUF_SZ;
789         err = i40evf_execute_vf_cmd(dev, &args);
790         if (err)
791                 PMD_DRV_LOG(ERR, "fail to execute command "
792                             "OP_DEL_ETHER_ADDRESS");
793
794         return err;
795 }
796
797 static int
798 i40evf_get_statics(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
799 {
800         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
801         struct i40e_virtchnl_queue_select q_stats;
802         struct i40e_eth_stats *pstats;
803         int err;
804         struct vf_cmd_info args;
805
806         memset(&q_stats, 0, sizeof(q_stats));
807         q_stats.vsi_id = vf->vsi_res->vsi_id;
808         args.ops = I40E_VIRTCHNL_OP_GET_STATS;
809         args.in_args = (u8 *)&q_stats;
810         args.in_args_size = sizeof(q_stats);
811         args.out_buffer = cmd_result_buffer;
812         args.out_size = I40E_AQ_BUF_SZ;
813
814         err = i40evf_execute_vf_cmd(dev, &args);
815         if (err) {
816                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_STATS");
817                 return err;
818         }
819         pstats = (struct i40e_eth_stats *)args.out_buffer;
820         stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
821                                                 pstats->rx_broadcast;
822         stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
823                                                 pstats->tx_unicast;
824         stats->ierrors = pstats->rx_discards;
825         stats->oerrors = pstats->tx_errors + pstats->tx_discards;
826         stats->ibytes = pstats->rx_bytes;
827         stats->obytes = pstats->tx_bytes;
828
829         return 0;
830 }
831
832 static int
833 i40evf_add_vlan(struct rte_eth_dev *dev, uint16_t vlanid)
834 {
835         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
836         struct i40e_virtchnl_vlan_filter_list *vlan_list;
837         uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_vlan_filter_list) +
838                                                         sizeof(uint16_t)];
839         int err;
840         struct vf_cmd_info args;
841
842         vlan_list = (struct i40e_virtchnl_vlan_filter_list *)cmd_buffer;
843         vlan_list->vsi_id = vf->vsi_res->vsi_id;
844         vlan_list->num_elements = 1;
845         vlan_list->vlan_id[0] = vlanid;
846
847         args.ops = I40E_VIRTCHNL_OP_ADD_VLAN;
848         args.in_args = (u8 *)&cmd_buffer;
849         args.in_args_size = sizeof(cmd_buffer);
850         args.out_buffer = cmd_result_buffer;
851         args.out_size = I40E_AQ_BUF_SZ;
852         err = i40evf_execute_vf_cmd(dev, &args);
853         if (err)
854                 PMD_DRV_LOG(ERR, "fail to execute command OP_ADD_VLAN");
855
856         return err;
857 }
858
859 static int
860 i40evf_del_vlan(struct rte_eth_dev *dev, uint16_t vlanid)
861 {
862         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
863         struct i40e_virtchnl_vlan_filter_list *vlan_list;
864         uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_vlan_filter_list) +
865                                                         sizeof(uint16_t)];
866         int err;
867         struct vf_cmd_info args;
868
869         vlan_list = (struct i40e_virtchnl_vlan_filter_list *)cmd_buffer;
870         vlan_list->vsi_id = vf->vsi_res->vsi_id;
871         vlan_list->num_elements = 1;
872         vlan_list->vlan_id[0] = vlanid;
873
874         args.ops = I40E_VIRTCHNL_OP_DEL_VLAN;
875         args.in_args = (u8 *)&cmd_buffer;
876         args.in_args_size = sizeof(cmd_buffer);
877         args.out_buffer = cmd_result_buffer;
878         args.out_size = I40E_AQ_BUF_SZ;
879         err = i40evf_execute_vf_cmd(dev, &args);
880         if (err)
881                 PMD_DRV_LOG(ERR, "fail to execute command OP_DEL_VLAN");
882
883         return err;
884 }
885
886 static int
887 i40evf_get_link_status(struct rte_eth_dev *dev, struct rte_eth_link *link)
888 {
889         int err;
890         struct vf_cmd_info args;
891         struct rte_eth_link *new_link;
892
893         args.ops = (enum i40e_virtchnl_ops)I40E_VIRTCHNL_OP_GET_LINK_STAT;
894         args.in_args = NULL;
895         args.in_args_size = 0;
896         args.out_buffer = cmd_result_buffer;
897         args.out_size = I40E_AQ_BUF_SZ;
898         err = i40evf_execute_vf_cmd(dev, &args);
899         if (err) {
900                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_LINK_STAT");
901                 return err;
902         }
903
904         new_link = (struct rte_eth_link *)args.out_buffer;
905         (void)rte_memcpy(link, new_link, sizeof(*link));
906
907         return 0;
908 }
909
910 static struct rte_pci_id pci_id_i40evf_map[] = {
911 #define RTE_PCI_DEV_ID_DECL_I40EVF(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
912 #include "rte_pci_dev_ids.h"
913 { .vendor_id = 0, /* sentinel */ },
914 };
915
916 static inline int
917 i40evf_dev_atomic_write_link_status(struct rte_eth_dev *dev,
918                                     struct rte_eth_link *link)
919 {
920         struct rte_eth_link *dst = &(dev->data->dev_link);
921         struct rte_eth_link *src = link;
922
923         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
924                                         *(uint64_t *)src) == 0)
925                 return -1;
926
927         return 0;
928 }
929
930 static int
931 i40evf_reset_vf(struct i40e_hw *hw)
932 {
933         int i, reset;
934
935         if (i40e_vf_reset(hw) != I40E_SUCCESS) {
936                 PMD_INIT_LOG(ERR, "Reset VF NIC failed");
937                 return -1;
938         }
939         /**
940           * After issuing vf reset command to pf, pf won't necessarily
941           * reset vf, it depends on what state it exactly is. If it's not
942           * initialized yet, it won't have vf reset since it's in a certain
943           * state. If not, it will try to reset. Even vf is reset, pf will
944           * set I40E_VFGEN_RSTAT to COMPLETE first, then wait 10ms and set
945           * it to ACTIVE. In this duration, vf may not catch the moment that
946           * COMPLETE is set. So, for vf, we'll try to wait a long time.
947           */
948         rte_delay_ms(200);
949
950         for (i = 0; i < MAX_RESET_WAIT_CNT; i++) {
951                 reset = rd32(hw, I40E_VFGEN_RSTAT) &
952                         I40E_VFGEN_RSTAT_VFR_STATE_MASK;
953                 reset = reset >> I40E_VFGEN_RSTAT_VFR_STATE_SHIFT;
954                 if (I40E_VFR_COMPLETED == reset || I40E_VFR_VFACTIVE == reset)
955                         break;
956                 else
957                         rte_delay_ms(50);
958         }
959
960         if (i >= MAX_RESET_WAIT_CNT) {
961                 PMD_INIT_LOG(ERR, "Reset VF NIC failed");
962                 return -1;
963         }
964
965         return 0;
966 }
967
968 static int
969 i40evf_init_vf(struct rte_eth_dev *dev)
970 {
971         int i, err, bufsz;
972         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
973         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
974
975         err = i40evf_set_mac_type(hw);
976         if (err) {
977                 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
978                 goto err;
979         }
980
981         i40e_init_adminq_parameter(hw);
982         err = i40e_init_adminq(hw);
983         if (err) {
984                 PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
985                 goto err;
986         }
987
988
989         /* Reset VF and wait until it's complete */
990         if (i40evf_reset_vf(hw)) {
991                 PMD_INIT_LOG(ERR, "reset NIC failed");
992                 goto err_aq;
993         }
994
995         /* VF reset, shutdown admin queue and initialize again */
996         if (i40e_shutdown_adminq(hw) != I40E_SUCCESS) {
997                 PMD_INIT_LOG(ERR, "i40e_shutdown_adminq failed");
998                 return -1;
999         }
1000
1001         i40e_init_adminq_parameter(hw);
1002         if (i40e_init_adminq(hw) != I40E_SUCCESS) {
1003                 PMD_INIT_LOG(ERR, "init_adminq failed");
1004                 return -1;
1005         }
1006         if (i40evf_check_api_version(dev) != 0) {
1007                 PMD_INIT_LOG(ERR, "check_api version failed");
1008                 goto err_aq;
1009         }
1010         bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
1011                 (I40E_MAX_VF_VSI * sizeof(struct i40e_virtchnl_vsi_resource));
1012         vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
1013         if (!vf->vf_res) {
1014                 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
1015                         goto err_aq;
1016         }
1017
1018         if (i40evf_get_vf_resource(dev) != 0) {
1019                 PMD_INIT_LOG(ERR, "i40evf_get_vf_config failed");
1020                 goto err_alloc;
1021         }
1022
1023         /* got VF config message back from PF, now we can parse it */
1024         for (i = 0; i < vf->vf_res->num_vsis; i++) {
1025                 if (vf->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
1026                         vf->vsi_res = &vf->vf_res->vsi_res[i];
1027         }
1028
1029         if (!vf->vsi_res) {
1030                 PMD_INIT_LOG(ERR, "no LAN VSI found");
1031                 goto err_alloc;
1032         }
1033
1034         vf->vsi.vsi_id = vf->vsi_res->vsi_id;
1035         vf->vsi.type = vf->vsi_res->vsi_type;
1036         vf->vsi.nb_qps = vf->vsi_res->num_queue_pairs;
1037
1038         /* check mac addr, if it's not valid, genrate one */
1039         if (I40E_SUCCESS != i40e_validate_mac_addr(\
1040                         vf->vsi_res->default_mac_addr))
1041                 eth_random_addr(vf->vsi_res->default_mac_addr);
1042
1043         ether_addr_copy((struct ether_addr *)vf->vsi_res->default_mac_addr,
1044                                         (struct ether_addr *)hw->mac.addr);
1045
1046         return 0;
1047
1048 err_alloc:
1049         rte_free(vf->vf_res);
1050 err_aq:
1051         i40e_shutdown_adminq(hw); /* ignore error */
1052 err:
1053         return -1;
1054 }
1055
1056 static int
1057 i40evf_dev_init(__rte_unused struct eth_driver *eth_drv,
1058                 struct rte_eth_dev *eth_dev)
1059 {
1060         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(\
1061                         eth_dev->data->dev_private);
1062
1063         PMD_INIT_FUNC_TRACE();
1064
1065         /* assign ops func pointer */
1066         eth_dev->dev_ops = &i40evf_eth_dev_ops;
1067         eth_dev->rx_pkt_burst = &i40e_recv_pkts;
1068         eth_dev->tx_pkt_burst = &i40e_xmit_pkts;
1069
1070         /*
1071          * For secondary processes, we don't initialise any further as primary
1072          * has already done this work.
1073          */
1074         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
1075                 if (eth_dev->data->scattered_rx)
1076                         eth_dev->rx_pkt_burst = i40e_recv_scattered_pkts;
1077                 return 0;
1078         }
1079
1080         hw->vendor_id = eth_dev->pci_dev->id.vendor_id;
1081         hw->device_id = eth_dev->pci_dev->id.device_id;
1082         hw->subsystem_vendor_id = eth_dev->pci_dev->id.subsystem_vendor_id;
1083         hw->subsystem_device_id = eth_dev->pci_dev->id.subsystem_device_id;
1084         hw->bus.device = eth_dev->pci_dev->addr.devid;
1085         hw->bus.func = eth_dev->pci_dev->addr.function;
1086         hw->hw_addr = (void *)eth_dev->pci_dev->mem_resource[0].addr;
1087
1088         if(i40evf_init_vf(eth_dev) != 0) {
1089                 PMD_INIT_LOG(ERR, "Init vf failed");
1090                 return -1;
1091         }
1092
1093         /* copy mac addr */
1094         eth_dev->data->mac_addrs = rte_zmalloc("i40evf_mac",
1095                                         ETHER_ADDR_LEN, 0);
1096         if (eth_dev->data->mac_addrs == NULL) {
1097                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to "
1098                                 "store MAC addresses", ETHER_ADDR_LEN);
1099                 return -ENOMEM;
1100         }
1101         ether_addr_copy((struct ether_addr *)hw->mac.addr,
1102                 (struct ether_addr *)eth_dev->data->mac_addrs);
1103
1104         return 0;
1105 }
1106
1107 /*
1108  * virtual function driver struct
1109  */
1110 static struct eth_driver rte_i40evf_pmd = {
1111         {
1112                 .name = "rte_i40evf_pmd",
1113                 .id_table = pci_id_i40evf_map,
1114                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
1115         },
1116         .eth_dev_init = i40evf_dev_init,
1117         .dev_private_size = sizeof(struct i40e_vf),
1118 };
1119
1120 /*
1121  * VF Driver initialization routine.
1122  * Invoked one at EAL init time.
1123  * Register itself as the [Virtual Poll Mode] Driver of PCI Fortville devices.
1124  */
1125 static int
1126 rte_i40evf_pmd_init(const char *name __rte_unused,
1127                     const char *params __rte_unused)
1128 {
1129         PMD_INIT_FUNC_TRACE();
1130
1131         rte_eth_driver_register(&rte_i40evf_pmd);
1132
1133         return 0;
1134 }
1135
1136 static struct rte_driver rte_i40evf_driver = {
1137         .type = PMD_PDEV,
1138         .init = rte_i40evf_pmd_init,
1139 };
1140
1141 PMD_REGISTER_DRIVER(rte_i40evf_driver);
1142
1143 static int
1144 i40evf_dev_configure(struct rte_eth_dev *dev)
1145 {
1146         return i40evf_init_vlan(dev);
1147 }
1148
1149 static int
1150 i40evf_init_vlan(struct rte_eth_dev *dev)
1151 {
1152         struct rte_eth_dev_data *data = dev->data;
1153         int ret;
1154
1155         /* Apply vlan offload setting */
1156         i40evf_vlan_offload_set(dev, ETH_VLAN_STRIP_MASK);
1157
1158         /* Apply pvid setting */
1159         ret = i40evf_vlan_pvid_set(dev, data->dev_conf.txmode.pvid,
1160                                 data->dev_conf.txmode.hw_vlan_insert_pvid);
1161         return ret;
1162 }
1163
1164 static void
1165 i40evf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1166 {
1167         bool enable_vlan_strip = 0;
1168         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1169         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1170
1171         /* Linux pf host doesn't support vlan offload yet */
1172         if (vf->host_is_dpdk) {
1173                 /* Vlan stripping setting */
1174                 if (mask & ETH_VLAN_STRIP_MASK) {
1175                         /* Enable or disable VLAN stripping */
1176                         if (dev_conf->rxmode.hw_vlan_strip)
1177                                 enable_vlan_strip = 1;
1178                         else
1179                                 enable_vlan_strip = 0;
1180
1181                         i40evf_config_vlan_offload(dev, enable_vlan_strip);
1182                 }
1183         }
1184 }
1185
1186 static int
1187 i40evf_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
1188 {
1189         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1190         struct i40e_vsi_vlan_pvid_info info;
1191         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1192
1193         memset(&info, 0, sizeof(info));
1194         info.on = on;
1195
1196         /* Linux pf host don't support vlan offload yet */
1197         if (vf->host_is_dpdk) {
1198                 if (info.on)
1199                         info.config.pvid = pvid;
1200                 else {
1201                         info.config.reject.tagged =
1202                                 dev_conf->txmode.hw_vlan_reject_tagged;
1203                         info.config.reject.untagged =
1204                                 dev_conf->txmode.hw_vlan_reject_untagged;
1205                 }
1206                 return i40evf_config_vlan_pvid(dev, &info);
1207         }
1208
1209         return 0;
1210 }
1211
1212 static int
1213 i40evf_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1214 {
1215         struct i40e_rx_queue *rxq;
1216         int err = 0;
1217         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1218
1219         PMD_INIT_FUNC_TRACE();
1220
1221         if (rx_queue_id < dev->data->nb_rx_queues) {
1222                 rxq = dev->data->rx_queues[rx_queue_id];
1223
1224                 err = i40e_alloc_rx_queue_mbufs(rxq);
1225                 if (err) {
1226                         PMD_DRV_LOG(ERR, "Failed to allocate RX queue mbuf");
1227                         return err;
1228                 }
1229
1230                 rte_wmb();
1231
1232                 /* Init the RX tail register. */
1233                 I40E_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
1234                 I40EVF_WRITE_FLUSH(hw);
1235
1236                 /* Ready to switch the queue on */
1237                 err = i40evf_switch_queue(dev, TRUE, rx_queue_id, TRUE);
1238
1239                 if (err)
1240                         PMD_DRV_LOG(ERR, "Failed to switch RX queue %u on",
1241                                     rx_queue_id);
1242         }
1243
1244         return err;
1245 }
1246
1247 static int
1248 i40evf_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1249 {
1250         struct i40e_rx_queue *rxq;
1251         int err;
1252
1253         if (rx_queue_id < dev->data->nb_rx_queues) {
1254                 rxq = dev->data->rx_queues[rx_queue_id];
1255
1256                 err = i40evf_switch_queue(dev, TRUE, rx_queue_id, FALSE);
1257
1258                 if (err) {
1259                         PMD_DRV_LOG(ERR, "Failed to switch RX queue %u off",
1260                                     rx_queue_id);
1261                         return err;
1262                 }
1263
1264                 i40e_rx_queue_release_mbufs(rxq);
1265                 i40e_reset_rx_queue(rxq);
1266         }
1267
1268         return 0;
1269 }
1270
1271 static int
1272 i40evf_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1273 {
1274         int err = 0;
1275
1276         PMD_INIT_FUNC_TRACE();
1277
1278         if (tx_queue_id < dev->data->nb_tx_queues) {
1279
1280                 /* Ready to switch the queue on */
1281                 err = i40evf_switch_queue(dev, FALSE, tx_queue_id, TRUE);
1282
1283                 if (err)
1284                         PMD_DRV_LOG(ERR, "Failed to switch TX queue %u on",
1285                                     tx_queue_id);
1286         }
1287
1288         return err;
1289 }
1290
1291 static int
1292 i40evf_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1293 {
1294         struct i40e_tx_queue *txq;
1295         int err;
1296
1297         if (tx_queue_id < dev->data->nb_tx_queues) {
1298                 txq = dev->data->tx_queues[tx_queue_id];
1299
1300                 err = i40evf_switch_queue(dev, FALSE, tx_queue_id, FALSE);
1301
1302                 if (err) {
1303                         PMD_DRV_LOG(ERR, "Failed to switch TX queue %u of",
1304                                     tx_queue_id);
1305                         return err;
1306                 }
1307
1308                 i40e_tx_queue_release_mbufs(txq);
1309                 i40e_reset_tx_queue(txq);
1310         }
1311
1312         return 0;
1313 }
1314
1315 static int
1316 i40evf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1317 {
1318         int ret;
1319
1320         if (on)
1321                 ret = i40evf_add_vlan(dev, vlan_id);
1322         else
1323                 ret = i40evf_del_vlan(dev,vlan_id);
1324
1325         return ret;
1326 }
1327
1328 static int
1329 i40evf_rx_init(struct rte_eth_dev *dev)
1330 {
1331         uint16_t i;
1332         struct i40e_rx_queue **rxq =
1333                 (struct i40e_rx_queue **)dev->data->rx_queues;
1334         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1335
1336         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1337                 rxq[i]->qrx_tail = hw->hw_addr + I40E_QRX_TAIL1(i);
1338                 I40E_PCI_REG_WRITE(rxq[i]->qrx_tail, rxq[i]->nb_rx_desc - 1);
1339         }
1340
1341         /* Flush the operation to write registers */
1342         I40EVF_WRITE_FLUSH(hw);
1343
1344         return 0;
1345 }
1346
1347 static void
1348 i40evf_tx_init(struct rte_eth_dev *dev)
1349 {
1350         uint16_t i;
1351         struct i40e_tx_queue **txq =
1352                 (struct i40e_tx_queue **)dev->data->tx_queues;
1353         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1354
1355         for (i = 0; i < dev->data->nb_tx_queues; i++)
1356                 txq[i]->qtx_tail = hw->hw_addr + I40E_QTX_TAIL1(i);
1357 }
1358
1359 static inline void
1360 i40evf_enable_queues_intr(struct i40e_hw *hw)
1361 {
1362         I40E_WRITE_REG(hw, I40E_VFINT_DYN_CTLN1(I40EVF_VSI_DEFAULT_MSIX_INTR - 1),
1363                         I40E_VFINT_DYN_CTLN1_INTENA_MASK |
1364                         I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
1365 }
1366
1367 static inline void
1368 i40evf_disable_queues_intr(struct i40e_hw *hw)
1369 {
1370         I40E_WRITE_REG(hw, I40E_VFINT_DYN_CTLN1(I40EVF_VSI_DEFAULT_MSIX_INTR - 1),
1371                         0);
1372 }
1373
1374 static int
1375 i40evf_dev_start(struct rte_eth_dev *dev)
1376 {
1377         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1378         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1379         struct ether_addr mac_addr;
1380
1381         PMD_INIT_FUNC_TRACE();
1382
1383         vf->max_pkt_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
1384         if (dev->data->dev_conf.rxmode.jumbo_frame == 1) {
1385                 if (vf->max_pkt_len <= ETHER_MAX_LEN ||
1386                         vf->max_pkt_len > I40E_FRAME_SIZE_MAX) {
1387                         PMD_DRV_LOG(ERR, "maximum packet length must "
1388                                     "be larger than %u and smaller than %u,"
1389                                     "as jumbo frame is enabled",
1390                                     (uint32_t)ETHER_MAX_LEN,
1391                                     (uint32_t)I40E_FRAME_SIZE_MAX);
1392                         return I40E_ERR_CONFIG;
1393                 }
1394         } else {
1395                 if (vf->max_pkt_len < ETHER_MIN_LEN ||
1396                         vf->max_pkt_len > ETHER_MAX_LEN) {
1397                         PMD_DRV_LOG(ERR, "maximum packet length must be "
1398                                     "larger than %u and smaller than %u, "
1399                                     "as jumbo frame is disabled",
1400                                     (uint32_t)ETHER_MIN_LEN,
1401                                     (uint32_t)ETHER_MAX_LEN);
1402                         return I40E_ERR_CONFIG;
1403                 }
1404         }
1405
1406         vf->num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
1407                                         dev->data->nb_tx_queues);
1408
1409         if (i40evf_rx_init(dev) != 0){
1410                 PMD_DRV_LOG(ERR, "failed to do RX init");
1411                 return -1;
1412         }
1413
1414         i40evf_tx_init(dev);
1415
1416         if (i40evf_configure_queues(dev) != 0) {
1417                 PMD_DRV_LOG(ERR, "configure queues failed");
1418                 goto err_queue;
1419         }
1420         if (i40evf_config_irq_map(dev)) {
1421                 PMD_DRV_LOG(ERR, "config_irq_map failed");
1422                 goto err_queue;
1423         }
1424
1425         /* Set mac addr */
1426         (void)rte_memcpy(mac_addr.addr_bytes, hw->mac.addr,
1427                                 sizeof(mac_addr.addr_bytes));
1428         if (i40evf_add_mac_addr(dev, &mac_addr)) {
1429                 PMD_DRV_LOG(ERR, "Failed to add mac addr");
1430                 goto err_queue;
1431         }
1432
1433         if (i40evf_start_queues(dev) != 0) {
1434                 PMD_DRV_LOG(ERR, "enable queues failed");
1435                 goto err_mac;
1436         }
1437
1438         i40evf_enable_queues_intr(hw);
1439         return 0;
1440
1441 err_mac:
1442         i40evf_del_mac_addr(dev, &mac_addr);
1443 err_queue:
1444         return -1;
1445 }
1446
1447 static void
1448 i40evf_dev_stop(struct rte_eth_dev *dev)
1449 {
1450         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1451
1452         PMD_INIT_FUNC_TRACE();
1453
1454         i40evf_disable_queues_intr(hw);
1455         i40evf_stop_queues(dev);
1456 }
1457
1458 static int
1459 i40evf_dev_link_update(struct rte_eth_dev *dev,
1460                        __rte_unused int wait_to_complete)
1461 {
1462         struct rte_eth_link new_link;
1463         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1464         /*
1465          * DPDK pf host provide interfacet to acquire link status
1466          * while Linux driver does not
1467          */
1468         if (vf->host_is_dpdk)
1469                 i40evf_get_link_status(dev, &new_link);
1470         else {
1471                 /* Always assume it's up, for Linux driver PF host */
1472                 new_link.link_duplex = ETH_LINK_AUTONEG_DUPLEX;
1473                 new_link.link_speed  = ETH_LINK_SPEED_10000;
1474                 new_link.link_status = 1;
1475         }
1476         i40evf_dev_atomic_write_link_status(dev, &new_link);
1477
1478         return 0;
1479 }
1480
1481 static void
1482 i40evf_dev_promiscuous_enable(struct rte_eth_dev *dev)
1483 {
1484         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1485         int ret;
1486
1487         /* If enabled, just return */
1488         if (vf->promisc_unicast_enabled)
1489                 return;
1490
1491         ret = i40evf_config_promisc(dev, 1, vf->promisc_multicast_enabled);
1492         if (ret == 0)
1493                 vf->promisc_unicast_enabled = TRUE;
1494 }
1495
1496 static void
1497 i40evf_dev_promiscuous_disable(struct rte_eth_dev *dev)
1498 {
1499         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1500         int ret;
1501
1502         /* If disabled, just return */
1503         if (!vf->promisc_unicast_enabled)
1504                 return;
1505
1506         ret = i40evf_config_promisc(dev, 0, vf->promisc_multicast_enabled);
1507         if (ret == 0)
1508                 vf->promisc_unicast_enabled = FALSE;
1509 }
1510
1511 static void
1512 i40evf_dev_allmulticast_enable(struct rte_eth_dev *dev)
1513 {
1514         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1515         int ret;
1516
1517         /* If enabled, just return */
1518         if (vf->promisc_multicast_enabled)
1519                 return;
1520
1521         ret = i40evf_config_promisc(dev, vf->promisc_unicast_enabled, 1);
1522         if (ret == 0)
1523                 vf->promisc_multicast_enabled = TRUE;
1524 }
1525
1526 static void
1527 i40evf_dev_allmulticast_disable(struct rte_eth_dev *dev)
1528 {
1529         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1530         int ret;
1531
1532         /* If enabled, just return */
1533         if (!vf->promisc_multicast_enabled)
1534                 return;
1535
1536         ret = i40evf_config_promisc(dev, vf->promisc_unicast_enabled, 0);
1537         if (ret == 0)
1538                 vf->promisc_multicast_enabled = FALSE;
1539 }
1540
1541 static void
1542 i40evf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1543 {
1544         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1545
1546         memset(dev_info, 0, sizeof(*dev_info));
1547         dev_info->max_rx_queues = vf->vsi_res->num_queue_pairs;
1548         dev_info->max_tx_queues = vf->vsi_res->num_queue_pairs;
1549         dev_info->min_rx_bufsize = I40E_BUF_SIZE_MIN;
1550         dev_info->max_rx_pktlen = I40E_FRAME_SIZE_MAX;
1551 }
1552
1553 static void
1554 i40evf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1555 {
1556         memset(stats, 0, sizeof(*stats));
1557         if (i40evf_get_statics(dev, stats))
1558                 PMD_DRV_LOG(ERR, "Get statics failed");
1559 }
1560
1561 static void
1562 i40evf_dev_close(struct rte_eth_dev *dev)
1563 {
1564         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1565
1566         i40evf_dev_stop(dev);
1567         i40evf_reset_vf(hw);
1568         i40e_shutdown_adminq(hw);
1569 }