net/iavf: fix scattered Rx enabling
[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
694                 ret = -EAGAIN;
695
696         return ret;
697 }
698
699 static int
700 iavf_dev_promiscuous_disable(struct rte_eth_dev *dev)
701 {
702         struct iavf_adapter *adapter =
703                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
704         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
705         int ret;
706
707         if (!vf->promisc_unicast_enabled)
708                 return 0;
709
710         ret = iavf_config_promisc(adapter, false,
711                                   vf->promisc_multicast_enabled);
712         if (!ret)
713                 vf->promisc_unicast_enabled = false;
714         else
715                 ret = -EAGAIN;
716
717         return ret;
718 }
719
720 static int
721 iavf_dev_allmulticast_enable(struct rte_eth_dev *dev)
722 {
723         struct iavf_adapter *adapter =
724                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
725         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
726         int ret;
727
728         if (vf->promisc_multicast_enabled)
729                 return 0;
730
731         ret = iavf_config_promisc(adapter, vf->promisc_unicast_enabled, true);
732         if (!ret)
733                 vf->promisc_multicast_enabled = true;
734         else
735                 ret = -EAGAIN;
736
737         return ret;
738 }
739
740 static int
741 iavf_dev_allmulticast_disable(struct rte_eth_dev *dev)
742 {
743         struct iavf_adapter *adapter =
744                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
745         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
746         int ret;
747
748         if (!vf->promisc_multicast_enabled)
749                 return 0;
750
751         ret = iavf_config_promisc(adapter, vf->promisc_unicast_enabled, false);
752         if (!ret)
753                 vf->promisc_multicast_enabled = false;
754         else
755                 ret = -EAGAIN;
756
757         return ret;
758 }
759
760 static int
761 iavf_dev_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *addr,
762                      __rte_unused uint32_t index,
763                      __rte_unused uint32_t pool)
764 {
765         struct iavf_adapter *adapter =
766                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
767         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
768         int err;
769
770         if (rte_is_zero_ether_addr(addr)) {
771                 PMD_DRV_LOG(ERR, "Invalid Ethernet Address");
772                 return -EINVAL;
773         }
774
775         err = iavf_add_del_eth_addr(adapter, addr, true);
776         if (err) {
777                 PMD_DRV_LOG(ERR, "fail to add MAC address");
778                 return -EIO;
779         }
780
781         vf->mac_num++;
782
783         return 0;
784 }
785
786 static void
787 iavf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
788 {
789         struct iavf_adapter *adapter =
790                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
791         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
792         struct rte_ether_addr *addr;
793         int err;
794
795         addr = &dev->data->mac_addrs[index];
796
797         err = iavf_add_del_eth_addr(adapter, addr, false);
798         if (err)
799                 PMD_DRV_LOG(ERR, "fail to delete MAC address");
800
801         vf->mac_num--;
802 }
803
804 static int
805 iavf_dev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
806 {
807         struct iavf_adapter *adapter =
808                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
809         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
810         int err;
811
812         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
813                 return -ENOTSUP;
814
815         err = iavf_add_del_vlan(adapter, vlan_id, on);
816         if (err)
817                 return -EIO;
818         return 0;
819 }
820
821 static int
822 iavf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
823 {
824         struct iavf_adapter *adapter =
825                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
826         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
827         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
828         int err;
829
830         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
831                 return -ENOTSUP;
832
833         /* Vlan stripping setting */
834         if (mask & ETH_VLAN_STRIP_MASK) {
835                 /* Enable or disable VLAN stripping */
836                 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
837                         err = iavf_enable_vlan_strip(adapter);
838                 else
839                         err = iavf_disable_vlan_strip(adapter);
840
841                 if (err)
842                         return -EIO;
843         }
844         return 0;
845 }
846
847 static int
848 iavf_dev_rss_reta_update(struct rte_eth_dev *dev,
849                         struct rte_eth_rss_reta_entry64 *reta_conf,
850                         uint16_t reta_size)
851 {
852         struct iavf_adapter *adapter =
853                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
854         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
855         uint8_t *lut;
856         uint16_t i, idx, shift;
857         int ret;
858
859         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
860                 return -ENOTSUP;
861
862         if (reta_size != vf->vf_res->rss_lut_size) {
863                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
864                         "(%d) doesn't match the number of hardware can "
865                         "support (%d)", reta_size, vf->vf_res->rss_lut_size);
866                 return -EINVAL;
867         }
868
869         lut = rte_zmalloc("rss_lut", reta_size, 0);
870         if (!lut) {
871                 PMD_DRV_LOG(ERR, "No memory can be allocated");
872                 return -ENOMEM;
873         }
874         /* store the old lut table temporarily */
875         rte_memcpy(lut, vf->rss_lut, reta_size);
876
877         for (i = 0; i < reta_size; i++) {
878                 idx = i / RTE_RETA_GROUP_SIZE;
879                 shift = i % RTE_RETA_GROUP_SIZE;
880                 if (reta_conf[idx].mask & (1ULL << shift))
881                         lut[i] = reta_conf[idx].reta[shift];
882         }
883
884         rte_memcpy(vf->rss_lut, lut, reta_size);
885         /* send virtchnnl ops to configure rss*/
886         ret = iavf_configure_rss_lut(adapter);
887         if (ret) /* revert back */
888                 rte_memcpy(vf->rss_lut, lut, reta_size);
889         rte_free(lut);
890
891         return ret;
892 }
893
894 static int
895 iavf_dev_rss_reta_query(struct rte_eth_dev *dev,
896                        struct rte_eth_rss_reta_entry64 *reta_conf,
897                        uint16_t reta_size)
898 {
899         struct iavf_adapter *adapter =
900                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
901         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
902         uint16_t i, idx, shift;
903
904         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
905                 return -ENOTSUP;
906
907         if (reta_size != vf->vf_res->rss_lut_size) {
908                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
909                         "(%d) doesn't match the number of hardware can "
910                         "support (%d)", reta_size, vf->vf_res->rss_lut_size);
911                 return -EINVAL;
912         }
913
914         for (i = 0; i < reta_size; i++) {
915                 idx = i / RTE_RETA_GROUP_SIZE;
916                 shift = i % RTE_RETA_GROUP_SIZE;
917                 if (reta_conf[idx].mask & (1ULL << shift))
918                         reta_conf[idx].reta[shift] = vf->rss_lut[i];
919         }
920
921         return 0;
922 }
923
924 static int
925 iavf_dev_rss_hash_update(struct rte_eth_dev *dev,
926                         struct rte_eth_rss_conf *rss_conf)
927 {
928         struct iavf_adapter *adapter =
929                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
930         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
931
932         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
933                 return -ENOTSUP;
934
935         /* HENA setting, it is enabled by default, no change */
936         if (!rss_conf->rss_key || rss_conf->rss_key_len == 0) {
937                 PMD_DRV_LOG(DEBUG, "No key to be configured");
938                 return 0;
939         } else if (rss_conf->rss_key_len != vf->vf_res->rss_key_size) {
940                 PMD_DRV_LOG(ERR, "The size of hash key configured "
941                         "(%d) doesn't match the size of hardware can "
942                         "support (%d)", rss_conf->rss_key_len,
943                         vf->vf_res->rss_key_size);
944                 return -EINVAL;
945         }
946
947         rte_memcpy(vf->rss_key, rss_conf->rss_key, rss_conf->rss_key_len);
948
949         return iavf_configure_rss_key(adapter);
950 }
951
952 static int
953 iavf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
954                           struct rte_eth_rss_conf *rss_conf)
955 {
956         struct iavf_adapter *adapter =
957                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
958         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
959
960         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
961                 return -ENOTSUP;
962
963          /* Just set it to default value now. */
964         rss_conf->rss_hf = IAVF_RSS_OFFLOAD_ALL;
965
966         if (!rss_conf->rss_key)
967                 return 0;
968
969         rss_conf->rss_key_len = vf->vf_res->rss_key_size;
970         rte_memcpy(rss_conf->rss_key, vf->rss_key, rss_conf->rss_key_len);
971
972         return 0;
973 }
974
975 static int
976 iavf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
977 {
978         uint32_t frame_size = mtu + IAVF_ETH_OVERHEAD;
979         int ret = 0;
980
981         if (mtu < RTE_ETHER_MIN_MTU || frame_size > IAVF_FRAME_SIZE_MAX)
982                 return -EINVAL;
983
984         /* mtu setting is forbidden if port is start */
985         if (dev->data->dev_started) {
986                 PMD_DRV_LOG(ERR, "port must be stopped before configuration");
987                 return -EBUSY;
988         }
989
990         if (frame_size > RTE_ETHER_MAX_LEN)
991                 dev->data->dev_conf.rxmode.offloads |=
992                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
993         else
994                 dev->data->dev_conf.rxmode.offloads &=
995                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
996
997         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
998
999         return ret;
1000 }
1001
1002 static int
1003 iavf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
1004                              struct rte_ether_addr *mac_addr)
1005 {
1006         struct iavf_adapter *adapter =
1007                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1008         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1009         struct rte_ether_addr *perm_addr, *old_addr;
1010         int ret;
1011
1012         old_addr = (struct rte_ether_addr *)hw->mac.addr;
1013         perm_addr = (struct rte_ether_addr *)hw->mac.perm_addr;
1014
1015         if (rte_is_same_ether_addr(mac_addr, old_addr))
1016                 return 0;
1017
1018         /* If the MAC address is configured by host, skip the setting */
1019         if (rte_is_valid_assigned_ether_addr(perm_addr))
1020                 return -EPERM;
1021
1022         ret = iavf_add_del_eth_addr(adapter, old_addr, false);
1023         if (ret)
1024                 PMD_DRV_LOG(ERR, "Fail to delete old MAC:"
1025                             " %02X:%02X:%02X:%02X:%02X:%02X",
1026                             old_addr->addr_bytes[0],
1027                             old_addr->addr_bytes[1],
1028                             old_addr->addr_bytes[2],
1029                             old_addr->addr_bytes[3],
1030                             old_addr->addr_bytes[4],
1031                             old_addr->addr_bytes[5]);
1032
1033         ret = iavf_add_del_eth_addr(adapter, mac_addr, true);
1034         if (ret)
1035                 PMD_DRV_LOG(ERR, "Fail to add new MAC:"
1036                             " %02X:%02X:%02X:%02X:%02X:%02X",
1037                             mac_addr->addr_bytes[0],
1038                             mac_addr->addr_bytes[1],
1039                             mac_addr->addr_bytes[2],
1040                             mac_addr->addr_bytes[3],
1041                             mac_addr->addr_bytes[4],
1042                             mac_addr->addr_bytes[5]);
1043
1044         if (ret)
1045                 return -EIO;
1046
1047         rte_ether_addr_copy(mac_addr, (struct rte_ether_addr *)hw->mac.addr);
1048         return 0;
1049 }
1050
1051 static void
1052 iavf_stat_update_48(uint64_t *offset, uint64_t *stat)
1053 {
1054         if (*stat >= *offset)
1055                 *stat = *stat - *offset;
1056         else
1057                 *stat = (uint64_t)((*stat +
1058                         ((uint64_t)1 << IAVF_48_BIT_WIDTH)) - *offset);
1059
1060         *stat &= IAVF_48_BIT_MASK;
1061 }
1062
1063 static void
1064 iavf_stat_update_32(uint64_t *offset, uint64_t *stat)
1065 {
1066         if (*stat >= *offset)
1067                 *stat = (uint64_t)(*stat - *offset);
1068         else
1069                 *stat = (uint64_t)((*stat +
1070                         ((uint64_t)1 << IAVF_32_BIT_WIDTH)) - *offset);
1071 }
1072
1073 static void
1074 iavf_update_stats(struct iavf_vsi *vsi, struct virtchnl_eth_stats *nes)
1075 {
1076         struct virtchnl_eth_stats *oes = &vsi->eth_stats_offset;
1077
1078         iavf_stat_update_48(&oes->rx_bytes, &nes->rx_bytes);
1079         iavf_stat_update_48(&oes->rx_unicast, &nes->rx_unicast);
1080         iavf_stat_update_48(&oes->rx_multicast, &nes->rx_multicast);
1081         iavf_stat_update_48(&oes->rx_broadcast, &nes->rx_broadcast);
1082         iavf_stat_update_32(&oes->rx_discards, &nes->rx_discards);
1083         iavf_stat_update_48(&oes->tx_bytes, &nes->tx_bytes);
1084         iavf_stat_update_48(&oes->tx_unicast, &nes->tx_unicast);
1085         iavf_stat_update_48(&oes->tx_multicast, &nes->tx_multicast);
1086         iavf_stat_update_48(&oes->tx_broadcast, &nes->tx_broadcast);
1087         iavf_stat_update_32(&oes->tx_errors, &nes->tx_errors);
1088         iavf_stat_update_32(&oes->tx_discards, &nes->tx_discards);
1089 }
1090
1091 static int
1092 iavf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1093 {
1094         struct iavf_adapter *adapter =
1095                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1096         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1097         struct iavf_vsi *vsi = &vf->vsi;
1098         struct virtchnl_eth_stats *pstats = NULL;
1099         int ret;
1100
1101         ret = iavf_query_stats(adapter, &pstats);
1102         if (ret == 0) {
1103                 iavf_update_stats(vsi, pstats);
1104                 stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
1105                                 pstats->rx_broadcast - pstats->rx_discards;
1106                 stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
1107                                                 pstats->tx_unicast;
1108                 stats->imissed = pstats->rx_discards;
1109                 stats->oerrors = pstats->tx_errors + pstats->tx_discards;
1110                 stats->ibytes = pstats->rx_bytes;
1111                 stats->ibytes -= stats->ipackets * RTE_ETHER_CRC_LEN;
1112                 stats->obytes = pstats->tx_bytes;
1113         } else {
1114                 PMD_DRV_LOG(ERR, "Get statistics failed");
1115         }
1116         return ret;
1117 }
1118
1119 static int
1120 iavf_dev_stats_reset(struct rte_eth_dev *dev)
1121 {
1122         int ret;
1123         struct iavf_adapter *adapter =
1124                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1125         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1126         struct iavf_vsi *vsi = &vf->vsi;
1127         struct virtchnl_eth_stats *pstats = NULL;
1128
1129         /* read stat values to clear hardware registers */
1130         ret = iavf_query_stats(adapter, &pstats);
1131         if (ret != 0)
1132                 return ret;
1133
1134         /* set stats offset base on current values */
1135         vsi->eth_stats_offset = *pstats;
1136
1137         return 0;
1138 }
1139
1140 static int
1141 iavf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1142 {
1143         struct iavf_adapter *adapter =
1144                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1145         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1146         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1147         uint16_t msix_intr;
1148
1149         msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1150         if (msix_intr == IAVF_MISC_VEC_ID) {
1151                 PMD_DRV_LOG(INFO, "MISC is also enabled for control");
1152                 IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1153                                IAVF_VFINT_DYN_CTL01_INTENA_MASK |
1154                                IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1155                                IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1156         } else {
1157                 IAVF_WRITE_REG(hw,
1158                                IAVF_VFINT_DYN_CTLN1
1159                                 (msix_intr - IAVF_RX_VEC_START),
1160                                IAVF_VFINT_DYN_CTLN1_INTENA_MASK |
1161                                IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1162                                IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK);
1163         }
1164
1165         IAVF_WRITE_FLUSH(hw);
1166
1167         rte_intr_ack(&pci_dev->intr_handle);
1168
1169         return 0;
1170 }
1171
1172 static int
1173 iavf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1174 {
1175         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1176         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1177         uint16_t msix_intr;
1178
1179         msix_intr = pci_dev->intr_handle.intr_vec[queue_id];
1180         if (msix_intr == IAVF_MISC_VEC_ID) {
1181                 PMD_DRV_LOG(ERR, "MISC is used for control, cannot disable it");
1182                 return -EIO;
1183         }
1184
1185         IAVF_WRITE_REG(hw,
1186                       IAVF_VFINT_DYN_CTLN1(msix_intr - IAVF_RX_VEC_START),
1187                       0);
1188
1189         IAVF_WRITE_FLUSH(hw);
1190         return 0;
1191 }
1192
1193 static int
1194 iavf_check_vf_reset_done(struct iavf_hw *hw)
1195 {
1196         int i, reset;
1197
1198         for (i = 0; i < IAVF_RESET_WAIT_CNT; i++) {
1199                 reset = IAVF_READ_REG(hw, IAVF_VFGEN_RSTAT) &
1200                         IAVF_VFGEN_RSTAT_VFR_STATE_MASK;
1201                 reset = reset >> IAVF_VFGEN_RSTAT_VFR_STATE_SHIFT;
1202                 if (reset == VIRTCHNL_VFR_VFACTIVE ||
1203                     reset == VIRTCHNL_VFR_COMPLETED)
1204                         break;
1205                 rte_delay_ms(20);
1206         }
1207
1208         if (i >= IAVF_RESET_WAIT_CNT)
1209                 return -1;
1210
1211         return 0;
1212 }
1213
1214 static int
1215 iavf_init_vf(struct rte_eth_dev *dev)
1216 {
1217         int err, bufsz;
1218         struct iavf_adapter *adapter =
1219                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1220         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1221         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1222
1223         err = iavf_set_mac_type(hw);
1224         if (err) {
1225                 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
1226                 goto err;
1227         }
1228
1229         err = iavf_check_vf_reset_done(hw);
1230         if (err) {
1231                 PMD_INIT_LOG(ERR, "VF is still resetting");
1232                 goto err;
1233         }
1234
1235         iavf_init_adminq_parameter(hw);
1236         err = iavf_init_adminq(hw);
1237         if (err) {
1238                 PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
1239                 goto err;
1240         }
1241
1242         vf->aq_resp = rte_zmalloc("vf_aq_resp", IAVF_AQ_BUF_SZ, 0);
1243         if (!vf->aq_resp) {
1244                 PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
1245                 goto err_aq;
1246         }
1247         if (iavf_check_api_version(adapter) != 0) {
1248                 PMD_INIT_LOG(ERR, "check_api version failed");
1249                 goto err_api;
1250         }
1251
1252         bufsz = sizeof(struct virtchnl_vf_resource) +
1253                 (IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
1254         vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
1255         if (!vf->vf_res) {
1256                 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
1257                 goto err_api;
1258         }
1259         if (iavf_get_vf_resource(adapter) != 0) {
1260                 PMD_INIT_LOG(ERR, "iavf_get_vf_config failed");
1261                 goto err_alloc;
1262         }
1263         /* Allocate memort for RSS info */
1264         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1265                 vf->rss_key = rte_zmalloc("rss_key",
1266                                           vf->vf_res->rss_key_size, 0);
1267                 if (!vf->rss_key) {
1268                         PMD_INIT_LOG(ERR, "unable to allocate rss_key memory");
1269                         goto err_rss;
1270                 }
1271                 vf->rss_lut = rte_zmalloc("rss_lut",
1272                                           vf->vf_res->rss_lut_size, 0);
1273                 if (!vf->rss_lut) {
1274                         PMD_INIT_LOG(ERR, "unable to allocate rss_lut memory");
1275                         goto err_rss;
1276                 }
1277         }
1278
1279         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
1280                 if (iavf_get_supported_rxdid(adapter) != 0) {
1281                         PMD_INIT_LOG(ERR, "failed to do get supported rxdid");
1282                         goto err_rss;
1283                 }
1284         }
1285
1286         return 0;
1287 err_rss:
1288         rte_free(vf->rss_key);
1289         rte_free(vf->rss_lut);
1290 err_alloc:
1291         rte_free(vf->vf_res);
1292         vf->vsi_res = NULL;
1293 err_api:
1294         rte_free(vf->aq_resp);
1295 err_aq:
1296         iavf_shutdown_adminq(hw);
1297 err:
1298         return -1;
1299 }
1300
1301 /* Enable default admin queue interrupt setting */
1302 static inline void
1303 iavf_enable_irq0(struct iavf_hw *hw)
1304 {
1305         /* Enable admin queue interrupt trigger */
1306         IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1,
1307                        IAVF_VFINT_ICR0_ENA1_ADMINQ_MASK);
1308
1309         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1310                        IAVF_VFINT_DYN_CTL01_INTENA_MASK |
1311                        IAVF_VFINT_DYN_CTL01_CLEARPBA_MASK |
1312                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1313
1314         IAVF_WRITE_FLUSH(hw);
1315 }
1316
1317 static inline void
1318 iavf_disable_irq0(struct iavf_hw *hw)
1319 {
1320         /* Disable all interrupt types */
1321         IAVF_WRITE_REG(hw, IAVF_VFINT_ICR0_ENA1, 0);
1322         IAVF_WRITE_REG(hw, IAVF_VFINT_DYN_CTL01,
1323                        IAVF_VFINT_DYN_CTL01_ITR_INDX_MASK);
1324         IAVF_WRITE_FLUSH(hw);
1325 }
1326
1327 static void
1328 iavf_dev_interrupt_handler(void *param)
1329 {
1330         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1331         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1332
1333         iavf_disable_irq0(hw);
1334
1335         iavf_handle_virtchnl_msg(dev);
1336
1337         iavf_enable_irq0(hw);
1338 }
1339
1340 static int
1341 iavf_dev_filter_ctrl(struct rte_eth_dev *dev,
1342                      enum rte_filter_type filter_type,
1343                      enum rte_filter_op filter_op,
1344                      void *arg)
1345 {
1346         int ret = 0;
1347
1348         if (!dev)
1349                 return -EINVAL;
1350
1351         switch (filter_type) {
1352         case RTE_ETH_FILTER_GENERIC:
1353                 if (filter_op != RTE_ETH_FILTER_GET)
1354                         return -EINVAL;
1355                 *(const void **)arg = &iavf_flow_ops;
1356                 break;
1357         default:
1358                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
1359                             filter_type);
1360                 ret = -EINVAL;
1361                 break;
1362         }
1363
1364         return ret;
1365 }
1366
1367
1368 static int
1369 iavf_dev_init(struct rte_eth_dev *eth_dev)
1370 {
1371         struct iavf_adapter *adapter =
1372                 IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1373         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
1374         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1375         int ret = 0;
1376
1377         PMD_INIT_FUNC_TRACE();
1378
1379         /* assign ops func pointer */
1380         eth_dev->dev_ops = &iavf_eth_dev_ops;
1381         eth_dev->rx_pkt_burst = &iavf_recv_pkts;
1382         eth_dev->tx_pkt_burst = &iavf_xmit_pkts;
1383         eth_dev->tx_pkt_prepare = &iavf_prep_pkts;
1384
1385         /* For secondary processes, we don't initialise any further as primary
1386          * has already done this work. Only check if we need a different RX
1387          * and TX function.
1388          */
1389         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1390                 iavf_set_rx_function(eth_dev);
1391                 iavf_set_tx_function(eth_dev);
1392                 return 0;
1393         }
1394         rte_eth_copy_pci_info(eth_dev, pci_dev);
1395
1396         hw->vendor_id = pci_dev->id.vendor_id;
1397         hw->device_id = pci_dev->id.device_id;
1398         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1399         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1400         hw->bus.bus_id = pci_dev->addr.bus;
1401         hw->bus.device = pci_dev->addr.devid;
1402         hw->bus.func = pci_dev->addr.function;
1403         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1404         hw->back = IAVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1405         adapter->eth_dev = eth_dev;
1406         adapter->stopped = 1;
1407
1408         /* Pass the information to the rte_eth_dev_close() that it should also
1409          * release the private port resources.
1410          */
1411         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1412
1413         if (iavf_init_vf(eth_dev) != 0) {
1414                 PMD_INIT_LOG(ERR, "Init vf failed");
1415                 return -1;
1416         }
1417
1418         /* set default ptype table */
1419         adapter->ptype_tbl = iavf_get_default_ptype_table();
1420
1421         /* copy mac addr */
1422         eth_dev->data->mac_addrs = rte_zmalloc(
1423                 "iavf_mac", RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX, 0);
1424         if (!eth_dev->data->mac_addrs) {
1425                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
1426                              " store MAC addresses",
1427                              RTE_ETHER_ADDR_LEN * IAVF_NUM_MACADDR_MAX);
1428                 return -ENOMEM;
1429         }
1430         /* If the MAC address is not configured by host,
1431          * generate a random one.
1432          */
1433         if (!rte_is_valid_assigned_ether_addr(
1434                         (struct rte_ether_addr *)hw->mac.addr))
1435                 rte_eth_random_addr(hw->mac.addr);
1436         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.addr,
1437                         &eth_dev->data->mac_addrs[0]);
1438
1439         /* register callback func to eal lib */
1440         rte_intr_callback_register(&pci_dev->intr_handle,
1441                                    iavf_dev_interrupt_handler,
1442                                    (void *)eth_dev);
1443
1444         /* enable uio intr after callback register */
1445         rte_intr_enable(&pci_dev->intr_handle);
1446
1447         /* configure and enable device interrupt */
1448         iavf_enable_irq0(hw);
1449
1450         ret = iavf_flow_init(adapter);
1451         if (ret) {
1452                 PMD_INIT_LOG(ERR, "Failed to initialize flow");
1453                 return ret;
1454         }
1455
1456         return 0;
1457 }
1458
1459 static void
1460 iavf_dev_close(struct rte_eth_dev *dev)
1461 {
1462         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1463         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1464         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1465         struct iavf_adapter *adapter =
1466                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1467         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1468
1469         iavf_dev_stop(dev);
1470         iavf_flow_flush(dev, NULL);
1471         iavf_flow_uninit(adapter);
1472         iavf_shutdown_adminq(hw);
1473         /* disable uio intr before callback unregister */
1474         rte_intr_disable(intr_handle);
1475
1476         /* unregister callback func from eal lib */
1477         rte_intr_callback_unregister(intr_handle,
1478                                      iavf_dev_interrupt_handler, dev);
1479         iavf_disable_irq0(hw);
1480
1481         dev->dev_ops = NULL;
1482         dev->rx_pkt_burst = NULL;
1483         dev->tx_pkt_burst = NULL;
1484
1485         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
1486                 if (vf->rss_lut) {
1487                         rte_free(vf->rss_lut);
1488                         vf->rss_lut = NULL;
1489                 }
1490                 if (vf->rss_key) {
1491                         rte_free(vf->rss_key);
1492                         vf->rss_key = NULL;
1493                 }
1494         }
1495
1496         rte_free(vf->vf_res);
1497         vf->vsi_res = NULL;
1498         vf->vf_res = NULL;
1499
1500         rte_free(vf->aq_resp);
1501         vf->aq_resp = NULL;
1502 }
1503
1504 static int
1505 iavf_dev_uninit(struct rte_eth_dev *dev)
1506 {
1507         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1508                 return -EPERM;
1509
1510         iavf_dev_close(dev);
1511
1512         return 0;
1513 }
1514
1515 /*
1516  * Reset VF device only to re-initialize resources in PMD layer
1517  */
1518 static int
1519 iavf_dev_reset(struct rte_eth_dev *dev)
1520 {
1521         int ret;
1522
1523         ret = iavf_dev_uninit(dev);
1524         if (ret)
1525                 return ret;
1526
1527         return iavf_dev_init(dev);
1528 }
1529
1530 static int
1531 iavf_dcf_cap_check_handler(__rte_unused const char *key,
1532                            const char *value, __rte_unused void *opaque)
1533 {
1534         if (strcmp(value, "dcf"))
1535                 return -1;
1536
1537         return 0;
1538 }
1539
1540 static int
1541 iavf_dcf_cap_selected(struct rte_devargs *devargs)
1542 {
1543         struct rte_kvargs *kvlist;
1544         const char *key = "cap";
1545         int ret = 0;
1546
1547         if (devargs == NULL)
1548                 return 0;
1549
1550         kvlist = rte_kvargs_parse(devargs->args, NULL);
1551         if (kvlist == NULL)
1552                 return 0;
1553
1554         if (!rte_kvargs_count(kvlist, key))
1555                 goto exit;
1556
1557         /* dcf capability selected when there's a key-value pair: cap=dcf */
1558         if (rte_kvargs_process(kvlist, key,
1559                                iavf_dcf_cap_check_handler, NULL) < 0)
1560                 goto exit;
1561
1562         ret = 1;
1563
1564 exit:
1565         rte_kvargs_free(kvlist);
1566         return ret;
1567 }
1568
1569 static int eth_iavf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1570                              struct rte_pci_device *pci_dev)
1571 {
1572         if (iavf_dcf_cap_selected(pci_dev->device.devargs))
1573                 return 1;
1574
1575         return rte_eth_dev_pci_generic_probe(pci_dev,
1576                 sizeof(struct iavf_adapter), iavf_dev_init);
1577 }
1578
1579 static int eth_iavf_pci_remove(struct rte_pci_device *pci_dev)
1580 {
1581         return rte_eth_dev_pci_generic_remove(pci_dev, iavf_dev_uninit);
1582 }
1583
1584 /* Adaptive virtual function driver struct */
1585 static struct rte_pci_driver rte_iavf_pmd = {
1586         .id_table = pci_id_iavf_map,
1587         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1588         .probe = eth_iavf_pci_probe,
1589         .remove = eth_iavf_pci_remove,
1590 };
1591
1592 RTE_PMD_REGISTER_PCI(net_iavf, rte_iavf_pmd);
1593 RTE_PMD_REGISTER_PCI_TABLE(net_iavf, pci_id_iavf_map);
1594 RTE_PMD_REGISTER_KMOD_DEP(net_iavf, "* igb_uio | vfio-pci");
1595 RTE_PMD_REGISTER_PARAM_STRING(net_iavf, "cap=dcf");
1596 RTE_LOG_REGISTER(iavf_logtype_init, pmd.net.iavf.init, NOTICE);
1597 RTE_LOG_REGISTER(iavf_logtype_driver, pmd.net.iavf.driver, NOTICE);
1598 #ifdef RTE_LIBRTE_IAVF_DEBUG_RX
1599 RTE_LOG_REGISTER(iavf_logtype_rx, pmd.net.iavf.rx, DEBUG);
1600 #endif
1601 #ifdef RTE_LIBRTE_IAVF_DEBUG_TX
1602 RTE_LOG_REGISTER(iavf_logtype_tx, pmd.net.iavf.tx, DEBUG);
1603 #endif
1604 #ifdef RTE_LIBRTE_IAVF_DEBUG_TX_FREE
1605 RTE_LOG_REGISTER(iavf_logtype_tx_free, pmd.net.iavf.tx_free, DEBUG);
1606 #endif