net/ice/base: support extended GPIO access
[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                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
754
755         dev_info->default_rxconf = (struct rte_eth_rxconf) {
756                 .rx_free_thresh = IAVF_DEFAULT_RX_FREE_THRESH,
757                 .rx_drop_en = 0,
758                 .offloads = 0,
759         };
760
761         dev_info->default_txconf = (struct rte_eth_txconf) {
762                 .tx_free_thresh = IAVF_DEFAULT_TX_FREE_THRESH,
763                 .tx_rs_thresh = IAVF_DEFAULT_TX_RS_THRESH,
764                 .offloads = 0,
765         };
766
767         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
768                 .nb_max = IAVF_MAX_RING_DESC,
769                 .nb_min = IAVF_MIN_RING_DESC,
770                 .nb_align = IAVF_ALIGN_RING_DESC,
771         };
772
773         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
774                 .nb_max = IAVF_MAX_RING_DESC,
775                 .nb_min = IAVF_MIN_RING_DESC,
776                 .nb_align = IAVF_ALIGN_RING_DESC,
777         };
778
779         return 0;
780 }
781
782 static const uint32_t *
783 iavf_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
784 {
785         static const uint32_t ptypes[] = {
786                 RTE_PTYPE_L2_ETHER,
787                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
788                 RTE_PTYPE_L4_FRAG,
789                 RTE_PTYPE_L4_ICMP,
790                 RTE_PTYPE_L4_NONFRAG,
791                 RTE_PTYPE_L4_SCTP,
792                 RTE_PTYPE_L4_TCP,
793                 RTE_PTYPE_L4_UDP,
794                 RTE_PTYPE_UNKNOWN
795         };
796         return ptypes;
797 }
798
799 int
800 iavf_dev_link_update(struct rte_eth_dev *dev,
801                     __rte_unused int wait_to_complete)
802 {
803         struct rte_eth_link new_link;
804         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
805
806         memset(&new_link, 0, sizeof(new_link));
807
808         /* Only read status info stored in VF, and the info is updated
809          *  when receive LINK_CHANGE evnet from PF by Virtchnnl.
810          */
811         switch (vf->link_speed) {
812         case 10:
813                 new_link.link_speed = ETH_SPEED_NUM_10M;
814                 break;
815         case 100:
816                 new_link.link_speed = ETH_SPEED_NUM_100M;
817                 break;
818         case 1000:
819                 new_link.link_speed = ETH_SPEED_NUM_1G;
820                 break;
821         case 10000:
822                 new_link.link_speed = ETH_SPEED_NUM_10G;
823                 break;
824         case 20000:
825                 new_link.link_speed = ETH_SPEED_NUM_20G;
826                 break;
827         case 25000:
828                 new_link.link_speed = ETH_SPEED_NUM_25G;
829                 break;
830         case 40000:
831                 new_link.link_speed = ETH_SPEED_NUM_40G;
832                 break;
833         case 50000:
834                 new_link.link_speed = ETH_SPEED_NUM_50G;
835                 break;
836         case 100000:
837                 new_link.link_speed = ETH_SPEED_NUM_100G;
838                 break;
839         default:
840                 new_link.link_speed = ETH_SPEED_NUM_NONE;
841                 break;
842         }
843
844         new_link.link_duplex = ETH_LINK_FULL_DUPLEX;
845         new_link.link_status = vf->link_up ? ETH_LINK_UP :
846                                              ETH_LINK_DOWN;
847         new_link.link_autoneg = !(dev->data->dev_conf.link_speeds &
848                                 ETH_LINK_SPEED_FIXED);
849
850         return rte_eth_linkstatus_set(dev, &new_link);
851 }
852
853 static int
854 iavf_dev_promiscuous_enable(struct rte_eth_dev *dev)
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
860         return iavf_config_promisc(adapter,
861                                   true, vf->promisc_multicast_enabled);
862 }
863
864 static int
865 iavf_dev_promiscuous_disable(struct rte_eth_dev *dev)
866 {
867         struct iavf_adapter *adapter =
868                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
869         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
870
871         return iavf_config_promisc(adapter,
872                                   false, vf->promisc_multicast_enabled);
873 }
874
875 static int
876 iavf_dev_allmulticast_enable(struct rte_eth_dev *dev)
877 {
878         struct iavf_adapter *adapter =
879                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
880         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
881
882         return iavf_config_promisc(adapter,
883                                   vf->promisc_unicast_enabled, true);
884 }
885
886 static int
887 iavf_dev_allmulticast_disable(struct rte_eth_dev *dev)
888 {
889         struct iavf_adapter *adapter =
890                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
891         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
892
893         return iavf_config_promisc(adapter,
894                                   vf->promisc_unicast_enabled, false);
895 }
896
897 static int
898 iavf_dev_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *addr,
899                      __rte_unused uint32_t index,
900                      __rte_unused uint32_t pool)
901 {
902         struct iavf_adapter *adapter =
903                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
904         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
905         int err;
906
907         if (rte_is_zero_ether_addr(addr)) {
908                 PMD_DRV_LOG(ERR, "Invalid Ethernet Address");
909                 return -EINVAL;
910         }
911
912         err = iavf_add_del_eth_addr(adapter, addr, true);
913         if (err) {
914                 PMD_DRV_LOG(ERR, "fail to add MAC address");
915                 return -EIO;
916         }
917
918         vf->mac_num++;
919
920         return 0;
921 }
922
923 static void
924 iavf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
925 {
926         struct iavf_adapter *adapter =
927                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
928         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
929         struct rte_ether_addr *addr;
930         int err;
931
932         addr = &dev->data->mac_addrs[index];
933
934         err = iavf_add_del_eth_addr(adapter, addr, false);
935         if (err)
936                 PMD_DRV_LOG(ERR, "fail to delete MAC address");
937
938         vf->mac_num--;
939 }
940
941 static int
942 iavf_dev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
943 {
944         struct iavf_adapter *adapter =
945                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
946         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
947         int err;
948
949         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
950                 return -ENOTSUP;
951
952         err = iavf_add_del_vlan(adapter, vlan_id, on);
953         if (err)
954                 return -EIO;
955         return 0;
956 }
957
958 static int
959 iavf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
960 {
961         struct iavf_adapter *adapter =
962                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
963         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
964         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
965         int err;
966
967         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
968                 return -ENOTSUP;
969
970         /* Vlan stripping setting */
971         if (mask & ETH_VLAN_STRIP_MASK) {
972                 /* Enable or disable VLAN stripping */
973                 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
974                         err = iavf_enable_vlan_strip(adapter);
975                 else
976                         err = iavf_disable_vlan_strip(adapter);
977
978                 if (err)
979                         return -EIO;
980         }
981         return 0;
982 }
983
984 static int
985 iavf_dev_rss_reta_update(struct rte_eth_dev *dev,
986                         struct rte_eth_rss_reta_entry64 *reta_conf,
987                         uint16_t reta_size)
988 {
989         struct iavf_adapter *adapter =
990                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
991         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
992         uint8_t *lut;
993         uint16_t i, idx, shift;
994         int ret;
995
996         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
997                 return -ENOTSUP;
998
999         if (reta_size != vf->vf_res->rss_lut_size) {
1000                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
1001                         "(%d) doesn't match the number of hardware can "
1002                         "support (%d)", reta_size, vf->vf_res->rss_lut_size);
1003                 return -EINVAL;
1004         }
1005
1006         lut = rte_zmalloc("rss_lut", reta_size, 0);
1007         if (!lut) {
1008                 PMD_DRV_LOG(ERR, "No memory can be allocated");
1009                 return -ENOMEM;
1010         }
1011         /* store the old lut table temporarily */
1012         rte_memcpy(lut, vf->rss_lut, reta_size);
1013
1014         for (i = 0; i < reta_size; i++) {
1015                 idx = i / RTE_RETA_GROUP_SIZE;
1016                 shift = i % RTE_RETA_GROUP_SIZE;
1017                 if (reta_conf[idx].mask & (1ULL << shift))
1018                         lut[i] = reta_conf[idx].reta[shift];
1019         }
1020
1021         rte_memcpy(vf->rss_lut, lut, reta_size);
1022         /* send virtchnnl ops to configure rss*/
1023         ret = iavf_configure_rss_lut(adapter);
1024         if (ret) /* revert back */
1025                 rte_memcpy(vf->rss_lut, lut, reta_size);
1026         rte_free(lut);
1027
1028         return ret;
1029 }
1030
1031 static int
1032 iavf_dev_rss_reta_query(struct rte_eth_dev *dev,
1033                        struct rte_eth_rss_reta_entry64 *reta_conf,
1034                        uint16_t reta_size)
1035 {
1036         struct iavf_adapter *adapter =
1037                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1038         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1039         uint16_t i, idx, shift;
1040
1041         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1042                 return -ENOTSUP;
1043
1044         if (reta_size != vf->vf_res->rss_lut_size) {
1045                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
1046                         "(%d) doesn't match the number of hardware can "
1047                         "support (%d)", reta_size, vf->vf_res->rss_lut_size);
1048                 return -EINVAL;
1049         }
1050
1051         for (i = 0; i < reta_size; i++) {
1052                 idx = i / RTE_RETA_GROUP_SIZE;
1053                 shift = i % RTE_RETA_GROUP_SIZE;
1054                 if (reta_conf[idx].mask & (1ULL << shift))
1055                         reta_conf[idx].reta[shift] = vf->rss_lut[i];
1056         }
1057
1058         return 0;
1059 }
1060
1061 static int
1062 iavf_dev_rss_hash_update(struct rte_eth_dev *dev,
1063                         struct rte_eth_rss_conf *rss_conf)
1064 {
1065         struct iavf_adapter *adapter =
1066                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1067         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1068
1069         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1070                 return -ENOTSUP;
1071
1072         /* HENA setting, it is enabled by default, no change */
1073         if (!rss_conf->rss_key || rss_conf->rss_key_len == 0) {
1074                 PMD_DRV_LOG(DEBUG, "No key to be configured");
1075                 return 0;
1076         } else if (rss_conf->rss_key_len != vf->vf_res->rss_key_size) {
1077                 PMD_DRV_LOG(ERR, "The size of hash key configured "
1078                         "(%d) doesn't match the size of hardware can "
1079                         "support (%d)", rss_conf->rss_key_len,
1080                         vf->vf_res->rss_key_size);
1081                 return -EINVAL;
1082         }
1083
1084         rte_memcpy(vf->rss_key, rss_conf->rss_key, rss_conf->rss_key_len);
1085
1086         return iavf_configure_rss_key(adapter);
1087 }
1088
1089 static int
1090 iavf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
1091                           struct rte_eth_rss_conf *rss_conf)
1092 {
1093         struct iavf_adapter *adapter =
1094                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1095         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1096
1097         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
1098                 return -ENOTSUP;
1099
1100          /* Just set it to default value now. */
1101         rss_conf->rss_hf = IAVF_RSS_OFFLOAD_ALL;
1102
1103         if (!rss_conf->rss_key)
1104                 return 0;
1105
1106         rss_conf->rss_key_len = vf->vf_res->rss_key_size;
1107         rte_memcpy(rss_conf->rss_key, vf->rss_key, rss_conf->rss_key_len);
1108
1109         return 0;
1110 }
1111
1112 static int
1113 iavf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1114 {
1115         uint32_t frame_size = mtu + IAVF_ETH_OVERHEAD;
1116         int ret = 0;
1117
1118         if (mtu < RTE_ETHER_MIN_MTU || frame_size > IAVF_FRAME_SIZE_MAX)
1119                 return -EINVAL;
1120
1121         /* mtu setting is forbidden if port is start */
1122         if (dev->data->dev_started) {
1123                 PMD_DRV_LOG(ERR, "port must be stopped before configuration");
1124                 return -EBUSY;
1125         }
1126
1127         if (frame_size > RTE_ETHER_MAX_LEN)
1128                 dev->data->dev_conf.rxmode.offloads |=
1129                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
1130         else
1131                 dev->data->dev_conf.rxmode.offloads &=
1132                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
1133
1134         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
1135
1136         return ret;
1137 }
1138
1139 static int
1140 iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
1141                              struct rte_ether_addr *mac_addr)
1142 {
1143         struct iavf_adapter *adapter =
1144                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1145         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1146         struct rte_ether_addr *perm_addr, *old_addr;
1147         int ret;
1148
1149         old_addr = (struct rte_ether_addr *)hw->mac.addr;
1150         perm_addr = (struct rte_ether_addr *)hw->mac.perm_addr;
1151
1152         /* If the MAC address is configured by host, skip the setting */
1153         if (rte_is_valid_assigned_ether_addr(perm_addr))
1154                 return -EPERM;
1155
1156         ret = iavf_add_del_eth_addr(adapter, old_addr, false);
1157         if (ret)
1158                 PMD_DRV_LOG(ERR, "Fail to delete old MAC:"
1159                             " %02X:%02X:%02X:%02X:%02X:%02X",
1160                             old_addr->addr_bytes[0],
1161                             old_addr->addr_bytes[1],
1162                             old_addr->addr_bytes[2],
1163                             old_addr->addr_bytes[3],
1164                             old_addr->addr_bytes[4],
1165                             old_addr->addr_bytes[5]);
1166
1167         ret = iavf_add_del_eth_addr(adapter, mac_addr, true);
1168         if (ret)
1169                 PMD_DRV_LOG(ERR, "Fail to add new MAC:"
1170                             " %02X:%02X:%02X:%02X:%02X:%02X",
1171                             mac_addr->addr_bytes[0],
1172                             mac_addr->addr_bytes[1],
1173                             mac_addr->addr_bytes[2],
1174                             mac_addr->addr_bytes[3],
1175                             mac_addr->addr_bytes[4],
1176                             mac_addr->addr_bytes[5]);
1177
1178         if (ret)
1179                 return -EIO;
1180
1181         rte_ether_addr_copy(mac_addr, (struct rte_ether_addr *)hw->mac.addr);
1182         return 0;
1183 }
1184
1185 static void
1186 iavf_stat_update_48(uint64_t *offset, uint64_t *stat)
1187 {
1188         if (*stat >= *offset)
1189                 *stat = *stat - *offset;
1190         else
1191                 *stat = (uint64_t)((*stat +
1192                         ((uint64_t)1 << IAVF_48_BIT_WIDTH)) - *offset);
1193
1194         *stat &= IAVF_48_BIT_MASK;
1195 }
1196
1197 static void
1198 iavf_stat_update_32(uint64_t *offset, uint64_t *stat)
1199 {
1200         if (*stat >= *offset)
1201                 *stat = (uint64_t)(*stat - *offset);
1202         else
1203                 *stat = (uint64_t)((*stat +
1204                         ((uint64_t)1 << IAVF_32_BIT_WIDTH)) - *offset);
1205 }
1206
1207 static void
1208 iavf_update_stats(struct iavf_vsi *vsi, struct virtchnl_eth_stats *nes)
1209 {
1210         struct virtchnl_eth_stats *oes = &vsi->eth_stats_offset;
1211
1212         iavf_stat_update_48(&oes->rx_bytes, &nes->rx_bytes);
1213         iavf_stat_update_48(&oes->rx_unicast, &nes->rx_unicast);
1214         iavf_stat_update_48(&oes->rx_multicast, &nes->rx_multicast);
1215         iavf_stat_update_48(&oes->rx_broadcast, &nes->rx_broadcast);
1216         iavf_stat_update_32(&oes->rx_discards, &nes->rx_discards);
1217         iavf_stat_update_48(&oes->tx_bytes, &nes->tx_bytes);
1218         iavf_stat_update_48(&oes->tx_unicast, &nes->tx_unicast);
1219         iavf_stat_update_48(&oes->tx_multicast, &nes->tx_multicast);
1220         iavf_stat_update_48(&oes->tx_broadcast, &nes->tx_broadcast);
1221         iavf_stat_update_32(&oes->tx_errors, &nes->tx_errors);
1222         iavf_stat_update_32(&oes->tx_discards, &nes->tx_discards);
1223 }
1224
1225 static int
1226 iavf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1227 {
1228         struct iavf_adapter *adapter =
1229                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1230         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1231         struct iavf_vsi *vsi = &vf->vsi;
1232         struct virtchnl_eth_stats *pstats = NULL;
1233         int ret;
1234
1235         ret = iavf_query_stats(adapter, &pstats);
1236         if (ret == 0) {
1237                 iavf_update_stats(vsi, pstats);
1238                 stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
1239                                 pstats->rx_broadcast - pstats->rx_discards;
1240                 stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
1241                                                 pstats->tx_unicast;
1242                 stats->imissed = pstats->rx_discards;
1243                 stats->oerrors = pstats->tx_errors + pstats->tx_discards;
1244                 stats->ibytes = pstats->rx_bytes;
1245                 stats->ibytes -= stats->ipackets * RTE_ETHER_CRC_LEN;
1246                 stats->obytes = pstats->tx_bytes;
1247         } else {
1248                 PMD_DRV_LOG(ERR, "Get statistics failed");
1249         }
1250         return ret;
1251 }
1252
1253 static int
1254 iavf_dev_stats_reset(struct rte_eth_dev *dev)
1255 {
1256         int ret;
1257         struct iavf_adapter *adapter =
1258                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1259         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1260         struct iavf_vsi *vsi = &vf->vsi;
1261         struct virtchnl_eth_stats *pstats = NULL;
1262
1263         /* read stat values to clear hardware registers */
1264         ret = iavf_query_stats(adapter, &pstats);
1265         if (ret != 0)
1266                 return ret;
1267
1268         /* set stats offset base on current values */
1269         vsi->eth_stats_offset = *pstats;
1270
1271         return 0;
1272 }
1273
1274 static int iavf_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
1275                                       struct rte_eth_xstat_name *xstats_names,
1276                                       __rte_unused unsigned int limit)
1277 {
1278         unsigned int i;
1279
1280         if (xstats_names != NULL)
1281                 for (i = 0; i < IAVF_NB_XSTATS; i++) {
1282                         snprintf(xstats_names[i].name,
1283                                 sizeof(xstats_names[i].name),
1284                                 "%s", rte_iavf_stats_strings[i].name);
1285                 }
1286         return IAVF_NB_XSTATS;
1287 }
1288
1289 static int iavf_dev_xstats_get(struct rte_eth_dev *dev,
1290                                  struct rte_eth_xstat *xstats, unsigned int n)
1291 {
1292         int ret;
1293         unsigned int i;
1294         struct iavf_adapter *adapter =
1295                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1296         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1297         struct iavf_vsi *vsi = &vf->vsi;
1298         struct virtchnl_eth_stats *pstats = NULL;
1299
1300         if (n < IAVF_NB_XSTATS)
1301                 return IAVF_NB_XSTATS;
1302
1303         ret = iavf_query_stats(adapter, &pstats);
1304         if (ret != 0)
1305                 return 0;
1306
1307         if (!xstats)
1308                 return 0;
1309
1310         iavf_update_stats(vsi, pstats);
1311
1312         /* loop over xstats array and values from pstats */
1313         for (i = 0; i < IAVF_NB_XSTATS; i++) {
1314                 xstats[i].id = i;
1315                 xstats[i].value = *(uint64_t *)(((char *)pstats) +
1316                         rte_iavf_stats_strings[i].offset);
1317         }
1318
1319         return IAVF_NB_XSTATS;
1320 }
1321
1322
1323 static int
1324 iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1325 {
1326         struct iavf_adapter *adapter =
1327                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1328         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1329         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1330         uint16_t msix_intr;
1331
1332         msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1333         if (msix_intr == IAVF_MISC_VEC_ID) {
1334                 PMD_DRV_LOG(INFO, "MISC is also enabled for control");
1335                 IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1336                                IAVF_VFINT_DYN_CTL01_INTENA_MASK |
1337                                IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1338                                IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1339         } else {
1340                 IAVF_WRITE_REG(hw,
1341                                IAVF_VFINT_DYN_CTLN1
1342                                 (msix_intr - IAVF_RX_VEC_START),
1343                                IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1344                                IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1345                                IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
1346         }
1347
1348         IAVF_WRITE_FLUSH(hw);
1349
1350         rte_intr_ack(&pci_dev->intr_handle);
1351
1352         return 0;
1353 }
1354
1355 static int
1356 iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1357 {
1358         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1359         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1360         uint16_t msix_intr;
1361
1362         msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1363         if (msix_intr == IAVF_MISC_VEC_ID) {
1364                 PMD_DRV_LOG(ERR, "MISC is used for control, cannot disable it");
1365                 return -EIO;
1366         }
1367
1368         IAVF_WRITE_REG(hw,
1369                       IAVF_VFINT_DYN_CTLN1(msix_intr - IAVF_RX_VEC_START),
1370                       0);
1371
1372         IAVF_WRITE_FLUSH(hw);
1373         return 0;
1374 }
1375
1376 static int
1377 iavf_check_vf_reset_done(struct iavf_hw *hw)
1378 {
1379         int i, reset;
1380
1381         for (i = 0; i < IAVF_RESET_WAIT_CNT; i++) {
1382                 reset = IAVF_READ_REG(hw, IAVF_VFGEN_RSTAT) &
1383                         IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1384                 reset = reset >> IAVF_VFGEN_RSTAT_VFR_STATE_SHIFT;
1385                 if (reset == VIRTCHNL_VFR_VFACTIVE ||
1386                     reset == VIRTCHNL_VFR_COMPLETED)
1387                         break;
1388                 rte_delay_ms(20);
1389         }
1390
1391         if (i >= IAVF_RESET_WAIT_CNT)
1392                 return -1;
1393
1394         return 0;
1395 }
1396
1397 static int
1398 iavf_init_vf(struct rte_eth_dev *dev)
1399 {
1400         int err, bufsz;
1401         struct iavf_adapter *adapter =
1402                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1403         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1404         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1405
1406         err = iavf_set_mac_type(hw);
1407         if (err) {
1408                 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
1409                 goto err;
1410         }
1411
1412         err = iavf_check_vf_reset_done(hw);
1413         if (err) {
1414                 PMD_INIT_LOG(ERR, "VF is still resetting");
1415                 goto err;
1416         }
1417
1418         iavf_init_adminq_parameter(hw);
1419         err = iavf_init_adminq(hw);
1420         if (err) {
1421                 PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
1422                 goto err;
1423         }
1424
1425         vf->aq_resp = rte_zmalloc("vf_aq_resp", IAVF_AQ_BUF_SZ, 0);
1426         if (!vf->aq_resp) {
1427                 PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
1428                 goto err_aq;
1429         }
1430         if (iavf_check_api_version(adapter) != 0) {
1431                 PMD_INIT_LOG(ERR, "check_api version failed");
1432                 goto err_api;
1433         }
1434
1435         bufsz = sizeof(struct virtchnl_vf_resource) +
1436                 (IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
1437         vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
1438         if (!vf->vf_res) {
1439                 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
1440                 goto err_api;
1441         }
1442         if (iavf_get_vf_resource(adapter) != 0) {
1443                 PMD_INIT_LOG(ERR, "iavf_get_vf_config failed");
1444                 goto err_alloc;
1445         }
1446         /* Allocate memort for RSS info */
1447         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1448                 vf->rss_key = rte_zmalloc("rss_key",
1449                                           vf->vf_res->rss_key_size, 0);
1450                 if (!vf->rss_key) {
1451                         PMD_INIT_LOG(ERR, "unable to allocate rss_key memory");
1452                         goto err_rss;
1453                 }
1454                 vf->rss_lut = rte_zmalloc("rss_lut",
1455                                           vf->vf_res->rss_lut_size, 0);
1456                 if (!vf->rss_lut) {
1457                         PMD_INIT_LOG(ERR, "unable to allocate rss_lut memory");
1458                         goto err_rss;
1459                 }
1460         }
1461
1462         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
1463                 if (iavf_get_supported_rxdid(adapter) != 0) {
1464                         PMD_INIT_LOG(ERR, "failed to do get supported rxdid");
1465                         goto err_rss;
1466                 }
1467         }
1468
1469         return 0;
1470 err_rss:
1471         rte_free(vf->rss_key);
1472         rte_free(vf->rss_lut);
1473 err_alloc:
1474         rte_free(vf->vf_res);
1475         vf->vsi_res = NULL;
1476 err_api:
1477         rte_free(vf->aq_resp);
1478 err_aq:
1479         iavf_shutdown_adminq(hw);
1480 err:
1481         return -1;
1482 }
1483
1484 /* Enable default admin queue interrupt setting */
1485 static inline void
1486 iavf_enable_irq0(struct iavf_hw *hw)
1487 {
1488         /* Enable admin queue interrupt trigger */
1489         IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1,
1490                        IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
1491
1492         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1493                        IAVF_VFINT_DYN_CTL01_INTENA_MASK |
1494                        IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1495                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1496
1497         IAVF_WRITE_FLUSH(hw);
1498 }
1499
1500 static inline void
1501 iavf_disable_irq0(struct iavf_hw *hw)
1502 {
1503         /* Disable all interrupt types */
1504         IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1, 0);
1505         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1506                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1507         IAVF_WRITE_FLUSH(hw);
1508 }
1509
1510 static void
1511 iavf_dev_interrupt_handler(void *param)
1512 {
1513         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1514         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1515
1516         iavf_disable_irq0(hw);
1517
1518         iavf_handle_virtchnl_msg(dev);
1519
1520         iavf_enable_irq0(hw);
1521 }
1522
1523 static int
1524 iavf_dev_filter_ctrl(struct rte_eth_dev *dev,
1525                      enum rte_filter_type filter_type,
1526                      enum rte_filter_op filter_op,
1527                      void *arg)
1528 {
1529         int ret = 0;
1530
1531         if (!dev)
1532                 return -EINVAL;
1533
1534         switch (filter_type) {
1535         case RTE_ETH_FILTER_GENERIC:
1536                 if (filter_op != RTE_ETH_FILTER_GET)
1537                         return -EINVAL;
1538                 *(const void **)arg = &iavf_flow_ops;
1539                 break;
1540         default:
1541                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
1542                             filter_type);
1543                 ret = -EINVAL;
1544                 break;
1545         }
1546
1547         return ret;
1548 }
1549
1550
1551 static int
1552 iavf_dev_init(struct rte_eth_dev *eth_dev)
1553 {
1554         struct iavf_adapter *adapter =
1555                 IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1556         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1557         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1558         int ret = 0;
1559
1560         PMD_INIT_FUNC_TRACE();
1561
1562         /* assign ops func pointer */
1563         eth_dev->dev_ops = &iavf_eth_dev_ops;
1564         eth_dev->rx_queue_count = iavf_dev_rxq_count;
1565         eth_dev->rx_descriptor_status = iavf_dev_rx_desc_status;
1566         eth_dev->tx_descriptor_status = iavf_dev_tx_desc_status;
1567         eth_dev->rx_pkt_burst = &iavf_recv_pkts;
1568         eth_dev->tx_pkt_burst = &iavf_xmit_pkts;
1569         eth_dev->tx_pkt_prepare = &iavf_prep_pkts;
1570
1571         /* For secondary processes, we don't initialise any further as primary
1572          * has already done this work. Only check if we need a different RX
1573          * and TX function.
1574          */
1575         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1576                 iavf_set_rx_function(eth_dev);
1577                 iavf_set_tx_function(eth_dev);
1578                 return 0;
1579         }
1580         rte_eth_copy_pci_info(eth_dev, pci_dev);
1581         eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1582
1583         hw->vendor_id = pci_dev->id.vendor_id;
1584         hw->device_id = pci_dev->id.device_id;
1585         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1586         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1587         hw->bus.bus_id = pci_dev->addr.bus;
1588         hw->bus.device = pci_dev->addr.devid;
1589         hw->bus.func = pci_dev->addr.function;
1590         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1591         hw->back = IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1592         adapter->eth_dev = eth_dev;
1593         adapter->stopped = 1;
1594
1595         if (iavf_init_vf(eth_dev) != 0) {
1596                 PMD_INIT_LOG(ERR, "Init vf failed");
1597                 return -1;
1598         }
1599
1600         /* set default ptype table */
1601         adapter->ptype_tbl = iavf_get_default_ptype_table();
1602
1603         /* copy mac addr */
1604         eth_dev->data->mac_addrs = rte_zmalloc(
1605                 "iavf_mac", RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX, 0);
1606         if (!eth_dev->data->mac_addrs) {
1607                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
1608                              " store MAC addresses",
1609                              RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX);
1610                 return -ENOMEM;
1611         }
1612         /* If the MAC address is not configured by host,
1613          * generate a random one.
1614          */
1615         if (!rte_is_valid_assigned_ether_addr(
1616                         (struct rte_ether_addr *)hw->mac.addr))
1617                 rte_eth_random_addr(hw->mac.addr);
1618         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.addr,
1619                         &eth_dev->data->mac_addrs[0]);
1620
1621         /* register callback func to eal lib */
1622         rte_intr_callback_register(&pci_dev->intr_handle,
1623                                    iavf_dev_interrupt_handler,
1624                                    (void *)eth_dev);
1625
1626         /* enable uio intr after callback register */
1627         rte_intr_enable(&pci_dev->intr_handle);
1628
1629         /* configure and enable device interrupt */
1630         iavf_enable_irq0(hw);
1631
1632         ret = iavf_flow_init(adapter);
1633         if (ret) {
1634                 PMD_INIT_LOG(ERR, "Failed to initialize flow");
1635                 return ret;
1636         }
1637
1638         return 0;
1639 }
1640
1641 static int
1642 iavf_dev_close(struct rte_eth_dev *dev)
1643 {
1644         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1645         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1646         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1647         struct iavf_adapter *adapter =
1648                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1649         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1650         int ret;
1651
1652         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1653                 return 0;
1654
1655         ret = iavf_dev_stop(dev);
1656
1657         iavf_flow_flush(dev, NULL);
1658         iavf_flow_uninit(adapter);
1659
1660         /*
1661          * disable promiscuous mode before reset vf
1662          * it is a workaround solution when work with kernel driver
1663          * and it is not the normal way
1664          */
1665         if (vf->promisc_unicast_enabled || vf->promisc_multicast_enabled)
1666                 iavf_config_promisc(adapter, false, false);
1667
1668         iavf_shutdown_adminq(hw);
1669         /* disable uio intr before callback unregister */
1670         rte_intr_disable(intr_handle);
1671
1672         /* unregister callback func from eal lib */
1673         rte_intr_callback_unregister(intr_handle,
1674                                      iavf_dev_interrupt_handler, dev);
1675         iavf_disable_irq0(hw);
1676
1677         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1678                 if (vf->rss_lut) {
1679                         rte_free(vf->rss_lut);
1680                         vf->rss_lut = NULL;
1681                 }
1682                 if (vf->rss_key) {
1683                         rte_free(vf->rss_key);
1684                         vf->rss_key = NULL;
1685                 }
1686         }
1687
1688         rte_free(vf->vf_res);
1689         vf->vsi_res = NULL;
1690         vf->vf_res = NULL;
1691
1692         rte_free(vf->aq_resp);
1693         vf->aq_resp = NULL;
1694
1695         vf->vf_reset = false;
1696
1697         return ret;
1698 }
1699
1700 static int
1701 iavf_dev_uninit(struct rte_eth_dev *dev)
1702 {
1703         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1704                 return -EPERM;
1705
1706         iavf_dev_close(dev);
1707
1708         return 0;
1709 }
1710
1711 /*
1712  * Reset VF device only to re-initialize resources in PMD layer
1713  */
1714 static int
1715 iavf_dev_reset(struct rte_eth_dev *dev)
1716 {
1717         int ret;
1718
1719         ret = iavf_dev_uninit(dev);
1720         if (ret)
1721                 return ret;
1722
1723         return iavf_dev_init(dev);
1724 }
1725
1726 static int
1727 iavf_dcf_cap_check_handler(__rte_unused const char *key,
1728                            const char *value, __rte_unused void *opaque)
1729 {
1730         if (strcmp(value, "dcf"))
1731                 return -1;
1732
1733         return 0;
1734 }
1735
1736 static int
1737 iavf_dcf_cap_selected(struct rte_devargs *devargs)
1738 {
1739         struct rte_kvargs *kvlist;
1740         const char *key = "cap";
1741         int ret = 0;
1742
1743         if (devargs == NULL)
1744                 return 0;
1745
1746         kvlist = rte_kvargs_parse(devargs->args, NULL);
1747         if (kvlist == NULL)
1748                 return 0;
1749
1750         if (!rte_kvargs_count(kvlist, key))
1751                 goto exit;
1752
1753         /* dcf capability selected when there's a key-value pair: cap=dcf */
1754         if (rte_kvargs_process(kvlist, key,
1755                                iavf_dcf_cap_check_handler, NULL) < 0)
1756                 goto exit;
1757
1758         ret = 1;
1759
1760 exit:
1761         rte_kvargs_free(kvlist);
1762         return ret;
1763 }
1764
1765 static int eth_iavf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1766                              struct rte_pci_device *pci_dev)
1767 {
1768         if (iavf_dcf_cap_selected(pci_dev->device.devargs))
1769                 return 1;
1770
1771         return rte_eth_dev_pci_generic_probe(pci_dev,
1772                 sizeof(struct iavf_adapter), iavf_dev_init);
1773 }
1774
1775 static int eth_iavf_pci_remove(struct rte_pci_device *pci_dev)
1776 {
1777         return rte_eth_dev_pci_generic_remove(pci_dev, iavf_dev_uninit);
1778 }
1779
1780 /* Adaptive virtual function driver struct */
1781 static struct rte_pci_driver rte_iavf_pmd = {
1782         .id_table = pci_id_iavf_map,
1783         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1784         .probe = eth_iavf_pci_probe,
1785         .remove = eth_iavf_pci_remove,
1786 };
1787
1788 RTE_PMD_REGISTER_PCI(net_iavf, rte_iavf_pmd);
1789 RTE_PMD_REGISTER_PCI_TABLE(net_iavf, pci_id_iavf_map);
1790 RTE_PMD_REGISTER_KMOD_DEP(net_iavf, "* igb_uio | vfio-pci");
1791 RTE_PMD_REGISTER_PARAM_STRING(net_iavf, "cap=dcf");
1792 RTE_LOG_REGISTER(iavf_logtype_init, pmd.net.iavf.init, NOTICE);
1793 RTE_LOG_REGISTER(iavf_logtype_driver, pmd.net.iavf.driver, NOTICE);
1794 #ifdef RTE_LIBRTE_IAVF_DEBUG_RX
1795 RTE_LOG_REGISTER(iavf_logtype_rx, pmd.net.iavf.rx, DEBUG);
1796 #endif
1797 #ifdef RTE_LIBRTE_IAVF_DEBUG_TX
1798 RTE_LOG_REGISTER(iavf_logtype_tx, pmd.net.iavf.tx, DEBUG);
1799 #endif
1800 #ifdef RTE_LIBRTE_IAVF_DEBUG_TX_FREE
1801 RTE_LOG_REGISTER(iavf_logtype_tx_free, pmd.net.iavf.tx_free, DEBUG);
1802 #endif