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