Skip to contentSkip to conversation

Virtio PCI libqos driver

PATCH Applied Marc Marí 3 participants 23 messages Aug 12, 2014 – Aug 17, 2014

v4 of 8 Applied Latest is v8 →

lore
Share & tools
lore

Marc Marí

author
#0 lore raw

v4 cover Virtio PCI libqos driver

Add functions for virtio PCI libqos driver. Add more debugging tools. Solve bugs found while generating tests.

v3: Solved problems, added indirect descriptor support and test for
configuration changes
v4: Solved bugs, changed some interfaces, added MSI-X and event_idx support.

Marc Marí (11):
tests: Functions bus_foreach and device_find from libqos virtio API
tests: Add virtio device initialization
libqtest: add QTEST_LOG for debugging qtest testcases
libqos: Correct mask to align size to PAGE_SIZE in malloc-pc
libqos: Change free function called in malloc
virtio-blk: Correct bug in support for flexible descriptor layout
libqos: Added basic virtqueue support to virtio implementation
libqos: Added indirect descriptor support to virtio implementation
libqos: Added test case for configuration changes in virtio-blk test
libqos: Added MSI-X support
libqos: Added EVENT_IDX support

 hw/block/virtio-blk.c     |   14 +-
 tests/Makefile            |    3 +-
 tests/libqos/malloc-pc.c  |    2 +-
 tests/libqos/malloc.h     |    2 +-
 tests/libqos/pci.c        |  110 +++++++++-
 tests/libqos/pci.h        |   10 +
 tests/libqos/virtio-pci.c |  337 ++++++++++++++++++++++++++++++
 tests/libqos/virtio-pci.h |   61 ++++++
 tests/libqos/virtio.c     |  255 +++++++++++++++++++++++
 tests/libqos/virtio.h     |  182 ++++++++++++++++
 tests/libqtest.c          |    7 +-
 tests/virtio-blk-test.c   |  501 ++++++++++++++++++++++++++++++++++++++++++++-
 12 files changed, 1463 insertions(+), 21 deletions(-)
 create mode 100644 tests/libqos/virtio-pci.c
 create mode 100644 tests/libqos/virtio-pci.h
 create mode 100644 tests/libqos/virtio.c
 create mode 100644 tests/libqos/virtio.h
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH v4 1/11 tests: Functions bus_foreach and device_find from libqos virtio API

Virtio header has been changed to compile and work with a real device. Functions bus_foreach and device_find have been implemented for PCI. Virtio-blk test case now opens a fake device.

---
 tests/Makefile            |    3 +-
 tests/libqos/virtio-pci.c |   75 +++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio-pci.h |   24 +++++++++++++++
 tests/libqos/virtio.h     |   23 ++++++++++++++
 tests/virtio-blk-test.c   |   61 +++++++++++++++++++++++++++++++-----
 5 files changed, 177 insertions(+), 9 deletions(-)
 create mode 100644 tests/libqos/virtio-pci.c
 create mode 100644 tests/libqos/virtio-pci.h
 create mode 100644 tests/libqos/virtio.h
tests/Makefile +2 -1
index 4b2e1bb..7c0f670 100644
@@ -294,6 +294,7 @@ libqos-obj-y += tests/libqos/i2c.o
 libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o
 libqos-pc-obj-y += tests/libqos/malloc-pc.o
 libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o
+libqos-virtio-obj-y = $(libqos-obj-y) $(libqos-pc-obj-y) tests/libqos/virtio-pci.o
 
 tests/rtc-test$(EXESUF): tests/rtc-test.o
 tests/m48t59-test$(EXESUF): tests/m48t59-test.o
@@ -315,7 +316,7 @@ tests/vmxnet3-test$(EXESUF): tests/vmxnet3-test.o
 tests/ne2000-test$(EXESUF): tests/ne2000-test.o
 tests/wdt_ib700-test$(EXESUF): tests/wdt_ib700-test.o
 tests/virtio-balloon-test$(EXESUF): tests/virtio-balloon-test.o
-tests/virtio-blk-test$(EXESUF): tests/virtio-blk-test.o
+tests/virtio-blk-test$(EXESUF): tests/virtio-blk-test.o $(libqos-virtio-obj-y)
 tests/virtio-net-test$(EXESUF): tests/virtio-net-test.o
 tests/virtio-rng-test$(EXESUF): tests/virtio-rng-test.o
 tests/virtio-scsi-test$(EXESUF): tests/virtio-scsi-test.o
tests/libqos/virtio-pci.c new file +75 -0

75 lines not on this page · raw message

tests/libqos/virtio-pci.h new file +24 -0
new file mode 100644
index 0000000..5101abb
@@ -0,0 +1,24 @@
+/*
+ * libqos virtio PCI definitions
+ *
+ * Copyright (c) 2014 Marc Marí
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef LIBQOS_VIRTIO_PCI_H
+#define LIBQOS_VIRTIO_PCI_H
+
+#include "libqos/virtio.h"
+#include "libqos/pci.h"
+
+typedef struct QVirtioPCIDevice {
+    QVirtioDevice vdev;
+    QPCIDevice *pdev;
+} QVirtioPCIDevice;
+
+void qvirtio_pci_foreach(QPCIBus *bus, uint16_t device_type,
+                void (*func)(QVirtioDevice *d, void *data), void *data);
+QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type);
+#endif
tests/libqos/virtio.h new file +23 -0
new file mode 100644
index 0000000..2a05798
@@ -0,0 +1,23 @@
+/*
+ * libqos virtio definitions
+ *
+ * Copyright (c) 2014 Marc Marí
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef LIBQOS_VIRTIO_H
+#define LIBQOS_VIRTIO_H
+
+#define QVIRTIO_VENDOR_ID       0x1AF4
+
+#define QVIRTIO_NET_DEVICE_ID   0x1
+#define QVIRTIO_BLK_DEVICE_ID   0x2
+
+typedef struct QVirtioDevice {
+    /* Device type */
+    uint16_t device_type;
+} QVirtioDevice;
+
+#endif
tests/virtio-blk-test.c +53 -8

84 lines not on this page · raw message

Signature · 1 line
1.7.10.4
Reviewed-by on v4 1/11
#2 lore raw

Re: [Qemu-devel] [PATCH v4 01/11] tests: Functions bus_foreach and device_find from libqos virtio API

Quoted 5 lines · Marc Marí

Virtio header has been changed to compile and work with a real device. Functions bus_foreach and device_find have been implemented for PCI. Virtio-blk test case now opens a fake device.

Quoted 10 lines
---
 tests/Makefile            |    3 +-
 tests/libqos/virtio-pci.c |   75 +++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio-pci.h |   24 +++++++++++++++
 tests/libqos/virtio.h     |   23 ++++++++++++++
 tests/virtio-blk-test.c   |   61 +++++++++++++++++++++++++++++++-----
 5 files changed, 177 insertions(+), 9 deletions(-)
 create mode 100644 tests/libqos/virtio-pci.c
 create mode 100644 tests/libqos/virtio-pci.h
 create mode 100644 tests/libqos/virtio.h
author
#3 lore raw

PATCH v4 2/11 tests: Add virtio device initialization

In reply to Marc Marí

Add functions to read and write virtio header fields. Add status bit setting in virtio-blk-device.

---
 tests/Makefile            |    2 +-
 tests/libqos/virtio-pci.c |   57 +++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio-pci.h |   18 ++++++++++++++
 tests/libqos/virtio.c     |   55 +++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio.h     |   30 ++++++++++++++++++++++++
 tests/virtio-blk-test.c   |   14 +++++++++++
 6 files changed, 175 insertions(+), 1 deletion(-)
 create mode 100644 tests/libqos/virtio.c
tests/Makefile +1 -1
index 7c0f670..e0e203f 100644
@@ -294,7 +294,7 @@ libqos-obj-y += tests/libqos/i2c.o
 libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o
 libqos-pc-obj-y += tests/libqos/malloc-pc.o
 libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o
-libqos-virtio-obj-y = $(libqos-obj-y) $(libqos-pc-obj-y) tests/libqos/virtio-pci.o
+libqos-virtio-obj-y = $(libqos-obj-y) $(libqos-pc-obj-y) tests/libqos/virtio.o tests/libqos/virtio-pci.o
 
 tests/rtc-test$(EXESUF): tests/rtc-test.o
 tests/m48t59-test$(EXESUF): tests/m48t59-test.o
tests/libqos/virtio-pci.c +57 -0

66 lines not on this page · raw message

tests/libqos/virtio-pci.h +18 -0
index 5101abb..26f902e 100644
@@ -13,12 +13,30 @@
 #include "libqos/virtio.h"
 #include "libqos/pci.h"
 
+#define QVIRTIO_DEVICE_FEATURES         0x00
+#define QVIRTIO_GUEST_FEATURES          0x04
+#define QVIRTIO_QUEUE_ADDRESS           0x08
+#define QVIRTIO_QUEUE_SIZE              0x0C
+#define QVIRTIO_QUEUE_SELECT            0x0E
+#define QVIRTIO_QUEUE_NOTIFY            0x10
+#define QVIRTIO_DEVICE_STATUS           0x12
+#define QVIRTIO_ISR_STATUS              0x13
+#define QVIRTIO_MSIX_CONF_VECTOR        0x14
+#define QVIRTIO_MSIX_QUEUE_VECTOR       0x16
+#define QVIRTIO_DEVICE_SPECIFIC_MSIX    0x18
+#define QVIRTIO_DEVICE_SPECIFIC_NO_MSIX 0x14
+
 typedef struct QVirtioPCIDevice {
     QVirtioDevice vdev;
     QPCIDevice *pdev;
+    void *addr;
 } QVirtioPCIDevice;
 
+extern const QVirtioBus qvirtio_pci;
+
 void qvirtio_pci_foreach(QPCIBus *bus, uint16_t device_type,
                 void (*func)(QVirtioDevice *d, void *data), void *data);
 QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type);
+void qvirtio_pci_device_enable(QVirtioPCIDevice *d);
+void qvirtio_pci_device_disable(QVirtioPCIDevice *d);
 #endif
tests/libqos/virtio.c new file +55 -0

55 lines not on this page · raw message

tests/libqos/virtio.h +30 -0
index 2a05798..23e975b 100644
@@ -12,6 +12,10 @@
 
 #define QVIRTIO_VENDOR_ID       0x1AF4
 
+#define QVIRTIO_RESET           0x0
+#define QVIRTIO_ACKNOWLEDGE     0x1
+#define QVIRTIO_DRIVER          0x2
+
 #define QVIRTIO_NET_DEVICE_ID   0x1
 #define QVIRTIO_BLK_DEVICE_ID   0x2
 
@@ -20,4 +24,30 @@ typedef struct QVirtioDevice {
     uint16_t device_type;
 } QVirtioDevice;
 
+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 status of the device */
+    uint8_t (*get_status)(QVirtioDevice *d);
+
+    /* Set status of the device  */
+    void (*set_status)(QVirtioDevice *d, uint8_t val);
+} QVirtioBus;
+
+uint8_t qvirtio_config_readb(const QVirtioBus *bus, QVirtioDevice *d,
+                                                                void *addr);
+uint16_t qvirtio_config_readw(const QVirtioBus *bus, QVirtioDevice *d,
+                                                                void *addr);
+uint32_t qvirtio_config_readl(const QVirtioBus *bus, QVirtioDevice *d,
+                                                                void *addr);
+uint64_t qvirtio_config_readq(const QVirtioBus *bus, QVirtioDevice *d,
+                                                                void *addr);
+
+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);
+
 #endif
tests/virtio-blk-test.c +14 -0
index 4d87a3e..a534521 100644
@@ -53,6 +53,8 @@ static void pci_basic(void)
 {
     QVirtioPCIDevice *dev;
     QPCIBus *bus;
+    void *addr;
+    uint64_t capacity;
 
     bus = test_start();
 
@@ -61,6 +63,18 @@ static void pci_basic(void)
     g_assert_cmphex(dev->vdev.device_type, ==, QVIRTIO_BLK_DEVICE_ID);
     g_assert_cmphex(dev->pdev->devfn, ==, ((PCI_SLOT << 3) | PCI_FN));
 
+    qvirtio_pci_device_enable(dev);
+    qvirtio_reset(&qvirtio_pci, &dev->vdev);
+    qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev);
+    qvirtio_set_driver(&qvirtio_pci, &dev->vdev);
+
+    /* MSI-X is not enabled */
+    addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
+
+    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
+    g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE/512);
+
+    qvirtio_pci_device_disable(dev);
     g_free(dev);
     test_end();
 }
Signature · 1 line
1.7.10.4
#4 lore raw

Re: [Qemu-devel] [PATCH v4 02/11] tests: Add virtio device initialization

tests/libqos/virtio-pci.c static void qvirtio_pci_assign_device(QVirtioDevice *d, void *data) · line 79

Quoted 4 lines · Marc Marí
tests/libqos/virtio-pci.c +4 -0
+static uint64_t qvirtio_pci_config_readq(QVirtioDevice *d, void *addr)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    return qpci_io_readl(dev->pdev, addr) | qpci_io_readl(dev->pdev, addr+4);

This is broken because it never shifts bits into the upper 32 bits. Which value to shift up depends on endianness. Another solution is to do byte reads into a uint64_t variable like memcpy.

I suggest you simply drop 64-bit accesses for now since they are probably not used.

author
#5 lore raw

PATCH v4 3/11 libqtest: add QTEST_LOG for debugging qtest testcases

In reply to Marc Marí
---
 tests/libqtest.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tests/libqtest.c +6 -1
index 98e8f4b..fbd600d 100644
@@ -167,11 +167,12 @@ QTestState *qtest_init(const char *extra_args)
     if (s->qemu_pid == 0) {
         command = g_strdup_printf("exec %s "
                                   "-qtest unix:%s,nowait "
-                                  "-qtest-log /dev/null "
+                                  "-qtest-log %s "
                                   "-qmp unix:%s,nowait "
                                   "-machine accel=qtest "
                                   "-display none "
                                   "%s", qemu_binary, socket_path,
+                                  getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
                                   qmp_socket_path,
                                   extra_args ?: "");
         execlp("/bin/sh", "sh", "-c", command, NULL);
@@ -397,10 +398,14 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
 
     /* No need to send anything for an empty QObject.  */
     if (qobj) {
+        int log = getenv("QTEST_LOG") != NULL;
         QString *qstr = qobject_to_json(qobj);
         const char *str = qstring_get_str(qstr);
         size_t size = qstring_get_length(qstr);
 
+        if (log) {
+            fprintf(stderr, "%s", str);
+        }
         /* Send QMP request */
         socket_send(s->qmp_fd, str, size);
 
Signature · 1 line
1.7.10.4
author
#8 lore raw

PATCH v4 4/11 libqos: Correct mask to align size to PAGE_SIZE in malloc-pc

In reply to Marc Marí
---
 tests/libqos/malloc-pc.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/libqos/malloc-pc.c +1 -1
index db1496c..2efd095 100644
@@ -36,7 +36,7 @@ static uint64_t pc_alloc(QGuestAllocator *allocator, size_t size)
 
 
     size += (PAGE_SIZE - 1);
-    size &= PAGE_SIZE;
+    size &= -PAGE_SIZE;
 
     g_assert_cmpint((s->start + size), <=, s->end);
 
Signature · 1 line
1.7.10.4
#9 lore raw

Re: [Qemu-devel] [PATCH v4 04/11] libqos: Correct mask to align size to PAGE_SIZE in malloc-pc

Quoted 7 lines · Marc Marí
---
 tests/libqos/malloc-pc.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan

author
#10 lore raw

PATCH v4 5/11 libqos: Change free function called in malloc

In reply to Marc Marí
---
 tests/libqos/malloc.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/libqos/malloc.h +1 -1
index 46f6000..5565381 100644
@@ -32,7 +32,7 @@ static inline uint64_t guest_alloc(QGuestAllocator *allocator, size_t size)
 
 static inline void guest_free(QGuestAllocator *allocator, uint64_t addr)
 {
-    allocator->alloc(allocator, addr);
+    allocator->free(allocator, addr);
 }
 
 #endif
Signature · 1 line
1.7.10.4
author
#12 lore raw

PATCH v4 6/11 virtio-blk: Correct bug in support for flexible descriptor layout

In reply to Marc Marí

Without this correction, only a three descriptor layout is accepted, and requests with just two descriptors are not completed and no error message is displayed.

---
 hw/block/virtio-blk.c |   14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
hw/block/virtio-blk.c +7 -7
index c241c50..302c39e 100644
@@ -404,19 +404,19 @@ void virtio_blk_handle_request(VirtIOBlockReq *req, MultiReqBuffer *mrb)
          * NB: per existing s/n string convention the string is
          * terminated by '\0' only when shorter than buffer.
          */
-        strncpy(req->elem.in_sg[0].iov_base,
-                s->blk.serial ? s->blk.serial : "",
-                MIN(req->elem.in_sg[0].iov_len, VIRTIO_BLK_ID_BYTES));
+        const char *serial = s->blk.serial ? s->blk.serial : "";
+        size_t size = MIN(strlen(serial) + 1,
+                          MIN(iov_size(in_iov, in_num),
+                              VIRTIO_BLK_ID_BYTES));
+        iov_from_buf(in_iov, in_num, 0, serial, size);
         virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
         virtio_blk_free_request(req);
     } else if (type & VIRTIO_BLK_T_OUT) {
-        qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
-                                 req->elem.out_num - 1);
+        qemu_iovec_init_external(&req->qiov, iov, out_num);
         virtio_blk_handle_write(req, mrb);
     } else if (type == VIRTIO_BLK_T_IN || type == VIRTIO_BLK_T_BARRIER) {
         /* VIRTIO_BLK_T_IN is 0, so we can't just & it. */
-        qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
-                                 req->elem.in_num - 1);
+        qemu_iovec_init_external(&req->qiov, in_iov, in_num);
         virtio_blk_handle_read(req);
     } else {
         virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP);
Signature · 1 line
1.7.10.4
#13 lore raw

Re: [Qemu-devel] [PATCH v4 06/11] virtio-blk: Correct bug in support for flexible descriptor layout

Quoted 9 lines · Marc Marí

Without this correction, only a three descriptor layout is accepted, and requests with just two descriptors are not completed and no error message is displayed.

---
 hw/block/virtio-blk.c |   14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan

author
#14 lore raw

PATCH v4 7/11 libqos: Added basic virtqueue support to virtio implementation

In reply to Marc Marí

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

---
 tests/libqos/virtio-pci.c |   91 ++++++++++++++++++++++++++-
 tests/libqos/virtio-pci.h |    2 +
 tests/libqos/virtio.c     |  100 +++++++++++++++++++++++++++++
 tests/libqos/virtio.h     |  101 ++++++++++++++++++++++++++++-
 tests/virtio-blk-test.c   |  154 +++++++++++++++++++++++++++++++++++++++++++--
 5 files changed, 441 insertions(+), 7 deletions(-)
tests/libqos/virtio-pci.c +89 -2
index f3ab578..5f92df7 100644
@@ -13,6 +13,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"
 
@@ -79,16 +81,93 @@ static uint64_t qvirtio_pci_config_readq(QVirtioDevice *d, void *addr)
     return qpci_io_readl(dev->pdev, addr) | qpci_io_readl(dev->pdev, addr+4);
 }
 
+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;
     return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS);
 }
 
-static void qvirtio_pci_set_status(QVirtioDevice *d, uint8_t val)
+static void qvirtio_pci_set_status(QVirtioDevice *d, uint8_t status)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    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)
+{
+    uint16_t aux;
+    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 */
+    aux = vq->size;
+    while ((aux & 1) != 0) {
+        aux = aux >> 1;
+    }
+    g_assert_cmpint(aux, !=, 1);
+
+    addr = guest_alloc(alloc, qvring_size(vq->size, QVIRTIO_PCI_ALIGN));
+    qvring_init(alloc, vq, addr);
+    qvirtio_pci_set_queue_address(d, vq->desc/4096);
+
+    /* TODO: MSI-X configuration */
+
+    return vq;
+}
+
+static void qvirtio_pci_virtqueue_kick(QVirtioDevice *d, QVirtQueue *vq)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS, val);
+    qpci_io_writew(dev->pdev, dev->addr + QVIRTIO_QUEUE_NOTIFY, vq->index);
 }
 
 const QVirtioBus qvirtio_pci = {
@@ -96,8 +175,16 @@ const QVirtioBus qvirtio_pci = {
     .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,
tests/libqos/virtio-pci.h +2 -0
index 26f902e..40bd12d 100644
@@ -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;
tests/libqos/virtio.c +100 -0

109 lines not on this page · raw message

tests/libqos/virtio.h +100 -1

142 lines not on this page · raw message

tests/virtio-blk-test.c +150 -4

174 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#15 lore raw

Re: [Qemu-devel] [PATCH v4 07/11] libqos: Added basic virtqueue support to virtio implementation

tests/libqos/virtio-pci.c static uint64_t qvirtio_pci_config_readq(QVirtioDevice *d, void *addr) · line 156

Quoted 6 lines · Marc Marí
tests/libqos/virtio-pci.c +6 -0
+    /* Check power of 2 */
+    aux = vq->size;
+    while ((aux & 1) != 0) {
+        aux = aux >> 1;
+    }
+    g_assert_cmpint(aux, !=, 1);

Power of 2 can be tested like this without a while loop:
g_assert_cmpint(vq->size & (vq->size - 1), ==, 0)

Quoted 5 lines
tests/libqos/virtio.c +5 -0
+void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)
+{
+    int i;
+
+    vq->desc = addr;

tests/libqos/virtio.c void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d) · line 108

Quoted 10 lines
tests/libqos/virtio.c +10 -0
+    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);
+    }

Please use space between operators:
writew(vq->desc + (16 * i) + 14, i + 1)

This applies to all patches.

tests/libqos/virtio.c void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d) · line 150

Quoted 8 lines
tests/libqos/virtio.c +8 -0
+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);

If you want to use typed pointers you can. It will make the code nicer to read, just be careful never to dereference them and only to access through writel()/readl():

typedef struct {
uint16_t foo; /* Forgot what these fields are called and didn't check */ uint16_t bar;
uint16_t ring[];
} VirtioAvail;

writel(&vq->avail.ring[idx % vq->size], free_head);

Now it looks more like normal C and the compiler is doing the address calculations for us.

author
#16 lore raw

Re: [Qemu-devel] [PATCH v4 07/11] libqos: Added basic virtqueue support to virtio implementation

Quoted 42 lines · El Wed, 13 Aug 2014 18:27:40 +0100 Stefan Hajnoczi <[email protected]>…
Quoted 8 lines

+void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr) +{

tests/libqos/virtio.c +5 -0
+    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)

tests/libqos/virtio.c void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d) · line 108

Quoted 8 lines
tests/libqos/virtio.c +8 -0
+        +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);
+    }

Please use space between operators:
writew(vq->desc + (16 * i) + 14, i + 1)

This applies to all patches.

tests/libqos/virtio.c void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d) · line 150

Quoted 9 lines

+void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
+
uint32_t free_head) +{

tests/libqos/virtio.c +5 -0
+    /* 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);

If you want to use typed pointers you can. It will make the code nicer to read, just be careful never to dereference them and only to access through writel()/readl():

typedef struct {
uint16_t foo; /* Forgot what these fields are called and didn't check */ uint16_t bar;
uint16_t ring[];
} VirtioAvail;

Quoted 4 lines

writel(&vq->avail.ring[idx % vq->size], free_head);

Now it looks more like normal C and the compiler is doing the address calculations for us.

If it is not dereferenced, it can work. But all references will have to be casted from (void *) to (uint64_t) always which is what readl and writel expect. I think this is better than calculating the addresses, but is still a bit ugly.

#17 lore raw

Re: [Qemu-devel] [PATCH v4 07/11] libqos: Added basic virtqueue support to virtio implementation

Il 13/08/2014 23:59, Marc Marí ha scritto:

Quoted 4 lines

If it is not dereferenced, it can work. But all references will have to be casted from (void *) to (uint64_t) always which is what readl and writel expect. I think this is better than calculating the addresses, but is still a bit ugly.

It would also break subtly on 32-bit builds if we ever try using addresses above the first 4G (which could be an interesting test, and doesn't require lots of host memory if one uses hotplug).

Paolo

author
#18 lore raw

PATCH v4 8/11 libqos: Added indirect descriptor support to virtio implementation

In reply to Marc Marí

Add functions necessary for working with indirect descriptors. Add test using new functions.

---
 tests/libqos/virtio-pci.c |   10 ++++
 tests/libqos/virtio.c     |   63 ++++++++++++++++++++++++
 tests/libqos/virtio.h     |   22 ++++++++-
 tests/virtio-blk-test.c   |  118 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 212 insertions(+), 1 deletion(-)
tests/libqos/virtio-pci.c +10 -0
index 5f92df7..184c430 100644
@@ -93,6 +93,12 @@ static void qvirtio_pci_set_features(QVirtioDevice *d, uint32_t features)
     qpci_io_writel(dev->pdev, dev->addr + QVIRTIO_GUEST_FEATURES, features);
 }
 
+static uint32_t qvirtio_pci_get_guest_features(QVirtioDevice *d)
+{
+    QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
+    return qpci_io_readl(dev->pdev, dev->addr + QVIRTIO_GUEST_FEATURES);
+}
+
 static uint8_t qvirtio_pci_get_status(QVirtioDevice *d)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
@@ -133,10 +139,12 @@ static QVirtQueue *qvirtio_pci_virtqueue_setup(QVirtioDevice *d,
                                         QGuestAllocator *alloc, uint16_t index)
 {
     uint16_t aux;
+    uint32_t feat;
     uint64_t addr;
     QVirtQueue *vq;
 
     vq = g_malloc0(sizeof(*vq));
+    feat = qvirtio_pci_get_guest_features(d);
 
     qvirtio_pci_queue_select(d, index);
     vq->index = index;
@@ -144,6 +152,7 @@ static QVirtQueue *qvirtio_pci_virtqueue_setup(QVirtioDevice *d,
     vq->free_head = 0;
     vq->num_free = vq->size;
     vq->align = QVIRTIO_PCI_ALIGN;
+    vq->indirect = (feat & QVIRTIO_F_RING_INDIRECT_DESC) != 0 ? true : false;
 
     /* Check different than 0 */
     g_assert_cmpint(vq->size, !=, 0);
@@ -177,6 +186,7 @@ const QVirtioBus qvirtio_pci = {
     .config_readq = qvirtio_pci_config_readq,
     .get_features = qvirtio_pci_get_features,
     .set_features = qvirtio_pci_set_features,
+    .get_guest_features = qvirtio_pci_get_guest_features,
     .get_status = qvirtio_pci_get_status,
     .set_status = qvirtio_pci_set_status,
     .get_isr_status = qvirtio_pci_get_isr_status,
tests/libqos/virtio.c +63 -0

75 lines not on this page · raw message

tests/libqos/virtio.h +21 -1
index 7a38910..d0c84a6 100644
@@ -22,6 +22,11 @@
 #define QVIRTIO_NET_DEVICE_ID   0x1
 #define QVIRTIO_BLK_DEVICE_ID   0x2
 
+#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 QVRING_DESC_F_NEXT      0x1
 #define QVRING_DESC_F_WRITE     0x2
 #define QVRING_DESC_F_INDIRECT  0x4
@@ -74,8 +79,15 @@ typedef struct QVirtQueue {
     uint32_t free_head;
     uint32_t num_free;
     uint32_t align;
+    bool indirect;
 } QVirtQueue;
 
+typedef struct QVRingIndirectDesc {
+    uint64_t desc; /* This points to an array fo QVRingDesc */
+    uint16_t index;
+    uint16_t elem;
+} QVRingIndirectDesc;
+
 typedef struct QVirtioBus {
     uint8_t (*config_readb)(QVirtioDevice *d, void *addr);
     uint16_t (*config_readw)(QVirtioDevice *d, void *addr);
@@ -85,9 +97,12 @@ typedef struct QVirtioBus {
     /* Get features of the device */
     uint32_t (*get_features)(QVirtioDevice *d);
 
-    /* Get features of the device */
+    /* Set features of the device */
     void (*set_features)(QVirtioDevice *d, uint32_t features);
 
+    /* Get features of the guest */
+    uint32_t (*get_guest_features)(QVirtioDevice *d);
+
     /* Get status of the device */
     uint8_t (*get_status)(QVirtioDevice *d);
 
@@ -144,8 +159,13 @@ QVirtQueue *qvirtio_virtqueue_setup(const QVirtioBus *bus, QVirtioDevice *d,
                                         QGuestAllocator *alloc, uint16_t index);
 
 void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr);
+QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
+                                        QGuestAllocator *alloc, uint16_t elem);
+void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data,
+                                                    uint32_t len, bool write);
 uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
                                                                     bool next);
+uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect);
 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
                                                             uint32_t free_head);
 
tests/virtio-blk-test.c +118 -0

130 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author
#19 lore raw

PATCH v4 9/11 libqos: Added test case for configuration changes in virtio-blk test

In reply to Marc Marí
---
 tests/virtio-blk-test.c |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
tests/virtio-blk-test.c +12 -2
index 06a9ac8..7269ade 100644
@@ -225,13 +225,14 @@ static void pci_basic(void)
     test_end();
 }
 
-static void pci_indirect(void)
+static void pci_indirect_config(void)
 {
     QVirtioPCIDevice *dev;
     QPCIBus *bus;
     QVirtQueue *vq;
     QGuestAllocator *alloc;
     QVRingIndirectDesc *indirect;
+    int n_size = TEST_IMAGE_SIZE/2;
     void *addr;
     uint64_t w_req;
     uint64_t r_req;
@@ -270,6 +271,15 @@ static void pci_indirect(void)
 
     qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);
 
+    qmp("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', "
+                                                    " 'size': %d } }", n_size);
+
+    g_assert(qvirtio_wait_isr(&qvirtio_pci, &dev->vdev, 0x2,
+                                                        QVIRTIO_BLK_TIMEOUT));
+
+    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
+    g_assert_cmpint(capacity, ==, n_size/512);
+
     /* Write and read with 2 descriptor layout */
     data = g_malloc0(512);
     strcpy(data, "TEST");
@@ -349,7 +359,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
 
     g_test_add_func("/virtio/blk/pci/basic", pci_basic);
-    g_test_add_func("/virtio/blk/pci/indirect", pci_indirect);
+    g_test_add_func("/virtio/blk/pci/indirect_config", pci_indirect_config);
 
     ret = g_test_run();
 
Signature · 1 line
1.7.10.4
#20 lore raw

Re: [Qemu-devel] [PATCH v4 09/11] libqos: Added test case for configuration changes in virtio-blk test

tests/virtio-blk-test.c int main(int argc, char **argv) · line 362

Quoted 6 lines · Marc Marí
tests/virtio-blk-test.c +1 -1
@@ -349,7 +359,7 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
 
     g_test_add_func("/virtio/blk/pci/basic", pci_basic);
-    g_test_add_func("/virtio/blk/pci/indirect", pci_indirect);
+    g_test_add_func("/virtio/blk/pci/indirect_config", pci_indirect_config);

Can you make this an independent test? I think it is unrelated to indirect descriptors.

Stefan

author
#21 lore raw

PATCH v4 10/11 libqos: Added MSI-X support

In reply to Marc Marí

Added MSI-X support for qtest PCI.
Added MSI-X support for virtio-pci.
Added MSI-X test case in virtio-blk-test.

---
 tests/libqos/pci.c        |  110 ++++++++++++++++++++++++-
 tests/libqos/pci.h        |   10 +++
 tests/libqos/virtio-pci.c |  145 ++++++++++++++++++++++++++++-----
 tests/libqos/virtio-pci.h |   17 ++++
 tests/libqos/virtio.c     |   17 +++-
 tests/libqos/virtio.h     |   11 ++-
 tests/virtio-blk-test.c   |  197 +++++++++++++++++++++++++++++++++++++--------
 7 files changed, 447 insertions(+), 60 deletions(-)
tests/libqos/pci.c +108 -2
index c9a0b91..82b487d 100644
@@ -15,8 +15,6 @@
 #include "hw/pci/pci_regs.h"
 #include <glib.h>
 
-#include <stdio.h>
-
 void qpci_device_foreach(QPCIBus *bus, int vendor_id, int device_id,
                          void (*func)(QPCIDevice *dev, int devfn, void *data),
                          void *data)
@@ -75,6 +73,114 @@ void qpci_device_enable(QPCIDevice *dev)
     qpci_config_writew(dev, PCI_COMMAND, cmd);
 }
 
+uint8_t qpci_find_capability(QPCIDevice *dev, uint8_t id)
+{
+    uint8_t cap;
+    uint8_t addr = qpci_config_readb(dev, PCI_CAPABILITY_LIST);
+
+    do {
+        cap = qpci_config_readb(dev, addr);
+        if (cap != id) {
+            addr = qpci_config_readb(dev, addr+PCI_CAP_LIST_NEXT);
+        }
+    } while (cap != id && addr != 0);
+
+    return addr;
+}
+
+void qpci_msix_enable(QPCIDevice *dev)
+{
+    uint8_t addr;
+    uint16_t val;
+    uint32_t table;
+    uint8_t bir_table;
+    uint8_t bir_pba;
+    void *offset;
+
+    addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
+    g_assert_cmphex(addr, !=, 0);
+
+    val = qpci_config_readw(dev, addr+PCI_MSIX_FLAGS);
+    qpci_config_writew(dev, addr+PCI_MSIX_FLAGS, val | PCI_MSIX_FLAGS_ENABLE);
+
+    table = qpci_config_readl(dev, addr+PCI_MSIX_TABLE);
+    bir_table = table & PCI_MSIX_FLAGS_BIRMASK;
+    offset = qpci_iomap(dev, bir_table);
+    dev->msix_table = offset+(table & ~PCI_MSIX_FLAGS_BIRMASK);
+
+    table = qpci_config_readl(dev, addr+PCI_MSIX_PBA);
+    bir_pba = table & PCI_MSIX_FLAGS_BIRMASK;
+    if (bir_pba != bir_table) {
+        offset = qpci_iomap(dev, bir_pba);
+    }
+    dev->msix_pba = offset+(table & ~PCI_MSIX_FLAGS_BIRMASK);
+
+    g_assert(dev->msix_table != NULL);
+    g_assert(dev->msix_pba != NULL);
+    dev->msix_enabled = true;
+}
+
+void qpci_msix_disable(QPCIDevice *dev)
+{
+    uint8_t addr;
+    uint16_t val;
+
+    g_assert(dev->msix_enabled);
+    addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
+    g_assert_cmphex(addr, !=, 0);
+    val = qpci_config_readw(dev, addr+PCI_MSIX_FLAGS);
+    qpci_config_writew(dev, addr+PCI_MSIX_FLAGS, val & ~PCI_MSIX_FLAGS_ENABLE);
+
+    qpci_iounmap(dev, dev->msix_table);
+    qpci_iounmap(dev, dev->msix_pba);
+    dev->msix_enabled = 0;
+    dev->msix_table = NULL;
+    dev->msix_pba = NULL;
+}
+
+bool qpci_msix_pending(QPCIDevice *dev, uint16_t entry)
+{
+    uint32_t pba_entry;
+    uint8_t bit_n = entry % 32;
+    void *addr = dev->msix_pba + (entry/32) * PCI_MSIX_ENTRY_SIZE/4;
+
+    g_assert(dev->msix_enabled);
+    pba_entry = qpci_io_readl(dev, addr);
+    qpci_io_writel(dev, addr, pba_entry & ~(1 << bit_n));
+    return (pba_entry & (1 << bit_n)) != 0;
+}
+
+bool qpci_msix_masked(QPCIDevice *dev, uint16_t entry)
+{
+    uint8_t addr;
+    uint16_t val;
+    void *vector_addr = dev->msix_table + (entry * PCI_MSIX_ENTRY_SIZE);
+
+    g_assert(dev->msix_enabled);
+    addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
+    g_assert_cmphex(addr, !=, 0);
+    val = qpci_config_readw(dev, addr+PCI_MSIX_FLAGS);
+
+    if (val & PCI_MSIX_FLAGS_MASKALL) {
+        return true;
+    } else {
+        return (qpci_io_readl(dev, vector_addr + PCI_MSIX_ENTRY_VECTOR_CTRL)
+                                            & PCI_MSIX_ENTRY_CTRL_MASKBIT) != 0;
+    }
+}
+
+uint16_t qpci_msix_table_size(QPCIDevice *dev)
+{
+    uint8_t addr;
+    uint16_t control;
+
+    addr = qpci_find_capability(dev, PCI_CAP_ID_MSIX);
+    g_assert_cmphex(addr, !=, 0);
+
+    control = qpci_config_readw(dev, addr+PCI_MSIX_FLAGS);
+    return (control & PCI_MSIX_FLAGS_QSIZE) + 1;
+}
+
 uint8_t qpci_config_readb(QPCIDevice *dev, uint8_t offset)
 {
     return dev->bus->config_readb(dev->bus, dev->devfn, offset);
tests/libqos/pci.h +10 -0
index 3439431..decf5f6 100644
@@ -14,6 +14,7 @@
 #define LIBQOS_PCI_H
 
 #include <stdint.h>
+#include "libqtest.h"
 
 #define QPCI_DEVFN(dev, fn) (((dev) << 3) | (fn))
 
@@ -49,6 +50,9 @@ struct QPCIDevice
 {
     QPCIBus *bus;
     int devfn;
+    bool msix_enabled;
+    void *msix_table;
+    void *msix_pba;
 };
 
 void qpci_device_foreach(QPCIBus *bus, int vendor_id, int device_id,
@@ -57,6 +61,12 @@ void qpci_device_foreach(QPCIBus *bus, int vendor_id, int device_id,
 QPCIDevice *qpci_device_find(QPCIBus *bus, int devfn);
 
 void qpci_device_enable(QPCIDevice *dev);
+uint8_t qpci_find_capability(QPCIDevice *dev, uint8_t id);
+void qpci_msix_enable(QPCIDevice *dev);
+void qpci_msix_disable(QPCIDevice *dev);
+bool qpci_msix_pending(QPCIDevice *dev, uint16_t entry);
+bool qpci_msix_masked(QPCIDevice *dev, uint16_t entry);
+uint16_t qpci_msix_table_size(QPCIDevice *dev);
 
 uint8_t qpci_config_readb(QPCIDevice *dev, uint8_t offset);
 uint16_t qpci_config_readw(QPCIDevice *dev, uint8_t offset);
tests/libqos/virtio-pci.c +126 -19

195 lines not on this page · raw message

tests/libqos/virtio-pci.h +17 -0
index 40bd12d..d6569aa 100644
@@ -28,12 +28,24 @@
 
 #define QVIRTIO_PCI_ALIGN   4096
 
+#define VIRTIO_MSI_NO_VECTOR    0xFFFF
+
 typedef struct QVirtioPCIDevice {
     QVirtioDevice vdev;
     QPCIDevice *pdev;
     void *addr;
+    uint16_t config_msix_entry;
+    uint64_t config_msix_addr;
+    uint32_t config_msix_data;
 } QVirtioPCIDevice;
 
+typedef struct QVirtQueuePCI {
+    QVirtQueue vq;
+    uint16_t msix_entry;
+    uint64_t msix_addr;
+    uint32_t msix_data;
+} QVirtQueuePCI;
+
 extern const QVirtioBus qvirtio_pci;
 
 void qvirtio_pci_foreach(QPCIBus *bus, uint16_t device_type,
@@ -41,4 +53,9 @@ void qvirtio_pci_foreach(QPCIBus *bus, uint16_t device_type,
 QVirtioPCIDevice *qvirtio_pci_device_find(QPCIBus *bus, uint16_t device_type);
 void qvirtio_pci_device_enable(QVirtioPCIDevice *d);
 void qvirtio_pci_device_disable(QVirtioPCIDevice *d);
+
+void qvirtio_pci_set_msix_configuration_vector(QVirtioPCIDevice *d,
+                                        QGuestAllocator *alloc, uint16_t entry);
+void qvirtqueue_pci_msix_setup(QVirtioPCIDevice *d, QVirtQueuePCI *vqpci,
+                                        QGuestAllocator *alloc, uint16_t entry);
 #endif
tests/libqos/virtio.c +15 -2
index 6310c48..7d1ee7c 100644
@@ -78,12 +78,25 @@ void qvirtio_set_driver_ok(const QVirtioBus *bus, QVirtioDevice *d)
                 QVIRTIO_DRIVER_OK | QVIRTIO_DRIVER | QVIRTIO_ACKNOWLEDGE);
 }
 
-bool qvirtio_wait_isr(const QVirtioBus *bus, QVirtioDevice *d, uint8_t mask,
+bool qvirtio_wait_queue_isr(const QVirtioBus *bus, QVirtioDevice *d,
+                                            QVirtQueue *vq, uint64_t timeout)
+{
+    do {
+        clock_step(10);
+        if (bus->get_queue_isr_status(d, vq)) {
+            break; /* It has ended */
+        }
+    } while (--timeout);
+
+    return timeout != 0;
+}
+
+bool qvirtio_wait_config_isr(const QVirtioBus *bus, QVirtioDevice *d,
                                                             uint64_t timeout)
 {
     do {
         clock_step(10);
-        if (bus->get_isr_status(d) & mask) {
+        if (bus->get_config_isr_status(d)) {
             break; /* It has ended */
         }
     } while (--timeout);
tests/libqos/virtio.h +8 -3
index d0c84a6..5010cfb 100644
@@ -109,8 +109,11 @@ typedef struct QVirtioBus {
     /* 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);
+    /* Get the queue ISR status of the device */
+    bool (*get_queue_isr_status)(QVirtioDevice *d, QVirtQueue *vq);
+
+    /* Get the configuration ISR status of the device */
+    bool (*get_config_isr_status)(QVirtioDevice *d);
 
     /* Select a queue to work on */
     void (*queue_select)(QVirtioDevice *d, uint16_t index);
@@ -153,7 +156,9 @@ 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,
+bool qvirtio_wait_queue_isr(const QVirtioBus *bus, QVirtioDevice *d,
+                                            QVirtQueue *vq, uint64_t timeout);
+bool qvirtio_wait_config_isr(const QVirtioBus *bus, QVirtioDevice *d,
                                                             uint64_t timeout);
 QVirtQueue *qvirtio_virtqueue_setup(const QVirtioBus *bus, QVirtioDevice *d,
                                         QGuestAllocator *alloc, uint16_t index);
tests/virtio-blk-test.c +163 -34

295 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author
#22 lore raw

PATCH v4 11/11 libqos: Added EVENT_IDX support

In reply to Marc Marí

Added avail_event and NO_NOTIFY check before notifying. Added used_event setting.

---
 tests/libqos/virtio-pci.c |    1 +
 tests/libqos/virtio.c     |   26 +++++++++++++++++++++++++-
 tests/libqos/virtio.h     |    5 +++++
 tests/virtio-blk-test.c   |   33 ++++++++++++++++++++++++++++-----
 4 files changed, 59 insertions(+), 6 deletions(-)
tests/libqos/virtio-pci.c +1 -0
index acf785a..a35978d 100644
@@ -193,6 +193,7 @@ static QVirtQueue *qvirtio_pci_virtqueue_setup(QVirtioDevice *d,
     vqpci->vq.num_free = vqpci->vq.size;
     vqpci->vq.align = QVIRTIO_PCI_ALIGN;
     vqpci->vq.indirect = (feat & QVIRTIO_F_RING_INDIRECT_DESC) != 0;
+    vqpci->vq.event = (feat & QVIRTIO_F_RING_EVENT_IDX) != 0;
 
     vqpci->msix_entry = -1;
     vqpci->msix_addr = 0;
tests/libqos/virtio.c +25 -1
index 7d1ee7c..ae640f2 100644
@@ -124,9 +124,13 @@ void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)
     writew(vq->avail, 0);
     /* vq->avail->idx */
     writew(vq->avail+2, 0);
+    /* vq->avail->used_event */
+    writew(vq->avail+4+(2*vq->size), 0);
 
     /* vq->used->flags */
     writew(vq->used, 0);
+    /* vq->used->avail_event */
+    writew(vq->used+2+(sizeof(struct QVRingUsedElem)*vq->size), 0);
 }
 
 QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
@@ -221,11 +225,31 @@ void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
 {
     /* vq->avail->idx */
     uint16_t idx = readl(vq->avail+2);
+    /* vq->used->flags */
+    uint16_t flags;
+    /* vq->used->avail_event */
+    uint16_t avail_event;
 
     /* 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);
+    /* Must read after idx is updated */
+    flags = readw(vq->avail);
+    avail_event = readw(vq->used+4+(sizeof(struct QVRingUsedElem)*vq->size));
+
+    /* < 1 because we add elements to avail queue one by one */
+    if ((flags & QVRING_USED_F_NO_NOTIFY) == 0 &&
+                            (!vq->event || (uint16_t)(idx-avail_event) < 1)) {
+        bus->virtqueue_kick(d, vq);
+    }
+}
+
+void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx)
+{
+    g_assert(vq->event);
+
+    /* vq->avail->used_event */
+    writew(vq->avail+4+(2*vq->size), idx);
 }
tests/libqos/virtio.h +5 -0
index 5010cfb..95eb519 100644
@@ -26,6 +26,7 @@
 #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_DESC_F_NEXT      0x1
 #define QVRING_DESC_F_WRITE     0x2
@@ -57,6 +58,7 @@ typedef struct QVRingAvail {
     uint16_t flags;
     uint16_t idx;
     uint16_t ring[0]; /* This is an array of uint16_t */
+    uint16_t used_event;
 } QVRingAvail;
 
 typedef struct QVRingUsedElem {
@@ -68,6 +70,7 @@ typedef struct QVRingUsed {
     uint16_t flags;
     uint16_t idx;
     QVRingUsedElem ring[0]; /* This is an array of QVRingUsedElem structs */
+    uint16_t avail_event;
 } QVRingUsed;
 
 typedef struct QVirtQueue {
@@ -80,6 +83,7 @@ typedef struct QVirtQueue {
     uint32_t num_free;
     uint32_t align;
     bool indirect;
+    bool event;
 } QVirtQueue;
 
 typedef struct QVRingIndirectDesc {
@@ -174,4 +178,5 @@ uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect);
 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
                                                             uint32_t free_head);
 
+void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx);
 #endif
tests/virtio-blk-test.c +28 -5
index 8a8b6ec..1203772 100644
@@ -353,7 +353,7 @@ static void pci_indirect_config(void)
     test_end();
 }
 
-static void pci_msix(void)
+static void pci_msix_idx(void)
 {
     QVirtioPCIDevice *dev;
     QPCIBus *bus;
@@ -395,7 +395,7 @@ static void pci_msix(void)
     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_F_NOTIFY_ON_EMPTY | QVIRTIO_BLK_F_SCSI);
     qvirtio_set_features(&qvirtio_pci, &dev->vdev, features);
 
     vqpci = (QVirtQueuePCI *)qvirtio_virtqueue_setup(&qvirtio_pci, &dev->vdev,
@@ -413,7 +413,6 @@ static void pci_msix(void)
     capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
     g_assert_cmpint(capacity, ==, n_size/512);
 
-    /* Write and read with 2 descriptor layout */
     data = g_malloc0(512);
     strcpy(data, "TEST");
 
@@ -430,13 +429,36 @@ static void pci_msix(void)
     /* w_req->status */
     writeb(w_req+528, 0xFF);
 
+    free_head = qvirtqueue_add(&vqpci->vq, w_req, 528, false, true);
+    qvirtqueue_add(&vqpci->vq, w_req+528, 1, true, false);
+    qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
+
+    g_assert(qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
+                                                        QVIRTIO_BLK_TIMEOUT));
+
+    /* Write request */
+    w_req = guest_alloc(alloc, sizeof(QVirtioBlkReq)+512);
+    /* w_req->type */
+    writel(w_req, QVIRTIO_BLK_T_OUT);
+    /* w_req->ioprio */
+    writel(w_req+4, 1);
+    /* w_req->sector */
+    writeq(w_req+8, 1);
+    /* w_req->data */
+    memwrite(w_req+16, data, 512);
+    /* w_req->status */
+    writeb(w_req+528, 0xFF);
+
     g_free(data);
 
+    /* Notify after processing the third request */
+    qvirtqueue_set_used_event(&vqpci->vq, 2);
     free_head = qvirtqueue_add(&vqpci->vq, w_req, 528, false, true);
     qvirtqueue_add(&vqpci->vq, w_req+528, 1, true, false);
     qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
 
-    g_assert(qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
+    /* No notification expected */
+    g_assert(!qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
                                                         QVIRTIO_BLK_TIMEOUT));
 
     status = readb(w_req+528);
@@ -458,6 +480,7 @@ static void pci_msix(void)
 
     qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
 
+
     g_assert(qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
                                                         QVIRTIO_BLK_TIMEOUT));
 
@@ -488,7 +511,7 @@ int main(int argc, char **argv)
 
     g_test_add_func("/virtio/blk/pci/basic", pci_basic);
     g_test_add_func("/virtio/blk/pci/indirect_config", pci_indirect_config);
-    g_test_add_func("/virtio/blk/pci/msix", pci_msix);
+    g_test_add_func("/virtio/blk/pci/msix_idx", pci_msix_idx);
 
     ret = g_test_run();
 
Signature · 1 line
1.7.10.4