virtio: move allocation before initialization
[dpdk.git] / lib / librte_pmd_virtio / virtio_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #ifdef RTE_EXEC_ENV_LINUXAPP
40 #include <dirent.h>
41 #endif
42
43 #include <rte_ethdev.h>
44 #include <rte_memcpy.h>
45 #include <rte_string_fns.h>
46 #include <rte_memzone.h>
47 #include <rte_malloc.h>
48 #include <rte_atomic.h>
49 #include <rte_branch_prediction.h>
50 #include <rte_pci.h>
51 #include <rte_ether.h>
52 #include <rte_common.h>
53
54 #include <rte_memory.h>
55 #include <rte_eal.h>
56 #include <rte_dev.h>
57
58 #include "virtio_ethdev.h"
59 #include "virtio_pci.h"
60 #include "virtio_logs.h"
61 #include "virtqueue.h"
62
63
64 static int eth_virtio_dev_init(struct eth_driver *eth_drv,
65                 struct rte_eth_dev *eth_dev);
66 static int  virtio_dev_configure(struct rte_eth_dev *dev);
67 static int  virtio_dev_start(struct rte_eth_dev *dev);
68 static void virtio_dev_stop(struct rte_eth_dev *dev);
69 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
70 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
71 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
72 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
73 static void virtio_dev_info_get(struct rte_eth_dev *dev,
74                                 struct rte_eth_dev_info *dev_info);
75 static int virtio_dev_link_update(struct rte_eth_dev *dev,
76         __rte_unused int wait_to_complete);
77
78 static void virtio_set_hwaddr(struct virtio_hw *hw);
79 static void virtio_get_hwaddr(struct virtio_hw *hw);
80
81 static void virtio_dev_rx_queue_release(__rte_unused void *rxq);
82 static void virtio_dev_tx_queue_release(__rte_unused void *txq);
83
84 static void virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats);
85 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
86 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
87
88 static int virtio_dev_queue_stats_mapping_set(
89         __rte_unused struct rte_eth_dev *eth_dev,
90         __rte_unused uint16_t queue_id,
91         __rte_unused uint8_t stat_idx,
92         __rte_unused uint8_t is_rx);
93
94 /*
95  * The set of PCI devices this driver supports
96  */
97 static struct rte_pci_id pci_id_virtio_map[] = {
98
99 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
100 #include "rte_pci_dev_ids.h"
101
102 { .vendor_id = 0, /* sentinel */ },
103 };
104
105 static int
106 virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl,
107                 int *dlen, int pkt_num)
108 {
109         uint32_t head = vq->vq_desc_head_idx, i;
110         int k, sum = 0;
111         virtio_net_ctrl_ack status = ~0;
112         struct virtio_pmd_ctrl result;
113
114         ctrl->status = status;
115
116         if (!vq->hw->cvq) {
117                 PMD_INIT_LOG(ERR,
118                              "%s(): Control queue is not supported.",
119                              __func__);
120                 return -1;
121         }
122
123         PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
124                 "vq->hw->cvq = %p vq = %p",
125                 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
126
127         if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
128                 return -1;
129
130         memcpy(vq->virtio_net_hdr_mz->addr, ctrl,
131                 sizeof(struct virtio_pmd_ctrl));
132
133         /*
134          * Format is enforced in qemu code:
135          * One TX packet for header;
136          * At least one TX packet per argument;
137          * One RX packet for ACK.
138          */
139         vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
140         vq->vq_ring.desc[head].addr = vq->virtio_net_hdr_mz->phys_addr;
141         vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
142         vq->vq_free_cnt--;
143         i = vq->vq_ring.desc[head].next;
144
145         for (k = 0; k < pkt_num; k++) {
146                 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
147                 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
148                         + sizeof(struct virtio_net_ctrl_hdr)
149                         + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
150                 vq->vq_ring.desc[i].len = dlen[k];
151                 sum += dlen[k];
152                 vq->vq_free_cnt--;
153                 i = vq->vq_ring.desc[i].next;
154         }
155
156         vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
157         vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
158                         + sizeof(struct virtio_net_ctrl_hdr);
159         vq->vq_ring.desc[i].len = sizeof(ctrl->status);
160         vq->vq_free_cnt--;
161
162         vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
163
164         vq_update_avail_ring(vq, head);
165         vq_update_avail_idx(vq);
166
167         PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
168
169         virtqueue_notify(vq);
170
171         while (vq->vq_used_cons_idx == vq->vq_ring.used->idx)
172                 usleep(100);
173
174         while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
175                 uint32_t idx, desc_idx, used_idx;
176                 struct vring_used_elem *uep;
177
178                 virtio_rmb();
179
180                 used_idx = (uint32_t)(vq->vq_used_cons_idx
181                                 & (vq->vq_nentries - 1));
182                 uep = &vq->vq_ring.used->ring[used_idx];
183                 idx = (uint32_t) uep->id;
184                 desc_idx = idx;
185
186                 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
187                         desc_idx = vq->vq_ring.desc[desc_idx].next;
188                         vq->vq_free_cnt++;
189                 }
190
191                 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
192                 vq->vq_desc_head_idx = idx;
193
194                 vq->vq_used_cons_idx++;
195                 vq->vq_free_cnt++;
196         }
197
198         PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
199                         vq->vq_free_cnt, vq->vq_desc_head_idx);
200
201         memcpy(&result, vq->virtio_net_hdr_mz->addr,
202                         sizeof(struct virtio_pmd_ctrl));
203
204         return result.status;
205 }
206
207 static int
208 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
209 {
210         struct virtio_hw *hw = dev->data->dev_private;
211         struct virtio_pmd_ctrl ctrl;
212         int dlen[1];
213         int ret;
214
215         ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
216         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
217         memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
218
219         dlen[0] = sizeof(uint16_t);
220
221         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
222
223         if (ret) {
224                 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
225                           "failed, this is too late now...");
226                 return -EINVAL;
227         }
228
229         return 0;
230 }
231
232 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
233                         int queue_type,
234                         uint16_t queue_idx,
235                         uint8_t  vtpci_queue_idx,
236                         uint16_t nb_desc,
237                         unsigned int socket_id,
238                         struct virtqueue **pvq)
239 {
240         char vq_name[VIRTQUEUE_MAX_NAME_SZ];
241         const struct rte_memzone *mz;
242         uint16_t vq_size;
243         int size;
244         struct virtio_hw *hw = dev->data->dev_private;
245         struct virtqueue  *vq = NULL;
246
247         /* Write the virtqueue index to the Queue Select Field */
248         VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vtpci_queue_idx);
249         PMD_INIT_LOG(DEBUG, "selecting queue: %d", vtpci_queue_idx);
250
251         /*
252          * Read the virtqueue size from the Queue Size field
253          * Always power of 2 and if 0 virtqueue does not exist
254          */
255         vq_size = VIRTIO_READ_REG_2(hw, VIRTIO_PCI_QUEUE_NUM);
256         PMD_INIT_LOG(DEBUG, "vq_size: %d nb_desc:%d", vq_size, nb_desc);
257         if (nb_desc == 0)
258                 nb_desc = vq_size;
259         if (vq_size == 0) {
260                 PMD_INIT_LOG(ERR, "%s: virtqueue does not exist", __func__);
261                 return -EINVAL;
262         } else if (!rte_is_power_of_2(vq_size)) {
263                 PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2", __func__);
264                 return -EINVAL;
265         } else if (nb_desc != vq_size) {
266                 PMD_INIT_LOG(ERR, "Warning: nb_desc(%d) is not equal to vq size (%d), fall to vq size",
267                         nb_desc, vq_size);
268                 nb_desc = vq_size;
269         }
270
271         if (queue_type == VTNET_RQ) {
272                 snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
273                         dev->data->port_id, queue_idx);
274                 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
275                         vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
276         } else if (queue_type == VTNET_TQ) {
277                 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
278                         dev->data->port_id, queue_idx);
279                 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
280                         vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
281         } else if (queue_type == VTNET_CQ) {
282                 snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
283                         dev->data->port_id);
284                 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
285                         vq_size * sizeof(struct vq_desc_extra),
286                         RTE_CACHE_LINE_SIZE);
287         }
288         if (vq == NULL) {
289                 PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue", __func__);
290                 return (-ENOMEM);
291         }
292
293         vq->hw = hw;
294         vq->port_id = dev->data->port_id;
295         vq->queue_id = queue_idx;
296         vq->vq_queue_index = vtpci_queue_idx;
297         vq->vq_nentries = vq_size;
298         vq->vq_free_cnt = vq_size;
299
300         /*
301          * Reserve a memzone for vring elements
302          */
303         size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
304         vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
305         PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size);
306
307         mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
308                 socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
309         if (mz == NULL) {
310                 rte_free(vq);
311                 return -ENOMEM;
312         }
313
314         /*
315          * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
316          * and only accepts 32 bit page frame number.
317          * Check if the allocated physical memory exceeds 16TB.
318          */
319         if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
320                 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
321                 rte_free(vq);
322                 return -ENOMEM;
323         }
324
325         memset(mz->addr, 0, sizeof(mz->len));
326         vq->mz = mz;
327         vq->vq_ring_mem = mz->phys_addr;
328         vq->vq_ring_virt_mem = mz->addr;
329         PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%"PRIx64, (uint64_t)mz->phys_addr);
330         PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)mz->addr);
331         vq->virtio_net_hdr_mz  = NULL;
332         vq->virtio_net_hdr_mem = 0;
333
334         if (queue_type == VTNET_TQ) {
335                 /*
336                  * For each xmit packet, allocate a virtio_net_hdr
337                  */
338                 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
339                         dev->data->port_id, queue_idx);
340                 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
341                         vq_size * hw->vtnet_hdr_size,
342                         socket_id, 0, RTE_CACHE_LINE_SIZE);
343                 if (vq->virtio_net_hdr_mz == NULL) {
344                         rte_free(vq);
345                         return -ENOMEM;
346                 }
347                 vq->virtio_net_hdr_mem =
348                         vq->virtio_net_hdr_mz->phys_addr;
349                 memset(vq->virtio_net_hdr_mz->addr, 0,
350                         vq_size * hw->vtnet_hdr_size);
351         } else if (queue_type == VTNET_CQ) {
352                 /* Allocate a page for control vq command, data and status */
353                 snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
354                         dev->data->port_id);
355                 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
356                         PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE);
357                 if (vq->virtio_net_hdr_mz == NULL) {
358                         rte_free(vq);
359                         return -ENOMEM;
360                 }
361                 vq->virtio_net_hdr_mem =
362                         vq->virtio_net_hdr_mz->phys_addr;
363                 memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
364         }
365
366         /*
367          * Set guest physical address of the virtqueue
368          * in VIRTIO_PCI_QUEUE_PFN config register of device
369          */
370         VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
371                         mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
372         *pvq = vq;
373         return 0;
374 }
375
376 static int
377 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
378                 uint32_t socket_id)
379 {
380         struct virtqueue *vq;
381         uint16_t nb_desc = 0;
382         int ret;
383         struct virtio_hw *hw = dev->data->dev_private;
384
385         PMD_INIT_FUNC_TRACE();
386         ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
387                         vtpci_queue_idx, nb_desc, socket_id, &vq);
388
389         if (ret < 0) {
390                 PMD_INIT_LOG(ERR, "control vq initialization failed");
391                 return ret;
392         }
393
394         hw->cvq = vq;
395         return 0;
396 }
397
398 static void
399 virtio_dev_close(struct rte_eth_dev *dev)
400 {
401         PMD_INIT_LOG(DEBUG, "virtio_dev_close");
402
403         virtio_dev_stop(dev);
404 }
405
406 static void
407 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
408 {
409         struct virtio_hw *hw = dev->data->dev_private;
410         struct virtio_pmd_ctrl ctrl;
411         int dlen[1];
412         int ret;
413
414         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
415         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
416         ctrl.data[0] = 1;
417         dlen[0] = 1;
418
419         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
420
421         if (ret)
422                 PMD_INIT_LOG(ERR, "Failed to enable promisc");
423 }
424
425 static void
426 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
427 {
428         struct virtio_hw *hw = dev->data->dev_private;
429         struct virtio_pmd_ctrl ctrl;
430         int dlen[1];
431         int ret;
432
433         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
434         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
435         ctrl.data[0] = 0;
436         dlen[0] = 1;
437
438         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
439
440         if (ret)
441                 PMD_INIT_LOG(ERR, "Failed to disable promisc");
442 }
443
444 static void
445 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
446 {
447         struct virtio_hw *hw = dev->data->dev_private;
448         struct virtio_pmd_ctrl ctrl;
449         int dlen[1];
450         int ret;
451
452         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
453         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
454         ctrl.data[0] = 1;
455         dlen[0] = 1;
456
457         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
458
459         if (ret)
460                 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
461 }
462
463 static void
464 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
465 {
466         struct virtio_hw *hw = dev->data->dev_private;
467         struct virtio_pmd_ctrl ctrl;
468         int dlen[1];
469         int ret;
470
471         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
472         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
473         ctrl.data[0] = 0;
474         dlen[0] = 1;
475
476         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
477
478         if (ret)
479                 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
480 }
481
482 /*
483  * dev_ops for virtio, bare necessities for basic operation
484  */
485 static struct eth_dev_ops virtio_eth_dev_ops = {
486         .dev_configure           = virtio_dev_configure,
487         .dev_start               = virtio_dev_start,
488         .dev_stop                = virtio_dev_stop,
489         .dev_close               = virtio_dev_close,
490         .promiscuous_enable      = virtio_dev_promiscuous_enable,
491         .promiscuous_disable     = virtio_dev_promiscuous_disable,
492         .allmulticast_enable     = virtio_dev_allmulticast_enable,
493         .allmulticast_disable    = virtio_dev_allmulticast_disable,
494
495         .dev_infos_get           = virtio_dev_info_get,
496         .stats_get               = virtio_dev_stats_get,
497         .stats_reset             = virtio_dev_stats_reset,
498         .link_update             = virtio_dev_link_update,
499         .mac_addr_add            = NULL,
500         .mac_addr_remove         = NULL,
501         .rx_queue_setup          = virtio_dev_rx_queue_setup,
502         /* meaningfull only to multiple queue */
503         .rx_queue_release        = virtio_dev_rx_queue_release,
504         .tx_queue_setup          = virtio_dev_tx_queue_setup,
505         /* meaningfull only to multiple queue */
506         .tx_queue_release        = virtio_dev_tx_queue_release,
507         /* collect stats per queue */
508         .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
509 };
510
511 static inline int
512 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
513                                 struct rte_eth_link *link)
514 {
515         struct rte_eth_link *dst = link;
516         struct rte_eth_link *src = &(dev->data->dev_link);
517
518         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
519                         *(uint64_t *)src) == 0)
520                 return -1;
521
522         return 0;
523 }
524
525 /**
526  * Atomically writes the link status information into global
527  * structure rte_eth_dev.
528  *
529  * @param dev
530  *   - Pointer to the structure rte_eth_dev to read from.
531  *   - Pointer to the buffer to be saved with the link status.
532  *
533  * @return
534  *   - On success, zero.
535  *   - On failure, negative value.
536  */
537 static inline int
538 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
539                 struct rte_eth_link *link)
540 {
541         struct rte_eth_link *dst = &(dev->data->dev_link);
542         struct rte_eth_link *src = link;
543
544         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
545                                         *(uint64_t *)src) == 0)
546                 return -1;
547
548         return 0;
549 }
550
551 static void
552 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
553 {
554         unsigned i;
555
556         for (i = 0; i < dev->data->nb_tx_queues; i++) {
557                 const struct virtqueue *txvq = dev->data->tx_queues[i];
558                 if (txvq == NULL)
559                         continue;
560
561                 stats->opackets += txvq->packets;
562                 stats->obytes += txvq->bytes;
563                 stats->oerrors += txvq->errors;
564
565                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
566                         stats->q_opackets[i] = txvq->packets;
567                         stats->q_obytes[i] = txvq->bytes;
568                 }
569         }
570
571         for (i = 0; i < dev->data->nb_rx_queues; i++) {
572                 const struct virtqueue *rxvq = dev->data->rx_queues[i];
573                 if (rxvq == NULL)
574                         continue;
575
576                 stats->ipackets += rxvq->packets;
577                 stats->ibytes += rxvq->bytes;
578                 stats->ierrors += rxvq->errors;
579
580                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
581                         stats->q_ipackets[i] = rxvq->packets;
582                         stats->q_ibytes[i] = rxvq->bytes;
583                 }
584         }
585
586         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
587 }
588
589 static void
590 virtio_dev_stats_reset(struct rte_eth_dev *dev)
591 {
592         unsigned int i;
593
594         for (i = 0; i < dev->data->nb_tx_queues; i++) {
595                 struct virtqueue *txvq = dev->data->tx_queues[i];
596                 if (txvq == NULL)
597                         continue;
598
599                 txvq->packets = 0;
600                 txvq->bytes = 0;
601                 txvq->errors = 0;
602         }
603
604         for (i = 0; i < dev->data->nb_rx_queues; i++) {
605                 struct virtqueue *rxvq = dev->data->rx_queues[i];
606                 if (rxvq == NULL)
607                         continue;
608
609                 rxvq->packets = 0;
610                 rxvq->bytes = 0;
611                 rxvq->errors = 0;
612         }
613
614         dev->data->rx_mbuf_alloc_failed = 0;
615 }
616
617 static void
618 virtio_set_hwaddr(struct virtio_hw *hw)
619 {
620         vtpci_write_dev_config(hw,
621                         offsetof(struct virtio_net_config, mac),
622                         &hw->mac_addr, ETHER_ADDR_LEN);
623 }
624
625 static void
626 virtio_get_hwaddr(struct virtio_hw *hw)
627 {
628         if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
629                 vtpci_read_dev_config(hw,
630                         offsetof(struct virtio_net_config, mac),
631                         &hw->mac_addr, ETHER_ADDR_LEN);
632         } else {
633                 eth_random_addr(&hw->mac_addr[0]);
634                 virtio_set_hwaddr(hw);
635         }
636 }
637
638
639 static void
640 virtio_negotiate_features(struct virtio_hw *hw)
641 {
642         uint32_t host_features, mask;
643
644         mask = VIRTIO_NET_F_CTRL_VLAN;
645         mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
646
647         /* TSO and LRO are only available when their corresponding
648          * checksum offload feature is also negotiated.
649          */
650         mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_ECN;
651         mask |= VIRTIO_NET_F_GUEST_TSO4 | VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN;
652         mask |= VTNET_LRO_FEATURES;
653
654         /* not negotiating INDIRECT descriptor table support */
655         mask |= VIRTIO_RING_F_INDIRECT_DESC;
656
657         /* Prepare guest_features: feature that driver wants to support */
658         hw->guest_features = VTNET_FEATURES & ~mask;
659         PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %x",
660                 hw->guest_features);
661
662         /* Read device(host) feature bits */
663         host_features = VIRTIO_READ_REG_4(hw, VIRTIO_PCI_HOST_FEATURES);
664         PMD_INIT_LOG(DEBUG, "host_features before negotiate = %x",
665                 host_features);
666
667         /*
668          * Negotiate features: Subset of device feature bits are written back
669          * guest feature bits.
670          */
671         hw->guest_features = vtpci_negotiate_features(hw, host_features);
672         PMD_INIT_LOG(DEBUG, "features after negotiate = %x",
673                 hw->guest_features);
674 }
675
676 #ifdef RTE_EXEC_ENV_LINUXAPP
677 static int
678 parse_sysfs_value(const char *filename, unsigned long *val)
679 {
680         FILE *f;
681         char buf[BUFSIZ];
682         char *end = NULL;
683
684         f = fopen(filename, "r");
685         if (f == NULL) {
686                 PMD_INIT_LOG(ERR, "%s(): cannot open sysfs value %s",
687                              __func__, filename);
688                 return -1;
689         }
690
691         if (fgets(buf, sizeof(buf), f) == NULL) {
692                 PMD_INIT_LOG(ERR, "%s(): cannot read sysfs value %s",
693                              __func__, filename);
694                 fclose(f);
695                 return -1;
696         }
697         *val = strtoul(buf, &end, 0);
698         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
699                 PMD_INIT_LOG(ERR, "%s(): cannot parse sysfs value %s",
700                              __func__, filename);
701                 fclose(f);
702                 return -1;
703         }
704         fclose(f);
705         return 0;
706 }
707
708 static int get_uio_dev(struct rte_pci_addr *loc, char *buf, unsigned int buflen)
709 {
710         unsigned int uio_num;
711         struct dirent *e;
712         DIR *dir;
713         char dirname[PATH_MAX];
714
715         /* depending on kernel version, uio can be located in uio/uioX
716          * or uio:uioX */
717         snprintf(dirname, sizeof(dirname),
718                      SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
719                      loc->domain, loc->bus, loc->devid, loc->function);
720         dir = opendir(dirname);
721         if (dir == NULL) {
722                 /* retry with the parent directory */
723                 snprintf(dirname, sizeof(dirname),
724                              SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
725                              loc->domain, loc->bus, loc->devid, loc->function);
726                 dir = opendir(dirname);
727
728                 if (dir == NULL) {
729                         PMD_INIT_LOG(ERR, "Cannot opendir %s", dirname);
730                         return -1;
731                 }
732         }
733
734         /* take the first file starting with "uio" */
735         while ((e = readdir(dir)) != NULL) {
736                 /* format could be uio%d ...*/
737                 int shortprefix_len = sizeof("uio") - 1;
738                 /* ... or uio:uio%d */
739                 int longprefix_len = sizeof("uio:uio") - 1;
740                 char *endptr;
741
742                 if (strncmp(e->d_name, "uio", 3) != 0)
743                         continue;
744
745                 /* first try uio%d */
746                 errno = 0;
747                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
748                 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
749                         snprintf(buf, buflen, "%s/uio%u", dirname, uio_num);
750                         break;
751                 }
752
753                 /* then try uio:uio%d */
754                 errno = 0;
755                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
756                 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
757                         snprintf(buf, buflen, "%s/uio:uio%u", dirname,
758                                      uio_num);
759                         break;
760                 }
761         }
762         closedir(dir);
763
764         /* No uio resource found */
765         if (e == NULL) {
766                 PMD_INIT_LOG(ERR, "Could not find uio resource");
767                 return -1;
768         }
769
770         return 0;
771 }
772
773 static int
774 virtio_has_msix(const struct rte_pci_addr *loc)
775 {
776         DIR *d;
777         char dirname[PATH_MAX];
778
779         snprintf(dirname, sizeof(dirname),
780                      SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/msi_irqs",
781                      loc->domain, loc->bus, loc->devid, loc->function);
782
783         d = opendir(dirname);
784         if (d)
785                 closedir(d);
786
787         return (d != NULL);
788 }
789
790 /* Extract I/O port numbers from sysfs */
791 static int virtio_resource_init(struct rte_pci_device *pci_dev)
792 {
793         char dirname[PATH_MAX];
794         char filename[PATH_MAX];
795         unsigned long start, size;
796
797         if (get_uio_dev(&pci_dev->addr, dirname, sizeof(dirname)) < 0)
798                 return -1;
799
800         /* get portio size */
801         snprintf(filename, sizeof(filename),
802                      "%s/portio/port0/size", dirname);
803         if (parse_sysfs_value(filename, &size) < 0) {
804                 PMD_INIT_LOG(ERR, "%s(): cannot parse size",
805                              __func__);
806                 return -1;
807         }
808
809         /* get portio start */
810         snprintf(filename, sizeof(filename),
811                  "%s/portio/port0/start", dirname);
812         if (parse_sysfs_value(filename, &start) < 0) {
813                 PMD_INIT_LOG(ERR, "%s(): cannot parse portio start",
814                              __func__);
815                 return -1;
816         }
817         pci_dev->mem_resource[0].addr = (void *)(uintptr_t)start;
818         pci_dev->mem_resource[0].len =  (uint64_t)size;
819         PMD_INIT_LOG(DEBUG,
820                      "PCI Port IO found start=0x%lx with size=0x%lx",
821                      start, size);
822         return 0;
823 }
824 #else
825 static int
826 virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
827 {
828         /* nic_uio does not enable interrupts, return 0 (false). */
829         return 0;
830 }
831
832 static int virtio_resource_init(struct rte_pci_device *pci_dev __rte_unused)
833 {
834         /* no setup required */
835         return 0;
836 }
837 #endif
838
839 /*
840  * Process Virtio Config changed interrupt and call the callback
841  * if link state changed.
842  */
843 static void
844 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
845                          void *param)
846 {
847         struct rte_eth_dev *dev = param;
848         struct virtio_hw *hw = dev->data->dev_private;
849         uint8_t isr;
850
851         /* Read interrupt status which clears interrupt */
852         isr = vtpci_isr(hw);
853         PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
854
855         if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
856                 PMD_DRV_LOG(ERR, "interrupt enable failed");
857
858         if (isr & VIRTIO_PCI_ISR_CONFIG) {
859                 if (virtio_dev_link_update(dev, 0) == 0)
860                         _rte_eth_dev_callback_process(dev,
861                                                       RTE_ETH_EVENT_INTR_LSC);
862         }
863
864 }
865
866 /*
867  * This function is based on probe() function in virtio_pci.c
868  * It returns 0 on success.
869  */
870 static int
871 eth_virtio_dev_init(__rte_unused struct eth_driver *eth_drv,
872                 struct rte_eth_dev *eth_dev)
873 {
874         struct virtio_hw *hw = eth_dev->data->dev_private;
875         struct virtio_net_config *config;
876         struct virtio_net_config local_config;
877         uint32_t offset_conf = sizeof(config->mac);
878         struct rte_pci_device *pci_dev;
879
880         RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
881
882         eth_dev->dev_ops = &virtio_eth_dev_ops;
883         eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
884
885         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
886                 return 0;
887
888         /* Allocate memory for storing MAC addresses */
889         eth_dev->data->mac_addrs = rte_zmalloc("virtio", ETHER_ADDR_LEN, 0);
890         if (eth_dev->data->mac_addrs == NULL) {
891                 PMD_INIT_LOG(ERR,
892                         "Failed to allocate %d bytes needed to store MAC addresses",
893                         ETHER_ADDR_LEN);
894                 return -ENOMEM;
895         }
896
897         pci_dev = eth_dev->pci_dev;
898         if (virtio_resource_init(pci_dev) < 0)
899                 return -1;
900
901         hw->use_msix = virtio_has_msix(&pci_dev->addr);
902         hw->io_base = (uint32_t)(uintptr_t)pci_dev->mem_resource[0].addr;
903
904         /* Reset the device although not necessary at startup */
905         vtpci_reset(hw);
906
907         /* Tell the host we've noticed this device. */
908         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
909
910         /* Tell the host we've known how to drive the device. */
911         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
912         virtio_negotiate_features(hw);
913
914         /* Setting up rx_header size for the device */
915         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
916                 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
917                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
918         } else {
919                 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
920                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
921         }
922
923         /* Copy the permanent MAC address to: virtio_hw */
924         virtio_get_hwaddr(hw);
925         ether_addr_copy((struct ether_addr *) hw->mac_addr,
926                         &eth_dev->data->mac_addrs[0]);
927         PMD_INIT_LOG(DEBUG,
928                      "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
929                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
930                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
931
932         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
933                 config = &local_config;
934
935                 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
936                         offset_conf += sizeof(config->status);
937                 } else {
938                         PMD_INIT_LOG(DEBUG,
939                                      "VIRTIO_NET_F_STATUS is not supported");
940                         config->status = 0;
941                 }
942
943                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
944                         offset_conf += sizeof(config->max_virtqueue_pairs);
945                 } else {
946                         PMD_INIT_LOG(DEBUG,
947                                      "VIRTIO_NET_F_MQ is not supported");
948                         config->max_virtqueue_pairs = 1;
949                 }
950
951                 vtpci_read_dev_config(hw, 0, (uint8_t *)config, offset_conf);
952
953                 hw->max_rx_queues =
954                         (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
955                         VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
956                 hw->max_tx_queues =
957                         (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
958                         VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
959
960                 virtio_dev_cq_queue_setup(eth_dev,
961                                         config->max_virtqueue_pairs * 2,
962                                         SOCKET_ID_ANY);
963
964                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
965                                 config->max_virtqueue_pairs);
966                 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
967                 PMD_INIT_LOG(DEBUG,
968                                 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
969                                 config->mac[0], config->mac[1],
970                                 config->mac[2], config->mac[3],
971                                 config->mac[4], config->mac[5]);
972         } else {
973                 hw->max_rx_queues = 1;
974                 hw->max_tx_queues = 1;
975         }
976
977         eth_dev->data->nb_rx_queues = hw->max_rx_queues;
978         eth_dev->data->nb_tx_queues = hw->max_tx_queues;
979
980         PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d   hw->max_tx_queues=%d",
981                         hw->max_rx_queues, hw->max_tx_queues);
982         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
983                         eth_dev->data->port_id, pci_dev->id.vendor_id,
984                         pci_dev->id.device_id);
985
986         /* Setup interrupt callback  */
987         rte_intr_callback_register(&pci_dev->intr_handle,
988                                    virtio_interrupt_handler, eth_dev);
989         return 0;
990 }
991
992 static struct eth_driver rte_virtio_pmd = {
993         {
994                 .name = "rte_virtio_pmd",
995                 .id_table = pci_id_virtio_map,
996                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
997         },
998         .eth_dev_init = eth_virtio_dev_init,
999         .dev_private_size = sizeof(struct virtio_hw),
1000 };
1001
1002 /*
1003  * Driver initialization routine.
1004  * Invoked once at EAL init time.
1005  * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1006  * Returns 0 on success.
1007  */
1008 static int
1009 rte_virtio_pmd_init(const char *name __rte_unused,
1010                     const char *param __rte_unused)
1011 {
1012         if (rte_eal_iopl_init() != 0) {
1013                 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1014                 return -1;
1015         }
1016
1017         rte_eth_driver_register(&rte_virtio_pmd);
1018         return 0;
1019 }
1020
1021 /*
1022  * Only 1 queue is supported, no queue release related operation
1023  */
1024 static void
1025 virtio_dev_rx_queue_release(__rte_unused void *rxq)
1026 {
1027 }
1028
1029 static void
1030 virtio_dev_tx_queue_release(__rte_unused void *txq)
1031 {
1032 }
1033
1034 /*
1035  * Configure virtio device
1036  * It returns 0 on success.
1037  */
1038 static int
1039 virtio_dev_configure(struct rte_eth_dev *dev)
1040 {
1041         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1042         struct virtio_hw *hw = dev->data->dev_private;
1043         int ret;
1044
1045         PMD_INIT_LOG(DEBUG, "configure");
1046
1047         if (rxmode->hw_ip_checksum) {
1048                 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1049                 return (-EINVAL);
1050         }
1051
1052         hw->vlan_strip = rxmode->hw_vlan_strip;
1053
1054         ret = vtpci_irq_config(hw, 0);
1055         if (ret != 0)
1056                 PMD_DRV_LOG(ERR, "failed to set config vector");
1057
1058         return ret;
1059 }
1060
1061
1062 static int
1063 virtio_dev_start(struct rte_eth_dev *dev)
1064 {
1065         uint16_t nb_queues, i;
1066         struct virtio_hw *hw = dev->data->dev_private;
1067
1068         /* Tell the host we've noticed this device. */
1069         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1070
1071         /* Tell the host we've known how to drive the device. */
1072         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1073
1074         virtio_dev_cq_start(dev);
1075
1076         /* Do final configuration before rx/tx engine starts */
1077         virtio_dev_rxtx_start(dev);
1078
1079         /* check if lsc interrupt feature is enabled */
1080         if (dev->data->dev_conf.intr_conf.lsc) {
1081                 if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1082                         PMD_DRV_LOG(ERR, "link status not supported by host");
1083                         return -ENOTSUP;
1084                 }
1085
1086                 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1087                         PMD_DRV_LOG(ERR, "interrupt enable failed");
1088                         return -EIO;
1089                 }
1090         }
1091
1092         /* Initialize Link state */
1093         virtio_dev_link_update(dev, 0);
1094
1095         vtpci_reinit_complete(hw);
1096
1097         /*Notify the backend
1098          *Otherwise the tap backend might already stop its queue due to fullness.
1099          *vhost backend will have no chance to be waked up
1100          */
1101         nb_queues = dev->data->nb_rx_queues;
1102         if (nb_queues > 1) {
1103                 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1104                         return -EINVAL;
1105         }
1106
1107         PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1108
1109         for (i = 0; i < nb_queues; i++)
1110                 virtqueue_notify(dev->data->rx_queues[i]);
1111
1112         PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1113
1114         for (i = 0; i < dev->data->nb_rx_queues; i++)
1115                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1116
1117         for (i = 0; i < dev->data->nb_tx_queues; i++)
1118                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1119
1120         return 0;
1121 }
1122
1123 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1124 {
1125         struct rte_mbuf *buf;
1126         int i, mbuf_num = 0;
1127
1128         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1129                 PMD_INIT_LOG(DEBUG,
1130                              "Before freeing rxq[%d] used and unused buf", i);
1131                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1132
1133                 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1134                                         dev->data->rx_queues[i])) != NULL) {
1135                         rte_pktmbuf_free(buf);
1136                         mbuf_num++;
1137                 }
1138
1139                 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1140                 PMD_INIT_LOG(DEBUG,
1141                              "After freeing rxq[%d] used and unused buf", i);
1142                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1143         }
1144
1145         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1146                 PMD_INIT_LOG(DEBUG,
1147                              "Before freeing txq[%d] used and unused bufs",
1148                              i);
1149                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1150
1151                 mbuf_num = 0;
1152                 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1153                                         dev->data->tx_queues[i])) != NULL) {
1154                         rte_pktmbuf_free(buf);
1155
1156                         mbuf_num++;
1157                 }
1158
1159                 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1160                 PMD_INIT_LOG(DEBUG,
1161                              "After freeing txq[%d] used and unused buf", i);
1162                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1163         }
1164 }
1165
1166 /*
1167  * Stop device: disable rx and tx functions to allow for reconfiguring.
1168  */
1169 static void
1170 virtio_dev_stop(struct rte_eth_dev *dev)
1171 {
1172         struct virtio_hw *hw = dev->data->dev_private;
1173
1174         /* reset the NIC */
1175         vtpci_irq_config(hw, 0);
1176         vtpci_reset(hw);
1177         virtio_dev_free_mbufs(dev);
1178 }
1179
1180 static int
1181 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1182 {
1183         struct rte_eth_link link, old;
1184         uint16_t status;
1185         struct virtio_hw *hw = dev->data->dev_private;
1186         memset(&link, 0, sizeof(link));
1187         virtio_dev_atomic_read_link_status(dev, &link);
1188         old = link;
1189         link.link_duplex = FULL_DUPLEX;
1190         link.link_speed  = SPEED_10G;
1191
1192         if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1193                 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1194                 vtpci_read_dev_config(hw,
1195                                 offsetof(struct virtio_net_config, status),
1196                                 &status, sizeof(status));
1197                 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1198                         link.link_status = 0;
1199                         PMD_INIT_LOG(DEBUG, "Port %d is down",
1200                                      dev->data->port_id);
1201                 } else {
1202                         link.link_status = 1;
1203                         PMD_INIT_LOG(DEBUG, "Port %d is up",
1204                                      dev->data->port_id);
1205                 }
1206         } else {
1207                 link.link_status = 1;   /* Link up */
1208         }
1209         virtio_dev_atomic_write_link_status(dev, &link);
1210
1211         return (old.link_status == link.link_status) ? -1 : 0;
1212 }
1213
1214 static void
1215 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1216 {
1217         struct virtio_hw *hw = dev->data->dev_private;
1218
1219         dev_info->driver_name = dev->driver->pci_drv.name;
1220         dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1221         dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1222         dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1223         dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1224         dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1225 }
1226
1227 /*
1228  * It enables testpmd to collect per queue stats.
1229  */
1230 static int
1231 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1232 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1233 __rte_unused uint8_t is_rx)
1234 {
1235         return 0;
1236 }
1237
1238 static struct rte_driver rte_virtio_driver = {
1239         .type = PMD_PDEV,
1240         .init = rte_virtio_pmd_init,
1241 };
1242
1243 PMD_REGISTER_DRIVER(rte_virtio_driver);