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