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