virtio-blk-test.c 20.3 KB
Newer Older
A
Andreas Färber 已提交
1 2 3 4
/*
 * QTest testcase for VirtIO Block Device
 *
 * Copyright (c) 2014 SUSE LINUX Products GmbH
5
 * Copyright (c) 2014 Marc Marí
A
Andreas Färber 已提交
6 7 8 9 10 11 12
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#include <glib.h>
#include <string.h>
13 14 15
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
A
Andreas Färber 已提交
16
#include "libqtest.h"
17 18 19
#include "libqos/virtio.h"
#include "libqos/virtio-pci.h"
#include "libqos/pci-pc.h"
20 21 22
#include "libqos/malloc.h"
#include "libqos/malloc-pc.h"
#include "qemu/bswap.h"
A
Andreas Färber 已提交
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#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)
44
#define QVIRTIO_BLK_TIMEOUT_US  (30 * 1000 * 1000)
45 46 47
#define PCI_SLOT                0x04
#define PCI_FN                  0x00

48 49
#define PCI_SLOT_HP             0x06

50 51 52 53 54 55 56
typedef struct QVirtioBlkReq {
    uint32_t type;
    uint32_t ioprio;
    uint64_t sector;
    char *data;
    uint8_t status;
} QVirtioBlkReq;
57

58
static char *drive_create(void)
A
Andreas Färber 已提交
59
{
60
    int fd, ret;
61
    char *tmp_path = g_strdup("/tmp/qtest.XXXXXX");
62 63 64 65 66 67 68 69

    /* Create a temporary raw image */
    fd = mkstemp(tmp_path);
    g_assert_cmpint(fd, >=, 0);
    ret = ftruncate(fd, TEST_IMAGE_SIZE);
    g_assert_cmpint(ret, ==, 0);
    close(fd);

70 71 72 73 74 75 76 77 78 79
    return tmp_path;
}

static QPCIBus *pci_test_start(void)
{
    char *cmdline;
    char *tmp_path;

    tmp_path = drive_create();

80
    cmdline = g_strdup_printf("-drive if=none,id=drive0,file=%s,format=raw "
81 82 83 84
                        "-drive if=none,id=drive1,file=/dev/null,format=raw "
                        "-device virtio-blk-pci,id=drv0,drive=drive0,"
                        "addr=%x.%x",
                        tmp_path, PCI_SLOT, PCI_FN);
85 86
    qtest_start(cmdline);
    unlink(tmp_path);
87
    g_free(tmp_path);
88
    g_free(cmdline);
89 90 91 92 93 94 95 96 97

    return qpci_init_pc();
}

static void test_end(void)
{
    qtest_end();
}

98
static QVirtioPCIDevice *virtio_blk_pci_init(QPCIBus *bus, int slot)
99 100 101 102 103 104
{
    QVirtioPCIDevice *dev;

    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);
105
    g_assert_cmphex(dev->pdev->devfn, ==, ((slot << 3) | PCI_FN));
106

107 108 109 110 111 112 113 114
    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);

    return dev;
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
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;
}

148 149
static void test_basic(const QVirtioBus *bus, QVirtioDevice *dev,
            QGuestAllocator *alloc, QVirtQueue *vq, uint64_t device_specific)
150
{
151 152
    QVirtioBlkReq req;
    uint64_t req_addr;
153
    uint64_t capacity;
154 155 156 157
    uint32_t features;
    uint32_t free_head;
    uint8_t status;
    char *data;
158

159
    capacity = qvirtio_config_readq(bus, dev, device_specific);
160

161 162
    g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);

163
    features = qvirtio_get_features(bus, dev);
164 165 166
    features = features & ~(QVIRTIO_F_BAD_FEATURE |
                    QVIRTIO_F_RING_INDIRECT_DESC | QVIRTIO_F_RING_EVENT_IDX |
                            QVIRTIO_BLK_F_SCSI);
167
    qvirtio_set_features(bus, dev, features);
168

169
    qvirtio_set_driver_ok(bus, dev);
170 171 172 173 174 175 176 177 178 179 180 181 182

    /* 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);

183 184 185
    free_head = qvirtqueue_add(vq, req_addr, 528, false, true);
    qvirtqueue_add(vq, req_addr + 528, 1, true, false);
    qvirtqueue_kick(bus, dev, vq, free_head);
186

187
    qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_BLK_TIMEOUT_US);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    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);

203 204
    free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
    qvirtqueue_add(vq, req_addr + 16, 513, true, false);
205

206
    qvirtqueue_kick(bus, dev, vq, free_head);
207

208
    qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_BLK_TIMEOUT_US);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    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);

229
    g_free(req.data);
230

231 232 233
    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);
234

235 236 237
    qvirtqueue_kick(bus, dev, vq, free_head);

    qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_BLK_TIMEOUT_US);
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    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);

253 254 255
    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);
256

257
    qvirtqueue_kick(bus, dev, vq, free_head);
258

259
    qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_BLK_TIMEOUT_US);
260 261 262 263 264 265 266 267 268
    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);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
}

static void pci_basic(void)
{
    QVirtioPCIDevice *dev;
    QPCIBus *bus;
    QVirtQueuePCI *vqpci;
    QGuestAllocator *alloc;
    void *addr;

    bus = pci_test_start();
    dev = virtio_blk_pci_init(bus, PCI_SLOT);

    alloc = pc_alloc_init();
    vqpci = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
                                                                    alloc, 0);

    /* MSI-X is not enabled */
287
    addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX;
288 289 290

    test_basic(&qvirtio_pci, &dev->vdev, alloc, &vqpci->vq,
                                                    (uint64_t)(uintptr_t)addr);
291 292

    /* End test */
M
Marc Marí 已提交
293
    guest_free(alloc, vqpci->vq.desc);
294
    pc_alloc_uninit(alloc);
295
    qvirtio_pci_device_disable(dev);
296
    g_free(dev);
297
    qpci_free_pc(bus);
298
    test_end();
A
Andreas Färber 已提交
299 300
}

301 302 303 304
static void pci_indirect(void)
{
    QVirtioPCIDevice *dev;
    QPCIBus *bus;
M
Marc Marí 已提交
305
    QVirtQueuePCI *vqpci;
306 307 308 309 310 311 312 313 314 315 316
    QGuestAllocator *alloc;
    QVirtioBlkReq req;
    QVRingIndirectDesc *indirect;
    void *addr;
    uint64_t req_addr;
    uint64_t capacity;
    uint32_t features;
    uint32_t free_head;
    uint8_t status;
    char *data;

317
    bus = pci_test_start();
318

319
    dev = virtio_blk_pci_init(bus, PCI_SLOT);
320 321

    /* MSI-X is not enabled */
322
    addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX;
323

324 325
    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                    (uint64_t)(uintptr_t)addr);
326 327 328 329 330 331 332 333 334
    g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);

    features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
    g_assert_cmphex(features & QVIRTIO_F_RING_INDIRECT_DESC, !=, 0);
    features = features & ~(QVIRTIO_F_BAD_FEATURE | QVIRTIO_F_RING_EVENT_IDX |
                                                            QVIRTIO_BLK_F_SCSI);
    qvirtio_set_features(&qvirtio_pci, &dev->vdev, features);

    alloc = pc_alloc_init();
M
Marc Marí 已提交
335 336
    vqpci = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
                                                                    alloc, 0);
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);

    /* 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);

    indirect = qvring_indirect_desc_setup(&dev->vdev, alloc, 2);
    qvring_indirect_desc_add(indirect, req_addr, 528, false);
    qvring_indirect_desc_add(indirect, req_addr + 528, 1, true);
M
Marc Marí 已提交
353 354
    free_head = qvirtqueue_add_indirect(&vqpci->vq, indirect);
    qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
355

356 357
    qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
                           QVIRTIO_BLK_TIMEOUT_US);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    status = readb(req_addr + 528);
    g_assert_cmpint(status, ==, 0);

    g_free(indirect);
    guest_free(alloc, req_addr);

    /* Read request */
    req.type = QVIRTIO_BLK_T_IN;
    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);

    indirect = qvring_indirect_desc_setup(&dev->vdev, alloc, 2);
    qvring_indirect_desc_add(indirect, req_addr, 16, false);
    qvring_indirect_desc_add(indirect, req_addr + 16, 513, true);
M
Marc Marí 已提交
378 379
    free_head = qvirtqueue_add_indirect(&vqpci->vq, indirect);
    qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
380

381 382
    qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
                           QVIRTIO_BLK_TIMEOUT_US);
383 384 385 386 387 388 389 390 391 392 393 394
    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);

    g_free(indirect);
    guest_free(alloc, req_addr);

    /* End test */
M
Marc Marí 已提交
395
    guest_free(alloc, vqpci->vq.desc);
396
    pc_alloc_uninit(alloc);
397 398
    qvirtio_pci_device_disable(dev);
    g_free(dev);
399
    qpci_free_pc(bus);
400 401 402
    test_end();
}

403 404 405 406 407 408 409 410
static void pci_config(void)
{
    QVirtioPCIDevice *dev;
    QPCIBus *bus;
    int n_size = TEST_IMAGE_SIZE / 2;
    void *addr;
    uint64_t capacity;

411
    bus = pci_test_start();
412

413
    dev = virtio_blk_pci_init(bus, PCI_SLOT);
414 415

    /* MSI-X is not enabled */
416
    addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX;
417

418 419
    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                    (uint64_t)(uintptr_t)addr);
420 421 422 423 424 425
    g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);

    qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);

    qmp("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', "
                                                    " 'size': %d } }", n_size);
426
    qvirtio_wait_config_isr(&qvirtio_pci, &dev->vdev, QVIRTIO_BLK_TIMEOUT_US);
427

428 429
    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                    (uint64_t)(uintptr_t)addr);
430 431 432 433
    g_assert_cmpint(capacity, ==, n_size / 512);

    qvirtio_pci_device_disable(dev);
    g_free(dev);
434
    qpci_free_pc(bus);
435 436 437
    test_end();
}

M
Marc Marí 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
static void pci_msix(void)
{
    QVirtioPCIDevice *dev;
    QPCIBus *bus;
    QVirtQueuePCI *vqpci;
    QGuestAllocator *alloc;
    QVirtioBlkReq req;
    int n_size = TEST_IMAGE_SIZE / 2;
    void *addr;
    uint64_t req_addr;
    uint64_t capacity;
    uint32_t features;
    uint32_t free_head;
    uint8_t status;
    char *data;

454
    bus = pci_test_start();
M
Marc Marí 已提交
455 456
    alloc = pc_alloc_init();

457
    dev = virtio_blk_pci_init(bus, PCI_SLOT);
M
Marc Marí 已提交
458 459 460 461 462
    qpci_msix_enable(dev->pdev);

    qvirtio_pci_set_msix_configuration_vector(dev, alloc, 0);

    /* MSI-X is enabled */
463
    addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_MSIX;
M
Marc Marí 已提交
464

465 466
    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                    (uint64_t)(uintptr_t)addr);
M
Marc Marí 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    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);

    vqpci = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
                                                                    alloc, 0);
    qvirtqueue_pci_msix_setup(dev, vqpci, alloc, 1);

    qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);

    qmp("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', "
                                                    " 'size': %d } }", n_size);

484
    qvirtio_wait_config_isr(&qvirtio_pci, &dev->vdev, QVIRTIO_BLK_TIMEOUT_US);
M
Marc Marí 已提交
485

486 487
    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                    (uint64_t)(uintptr_t)addr);
M
Marc Marí 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    g_assert_cmpint(capacity, ==, n_size / 512);

    /* 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(&vqpci->vq, req_addr, 528, false, true);
    qvirtqueue_add(&vqpci->vq, req_addr + 528, 1, true, false);
    qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);

505 506
    qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
                           QVIRTIO_BLK_TIMEOUT_US);
M
Marc Marí 已提交
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527

    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(&vqpci->vq, req_addr, 16, false, true);
    qvirtqueue_add(&vqpci->vq, req_addr + 16, 513, true, false);

    qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);

M
Marc Marí 已提交
528

529 530
    qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
                           QVIRTIO_BLK_TIMEOUT_US);
M
Marc Marí 已提交
531 532 533 534 535 536 537 538 539 540 541 542

    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);

    /* End test */
543 544
    guest_free(alloc, vqpci->vq.desc);
    pc_alloc_uninit(alloc);
M
Marc Marí 已提交
545 546 547
    qpci_msix_disable(dev->pdev);
    qvirtio_pci_device_disable(dev);
    g_free(dev);
548
    qpci_free_pc(bus);
M
Marc Marí 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    test_end();
}

static void pci_idx(void)
{
    QVirtioPCIDevice *dev;
    QPCIBus *bus;
    QVirtQueuePCI *vqpci;
    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;

567
    bus = pci_test_start();
M
Marc Marí 已提交
568 569
    alloc = pc_alloc_init();

570
    dev = virtio_blk_pci_init(bus, PCI_SLOT);
M
Marc Marí 已提交
571 572 573 574 575
    qpci_msix_enable(dev->pdev);

    qvirtio_pci_set_msix_configuration_vector(dev, alloc, 0);

    /* MSI-X is enabled */
576
    addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_MSIX;
M
Marc Marí 已提交
577

578 579
    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                    (uint64_t)(uintptr_t)addr);
M
Marc Marí 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    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_NOTIFY_ON_EMPTY | QVIRTIO_BLK_F_SCSI);
    qvirtio_set_features(&qvirtio_pci, &dev->vdev, features);

    vqpci = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
                                                                    alloc, 0);
    qvirtqueue_pci_msix_setup(dev, vqpci, alloc, 1);

    qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);

    /* 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(&vqpci->vq, req_addr, 528, false, true);
    qvirtqueue_add(&vqpci->vq, req_addr + 528, 1, true, false);
    qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);

609 610
    qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
                           QVIRTIO_BLK_TIMEOUT_US);
M
Marc Marí 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

    /* 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);

    /* Notify after processing the third request */
    qvirtqueue_set_used_event(&vqpci->vq, 2);
    free_head = qvirtqueue_add(&vqpci->vq, req_addr, 528, false, true);
    qvirtqueue_add(&vqpci->vq, req_addr + 528, 1, true, false);
    qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);

    /* No notification expected */
630 631 632
    status = qvirtio_wait_status_byte_no_isr(&qvirtio_pci, &dev->vdev,
                                             &vqpci->vq, req_addr + 528,
                                             QVIRTIO_BLK_TIMEOUT_US);
M
Marc Marí 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
    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(&vqpci->vq, req_addr, 16, false, true);
    qvirtqueue_add(&vqpci->vq, req_addr + 16, 513, true, false);

    qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);


653 654
    qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
                           QVIRTIO_BLK_TIMEOUT_US);
M
Marc Marí 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667

    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);

    /* End test */
    guest_free(alloc, vqpci->vq.desc);
668
    pc_alloc_uninit(alloc);
M
Marc Marí 已提交
669 670 671
    qpci_msix_disable(dev->pdev);
    qvirtio_pci_device_disable(dev);
    g_free(dev);
672
    qpci_free_pc(bus);
M
Marc Marí 已提交
673 674 675
    test_end();
}

676
static void pci_hotplug(void)
677 678 679 680
{
    QPCIBus *bus;
    QVirtioPCIDevice *dev;

681
    bus = pci_test_start();
682 683 684 685 686

    /* plug secondary disk */
    qpci_plug_device_test("virtio-blk-pci", "drv1", PCI_SLOT_HP,
                          "'drive': 'drive1'");

687
    dev = virtio_blk_pci_init(bus, PCI_SLOT_HP);
688 689 690 691 692 693
    g_assert(dev);
    qvirtio_pci_device_disable(dev);
    g_free(dev);

    /* unplug secondary disk */
    qpci_unplug_acpi_device_test("drv1", PCI_SLOT_HP);
694
    qpci_free_pc(bus);
695 696 697
    test_end();
}

A
Andreas Färber 已提交
698 699 700 701 702 703
int main(int argc, char **argv)
{
    int ret;

    g_test_init(&argc, &argv, NULL);

704
    g_test_add_func("/virtio/blk/pci/basic", pci_basic);
705
    g_test_add_func("/virtio/blk/pci/indirect", pci_indirect);
706
    g_test_add_func("/virtio/blk/pci/config", pci_config);
M
Marc Marí 已提交
707
    g_test_add_func("/virtio/blk/pci/msix", pci_msix);
M
Marc Marí 已提交
708
    g_test_add_func("/virtio/blk/pci/idx", pci_idx);
709
    g_test_add_func("/virtio/blk/pci/hotplug", pci_hotplug);
A
Andreas Färber 已提交
710

711
    ret = g_test_run();
A
Andreas Färber 已提交
712 713 714

    return ret;
}