4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #ifdef RTE_EXEC_ENV_LINUXAPP
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>
51 #include <rte_ether.h>
52 #include <rte_common.h>
54 #include <rte_memory.h>
58 #include "virtio_ethdev.h"
59 #include "virtio_pci.h"
60 #include "virtio_logs.h"
61 #include "virtqueue.h"
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);
78 static void virtio_set_hwaddr(struct virtio_hw *hw);
79 static void virtio_get_hwaddr(struct virtio_hw *hw);
81 static void virtio_dev_rx_queue_release(__rte_unused void *rxq);
82 static void virtio_dev_tx_queue_release(__rte_unused void *txq);
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);
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);
97 * The set of PCI devices this driver supports
99 static struct rte_pci_id pci_id_virtio_map[] = {
101 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
102 #include "rte_pci_dev_ids.h"
104 { .vendor_id = 0, /* sentinel */ },
108 virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl,
109 int *dlen, int pkt_num)
111 uint32_t head = vq->vq_desc_head_idx, i;
113 virtio_net_ctrl_ack status = ~0;
114 struct virtio_pmd_ctrl result;
116 ctrl->status = status;
120 "%s(): Control queue is not supported.",
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);
129 if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
132 memcpy(vq->virtio_net_hdr_mz->addr, ctrl,
133 sizeof(struct virtio_pmd_ctrl));
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.
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);
145 i = vq->vq_ring.desc[head].next;
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];
155 i = vq->vq_ring.desc[i].next;
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);
164 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
166 vq_update_avail_ring(vq, head);
167 vq_update_avail_idx(vq);
169 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
171 virtqueue_notify(vq);
173 while (vq->vq_used_cons_idx == vq->vq_ring.used->idx)
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;
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;
188 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
189 desc_idx = vq->vq_ring.desc[desc_idx].next;
193 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
194 vq->vq_desc_head_idx = idx;
196 vq->vq_used_cons_idx++;
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);
203 memcpy(&result, vq->virtio_net_hdr_mz->addr,
204 sizeof(struct virtio_pmd_ctrl));
206 return result.status;
210 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
212 struct virtio_hw *hw = dev->data->dev_private;
213 struct virtio_pmd_ctrl ctrl;
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));
221 dlen[0] = sizeof(uint16_t);
223 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
226 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
227 "failed, this is too late now...");
234 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
237 uint8_t vtpci_queue_idx,
239 unsigned int socket_id,
240 struct virtqueue **pvq)
242 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
243 const struct rte_memzone *mz;
246 struct virtio_hw *hw = dev->data->dev_private;
247 struct virtqueue *vq = NULL;
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);
254 * Read the virtqueue size from the Queue Size field
255 * Always power of 2 and if 0 virtqueue does not exist
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);
262 PMD_INIT_LOG(ERR, "%s: virtqueue does not exist", __func__);
264 } else if (!rte_is_power_of_2(vq_size)) {
265 PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2", __func__);
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",
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",
286 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
287 vq_size * sizeof(struct vq_desc_extra),
288 RTE_CACHE_LINE_SIZE);
291 PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue", __func__);
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;
303 * Reserve a memzone for vring elements
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);
309 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
310 socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
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.
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!");
327 memset(mz->addr, 0, sizeof(mz->len));
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;
336 if (queue_type == VTNET_TQ) {
338 * For each xmit packet, allocate a virtio_net_hdr
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) {
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",
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) {
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);
369 * Set guest physical address of the virtqueue
370 * in VIRTIO_PCI_QUEUE_PFN config register of device
372 VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
373 mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
379 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
382 struct virtqueue *vq;
383 uint16_t nb_desc = 0;
385 struct virtio_hw *hw = dev->data->dev_private;
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);
392 PMD_INIT_LOG(ERR, "control vq initialization failed");
401 virtio_dev_close(struct rte_eth_dev *dev)
403 struct virtio_hw *hw = dev->data->dev_private;
405 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
408 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
411 virtio_dev_free_mbufs(dev);
415 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
417 struct virtio_hw *hw = dev->data->dev_private;
418 struct virtio_pmd_ctrl ctrl;
422 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
423 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
427 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
430 PMD_INIT_LOG(ERR, "Failed to enable promisc");
434 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
436 struct virtio_hw *hw = dev->data->dev_private;
437 struct virtio_pmd_ctrl ctrl;
441 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
442 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
446 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
449 PMD_INIT_LOG(ERR, "Failed to disable promisc");
453 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
455 struct virtio_hw *hw = dev->data->dev_private;
456 struct virtio_pmd_ctrl ctrl;
460 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
461 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
465 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
468 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
472 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
474 struct virtio_hw *hw = dev->data->dev_private;
475 struct virtio_pmd_ctrl ctrl;
479 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
480 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
484 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
487 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
491 * dev_ops for virtio, bare necessities for basic operation
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,
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,
521 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
522 struct rte_eth_link *link)
524 struct rte_eth_link *dst = link;
525 struct rte_eth_link *src = &(dev->data->dev_link);
527 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
528 *(uint64_t *)src) == 0)
535 * Atomically writes the link status information into global
536 * structure rte_eth_dev.
539 * - Pointer to the structure rte_eth_dev to read from.
540 * - Pointer to the buffer to be saved with the link status.
543 * - On success, zero.
544 * - On failure, negative value.
547 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
548 struct rte_eth_link *link)
550 struct rte_eth_link *dst = &(dev->data->dev_link);
551 struct rte_eth_link *src = link;
553 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
554 *(uint64_t *)src) == 0)
561 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
565 for (i = 0; i < dev->data->nb_tx_queues; i++) {
566 const struct virtqueue *txvq = dev->data->tx_queues[i];
570 stats->opackets += txvq->packets;
571 stats->obytes += txvq->bytes;
572 stats->oerrors += txvq->errors;
574 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
575 stats->q_opackets[i] = txvq->packets;
576 stats->q_obytes[i] = txvq->bytes;
580 for (i = 0; i < dev->data->nb_rx_queues; i++) {
581 const struct virtqueue *rxvq = dev->data->rx_queues[i];
585 stats->ipackets += rxvq->packets;
586 stats->ibytes += rxvq->bytes;
587 stats->ierrors += rxvq->errors;
589 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
590 stats->q_ipackets[i] = rxvq->packets;
591 stats->q_ibytes[i] = rxvq->bytes;
595 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
599 virtio_dev_stats_reset(struct rte_eth_dev *dev)
603 for (i = 0; i < dev->data->nb_tx_queues; i++) {
604 struct virtqueue *txvq = dev->data->tx_queues[i];
613 for (i = 0; i < dev->data->nb_rx_queues; i++) {
614 struct virtqueue *rxvq = dev->data->rx_queues[i];
623 dev->data->rx_mbuf_alloc_failed = 0;
627 virtio_set_hwaddr(struct virtio_hw *hw)
629 vtpci_write_dev_config(hw,
630 offsetof(struct virtio_net_config, mac),
631 &hw->mac_addr, ETHER_ADDR_LEN);
635 virtio_get_hwaddr(struct virtio_hw *hw)
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);
642 eth_random_addr(&hw->mac_addr[0]);
643 virtio_set_hwaddr(hw);
648 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
650 struct virtio_hw *hw = dev->data->dev_private;
651 struct virtio_pmd_ctrl ctrl;
654 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
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);
662 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
666 virtio_negotiate_features(struct virtio_hw *hw)
668 uint32_t host_features, mask;
670 /* checksum offload not implemented */
671 mask = VIRTIO_NET_F_CSUM | VIRTIO_NET_F_GUEST_CSUM;
673 /* TSO and LRO are only available when their corresponding
674 * checksum offload feature is also negotiated.
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;
680 /* not negotiating INDIRECT descriptor table support */
681 mask |= VIRTIO_RING_F_INDIRECT_DESC;
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",
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",
694 * Negotiate features: Subset of device feature bits are written back
695 * guest feature bits.
697 hw->guest_features = vtpci_negotiate_features(hw, host_features);
698 PMD_INIT_LOG(DEBUG, "features after negotiate = %x",
702 #ifdef RTE_EXEC_ENV_LINUXAPP
704 parse_sysfs_value(const char *filename, unsigned long *val)
710 f = fopen(filename, "r");
712 PMD_INIT_LOG(ERR, "%s(): cannot open sysfs value %s",
717 if (fgets(buf, sizeof(buf), f) == NULL) {
718 PMD_INIT_LOG(ERR, "%s(): cannot read sysfs value %s",
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",
734 static int get_uio_dev(struct rte_pci_addr *loc, char *buf, unsigned int buflen)
736 unsigned int uio_num;
739 char dirname[PATH_MAX];
741 /* depending on kernel version, uio can be located in 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);
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);
755 PMD_INIT_LOG(ERR, "Cannot opendir %s", dirname);
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;
768 if (strncmp(e->d_name, "uio", 3) != 0)
771 /* first try uio%d */
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);
779 /* then try uio:uio%d */
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,
790 /* No uio resource found */
792 PMD_INIT_LOG(ERR, "Could not find uio resource");
800 virtio_has_msix(const struct rte_pci_addr *loc)
803 char dirname[PATH_MAX];
805 snprintf(dirname, sizeof(dirname),
806 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/msi_irqs",
807 loc->domain, loc->bus, loc->devid, loc->function);
809 d = opendir(dirname);
816 /* Extract I/O port numbers from sysfs */
817 static int virtio_resource_init(struct rte_pci_device *pci_dev)
819 char dirname[PATH_MAX];
820 char filename[PATH_MAX];
821 unsigned long start, size;
823 if (get_uio_dev(&pci_dev->addr, dirname, sizeof(dirname)) < 0)
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",
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",
843 pci_dev->mem_resource[0].addr = (void *)(uintptr_t)start;
844 pci_dev->mem_resource[0].len = (uint64_t)size;
846 "PCI Port IO found start=0x%lx with size=0x%lx",
852 virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
854 /* nic_uio does not enable interrupts, return 0 (false). */
858 static int virtio_resource_init(struct rte_pci_device *pci_dev __rte_unused)
860 /* no setup required */
866 * Process Virtio Config changed interrupt and call the callback
867 * if link state changed.
870 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
873 struct rte_eth_dev *dev = param;
874 struct virtio_hw *hw = dev->data->dev_private;
877 /* Read interrupt status which clears interrupt */
879 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
881 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
882 PMD_DRV_LOG(ERR, "interrupt enable failed");
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);
893 * This function is based on probe() function in virtio_pci.c
894 * It returns 0 on success.
897 eth_virtio_dev_init(__rte_unused struct eth_driver *eth_drv,
898 struct rte_eth_dev *eth_dev)
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;
906 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
908 eth_dev->dev_ops = &virtio_eth_dev_ops;
909 eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
911 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
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) {
918 "Failed to allocate %d bytes needed to store MAC addresses",
923 pci_dev = eth_dev->pci_dev;
924 if (virtio_resource_init(pci_dev) < 0)
927 hw->use_msix = virtio_has_msix(&pci_dev->addr);
928 hw->io_base = (uint32_t)(uintptr_t)pci_dev->mem_resource[0].addr;
930 /* Reset the device although not necessary at startup */
933 /* Tell the host we've noticed this device. */
934 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
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);
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);
945 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
946 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
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 ð_dev->data->mac_addrs[0]);
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]);
958 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
959 config = &local_config;
961 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
962 offset_conf += sizeof(config->status);
965 "VIRTIO_NET_F_STATUS is not supported");
969 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
970 offset_conf += sizeof(config->max_virtqueue_pairs);
973 "VIRTIO_NET_F_MQ is not supported");
974 config->max_virtqueue_pairs = 1;
977 vtpci_read_dev_config(hw, 0, (uint8_t *)config, offset_conf);
980 (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
981 VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
983 (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
984 VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
986 virtio_dev_cq_queue_setup(eth_dev,
987 config->max_virtqueue_pairs * 2,
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);
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]);
999 hw->max_rx_queues = 1;
1000 hw->max_tx_queues = 1;
1003 eth_dev->data->nb_rx_queues = hw->max_rx_queues;
1004 eth_dev->data->nb_tx_queues = hw->max_tx_queues;
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);
1012 /* Setup interrupt callback */
1013 rte_intr_callback_register(&pci_dev->intr_handle,
1014 virtio_interrupt_handler, eth_dev);
1016 virtio_dev_cq_start(eth_dev);
1021 static struct eth_driver rte_virtio_pmd = {
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,
1027 .eth_dev_init = eth_virtio_dev_init,
1028 .dev_private_size = sizeof(struct virtio_hw),
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.
1038 rte_virtio_pmd_init(const char *name __rte_unused,
1039 const char *param __rte_unused)
1041 if (rte_eal_iopl_init() != 0) {
1042 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1046 rte_eth_driver_register(&rte_virtio_pmd);
1051 * Only 1 queue is supported, no queue release related operation
1054 virtio_dev_rx_queue_release(__rte_unused void *rxq)
1059 virtio_dev_tx_queue_release(__rte_unused void *txq)
1064 * Configure virtio device
1065 * It returns 0 on success.
1068 virtio_dev_configure(struct rte_eth_dev *dev)
1070 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1071 struct virtio_hw *hw = dev->data->dev_private;
1073 PMD_INIT_LOG(DEBUG, "configure");
1075 if (rxmode->hw_ip_checksum) {
1076 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1080 hw->vlan_strip = rxmode->hw_vlan_strip;
1082 if (rxmode->hw_vlan_filter
1083 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1085 "vlan filtering not available on this host");
1089 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1090 PMD_DRV_LOG(ERR, "failed to set config vector");
1099 virtio_dev_start(struct rte_eth_dev *dev)
1101 uint16_t nb_queues, i;
1102 struct virtio_hw *hw = dev->data->dev_private;
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");
1111 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1112 PMD_DRV_LOG(ERR, "interrupt enable failed");
1117 /* Initialize Link state */
1118 virtio_dev_link_update(dev, 0);
1120 /* On restart after stop do not touch queues */
1124 /* Do final configuration before rx/tx engine starts */
1125 virtio_dev_rxtx_start(dev);
1126 vtpci_reinit_complete(hw);
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
1134 nb_queues = dev->data->nb_rx_queues;
1135 if (nb_queues > 1) {
1136 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1140 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1142 for (i = 0; i < nb_queues; i++)
1143 virtqueue_notify(dev->data->rx_queues[i]);
1145 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1147 for (i = 0; i < dev->data->nb_rx_queues; i++)
1148 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1150 for (i = 0; i < dev->data->nb_tx_queues; i++)
1151 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1156 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1158 struct rte_mbuf *buf;
1159 int i, mbuf_num = 0;
1161 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1163 "Before freeing rxq[%d] used and unused buf", i);
1164 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1166 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1167 dev->data->rx_queues[i])) != NULL) {
1168 rte_pktmbuf_free(buf);
1172 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1174 "After freeing rxq[%d] used and unused buf", i);
1175 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1178 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1180 "Before freeing txq[%d] used and unused bufs",
1182 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1185 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1186 dev->data->tx_queues[i])) != NULL) {
1187 rte_pktmbuf_free(buf);
1192 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1194 "After freeing txq[%d] used and unused buf", i);
1195 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1200 * Stop device: disable interrupt and mark link down
1203 virtio_dev_stop(struct rte_eth_dev *dev)
1205 struct rte_eth_link link;
1207 PMD_INIT_LOG(DEBUG, "stop");
1209 if (dev->data->dev_conf.intr_conf.lsc)
1210 rte_intr_disable(&dev->pci_dev->intr_handle);
1212 memset(&link, 0, sizeof(link));
1213 virtio_dev_atomic_write_link_status(dev, &link);
1217 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1219 struct rte_eth_link link, old;
1221 struct virtio_hw *hw = dev->data->dev_private;
1222 memset(&link, 0, sizeof(link));
1223 virtio_dev_atomic_read_link_status(dev, &link);
1225 link.link_duplex = FULL_DUPLEX;
1226 link.link_speed = SPEED_10G;
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);
1238 link.link_status = 1;
1239 PMD_INIT_LOG(DEBUG, "Port %d is up",
1240 dev->data->port_id);
1243 link.link_status = 1; /* Link up */
1245 virtio_dev_atomic_write_link_status(dev, &link);
1247 return (old.link_status == link.link_status) ? -1 : 0;
1251 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1253 struct virtio_hw *hw = dev->data->dev_private;
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;
1264 * It enables testpmd to collect per queue stats.
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)
1274 static struct rte_driver rte_virtio_driver = {
1276 .init = rte_virtio_pmd_init,
1279 PMD_REGISTER_DRIVER(rte_virtio_driver);