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