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

Signed-off-by: Marc Marí <marc.mari.barc...@gmail.com>
---
 tests/libqos/virtio.c   |   62 ++++++++++++++++++++++++
 tests/libqos/virtio.h   |   11 +++++
 tests/virtio-blk-test.c |  120 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 193 insertions(+)

diff --git a/tests/libqos/virtio.c b/tests/libqos/virtio.c
index fe45836..9657b4c 100644
--- a/tests/libqos/virtio.c
+++ b/tests/libqos/virtio.c
@@ -105,6 +105,51 @@ void qvring_init(const QGuestAllocator *alloc, QVirtQueue 
*vq, uint64_t addr)
     writew(vq->used, 0);
 }
 
+QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
+                                        QGuestAllocator *alloc, uint16_t elem)
+{
+    int i;
+    QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
+
+    indirect->index = 0;
+    indirect->elem = elem;
+    indirect->desc = guest_alloc(alloc, sizeof(QVRingDesc)*elem);
+
+    for (i = 0; i < elem-1; ++i) {
+        /* indirect->desc[i].addr */
+        writeq(indirect->desc+(16*i), 0);
+        /* indirect->desc[i].flags */
+        writew(indirect->desc+(16*i)+12, QVRING_DESC_F_NEXT);
+        /* indirect->desc[i].next */
+        writew(indirect->desc+(16*i)+14, i+1);
+    }
+
+    return indirect;
+}
+
+void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data,
+                                                    uint32_t len, int write)
+{
+    uint16_t flags;
+
+    g_assert_cmpint(indirect->index, <, indirect->elem);
+
+    flags = readw(indirect->desc+(16*indirect->index)+12);
+
+    if (write) {
+        flags |= QVRING_DESC_F_WRITE;
+    }
+
+    /* indirect->desc[indirect->index].addr */
+    writeq(indirect->desc+(16*indirect->index), data);
+    /* indirect->desc[indirect->index].len */
+    writel(indirect->desc+(16*indirect->index)+8, len);
+    /* indirect->desc[indirect->index].flags */
+    writew(indirect->desc+(16*indirect->index)+12, flags);
+
+    indirect->index++;
+}
+
 uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, int write,
                                                                     int next)
 {
@@ -129,6 +174,23 @@ uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, 
uint32_t len, int write,
     return vq->free_head++; /* Return and increase, in this order */
 }
 
+uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
+{
+    g_assert_cmpint(vq->size, >=, indirect->elem);
+    g_assert_cmpint(indirect->index, ==, indirect->elem);
+
+    vq->num_free--;
+
+    /* vq->desc[vq->free_head].addr */
+    writeq(vq->desc+(16*vq->free_head), indirect->desc);
+    /* vq->desc[vq->free_head].len */
+    writel(vq->desc+(16*vq->free_head)+8, sizeof(QVRingDesc)*indirect->elem);
+    /* vq->desc[vq->free_head].flags */
+    writew(vq->desc+(16*vq->free_head)+12, QVRING_DESC_F_INDIRECT);
+
+    return vq->free_head++; /* Return and increase, in this order */
+}
+
 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
                                                             uint32_t free_head)
 {
diff --git a/tests/libqos/virtio.h b/tests/libqos/virtio.h
index 1ac1fea..f5b8c84 100644
--- a/tests/libqos/virtio.h
+++ b/tests/libqos/virtio.h
@@ -70,6 +70,12 @@ typedef struct QVirtQueue {
     uint32_t align;
 } 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);
@@ -133,8 +139,13 @@ int qvirtio_wait_isr(const QVirtioBus *bus, QVirtioDevice 
*d, uint8_t mask,
                                                             uint64_t timeout);
 
 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, int write);
 uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, int write,
                                                                     int next);
+uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect);
 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
                                                             uint32_t 
free_head);
 
diff --git a/tests/virtio-blk-test.c b/tests/virtio-blk-test.c
index 4f53bcb..54326d3 100644
--- a/tests/virtio-blk-test.c
+++ b/tests/virtio-blk-test.c
@@ -229,6 +229,125 @@ static void pci_basic(void)
     test_end();
 }
 
+static void pci_indirect(void)
+{
+    QVirtioPCIDevice *dev;
+    QPCIBus *bus;
+    QVirtQueue *vq;
+    QGuestAllocator *alloc;
+    QVRingIndirectDesc *indirect;
+    int isr_result;
+    void *addr;
+    uint64_t w_req;
+    uint64_t r_req;
+    uint64_t capacity;
+    uint32_t features;
+    uint32_t free_head;
+    uint8_t status;
+    char *data;
+
+    bus = test_start();
+
+    dev = qvirtio_pci_device_find(bus, QVIRTIO_BLK_DEVICE_ID);
+    g_assert(dev != NULL);
+    g_assert_cmphex(dev->vdev.device_type, ==, QVIRTIO_BLK_DEVICE_ID);
+    g_assert_cmphex(dev->pdev->devfn, ==, ((PCI_SLOT << 3) | PCI_FN));
+
+    qvirtio_pci_enable_device(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);
+
+    features = qvirtio_pci.get_features(&dev->vdev);
+    g_assert_cmphex(features & QVIRTIO_F_RING_INDIRECT_DESC, !=, 0);
+    features = features & ~(QVIRTIO_F_RING_EVENT_IDX | QVIRTIO_BLK_F_SCSI);
+    qvirtio_pci.set_features(&dev->vdev, features);
+
+    alloc = pc_alloc_init();
+    vq = qvirtio_pci.virtqueue_setup(&dev->vdev, alloc, 0);
+
+    qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);
+
+    /* Write and read with 2 descriptor layout */
+    data = g_malloc0(512);
+    strcpy(data, "TEST");
+
+    /* 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, 0);
+    /* w_req->data */
+    memwrite(w_req+16, data, 512);
+    /* w_req->status */
+    writeb(w_req+528, 0xFF);
+
+    g_free(data);
+
+    indirect = qvring_indirect_desc_setup(&dev->vdev, alloc, 2);
+    qvring_indirect_desc_add(indirect, w_req, 528, 0);
+    qvring_indirect_desc_add(indirect, w_req+528, 1, 1);
+    free_head = qvirtqueue_add_indirect(vq, indirect);
+    qvirtqueue_kick(&qvirtio_pci, &dev->vdev, vq, free_head);
+
+    isr_result = qvirtio_wait_isr(&qvirtio_pci, &dev->vdev, 0x1,
+                                                        QVIRTIO_BLK_TIMEOUT);
+    g_assert_cmpint(isr_result, ==, 0);
+    status = readb(w_req+528);
+    g_assert_cmpint(status, ==, 0);
+
+    g_free(indirect);
+
+    /* Read request */
+    r_req = guest_alloc(alloc, sizeof(QVirtioBlkReq)+512);
+    /* r_req->type */
+    writel(r_req, QVIRTIO_BLK_T_IN);
+    /* r_req->ioprio */
+    writel(r_req+4, 1);
+    /* r_req->sector */
+    writeq(r_req+8, 0);
+    /* r_req->status */
+    writeb(r_req+528, 0xFF);
+
+    indirect = qvring_indirect_desc_setup(&dev->vdev, alloc, 2);
+    qvring_indirect_desc_add(indirect, r_req, 16, 0);
+    qvring_indirect_desc_add(indirect, r_req+16, 513, 1);
+    free_head = qvirtqueue_add_indirect(vq, indirect);
+    qvirtqueue_kick(&qvirtio_pci, &dev->vdev, vq, free_head);
+
+    isr_result = qvirtio_wait_isr(&qvirtio_pci, &dev->vdev, 0x1,
+                                                        QVIRTIO_BLK_TIMEOUT);
+    g_assert_cmpint(isr_result, ==, 0);
+    status = readb(r_req+528);
+    g_assert_cmpint(status, ==, 0);
+
+    g_free(indirect);
+
+    data = g_malloc0(512);
+    memread(r_req+16, data, 512);
+    g_assert_cmpstr(data, ==, "TEST");
+    g_free(data);
+    g_free(indirect);
+
+    guest_free(alloc, w_req);
+    guest_free(alloc, r_req);
+
+    /* End test */
+    guest_free(alloc, vq->desc);
+    qvirtio_pci_disable_device(dev);
+    g_free(dev);
+    test_end();
+}
+
 int main(int argc, char **argv)
 {
     int ret;
@@ -236,6 +355,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);
 
     ret = g_test_run();
 
-- 
1.7.10.4


Reply via email to