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