net/iavf: enable multiple queues configuration for large VF
[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         uint16_t interval, i;
441         int vec;
442
443         if (rte_intr_cap_multiple(intr_handle) &&
444             dev->data->dev_conf.intr_conf.rxq) {
445                 if (rte_intr_efd_enable(intr_handle, dev->data->nb_rx_queues))
446                         return -1;
447         }
448
449         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
450                 intr_handle->intr_vec =
451                         rte_zmalloc("intr_vec",
452                                     dev->data->nb_rx_queues * sizeof(int), 0);
453                 if (!intr_handle->intr_vec) {
454                         PMD_DRV_LOG(ERR, "Failed to allocate %d rx intr_vec",
455                                     dev->data->nb_rx_queues);
456                         return -1;
457                 }
458         }
459
460         if (!dev->data->dev_conf.intr_conf.rxq ||
461             !rte_intr_dp_is_en(intr_handle)) {
462                 /* Rx interrupt disabled, Map interrupt only for writeback */
463                 vf->nb_msix = 1;
464                 if (vf->vf_res->vf_cap_flags &
465                     VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
466                         /* If WB_ON_ITR supports, enable it */
467                         vf->msix_base = IAVF_RX_VEC_START;
468                         IAVF_WRITE_REG(hw,
469                                        IAVF_VFINT_DYN_CTLN1(vf->msix_base - 1),
470                                        IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK |
471                                        IAVF_VFINT_DYN_CTLN1_WB_ON_ITR_MASK);
472                 } else {
473                         /* If no WB_ON_ITR offload flags, need to set
474                          * interrupt for descriptor write back.
475                          */
476                         vf->msix_base = IAVF_MISC_VEC_ID;
477
478                         /* set ITR to max */
479                         interval = iavf_calc_itr_interval(
480                                         IAVF_QUEUE_ITR_INTERVAL_MAX);
481                         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
482                                        IAVF_VFINT_DYN_CTL01_INTENA_MASK |
483                                        (IAVF_ITR_INDEX_DEFAULT <<
484                                         IAVF_VFINT_DYN_CTL01_ITR_INDX_SHIFT) |
485                                        (interval <<
486                                         IAVF_VFINT_DYN_CTL01_INTERVAL_SHIFT));
487                 }
488                 IAVF_WRITE_FLUSH(hw);
489                 /* map all queues to the same interrupt */
490                 for (i = 0; i < dev->data->nb_rx_queues; i++)
491                         vf->rxq_map[vf->msix_base] |= 1 << i;
492         } else {
493                 if (!rte_intr_allow_others(intr_handle)) {
494                         vf->nb_msix = 1;
495                         vf->msix_base = IAVF_MISC_VEC_ID;
496                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
497                                 vf->rxq_map[vf->msix_base] |= 1 << i;
498                                 intr_handle->intr_vec[i] = IAVF_MISC_VEC_ID;
499                         }
500                         PMD_DRV_LOG(DEBUG,
501                                     "vector %u are mapping to all Rx queues",
502                                     vf->msix_base);
503                 } else {
504                         /* If Rx interrupt is reuquired, and we can use
505                          * multi interrupts, then the vec is from 1
506                          */
507                         vf->nb_msix = RTE_MIN(vf->vf_res->max_vectors,
508                                               intr_handle->nb_efd);
509                         vf->msix_base = IAVF_RX_VEC_START;
510                         vec = IAVF_RX_VEC_START;
511                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
512                                 vf->rxq_map[vec] |= 1 << i;
513                                 intr_handle->intr_vec[i] = vec++;
514                                 if (vec >= vf->nb_msix)
515                                         vec = IAVF_RX_VEC_START;
516                         }
517                         PMD_DRV_LOG(DEBUG,
518                                     "%u vectors are mapping to %u Rx queues",
519                                     vf->nb_msix, dev->data->nb_rx_queues);
520                 }
521         }
522
523         if (iavf_config_irq_map(adapter)) {
524                 PMD_DRV_LOG(ERR, "config interrupt mapping failed");
525                 return -1;
526         }
527         return 0;
528 }
529
530 static int
531 iavf_start_queues(struct rte_eth_dev *dev)
532 {
533         struct iavf_rx_queue *rxq;
534         struct iavf_tx_queue *txq;
535         int i;
536
537         for (i = 0; i < dev->data->nb_tx_queues; i++) {
538                 txq = dev->data->tx_queues[i];
539                 if (txq->tx_deferred_start)
540                         continue;
541                 if (iavf_dev_tx_queue_start(dev, i) != 0) {
542                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
543                         return -1;
544                 }
545         }
546
547         for (i = 0; i < dev->data->nb_rx_queues; i++) {
548                 rxq = dev->data->rx_queues[i];
549                 if (rxq->rx_deferred_start)
550                         continue;
551                 if (iavf_dev_rx_queue_start(dev, i) != 0) {
552                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
553                         return -1;
554                 }
555         }
556
557         return 0;
558 }
559
560 static int
561 iavf_dev_start(struct rte_eth_dev *dev)
562 {
563         struct iavf_adapter *adapter =
564                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
565         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
566         struct rte_intr_handle *intr_handle = dev->intr_handle;
567         uint16_t num_queue_pairs;
568         uint16_t index = 0;
569
570         PMD_INIT_FUNC_TRACE();
571
572         adapter->stopped = 0;
573
574         vf->max_pkt_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
575         vf->num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
576                                       dev->data->nb_tx_queues);
577         num_queue_pairs = vf->num_queue_pairs;
578
579         if (iavf_init_queues(dev) != 0) {
580                 PMD_DRV_LOG(ERR, "failed to do Queue init");
581                 return -1;
582         }
583
584         /* If needed, send configure queues msg multiple times to make the
585          * adminq buffer length smaller than the 4K limitation.
586          */
587         while (num_queue_pairs > IAVF_CFG_Q_NUM_PER_BUF) {
588                 if (iavf_configure_queues(adapter,
589                                 IAVF_CFG_Q_NUM_PER_BUF, index) != 0) {
590                         PMD_DRV_LOG(ERR, "configure queues failed");
591                         goto err_queue;
592                 }
593                 num_queue_pairs -= IAVF_CFG_Q_NUM_PER_BUF;
594                 index += IAVF_CFG_Q_NUM_PER_BUF;
595         }
596
597         if (iavf_configure_queues(adapter, num_queue_pairs, index) != 0) {
598                 PMD_DRV_LOG(ERR, "configure queues failed");
599                 goto err_queue;
600         }
601
602         if (iavf_config_rx_queues_irqs(dev, intr_handle) != 0) {
603                 PMD_DRV_LOG(ERR, "configure irq failed");
604                 goto err_queue;
605         }
606         /* re-enable intr again, because efd assign may change */
607         if (dev->data->dev_conf.intr_conf.rxq != 0) {
608                 rte_intr_disable(intr_handle);
609                 rte_intr_enable(intr_handle);
610         }
611
612         /* Set all mac addrs */
613         iavf_add_del_all_mac_addr(adapter, true);
614
615         /* Set all multicast addresses */
616         iavf_add_del_mc_addr_list(adapter, vf->mc_addrs, vf->mc_addrs_num,
617                                   true);
618
619         if (iavf_start_queues(dev) != 0) {
620                 PMD_DRV_LOG(ERR, "enable queues failed");
621                 goto err_mac;
622         }
623
624         return 0;
625
626 err_mac:
627         iavf_add_del_all_mac_addr(adapter, false);
628 err_queue:
629         return -1;
630 }
631
632 static int
633 iavf_dev_stop(struct rte_eth_dev *dev)
634 {
635         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
636         struct iavf_adapter *adapter =
637                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
638         struct rte_intr_handle *intr_handle = dev->intr_handle;
639
640         PMD_INIT_FUNC_TRACE();
641
642         if (adapter->stopped == 1)
643                 return 0;
644
645         iavf_stop_queues(dev);
646
647         /* Disable the interrupt for Rx */
648         rte_intr_efd_disable(intr_handle);
649         /* Rx interrupt vector mapping free */
650         if (intr_handle->intr_vec) {
651                 rte_free(intr_handle->intr_vec);
652                 intr_handle->intr_vec = NULL;
653         }
654
655         /* remove all mac addrs */
656         iavf_add_del_all_mac_addr(adapter, false);
657
658         /* remove all multicast addresses */
659         iavf_add_del_mc_addr_list(adapter, vf->mc_addrs, vf->mc_addrs_num,
660                                   false);
661
662         adapter->stopped = 1;
663         dev->data->dev_started = 0;
664
665         return 0;
666 }
667
668 static int
669 iavf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
670 {
671         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
672
673         dev_info->max_rx_queues = IAVF_MAX_NUM_QUEUES_LV;
674         dev_info->max_tx_queues = IAVF_MAX_NUM_QUEUES_LV;
675         dev_info->min_rx_bufsize = IAVF_BUF_SIZE_MIN;
676         dev_info->max_rx_pktlen = IAVF_FRAME_SIZE_MAX;
677         dev_info->max_mtu = dev_info->max_rx_pktlen - IAVF_ETH_OVERHEAD;
678         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
679         dev_info->hash_key_size = vf->vf_res->rss_key_size;
680         dev_info->reta_size = vf->vf_res->rss_lut_size;
681         dev_info->flow_type_rss_offloads = IAVF_RSS_OFFLOAD_ALL;
682         dev_info->max_mac_addrs = IAVF_NUM_MACADDR_MAX;
683         dev_info->rx_offload_capa =
684                 DEV_RX_OFFLOAD_VLAN_STRIP |
685                 DEV_RX_OFFLOAD_QINQ_STRIP |
686                 DEV_RX_OFFLOAD_IPV4_CKSUM |
687                 DEV_RX_OFFLOAD_UDP_CKSUM |
688                 DEV_RX_OFFLOAD_TCP_CKSUM |
689                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
690                 DEV_RX_OFFLOAD_SCATTER |
691                 DEV_RX_OFFLOAD_JUMBO_FRAME |
692                 DEV_RX_OFFLOAD_VLAN_FILTER |
693                 DEV_RX_OFFLOAD_RSS_HASH;
694         dev_info->tx_offload_capa =
695                 DEV_TX_OFFLOAD_VLAN_INSERT |
696                 DEV_TX_OFFLOAD_QINQ_INSERT |
697                 DEV_TX_OFFLOAD_IPV4_CKSUM |
698                 DEV_TX_OFFLOAD_UDP_CKSUM |
699                 DEV_TX_OFFLOAD_TCP_CKSUM |
700                 DEV_TX_OFFLOAD_SCTP_CKSUM |
701                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
702                 DEV_TX_OFFLOAD_TCP_TSO |
703                 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
704                 DEV_TX_OFFLOAD_GRE_TNL_TSO |
705                 DEV_TX_OFFLOAD_IPIP_TNL_TSO |
706                 DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
707                 DEV_TX_OFFLOAD_MULTI_SEGS;
708
709         dev_info->default_rxconf = (struct rte_eth_rxconf) {
710                 .rx_free_thresh = IAVF_DEFAULT_RX_FREE_THRESH,
711                 .rx_drop_en = 0,
712                 .offloads = 0,
713         };
714
715         dev_info->default_txconf = (struct rte_eth_txconf) {
716                 .tx_free_thresh = IAVF_DEFAULT_TX_FREE_THRESH,
717                 .tx_rs_thresh = IAVF_DEFAULT_TX_RS_THRESH,
718                 .offloads = 0,
719         };
720
721         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
722                 .nb_max = IAVF_MAX_RING_DESC,
723                 .nb_min = IAVF_MIN_RING_DESC,
724                 .nb_align = IAVF_ALIGN_RING_DESC,
725         };
726
727         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
728                 .nb_max = IAVF_MAX_RING_DESC,
729                 .nb_min = IAVF_MIN_RING_DESC,
730                 .nb_align = IAVF_ALIGN_RING_DESC,
731         };
732
733         return 0;
734 }
735
736 static const uint32_t *
737 iavf_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
738 {
739         static const uint32_t ptypes[] = {
740                 RTE_PTYPE_L2_ETHER,
741                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
742                 RTE_PTYPE_L4_FRAG,
743                 RTE_PTYPE_L4_ICMP,
744                 RTE_PTYPE_L4_NONFRAG,
745                 RTE_PTYPE_L4_SCTP,
746                 RTE_PTYPE_L4_TCP,
747                 RTE_PTYPE_L4_UDP,
748                 RTE_PTYPE_UNKNOWN
749         };
750         return ptypes;
751 }
752
753 int
754 iavf_dev_link_update(struct rte_eth_dev *dev,
755                     __rte_unused int wait_to_complete)
756 {
757         struct rte_eth_link new_link;
758         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
759
760         memset(&new_link, 0, sizeof(new_link));
761
762         /* Only read status info stored in VF, and the info is updated
763          *  when receive LINK_CHANGE evnet from PF by Virtchnnl.
764          */
765         switch (vf->link_speed) {
766         case 10:
767                 new_link.link_speed = ETH_SPEED_NUM_10M;
768                 break;
769         case 100:
770                 new_link.link_speed = ETH_SPEED_NUM_100M;
771                 break;
772         case 1000:
773                 new_link.link_speed = ETH_SPEED_NUM_1G;
774                 break;
775         case 10000:
776                 new_link.link_speed = ETH_SPEED_NUM_10G;
777                 break;
778         case 20000:
779                 new_link.link_speed = ETH_SPEED_NUM_20G;
780                 break;
781         case 25000:
782                 new_link.link_speed = ETH_SPEED_NUM_25G;
783                 break;
784         case 40000:
785                 new_link.link_speed = ETH_SPEED_NUM_40G;
786                 break;
787         case 50000:
788                 new_link.link_speed = ETH_SPEED_NUM_50G;
789                 break;
790         case 100000:
791                 new_link.link_speed = ETH_SPEED_NUM_100G;
792                 break;
793         default:
794                 new_link.link_speed = ETH_SPEED_NUM_NONE;
795                 break;
796         }
797
798         new_link.link_duplex = ETH_LINK_FULL_DUPLEX;
799         new_link.link_status = vf->link_up ? ETH_LINK_UP :
800                                              ETH_LINK_DOWN;
801         new_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
802                                 ETH_LINK_SPEED_FIXED);
803
804         return rte_eth_linkstatus_set(dev, &new_link);
805 }
806
807 static int
808 iavf_dev_promiscuous_enable(struct rte_eth_dev *dev)
809 {
810         struct iavf_adapter *adapter =
811                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
812         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
813
814         return iavf_config_promisc(adapter,
815                                   true, vf->promisc_multicast_enabled);
816 }
817
818 static int
819 iavf_dev_promiscuous_disable(struct rte_eth_dev *dev)
820 {
821         struct iavf_adapter *adapter =
822                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
823         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
824
825         return iavf_config_promisc(adapter,
826                                   false, vf->promisc_multicast_enabled);
827 }
828
829 static int
830 iavf_dev_allmulticast_enable(struct rte_eth_dev *dev)
831 {
832         struct iavf_adapter *adapter =
833                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
834         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
835
836         return iavf_config_promisc(adapter,
837                                   vf->promisc_unicast_enabled, true);
838 }
839
840 static int
841 iavf_dev_allmulticast_disable(struct rte_eth_dev *dev)
842 {
843         struct iavf_adapter *adapter =
844                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
845         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
846
847         return iavf_config_promisc(adapter,
848                                   vf->promisc_unicast_enabled, false);
849 }
850
851 static int
852 iavf_dev_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *addr,
853                      __rte_unused uint32_t index,
854                      __rte_unused uint32_t pool)
855 {
856         struct iavf_adapter *adapter =
857                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
858         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
859         int err;
860
861         if (rte_is_zero_ether_addr(addr)) {
862                 PMD_DRV_LOG(ERR, "Invalid Ethernet Address");
863                 return -EINVAL;
864         }
865
866         err = iavf_add_del_eth_addr(adapter, addr, true);
867         if (err) {
868                 PMD_DRV_LOG(ERR, "fail to add MAC address");
869                 return -EIO;
870         }
871
872         vf->mac_num++;
873
874         return 0;
875 }
876
877 static void
878 iavf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
879 {
880         struct iavf_adapter *adapter =
881                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
882         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
883         struct rte_ether_addr *addr;
884         int err;
885
886         addr = &dev->data->mac_addrs[index];
887
888         err = iavf_add_del_eth_addr(adapter, addr, false);
889         if (err)
890                 PMD_DRV_LOG(ERR, "fail to delete MAC address");
891
892         vf->mac_num--;
893 }
894
895 static int
896 iavf_dev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
897 {
898         struct iavf_adapter *adapter =
899                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
900         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
901         int err;
902
903         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
904                 return -ENOTSUP;
905
906         err = iavf_add_del_vlan(adapter, vlan_id, on);
907         if (err)
908                 return -EIO;
909         return 0;
910 }
911
912 static int
913 iavf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
914 {
915         struct iavf_adapter *adapter =
916                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
917         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
918         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
919         int err;
920
921         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
922                 return -ENOTSUP;
923
924         /* Vlan stripping setting */
925         if (mask & ETH_VLAN_STRIP_MASK) {
926                 /* Enable or disable VLAN stripping */
927                 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
928                         err = iavf_enable_vlan_strip(adapter);
929                 else
930                         err = iavf_disable_vlan_strip(adapter);
931
932                 if (err)
933                         return -EIO;
934         }
935         return 0;
936 }
937
938 static int
939 iavf_dev_rss_reta_update(struct rte_eth_dev *dev,
940                         struct rte_eth_rss_reta_entry64 *reta_conf,
941                         uint16_t reta_size)
942 {
943         struct iavf_adapter *adapter =
944                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
945         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
946         uint8_t *lut;
947         uint16_t i, idx, shift;
948         int ret;
949
950         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
951                 return -ENOTSUP;
952
953         if (reta_size != vf->vf_res->rss_lut_size) {
954                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
955                         "(%d) doesn't match the number of hardware can "
956                         "support (%d)", reta_size, vf->vf_res->rss_lut_size);
957                 return -EINVAL;
958         }
959
960         lut = rte_zmalloc("rss_lut", reta_size, 0);
961         if (!lut) {
962                 PMD_DRV_LOG(ERR, "No memory can be allocated");
963                 return -ENOMEM;
964         }
965         /* store the old lut table temporarily */
966         rte_memcpy(lut, vf->rss_lut, reta_size);
967
968         for (i = 0; i < reta_size; i++) {
969                 idx = i / RTE_RETA_GROUP_SIZE;
970                 shift = i % RTE_RETA_GROUP_SIZE;
971                 if (reta_conf[idx].mask & (1ULL << shift))
972                         lut[i] = reta_conf[idx].reta[shift];
973         }
974
975         rte_memcpy(vf->rss_lut, lut, reta_size);
976         /* send virtchnnl ops to configure rss*/
977         ret = iavf_configure_rss_lut(adapter);
978         if (ret) /* revert back */
979                 rte_memcpy(vf->rss_lut, lut, reta_size);
980         rte_free(lut);
981
982         return ret;
983 }
984
985 static int
986 iavf_dev_rss_reta_query(struct rte_eth_dev *dev,
987                        struct rte_eth_rss_reta_entry64 *reta_conf,
988                        uint16_t reta_size)
989 {
990         struct iavf_adapter *adapter =
991                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
992         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
993         uint16_t i, idx, shift;
994
995         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
996                 return -ENOTSUP;
997
998         if (reta_size != vf->vf_res->rss_lut_size) {
999                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
1000                         "(%d) doesn't match the number of hardware can "
1001                         "support (%d)", reta_size, vf->vf_res->rss_lut_size);
1002                 return -EINVAL;
1003         }
1004
1005         for (i = 0; i < reta_size; i++) {
1006                 idx = i / RTE_RETA_GROUP_SIZE;
1007                 shift = i % RTE_RETA_GROUP_SIZE;
1008                 if (reta_conf[idx].mask & (1ULL << shift))
1009                         reta_conf[idx].reta[shift] = vf->rss_lut[i];
1010         }
1011
1012         return 0;
1013 }
1014
1015 static int
1016 iavf_dev_rss_hash_update(struct rte_eth_dev *dev,
1017                         struct rte_eth_rss_conf *rss_conf)
1018 {
1019         struct iavf_adapter *adapter =
1020                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1021         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1022
1023         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1024                 return -ENOTSUP;
1025
1026         /* HENA setting, it is enabled by default, no change */
1027         if (!rss_conf->rss_key || rss_conf->rss_key_len == 0) {
1028                 PMD_DRV_LOG(DEBUG, "No key to be configured");
1029                 return 0;
1030         } else if (rss_conf->rss_key_len != vf->vf_res->rss_key_size) {
1031                 PMD_DRV_LOG(ERR, "The size of hash key configured "
1032                         "(%d) doesn't match the size of hardware can "
1033                         "support (%d)", rss_conf->rss_key_len,
1034                         vf->vf_res->rss_key_size);
1035                 return -EINVAL;
1036         }
1037
1038         rte_memcpy(vf->rss_key, rss_conf->rss_key, rss_conf->rss_key_len);
1039
1040         return iavf_configure_rss_key(adapter);
1041 }
1042
1043 static int
1044 iavf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
1045                           struct rte_eth_rss_conf *rss_conf)
1046 {
1047         struct iavf_adapter *adapter =
1048                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1049         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1050
1051         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1052                 return -ENOTSUP;
1053
1054          /* Just set it to default value now. */
1055         rss_conf->rss_hf = IAVF_RSS_OFFLOAD_ALL;
1056
1057         if (!rss_conf->rss_key)
1058                 return 0;
1059
1060         rss_conf->rss_key_len = vf->vf_res->rss_key_size;
1061         rte_memcpy(rss_conf->rss_key, vf->rss_key, rss_conf->rss_key_len);
1062
1063         return 0;
1064 }
1065
1066 static int
1067 iavf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1068 {
1069         uint32_t frame_size = mtu + IAVF_ETH_OVERHEAD;
1070         int ret = 0;
1071
1072         if (mtu < RTE_ETHER_MIN_MTU || frame_size > IAVF_FRAME_SIZE_MAX)
1073                 return -EINVAL;
1074
1075         /* mtu setting is forbidden if port is start */
1076         if (dev->data->dev_started) {
1077                 PMD_DRV_LOG(ERR, "port must be stopped before configuration");
1078                 return -EBUSY;
1079         }
1080
1081         if (frame_size > RTE_ETHER_MAX_LEN)
1082                 dev->data->dev_conf.rxmode.offloads |=
1083                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
1084         else
1085                 dev->data->dev_conf.rxmode.offloads &=
1086                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
1087
1088         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
1089
1090         return ret;
1091 }
1092
1093 static int
1094 iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
1095                              struct rte_ether_addr *mac_addr)
1096 {
1097         struct iavf_adapter *adapter =
1098                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1099         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1100         struct rte_ether_addr *perm_addr, *old_addr;
1101         int ret;
1102
1103         old_addr = (struct rte_ether_addr *)hw->mac.addr;
1104         perm_addr = (struct rte_ether_addr *)hw->mac.perm_addr;
1105
1106         /* If the MAC address is configured by host, skip the setting */
1107         if (rte_is_valid_assigned_ether_addr(perm_addr))
1108                 return -EPERM;
1109
1110         ret = iavf_add_del_eth_addr(adapter, old_addr, false);
1111         if (ret)
1112                 PMD_DRV_LOG(ERR, "Fail to delete old MAC:"
1113                             " %02X:%02X:%02X:%02X:%02X:%02X",
1114                             old_addr->addr_bytes[0],
1115                             old_addr->addr_bytes[1],
1116                             old_addr->addr_bytes[2],
1117                             old_addr->addr_bytes[3],
1118                             old_addr->addr_bytes[4],
1119                             old_addr->addr_bytes[5]);
1120
1121         ret = iavf_add_del_eth_addr(adapter, mac_addr, true);
1122         if (ret)
1123                 PMD_DRV_LOG(ERR, "Fail to add new MAC:"
1124                             " %02X:%02X:%02X:%02X:%02X:%02X",
1125                             mac_addr->addr_bytes[0],
1126                             mac_addr->addr_bytes[1],
1127                             mac_addr->addr_bytes[2],
1128                             mac_addr->addr_bytes[3],
1129                             mac_addr->addr_bytes[4],
1130                             mac_addr->addr_bytes[5]);
1131
1132         if (ret)
1133                 return -EIO;
1134
1135         rte_ether_addr_copy(mac_addr, (struct rte_ether_addr *)hw->mac.addr);
1136         return 0;
1137 }
1138
1139 static void
1140 iavf_stat_update_48(uint64_t *offset, uint64_t *stat)
1141 {
1142         if (*stat >= *offset)
1143                 *stat = *stat - *offset;
1144         else
1145                 *stat = (uint64_t)((*stat +
1146                         ((uint64_t)1 << IAVF_48_BIT_WIDTH)) - *offset);
1147
1148         *stat &= IAVF_48_BIT_MASK;
1149 }
1150
1151 static void
1152 iavf_stat_update_32(uint64_t *offset, uint64_t *stat)
1153 {
1154         if (*stat >= *offset)
1155                 *stat = (uint64_t)(*stat - *offset);
1156         else
1157                 *stat = (uint64_t)((*stat +
1158                         ((uint64_t)1 << IAVF_32_BIT_WIDTH)) - *offset);
1159 }
1160
1161 static void
1162 iavf_update_stats(struct iavf_vsi *vsi, struct virtchnl_eth_stats *nes)
1163 {
1164         struct virtchnl_eth_stats *oes = &vsi->eth_stats_offset;
1165
1166         iavf_stat_update_48(&oes->rx_bytes, &nes->rx_bytes);
1167         iavf_stat_update_48(&oes->rx_unicast, &nes->rx_unicast);
1168         iavf_stat_update_48(&oes->rx_multicast, &nes->rx_multicast);
1169         iavf_stat_update_48(&oes->rx_broadcast, &nes->rx_broadcast);
1170         iavf_stat_update_32(&oes->rx_discards, &nes->rx_discards);
1171         iavf_stat_update_48(&oes->tx_bytes, &nes->tx_bytes);
1172         iavf_stat_update_48(&oes->tx_unicast, &nes->tx_unicast);
1173         iavf_stat_update_48(&oes->tx_multicast, &nes->tx_multicast);
1174         iavf_stat_update_48(&oes->tx_broadcast, &nes->tx_broadcast);
1175         iavf_stat_update_32(&oes->tx_errors, &nes->tx_errors);
1176         iavf_stat_update_32(&oes->tx_discards, &nes->tx_discards);
1177 }
1178
1179 static int
1180 iavf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1181 {
1182         struct iavf_adapter *adapter =
1183                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1184         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1185         struct iavf_vsi *vsi = &vf->vsi;
1186         struct virtchnl_eth_stats *pstats = NULL;
1187         int ret;
1188
1189         ret = iavf_query_stats(adapter, &pstats);
1190         if (ret == 0) {
1191                 iavf_update_stats(vsi, pstats);
1192                 stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
1193                                 pstats->rx_broadcast - pstats->rx_discards;
1194                 stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
1195                                                 pstats->tx_unicast;
1196                 stats->imissed = pstats->rx_discards;
1197                 stats->oerrors = pstats->tx_errors + pstats->tx_discards;
1198                 stats->ibytes = pstats->rx_bytes;
1199                 stats->ibytes -= stats->ipackets * RTE_ETHER_CRC_LEN;
1200                 stats->obytes = pstats->tx_bytes;
1201         } else {
1202                 PMD_DRV_LOG(ERR, "Get statistics failed");
1203         }
1204         return ret;
1205 }
1206
1207 static int
1208 iavf_dev_stats_reset(struct rte_eth_dev *dev)
1209 {
1210         int ret;
1211         struct iavf_adapter *adapter =
1212                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1213         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1214         struct iavf_vsi *vsi = &vf->vsi;
1215         struct virtchnl_eth_stats *pstats = NULL;
1216
1217         /* read stat values to clear hardware registers */
1218         ret = iavf_query_stats(adapter, &pstats);
1219         if (ret != 0)
1220                 return ret;
1221
1222         /* set stats offset base on current values */
1223         vsi->eth_stats_offset = *pstats;
1224
1225         return 0;
1226 }
1227
1228 static int iavf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
1229                                       struct rte_eth_xstat_name *xstats_names,
1230                                       __rte_unused unsigned int limit)
1231 {
1232         unsigned int i;
1233
1234         if (xstats_names != NULL)
1235                 for (i = 0; i < IAVF_NB_XSTATS; i++) {
1236                         snprintf(xstats_names[i].name,
1237                                 sizeof(xstats_names[i].name),
1238                                 "%s", rte_iavf_stats_strings[i].name);
1239                 }
1240         return IAVF_NB_XSTATS;
1241 }
1242
1243 static int iavf_dev_xstats_get(struct rte_eth_dev *dev,
1244                                  struct rte_eth_xstat *xstats, unsigned int n)
1245 {
1246         int ret;
1247         unsigned int i;
1248         struct iavf_adapter *adapter =
1249                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1250         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1251         struct iavf_vsi *vsi = &vf->vsi;
1252         struct virtchnl_eth_stats *pstats = NULL;
1253
1254         if (n < IAVF_NB_XSTATS)
1255                 return IAVF_NB_XSTATS;
1256
1257         ret = iavf_query_stats(adapter, &pstats);
1258         if (ret != 0)
1259                 return 0;
1260
1261         if (!xstats)
1262                 return 0;
1263
1264         iavf_update_stats(vsi, pstats);
1265
1266         /* loop over xstats array and values from pstats */
1267         for (i = 0; i < IAVF_NB_XSTATS; i++) {
1268                 xstats[i].id = i;
1269                 xstats[i].value = *(uint64_t *)(((char *)pstats) +
1270                         rte_iavf_stats_strings[i].offset);
1271         }
1272
1273         return IAVF_NB_XSTATS;
1274 }
1275
1276
1277 static int
1278 iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1279 {
1280         struct iavf_adapter *adapter =
1281                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1282         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1283         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1284         uint16_t msix_intr;
1285
1286         msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1287         if (msix_intr == IAVF_MISC_VEC_ID) {
1288                 PMD_DRV_LOG(INFO, "MISC is also enabled for control");
1289                 IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1290                                IAVF_VFINT_DYN_CTL01_INTENA_MASK |
1291                                IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1292                                IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1293         } else {
1294                 IAVF_WRITE_REG(hw,
1295                                IAVF_VFINT_DYN_CTLN1
1296                                 (msix_intr - IAVF_RX_VEC_START),
1297                                IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1298                                IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1299                                IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
1300         }
1301
1302         IAVF_WRITE_FLUSH(hw);
1303
1304         rte_intr_ack(&pci_dev->intr_handle);
1305
1306         return 0;
1307 }
1308
1309 static int
1310 iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1311 {
1312         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1313         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1314         uint16_t msix_intr;
1315
1316         msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1317         if (msix_intr == IAVF_MISC_VEC_ID) {
1318                 PMD_DRV_LOG(ERR, "MISC is used for control, cannot disable it");
1319                 return -EIO;
1320         }
1321
1322         IAVF_WRITE_REG(hw,
1323                       IAVF_VFINT_DYN_CTLN1(msix_intr - IAVF_RX_VEC_START),
1324                       0);
1325
1326         IAVF_WRITE_FLUSH(hw);
1327         return 0;
1328 }
1329
1330 static int
1331 iavf_check_vf_reset_done(struct iavf_hw *hw)
1332 {
1333         int i, reset;
1334
1335         for (i = 0; i < IAVF_RESET_WAIT_CNT; i++) {
1336                 reset = IAVF_READ_REG(hw, IAVF_VFGEN_RSTAT) &
1337                         IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1338                 reset = reset >> IAVF_VFGEN_RSTAT_VFR_STATE_SHIFT;
1339                 if (reset == VIRTCHNL_VFR_VFACTIVE ||
1340                     reset == VIRTCHNL_VFR_COMPLETED)
1341                         break;
1342                 rte_delay_ms(20);
1343         }
1344
1345         if (i >= IAVF_RESET_WAIT_CNT)
1346                 return -1;
1347
1348         return 0;
1349 }
1350
1351 static int
1352 iavf_init_vf(struct rte_eth_dev *dev)
1353 {
1354         int err, bufsz;
1355         struct iavf_adapter *adapter =
1356                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1357         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1358         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1359
1360         err = iavf_set_mac_type(hw);
1361         if (err) {
1362                 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
1363                 goto err;
1364         }
1365
1366         err = iavf_check_vf_reset_done(hw);
1367         if (err) {
1368                 PMD_INIT_LOG(ERR, "VF is still resetting");
1369                 goto err;
1370         }
1371
1372         iavf_init_adminq_parameter(hw);
1373         err = iavf_init_adminq(hw);
1374         if (err) {
1375                 PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
1376                 goto err;
1377         }
1378
1379         vf->aq_resp = rte_zmalloc("vf_aq_resp", IAVF_AQ_BUF_SZ, 0);
1380         if (!vf->aq_resp) {
1381                 PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
1382                 goto err_aq;
1383         }
1384         if (iavf_check_api_version(adapter) != 0) {
1385                 PMD_INIT_LOG(ERR, "check_api version failed");
1386                 goto err_api;
1387         }
1388
1389         bufsz = sizeof(struct virtchnl_vf_resource) +
1390                 (IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
1391         vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
1392         if (!vf->vf_res) {
1393                 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
1394                 goto err_api;
1395         }
1396         if (iavf_get_vf_resource(adapter) != 0) {
1397                 PMD_INIT_LOG(ERR, "iavf_get_vf_config failed");
1398                 goto err_alloc;
1399         }
1400         /* Allocate memort for RSS info */
1401         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1402                 vf->rss_key = rte_zmalloc("rss_key",
1403                                           vf->vf_res->rss_key_size, 0);
1404                 if (!vf->rss_key) {
1405                         PMD_INIT_LOG(ERR, "unable to allocate rss_key memory");
1406                         goto err_rss;
1407                 }
1408                 vf->rss_lut = rte_zmalloc("rss_lut",
1409                                           vf->vf_res->rss_lut_size, 0);
1410                 if (!vf->rss_lut) {
1411                         PMD_INIT_LOG(ERR, "unable to allocate rss_lut memory");
1412                         goto err_rss;
1413                 }
1414         }
1415
1416         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
1417                 if (iavf_get_supported_rxdid(adapter) != 0) {
1418                         PMD_INIT_LOG(ERR, "failed to do get supported rxdid");
1419                         goto err_rss;
1420                 }
1421         }
1422
1423         return 0;
1424 err_rss:
1425         rte_free(vf->rss_key);
1426         rte_free(vf->rss_lut);
1427 err_alloc:
1428         rte_free(vf->vf_res);
1429         vf->vsi_res = NULL;
1430 err_api:
1431         rte_free(vf->aq_resp);
1432 err_aq:
1433         iavf_shutdown_adminq(hw);
1434 err:
1435         return -1;
1436 }
1437
1438 /* Enable default admin queue interrupt setting */
1439 static inline void
1440 iavf_enable_irq0(struct iavf_hw *hw)
1441 {
1442         /* Enable admin queue interrupt trigger */
1443         IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1,
1444                        IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
1445
1446         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1447                        IAVF_VFINT_DYN_CTL01_INTENA_MASK |
1448                        IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1449                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1450
1451         IAVF_WRITE_FLUSH(hw);
1452 }
1453
1454 static inline void
1455 iavf_disable_irq0(struct iavf_hw *hw)
1456 {
1457         /* Disable all interrupt types */
1458         IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1, 0);
1459         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1460                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1461         IAVF_WRITE_FLUSH(hw);
1462 }
1463
1464 static void
1465 iavf_dev_interrupt_handler(void *param)
1466 {
1467         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1468         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1469
1470         iavf_disable_irq0(hw);
1471
1472         iavf_handle_virtchnl_msg(dev);
1473
1474         iavf_enable_irq0(hw);
1475 }
1476
1477 static int
1478 iavf_dev_filter_ctrl(struct rte_eth_dev *dev,
1479                      enum rte_filter_type filter_type,
1480                      enum rte_filter_op filter_op,
1481                      void *arg)
1482 {
1483         int ret = 0;
1484
1485         if (!dev)
1486                 return -EINVAL;
1487
1488         switch (filter_type) {
1489         case RTE_ETH_FILTER_GENERIC:
1490                 if (filter_op != RTE_ETH_FILTER_GET)
1491                         return -EINVAL;
1492                 *(const void **)arg = &iavf_flow_ops;
1493                 break;
1494         default:
1495                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
1496                             filter_type);
1497                 ret = -EINVAL;
1498                 break;
1499         }
1500
1501         return ret;
1502 }
1503
1504
1505 static int
1506 iavf_dev_init(struct rte_eth_dev *eth_dev)
1507 {
1508         struct iavf_adapter *adapter =
1509                 IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1510         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1511         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1512         int ret = 0;
1513
1514         PMD_INIT_FUNC_TRACE();
1515
1516         /* assign ops func pointer */
1517         eth_dev->dev_ops = &iavf_eth_dev_ops;
1518         eth_dev->rx_queue_count = iavf_dev_rxq_count;
1519         eth_dev->rx_descriptor_status = iavf_dev_rx_desc_status;
1520         eth_dev->tx_descriptor_status = iavf_dev_tx_desc_status;
1521         eth_dev->rx_pkt_burst = &iavf_recv_pkts;
1522         eth_dev->tx_pkt_burst = &iavf_xmit_pkts;
1523         eth_dev->tx_pkt_prepare = &iavf_prep_pkts;
1524
1525         /* For secondary processes, we don't initialise any further as primary
1526          * has already done this work. Only check if we need a different RX
1527          * and TX function.
1528          */
1529         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1530                 iavf_set_rx_function(eth_dev);
1531                 iavf_set_tx_function(eth_dev);
1532                 return 0;
1533         }
1534         rte_eth_copy_pci_info(eth_dev, pci_dev);
1535         eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1536
1537         hw->vendor_id = pci_dev->id.vendor_id;
1538         hw->device_id = pci_dev->id.device_id;
1539         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1540         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1541         hw->bus.bus_id = pci_dev->addr.bus;
1542         hw->bus.device = pci_dev->addr.devid;
1543         hw->bus.func = pci_dev->addr.function;
1544         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1545         hw->back = IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1546         adapter->eth_dev = eth_dev;
1547         adapter->stopped = 1;
1548
1549         if (iavf_init_vf(eth_dev) != 0) {
1550                 PMD_INIT_LOG(ERR, "Init vf failed");
1551                 return -1;
1552         }
1553
1554         /* set default ptype table */
1555         adapter->ptype_tbl = iavf_get_default_ptype_table();
1556
1557         /* copy mac addr */
1558         eth_dev->data->mac_addrs = rte_zmalloc(
1559                 "iavf_mac", RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX, 0);
1560         if (!eth_dev->data->mac_addrs) {
1561                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
1562                              " store MAC addresses",
1563                              RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX);
1564                 return -ENOMEM;
1565         }
1566         /* If the MAC address is not configured by host,
1567          * generate a random one.
1568          */
1569         if (!rte_is_valid_assigned_ether_addr(
1570                         (struct rte_ether_addr *)hw->mac.addr))
1571                 rte_eth_random_addr(hw->mac.addr);
1572         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.addr,
1573                         &eth_dev->data->mac_addrs[0]);
1574
1575         /* register callback func to eal lib */
1576         rte_intr_callback_register(&pci_dev->intr_handle,
1577                                    iavf_dev_interrupt_handler,
1578                                    (void *)eth_dev);
1579
1580         /* enable uio intr after callback register */
1581         rte_intr_enable(&pci_dev->intr_handle);
1582
1583         /* configure and enable device interrupt */
1584         iavf_enable_irq0(hw);
1585
1586         ret = iavf_flow_init(adapter);
1587         if (ret) {
1588                 PMD_INIT_LOG(ERR, "Failed to initialize flow");
1589                 return ret;
1590         }
1591
1592         return 0;
1593 }
1594
1595 static int
1596 iavf_dev_close(struct rte_eth_dev *dev)
1597 {
1598         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1599         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1600         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1601         struct iavf_adapter *adapter =
1602                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1603         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1604         int ret;
1605
1606         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1607                 return 0;
1608
1609         ret = iavf_dev_stop(dev);
1610
1611         iavf_flow_flush(dev, NULL);
1612         iavf_flow_uninit(adapter);
1613
1614         /*
1615          * disable promiscuous mode before reset vf
1616          * it is a workaround solution when work with kernel driver
1617          * and it is not the normal way
1618          */
1619         if (vf->promisc_unicast_enabled || vf->promisc_multicast_enabled)
1620                 iavf_config_promisc(adapter, false, false);
1621
1622         iavf_shutdown_adminq(hw);
1623         /* disable uio intr before callback unregister */
1624         rte_intr_disable(intr_handle);
1625
1626         /* unregister callback func from eal lib */
1627         rte_intr_callback_unregister(intr_handle,
1628                                      iavf_dev_interrupt_handler, dev);
1629         iavf_disable_irq0(hw);
1630
1631         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1632                 if (vf->rss_lut) {
1633                         rte_free(vf->rss_lut);
1634                         vf->rss_lut = NULL;
1635                 }
1636                 if (vf->rss_key) {
1637                         rte_free(vf->rss_key);
1638                         vf->rss_key = NULL;
1639                 }
1640         }
1641
1642         rte_free(vf->vf_res);
1643         vf->vsi_res = NULL;
1644         vf->vf_res = NULL;
1645
1646         rte_free(vf->aq_resp);
1647         vf->aq_resp = NULL;
1648
1649         vf->vf_reset = false;
1650
1651         return ret;
1652 }
1653
1654 static int
1655 iavf_dev_uninit(struct rte_eth_dev *dev)
1656 {
1657         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1658                 return -EPERM;
1659
1660         iavf_dev_close(dev);
1661
1662         return 0;
1663 }
1664
1665 /*
1666  * Reset VF device only to re-initialize resources in PMD layer
1667  */
1668 static int
1669 iavf_dev_reset(struct rte_eth_dev *dev)
1670 {
1671         int ret;
1672
1673         ret = iavf_dev_uninit(dev);
1674         if (ret)
1675                 return ret;
1676
1677         return iavf_dev_init(dev);
1678 }
1679
1680 static int
1681 iavf_dcf_cap_check_handler(__rte_unused const char *key,
1682                            const char *value, __rte_unused void *opaque)
1683 {
1684         if (strcmp(value, "dcf"))
1685                 return -1;
1686
1687         return 0;
1688 }
1689
1690 static int
1691 iavf_dcf_cap_selected(struct rte_devargs *devargs)
1692 {
1693         struct rte_kvargs *kvlist;
1694         const char *key = "cap";
1695         int ret = 0;
1696
1697         if (devargs == NULL)
1698                 return 0;
1699
1700         kvlist = rte_kvargs_parse(devargs->args, NULL);
1701         if (kvlist == NULL)
1702                 return 0;
1703
1704         if (!rte_kvargs_count(kvlist, key))
1705                 goto exit;
1706
1707         /* dcf capability selected when there's a key-value pair: cap=dcf */
1708         if (rte_kvargs_process(kvlist, key,
1709                                iavf_dcf_cap_check_handler, NULL) < 0)
1710                 goto exit;
1711
1712         ret = 1;
1713
1714 exit:
1715         rte_kvargs_free(kvlist);
1716         return ret;
1717 }
1718
1719 static int eth_iavf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1720                              struct rte_pci_device *pci_dev)
1721 {
1722         if (iavf_dcf_cap_selected(pci_dev->device.devargs))
1723                 return 1;
1724
1725         return rte_eth_dev_pci_generic_probe(pci_dev,
1726                 sizeof(struct iavf_adapter), iavf_dev_init);
1727 }
1728
1729 static int eth_iavf_pci_remove(struct rte_pci_device *pci_dev)
1730 {
1731         return rte_eth_dev_pci_generic_remove(pci_dev, iavf_dev_uninit);
1732 }
1733
1734 /* Adaptive virtual function driver struct */
1735 static struct rte_pci_driver rte_iavf_pmd = {
1736         .id_table = pci_id_iavf_map,
1737         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1738         .probe = eth_iavf_pci_probe,
1739         .remove = eth_iavf_pci_remove,
1740 };
1741
1742 RTE_PMD_REGISTER_PCI(net_iavf, rte_iavf_pmd);
1743 RTE_PMD_REGISTER_PCI_TABLE(net_iavf, pci_id_iavf_map);
1744 RTE_PMD_REGISTER_KMOD_DEP(net_iavf, "* igb_uio | vfio-pci");
1745 RTE_PMD_REGISTER_PARAM_STRING(net_iavf, "cap=dcf");
1746 RTE_LOG_REGISTER(iavf_logtype_init, pmd.net.iavf.init, NOTICE);
1747 RTE_LOG_REGISTER(iavf_logtype_driver, pmd.net.iavf.driver, NOTICE);
1748 #ifdef RTE_LIBRTE_IAVF_DEBUG_RX
1749 RTE_LOG_REGISTER(iavf_logtype_rx, pmd.net.iavf.rx, DEBUG);
1750 #endif
1751 #ifdef RTE_LIBRTE_IAVF_DEBUG_TX
1752 RTE_LOG_REGISTER(iavf_logtype_tx, pmd.net.iavf.tx, DEBUG);
1753 #endif
1754 #ifdef RTE_LIBRTE_IAVF_DEBUG_TX_FREE
1755 RTE_LOG_REGISTER(iavf_logtype_tx_free, pmd.net.iavf.tx_free, DEBUG);
1756 #endif