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