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