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