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