ethdev: move jumbo frame offload check to library
[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         return ret;
1477 }
1478
1479 static int
1480 iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
1481                              struct rte_ether_addr *mac_addr)
1482 {
1483         struct iavf_adapter *adapter =
1484                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1485         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1486         struct rte_ether_addr *old_addr;
1487         int ret;
1488
1489         old_addr = (struct rte_ether_addr *)hw->mac.addr;
1490
1491         if (rte_is_same_ether_addr(old_addr, mac_addr))
1492                 return 0;
1493
1494         ret = iavf_add_del_eth_addr(adapter, old_addr, false, VIRTCHNL_ETHER_ADDR_PRIMARY);
1495         if (ret)
1496                 PMD_DRV_LOG(ERR, "Fail to delete old MAC:"
1497                             RTE_ETHER_ADDR_PRT_FMT,
1498                                 RTE_ETHER_ADDR_BYTES(old_addr));
1499
1500         ret = iavf_add_del_eth_addr(adapter, mac_addr, true, VIRTCHNL_ETHER_ADDR_PRIMARY);
1501         if (ret)
1502                 PMD_DRV_LOG(ERR, "Fail to add new MAC:"
1503                             RTE_ETHER_ADDR_PRT_FMT,
1504                                 RTE_ETHER_ADDR_BYTES(mac_addr));
1505
1506         if (ret)
1507                 return -EIO;
1508
1509         rte_ether_addr_copy(mac_addr, (struct rte_ether_addr *)hw->mac.addr);
1510         return 0;
1511 }
1512
1513 static void
1514 iavf_stat_update_48(uint64_t *offset, uint64_t *stat)
1515 {
1516         if (*stat >= *offset)
1517                 *stat = *stat - *offset;
1518         else
1519                 *stat = (uint64_t)((*stat +
1520                         ((uint64_t)1 << IAVF_48_BIT_WIDTH)) - *offset);
1521
1522         *stat &= IAVF_48_BIT_MASK;
1523 }
1524
1525 static void
1526 iavf_stat_update_32(uint64_t *offset, uint64_t *stat)
1527 {
1528         if (*stat >= *offset)
1529                 *stat = (uint64_t)(*stat - *offset);
1530         else
1531                 *stat = (uint64_t)((*stat +
1532                         ((uint64_t)1 << IAVF_32_BIT_WIDTH)) - *offset);
1533 }
1534
1535 static void
1536 iavf_update_stats(struct iavf_vsi *vsi, struct virtchnl_eth_stats *nes)
1537 {
1538         struct virtchnl_eth_stats *oes = &vsi->eth_stats_offset;
1539
1540         iavf_stat_update_48(&oes->rx_bytes, &nes->rx_bytes);
1541         iavf_stat_update_48(&oes->rx_unicast, &nes->rx_unicast);
1542         iavf_stat_update_48(&oes->rx_multicast, &nes->rx_multicast);
1543         iavf_stat_update_48(&oes->rx_broadcast, &nes->rx_broadcast);
1544         iavf_stat_update_32(&oes->rx_discards, &nes->rx_discards);
1545         iavf_stat_update_48(&oes->tx_bytes, &nes->tx_bytes);
1546         iavf_stat_update_48(&oes->tx_unicast, &nes->tx_unicast);
1547         iavf_stat_update_48(&oes->tx_multicast, &nes->tx_multicast);
1548         iavf_stat_update_48(&oes->tx_broadcast, &nes->tx_broadcast);
1549         iavf_stat_update_32(&oes->tx_errors, &nes->tx_errors);
1550         iavf_stat_update_32(&oes->tx_discards, &nes->tx_discards);
1551 }
1552
1553 static int
1554 iavf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1555 {
1556         struct iavf_adapter *adapter =
1557                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1558         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1559         struct iavf_vsi *vsi = &vf->vsi;
1560         struct virtchnl_eth_stats *pstats = NULL;
1561         int ret;
1562
1563         ret = iavf_query_stats(adapter, &pstats);
1564         if (ret == 0) {
1565                 uint8_t crc_stats_len = (dev->data->dev_conf.rxmode.offloads &
1566                                          DEV_RX_OFFLOAD_KEEP_CRC) ? 0 :
1567                                          RTE_ETHER_CRC_LEN;
1568                 iavf_update_stats(vsi, pstats);
1569                 stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
1570                                 pstats->rx_broadcast - pstats->rx_discards;
1571                 stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
1572                                                 pstats->tx_unicast;
1573                 stats->imissed = pstats->rx_discards;
1574                 stats->oerrors = pstats->tx_errors + pstats->tx_discards;
1575                 stats->ibytes = pstats->rx_bytes;
1576                 stats->ibytes -= stats->ipackets * crc_stats_len;
1577                 stats->obytes = pstats->tx_bytes;
1578         } else {
1579                 PMD_DRV_LOG(ERR, "Get statistics failed");
1580         }
1581         return ret;
1582 }
1583
1584 static int
1585 iavf_dev_stats_reset(struct rte_eth_dev *dev)
1586 {
1587         int ret;
1588         struct iavf_adapter *adapter =
1589                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1590         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1591         struct iavf_vsi *vsi = &vf->vsi;
1592         struct virtchnl_eth_stats *pstats = NULL;
1593
1594         /* read stat values to clear hardware registers */
1595         ret = iavf_query_stats(adapter, &pstats);
1596         if (ret != 0)
1597                 return ret;
1598
1599         /* set stats offset base on current values */
1600         vsi->eth_stats_offset = *pstats;
1601
1602         return 0;
1603 }
1604
1605 static int iavf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
1606                                       struct rte_eth_xstat_name *xstats_names,
1607                                       __rte_unused unsigned int limit)
1608 {
1609         unsigned int i;
1610
1611         if (xstats_names != NULL)
1612                 for (i = 0; i < IAVF_NB_XSTATS; i++) {
1613                         snprintf(xstats_names[i].name,
1614                                 sizeof(xstats_names[i].name),
1615                                 "%s", rte_iavf_stats_strings[i].name);
1616                 }
1617         return IAVF_NB_XSTATS;
1618 }
1619
1620 static int iavf_dev_xstats_get(struct rte_eth_dev *dev,
1621                                  struct rte_eth_xstat *xstats, unsigned int n)
1622 {
1623         int ret;
1624         unsigned int i;
1625         struct iavf_adapter *adapter =
1626                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1627         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1628         struct iavf_vsi *vsi = &vf->vsi;
1629         struct virtchnl_eth_stats *pstats = NULL;
1630
1631         if (n < IAVF_NB_XSTATS)
1632                 return IAVF_NB_XSTATS;
1633
1634         ret = iavf_query_stats(adapter, &pstats);
1635         if (ret != 0)
1636                 return 0;
1637
1638         if (!xstats)
1639                 return 0;
1640
1641         iavf_update_stats(vsi, pstats);
1642
1643         /* loop over xstats array and values from pstats */
1644         for (i = 0; i < IAVF_NB_XSTATS; i++) {
1645                 xstats[i].id = i;
1646                 xstats[i].value = *(uint64_t *)(((char *)pstats) +
1647                         rte_iavf_stats_strings[i].offset);
1648         }
1649
1650         return IAVF_NB_XSTATS;
1651 }
1652
1653
1654 static int
1655 iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1656 {
1657         struct iavf_adapter *adapter =
1658                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1659         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1660         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1661         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1662         uint16_t msix_intr;
1663
1664         msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1665         if (msix_intr == IAVF_MISC_VEC_ID) {
1666                 PMD_DRV_LOG(INFO, "MISC is also enabled for control");
1667                 IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1668                                IAVF_VFINT_DYN_CTL01_INTENA_MASK |
1669                                IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1670                                IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1671         } else {
1672                 IAVF_WRITE_REG(hw,
1673                                IAVF_VFINT_DYN_CTLN1
1674                                 (msix_intr - IAVF_RX_VEC_START),
1675                                IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1676                                IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1677                                IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
1678         }
1679
1680         IAVF_WRITE_FLUSH(hw);
1681
1682         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
1683                 rte_intr_ack(&pci_dev->intr_handle);
1684
1685         return 0;
1686 }
1687
1688 static int
1689 iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1690 {
1691         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1692         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1693         uint16_t msix_intr;
1694
1695         msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1696         if (msix_intr == IAVF_MISC_VEC_ID) {
1697                 PMD_DRV_LOG(ERR, "MISC is used for control, cannot disable it");
1698                 return -EIO;
1699         }
1700
1701         IAVF_WRITE_REG(hw,
1702                       IAVF_VFINT_DYN_CTLN1(msix_intr - IAVF_RX_VEC_START),
1703                       0);
1704
1705         IAVF_WRITE_FLUSH(hw);
1706         return 0;
1707 }
1708
1709 static int
1710 iavf_check_vf_reset_done(struct iavf_hw *hw)
1711 {
1712         int i, reset;
1713
1714         for (i = 0; i < IAVF_RESET_WAIT_CNT; i++) {
1715                 reset = IAVF_READ_REG(hw, IAVF_VFGEN_RSTAT) &
1716                         IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1717                 reset = reset >> IAVF_VFGEN_RSTAT_VFR_STATE_SHIFT;
1718                 if (reset == VIRTCHNL_VFR_VFACTIVE ||
1719                     reset == VIRTCHNL_VFR_COMPLETED)
1720                         break;
1721                 rte_delay_ms(20);
1722         }
1723
1724         if (i >= IAVF_RESET_WAIT_CNT)
1725                 return -1;
1726
1727         return 0;
1728 }
1729
1730 static int
1731 iavf_lookup_proto_xtr_type(const char *flex_name)
1732 {
1733         static struct {
1734                 const char *name;
1735                 enum iavf_proto_xtr_type type;
1736         } xtr_type_map[] = {
1737                 { "vlan",      IAVF_PROTO_XTR_VLAN      },
1738                 { "ipv4",      IAVF_PROTO_XTR_IPV4      },
1739                 { "ipv6",      IAVF_PROTO_XTR_IPV6      },
1740                 { "ipv6_flow", IAVF_PROTO_XTR_IPV6_FLOW },
1741                 { "tcp",       IAVF_PROTO_XTR_TCP       },
1742                 { "ip_offset", IAVF_PROTO_XTR_IP_OFFSET },
1743         };
1744         uint32_t i;
1745
1746         for (i = 0; i < RTE_DIM(xtr_type_map); i++) {
1747                 if (strcmp(flex_name, xtr_type_map[i].name) == 0)
1748                         return xtr_type_map[i].type;
1749         }
1750
1751         PMD_DRV_LOG(ERR, "wrong proto_xtr type, "
1752                     "it should be: vlan|ipv4|ipv6|ipv6_flow|tcp|ip_offset");
1753
1754         return -1;
1755 }
1756
1757 /**
1758  * Parse elem, the elem could be single number/range or '(' ')' group
1759  * 1) A single number elem, it's just a simple digit. e.g. 9
1760  * 2) A single range elem, two digits with a '-' between. e.g. 2-6
1761  * 3) A group elem, combines multiple 1) or 2) with '( )'. e.g (0,2-4,6)
1762  *    Within group elem, '-' used for a range separator;
1763  *                       ',' used for a single number.
1764  */
1765 static int
1766 iavf_parse_queue_set(const char *input, int xtr_type,
1767                      struct iavf_devargs *devargs)
1768 {
1769         const char *str = input;
1770         char *end = NULL;
1771         uint32_t min, max;
1772         uint32_t idx;
1773
1774         while (isblank(*str))
1775                 str++;
1776
1777         if (!isdigit(*str) && *str != '(')
1778                 return -1;
1779
1780         /* process single number or single range of number */
1781         if (*str != '(') {
1782                 errno = 0;
1783                 idx = strtoul(str, &end, 10);
1784                 if (errno || !end || idx >= IAVF_MAX_QUEUE_NUM)
1785                         return -1;
1786
1787                 while (isblank(*end))
1788                         end++;
1789
1790                 min = idx;
1791                 max = idx;
1792
1793                 /* process single <number>-<number> */
1794                 if (*end == '-') {
1795                         end++;
1796                         while (isblank(*end))
1797                                 end++;
1798                         if (!isdigit(*end))
1799                                 return -1;
1800
1801                         errno = 0;
1802                         idx = strtoul(end, &end, 10);
1803                         if (errno || !end || idx >= IAVF_MAX_QUEUE_NUM)
1804                                 return -1;
1805
1806                         max = idx;
1807                         while (isblank(*end))
1808                                 end++;
1809                 }
1810
1811                 if (*end != ':')
1812                         return -1;
1813
1814                 for (idx = RTE_MIN(min, max);
1815                      idx <= RTE_MAX(min, max); idx++)
1816                         devargs->proto_xtr[idx] = xtr_type;
1817
1818                 return 0;
1819         }
1820
1821         /* process set within bracket */
1822         str++;
1823         while (isblank(*str))
1824                 str++;
1825         if (*str == '\0')
1826                 return -1;
1827
1828         min = IAVF_MAX_QUEUE_NUM;
1829         do {
1830                 /* go ahead to the first digit */
1831                 while (isblank(*str))
1832                         str++;
1833                 if (!isdigit(*str))
1834                         return -1;
1835
1836                 /* get the digit value */
1837                 errno = 0;
1838                 idx = strtoul(str, &end, 10);
1839                 if (errno || !end || idx >= IAVF_MAX_QUEUE_NUM)
1840                         return -1;
1841
1842                 /* go ahead to separator '-',',' and ')' */
1843                 while (isblank(*end))
1844                         end++;
1845                 if (*end == '-') {
1846                         if (min == IAVF_MAX_QUEUE_NUM)
1847                                 min = idx;
1848                         else /* avoid continuous '-' */
1849                                 return -1;
1850                 } else if (*end == ',' || *end == ')') {
1851                         max = idx;
1852                         if (min == IAVF_MAX_QUEUE_NUM)
1853                                 min = idx;
1854
1855                         for (idx = RTE_MIN(min, max);
1856                              idx <= RTE_MAX(min, max); idx++)
1857                                 devargs->proto_xtr[idx] = xtr_type;
1858
1859                         min = IAVF_MAX_QUEUE_NUM;
1860                 } else {
1861                         return -1;
1862                 }
1863
1864                 str = end + 1;
1865         } while (*end != ')' && *end != '\0');
1866
1867         return 0;
1868 }
1869
1870 static int
1871 iavf_parse_queue_proto_xtr(const char *queues, struct iavf_devargs *devargs)
1872 {
1873         const char *queue_start;
1874         uint32_t idx;
1875         int xtr_type;
1876         char flex_name[32];
1877
1878         while (isblank(*queues))
1879                 queues++;
1880
1881         if (*queues != '[') {
1882                 xtr_type = iavf_lookup_proto_xtr_type(queues);
1883                 if (xtr_type < 0)
1884                         return -1;
1885
1886                 devargs->proto_xtr_dflt = xtr_type;
1887
1888                 return 0;
1889         }
1890
1891         queues++;
1892         do {
1893                 while (isblank(*queues))
1894                         queues++;
1895                 if (*queues == '\0')
1896                         return -1;
1897
1898                 queue_start = queues;
1899
1900                 /* go across a complete bracket */
1901                 if (*queue_start == '(') {
1902                         queues += strcspn(queues, ")");
1903                         if (*queues != ')')
1904                                 return -1;
1905                 }
1906
1907                 /* scan the separator ':' */
1908                 queues += strcspn(queues, ":");
1909                 if (*queues++ != ':')
1910                         return -1;
1911                 while (isblank(*queues))
1912                         queues++;
1913
1914                 for (idx = 0; ; idx++) {
1915                         if (isblank(queues[idx]) ||
1916                             queues[idx] == ',' ||
1917                             queues[idx] == ']' ||
1918                             queues[idx] == '\0')
1919                                 break;
1920
1921                         if (idx > sizeof(flex_name) - 2)
1922                                 return -1;
1923
1924                         flex_name[idx] = queues[idx];
1925                 }
1926                 flex_name[idx] = '\0';
1927                 xtr_type = iavf_lookup_proto_xtr_type(flex_name);
1928                 if (xtr_type < 0)
1929                         return -1;
1930
1931                 queues += idx;
1932
1933                 while (isblank(*queues) || *queues == ',' || *queues == ']')
1934                         queues++;
1935
1936                 if (iavf_parse_queue_set(queue_start, xtr_type, devargs) < 0)
1937                         return -1;
1938         } while (*queues != '\0');
1939
1940         return 0;
1941 }
1942
1943 static int
1944 iavf_handle_proto_xtr_arg(__rte_unused const char *key, const char *value,
1945                           void *extra_args)
1946 {
1947         struct iavf_devargs *devargs = extra_args;
1948
1949         if (!value || !extra_args)
1950                 return -EINVAL;
1951
1952         if (iavf_parse_queue_proto_xtr(value, devargs) < 0) {
1953                 PMD_DRV_LOG(ERR, "the proto_xtr's parameter is wrong : '%s'",
1954                             value);
1955                 return -1;
1956         }
1957
1958         return 0;
1959 }
1960
1961 static int iavf_parse_devargs(struct rte_eth_dev *dev)
1962 {
1963         struct iavf_adapter *ad =
1964                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1965         struct rte_devargs *devargs = dev->device->devargs;
1966         struct rte_kvargs *kvlist;
1967         int ret;
1968
1969         if (!devargs)
1970                 return 0;
1971
1972         kvlist = rte_kvargs_parse(devargs->args, iavf_valid_args);
1973         if (!kvlist) {
1974                 PMD_INIT_LOG(ERR, "invalid kvargs key\n");
1975                 return -EINVAL;
1976         }
1977
1978         ad->devargs.proto_xtr_dflt = IAVF_PROTO_XTR_NONE;
1979         memset(ad->devargs.proto_xtr, IAVF_PROTO_XTR_NONE,
1980                sizeof(ad->devargs.proto_xtr));
1981
1982         ret = rte_kvargs_process(kvlist, IAVF_PROTO_XTR_ARG,
1983                                  &iavf_handle_proto_xtr_arg, &ad->devargs);
1984         if (ret)
1985                 goto bail;
1986
1987 bail:
1988         rte_kvargs_free(kvlist);
1989         return ret;
1990 }
1991
1992 static void
1993 iavf_init_proto_xtr(struct rte_eth_dev *dev)
1994 {
1995         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1996         struct iavf_adapter *ad =
1997                         IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1998         const struct iavf_proto_xtr_ol *xtr_ol;
1999         bool proto_xtr_enable = false;
2000         int offset;
2001         uint16_t i;
2002
2003         vf->proto_xtr = rte_zmalloc("vf proto xtr",
2004                                     vf->vsi_res->num_queue_pairs, 0);
2005         if (unlikely(!(vf->proto_xtr))) {
2006                 PMD_DRV_LOG(ERR, "no memory for setting up proto_xtr's table");
2007                 return;
2008         }
2009
2010         for (i = 0; i < vf->vsi_res->num_queue_pairs; i++) {
2011                 vf->proto_xtr[i] = ad->devargs.proto_xtr[i] !=
2012                                         IAVF_PROTO_XTR_NONE ?
2013                                         ad->devargs.proto_xtr[i] :
2014                                         ad->devargs.proto_xtr_dflt;
2015
2016                 if (vf->proto_xtr[i] != IAVF_PROTO_XTR_NONE) {
2017                         uint8_t type = vf->proto_xtr[i];
2018
2019                         iavf_proto_xtr_params[type].required = true;
2020                         proto_xtr_enable = true;
2021                 }
2022         }
2023
2024         if (likely(!proto_xtr_enable))
2025                 return;
2026
2027         offset = rte_mbuf_dynfield_register(&iavf_proto_xtr_metadata_param);
2028         if (unlikely(offset == -1)) {
2029                 PMD_DRV_LOG(ERR,
2030                             "failed to extract protocol metadata, error %d",
2031                             -rte_errno);
2032                 return;
2033         }
2034
2035         PMD_DRV_LOG(DEBUG,
2036                     "proto_xtr metadata offset in mbuf is : %d",
2037                     offset);
2038         rte_pmd_ifd_dynfield_proto_xtr_metadata_offs = offset;
2039
2040         for (i = 0; i < RTE_DIM(iavf_proto_xtr_params); i++) {
2041                 xtr_ol = &iavf_proto_xtr_params[i];
2042
2043                 uint8_t rxdid = iavf_proto_xtr_type_to_rxdid((uint8_t)i);
2044
2045                 if (!xtr_ol->required)
2046                         continue;
2047
2048                 if (!(vf->supported_rxdid & BIT(rxdid))) {
2049                         PMD_DRV_LOG(ERR,
2050                                     "rxdid[%u] is not supported in hardware",
2051                                     rxdid);
2052                         rte_pmd_ifd_dynfield_proto_xtr_metadata_offs = -1;
2053                         break;
2054                 }
2055
2056                 offset = rte_mbuf_dynflag_register(&xtr_ol->param);
2057                 if (unlikely(offset == -1)) {
2058                         PMD_DRV_LOG(ERR,
2059                                     "failed to register proto_xtr offload '%s', error %d",
2060                                     xtr_ol->param.name, -rte_errno);
2061
2062                         rte_pmd_ifd_dynfield_proto_xtr_metadata_offs = -1;
2063                         break;
2064                 }
2065
2066                 PMD_DRV_LOG(DEBUG,
2067                             "proto_xtr offload '%s' offset in mbuf is : %d",
2068                             xtr_ol->param.name, offset);
2069                 *xtr_ol->ol_flag = 1ULL << offset;
2070         }
2071 }
2072
2073 static int
2074 iavf_init_vf(struct rte_eth_dev *dev)
2075 {
2076         int err, bufsz;
2077         struct iavf_adapter *adapter =
2078                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
2079         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2080         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2081
2082         vf->eth_dev = dev;
2083
2084         err = iavf_parse_devargs(dev);
2085         if (err) {
2086                 PMD_INIT_LOG(ERR, "Failed to parse devargs");
2087                 goto err;
2088         }
2089
2090         err = iavf_set_mac_type(hw);
2091         if (err) {
2092                 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
2093                 goto err;
2094         }
2095
2096         err = iavf_check_vf_reset_done(hw);
2097         if (err) {
2098                 PMD_INIT_LOG(ERR, "VF is still resetting");
2099                 goto err;
2100         }
2101
2102         iavf_init_adminq_parameter(hw);
2103         err = iavf_init_adminq(hw);
2104         if (err) {
2105                 PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
2106                 goto err;
2107         }
2108
2109         vf->aq_resp = rte_zmalloc("vf_aq_resp", IAVF_AQ_BUF_SZ, 0);
2110         if (!vf->aq_resp) {
2111                 PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
2112                 goto err_aq;
2113         }
2114         if (iavf_check_api_version(adapter) != 0) {
2115                 PMD_INIT_LOG(ERR, "check_api version failed");
2116                 goto err_api;
2117         }
2118
2119         bufsz = sizeof(struct virtchnl_vf_resource) +
2120                 (IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
2121         vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
2122         if (!vf->vf_res) {
2123                 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
2124                 goto err_api;
2125         }
2126
2127         if (iavf_get_vf_resource(adapter) != 0) {
2128                 PMD_INIT_LOG(ERR, "iavf_get_vf_config failed");
2129                 goto err_alloc;
2130         }
2131         /* Allocate memort for RSS info */
2132         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2133                 vf->rss_key = rte_zmalloc("rss_key",
2134                                           vf->vf_res->rss_key_size, 0);
2135                 if (!vf->rss_key) {
2136                         PMD_INIT_LOG(ERR, "unable to allocate rss_key memory");
2137                         goto err_rss;
2138                 }
2139                 vf->rss_lut = rte_zmalloc("rss_lut",
2140                                           vf->vf_res->rss_lut_size, 0);
2141                 if (!vf->rss_lut) {
2142                         PMD_INIT_LOG(ERR, "unable to allocate rss_lut memory");
2143                         goto err_rss;
2144                 }
2145         }
2146
2147         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
2148                 if (iavf_get_supported_rxdid(adapter) != 0) {
2149                         PMD_INIT_LOG(ERR, "failed to do get supported rxdid");
2150                         goto err_rss;
2151                 }
2152         }
2153
2154         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN_V2) {
2155                 if (iavf_get_vlan_offload_caps_v2(adapter) != 0) {
2156                         PMD_INIT_LOG(ERR, "failed to do get VLAN offload v2 capabilities");
2157                         goto err_rss;
2158                 }
2159         }
2160
2161         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_QOS) {
2162                 bufsz = sizeof(struct virtchnl_qos_cap_list) +
2163                         IAVF_MAX_TRAFFIC_CLASS *
2164                         sizeof(struct virtchnl_qos_cap_elem);
2165                 vf->qos_cap = rte_zmalloc("qos_cap", bufsz, 0);
2166                 if (!vf->qos_cap) {
2167                         PMD_INIT_LOG(ERR, "unable to allocate qos_cap memory");
2168                         goto err_rss;
2169                 }
2170                 iavf_tm_conf_init(dev);
2171         }
2172
2173         iavf_init_proto_xtr(dev);
2174
2175         return 0;
2176 err_rss:
2177         rte_free(vf->rss_key);
2178         rte_free(vf->rss_lut);
2179 err_alloc:
2180         rte_free(vf->qos_cap);
2181         rte_free(vf->vf_res);
2182         vf->vsi_res = NULL;
2183 err_api:
2184         rte_free(vf->aq_resp);
2185 err_aq:
2186         iavf_shutdown_adminq(hw);
2187 err:
2188         return -1;
2189 }
2190
2191 static void
2192 iavf_uninit_vf(struct rte_eth_dev *dev)
2193 {
2194         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2195         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2196
2197         iavf_shutdown_adminq(hw);
2198
2199         rte_free(vf->vf_res);
2200         vf->vsi_res = NULL;
2201         vf->vf_res = NULL;
2202
2203         rte_free(vf->aq_resp);
2204         vf->aq_resp = NULL;
2205
2206         rte_free(vf->qos_cap);
2207         vf->qos_cap = NULL;
2208
2209         rte_free(vf->rss_lut);
2210         vf->rss_lut = NULL;
2211         rte_free(vf->rss_key);
2212         vf->rss_key = NULL;
2213 }
2214
2215 /* Enable default admin queue interrupt setting */
2216 static inline void
2217 iavf_enable_irq0(struct iavf_hw *hw)
2218 {
2219         /* Enable admin queue interrupt trigger */
2220         IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1,
2221                        IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
2222
2223         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
2224                        IAVF_VFINT_DYN_CTL01_INTENA_MASK |
2225                        IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
2226                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
2227
2228         IAVF_WRITE_FLUSH(hw);
2229 }
2230
2231 static inline void
2232 iavf_disable_irq0(struct iavf_hw *hw)
2233 {
2234         /* Disable all interrupt types */
2235         IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1, 0);
2236         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
2237                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
2238         IAVF_WRITE_FLUSH(hw);
2239 }
2240
2241 static void
2242 iavf_dev_interrupt_handler(void *param)
2243 {
2244         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2245         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2246
2247         iavf_disable_irq0(hw);
2248
2249         iavf_handle_virtchnl_msg(dev);
2250
2251         iavf_enable_irq0(hw);
2252 }
2253
2254 void
2255 iavf_dev_alarm_handler(void *param)
2256 {
2257         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2258         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2259         uint32_t icr0;
2260
2261         iavf_disable_irq0(hw);
2262
2263         /* read out interrupt causes */
2264         icr0 = IAVF_READ_REG(hw, IAVF_VFINT_ICR01);
2265
2266         if (icr0 & IAVF_VFINT_ICR01_ADMINQ_MASK) {
2267                 PMD_DRV_LOG(DEBUG, "ICR01_ADMINQ is reported");
2268                 iavf_handle_virtchnl_msg(dev);
2269         }
2270
2271         iavf_enable_irq0(hw);
2272
2273         rte_eal_alarm_set(IAVF_ALARM_INTERVAL,
2274                           iavf_dev_alarm_handler, dev);
2275 }
2276
2277 static int
2278 iavf_dev_flow_ops_get(struct rte_eth_dev *dev,
2279                       const struct rte_flow_ops **ops)
2280 {
2281         if (!dev)
2282                 return -EINVAL;
2283
2284         *ops = &iavf_flow_ops;
2285         return 0;
2286 }
2287
2288 static void
2289 iavf_default_rss_disable(struct iavf_adapter *adapter)
2290 {
2291         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
2292         int ret = 0;
2293
2294         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2295                 /* Set hena = 0 to ask PF to cleanup all existing RSS. */
2296                 ret = iavf_set_hena(adapter, 0);
2297                 if (ret)
2298                         /* It is a workaround, temporarily allow error to be
2299                          * returned due to possible lack of PF handling for
2300                          * hena = 0.
2301                          */
2302                         PMD_INIT_LOG(WARNING, "fail to disable default RSS,"
2303                                     "lack PF support");
2304         }
2305 }
2306
2307 static int
2308 iavf_dev_init(struct rte_eth_dev *eth_dev)
2309 {
2310         struct iavf_adapter *adapter =
2311                 IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
2312         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
2313         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
2314         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2315         int ret = 0;
2316
2317         PMD_INIT_FUNC_TRACE();
2318
2319         /* assign ops func pointer */
2320         eth_dev->dev_ops = &iavf_eth_dev_ops;
2321         eth_dev->rx_queue_count = iavf_dev_rxq_count;
2322         eth_dev->rx_descriptor_status = iavf_dev_rx_desc_status;
2323         eth_dev->tx_descriptor_status = iavf_dev_tx_desc_status;
2324         eth_dev->rx_pkt_burst = &iavf_recv_pkts;
2325         eth_dev->tx_pkt_burst = &iavf_xmit_pkts;
2326         eth_dev->tx_pkt_prepare = &iavf_prep_pkts;
2327
2328         /* For secondary processes, we don't initialise any further as primary
2329          * has already done this work. Only check if we need a different RX
2330          * and TX function.
2331          */
2332         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2333                 iavf_set_rx_function(eth_dev);
2334                 iavf_set_tx_function(eth_dev);
2335                 return 0;
2336         }
2337         rte_eth_copy_pci_info(eth_dev, pci_dev);
2338
2339         hw->vendor_id = pci_dev->id.vendor_id;
2340         hw->device_id = pci_dev->id.device_id;
2341         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
2342         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
2343         hw->bus.bus_id = pci_dev->addr.bus;
2344         hw->bus.device = pci_dev->addr.devid;
2345         hw->bus.func = pci_dev->addr.function;
2346         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
2347         hw->back = IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
2348         adapter->dev_data = eth_dev->data;
2349         adapter->stopped = 1;
2350
2351         if (iavf_init_vf(eth_dev) != 0) {
2352                 PMD_INIT_LOG(ERR, "Init vf failed");
2353                 return -1;
2354         }
2355
2356         /* set default ptype table */
2357         adapter->ptype_tbl = iavf_get_default_ptype_table();
2358
2359         /* copy mac addr */
2360         eth_dev->data->mac_addrs = rte_zmalloc(
2361                 "iavf_mac", RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX, 0);
2362         if (!eth_dev->data->mac_addrs) {
2363                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
2364                              " store MAC addresses",
2365                              RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX);
2366                 ret = -ENOMEM;
2367                 goto init_vf_err;
2368         }
2369         /* If the MAC address is not configured by host,
2370          * generate a random one.
2371          */
2372         if (!rte_is_valid_assigned_ether_addr(
2373                         (struct rte_ether_addr *)hw->mac.addr))
2374                 rte_eth_random_addr(hw->mac.addr);
2375         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.addr,
2376                         &eth_dev->data->mac_addrs[0]);
2377
2378         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
2379                 /* register callback func to eal lib */
2380                 rte_intr_callback_register(&pci_dev->intr_handle,
2381                                            iavf_dev_interrupt_handler,
2382                                            (void *)eth_dev);
2383
2384                 /* enable uio intr after callback register */
2385                 rte_intr_enable(&pci_dev->intr_handle);
2386         } else {
2387                 rte_eal_alarm_set(IAVF_ALARM_INTERVAL,
2388                                   iavf_dev_alarm_handler, eth_dev);
2389         }
2390
2391         /* configure and enable device interrupt */
2392         iavf_enable_irq0(hw);
2393
2394         ret = iavf_flow_init(adapter);
2395         if (ret) {
2396                 PMD_INIT_LOG(ERR, "Failed to initialize flow");
2397                 goto flow_init_err;
2398         }
2399
2400         iavf_default_rss_disable(adapter);
2401
2402         return 0;
2403
2404 flow_init_err:
2405         rte_free(eth_dev->data->mac_addrs);
2406         eth_dev->data->mac_addrs = NULL;
2407
2408 init_vf_err:
2409         iavf_uninit_vf(eth_dev);
2410
2411         return ret;
2412 }
2413
2414 static int
2415 iavf_dev_close(struct rte_eth_dev *dev)
2416 {
2417         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2418         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2419         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2420         struct iavf_adapter *adapter =
2421                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
2422         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
2423         int ret;
2424
2425         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2426                 return 0;
2427
2428         ret = iavf_dev_stop(dev);
2429
2430         iavf_flow_flush(dev, NULL);
2431         iavf_flow_uninit(adapter);
2432
2433         /*
2434          * disable promiscuous mode before reset vf
2435          * it is a workaround solution when work with kernel driver
2436          * and it is not the normal way
2437          */
2438         if (vf->promisc_unicast_enabled || vf->promisc_multicast_enabled)
2439                 iavf_config_promisc(adapter, false, false);
2440
2441         iavf_shutdown_adminq(hw);
2442         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
2443                 /* disable uio intr before callback unregister */
2444                 rte_intr_disable(intr_handle);
2445
2446                 /* unregister callback func from eal lib */
2447                 rte_intr_callback_unregister(intr_handle,
2448                                              iavf_dev_interrupt_handler, dev);
2449         } else {
2450                 rte_eal_alarm_cancel(iavf_dev_alarm_handler, dev);
2451         }
2452         iavf_disable_irq0(hw);
2453
2454         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_QOS)
2455                 iavf_tm_conf_uninit(dev);
2456
2457         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
2458                 if (vf->rss_lut) {
2459                         rte_free(vf->rss_lut);
2460                         vf->rss_lut = NULL;
2461                 }
2462                 if (vf->rss_key) {
2463                         rte_free(vf->rss_key);
2464                         vf->rss_key = NULL;
2465                 }
2466         }
2467
2468         rte_free(vf->vf_res);
2469         vf->vsi_res = NULL;
2470         vf->vf_res = NULL;
2471
2472         rte_free(vf->aq_resp);
2473         vf->aq_resp = NULL;
2474
2475         /*
2476          * If the VF is reset via VFLR, the device will be knocked out of bus
2477          * master mode, and the driver will fail to recover from the reset. Fix
2478          * this by enabling bus mastering after every reset. In a non-VFLR case,
2479          * the bus master bit will not be disabled, and this call will have no
2480          * effect.
2481          */
2482         if (vf->vf_reset && !rte_pci_set_bus_master(pci_dev, true))
2483                 vf->vf_reset = false;
2484
2485         return ret;
2486 }
2487
2488 static int
2489 iavf_dev_uninit(struct rte_eth_dev *dev)
2490 {
2491         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2492                 return -EPERM;
2493
2494         iavf_dev_close(dev);
2495
2496         return 0;
2497 }
2498
2499 /*
2500  * Reset VF device only to re-initialize resources in PMD layer
2501  */
2502 static int
2503 iavf_dev_reset(struct rte_eth_dev *dev)
2504 {
2505         int ret;
2506
2507         ret = iavf_dev_uninit(dev);
2508         if (ret)
2509                 return ret;
2510
2511         return iavf_dev_init(dev);
2512 }
2513
2514 static int
2515 iavf_dcf_cap_check_handler(__rte_unused const char *key,
2516                            const char *value, __rte_unused void *opaque)
2517 {
2518         if (strcmp(value, "dcf"))
2519                 return -1;
2520
2521         return 0;
2522 }
2523
2524 static int
2525 iavf_dcf_cap_selected(struct rte_devargs *devargs)
2526 {
2527         struct rte_kvargs *kvlist;
2528         const char *key = "cap";
2529         int ret = 0;
2530
2531         if (devargs == NULL)
2532                 return 0;
2533
2534         kvlist = rte_kvargs_parse(devargs->args, NULL);
2535         if (kvlist == NULL)
2536                 return 0;
2537
2538         if (!rte_kvargs_count(kvlist, key))
2539                 goto exit;
2540
2541         /* dcf capability selected when there's a key-value pair: cap=dcf */
2542         if (rte_kvargs_process(kvlist, key,
2543                                iavf_dcf_cap_check_handler, NULL) < 0)
2544                 goto exit;
2545
2546         ret = 1;
2547
2548 exit:
2549         rte_kvargs_free(kvlist);
2550         return ret;
2551 }
2552
2553 static int eth_iavf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2554                              struct rte_pci_device *pci_dev)
2555 {
2556         if (iavf_dcf_cap_selected(pci_dev->device.devargs))
2557                 return 1;
2558
2559         return rte_eth_dev_pci_generic_probe(pci_dev,
2560                 sizeof(struct iavf_adapter), iavf_dev_init);
2561 }
2562
2563 static int eth_iavf_pci_remove(struct rte_pci_device *pci_dev)
2564 {
2565         return rte_eth_dev_pci_generic_remove(pci_dev, iavf_dev_uninit);
2566 }
2567
2568 /* Adaptive virtual function driver struct */
2569 static struct rte_pci_driver rte_iavf_pmd = {
2570         .id_table = pci_id_iavf_map,
2571         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
2572         .probe = eth_iavf_pci_probe,
2573         .remove = eth_iavf_pci_remove,
2574 };
2575
2576 RTE_PMD_REGISTER_PCI(net_iavf, rte_iavf_pmd);
2577 RTE_PMD_REGISTER_PCI_TABLE(net_iavf, pci_id_iavf_map);
2578 RTE_PMD_REGISTER_KMOD_DEP(net_iavf, "* igb_uio | vfio-pci");
2579 RTE_PMD_REGISTER_PARAM_STRING(net_iavf, "cap=dcf");
2580 RTE_LOG_REGISTER_SUFFIX(iavf_logtype_init, init, NOTICE);
2581 RTE_LOG_REGISTER_SUFFIX(iavf_logtype_driver, driver, NOTICE);
2582 #ifdef RTE_ETHDEV_DEBUG_RX
2583 RTE_LOG_REGISTER_SUFFIX(iavf_logtype_rx, rx, DEBUG);
2584 #endif
2585 #ifdef RTE_ETHDEV_DEBUG_TX
2586 RTE_LOG_REGISTER_SUFFIX(iavf_logtype_tx, tx, DEBUG);
2587 #endif