virtio: remove unnecessary adapter structure
[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_alignment = VIRTIO_PCI_VRING_ALIGN;
298         vq->vq_nentries = vq_size;
299         vq->vq_free_cnt = vq_size;
300
301         /*
302          * Reserve a memzone for vring elements
303          */
304         size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
305         vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
306         PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size);
307
308         mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
309                 socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
310         if (mz == NULL) {
311                 rte_free(vq);
312                 return -ENOMEM;
313         }
314
315         /*
316          * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
317          * and only accepts 32 bit page frame number.
318          * Check if the allocated physical memory exceeds 16TB.
319          */
320         if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
321                 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
322                 rte_free(vq);
323                 return -ENOMEM;
324         }
325
326         memset(mz->addr, 0, sizeof(mz->len));
327         vq->mz = mz;
328         vq->vq_ring_mem = mz->phys_addr;
329         vq->vq_ring_virt_mem = mz->addr;
330         PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem:      0x%"PRIx64, (uint64_t)mz->phys_addr);
331         PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)mz->addr);
332         vq->virtio_net_hdr_mz  = NULL;
333         vq->virtio_net_hdr_mem = 0;
334
335         if (queue_type == VTNET_TQ) {
336                 /*
337                  * For each xmit packet, allocate a virtio_net_hdr
338                  */
339                 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
340                         dev->data->port_id, queue_idx);
341                 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
342                         vq_size * hw->vtnet_hdr_size,
343                         socket_id, 0, RTE_CACHE_LINE_SIZE);
344                 if (vq->virtio_net_hdr_mz == NULL) {
345                         rte_free(vq);
346                         return -ENOMEM;
347                 }
348                 vq->virtio_net_hdr_mem =
349                         vq->virtio_net_hdr_mz->phys_addr;
350                 memset(vq->virtio_net_hdr_mz->addr, 0,
351                         vq_size * hw->vtnet_hdr_size);
352         } else if (queue_type == VTNET_CQ) {
353                 /* Allocate a page for control vq command, data and status */
354                 snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
355                         dev->data->port_id);
356                 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
357                         PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE);
358                 if (vq->virtio_net_hdr_mz == NULL) {
359                         rte_free(vq);
360                         return -ENOMEM;
361                 }
362                 vq->virtio_net_hdr_mem =
363                         vq->virtio_net_hdr_mz->phys_addr;
364                 memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
365         }
366
367         /*
368          * Set guest physical address of the virtqueue
369          * in VIRTIO_PCI_QUEUE_PFN config register of device
370          */
371         VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
372                         mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
373         *pvq = vq;
374         return 0;
375 }
376
377 static int
378 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
379                 uint32_t socket_id)
380 {
381         struct virtqueue *vq;
382         uint16_t nb_desc = 0;
383         int ret;
384         struct virtio_hw *hw = dev->data->dev_private;
385
386         PMD_INIT_FUNC_TRACE();
387         ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
388                         vtpci_queue_idx, nb_desc, socket_id, &vq);
389
390         if (ret < 0) {
391                 PMD_INIT_LOG(ERR, "control vq initialization failed");
392                 return ret;
393         }
394
395         hw->cvq = vq;
396         return 0;
397 }
398
399 static void
400 virtio_dev_close(struct rte_eth_dev *dev)
401 {
402         PMD_INIT_LOG(DEBUG, "virtio_dev_close");
403
404         virtio_dev_stop(dev);
405 }
406
407 static void
408 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
409 {
410         struct virtio_hw *hw = dev->data->dev_private;
411         struct virtio_pmd_ctrl ctrl;
412         int dlen[1];
413         int ret;
414
415         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
416         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
417         ctrl.data[0] = 1;
418         dlen[0] = 1;
419
420         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
421
422         if (ret)
423                 PMD_INIT_LOG(ERR, "Failed to enable promisc");
424 }
425
426 static void
427 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
428 {
429         struct virtio_hw *hw = dev->data->dev_private;
430         struct virtio_pmd_ctrl ctrl;
431         int dlen[1];
432         int ret;
433
434         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
435         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
436         ctrl.data[0] = 0;
437         dlen[0] = 1;
438
439         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
440
441         if (ret)
442                 PMD_INIT_LOG(ERR, "Failed to disable promisc");
443 }
444
445 static void
446 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
447 {
448         struct virtio_hw *hw = dev->data->dev_private;
449         struct virtio_pmd_ctrl ctrl;
450         int dlen[1];
451         int ret;
452
453         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
454         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
455         ctrl.data[0] = 1;
456         dlen[0] = 1;
457
458         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
459
460         if (ret)
461                 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
462 }
463
464 static void
465 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
466 {
467         struct virtio_hw *hw = dev->data->dev_private;
468         struct virtio_pmd_ctrl ctrl;
469         int dlen[1];
470         int ret;
471
472         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
473         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
474         ctrl.data[0] = 0;
475         dlen[0] = 1;
476
477         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
478
479         if (ret)
480                 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
481 }
482
483 /*
484  * dev_ops for virtio, bare necessities for basic operation
485  */
486 static struct eth_dev_ops virtio_eth_dev_ops = {
487         .dev_configure           = virtio_dev_configure,
488         .dev_start               = virtio_dev_start,
489         .dev_stop                = virtio_dev_stop,
490         .dev_close               = virtio_dev_close,
491         .promiscuous_enable      = virtio_dev_promiscuous_enable,
492         .promiscuous_disable     = virtio_dev_promiscuous_disable,
493         .allmulticast_enable     = virtio_dev_allmulticast_enable,
494         .allmulticast_disable    = virtio_dev_allmulticast_disable,
495
496         .dev_infos_get           = virtio_dev_info_get,
497         .stats_get               = virtio_dev_stats_get,
498         .stats_reset             = virtio_dev_stats_reset,
499         .link_update             = virtio_dev_link_update,
500         .mac_addr_add            = NULL,
501         .mac_addr_remove         = NULL,
502         .rx_queue_setup          = virtio_dev_rx_queue_setup,
503         /* meaningfull only to multiple queue */
504         .rx_queue_release        = virtio_dev_rx_queue_release,
505         .tx_queue_setup          = virtio_dev_tx_queue_setup,
506         /* meaningfull only to multiple queue */
507         .tx_queue_release        = virtio_dev_tx_queue_release,
508         /* collect stats per queue */
509         .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
510 };
511
512 static inline int
513 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
514                                 struct rte_eth_link *link)
515 {
516         struct rte_eth_link *dst = link;
517         struct rte_eth_link *src = &(dev->data->dev_link);
518
519         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
520                         *(uint64_t *)src) == 0)
521                 return -1;
522
523         return 0;
524 }
525
526 /**
527  * Atomically writes the link status information into global
528  * structure rte_eth_dev.
529  *
530  * @param dev
531  *   - Pointer to the structure rte_eth_dev to read from.
532  *   - Pointer to the buffer to be saved with the link status.
533  *
534  * @return
535  *   - On success, zero.
536  *   - On failure, negative value.
537  */
538 static inline int
539 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
540                 struct rte_eth_link *link)
541 {
542         struct rte_eth_link *dst = &(dev->data->dev_link);
543         struct rte_eth_link *src = link;
544
545         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
546                                         *(uint64_t *)src) == 0)
547                 return -1;
548
549         return 0;
550 }
551
552 static void
553 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
554 {
555         unsigned i;
556
557         for (i = 0; i < dev->data->nb_tx_queues; i++) {
558                 const struct virtqueue *txvq = dev->data->tx_queues[i];
559                 if (txvq == NULL)
560                         continue;
561
562                 stats->opackets += txvq->packets;
563                 stats->obytes += txvq->bytes;
564                 stats->oerrors += txvq->errors;
565
566                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
567                         stats->q_opackets[i] = txvq->packets;
568                         stats->q_obytes[i] = txvq->bytes;
569                 }
570         }
571
572         for (i = 0; i < dev->data->nb_rx_queues; i++) {
573                 const struct virtqueue *rxvq = dev->data->rx_queues[i];
574                 if (rxvq == NULL)
575                         continue;
576
577                 stats->ipackets += rxvq->packets;
578                 stats->ibytes += rxvq->bytes;
579                 stats->ierrors += rxvq->errors;
580
581                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
582                         stats->q_ipackets[i] = rxvq->packets;
583                         stats->q_ibytes[i] = rxvq->bytes;
584                 }
585         }
586
587         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
588 }
589
590 static void
591 virtio_dev_stats_reset(struct rte_eth_dev *dev)
592 {
593         unsigned int i;
594
595         for (i = 0; i < dev->data->nb_tx_queues; i++) {
596                 struct virtqueue *txvq = dev->data->tx_queues[i];
597                 if (txvq == NULL)
598                         continue;
599
600                 txvq->packets = 0;
601                 txvq->bytes = 0;
602                 txvq->errors = 0;
603         }
604
605         for (i = 0; i < dev->data->nb_rx_queues; i++) {
606                 struct virtqueue *rxvq = dev->data->rx_queues[i];
607                 if (rxvq == NULL)
608                         continue;
609
610                 rxvq->packets = 0;
611                 rxvq->bytes = 0;
612                 rxvq->errors = 0;
613         }
614
615         dev->data->rx_mbuf_alloc_failed = 0;
616 }
617
618 static void
619 virtio_set_hwaddr(struct virtio_hw *hw)
620 {
621         vtpci_write_dev_config(hw,
622                         offsetof(struct virtio_net_config, mac),
623                         &hw->mac_addr, ETHER_ADDR_LEN);
624 }
625
626 static void
627 virtio_get_hwaddr(struct virtio_hw *hw)
628 {
629         if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
630                 vtpci_read_dev_config(hw,
631                         offsetof(struct virtio_net_config, mac),
632                         &hw->mac_addr, ETHER_ADDR_LEN);
633         } else {
634                 eth_random_addr(&hw->mac_addr[0]);
635                 virtio_set_hwaddr(hw);
636         }
637 }
638
639
640 static void
641 virtio_negotiate_features(struct virtio_hw *hw)
642 {
643         uint32_t host_features, mask;
644
645         mask = VIRTIO_NET_F_CTRL_VLAN;
646         mask |= VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
647
648         /* TSO and LRO are only available when their corresponding
649          * checksum offload feature is also negotiated.
650          */
651         mask |= VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_ECN;
652         mask |= VIRTIO_NET_F_GUEST_TSO4 | VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN;
653         mask |= VTNET_LRO_FEATURES;
654
655         /* not negotiating INDIRECT descriptor table support */
656         mask |= VIRTIO_RING_F_INDIRECT_DESC;
657
658         /* Prepare guest_features: feature that driver wants to support */
659         hw->guest_features = VTNET_FEATURES & ~mask;
660         PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %x",
661                 hw->guest_features);
662
663         /* Read device(host) feature bits */
664         host_features = VIRTIO_READ_REG_4(hw, VIRTIO_PCI_HOST_FEATURES);
665         PMD_INIT_LOG(DEBUG, "host_features before negotiate = %x",
666                 host_features);
667
668         /*
669          * Negotiate features: Subset of device feature bits are written back
670          * guest feature bits.
671          */
672         hw->guest_features = vtpci_negotiate_features(hw, host_features);
673         PMD_INIT_LOG(DEBUG, "features after negotiate = %x",
674                 hw->guest_features);
675 }
676
677 #ifdef RTE_EXEC_ENV_LINUXAPP
678 static int
679 parse_sysfs_value(const char *filename, unsigned long *val)
680 {
681         FILE *f;
682         char buf[BUFSIZ];
683         char *end = NULL;
684
685         f = fopen(filename, "r");
686         if (f == NULL) {
687                 PMD_INIT_LOG(ERR, "%s(): cannot open sysfs value %s",
688                              __func__, filename);
689                 return -1;
690         }
691
692         if (fgets(buf, sizeof(buf), f) == NULL) {
693                 PMD_INIT_LOG(ERR, "%s(): cannot read sysfs value %s",
694                              __func__, filename);
695                 fclose(f);
696                 return -1;
697         }
698         *val = strtoul(buf, &end, 0);
699         if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
700                 PMD_INIT_LOG(ERR, "%s(): cannot parse sysfs value %s",
701                              __func__, filename);
702                 fclose(f);
703                 return -1;
704         }
705         fclose(f);
706         return 0;
707 }
708
709 static int get_uio_dev(struct rte_pci_addr *loc, char *buf, unsigned int buflen)
710 {
711         unsigned int uio_num;
712         struct dirent *e;
713         DIR *dir;
714         char dirname[PATH_MAX];
715
716         /* depending on kernel version, uio can be located in uio/uioX
717          * or uio:uioX */
718         snprintf(dirname, sizeof(dirname),
719                      SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
720                      loc->domain, loc->bus, loc->devid, loc->function);
721         dir = opendir(dirname);
722         if (dir == NULL) {
723                 /* retry with the parent directory */
724                 snprintf(dirname, sizeof(dirname),
725                              SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
726                              loc->domain, loc->bus, loc->devid, loc->function);
727                 dir = opendir(dirname);
728
729                 if (dir == NULL) {
730                         PMD_INIT_LOG(ERR, "Cannot opendir %s", dirname);
731                         return -1;
732                 }
733         }
734
735         /* take the first file starting with "uio" */
736         while ((e = readdir(dir)) != NULL) {
737                 /* format could be uio%d ...*/
738                 int shortprefix_len = sizeof("uio") - 1;
739                 /* ... or uio:uio%d */
740                 int longprefix_len = sizeof("uio:uio") - 1;
741                 char *endptr;
742
743                 if (strncmp(e->d_name, "uio", 3) != 0)
744                         continue;
745
746                 /* first try uio%d */
747                 errno = 0;
748                 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
749                 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
750                         snprintf(buf, buflen, "%s/uio%u", dirname, uio_num);
751                         break;
752                 }
753
754                 /* then try uio:uio%d */
755                 errno = 0;
756                 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
757                 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
758                         snprintf(buf, buflen, "%s/uio:uio%u", dirname,
759                                      uio_num);
760                         break;
761                 }
762         }
763         closedir(dir);
764
765         /* No uio resource found */
766         if (e == NULL) {
767                 PMD_INIT_LOG(ERR, "Could not find uio resource");
768                 return -1;
769         }
770
771         return 0;
772 }
773
774 static int
775 virtio_has_msix(const struct rte_pci_addr *loc)
776 {
777         DIR *d;
778         char dirname[PATH_MAX];
779
780         snprintf(dirname, sizeof(dirname),
781                      SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/msi_irqs",
782                      loc->domain, loc->bus, loc->devid, loc->function);
783
784         d = opendir(dirname);
785         if (d)
786                 closedir(d);
787
788         return (d != NULL);
789 }
790
791 /* Extract I/O port numbers from sysfs */
792 static int virtio_resource_init(struct rte_pci_device *pci_dev)
793 {
794         char dirname[PATH_MAX];
795         char filename[PATH_MAX];
796         unsigned long start, size;
797
798         if (get_uio_dev(&pci_dev->addr, dirname, sizeof(dirname)) < 0)
799                 return -1;
800
801         /* get portio size */
802         snprintf(filename, sizeof(filename),
803                      "%s/portio/port0/size", dirname);
804         if (parse_sysfs_value(filename, &size) < 0) {
805                 PMD_INIT_LOG(ERR, "%s(): cannot parse size",
806                              __func__);
807                 return -1;
808         }
809
810         /* get portio start */
811         snprintf(filename, sizeof(filename),
812                  "%s/portio/port0/start", dirname);
813         if (parse_sysfs_value(filename, &start) < 0) {
814                 PMD_INIT_LOG(ERR, "%s(): cannot parse portio start",
815                              __func__);
816                 return -1;
817         }
818         pci_dev->mem_resource[0].addr = (void *)(uintptr_t)start;
819         pci_dev->mem_resource[0].len =  (uint64_t)size;
820         PMD_INIT_LOG(DEBUG,
821                      "PCI Port IO found start=0x%lx with size=0x%lx",
822                      start, size);
823         return 0;
824 }
825 #else
826 static int
827 virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
828 {
829         /* nic_uio does not enable interrupts, return 0 (false). */
830         return 0;
831 }
832
833 static int virtio_resource_init(struct rte_pci_device *pci_dev __rte_unused)
834 {
835         /* no setup required */
836         return 0;
837 }
838 #endif
839
840 /*
841  * Process Virtio Config changed interrupt and call the callback
842  * if link state changed.
843  */
844 static void
845 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
846                          void *param)
847 {
848         struct rte_eth_dev *dev = param;
849         struct virtio_hw *hw = dev->data->dev_private;
850         uint8_t isr;
851
852         /* Read interrupt status which clears interrupt */
853         isr = vtpci_isr(hw);
854         PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
855
856         if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
857                 PMD_DRV_LOG(ERR, "interrupt enable failed");
858
859         if (isr & VIRTIO_PCI_ISR_CONFIG) {
860                 if (virtio_dev_link_update(dev, 0) == 0)
861                         _rte_eth_dev_callback_process(dev,
862                                                       RTE_ETH_EVENT_INTR_LSC);
863         }
864
865 }
866
867 /*
868  * This function is based on probe() function in virtio_pci.c
869  * It returns 0 on success.
870  */
871 static int
872 eth_virtio_dev_init(__rte_unused struct eth_driver *eth_drv,
873                 struct rte_eth_dev *eth_dev)
874 {
875         struct virtio_hw *hw = eth_dev->data->dev_private;
876         struct virtio_net_config *config;
877         struct virtio_net_config local_config;
878         uint32_t offset_conf = sizeof(config->mac);
879         struct rte_pci_device *pci_dev;
880
881         RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
882
883         eth_dev->dev_ops = &virtio_eth_dev_ops;
884         eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
885
886         if (rte_eal_process_type() == RTE_PROC_SECONDARY)
887                 return 0;
888
889         pci_dev = eth_dev->pci_dev;
890         if (virtio_resource_init(pci_dev) < 0)
891                 return -1;
892
893         hw->use_msix = virtio_has_msix(&pci_dev->addr);
894         hw->io_base = (uint32_t)(uintptr_t)pci_dev->mem_resource[0].addr;
895
896         /* Reset the device although not necessary at startup */
897         vtpci_reset(hw);
898
899         /* Tell the host we've noticed this device. */
900         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
901
902         /* Tell the host we've known how to drive the device. */
903         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
904         virtio_negotiate_features(hw);
905
906         /* Setting up rx_header size for the device */
907         if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
908                 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
909                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
910         } else {
911                 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
912                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
913         }
914
915         /* Allocate memory for storing MAC addresses */
916         eth_dev->data->mac_addrs = rte_zmalloc("virtio", ETHER_ADDR_LEN, 0);
917         if (eth_dev->data->mac_addrs == NULL) {
918                 PMD_INIT_LOG(ERR,
919                         "Failed to allocate %d bytes needed to store MAC addresses",
920                         ETHER_ADDR_LEN);
921                 return -ENOMEM;
922         }
923
924         /* Copy the permanent MAC address to: virtio_hw */
925         virtio_get_hwaddr(hw);
926         ether_addr_copy((struct ether_addr *) hw->mac_addr,
927                         &eth_dev->data->mac_addrs[0]);
928         PMD_INIT_LOG(DEBUG,
929                      "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
930                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
931                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
932
933         if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
934                 config = &local_config;
935
936                 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
937                         offset_conf += sizeof(config->status);
938                 } else {
939                         PMD_INIT_LOG(DEBUG,
940                                      "VIRTIO_NET_F_STATUS is not supported");
941                         config->status = 0;
942                 }
943
944                 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
945                         offset_conf += sizeof(config->max_virtqueue_pairs);
946                 } else {
947                         PMD_INIT_LOG(DEBUG,
948                                      "VIRTIO_NET_F_MQ is not supported");
949                         config->max_virtqueue_pairs = 1;
950                 }
951
952                 vtpci_read_dev_config(hw, 0, (uint8_t *)config, offset_conf);
953
954                 hw->max_rx_queues =
955                         (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
956                         VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
957                 hw->max_tx_queues =
958                         (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
959                         VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
960
961                 virtio_dev_cq_queue_setup(eth_dev,
962                                         config->max_virtqueue_pairs * 2,
963                                         SOCKET_ID_ANY);
964
965                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
966                                 config->max_virtqueue_pairs);
967                 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
968                 PMD_INIT_LOG(DEBUG,
969                                 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
970                                 config->mac[0], config->mac[1],
971                                 config->mac[2], config->mac[3],
972                                 config->mac[4], config->mac[5]);
973         } else {
974                 hw->max_rx_queues = 1;
975                 hw->max_tx_queues = 1;
976         }
977
978         eth_dev->data->nb_rx_queues = hw->max_rx_queues;
979         eth_dev->data->nb_tx_queues = hw->max_tx_queues;
980
981         PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d   hw->max_tx_queues=%d",
982                         hw->max_rx_queues, hw->max_tx_queues);
983         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
984                         eth_dev->data->port_id, pci_dev->id.vendor_id,
985                         pci_dev->id.device_id);
986
987         /* Setup interrupt callback  */
988         rte_intr_callback_register(&pci_dev->intr_handle,
989                                    virtio_interrupt_handler, eth_dev);
990         return 0;
991 }
992
993 static struct eth_driver rte_virtio_pmd = {
994         {
995                 .name = "rte_virtio_pmd",
996                 .id_table = pci_id_virtio_map,
997                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
998         },
999         .eth_dev_init = eth_virtio_dev_init,
1000         .dev_private_size = sizeof(struct virtio_hw),
1001 };
1002
1003 /*
1004  * Driver initialization routine.
1005  * Invoked once at EAL init time.
1006  * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1007  * Returns 0 on success.
1008  */
1009 static int
1010 rte_virtio_pmd_init(const char *name __rte_unused,
1011                     const char *param __rte_unused)
1012 {
1013         if (rte_eal_iopl_init() != 0) {
1014                 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1015                 return -1;
1016         }
1017
1018         rte_eth_driver_register(&rte_virtio_pmd);
1019         return 0;
1020 }
1021
1022 /*
1023  * Only 1 queue is supported, no queue release related operation
1024  */
1025 static void
1026 virtio_dev_rx_queue_release(__rte_unused void *rxq)
1027 {
1028 }
1029
1030 static void
1031 virtio_dev_tx_queue_release(__rte_unused void *txq)
1032 {
1033 }
1034
1035 /*
1036  * Configure virtio device
1037  * It returns 0 on success.
1038  */
1039 static int
1040 virtio_dev_configure(struct rte_eth_dev *dev)
1041 {
1042         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1043         struct virtio_hw *hw = dev->data->dev_private;
1044         int ret;
1045
1046         PMD_INIT_LOG(DEBUG, "configure");
1047
1048         if (rxmode->hw_ip_checksum) {
1049                 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1050                 return (-EINVAL);
1051         }
1052
1053         ret = vtpci_irq_config(hw, 0);
1054         if (ret != 0)
1055                 PMD_DRV_LOG(ERR, "failed to set config vector");
1056
1057         return ret;
1058 }
1059
1060
1061 static int
1062 virtio_dev_start(struct rte_eth_dev *dev)
1063 {
1064         uint16_t nb_queues, i;
1065         struct virtio_hw *hw = dev->data->dev_private;
1066
1067         /* Tell the host we've noticed this device. */
1068         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1069
1070         /* Tell the host we've known how to drive the device. */
1071         vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1072
1073         virtio_dev_cq_start(dev);
1074
1075         /* Do final configuration before rx/tx engine starts */
1076         virtio_dev_rxtx_start(dev);
1077
1078         /* check if lsc interrupt feature is enabled */
1079         if (dev->data->dev_conf.intr_conf.lsc) {
1080                 if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1081                         PMD_DRV_LOG(ERR, "link status not supported by host");
1082                         return -ENOTSUP;
1083                 }
1084
1085                 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1086                         PMD_DRV_LOG(ERR, "interrupt enable failed");
1087                         return -EIO;
1088                 }
1089         }
1090
1091         /* Initialize Link state */
1092         virtio_dev_link_update(dev, 0);
1093
1094         vtpci_reinit_complete(hw);
1095
1096         /*Notify the backend
1097          *Otherwise the tap backend might already stop its queue due to fullness.
1098          *vhost backend will have no chance to be waked up
1099          */
1100         nb_queues = dev->data->nb_rx_queues;
1101         if (nb_queues > 1) {
1102                 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1103                         return -EINVAL;
1104         }
1105
1106         PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1107
1108         for (i = 0; i < nb_queues; i++)
1109                 virtqueue_notify(dev->data->rx_queues[i]);
1110
1111         PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1112
1113         for (i = 0; i < dev->data->nb_rx_queues; i++)
1114                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1115
1116         for (i = 0; i < dev->data->nb_tx_queues; i++)
1117                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1118
1119         return 0;
1120 }
1121
1122 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1123 {
1124         struct rte_mbuf *buf;
1125         int i, mbuf_num = 0;
1126
1127         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1128                 PMD_INIT_LOG(DEBUG,
1129                              "Before freeing rxq[%d] used and unused buf", i);
1130                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1131
1132                 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1133                                         dev->data->rx_queues[i])) != NULL) {
1134                         rte_pktmbuf_free(buf);
1135                         mbuf_num++;
1136                 }
1137
1138                 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1139                 PMD_INIT_LOG(DEBUG,
1140                              "After freeing rxq[%d] used and unused buf", i);
1141                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1142         }
1143
1144         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1145                 PMD_INIT_LOG(DEBUG,
1146                              "Before freeing txq[%d] used and unused bufs",
1147                              i);
1148                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1149
1150                 mbuf_num = 0;
1151                 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1152                                         dev->data->tx_queues[i])) != NULL) {
1153                         rte_pktmbuf_free(buf);
1154
1155                         mbuf_num++;
1156                 }
1157
1158                 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1159                 PMD_INIT_LOG(DEBUG,
1160                              "After freeing txq[%d] used and unused buf", i);
1161                 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1162         }
1163 }
1164
1165 /*
1166  * Stop device: disable rx and tx functions to allow for reconfiguring.
1167  */
1168 static void
1169 virtio_dev_stop(struct rte_eth_dev *dev)
1170 {
1171         struct virtio_hw *hw = dev->data->dev_private;
1172
1173         /* reset the NIC */
1174         vtpci_irq_config(hw, 0);
1175         vtpci_reset(hw);
1176         virtio_dev_free_mbufs(dev);
1177 }
1178
1179 static int
1180 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1181 {
1182         struct rte_eth_link link, old;
1183         uint16_t status;
1184         struct virtio_hw *hw = dev->data->dev_private;
1185         memset(&link, 0, sizeof(link));
1186         virtio_dev_atomic_read_link_status(dev, &link);
1187         old = link;
1188         link.link_duplex = FULL_DUPLEX;
1189         link.link_speed  = SPEED_10G;
1190
1191         if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1192                 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1193                 vtpci_read_dev_config(hw,
1194                                 offsetof(struct virtio_net_config, status),
1195                                 &status, sizeof(status));
1196                 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1197                         link.link_status = 0;
1198                         PMD_INIT_LOG(DEBUG, "Port %d is down",
1199                                      dev->data->port_id);
1200                 } else {
1201                         link.link_status = 1;
1202                         PMD_INIT_LOG(DEBUG, "Port %d is up",
1203                                      dev->data->port_id);
1204                 }
1205         } else {
1206                 link.link_status = 1;   /* Link up */
1207         }
1208         virtio_dev_atomic_write_link_status(dev, &link);
1209
1210         return (old.link_status == link.link_status) ? -1 : 0;
1211 }
1212
1213 static void
1214 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1215 {
1216         struct virtio_hw *hw = dev->data->dev_private;
1217
1218         dev_info->driver_name = dev->driver->pci_drv.name;
1219         dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1220         dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1221         dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1222         dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1223         dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1224 }
1225
1226 /*
1227  * It enables testpmd to collect per queue stats.
1228  */
1229 static int
1230 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1231 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1232 __rte_unused uint8_t is_rx)
1233 {
1234         return 0;
1235 }
1236
1237 static struct rte_driver rte_virtio_driver = {
1238         .type = PMD_PDEV,
1239         .init = rte_virtio_pmd_init,
1240 };
1241
1242 PMD_REGISTER_DRIVER(rte_virtio_driver);