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