json: Update references to RFC 7159 to RFC 8259
[qemu/ar7.git] / tests / ide-test.c
blob33cef61e1f09b36999c9e62d0e738307c496e04e
1 /*
2 * IDE test cases
4 * Copyright (c) 2013 Kevin Wolf <kwolf@redhat.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
28 #include "libqtest.h"
29 #include "libqos/libqos.h"
30 #include "libqos/pci-pc.h"
31 #include "libqos/malloc-pc.h"
32 #include "qapi/qmp/qdict.h"
33 #include "qemu-common.h"
34 #include "qemu/bswap.h"
35 #include "hw/pci/pci_ids.h"
36 #include "hw/pci/pci_regs.h"
38 /* TODO actually test the results and get rid of this */
39 #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__))
41 #define TEST_IMAGE_SIZE 64 * 1024 * 1024
43 #define IDE_PCI_DEV 1
44 #define IDE_PCI_FUNC 1
46 #define IDE_BASE 0x1f0
47 #define IDE_PRIMARY_IRQ 14
49 #define ATAPI_BLOCK_SIZE 2048
51 /* How many bytes to receive via ATAPI PIO at one time.
52 * Must be less than 0xFFFF. */
53 #define BYTE_COUNT_LIMIT 5120
55 enum {
56 reg_data = 0x0,
57 reg_feature = 0x1,
58 reg_error = 0x1,
59 reg_nsectors = 0x2,
60 reg_lba_low = 0x3,
61 reg_lba_middle = 0x4,
62 reg_lba_high = 0x5,
63 reg_device = 0x6,
64 reg_status = 0x7,
65 reg_command = 0x7,
68 enum {
69 BSY = 0x80,
70 DRDY = 0x40,
71 DF = 0x20,
72 DRQ = 0x08,
73 ERR = 0x01,
76 /* Error field */
77 enum {
78 ABRT = 0x04,
81 enum {
82 DEV = 0x10,
83 LBA = 0x40,
86 enum {
87 bmreg_cmd = 0x0,
88 bmreg_status = 0x2,
89 bmreg_prdt = 0x4,
92 enum {
93 CMD_DSM = 0x06,
94 CMD_READ_DMA = 0xc8,
95 CMD_WRITE_DMA = 0xca,
96 CMD_FLUSH_CACHE = 0xe7,
97 CMD_IDENTIFY = 0xec,
98 CMD_PACKET = 0xa0,
100 CMDF_ABORT = 0x100,
101 CMDF_NO_BM = 0x200,
104 enum {
105 BM_CMD_START = 0x1,
106 BM_CMD_WRITE = 0x8, /* write = from device to memory */
109 enum {
110 BM_STS_ACTIVE = 0x1,
111 BM_STS_ERROR = 0x2,
112 BM_STS_INTR = 0x4,
115 enum {
116 PRDT_EOT = 0x80000000,
119 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
120 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
122 static QPCIBus *pcibus = NULL;
123 static QGuestAllocator *guest_malloc;
125 static char tmp_path[] = "/tmp/qtest.XXXXXX";
126 static char debug_path[] = "/tmp/qtest-blkdebug.XXXXXX";
128 static void ide_test_start(const char *cmdline_fmt, ...)
130 va_list ap;
131 char *cmdline;
133 va_start(ap, cmdline_fmt);
134 cmdline = g_strdup_vprintf(cmdline_fmt, ap);
135 va_end(ap);
137 qtest_start(cmdline);
138 guest_malloc = pc_alloc_init(global_qtest);
140 g_free(cmdline);
143 static void ide_test_quit(void)
145 pc_alloc_uninit(guest_malloc);
146 guest_malloc = NULL;
147 qtest_end();
150 static QPCIDevice *get_pci_device(QPCIBar *bmdma_bar, QPCIBar *ide_bar)
152 QPCIDevice *dev;
153 uint16_t vendor_id, device_id;
155 if (!pcibus) {
156 pcibus = qpci_init_pc(global_qtest, NULL);
159 /* Find PCI device and verify it's the right one */
160 dev = qpci_device_find(pcibus, QPCI_DEVFN(IDE_PCI_DEV, IDE_PCI_FUNC));
161 g_assert(dev != NULL);
163 vendor_id = qpci_config_readw(dev, PCI_VENDOR_ID);
164 device_id = qpci_config_readw(dev, PCI_DEVICE_ID);
165 g_assert(vendor_id == PCI_VENDOR_ID_INTEL);
166 g_assert(device_id == PCI_DEVICE_ID_INTEL_82371SB_1);
168 /* Map bmdma BAR */
169 *bmdma_bar = qpci_iomap(dev, 4, NULL);
171 *ide_bar = qpci_legacy_iomap(dev, IDE_BASE);
173 qpci_device_enable(dev);
175 return dev;
178 static void free_pci_device(QPCIDevice *dev)
180 /* libqos doesn't have a function for this, so free it manually */
181 g_free(dev);
184 typedef struct PrdtEntry {
185 uint32_t addr;
186 uint32_t size;
187 } QEMU_PACKED PrdtEntry;
189 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
190 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
192 static uint64_t trim_range_le(uint64_t sector, uint16_t count)
194 /* 2-byte range, 6-byte LBA */
195 return cpu_to_le64(((uint64_t)count << 48) + sector);
198 static int send_dma_request(int cmd, uint64_t sector, int nb_sectors,
199 PrdtEntry *prdt, int prdt_entries,
200 void(*post_exec)(QPCIDevice *dev, QPCIBar ide_bar,
201 uint64_t sector, int nb_sectors))
203 QPCIDevice *dev;
204 QPCIBar bmdma_bar, ide_bar;
205 uintptr_t guest_prdt;
206 size_t len;
207 bool from_dev;
208 uint8_t status;
209 int flags;
211 dev = get_pci_device(&bmdma_bar, &ide_bar);
213 flags = cmd & ~0xff;
214 cmd &= 0xff;
216 switch (cmd) {
217 case CMD_READ_DMA:
218 case CMD_PACKET:
219 /* Assuming we only test data reads w/ ATAPI, otherwise we need to know
220 * the SCSI command being sent in the packet, too. */
221 from_dev = true;
222 break;
223 case CMD_DSM:
224 case CMD_WRITE_DMA:
225 from_dev = false;
226 break;
227 default:
228 g_assert_not_reached();
231 if (flags & CMDF_NO_BM) {
232 qpci_config_writew(dev, PCI_COMMAND,
233 PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
236 /* Select device 0 */
237 qpci_io_writeb(dev, ide_bar, reg_device, 0 | LBA);
239 /* Stop any running transfer, clear any pending interrupt */
240 qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, 0);
241 qpci_io_writeb(dev, bmdma_bar, bmreg_status, BM_STS_INTR);
243 /* Setup PRDT */
244 len = sizeof(*prdt) * prdt_entries;
245 guest_prdt = guest_alloc(guest_malloc, len);
246 memwrite(guest_prdt, prdt, len);
247 qpci_io_writel(dev, bmdma_bar, bmreg_prdt, guest_prdt);
249 /* ATA DMA command */
250 if (cmd == CMD_PACKET) {
251 /* Enables ATAPI DMA; otherwise PIO is attempted */
252 qpci_io_writeb(dev, ide_bar, reg_feature, 0x01);
253 } else {
254 if (cmd == CMD_DSM) {
255 /* trim bit */
256 qpci_io_writeb(dev, ide_bar, reg_feature, 0x01);
258 qpci_io_writeb(dev, ide_bar, reg_nsectors, nb_sectors);
259 qpci_io_writeb(dev, ide_bar, reg_lba_low, sector & 0xff);
260 qpci_io_writeb(dev, ide_bar, reg_lba_middle, (sector >> 8) & 0xff);
261 qpci_io_writeb(dev, ide_bar, reg_lba_high, (sector >> 16) & 0xff);
264 qpci_io_writeb(dev, ide_bar, reg_command, cmd);
266 if (post_exec) {
267 post_exec(dev, ide_bar, sector, nb_sectors);
270 /* Start DMA transfer */
271 qpci_io_writeb(dev, bmdma_bar, bmreg_cmd,
272 BM_CMD_START | (from_dev ? BM_CMD_WRITE : 0));
274 if (flags & CMDF_ABORT) {
275 qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, 0);
278 /* Wait for the DMA transfer to complete */
279 do {
280 status = qpci_io_readb(dev, bmdma_bar, bmreg_status);
281 } while ((status & (BM_STS_ACTIVE | BM_STS_INTR)) == BM_STS_ACTIVE);
283 g_assert_cmpint(get_irq(IDE_PRIMARY_IRQ), ==, !!(status & BM_STS_INTR));
285 /* Check IDE status code */
286 assert_bit_set(qpci_io_readb(dev, ide_bar, reg_status), DRDY);
287 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), BSY | DRQ);
289 /* Reading the status register clears the IRQ */
290 g_assert(!get_irq(IDE_PRIMARY_IRQ));
292 /* Stop DMA transfer if still active */
293 if (status & BM_STS_ACTIVE) {
294 qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, 0);
297 free_pci_device(dev);
299 return status;
302 static void test_bmdma_simple_rw(void)
304 QPCIDevice *dev;
305 QPCIBar bmdma_bar, ide_bar;
306 uint8_t status;
307 uint8_t *buf;
308 uint8_t *cmpbuf;
309 size_t len = 512;
310 uintptr_t guest_buf = guest_alloc(guest_malloc, len);
312 PrdtEntry prdt[] = {
314 .addr = cpu_to_le32(guest_buf),
315 .size = cpu_to_le32(len | PRDT_EOT),
319 dev = get_pci_device(&bmdma_bar, &ide_bar);
321 buf = g_malloc(len);
322 cmpbuf = g_malloc(len);
324 /* Write 0x55 pattern to sector 0 */
325 memset(buf, 0x55, len);
326 memwrite(guest_buf, buf, len);
328 status = send_dma_request(CMD_WRITE_DMA, 0, 1, prdt,
329 ARRAY_SIZE(prdt), NULL);
330 g_assert_cmphex(status, ==, BM_STS_INTR);
331 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
333 /* Write 0xaa pattern to sector 1 */
334 memset(buf, 0xaa, len);
335 memwrite(guest_buf, buf, len);
337 status = send_dma_request(CMD_WRITE_DMA, 1, 1, prdt,
338 ARRAY_SIZE(prdt), NULL);
339 g_assert_cmphex(status, ==, BM_STS_INTR);
340 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
342 /* Read and verify 0x55 pattern in sector 0 */
343 memset(cmpbuf, 0x55, len);
345 status = send_dma_request(CMD_READ_DMA, 0, 1, prdt, ARRAY_SIZE(prdt), NULL);
346 g_assert_cmphex(status, ==, BM_STS_INTR);
347 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
349 memread(guest_buf, buf, len);
350 g_assert(memcmp(buf, cmpbuf, len) == 0);
352 /* Read and verify 0xaa pattern in sector 1 */
353 memset(cmpbuf, 0xaa, len);
355 status = send_dma_request(CMD_READ_DMA, 1, 1, prdt, ARRAY_SIZE(prdt), NULL);
356 g_assert_cmphex(status, ==, BM_STS_INTR);
357 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
359 memread(guest_buf, buf, len);
360 g_assert(memcmp(buf, cmpbuf, len) == 0);
363 free_pci_device(dev);
364 g_free(buf);
365 g_free(cmpbuf);
368 static void test_bmdma_trim(void)
370 QPCIDevice *dev;
371 QPCIBar bmdma_bar, ide_bar;
372 uint8_t status;
373 const uint64_t trim_range[] = { trim_range_le(0, 2),
374 trim_range_le(6, 8),
375 trim_range_le(10, 1),
377 const uint64_t bad_range = trim_range_le(TEST_IMAGE_SIZE / 512 - 1, 2);
378 size_t len = 512;
379 uint8_t *buf;
380 uintptr_t guest_buf = guest_alloc(guest_malloc, len);
382 PrdtEntry prdt[] = {
384 .addr = cpu_to_le32(guest_buf),
385 .size = cpu_to_le32(len | PRDT_EOT),
389 dev = get_pci_device(&bmdma_bar, &ide_bar);
391 buf = g_malloc(len);
393 /* Normal request */
394 *((uint64_t *)buf) = trim_range[0];
395 *((uint64_t *)buf + 1) = trim_range[1];
397 memwrite(guest_buf, buf, 2 * sizeof(uint64_t));
399 status = send_dma_request(CMD_DSM, 0, 1, prdt,
400 ARRAY_SIZE(prdt), NULL);
401 g_assert_cmphex(status, ==, BM_STS_INTR);
402 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
404 /* Request contains invalid range */
405 *((uint64_t *)buf) = trim_range[2];
406 *((uint64_t *)buf + 1) = bad_range;
408 memwrite(guest_buf, buf, 2 * sizeof(uint64_t));
410 status = send_dma_request(CMD_DSM, 0, 1, prdt,
411 ARRAY_SIZE(prdt), NULL);
412 g_assert_cmphex(status, ==, BM_STS_INTR);
413 assert_bit_set(qpci_io_readb(dev, ide_bar, reg_status), ERR);
414 assert_bit_set(qpci_io_readb(dev, ide_bar, reg_error), ABRT);
416 free_pci_device(dev);
417 g_free(buf);
420 static void test_bmdma_short_prdt(void)
422 QPCIDevice *dev;
423 QPCIBar bmdma_bar, ide_bar;
424 uint8_t status;
426 PrdtEntry prdt[] = {
428 .addr = 0,
429 .size = cpu_to_le32(0x10 | PRDT_EOT),
433 dev = get_pci_device(&bmdma_bar, &ide_bar);
435 /* Normal request */
436 status = send_dma_request(CMD_READ_DMA, 0, 1,
437 prdt, ARRAY_SIZE(prdt), NULL);
438 g_assert_cmphex(status, ==, 0);
439 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
441 /* Abort the request before it completes */
442 status = send_dma_request(CMD_READ_DMA | CMDF_ABORT, 0, 1,
443 prdt, ARRAY_SIZE(prdt), NULL);
444 g_assert_cmphex(status, ==, 0);
445 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
446 free_pci_device(dev);
449 static void test_bmdma_one_sector_short_prdt(void)
451 QPCIDevice *dev;
452 QPCIBar bmdma_bar, ide_bar;
453 uint8_t status;
455 /* Read 2 sectors but only give 1 sector in PRDT */
456 PrdtEntry prdt[] = {
458 .addr = 0,
459 .size = cpu_to_le32(0x200 | PRDT_EOT),
463 dev = get_pci_device(&bmdma_bar, &ide_bar);
465 /* Normal request */
466 status = send_dma_request(CMD_READ_DMA, 0, 2,
467 prdt, ARRAY_SIZE(prdt), NULL);
468 g_assert_cmphex(status, ==, 0);
469 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
471 /* Abort the request before it completes */
472 status = send_dma_request(CMD_READ_DMA | CMDF_ABORT, 0, 2,
473 prdt, ARRAY_SIZE(prdt), NULL);
474 g_assert_cmphex(status, ==, 0);
475 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
476 free_pci_device(dev);
479 static void test_bmdma_long_prdt(void)
481 QPCIDevice *dev;
482 QPCIBar bmdma_bar, ide_bar;
483 uint8_t status;
485 PrdtEntry prdt[] = {
487 .addr = 0,
488 .size = cpu_to_le32(0x1000 | PRDT_EOT),
492 dev = get_pci_device(&bmdma_bar, &ide_bar);
494 /* Normal request */
495 status = send_dma_request(CMD_READ_DMA, 0, 1,
496 prdt, ARRAY_SIZE(prdt), NULL);
497 g_assert_cmphex(status, ==, BM_STS_ACTIVE | BM_STS_INTR);
498 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
500 /* Abort the request before it completes */
501 status = send_dma_request(CMD_READ_DMA | CMDF_ABORT, 0, 1,
502 prdt, ARRAY_SIZE(prdt), NULL);
503 g_assert_cmphex(status, ==, BM_STS_INTR);
504 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
505 free_pci_device(dev);
508 static void test_bmdma_no_busmaster(void)
510 QPCIDevice *dev;
511 QPCIBar bmdma_bar, ide_bar;
512 uint8_t status;
514 dev = get_pci_device(&bmdma_bar, &ide_bar);
516 /* No PRDT_EOT, each entry addr 0/size 64k, and in theory qemu shouldn't be
517 * able to access it anyway because the Bus Master bit in the PCI command
518 * register isn't set. This is complete nonsense, but it used to be pretty
519 * good at confusing and occasionally crashing qemu. */
520 PrdtEntry prdt[4096] = { };
522 status = send_dma_request(CMD_READ_DMA | CMDF_NO_BM, 0, 512,
523 prdt, ARRAY_SIZE(prdt), NULL);
525 /* Not entirely clear what the expected result is, but this is what we get
526 * in practice. At least we want to be aware of any changes. */
527 g_assert_cmphex(status, ==, BM_STS_ACTIVE | BM_STS_INTR);
528 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
529 free_pci_device(dev);
532 static void test_bmdma_setup(void)
534 ide_test_start(
535 "-drive file=%s,if=ide,cache=writeback,format=raw "
536 "-global ide-hd.serial=%s -global ide-hd.ver=%s",
537 tmp_path, "testdisk", "version");
538 qtest_irq_intercept_in(global_qtest, "ioapic");
541 static void test_bmdma_teardown(void)
543 ide_test_quit();
546 static void string_cpu_to_be16(uint16_t *s, size_t bytes)
548 g_assert((bytes & 1) == 0);
549 bytes /= 2;
551 while (bytes--) {
552 *s = cpu_to_be16(*s);
553 s++;
557 static void test_identify(void)
559 QPCIDevice *dev;
560 QPCIBar bmdma_bar, ide_bar;
561 uint8_t data;
562 uint16_t buf[256];
563 int i;
564 int ret;
566 ide_test_start(
567 "-drive file=%s,if=ide,cache=writeback,format=raw "
568 "-global ide-hd.serial=%s -global ide-hd.ver=%s",
569 tmp_path, "testdisk", "version");
571 dev = get_pci_device(&bmdma_bar, &ide_bar);
573 /* IDENTIFY command on device 0*/
574 qpci_io_writeb(dev, ide_bar, reg_device, 0);
575 qpci_io_writeb(dev, ide_bar, reg_command, CMD_IDENTIFY);
577 /* Read in the IDENTIFY buffer and check registers */
578 data = qpci_io_readb(dev, ide_bar, reg_device);
579 g_assert_cmpint(data & DEV, ==, 0);
581 for (i = 0; i < 256; i++) {
582 data = qpci_io_readb(dev, ide_bar, reg_status);
583 assert_bit_set(data, DRDY | DRQ);
584 assert_bit_clear(data, BSY | DF | ERR);
586 buf[i] = qpci_io_readw(dev, ide_bar, reg_data);
589 data = qpci_io_readb(dev, ide_bar, reg_status);
590 assert_bit_set(data, DRDY);
591 assert_bit_clear(data, BSY | DF | ERR | DRQ);
593 /* Check serial number/version in the buffer */
594 string_cpu_to_be16(&buf[10], 20);
595 ret = memcmp(&buf[10], "testdisk ", 20);
596 g_assert(ret == 0);
598 string_cpu_to_be16(&buf[23], 8);
599 ret = memcmp(&buf[23], "version ", 8);
600 g_assert(ret == 0);
602 /* Write cache enabled bit */
603 assert_bit_set(buf[85], 0x20);
605 ide_test_quit();
606 free_pci_device(dev);
610 * Write sector 1 with random data to make IDE storage dirty
611 * Needed for flush tests so that flushes actually go though the block layer
613 static void make_dirty(uint8_t device)
615 QPCIDevice *dev;
616 QPCIBar bmdma_bar, ide_bar;
617 uint8_t status;
618 size_t len = 512;
619 uintptr_t guest_buf;
620 void* buf;
622 dev = get_pci_device(&bmdma_bar, &ide_bar);
624 guest_buf = guest_alloc(guest_malloc, len);
625 buf = g_malloc(len);
626 memset(buf, rand() % 255 + 1, len);
627 g_assert(guest_buf);
628 g_assert(buf);
630 memwrite(guest_buf, buf, len);
632 PrdtEntry prdt[] = {
634 .addr = cpu_to_le32(guest_buf),
635 .size = cpu_to_le32(len | PRDT_EOT),
639 status = send_dma_request(CMD_WRITE_DMA, 1, 1, prdt,
640 ARRAY_SIZE(prdt), NULL);
641 g_assert_cmphex(status, ==, BM_STS_INTR);
642 assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR);
644 g_free(buf);
645 free_pci_device(dev);
648 static void test_flush(void)
650 QPCIDevice *dev;
651 QPCIBar bmdma_bar, ide_bar;
652 uint8_t data;
654 ide_test_start(
655 "-drive file=blkdebug::%s,if=ide,cache=writeback,format=raw",
656 tmp_path);
658 dev = get_pci_device(&bmdma_bar, &ide_bar);
660 qtest_irq_intercept_in(global_qtest, "ioapic");
662 /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
663 make_dirty(0);
665 /* Delay the completion of the flush request until we explicitly do it */
666 g_free(hmp("qemu-io ide0-hd0 \"break flush_to_os A\""));
668 /* FLUSH CACHE command on device 0*/
669 qpci_io_writeb(dev, ide_bar, reg_device, 0);
670 qpci_io_writeb(dev, ide_bar, reg_command, CMD_FLUSH_CACHE);
672 /* Check status while request is in flight*/
673 data = qpci_io_readb(dev, ide_bar, reg_status);
674 assert_bit_set(data, BSY | DRDY);
675 assert_bit_clear(data, DF | ERR | DRQ);
677 /* Complete the command */
678 g_free(hmp("qemu-io ide0-hd0 \"resume A\""));
680 /* Check registers */
681 data = qpci_io_readb(dev, ide_bar, reg_device);
682 g_assert_cmpint(data & DEV, ==, 0);
684 do {
685 data = qpci_io_readb(dev, ide_bar, reg_status);
686 } while (data & BSY);
688 assert_bit_set(data, DRDY);
689 assert_bit_clear(data, BSY | DF | ERR | DRQ);
691 ide_test_quit();
692 free_pci_device(dev);
695 static void test_retry_flush(const char *machine)
697 QPCIDevice *dev;
698 QPCIBar bmdma_bar, ide_bar;
699 uint8_t data;
701 prepare_blkdebug_script(debug_path, "flush_to_disk");
703 ide_test_start(
704 "-drive file=blkdebug:%s:%s,if=ide,cache=writeback,format=raw,"
705 "rerror=stop,werror=stop",
706 debug_path, tmp_path);
708 dev = get_pci_device(&bmdma_bar, &ide_bar);
710 qtest_irq_intercept_in(global_qtest, "ioapic");
712 /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
713 make_dirty(0);
715 /* FLUSH CACHE command on device 0*/
716 qpci_io_writeb(dev, ide_bar, reg_device, 0);
717 qpci_io_writeb(dev, ide_bar, reg_command, CMD_FLUSH_CACHE);
719 /* Check status while request is in flight*/
720 data = qpci_io_readb(dev, ide_bar, reg_status);
721 assert_bit_set(data, BSY | DRDY);
722 assert_bit_clear(data, DF | ERR | DRQ);
724 qmp_eventwait("STOP");
726 /* Complete the command */
727 qmp_discard_response("{'execute':'cont' }");
729 /* Check registers */
730 data = qpci_io_readb(dev, ide_bar, reg_device);
731 g_assert_cmpint(data & DEV, ==, 0);
733 do {
734 data = qpci_io_readb(dev, ide_bar, reg_status);
735 } while (data & BSY);
737 assert_bit_set(data, DRDY);
738 assert_bit_clear(data, BSY | DF | ERR | DRQ);
740 ide_test_quit();
741 free_pci_device(dev);
744 static void test_flush_nodev(void)
746 QPCIDevice *dev;
747 QPCIBar bmdma_bar, ide_bar;
749 ide_test_start("");
751 dev = get_pci_device(&bmdma_bar, &ide_bar);
753 /* FLUSH CACHE command on device 0*/
754 qpci_io_writeb(dev, ide_bar, reg_device, 0);
755 qpci_io_writeb(dev, ide_bar, reg_command, CMD_FLUSH_CACHE);
757 /* Just testing that qemu doesn't crash... */
759 free_pci_device(dev);
760 ide_test_quit();
763 static void test_flush_empty_drive(void)
765 QPCIDevice *dev;
766 QPCIBar bmdma_bar, ide_bar;
768 ide_test_start("-device ide-cd,bus=ide.0");
769 dev = get_pci_device(&bmdma_bar, &ide_bar);
771 /* FLUSH CACHE command on device 0 */
772 qpci_io_writeb(dev, ide_bar, reg_device, 0);
773 qpci_io_writeb(dev, ide_bar, reg_command, CMD_FLUSH_CACHE);
775 /* Just testing that qemu doesn't crash... */
777 free_pci_device(dev);
778 ide_test_quit();
781 static void test_pci_retry_flush(void)
783 test_retry_flush("pc");
786 static void test_isa_retry_flush(void)
788 test_retry_flush("isapc");
791 typedef struct Read10CDB {
792 uint8_t opcode;
793 uint8_t flags;
794 uint32_t lba;
795 uint8_t reserved;
796 uint16_t nblocks;
797 uint8_t control;
798 uint16_t padding;
799 } __attribute__((__packed__)) Read10CDB;
801 static void send_scsi_cdb_read10(QPCIDevice *dev, QPCIBar ide_bar,
802 uint64_t lba, int nblocks)
804 Read10CDB pkt = { .padding = 0 };
805 int i;
807 g_assert_cmpint(lba, <=, UINT32_MAX);
808 g_assert_cmpint(nblocks, <=, UINT16_MAX);
809 g_assert_cmpint(nblocks, >=, 0);
811 /* Construct SCSI CDB packet */
812 pkt.opcode = 0x28;
813 pkt.lba = cpu_to_be32(lba);
814 pkt.nblocks = cpu_to_be16(nblocks);
816 /* Send Packet */
817 for (i = 0; i < sizeof(Read10CDB)/2; i++) {
818 qpci_io_writew(dev, ide_bar, reg_data,
819 le16_to_cpu(((uint16_t *)&pkt)[i]));
823 static void nsleep(int64_t nsecs)
825 const struct timespec val = { .tv_nsec = nsecs };
826 nanosleep(&val, NULL);
827 clock_set(nsecs);
830 static uint8_t ide_wait_clear(uint8_t flag)
832 QPCIDevice *dev;
833 QPCIBar bmdma_bar, ide_bar;
834 uint8_t data;
835 time_t st;
837 dev = get_pci_device(&bmdma_bar, &ide_bar);
839 /* Wait with a 5 second timeout */
840 time(&st);
841 while (true) {
842 data = qpci_io_readb(dev, ide_bar, reg_status);
843 if (!(data & flag)) {
844 free_pci_device(dev);
845 return data;
847 if (difftime(time(NULL), st) > 5.0) {
848 break;
850 nsleep(400);
852 g_assert_not_reached();
855 static void ide_wait_intr(int irq)
857 time_t st;
858 bool intr;
860 time(&st);
861 while (true) {
862 intr = get_irq(irq);
863 if (intr) {
864 return;
866 if (difftime(time(NULL), st) > 5.0) {
867 break;
869 nsleep(400);
872 g_assert_not_reached();
875 static void cdrom_pio_impl(int nblocks)
877 QPCIDevice *dev;
878 QPCIBar bmdma_bar, ide_bar;
879 FILE *fh;
880 int patt_blocks = MAX(16, nblocks);
881 size_t patt_len = ATAPI_BLOCK_SIZE * patt_blocks;
882 char *pattern = g_malloc(patt_len);
883 size_t rxsize = ATAPI_BLOCK_SIZE * nblocks;
884 uint16_t *rx = g_malloc0(rxsize);
885 int i, j;
886 uint8_t data;
887 uint16_t limit;
888 size_t ret;
890 /* Prepopulate the CDROM with an interesting pattern */
891 generate_pattern(pattern, patt_len, ATAPI_BLOCK_SIZE);
892 fh = fopen(tmp_path, "w+");
893 ret = fwrite(pattern, ATAPI_BLOCK_SIZE, patt_blocks, fh);
894 g_assert_cmpint(ret, ==, patt_blocks);
895 fclose(fh);
897 ide_test_start("-drive if=none,file=%s,media=cdrom,format=raw,id=sr0,index=0 "
898 "-device ide-cd,drive=sr0,bus=ide.0", tmp_path);
899 dev = get_pci_device(&bmdma_bar, &ide_bar);
900 qtest_irq_intercept_in(global_qtest, "ioapic");
902 /* PACKET command on device 0 */
903 qpci_io_writeb(dev, ide_bar, reg_device, 0);
904 qpci_io_writeb(dev, ide_bar, reg_lba_middle, BYTE_COUNT_LIMIT & 0xFF);
905 qpci_io_writeb(dev, ide_bar, reg_lba_high, (BYTE_COUNT_LIMIT >> 8 & 0xFF));
906 qpci_io_writeb(dev, ide_bar, reg_command, CMD_PACKET);
907 /* HP0: Check_Status_A State */
908 nsleep(400);
909 data = ide_wait_clear(BSY);
910 /* HP1: Send_Packet State */
911 assert_bit_set(data, DRQ | DRDY);
912 assert_bit_clear(data, ERR | DF | BSY);
914 /* SCSI CDB (READ10) -- read n*2048 bytes from block 0 */
915 send_scsi_cdb_read10(dev, ide_bar, 0, nblocks);
917 /* Read data back: occurs in bursts of 'BYTE_COUNT_LIMIT' bytes.
918 * If BYTE_COUNT_LIMIT is odd, we transfer BYTE_COUNT_LIMIT - 1 bytes.
919 * We allow an odd limit only when the remaining transfer size is
920 * less than BYTE_COUNT_LIMIT. However, SCSI's read10 command can only
921 * request n blocks, so our request size is always even.
922 * For this reason, we assume there is never a hanging byte to fetch. */
923 g_assert(!(rxsize & 1));
924 limit = BYTE_COUNT_LIMIT & ~1;
925 for (i = 0; i < DIV_ROUND_UP(rxsize, limit); i++) {
926 size_t offset = i * (limit / 2);
927 size_t rem = (rxsize / 2) - offset;
929 /* HP3: INTRQ_Wait */
930 ide_wait_intr(IDE_PRIMARY_IRQ);
932 /* HP2: Check_Status_B (and clear IRQ) */
933 data = ide_wait_clear(BSY);
934 assert_bit_set(data, DRQ | DRDY);
935 assert_bit_clear(data, ERR | DF | BSY);
937 /* HP4: Transfer_Data */
938 for (j = 0; j < MIN((limit / 2), rem); j++) {
939 rx[offset + j] = cpu_to_le16(qpci_io_readw(dev, ide_bar,
940 reg_data));
944 /* Check for final completion IRQ */
945 ide_wait_intr(IDE_PRIMARY_IRQ);
947 /* Sanity check final state */
948 data = ide_wait_clear(DRQ);
949 assert_bit_set(data, DRDY);
950 assert_bit_clear(data, DRQ | ERR | DF | BSY);
952 g_assert_cmpint(memcmp(pattern, rx, rxsize), ==, 0);
953 g_free(pattern);
954 g_free(rx);
955 test_bmdma_teardown();
956 free_pci_device(dev);
959 static void test_cdrom_pio(void)
961 cdrom_pio_impl(1);
964 static void test_cdrom_pio_large(void)
966 /* Test a few loops of the PIO DRQ mechanism. */
967 cdrom_pio_impl(BYTE_COUNT_LIMIT * 4 / ATAPI_BLOCK_SIZE);
971 static void test_cdrom_dma(void)
973 static const size_t len = ATAPI_BLOCK_SIZE;
974 size_t ret;
975 char *pattern = g_malloc(ATAPI_BLOCK_SIZE * 16);
976 char *rx = g_malloc0(len);
977 uintptr_t guest_buf;
978 PrdtEntry prdt[1];
979 FILE *fh;
981 ide_test_start("-drive if=none,file=%s,media=cdrom,format=raw,id=sr0,index=0 "
982 "-device ide-cd,drive=sr0,bus=ide.0", tmp_path);
983 qtest_irq_intercept_in(global_qtest, "ioapic");
985 guest_buf = guest_alloc(guest_malloc, len);
986 prdt[0].addr = cpu_to_le32(guest_buf);
987 prdt[0].size = cpu_to_le32(len | PRDT_EOT);
989 generate_pattern(pattern, ATAPI_BLOCK_SIZE * 16, ATAPI_BLOCK_SIZE);
990 fh = fopen(tmp_path, "w+");
991 ret = fwrite(pattern, ATAPI_BLOCK_SIZE, 16, fh);
992 g_assert_cmpint(ret, ==, 16);
993 fclose(fh);
995 send_dma_request(CMD_PACKET, 0, 1, prdt, 1, send_scsi_cdb_read10);
997 /* Read back data from guest memory into local qtest memory */
998 memread(guest_buf, rx, len);
999 g_assert_cmpint(memcmp(pattern, rx, len), ==, 0);
1001 g_free(pattern);
1002 g_free(rx);
1003 test_bmdma_teardown();
1006 int main(int argc, char **argv)
1008 const char *arch = qtest_get_arch();
1009 int fd;
1010 int ret;
1012 /* Check architecture */
1013 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
1014 g_test_message("Skipping test for non-x86\n");
1015 return 0;
1018 /* Create temporary blkdebug instructions */
1019 fd = mkstemp(debug_path);
1020 g_assert(fd >= 0);
1021 close(fd);
1023 /* Create a temporary raw image */
1024 fd = mkstemp(tmp_path);
1025 g_assert(fd >= 0);
1026 ret = ftruncate(fd, TEST_IMAGE_SIZE);
1027 g_assert(ret == 0);
1028 close(fd);
1030 /* Run the tests */
1031 g_test_init(&argc, &argv, NULL);
1033 qtest_add_func("/ide/identify", test_identify);
1035 qtest_add_func("/ide/bmdma/setup", test_bmdma_setup);
1036 qtest_add_func("/ide/bmdma/simple_rw", test_bmdma_simple_rw);
1037 qtest_add_func("/ide/bmdma/trim", test_bmdma_trim);
1038 qtest_add_func("/ide/bmdma/short_prdt", test_bmdma_short_prdt);
1039 qtest_add_func("/ide/bmdma/one_sector_short_prdt",
1040 test_bmdma_one_sector_short_prdt);
1041 qtest_add_func("/ide/bmdma/long_prdt", test_bmdma_long_prdt);
1042 qtest_add_func("/ide/bmdma/no_busmaster", test_bmdma_no_busmaster);
1043 qtest_add_func("/ide/bmdma/teardown", test_bmdma_teardown);
1045 qtest_add_func("/ide/flush", test_flush);
1046 qtest_add_func("/ide/flush/nodev", test_flush_nodev);
1047 qtest_add_func("/ide/flush/empty_drive", test_flush_empty_drive);
1048 qtest_add_func("/ide/flush/retry_pci", test_pci_retry_flush);
1049 qtest_add_func("/ide/flush/retry_isa", test_isa_retry_flush);
1051 qtest_add_func("/ide/cdrom/pio", test_cdrom_pio);
1052 qtest_add_func("/ide/cdrom/pio_large", test_cdrom_pio_large);
1053 qtest_add_func("/ide/cdrom/dma", test_cdrom_dma);
1055 ret = g_test_run();
1057 /* Cleanup */
1058 unlink(tmp_path);
1059 unlink(debug_path);
1061 return ret;