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