replication: interrupt failover if the main device is closed
[qemu/kevin.git] / tests / virtio-blk-test.c
blob05069173413b86c8abb580de61083d86dcbfaf4a
1 /*
2 * QTest testcase for VirtIO Block Device
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
5 * Copyright (c) 2014 Marc MarĂ­
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "libqtest.h"
13 #include "libqos/virtio.h"
14 #include "libqos/virtio-pci.h"
15 #include "libqos/virtio-mmio.h"
16 #include "libqos/pci-pc.h"
17 #include "libqos/malloc.h"
18 #include "libqos/malloc-pc.h"
19 #include "libqos/malloc-generic.h"
20 #include "qemu/bswap.h"
21 #include "standard-headers/linux/virtio_ids.h"
22 #include "standard-headers/linux/virtio_config.h"
23 #include "standard-headers/linux/virtio_ring.h"
24 #include "standard-headers/linux/virtio_blk.h"
25 #include "standard-headers/linux/virtio_pci.h"
27 #define TEST_IMAGE_SIZE (64 * 1024 * 1024)
28 #define QVIRTIO_BLK_TIMEOUT_US (30 * 1000 * 1000)
29 #define PCI_SLOT_HP 0x06
30 #define PCI_SLOT 0x04
31 #define PCI_FN 0x00
33 #define MMIO_PAGE_SIZE 4096
34 #define MMIO_DEV_BASE_ADDR 0x0A003E00
35 #define MMIO_RAM_ADDR 0x40000000
36 #define MMIO_RAM_SIZE 0x20000000
38 typedef struct QVirtioBlkReq {
39 uint32_t type;
40 uint32_t ioprio;
41 uint64_t sector;
42 char *data;
43 uint8_t status;
44 } QVirtioBlkReq;
46 static char *drive_create(void)
48 int fd, ret;
49 char *tmp_path = g_strdup("/tmp/qtest.XXXXXX");
51 /* Create a temporary raw image */
52 fd = mkstemp(tmp_path);
53 g_assert_cmpint(fd, >=, 0);
54 ret = ftruncate(fd, TEST_IMAGE_SIZE);
55 g_assert_cmpint(ret, ==, 0);
56 close(fd);
58 return tmp_path;
61 static QPCIBus *pci_test_start(void)
63 char *cmdline;
64 char *tmp_path;
66 tmp_path = drive_create();
68 cmdline = g_strdup_printf("-drive if=none,id=drive0,file=%s,format=raw "
69 "-drive if=none,id=drive1,file=/dev/null,format=raw "
70 "-device virtio-blk-pci,id=drv0,drive=drive0,"
71 "addr=%x.%x",
72 tmp_path, PCI_SLOT, PCI_FN);
73 qtest_start(cmdline);
74 unlink(tmp_path);
75 g_free(tmp_path);
76 g_free(cmdline);
78 return qpci_init_pc(NULL);
81 static void arm_test_start(void)
83 char *cmdline;
84 char *tmp_path;
86 tmp_path = drive_create();
88 cmdline = g_strdup_printf("-machine virt "
89 "-drive if=none,id=drive0,file=%s,format=raw "
90 "-device virtio-blk-device,drive=drive0",
91 tmp_path);
92 qtest_start(cmdline);
93 unlink(tmp_path);
94 g_free(tmp_path);
95 g_free(cmdline);
98 static void test_end(void)
100 qtest_end();
103 static QVirtioPCIDevice *virtio_blk_pci_init(QPCIBus *bus, int slot)
105 QVirtioPCIDevice *dev;
107 dev = qvirtio_pci_device_find(bus, VIRTIO_ID_BLOCK);
108 g_assert(dev != NULL);
109 g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_BLOCK);
110 g_assert_cmphex(dev->pdev->devfn, ==, ((slot << 3) | PCI_FN));
112 qvirtio_pci_device_enable(dev);
113 qvirtio_reset(&qvirtio_pci, &dev->vdev);
114 qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev);
115 qvirtio_set_driver(&qvirtio_pci, &dev->vdev);
117 return dev;
120 static inline void virtio_blk_fix_request(QVirtioBlkReq *req)
122 #ifdef HOST_WORDS_BIGENDIAN
123 bool host_endian = true;
124 #else
125 bool host_endian = false;
126 #endif
128 if (target_big_endian() != host_endian) {
129 req->type = bswap32(req->type);
130 req->ioprio = bswap32(req->ioprio);
131 req->sector = bswap64(req->sector);
135 static uint64_t virtio_blk_request(QGuestAllocator *alloc, QVirtioBlkReq *req,
136 uint64_t data_size)
138 uint64_t addr;
139 uint8_t status = 0xFF;
141 g_assert_cmpuint(data_size % 512, ==, 0);
142 addr = guest_alloc(alloc, sizeof(*req) + data_size);
144 virtio_blk_fix_request(req);
146 memwrite(addr, req, 16);
147 memwrite(addr + 16, req->data, data_size);
148 memwrite(addr + 16 + data_size, &status, sizeof(status));
150 return addr;
153 static void test_basic(const QVirtioBus *bus, QVirtioDevice *dev,
154 QGuestAllocator *alloc, QVirtQueue *vq, uint64_t device_specific)
156 QVirtioBlkReq req;
157 uint64_t req_addr;
158 uint64_t capacity;
159 uint32_t features;
160 uint32_t free_head;
161 uint8_t status;
162 char *data;
164 capacity = qvirtio_config_readq(bus, dev, device_specific);
166 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
168 features = qvirtio_get_features(bus, dev);
169 features = features & ~(QVIRTIO_F_BAD_FEATURE |
170 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
171 (1u << VIRTIO_RING_F_EVENT_IDX) |
172 (1u << VIRTIO_BLK_F_SCSI));
173 qvirtio_set_features(bus, dev, features);
175 qvirtio_set_driver_ok(bus, dev);
177 /* Write and read with 3 descriptor layout */
178 /* Write request */
179 req.type = VIRTIO_BLK_T_OUT;
180 req.ioprio = 1;
181 req.sector = 0;
182 req.data = g_malloc0(512);
183 strcpy(req.data, "TEST");
185 req_addr = virtio_blk_request(alloc, &req, 512);
187 g_free(req.data);
189 free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
190 qvirtqueue_add(vq, req_addr + 16, 512, false, true);
191 qvirtqueue_add(vq, req_addr + 528, 1, true, false);
193 qvirtqueue_kick(bus, dev, vq, free_head);
195 qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_BLK_TIMEOUT_US);
196 status = readb(req_addr + 528);
197 g_assert_cmpint(status, ==, 0);
199 guest_free(alloc, req_addr);
201 /* Read request */
202 req.type = VIRTIO_BLK_T_IN;
203 req.ioprio = 1;
204 req.sector = 0;
205 req.data = g_malloc0(512);
207 req_addr = virtio_blk_request(alloc, &req, 512);
209 g_free(req.data);
211 free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
212 qvirtqueue_add(vq, req_addr + 16, 512, true, true);
213 qvirtqueue_add(vq, req_addr + 528, 1, true, false);
215 qvirtqueue_kick(bus, dev, vq, free_head);
217 qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_BLK_TIMEOUT_US);
218 status = readb(req_addr + 528);
219 g_assert_cmpint(status, ==, 0);
221 data = g_malloc0(512);
222 memread(req_addr + 16, data, 512);
223 g_assert_cmpstr(data, ==, "TEST");
224 g_free(data);
226 guest_free(alloc, req_addr);
228 if (features & (1u << VIRTIO_F_ANY_LAYOUT)) {
229 /* Write and read with 2 descriptor layout */
230 /* Write request */
231 req.type = VIRTIO_BLK_T_OUT;
232 req.ioprio = 1;
233 req.sector = 1;
234 req.data = g_malloc0(512);
235 strcpy(req.data, "TEST");
237 req_addr = virtio_blk_request(alloc, &req, 512);
239 g_free(req.data);
241 free_head = qvirtqueue_add(vq, req_addr, 528, false, true);
242 qvirtqueue_add(vq, req_addr + 528, 1, true, false);
243 qvirtqueue_kick(bus, dev, vq, free_head);
245 qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_BLK_TIMEOUT_US);
246 status = readb(req_addr + 528);
247 g_assert_cmpint(status, ==, 0);
249 guest_free(alloc, req_addr);
251 /* Read request */
252 req.type = VIRTIO_BLK_T_IN;
253 req.ioprio = 1;
254 req.sector = 1;
255 req.data = g_malloc0(512);
257 req_addr = virtio_blk_request(alloc, &req, 512);
259 g_free(req.data);
261 free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
262 qvirtqueue_add(vq, req_addr + 16, 513, true, false);
264 qvirtqueue_kick(bus, dev, vq, free_head);
266 qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_BLK_TIMEOUT_US);
267 status = readb(req_addr + 528);
268 g_assert_cmpint(status, ==, 0);
270 data = g_malloc0(512);
271 memread(req_addr + 16, data, 512);
272 g_assert_cmpstr(data, ==, "TEST");
273 g_free(data);
275 guest_free(alloc, req_addr);
279 static void pci_basic(void)
281 QVirtioPCIDevice *dev;
282 QPCIBus *bus;
283 QVirtQueuePCI *vqpci;
284 QGuestAllocator *alloc;
285 void *addr;
287 bus = pci_test_start();
288 dev = virtio_blk_pci_init(bus, PCI_SLOT);
290 alloc = pc_alloc_init();
291 vqpci = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
292 alloc, 0);
294 /* MSI-X is not enabled */
295 addr = dev->addr + VIRTIO_PCI_CONFIG_OFF(false);
297 test_basic(&qvirtio_pci, &dev->vdev, alloc, &vqpci->vq,
298 (uint64_t)(uintptr_t)addr);
300 /* End test */
301 qvirtqueue_cleanup(&qvirtio_pci, &vqpci->vq, alloc);
302 pc_alloc_uninit(alloc);
303 qvirtio_pci_device_disable(dev);
304 g_free(dev);
305 qpci_free_pc(bus);
306 test_end();
309 static void pci_indirect(void)
311 QVirtioPCIDevice *dev;
312 QPCIBus *bus;
313 QVirtQueuePCI *vqpci;
314 QGuestAllocator *alloc;
315 QVirtioBlkReq req;
316 QVRingIndirectDesc *indirect;
317 void *addr;
318 uint64_t req_addr;
319 uint64_t capacity;
320 uint32_t features;
321 uint32_t free_head;
322 uint8_t status;
323 char *data;
325 bus = pci_test_start();
327 dev = virtio_blk_pci_init(bus, PCI_SLOT);
329 /* MSI-X is not enabled */
330 addr = dev->addr + VIRTIO_PCI_CONFIG_OFF(false);
332 capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
333 (uint64_t)(uintptr_t)addr);
334 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
336 features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
337 g_assert_cmphex(features & (1u << VIRTIO_RING_F_INDIRECT_DESC), !=, 0);
338 features = features & ~(QVIRTIO_F_BAD_FEATURE |
339 (1u << VIRTIO_RING_F_EVENT_IDX) |
340 (1u << VIRTIO_BLK_F_SCSI));
341 qvirtio_set_features(&qvirtio_pci, &dev->vdev, features);
343 alloc = pc_alloc_init();
344 vqpci = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
345 alloc, 0);
346 qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);
348 /* Write request */
349 req.type = VIRTIO_BLK_T_OUT;
350 req.ioprio = 1;
351 req.sector = 0;
352 req.data = g_malloc0(512);
353 strcpy(req.data, "TEST");
355 req_addr = virtio_blk_request(alloc, &req, 512);
357 g_free(req.data);
359 indirect = qvring_indirect_desc_setup(&dev->vdev, alloc, 2);
360 qvring_indirect_desc_add(indirect, req_addr, 528, false);
361 qvring_indirect_desc_add(indirect, req_addr + 528, 1, true);
362 free_head = qvirtqueue_add_indirect(&vqpci->vq, indirect);
363 qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
365 qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
366 QVIRTIO_BLK_TIMEOUT_US);
367 status = readb(req_addr + 528);
368 g_assert_cmpint(status, ==, 0);
370 g_free(indirect);
371 guest_free(alloc, req_addr);
373 /* Read request */
374 req.type = VIRTIO_BLK_T_IN;
375 req.ioprio = 1;
376 req.sector = 0;
377 req.data = g_malloc0(512);
378 strcpy(req.data, "TEST");
380 req_addr = virtio_blk_request(alloc, &req, 512);
382 g_free(req.data);
384 indirect = qvring_indirect_desc_setup(&dev->vdev, alloc, 2);
385 qvring_indirect_desc_add(indirect, req_addr, 16, false);
386 qvring_indirect_desc_add(indirect, req_addr + 16, 513, true);
387 free_head = qvirtqueue_add_indirect(&vqpci->vq, indirect);
388 qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
390 qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
391 QVIRTIO_BLK_TIMEOUT_US);
392 status = readb(req_addr + 528);
393 g_assert_cmpint(status, ==, 0);
395 data = g_malloc0(512);
396 memread(req_addr + 16, data, 512);
397 g_assert_cmpstr(data, ==, "TEST");
398 g_free(data);
400 g_free(indirect);
401 guest_free(alloc, req_addr);
403 /* End test */
404 qvirtqueue_cleanup(&qvirtio_pci, &vqpci->vq, alloc);
405 pc_alloc_uninit(alloc);
406 qvirtio_pci_device_disable(dev);
407 g_free(dev);
408 qpci_free_pc(bus);
409 test_end();
412 static void pci_config(void)
414 QVirtioPCIDevice *dev;
415 QPCIBus *bus;
416 int n_size = TEST_IMAGE_SIZE / 2;
417 void *addr;
418 uint64_t capacity;
420 bus = pci_test_start();
422 dev = virtio_blk_pci_init(bus, PCI_SLOT);
424 /* MSI-X is not enabled */
425 addr = dev->addr + VIRTIO_PCI_CONFIG_OFF(false);
427 capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
428 (uint64_t)(uintptr_t)addr);
429 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
431 qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);
433 qmp("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', "
434 " 'size': %d } }", n_size);
435 qvirtio_wait_config_isr(&qvirtio_pci, &dev->vdev, QVIRTIO_BLK_TIMEOUT_US);
437 capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
438 (uint64_t)(uintptr_t)addr);
439 g_assert_cmpint(capacity, ==, n_size / 512);
441 qvirtio_pci_device_disable(dev);
442 g_free(dev);
443 qpci_free_pc(bus);
444 test_end();
447 static void pci_msix(void)
449 QVirtioPCIDevice *dev;
450 QPCIBus *bus;
451 QVirtQueuePCI *vqpci;
452 QGuestAllocator *alloc;
453 QVirtioBlkReq req;
454 int n_size = TEST_IMAGE_SIZE / 2;
455 void *addr;
456 uint64_t req_addr;
457 uint64_t capacity;
458 uint32_t features;
459 uint32_t free_head;
460 uint8_t status;
461 char *data;
463 bus = pci_test_start();
464 alloc = pc_alloc_init();
466 dev = virtio_blk_pci_init(bus, PCI_SLOT);
467 qpci_msix_enable(dev->pdev);
469 qvirtio_pci_set_msix_configuration_vector(dev, alloc, 0);
471 /* MSI-X is enabled */
472 addr = dev->addr + VIRTIO_PCI_CONFIG_OFF(true);
474 capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
475 (uint64_t)(uintptr_t)addr);
476 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
478 features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
479 features = features & ~(QVIRTIO_F_BAD_FEATURE |
480 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
481 (1u << VIRTIO_RING_F_EVENT_IDX) |
482 (1u << VIRTIO_BLK_F_SCSI));
483 qvirtio_set_features(&qvirtio_pci, &dev->vdev, features);
485 vqpci = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
486 alloc, 0);
487 qvirtqueue_pci_msix_setup(dev, vqpci, alloc, 1);
489 qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);
491 qmp("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', "
492 " 'size': %d } }", n_size);
494 qvirtio_wait_config_isr(&qvirtio_pci, &dev->vdev, QVIRTIO_BLK_TIMEOUT_US);
496 capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
497 (uint64_t)(uintptr_t)addr);
498 g_assert_cmpint(capacity, ==, n_size / 512);
500 /* Write request */
501 req.type = VIRTIO_BLK_T_OUT;
502 req.ioprio = 1;
503 req.sector = 0;
504 req.data = g_malloc0(512);
505 strcpy(req.data, "TEST");
507 req_addr = virtio_blk_request(alloc, &req, 512);
509 g_free(req.data);
511 free_head = qvirtqueue_add(&vqpci->vq, req_addr, 16, false, true);
512 qvirtqueue_add(&vqpci->vq, req_addr + 16, 512, false, true);
513 qvirtqueue_add(&vqpci->vq, req_addr + 528, 1, true, false);
514 qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
516 qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
517 QVIRTIO_BLK_TIMEOUT_US);
519 status = readb(req_addr + 528);
520 g_assert_cmpint(status, ==, 0);
522 guest_free(alloc, req_addr);
524 /* Read request */
525 req.type = VIRTIO_BLK_T_IN;
526 req.ioprio = 1;
527 req.sector = 0;
528 req.data = g_malloc0(512);
530 req_addr = virtio_blk_request(alloc, &req, 512);
532 g_free(req.data);
534 free_head = qvirtqueue_add(&vqpci->vq, req_addr, 16, false, true);
535 qvirtqueue_add(&vqpci->vq, req_addr + 16, 512, true, true);
536 qvirtqueue_add(&vqpci->vq, req_addr + 528, 1, true, false);
538 qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
541 qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
542 QVIRTIO_BLK_TIMEOUT_US);
544 status = readb(req_addr + 528);
545 g_assert_cmpint(status, ==, 0);
547 data = g_malloc0(512);
548 memread(req_addr + 16, data, 512);
549 g_assert_cmpstr(data, ==, "TEST");
550 g_free(data);
552 guest_free(alloc, req_addr);
554 /* End test */
555 qvirtqueue_cleanup(&qvirtio_pci, &vqpci->vq, alloc);
556 pc_alloc_uninit(alloc);
557 qpci_msix_disable(dev->pdev);
558 qvirtio_pci_device_disable(dev);
559 g_free(dev);
560 qpci_free_pc(bus);
561 test_end();
564 static void pci_idx(void)
566 QVirtioPCIDevice *dev;
567 QPCIBus *bus;
568 QVirtQueuePCI *vqpci;
569 QGuestAllocator *alloc;
570 QVirtioBlkReq req;
571 void *addr;
572 uint64_t req_addr;
573 uint64_t capacity;
574 uint32_t features;
575 uint32_t free_head;
576 uint8_t status;
577 char *data;
579 bus = pci_test_start();
580 alloc = pc_alloc_init();
582 dev = virtio_blk_pci_init(bus, PCI_SLOT);
583 qpci_msix_enable(dev->pdev);
585 qvirtio_pci_set_msix_configuration_vector(dev, alloc, 0);
587 /* MSI-X is enabled */
588 addr = dev->addr + VIRTIO_PCI_CONFIG_OFF(true);
590 capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
591 (uint64_t)(uintptr_t)addr);
592 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
594 features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
595 features = features & ~(QVIRTIO_F_BAD_FEATURE |
596 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
597 (1u << VIRTIO_F_NOTIFY_ON_EMPTY) |
598 (1u << VIRTIO_BLK_F_SCSI));
599 qvirtio_set_features(&qvirtio_pci, &dev->vdev, features);
601 vqpci = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
602 alloc, 0);
603 qvirtqueue_pci_msix_setup(dev, vqpci, alloc, 1);
605 qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);
607 /* Write request */
608 req.type = VIRTIO_BLK_T_OUT;
609 req.ioprio = 1;
610 req.sector = 0;
611 req.data = g_malloc0(512);
612 strcpy(req.data, "TEST");
614 req_addr = virtio_blk_request(alloc, &req, 512);
616 g_free(req.data);
618 free_head = qvirtqueue_add(&vqpci->vq, req_addr, 16, false, true);
619 qvirtqueue_add(&vqpci->vq, req_addr + 16, 512, false, true);
620 qvirtqueue_add(&vqpci->vq, req_addr + 528, 1, true, false);
621 qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
623 qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
624 QVIRTIO_BLK_TIMEOUT_US);
626 /* Write request */
627 req.type = VIRTIO_BLK_T_OUT;
628 req.ioprio = 1;
629 req.sector = 1;
630 req.data = g_malloc0(512);
631 strcpy(req.data, "TEST");
633 req_addr = virtio_blk_request(alloc, &req, 512);
635 g_free(req.data);
637 /* Notify after processing the third request */
638 qvirtqueue_set_used_event(&vqpci->vq, 2);
639 free_head = qvirtqueue_add(&vqpci->vq, req_addr, 16, false, true);
640 qvirtqueue_add(&vqpci->vq, req_addr + 16, 512, false, true);
641 qvirtqueue_add(&vqpci->vq, req_addr + 528, 1, true, false);
642 qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
644 /* No notification expected */
645 status = qvirtio_wait_status_byte_no_isr(&qvirtio_pci, &dev->vdev,
646 &vqpci->vq, req_addr + 528,
647 QVIRTIO_BLK_TIMEOUT_US);
648 g_assert_cmpint(status, ==, 0);
650 guest_free(alloc, req_addr);
652 /* Read request */
653 req.type = VIRTIO_BLK_T_IN;
654 req.ioprio = 1;
655 req.sector = 1;
656 req.data = g_malloc0(512);
658 req_addr = virtio_blk_request(alloc, &req, 512);
660 g_free(req.data);
662 free_head = qvirtqueue_add(&vqpci->vq, req_addr, 16, false, true);
663 qvirtqueue_add(&vqpci->vq, req_addr + 16, 512, true, true);
664 qvirtqueue_add(&vqpci->vq, req_addr + 528, 1, true, false);
666 qvirtqueue_kick(&qvirtio_pci, &dev->vdev, &vqpci->vq, free_head);
668 qvirtio_wait_queue_isr(&qvirtio_pci, &dev->vdev, &vqpci->vq,
669 QVIRTIO_BLK_TIMEOUT_US);
671 status = readb(req_addr + 528);
672 g_assert_cmpint(status, ==, 0);
674 data = g_malloc0(512);
675 memread(req_addr + 16, data, 512);
676 g_assert_cmpstr(data, ==, "TEST");
677 g_free(data);
679 guest_free(alloc, req_addr);
681 /* End test */
682 qvirtqueue_cleanup(&qvirtio_pci, &vqpci->vq, alloc);
683 pc_alloc_uninit(alloc);
684 qpci_msix_disable(dev->pdev);
685 qvirtio_pci_device_disable(dev);
686 g_free(dev);
687 qpci_free_pc(bus);
688 test_end();
691 static void pci_hotplug(void)
693 QPCIBus *bus;
694 QVirtioPCIDevice *dev;
696 bus = pci_test_start();
698 /* plug secondary disk */
699 qpci_plug_device_test("virtio-blk-pci", "drv1", PCI_SLOT_HP,
700 "'drive': 'drive1'");
702 dev = virtio_blk_pci_init(bus, PCI_SLOT_HP);
703 g_assert(dev);
704 qvirtio_pci_device_disable(dev);
705 g_free(dev);
707 /* unplug secondary disk */
708 qpci_unplug_acpi_device_test("drv1", PCI_SLOT_HP);
709 qpci_free_pc(bus);
710 test_end();
713 static void mmio_basic(void)
715 QVirtioMMIODevice *dev;
716 QVirtQueue *vq;
717 QGuestAllocator *alloc;
718 int n_size = TEST_IMAGE_SIZE / 2;
719 uint64_t capacity;
721 arm_test_start();
723 dev = qvirtio_mmio_init_device(MMIO_DEV_BASE_ADDR, MMIO_PAGE_SIZE);
724 g_assert(dev != NULL);
725 g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_BLOCK);
727 qvirtio_reset(&qvirtio_mmio, &dev->vdev);
728 qvirtio_set_acknowledge(&qvirtio_mmio, &dev->vdev);
729 qvirtio_set_driver(&qvirtio_mmio, &dev->vdev);
731 alloc = generic_alloc_init(MMIO_RAM_ADDR, MMIO_RAM_SIZE, MMIO_PAGE_SIZE);
732 vq = qvirtqueue_setup(&qvirtio_mmio, &dev->vdev, alloc, 0);
734 test_basic(&qvirtio_mmio, &dev->vdev, alloc, vq,
735 QVIRTIO_MMIO_DEVICE_SPECIFIC);
737 qmp("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', "
738 " 'size': %d } }", n_size);
740 qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
741 QVIRTIO_BLK_TIMEOUT_US);
743 capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev,
744 QVIRTIO_MMIO_DEVICE_SPECIFIC);
745 g_assert_cmpint(capacity, ==, n_size / 512);
747 /* End test */
748 qvirtqueue_cleanup(&qvirtio_mmio, vq, alloc);
749 generic_alloc_uninit(alloc);
750 g_free(dev);
751 test_end();
754 int main(int argc, char **argv)
756 const char *arch = qtest_get_arch();
758 g_test_init(&argc, &argv, NULL);
760 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
761 qtest_add_func("/virtio/blk/pci/basic", pci_basic);
762 qtest_add_func("/virtio/blk/pci/indirect", pci_indirect);
763 qtest_add_func("/virtio/blk/pci/config", pci_config);
764 qtest_add_func("/virtio/blk/pci/msix", pci_msix);
765 qtest_add_func("/virtio/blk/pci/idx", pci_idx);
766 qtest_add_func("/virtio/blk/pci/hotplug", pci_hotplug);
767 } else if (strcmp(arch, "arm") == 0) {
768 qtest_add_func("/virtio/blk/mmio/basic", mmio_basic);
771 return g_test_run();