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