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