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