ati-vga: Add registers for getting apertures
[qemu/ar7.git] / tests / virtio-blk-test.c
blob982ff1538c0ae63abc5233d57469ae4d1930cc73
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 "qemu/bswap.h"
14 #include "qemu/module.h"
15 #include "standard-headers/linux/virtio_blk.h"
16 #include "standard-headers/linux/virtio_pci.h"
17 #include "libqos/qgraph.h"
18 #include "libqos/virtio-blk.h"
20 /* TODO actually test the results and get rid of this */
21 #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__))
23 #define TEST_IMAGE_SIZE (64 * 1024 * 1024)
24 #define QVIRTIO_BLK_TIMEOUT_US (30 * 1000 * 1000)
25 #define PCI_SLOT_HP 0x06
27 typedef struct QVirtioBlkReq {
28 uint32_t type;
29 uint32_t ioprio;
30 uint64_t sector;
31 char *data;
32 uint8_t status;
33 } QVirtioBlkReq;
36 #ifdef HOST_WORDS_BIGENDIAN
37 const bool host_is_big_endian = true;
38 #else
39 const bool host_is_big_endian; /* false */
40 #endif
42 static void drive_destroy(void *path)
44 unlink(path);
45 g_free(path);
46 qos_invalidate_command_line();
49 static char *drive_create(void)
51 int fd, ret;
52 char *t_path = g_strdup("/tmp/qtest.XXXXXX");
54 /* Create a temporary raw image */
55 fd = mkstemp(t_path);
56 g_assert_cmpint(fd, >=, 0);
57 ret = ftruncate(fd, TEST_IMAGE_SIZE);
58 g_assert_cmpint(ret, ==, 0);
59 close(fd);
61 g_test_queue_destroy(drive_destroy, t_path);
62 return t_path;
65 static inline void virtio_blk_fix_request(QVirtioDevice *d, QVirtioBlkReq *req)
67 if (qvirtio_is_big_endian(d) != host_is_big_endian) {
68 req->type = bswap32(req->type);
69 req->ioprio = bswap32(req->ioprio);
70 req->sector = bswap64(req->sector);
75 static inline void virtio_blk_fix_dwz_hdr(QVirtioDevice *d,
76 struct virtio_blk_discard_write_zeroes *dwz_hdr)
78 if (qvirtio_is_big_endian(d) != host_is_big_endian) {
79 dwz_hdr->sector = bswap64(dwz_hdr->sector);
80 dwz_hdr->num_sectors = bswap32(dwz_hdr->num_sectors);
81 dwz_hdr->flags = bswap32(dwz_hdr->flags);
85 static uint64_t virtio_blk_request(QGuestAllocator *alloc, QVirtioDevice *d,
86 QVirtioBlkReq *req, uint64_t data_size)
88 uint64_t addr;
89 uint8_t status = 0xFF;
91 switch (req->type) {
92 case VIRTIO_BLK_T_IN:
93 case VIRTIO_BLK_T_OUT:
94 g_assert_cmpuint(data_size % 512, ==, 0);
95 break;
96 case VIRTIO_BLK_T_DISCARD:
97 case VIRTIO_BLK_T_WRITE_ZEROES:
98 g_assert_cmpuint(data_size %
99 sizeof(struct virtio_blk_discard_write_zeroes), ==, 0);
100 break;
101 default:
102 g_assert_cmpuint(data_size, ==, 0);
105 addr = guest_alloc(alloc, sizeof(*req) + data_size);
107 virtio_blk_fix_request(d, req);
109 memwrite(addr, req, 16);
110 memwrite(addr + 16, req->data, data_size);
111 memwrite(addr + 16 + data_size, &status, sizeof(status));
113 return addr;
116 static void test_basic(QVirtioDevice *dev, QGuestAllocator *alloc,
117 QVirtQueue *vq)
119 QVirtioBlkReq req;
120 uint64_t req_addr;
121 uint64_t capacity;
122 uint32_t features;
123 uint32_t free_head;
124 uint8_t status;
125 char *data;
126 QTestState *qts = global_qtest;
128 capacity = qvirtio_config_readq(dev, 0);
130 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
132 features = qvirtio_get_features(dev);
133 features = features & ~(QVIRTIO_F_BAD_FEATURE |
134 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
135 (1u << VIRTIO_RING_F_EVENT_IDX) |
136 (1u << VIRTIO_BLK_F_SCSI));
137 qvirtio_set_features(dev, features);
139 qvirtio_set_driver_ok(dev);
141 /* Write and read with 3 descriptor layout */
142 /* Write request */
143 req.type = VIRTIO_BLK_T_OUT;
144 req.ioprio = 1;
145 req.sector = 0;
146 req.data = g_malloc0(512);
147 strcpy(req.data, "TEST");
149 req_addr = virtio_blk_request(alloc, dev, &req, 512);
151 g_free(req.data);
153 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
154 qvirtqueue_add(qts, vq, req_addr + 16, 512, false, true);
155 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
157 qvirtqueue_kick(qts, dev, vq, free_head);
159 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
160 QVIRTIO_BLK_TIMEOUT_US);
161 status = readb(req_addr + 528);
162 g_assert_cmpint(status, ==, 0);
164 guest_free(alloc, req_addr);
166 /* Read request */
167 req.type = VIRTIO_BLK_T_IN;
168 req.ioprio = 1;
169 req.sector = 0;
170 req.data = g_malloc0(512);
172 req_addr = virtio_blk_request(alloc, dev, &req, 512);
174 g_free(req.data);
176 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
177 qvirtqueue_add(qts, vq, req_addr + 16, 512, true, true);
178 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
180 qvirtqueue_kick(qts, dev, vq, free_head);
182 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
183 QVIRTIO_BLK_TIMEOUT_US);
184 status = readb(req_addr + 528);
185 g_assert_cmpint(status, ==, 0);
187 data = g_malloc0(512);
188 memread(req_addr + 16, data, 512);
189 g_assert_cmpstr(data, ==, "TEST");
190 g_free(data);
192 guest_free(alloc, req_addr);
194 if (features & (1u << VIRTIO_BLK_F_WRITE_ZEROES)) {
195 struct virtio_blk_discard_write_zeroes dwz_hdr;
196 void *expected;
199 * WRITE_ZEROES request on the same sector of previous test where
200 * we wrote "TEST".
202 req.type = VIRTIO_BLK_T_WRITE_ZEROES;
203 req.data = (char *) &dwz_hdr;
204 dwz_hdr.sector = 0;
205 dwz_hdr.num_sectors = 1;
206 dwz_hdr.flags = 0;
208 virtio_blk_fix_dwz_hdr(dev, &dwz_hdr);
210 req_addr = virtio_blk_request(alloc, dev, &req, sizeof(dwz_hdr));
212 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
213 qvirtqueue_add(qts, vq, req_addr + 16, sizeof(dwz_hdr), false, true);
214 qvirtqueue_add(qts, vq, req_addr + 16 + sizeof(dwz_hdr), 1, true,
215 false);
217 qvirtqueue_kick(qts, dev, vq, free_head);
219 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
220 QVIRTIO_BLK_TIMEOUT_US);
221 status = readb(req_addr + 16 + sizeof(dwz_hdr));
222 g_assert_cmpint(status, ==, 0);
224 guest_free(alloc, req_addr);
226 /* Read request to check if the sector contains all zeroes */
227 req.type = VIRTIO_BLK_T_IN;
228 req.ioprio = 1;
229 req.sector = 0;
230 req.data = g_malloc0(512);
232 req_addr = virtio_blk_request(alloc, dev, &req, 512);
234 g_free(req.data);
236 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
237 qvirtqueue_add(qts, vq, req_addr + 16, 512, true, true);
238 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
240 qvirtqueue_kick(qts, dev, vq, free_head);
242 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
243 QVIRTIO_BLK_TIMEOUT_US);
244 status = readb(req_addr + 528);
245 g_assert_cmpint(status, ==, 0);
247 data = g_malloc(512);
248 expected = g_malloc0(512);
249 memread(req_addr + 16, data, 512);
250 g_assert_cmpmem(data, 512, expected, 512);
251 g_free(expected);
252 g_free(data);
254 guest_free(alloc, req_addr);
257 if (features & (1u << VIRTIO_BLK_F_DISCARD)) {
258 struct virtio_blk_discard_write_zeroes dwz_hdr;
260 req.type = VIRTIO_BLK_T_DISCARD;
261 req.data = (char *) &dwz_hdr;
262 dwz_hdr.sector = 0;
263 dwz_hdr.num_sectors = 1;
264 dwz_hdr.flags = 0;
266 virtio_blk_fix_dwz_hdr(dev, &dwz_hdr);
268 req_addr = virtio_blk_request(alloc, dev, &req, sizeof(dwz_hdr));
270 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
271 qvirtqueue_add(qts, vq, req_addr + 16, sizeof(dwz_hdr), false, true);
272 qvirtqueue_add(qts, vq, req_addr + 16 + sizeof(dwz_hdr), 1, true, false);
274 qvirtqueue_kick(qts, dev, vq, free_head);
276 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
277 QVIRTIO_BLK_TIMEOUT_US);
278 status = readb(req_addr + 16 + sizeof(dwz_hdr));
279 g_assert_cmpint(status, ==, 0);
281 guest_free(alloc, req_addr);
284 if (features & (1u << VIRTIO_F_ANY_LAYOUT)) {
285 /* Write and read with 2 descriptor layout */
286 /* Write request */
287 req.type = VIRTIO_BLK_T_OUT;
288 req.ioprio = 1;
289 req.sector = 1;
290 req.data = g_malloc0(512);
291 strcpy(req.data, "TEST");
293 req_addr = virtio_blk_request(alloc, dev, &req, 512);
295 g_free(req.data);
297 free_head = qvirtqueue_add(qts, vq, req_addr, 528, false, true);
298 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
299 qvirtqueue_kick(qts, dev, vq, free_head);
301 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
302 QVIRTIO_BLK_TIMEOUT_US);
303 status = readb(req_addr + 528);
304 g_assert_cmpint(status, ==, 0);
306 guest_free(alloc, req_addr);
308 /* Read request */
309 req.type = VIRTIO_BLK_T_IN;
310 req.ioprio = 1;
311 req.sector = 1;
312 req.data = g_malloc0(512);
314 req_addr = virtio_blk_request(alloc, dev, &req, 512);
316 g_free(req.data);
318 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
319 qvirtqueue_add(qts, vq, req_addr + 16, 513, true, false);
321 qvirtqueue_kick(qts, dev, vq, free_head);
323 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
324 QVIRTIO_BLK_TIMEOUT_US);
325 status = readb(req_addr + 528);
326 g_assert_cmpint(status, ==, 0);
328 data = g_malloc0(512);
329 memread(req_addr + 16, data, 512);
330 g_assert_cmpstr(data, ==, "TEST");
331 g_free(data);
333 guest_free(alloc, req_addr);
337 static void basic(void *obj, void *data, QGuestAllocator *t_alloc)
339 QVirtioBlk *blk_if = obj;
340 QVirtQueue *vq;
341 vq = qvirtqueue_setup(blk_if->vdev, t_alloc, 0);
342 test_basic(blk_if->vdev, t_alloc, vq);
343 qvirtqueue_cleanup(blk_if->vdev->bus, vq, t_alloc);
347 static void indirect(void *obj, void *u_data, QGuestAllocator *t_alloc)
349 QVirtQueue *vq;
350 QVirtioBlk *blk_if = obj;
351 QVirtioDevice *dev = blk_if->vdev;
352 QVirtioBlkReq req;
353 QVRingIndirectDesc *indirect;
354 uint64_t req_addr;
355 uint64_t capacity;
356 uint32_t features;
357 uint32_t free_head;
358 uint8_t status;
359 char *data;
360 QTestState *qts = global_qtest;
362 capacity = qvirtio_config_readq(dev, 0);
363 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
365 features = qvirtio_get_features(dev);
366 g_assert_cmphex(features & (1u << VIRTIO_RING_F_INDIRECT_DESC), !=, 0);
367 features = features & ~(QVIRTIO_F_BAD_FEATURE |
368 (1u << VIRTIO_RING_F_EVENT_IDX) |
369 (1u << VIRTIO_BLK_F_SCSI));
370 qvirtio_set_features(dev, features);
372 vq = qvirtqueue_setup(dev, t_alloc, 0);
373 qvirtio_set_driver_ok(dev);
375 /* Write request */
376 req.type = VIRTIO_BLK_T_OUT;
377 req.ioprio = 1;
378 req.sector = 0;
379 req.data = g_malloc0(512);
380 strcpy(req.data, "TEST");
382 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
384 g_free(req.data);
386 indirect = qvring_indirect_desc_setup(qts, dev, t_alloc, 2);
387 qvring_indirect_desc_add(qts, indirect, req_addr, 528, false);
388 qvring_indirect_desc_add(qts, indirect, req_addr + 528, 1, true);
389 free_head = qvirtqueue_add_indirect(qts, vq, indirect);
390 qvirtqueue_kick(qts, dev, vq, free_head);
392 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
393 QVIRTIO_BLK_TIMEOUT_US);
394 status = readb(req_addr + 528);
395 g_assert_cmpint(status, ==, 0);
397 g_free(indirect);
398 guest_free(t_alloc, req_addr);
400 /* Read request */
401 req.type = VIRTIO_BLK_T_IN;
402 req.ioprio = 1;
403 req.sector = 0;
404 req.data = g_malloc0(512);
405 strcpy(req.data, "TEST");
407 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
409 g_free(req.data);
411 indirect = qvring_indirect_desc_setup(qts, dev, t_alloc, 2);
412 qvring_indirect_desc_add(qts, indirect, req_addr, 16, false);
413 qvring_indirect_desc_add(qts, indirect, req_addr + 16, 513, true);
414 free_head = qvirtqueue_add_indirect(qts, vq, indirect);
415 qvirtqueue_kick(qts, dev, vq, free_head);
417 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
418 QVIRTIO_BLK_TIMEOUT_US);
419 status = readb(req_addr + 528);
420 g_assert_cmpint(status, ==, 0);
422 data = g_malloc0(512);
423 memread(req_addr + 16, data, 512);
424 g_assert_cmpstr(data, ==, "TEST");
425 g_free(data);
427 g_free(indirect);
428 guest_free(t_alloc, req_addr);
429 qvirtqueue_cleanup(dev->bus, vq, t_alloc);
432 static void config(void *obj, void *data, QGuestAllocator *t_alloc)
434 QVirtioBlk *blk_if = obj;
435 QVirtioDevice *dev = blk_if->vdev;
436 int n_size = TEST_IMAGE_SIZE / 2;
437 uint64_t capacity;
439 capacity = qvirtio_config_readq(dev, 0);
440 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
442 qvirtio_set_driver_ok(dev);
444 qmp_discard_response("{ 'execute': 'block_resize', "
445 " 'arguments': { 'device': 'drive0', "
446 " 'size': %d } }", n_size);
447 qvirtio_wait_config_isr(dev, QVIRTIO_BLK_TIMEOUT_US);
449 capacity = qvirtio_config_readq(dev, 0);
450 g_assert_cmpint(capacity, ==, n_size / 512);
453 static void msix(void *obj, void *u_data, QGuestAllocator *t_alloc)
455 QVirtQueue *vq;
456 QVirtioBlkPCI *blk = obj;
457 QVirtioPCIDevice *pdev = &blk->pci_vdev;
458 QVirtioDevice *dev = &pdev->vdev;
459 QVirtioBlkReq req;
460 int n_size = TEST_IMAGE_SIZE / 2;
461 uint64_t req_addr;
462 uint64_t capacity;
463 uint32_t features;
464 uint32_t free_head;
465 uint8_t status;
466 char *data;
467 QOSGraphObject *blk_object = obj;
468 QPCIDevice *pci_dev = blk_object->get_driver(blk_object, "pci-device");
469 QTestState *qts = global_qtest;
471 if (qpci_check_buggy_msi(pci_dev)) {
472 return;
475 qpci_msix_enable(pdev->pdev);
476 qvirtio_pci_set_msix_configuration_vector(pdev, t_alloc, 0);
478 capacity = qvirtio_config_readq(dev, 0);
479 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
481 features = qvirtio_get_features(dev);
482 features = features & ~(QVIRTIO_F_BAD_FEATURE |
483 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
484 (1u << VIRTIO_RING_F_EVENT_IDX) |
485 (1u << VIRTIO_BLK_F_SCSI));
486 qvirtio_set_features(dev, features);
488 vq = qvirtqueue_setup(dev, t_alloc, 0);
489 qvirtqueue_pci_msix_setup(pdev, (QVirtQueuePCI *)vq, t_alloc, 1);
491 qvirtio_set_driver_ok(dev);
493 qmp_discard_response("{ 'execute': 'block_resize', "
494 " 'arguments': { 'device': 'drive0', "
495 " 'size': %d } }", n_size);
497 qvirtio_wait_config_isr(dev, QVIRTIO_BLK_TIMEOUT_US);
499 capacity = qvirtio_config_readq(dev, 0);
500 g_assert_cmpint(capacity, ==, n_size / 512);
502 /* Write request */
503 req.type = VIRTIO_BLK_T_OUT;
504 req.ioprio = 1;
505 req.sector = 0;
506 req.data = g_malloc0(512);
507 strcpy(req.data, "TEST");
509 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
511 g_free(req.data);
513 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
514 qvirtqueue_add(qts, vq, req_addr + 16, 512, false, true);
515 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
516 qvirtqueue_kick(qts, dev, vq, free_head);
518 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
519 QVIRTIO_BLK_TIMEOUT_US);
521 status = readb(req_addr + 528);
522 g_assert_cmpint(status, ==, 0);
524 guest_free(t_alloc, req_addr);
526 /* Read request */
527 req.type = VIRTIO_BLK_T_IN;
528 req.ioprio = 1;
529 req.sector = 0;
530 req.data = g_malloc0(512);
532 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
534 g_free(req.data);
536 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
537 qvirtqueue_add(qts, vq, req_addr + 16, 512, true, true);
538 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
540 qvirtqueue_kick(qts, dev, vq, free_head);
543 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
544 QVIRTIO_BLK_TIMEOUT_US);
546 status = readb(req_addr + 528);
547 g_assert_cmpint(status, ==, 0);
549 data = g_malloc0(512);
550 memread(req_addr + 16, data, 512);
551 g_assert_cmpstr(data, ==, "TEST");
552 g_free(data);
554 guest_free(t_alloc, req_addr);
556 /* End test */
557 qpci_msix_disable(pdev->pdev);
558 qvirtqueue_cleanup(dev->bus, vq, t_alloc);
561 static void idx(void *obj, void *u_data, QGuestAllocator *t_alloc)
563 QVirtQueue *vq;
564 QVirtioBlkPCI *blk = obj;
565 QVirtioPCIDevice *pdev = &blk->pci_vdev;
566 QVirtioDevice *dev = &pdev->vdev;
567 QVirtioBlkReq req;
568 uint64_t req_addr;
569 uint64_t capacity;
570 uint32_t features;
571 uint32_t free_head;
572 uint32_t write_head;
573 uint32_t desc_idx;
574 uint8_t status;
575 char *data;
576 QOSGraphObject *blk_object = obj;
577 QPCIDevice *pci_dev = blk_object->get_driver(blk_object, "pci-device");
578 QTestState *qts = global_qtest;
580 if (qpci_check_buggy_msi(pci_dev)) {
581 return;
584 qpci_msix_enable(pdev->pdev);
585 qvirtio_pci_set_msix_configuration_vector(pdev, t_alloc, 0);
587 capacity = qvirtio_config_readq(dev, 0);
588 g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
590 features = qvirtio_get_features(dev);
591 features = features & ~(QVIRTIO_F_BAD_FEATURE |
592 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
593 (1u << VIRTIO_F_NOTIFY_ON_EMPTY) |
594 (1u << VIRTIO_BLK_F_SCSI));
595 qvirtio_set_features(dev, features);
597 vq = qvirtqueue_setup(dev, t_alloc, 0);
598 qvirtqueue_pci_msix_setup(pdev, (QVirtQueuePCI *)vq, t_alloc, 1);
600 qvirtio_set_driver_ok(dev);
602 /* Write request */
603 req.type = VIRTIO_BLK_T_OUT;
604 req.ioprio = 1;
605 req.sector = 0;
606 req.data = g_malloc0(512);
607 strcpy(req.data, "TEST");
609 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
611 g_free(req.data);
613 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
614 qvirtqueue_add(qts, vq, req_addr + 16, 512, false, true);
615 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
616 qvirtqueue_kick(qts, dev, vq, free_head);
618 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
619 QVIRTIO_BLK_TIMEOUT_US);
621 /* Write request */
622 req.type = VIRTIO_BLK_T_OUT;
623 req.ioprio = 1;
624 req.sector = 1;
625 req.data = g_malloc0(512);
626 strcpy(req.data, "TEST");
628 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
630 g_free(req.data);
632 /* Notify after processing the third request */
633 qvirtqueue_set_used_event(qts, vq, 2);
634 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
635 qvirtqueue_add(qts, vq, req_addr + 16, 512, false, true);
636 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
637 qvirtqueue_kick(qts, dev, vq, free_head);
638 write_head = free_head;
640 /* No notification expected */
641 status = qvirtio_wait_status_byte_no_isr(qts, dev,
642 vq, req_addr + 528,
643 QVIRTIO_BLK_TIMEOUT_US);
644 g_assert_cmpint(status, ==, 0);
646 guest_free(t_alloc, req_addr);
648 /* Read request */
649 req.type = VIRTIO_BLK_T_IN;
650 req.ioprio = 1;
651 req.sector = 1;
652 req.data = g_malloc0(512);
654 req_addr = virtio_blk_request(t_alloc, dev, &req, 512);
656 g_free(req.data);
658 free_head = qvirtqueue_add(qts, vq, req_addr, 16, false, true);
659 qvirtqueue_add(qts, vq, req_addr + 16, 512, true, true);
660 qvirtqueue_add(qts, vq, req_addr + 528, 1, true, false);
662 qvirtqueue_kick(qts, dev, vq, free_head);
664 /* We get just one notification for both requests */
665 qvirtio_wait_used_elem(qts, dev, vq, write_head, NULL,
666 QVIRTIO_BLK_TIMEOUT_US);
667 g_assert(qvirtqueue_get_buf(qts, vq, &desc_idx, NULL));
668 g_assert_cmpint(desc_idx, ==, free_head);
670 status = readb(req_addr + 528);
671 g_assert_cmpint(status, ==, 0);
673 data = g_malloc0(512);
674 memread(req_addr + 16, data, 512);
675 g_assert_cmpstr(data, ==, "TEST");
676 g_free(data);
678 guest_free(t_alloc, req_addr);
680 /* End test */
681 qpci_msix_disable(pdev->pdev);
683 qvirtqueue_cleanup(dev->bus, vq, t_alloc);
686 static void pci_hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
688 QVirtioPCIDevice *dev1 = obj;
689 QVirtioPCIDevice *dev;
690 QTestState *qts = dev1->pdev->bus->qts;
692 /* plug secondary disk */
693 qtest_qmp_device_add(qts, "virtio-blk-pci", "drv1",
694 "{'addr': %s, 'drive': 'drive1'}",
695 stringify(PCI_SLOT_HP) ".0");
697 dev = virtio_pci_new(dev1->pdev->bus,
698 &(QPCIAddress) { .devfn = QPCI_DEVFN(PCI_SLOT_HP, 0) });
699 g_assert_nonnull(dev);
700 g_assert_cmpint(dev->vdev.device_type, ==, VIRTIO_ID_BLOCK);
701 qvirtio_pci_device_disable(dev);
702 qos_object_destroy((QOSGraphObject *)dev);
704 /* unplug secondary disk */
705 qpci_unplug_acpi_device_test(qts, "drv1", PCI_SLOT_HP);
709 * Check that setting the vring addr on a non-existent virtqueue does
710 * not crash.
712 static void test_nonexistent_virtqueue(void *obj, void *data,
713 QGuestAllocator *t_alloc)
715 QVirtioBlkPCI *blk = obj;
716 QVirtioPCIDevice *pdev = &blk->pci_vdev;
717 QPCIBar bar0;
718 QPCIDevice *dev;
720 dev = qpci_device_find(pdev->pdev->bus, QPCI_DEVFN(4, 0));
721 g_assert(dev != NULL);
722 qpci_device_enable(dev);
724 bar0 = qpci_iomap(dev, 0, NULL);
726 qpci_io_writeb(dev, bar0, VIRTIO_PCI_QUEUE_SEL, 2);
727 qpci_io_writel(dev, bar0, VIRTIO_PCI_QUEUE_PFN, 1);
730 g_free(dev);
733 static void resize(void *obj, void *data, QGuestAllocator *t_alloc)
735 QVirtioBlk *blk_if = obj;
736 QVirtioDevice *dev = blk_if->vdev;
737 int n_size = TEST_IMAGE_SIZE / 2;
738 uint64_t capacity;
739 QVirtQueue *vq;
741 vq = qvirtqueue_setup(dev, t_alloc, 0);
743 test_basic(dev, t_alloc, vq);
745 qmp_discard_response("{ 'execute': 'block_resize', "
746 " 'arguments': { 'device': 'drive0', "
747 " 'size': %d } }", n_size);
749 qvirtio_wait_queue_isr(dev, vq, QVIRTIO_BLK_TIMEOUT_US);
751 capacity = qvirtio_config_readq(dev, 0);
752 g_assert_cmpint(capacity, ==, n_size / 512);
754 qvirtqueue_cleanup(dev->bus, vq, t_alloc);
758 static void *virtio_blk_test_setup(GString *cmd_line, void *arg)
760 char *tmp_path = drive_create();
762 g_string_append_printf(cmd_line,
763 " -drive if=none,id=drive0,file=%s,"
764 "format=raw,auto-read-only=off "
765 "-drive if=none,id=drive1,file=null-co://,"
766 "file.read-zeroes=on,format=raw ",
767 tmp_path);
769 return arg;
772 static void register_virtio_blk_test(void)
774 QOSGraphTestOptions opts = {
775 .before = virtio_blk_test_setup,
778 qos_add_test("indirect", "virtio-blk", indirect, &opts);
779 qos_add_test("config", "virtio-blk", config, &opts);
780 qos_add_test("basic", "virtio-blk", basic, &opts);
781 qos_add_test("resize", "virtio-blk", resize, &opts);
783 /* tests just for virtio-blk-pci */
784 qos_add_test("msix", "virtio-blk-pci", msix, &opts);
785 qos_add_test("idx", "virtio-blk-pci", idx, &opts);
786 qos_add_test("nxvirtq", "virtio-blk-pci",
787 test_nonexistent_virtqueue, &opts);
788 qos_add_test("hotplug", "virtio-blk-pci", pci_hotplug, &opts);
791 libqos_init(register_virtio_blk_test);