Imported Upstream version 16.11.1
[deb_dpdk.git] / drivers / net / i40e / i40e_ethdev_vf.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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_eal.h>
55 #include <rte_alarm.h>
56 #include <rte_ether.h>
57 #include <rte_ethdev.h>
58 #include <rte_atomic.h>
59 #include <rte_malloc.h>
60 #include <rte_dev.h>
61
62 #include "i40e_logs.h"
63 #include "base/i40e_prototype.h"
64 #include "base/i40e_adminq_cmd.h"
65 #include "base/i40e_type.h"
66
67 #include "i40e_rxtx.h"
68 #include "i40e_ethdev.h"
69 #include "i40e_pf.h"
70 #define I40EVF_VSI_DEFAULT_MSIX_INTR     1
71 #define I40EVF_VSI_DEFAULT_MSIX_INTR_LNX 0
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 buf_len;
82         uint16_t msg_len;
83         uint8_t *msg;
84 };
85
86 struct vf_cmd_info {
87         enum i40e_virtchnl_ops ops;
88         uint8_t *in_args;
89         uint32_t in_args_size;
90         uint8_t *out_buffer;
91         /* Input & output type. pass in buffer size and pass out
92          * actual return result
93          */
94         uint32_t out_size;
95 };
96
97 enum i40evf_aq_result {
98         I40EVF_MSG_ERR = -1, /* Meet error when accessing admin queue */
99         I40EVF_MSG_NON,      /* Read nothing from admin queue */
100         I40EVF_MSG_SYS,      /* Read system msg from admin queue */
101         I40EVF_MSG_CMD,      /* Read async command result */
102 };
103
104 static int i40evf_dev_configure(struct rte_eth_dev *dev);
105 static int i40evf_dev_start(struct rte_eth_dev *dev);
106 static void i40evf_dev_stop(struct rte_eth_dev *dev);
107 static void i40evf_dev_info_get(struct rte_eth_dev *dev,
108                                 struct rte_eth_dev_info *dev_info);
109 static int i40evf_dev_link_update(struct rte_eth_dev *dev,
110                                   __rte_unused int wait_to_complete);
111 static void i40evf_dev_stats_get(struct rte_eth_dev *dev,
112                                 struct rte_eth_stats *stats);
113 static int i40evf_dev_xstats_get(struct rte_eth_dev *dev,
114                                  struct rte_eth_xstat *xstats, unsigned n);
115 static int i40evf_dev_xstats_get_names(struct rte_eth_dev *dev,
116                                        struct rte_eth_xstat_name *xstats_names,
117                                        unsigned limit);
118 static void i40evf_dev_xstats_reset(struct rte_eth_dev *dev);
119 static int i40evf_vlan_filter_set(struct rte_eth_dev *dev,
120                                   uint16_t vlan_id, int on);
121 static void i40evf_vlan_offload_set(struct rte_eth_dev *dev, int mask);
122 static int i40evf_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid,
123                                 int on);
124 static void i40evf_dev_close(struct rte_eth_dev *dev);
125 static void i40evf_dev_promiscuous_enable(struct rte_eth_dev *dev);
126 static void i40evf_dev_promiscuous_disable(struct rte_eth_dev *dev);
127 static void i40evf_dev_allmulticast_enable(struct rte_eth_dev *dev);
128 static void i40evf_dev_allmulticast_disable(struct rte_eth_dev *dev);
129 static int i40evf_init_vlan(struct rte_eth_dev *dev);
130 static int i40evf_dev_rx_queue_start(struct rte_eth_dev *dev,
131                                      uint16_t rx_queue_id);
132 static int i40evf_dev_rx_queue_stop(struct rte_eth_dev *dev,
133                                     uint16_t rx_queue_id);
134 static int i40evf_dev_tx_queue_start(struct rte_eth_dev *dev,
135                                      uint16_t tx_queue_id);
136 static int i40evf_dev_tx_queue_stop(struct rte_eth_dev *dev,
137                                     uint16_t tx_queue_id);
138 static void i40evf_add_mac_addr(struct rte_eth_dev *dev,
139                                 struct ether_addr *addr,
140                                 uint32_t index,
141                                 uint32_t pool);
142 static void i40evf_del_mac_addr(struct rte_eth_dev *dev, uint32_t index);
143 static int i40evf_dev_rss_reta_update(struct rte_eth_dev *dev,
144                         struct rte_eth_rss_reta_entry64 *reta_conf,
145                         uint16_t reta_size);
146 static int i40evf_dev_rss_reta_query(struct rte_eth_dev *dev,
147                         struct rte_eth_rss_reta_entry64 *reta_conf,
148                         uint16_t reta_size);
149 static int i40evf_config_rss(struct i40e_vf *vf);
150 static int i40evf_dev_rss_hash_update(struct rte_eth_dev *dev,
151                                       struct rte_eth_rss_conf *rss_conf);
152 static int i40evf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
153                                         struct rte_eth_rss_conf *rss_conf);
154 static int
155 i40evf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id);
156 static int
157 i40evf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id);
158 static void i40evf_handle_pf_event(__rte_unused struct rte_eth_dev *dev,
159                                    uint8_t *msg,
160                                    uint16_t msglen);
161
162 /* Default hash key buffer for RSS */
163 static uint32_t rss_key_default[I40E_VFQF_HKEY_MAX_INDEX + 1];
164
165 struct rte_i40evf_xstats_name_off {
166         char name[RTE_ETH_XSTATS_NAME_SIZE];
167         unsigned offset;
168 };
169
170 static const struct rte_i40evf_xstats_name_off rte_i40evf_stats_strings[] = {
171         {"rx_bytes", offsetof(struct i40e_eth_stats, rx_bytes)},
172         {"rx_unicast_packets", offsetof(struct i40e_eth_stats, rx_unicast)},
173         {"rx_multicast_packets", offsetof(struct i40e_eth_stats, rx_multicast)},
174         {"rx_broadcast_packets", offsetof(struct i40e_eth_stats, rx_broadcast)},
175         {"rx_dropped_packets", offsetof(struct i40e_eth_stats, rx_discards)},
176         {"rx_unknown_protocol_packets", offsetof(struct i40e_eth_stats,
177                 rx_unknown_protocol)},
178         {"tx_bytes", offsetof(struct i40e_eth_stats, tx_bytes)},
179         {"tx_unicast_packets", offsetof(struct i40e_eth_stats, tx_unicast)},
180         {"tx_multicast_packets", offsetof(struct i40e_eth_stats, tx_multicast)},
181         {"tx_broadcast_packets", offsetof(struct i40e_eth_stats, tx_broadcast)},
182         {"tx_dropped_packets", offsetof(struct i40e_eth_stats, tx_discards)},
183         {"tx_error_packets", offsetof(struct i40e_eth_stats, tx_errors)},
184 };
185
186 #define I40EVF_NB_XSTATS (sizeof(rte_i40evf_stats_strings) / \
187                 sizeof(rte_i40evf_stats_strings[0]))
188
189 static const struct eth_dev_ops i40evf_eth_dev_ops = {
190         .dev_configure        = i40evf_dev_configure,
191         .dev_start            = i40evf_dev_start,
192         .dev_stop             = i40evf_dev_stop,
193         .promiscuous_enable   = i40evf_dev_promiscuous_enable,
194         .promiscuous_disable  = i40evf_dev_promiscuous_disable,
195         .allmulticast_enable  = i40evf_dev_allmulticast_enable,
196         .allmulticast_disable = i40evf_dev_allmulticast_disable,
197         .link_update          = i40evf_dev_link_update,
198         .stats_get            = i40evf_dev_stats_get,
199         .xstats_get           = i40evf_dev_xstats_get,
200         .xstats_get_names     = i40evf_dev_xstats_get_names,
201         .xstats_reset         = i40evf_dev_xstats_reset,
202         .dev_close            = i40evf_dev_close,
203         .dev_infos_get        = i40evf_dev_info_get,
204         .dev_supported_ptypes_get = i40e_dev_supported_ptypes_get,
205         .vlan_filter_set      = i40evf_vlan_filter_set,
206         .vlan_offload_set     = i40evf_vlan_offload_set,
207         .vlan_pvid_set        = i40evf_vlan_pvid_set,
208         .rx_queue_start       = i40evf_dev_rx_queue_start,
209         .rx_queue_stop        = i40evf_dev_rx_queue_stop,
210         .tx_queue_start       = i40evf_dev_tx_queue_start,
211         .tx_queue_stop        = i40evf_dev_tx_queue_stop,
212         .rx_queue_setup       = i40e_dev_rx_queue_setup,
213         .rx_queue_release     = i40e_dev_rx_queue_release,
214         .rx_queue_intr_enable = i40evf_dev_rx_queue_intr_enable,
215         .rx_queue_intr_disable = i40evf_dev_rx_queue_intr_disable,
216         .rx_descriptor_done   = i40e_dev_rx_descriptor_done,
217         .tx_queue_setup       = i40e_dev_tx_queue_setup,
218         .tx_queue_release     = i40e_dev_tx_queue_release,
219         .rx_queue_count       = i40e_dev_rx_queue_count,
220         .rxq_info_get         = i40e_rxq_info_get,
221         .txq_info_get         = i40e_txq_info_get,
222         .mac_addr_add         = i40evf_add_mac_addr,
223         .mac_addr_remove      = i40evf_del_mac_addr,
224         .reta_update          = i40evf_dev_rss_reta_update,
225         .reta_query           = i40evf_dev_rss_reta_query,
226         .rss_hash_update      = i40evf_dev_rss_hash_update,
227         .rss_hash_conf_get    = i40evf_dev_rss_hash_conf_get,
228 };
229
230 /*
231  * Read data in admin queue to get msg from pf driver
232  */
233 static enum i40evf_aq_result
234 i40evf_read_pfmsg(struct rte_eth_dev *dev, struct i40evf_arq_msg_info *data)
235 {
236         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
237         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
238         struct i40e_arq_event_info event;
239         enum i40e_virtchnl_ops opcode;
240         enum i40e_status_code retval;
241         int ret;
242         enum i40evf_aq_result result = I40EVF_MSG_NON;
243
244         event.buf_len = data->buf_len;
245         event.msg_buf = data->msg;
246         ret = i40e_clean_arq_element(hw, &event, NULL);
247         /* Can't read any msg from adminQ */
248         if (ret) {
249                 if (ret != I40E_ERR_ADMIN_QUEUE_NO_WORK)
250                         result = I40EVF_MSG_ERR;
251                 return result;
252         }
253
254         opcode = (enum i40e_virtchnl_ops)rte_le_to_cpu_32(event.desc.cookie_high);
255         retval = (enum i40e_status_code)rte_le_to_cpu_32(event.desc.cookie_low);
256         /* pf sys event */
257         if (opcode == I40E_VIRTCHNL_OP_EVENT) {
258                 struct i40e_virtchnl_pf_event *vpe =
259                         (struct i40e_virtchnl_pf_event *)event.msg_buf;
260
261                 result = I40EVF_MSG_SYS;
262                 switch (vpe->event) {
263                 case I40E_VIRTCHNL_EVENT_LINK_CHANGE:
264                         vf->link_up =
265                                 vpe->event_data.link_event.link_status;
266                         vf->link_speed =
267                                 vpe->event_data.link_event.link_speed;
268                         vf->pend_msg |= PFMSG_LINK_CHANGE;
269                         PMD_DRV_LOG(INFO, "Link status update:%s",
270                                     vf->link_up ? "up" : "down");
271                         break;
272                 case I40E_VIRTCHNL_EVENT_RESET_IMPENDING:
273                         vf->vf_reset = true;
274                         vf->pend_msg |= PFMSG_RESET_IMPENDING;
275                         PMD_DRV_LOG(INFO, "vf is reseting");
276                         break;
277                 case I40E_VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
278                         vf->dev_closed = true;
279                         vf->pend_msg |= PFMSG_DRIVER_CLOSE;
280                         PMD_DRV_LOG(INFO, "PF driver closed");
281                         break;
282                 default:
283                         PMD_DRV_LOG(ERR, "%s: Unknown event %d from pf",
284                                     __func__, vpe->event);
285                 }
286         } else {
287                 /* async reply msg on command issued by vf previously */
288                 result = I40EVF_MSG_CMD;
289                 /* Actual data length read from PF */
290                 data->msg_len = event.msg_len;
291         }
292
293         data->result = retval;
294         data->ops = opcode;
295
296         return result;
297 }
298
299 /**
300  * clear current command. Only call in case execute
301  * _atomic_set_cmd successfully.
302  */
303 static inline void
304 _clear_cmd(struct i40e_vf *vf)
305 {
306         rte_wmb();
307         vf->pend_cmd = I40E_VIRTCHNL_OP_UNKNOWN;
308 }
309
310 /*
311  * Check there is pending cmd in execution. If none, set new command.
312  */
313 static inline int
314 _atomic_set_cmd(struct i40e_vf *vf, enum i40e_virtchnl_ops ops)
315 {
316         int ret = rte_atomic32_cmpset(&vf->pend_cmd,
317                         I40E_VIRTCHNL_OP_UNKNOWN, ops);
318
319         if (!ret)
320                 PMD_DRV_LOG(ERR, "There is incomplete cmd %d", vf->pend_cmd);
321
322         return !ret;
323 }
324
325 #define MAX_TRY_TIMES 200
326 #define ASQ_DELAY_MS  10
327
328 static int
329 i40evf_execute_vf_cmd(struct rte_eth_dev *dev, struct vf_cmd_info *args)
330 {
331         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
332         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
333         struct i40evf_arq_msg_info info;
334         enum i40evf_aq_result ret;
335         int err, i = 0;
336
337         if (_atomic_set_cmd(vf, args->ops))
338                 return -1;
339
340         info.msg = args->out_buffer;
341         info.buf_len = args->out_size;
342         info.ops = I40E_VIRTCHNL_OP_UNKNOWN;
343         info.result = I40E_SUCCESS;
344
345         err = i40e_aq_send_msg_to_pf(hw, args->ops, I40E_SUCCESS,
346                      args->in_args, args->in_args_size, NULL);
347         if (err) {
348                 PMD_DRV_LOG(ERR, "fail to send cmd %d", args->ops);
349                 _clear_cmd(vf);
350                 return err;
351         }
352
353         switch (args->ops) {
354         case I40E_VIRTCHNL_OP_RESET_VF:
355                 /*no need to process in this function */
356                 err = 0;
357                 break;
358         case I40E_VIRTCHNL_OP_VERSION:
359         case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
360                 /* for init adminq commands, need to poll the response */
361                 err = -1;
362                 do {
363                         ret = i40evf_read_pfmsg(dev, &info);
364                         vf->cmd_retval = info.result;
365                         if (ret == I40EVF_MSG_CMD) {
366                                 err = 0;
367                                 break;
368                         } else if (ret == I40EVF_MSG_ERR)
369                                 break;
370                         rte_delay_ms(ASQ_DELAY_MS);
371                         /* If don't read msg or read sys event, continue */
372                 } while (i++ < MAX_TRY_TIMES);
373                 _clear_cmd(vf);
374                 break;
375
376         default:
377                 /* for other adminq in running time, waiting the cmd done flag */
378                 err = -1;
379                 do {
380                         if (vf->pend_cmd == I40E_VIRTCHNL_OP_UNKNOWN) {
381                                 err = 0;
382                                 break;
383                         }
384                         rte_delay_ms(ASQ_DELAY_MS);
385                         /* If don't read msg or read sys event, continue */
386                 } while (i++ < MAX_TRY_TIMES);
387                 break;
388         }
389
390         return err | vf->cmd_retval;
391 }
392
393 /*
394  * Check API version with sync wait until version read or fail from admin queue
395  */
396 static int
397 i40evf_check_api_version(struct rte_eth_dev *dev)
398 {
399         struct i40e_virtchnl_version_info version, *pver;
400         int err;
401         struct vf_cmd_info args;
402         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
403
404         version.major = I40E_VIRTCHNL_VERSION_MAJOR;
405         version.minor = I40E_VIRTCHNL_VERSION_MINOR;
406
407         args.ops = I40E_VIRTCHNL_OP_VERSION;
408         args.in_args = (uint8_t *)&version;
409         args.in_args_size = sizeof(version);
410         args.out_buffer = vf->aq_resp;
411         args.out_size = I40E_AQ_BUF_SZ;
412
413         err = i40evf_execute_vf_cmd(dev, &args);
414         if (err) {
415                 PMD_INIT_LOG(ERR, "fail to execute command OP_VERSION");
416                 return err;
417         }
418
419         pver = (struct i40e_virtchnl_version_info *)args.out_buffer;
420         vf->version_major = pver->major;
421         vf->version_minor = pver->minor;
422         if (vf->version_major == I40E_DPDK_VERSION_MAJOR)
423                 PMD_DRV_LOG(INFO, "Peer is DPDK PF host");
424         else if ((vf->version_major == I40E_VIRTCHNL_VERSION_MAJOR) &&
425                 (vf->version_minor <= I40E_VIRTCHNL_VERSION_MINOR))
426                 PMD_DRV_LOG(INFO, "Peer is Linux PF host");
427         else {
428                 PMD_INIT_LOG(ERR, "PF/VF API version mismatch:(%u.%u)-(%u.%u)",
429                                         vf->version_major, vf->version_minor,
430                                                 I40E_VIRTCHNL_VERSION_MAJOR,
431                                                 I40E_VIRTCHNL_VERSION_MINOR);
432                 return -1;
433         }
434
435         return 0;
436 }
437
438 static int
439 i40evf_get_vf_resource(struct rte_eth_dev *dev)
440 {
441         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
442         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
443         int err;
444         struct vf_cmd_info args;
445         uint32_t caps, len;
446
447         args.ops = I40E_VIRTCHNL_OP_GET_VF_RESOURCES;
448         args.out_buffer = vf->aq_resp;
449         args.out_size = I40E_AQ_BUF_SZ;
450         if (PF_IS_V11(vf)) {
451                 caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
452                        I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ |
453                        I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
454                        I40E_VIRTCHNL_VF_OFFLOAD_VLAN |
455                        I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING;
456                 args.in_args = (uint8_t *)&caps;
457                 args.in_args_size = sizeof(caps);
458         } else {
459                 args.in_args = NULL;
460                 args.in_args_size = 0;
461         }
462         err = i40evf_execute_vf_cmd(dev, &args);
463
464         if (err) {
465                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_VF_RESOURCE");
466                 return err;
467         }
468
469         len =  sizeof(struct i40e_virtchnl_vf_resource) +
470                 I40E_MAX_VF_VSI * sizeof(struct i40e_virtchnl_vsi_resource);
471
472         (void)rte_memcpy(vf->vf_res, args.out_buffer,
473                         RTE_MIN(args.out_size, len));
474         i40e_vf_parse_hw_config(hw, vf->vf_res);
475
476         return 0;
477 }
478
479 static int
480 i40evf_config_promisc(struct rte_eth_dev *dev,
481                       bool enable_unicast,
482                       bool enable_multicast)
483 {
484         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
485         int err;
486         struct vf_cmd_info args;
487         struct i40e_virtchnl_promisc_info promisc;
488
489         promisc.flags = 0;
490         promisc.vsi_id = vf->vsi_res->vsi_id;
491
492         if (enable_unicast)
493                 promisc.flags |= I40E_FLAG_VF_UNICAST_PROMISC;
494
495         if (enable_multicast)
496                 promisc.flags |= I40E_FLAG_VF_MULTICAST_PROMISC;
497
498         args.ops = I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
499         args.in_args = (uint8_t *)&promisc;
500         args.in_args_size = sizeof(promisc);
501         args.out_buffer = vf->aq_resp;
502         args.out_size = I40E_AQ_BUF_SZ;
503
504         err = i40evf_execute_vf_cmd(dev, &args);
505
506         if (err)
507                 PMD_DRV_LOG(ERR, "fail to execute command "
508                             "CONFIG_PROMISCUOUS_MODE");
509         return err;
510 }
511
512 /* Configure vlan and double vlan offload. Use flag to specify which part to configure */
513 static int
514 i40evf_config_vlan_offload(struct rte_eth_dev *dev,
515                                 bool enable_vlan_strip)
516 {
517         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
518         int err;
519         struct vf_cmd_info args;
520         struct i40e_virtchnl_vlan_offload_info offload;
521
522         offload.vsi_id = vf->vsi_res->vsi_id;
523         offload.enable_vlan_strip = enable_vlan_strip;
524
525         args.ops = (enum i40e_virtchnl_ops)I40E_VIRTCHNL_OP_CFG_VLAN_OFFLOAD;
526         args.in_args = (uint8_t *)&offload;
527         args.in_args_size = sizeof(offload);
528         args.out_buffer = vf->aq_resp;
529         args.out_size = I40E_AQ_BUF_SZ;
530
531         err = i40evf_execute_vf_cmd(dev, &args);
532         if (err)
533                 PMD_DRV_LOG(ERR, "fail to execute command CFG_VLAN_OFFLOAD");
534
535         return err;
536 }
537
538 static int
539 i40evf_config_vlan_pvid(struct rte_eth_dev *dev,
540                                 struct i40e_vsi_vlan_pvid_info *info)
541 {
542         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
543         int err;
544         struct vf_cmd_info args;
545         struct i40e_virtchnl_pvid_info tpid_info;
546
547         if (info == NULL) {
548                 PMD_DRV_LOG(ERR, "invalid parameters");
549                 return I40E_ERR_PARAM;
550         }
551
552         memset(&tpid_info, 0, sizeof(tpid_info));
553         tpid_info.vsi_id = vf->vsi_res->vsi_id;
554         (void)rte_memcpy(&tpid_info.info, info, sizeof(*info));
555
556         args.ops = (enum i40e_virtchnl_ops)I40E_VIRTCHNL_OP_CFG_VLAN_PVID;
557         args.in_args = (uint8_t *)&tpid_info;
558         args.in_args_size = sizeof(tpid_info);
559         args.out_buffer = vf->aq_resp;
560         args.out_size = I40E_AQ_BUF_SZ;
561
562         err = i40evf_execute_vf_cmd(dev, &args);
563         if (err)
564                 PMD_DRV_LOG(ERR, "fail to execute command CFG_VLAN_PVID");
565
566         return err;
567 }
568
569 static void
570 i40evf_fill_virtchnl_vsi_txq_info(struct i40e_virtchnl_txq_info *txq_info,
571                                   uint16_t vsi_id,
572                                   uint16_t queue_id,
573                                   uint16_t nb_txq,
574                                   struct i40e_tx_queue *txq)
575 {
576         txq_info->vsi_id = vsi_id;
577         txq_info->queue_id = queue_id;
578         if (queue_id < nb_txq) {
579                 txq_info->ring_len = txq->nb_tx_desc;
580                 txq_info->dma_ring_addr = txq->tx_ring_phys_addr;
581         }
582 }
583
584 static void
585 i40evf_fill_virtchnl_vsi_rxq_info(struct i40e_virtchnl_rxq_info *rxq_info,
586                                   uint16_t vsi_id,
587                                   uint16_t queue_id,
588                                   uint16_t nb_rxq,
589                                   uint32_t max_pkt_size,
590                                   struct i40e_rx_queue *rxq)
591 {
592         rxq_info->vsi_id = vsi_id;
593         rxq_info->queue_id = queue_id;
594         rxq_info->max_pkt_size = max_pkt_size;
595         if (queue_id < nb_rxq) {
596                 rxq_info->ring_len = rxq->nb_rx_desc;
597                 rxq_info->dma_ring_addr = rxq->rx_ring_phys_addr;
598                 rxq_info->databuffer_size =
599                         (rte_pktmbuf_data_room_size(rxq->mp) -
600                                 RTE_PKTMBUF_HEADROOM);
601         }
602 }
603
604 /* It configures VSI queues to co-work with Linux PF host */
605 static int
606 i40evf_configure_vsi_queues(struct rte_eth_dev *dev)
607 {
608         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
609         struct i40e_rx_queue **rxq =
610                 (struct i40e_rx_queue **)dev->data->rx_queues;
611         struct i40e_tx_queue **txq =
612                 (struct i40e_tx_queue **)dev->data->tx_queues;
613         struct i40e_virtchnl_vsi_queue_config_info *vc_vqci;
614         struct i40e_virtchnl_queue_pair_info *vc_qpi;
615         struct vf_cmd_info args;
616         uint16_t i, nb_qp = vf->num_queue_pairs;
617         const uint32_t size =
618                 I40E_VIRTCHNL_CONFIG_VSI_QUEUES_SIZE(vc_vqci, nb_qp);
619         uint8_t buff[size];
620         int ret;
621
622         memset(buff, 0, sizeof(buff));
623         vc_vqci = (struct i40e_virtchnl_vsi_queue_config_info *)buff;
624         vc_vqci->vsi_id = vf->vsi_res->vsi_id;
625         vc_vqci->num_queue_pairs = nb_qp;
626
627         for (i = 0, vc_qpi = vc_vqci->qpair; i < nb_qp; i++, vc_qpi++) {
628                 i40evf_fill_virtchnl_vsi_txq_info(&vc_qpi->txq,
629                         vc_vqci->vsi_id, i, dev->data->nb_tx_queues, txq[i]);
630                 i40evf_fill_virtchnl_vsi_rxq_info(&vc_qpi->rxq,
631                         vc_vqci->vsi_id, i, dev->data->nb_rx_queues,
632                                         vf->max_pkt_len, rxq[i]);
633         }
634         memset(&args, 0, sizeof(args));
635         args.ops = I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES;
636         args.in_args = (uint8_t *)vc_vqci;
637         args.in_args_size = size;
638         args.out_buffer = vf->aq_resp;
639         args.out_size = I40E_AQ_BUF_SZ;
640         ret = i40evf_execute_vf_cmd(dev, &args);
641         if (ret)
642                 PMD_DRV_LOG(ERR, "Failed to execute command of "
643                         "I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES\n");
644
645         return ret;
646 }
647
648 /* It configures VSI queues to co-work with DPDK PF host */
649 static int
650 i40evf_configure_vsi_queues_ext(struct rte_eth_dev *dev)
651 {
652         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
653         struct i40e_rx_queue **rxq =
654                 (struct i40e_rx_queue **)dev->data->rx_queues;
655         struct i40e_tx_queue **txq =
656                 (struct i40e_tx_queue **)dev->data->tx_queues;
657         struct i40e_virtchnl_vsi_queue_config_ext_info *vc_vqcei;
658         struct i40e_virtchnl_queue_pair_ext_info *vc_qpei;
659         struct vf_cmd_info args;
660         uint16_t i, nb_qp = vf->num_queue_pairs;
661         const uint32_t size =
662                 I40E_VIRTCHNL_CONFIG_VSI_QUEUES_SIZE(vc_vqcei, nb_qp);
663         uint8_t buff[size];
664         int ret;
665
666         memset(buff, 0, sizeof(buff));
667         vc_vqcei = (struct i40e_virtchnl_vsi_queue_config_ext_info *)buff;
668         vc_vqcei->vsi_id = vf->vsi_res->vsi_id;
669         vc_vqcei->num_queue_pairs = nb_qp;
670         vc_qpei = vc_vqcei->qpair;
671         for (i = 0; i < nb_qp; i++, vc_qpei++) {
672                 i40evf_fill_virtchnl_vsi_txq_info(&vc_qpei->txq,
673                         vc_vqcei->vsi_id, i, dev->data->nb_tx_queues, txq[i]);
674                 i40evf_fill_virtchnl_vsi_rxq_info(&vc_qpei->rxq,
675                         vc_vqcei->vsi_id, i, dev->data->nb_rx_queues,
676                                         vf->max_pkt_len, rxq[i]);
677                 if (i < dev->data->nb_rx_queues)
678                         /*
679                          * It adds extra info for configuring VSI queues, which
680                          * is needed to enable the configurable crc stripping
681                          * in VF.
682                          */
683                         vc_qpei->rxq_ext.crcstrip =
684                                 dev->data->dev_conf.rxmode.hw_strip_crc;
685         }
686         memset(&args, 0, sizeof(args));
687         args.ops =
688                 (enum i40e_virtchnl_ops)I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT;
689         args.in_args = (uint8_t *)vc_vqcei;
690         args.in_args_size = size;
691         args.out_buffer = vf->aq_resp;
692         args.out_size = I40E_AQ_BUF_SZ;
693         ret = i40evf_execute_vf_cmd(dev, &args);
694         if (ret)
695                 PMD_DRV_LOG(ERR, "Failed to execute command of "
696                         "I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES_EXT\n");
697
698         return ret;
699 }
700
701 static int
702 i40evf_configure_queues(struct rte_eth_dev *dev)
703 {
704         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
705
706         if (vf->version_major == I40E_DPDK_VERSION_MAJOR)
707                 /* To support DPDK PF host */
708                 return i40evf_configure_vsi_queues_ext(dev);
709         else
710                 /* To support Linux PF host */
711                 return i40evf_configure_vsi_queues(dev);
712 }
713
714 static int
715 i40evf_config_irq_map(struct rte_eth_dev *dev)
716 {
717         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
718         struct vf_cmd_info args;
719         uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_irq_map_info) + \
720                 sizeof(struct i40e_virtchnl_vector_map)];
721         struct i40e_virtchnl_irq_map_info *map_info;
722         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
723         uint32_t vector_id;
724         int i, err;
725
726         if (rte_intr_allow_others(intr_handle)) {
727                 if (vf->version_major == I40E_DPDK_VERSION_MAJOR)
728                         vector_id = I40EVF_VSI_DEFAULT_MSIX_INTR;
729                 else
730                         vector_id = I40EVF_VSI_DEFAULT_MSIX_INTR_LNX;
731         } else {
732                 vector_id = I40E_MISC_VEC_ID;
733         }
734
735         map_info = (struct i40e_virtchnl_irq_map_info *)cmd_buffer;
736         map_info->num_vectors = 1;
737         map_info->vecmap[0].rxitr_idx = I40E_ITR_INDEX_DEFAULT;
738         map_info->vecmap[0].vsi_id = vf->vsi_res->vsi_id;
739         /* Alway use default dynamic MSIX interrupt */
740         map_info->vecmap[0].vector_id = vector_id;
741         /* Don't map any tx queue */
742         map_info->vecmap[0].txq_map = 0;
743         map_info->vecmap[0].rxq_map = 0;
744         for (i = 0; i < dev->data->nb_rx_queues; i++) {
745                 map_info->vecmap[0].rxq_map |= 1 << i;
746                 if (rte_intr_dp_is_en(intr_handle))
747                         intr_handle->intr_vec[i] = vector_id;
748         }
749
750         args.ops = I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP;
751         args.in_args = (u8 *)cmd_buffer;
752         args.in_args_size = sizeof(cmd_buffer);
753         args.out_buffer = vf->aq_resp;
754         args.out_size = I40E_AQ_BUF_SZ;
755         err = i40evf_execute_vf_cmd(dev, &args);
756         if (err)
757                 PMD_DRV_LOG(ERR, "fail to execute command OP_ENABLE_QUEUES");
758
759         return err;
760 }
761
762 static int
763 i40evf_switch_queue(struct rte_eth_dev *dev, bool isrx, uint16_t qid,
764                                 bool on)
765 {
766         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
767         struct i40e_virtchnl_queue_select queue_select;
768         int err;
769         struct vf_cmd_info args;
770         memset(&queue_select, 0, sizeof(queue_select));
771         queue_select.vsi_id = vf->vsi_res->vsi_id;
772
773         if (isrx)
774                 queue_select.rx_queues |= 1 << qid;
775         else
776                 queue_select.tx_queues |= 1 << qid;
777
778         if (on)
779                 args.ops = I40E_VIRTCHNL_OP_ENABLE_QUEUES;
780         else
781                 args.ops = I40E_VIRTCHNL_OP_DISABLE_QUEUES;
782         args.in_args = (u8 *)&queue_select;
783         args.in_args_size = sizeof(queue_select);
784         args.out_buffer = vf->aq_resp;
785         args.out_size = I40E_AQ_BUF_SZ;
786         err = i40evf_execute_vf_cmd(dev, &args);
787         if (err)
788                 PMD_DRV_LOG(ERR, "fail to switch %s %u %s",
789                             isrx ? "RX" : "TX", qid, on ? "on" : "off");
790
791         return err;
792 }
793
794 static int
795 i40evf_start_queues(struct rte_eth_dev *dev)
796 {
797         struct rte_eth_dev_data *dev_data = dev->data;
798         int i;
799         struct i40e_rx_queue *rxq;
800         struct i40e_tx_queue *txq;
801
802         for (i = 0; i < dev->data->nb_rx_queues; i++) {
803                 rxq = dev_data->rx_queues[i];
804                 if (rxq->rx_deferred_start)
805                         continue;
806                 if (i40evf_dev_rx_queue_start(dev, i) != 0) {
807                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
808                         return -1;
809                 }
810         }
811
812         for (i = 0; i < dev->data->nb_tx_queues; i++) {
813                 txq = dev_data->tx_queues[i];
814                 if (txq->tx_deferred_start)
815                         continue;
816                 if (i40evf_dev_tx_queue_start(dev, i) != 0) {
817                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
818                         return -1;
819                 }
820         }
821
822         return 0;
823 }
824
825 static int
826 i40evf_stop_queues(struct rte_eth_dev *dev)
827 {
828         int i;
829
830         /* Stop TX queues first */
831         for (i = 0; i < dev->data->nb_tx_queues; i++) {
832                 if (i40evf_dev_tx_queue_stop(dev, i) != 0) {
833                         PMD_DRV_LOG(ERR, "Fail to stop queue %u", i);
834                         return -1;
835                 }
836         }
837
838         /* Then stop RX queues */
839         for (i = 0; i < dev->data->nb_rx_queues; i++) {
840                 if (i40evf_dev_rx_queue_stop(dev, i) != 0) {
841                         PMD_DRV_LOG(ERR, "Fail to stop queue %u", i);
842                         return -1;
843                 }
844         }
845
846         return 0;
847 }
848
849 static void
850 i40evf_add_mac_addr(struct rte_eth_dev *dev,
851                     struct ether_addr *addr,
852                     __rte_unused uint32_t index,
853                     __rte_unused uint32_t pool)
854 {
855         struct i40e_virtchnl_ether_addr_list *list;
856         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
857         uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_ether_addr_list) + \
858                         sizeof(struct i40e_virtchnl_ether_addr)];
859         int err;
860         struct vf_cmd_info args;
861
862         if (i40e_validate_mac_addr(addr->addr_bytes) != I40E_SUCCESS) {
863                 PMD_DRV_LOG(ERR, "Invalid mac:%x:%x:%x:%x:%x:%x",
864                             addr->addr_bytes[0], addr->addr_bytes[1],
865                             addr->addr_bytes[2], addr->addr_bytes[3],
866                             addr->addr_bytes[4], addr->addr_bytes[5]);
867                 return;
868         }
869
870         list = (struct i40e_virtchnl_ether_addr_list *)cmd_buffer;
871         list->vsi_id = vf->vsi_res->vsi_id;
872         list->num_elements = 1;
873         (void)rte_memcpy(list->list[0].addr, addr->addr_bytes,
874                                         sizeof(addr->addr_bytes));
875
876         args.ops = I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS;
877         args.in_args = cmd_buffer;
878         args.in_args_size = sizeof(cmd_buffer);
879         args.out_buffer = vf->aq_resp;
880         args.out_size = I40E_AQ_BUF_SZ;
881         err = i40evf_execute_vf_cmd(dev, &args);
882         if (err)
883                 PMD_DRV_LOG(ERR, "fail to execute command "
884                             "OP_ADD_ETHER_ADDRESS");
885
886         return;
887 }
888
889 static void
890 i40evf_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
891 {
892         struct i40e_virtchnl_ether_addr_list *list;
893         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
894         struct rte_eth_dev_data *data = dev->data;
895         struct ether_addr *addr;
896         uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_ether_addr_list) + \
897                         sizeof(struct i40e_virtchnl_ether_addr)];
898         int err;
899         struct vf_cmd_info args;
900
901         addr = &(data->mac_addrs[index]);
902
903         if (i40e_validate_mac_addr(addr->addr_bytes) != I40E_SUCCESS) {
904                 PMD_DRV_LOG(ERR, "Invalid mac:%x-%x-%x-%x-%x-%x",
905                             addr->addr_bytes[0], addr->addr_bytes[1],
906                             addr->addr_bytes[2], addr->addr_bytes[3],
907                             addr->addr_bytes[4], addr->addr_bytes[5]);
908                 return;
909         }
910
911         list = (struct i40e_virtchnl_ether_addr_list *)cmd_buffer;
912         list->vsi_id = vf->vsi_res->vsi_id;
913         list->num_elements = 1;
914         (void)rte_memcpy(list->list[0].addr, addr->addr_bytes,
915                         sizeof(addr->addr_bytes));
916
917         args.ops = I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS;
918         args.in_args = cmd_buffer;
919         args.in_args_size = sizeof(cmd_buffer);
920         args.out_buffer = vf->aq_resp;
921         args.out_size = I40E_AQ_BUF_SZ;
922         err = i40evf_execute_vf_cmd(dev, &args);
923         if (err)
924                 PMD_DRV_LOG(ERR, "fail to execute command "
925                             "OP_DEL_ETHER_ADDRESS");
926         return;
927 }
928
929 static int
930 i40evf_update_stats(struct rte_eth_dev *dev, struct i40e_eth_stats **pstats)
931 {
932         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
933         struct i40e_virtchnl_queue_select q_stats;
934         int err;
935         struct vf_cmd_info args;
936
937         memset(&q_stats, 0, sizeof(q_stats));
938         q_stats.vsi_id = vf->vsi_res->vsi_id;
939         args.ops = I40E_VIRTCHNL_OP_GET_STATS;
940         args.in_args = (u8 *)&q_stats;
941         args.in_args_size = sizeof(q_stats);
942         args.out_buffer = vf->aq_resp;
943         args.out_size = I40E_AQ_BUF_SZ;
944
945         err = i40evf_execute_vf_cmd(dev, &args);
946         if (err) {
947                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_STATS");
948                 *pstats = NULL;
949                 return err;
950         }
951         *pstats = (struct i40e_eth_stats *)args.out_buffer;
952         return 0;
953 }
954
955 static int
956 i40evf_get_statics(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
957 {
958         int ret;
959         struct i40e_eth_stats *pstats = NULL;
960
961         ret = i40evf_update_stats(dev, &pstats);
962         if (ret != 0)
963                 return 0;
964
965         stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
966                                                 pstats->rx_broadcast;
967         stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
968                                                 pstats->tx_unicast;
969         stats->imissed = pstats->rx_discards;
970         stats->oerrors = pstats->tx_errors + pstats->tx_discards;
971         stats->ibytes = pstats->rx_bytes;
972         stats->obytes = pstats->tx_bytes;
973
974         return 0;
975 }
976
977 static void
978 i40evf_dev_xstats_reset(struct rte_eth_dev *dev)
979 {
980         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
981         struct i40e_eth_stats *pstats = NULL;
982
983         /* read stat values to clear hardware registers */
984         i40evf_update_stats(dev, &pstats);
985
986         /* set stats offset base on current values */
987         vf->vsi.eth_stats_offset = vf->vsi.eth_stats;
988 }
989
990 static int i40evf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
991                                       struct rte_eth_xstat_name *xstats_names,
992                                       __rte_unused unsigned limit)
993 {
994         unsigned i;
995
996         if (xstats_names != NULL)
997                 for (i = 0; i < I40EVF_NB_XSTATS; i++) {
998                         snprintf(xstats_names[i].name,
999                                 sizeof(xstats_names[i].name),
1000                                 "%s", rte_i40evf_stats_strings[i].name);
1001                 }
1002         return I40EVF_NB_XSTATS;
1003 }
1004
1005 static int i40evf_dev_xstats_get(struct rte_eth_dev *dev,
1006                                  struct rte_eth_xstat *xstats, unsigned n)
1007 {
1008         int ret;
1009         unsigned i;
1010         struct i40e_eth_stats *pstats = NULL;
1011
1012         if (n < I40EVF_NB_XSTATS)
1013                 return I40EVF_NB_XSTATS;
1014
1015         ret = i40evf_update_stats(dev, &pstats);
1016         if (ret != 0)
1017                 return 0;
1018
1019         if (!xstats)
1020                 return 0;
1021
1022         /* loop over xstats array and values from pstats */
1023         for (i = 0; i < I40EVF_NB_XSTATS; i++) {
1024                 xstats[i].id = i;
1025                 xstats[i].value = *(uint64_t *)(((char *)pstats) +
1026                         rte_i40evf_stats_strings[i].offset);
1027         }
1028
1029         return I40EVF_NB_XSTATS;
1030 }
1031
1032 static int
1033 i40evf_add_vlan(struct rte_eth_dev *dev, uint16_t vlanid)
1034 {
1035         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1036         struct i40e_virtchnl_vlan_filter_list *vlan_list;
1037         uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_vlan_filter_list) +
1038                                                         sizeof(uint16_t)];
1039         int err;
1040         struct vf_cmd_info args;
1041
1042         vlan_list = (struct i40e_virtchnl_vlan_filter_list *)cmd_buffer;
1043         vlan_list->vsi_id = vf->vsi_res->vsi_id;
1044         vlan_list->num_elements = 1;
1045         vlan_list->vlan_id[0] = vlanid;
1046
1047         args.ops = I40E_VIRTCHNL_OP_ADD_VLAN;
1048         args.in_args = (u8 *)&cmd_buffer;
1049         args.in_args_size = sizeof(cmd_buffer);
1050         args.out_buffer = vf->aq_resp;
1051         args.out_size = I40E_AQ_BUF_SZ;
1052         err = i40evf_execute_vf_cmd(dev, &args);
1053         if (err)
1054                 PMD_DRV_LOG(ERR, "fail to execute command OP_ADD_VLAN");
1055
1056         return err;
1057 }
1058
1059 static int
1060 i40evf_del_vlan(struct rte_eth_dev *dev, uint16_t vlanid)
1061 {
1062         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1063         struct i40e_virtchnl_vlan_filter_list *vlan_list;
1064         uint8_t cmd_buffer[sizeof(struct i40e_virtchnl_vlan_filter_list) +
1065                                                         sizeof(uint16_t)];
1066         int err;
1067         struct vf_cmd_info args;
1068
1069         vlan_list = (struct i40e_virtchnl_vlan_filter_list *)cmd_buffer;
1070         vlan_list->vsi_id = vf->vsi_res->vsi_id;
1071         vlan_list->num_elements = 1;
1072         vlan_list->vlan_id[0] = vlanid;
1073
1074         args.ops = I40E_VIRTCHNL_OP_DEL_VLAN;
1075         args.in_args = (u8 *)&cmd_buffer;
1076         args.in_args_size = sizeof(cmd_buffer);
1077         args.out_buffer = vf->aq_resp;
1078         args.out_size = I40E_AQ_BUF_SZ;
1079         err = i40evf_execute_vf_cmd(dev, &args);
1080         if (err)
1081                 PMD_DRV_LOG(ERR, "fail to execute command OP_DEL_VLAN");
1082
1083         return err;
1084 }
1085
1086 static const struct rte_pci_id pci_id_i40evf_map[] = {
1087         { RTE_PCI_DEVICE(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_VF) },
1088         { RTE_PCI_DEVICE(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_VF_HV) },
1089         { RTE_PCI_DEVICE(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_X722_A0_VF) },
1090         { RTE_PCI_DEVICE(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_X722_VF) },
1091         { RTE_PCI_DEVICE(I40E_INTEL_VENDOR_ID, I40E_DEV_ID_X722_VF_HV) },
1092         { .vendor_id = 0, /* sentinel */ },
1093 };
1094
1095 static inline int
1096 i40evf_dev_atomic_write_link_status(struct rte_eth_dev *dev,
1097                                     struct rte_eth_link *link)
1098 {
1099         struct rte_eth_link *dst = &(dev->data->dev_link);
1100         struct rte_eth_link *src = link;
1101
1102         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
1103                                         *(uint64_t *)src) == 0)
1104                 return -1;
1105
1106         return 0;
1107 }
1108
1109 /* Disable IRQ0 */
1110 static inline void
1111 i40evf_disable_irq0(struct i40e_hw *hw)
1112 {
1113         /* Disable all interrupt types */
1114         I40E_WRITE_REG(hw, I40E_VFINT_ICR0_ENA1, 0);
1115         I40E_WRITE_REG(hw, I40E_VFINT_DYN_CTL01,
1116                        I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
1117         I40EVF_WRITE_FLUSH(hw);
1118 }
1119
1120 /* Enable IRQ0 */
1121 static inline void
1122 i40evf_enable_irq0(struct i40e_hw *hw)
1123 {
1124         /* Enable admin queue interrupt trigger */
1125         uint32_t val;
1126
1127         i40evf_disable_irq0(hw);
1128         val = I40E_READ_REG(hw, I40E_VFINT_ICR0_ENA1);
1129         val |= I40E_VFINT_ICR0_ENA1_ADMINQ_MASK |
1130                 I40E_VFINT_ICR0_ENA1_LINK_STAT_CHANGE_MASK;
1131         I40E_WRITE_REG(hw, I40E_VFINT_ICR0_ENA1, val);
1132
1133         I40E_WRITE_REG(hw, I40E_VFINT_DYN_CTL01,
1134                 I40E_VFINT_DYN_CTL01_INTENA_MASK |
1135                 I40E_VFINT_DYN_CTL01_CLEARPBA_MASK |
1136                 I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
1137
1138         I40EVF_WRITE_FLUSH(hw);
1139 }
1140
1141 static int
1142 i40evf_reset_vf(struct i40e_hw *hw)
1143 {
1144         int i, reset;
1145
1146         if (i40e_vf_reset(hw) != I40E_SUCCESS) {
1147                 PMD_INIT_LOG(ERR, "Reset VF NIC failed");
1148                 return -1;
1149         }
1150         /**
1151           * After issuing vf reset command to pf, pf won't necessarily
1152           * reset vf, it depends on what state it exactly is. If it's not
1153           * initialized yet, it won't have vf reset since it's in a certain
1154           * state. If not, it will try to reset. Even vf is reset, pf will
1155           * set I40E_VFGEN_RSTAT to COMPLETE first, then wait 10ms and set
1156           * it to ACTIVE. In this duration, vf may not catch the moment that
1157           * COMPLETE is set. So, for vf, we'll try to wait a long time.
1158           */
1159         rte_delay_ms(200);
1160
1161         for (i = 0; i < MAX_RESET_WAIT_CNT; i++) {
1162                 reset = rd32(hw, I40E_VFGEN_RSTAT) &
1163                         I40E_VFGEN_RSTAT_VFR_STATE_MASK;
1164                 reset = reset >> I40E_VFGEN_RSTAT_VFR_STATE_SHIFT;
1165                 if (I40E_VFR_COMPLETED == reset || I40E_VFR_VFACTIVE == reset)
1166                         break;
1167                 else
1168                         rte_delay_ms(50);
1169         }
1170
1171         if (i >= MAX_RESET_WAIT_CNT) {
1172                 PMD_INIT_LOG(ERR, "Reset VF NIC failed");
1173                 return -1;
1174         }
1175
1176         return 0;
1177 }
1178
1179 static int
1180 i40evf_init_vf(struct rte_eth_dev *dev)
1181 {
1182         int i, err, bufsz;
1183         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1184         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1185         struct ether_addr *p_mac_addr;
1186         uint16_t interval =
1187                 i40e_calc_itr_interval(I40E_QUEUE_ITR_INTERVAL_MAX);
1188
1189         vf->adapter = I40E_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1190         vf->dev_data = dev->data;
1191         err = i40e_set_mac_type(hw);
1192         if (err) {
1193                 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
1194                 goto err;
1195         }
1196
1197         i40e_init_adminq_parameter(hw);
1198         err = i40e_init_adminq(hw);
1199         if (err) {
1200                 PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
1201                 goto err;
1202         }
1203
1204         /* Reset VF and wait until it's complete */
1205         if (i40evf_reset_vf(hw)) {
1206                 PMD_INIT_LOG(ERR, "reset NIC failed");
1207                 goto err_aq;
1208         }
1209
1210         /* VF reset, shutdown admin queue and initialize again */
1211         if (i40e_shutdown_adminq(hw) != I40E_SUCCESS) {
1212                 PMD_INIT_LOG(ERR, "i40e_shutdown_adminq failed");
1213                 return -1;
1214         }
1215
1216         i40e_init_adminq_parameter(hw);
1217         if (i40e_init_adminq(hw) != I40E_SUCCESS) {
1218                 PMD_INIT_LOG(ERR, "init_adminq failed");
1219                 return -1;
1220         }
1221         vf->aq_resp = rte_zmalloc("vf_aq_resp", I40E_AQ_BUF_SZ, 0);
1222         if (!vf->aq_resp) {
1223                 PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
1224                         goto err_aq;
1225         }
1226         if (i40evf_check_api_version(dev) != 0) {
1227                 PMD_INIT_LOG(ERR, "check_api version failed");
1228                 goto err_aq;
1229         }
1230         bufsz = sizeof(struct i40e_virtchnl_vf_resource) +
1231                 (I40E_MAX_VF_VSI * sizeof(struct i40e_virtchnl_vsi_resource));
1232         vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
1233         if (!vf->vf_res) {
1234                 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
1235                         goto err_aq;
1236         }
1237
1238         if (i40evf_get_vf_resource(dev) != 0) {
1239                 PMD_INIT_LOG(ERR, "i40evf_get_vf_config failed");
1240                 goto err_alloc;
1241         }
1242
1243         /* got VF config message back from PF, now we can parse it */
1244         for (i = 0; i < vf->vf_res->num_vsis; i++) {
1245                 if (vf->vf_res->vsi_res[i].vsi_type == I40E_VSI_SRIOV)
1246                         vf->vsi_res = &vf->vf_res->vsi_res[i];
1247         }
1248
1249         if (!vf->vsi_res) {
1250                 PMD_INIT_LOG(ERR, "no LAN VSI found");
1251                 goto err_alloc;
1252         }
1253
1254         if (hw->mac.type == I40E_MAC_X722_VF)
1255                 vf->flags = I40E_FLAG_RSS_AQ_CAPABLE;
1256         vf->vsi.vsi_id = vf->vsi_res->vsi_id;
1257         vf->vsi.type = vf->vsi_res->vsi_type;
1258         vf->vsi.nb_qps = vf->vsi_res->num_queue_pairs;
1259         vf->vsi.adapter = I40E_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1260
1261         /* Store the MAC address configured by host, or generate random one */
1262         p_mac_addr = (struct ether_addr *)(vf->vsi_res->default_mac_addr);
1263         if (is_valid_assigned_ether_addr(p_mac_addr)) /* Configured by host */
1264                 ether_addr_copy(p_mac_addr, (struct ether_addr *)hw->mac.addr);
1265         else
1266                 eth_random_addr(hw->mac.addr); /* Generate a random one */
1267
1268         /* If the PF host is not DPDK, set the interval of ITR0 to max*/
1269         if (vf->version_major != I40E_DPDK_VERSION_MAJOR) {
1270                 I40E_WRITE_REG(hw, I40E_VFINT_DYN_CTL01,
1271                                (I40E_ITR_INDEX_DEFAULT <<
1272                                 I40E_VFINT_DYN_CTL0_ITR_INDX_SHIFT) |
1273                                (interval <<
1274                                 I40E_VFINT_DYN_CTL0_INTERVAL_SHIFT));
1275                 I40EVF_WRITE_FLUSH(hw);
1276         }
1277
1278         return 0;
1279
1280 err_alloc:
1281         rte_free(vf->vf_res);
1282 err_aq:
1283         i40e_shutdown_adminq(hw); /* ignore error */
1284 err:
1285         return -1;
1286 }
1287
1288 static int
1289 i40evf_uninit_vf(struct rte_eth_dev *dev)
1290 {
1291         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1292         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1293
1294         PMD_INIT_FUNC_TRACE();
1295
1296         if (hw->adapter_stopped == 0)
1297                 i40evf_dev_close(dev);
1298         rte_free(vf->vf_res);
1299         vf->vf_res = NULL;
1300         rte_free(vf->aq_resp);
1301         vf->aq_resp = NULL;
1302
1303         return 0;
1304 }
1305
1306 static void
1307 i40evf_handle_pf_event(__rte_unused struct rte_eth_dev *dev,
1308                            uint8_t *msg,
1309                            __rte_unused uint16_t msglen)
1310 {
1311         struct i40e_virtchnl_pf_event *pf_msg =
1312                         (struct i40e_virtchnl_pf_event *)msg;
1313         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1314
1315         switch (pf_msg->event) {
1316         case I40E_VIRTCHNL_EVENT_RESET_IMPENDING:
1317                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event\n");
1318                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET, NULL);
1319                 break;
1320         case I40E_VIRTCHNL_EVENT_LINK_CHANGE:
1321                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event\n");
1322                 vf->link_up = pf_msg->event_data.link_event.link_status;
1323                 vf->link_speed = pf_msg->event_data.link_event.link_speed;
1324                 break;
1325         case I40E_VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
1326                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event\n");
1327                 break;
1328         default:
1329                 PMD_DRV_LOG(ERR, " unknown event received %u", pf_msg->event);
1330                 break;
1331         }
1332 }
1333
1334 static void
1335 i40evf_handle_aq_msg(struct rte_eth_dev *dev)
1336 {
1337         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1338         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1339         struct i40e_arq_event_info info;
1340         uint16_t pending, aq_opc;
1341         enum i40e_virtchnl_ops msg_opc;
1342         enum i40e_status_code msg_ret;
1343         int ret;
1344
1345         info.buf_len = I40E_AQ_BUF_SZ;
1346         if (!vf->aq_resp) {
1347                 PMD_DRV_LOG(ERR, "Buffer for adminq resp should not be NULL");
1348                 return;
1349         }
1350         info.msg_buf = vf->aq_resp;
1351
1352         pending = 1;
1353         while (pending) {
1354                 ret = i40e_clean_arq_element(hw, &info, &pending);
1355
1356                 if (ret != I40E_SUCCESS) {
1357                         PMD_DRV_LOG(INFO, "Failed to read msg from AdminQ,"
1358                                     "ret: %d", ret);
1359                         break;
1360                 }
1361                 aq_opc = rte_le_to_cpu_16(info.desc.opcode);
1362                 /* For the message sent from pf to vf, opcode is stored in
1363                  * cookie_high of struct i40e_aq_desc, while return error code
1364                  * are stored in cookie_low, Which is done by
1365                  * i40e_aq_send_msg_to_vf in PF driver.*/
1366                 msg_opc = (enum i40e_virtchnl_ops)rte_le_to_cpu_32(
1367                                                   info.desc.cookie_high);
1368                 msg_ret = (enum i40e_status_code)rte_le_to_cpu_32(
1369                                                   info.desc.cookie_low);
1370                 switch (aq_opc) {
1371                 case i40e_aqc_opc_send_msg_to_vf:
1372                         if (msg_opc == I40E_VIRTCHNL_OP_EVENT)
1373                                 /* process event*/
1374                                 i40evf_handle_pf_event(dev, info.msg_buf,
1375                                                        info.msg_len);
1376                         else {
1377                                 /* read message and it's expected one */
1378                                 if (msg_opc == vf->pend_cmd) {
1379                                         vf->cmd_retval = msg_ret;
1380                                         /* prevent compiler reordering */
1381                                         rte_compiler_barrier();
1382                                         _clear_cmd(vf);
1383                                 } else
1384                                         PMD_DRV_LOG(ERR, "command mismatch,"
1385                                                 "expect %u, get %u",
1386                                                 vf->pend_cmd, msg_opc);
1387                                 PMD_DRV_LOG(DEBUG, "adminq response is received,"
1388                                              " opcode = %d\n", msg_opc);
1389                         }
1390                         break;
1391                 default:
1392                         PMD_DRV_LOG(ERR, "Request %u is not supported yet",
1393                                     aq_opc);
1394                         break;
1395                 }
1396         }
1397 }
1398
1399 /**
1400  * Interrupt handler triggered by NIC  for handling
1401  * specific interrupt. Only adminq interrupt is processed in VF.
1402  *
1403  * @param handle
1404  *  Pointer to interrupt handle.
1405  * @param param
1406  *  The address of parameter (struct rte_eth_dev *) regsitered before.
1407  *
1408  * @return
1409  *  void
1410  */
1411 static void
1412 i40evf_dev_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
1413                              void *param)
1414 {
1415         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1416         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1417         uint32_t icr0;
1418
1419         i40evf_disable_irq0(hw);
1420
1421         /* read out interrupt causes */
1422         icr0 = I40E_READ_REG(hw, I40E_VFINT_ICR01);
1423
1424         /* No interrupt event indicated */
1425         if (!(icr0 & I40E_VFINT_ICR01_INTEVENT_MASK)) {
1426                 PMD_DRV_LOG(DEBUG, "No interrupt event, nothing to do\n");
1427                 goto done;
1428         }
1429
1430         if (icr0 & I40E_VFINT_ICR01_ADMINQ_MASK) {
1431                 PMD_DRV_LOG(DEBUG, "ICR01_ADMINQ is reported\n");
1432                 i40evf_handle_aq_msg(dev);
1433         }
1434
1435         /* Link Status Change interrupt */
1436         if (icr0 & I40E_VFINT_ICR01_LINK_STAT_CHANGE_MASK)
1437                 PMD_DRV_LOG(DEBUG, "LINK_STAT_CHANGE is reported,"
1438                                    " do nothing\n");
1439
1440 done:
1441         i40evf_enable_irq0(hw);
1442         rte_intr_enable(&dev->pci_dev->intr_handle);
1443 }
1444
1445 static int
1446 i40evf_dev_init(struct rte_eth_dev *eth_dev)
1447 {
1448         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(\
1449                         eth_dev->data->dev_private);
1450         struct rte_pci_device *pci_dev = eth_dev->pci_dev;
1451
1452         PMD_INIT_FUNC_TRACE();
1453
1454         /* assign ops func pointer */
1455         eth_dev->dev_ops = &i40evf_eth_dev_ops;
1456         eth_dev->rx_pkt_burst = &i40e_recv_pkts;
1457         eth_dev->tx_pkt_burst = &i40e_xmit_pkts;
1458
1459         /*
1460          * For secondary processes, we don't initialise any further as primary
1461          * has already done this work.
1462          */
1463         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
1464                 i40e_set_rx_function(eth_dev);
1465                 i40e_set_tx_function(eth_dev);
1466                 return 0;
1467         }
1468
1469         rte_eth_copy_pci_info(eth_dev, eth_dev->pci_dev);
1470
1471         hw->vendor_id = eth_dev->pci_dev->id.vendor_id;
1472         hw->device_id = eth_dev->pci_dev->id.device_id;
1473         hw->subsystem_vendor_id = eth_dev->pci_dev->id.subsystem_vendor_id;
1474         hw->subsystem_device_id = eth_dev->pci_dev->id.subsystem_device_id;
1475         hw->bus.device = eth_dev->pci_dev->addr.devid;
1476         hw->bus.func = eth_dev->pci_dev->addr.function;
1477         hw->hw_addr = (void *)eth_dev->pci_dev->mem_resource[0].addr;
1478         hw->adapter_stopped = 0;
1479
1480         if(i40evf_init_vf(eth_dev) != 0) {
1481                 PMD_INIT_LOG(ERR, "Init vf failed");
1482                 return -1;
1483         }
1484
1485         /* register callback func to eal lib */
1486         rte_intr_callback_register(&pci_dev->intr_handle,
1487                 i40evf_dev_interrupt_handler, (void *)eth_dev);
1488
1489         /* enable uio intr after callback register */
1490         rte_intr_enable(&pci_dev->intr_handle);
1491
1492         /* configure and enable device interrupt */
1493         i40evf_enable_irq0(hw);
1494
1495         /* copy mac addr */
1496         eth_dev->data->mac_addrs = rte_zmalloc("i40evf_mac",
1497                                         ETHER_ADDR_LEN * I40E_NUM_MACADDR_MAX,
1498                                         0);
1499         if (eth_dev->data->mac_addrs == NULL) {
1500                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
1501                                 " store MAC addresses",
1502                                 ETHER_ADDR_LEN * I40E_NUM_MACADDR_MAX);
1503                 return -ENOMEM;
1504         }
1505         ether_addr_copy((struct ether_addr *)hw->mac.addr,
1506                         &eth_dev->data->mac_addrs[0]);
1507
1508         return 0;
1509 }
1510
1511 static int
1512 i40evf_dev_uninit(struct rte_eth_dev *eth_dev)
1513 {
1514         PMD_INIT_FUNC_TRACE();
1515
1516         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1517                 return -EPERM;
1518
1519         eth_dev->dev_ops = NULL;
1520         eth_dev->rx_pkt_burst = NULL;
1521         eth_dev->tx_pkt_burst = NULL;
1522
1523         if (i40evf_uninit_vf(eth_dev) != 0) {
1524                 PMD_INIT_LOG(ERR, "i40evf_uninit_vf failed");
1525                 return -1;
1526         }
1527
1528         rte_free(eth_dev->data->mac_addrs);
1529         eth_dev->data->mac_addrs = NULL;
1530
1531         return 0;
1532 }
1533 /*
1534  * virtual function driver struct
1535  */
1536 static struct eth_driver rte_i40evf_pmd = {
1537         .pci_drv = {
1538                 .id_table = pci_id_i40evf_map,
1539                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_DETACHABLE,
1540                 .probe = rte_eth_dev_pci_probe,
1541                 .remove = rte_eth_dev_pci_remove,
1542         },
1543         .eth_dev_init = i40evf_dev_init,
1544         .eth_dev_uninit = i40evf_dev_uninit,
1545         .dev_private_size = sizeof(struct i40e_adapter),
1546 };
1547
1548 RTE_PMD_REGISTER_PCI(net_i40e_vf, rte_i40evf_pmd.pci_drv);
1549 RTE_PMD_REGISTER_PCI_TABLE(net_i40e_vf, pci_id_i40evf_map);
1550
1551 static int
1552 i40evf_dev_configure(struct rte_eth_dev *dev)
1553 {
1554         struct i40e_adapter *ad =
1555                 I40E_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1556         struct rte_eth_conf *conf = &dev->data->dev_conf;
1557         struct i40e_vf *vf;
1558
1559         /* Initialize to TRUE. If any of Rx queues doesn't meet the bulk
1560          * allocation or vector Rx preconditions we will reset it.
1561          */
1562         ad->rx_bulk_alloc_allowed = true;
1563         ad->rx_vec_allowed = true;
1564         ad->tx_simple_allowed = true;
1565         ad->tx_vec_allowed = true;
1566
1567         /* For non-DPDK PF drivers, VF has no ability to disable HW
1568          * CRC strip, and is implicitly enabled by the PF.
1569          */
1570         if (!conf->rxmode.hw_strip_crc) {
1571                 vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1572                 if ((vf->version_major == I40E_VIRTCHNL_VERSION_MAJOR) &&
1573                     (vf->version_minor <= I40E_VIRTCHNL_VERSION_MINOR)) {
1574                         /* Peer is running non-DPDK PF driver. */
1575                         PMD_INIT_LOG(ERR, "VF can't disable HW CRC Strip");
1576                         return -EINVAL;
1577                 }
1578         }
1579
1580         return i40evf_init_vlan(dev);
1581 }
1582
1583 static int
1584 i40evf_init_vlan(struct rte_eth_dev *dev)
1585 {
1586         struct rte_eth_dev_data *data = dev->data;
1587         int ret;
1588
1589         /* Apply vlan offload setting */
1590         i40evf_vlan_offload_set(dev, ETH_VLAN_STRIP_MASK);
1591
1592         /* Apply pvid setting */
1593         ret = i40evf_vlan_pvid_set(dev, data->dev_conf.txmode.pvid,
1594                                 data->dev_conf.txmode.hw_vlan_insert_pvid);
1595         return ret;
1596 }
1597
1598 static void
1599 i40evf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1600 {
1601         bool enable_vlan_strip = 0;
1602         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1603         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1604
1605         /* Linux pf host doesn't support vlan offload yet */
1606         if (vf->version_major == I40E_DPDK_VERSION_MAJOR) {
1607                 /* Vlan stripping setting */
1608                 if (mask & ETH_VLAN_STRIP_MASK) {
1609                         /* Enable or disable VLAN stripping */
1610                         if (dev_conf->rxmode.hw_vlan_strip)
1611                                 enable_vlan_strip = 1;
1612                         else
1613                                 enable_vlan_strip = 0;
1614
1615                         i40evf_config_vlan_offload(dev, enable_vlan_strip);
1616                 }
1617         }
1618 }
1619
1620 static int
1621 i40evf_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
1622 {
1623         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1624         struct i40e_vsi_vlan_pvid_info info;
1625         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1626
1627         memset(&info, 0, sizeof(info));
1628         info.on = on;
1629
1630         /* Linux pf host don't support vlan offload yet */
1631         if (vf->version_major == I40E_DPDK_VERSION_MAJOR) {
1632                 if (info.on)
1633                         info.config.pvid = pvid;
1634                 else {
1635                         info.config.reject.tagged =
1636                                 dev_conf->txmode.hw_vlan_reject_tagged;
1637                         info.config.reject.untagged =
1638                                 dev_conf->txmode.hw_vlan_reject_untagged;
1639                 }
1640                 return i40evf_config_vlan_pvid(dev, &info);
1641         }
1642
1643         return 0;
1644 }
1645
1646 static int
1647 i40evf_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1648 {
1649         struct i40e_rx_queue *rxq;
1650         int err = 0;
1651         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1652
1653         PMD_INIT_FUNC_TRACE();
1654
1655         if (rx_queue_id < dev->data->nb_rx_queues) {
1656                 rxq = dev->data->rx_queues[rx_queue_id];
1657
1658                 err = i40e_alloc_rx_queue_mbufs(rxq);
1659                 if (err) {
1660                         PMD_DRV_LOG(ERR, "Failed to allocate RX queue mbuf");
1661                         return err;
1662                 }
1663
1664                 rte_wmb();
1665
1666                 /* Init the RX tail register. */
1667                 I40E_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
1668                 I40EVF_WRITE_FLUSH(hw);
1669
1670                 /* Ready to switch the queue on */
1671                 err = i40evf_switch_queue(dev, TRUE, rx_queue_id, TRUE);
1672
1673                 if (err)
1674                         PMD_DRV_LOG(ERR, "Failed to switch RX queue %u on",
1675                                     rx_queue_id);
1676                 else
1677                         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1678         }
1679
1680         return err;
1681 }
1682
1683 static int
1684 i40evf_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1685 {
1686         struct i40e_rx_queue *rxq;
1687         int err;
1688
1689         if (rx_queue_id < dev->data->nb_rx_queues) {
1690                 rxq = dev->data->rx_queues[rx_queue_id];
1691
1692                 err = i40evf_switch_queue(dev, TRUE, rx_queue_id, FALSE);
1693
1694                 if (err) {
1695                         PMD_DRV_LOG(ERR, "Failed to switch RX queue %u off",
1696                                     rx_queue_id);
1697                         return err;
1698                 }
1699
1700                 i40e_rx_queue_release_mbufs(rxq);
1701                 i40e_reset_rx_queue(rxq);
1702                 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1703         }
1704
1705         return 0;
1706 }
1707
1708 static int
1709 i40evf_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1710 {
1711         int err = 0;
1712
1713         PMD_INIT_FUNC_TRACE();
1714
1715         if (tx_queue_id < dev->data->nb_tx_queues) {
1716
1717                 /* Ready to switch the queue on */
1718                 err = i40evf_switch_queue(dev, FALSE, tx_queue_id, TRUE);
1719
1720                 if (err)
1721                         PMD_DRV_LOG(ERR, "Failed to switch TX queue %u on",
1722                                     tx_queue_id);
1723                 else
1724                         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
1725         }
1726
1727         return err;
1728 }
1729
1730 static int
1731 i40evf_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1732 {
1733         struct i40e_tx_queue *txq;
1734         int err;
1735
1736         if (tx_queue_id < dev->data->nb_tx_queues) {
1737                 txq = dev->data->tx_queues[tx_queue_id];
1738
1739                 err = i40evf_switch_queue(dev, FALSE, tx_queue_id, FALSE);
1740
1741                 if (err) {
1742                         PMD_DRV_LOG(ERR, "Failed to switch TX queue %u off",
1743                                     tx_queue_id);
1744                         return err;
1745                 }
1746
1747                 i40e_tx_queue_release_mbufs(txq);
1748                 i40e_reset_tx_queue(txq);
1749                 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
1750         }
1751
1752         return 0;
1753 }
1754
1755 static int
1756 i40evf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1757 {
1758         int ret;
1759
1760         if (on)
1761                 ret = i40evf_add_vlan(dev, vlan_id);
1762         else
1763                 ret = i40evf_del_vlan(dev,vlan_id);
1764
1765         return ret;
1766 }
1767
1768 static int
1769 i40evf_rxq_init(struct rte_eth_dev *dev, struct i40e_rx_queue *rxq)
1770 {
1771         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1772         struct rte_eth_dev_data *dev_data = dev->data;
1773         struct rte_pktmbuf_pool_private *mbp_priv;
1774         uint16_t buf_size, len;
1775
1776         rxq->qrx_tail = hw->hw_addr + I40E_QRX_TAIL1(rxq->queue_id);
1777         I40E_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
1778         I40EVF_WRITE_FLUSH(hw);
1779
1780         /* Calculate the maximum packet length allowed */
1781         mbp_priv = rte_mempool_get_priv(rxq->mp);
1782         buf_size = (uint16_t)(mbp_priv->mbuf_data_room_size -
1783                                         RTE_PKTMBUF_HEADROOM);
1784         rxq->hs_mode = i40e_header_split_none;
1785         rxq->rx_hdr_len = 0;
1786         rxq->rx_buf_len = RTE_ALIGN(buf_size, (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
1787         len = rxq->rx_buf_len * I40E_MAX_CHAINED_RX_BUFFERS;
1788         rxq->max_pkt_len = RTE_MIN(len,
1789                 dev_data->dev_conf.rxmode.max_rx_pkt_len);
1790
1791         /**
1792          * Check if the jumbo frame and maximum packet length are set correctly
1793          */
1794         if (dev_data->dev_conf.rxmode.jumbo_frame == 1) {
1795                 if (rxq->max_pkt_len <= ETHER_MAX_LEN ||
1796                     rxq->max_pkt_len > I40E_FRAME_SIZE_MAX) {
1797                         PMD_DRV_LOG(ERR, "maximum packet length must be "
1798                                 "larger than %u and smaller than %u, as jumbo "
1799                                 "frame is enabled", (uint32_t)ETHER_MAX_LEN,
1800                                         (uint32_t)I40E_FRAME_SIZE_MAX);
1801                         return I40E_ERR_CONFIG;
1802                 }
1803         } else {
1804                 if (rxq->max_pkt_len < ETHER_MIN_LEN ||
1805                     rxq->max_pkt_len > ETHER_MAX_LEN) {
1806                         PMD_DRV_LOG(ERR, "maximum packet length must be "
1807                                 "larger than %u and smaller than %u, as jumbo "
1808                                 "frame is disabled", (uint32_t)ETHER_MIN_LEN,
1809                                                 (uint32_t)ETHER_MAX_LEN);
1810                         return I40E_ERR_CONFIG;
1811                 }
1812         }
1813
1814         if (dev_data->dev_conf.rxmode.enable_scatter ||
1815             (rxq->max_pkt_len + 2 * I40E_VLAN_TAG_SIZE) > buf_size) {
1816                 dev_data->scattered_rx = 1;
1817         }
1818
1819         return 0;
1820 }
1821
1822 static int
1823 i40evf_rx_init(struct rte_eth_dev *dev)
1824 {
1825         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1826         uint16_t i;
1827         int ret = I40E_SUCCESS;
1828         struct i40e_rx_queue **rxq =
1829                 (struct i40e_rx_queue **)dev->data->rx_queues;
1830
1831         i40evf_config_rss(vf);
1832         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1833                 if (!rxq[i] || !rxq[i]->q_set)
1834                         continue;
1835                 ret = i40evf_rxq_init(dev, rxq[i]);
1836                 if (ret != I40E_SUCCESS)
1837                         break;
1838         }
1839         if (ret == I40E_SUCCESS)
1840                 i40e_set_rx_function(dev);
1841
1842         return ret;
1843 }
1844
1845 static void
1846 i40evf_tx_init(struct rte_eth_dev *dev)
1847 {
1848         uint16_t i;
1849         struct i40e_tx_queue **txq =
1850                 (struct i40e_tx_queue **)dev->data->tx_queues;
1851         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1852
1853         for (i = 0; i < dev->data->nb_tx_queues; i++)
1854                 txq[i]->qtx_tail = hw->hw_addr + I40E_QTX_TAIL1(i);
1855
1856         i40e_set_tx_function(dev);
1857 }
1858
1859 static inline void
1860 i40evf_enable_queues_intr(struct rte_eth_dev *dev)
1861 {
1862         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1863         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1864         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
1865
1866         if (!rte_intr_allow_others(intr_handle)) {
1867                 I40E_WRITE_REG(hw,
1868                                I40E_VFINT_DYN_CTL01,
1869                                I40E_VFINT_DYN_CTL01_INTENA_MASK |
1870                                I40E_VFINT_DYN_CTL01_CLEARPBA_MASK |
1871                                I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
1872                 I40EVF_WRITE_FLUSH(hw);
1873                 return;
1874         }
1875
1876         if (vf->version_major == I40E_DPDK_VERSION_MAJOR)
1877                 /* To support DPDK PF host */
1878                 I40E_WRITE_REG(hw,
1879                         I40E_VFINT_DYN_CTLN1(I40EVF_VSI_DEFAULT_MSIX_INTR - 1),
1880                         I40E_VFINT_DYN_CTLN1_INTENA_MASK |
1881                         I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
1882         /* If host driver is kernel driver, do nothing.
1883          * Interrupt 0 is used for rx packets, but don't set
1884          * I40E_VFINT_DYN_CTL01,
1885          * because it is already done in i40evf_enable_irq0.
1886          */
1887
1888         I40EVF_WRITE_FLUSH(hw);
1889 }
1890
1891 static inline void
1892 i40evf_disable_queues_intr(struct rte_eth_dev *dev)
1893 {
1894         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1895         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1896         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
1897
1898         if (!rte_intr_allow_others(intr_handle)) {
1899                 I40E_WRITE_REG(hw, I40E_VFINT_DYN_CTL01,
1900                                I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
1901                 I40EVF_WRITE_FLUSH(hw);
1902                 return;
1903         }
1904
1905         if (vf->version_major == I40E_DPDK_VERSION_MAJOR)
1906                 I40E_WRITE_REG(hw,
1907                                I40E_VFINT_DYN_CTLN1(I40EVF_VSI_DEFAULT_MSIX_INTR
1908                                                     - 1),
1909                                0);
1910         /* If host driver is kernel driver, do nothing.
1911          * Interrupt 0 is used for rx packets, but don't zero
1912          * I40E_VFINT_DYN_CTL01,
1913          * because interrupt 0 is also used for adminq processing.
1914          */
1915
1916         I40EVF_WRITE_FLUSH(hw);
1917 }
1918
1919 static int
1920 i40evf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1921 {
1922         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
1923         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1924         uint16_t interval =
1925                 i40e_calc_itr_interval(RTE_LIBRTE_I40E_ITR_INTERVAL);
1926         uint16_t msix_intr;
1927
1928         msix_intr = intr_handle->intr_vec[queue_id];
1929         if (msix_intr == I40E_MISC_VEC_ID)
1930                 I40E_WRITE_REG(hw, I40E_VFINT_DYN_CTL01,
1931                                I40E_VFINT_DYN_CTL01_INTENA_MASK |
1932                                I40E_VFINT_DYN_CTL01_CLEARPBA_MASK |
1933                                (0 << I40E_VFINT_DYN_CTL01_ITR_INDX_SHIFT) |
1934                                (interval <<
1935                                 I40E_VFINT_DYN_CTL01_INTERVAL_SHIFT));
1936         else
1937                 I40E_WRITE_REG(hw,
1938                                I40E_VFINT_DYN_CTLN1(msix_intr -
1939                                                     I40E_RX_VEC_START),
1940                                I40E_VFINT_DYN_CTLN1_INTENA_MASK |
1941                                I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK |
1942                                (0 << I40E_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
1943                                (interval <<
1944                                 I40E_VFINT_DYN_CTLN1_INTERVAL_SHIFT));
1945
1946         I40EVF_WRITE_FLUSH(hw);
1947
1948         rte_intr_enable(&dev->pci_dev->intr_handle);
1949
1950         return 0;
1951 }
1952
1953 static int
1954 i40evf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1955 {
1956         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
1957         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1958         uint16_t msix_intr;
1959
1960         msix_intr = intr_handle->intr_vec[queue_id];
1961         if (msix_intr == I40E_MISC_VEC_ID)
1962                 I40E_WRITE_REG(hw, I40E_VFINT_DYN_CTL01, 0);
1963         else
1964                 I40E_WRITE_REG(hw,
1965                                I40E_VFINT_DYN_CTLN1(msix_intr -
1966                                                     I40E_RX_VEC_START),
1967                                0);
1968
1969         I40EVF_WRITE_FLUSH(hw);
1970
1971         return 0;
1972 }
1973
1974 static void
1975 i40evf_add_del_all_mac_addr(struct rte_eth_dev *dev, bool add)
1976 {
1977         struct i40e_virtchnl_ether_addr_list *list;
1978         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1979         int err, i, j;
1980         int next_begin = 0;
1981         int begin = 0;
1982         uint32_t len;
1983         struct ether_addr *addr;
1984         struct vf_cmd_info args;
1985
1986         do {
1987                 j = 0;
1988                 len = sizeof(struct i40e_virtchnl_ether_addr_list);
1989                 for (i = begin; i < I40E_NUM_MACADDR_MAX; i++, next_begin++) {
1990                         if (is_zero_ether_addr(&dev->data->mac_addrs[i]))
1991                                 continue;
1992                         len += sizeof(struct i40e_virtchnl_ether_addr);
1993                         if (len >= I40E_AQ_BUF_SZ) {
1994                                 next_begin = i + 1;
1995                                 break;
1996                         }
1997                 }
1998
1999                 list = rte_zmalloc("i40evf_del_mac_buffer", len, 0);
2000
2001                 for (i = begin; i < next_begin; i++) {
2002                         addr = &dev->data->mac_addrs[i];
2003                         if (is_zero_ether_addr(addr))
2004                                 continue;
2005                         (void)rte_memcpy(list->list[j].addr, addr->addr_bytes,
2006                                          sizeof(addr->addr_bytes));
2007                         PMD_DRV_LOG(DEBUG, "add/rm mac:%x:%x:%x:%x:%x:%x",
2008                                     addr->addr_bytes[0], addr->addr_bytes[1],
2009                                     addr->addr_bytes[2], addr->addr_bytes[3],
2010                                     addr->addr_bytes[4], addr->addr_bytes[5]);
2011                         j++;
2012                 }
2013                 list->vsi_id = vf->vsi_res->vsi_id;
2014                 list->num_elements = j;
2015                 args.ops = add ? I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS :
2016                            I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS;
2017                 args.in_args = (uint8_t *)list;
2018                 args.in_args_size = len;
2019                 args.out_buffer = vf->aq_resp;
2020                 args.out_size = I40E_AQ_BUF_SZ;
2021                 err = i40evf_execute_vf_cmd(dev, &args);
2022                 if (err)
2023                         PMD_DRV_LOG(ERR, "fail to execute command %s",
2024                                     add ? "OP_ADD_ETHER_ADDRESS" :
2025                                     "OP_DEL_ETHER_ADDRESS");
2026                 rte_free(list);
2027                 begin = next_begin;
2028         } while (begin < I40E_NUM_MACADDR_MAX);
2029 }
2030
2031 static int
2032 i40evf_dev_start(struct rte_eth_dev *dev)
2033 {
2034         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2035         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2036         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
2037         uint32_t intr_vector = 0;
2038
2039         PMD_INIT_FUNC_TRACE();
2040
2041         hw->adapter_stopped = 0;
2042
2043         vf->max_pkt_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
2044         vf->num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
2045                                         dev->data->nb_tx_queues);
2046
2047         /* check and configure queue intr-vector mapping */
2048         if (dev->data->dev_conf.intr_conf.rxq != 0) {
2049                 intr_vector = dev->data->nb_rx_queues;
2050                 if (rte_intr_efd_enable(intr_handle, intr_vector))
2051                         return -1;
2052         }
2053
2054         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
2055                 intr_handle->intr_vec =
2056                         rte_zmalloc("intr_vec",
2057                                     dev->data->nb_rx_queues * sizeof(int), 0);
2058                 if (!intr_handle->intr_vec) {
2059                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
2060                                      " intr_vec\n", dev->data->nb_rx_queues);
2061                         return -ENOMEM;
2062                 }
2063         }
2064
2065         if (i40evf_rx_init(dev) != 0){
2066                 PMD_DRV_LOG(ERR, "failed to do RX init");
2067                 return -1;
2068         }
2069
2070         i40evf_tx_init(dev);
2071
2072         if (i40evf_configure_queues(dev) != 0) {
2073                 PMD_DRV_LOG(ERR, "configure queues failed");
2074                 goto err_queue;
2075         }
2076         if (i40evf_config_irq_map(dev)) {
2077                 PMD_DRV_LOG(ERR, "config_irq_map failed");
2078                 goto err_queue;
2079         }
2080
2081         /* Set all mac addrs */
2082         i40evf_add_del_all_mac_addr(dev, TRUE);
2083
2084         if (i40evf_start_queues(dev) != 0) {
2085                 PMD_DRV_LOG(ERR, "enable queues failed");
2086                 goto err_mac;
2087         }
2088
2089         i40evf_enable_queues_intr(dev);
2090         return 0;
2091
2092 err_mac:
2093         i40evf_add_del_all_mac_addr(dev, FALSE);
2094 err_queue:
2095         return -1;
2096 }
2097
2098 static void
2099 i40evf_dev_stop(struct rte_eth_dev *dev)
2100 {
2101         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
2102
2103         PMD_INIT_FUNC_TRACE();
2104
2105         i40evf_stop_queues(dev);
2106         i40evf_disable_queues_intr(dev);
2107         i40e_dev_clear_queues(dev);
2108
2109         /* Clean datapath event and queue/vec mapping */
2110         rte_intr_efd_disable(intr_handle);
2111         if (intr_handle->intr_vec) {
2112                 rte_free(intr_handle->intr_vec);
2113                 intr_handle->intr_vec = NULL;
2114         }
2115         /* remove all mac addrs */
2116         i40evf_add_del_all_mac_addr(dev, FALSE);
2117
2118 }
2119
2120 static int
2121 i40evf_dev_link_update(struct rte_eth_dev *dev,
2122                        __rte_unused int wait_to_complete)
2123 {
2124         struct rte_eth_link new_link;
2125         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2126         /*
2127          * DPDK pf host provide interfacet to acquire link status
2128          * while Linux driver does not
2129          */
2130
2131         /* Linux driver PF host */
2132         switch (vf->link_speed) {
2133         case I40E_LINK_SPEED_100MB:
2134                 new_link.link_speed = ETH_SPEED_NUM_100M;
2135                 break;
2136         case I40E_LINK_SPEED_1GB:
2137                 new_link.link_speed = ETH_SPEED_NUM_1G;
2138                 break;
2139         case I40E_LINK_SPEED_10GB:
2140                 new_link.link_speed = ETH_SPEED_NUM_10G;
2141                 break;
2142         case I40E_LINK_SPEED_20GB:
2143                 new_link.link_speed = ETH_SPEED_NUM_20G;
2144                 break;
2145         case I40E_LINK_SPEED_40GB:
2146                 new_link.link_speed = ETH_SPEED_NUM_40G;
2147                 break;
2148         default:
2149                 new_link.link_speed = ETH_SPEED_NUM_100M;
2150                 break;
2151         }
2152         /* full duplex only */
2153         new_link.link_duplex = ETH_LINK_FULL_DUPLEX;
2154         new_link.link_status = vf->link_up ? ETH_LINK_UP :
2155                                              ETH_LINK_DOWN;
2156
2157         i40evf_dev_atomic_write_link_status(dev, &new_link);
2158
2159         return 0;
2160 }
2161
2162 static void
2163 i40evf_dev_promiscuous_enable(struct rte_eth_dev *dev)
2164 {
2165         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2166         int ret;
2167
2168         /* If enabled, just return */
2169         if (vf->promisc_unicast_enabled)
2170                 return;
2171
2172         ret = i40evf_config_promisc(dev, 1, vf->promisc_multicast_enabled);
2173         if (ret == 0)
2174                 vf->promisc_unicast_enabled = TRUE;
2175 }
2176
2177 static void
2178 i40evf_dev_promiscuous_disable(struct rte_eth_dev *dev)
2179 {
2180         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2181         int ret;
2182
2183         /* If disabled, just return */
2184         if (!vf->promisc_unicast_enabled)
2185                 return;
2186
2187         ret = i40evf_config_promisc(dev, 0, vf->promisc_multicast_enabled);
2188         if (ret == 0)
2189                 vf->promisc_unicast_enabled = FALSE;
2190 }
2191
2192 static void
2193 i40evf_dev_allmulticast_enable(struct rte_eth_dev *dev)
2194 {
2195         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2196         int ret;
2197
2198         /* If enabled, just return */
2199         if (vf->promisc_multicast_enabled)
2200                 return;
2201
2202         ret = i40evf_config_promisc(dev, vf->promisc_unicast_enabled, 1);
2203         if (ret == 0)
2204                 vf->promisc_multicast_enabled = TRUE;
2205 }
2206
2207 static void
2208 i40evf_dev_allmulticast_disable(struct rte_eth_dev *dev)
2209 {
2210         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2211         int ret;
2212
2213         /* If enabled, just return */
2214         if (!vf->promisc_multicast_enabled)
2215                 return;
2216
2217         ret = i40evf_config_promisc(dev, vf->promisc_unicast_enabled, 0);
2218         if (ret == 0)
2219                 vf->promisc_multicast_enabled = FALSE;
2220 }
2221
2222 static void
2223 i40evf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2224 {
2225         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2226
2227         memset(dev_info, 0, sizeof(*dev_info));
2228         dev_info->max_rx_queues = vf->vsi_res->num_queue_pairs;
2229         dev_info->max_tx_queues = vf->vsi_res->num_queue_pairs;
2230         dev_info->min_rx_bufsize = I40E_BUF_SIZE_MIN;
2231         dev_info->max_rx_pktlen = I40E_FRAME_SIZE_MAX;
2232         dev_info->hash_key_size = (I40E_VFQF_HKEY_MAX_INDEX + 1) * sizeof(uint32_t);
2233         dev_info->reta_size = ETH_RSS_RETA_SIZE_64;
2234         dev_info->flow_type_rss_offloads = I40E_RSS_OFFLOAD_ALL;
2235         dev_info->max_mac_addrs = I40E_NUM_MACADDR_MAX;
2236         dev_info->rx_offload_capa =
2237                 DEV_RX_OFFLOAD_VLAN_STRIP |
2238                 DEV_RX_OFFLOAD_QINQ_STRIP |
2239                 DEV_RX_OFFLOAD_IPV4_CKSUM |
2240                 DEV_RX_OFFLOAD_UDP_CKSUM |
2241                 DEV_RX_OFFLOAD_TCP_CKSUM;
2242         dev_info->tx_offload_capa =
2243                 DEV_TX_OFFLOAD_VLAN_INSERT |
2244                 DEV_TX_OFFLOAD_QINQ_INSERT |
2245                 DEV_TX_OFFLOAD_IPV4_CKSUM |
2246                 DEV_TX_OFFLOAD_UDP_CKSUM |
2247                 DEV_TX_OFFLOAD_TCP_CKSUM |
2248                 DEV_TX_OFFLOAD_SCTP_CKSUM;
2249
2250         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2251                 .rx_thresh = {
2252                         .pthresh = I40E_DEFAULT_RX_PTHRESH,
2253                         .hthresh = I40E_DEFAULT_RX_HTHRESH,
2254                         .wthresh = I40E_DEFAULT_RX_WTHRESH,
2255                 },
2256                 .rx_free_thresh = I40E_DEFAULT_RX_FREE_THRESH,
2257                 .rx_drop_en = 0,
2258         };
2259
2260         dev_info->default_txconf = (struct rte_eth_txconf) {
2261                 .tx_thresh = {
2262                         .pthresh = I40E_DEFAULT_TX_PTHRESH,
2263                         .hthresh = I40E_DEFAULT_TX_HTHRESH,
2264                         .wthresh = I40E_DEFAULT_TX_WTHRESH,
2265                 },
2266                 .tx_free_thresh = I40E_DEFAULT_TX_FREE_THRESH,
2267                 .tx_rs_thresh = I40E_DEFAULT_TX_RSBIT_THRESH,
2268                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
2269                                 ETH_TXQ_FLAGS_NOOFFLOADS,
2270         };
2271
2272         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
2273                 .nb_max = I40E_MAX_RING_DESC,
2274                 .nb_min = I40E_MIN_RING_DESC,
2275                 .nb_align = I40E_ALIGN_RING_DESC,
2276         };
2277
2278         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
2279                 .nb_max = I40E_MAX_RING_DESC,
2280                 .nb_min = I40E_MIN_RING_DESC,
2281                 .nb_align = I40E_ALIGN_RING_DESC,
2282         };
2283 }
2284
2285 static void
2286 i40evf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2287 {
2288         if (i40evf_get_statics(dev, stats))
2289                 PMD_DRV_LOG(ERR, "Get statics failed");
2290 }
2291
2292 static void
2293 i40evf_dev_close(struct rte_eth_dev *dev)
2294 {
2295         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2296         struct rte_pci_device *pci_dev = dev->pci_dev;
2297
2298         i40evf_dev_stop(dev);
2299         hw->adapter_stopped = 1;
2300         i40e_dev_free_queues(dev);
2301         i40evf_reset_vf(hw);
2302         i40e_shutdown_adminq(hw);
2303         /* disable uio intr before callback unregister */
2304         rte_intr_disable(&pci_dev->intr_handle);
2305
2306         /* unregister callback func from eal lib */
2307         rte_intr_callback_unregister(&pci_dev->intr_handle,
2308                 i40evf_dev_interrupt_handler, (void *)dev);
2309         i40evf_disable_irq0(hw);
2310 }
2311
2312 static int
2313 i40evf_get_rss_lut(struct i40e_vsi *vsi, uint8_t *lut, uint16_t lut_size)
2314 {
2315         struct i40e_vf *vf = I40E_VSI_TO_VF(vsi);
2316         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
2317         int ret;
2318
2319         if (!lut)
2320                 return -EINVAL;
2321
2322         if (vf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
2323                 ret = i40e_aq_get_rss_lut(hw, vsi->vsi_id, FALSE,
2324                                           lut, lut_size);
2325                 if (ret) {
2326                         PMD_DRV_LOG(ERR, "Failed to get RSS lookup table");
2327                         return ret;
2328                 }
2329         } else {
2330                 uint32_t *lut_dw = (uint32_t *)lut;
2331                 uint16_t i, lut_size_dw = lut_size / 4;
2332
2333                 for (i = 0; i < lut_size_dw; i++)
2334                         lut_dw[i] = I40E_READ_REG(hw, I40E_VFQF_HLUT(i));
2335         }
2336
2337         return 0;
2338 }
2339
2340 static int
2341 i40evf_set_rss_lut(struct i40e_vsi *vsi, uint8_t *lut, uint16_t lut_size)
2342 {
2343         struct i40e_vf *vf;
2344         struct i40e_hw *hw;
2345         int ret;
2346
2347         if (!vsi || !lut)
2348                 return -EINVAL;
2349
2350         vf = I40E_VSI_TO_VF(vsi);
2351         hw = I40E_VSI_TO_HW(vsi);
2352
2353         if (vf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
2354                 ret = i40e_aq_set_rss_lut(hw, vsi->vsi_id, FALSE,
2355                                           lut, lut_size);
2356                 if (ret) {
2357                         PMD_DRV_LOG(ERR, "Failed to set RSS lookup table");
2358                         return ret;
2359                 }
2360         } else {
2361                 uint32_t *lut_dw = (uint32_t *)lut;
2362                 uint16_t i, lut_size_dw = lut_size / 4;
2363
2364                 for (i = 0; i < lut_size_dw; i++)
2365                         I40E_WRITE_REG(hw, I40E_VFQF_HLUT(i), lut_dw[i]);
2366                 I40EVF_WRITE_FLUSH(hw);
2367         }
2368
2369         return 0;
2370 }
2371
2372 static int
2373 i40evf_dev_rss_reta_update(struct rte_eth_dev *dev,
2374                            struct rte_eth_rss_reta_entry64 *reta_conf,
2375                            uint16_t reta_size)
2376 {
2377         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2378         uint8_t *lut;
2379         uint16_t i, idx, shift;
2380         int ret;
2381
2382         if (reta_size != ETH_RSS_RETA_SIZE_64) {
2383                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
2384                         "(%d) doesn't match the number of hardware can "
2385                         "support (%d)\n", reta_size, ETH_RSS_RETA_SIZE_64);
2386                 return -EINVAL;
2387         }
2388
2389         lut = rte_zmalloc("i40e_rss_lut", reta_size, 0);
2390         if (!lut) {
2391                 PMD_DRV_LOG(ERR, "No memory can be allocated");
2392                 return -ENOMEM;
2393         }
2394         ret = i40evf_get_rss_lut(&vf->vsi, lut, reta_size);
2395         if (ret)
2396                 goto out;
2397         for (i = 0; i < reta_size; i++) {
2398                 idx = i / RTE_RETA_GROUP_SIZE;
2399                 shift = i % RTE_RETA_GROUP_SIZE;
2400                 if (reta_conf[idx].mask & (1ULL << shift))
2401                         lut[i] = reta_conf[idx].reta[shift];
2402         }
2403         ret = i40evf_set_rss_lut(&vf->vsi, lut, reta_size);
2404
2405 out:
2406         rte_free(lut);
2407
2408         return ret;
2409 }
2410
2411 static int
2412 i40evf_dev_rss_reta_query(struct rte_eth_dev *dev,
2413                           struct rte_eth_rss_reta_entry64 *reta_conf,
2414                           uint16_t reta_size)
2415 {
2416         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2417         uint16_t i, idx, shift;
2418         uint8_t *lut;
2419         int ret;
2420
2421         if (reta_size != ETH_RSS_RETA_SIZE_64) {
2422                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
2423                         "(%d) doesn't match the number of hardware can "
2424                         "support (%d)\n", reta_size, ETH_RSS_RETA_SIZE_64);
2425                 return -EINVAL;
2426         }
2427
2428         lut = rte_zmalloc("i40e_rss_lut", reta_size, 0);
2429         if (!lut) {
2430                 PMD_DRV_LOG(ERR, "No memory can be allocated");
2431                 return -ENOMEM;
2432         }
2433
2434         ret = i40evf_get_rss_lut(&vf->vsi, lut, reta_size);
2435         if (ret)
2436                 goto out;
2437         for (i = 0; i < reta_size; i++) {
2438                 idx = i / RTE_RETA_GROUP_SIZE;
2439                 shift = i % RTE_RETA_GROUP_SIZE;
2440                 if (reta_conf[idx].mask & (1ULL << shift))
2441                         reta_conf[idx].reta[shift] = lut[i];
2442         }
2443
2444 out:
2445         rte_free(lut);
2446
2447         return ret;
2448 }
2449
2450 static int
2451 i40evf_set_rss_key(struct i40e_vsi *vsi, uint8_t *key, uint8_t key_len)
2452 {
2453         struct i40e_vf *vf = I40E_VSI_TO_VF(vsi);
2454         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
2455         int ret = 0;
2456
2457         if (!key || key_len == 0) {
2458                 PMD_DRV_LOG(DEBUG, "No key to be configured");
2459                 return 0;
2460         } else if (key_len != (I40E_VFQF_HKEY_MAX_INDEX + 1) *
2461                 sizeof(uint32_t)) {
2462                 PMD_DRV_LOG(ERR, "Invalid key length %u", key_len);
2463                 return -EINVAL;
2464         }
2465
2466         if (vf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
2467                 struct i40e_aqc_get_set_rss_key_data *key_dw =
2468                         (struct i40e_aqc_get_set_rss_key_data *)key;
2469
2470                 ret = i40e_aq_set_rss_key(hw, vsi->vsi_id, key_dw);
2471                 if (ret)
2472                         PMD_INIT_LOG(ERR, "Failed to configure RSS key "
2473                                      "via AQ");
2474         } else {
2475                 uint32_t *hash_key = (uint32_t *)key;
2476                 uint16_t i;
2477
2478                 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
2479                         i40e_write_rx_ctl(hw, I40E_VFQF_HKEY(i), hash_key[i]);
2480                 I40EVF_WRITE_FLUSH(hw);
2481         }
2482
2483         return ret;
2484 }
2485
2486 static int
2487 i40evf_get_rss_key(struct i40e_vsi *vsi, uint8_t *key, uint8_t *key_len)
2488 {
2489         struct i40e_vf *vf = I40E_VSI_TO_VF(vsi);
2490         struct i40e_hw *hw = I40E_VSI_TO_HW(vsi);
2491         int ret;
2492
2493         if (!key || !key_len)
2494                 return -EINVAL;
2495
2496         if (vf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
2497                 ret = i40e_aq_get_rss_key(hw, vsi->vsi_id,
2498                         (struct i40e_aqc_get_set_rss_key_data *)key);
2499                 if (ret) {
2500                         PMD_INIT_LOG(ERR, "Failed to get RSS key via AQ");
2501                         return ret;
2502                 }
2503         } else {
2504                 uint32_t *key_dw = (uint32_t *)key;
2505                 uint16_t i;
2506
2507                 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
2508                         key_dw[i] = i40e_read_rx_ctl(hw, I40E_VFQF_HKEY(i));
2509         }
2510         *key_len = (I40E_VFQF_HKEY_MAX_INDEX + 1) * sizeof(uint32_t);
2511
2512         return 0;
2513 }
2514
2515 static int
2516 i40evf_hw_rss_hash_set(struct i40e_vf *vf, struct rte_eth_rss_conf *rss_conf)
2517 {
2518         struct i40e_hw *hw = I40E_VF_TO_HW(vf);
2519         uint64_t rss_hf, hena;
2520         int ret;
2521
2522         ret = i40evf_set_rss_key(&vf->vsi, rss_conf->rss_key,
2523                                  rss_conf->rss_key_len);
2524         if (ret)
2525                 return ret;
2526
2527         rss_hf = rss_conf->rss_hf;
2528         hena = (uint64_t)i40e_read_rx_ctl(hw, I40E_VFQF_HENA(0));
2529         hena |= ((uint64_t)i40e_read_rx_ctl(hw, I40E_VFQF_HENA(1))) << 32;
2530         if (hw->mac.type == I40E_MAC_X722)
2531                 hena &= ~I40E_RSS_HENA_ALL_X722;
2532         else
2533                 hena &= ~I40E_RSS_HENA_ALL;
2534         hena |= i40e_config_hena(rss_hf, hw->mac.type);
2535         i40e_write_rx_ctl(hw, I40E_VFQF_HENA(0), (uint32_t)hena);
2536         i40e_write_rx_ctl(hw, I40E_VFQF_HENA(1), (uint32_t)(hena >> 32));
2537         I40EVF_WRITE_FLUSH(hw);
2538
2539         return 0;
2540 }
2541
2542 static void
2543 i40evf_disable_rss(struct i40e_vf *vf)
2544 {
2545         struct i40e_hw *hw = I40E_VF_TO_HW(vf);
2546         uint64_t hena;
2547
2548         hena = (uint64_t)i40e_read_rx_ctl(hw, I40E_VFQF_HENA(0));
2549         hena |= ((uint64_t)i40e_read_rx_ctl(hw, I40E_VFQF_HENA(1))) << 32;
2550         if (hw->mac.type == I40E_MAC_X722)
2551                 hena &= ~I40E_RSS_HENA_ALL_X722;
2552         else
2553                 hena &= ~I40E_RSS_HENA_ALL;
2554         i40e_write_rx_ctl(hw, I40E_VFQF_HENA(0), (uint32_t)hena);
2555         i40e_write_rx_ctl(hw, I40E_VFQF_HENA(1), (uint32_t)(hena >> 32));
2556         I40EVF_WRITE_FLUSH(hw);
2557 }
2558
2559 static int
2560 i40evf_config_rss(struct i40e_vf *vf)
2561 {
2562         struct i40e_hw *hw = I40E_VF_TO_HW(vf);
2563         struct rte_eth_rss_conf rss_conf;
2564         uint32_t i, j, lut = 0, nb_q = (I40E_VFQF_HLUT_MAX_INDEX + 1) * 4;
2565         uint16_t num;
2566
2567         if (vf->dev_data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_RSS) {
2568                 i40evf_disable_rss(vf);
2569                 PMD_DRV_LOG(DEBUG, "RSS not configured\n");
2570                 return 0;
2571         }
2572
2573         num = RTE_MIN(vf->dev_data->nb_rx_queues, I40E_MAX_QP_NUM_PER_VF);
2574         /* Fill out the look up table */
2575         for (i = 0, j = 0; i < nb_q; i++, j++) {
2576                 if (j >= num)
2577                         j = 0;
2578                 lut = (lut << 8) | j;
2579                 if ((i & 3) == 3)
2580                         I40E_WRITE_REG(hw, I40E_VFQF_HLUT(i >> 2), lut);
2581         }
2582
2583         rss_conf = vf->dev_data->dev_conf.rx_adv_conf.rss_conf;
2584         if ((rss_conf.rss_hf & I40E_RSS_OFFLOAD_ALL) == 0) {
2585                 i40evf_disable_rss(vf);
2586                 PMD_DRV_LOG(DEBUG, "No hash flag is set\n");
2587                 return 0;
2588         }
2589
2590         if (rss_conf.rss_key == NULL || rss_conf.rss_key_len <
2591                 (I40E_VFQF_HKEY_MAX_INDEX + 1) * sizeof(uint32_t)) {
2592                 /* Calculate the default hash key */
2593                 for (i = 0; i <= I40E_VFQF_HKEY_MAX_INDEX; i++)
2594                         rss_key_default[i] = (uint32_t)rte_rand();
2595                 rss_conf.rss_key = (uint8_t *)rss_key_default;
2596                 rss_conf.rss_key_len = (I40E_VFQF_HKEY_MAX_INDEX + 1) *
2597                         sizeof(uint32_t);
2598         }
2599
2600         return i40evf_hw_rss_hash_set(vf, &rss_conf);
2601 }
2602
2603 static int
2604 i40evf_dev_rss_hash_update(struct rte_eth_dev *dev,
2605                            struct rte_eth_rss_conf *rss_conf)
2606 {
2607         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2608         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2609         uint64_t rss_hf = rss_conf->rss_hf & I40E_RSS_OFFLOAD_ALL;
2610         uint64_t hena;
2611
2612         hena = (uint64_t)i40e_read_rx_ctl(hw, I40E_VFQF_HENA(0));
2613         hena |= ((uint64_t)i40e_read_rx_ctl(hw, I40E_VFQF_HENA(1))) << 32;
2614         if (!(hena & ((hw->mac.type == I40E_MAC_X722)
2615                  ? I40E_RSS_HENA_ALL_X722
2616                  : I40E_RSS_HENA_ALL))) { /* RSS disabled */
2617                 if (rss_hf != 0) /* Enable RSS */
2618                         return -EINVAL;
2619                 return 0;
2620         }
2621
2622         /* RSS enabled */
2623         if (rss_hf == 0) /* Disable RSS */
2624                 return -EINVAL;
2625
2626         return i40evf_hw_rss_hash_set(vf, rss_conf);
2627 }
2628
2629 static int
2630 i40evf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
2631                              struct rte_eth_rss_conf *rss_conf)
2632 {
2633         struct i40e_vf *vf = I40EVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2634         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2635         uint64_t hena;
2636
2637         i40evf_get_rss_key(&vf->vsi, rss_conf->rss_key,
2638                            &rss_conf->rss_key_len);
2639
2640         hena = (uint64_t)i40e_read_rx_ctl(hw, I40E_VFQF_HENA(0));
2641         hena |= ((uint64_t)i40e_read_rx_ctl(hw, I40E_VFQF_HENA(1))) << 32;
2642         rss_conf->rss_hf = i40e_parse_hena(hena);
2643
2644         return 0;
2645 }