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.
11 #include "qemu/osdep.h"
13 #include "libqos/libqos-pc.h"
14 #include "libqos/libqos-spapr.h"
15 #include "libqos/virtio.h"
16 #include "libqos/virtio-pci.h"
17 #include "libqos/virtio-mmio.h"
18 #include "libqos/malloc-generic.h"
19 #include "qemu/bswap.h"
20 #include "standard-headers/linux/virtio_ids.h"
21 #include "standard-headers/linux/virtio_config.h"
22 #include "standard-headers/linux/virtio_ring.h"
23 #include "standard-headers/linux/virtio_blk.h"
24 #include "standard-headers/linux/virtio_pci.h"
26 #define TEST_IMAGE_SIZE (64 * 1024 * 1024)
27 #define QVIRTIO_BLK_TIMEOUT_US (30 * 1000 * 1000)
28 #define PCI_SLOT_HP 0x06
32 #define MMIO_PAGE_SIZE 4096
33 #define MMIO_DEV_BASE_ADDR 0x0A003E00
34 #define MMIO_RAM_ADDR 0x40000000
35 #define MMIO_RAM_SIZE 0x20000000
37 typedef struct QVirtioBlkReq
{
45 static char *drive_create(void)
48 char *tmp_path
= g_strdup("/tmp/qtest.XXXXXX");
50 /* Create a temporary raw image */
51 fd
= mkstemp(tmp_path
);
52 g_assert_cmpint(fd
, >=, 0);
53 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
54 g_assert_cmpint(ret
, ==, 0);
60 static QOSState
*pci_test_start(void)
63 const char *arch
= qtest_get_arch();
65 const char *cmd
= "-drive if=none,id=drive0,file=%s,format=raw "
66 "-drive if=none,id=drive1,file=null-co://,format=raw "
67 "-device virtio-blk-pci,id=drv0,drive=drive0,"
70 tmp_path
= drive_create();
72 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
73 qs
= qtest_pc_boot(cmd
, tmp_path
, PCI_SLOT
, PCI_FN
);
74 } else if (strcmp(arch
, "ppc64") == 0) {
75 qs
= qtest_spapr_boot(cmd
, tmp_path
, PCI_SLOT
, PCI_FN
);
77 g_printerr("virtio-blk tests are only available on x86 or ppc64\n");
85 static void arm_test_start(void)
89 tmp_path
= drive_create();
91 global_qtest
= qtest_startf("-machine virt "
92 "-drive if=none,id=drive0,file=%s,format=raw "
93 "-device virtio-blk-device,drive=drive0",
99 static void test_end(void)
104 static QVirtioPCIDevice
*virtio_blk_pci_init(QPCIBus
*bus
, int slot
)
106 QVirtioPCIDevice
*dev
;
108 dev
= qvirtio_pci_device_find_slot(bus
, VIRTIO_ID_BLOCK
, slot
);
109 g_assert(dev
!= NULL
);
110 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_BLOCK
);
111 g_assert_cmphex(dev
->pdev
->devfn
, ==, ((slot
<< 3) | PCI_FN
));
113 qvirtio_pci_device_enable(dev
);
114 qvirtio_reset(&dev
->vdev
);
115 qvirtio_set_acknowledge(&dev
->vdev
);
116 qvirtio_set_driver(&dev
->vdev
);
121 static inline void virtio_blk_fix_request(QVirtioDevice
*d
, QVirtioBlkReq
*req
)
123 #ifdef HOST_WORDS_BIGENDIAN
124 const bool host_is_big_endian
= true;
126 const bool host_is_big_endian
= false;
129 if (qvirtio_is_big_endian(d
) != host_is_big_endian
) {
130 req
->type
= bswap32(req
->type
);
131 req
->ioprio
= bswap32(req
->ioprio
);
132 req
->sector
= bswap64(req
->sector
);
136 static uint64_t virtio_blk_request(QGuestAllocator
*alloc
, QVirtioDevice
*d
,
137 QVirtioBlkReq
*req
, uint64_t data_size
)
140 uint8_t status
= 0xFF;
142 g_assert_cmpuint(data_size
% 512, ==, 0);
143 addr
= guest_alloc(alloc
, sizeof(*req
) + data_size
);
145 virtio_blk_fix_request(d
, req
);
147 memwrite(addr
, req
, 16);
148 memwrite(addr
+ 16, req
->data
, data_size
);
149 memwrite(addr
+ 16 + data_size
, &status
, sizeof(status
));
154 static void test_basic(QVirtioDevice
*dev
, QGuestAllocator
*alloc
,
165 capacity
= qvirtio_config_readq(dev
, 0);
167 g_assert_cmpint(capacity
, ==, TEST_IMAGE_SIZE
/ 512);
169 features
= qvirtio_get_features(dev
);
170 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
171 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
172 (1u << VIRTIO_RING_F_EVENT_IDX
) |
173 (1u << VIRTIO_BLK_F_SCSI
));
174 qvirtio_set_features(dev
, features
);
176 qvirtio_set_driver_ok(dev
);
178 /* Write and read with 3 descriptor layout */
180 req
.type
= VIRTIO_BLK_T_OUT
;
183 req
.data
= g_malloc0(512);
184 strcpy(req
.data
, "TEST");
186 req_addr
= virtio_blk_request(alloc
, dev
, &req
, 512);
190 free_head
= qvirtqueue_add(vq
, req_addr
, 16, false, true);
191 qvirtqueue_add(vq
, req_addr
+ 16, 512, false, true);
192 qvirtqueue_add(vq
, req_addr
+ 528, 1, true, false);
194 qvirtqueue_kick(dev
, vq
, free_head
);
196 qvirtio_wait_used_elem(dev
, vq
, free_head
, QVIRTIO_BLK_TIMEOUT_US
);
197 status
= readb(req_addr
+ 528);
198 g_assert_cmpint(status
, ==, 0);
200 guest_free(alloc
, req_addr
);
203 req
.type
= VIRTIO_BLK_T_IN
;
206 req
.data
= g_malloc0(512);
208 req_addr
= virtio_blk_request(alloc
, dev
, &req
, 512);
212 free_head
= qvirtqueue_add(vq
, req_addr
, 16, false, true);
213 qvirtqueue_add(vq
, req_addr
+ 16, 512, true, true);
214 qvirtqueue_add(vq
, req_addr
+ 528, 1, true, false);
216 qvirtqueue_kick(dev
, vq
, free_head
);
218 qvirtio_wait_used_elem(dev
, vq
, free_head
, QVIRTIO_BLK_TIMEOUT_US
);
219 status
= readb(req_addr
+ 528);
220 g_assert_cmpint(status
, ==, 0);
222 data
= g_malloc0(512);
223 memread(req_addr
+ 16, data
, 512);
224 g_assert_cmpstr(data
, ==, "TEST");
227 guest_free(alloc
, req_addr
);
229 if (features
& (1u << VIRTIO_F_ANY_LAYOUT
)) {
230 /* Write and read with 2 descriptor layout */
232 req
.type
= VIRTIO_BLK_T_OUT
;
235 req
.data
= g_malloc0(512);
236 strcpy(req
.data
, "TEST");
238 req_addr
= virtio_blk_request(alloc
, dev
, &req
, 512);
242 free_head
= qvirtqueue_add(vq
, req_addr
, 528, false, true);
243 qvirtqueue_add(vq
, req_addr
+ 528, 1, true, false);
244 qvirtqueue_kick(dev
, vq
, free_head
);
246 qvirtio_wait_used_elem(dev
, vq
, free_head
, QVIRTIO_BLK_TIMEOUT_US
);
247 status
= readb(req_addr
+ 528);
248 g_assert_cmpint(status
, ==, 0);
250 guest_free(alloc
, req_addr
);
253 req
.type
= VIRTIO_BLK_T_IN
;
256 req
.data
= g_malloc0(512);
258 req_addr
= virtio_blk_request(alloc
, dev
, &req
, 512);
262 free_head
= qvirtqueue_add(vq
, req_addr
, 16, false, true);
263 qvirtqueue_add(vq
, req_addr
+ 16, 513, true, false);
265 qvirtqueue_kick(dev
, vq
, free_head
);
267 qvirtio_wait_used_elem(dev
, vq
, free_head
, QVIRTIO_BLK_TIMEOUT_US
);
268 status
= readb(req_addr
+ 528);
269 g_assert_cmpint(status
, ==, 0);
271 data
= g_malloc0(512);
272 memread(req_addr
+ 16, data
, 512);
273 g_assert_cmpstr(data
, ==, "TEST");
276 guest_free(alloc
, req_addr
);
280 static void pci_basic(void)
282 QVirtioPCIDevice
*dev
;
284 QVirtQueuePCI
*vqpci
;
286 qs
= pci_test_start();
287 dev
= virtio_blk_pci_init(qs
->pcibus
, PCI_SLOT
);
289 vqpci
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 0);
291 test_basic(&dev
->vdev
, qs
->alloc
, &vqpci
->vq
);
294 qvirtqueue_cleanup(dev
->vdev
.bus
, &vqpci
->vq
, qs
->alloc
);
295 qvirtio_pci_device_disable(dev
);
296 qvirtio_pci_device_free(dev
);
300 static void pci_indirect(void)
302 QVirtioPCIDevice
*dev
;
303 QVirtQueuePCI
*vqpci
;
306 QVRingIndirectDesc
*indirect
;
314 qs
= pci_test_start();
316 dev
= virtio_blk_pci_init(qs
->pcibus
, PCI_SLOT
);
318 capacity
= qvirtio_config_readq(&dev
->vdev
, 0);
319 g_assert_cmpint(capacity
, ==, TEST_IMAGE_SIZE
/ 512);
321 features
= qvirtio_get_features(&dev
->vdev
);
322 g_assert_cmphex(features
& (1u << VIRTIO_RING_F_INDIRECT_DESC
), !=, 0);
323 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
324 (1u << VIRTIO_RING_F_EVENT_IDX
) |
325 (1u << VIRTIO_BLK_F_SCSI
));
326 qvirtio_set_features(&dev
->vdev
, features
);
328 vqpci
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 0);
329 qvirtio_set_driver_ok(&dev
->vdev
);
332 req
.type
= VIRTIO_BLK_T_OUT
;
335 req
.data
= g_malloc0(512);
336 strcpy(req
.data
, "TEST");
338 req_addr
= virtio_blk_request(qs
->alloc
, &dev
->vdev
, &req
, 512);
342 indirect
= qvring_indirect_desc_setup(&dev
->vdev
, qs
->alloc
, 2);
343 qvring_indirect_desc_add(indirect
, req_addr
, 528, false);
344 qvring_indirect_desc_add(indirect
, req_addr
+ 528, 1, true);
345 free_head
= qvirtqueue_add_indirect(&vqpci
->vq
, indirect
);
346 qvirtqueue_kick(&dev
->vdev
, &vqpci
->vq
, free_head
);
348 qvirtio_wait_used_elem(&dev
->vdev
, &vqpci
->vq
, free_head
,
349 QVIRTIO_BLK_TIMEOUT_US
);
350 status
= readb(req_addr
+ 528);
351 g_assert_cmpint(status
, ==, 0);
354 guest_free(qs
->alloc
, req_addr
);
357 req
.type
= VIRTIO_BLK_T_IN
;
360 req
.data
= g_malloc0(512);
361 strcpy(req
.data
, "TEST");
363 req_addr
= virtio_blk_request(qs
->alloc
, &dev
->vdev
, &req
, 512);
367 indirect
= qvring_indirect_desc_setup(&dev
->vdev
, qs
->alloc
, 2);
368 qvring_indirect_desc_add(indirect
, req_addr
, 16, false);
369 qvring_indirect_desc_add(indirect
, req_addr
+ 16, 513, true);
370 free_head
= qvirtqueue_add_indirect(&vqpci
->vq
, indirect
);
371 qvirtqueue_kick(&dev
->vdev
, &vqpci
->vq
, free_head
);
373 qvirtio_wait_used_elem(&dev
->vdev
, &vqpci
->vq
, free_head
,
374 QVIRTIO_BLK_TIMEOUT_US
);
375 status
= readb(req_addr
+ 528);
376 g_assert_cmpint(status
, ==, 0);
378 data
= g_malloc0(512);
379 memread(req_addr
+ 16, data
, 512);
380 g_assert_cmpstr(data
, ==, "TEST");
384 guest_free(qs
->alloc
, req_addr
);
387 qvirtqueue_cleanup(dev
->vdev
.bus
, &vqpci
->vq
, qs
->alloc
);
388 qvirtio_pci_device_disable(dev
);
389 qvirtio_pci_device_free(dev
);
393 static void pci_config(void)
395 QVirtioPCIDevice
*dev
;
397 int n_size
= TEST_IMAGE_SIZE
/ 2;
400 qs
= pci_test_start();
402 dev
= virtio_blk_pci_init(qs
->pcibus
, PCI_SLOT
);
404 capacity
= qvirtio_config_readq(&dev
->vdev
, 0);
405 g_assert_cmpint(capacity
, ==, TEST_IMAGE_SIZE
/ 512);
407 qvirtio_set_driver_ok(&dev
->vdev
);
409 qmp_discard_response("{ 'execute': 'block_resize', "
410 " 'arguments': { 'device': 'drive0', "
411 " 'size': %d } }", n_size
);
412 qvirtio_wait_config_isr(&dev
->vdev
, QVIRTIO_BLK_TIMEOUT_US
);
414 capacity
= qvirtio_config_readq(&dev
->vdev
, 0);
415 g_assert_cmpint(capacity
, ==, n_size
/ 512);
417 qvirtio_pci_device_disable(dev
);
418 qvirtio_pci_device_free(dev
);
423 static void pci_msix(void)
425 QVirtioPCIDevice
*dev
;
427 QVirtQueuePCI
*vqpci
;
429 int n_size
= TEST_IMAGE_SIZE
/ 2;
437 qs
= pci_test_start();
439 dev
= virtio_blk_pci_init(qs
->pcibus
, PCI_SLOT
);
440 qpci_msix_enable(dev
->pdev
);
442 qvirtio_pci_set_msix_configuration_vector(dev
, qs
->alloc
, 0);
444 capacity
= qvirtio_config_readq(&dev
->vdev
, 0);
445 g_assert_cmpint(capacity
, ==, TEST_IMAGE_SIZE
/ 512);
447 features
= qvirtio_get_features(&dev
->vdev
);
448 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
449 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
450 (1u << VIRTIO_RING_F_EVENT_IDX
) |
451 (1u << VIRTIO_BLK_F_SCSI
));
452 qvirtio_set_features(&dev
->vdev
, features
);
454 vqpci
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 0);
455 qvirtqueue_pci_msix_setup(dev
, vqpci
, qs
->alloc
, 1);
457 qvirtio_set_driver_ok(&dev
->vdev
);
459 qmp_discard_response("{ 'execute': 'block_resize', "
460 " 'arguments': { 'device': 'drive0', "
461 " 'size': %d } }", n_size
);
463 qvirtio_wait_config_isr(&dev
->vdev
, QVIRTIO_BLK_TIMEOUT_US
);
465 capacity
= qvirtio_config_readq(&dev
->vdev
, 0);
466 g_assert_cmpint(capacity
, ==, n_size
/ 512);
469 req
.type
= VIRTIO_BLK_T_OUT
;
472 req
.data
= g_malloc0(512);
473 strcpy(req
.data
, "TEST");
475 req_addr
= virtio_blk_request(qs
->alloc
, &dev
->vdev
, &req
, 512);
479 free_head
= qvirtqueue_add(&vqpci
->vq
, req_addr
, 16, false, true);
480 qvirtqueue_add(&vqpci
->vq
, req_addr
+ 16, 512, false, true);
481 qvirtqueue_add(&vqpci
->vq
, req_addr
+ 528, 1, true, false);
482 qvirtqueue_kick(&dev
->vdev
, &vqpci
->vq
, free_head
);
484 qvirtio_wait_used_elem(&dev
->vdev
, &vqpci
->vq
, free_head
,
485 QVIRTIO_BLK_TIMEOUT_US
);
487 status
= readb(req_addr
+ 528);
488 g_assert_cmpint(status
, ==, 0);
490 guest_free(qs
->alloc
, req_addr
);
493 req
.type
= VIRTIO_BLK_T_IN
;
496 req
.data
= g_malloc0(512);
498 req_addr
= virtio_blk_request(qs
->alloc
, &dev
->vdev
, &req
, 512);
502 free_head
= qvirtqueue_add(&vqpci
->vq
, req_addr
, 16, false, true);
503 qvirtqueue_add(&vqpci
->vq
, req_addr
+ 16, 512, true, true);
504 qvirtqueue_add(&vqpci
->vq
, req_addr
+ 528, 1, true, false);
506 qvirtqueue_kick(&dev
->vdev
, &vqpci
->vq
, free_head
);
509 qvirtio_wait_used_elem(&dev
->vdev
, &vqpci
->vq
, free_head
,
510 QVIRTIO_BLK_TIMEOUT_US
);
512 status
= readb(req_addr
+ 528);
513 g_assert_cmpint(status
, ==, 0);
515 data
= g_malloc0(512);
516 memread(req_addr
+ 16, data
, 512);
517 g_assert_cmpstr(data
, ==, "TEST");
520 guest_free(qs
->alloc
, req_addr
);
523 qvirtqueue_cleanup(dev
->vdev
.bus
, &vqpci
->vq
, qs
->alloc
);
524 qpci_msix_disable(dev
->pdev
);
525 qvirtio_pci_device_disable(dev
);
526 qvirtio_pci_device_free(dev
);
530 static void pci_idx(void)
532 QVirtioPCIDevice
*dev
;
534 QVirtQueuePCI
*vqpci
;
545 qs
= pci_test_start();
547 dev
= virtio_blk_pci_init(qs
->pcibus
, PCI_SLOT
);
548 qpci_msix_enable(dev
->pdev
);
550 qvirtio_pci_set_msix_configuration_vector(dev
, qs
->alloc
, 0);
552 capacity
= qvirtio_config_readq(&dev
->vdev
, 0);
553 g_assert_cmpint(capacity
, ==, TEST_IMAGE_SIZE
/ 512);
555 features
= qvirtio_get_features(&dev
->vdev
);
556 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
557 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
558 (1u << VIRTIO_F_NOTIFY_ON_EMPTY
) |
559 (1u << VIRTIO_BLK_F_SCSI
));
560 qvirtio_set_features(&dev
->vdev
, features
);
562 vqpci
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 0);
563 qvirtqueue_pci_msix_setup(dev
, vqpci
, qs
->alloc
, 1);
565 qvirtio_set_driver_ok(&dev
->vdev
);
568 req
.type
= VIRTIO_BLK_T_OUT
;
571 req
.data
= g_malloc0(512);
572 strcpy(req
.data
, "TEST");
574 req_addr
= virtio_blk_request(qs
->alloc
, &dev
->vdev
, &req
, 512);
578 free_head
= qvirtqueue_add(&vqpci
->vq
, req_addr
, 16, false, true);
579 qvirtqueue_add(&vqpci
->vq
, req_addr
+ 16, 512, false, true);
580 qvirtqueue_add(&vqpci
->vq
, req_addr
+ 528, 1, true, false);
581 qvirtqueue_kick(&dev
->vdev
, &vqpci
->vq
, free_head
);
583 qvirtio_wait_used_elem(&dev
->vdev
, &vqpci
->vq
, free_head
,
584 QVIRTIO_BLK_TIMEOUT_US
);
587 req
.type
= VIRTIO_BLK_T_OUT
;
590 req
.data
= g_malloc0(512);
591 strcpy(req
.data
, "TEST");
593 req_addr
= virtio_blk_request(qs
->alloc
, &dev
->vdev
, &req
, 512);
597 /* Notify after processing the third request */
598 qvirtqueue_set_used_event(&vqpci
->vq
, 2);
599 free_head
= qvirtqueue_add(&vqpci
->vq
, req_addr
, 16, false, true);
600 qvirtqueue_add(&vqpci
->vq
, req_addr
+ 16, 512, false, true);
601 qvirtqueue_add(&vqpci
->vq
, req_addr
+ 528, 1, true, false);
602 qvirtqueue_kick(&dev
->vdev
, &vqpci
->vq
, free_head
);
603 write_head
= free_head
;
605 /* No notification expected */
606 status
= qvirtio_wait_status_byte_no_isr(&dev
->vdev
,
607 &vqpci
->vq
, req_addr
+ 528,
608 QVIRTIO_BLK_TIMEOUT_US
);
609 g_assert_cmpint(status
, ==, 0);
611 guest_free(qs
->alloc
, req_addr
);
614 req
.type
= VIRTIO_BLK_T_IN
;
617 req
.data
= g_malloc0(512);
619 req_addr
= virtio_blk_request(qs
->alloc
, &dev
->vdev
, &req
, 512);
623 free_head
= qvirtqueue_add(&vqpci
->vq
, req_addr
, 16, false, true);
624 qvirtqueue_add(&vqpci
->vq
, req_addr
+ 16, 512, true, true);
625 qvirtqueue_add(&vqpci
->vq
, req_addr
+ 528, 1, true, false);
627 qvirtqueue_kick(&dev
->vdev
, &vqpci
->vq
, free_head
);
629 /* We get just one notification for both requests */
630 qvirtio_wait_used_elem(&dev
->vdev
, &vqpci
->vq
, write_head
,
631 QVIRTIO_BLK_TIMEOUT_US
);
632 g_assert(qvirtqueue_get_buf(&vqpci
->vq
, &desc_idx
));
633 g_assert_cmpint(desc_idx
, ==, free_head
);
635 status
= readb(req_addr
+ 528);
636 g_assert_cmpint(status
, ==, 0);
638 data
= g_malloc0(512);
639 memread(req_addr
+ 16, data
, 512);
640 g_assert_cmpstr(data
, ==, "TEST");
643 guest_free(qs
->alloc
, req_addr
);
646 qvirtqueue_cleanup(dev
->vdev
.bus
, &vqpci
->vq
, qs
->alloc
);
647 qpci_msix_disable(dev
->pdev
);
648 qvirtio_pci_device_disable(dev
);
649 qvirtio_pci_device_free(dev
);
653 static void pci_hotplug(void)
655 QVirtioPCIDevice
*dev
;
657 const char *arch
= qtest_get_arch();
659 qs
= pci_test_start();
661 /* plug secondary disk */
662 qpci_plug_device_test("virtio-blk-pci", "drv1", PCI_SLOT_HP
,
663 "'drive': 'drive1'");
665 dev
= virtio_blk_pci_init(qs
->pcibus
, PCI_SLOT_HP
);
667 qvirtio_pci_device_disable(dev
);
668 qvirtio_pci_device_free(dev
);
670 /* unplug secondary disk */
671 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
672 qpci_unplug_acpi_device_test("drv1", PCI_SLOT_HP
);
678 * Check that setting the vring addr on a non-existent virtqueue does
681 static void test_nonexistent_virtqueue(void)
687 qs
= pci_test_start();
688 dev
= qpci_device_find(qs
->pcibus
, QPCI_DEVFN(4, 0));
689 g_assert(dev
!= NULL
);
691 qpci_device_enable(dev
);
692 bar0
= qpci_iomap(dev
, 0, NULL
);
694 qpci_io_writeb(dev
, bar0
, VIRTIO_PCI_QUEUE_SEL
, 2);
695 qpci_io_writel(dev
, bar0
, VIRTIO_PCI_QUEUE_PFN
, 1);
701 static void mmio_basic(void)
703 QVirtioMMIODevice
*dev
;
705 QGuestAllocator
*alloc
;
706 int n_size
= TEST_IMAGE_SIZE
/ 2;
711 dev
= qvirtio_mmio_init_device(MMIO_DEV_BASE_ADDR
, MMIO_PAGE_SIZE
);
712 g_assert(dev
!= NULL
);
713 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_BLOCK
);
715 qvirtio_reset(&dev
->vdev
);
716 qvirtio_set_acknowledge(&dev
->vdev
);
717 qvirtio_set_driver(&dev
->vdev
);
719 alloc
= generic_alloc_init(MMIO_RAM_ADDR
, MMIO_RAM_SIZE
, MMIO_PAGE_SIZE
);
720 vq
= qvirtqueue_setup(&dev
->vdev
, alloc
, 0);
722 test_basic(&dev
->vdev
, alloc
, vq
);
724 qmp_discard_response("{ 'execute': 'block_resize', "
725 " 'arguments': { 'device': 'drive0', "
726 " 'size': %d } }", n_size
);
728 qvirtio_wait_queue_isr(&dev
->vdev
, vq
, QVIRTIO_BLK_TIMEOUT_US
);
730 capacity
= qvirtio_config_readq(&dev
->vdev
, 0);
731 g_assert_cmpint(capacity
, ==, n_size
/ 512);
734 qvirtqueue_cleanup(dev
->vdev
.bus
, vq
, alloc
);
736 generic_alloc_uninit(alloc
);
740 int main(int argc
, char **argv
)
742 const char *arch
= qtest_get_arch();
744 g_test_init(&argc
, &argv
, NULL
);
746 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0 ||
747 strcmp(arch
, "ppc64") == 0) {
748 qtest_add_func("/virtio/blk/pci/basic", pci_basic
);
749 qtest_add_func("/virtio/blk/pci/indirect", pci_indirect
);
750 qtest_add_func("/virtio/blk/pci/config", pci_config
);
751 qtest_add_func("/virtio/blk/pci/nxvirtq", test_nonexistent_virtqueue
);
752 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
753 qtest_add_func("/virtio/blk/pci/msix", pci_msix
);
754 qtest_add_func("/virtio/blk/pci/idx", pci_idx
);
756 qtest_add_func("/virtio/blk/pci/hotplug", pci_hotplug
);
757 } else if (strcmp(arch
, "arm") == 0) {
758 qtest_add_func("/virtio/blk/mmio/basic", mmio_basic
);