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