libqos: Added basic virtqueue support to virtio implementation

Add status changing and feature negotiation.
Add basic virtqueue support for adding and sending virtqueue requests.
Add ISR checking.

[Squashed request endianness fix by Greg Kurz <gkurz@linux.vnet.ibm.com>
--Stefan]

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Marc Marí <marc.mari.barcelo@gmail.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Marc Marí 2014-09-01 12:07:56 +02:00 committed by Stefan Hajnoczi
parent 46e0cf7629
commit bf3c63d201
5 changed files with 476 additions and 3 deletions

View File

@ -14,6 +14,8 @@
#include "libqos/virtio-pci.h"
#include "libqos/pci.h"
#include "libqos/pci-pc.h"
#include "libqos/malloc.h"
#include "libqos/malloc-pc.h"
#include "hw/pci/pci_regs.h"
@ -93,6 +95,18 @@ static uint64_t qvirtio_pci_config_readq(QVirtioDevice *d, void *addr)
return u64;
}
static uint32_t qvirtio_pci_get_features(QVirtioDevice *d)
{
QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
return qpci_io_readl(dev->pdev, dev->addr + QVIRTIO_DEVICE_FEATURES);
}
static void qvirtio_pci_set_features(QVirtioDevice *d, uint32_t features)
{
QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
qpci_io_writel(dev->pdev, dev->addr + QVIRTIO_GUEST_FEATURES, features);
}
static uint8_t qvirtio_pci_get_status(QVirtioDevice *d)
{
QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
@ -105,13 +119,81 @@ static void qvirtio_pci_set_status(QVirtioDevice *d, uint8_t status)
qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS, status);
}
static uint8_t qvirtio_pci_get_isr_status(QVirtioDevice *d)
{
QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_ISR_STATUS);
}
static void qvirtio_pci_queue_select(QVirtioDevice *d, uint16_t index)
{
QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_QUEUE_SELECT, index);
}
static uint16_t qvirtio_pci_get_queue_size(QVirtioDevice *d)
{
QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
return qpci_io_readw(dev->pdev, dev->addr + QVIRTIO_QUEUE_SIZE);
}
static void qvirtio_pci_set_queue_address(QVirtioDevice *d, uint32_t pfn)
{
QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
qpci_io_writel(dev->pdev, dev->addr + QVIRTIO_QUEUE_ADDRESS, pfn);
}
static QVirtQueue *qvirtio_pci_virtqueue_setup(QVirtioDevice *d,
QGuestAllocator *alloc, uint16_t index)
{
uint64_t addr;
QVirtQueue *vq;
vq = g_malloc0(sizeof(*vq));
qvirtio_pci_queue_select(d, index);
vq->index = index;
vq->size = qvirtio_pci_get_queue_size(d);
vq->free_head = 0;
vq->num_free = vq->size;
vq->align = QVIRTIO_PCI_ALIGN;
/* Check different than 0 */
g_assert_cmpint(vq->size, !=, 0);
/* Check power of 2 */
g_assert_cmpint(vq->size & (vq->size - 1), ==, 0);
addr = guest_alloc(alloc, qvring_size(vq->size, QVIRTIO_PCI_ALIGN));
qvring_init(alloc, vq, addr);
qvirtio_pci_set_queue_address(d, vq->desc / QVIRTIO_PCI_ALIGN);
/* TODO: MSI-X configuration */
return vq;
}
static void qvirtio_pci_virtqueue_kick(QVirtioDevice *d, QVirtQueue *vq)
{
QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
qpci_io_writew(dev->pdev, dev->addr + QVIRTIO_QUEUE_NOTIFY, vq->index);
}
const QVirtioBus qvirtio_pci = {
.config_readb = qvirtio_pci_config_readb,
.config_readw = qvirtio_pci_config_readw,
.config_readl = qvirtio_pci_config_readl,
.config_readq = qvirtio_pci_config_readq,
.get_features = qvirtio_pci_get_features,
.set_features = qvirtio_pci_set_features,
.get_status = qvirtio_pci_get_status,
.set_status = qvirtio_pci_set_status,
.get_isr_status = qvirtio_pci_get_isr_status,
.queue_select = qvirtio_pci_queue_select,
.get_queue_size = qvirtio_pci_get_queue_size,
.set_queue_address = qvirtio_pci_set_queue_address,
.virtqueue_setup = qvirtio_pci_virtqueue_setup,
.virtqueue_kick = qvirtio_pci_virtqueue_kick,
};
void qvirtio_pci_foreach(QPCIBus *bus, uint16_t device_type,

View File

@ -26,6 +26,8 @@
#define QVIRTIO_DEVICE_SPECIFIC_MSIX 0x18
#define QVIRTIO_DEVICE_SPECIFIC_NO_MSIX 0x14
#define QVIRTIO_PCI_ALIGN 4096
typedef struct QVirtioPCIDevice {
QVirtioDevice vdev;
QPCIDevice *pdev;

View File

@ -35,6 +35,23 @@ uint64_t qvirtio_config_readq(const QVirtioBus *bus, QVirtioDevice *d,
return bus->config_readq(d, addr);
}
uint32_t qvirtio_get_features(const QVirtioBus *bus, QVirtioDevice *d)
{
return bus->get_features(d);
}
void qvirtio_set_features(const QVirtioBus *bus, QVirtioDevice *d,
uint32_t features)
{
bus->set_features(d, features);
}
QVirtQueue *qvirtqueue_setup(const QVirtioBus *bus, QVirtioDevice *d,
QGuestAllocator *alloc, uint16_t index)
{
return bus->virtqueue_setup(d, alloc, index);
}
void qvirtio_reset(const QVirtioBus *bus, QVirtioDevice *d)
{
bus->set_status(d, QVIRTIO_RESET);
@ -53,3 +70,86 @@ void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d)
g_assert_cmphex(bus->get_status(d), ==,
QVIRTIO_DRIVER | QVIRTIO_ACKNOWLEDGE);
}
void qvirtio_set_driver_ok(const QVirtioBus *bus, QVirtioDevice *d)
{
bus->set_status(d, bus->get_status(d) | QVIRTIO_DRIVER_OK);
g_assert_cmphex(bus->get_status(d), ==,
QVIRTIO_DRIVER_OK | QVIRTIO_DRIVER | QVIRTIO_ACKNOWLEDGE);
}
bool qvirtio_wait_isr(const QVirtioBus *bus, QVirtioDevice *d, uint8_t mask,
uint64_t timeout)
{
do {
clock_step(10);
if (bus->get_isr_status(d) & mask) {
break; /* It has ended */
}
} while (--timeout);
return timeout != 0;
}
void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)
{
int i;
vq->desc = addr;
vq->avail = vq->desc + vq->size*sizeof(QVRingDesc);
vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
+ vq->align - 1) & ~(vq->align - 1));
for (i = 0; i < vq->size - 1; i++) {
/* vq->desc[i].addr */
writew(vq->desc + (16 * i), 0);
/* vq->desc[i].next */
writew(vq->desc + (16 * i) + 14, i + 1);
}
/* vq->avail->flags */
writew(vq->avail, 0);
/* vq->avail->idx */
writew(vq->avail + 2, 0);
/* vq->used->flags */
writew(vq->used, 0);
}
uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
bool next)
{
uint16_t flags = 0;
vq->num_free--;
if (write) {
flags |= QVRING_DESC_F_WRITE;
}
if (next) {
flags |= QVRING_DESC_F_NEXT;
}
/* vq->desc[vq->free_head].addr */
writeq(vq->desc + (16 * vq->free_head), data);
/* vq->desc[vq->free_head].len */
writel(vq->desc + (16 * vq->free_head) + 8, len);
/* vq->desc[vq->free_head].flags */
writew(vq->desc + (16 * vq->free_head) + 12, flags);
return vq->free_head++; /* Return and increase, in this order */
}
void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
uint32_t free_head)
{
/* vq->avail->idx */
uint16_t idx = readl(vq->avail + 2);
/* vq->avail->ring[idx % vq->size] */
writel(vq->avail + 4 + (2 * (idx % vq->size)), free_head);
/* vq->avail->idx */
writel(vq->avail + 2, idx + 1);
bus->virtqueue_kick(d, vq);
}

View File

@ -10,33 +10,117 @@
#ifndef LIBQOS_VIRTIO_H
#define LIBQOS_VIRTIO_H
#include "libqos/malloc.h"
#define QVIRTIO_VENDOR_ID 0x1AF4
#define QVIRTIO_RESET 0x0
#define QVIRTIO_ACKNOWLEDGE 0x1
#define QVIRTIO_DRIVER 0x2
#define QVIRTIO_DRIVER_OK 0x4
#define QVIRTIO_NET_DEVICE_ID 0x1
#define QVIRTIO_BLK_DEVICE_ID 0x2
#define QVRING_DESC_F_NEXT 0x1
#define QVRING_DESC_F_WRITE 0x2
#define QVRING_DESC_F_INDIRECT 0x4
#define QVIRTIO_F_NOTIFY_ON_EMPTY 0x01000000
#define QVIRTIO_F_ANY_LAYOUT 0x08000000
#define QVIRTIO_F_RING_INDIRECT_DESC 0x10000000
#define QVIRTIO_F_RING_EVENT_IDX 0x20000000
#define QVIRTIO_F_BAD_FEATURE 0x40000000
#define QVRING_AVAIL_F_NO_INTERRUPT 1
#define QVRING_USED_F_NO_NOTIFY 1
typedef struct QVirtioDevice {
/* Device type */
uint16_t device_type;
} QVirtioDevice;
typedef struct QVRingDesc {
uint64_t addr;
uint32_t len;
uint16_t flags;
uint16_t next;
} QVRingDesc;
typedef struct QVRingAvail {
uint16_t flags;
uint16_t idx;
uint16_t ring[0]; /* This is an array of uint16_t */
} QVRingAvail;
typedef struct QVRingUsedElem {
uint32_t id;
uint32_t len;
} QVRingUsedElem;
typedef struct QVRingUsed {
uint16_t flags;
uint16_t idx;
QVRingUsedElem ring[0]; /* This is an array of QVRingUsedElem structs */
} QVRingUsed;
typedef struct QVirtQueue {
uint64_t desc; /* This points to an array of QVRingDesc */
uint64_t avail; /* This points to a QVRingAvail */
uint64_t used; /* This points to a QVRingDesc */
uint16_t index;
uint32_t size;
uint32_t free_head;
uint32_t num_free;
uint32_t align;
} QVirtQueue;
typedef struct QVirtioBus {
uint8_t (*config_readb)(QVirtioDevice *d, void *addr);
uint16_t (*config_readw)(QVirtioDevice *d, void *addr);
uint32_t (*config_readl)(QVirtioDevice *d, void *addr);
uint64_t (*config_readq)(QVirtioDevice *d, void *addr);
/* Get features of the device */
uint32_t (*get_features)(QVirtioDevice *d);
/* Get features of the device */
void (*set_features)(QVirtioDevice *d, uint32_t features);
/* Get status of the device */
uint8_t (*get_status)(QVirtioDevice *d);
/* Set status of the device */
void (*set_status)(QVirtioDevice *d, uint8_t status);
/* Get the ISR status of the device */
uint8_t (*get_isr_status)(QVirtioDevice *d);
/* Select a queue to work on */
void (*queue_select)(QVirtioDevice *d, uint16_t index);
/* Get the size of the selected queue */
uint16_t (*get_queue_size)(QVirtioDevice *d);
/* Set the address of the selected queue */
void (*set_queue_address)(QVirtioDevice *d, uint32_t pfn);
/* Setup the virtqueue specified by index */
QVirtQueue *(*virtqueue_setup)(QVirtioDevice *d, QGuestAllocator *alloc,
uint16_t index);
/* Notify changes in virtqueue */
void (*virtqueue_kick)(QVirtioDevice *d, QVirtQueue *vq);
} QVirtioBus;
static inline uint32_t qvring_size(uint32_t num, uint32_t align)
{
return ((sizeof(struct QVRingDesc) * num + sizeof(uint16_t) * (3 + num)
+ align - 1) & ~(align - 1))
+ sizeof(uint16_t) * 3 + sizeof(struct QVRingUsedElem) * num;
}
uint8_t qvirtio_config_readb(const QVirtioBus *bus, QVirtioDevice *d,
void *addr);
uint16_t qvirtio_config_readw(const QVirtioBus *bus, QVirtioDevice *d,
@ -45,9 +129,24 @@ uint32_t qvirtio_config_readl(const QVirtioBus *bus, QVirtioDevice *d,
void *addr);
uint64_t qvirtio_config_readq(const QVirtioBus *bus, QVirtioDevice *d,
void *addr);
uint32_t qvirtio_get_features(const QVirtioBus *bus, QVirtioDevice *d);
void qvirtio_set_features(const QVirtioBus *bus, QVirtioDevice *d,
uint32_t features);
void qvirtio_reset(const QVirtioBus *bus, QVirtioDevice *d);
void qvirtio_set_acknowledge(const QVirtioBus *bus, QVirtioDevice *d);
void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d);
void qvirtio_set_driver_ok(const QVirtioBus *bus, QVirtioDevice *d);
bool qvirtio_wait_isr(const QVirtioBus *bus, QVirtioDevice *d, uint8_t mask,
uint64_t timeout);
QVirtQueue *qvirtqueue_setup(const QVirtioBus *bus, QVirtioDevice *d,
QGuestAllocator *alloc, uint16_t index);
void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr);
uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
bool next);
void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
uint32_t free_head);
#endif

View File

@ -17,10 +17,41 @@
#include "libqos/virtio.h"
#include "libqos/virtio-pci.h"
#include "libqos/pci-pc.h"
#include "libqos/malloc.h"
#include "libqos/malloc-pc.h"
#include "qemu/bswap.h"
#define TEST_IMAGE_SIZE (64 * 1024 * 1024)
#define PCI_SLOT 0x04
#define PCI_FN 0x00
#define QVIRTIO_BLK_F_BARRIER 0x00000001
#define QVIRTIO_BLK_F_SIZE_MAX 0x00000002
#define QVIRTIO_BLK_F_SEG_MAX 0x00000004
#define QVIRTIO_BLK_F_GEOMETRY 0x00000010
#define QVIRTIO_BLK_F_RO 0x00000020
#define QVIRTIO_BLK_F_BLK_SIZE 0x00000040
#define QVIRTIO_BLK_F_SCSI 0x00000080
#define QVIRTIO_BLK_F_WCE 0x00000200
#define QVIRTIO_BLK_F_TOPOLOGY 0x00000400
#define QVIRTIO_BLK_F_CONFIG_WCE 0x00000800
#define QVIRTIO_BLK_T_IN 0
#define QVIRTIO_BLK_T_OUT 1
#define QVIRTIO_BLK_T_SCSI_CMD 2
#define QVIRTIO_BLK_T_SCSI_CMD_OUT 3
#define QVIRTIO_BLK_T_FLUSH 4
#define QVIRTIO_BLK_T_FLUSH_OUT 5
#define QVIRTIO_BLK_T_GET_ID 8
#define TEST_IMAGE_SIZE (64 * 1024 * 1024)
#define QVIRTIO_BLK_TIMEOUT 100
#define PCI_SLOT 0x04
#define PCI_FN 0x00
typedef struct QVirtioBlkReq {
uint32_t type;
uint32_t ioprio;
uint64_t sector;
char *data;
uint8_t status;
} QVirtioBlkReq;
static QPCIBus *test_start(void)
{
@ -66,12 +97,53 @@ static QVirtioPCIDevice *virtio_blk_init(QPCIBus *bus)
return dev;
}
static inline void virtio_blk_fix_request(QVirtioBlkReq *req)
{
#ifdef HOST_WORDS_BIGENDIAN
bool host_endian = true;
#else
bool host_endian = false;
#endif
if (qtest_big_endian() != host_endian) {
req->type = bswap32(req->type);
req->ioprio = bswap32(req->ioprio);
req->sector = bswap64(req->sector);
}
}
static uint64_t virtio_blk_request(QGuestAllocator *alloc, QVirtioBlkReq *req,
uint64_t data_size)
{
uint64_t addr;
uint8_t status = 0xFF;
g_assert_cmpuint(data_size % 512, ==, 0);
addr = guest_alloc(alloc, sizeof(*req) + data_size);
virtio_blk_fix_request(req);
memwrite(addr, req, 16);
memwrite(addr + 16, req->data, data_size);
memwrite(addr + 16 + data_size, &status, sizeof(status));
return addr;
}
static void pci_basic(void)
{
QVirtioPCIDevice *dev;
QPCIBus *bus;
QVirtQueue *vq;
QGuestAllocator *alloc;
QVirtioBlkReq req;
void *addr;
uint64_t req_addr;
uint64_t capacity;
uint32_t features;
uint32_t free_head;
uint8_t status;
char *data;
bus = test_start();
@ -83,6 +155,124 @@ static void pci_basic(void)
capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
features = features & ~(QVIRTIO_F_BAD_FEATURE |
QVIRTIO_F_RING_INDIRECT_DESC | QVIRTIO_F_RING_EVENT_IDX |
QVIRTIO_BLK_F_SCSI);
qvirtio_set_features(&qvirtio_pci, &dev->vdev, features);
alloc = pc_alloc_init();
vq = qvirtqueue_setup(&qvirtio_pci, &dev->vdev, alloc, 0);
qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);
/* Write and read with 2 descriptor layout */
/* Write request */
req.type = QVIRTIO_BLK_T_OUT;
req.ioprio = 1;
req.sector = 0;
req.data = g_malloc0(512);
strcpy(req.data, "TEST");
req_addr = virtio_blk_request(alloc, &req, 512);
g_free(req.data);
free_head = qvirtqueue_add(vq, req_addr, 528, false, true);
qvirtqueue_add(vq, req_addr + 528, 1, true, false);
qvirtqueue_kick(&qvirtio_pci, &dev->vdev, vq, free_head);
g_assert(qvirtio_wait_isr(&qvirtio_pci, &dev->vdev, 0x1,
QVIRTIO_BLK_TIMEOUT));
status = readb(req_addr + 528);
g_assert_cmpint(status, ==, 0);
guest_free(alloc, req_addr);
/* Read request */
req.type = QVIRTIO_BLK_T_IN;
req.ioprio = 1;
req.sector = 0;
req.data = g_malloc0(512);
req_addr = virtio_blk_request(alloc, &req, 512);
g_free(req.data);
free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
qvirtqueue_add(vq, req_addr + 16, 513, true, false);
qvirtqueue_kick(&qvirtio_pci, &dev->vdev, vq, free_head);
g_assert(qvirtio_wait_isr(&qvirtio_pci, &dev->vdev, 0x1,
QVIRTIO_BLK_TIMEOUT));
status = readb(req_addr + 528);
g_assert_cmpint(status, ==, 0);
data = g_malloc0(512);
memread(req_addr + 16, data, 512);
g_assert_cmpstr(data, ==, "TEST");
g_free(data);
guest_free(alloc, req_addr);
/* Write and read with 3 descriptor layout */
/* Write request */
req.type = QVIRTIO_BLK_T_OUT;
req.ioprio = 1;
req.sector = 1;
req.data = g_malloc0(512);
strcpy(req.data, "TEST");
req_addr = virtio_blk_request(alloc, &req, 512);
g_free(req.data);
free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
qvirtqueue_add(vq, req_addr + 16, 512, false, true);
qvirtqueue_add(vq, req_addr + 528, 1, true, false);
qvirtqueue_kick(&qvirtio_pci, &dev->vdev, vq, free_head);
g_assert(qvirtio_wait_isr(&qvirtio_pci, &dev->vdev, 0x1,
QVIRTIO_BLK_TIMEOUT));
status = readb(req_addr + 528);
g_assert_cmpint(status, ==, 0);
guest_free(alloc, req_addr);
/* Read request */
req.type = QVIRTIO_BLK_T_IN;
req.ioprio = 1;
req.sector = 1;
req.data = g_malloc0(512);
req_addr = virtio_blk_request(alloc, &req, 512);
g_free(req.data);
free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
qvirtqueue_add(vq, req_addr + 16, 512, true, true);
qvirtqueue_add(vq, req_addr + 528, 1, true, false);
qvirtqueue_kick(&qvirtio_pci, &dev->vdev, vq, free_head);
g_assert(qvirtio_wait_isr(&qvirtio_pci, &dev->vdev, 0x1,
QVIRTIO_BLK_TIMEOUT));
status = readb(req_addr + 528);
g_assert_cmpint(status, ==, 0);
guest_free(alloc, req_addr);
data = g_malloc0(512);
memread(req_addr + 16, data, 512);
g_assert_cmpstr(data, ==, "TEST");
g_free(data);
guest_free(alloc, req_addr);
/* End test */
guest_free(alloc, vq->desc);
qvirtio_pci_device_disable(dev);
g_free(dev);
test_end();