49e6dd1254599301ded917b7b146ee551f93c64a
[dpdk.git] / drivers / net / iavf / iavf_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <sys/queue.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdarg.h>
12 #include <inttypes.h>
13 #include <rte_byteorder.h>
14 #include <rte_common.h>
15
16 #include <rte_interrupts.h>
17 #include <rte_debug.h>
18 #include <rte_pci.h>
19 #include <rte_atomic.h>
20 #include <rte_eal.h>
21 #include <rte_ether.h>
22 #include <rte_ethdev_driver.h>
23 #include <rte_ethdev_pci.h>
24 #include <rte_malloc.h>
25 #include <rte_memzone.h>
26 #include <rte_dev.h>
27
28 #include "iavf.h"
29 #include "iavf_rxtx.h"
30 #include "iavf_generic_flow.h"
31 #include "rte_pmd_iavf.h"
32
33 /* devargs */
34 #define IAVF_PROTO_XTR_ARG         "proto_xtr"
35
36 static const char * const iavf_valid_args[] = {
37         IAVF_PROTO_XTR_ARG,
38         NULL
39 };
40
41 static const struct rte_mbuf_dynfield iavf_proto_xtr_metadata_param = {
42         .name = "intel_pmd_dynfield_proto_xtr_metadata",
43         .size = sizeof(uint32_t),
44         .align = __alignof__(uint32_t),
45         .flags = 0,
46 };
47
48 struct iavf_proto_xtr_ol {
49         const struct rte_mbuf_dynflag param;
50         uint64_t *ol_flag;
51         bool required;
52 };
53
54 static struct iavf_proto_xtr_ol iavf_proto_xtr_params[] = {
55         [IAVF_PROTO_XTR_VLAN] = {
56                 .param = { .name = "intel_pmd_dynflag_proto_xtr_vlan" },
57                 .ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_vlan_mask },
58         [IAVF_PROTO_XTR_IPV4] = {
59                 .param = { .name = "intel_pmd_dynflag_proto_xtr_ipv4" },
60                 .ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_ipv4_mask },
61         [IAVF_PROTO_XTR_IPV6] = {
62                 .param = { .name = "intel_pmd_dynflag_proto_xtr_ipv6" },
63                 .ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_ipv6_mask },
64         [IAVF_PROTO_XTR_IPV6_FLOW] = {
65                 .param = { .name = "intel_pmd_dynflag_proto_xtr_ipv6_flow" },
66                 .ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_ipv6_flow_mask },
67         [IAVF_PROTO_XTR_TCP] = {
68                 .param = { .name = "intel_pmd_dynflag_proto_xtr_tcp" },
69                 .ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_tcp_mask },
70         [IAVF_PROTO_XTR_IP_OFFSET] = {
71                 .param = { .name = "intel_pmd_dynflag_proto_xtr_ip_offset" },
72                 .ol_flag = &rte_pmd_ifd_dynflag_proto_xtr_ip_offset_mask },
73 };
74
75 static int iavf_dev_configure(struct rte_eth_dev *dev);
76 static int iavf_dev_start(struct rte_eth_dev *dev);
77 static int iavf_dev_stop(struct rte_eth_dev *dev);
78 static int iavf_dev_close(struct rte_eth_dev *dev);
79 static int iavf_dev_reset(struct rte_eth_dev *dev);
80 static int iavf_dev_info_get(struct rte_eth_dev *dev,
81                              struct rte_eth_dev_info *dev_info);
82 static const uint32_t *iavf_dev_supported_ptypes_get(struct rte_eth_dev *dev);
83 static int iavf_dev_stats_get(struct rte_eth_dev *dev,
84                              struct rte_eth_stats *stats);
85 static int iavf_dev_stats_reset(struct rte_eth_dev *dev);
86 static int iavf_dev_xstats_get(struct rte_eth_dev *dev,
87                                  struct rte_eth_xstat *xstats, unsigned int n);
88 static int iavf_dev_xstats_get_names(struct rte_eth_dev *dev,
89                                        struct rte_eth_xstat_name *xstats_names,
90                                        unsigned int limit);
91 static int iavf_dev_promiscuous_enable(struct rte_eth_dev *dev);
92 static int iavf_dev_promiscuous_disable(struct rte_eth_dev *dev);
93 static int iavf_dev_allmulticast_enable(struct rte_eth_dev *dev);
94 static int iavf_dev_allmulticast_disable(struct rte_eth_dev *dev);
95 static int iavf_dev_add_mac_addr(struct rte_eth_dev *dev,
96                                 struct rte_ether_addr *addr,
97                                 uint32_t index,
98                                 uint32_t pool);
99 static void iavf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index);
100 static int iavf_dev_vlan_filter_set(struct rte_eth_dev *dev,
101                                    uint16_t vlan_id, int on);
102 static int iavf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
103 static int iavf_dev_rss_reta_update(struct rte_eth_dev *dev,
104                                    struct rte_eth_rss_reta_entry64 *reta_conf,
105                                    uint16_t reta_size);
106 static int iavf_dev_rss_reta_query(struct rte_eth_dev *dev,
107                                   struct rte_eth_rss_reta_entry64 *reta_conf,
108                                   uint16_t reta_size);
109 static int iavf_dev_rss_hash_update(struct rte_eth_dev *dev,
110                                    struct rte_eth_rss_conf *rss_conf);
111 static int iavf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
112                                      struct rte_eth_rss_conf *rss_conf);
113 static int iavf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
114 static int iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
115                                          struct rte_ether_addr *mac_addr);
116 static int iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev,
117                                         uint16_t queue_id);
118 static int iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev,
119                                          uint16_t queue_id);
120 static int iavf_dev_filter_ctrl(struct rte_eth_dev *dev,
121                      enum rte_filter_type filter_type,
122                      enum rte_filter_op filter_op,
123                      void *arg);
124 static int iavf_set_mc_addr_list(struct rte_eth_dev *dev,
125                         struct rte_ether_addr *mc_addrs,
126                         uint32_t mc_addrs_num);
127
128 static const struct rte_pci_id pci_id_iavf_map[] = {
129         { RTE_PCI_DEVICE(IAVF_INTEL_VENDOR_ID, IAVF_DEV_ID_ADAPTIVE_VF) },
130         { .vendor_id = 0, /* sentinel */ },
131 };
132
133 struct rte_iavf_xstats_name_off {
134         char name[RTE_ETH_XSTATS_NAME_SIZE];
135         unsigned int offset;
136 };
137
138 static const struct rte_iavf_xstats_name_off rte_iavf_stats_strings[] = {
139         {"rx_bytes", offsetof(struct iavf_eth_stats, rx_bytes)},
140         {"rx_unicast_packets", offsetof(struct iavf_eth_stats, rx_unicast)},
141         {"rx_multicast_packets", offsetof(struct iavf_eth_stats, rx_multicast)},
142         {"rx_broadcast_packets", offsetof(struct iavf_eth_stats, rx_broadcast)},
143         {"rx_dropped_packets", offsetof(struct iavf_eth_stats, rx_discards)},
144         {"rx_unknown_protocol_packets", offsetof(struct iavf_eth_stats,
145                 rx_unknown_protocol)},
146         {"tx_bytes", offsetof(struct iavf_eth_stats, tx_bytes)},
147         {"tx_unicast_packets", offsetof(struct iavf_eth_stats, tx_unicast)},
148         {"tx_multicast_packets", offsetof(struct iavf_eth_stats, tx_multicast)},
149         {"tx_broadcast_packets", offsetof(struct iavf_eth_stats, tx_broadcast)},
150         {"tx_dropped_packets", offsetof(struct iavf_eth_stats, tx_discards)},
151         {"tx_error_packets", offsetof(struct iavf_eth_stats, tx_errors)},
152 };
153
154 #define IAVF_NB_XSTATS (sizeof(rte_iavf_stats_strings) / \
155                 sizeof(rte_iavf_stats_strings[0]))
156
157 static const struct eth_dev_ops iavf_eth_dev_ops = {
158         .dev_configure              = iavf_dev_configure,
159         .dev_start                  = iavf_dev_start,
160         .dev_stop                   = iavf_dev_stop,
161         .dev_close                  = iavf_dev_close,
162         .dev_reset                  = iavf_dev_reset,
163         .dev_infos_get              = iavf_dev_info_get,
164         .dev_supported_ptypes_get   = iavf_dev_supported_ptypes_get,
165         .link_update                = iavf_dev_link_update,
166         .stats_get                  = iavf_dev_stats_get,
167         .stats_reset                = iavf_dev_stats_reset,
168         .xstats_get                 = iavf_dev_xstats_get,
169         .xstats_get_names           = iavf_dev_xstats_get_names,
170         .xstats_reset               = iavf_dev_stats_reset,
171         .promiscuous_enable         = iavf_dev_promiscuous_enable,
172         .promiscuous_disable        = iavf_dev_promiscuous_disable,
173         .allmulticast_enable        = iavf_dev_allmulticast_enable,
174         .allmulticast_disable       = iavf_dev_allmulticast_disable,
175         .mac_addr_add               = iavf_dev_add_mac_addr,
176         .mac_addr_remove            = iavf_dev_del_mac_addr,
177         .set_mc_addr_list                       = iavf_set_mc_addr_list,
178         .vlan_filter_set            = iavf_dev_vlan_filter_set,
179         .vlan_offload_set           = iavf_dev_vlan_offload_set,
180         .rx_queue_start             = iavf_dev_rx_queue_start,
181         .rx_queue_stop              = iavf_dev_rx_queue_stop,
182         .tx_queue_start             = iavf_dev_tx_queue_start,
183         .tx_queue_stop              = iavf_dev_tx_queue_stop,
184         .rx_queue_setup             = iavf_dev_rx_queue_setup,
185         .rx_queue_release           = iavf_dev_rx_queue_release,
186         .tx_queue_setup             = iavf_dev_tx_queue_setup,
187         .tx_queue_release           = iavf_dev_tx_queue_release,
188         .mac_addr_set               = iavf_dev_set_default_mac_addr,
189         .reta_update                = iavf_dev_rss_reta_update,
190         .reta_query                 = iavf_dev_rss_reta_query,
191         .rss_hash_update            = iavf_dev_rss_hash_update,
192         .rss_hash_conf_get          = iavf_dev_rss_hash_conf_get,
193         .rxq_info_get               = iavf_dev_rxq_info_get,
194         .txq_info_get               = iavf_dev_txq_info_get,
195         .mtu_set                    = iavf_dev_mtu_set,
196         .rx_queue_intr_enable       = iavf_dev_rx_queue_intr_enable,
197         .rx_queue_intr_disable      = iavf_dev_rx_queue_intr_disable,
198         .filter_ctrl                = iavf_dev_filter_ctrl,
199         .tx_done_cleanup            = iavf_dev_tx_done_cleanup,
200 };
201
202 static int
203 iavf_set_mc_addr_list(struct rte_eth_dev *dev,
204                         struct rte_ether_addr *mc_addrs,
205                         uint32_t mc_addrs_num)
206 {
207         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
208         struct iavf_adapter *adapter =
209                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
210         int err, ret;
211
212         if (mc_addrs_num > IAVF_NUM_MACADDR_MAX) {
213                 PMD_DRV_LOG(ERR,
214                             "can't add more than a limited number (%u) of addresses.",
215                             (uint32_t)IAVF_NUM_MACADDR_MAX);
216                 return -EINVAL;
217         }
218
219         /* flush previous addresses */
220         err = iavf_add_del_mc_addr_list(adapter, vf->mc_addrs, vf->mc_addrs_num,
221                                         false);
222         if (err)
223                 return err;
224
225         /* add new ones */
226         err = iavf_add_del_mc_addr_list(adapter, mc_addrs, mc_addrs_num, true);
227
228         if (err) {
229                 /* if adding mac address list fails, should add the previous
230                  * addresses back.
231                  */
232                 ret = iavf_add_del_mc_addr_list(adapter, vf->mc_addrs,
233                                                 vf->mc_addrs_num, true);
234                 if (ret)
235                         return ret;
236         } else {
237                 vf->mc_addrs_num = mc_addrs_num;
238                 memcpy(vf->mc_addrs,
239                        mc_addrs, mc_addrs_num * sizeof(*mc_addrs));
240         }
241
242         return err;
243 }
244
245 static int
246 iavf_init_rss(struct iavf_adapter *adapter)
247 {
248         struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(adapter);
249         struct rte_eth_rss_conf *rss_conf;
250         uint16_t i, j, nb_q;
251         int ret;
252
253         rss_conf = &adapter->eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
254         nb_q = RTE_MIN(adapter->eth_dev->data->nb_rx_queues,
255                        vf->max_rss_qregion);
256
257         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF)) {
258                 PMD_DRV_LOG(DEBUG, "RSS is not supported");
259                 return -ENOTSUP;
260         }
261         if (adapter->eth_dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_RSS) {
262                 PMD_DRV_LOG(WARNING, "RSS is enabled by PF by default");
263                 /* set all lut items to default queue */
264                 for (i = 0; i < vf->vf_res->rss_lut_size; i++)
265                         vf->rss_lut[i] = 0;
266                 ret = iavf_configure_rss_lut(adapter);
267                 return ret;
268         }
269
270         /* configure RSS key */
271         if (!rss_conf->rss_key) {
272                 /* Calculate the default hash key */
273                 for (i = 0; i <= vf->vf_res->rss_key_size; i++)
274                         vf->rss_key[i] = (uint8_t)rte_rand();
275         } else
276                 rte_memcpy(vf->rss_key, rss_conf->rss_key,
277                            RTE_MIN(rss_conf->rss_key_len,
278                                    vf->vf_res->rss_key_size));
279
280         /* init RSS LUT table */
281         for (i = 0, j = 0; i < vf->vf_res->rss_lut_size; i++, j++) {
282                 if (j >= nb_q)
283                         j = 0;
284                 vf->rss_lut[i] = j;
285         }
286         /* send virtchnnl ops to configure rss*/
287         ret = iavf_configure_rss_lut(adapter);
288         if (ret)
289                 return ret;
290         ret = iavf_configure_rss_key(adapter);
291         if (ret)
292                 return ret;
293
294         /* Set RSS hash configuration based on rss_conf->rss_hf. */
295         ret = iavf_rss_hash_set(adapter, rss_conf->rss_hf, true);
296         if (ret) {
297                 PMD_DRV_LOG(ERR, "fail to set default RSS");
298                 return ret;
299         }
300
301         return 0;
302 }
303
304 static int
305 iavf_queues_req_reset(struct rte_eth_dev *dev, uint16_t num)
306 {
307         struct iavf_adapter *ad =
308                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
309         struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(ad);
310         int ret;
311
312         ret = iavf_request_queues(ad, num);
313         if (ret) {
314                 PMD_DRV_LOG(ERR, "request queues from PF failed");
315                 return ret;
316         }
317         PMD_DRV_LOG(INFO, "change queue pairs from %u to %u",
318                         vf->vsi_res->num_queue_pairs, num);
319
320         ret = iavf_dev_reset(dev);
321         if (ret) {
322                 PMD_DRV_LOG(ERR, "vf reset failed");
323                 return ret;
324         }
325
326         return 0;
327 }
328
329 static int
330 iavf_dev_configure(struct rte_eth_dev *dev)
331 {
332         struct iavf_adapter *ad =
333                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
334         struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(ad);
335         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
336         uint16_t num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
337                 dev->data->nb_tx_queues);
338         int ret;
339
340         ad->rx_bulk_alloc_allowed = true;
341         /* Initialize to TRUE. If any of Rx queues doesn't meet the
342          * vector Rx/Tx preconditions, it will be reset.
343          */
344         ad->rx_vec_allowed = true;
345         ad->tx_vec_allowed = true;
346
347         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
348                 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
349
350         /* Large VF setting */
351         if (num_queue_pairs > IAVF_MAX_NUM_QUEUES_DFLT) {
352                 if (!(vf->vf_res->vf_cap_flags &
353                                 VIRTCHNL_VF_LARGE_NUM_QPAIRS)) {
354                         PMD_DRV_LOG(ERR, "large VF is not supported");
355                         return -1;
356                 }
357
358                 if (num_queue_pairs > IAVF_MAX_NUM_QUEUES_LV) {
359                         PMD_DRV_LOG(ERR, "queue pairs number cannot be larger than %u",
360                                 IAVF_MAX_NUM_QUEUES_LV);
361                         return -1;
362                 }
363
364                 ret = iavf_queues_req_reset(dev, num_queue_pairs);
365                 if (ret)
366                         return ret;
367
368                 ret = iavf_get_max_rss_queue_region(ad);
369                 if (ret) {
370                         PMD_INIT_LOG(ERR, "get max rss queue region failed");
371                         return ret;
372                 }
373
374                 vf->lv_enabled = true;
375         } else {
376                 /* Check if large VF is already enabled. If so, disable and
377                  * release redundant queue resource.
378                  * Or check if enough queue pairs. If not, request them from PF.
379                  */
380                 if (vf->lv_enabled ||
381                     num_queue_pairs > vf->vsi_res->num_queue_pairs) {
382                         ret = iavf_queues_req_reset(dev, num_queue_pairs);
383                         if (ret)
384                                 return ret;
385
386                         vf->lv_enabled = false;
387                 }
388                 /* if large VF is not required, use default rss queue region */
389                 vf->max_rss_qregion = IAVF_MAX_NUM_QUEUES_DFLT;
390         }
391
392         /* Vlan stripping setting */
393         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN) {
394                 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
395                         iavf_enable_vlan_strip(ad);
396                 else
397                         iavf_disable_vlan_strip(ad);
398         }
399
400         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
401                 if (iavf_init_rss(ad) != 0) {
402                         PMD_DRV_LOG(ERR, "configure rss failed");
403                         return -1;
404                 }
405         }
406         return 0;
407 }
408
409 static int
410 iavf_init_rxq(struct rte_eth_dev *dev, struct iavf_rx_queue *rxq)
411 {
412         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
413         struct rte_eth_dev_data *dev_data = dev->data;
414         uint16_t buf_size, max_pkt_len, len;
415
416         buf_size = rte_pktmbuf_data_room_size(rxq->mp) - RTE_PKTMBUF_HEADROOM;
417
418         /* Calculate the maximum packet length allowed */
419         len = rxq->rx_buf_len * IAVF_MAX_CHAINED_RX_BUFFERS;
420         max_pkt_len = RTE_MIN(len, dev->data->dev_conf.rxmode.max_rx_pkt_len);
421
422         /* Check if the jumbo frame and maximum packet length are set
423          * correctly.
424          */
425         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
426                 if (max_pkt_len <= RTE_ETHER_MAX_LEN ||
427                     max_pkt_len > IAVF_FRAME_SIZE_MAX) {
428                         PMD_DRV_LOG(ERR, "maximum packet length must be "
429                                     "larger than %u and smaller than %u, "
430                                     "as jumbo frame is enabled",
431                                     (uint32_t)RTE_ETHER_MAX_LEN,
432                                     (uint32_t)IAVF_FRAME_SIZE_MAX);
433                         return -EINVAL;
434                 }
435         } else {
436                 if (max_pkt_len < RTE_ETHER_MIN_LEN ||
437                     max_pkt_len > RTE_ETHER_MAX_LEN) {
438                         PMD_DRV_LOG(ERR, "maximum packet length must be "
439                                     "larger than %u and smaller than %u, "
440                                     "as jumbo frame is disabled",
441                                     (uint32_t)RTE_ETHER_MIN_LEN,
442                                     (uint32_t)RTE_ETHER_MAX_LEN);
443                         return -EINVAL;
444                 }
445         }
446
447         rxq->max_pkt_len = max_pkt_len;
448         if ((dev_data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) ||
449             rxq->max_pkt_len > buf_size) {
450                 dev_data->scattered_rx = 1;
451         }
452         IAVF_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
453         IAVF_WRITE_FLUSH(hw);
454
455         return 0;
456 }
457
458 static int
459 iavf_init_queues(struct rte_eth_dev *dev)
460 {
461         struct iavf_rx_queue **rxq =
462                 (struct iavf_rx_queue **)dev->data->rx_queues;
463         int i, ret = IAVF_SUCCESS;
464
465         for (i = 0; i < dev->data->nb_rx_queues; i++) {
466                 if (!rxq[i] || !rxq[i]->q_set)
467                         continue;
468                 ret = iavf_init_rxq(dev, rxq[i]);
469                 if (ret != IAVF_SUCCESS)
470                         break;
471         }
472         /* set rx/tx function to vector/scatter/single-segment
473          * according to parameters
474          */
475         iavf_set_rx_function(dev);
476         iavf_set_tx_function(dev);
477
478         return ret;
479 }
480
481 static int iavf_config_rx_queues_irqs(struct rte_eth_dev *dev,
482                                      struct rte_intr_handle *intr_handle)
483 {
484         struct iavf_adapter *adapter =
485                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
486         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
487         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
488         struct iavf_qv_map *qv_map;
489         uint16_t interval, i;
490         int vec;
491
492         if (rte_intr_cap_multiple(intr_handle) &&
493             dev->data->dev_conf.intr_conf.rxq) {
494                 if (rte_intr_efd_enable(intr_handle, dev->data->nb_rx_queues))
495                         return -1;
496         }
497
498         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
499                 intr_handle->intr_vec =
500                         rte_zmalloc("intr_vec",
501                                     dev->data->nb_rx_queues * sizeof(int), 0);
502                 if (!intr_handle->intr_vec) {
503                         PMD_DRV_LOG(ERR, "Failed to allocate %d rx intr_vec",
504                                     dev->data->nb_rx_queues);
505                         return -1;
506                 }
507         }
508
509         qv_map = rte_zmalloc("qv_map",
510                 dev->data->nb_rx_queues * sizeof(struct iavf_qv_map), 0);
511         if (!qv_map) {
512                 PMD_DRV_LOG(ERR, "Failed to allocate %d queue-vector map",
513                                 dev->data->nb_rx_queues);
514                 return -1;
515         }
516
517         if (!dev->data->dev_conf.intr_conf.rxq ||
518             !rte_intr_dp_is_en(intr_handle)) {
519                 /* Rx interrupt disabled, Map interrupt only for writeback */
520                 vf->nb_msix = 1;
521                 if (vf->vf_res->vf_cap_flags &
522                     VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
523                         /* If WB_ON_ITR supports, enable it */
524                         vf->msix_base = IAVF_RX_VEC_START;
525                         /* Set the ITR for index zero, to 2us to make sure that
526                          * we leave time for aggregation to occur, but don't
527                          * increase latency dramatically.
528                          */
529                         IAVF_WRITE_REG(hw,
530                                        IAVF_VFINT_DYN_CTLN1(vf->msix_base - 1),
531                                        (0 << IAVF_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) |
532                                        IAVF_VFINT_DYN_CTLN1_WB_ON_ITR_MASK |
533                                        (2UL << IAVF_VFINT_DYN_CTLN1_INTERVAL_SHIFT));
534                         /* debug - check for success! the return value
535                          * should be 2, offset is 0x2800
536                          */
537                         /* IAVF_READ_REG(hw, IAVF_VFINT_ITRN1(0, 0)); */
538                 } else {
539                         /* If no WB_ON_ITR offload flags, need to set
540                          * interrupt for descriptor write back.
541                          */
542                         vf->msix_base = IAVF_MISC_VEC_ID;
543
544                         /* set ITR to max */
545                         interval = iavf_calc_itr_interval(
546                                         IAVF_QUEUE_ITR_INTERVAL_MAX);
547                         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
548                                        IAVF_VFINT_DYN_CTL01_INTENA_MASK |
549                                        (IAVF_ITR_INDEX_DEFAULT <<
550                                         IAVF_VFINT_DYN_CTL01_ITR_INDX_SHIFT) |
551                                        (interval <<
552                                         IAVF_VFINT_DYN_CTL01_INTERVAL_SHIFT));
553                 }
554                 IAVF_WRITE_FLUSH(hw);
555                 /* map all queues to the same interrupt */
556                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
557                         qv_map[i].queue_id = i;
558                         qv_map[i].vector_id = vf->msix_base;
559                 }
560                 vf->qv_map = qv_map;
561         } else {
562                 if (!rte_intr_allow_others(intr_handle)) {
563                         vf->nb_msix = 1;
564                         vf->msix_base = IAVF_MISC_VEC_ID;
565                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
566                                 qv_map[i].queue_id = i;
567                                 qv_map[i].vector_id = vf->msix_base;
568                                 intr_handle->intr_vec[i] = IAVF_MISC_VEC_ID;
569                         }
570                         vf->qv_map = qv_map;
571                         PMD_DRV_LOG(DEBUG,
572                                     "vector %u are mapping to all Rx queues",
573                                     vf->msix_base);
574                 } else {
575                         /* If Rx interrupt is reuquired, and we can use
576                          * multi interrupts, then the vec is from 1
577                          */
578                         vf->nb_msix = RTE_MIN(vf->vf_res->max_vectors,
579                                               intr_handle->nb_efd);
580                         vf->msix_base = IAVF_RX_VEC_START;
581                         vec = IAVF_RX_VEC_START;
582                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
583                                 qv_map[i].queue_id = i;
584                                 qv_map[i].vector_id = vec;
585                                 intr_handle->intr_vec[i] = vec++;
586                                 if (vec >= vf->nb_msix)
587                                         vec = IAVF_RX_VEC_START;
588                         }
589                         vf->qv_map = qv_map;
590                         PMD_DRV_LOG(DEBUG,
591                                     "%u vectors are mapping to %u Rx queues",
592                                     vf->nb_msix, dev->data->nb_rx_queues);
593                 }
594         }
595
596         if (!vf->lv_enabled) {
597                 if (iavf_config_irq_map(adapter)) {
598                         PMD_DRV_LOG(ERR, "config interrupt mapping failed");
599                         return -1;
600                 }
601         } else {
602                 uint16_t num_qv_maps = dev->data->nb_rx_queues;
603                 uint16_t index = 0;
604
605                 while (num_qv_maps > IAVF_IRQ_MAP_NUM_PER_BUF) {
606                         if (iavf_config_irq_map_lv(adapter,
607                                         IAVF_IRQ_MAP_NUM_PER_BUF, index)) {
608                                 PMD_DRV_LOG(ERR, "config interrupt mapping for large VF failed");
609                                 return -1;
610                         }
611                         num_qv_maps -= IAVF_IRQ_MAP_NUM_PER_BUF;
612                         index += IAVF_IRQ_MAP_NUM_PER_BUF;
613                 }
614
615                 if (iavf_config_irq_map_lv(adapter, num_qv_maps, index)) {
616                         PMD_DRV_LOG(ERR, "config interrupt mapping for large VF failed");
617                         return -1;
618                 }
619         }
620         return 0;
621 }
622
623 static int
624 iavf_start_queues(struct rte_eth_dev *dev)
625 {
626         struct iavf_rx_queue *rxq;
627         struct iavf_tx_queue *txq;
628         int i;
629
630         for (i = 0; i < dev->data->nb_tx_queues; i++) {
631                 txq = dev->data->tx_queues[i];
632                 if (txq->tx_deferred_start)
633                         continue;
634                 if (iavf_dev_tx_queue_start(dev, i) != 0) {
635                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
636                         return -1;
637                 }
638         }
639
640         for (i = 0; i < dev->data->nb_rx_queues; i++) {
641                 rxq = dev->data->rx_queues[i];
642                 if (rxq->rx_deferred_start)
643                         continue;
644                 if (iavf_dev_rx_queue_start(dev, i) != 0) {
645                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
646                         return -1;
647                 }
648         }
649
650         return 0;
651 }
652
653 static int
654 iavf_dev_start(struct rte_eth_dev *dev)
655 {
656         struct iavf_adapter *adapter =
657                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
658         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
659         struct rte_intr_handle *intr_handle = dev->intr_handle;
660         uint16_t num_queue_pairs;
661         uint16_t index = 0;
662
663         PMD_INIT_FUNC_TRACE();
664
665         adapter->stopped = 0;
666
667         vf->max_pkt_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
668         vf->num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
669                                       dev->data->nb_tx_queues);
670         num_queue_pairs = vf->num_queue_pairs;
671
672         if (iavf_init_queues(dev) != 0) {
673                 PMD_DRV_LOG(ERR, "failed to do Queue init");
674                 return -1;
675         }
676
677         /* If needed, send configure queues msg multiple times to make the
678          * adminq buffer length smaller than the 4K limitation.
679          */
680         while (num_queue_pairs > IAVF_CFG_Q_NUM_PER_BUF) {
681                 if (iavf_configure_queues(adapter,
682                                 IAVF_CFG_Q_NUM_PER_BUF, index) != 0) {
683                         PMD_DRV_LOG(ERR, "configure queues failed");
684                         goto err_queue;
685                 }
686                 num_queue_pairs -= IAVF_CFG_Q_NUM_PER_BUF;
687                 index += IAVF_CFG_Q_NUM_PER_BUF;
688         }
689
690         if (iavf_configure_queues(adapter, num_queue_pairs, index) != 0) {
691                 PMD_DRV_LOG(ERR, "configure queues failed");
692                 goto err_queue;
693         }
694
695         if (iavf_config_rx_queues_irqs(dev, intr_handle) != 0) {
696                 PMD_DRV_LOG(ERR, "configure irq failed");
697                 goto err_queue;
698         }
699         /* re-enable intr again, because efd assign may change */
700         if (dev->data->dev_conf.intr_conf.rxq != 0) {
701                 rte_intr_disable(intr_handle);
702                 rte_intr_enable(intr_handle);
703         }
704
705         /* Set all mac addrs */
706         iavf_add_del_all_mac_addr(adapter, true);
707
708         /* Set all multicast addresses */
709         iavf_add_del_mc_addr_list(adapter, vf->mc_addrs, vf->mc_addrs_num,
710                                   true);
711
712         if (iavf_start_queues(dev) != 0) {
713                 PMD_DRV_LOG(ERR, "enable queues failed");
714                 goto err_mac;
715         }
716
717         return 0;
718
719 err_mac:
720         iavf_add_del_all_mac_addr(adapter, false);
721 err_queue:
722         return -1;
723 }
724
725 static int
726 iavf_dev_stop(struct rte_eth_dev *dev)
727 {
728         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
729         struct iavf_adapter *adapter =
730                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
731         struct rte_intr_handle *intr_handle = dev->intr_handle;
732
733         PMD_INIT_FUNC_TRACE();
734
735         if (adapter->stopped == 1)
736                 return 0;
737
738         iavf_stop_queues(dev);
739
740         /* Disable the interrupt for Rx */
741         rte_intr_efd_disable(intr_handle);
742         /* Rx interrupt vector mapping free */
743         if (intr_handle->intr_vec) {
744                 rte_free(intr_handle->intr_vec);
745                 intr_handle->intr_vec = NULL;
746         }
747
748         /* remove all mac addrs */
749         iavf_add_del_all_mac_addr(adapter, false);
750
751         /* remove all multicast addresses */
752         iavf_add_del_mc_addr_list(adapter, vf->mc_addrs, vf->mc_addrs_num,
753                                   false);
754
755         adapter->stopped = 1;
756         dev->data->dev_started = 0;
757
758         return 0;
759 }
760
761 static int
762 iavf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
763 {
764         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
765
766         dev_info->max_rx_queues = IAVF_MAX_NUM_QUEUES_LV;
767         dev_info->max_tx_queues = IAVF_MAX_NUM_QUEUES_LV;
768         dev_info->min_rx_bufsize = IAVF_BUF_SIZE_MIN;
769         dev_info->max_rx_pktlen = IAVF_FRAME_SIZE_MAX;
770         dev_info->max_mtu = dev_info->max_rx_pktlen - IAVF_ETH_OVERHEAD;
771         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
772         dev_info->hash_key_size = vf->vf_res->rss_key_size;
773         dev_info->reta_size = vf->vf_res->rss_lut_size;
774         dev_info->flow_type_rss_offloads = IAVF_RSS_OFFLOAD_ALL;
775         dev_info->max_mac_addrs = IAVF_NUM_MACADDR_MAX;
776         dev_info->rx_offload_capa =
777                 DEV_RX_OFFLOAD_VLAN_STRIP |
778                 DEV_RX_OFFLOAD_QINQ_STRIP |
779                 DEV_RX_OFFLOAD_IPV4_CKSUM |
780                 DEV_RX_OFFLOAD_UDP_CKSUM |
781                 DEV_RX_OFFLOAD_TCP_CKSUM |
782                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
783                 DEV_RX_OFFLOAD_SCATTER |
784                 DEV_RX_OFFLOAD_JUMBO_FRAME |
785                 DEV_RX_OFFLOAD_VLAN_FILTER |
786                 DEV_RX_OFFLOAD_RSS_HASH;
787         dev_info->tx_offload_capa =
788                 DEV_TX_OFFLOAD_VLAN_INSERT |
789                 DEV_TX_OFFLOAD_QINQ_INSERT |
790                 DEV_TX_OFFLOAD_IPV4_CKSUM |
791                 DEV_TX_OFFLOAD_UDP_CKSUM |
792                 DEV_TX_OFFLOAD_TCP_CKSUM |
793                 DEV_TX_OFFLOAD_SCTP_CKSUM |
794                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
795                 DEV_TX_OFFLOAD_TCP_TSO |
796                 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
797                 DEV_TX_OFFLOAD_GRE_TNL_TSO |
798                 DEV_TX_OFFLOAD_IPIP_TNL_TSO |
799                 DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
800                 DEV_TX_OFFLOAD_MULTI_SEGS |
801                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
802
803         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_CRC)
804                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_KEEP_CRC;
805
806         dev_info->default_rxconf = (struct rte_eth_rxconf) {
807                 .rx_free_thresh = IAVF_DEFAULT_RX_FREE_THRESH,
808                 .rx_drop_en = 0,
809                 .offloads = 0,
810         };
811
812         dev_info->default_txconf = (struct rte_eth_txconf) {
813                 .tx_free_thresh = IAVF_DEFAULT_TX_FREE_THRESH,
814                 .tx_rs_thresh = IAVF_DEFAULT_TX_RS_THRESH,
815                 .offloads = 0,
816         };
817
818         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
819                 .nb_max = IAVF_MAX_RING_DESC,
820                 .nb_min = IAVF_MIN_RING_DESC,
821                 .nb_align = IAVF_ALIGN_RING_DESC,
822         };
823
824         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
825                 .nb_max = IAVF_MAX_RING_DESC,
826                 .nb_min = IAVF_MIN_RING_DESC,
827                 .nb_align = IAVF_ALIGN_RING_DESC,
828         };
829
830         return 0;
831 }
832
833 static const uint32_t *
834 iavf_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
835 {
836         static const uint32_t ptypes[] = {
837                 RTE_PTYPE_L2_ETHER,
838                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
839                 RTE_PTYPE_L4_FRAG,
840                 RTE_PTYPE_L4_ICMP,
841                 RTE_PTYPE_L4_NONFRAG,
842                 RTE_PTYPE_L4_SCTP,
843                 RTE_PTYPE_L4_TCP,
844                 RTE_PTYPE_L4_UDP,
845                 RTE_PTYPE_UNKNOWN
846         };
847         return ptypes;
848 }
849
850 int
851 iavf_dev_link_update(struct rte_eth_dev *dev,
852                     __rte_unused int wait_to_complete)
853 {
854         struct rte_eth_link new_link;
855         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
856
857         memset(&new_link, 0, sizeof(new_link));
858
859         /* Only read status info stored in VF, and the info is updated
860          *  when receive LINK_CHANGE evnet from PF by Virtchnnl.
861          */
862         switch (vf->link_speed) {
863         case 10:
864                 new_link.link_speed = ETH_SPEED_NUM_10M;
865                 break;
866         case 100:
867                 new_link.link_speed = ETH_SPEED_NUM_100M;
868                 break;
869         case 1000:
870                 new_link.link_speed = ETH_SPEED_NUM_1G;
871                 break;
872         case 10000:
873                 new_link.link_speed = ETH_SPEED_NUM_10G;
874                 break;
875         case 20000:
876                 new_link.link_speed = ETH_SPEED_NUM_20G;
877                 break;
878         case 25000:
879                 new_link.link_speed = ETH_SPEED_NUM_25G;
880                 break;
881         case 40000:
882                 new_link.link_speed = ETH_SPEED_NUM_40G;
883                 break;
884         case 50000:
885                 new_link.link_speed = ETH_SPEED_NUM_50G;
886                 break;
887         case 100000:
888                 new_link.link_speed = ETH_SPEED_NUM_100G;
889                 break;
890         default:
891                 new_link.link_speed = ETH_SPEED_NUM_NONE;
892                 break;
893         }
894
895         new_link.link_duplex = ETH_LINK_FULL_DUPLEX;
896         new_link.link_status = vf->link_up ? ETH_LINK_UP :
897                                              ETH_LINK_DOWN;
898         new_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
899                                 ETH_LINK_SPEED_FIXED);
900
901         return rte_eth_linkstatus_set(dev, &new_link);
902 }
903
904 static int
905 iavf_dev_promiscuous_enable(struct rte_eth_dev *dev)
906 {
907         struct iavf_adapter *adapter =
908                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
909         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
910
911         return iavf_config_promisc(adapter,
912                                   true, vf->promisc_multicast_enabled);
913 }
914
915 static int
916 iavf_dev_promiscuous_disable(struct rte_eth_dev *dev)
917 {
918         struct iavf_adapter *adapter =
919                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
920         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
921
922         return iavf_config_promisc(adapter,
923                                   false, vf->promisc_multicast_enabled);
924 }
925
926 static int
927 iavf_dev_allmulticast_enable(struct rte_eth_dev *dev)
928 {
929         struct iavf_adapter *adapter =
930                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
931         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
932
933         return iavf_config_promisc(adapter,
934                                   vf->promisc_unicast_enabled, true);
935 }
936
937 static int
938 iavf_dev_allmulticast_disable(struct rte_eth_dev *dev)
939 {
940         struct iavf_adapter *adapter =
941                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
942         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
943
944         return iavf_config_promisc(adapter,
945                                   vf->promisc_unicast_enabled, false);
946 }
947
948 static int
949 iavf_dev_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *addr,
950                      __rte_unused uint32_t index,
951                      __rte_unused uint32_t pool)
952 {
953         struct iavf_adapter *adapter =
954                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
955         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
956         int err;
957
958         if (rte_is_zero_ether_addr(addr)) {
959                 PMD_DRV_LOG(ERR, "Invalid Ethernet Address");
960                 return -EINVAL;
961         }
962
963         err = iavf_add_del_eth_addr(adapter, addr, true);
964         if (err) {
965                 PMD_DRV_LOG(ERR, "fail to add MAC address");
966                 return -EIO;
967         }
968
969         vf->mac_num++;
970
971         return 0;
972 }
973
974 static void
975 iavf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
976 {
977         struct iavf_adapter *adapter =
978                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
979         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
980         struct rte_ether_addr *addr;
981         int err;
982
983         addr = &dev->data->mac_addrs[index];
984
985         err = iavf_add_del_eth_addr(adapter, addr, false);
986         if (err)
987                 PMD_DRV_LOG(ERR, "fail to delete MAC address");
988
989         vf->mac_num--;
990 }
991
992 static int
993 iavf_dev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
994 {
995         struct iavf_adapter *adapter =
996                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
997         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
998         int err;
999
1000         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
1001                 return -ENOTSUP;
1002
1003         err = iavf_add_del_vlan(adapter, vlan_id, on);
1004         if (err)
1005                 return -EIO;
1006         return 0;
1007 }
1008
1009 static int
1010 iavf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1011 {
1012         struct iavf_adapter *adapter =
1013                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1014         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1015         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1016         int err;
1017
1018         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
1019                 return -ENOTSUP;
1020
1021         /* Vlan stripping setting */
1022         if (mask & ETH_VLAN_STRIP_MASK) {
1023                 /* Enable or disable VLAN stripping */
1024                 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
1025                         err = iavf_enable_vlan_strip(adapter);
1026                 else
1027                         err = iavf_disable_vlan_strip(adapter);
1028
1029                 if (err)
1030                         return -EIO;
1031         }
1032         return 0;
1033 }
1034
1035 static int
1036 iavf_dev_rss_reta_update(struct rte_eth_dev *dev,
1037                         struct rte_eth_rss_reta_entry64 *reta_conf,
1038                         uint16_t reta_size)
1039 {
1040         struct iavf_adapter *adapter =
1041                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1042         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1043         uint8_t *lut;
1044         uint16_t i, idx, shift;
1045         int ret;
1046
1047         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1048                 return -ENOTSUP;
1049
1050         if (reta_size != vf->vf_res->rss_lut_size) {
1051                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
1052                         "(%d) doesn't match the number of hardware can "
1053                         "support (%d)", reta_size, vf->vf_res->rss_lut_size);
1054                 return -EINVAL;
1055         }
1056
1057         lut = rte_zmalloc("rss_lut", reta_size, 0);
1058         if (!lut) {
1059                 PMD_DRV_LOG(ERR, "No memory can be allocated");
1060                 return -ENOMEM;
1061         }
1062         /* store the old lut table temporarily */
1063         rte_memcpy(lut, vf->rss_lut, reta_size);
1064
1065         for (i = 0; i < reta_size; i++) {
1066                 idx = i / RTE_RETA_GROUP_SIZE;
1067                 shift = i % RTE_RETA_GROUP_SIZE;
1068                 if (reta_conf[idx].mask & (1ULL << shift))
1069                         lut[i] = reta_conf[idx].reta[shift];
1070         }
1071
1072         rte_memcpy(vf->rss_lut, lut, reta_size);
1073         /* send virtchnnl ops to configure rss*/
1074         ret = iavf_configure_rss_lut(adapter);
1075         if (ret) /* revert back */
1076                 rte_memcpy(vf->rss_lut, lut, reta_size);
1077         rte_free(lut);
1078
1079         return ret;
1080 }
1081
1082 static int
1083 iavf_dev_rss_reta_query(struct rte_eth_dev *dev,
1084                        struct rte_eth_rss_reta_entry64 *reta_conf,
1085                        uint16_t reta_size)
1086 {
1087         struct iavf_adapter *adapter =
1088                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1089         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1090         uint16_t i, idx, shift;
1091
1092         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1093                 return -ENOTSUP;
1094
1095         if (reta_size != vf->vf_res->rss_lut_size) {
1096                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
1097                         "(%d) doesn't match the number of hardware can "
1098                         "support (%d)", reta_size, vf->vf_res->rss_lut_size);
1099                 return -EINVAL;
1100         }
1101
1102         for (i = 0; i < reta_size; i++) {
1103                 idx = i / RTE_RETA_GROUP_SIZE;
1104                 shift = i % RTE_RETA_GROUP_SIZE;
1105                 if (reta_conf[idx].mask & (1ULL << shift))
1106                         reta_conf[idx].reta[shift] = vf->rss_lut[i];
1107         }
1108
1109         return 0;
1110 }
1111
1112 static int
1113 iavf_set_rss_key(struct iavf_adapter *adapter, uint8_t *key, uint8_t key_len)
1114 {
1115         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1116
1117         /* HENA setting, it is enabled by default, no change */
1118         if (!key || key_len == 0) {
1119                 PMD_DRV_LOG(DEBUG, "No key to be configured");
1120                 return 0;
1121         } else if (key_len != vf->vf_res->rss_key_size) {
1122                 PMD_DRV_LOG(ERR, "The size of hash key configured "
1123                         "(%d) doesn't match the size of hardware can "
1124                         "support (%d)", key_len,
1125                         vf->vf_res->rss_key_size);
1126                 return -EINVAL;
1127         }
1128
1129         rte_memcpy(vf->rss_key, key, key_len);
1130
1131         return iavf_configure_rss_key(adapter);
1132 }
1133
1134 static int
1135 iavf_dev_rss_hash_update(struct rte_eth_dev *dev,
1136                         struct rte_eth_rss_conf *rss_conf)
1137 {
1138         struct iavf_adapter *adapter =
1139                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1140         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1141         int ret;
1142
1143         adapter->eth_dev->data->dev_conf.rx_adv_conf.rss_conf = *rss_conf;
1144
1145         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1146                 return -ENOTSUP;
1147
1148         /* Set hash key. */
1149         ret = iavf_set_rss_key(adapter, rss_conf->rss_key,
1150                                rss_conf->rss_key_len);
1151         if (ret)
1152                 return ret;
1153
1154         if (rss_conf->rss_hf == 0)
1155                 return 0;
1156
1157         /* Overwritten default RSS. */
1158         ret = iavf_set_hena(adapter, 0);
1159         if (ret)
1160                 PMD_DRV_LOG(ERR, "%s Remove rss vsi fail %d",
1161                             __func__, ret);
1162
1163         /* Set new RSS configuration. */
1164         ret = iavf_rss_hash_set(adapter, rss_conf->rss_hf, true);
1165         if (ret) {
1166                 PMD_DRV_LOG(ERR, "fail to set new RSS");
1167                 return ret;
1168         }
1169
1170         return 0;
1171 }
1172
1173 static int
1174 iavf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
1175                           struct rte_eth_rss_conf *rss_conf)
1176 {
1177         struct iavf_adapter *adapter =
1178                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1179         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1180
1181         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1182                 return -ENOTSUP;
1183
1184         rss_conf->rss_hf = vf->rss_hf;
1185
1186         if (!rss_conf->rss_key)
1187                 return 0;
1188
1189         rss_conf->rss_key_len = vf->vf_res->rss_key_size;
1190         rte_memcpy(rss_conf->rss_key, vf->rss_key, rss_conf->rss_key_len);
1191
1192         return 0;
1193 }
1194
1195 static int
1196 iavf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1197 {
1198         uint32_t frame_size = mtu + IAVF_ETH_OVERHEAD;
1199         int ret = 0;
1200
1201         if (mtu < RTE_ETHER_MIN_MTU || frame_size > IAVF_FRAME_SIZE_MAX)
1202                 return -EINVAL;
1203
1204         /* mtu setting is forbidden if port is start */
1205         if (dev->data->dev_started) {
1206                 PMD_DRV_LOG(ERR, "port must be stopped before configuration");
1207                 return -EBUSY;
1208         }
1209
1210         if (frame_size > RTE_ETHER_MAX_LEN)
1211                 dev->data->dev_conf.rxmode.offloads |=
1212                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
1213         else
1214                 dev->data->dev_conf.rxmode.offloads &=
1215                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
1216
1217         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
1218
1219         return ret;
1220 }
1221
1222 static int
1223 iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
1224                              struct rte_ether_addr *mac_addr)
1225 {
1226         struct iavf_adapter *adapter =
1227                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1228         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1229         struct rte_ether_addr *perm_addr, *old_addr;
1230         int ret;
1231
1232         old_addr = (struct rte_ether_addr *)hw->mac.addr;
1233         perm_addr = (struct rte_ether_addr *)hw->mac.perm_addr;
1234
1235         /* If the MAC address is configured by host, skip the setting */
1236         if (rte_is_valid_assigned_ether_addr(perm_addr))
1237                 return -EPERM;
1238
1239         ret = iavf_add_del_eth_addr(adapter, old_addr, false);
1240         if (ret)
1241                 PMD_DRV_LOG(ERR, "Fail to delete old MAC:"
1242                             " %02X:%02X:%02X:%02X:%02X:%02X",
1243                             old_addr->addr_bytes[0],
1244                             old_addr->addr_bytes[1],
1245                             old_addr->addr_bytes[2],
1246                             old_addr->addr_bytes[3],
1247                             old_addr->addr_bytes[4],
1248                             old_addr->addr_bytes[5]);
1249
1250         ret = iavf_add_del_eth_addr(adapter, mac_addr, true);
1251         if (ret)
1252                 PMD_DRV_LOG(ERR, "Fail to add new MAC:"
1253                             " %02X:%02X:%02X:%02X:%02X:%02X",
1254                             mac_addr->addr_bytes[0],
1255                             mac_addr->addr_bytes[1],
1256                             mac_addr->addr_bytes[2],
1257                             mac_addr->addr_bytes[3],
1258                             mac_addr->addr_bytes[4],
1259                             mac_addr->addr_bytes[5]);
1260
1261         if (ret)
1262                 return -EIO;
1263
1264         rte_ether_addr_copy(mac_addr, (struct rte_ether_addr *)hw->mac.addr);
1265         return 0;
1266 }
1267
1268 static void
1269 iavf_stat_update_48(uint64_t *offset, uint64_t *stat)
1270 {
1271         if (*stat >= *offset)
1272                 *stat = *stat - *offset;
1273         else
1274                 *stat = (uint64_t)((*stat +
1275                         ((uint64_t)1 << IAVF_48_BIT_WIDTH)) - *offset);
1276
1277         *stat &= IAVF_48_BIT_MASK;
1278 }
1279
1280 static void
1281 iavf_stat_update_32(uint64_t *offset, uint64_t *stat)
1282 {
1283         if (*stat >= *offset)
1284                 *stat = (uint64_t)(*stat - *offset);
1285         else
1286                 *stat = (uint64_t)((*stat +
1287                         ((uint64_t)1 << IAVF_32_BIT_WIDTH)) - *offset);
1288 }
1289
1290 static void
1291 iavf_update_stats(struct iavf_vsi *vsi, struct virtchnl_eth_stats *nes)
1292 {
1293         struct virtchnl_eth_stats *oes = &vsi->eth_stats_offset;
1294
1295         iavf_stat_update_48(&oes->rx_bytes, &nes->rx_bytes);
1296         iavf_stat_update_48(&oes->rx_unicast, &nes->rx_unicast);
1297         iavf_stat_update_48(&oes->rx_multicast, &nes->rx_multicast);
1298         iavf_stat_update_48(&oes->rx_broadcast, &nes->rx_broadcast);
1299         iavf_stat_update_32(&oes->rx_discards, &nes->rx_discards);
1300         iavf_stat_update_48(&oes->tx_bytes, &nes->tx_bytes);
1301         iavf_stat_update_48(&oes->tx_unicast, &nes->tx_unicast);
1302         iavf_stat_update_48(&oes->tx_multicast, &nes->tx_multicast);
1303         iavf_stat_update_48(&oes->tx_broadcast, &nes->tx_broadcast);
1304         iavf_stat_update_32(&oes->tx_errors, &nes->tx_errors);
1305         iavf_stat_update_32(&oes->tx_discards, &nes->tx_discards);
1306 }
1307
1308 static int
1309 iavf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1310 {
1311         struct iavf_adapter *adapter =
1312                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1313         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1314         struct iavf_vsi *vsi = &vf->vsi;
1315         struct virtchnl_eth_stats *pstats = NULL;
1316         int ret;
1317
1318         ret = iavf_query_stats(adapter, &pstats);
1319         if (ret == 0) {
1320                 iavf_update_stats(vsi, pstats);
1321                 stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
1322                                 pstats->rx_broadcast - pstats->rx_discards;
1323                 stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
1324                                                 pstats->tx_unicast;
1325                 stats->imissed = pstats->rx_discards;
1326                 stats->oerrors = pstats->tx_errors + pstats->tx_discards;
1327                 stats->ibytes = pstats->rx_bytes;
1328                 stats->ibytes -= stats->ipackets * RTE_ETHER_CRC_LEN;
1329                 stats->obytes = pstats->tx_bytes;
1330         } else {
1331                 PMD_DRV_LOG(ERR, "Get statistics failed");
1332         }
1333         return ret;
1334 }
1335
1336 static int
1337 iavf_dev_stats_reset(struct rte_eth_dev *dev)
1338 {
1339         int ret;
1340         struct iavf_adapter *adapter =
1341                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1342         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1343         struct iavf_vsi *vsi = &vf->vsi;
1344         struct virtchnl_eth_stats *pstats = NULL;
1345
1346         /* read stat values to clear hardware registers */
1347         ret = iavf_query_stats(adapter, &pstats);
1348         if (ret != 0)
1349                 return ret;
1350
1351         /* set stats offset base on current values */
1352         vsi->eth_stats_offset = *pstats;
1353
1354         return 0;
1355 }
1356
1357 static int iavf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
1358                                       struct rte_eth_xstat_name *xstats_names,
1359                                       __rte_unused unsigned int limit)
1360 {
1361         unsigned int i;
1362
1363         if (xstats_names != NULL)
1364                 for (i = 0; i < IAVF_NB_XSTATS; i++) {
1365                         snprintf(xstats_names[i].name,
1366                                 sizeof(xstats_names[i].name),
1367                                 "%s", rte_iavf_stats_strings[i].name);
1368                 }
1369         return IAVF_NB_XSTATS;
1370 }
1371
1372 static int iavf_dev_xstats_get(struct rte_eth_dev *dev,
1373                                  struct rte_eth_xstat *xstats, unsigned int n)
1374 {
1375         int ret;
1376         unsigned int i;
1377         struct iavf_adapter *adapter =
1378                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1379         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1380         struct iavf_vsi *vsi = &vf->vsi;
1381         struct virtchnl_eth_stats *pstats = NULL;
1382
1383         if (n < IAVF_NB_XSTATS)
1384                 return IAVF_NB_XSTATS;
1385
1386         ret = iavf_query_stats(adapter, &pstats);
1387         if (ret != 0)
1388                 return 0;
1389
1390         if (!xstats)
1391                 return 0;
1392
1393         iavf_update_stats(vsi, pstats);
1394
1395         /* loop over xstats array and values from pstats */
1396         for (i = 0; i < IAVF_NB_XSTATS; i++) {
1397                 xstats[i].id = i;
1398                 xstats[i].value = *(uint64_t *)(((char *)pstats) +
1399                         rte_iavf_stats_strings[i].offset);
1400         }
1401
1402         return IAVF_NB_XSTATS;
1403 }
1404
1405
1406 static int
1407 iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1408 {
1409         struct iavf_adapter *adapter =
1410                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1411         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1412         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1413         uint16_t msix_intr;
1414
1415         msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1416         if (msix_intr == IAVF_MISC_VEC_ID) {
1417                 PMD_DRV_LOG(INFO, "MISC is also enabled for control");
1418                 IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1419                                IAVF_VFINT_DYN_CTL01_INTENA_MASK |
1420                                IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1421                                IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1422         } else {
1423                 IAVF_WRITE_REG(hw,
1424                                IAVF_VFINT_DYN_CTLN1
1425                                 (msix_intr - IAVF_RX_VEC_START),
1426                                IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1427                                IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1428                                IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
1429         }
1430
1431         IAVF_WRITE_FLUSH(hw);
1432
1433         rte_intr_ack(&pci_dev->intr_handle);
1434
1435         return 0;
1436 }
1437
1438 static int
1439 iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1440 {
1441         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1442         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1443         uint16_t msix_intr;
1444
1445         msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1446         if (msix_intr == IAVF_MISC_VEC_ID) {
1447                 PMD_DRV_LOG(ERR, "MISC is used for control, cannot disable it");
1448                 return -EIO;
1449         }
1450
1451         IAVF_WRITE_REG(hw,
1452                       IAVF_VFINT_DYN_CTLN1(msix_intr - IAVF_RX_VEC_START),
1453                       0);
1454
1455         IAVF_WRITE_FLUSH(hw);
1456         return 0;
1457 }
1458
1459 static int
1460 iavf_check_vf_reset_done(struct iavf_hw *hw)
1461 {
1462         int i, reset;
1463
1464         for (i = 0; i < IAVF_RESET_WAIT_CNT; i++) {
1465                 reset = IAVF_READ_REG(hw, IAVF_VFGEN_RSTAT) &
1466                         IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1467                 reset = reset >> IAVF_VFGEN_RSTAT_VFR_STATE_SHIFT;
1468                 if (reset == VIRTCHNL_VFR_VFACTIVE ||
1469                     reset == VIRTCHNL_VFR_COMPLETED)
1470                         break;
1471                 rte_delay_ms(20);
1472         }
1473
1474         if (i >= IAVF_RESET_WAIT_CNT)
1475                 return -1;
1476
1477         return 0;
1478 }
1479
1480 static int
1481 iavf_lookup_proto_xtr_type(const char *flex_name)
1482 {
1483         static struct {
1484                 const char *name;
1485                 enum iavf_proto_xtr_type type;
1486         } xtr_type_map[] = {
1487                 { "vlan",      IAVF_PROTO_XTR_VLAN      },
1488                 { "ipv4",      IAVF_PROTO_XTR_IPV4      },
1489                 { "ipv6",      IAVF_PROTO_XTR_IPV6      },
1490                 { "ipv6_flow", IAVF_PROTO_XTR_IPV6_FLOW },
1491                 { "tcp",       IAVF_PROTO_XTR_TCP       },
1492                 { "ip_offset", IAVF_PROTO_XTR_IP_OFFSET },
1493         };
1494         uint32_t i;
1495
1496         for (i = 0; i < RTE_DIM(xtr_type_map); i++) {
1497                 if (strcmp(flex_name, xtr_type_map[i].name) == 0)
1498                         return xtr_type_map[i].type;
1499         }
1500
1501         PMD_DRV_LOG(ERR, "wrong proto_xtr type, "
1502                     "it should be: vlan|ipv4|ipv6|ipv6_flow|tcp|ip_offset");
1503
1504         return -1;
1505 }
1506
1507 /**
1508  * Parse elem, the elem could be single number/range or '(' ')' group
1509  * 1) A single number elem, it's just a simple digit. e.g. 9
1510  * 2) A single range elem, two digits with a '-' between. e.g. 2-6
1511  * 3) A group elem, combines multiple 1) or 2) with '( )'. e.g (0,2-4,6)
1512  *    Within group elem, '-' used for a range separator;
1513  *                       ',' used for a single number.
1514  */
1515 static int
1516 iavf_parse_queue_set(const char *input, int xtr_type,
1517                      struct iavf_devargs *devargs)
1518 {
1519         const char *str = input;
1520         char *end = NULL;
1521         uint32_t min, max;
1522         uint32_t idx;
1523
1524         while (isblank(*str))
1525                 str++;
1526
1527         if (!isdigit(*str) && *str != '(')
1528                 return -1;
1529
1530         /* process single number or single range of number */
1531         if (*str != '(') {
1532                 errno = 0;
1533                 idx = strtoul(str, &end, 10);
1534                 if (errno || !end || idx >= IAVF_MAX_QUEUE_NUM)
1535                         return -1;
1536
1537                 while (isblank(*end))
1538                         end++;
1539
1540                 min = idx;
1541                 max = idx;
1542
1543                 /* process single <number>-<number> */
1544                 if (*end == '-') {
1545                         end++;
1546                         while (isblank(*end))
1547                                 end++;
1548                         if (!isdigit(*end))
1549                                 return -1;
1550
1551                         errno = 0;
1552                         idx = strtoul(end, &end, 10);
1553                         if (errno || !end || idx >= IAVF_MAX_QUEUE_NUM)
1554                                 return -1;
1555
1556                         max = idx;
1557                         while (isblank(*end))
1558                                 end++;
1559                 }
1560
1561                 if (*end != ':')
1562                         return -1;
1563
1564                 for (idx = RTE_MIN(min, max);
1565                      idx <= RTE_MAX(min, max); idx++)
1566                         devargs->proto_xtr[idx] = xtr_type;
1567
1568                 return 0;
1569         }
1570
1571         /* process set within bracket */
1572         str++;
1573         while (isblank(*str))
1574                 str++;
1575         if (*str == '\0')
1576                 return -1;
1577
1578         min = IAVF_MAX_QUEUE_NUM;
1579         do {
1580                 /* go ahead to the first digit */
1581                 while (isblank(*str))
1582                         str++;
1583                 if (!isdigit(*str))
1584                         return -1;
1585
1586                 /* get the digit value */
1587                 errno = 0;
1588                 idx = strtoul(str, &end, 10);
1589                 if (errno || !end || idx >= IAVF_MAX_QUEUE_NUM)
1590                         return -1;
1591
1592                 /* go ahead to separator '-',',' and ')' */
1593                 while (isblank(*end))
1594                         end++;
1595                 if (*end == '-') {
1596                         if (min == IAVF_MAX_QUEUE_NUM)
1597                                 min = idx;
1598                         else /* avoid continuous '-' */
1599                                 return -1;
1600                 } else if (*end == ',' || *end == ')') {
1601                         max = idx;
1602                         if (min == IAVF_MAX_QUEUE_NUM)
1603                                 min = idx;
1604
1605                         for (idx = RTE_MIN(min, max);
1606                              idx <= RTE_MAX(min, max); idx++)
1607                                 devargs->proto_xtr[idx] = xtr_type;
1608
1609                         min = IAVF_MAX_QUEUE_NUM;
1610                 } else {
1611                         return -1;
1612                 }
1613
1614                 str = end + 1;
1615         } while (*end != ')' && *end != '\0');
1616
1617         return 0;
1618 }
1619
1620 static int
1621 iavf_parse_queue_proto_xtr(const char *queues, struct iavf_devargs *devargs)
1622 {
1623         const char *queue_start;
1624         uint32_t idx;
1625         int xtr_type;
1626         char flex_name[32];
1627
1628         while (isblank(*queues))
1629                 queues++;
1630
1631         if (*queues != '[') {
1632                 xtr_type = iavf_lookup_proto_xtr_type(queues);
1633                 if (xtr_type < 0)
1634                         return -1;
1635
1636                 devargs->proto_xtr_dflt = xtr_type;
1637
1638                 return 0;
1639         }
1640
1641         queues++;
1642         do {
1643                 while (isblank(*queues))
1644                         queues++;
1645                 if (*queues == '\0')
1646                         return -1;
1647
1648                 queue_start = queues;
1649
1650                 /* go across a complete bracket */
1651                 if (*queue_start == '(') {
1652                         queues += strcspn(queues, ")");
1653                         if (*queues != ')')
1654                                 return -1;
1655                 }
1656
1657                 /* scan the separator ':' */
1658                 queues += strcspn(queues, ":");
1659                 if (*queues++ != ':')
1660                         return -1;
1661                 while (isblank(*queues))
1662                         queues++;
1663
1664                 for (idx = 0; ; idx++) {
1665                         if (isblank(queues[idx]) ||
1666                             queues[idx] == ',' ||
1667                             queues[idx] == ']' ||
1668                             queues[idx] == '\0')
1669                                 break;
1670
1671                         if (idx > sizeof(flex_name) - 2)
1672                                 return -1;
1673
1674                         flex_name[idx] = queues[idx];
1675                 }
1676                 flex_name[idx] = '\0';
1677                 xtr_type = iavf_lookup_proto_xtr_type(flex_name);
1678                 if (xtr_type < 0)
1679                         return -1;
1680
1681                 queues += idx;
1682
1683                 while (isblank(*queues) || *queues == ',' || *queues == ']')
1684                         queues++;
1685
1686                 if (iavf_parse_queue_set(queue_start, xtr_type, devargs) < 0)
1687                         return -1;
1688         } while (*queues != '\0');
1689
1690         return 0;
1691 }
1692
1693 static int
1694 iavf_handle_proto_xtr_arg(__rte_unused const char *key, const char *value,
1695                           void *extra_args)
1696 {
1697         struct iavf_devargs *devargs = extra_args;
1698
1699         if (!value || !extra_args)
1700                 return -EINVAL;
1701
1702         if (iavf_parse_queue_proto_xtr(value, devargs) < 0) {
1703                 PMD_DRV_LOG(ERR, "the proto_xtr's parameter is wrong : '%s'",
1704                             value);
1705                 return -1;
1706         }
1707
1708         return 0;
1709 }
1710
1711 static int iavf_parse_devargs(struct rte_eth_dev *dev)
1712 {
1713         struct iavf_adapter *ad =
1714                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1715         struct rte_devargs *devargs = dev->device->devargs;
1716         struct rte_kvargs *kvlist;
1717         int ret;
1718
1719         if (!devargs)
1720                 return 0;
1721
1722         kvlist = rte_kvargs_parse(devargs->args, iavf_valid_args);
1723         if (!kvlist) {
1724                 PMD_INIT_LOG(ERR, "invalid kvargs key\n");
1725                 return -EINVAL;
1726         }
1727
1728         ad->devargs.proto_xtr_dflt = IAVF_PROTO_XTR_NONE;
1729         memset(ad->devargs.proto_xtr, IAVF_PROTO_XTR_NONE,
1730                sizeof(ad->devargs.proto_xtr));
1731
1732         ret = rte_kvargs_process(kvlist, IAVF_PROTO_XTR_ARG,
1733                                  &iavf_handle_proto_xtr_arg, &ad->devargs);
1734         if (ret)
1735                 goto bail;
1736
1737 bail:
1738         rte_kvargs_free(kvlist);
1739         return ret;
1740 }
1741
1742 static void
1743 iavf_init_proto_xtr(struct rte_eth_dev *dev)
1744 {
1745         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1746         struct iavf_adapter *ad =
1747                         IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1748         const struct iavf_proto_xtr_ol *xtr_ol;
1749         bool proto_xtr_enable = false;
1750         int offset;
1751         uint16_t i;
1752
1753         vf->proto_xtr = rte_zmalloc("vf proto xtr",
1754                                     vf->vsi_res->num_queue_pairs, 0);
1755         if (unlikely(!(vf->proto_xtr))) {
1756                 PMD_DRV_LOG(ERR, "no memory for setting up proto_xtr's table");
1757                 return;
1758         }
1759
1760         for (i = 0; i < vf->vsi_res->num_queue_pairs; i++) {
1761                 vf->proto_xtr[i] = ad->devargs.proto_xtr[i] !=
1762                                         IAVF_PROTO_XTR_NONE ?
1763                                         ad->devargs.proto_xtr[i] :
1764                                         ad->devargs.proto_xtr_dflt;
1765
1766                 if (vf->proto_xtr[i] != IAVF_PROTO_XTR_NONE) {
1767                         uint8_t type = vf->proto_xtr[i];
1768
1769                         iavf_proto_xtr_params[type].required = true;
1770                         proto_xtr_enable = true;
1771                 }
1772         }
1773
1774         if (likely(!proto_xtr_enable))
1775                 return;
1776
1777         offset = rte_mbuf_dynfield_register(&iavf_proto_xtr_metadata_param);
1778         if (unlikely(offset == -1)) {
1779                 PMD_DRV_LOG(ERR,
1780                             "failed to extract protocol metadata, error %d",
1781                             -rte_errno);
1782                 return;
1783         }
1784
1785         PMD_DRV_LOG(DEBUG,
1786                     "proto_xtr metadata offset in mbuf is : %d",
1787                     offset);
1788         rte_pmd_ifd_dynfield_proto_xtr_metadata_offs = offset;
1789
1790         for (i = 0; i < RTE_DIM(iavf_proto_xtr_params); i++) {
1791                 xtr_ol = &iavf_proto_xtr_params[i];
1792
1793                 uint8_t rxdid = iavf_proto_xtr_type_to_rxdid((uint8_t)i);
1794
1795                 if (!xtr_ol->required)
1796                         continue;
1797
1798                 if (!(vf->supported_rxdid & BIT(rxdid))) {
1799                         PMD_DRV_LOG(ERR,
1800                                     "rxdid[%u] is not supported in hardware",
1801                                     rxdid);
1802                         rte_pmd_ifd_dynfield_proto_xtr_metadata_offs = -1;
1803                         break;
1804                 }
1805
1806                 offset = rte_mbuf_dynflag_register(&xtr_ol->param);
1807                 if (unlikely(offset == -1)) {
1808                         PMD_DRV_LOG(ERR,
1809                                     "failed to register proto_xtr offload '%s', error %d",
1810                                     xtr_ol->param.name, -rte_errno);
1811
1812                         rte_pmd_ifd_dynfield_proto_xtr_metadata_offs = -1;
1813                         break;
1814                 }
1815
1816                 PMD_DRV_LOG(DEBUG,
1817                             "proto_xtr offload '%s' offset in mbuf is : %d",
1818                             xtr_ol->param.name, offset);
1819                 *xtr_ol->ol_flag = 1ULL << offset;
1820         }
1821 }
1822
1823 static int
1824 iavf_init_vf(struct rte_eth_dev *dev)
1825 {
1826         int err, bufsz;
1827         struct iavf_adapter *adapter =
1828                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1829         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1830         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1831
1832         err = iavf_parse_devargs(dev);
1833         if (err) {
1834                 PMD_INIT_LOG(ERR, "Failed to parse devargs");
1835                 goto err;
1836         }
1837
1838         err = iavf_set_mac_type(hw);
1839         if (err) {
1840                 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
1841                 goto err;
1842         }
1843
1844         err = iavf_check_vf_reset_done(hw);
1845         if (err) {
1846                 PMD_INIT_LOG(ERR, "VF is still resetting");
1847                 goto err;
1848         }
1849
1850         iavf_init_adminq_parameter(hw);
1851         err = iavf_init_adminq(hw);
1852         if (err) {
1853                 PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
1854                 goto err;
1855         }
1856
1857         vf->aq_resp = rte_zmalloc("vf_aq_resp", IAVF_AQ_BUF_SZ, 0);
1858         if (!vf->aq_resp) {
1859                 PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
1860                 goto err_aq;
1861         }
1862         if (iavf_check_api_version(adapter) != 0) {
1863                 PMD_INIT_LOG(ERR, "check_api version failed");
1864                 goto err_api;
1865         }
1866
1867         bufsz = sizeof(struct virtchnl_vf_resource) +
1868                 (IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
1869         vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
1870         if (!vf->vf_res) {
1871                 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
1872                 goto err_api;
1873         }
1874         if (iavf_get_vf_resource(adapter) != 0) {
1875                 PMD_INIT_LOG(ERR, "iavf_get_vf_config failed");
1876                 goto err_alloc;
1877         }
1878         /* Allocate memort for RSS info */
1879         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1880                 vf->rss_key = rte_zmalloc("rss_key",
1881                                           vf->vf_res->rss_key_size, 0);
1882                 if (!vf->rss_key) {
1883                         PMD_INIT_LOG(ERR, "unable to allocate rss_key memory");
1884                         goto err_rss;
1885                 }
1886                 vf->rss_lut = rte_zmalloc("rss_lut",
1887                                           vf->vf_res->rss_lut_size, 0);
1888                 if (!vf->rss_lut) {
1889                         PMD_INIT_LOG(ERR, "unable to allocate rss_lut memory");
1890                         goto err_rss;
1891                 }
1892         }
1893
1894         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
1895                 if (iavf_get_supported_rxdid(adapter) != 0) {
1896                         PMD_INIT_LOG(ERR, "failed to do get supported rxdid");
1897                         goto err_rss;
1898                 }
1899         }
1900
1901         iavf_init_proto_xtr(dev);
1902
1903         return 0;
1904 err_rss:
1905         rte_free(vf->rss_key);
1906         rte_free(vf->rss_lut);
1907 err_alloc:
1908         rte_free(vf->vf_res);
1909         vf->vsi_res = NULL;
1910 err_api:
1911         rte_free(vf->aq_resp);
1912 err_aq:
1913         iavf_shutdown_adminq(hw);
1914 err:
1915         return -1;
1916 }
1917
1918 /* Enable default admin queue interrupt setting */
1919 static inline void
1920 iavf_enable_irq0(struct iavf_hw *hw)
1921 {
1922         /* Enable admin queue interrupt trigger */
1923         IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1,
1924                        IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
1925
1926         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1927                        IAVF_VFINT_DYN_CTL01_INTENA_MASK |
1928                        IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1929                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1930
1931         IAVF_WRITE_FLUSH(hw);
1932 }
1933
1934 static inline void
1935 iavf_disable_irq0(struct iavf_hw *hw)
1936 {
1937         /* Disable all interrupt types */
1938         IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1, 0);
1939         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1940                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1941         IAVF_WRITE_FLUSH(hw);
1942 }
1943
1944 static void
1945 iavf_dev_interrupt_handler(void *param)
1946 {
1947         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1948         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1949
1950         iavf_disable_irq0(hw);
1951
1952         iavf_handle_virtchnl_msg(dev);
1953
1954         iavf_enable_irq0(hw);
1955 }
1956
1957 static int
1958 iavf_dev_filter_ctrl(struct rte_eth_dev *dev,
1959                      enum rte_filter_type filter_type,
1960                      enum rte_filter_op filter_op,
1961                      void *arg)
1962 {
1963         int ret = 0;
1964
1965         if (!dev)
1966                 return -EINVAL;
1967
1968         switch (filter_type) {
1969         case RTE_ETH_FILTER_GENERIC:
1970                 if (filter_op != RTE_ETH_FILTER_GET)
1971                         return -EINVAL;
1972                 *(const void **)arg = &iavf_flow_ops;
1973                 break;
1974         default:
1975                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
1976                             filter_type);
1977                 ret = -EINVAL;
1978                 break;
1979         }
1980
1981         return ret;
1982 }
1983
1984
1985 static int
1986 iavf_dev_init(struct rte_eth_dev *eth_dev)
1987 {
1988         struct iavf_adapter *adapter =
1989                 IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1990         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1991         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1992         int ret = 0;
1993
1994         PMD_INIT_FUNC_TRACE();
1995
1996         /* assign ops func pointer */
1997         eth_dev->dev_ops = &iavf_eth_dev_ops;
1998         eth_dev->rx_queue_count = iavf_dev_rxq_count;
1999         eth_dev->rx_descriptor_status = iavf_dev_rx_desc_status;
2000         eth_dev->tx_descriptor_status = iavf_dev_tx_desc_status;
2001         eth_dev->rx_pkt_burst = &iavf_recv_pkts;
2002         eth_dev->tx_pkt_burst = &iavf_xmit_pkts;
2003         eth_dev->tx_pkt_prepare = &iavf_prep_pkts;
2004
2005         /* For secondary processes, we don't initialise any further as primary
2006          * has already done this work. Only check if we need a different RX
2007          * and TX function.
2008          */
2009         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2010                 iavf_set_rx_function(eth_dev);
2011                 iavf_set_tx_function(eth_dev);
2012                 return 0;
2013         }
2014         rte_eth_copy_pci_info(eth_dev, pci_dev);
2015         eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
2016
2017         hw->vendor_id = pci_dev->id.vendor_id;
2018         hw->device_id = pci_dev->id.device_id;
2019         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
2020         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
2021         hw->bus.bus_id = pci_dev->addr.bus;
2022         hw->bus.device = pci_dev->addr.devid;
2023         hw->bus.func = pci_dev->addr.function;
2024         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
2025         hw->back = IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
2026         adapter->eth_dev = eth_dev;
2027         adapter->stopped = 1;
2028
2029         if (iavf_init_vf(eth_dev) != 0) {
2030                 PMD_INIT_LOG(ERR, "Init vf failed");
2031                 return -1;
2032         }
2033
2034         /* set default ptype table */
2035         adapter->ptype_tbl = iavf_get_default_ptype_table();
2036
2037         /* copy mac addr */
2038         eth_dev->data->mac_addrs = rte_zmalloc(
2039                 "iavf_mac", RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX, 0);
2040         if (!eth_dev->data->mac_addrs) {
2041                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
2042                              " store MAC addresses",
2043                              RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX);
2044                 return -ENOMEM;
2045         }
2046         /* If the MAC address is not configured by host,
2047          * generate a random one.
2048          */
2049         if (!rte_is_valid_assigned_ether_addr(
2050                         (struct rte_ether_addr *)hw->mac.addr))
2051                 rte_eth_random_addr(hw->mac.addr);
2052         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.addr,
2053                         &eth_dev->data->mac_addrs[0]);
2054
2055         /* register callback func to eal lib */
2056         rte_intr_callback_register(&pci_dev->intr_handle,
2057                                    iavf_dev_interrupt_handler,
2058                                    (void *)eth_dev);
2059
2060         /* enable uio intr after callback register */
2061         rte_intr_enable(&pci_dev->intr_handle);
2062
2063         /* configure and enable device interrupt */
2064         iavf_enable_irq0(hw);
2065
2066         ret = iavf_flow_init(adapter);
2067         if (ret) {
2068                 PMD_INIT_LOG(ERR, "Failed to initialize flow");
2069                 return ret;
2070         }
2071
2072         /* Set hena = 0 to ask PF to cleanup all existing RSS. */
2073         ret = iavf_set_hena(adapter, 0);
2074         if (ret) {
2075                 PMD_DRV_LOG(ERR, "fail to disable default PF RSS");
2076                 return ret;
2077         }
2078
2079         return 0;
2080 }
2081
2082 static int
2083 iavf_dev_close(struct rte_eth_dev *dev)
2084 {
2085         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2086         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2087         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2088         struct iavf_adapter *adapter =
2089                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
2090         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2091         int ret;
2092
2093         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2094                 return 0;
2095
2096         ret = iavf_dev_stop(dev);
2097
2098         iavf_flow_flush(dev, NULL);
2099         iavf_flow_uninit(adapter);
2100
2101         /*
2102          * disable promiscuous mode before reset vf
2103          * it is a workaround solution when work with kernel driver
2104          * and it is not the normal way
2105          */
2106         if (vf->promisc_unicast_enabled || vf->promisc_multicast_enabled)
2107                 iavf_config_promisc(adapter, false, false);
2108
2109         iavf_shutdown_adminq(hw);
2110         /* disable uio intr before callback unregister */
2111         rte_intr_disable(intr_handle);
2112
2113         /* unregister callback func from eal lib */
2114         rte_intr_callback_unregister(intr_handle,
2115                                      iavf_dev_interrupt_handler, dev);
2116         iavf_disable_irq0(hw);
2117
2118         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2119                 if (vf->rss_lut) {
2120                         rte_free(vf->rss_lut);
2121                         vf->rss_lut = NULL;
2122                 }
2123                 if (vf->rss_key) {
2124                         rte_free(vf->rss_key);
2125                         vf->rss_key = NULL;
2126                 }
2127         }
2128
2129         rte_free(vf->vf_res);
2130         vf->vsi_res = NULL;
2131         vf->vf_res = NULL;
2132
2133         rte_free(vf->aq_resp);
2134         vf->aq_resp = NULL;
2135
2136         vf->vf_reset = false;
2137
2138         return ret;
2139 }
2140
2141 static int
2142 iavf_dev_uninit(struct rte_eth_dev *dev)
2143 {
2144         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2145                 return -EPERM;
2146
2147         iavf_dev_close(dev);
2148
2149         return 0;
2150 }
2151
2152 /*
2153  * Reset VF device only to re-initialize resources in PMD layer
2154  */
2155 static int
2156 iavf_dev_reset(struct rte_eth_dev *dev)
2157 {
2158         int ret;
2159
2160         ret = iavf_dev_uninit(dev);
2161         if (ret)
2162                 return ret;
2163
2164         return iavf_dev_init(dev);
2165 }
2166
2167 static int
2168 iavf_dcf_cap_check_handler(__rte_unused const char *key,
2169                            const char *value, __rte_unused void *opaque)
2170 {
2171         if (strcmp(value, "dcf"))
2172                 return -1;
2173
2174         return 0;
2175 }
2176
2177 static int
2178 iavf_dcf_cap_selected(struct rte_devargs *devargs)
2179 {
2180         struct rte_kvargs *kvlist;
2181         const char *key = "cap";
2182         int ret = 0;
2183
2184         if (devargs == NULL)
2185                 return 0;
2186
2187         kvlist = rte_kvargs_parse(devargs->args, NULL);
2188         if (kvlist == NULL)
2189                 return 0;
2190
2191         if (!rte_kvargs_count(kvlist, key))
2192                 goto exit;
2193
2194         /* dcf capability selected when there's a key-value pair: cap=dcf */
2195         if (rte_kvargs_process(kvlist, key,
2196                                iavf_dcf_cap_check_handler, NULL) < 0)
2197                 goto exit;
2198
2199         ret = 1;
2200
2201 exit:
2202         rte_kvargs_free(kvlist);
2203         return ret;
2204 }
2205
2206 static int eth_iavf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2207                              struct rte_pci_device *pci_dev)
2208 {
2209         if (iavf_dcf_cap_selected(pci_dev->device.devargs))
2210                 return 1;
2211
2212         return rte_eth_dev_pci_generic_probe(pci_dev,
2213                 sizeof(struct iavf_adapter), iavf_dev_init);
2214 }
2215
2216 static int eth_iavf_pci_remove(struct rte_pci_device *pci_dev)
2217 {
2218         return rte_eth_dev_pci_generic_remove(pci_dev, iavf_dev_uninit);
2219 }
2220
2221 /* Adaptive virtual function driver struct */
2222 static struct rte_pci_driver rte_iavf_pmd = {
2223         .id_table = pci_id_iavf_map,
2224         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
2225         .probe = eth_iavf_pci_probe,
2226         .remove = eth_iavf_pci_remove,
2227 };
2228
2229 RTE_PMD_REGISTER_PCI(net_iavf, rte_iavf_pmd);
2230 RTE_PMD_REGISTER_PCI_TABLE(net_iavf, pci_id_iavf_map);
2231 RTE_PMD_REGISTER_KMOD_DEP(net_iavf, "* igb_uio | vfio-pci");
2232 RTE_PMD_REGISTER_PARAM_STRING(net_iavf, "cap=dcf");
2233 RTE_LOG_REGISTER(iavf_logtype_init, pmd.net.iavf.init, NOTICE);
2234 RTE_LOG_REGISTER(iavf_logtype_driver, pmd.net.iavf.driver, NOTICE);
2235 #ifdef RTE_LIBRTE_IAVF_DEBUG_RX
2236 RTE_LOG_REGISTER(iavf_logtype_rx, pmd.net.iavf.rx, DEBUG);
2237 #endif
2238 #ifdef RTE_LIBRTE_IAVF_DEBUG_TX
2239 RTE_LOG_REGISTER(iavf_logtype_tx, pmd.net.iavf.tx, DEBUG);
2240 #endif
2241 #ifdef RTE_LIBRTE_IAVF_DEBUG_TX_FREE
2242 RTE_LOG_REGISTER(iavf_logtype_tx_free, pmd.net.iavf.tx_free, DEBUG);
2243 #endif