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