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
44 #include <rte_ethdev.h>
45 #include <rte_memcpy.h>
46 #include <rte_string_fns.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
52 #include <rte_ether.h>
53 #include <rte_common.h>
55 #include <rte_memory.h>
59 #include "virtio_ethdev.h"
60 #include "virtio_pci.h"
61 #include "virtio_logs.h"
62 #include "virtqueue.h"
65 static int eth_virtio_dev_init(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);
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 static void virtio_mac_addr_set(struct rte_eth_dev *dev,
94 struct ether_addr *mac_addr);
96 static int virtio_dev_queue_stats_mapping_set(
97 __rte_unused struct rte_eth_dev *eth_dev,
98 __rte_unused uint16_t queue_id,
99 __rte_unused uint8_t stat_idx,
100 __rte_unused uint8_t is_rx);
103 * The set of PCI devices this driver supports
105 static const struct rte_pci_id pci_id_virtio_map[] = {
107 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
108 #include "rte_pci_dev_ids.h"
110 { .vendor_id = 0, /* sentinel */ },
114 virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl,
115 int *dlen, int pkt_num)
117 uint16_t head = vq->vq_desc_head_idx, i;
119 virtio_net_ctrl_ack status = ~0;
120 struct virtio_pmd_ctrl result;
122 ctrl->status = status;
126 "%s(): Control queue is not supported.",
131 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
132 "vq->hw->cvq = %p vq = %p",
133 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
135 if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
138 memcpy(vq->virtio_net_hdr_mz->addr, ctrl,
139 sizeof(struct virtio_pmd_ctrl));
142 * Format is enforced in qemu code:
143 * One TX packet for header;
144 * At least one TX packet per argument;
145 * One RX packet for ACK.
147 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
148 vq->vq_ring.desc[head].addr = vq->virtio_net_hdr_mz->phys_addr;
149 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
151 i = vq->vq_ring.desc[head].next;
153 for (k = 0; k < pkt_num; k++) {
154 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
155 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
156 + sizeof(struct virtio_net_ctrl_hdr)
157 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
158 vq->vq_ring.desc[i].len = dlen[k];
161 i = vq->vq_ring.desc[i].next;
164 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
165 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
166 + sizeof(struct virtio_net_ctrl_hdr);
167 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
170 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
172 vq_update_avail_ring(vq, head);
173 vq_update_avail_idx(vq);
175 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
177 virtqueue_notify(vq);
180 while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) {
185 while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
186 uint32_t idx, desc_idx, used_idx;
187 struct vring_used_elem *uep;
189 used_idx = (uint32_t)(vq->vq_used_cons_idx
190 & (vq->vq_nentries - 1));
191 uep = &vq->vq_ring.used->ring[used_idx];
192 idx = (uint32_t) uep->id;
195 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
196 desc_idx = vq->vq_ring.desc[desc_idx].next;
200 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
201 vq->vq_desc_head_idx = idx;
203 vq->vq_used_cons_idx++;
207 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
208 vq->vq_free_cnt, vq->vq_desc_head_idx);
210 memcpy(&result, vq->virtio_net_hdr_mz->addr,
211 sizeof(struct virtio_pmd_ctrl));
213 return result.status;
217 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
219 struct virtio_hw *hw = dev->data->dev_private;
220 struct virtio_pmd_ctrl ctrl;
224 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
225 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
226 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
228 dlen[0] = sizeof(uint16_t);
230 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
232 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
233 "failed, this is too late now...");
240 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
243 uint16_t vtpci_queue_idx,
245 unsigned int socket_id,
246 struct virtqueue **pvq)
248 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
249 const struct rte_memzone *mz;
252 struct virtio_hw *hw = dev->data->dev_private;
253 struct virtqueue *vq = NULL;
255 /* Write the virtqueue index to the Queue Select Field */
256 VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vtpci_queue_idx);
257 PMD_INIT_LOG(DEBUG, "selecting queue: %d", vtpci_queue_idx);
260 * Read the virtqueue size from the Queue Size field
261 * Always power of 2 and if 0 virtqueue does not exist
263 vq_size = VIRTIO_READ_REG_2(hw, VIRTIO_PCI_QUEUE_NUM);
264 PMD_INIT_LOG(DEBUG, "vq_size: %d nb_desc:%d", vq_size, nb_desc);
268 PMD_INIT_LOG(ERR, "%s: virtqueue does not exist", __func__);
272 if (!rte_is_power_of_2(vq_size)) {
273 PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2", __func__);
277 if (nb_desc < vq_size) {
278 if (!rte_is_power_of_2(nb_desc)) {
280 "nb_desc(%u) size is not powerof 2",
287 if (queue_type == VTNET_RQ) {
288 snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
289 dev->data->port_id, queue_idx);
290 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
291 vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
292 } else if (queue_type == VTNET_TQ) {
293 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
294 dev->data->port_id, queue_idx);
295 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
296 vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
297 } else if (queue_type == VTNET_CQ) {
298 snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
300 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
301 vq_size * sizeof(struct vq_desc_extra),
302 RTE_CACHE_LINE_SIZE);
305 PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue", __func__);
310 vq->port_id = dev->data->port_id;
311 vq->queue_id = queue_idx;
312 vq->vq_queue_index = vtpci_queue_idx;
313 vq->vq_nentries = vq_size;
314 vq->vq_free_cnt = vq_size;
317 * Reserve a memzone for vring elements
319 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
320 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
321 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size);
323 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
324 socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
331 * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
332 * and only accepts 32 bit page frame number.
333 * Check if the allocated physical memory exceeds 16TB.
335 if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
336 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
341 memset(mz->addr, 0, sizeof(mz->len));
343 vq->vq_ring_mem = mz->phys_addr;
344 vq->vq_ring_virt_mem = mz->addr;
345 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%"PRIx64, (uint64_t)mz->phys_addr);
346 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)(uintptr_t)mz->addr);
347 vq->virtio_net_hdr_mz = NULL;
348 vq->virtio_net_hdr_mem = 0;
350 if (queue_type == VTNET_TQ) {
352 * For each xmit packet, allocate a virtio_net_hdr
354 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
355 dev->data->port_id, queue_idx);
356 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
357 vq_size * hw->vtnet_hdr_size,
358 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,
366 vq_size * hw->vtnet_hdr_size);
367 } else if (queue_type == VTNET_CQ) {
368 /* Allocate a page for control vq command, data and status */
369 snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
371 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
372 PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE);
373 if (vq->virtio_net_hdr_mz == NULL) {
377 vq->virtio_net_hdr_mem =
378 vq->virtio_net_hdr_mz->phys_addr;
379 memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
383 * Set guest physical address of the virtqueue
384 * in VIRTIO_PCI_QUEUE_PFN config register of device
386 VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
387 mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
393 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
396 struct virtqueue *vq;
397 uint16_t nb_desc = 0;
399 struct virtio_hw *hw = dev->data->dev_private;
401 PMD_INIT_FUNC_TRACE();
402 ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
403 vtpci_queue_idx, nb_desc, socket_id, &vq);
405 PMD_INIT_LOG(ERR, "control vq initialization failed");
414 virtio_dev_close(struct rte_eth_dev *dev)
416 struct virtio_hw *hw = dev->data->dev_private;
417 struct rte_pci_device *pci_dev = dev->pci_dev;
419 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
422 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
423 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
426 virtio_dev_free_mbufs(dev);
430 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
432 struct virtio_hw *hw = dev->data->dev_private;
433 struct virtio_pmd_ctrl ctrl;
437 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
438 PMD_INIT_LOG(INFO, "host does not support rx control\n");
442 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
443 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
447 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
449 PMD_INIT_LOG(ERR, "Failed to enable promisc");
453 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
455 struct virtio_hw *hw = dev->data->dev_private;
456 struct virtio_pmd_ctrl ctrl;
460 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
461 PMD_INIT_LOG(INFO, "host does not support rx control\n");
465 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
466 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
470 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
472 PMD_INIT_LOG(ERR, "Failed to disable promisc");
476 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
478 struct virtio_hw *hw = dev->data->dev_private;
479 struct virtio_pmd_ctrl ctrl;
483 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
484 PMD_INIT_LOG(INFO, "host does not support rx control\n");
488 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
489 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
493 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
495 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
499 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
501 struct virtio_hw *hw = dev->data->dev_private;
502 struct virtio_pmd_ctrl ctrl;
506 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
507 PMD_INIT_LOG(INFO, "host does not support rx control\n");
511 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
512 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
516 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
518 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
522 * dev_ops for virtio, bare necessities for basic operation
524 static const struct eth_dev_ops virtio_eth_dev_ops = {
525 .dev_configure = virtio_dev_configure,
526 .dev_start = virtio_dev_start,
527 .dev_stop = virtio_dev_stop,
528 .dev_close = virtio_dev_close,
529 .promiscuous_enable = virtio_dev_promiscuous_enable,
530 .promiscuous_disable = virtio_dev_promiscuous_disable,
531 .allmulticast_enable = virtio_dev_allmulticast_enable,
532 .allmulticast_disable = virtio_dev_allmulticast_disable,
534 .dev_infos_get = virtio_dev_info_get,
535 .stats_get = virtio_dev_stats_get,
536 .stats_reset = virtio_dev_stats_reset,
537 .link_update = virtio_dev_link_update,
538 .rx_queue_setup = virtio_dev_rx_queue_setup,
539 /* meaningfull only to multiple queue */
540 .rx_queue_release = virtio_dev_rx_queue_release,
541 .tx_queue_setup = virtio_dev_tx_queue_setup,
542 /* meaningfull only to multiple queue */
543 .tx_queue_release = virtio_dev_tx_queue_release,
544 /* collect stats per queue */
545 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
546 .vlan_filter_set = virtio_vlan_filter_set,
547 .mac_addr_add = virtio_mac_addr_add,
548 .mac_addr_remove = virtio_mac_addr_remove,
549 .mac_addr_set = virtio_mac_addr_set,
553 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
554 struct rte_eth_link *link)
556 struct rte_eth_link *dst = link;
557 struct rte_eth_link *src = &(dev->data->dev_link);
559 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
560 *(uint64_t *)src) == 0)
567 * Atomically writes the link status information into global
568 * structure rte_eth_dev.
571 * - Pointer to the structure rte_eth_dev to read from.
572 * - Pointer to the buffer to be saved with the link status.
575 * - On success, zero.
576 * - On failure, negative value.
579 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
580 struct rte_eth_link *link)
582 struct rte_eth_link *dst = &(dev->data->dev_link);
583 struct rte_eth_link *src = link;
585 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
586 *(uint64_t *)src) == 0)
593 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
597 for (i = 0; i < dev->data->nb_tx_queues; i++) {
598 const struct virtqueue *txvq = dev->data->tx_queues[i];
602 stats->opackets += txvq->packets;
603 stats->obytes += txvq->bytes;
604 stats->oerrors += txvq->errors;
606 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
607 stats->q_opackets[i] = txvq->packets;
608 stats->q_obytes[i] = txvq->bytes;
612 for (i = 0; i < dev->data->nb_rx_queues; i++) {
613 const struct virtqueue *rxvq = dev->data->rx_queues[i];
617 stats->ipackets += rxvq->packets;
618 stats->ibytes += rxvq->bytes;
619 stats->ierrors += rxvq->errors;
621 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
622 stats->q_ipackets[i] = rxvq->packets;
623 stats->q_ibytes[i] = rxvq->bytes;
627 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
631 virtio_dev_stats_reset(struct rte_eth_dev *dev)
635 for (i = 0; i < dev->data->nb_tx_queues; i++) {
636 struct virtqueue *txvq = dev->data->tx_queues[i];
645 for (i = 0; i < dev->data->nb_rx_queues; i++) {
646 struct virtqueue *rxvq = dev->data->rx_queues[i];
655 dev->data->rx_mbuf_alloc_failed = 0;
659 virtio_set_hwaddr(struct virtio_hw *hw)
661 vtpci_write_dev_config(hw,
662 offsetof(struct virtio_net_config, mac),
663 &hw->mac_addr, ETHER_ADDR_LEN);
667 virtio_get_hwaddr(struct virtio_hw *hw)
669 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
670 vtpci_read_dev_config(hw,
671 offsetof(struct virtio_net_config, mac),
672 &hw->mac_addr, ETHER_ADDR_LEN);
674 eth_random_addr(&hw->mac_addr[0]);
675 virtio_set_hwaddr(hw);
680 virtio_mac_table_set(struct virtio_hw *hw,
681 const struct virtio_net_ctrl_mac *uc,
682 const struct virtio_net_ctrl_mac *mc)
684 struct virtio_pmd_ctrl ctrl;
687 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
688 PMD_DRV_LOG(INFO, "host does not support mac table\n");
692 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
693 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
695 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
696 memcpy(ctrl.data, uc, len[0]);
698 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
699 memcpy(ctrl.data + len[0], mc, len[1]);
701 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
703 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
707 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
708 uint32_t index, uint32_t vmdq __rte_unused)
710 struct virtio_hw *hw = dev->data->dev_private;
711 const struct ether_addr *addrs = dev->data->mac_addrs;
713 struct virtio_net_ctrl_mac *uc, *mc;
715 if (index >= VIRTIO_MAX_MAC_ADDRS) {
716 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
720 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
722 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
725 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
726 const struct ether_addr *addr
727 = (i == index) ? mac_addr : addrs + i;
728 struct virtio_net_ctrl_mac *tbl
729 = is_multicast_ether_addr(addr) ? mc : uc;
731 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
734 virtio_mac_table_set(hw, uc, mc);
738 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
740 struct virtio_hw *hw = dev->data->dev_private;
741 struct ether_addr *addrs = dev->data->mac_addrs;
742 struct virtio_net_ctrl_mac *uc, *mc;
745 if (index >= VIRTIO_MAX_MAC_ADDRS) {
746 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
750 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
752 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
755 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
756 struct virtio_net_ctrl_mac *tbl;
758 if (i == index || is_zero_ether_addr(addrs + i))
761 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
762 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
765 virtio_mac_table_set(hw, uc, mc);
769 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
771 struct virtio_hw *hw = dev->data->dev_private;
773 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
775 /* Use atomic update if available */
776 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
777 struct virtio_pmd_ctrl ctrl;
778 int len = ETHER_ADDR_LEN;
780 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
781 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
783 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
784 virtio_send_command(hw->cvq, &ctrl, &len, 1);
785 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
786 virtio_set_hwaddr(hw);
790 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
792 struct virtio_hw *hw = dev->data->dev_private;
793 struct virtio_pmd_ctrl ctrl;
796 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
799 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
800 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
801 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
802 len = sizeof(vlan_id);
804 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
808 virtio_negotiate_features(struct virtio_hw *hw)
810 uint32_t host_features;
812 /* Prepare guest_features: feature that driver wants to support */
813 hw->guest_features = VIRTIO_PMD_GUEST_FEATURES;
814 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %x",
817 /* Read device(host) feature bits */
818 host_features = VIRTIO_READ_REG_4(hw, VIRTIO_PCI_HOST_FEATURES);
819 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %x",
823 * Negotiate features: Subset of device feature bits are written back
824 * guest feature bits.
826 hw->guest_features = vtpci_negotiate_features(hw, host_features);
827 PMD_INIT_LOG(DEBUG, "features after negotiate = %x",
831 #ifdef RTE_EXEC_ENV_LINUXAPP
833 parse_sysfs_value(const char *filename, unsigned long *val)
839 f = fopen(filename, "r");
841 PMD_INIT_LOG(ERR, "%s(): cannot open sysfs value %s",
846 if (fgets(buf, sizeof(buf), f) == NULL) {
847 PMD_INIT_LOG(ERR, "%s(): cannot read sysfs value %s",
852 *val = strtoul(buf, &end, 0);
853 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
854 PMD_INIT_LOG(ERR, "%s(): cannot parse sysfs value %s",
863 static int get_uio_dev(struct rte_pci_addr *loc, char *buf, unsigned int buflen,
864 unsigned int *uio_num)
868 char dirname[PATH_MAX];
870 /* depending on kernel version, uio can be located in uio/uioX
872 snprintf(dirname, sizeof(dirname),
873 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
874 loc->domain, loc->bus, loc->devid, loc->function);
875 dir = opendir(dirname);
877 /* retry with the parent directory */
878 snprintf(dirname, sizeof(dirname),
879 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
880 loc->domain, loc->bus, loc->devid, loc->function);
881 dir = opendir(dirname);
884 PMD_INIT_LOG(ERR, "Cannot opendir %s", dirname);
889 /* take the first file starting with "uio" */
890 while ((e = readdir(dir)) != NULL) {
891 /* format could be uio%d ...*/
892 int shortprefix_len = sizeof("uio") - 1;
893 /* ... or uio:uio%d */
894 int longprefix_len = sizeof("uio:uio") - 1;
897 if (strncmp(e->d_name, "uio", 3) != 0)
900 /* first try uio%d */
902 *uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
903 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
904 snprintf(buf, buflen, "%s/uio%u", dirname, *uio_num);
908 /* then try uio:uio%d */
910 *uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
911 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
912 snprintf(buf, buflen, "%s/uio:uio%u", dirname,
919 /* No uio resource found */
921 PMD_INIT_LOG(ERR, "Could not find uio resource");
929 virtio_has_msix(const struct rte_pci_addr *loc)
932 char dirname[PATH_MAX];
934 snprintf(dirname, sizeof(dirname),
935 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/msi_irqs",
936 loc->domain, loc->bus, loc->devid, loc->function);
938 d = opendir(dirname);
945 /* Extract I/O port numbers from sysfs */
946 static int virtio_resource_init_by_uio(struct rte_pci_device *pci_dev)
948 char dirname[PATH_MAX];
949 char filename[PATH_MAX];
950 unsigned long start, size;
951 unsigned int uio_num;
953 if (get_uio_dev(&pci_dev->addr, dirname, sizeof(dirname), &uio_num) < 0)
956 /* get portio size */
957 snprintf(filename, sizeof(filename),
958 "%s/portio/port0/size", dirname);
959 if (parse_sysfs_value(filename, &size) < 0) {
960 PMD_INIT_LOG(ERR, "%s(): cannot parse size",
965 /* get portio start */
966 snprintf(filename, sizeof(filename),
967 "%s/portio/port0/start", dirname);
968 if (parse_sysfs_value(filename, &start) < 0) {
969 PMD_INIT_LOG(ERR, "%s(): cannot parse portio start",
973 pci_dev->mem_resource[0].addr = (void *)(uintptr_t)start;
974 pci_dev->mem_resource[0].len = (uint64_t)size;
976 "PCI Port IO found start=0x%lx with size=0x%lx",
980 memset(dirname, 0, sizeof(dirname));
981 snprintf(dirname, sizeof(dirname), "/dev/uio%u", uio_num);
982 pci_dev->intr_handle.fd = open(dirname, O_RDWR);
983 if (pci_dev->intr_handle.fd < 0) {
984 PMD_INIT_LOG(ERR, "Cannot open %s: %s\n",
985 dirname, strerror(errno));
989 pci_dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
990 pci_dev->driver->drv_flags |= RTE_PCI_DRV_INTR_LSC;
995 /* Extract port I/O numbers from proc/ioports */
996 static int virtio_resource_init_by_ioports(struct rte_pci_device *pci_dev)
1006 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
1007 pci_dev->addr.domain,
1009 pci_dev->addr.devid,
1010 pci_dev->addr.function);
1012 fp = fopen("/proc/ioports", "r");
1014 PMD_INIT_LOG(ERR, "%s(): can't open ioports", __func__);
1018 while (getdelim(&line, &linesz, '\n', fp) > 0) {
1023 n = strcspn(ptr, ":");
1027 while (*left && isspace(*left))
1030 if (!strncmp(left, pci_id, strlen(pci_id))) {
1033 while (*ptr && isspace(*ptr))
1036 sscanf(ptr, "%04hx-%04hx", &start, &end);
1037 size = end - start + 1;
1049 pci_dev->mem_resource[0].addr = (void *)(uintptr_t)(uint32_t)start;
1050 pci_dev->mem_resource[0].len = (uint64_t)size;
1052 "PCI Port IO found start=0x%x with size=0x%x",
1055 /* can't support lsc interrupt without uio */
1056 pci_dev->driver->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
1061 /* Extract I/O port numbers from sysfs */
1062 static int virtio_resource_init(struct rte_pci_device *pci_dev)
1064 if (virtio_resource_init_by_uio(pci_dev) == 0)
1067 return virtio_resource_init_by_ioports(pci_dev);
1072 virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
1074 /* nic_uio does not enable interrupts, return 0 (false). */
1078 static int virtio_resource_init(struct rte_pci_device *pci_dev __rte_unused)
1080 /* no setup required */
1086 * Process Virtio Config changed interrupt and call the callback
1087 * if link state changed.
1090 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
1093 struct rte_eth_dev *dev = param;
1094 struct virtio_hw *hw = dev->data->dev_private;
1097 /* Read interrupt status which clears interrupt */
1098 isr = vtpci_isr(hw);
1099 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1101 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
1102 PMD_DRV_LOG(ERR, "interrupt enable failed");
1104 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1105 if (virtio_dev_link_update(dev, 0) == 0)
1106 _rte_eth_dev_callback_process(dev,
1107 RTE_ETH_EVENT_INTR_LSC);
1113 rx_func_get(struct rte_eth_dev *eth_dev)
1115 struct virtio_hw *hw = eth_dev->data->dev_private;
1116 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1117 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1119 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1123 * This function is based on probe() function in virtio_pci.c
1124 * It returns 0 on success.
1127 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1129 struct virtio_hw *hw = eth_dev->data->dev_private;
1130 struct virtio_net_config *config;
1131 struct virtio_net_config local_config;
1132 uint32_t offset_conf = sizeof(config->mac);
1133 struct rte_pci_device *pci_dev;
1135 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
1137 eth_dev->dev_ops = &virtio_eth_dev_ops;
1138 eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1140 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1141 rx_func_get(eth_dev);
1145 /* Allocate memory for storing MAC addresses */
1146 eth_dev->data->mac_addrs = rte_zmalloc("virtio", ETHER_ADDR_LEN, 0);
1147 if (eth_dev->data->mac_addrs == NULL) {
1149 "Failed to allocate %d bytes needed to store MAC addresses",
1154 pci_dev = eth_dev->pci_dev;
1155 if (virtio_resource_init(pci_dev) < 0)
1158 hw->use_msix = virtio_has_msix(&pci_dev->addr);
1159 hw->io_base = (uint32_t)(uintptr_t)pci_dev->mem_resource[0].addr;
1161 /* Reset the device although not necessary at startup */
1164 /* Tell the host we've noticed this device. */
1165 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1167 /* Tell the host we've known how to drive the device. */
1168 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1169 virtio_negotiate_features(hw);
1171 rx_func_get(eth_dev);
1173 /* Setting up rx_header size for the device */
1174 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1175 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1177 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1179 /* Copy the permanent MAC address to: virtio_hw */
1180 virtio_get_hwaddr(hw);
1181 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1182 ð_dev->data->mac_addrs[0]);
1184 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1185 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1186 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1188 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1189 config = &local_config;
1191 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1192 offset_conf += sizeof(config->status);
1195 "VIRTIO_NET_F_STATUS is not supported");
1199 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1200 offset_conf += sizeof(config->max_virtqueue_pairs);
1203 "VIRTIO_NET_F_MQ is not supported");
1204 config->max_virtqueue_pairs = 1;
1207 vtpci_read_dev_config(hw, 0, (uint8_t *)config, offset_conf);
1210 (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
1211 VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
1213 (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
1214 VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
1216 virtio_dev_cq_queue_setup(eth_dev,
1217 config->max_virtqueue_pairs * 2,
1220 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1221 config->max_virtqueue_pairs);
1222 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1224 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1225 config->mac[0], config->mac[1],
1226 config->mac[2], config->mac[3],
1227 config->mac[4], config->mac[5]);
1229 hw->max_rx_queues = 1;
1230 hw->max_tx_queues = 1;
1233 eth_dev->data->nb_rx_queues = hw->max_rx_queues;
1234 eth_dev->data->nb_tx_queues = hw->max_tx_queues;
1236 PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d hw->max_tx_queues=%d",
1237 hw->max_rx_queues, hw->max_tx_queues);
1238 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1239 eth_dev->data->port_id, pci_dev->id.vendor_id,
1240 pci_dev->id.device_id);
1242 /* Setup interrupt callback */
1243 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1244 rte_intr_callback_register(&pci_dev->intr_handle,
1245 virtio_interrupt_handler, eth_dev);
1247 virtio_dev_cq_start(eth_dev);
1252 static struct eth_driver rte_virtio_pmd = {
1254 .name = "rte_virtio_pmd",
1255 .id_table = pci_id_virtio_map,
1257 .eth_dev_init = eth_virtio_dev_init,
1258 .dev_private_size = sizeof(struct virtio_hw),
1262 * Driver initialization routine.
1263 * Invoked once at EAL init time.
1264 * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1265 * Returns 0 on success.
1268 rte_virtio_pmd_init(const char *name __rte_unused,
1269 const char *param __rte_unused)
1271 if (rte_eal_iopl_init() != 0) {
1272 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1276 rte_eth_driver_register(&rte_virtio_pmd);
1281 * Only 1 queue is supported, no queue release related operation
1284 virtio_dev_rx_queue_release(__rte_unused void *rxq)
1289 virtio_dev_tx_queue_release(__rte_unused void *txq)
1294 * Configure virtio device
1295 * It returns 0 on success.
1298 virtio_dev_configure(struct rte_eth_dev *dev)
1300 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1301 struct virtio_hw *hw = dev->data->dev_private;
1302 struct rte_pci_device *pci_dev = dev->pci_dev;
1304 PMD_INIT_LOG(DEBUG, "configure");
1306 if (rxmode->hw_ip_checksum) {
1307 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1311 hw->vlan_strip = rxmode->hw_vlan_strip;
1313 if (rxmode->hw_vlan_filter
1314 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1316 "vlan filtering not available on this host");
1320 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1321 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1322 PMD_DRV_LOG(ERR, "failed to set config vector");
1331 virtio_dev_start(struct rte_eth_dev *dev)
1333 uint16_t nb_queues, i;
1334 struct virtio_hw *hw = dev->data->dev_private;
1335 struct rte_pci_device *pci_dev = dev->pci_dev;
1337 /* check if lsc interrupt feature is enabled */
1338 if ((dev->data->dev_conf.intr_conf.lsc) &&
1339 (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
1340 if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1341 PMD_DRV_LOG(ERR, "link status not supported by host");
1345 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1346 PMD_DRV_LOG(ERR, "interrupt enable failed");
1351 /* Initialize Link state */
1352 virtio_dev_link_update(dev, 0);
1354 /* On restart after stop do not touch queues */
1358 /* Do final configuration before rx/tx engine starts */
1359 virtio_dev_rxtx_start(dev);
1360 vtpci_reinit_complete(hw);
1364 /*Notify the backend
1365 *Otherwise the tap backend might already stop its queue due to fullness.
1366 *vhost backend will have no chance to be waked up
1368 nb_queues = dev->data->nb_rx_queues;
1369 if (nb_queues > 1) {
1370 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1374 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1376 for (i = 0; i < nb_queues; i++)
1377 virtqueue_notify(dev->data->rx_queues[i]);
1379 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1381 for (i = 0; i < dev->data->nb_rx_queues; i++)
1382 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1384 for (i = 0; i < dev->data->nb_tx_queues; i++)
1385 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1390 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1392 struct rte_mbuf *buf;
1393 int i, mbuf_num = 0;
1395 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1397 "Before freeing rxq[%d] used and unused buf", i);
1398 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1400 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1401 dev->data->rx_queues[i])) != NULL) {
1402 rte_pktmbuf_free(buf);
1406 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1408 "After freeing rxq[%d] used and unused buf", i);
1409 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1412 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1414 "Before freeing txq[%d] used and unused bufs",
1416 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1419 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1420 dev->data->tx_queues[i])) != NULL) {
1421 rte_pktmbuf_free(buf);
1426 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1428 "After freeing txq[%d] used and unused buf", i);
1429 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1434 * Stop device: disable interrupt and mark link down
1437 virtio_dev_stop(struct rte_eth_dev *dev)
1439 struct rte_eth_link link;
1441 PMD_INIT_LOG(DEBUG, "stop");
1443 if (dev->data->dev_conf.intr_conf.lsc)
1444 rte_intr_disable(&dev->pci_dev->intr_handle);
1446 memset(&link, 0, sizeof(link));
1447 virtio_dev_atomic_write_link_status(dev, &link);
1451 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1453 struct rte_eth_link link, old;
1455 struct virtio_hw *hw = dev->data->dev_private;
1456 memset(&link, 0, sizeof(link));
1457 virtio_dev_atomic_read_link_status(dev, &link);
1459 link.link_duplex = FULL_DUPLEX;
1460 link.link_speed = SPEED_10G;
1462 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1463 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1464 vtpci_read_dev_config(hw,
1465 offsetof(struct virtio_net_config, status),
1466 &status, sizeof(status));
1467 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1468 link.link_status = 0;
1469 PMD_INIT_LOG(DEBUG, "Port %d is down",
1470 dev->data->port_id);
1472 link.link_status = 1;
1473 PMD_INIT_LOG(DEBUG, "Port %d is up",
1474 dev->data->port_id);
1477 link.link_status = 1; /* Link up */
1479 virtio_dev_atomic_write_link_status(dev, &link);
1481 return (old.link_status == link.link_status) ? -1 : 0;
1485 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1487 struct virtio_hw *hw = dev->data->dev_private;
1489 dev_info->driver_name = dev->driver->pci_drv.name;
1490 dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1491 dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1492 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1493 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1494 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1495 dev_info->default_txconf = (struct rte_eth_txconf) {
1496 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
1501 * It enables testpmd to collect per queue stats.
1504 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1505 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1506 __rte_unused uint8_t is_rx)
1511 static struct rte_driver rte_virtio_driver = {
1513 .init = rte_virtio_pmd_init,
1516 PMD_REGISTER_DRIVER(rte_virtio_driver);