net/avf: enable bulk allocate Rx
[dpdk.git] / drivers / net / avf / avf_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.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 "avf_log.h"
29 #include "base/avf_prototype.h"
30 #include "base/avf_adminq_cmd.h"
31 #include "base/avf_type.h"
32
33 #include "avf.h"
34 #include "avf_rxtx.h"
35
36 static int avf_dev_configure(struct rte_eth_dev *dev);
37 static int avf_dev_start(struct rte_eth_dev *dev);
38 static void avf_dev_stop(struct rte_eth_dev *dev);
39 static void avf_dev_close(struct rte_eth_dev *dev);
40 static void avf_dev_info_get(struct rte_eth_dev *dev,
41                              struct rte_eth_dev_info *dev_info);
42 static const uint32_t *avf_dev_supported_ptypes_get(struct rte_eth_dev *dev);
43 static int avf_dev_stats_get(struct rte_eth_dev *dev,
44                              struct rte_eth_stats *stats);
45 static void avf_dev_promiscuous_enable(struct rte_eth_dev *dev);
46 static void avf_dev_promiscuous_disable(struct rte_eth_dev *dev);
47 static void avf_dev_allmulticast_enable(struct rte_eth_dev *dev);
48 static void avf_dev_allmulticast_disable(struct rte_eth_dev *dev);
49 static int avf_dev_add_mac_addr(struct rte_eth_dev *dev,
50                                 struct ether_addr *addr,
51                                 uint32_t index,
52                                 uint32_t pool);
53 static void avf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index);
54 static int avf_dev_vlan_filter_set(struct rte_eth_dev *dev,
55                                    uint16_t vlan_id, int on);
56 static int avf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
57 static int avf_dev_rss_reta_update(struct rte_eth_dev *dev,
58                                    struct rte_eth_rss_reta_entry64 *reta_conf,
59                                    uint16_t reta_size);
60 static int avf_dev_rss_reta_query(struct rte_eth_dev *dev,
61                                   struct rte_eth_rss_reta_entry64 *reta_conf,
62                                   uint16_t reta_size);
63 static int avf_dev_rss_hash_update(struct rte_eth_dev *dev,
64                                    struct rte_eth_rss_conf *rss_conf);
65 static int avf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
66                                      struct rte_eth_rss_conf *rss_conf);
67 static int avf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
68 static void avf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
69                                          struct ether_addr *mac_addr);
70
71 int avf_logtype_init;
72 int avf_logtype_driver;
73 static const struct rte_pci_id pci_id_avf_map[] = {
74         { RTE_PCI_DEVICE(AVF_INTEL_VENDOR_ID, AVF_DEV_ID_ADAPTIVE_VF) },
75         { .vendor_id = 0, /* sentinel */ },
76 };
77
78 static const struct eth_dev_ops avf_eth_dev_ops = {
79         .dev_configure              = avf_dev_configure,
80         .dev_start                  = avf_dev_start,
81         .dev_stop                   = avf_dev_stop,
82         .dev_close                  = avf_dev_close,
83         .dev_infos_get              = avf_dev_info_get,
84         .dev_supported_ptypes_get   = avf_dev_supported_ptypes_get,
85         .link_update                = avf_dev_link_update,
86         .stats_get                  = avf_dev_stats_get,
87         .promiscuous_enable         = avf_dev_promiscuous_enable,
88         .promiscuous_disable        = avf_dev_promiscuous_disable,
89         .allmulticast_enable        = avf_dev_allmulticast_enable,
90         .allmulticast_disable       = avf_dev_allmulticast_disable,
91         .mac_addr_add               = avf_dev_add_mac_addr,
92         .mac_addr_remove            = avf_dev_del_mac_addr,
93         .vlan_filter_set            = avf_dev_vlan_filter_set,
94         .vlan_offload_set           = avf_dev_vlan_offload_set,
95         .rx_queue_start             = avf_dev_rx_queue_start,
96         .rx_queue_stop              = avf_dev_rx_queue_stop,
97         .tx_queue_start             = avf_dev_tx_queue_start,
98         .tx_queue_stop              = avf_dev_tx_queue_stop,
99         .rx_queue_setup             = avf_dev_rx_queue_setup,
100         .rx_queue_release           = avf_dev_rx_queue_release,
101         .tx_queue_setup             = avf_dev_tx_queue_setup,
102         .tx_queue_release           = avf_dev_tx_queue_release,
103         .mac_addr_set               = avf_dev_set_default_mac_addr,
104         .reta_update                = avf_dev_rss_reta_update,
105         .reta_query                 = avf_dev_rss_reta_query,
106         .rss_hash_update            = avf_dev_rss_hash_update,
107         .rss_hash_conf_get          = avf_dev_rss_hash_conf_get,
108         .rxq_info_get               = avf_dev_rxq_info_get,
109         .txq_info_get               = avf_dev_txq_info_get,
110         .rx_queue_count             = avf_dev_rxq_count,
111         .rx_descriptor_status       = avf_dev_rx_desc_status,
112         .tx_descriptor_status       = avf_dev_tx_desc_status,
113         .mtu_set                    = avf_dev_mtu_set,
114 };
115
116 static int
117 avf_dev_configure(struct rte_eth_dev *dev)
118 {
119         struct avf_adapter *ad =
120                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
121         struct avf_info *vf =  AVF_DEV_PRIVATE_TO_VF(ad);
122         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
123
124         ad->rx_bulk_alloc_allowed = true;
125 #ifdef RTE_LIBRTE_AVF_INC_VECTOR
126         /* Initialize to TRUE. If any of Rx queues doesn't meet the
127          * vector Rx/Tx preconditions, it will be reset.
128          */
129         ad->rx_vec_allowed = true;
130         ad->tx_vec_allowed = true;
131 #else
132         ad->rx_vec_allowed = false;
133         ad->tx_vec_allowed = false;
134 #endif
135
136         /* Vlan stripping setting */
137         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN) {
138                 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
139                         avf_enable_vlan_strip(ad);
140                 else
141                         avf_disable_vlan_strip(ad);
142         }
143         return 0;
144 }
145
146 static int
147 avf_init_rss(struct avf_adapter *adapter)
148 {
149         struct avf_info *vf =  AVF_DEV_PRIVATE_TO_VF(adapter);
150         struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(adapter);
151         struct rte_eth_rss_conf *rss_conf;
152         uint8_t i, j, nb_q;
153         int ret;
154
155         rss_conf = &adapter->eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
156         nb_q = RTE_MIN(adapter->eth_dev->data->nb_rx_queues,
157                        AVF_MAX_NUM_QUEUES);
158
159         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF)) {
160                 PMD_DRV_LOG(DEBUG, "RSS is not supported");
161                 return -ENOTSUP;
162         }
163         if (adapter->eth_dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_RSS) {
164                 PMD_DRV_LOG(WARNING, "RSS is enabled by PF by default");
165                 /* set all lut items to default queue */
166                 for (i = 0; i < vf->vf_res->rss_lut_size; i++)
167                         vf->rss_lut[i] = 0;
168                 ret = avf_configure_rss_lut(adapter);
169                 return ret;
170         }
171
172         /* In AVF, RSS enablement is set by PF driver. It is not supported
173          * to set based on rss_conf->rss_hf.
174          */
175
176         /* configure RSS key */
177         if (!rss_conf->rss_key) {
178                 /* Calculate the default hash key */
179                 for (i = 0; i <= vf->vf_res->rss_key_size; i++)
180                         vf->rss_key[i] = (uint8_t)rte_rand();
181         } else
182                 rte_memcpy(vf->rss_key, rss_conf->rss_key,
183                            RTE_MIN(rss_conf->rss_key_len,
184                                    vf->vf_res->rss_key_size));
185
186         /* init RSS LUT table */
187         for (i = 0; i < vf->vf_res->rss_lut_size; i++, j++) {
188                 if (j >= nb_q)
189                         j = 0;
190                 vf->rss_lut[i] = j;
191         }
192         /* send virtchnnl ops to configure rss*/
193         ret = avf_configure_rss_lut(adapter);
194         if (ret)
195                 return ret;
196         ret = avf_configure_rss_key(adapter);
197         if (ret)
198                 return ret;
199
200         return 0;
201 }
202
203 static int
204 avf_init_rxq(struct rte_eth_dev *dev, struct avf_rx_queue *rxq)
205 {
206         struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
207         struct rte_eth_dev_data *dev_data = dev->data;
208         uint16_t buf_size, max_pkt_len, len;
209
210         buf_size = rte_pktmbuf_data_room_size(rxq->mp) - RTE_PKTMBUF_HEADROOM;
211
212         /* Calculate the maximum packet length allowed */
213         len = rxq->rx_buf_len * AVF_MAX_CHAINED_RX_BUFFERS;
214         max_pkt_len = RTE_MIN(len, dev->data->dev_conf.rxmode.max_rx_pkt_len);
215
216         /* Check if the jumbo frame and maximum packet length are set
217          * correctly.
218          */
219         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
220                 if (max_pkt_len <= ETHER_MAX_LEN ||
221                     max_pkt_len > AVF_FRAME_SIZE_MAX) {
222                         PMD_DRV_LOG(ERR, "maximum packet length must be "
223                                     "larger than %u and smaller than %u, "
224                                     "as jumbo frame is enabled",
225                                     (uint32_t)ETHER_MAX_LEN,
226                                     (uint32_t)AVF_FRAME_SIZE_MAX);
227                         return -EINVAL;
228                 }
229         } else {
230                 if (max_pkt_len < ETHER_MIN_LEN ||
231                     max_pkt_len > ETHER_MAX_LEN) {
232                         PMD_DRV_LOG(ERR, "maximum packet length must be "
233                                     "larger than %u and smaller than %u, "
234                                     "as jumbo frame is disabled",
235                                     (uint32_t)ETHER_MIN_LEN,
236                                     (uint32_t)ETHER_MAX_LEN);
237                         return -EINVAL;
238                 }
239         }
240
241         rxq->max_pkt_len = max_pkt_len;
242         if ((dev_data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) ||
243             (rxq->max_pkt_len + 2 * AVF_VLAN_TAG_SIZE) > buf_size) {
244                 dev_data->scattered_rx = 1;
245         }
246         AVF_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
247         AVF_WRITE_FLUSH(hw);
248
249         return 0;
250 }
251
252 static int
253 avf_init_queues(struct rte_eth_dev *dev)
254 {
255         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
256         struct avf_rx_queue **rxq =
257                 (struct avf_rx_queue **)dev->data->rx_queues;
258         struct avf_tx_queue **txq =
259                 (struct avf_tx_queue **)dev->data->tx_queues;
260         int i, ret = AVF_SUCCESS;
261
262         for (i = 0; i < dev->data->nb_rx_queues; i++) {
263                 if (!rxq[i] || !rxq[i]->q_set)
264                         continue;
265                 ret = avf_init_rxq(dev, rxq[i]);
266                 if (ret != AVF_SUCCESS)
267                         break;
268         }
269         /* set rx/tx function to vector/scatter/single-segment
270          * according to parameters
271          */
272         avf_set_rx_function(dev);
273         avf_set_tx_function(dev);
274
275         return ret;
276 }
277
278 static int
279 avf_start_queues(struct rte_eth_dev *dev)
280 {
281         struct avf_rx_queue *rxq;
282         struct avf_tx_queue *txq;
283         int i;
284
285         for (i = 0; i < dev->data->nb_tx_queues; i++) {
286                 txq = dev->data->tx_queues[i];
287                 if (txq->tx_deferred_start)
288                         continue;
289                 if (avf_dev_tx_queue_start(dev, i) != 0) {
290                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
291                         return -1;
292                 }
293         }
294
295         for (i = 0; i < dev->data->nb_rx_queues; i++) {
296                 rxq = dev->data->rx_queues[i];
297                 if (rxq->rx_deferred_start)
298                         continue;
299                 if (avf_dev_rx_queue_start(dev, i) != 0) {
300                         PMD_DRV_LOG(ERR, "Fail to start queue %u", i);
301                         return -1;
302                 }
303         }
304
305         return 0;
306 }
307
308 static int
309 avf_dev_start(struct rte_eth_dev *dev)
310 {
311         struct avf_adapter *adapter =
312                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
313         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
314         struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
315         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
316         struct rte_intr_handle *intr_handle = dev->intr_handle;
317         uint16_t interval;
318         int i;
319
320         PMD_INIT_FUNC_TRACE();
321
322         hw->adapter_stopped = 0;
323
324         vf->max_pkt_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
325         vf->num_queue_pairs = RTE_MAX(dev->data->nb_rx_queues,
326                                       dev->data->nb_tx_queues);
327
328         /* TODO: Rx interrupt */
329
330         if (avf_init_queues(dev) != 0) {
331                 PMD_DRV_LOG(ERR, "failed to do Queue init");
332                 return -1;
333         }
334
335         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
336                 if (avf_init_rss(adapter) != 0) {
337                         PMD_DRV_LOG(ERR, "configure rss failed");
338                         goto err_rss;
339                 }
340         }
341
342         if (avf_configure_queues(adapter) != 0) {
343                 PMD_DRV_LOG(ERR, "configure queues failed");
344                 goto err_queue;
345         }
346
347         /* Map interrupt for writeback */
348         vf->nb_msix = 1;
349         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
350                 /* If WB_ON_ITR supports, enable it */
351                 vf->msix_base = AVF_RX_VEC_START;
352                 AVF_WRITE_REG(hw, AVFINT_DYN_CTLN1(vf->msix_base - 1),
353                               AVFINT_DYN_CTLN1_ITR_INDX_MASK |
354                               AVFINT_DYN_CTLN1_WB_ON_ITR_MASK);
355         } else {
356                 /* If no WB_ON_ITR offload flags, need to set interrupt for
357                  * descriptor write back.
358                  */
359                 vf->msix_base = AVF_MISC_VEC_ID;
360
361                 /* set ITR to max */
362                 interval = avf_calc_itr_interval(AVF_QUEUE_ITR_INTERVAL_MAX);
363                 AVF_WRITE_REG(hw, AVFINT_DYN_CTL01,
364                               AVFINT_DYN_CTL01_INTENA_MASK |
365                               (AVF_ITR_INDEX_DEFAULT <<
366                                AVFINT_DYN_CTL01_ITR_INDX_SHIFT) |
367                               (interval << AVFINT_DYN_CTL01_INTERVAL_SHIFT));
368         }
369         AVF_WRITE_FLUSH(hw);
370         /* map all queues to the same interrupt */
371         for (i = 0; i < dev->data->nb_rx_queues; i++)
372                 vf->rxq_map[0] |= 1 << i;
373         if (avf_config_irq_map(adapter)) {
374                 PMD_DRV_LOG(ERR, "config interrupt mapping failed");
375                 goto err_queue;
376         }
377
378         /* Set all mac addrs */
379         avf_add_del_all_mac_addr(adapter, TRUE);
380
381         if (avf_start_queues(dev) != 0) {
382                 PMD_DRV_LOG(ERR, "enable queues failed");
383                 goto err_mac;
384         }
385
386         /* TODO: enable interrupt for RX interrupt */
387         return 0;
388
389 err_mac:
390         avf_add_del_all_mac_addr(adapter, FALSE);
391 err_queue:
392 err_rss:
393         return -1;
394 }
395
396 static void
397 avf_dev_stop(struct rte_eth_dev *dev)
398 {
399         struct avf_adapter *adapter =
400                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
401         struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev);
402         int ret, i;
403
404         PMD_INIT_FUNC_TRACE();
405
406         if (hw->adapter_stopped == 1)
407                 return;
408
409         avf_stop_queues(dev);
410
411         /*TODO: Disable the interrupt for Rx*/
412
413         /* TODO: Rx interrupt vector mapping free */
414
415         /* remove all mac addrs */
416         avf_add_del_all_mac_addr(adapter, FALSE);
417         hw->adapter_stopped = 1;
418 }
419
420 static void
421 avf_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
422 {
423         struct avf_adapter *adapter =
424                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
425         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
426
427         memset(dev_info, 0, sizeof(*dev_info));
428         dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
429         dev_info->max_rx_queues = vf->vsi_res->num_queue_pairs;
430         dev_info->max_tx_queues = vf->vsi_res->num_queue_pairs;
431         dev_info->min_rx_bufsize = AVF_BUF_SIZE_MIN;
432         dev_info->max_rx_pktlen = AVF_FRAME_SIZE_MAX;
433         dev_info->hash_key_size = vf->vf_res->rss_key_size;
434         dev_info->reta_size = vf->vf_res->rss_lut_size;
435         dev_info->flow_type_rss_offloads = AVF_RSS_OFFLOAD_ALL;
436         dev_info->max_mac_addrs = AVF_NUM_MACADDR_MAX;
437         dev_info->rx_offload_capa =
438                 DEV_RX_OFFLOAD_VLAN_STRIP |
439                 DEV_RX_OFFLOAD_IPV4_CKSUM |
440                 DEV_RX_OFFLOAD_UDP_CKSUM |
441                 DEV_RX_OFFLOAD_TCP_CKSUM;
442         dev_info->tx_offload_capa =
443                 DEV_TX_OFFLOAD_VLAN_INSERT |
444                 DEV_TX_OFFLOAD_IPV4_CKSUM |
445                 DEV_TX_OFFLOAD_UDP_CKSUM |
446                 DEV_TX_OFFLOAD_TCP_CKSUM |
447                 DEV_TX_OFFLOAD_SCTP_CKSUM |
448                 DEV_TX_OFFLOAD_TCP_TSO;
449
450         dev_info->default_rxconf = (struct rte_eth_rxconf) {
451                 .rx_free_thresh = AVF_DEFAULT_RX_FREE_THRESH,
452                 .rx_drop_en = 0,
453         };
454
455         dev_info->default_txconf = (struct rte_eth_txconf) {
456                 .tx_free_thresh = AVF_DEFAULT_TX_FREE_THRESH,
457                 .tx_rs_thresh = AVF_DEFAULT_TX_RS_THRESH,
458                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
459                                 ETH_TXQ_FLAGS_NOOFFLOADS,
460         };
461
462         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
463                 .nb_max = AVF_MAX_RING_DESC,
464                 .nb_min = AVF_MIN_RING_DESC,
465                 .nb_align = AVF_ALIGN_RING_DESC,
466         };
467
468         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
469                 .nb_max = AVF_MAX_RING_DESC,
470                 .nb_min = AVF_MIN_RING_DESC,
471                 .nb_align = AVF_ALIGN_RING_DESC,
472         };
473 }
474
475 static const uint32_t *
476 avf_dev_supported_ptypes_get(struct rte_eth_dev *dev)
477 {
478         static const uint32_t ptypes[] = {
479                 RTE_PTYPE_L2_ETHER,
480                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
481                 RTE_PTYPE_L4_FRAG,
482                 RTE_PTYPE_L4_ICMP,
483                 RTE_PTYPE_L4_NONFRAG,
484                 RTE_PTYPE_L4_SCTP,
485                 RTE_PTYPE_L4_TCP,
486                 RTE_PTYPE_L4_UDP,
487                 RTE_PTYPE_UNKNOWN
488         };
489         return ptypes;
490 }
491
492 int
493 avf_dev_link_update(struct rte_eth_dev *dev,
494                     __rte_unused int wait_to_complete)
495 {
496         struct rte_eth_link new_link;
497         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
498
499         /* Only read status info stored in VF, and the info is updated
500          *  when receive LINK_CHANGE evnet from PF by Virtchnnl.
501          */
502         switch (vf->link_speed) {
503         case VIRTCHNL_LINK_SPEED_100MB:
504                 new_link.link_speed = ETH_SPEED_NUM_100M;
505                 break;
506         case VIRTCHNL_LINK_SPEED_1GB:
507                 new_link.link_speed = ETH_SPEED_NUM_1G;
508                 break;
509         case VIRTCHNL_LINK_SPEED_10GB:
510                 new_link.link_speed = ETH_SPEED_NUM_10G;
511                 break;
512         case VIRTCHNL_LINK_SPEED_20GB:
513                 new_link.link_speed = ETH_SPEED_NUM_20G;
514                 break;
515         case VIRTCHNL_LINK_SPEED_25GB:
516                 new_link.link_speed = ETH_SPEED_NUM_25G;
517                 break;
518         case VIRTCHNL_LINK_SPEED_40GB:
519                 new_link.link_speed = ETH_SPEED_NUM_40G;
520                 break;
521         default:
522                 new_link.link_speed = ETH_SPEED_NUM_NONE;
523                 break;
524         }
525
526         new_link.link_duplex = ETH_LINK_FULL_DUPLEX;
527         new_link.link_status = vf->link_up ? ETH_LINK_UP :
528                                              ETH_LINK_DOWN;
529         new_link.link_autoneg = !!(dev->data->dev_conf.link_speeds &
530                                 ETH_LINK_SPEED_FIXED);
531
532         rte_atomic64_cmpset((uint64_t *)&dev->data->dev_link,
533                             *(uint64_t *)&dev->data->dev_link,
534                             *(uint64_t *)&new_link);
535
536         return 0;
537 }
538
539 static void
540 avf_dev_promiscuous_enable(struct rte_eth_dev *dev)
541 {
542         struct avf_adapter *adapter =
543                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
544         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
545         int ret;
546
547         if (vf->promisc_unicast_enabled)
548                 return;
549
550         ret = avf_config_promisc(adapter, TRUE, vf->promisc_multicast_enabled);
551         if (!ret)
552                 vf->promisc_unicast_enabled = TRUE;
553 }
554
555 static void
556 avf_dev_promiscuous_disable(struct rte_eth_dev *dev)
557 {
558         struct avf_adapter *adapter =
559                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
560         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
561         int ret;
562
563         if (!vf->promisc_unicast_enabled)
564                 return;
565
566         ret = avf_config_promisc(adapter, FALSE, vf->promisc_multicast_enabled);
567         if (!ret)
568                 vf->promisc_unicast_enabled = FALSE;
569 }
570
571 static void
572 avf_dev_allmulticast_enable(struct rte_eth_dev *dev)
573 {
574         struct avf_adapter *adapter =
575                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
576         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
577         int ret;
578
579         if (vf->promisc_multicast_enabled)
580                 return;
581
582         ret = avf_config_promisc(adapter, vf->promisc_unicast_enabled, TRUE);
583         if (!ret)
584                 vf->promisc_multicast_enabled = TRUE;
585 }
586
587 static void
588 avf_dev_allmulticast_disable(struct rte_eth_dev *dev)
589 {
590         struct avf_adapter *adapter =
591                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
592         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
593         int ret;
594
595         if (!vf->promisc_multicast_enabled)
596                 return;
597
598         ret = avf_config_promisc(adapter, vf->promisc_unicast_enabled, FALSE);
599         if (!ret)
600                 vf->promisc_multicast_enabled = FALSE;
601 }
602
603 static int
604 avf_dev_add_mac_addr(struct rte_eth_dev *dev, struct ether_addr *addr,
605                      __rte_unused uint32_t index,
606                      __rte_unused uint32_t pool)
607 {
608         struct avf_adapter *adapter =
609                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
610         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
611         int err;
612
613         if (is_zero_ether_addr(addr)) {
614                 PMD_DRV_LOG(ERR, "Invalid Ethernet Address");
615                 return -EINVAL;
616         }
617
618         err = avf_add_del_eth_addr(adapter, addr, TRUE);
619         if (err) {
620                 PMD_DRV_LOG(ERR, "fail to add MAC address");
621                 return -EIO;
622         }
623
624         vf->mac_num++;
625
626         return 0;
627 }
628
629 static void
630 avf_dev_del_mac_addr(struct rte_eth_dev *dev, uint32_t index)
631 {
632         struct avf_adapter *adapter =
633                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
634         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
635         struct ether_addr *addr;
636         int err;
637
638         addr = &dev->data->mac_addrs[index];
639
640         err = avf_add_del_eth_addr(adapter, addr, FALSE);
641         if (err)
642                 PMD_DRV_LOG(ERR, "fail to delete MAC address");
643
644         vf->mac_num--;
645 }
646
647 static int
648 avf_dev_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
649 {
650         struct avf_adapter *adapter =
651                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
652         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
653         int err;
654
655         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
656                 return -ENOTSUP;
657
658         err = avf_add_del_vlan(adapter, vlan_id, on);
659         if (err)
660                 return -EIO;
661         return 0;
662 }
663
664 static int
665 avf_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
666 {
667         struct avf_adapter *adapter =
668                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
669         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
670         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
671         int err;
672
673         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
674                 return -ENOTSUP;
675
676         /* Vlan stripping setting */
677         if (mask & ETH_VLAN_STRIP_MASK) {
678                 /* Enable or disable VLAN stripping */
679                 if (dev_conf->rxmode.hw_vlan_strip)
680                         err = avf_enable_vlan_strip(adapter);
681                 else
682                         err = avf_disable_vlan_strip(adapter);
683         }
684
685         if (err)
686                 return -EIO;
687         return 0;
688 }
689
690 static int
691 avf_dev_rss_reta_update(struct rte_eth_dev *dev,
692                         struct rte_eth_rss_reta_entry64 *reta_conf,
693                         uint16_t reta_size)
694 {
695         struct avf_adapter *adapter =
696                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
697         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
698         uint8_t *lut;
699         uint16_t i, idx, shift;
700         int ret;
701
702         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
703                 return -ENOTSUP;
704
705         if (reta_size != vf->vf_res->rss_lut_size) {
706                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
707                         "(%d) doesn't match the number of hardware can "
708                         "support (%d)", reta_size, vf->vf_res->rss_lut_size);
709                 return -EINVAL;
710         }
711
712         lut = rte_zmalloc("rss_lut", reta_size, 0);
713         if (!lut) {
714                 PMD_DRV_LOG(ERR, "No memory can be allocated");
715                 return -ENOMEM;
716         }
717         /* store the old lut table temporarily */
718         rte_memcpy(lut, vf->rss_lut, reta_size);
719
720         for (i = 0; i < reta_size; i++) {
721                 idx = i / RTE_RETA_GROUP_SIZE;
722                 shift = i % RTE_RETA_GROUP_SIZE;
723                 if (reta_conf[idx].mask & (1ULL << shift))
724                         lut[i] = reta_conf[idx].reta[shift];
725         }
726
727         rte_memcpy(vf->rss_lut, lut, reta_size);
728         /* send virtchnnl ops to configure rss*/
729         ret = avf_configure_rss_lut(adapter);
730         if (ret) /* revert back */
731                 rte_memcpy(vf->rss_lut, lut, reta_size);
732         rte_free(lut);
733
734         return ret;
735 }
736
737 static int
738 avf_dev_rss_reta_query(struct rte_eth_dev *dev,
739                        struct rte_eth_rss_reta_entry64 *reta_conf,
740                        uint16_t reta_size)
741 {
742         struct avf_adapter *adapter =
743                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
744         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
745         uint16_t i, idx, shift;
746
747         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
748                 return -ENOTSUP;
749
750         if (reta_size != vf->vf_res->rss_lut_size) {
751                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
752                         "(%d) doesn't match the number of hardware can "
753                         "support (%d)", reta_size, vf->vf_res->rss_lut_size);
754                 return -EINVAL;
755         }
756
757         for (i = 0; i < reta_size; i++) {
758                 idx = i / RTE_RETA_GROUP_SIZE;
759                 shift = i % RTE_RETA_GROUP_SIZE;
760                 if (reta_conf[idx].mask & (1ULL << shift))
761                         reta_conf[idx].reta[shift] = vf->rss_lut[i];
762         }
763
764         return 0;
765 }
766
767 static int
768 avf_dev_rss_hash_update(struct rte_eth_dev *dev,
769                         struct rte_eth_rss_conf *rss_conf)
770 {
771         struct avf_adapter *adapter =
772                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
773         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
774
775         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
776                 return -ENOTSUP;
777
778         /* HENA setting, it is enabled by default, no change */
779         if (!rss_conf->rss_key || rss_conf->rss_key_len == 0) {
780                 PMD_DRV_LOG(DEBUG, "No key to be configured");
781                 return 0;
782         } else if (rss_conf->rss_key_len != vf->vf_res->rss_key_size) {
783                 PMD_DRV_LOG(ERR, "The size of hash key configured "
784                         "(%d) doesn't match the size of hardware can "
785                         "support (%d)", rss_conf->rss_key_len,
786                         vf->vf_res->rss_key_size);
787                 return -EINVAL;
788         }
789
790         rte_memcpy(vf->rss_key, rss_conf->rss_key, rss_conf->rss_key_len);
791
792         return avf_configure_rss_key(adapter);
793 }
794
795 static int
796 avf_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
797                           struct rte_eth_rss_conf *rss_conf)
798 {
799         struct avf_adapter *adapter =
800                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
801         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(adapter);
802
803         if (!(vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF))
804                 return -ENOTSUP;
805
806          /* Just set it to default value now. */
807         rss_conf->rss_hf = AVF_RSS_OFFLOAD_ALL;
808
809         if (!rss_conf->rss_key)
810                 return 0;
811
812         rss_conf->rss_key_len = vf->vf_res->rss_key_size;
813         rte_memcpy(rss_conf->rss_key, vf->rss_key, rss_conf->rss_key_len);
814
815         return 0;
816 }
817
818 static int
819 avf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
820 {
821         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
822         uint32_t frame_size = mtu + AVF_ETH_OVERHEAD;
823         int ret = 0;
824
825         if (mtu < ETHER_MIN_MTU || frame_size > AVF_FRAME_SIZE_MAX)
826                 return -EINVAL;
827
828         /* mtu setting is forbidden if port is start */
829         if (dev->data->dev_started) {
830                 PMD_DRV_LOG(ERR, "port must be stopped before configuration");
831                 return -EBUSY;
832         }
833
834         if (frame_size > ETHER_MAX_LEN)
835                 dev->data->dev_conf.rxmode.offloads |=
836                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
837         else
838                 dev->data->dev_conf.rxmode.offloads &=
839                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
840
841         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
842
843         return ret;
844 }
845
846 static void
847 avf_dev_set_default_mac_addr(struct rte_eth_dev *dev,
848                              struct ether_addr *mac_addr)
849 {
850         struct avf_adapter *adapter =
851                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
852         struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(adapter);
853         struct ether_addr *perm_addr, *old_addr;
854         int ret;
855
856         old_addr = (struct ether_addr *)hw->mac.addr;
857         perm_addr = (struct ether_addr *)hw->mac.perm_addr;
858
859         if (is_same_ether_addr(mac_addr, old_addr))
860                 return;
861
862         /* If the MAC address is configured by host, skip the setting */
863         if (is_valid_assigned_ether_addr(perm_addr))
864                 return;
865
866         ret = avf_add_del_eth_addr(adapter, old_addr, FALSE);
867         if (ret)
868                 PMD_DRV_LOG(ERR, "Fail to delete old MAC:"
869                             " %02X:%02X:%02X:%02X:%02X:%02X",
870                             old_addr->addr_bytes[0],
871                             old_addr->addr_bytes[1],
872                             old_addr->addr_bytes[2],
873                             old_addr->addr_bytes[3],
874                             old_addr->addr_bytes[4],
875                             old_addr->addr_bytes[5]);
876
877         ret = avf_add_del_eth_addr(adapter, mac_addr, TRUE);
878         if (ret)
879                 PMD_DRV_LOG(ERR, "Fail to add new MAC:"
880                             " %02X:%02X:%02X:%02X:%02X:%02X",
881                             mac_addr->addr_bytes[0],
882                             mac_addr->addr_bytes[1],
883                             mac_addr->addr_bytes[2],
884                             mac_addr->addr_bytes[3],
885                             mac_addr->addr_bytes[4],
886                             mac_addr->addr_bytes[5]);
887
888         ether_addr_copy(mac_addr, (struct ether_addr *)hw->mac.addr);
889 }
890
891 static int
892 avf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
893 {
894         struct avf_adapter *adapter =
895                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
896         struct virtchnl_eth_stats *pstats = NULL;
897         int ret;
898
899         ret = avf_query_stats(adapter, &pstats);
900         if (ret == 0) {
901                 stats->ipackets = pstats->rx_unicast + pstats->rx_multicast +
902                                                 pstats->rx_broadcast;
903                 stats->opackets = pstats->tx_broadcast + pstats->tx_multicast +
904                                                 pstats->tx_unicast;
905                 stats->imissed = pstats->rx_discards;
906                 stats->oerrors = pstats->tx_errors + pstats->tx_discards;
907                 stats->ibytes = pstats->rx_bytes;
908                 stats->obytes = pstats->tx_bytes;
909         } else {
910                 PMD_DRV_LOG(ERR, "Get statistics failed");
911         }
912         return -EIO;
913 }
914
915 static int
916 avf_check_vf_reset_done(struct avf_hw *hw)
917 {
918         int i, reset;
919
920         for (i = 0; i < AVF_RESET_WAIT_CNT; i++) {
921                 reset = AVF_READ_REG(hw, AVFGEN_RSTAT) &
922                         AVFGEN_RSTAT_VFR_STATE_MASK;
923                 reset = reset >> AVFGEN_RSTAT_VFR_STATE_SHIFT;
924                 if (reset == VIRTCHNL_VFR_VFACTIVE ||
925                     reset == VIRTCHNL_VFR_COMPLETED)
926                         break;
927                 rte_delay_ms(20);
928         }
929
930         if (i >= AVF_RESET_WAIT_CNT)
931                 return -1;
932
933         return 0;
934 }
935
936 static int
937 avf_init_vf(struct rte_eth_dev *dev)
938 {
939         int i, err, bufsz;
940         struct avf_adapter *adapter =
941                 AVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
942         struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
943         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
944
945         err = avf_set_mac_type(hw);
946         if (err) {
947                 PMD_INIT_LOG(ERR, "set_mac_type failed: %d", err);
948                 goto err;
949         }
950
951         err = avf_check_vf_reset_done(hw);
952         if (err) {
953                 PMD_INIT_LOG(ERR, "VF is still resetting");
954                 goto err;
955         }
956
957         avf_init_adminq_parameter(hw);
958         err = avf_init_adminq(hw);
959         if (err) {
960                 PMD_INIT_LOG(ERR, "init_adminq failed: %d", err);
961                 goto err;
962         }
963
964         vf->aq_resp = rte_zmalloc("vf_aq_resp", AVF_AQ_BUF_SZ, 0);
965         if (!vf->aq_resp) {
966                 PMD_INIT_LOG(ERR, "unable to allocate vf_aq_resp memory");
967                 goto err_aq;
968         }
969         if (avf_check_api_version(adapter) != 0) {
970                 PMD_INIT_LOG(ERR, "check_api version failed");
971                 goto err_api;
972         }
973
974         bufsz = sizeof(struct virtchnl_vf_resource) +
975                 (AVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource));
976         vf->vf_res = rte_zmalloc("vf_res", bufsz, 0);
977         if (!vf->vf_res) {
978                 PMD_INIT_LOG(ERR, "unable to allocate vf_res memory");
979                 goto err_api;
980         }
981         if (avf_get_vf_resource(adapter) != 0) {
982                 PMD_INIT_LOG(ERR, "avf_get_vf_config failed");
983                 goto err_alloc;
984         }
985         /* Allocate memort for RSS info */
986         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
987                 vf->rss_key = rte_zmalloc("rss_key",
988                                           vf->vf_res->rss_key_size, 0);
989                 if (!vf->rss_key) {
990                         PMD_INIT_LOG(ERR, "unable to allocate rss_key memory");
991                         goto err_rss;
992                 }
993                 vf->rss_lut = rte_zmalloc("rss_lut",
994                                           vf->vf_res->rss_lut_size, 0);
995                 if (!vf->rss_lut) {
996                         PMD_INIT_LOG(ERR, "unable to allocate rss_lut memory");
997                         goto err_rss;
998                 }
999         }
1000         return 0;
1001 err_rss:
1002         rte_free(vf->rss_key);
1003         rte_free(vf->rss_lut);
1004 err_alloc:
1005         rte_free(vf->vf_res);
1006         vf->vsi_res = NULL;
1007 err_api:
1008         rte_free(vf->aq_resp);
1009 err_aq:
1010         avf_shutdown_adminq(hw);
1011 err:
1012         return -1;
1013 }
1014
1015 /* Enable default admin queue interrupt setting */
1016 static inline void
1017 avf_enable_irq0(struct avf_hw *hw)
1018 {
1019         /* Enable admin queue interrupt trigger */
1020         AVF_WRITE_REG(hw, AVFINT_ICR0_ENA1, AVFINT_ICR0_ENA1_ADMINQ_MASK);
1021
1022         AVF_WRITE_REG(hw, AVFINT_DYN_CTL01, AVFINT_DYN_CTL01_INTENA_MASK |
1023                                             AVFINT_DYN_CTL01_ITR_INDX_MASK);
1024
1025         AVF_WRITE_FLUSH(hw);
1026 }
1027
1028 static inline void
1029 avf_disable_irq0(struct avf_hw *hw)
1030 {
1031         /* Disable all interrupt types */
1032         AVF_WRITE_REG(hw, AVFINT_ICR0_ENA1, 0);
1033         AVF_WRITE_REG(hw, AVFINT_DYN_CTL01,
1034                       AVFINT_DYN_CTL01_ITR_INDX_MASK);
1035         AVF_WRITE_FLUSH(hw);
1036 }
1037
1038 static void
1039 avf_dev_interrupt_handler(void *param)
1040 {
1041         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1042         struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1043
1044         avf_disable_irq0(hw);
1045
1046         avf_handle_virtchnl_msg(dev);
1047
1048 done:
1049         avf_enable_irq0(hw);
1050 }
1051
1052 static int
1053 avf_dev_init(struct rte_eth_dev *eth_dev)
1054 {
1055         struct avf_adapter *adapter =
1056                 AVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1057         struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(adapter);
1058         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1059
1060         PMD_INIT_FUNC_TRACE();
1061
1062         /* assign ops func pointer */
1063         eth_dev->dev_ops = &avf_eth_dev_ops;
1064         eth_dev->rx_pkt_burst = &avf_recv_pkts;
1065         eth_dev->tx_pkt_burst = &avf_xmit_pkts;
1066         eth_dev->tx_pkt_prepare = &avf_prep_pkts;
1067
1068         /* For secondary processes, we don't initialise any further as primary
1069          * has already done this work. Only check if we need a different RX
1070          * and TX function.
1071          */
1072         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1073                 avf_set_rx_function(eth_dev);
1074                 avf_set_tx_function(eth_dev);
1075                 return 0;
1076         }
1077         rte_eth_copy_pci_info(eth_dev, pci_dev);
1078
1079         hw->vendor_id = pci_dev->id.vendor_id;
1080         hw->device_id = pci_dev->id.device_id;
1081         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1082         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1083         hw->bus.bus_id = pci_dev->addr.bus;
1084         hw->bus.device = pci_dev->addr.devid;
1085         hw->bus.func = pci_dev->addr.function;
1086         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1087         hw->back = AVF_DEV_PRIVATE_TO_ADAPTER(eth_dev->data->dev_private);
1088         adapter->eth_dev = eth_dev;
1089
1090         if (avf_init_vf(eth_dev) != 0) {
1091                 PMD_INIT_LOG(ERR, "Init vf failed");
1092                 return -1;
1093         }
1094
1095         /* copy mac addr */
1096         eth_dev->data->mac_addrs = rte_zmalloc(
1097                                         "avf_mac",
1098                                         ETHER_ADDR_LEN * AVF_NUM_MACADDR_MAX,
1099                                         0);
1100         if (!eth_dev->data->mac_addrs) {
1101                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to"
1102                              " store MAC addresses",
1103                              ETHER_ADDR_LEN * AVF_NUM_MACADDR_MAX);
1104                 return -ENOMEM;
1105         }
1106         /* If the MAC address is not configured by host,
1107          * generate a random one.
1108          */
1109         if (!is_valid_assigned_ether_addr((struct ether_addr *)hw->mac.addr))
1110                 eth_random_addr(hw->mac.addr);
1111         ether_addr_copy((struct ether_addr *)hw->mac.addr,
1112                         &eth_dev->data->mac_addrs[0]);
1113
1114         /* register callback func to eal lib */
1115         rte_intr_callback_register(&pci_dev->intr_handle,
1116                                    avf_dev_interrupt_handler,
1117                                    (void *)eth_dev);
1118
1119         /* enable uio intr after callback register */
1120         rte_intr_enable(&pci_dev->intr_handle);
1121
1122         /* configure and enable device interrupt */
1123         avf_enable_irq0(hw);
1124
1125         return 0;
1126 }
1127
1128 static void
1129 avf_dev_close(struct rte_eth_dev *dev)
1130 {
1131         struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1132         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1133         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1134
1135         avf_dev_stop(dev);
1136         avf_shutdown_adminq(hw);
1137         /* disable uio intr before callback unregister */
1138         rte_intr_disable(intr_handle);
1139
1140         /* unregister callback func from eal lib */
1141         rte_intr_callback_unregister(intr_handle,
1142                                      avf_dev_interrupt_handler, dev);
1143         avf_disable_irq0(hw);
1144 }
1145
1146 static int
1147 avf_dev_uninit(struct rte_eth_dev *dev)
1148 {
1149         struct avf_info *vf = AVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1150         struct avf_hw *hw = AVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1151
1152         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1153                 return -EPERM;
1154
1155         dev->dev_ops = NULL;
1156         dev->rx_pkt_burst = NULL;
1157         dev->tx_pkt_burst = NULL;
1158         if (hw->adapter_stopped == 0)
1159                 avf_dev_close(dev);
1160
1161         rte_free(vf->vf_res);
1162         vf->vsi_res = NULL;
1163         vf->vf_res = NULL;
1164
1165         rte_free(vf->aq_resp);
1166         vf->aq_resp = NULL;
1167
1168         rte_free(dev->data->mac_addrs);
1169         dev->data->mac_addrs = NULL;
1170
1171         if (vf->rss_lut) {
1172                 rte_free(vf->rss_lut);
1173                 vf->rss_lut = NULL;
1174         }
1175         if (vf->rss_key) {
1176                 rte_free(vf->rss_key);
1177                 vf->rss_key = NULL;
1178         }
1179
1180         return 0;
1181 }
1182
1183 static int eth_avf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1184                              struct rte_pci_device *pci_dev)
1185 {
1186         return rte_eth_dev_pci_generic_probe(pci_dev,
1187                 sizeof(struct avf_adapter), avf_dev_init);
1188 }
1189
1190 static int eth_avf_pci_remove(struct rte_pci_device *pci_dev)
1191 {
1192         return rte_eth_dev_pci_generic_remove(pci_dev, avf_dev_uninit);
1193 }
1194
1195 /* Adaptive virtual function driver struct */
1196 static struct rte_pci_driver rte_avf_pmd = {
1197         .id_table = pci_id_avf_map,
1198         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
1199                      RTE_PCI_DRV_IOVA_AS_VA,
1200         .probe = eth_avf_pci_probe,
1201         .remove = eth_avf_pci_remove,
1202 };
1203
1204 RTE_PMD_REGISTER_PCI(net_avf, rte_avf_pmd);
1205 RTE_PMD_REGISTER_PCI_TABLE(net_avf, pci_id_avf_map);
1206 RTE_PMD_REGISTER_KMOD_DEP(net_avf, "* igb_uio | vfio-pci");
1207 RTE_INIT(avf_init_log);
1208 static void
1209 avf_init_log(void)
1210 {
1211         avf_logtype_init = rte_log_register("pmd.avf.init");
1212         if (avf_logtype_init >= 0)
1213                 rte_log_set_level(avf_logtype_init, RTE_LOG_NOTICE);
1214         avf_logtype_driver = rte_log_register("pmd.avf.driver");
1215         if (avf_logtype_driver >= 0)
1216                 rte_log_set_level(avf_logtype_driver, RTE_LOG_NOTICE);
1217 }
1218
1219 /* memory func for base code */
1220 enum avf_status_code
1221 avf_allocate_dma_mem_d(__rte_unused struct avf_hw *hw,
1222                        struct avf_dma_mem *mem,
1223                        u64 size,
1224                        u32 alignment)
1225 {
1226         const struct rte_memzone *mz = NULL;
1227         char z_name[RTE_MEMZONE_NAMESIZE];
1228
1229         if (!mem)
1230                 return AVF_ERR_PARAM;
1231
1232         snprintf(z_name, sizeof(z_name), "avf_dma_%"PRIu64, rte_rand());
1233         mz = rte_memzone_reserve_bounded(z_name, size, SOCKET_ID_ANY, 0,
1234                                          alignment, RTE_PGSIZE_2M);
1235         if (!mz)
1236                 return AVF_ERR_NO_MEMORY;
1237
1238         mem->size = size;
1239         mem->va = mz->addr;
1240         mem->pa = mz->phys_addr;
1241         mem->zone = (const void *)mz;
1242         PMD_DRV_LOG(DEBUG,
1243                     "memzone %s allocated with physical address: %"PRIu64,
1244                     mz->name, mem->pa);
1245
1246         return AVF_SUCCESS;
1247 }
1248
1249 enum avf_status_code
1250 avf_free_dma_mem_d(__rte_unused struct avf_hw *hw,
1251                    struct avf_dma_mem *mem)
1252 {
1253         if (!mem)
1254                 return AVF_ERR_PARAM;
1255
1256         PMD_DRV_LOG(DEBUG,
1257                     "memzone %s to be freed with physical address: %"PRIu64,
1258                     ((const struct rte_memzone *)mem->zone)->name, mem->pa);
1259         rte_memzone_free((const struct rte_memzone *)mem->zone);
1260         mem->zone = NULL;
1261         mem->va = NULL;
1262         mem->pa = (u64)0;
1263
1264         return AVF_SUCCESS;
1265 }
1266
1267 enum avf_status_code
1268 avf_allocate_virt_mem_d(__rte_unused struct avf_hw *hw,
1269                         struct avf_virt_mem *mem,
1270                         u32 size)
1271 {
1272         if (!mem)
1273                 return AVF_ERR_PARAM;
1274
1275         mem->size = size;
1276         mem->va = rte_zmalloc("avf", size, 0);
1277
1278         if (mem->va)
1279                 return AVF_SUCCESS;
1280         else
1281                 return AVF_ERR_NO_MEMORY;
1282 }
1283
1284 enum avf_status_code
1285 avf_free_virt_mem_d(__rte_unused struct avf_hw *hw,
1286                     struct avf_virt_mem *mem)
1287 {
1288         if (!mem)
1289                 return AVF_ERR_PARAM;
1290
1291         rte_free(mem->va);
1292         mem->va = NULL;
1293
1294         return AVF_SUCCESS;
1295 }