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