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