4 * Copyright(c) 2010-2015 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>
54 #include <rte_errno.h>
56 #include <rte_memory.h>
60 #include "virtio_ethdev.h"
61 #include "virtio_pci.h"
62 #include "virtio_logs.h"
63 #include "virtqueue.h"
64 #include "virtio_rxtx.h"
67 static int eth_virtio_dev_init(struct rte_eth_dev *eth_dev);
68 static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev);
69 static int virtio_dev_configure(struct rte_eth_dev *dev);
70 static int virtio_dev_start(struct rte_eth_dev *dev);
71 static void virtio_dev_stop(struct rte_eth_dev *dev);
72 static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
73 static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
74 static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
75 static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
76 static void virtio_dev_info_get(struct rte_eth_dev *dev,
77 struct rte_eth_dev_info *dev_info);
78 static int virtio_dev_link_update(struct rte_eth_dev *dev,
79 __rte_unused int wait_to_complete);
81 static void virtio_set_hwaddr(struct virtio_hw *hw);
82 static void virtio_get_hwaddr(struct virtio_hw *hw);
84 static void virtio_dev_stats_get(struct rte_eth_dev *dev,
85 struct rte_eth_stats *stats);
86 static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
87 struct rte_eth_xstats *xstats, unsigned n);
88 static void virtio_dev_stats_reset(struct rte_eth_dev *dev);
89 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
90 static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
91 uint16_t vlan_id, int on);
92 static void virtio_mac_addr_add(struct rte_eth_dev *dev,
93 struct ether_addr *mac_addr,
94 uint32_t index, uint32_t vmdq __rte_unused);
95 static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
96 static void virtio_mac_addr_set(struct rte_eth_dev *dev,
97 struct ether_addr *mac_addr);
99 static int virtio_dev_queue_stats_mapping_set(
100 __rte_unused struct rte_eth_dev *eth_dev,
101 __rte_unused uint16_t queue_id,
102 __rte_unused uint8_t stat_idx,
103 __rte_unused uint8_t is_rx);
106 * The set of PCI devices this driver supports
108 static const struct rte_pci_id pci_id_virtio_map[] = {
110 #define RTE_PCI_DEV_ID_DECL_VIRTIO(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
111 #include "rte_pci_dev_ids.h"
113 { .vendor_id = 0, /* sentinel */ },
116 struct rte_virtio_xstats_name_off {
117 char name[RTE_ETH_XSTATS_NAME_SIZE];
121 /* [rt]x_qX_ is prepended to the name string here */
122 static const struct rte_virtio_xstats_name_off rte_virtio_q_stat_strings[] = {
123 {"good_packets", offsetof(struct virtqueue, packets)},
124 {"good_bytes", offsetof(struct virtqueue, bytes)},
125 {"errors", offsetof(struct virtqueue, errors)},
126 {"multicast_packets", offsetof(struct virtqueue, multicast)},
127 {"broadcast_packets", offsetof(struct virtqueue, broadcast)},
128 {"undersize_packets", offsetof(struct virtqueue, size_bins[0])},
129 {"size_64_packets", offsetof(struct virtqueue, size_bins[1])},
130 {"size_65_127_packets", offsetof(struct virtqueue, size_bins[2])},
131 {"size_128_255_packets", offsetof(struct virtqueue, size_bins[3])},
132 {"size_256_511_packets", offsetof(struct virtqueue, size_bins[4])},
133 {"size_512_1023_packets", offsetof(struct virtqueue, size_bins[5])},
134 {"size_1024_1517_packets", offsetof(struct virtqueue, size_bins[6])},
135 {"size_1518_max_packets", offsetof(struct virtqueue, size_bins[7])},
138 #define VIRTIO_NB_Q_XSTATS (sizeof(rte_virtio_q_stat_strings) / \
139 sizeof(rte_virtio_q_stat_strings[0]))
142 virtio_send_command(struct virtqueue *vq, struct virtio_pmd_ctrl *ctrl,
143 int *dlen, int pkt_num)
147 virtio_net_ctrl_ack status = ~0;
148 struct virtio_pmd_ctrl result;
150 ctrl->status = status;
152 if (!(vq && vq->hw->cvq)) {
154 "%s(): Control queue is not supported.",
158 head = vq->vq_desc_head_idx;
160 PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, "
161 "vq->hw->cvq = %p vq = %p",
162 vq->vq_desc_head_idx, status, vq->hw->cvq, vq);
164 if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1))
167 memcpy(vq->virtio_net_hdr_mz->addr, ctrl,
168 sizeof(struct virtio_pmd_ctrl));
171 * Format is enforced in qemu code:
172 * One TX packet for header;
173 * At least one TX packet per argument;
174 * One RX packet for ACK.
176 vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT;
177 vq->vq_ring.desc[head].addr = vq->virtio_net_hdr_mz->phys_addr;
178 vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr);
180 i = vq->vq_ring.desc[head].next;
182 for (k = 0; k < pkt_num; k++) {
183 vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT;
184 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
185 + sizeof(struct virtio_net_ctrl_hdr)
186 + sizeof(ctrl->status) + sizeof(uint8_t)*sum;
187 vq->vq_ring.desc[i].len = dlen[k];
190 i = vq->vq_ring.desc[i].next;
193 vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
194 vq->vq_ring.desc[i].addr = vq->virtio_net_hdr_mz->phys_addr
195 + sizeof(struct virtio_net_ctrl_hdr);
196 vq->vq_ring.desc[i].len = sizeof(ctrl->status);
199 vq->vq_desc_head_idx = vq->vq_ring.desc[i].next;
201 vq_update_avail_ring(vq, head);
202 vq_update_avail_idx(vq);
204 PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index);
206 virtqueue_notify(vq);
209 while (vq->vq_used_cons_idx == vq->vq_ring.used->idx) {
214 while (vq->vq_used_cons_idx != vq->vq_ring.used->idx) {
215 uint32_t idx, desc_idx, used_idx;
216 struct vring_used_elem *uep;
218 used_idx = (uint32_t)(vq->vq_used_cons_idx
219 & (vq->vq_nentries - 1));
220 uep = &vq->vq_ring.used->ring[used_idx];
221 idx = (uint32_t) uep->id;
224 while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) {
225 desc_idx = vq->vq_ring.desc[desc_idx].next;
229 vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx;
230 vq->vq_desc_head_idx = idx;
232 vq->vq_used_cons_idx++;
236 PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d",
237 vq->vq_free_cnt, vq->vq_desc_head_idx);
239 memcpy(&result, vq->virtio_net_hdr_mz->addr,
240 sizeof(struct virtio_pmd_ctrl));
242 return result.status;
246 virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
248 struct virtio_hw *hw = dev->data->dev_private;
249 struct virtio_pmd_ctrl ctrl;
253 ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
254 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
255 memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
257 dlen[0] = sizeof(uint16_t);
259 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
261 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
262 "failed, this is too late now...");
270 virtio_dev_queue_release(struct virtqueue *vq) {
271 struct virtio_hw *hw;
275 /* Select and deactivate the queue */
276 VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vq->vq_queue_index);
277 VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN, 0);
279 rte_free(vq->sw_ring);
284 int virtio_dev_queue_setup(struct rte_eth_dev *dev,
287 uint16_t vtpci_queue_idx,
289 unsigned int socket_id,
290 struct virtqueue **pvq)
292 char vq_name[VIRTQUEUE_MAX_NAME_SZ];
293 const struct rte_memzone *mz;
294 unsigned int vq_size, size;
295 struct virtio_hw *hw = dev->data->dev_private;
296 struct virtqueue *vq = NULL;
298 /* Write the virtqueue index to the Queue Select Field */
299 VIRTIO_WRITE_REG_2(hw, VIRTIO_PCI_QUEUE_SEL, vtpci_queue_idx);
300 PMD_INIT_LOG(DEBUG, "selecting queue: %u", vtpci_queue_idx);
303 * Read the virtqueue size from the Queue Size field
304 * Always power of 2 and if 0 virtqueue does not exist
306 vq_size = VIRTIO_READ_REG_2(hw, VIRTIO_PCI_QUEUE_NUM);
307 PMD_INIT_LOG(DEBUG, "vq_size: %u nb_desc:%u", vq_size, nb_desc);
309 PMD_INIT_LOG(ERR, "%s: virtqueue does not exist", __func__);
313 if (!rte_is_power_of_2(vq_size)) {
314 PMD_INIT_LOG(ERR, "%s: virtqueue size is not powerof 2", __func__);
318 if (queue_type == VTNET_RQ) {
319 snprintf(vq_name, sizeof(vq_name), "port%d_rvq%d",
320 dev->data->port_id, queue_idx);
321 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
322 vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
323 vq->sw_ring = rte_zmalloc_socket("rxq->sw_ring",
324 (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) *
325 sizeof(vq->sw_ring[0]), RTE_CACHE_LINE_SIZE, socket_id);
326 } else if (queue_type == VTNET_TQ) {
327 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d",
328 dev->data->port_id, queue_idx);
329 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
330 vq_size * sizeof(struct vq_desc_extra), RTE_CACHE_LINE_SIZE);
331 } else if (queue_type == VTNET_CQ) {
332 snprintf(vq_name, sizeof(vq_name), "port%d_cvq",
334 vq = rte_zmalloc(vq_name, sizeof(struct virtqueue) +
335 vq_size * sizeof(struct vq_desc_extra),
336 RTE_CACHE_LINE_SIZE);
339 PMD_INIT_LOG(ERR, "%s: Can not allocate virtqueue", __func__);
342 if (queue_type == VTNET_RQ && vq->sw_ring == NULL) {
343 PMD_INIT_LOG(ERR, "%s: Can not allocate RX soft ring",
350 vq->port_id = dev->data->port_id;
351 vq->queue_id = queue_idx;
352 vq->vq_queue_index = vtpci_queue_idx;
353 vq->vq_nentries = vq_size;
355 if (nb_desc == 0 || nb_desc > vq_size)
357 vq->vq_free_cnt = nb_desc;
360 * Reserve a memzone for vring elements
362 size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
363 vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
364 PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", size, vq->vq_ring_size);
366 mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
367 socket_id, 0, VIRTIO_PCI_VRING_ALIGN);
369 if (rte_errno == EEXIST)
370 mz = rte_memzone_lookup(vq_name);
378 * Virtio PCI device VIRTIO_PCI_QUEUE_PF register is 32bit,
379 * and only accepts 32 bit page frame number.
380 * Check if the allocated physical memory exceeds 16TB.
382 if ((mz->phys_addr + vq->vq_ring_size - 1) >> (VIRTIO_PCI_QUEUE_ADDR_SHIFT + 32)) {
383 PMD_INIT_LOG(ERR, "vring address shouldn't be above 16TB!");
388 memset(mz->addr, 0, sizeof(mz->len));
390 vq->vq_ring_mem = mz->phys_addr;
391 vq->vq_ring_virt_mem = mz->addr;
392 PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%"PRIx64, (uint64_t)mz->phys_addr);
393 PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%"PRIx64, (uint64_t)(uintptr_t)mz->addr);
394 vq->virtio_net_hdr_mz = NULL;
395 vq->virtio_net_hdr_mem = 0;
397 if (queue_type == VTNET_TQ) {
399 * For each xmit packet, allocate a virtio_net_hdr
401 snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
402 dev->data->port_id, queue_idx);
403 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
404 vq_size * hw->vtnet_hdr_size,
405 socket_id, 0, RTE_CACHE_LINE_SIZE);
406 if (vq->virtio_net_hdr_mz == NULL) {
407 if (rte_errno == EEXIST)
408 vq->virtio_net_hdr_mz =
409 rte_memzone_lookup(vq_name);
410 if (vq->virtio_net_hdr_mz == NULL) {
415 vq->virtio_net_hdr_mem =
416 vq->virtio_net_hdr_mz->phys_addr;
417 memset(vq->virtio_net_hdr_mz->addr, 0,
418 vq_size * hw->vtnet_hdr_size);
419 } else if (queue_type == VTNET_CQ) {
420 /* Allocate a page for control vq command, data and status */
421 snprintf(vq_name, sizeof(vq_name), "port%d_cvq_hdrzone",
423 vq->virtio_net_hdr_mz = rte_memzone_reserve_aligned(vq_name,
424 PAGE_SIZE, socket_id, 0, RTE_CACHE_LINE_SIZE);
425 if (vq->virtio_net_hdr_mz == NULL) {
426 if (rte_errno == EEXIST)
427 vq->virtio_net_hdr_mz =
428 rte_memzone_lookup(vq_name);
429 if (vq->virtio_net_hdr_mz == NULL) {
434 vq->virtio_net_hdr_mem =
435 vq->virtio_net_hdr_mz->phys_addr;
436 memset(vq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE);
440 * Set guest physical address of the virtqueue
441 * in VIRTIO_PCI_QUEUE_PFN config register of device
443 VIRTIO_WRITE_REG_4(hw, VIRTIO_PCI_QUEUE_PFN,
444 mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
450 virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx,
453 struct virtqueue *vq;
455 struct virtio_hw *hw = dev->data->dev_private;
457 PMD_INIT_FUNC_TRACE();
458 ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX,
459 vtpci_queue_idx, 0, socket_id, &vq);
461 PMD_INIT_LOG(ERR, "control vq initialization failed");
470 virtio_free_queues(struct rte_eth_dev *dev)
474 for (i = 0; i < dev->data->nb_rx_queues; i++)
475 virtio_dev_rx_queue_release(dev->data->rx_queues[i]);
477 dev->data->nb_rx_queues = 0;
479 for (i = 0; i < dev->data->nb_tx_queues; i++)
480 virtio_dev_tx_queue_release(dev->data->tx_queues[i]);
482 dev->data->nb_tx_queues = 0;
486 virtio_dev_close(struct rte_eth_dev *dev)
488 struct virtio_hw *hw = dev->data->dev_private;
489 struct rte_pci_device *pci_dev = dev->pci_dev;
491 PMD_INIT_LOG(DEBUG, "virtio_dev_close");
494 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
495 vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR);
498 virtio_dev_free_mbufs(dev);
499 virtio_free_queues(dev);
503 virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
505 struct virtio_hw *hw = dev->data->dev_private;
506 struct virtio_pmd_ctrl ctrl;
510 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
511 PMD_INIT_LOG(INFO, "host does not support rx control\n");
515 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
516 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
520 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
522 PMD_INIT_LOG(ERR, "Failed to enable promisc");
526 virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
528 struct virtio_hw *hw = dev->data->dev_private;
529 struct virtio_pmd_ctrl ctrl;
533 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
534 PMD_INIT_LOG(INFO, "host does not support rx control\n");
538 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
539 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
543 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
545 PMD_INIT_LOG(ERR, "Failed to disable promisc");
549 virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
551 struct virtio_hw *hw = dev->data->dev_private;
552 struct virtio_pmd_ctrl ctrl;
556 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
557 PMD_INIT_LOG(INFO, "host does not support rx control\n");
561 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
562 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
566 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
568 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
572 virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
574 struct virtio_hw *hw = dev->data->dev_private;
575 struct virtio_pmd_ctrl ctrl;
579 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
580 PMD_INIT_LOG(INFO, "host does not support rx control\n");
584 ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
585 ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
589 ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
591 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
595 * dev_ops for virtio, bare necessities for basic operation
597 static const struct eth_dev_ops virtio_eth_dev_ops = {
598 .dev_configure = virtio_dev_configure,
599 .dev_start = virtio_dev_start,
600 .dev_stop = virtio_dev_stop,
601 .dev_close = virtio_dev_close,
602 .promiscuous_enable = virtio_dev_promiscuous_enable,
603 .promiscuous_disable = virtio_dev_promiscuous_disable,
604 .allmulticast_enable = virtio_dev_allmulticast_enable,
605 .allmulticast_disable = virtio_dev_allmulticast_disable,
607 .dev_infos_get = virtio_dev_info_get,
608 .stats_get = virtio_dev_stats_get,
609 .xstats_get = virtio_dev_xstats_get,
610 .stats_reset = virtio_dev_stats_reset,
611 .xstats_reset = virtio_dev_stats_reset,
612 .link_update = virtio_dev_link_update,
613 .rx_queue_setup = virtio_dev_rx_queue_setup,
614 .rx_queue_release = virtio_dev_rx_queue_release,
615 .tx_queue_setup = virtio_dev_tx_queue_setup,
616 .tx_queue_release = virtio_dev_tx_queue_release,
617 /* collect stats per queue */
618 .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
619 .vlan_filter_set = virtio_vlan_filter_set,
620 .mac_addr_add = virtio_mac_addr_add,
621 .mac_addr_remove = virtio_mac_addr_remove,
622 .mac_addr_set = virtio_mac_addr_set,
626 virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev,
627 struct rte_eth_link *link)
629 struct rte_eth_link *dst = link;
630 struct rte_eth_link *src = &(dev->data->dev_link);
632 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
633 *(uint64_t *)src) == 0)
640 * Atomically writes the link status information into global
641 * structure rte_eth_dev.
644 * - Pointer to the structure rte_eth_dev to read from.
645 * - Pointer to the buffer to be saved with the link status.
648 * - On success, zero.
649 * - On failure, negative value.
652 virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev,
653 struct rte_eth_link *link)
655 struct rte_eth_link *dst = &(dev->data->dev_link);
656 struct rte_eth_link *src = link;
658 if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
659 *(uint64_t *)src) == 0)
666 virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
670 for (i = 0; i < dev->data->nb_tx_queues; i++) {
671 const struct virtqueue *txvq = dev->data->tx_queues[i];
675 stats->opackets += txvq->packets;
676 stats->obytes += txvq->bytes;
677 stats->oerrors += txvq->errors;
679 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
680 stats->q_opackets[i] = txvq->packets;
681 stats->q_obytes[i] = txvq->bytes;
685 for (i = 0; i < dev->data->nb_rx_queues; i++) {
686 const struct virtqueue *rxvq = dev->data->rx_queues[i];
690 stats->ipackets += rxvq->packets;
691 stats->ibytes += rxvq->bytes;
692 stats->ierrors += rxvq->errors;
694 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
695 stats->q_ipackets[i] = rxvq->packets;
696 stats->q_ibytes[i] = rxvq->bytes;
700 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
704 virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstats *xstats,
710 unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_Q_XSTATS +
711 dev->data->nb_rx_queues * VIRTIO_NB_Q_XSTATS;
716 for (i = 0; i < dev->data->nb_rx_queues; i++) {
717 struct virtqueue *rxvq = dev->data->rx_queues[i];
724 for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) {
725 snprintf(xstats[count].name, sizeof(xstats[count].name),
727 rte_virtio_q_stat_strings[t].name);
728 xstats[count].value = *(uint64_t *)(((char *)rxvq) +
729 rte_virtio_q_stat_strings[t].offset);
734 for (i = 0; i < dev->data->nb_tx_queues; i++) {
735 struct virtqueue *txvq = dev->data->tx_queues[i];
742 for (t = 0; t < VIRTIO_NB_Q_XSTATS; t++) {
743 snprintf(xstats[count].name, sizeof(xstats[count].name),
745 rte_virtio_q_stat_strings[t].name);
746 xstats[count].value = *(uint64_t *)(((char *)txvq) +
747 rte_virtio_q_stat_strings[t].offset);
756 virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
758 virtio_update_stats(dev, stats);
762 virtio_dev_stats_reset(struct rte_eth_dev *dev)
766 for (i = 0; i < dev->data->nb_tx_queues; i++) {
767 struct virtqueue *txvq = dev->data->tx_queues[i];
776 memset(txvq->size_bins, 0, sizeof(txvq->size_bins[0]) * 8);
779 for (i = 0; i < dev->data->nb_rx_queues; i++) {
780 struct virtqueue *rxvq = dev->data->rx_queues[i];
789 memset(rxvq->size_bins, 0, sizeof(rxvq->size_bins[0]) * 8);
794 virtio_set_hwaddr(struct virtio_hw *hw)
796 vtpci_write_dev_config(hw,
797 offsetof(struct virtio_net_config, mac),
798 &hw->mac_addr, ETHER_ADDR_LEN);
802 virtio_get_hwaddr(struct virtio_hw *hw)
804 if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) {
805 vtpci_read_dev_config(hw,
806 offsetof(struct virtio_net_config, mac),
807 &hw->mac_addr, ETHER_ADDR_LEN);
809 eth_random_addr(&hw->mac_addr[0]);
810 virtio_set_hwaddr(hw);
815 virtio_mac_table_set(struct virtio_hw *hw,
816 const struct virtio_net_ctrl_mac *uc,
817 const struct virtio_net_ctrl_mac *mc)
819 struct virtio_pmd_ctrl ctrl;
822 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
823 PMD_DRV_LOG(INFO, "host does not support mac table\n");
827 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
828 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
830 len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries);
831 memcpy(ctrl.data, uc, len[0]);
833 len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries);
834 memcpy(ctrl.data + len[0], mc, len[1]);
836 err = virtio_send_command(hw->cvq, &ctrl, len, 2);
838 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
842 virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
843 uint32_t index, uint32_t vmdq __rte_unused)
845 struct virtio_hw *hw = dev->data->dev_private;
846 const struct ether_addr *addrs = dev->data->mac_addrs;
848 struct virtio_net_ctrl_mac *uc, *mc;
850 if (index >= VIRTIO_MAX_MAC_ADDRS) {
851 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
855 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
857 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
860 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
861 const struct ether_addr *addr
862 = (i == index) ? mac_addr : addrs + i;
863 struct virtio_net_ctrl_mac *tbl
864 = is_multicast_ether_addr(addr) ? mc : uc;
866 memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN);
869 virtio_mac_table_set(hw, uc, mc);
873 virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
875 struct virtio_hw *hw = dev->data->dev_private;
876 struct ether_addr *addrs = dev->data->mac_addrs;
877 struct virtio_net_ctrl_mac *uc, *mc;
880 if (index >= VIRTIO_MAX_MAC_ADDRS) {
881 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
885 uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries));
887 mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries));
890 for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
891 struct virtio_net_ctrl_mac *tbl;
893 if (i == index || is_zero_ether_addr(addrs + i))
896 tbl = is_multicast_ether_addr(addrs + i) ? mc : uc;
897 memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN);
900 virtio_mac_table_set(hw, uc, mc);
904 virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
906 struct virtio_hw *hw = dev->data->dev_private;
908 memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN);
910 /* Use atomic update if available */
911 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
912 struct virtio_pmd_ctrl ctrl;
913 int len = ETHER_ADDR_LEN;
915 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
916 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
918 memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN);
919 virtio_send_command(hw->cvq, &ctrl, &len, 1);
920 } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC))
921 virtio_set_hwaddr(hw);
925 virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
927 struct virtio_hw *hw = dev->data->dev_private;
928 struct virtio_pmd_ctrl ctrl;
931 if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
934 ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
935 ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
936 memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
937 len = sizeof(vlan_id);
939 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
943 virtio_negotiate_features(struct virtio_hw *hw)
945 uint32_t host_features;
947 /* Prepare guest_features: feature that driver wants to support */
948 hw->guest_features = VIRTIO_PMD_GUEST_FEATURES;
949 PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %x",
952 /* Read device(host) feature bits */
953 host_features = VIRTIO_READ_REG_4(hw, VIRTIO_PCI_HOST_FEATURES);
954 PMD_INIT_LOG(DEBUG, "host_features before negotiate = %x",
958 * Negotiate features: Subset of device feature bits are written back
959 * guest feature bits.
961 hw->guest_features = vtpci_negotiate_features(hw, host_features);
962 PMD_INIT_LOG(DEBUG, "features after negotiate = %x",
966 #ifdef RTE_EXEC_ENV_LINUXAPP
968 parse_sysfs_value(const char *filename, unsigned long *val)
974 f = fopen(filename, "r");
976 PMD_INIT_LOG(ERR, "%s(): cannot open sysfs value %s",
981 if (fgets(buf, sizeof(buf), f) == NULL) {
982 PMD_INIT_LOG(ERR, "%s(): cannot read sysfs value %s",
987 *val = strtoul(buf, &end, 0);
988 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
989 PMD_INIT_LOG(ERR, "%s(): cannot parse sysfs value %s",
998 static int get_uio_dev(struct rte_pci_addr *loc, char *buf, unsigned int buflen,
999 unsigned int *uio_num)
1003 char dirname[PATH_MAX];
1005 /* depending on kernel version, uio can be located in uio/uioX
1007 snprintf(dirname, sizeof(dirname),
1008 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/uio",
1009 loc->domain, loc->bus, loc->devid, loc->function);
1010 dir = opendir(dirname);
1012 /* retry with the parent directory */
1013 snprintf(dirname, sizeof(dirname),
1014 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT,
1015 loc->domain, loc->bus, loc->devid, loc->function);
1016 dir = opendir(dirname);
1019 PMD_INIT_LOG(ERR, "Cannot opendir %s", dirname);
1024 /* take the first file starting with "uio" */
1025 while ((e = readdir(dir)) != NULL) {
1026 /* format could be uio%d ...*/
1027 int shortprefix_len = sizeof("uio") - 1;
1028 /* ... or uio:uio%d */
1029 int longprefix_len = sizeof("uio:uio") - 1;
1032 if (strncmp(e->d_name, "uio", 3) != 0)
1035 /* first try uio%d */
1037 *uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
1038 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
1039 snprintf(buf, buflen, "%s/uio%u", dirname, *uio_num);
1043 /* then try uio:uio%d */
1045 *uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
1046 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {
1047 snprintf(buf, buflen, "%s/uio:uio%u", dirname,
1054 /* No uio resource found */
1056 PMD_INIT_LOG(ERR, "Could not find uio resource");
1064 virtio_has_msix(const struct rte_pci_addr *loc)
1067 char dirname[PATH_MAX];
1069 snprintf(dirname, sizeof(dirname),
1070 SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/msi_irqs",
1071 loc->domain, loc->bus, loc->devid, loc->function);
1073 d = opendir(dirname);
1080 /* Extract I/O port numbers from sysfs */
1081 static int virtio_resource_init_by_uio(struct rte_pci_device *pci_dev)
1083 char dirname[PATH_MAX];
1084 char filename[PATH_MAX];
1085 unsigned long start, size;
1086 unsigned int uio_num;
1088 if (get_uio_dev(&pci_dev->addr, dirname, sizeof(dirname), &uio_num) < 0)
1091 /* get portio size */
1092 snprintf(filename, sizeof(filename),
1093 "%s/portio/port0/size", dirname);
1094 if (parse_sysfs_value(filename, &size) < 0) {
1095 PMD_INIT_LOG(ERR, "%s(): cannot parse size",
1100 /* get portio start */
1101 snprintf(filename, sizeof(filename),
1102 "%s/portio/port0/start", dirname);
1103 if (parse_sysfs_value(filename, &start) < 0) {
1104 PMD_INIT_LOG(ERR, "%s(): cannot parse portio start",
1108 pci_dev->mem_resource[0].addr = (void *)(uintptr_t)start;
1109 pci_dev->mem_resource[0].len = (uint64_t)size;
1111 "PCI Port IO found start=0x%lx with size=0x%lx",
1115 memset(dirname, 0, sizeof(dirname));
1116 snprintf(dirname, sizeof(dirname), "/dev/uio%u", uio_num);
1117 pci_dev->intr_handle.fd = open(dirname, O_RDWR);
1118 if (pci_dev->intr_handle.fd < 0) {
1119 PMD_INIT_LOG(ERR, "Cannot open %s: %s\n",
1120 dirname, strerror(errno));
1124 pci_dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
1125 pci_dev->driver->drv_flags |= RTE_PCI_DRV_INTR_LSC;
1130 /* Extract port I/O numbers from proc/ioports */
1131 static int virtio_resource_init_by_ioports(struct rte_pci_device *pci_dev)
1133 uint16_t start, end;
1141 snprintf(pci_id, sizeof(pci_id), PCI_PRI_FMT,
1142 pci_dev->addr.domain,
1144 pci_dev->addr.devid,
1145 pci_dev->addr.function);
1147 fp = fopen("/proc/ioports", "r");
1149 PMD_INIT_LOG(ERR, "%s(): can't open ioports", __func__);
1153 while (getdelim(&line, &linesz, '\n', fp) > 0) {
1158 n = strcspn(ptr, ":");
1162 while (*left && isspace(*left))
1165 if (!strncmp(left, pci_id, strlen(pci_id))) {
1168 while (*ptr && isspace(*ptr))
1171 sscanf(ptr, "%04hx-%04hx", &start, &end);
1172 size = end - start + 1;
1184 pci_dev->mem_resource[0].addr = (void *)(uintptr_t)(uint32_t)start;
1185 pci_dev->mem_resource[0].len = (uint64_t)size;
1187 "PCI Port IO found start=0x%x with size=0x%x",
1190 /* can't support lsc interrupt without uio */
1191 pci_dev->driver->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
1196 /* Extract I/O port numbers from sysfs */
1197 static int virtio_resource_init(struct rte_pci_device *pci_dev)
1199 if (virtio_resource_init_by_uio(pci_dev) == 0)
1202 return virtio_resource_init_by_ioports(pci_dev);
1207 virtio_has_msix(const struct rte_pci_addr *loc __rte_unused)
1209 /* nic_uio does not enable interrupts, return 0 (false). */
1213 static int virtio_resource_init(struct rte_pci_device *pci_dev __rte_unused)
1215 /* no setup required */
1221 * Process Virtio Config changed interrupt and call the callback
1222 * if link state changed.
1225 virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
1228 struct rte_eth_dev *dev = param;
1229 struct virtio_hw *hw = dev->data->dev_private;
1232 /* Read interrupt status which clears interrupt */
1233 isr = vtpci_isr(hw);
1234 PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
1236 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
1237 PMD_DRV_LOG(ERR, "interrupt enable failed");
1239 if (isr & VIRTIO_PCI_ISR_CONFIG) {
1240 if (virtio_dev_link_update(dev, 0) == 0)
1241 _rte_eth_dev_callback_process(dev,
1242 RTE_ETH_EVENT_INTR_LSC);
1248 rx_func_get(struct rte_eth_dev *eth_dev)
1250 struct virtio_hw *hw = eth_dev->data->dev_private;
1251 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1252 eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
1254 eth_dev->rx_pkt_burst = &virtio_recv_pkts;
1258 * This function is based on probe() function in virtio_pci.c
1259 * It returns 0 on success.
1262 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
1264 struct virtio_hw *hw = eth_dev->data->dev_private;
1265 struct virtio_net_config *config;
1266 struct virtio_net_config local_config;
1267 struct rte_pci_device *pci_dev;
1269 RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr));
1271 eth_dev->dev_ops = &virtio_eth_dev_ops;
1272 eth_dev->tx_pkt_burst = &virtio_xmit_pkts;
1274 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1275 rx_func_get(eth_dev);
1279 /* Allocate memory for storing MAC addresses */
1280 eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0);
1281 if (eth_dev->data->mac_addrs == NULL) {
1283 "Failed to allocate %d bytes needed to store MAC addresses",
1284 VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN);
1288 pci_dev = eth_dev->pci_dev;
1290 if (virtio_resource_init(pci_dev) < 0)
1293 hw->use_msix = virtio_has_msix(&pci_dev->addr);
1294 hw->io_base = (uint32_t)(uintptr_t)pci_dev->mem_resource[0].addr;
1296 /* Reset the device although not necessary at startup */
1299 /* Tell the host we've noticed this device. */
1300 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
1302 /* Tell the host we've known how to drive the device. */
1303 vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
1304 virtio_negotiate_features(hw);
1306 /* If host does not support status then disable LSC */
1307 if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS))
1308 pci_dev->driver->drv_flags &= ~RTE_PCI_DRV_INTR_LSC;
1310 rte_eth_copy_pci_info(eth_dev, pci_dev);
1312 rx_func_get(eth_dev);
1314 /* Setting up rx_header size for the device */
1315 if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF))
1316 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1318 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
1320 /* Copy the permanent MAC address to: virtio_hw */
1321 virtio_get_hwaddr(hw);
1322 ether_addr_copy((struct ether_addr *) hw->mac_addr,
1323 ð_dev->data->mac_addrs[0]);
1325 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1326 hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
1327 hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
1329 if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
1330 config = &local_config;
1332 vtpci_read_dev_config(hw,
1333 offsetof(struct virtio_net_config, mac),
1334 &config->mac, sizeof(config->mac));
1336 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1337 vtpci_read_dev_config(hw,
1338 offsetof(struct virtio_net_config, status),
1339 &config->status, sizeof(config->status));
1342 "VIRTIO_NET_F_STATUS is not supported");
1346 if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) {
1347 vtpci_read_dev_config(hw,
1348 offsetof(struct virtio_net_config, max_virtqueue_pairs),
1349 &config->max_virtqueue_pairs,
1350 sizeof(config->max_virtqueue_pairs));
1353 "VIRTIO_NET_F_MQ is not supported");
1354 config->max_virtqueue_pairs = 1;
1358 (VIRTIO_MAX_RX_QUEUES < config->max_virtqueue_pairs) ?
1359 VIRTIO_MAX_RX_QUEUES : config->max_virtqueue_pairs;
1361 (VIRTIO_MAX_TX_QUEUES < config->max_virtqueue_pairs) ?
1362 VIRTIO_MAX_TX_QUEUES : config->max_virtqueue_pairs;
1364 virtio_dev_cq_queue_setup(eth_dev,
1365 config->max_virtqueue_pairs * 2,
1368 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
1369 config->max_virtqueue_pairs);
1370 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
1372 "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X",
1373 config->mac[0], config->mac[1],
1374 config->mac[2], config->mac[3],
1375 config->mac[4], config->mac[5]);
1377 hw->max_rx_queues = 1;
1378 hw->max_tx_queues = 1;
1381 eth_dev->data->nb_rx_queues = hw->max_rx_queues;
1382 eth_dev->data->nb_tx_queues = hw->max_tx_queues;
1384 PMD_INIT_LOG(DEBUG, "hw->max_rx_queues=%d hw->max_tx_queues=%d",
1385 hw->max_rx_queues, hw->max_tx_queues);
1386 PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1387 eth_dev->data->port_id, pci_dev->id.vendor_id,
1388 pci_dev->id.device_id);
1390 /* Setup interrupt callback */
1391 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1392 rte_intr_callback_register(&pci_dev->intr_handle,
1393 virtio_interrupt_handler, eth_dev);
1395 virtio_dev_cq_start(eth_dev);
1401 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
1403 struct rte_pci_device *pci_dev;
1404 struct virtio_hw *hw = eth_dev->data->dev_private;
1406 PMD_INIT_FUNC_TRACE();
1408 if (rte_eal_process_type() == RTE_PROC_SECONDARY)
1411 if (hw->started == 1) {
1412 virtio_dev_stop(eth_dev);
1413 virtio_dev_close(eth_dev);
1415 pci_dev = eth_dev->pci_dev;
1417 eth_dev->dev_ops = NULL;
1418 eth_dev->tx_pkt_burst = NULL;
1419 eth_dev->rx_pkt_burst = NULL;
1421 virtio_dev_queue_release(hw->cvq);
1423 rte_free(eth_dev->data->mac_addrs);
1424 eth_dev->data->mac_addrs = NULL;
1426 /* reset interrupt callback */
1427 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1428 rte_intr_callback_unregister(&pci_dev->intr_handle,
1429 virtio_interrupt_handler,
1432 PMD_INIT_LOG(DEBUG, "dev_uninit completed");
1437 static struct eth_driver rte_virtio_pmd = {
1439 .name = "rte_virtio_pmd",
1440 .id_table = pci_id_virtio_map,
1441 .drv_flags = RTE_PCI_DRV_DETACHABLE,
1443 .eth_dev_init = eth_virtio_dev_init,
1444 .eth_dev_uninit = eth_virtio_dev_uninit,
1445 .dev_private_size = sizeof(struct virtio_hw),
1449 * Driver initialization routine.
1450 * Invoked once at EAL init time.
1451 * Register itself as the [Poll Mode] Driver of PCI virtio devices.
1452 * Returns 0 on success.
1455 rte_virtio_pmd_init(const char *name __rte_unused,
1456 const char *param __rte_unused)
1458 if (rte_eal_iopl_init() != 0) {
1459 PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD");
1463 rte_eth_driver_register(&rte_virtio_pmd);
1468 * Configure virtio device
1469 * It returns 0 on success.
1472 virtio_dev_configure(struct rte_eth_dev *dev)
1474 const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1475 struct virtio_hw *hw = dev->data->dev_private;
1476 struct rte_pci_device *pci_dev = dev->pci_dev;
1478 PMD_INIT_LOG(DEBUG, "configure");
1480 if (rxmode->hw_ip_checksum) {
1481 PMD_DRV_LOG(ERR, "HW IP checksum not supported");
1485 hw->vlan_strip = rxmode->hw_vlan_strip;
1487 if (rxmode->hw_vlan_filter
1488 && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
1490 "vlan filtering not available on this host");
1494 if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
1495 if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) {
1496 PMD_DRV_LOG(ERR, "failed to set config vector");
1505 virtio_dev_start(struct rte_eth_dev *dev)
1507 uint16_t nb_queues, i;
1508 struct virtio_hw *hw = dev->data->dev_private;
1509 struct rte_pci_device *pci_dev = dev->pci_dev;
1511 /* check if lsc interrupt feature is enabled */
1512 if (dev->data->dev_conf.intr_conf.lsc) {
1513 if (!(pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)) {
1514 PMD_DRV_LOG(ERR, "link status not supported by host");
1518 if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
1519 PMD_DRV_LOG(ERR, "interrupt enable failed");
1524 /* Initialize Link state */
1525 virtio_dev_link_update(dev, 0);
1527 /* On restart after stop do not touch queues */
1531 /* Do final configuration before rx/tx engine starts */
1532 virtio_dev_rxtx_start(dev);
1533 vtpci_reinit_complete(hw);
1537 /*Notify the backend
1538 *Otherwise the tap backend might already stop its queue due to fullness.
1539 *vhost backend will have no chance to be waked up
1541 nb_queues = dev->data->nb_rx_queues;
1542 if (nb_queues > 1) {
1543 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
1547 PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues);
1549 for (i = 0; i < nb_queues; i++)
1550 virtqueue_notify(dev->data->rx_queues[i]);
1552 PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
1554 for (i = 0; i < dev->data->nb_rx_queues; i++)
1555 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1557 for (i = 0; i < dev->data->nb_tx_queues; i++)
1558 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1563 static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
1565 struct rte_mbuf *buf;
1566 int i, mbuf_num = 0;
1568 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1570 "Before freeing rxq[%d] used and unused buf", i);
1571 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1573 PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p",
1574 i, dev->data->rx_queues[i]);
1575 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1576 dev->data->rx_queues[i])) != NULL) {
1577 rte_pktmbuf_free(buf);
1581 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1583 "After freeing rxq[%d] used and unused buf", i);
1584 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->rx_queues[i]);
1587 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1589 "Before freeing txq[%d] used and unused bufs",
1591 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1594 while ((buf = (struct rte_mbuf *)virtqueue_detatch_unused(
1595 dev->data->tx_queues[i])) != NULL) {
1596 rte_pktmbuf_free(buf);
1601 PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num);
1603 "After freeing txq[%d] used and unused buf", i);
1604 VIRTQUEUE_DUMP((struct virtqueue *)dev->data->tx_queues[i]);
1609 * Stop device: disable interrupt and mark link down
1612 virtio_dev_stop(struct rte_eth_dev *dev)
1614 struct rte_eth_link link;
1616 PMD_INIT_LOG(DEBUG, "stop");
1618 if (dev->data->dev_conf.intr_conf.lsc)
1619 rte_intr_disable(&dev->pci_dev->intr_handle);
1621 memset(&link, 0, sizeof(link));
1622 virtio_dev_atomic_write_link_status(dev, &link);
1626 virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
1628 struct rte_eth_link link, old;
1630 struct virtio_hw *hw = dev->data->dev_private;
1631 memset(&link, 0, sizeof(link));
1632 virtio_dev_atomic_read_link_status(dev, &link);
1634 link.link_duplex = FULL_DUPLEX;
1635 link.link_speed = SPEED_10G;
1637 if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) {
1638 PMD_INIT_LOG(DEBUG, "Get link status from hw");
1639 vtpci_read_dev_config(hw,
1640 offsetof(struct virtio_net_config, status),
1641 &status, sizeof(status));
1642 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
1643 link.link_status = 0;
1644 PMD_INIT_LOG(DEBUG, "Port %d is down",
1645 dev->data->port_id);
1647 link.link_status = 1;
1648 PMD_INIT_LOG(DEBUG, "Port %d is up",
1649 dev->data->port_id);
1652 link.link_status = 1; /* Link up */
1654 virtio_dev_atomic_write_link_status(dev, &link);
1656 return (old.link_status == link.link_status) ? -1 : 0;
1660 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1662 struct virtio_hw *hw = dev->data->dev_private;
1664 dev_info->driver_name = dev->driver->pci_drv.name;
1665 dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1666 dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1667 dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
1668 dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
1669 dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
1670 dev_info->default_txconf = (struct rte_eth_txconf) {
1671 .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS
1676 * It enables testpmd to collect per queue stats.
1679 virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
1680 __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
1681 __rte_unused uint8_t is_rx)
1686 static struct rte_driver rte_virtio_driver = {
1688 .init = rte_virtio_pmd_init,
1691 PMD_REGISTER_DRIVER(rte_virtio_driver);