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
25 #include "qemu/osdep.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/bswap.h"
34 #include "hw/pci/pci_ids.h"
35 #include "hw/pci/pci_regs.h"
37 /* TODO actually test the results and get rid of this */
38 #define qmp_discard_response(q, ...) qobject_unref(qtest_qmp(q, __VA_ARGS__))
40 #define TEST_IMAGE_SIZE 64 * 1024 * 1024
43 #define IDE_PCI_FUNC 1
45 #define IDE_BASE 0x1f0
46 #define IDE_PRIMARY_IRQ 14
48 #define ATAPI_BLOCK_SIZE 2048
50 /* How many bytes to receive via ATAPI PIO at one time.
51 * Must be less than 0xFFFF. */
52 #define BYTE_COUNT_LIMIT 5120
96 CMD_FLUSH_CACHE
= 0xe7,
106 BM_CMD_WRITE
= 0x8, /* write = from device to memory */
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
[2];
126 static char *debug_path
;
128 static QTestState
*ide_test_start(const char *cmdline_fmt
, ...)
131 g_autofree
char *full_fmt
= g_strdup_printf("-machine pc %s", cmdline_fmt
);
134 va_start(ap
, cmdline_fmt
);
135 qts
= qtest_vinitf(full_fmt
, ap
);
138 pc_alloc_init(&guest_malloc
, qts
, 0);
143 static void ide_test_quit(QTestState
*qts
)
146 qpci_free_pc(pcibus
);
149 alloc_destroy(&guest_malloc
);
153 static QPCIDevice
*get_pci_device(QTestState
*qts
, QPCIBar
*bmdma_bar
,
157 uint16_t vendor_id
, device_id
;
160 pcibus
= qpci_new_pc(qts
, NULL
);
163 /* Find PCI device and verify it's the right one */
164 dev
= qpci_device_find(pcibus
, QPCI_DEVFN(IDE_PCI_DEV
, IDE_PCI_FUNC
));
165 g_assert(dev
!= NULL
);
167 vendor_id
= qpci_config_readw(dev
, PCI_VENDOR_ID
);
168 device_id
= qpci_config_readw(dev
, PCI_DEVICE_ID
);
169 g_assert(vendor_id
== PCI_VENDOR_ID_INTEL
);
170 g_assert(device_id
== PCI_DEVICE_ID_INTEL_82371SB_1
);
173 *bmdma_bar
= qpci_iomap(dev
, 4, NULL
);
175 *ide_bar
= qpci_legacy_iomap(dev
, IDE_BASE
);
177 qpci_device_enable(dev
);
182 static void free_pci_device(QPCIDevice
*dev
)
184 /* libqos doesn't have a function for this, so free it manually */
188 typedef struct PrdtEntry
{
191 } QEMU_PACKED PrdtEntry
;
193 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
194 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
196 static uint64_t trim_range_le(uint64_t sector
, uint16_t count
)
198 /* 2-byte range, 6-byte LBA */
199 return cpu_to_le64(((uint64_t)count
<< 48) + sector
);
202 static int send_dma_request(QTestState
*qts
, int cmd
, uint64_t sector
,
203 int nb_sectors
, PrdtEntry
*prdt
, int prdt_entries
,
204 void(*post_exec
)(QPCIDevice
*dev
, QPCIBar ide_bar
,
205 uint64_t sector
, int nb_sectors
))
208 QPCIBar bmdma_bar
, ide_bar
;
209 uintptr_t guest_prdt
;
215 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
223 /* Assuming we only test data reads w/ ATAPI, otherwise we need to know
224 * the SCSI command being sent in the packet, too. */
232 g_assert_not_reached();
235 if (flags
& CMDF_NO_BM
) {
236 qpci_config_writew(dev
, PCI_COMMAND
,
237 PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
);
240 /* Select device 0 */
241 qpci_io_writeb(dev
, ide_bar
, reg_device
, 0 | LBA
);
243 /* Stop any running transfer, clear any pending interrupt */
244 qpci_io_writeb(dev
, bmdma_bar
, bmreg_cmd
, 0);
245 qpci_io_writeb(dev
, bmdma_bar
, bmreg_status
, BM_STS_INTR
);
248 len
= sizeof(*prdt
) * prdt_entries
;
249 guest_prdt
= guest_alloc(&guest_malloc
, len
);
250 qtest_memwrite(qts
, guest_prdt
, prdt
, len
);
251 qpci_io_writel(dev
, bmdma_bar
, bmreg_prdt
, guest_prdt
);
253 /* ATA DMA command */
254 if (cmd
== CMD_PACKET
) {
255 /* Enables ATAPI DMA; otherwise PIO is attempted */
256 qpci_io_writeb(dev
, ide_bar
, reg_feature
, 0x01);
258 if (cmd
== CMD_DSM
) {
260 qpci_io_writeb(dev
, ide_bar
, reg_feature
, 0x01);
262 qpci_io_writeb(dev
, ide_bar
, reg_nsectors
, nb_sectors
);
263 qpci_io_writeb(dev
, ide_bar
, reg_lba_low
, sector
& 0xff);
264 qpci_io_writeb(dev
, ide_bar
, reg_lba_middle
, (sector
>> 8) & 0xff);
265 qpci_io_writeb(dev
, ide_bar
, reg_lba_high
, (sector
>> 16) & 0xff);
268 qpci_io_writeb(dev
, ide_bar
, reg_command
, cmd
);
271 post_exec(dev
, ide_bar
, sector
, nb_sectors
);
274 /* Start DMA transfer */
275 qpci_io_writeb(dev
, bmdma_bar
, bmreg_cmd
,
276 BM_CMD_START
| (from_dev
? BM_CMD_WRITE
: 0));
278 if (flags
& CMDF_ABORT
) {
279 qpci_io_writeb(dev
, bmdma_bar
, bmreg_cmd
, 0);
282 /* Wait for the DMA transfer to complete */
284 status
= qpci_io_readb(dev
, bmdma_bar
, bmreg_status
);
285 } while ((status
& (BM_STS_ACTIVE
| BM_STS_INTR
)) == BM_STS_ACTIVE
);
287 g_assert_cmpint(qtest_get_irq(qts
, IDE_PRIMARY_IRQ
), ==,
288 !!(status
& BM_STS_INTR
));
290 /* Check IDE status code */
291 assert_bit_set(qpci_io_readb(dev
, ide_bar
, reg_status
), DRDY
);
292 assert_bit_clear(qpci_io_readb(dev
, ide_bar
, reg_status
), BSY
| DRQ
);
294 /* Reading the status register clears the IRQ */
295 g_assert(!qtest_get_irq(qts
, IDE_PRIMARY_IRQ
));
297 /* Stop DMA transfer if still active */
298 if (status
& BM_STS_ACTIVE
) {
299 qpci_io_writeb(dev
, bmdma_bar
, bmreg_cmd
, 0);
302 free_pci_device(dev
);
307 static QTestState
*test_bmdma_setup(void)
311 qts
= ide_test_start(
312 "-drive file=%s,if=ide,cache=writeback,format=raw "
313 "-global ide-hd.serial=%s -global ide-hd.ver=%s",
314 tmp_path
[0], "testdisk", "version");
315 qtest_irq_intercept_in(qts
, "ioapic");
320 static void test_bmdma_teardown(QTestState
*qts
)
325 static void test_bmdma_simple_rw(void)
329 QPCIBar bmdma_bar
, ide_bar
;
337 qts
= test_bmdma_setup();
339 guest_buf
= guest_alloc(&guest_malloc
, len
);
340 prdt
[0].addr
= cpu_to_le32(guest_buf
);
341 prdt
[0].size
= cpu_to_le32(len
| PRDT_EOT
);
343 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
346 cmpbuf
= g_malloc(len
);
348 /* Write 0x55 pattern to sector 0 */
349 memset(buf
, 0x55, len
);
350 qtest_memwrite(qts
, guest_buf
, buf
, len
);
352 status
= send_dma_request(qts
, CMD_WRITE_DMA
, 0, 1, prdt
,
353 ARRAY_SIZE(prdt
), NULL
);
354 g_assert_cmphex(status
, ==, BM_STS_INTR
);
355 assert_bit_clear(qpci_io_readb(dev
, ide_bar
, reg_status
), DF
| ERR
);
357 /* Write 0xaa pattern to sector 1 */
358 memset(buf
, 0xaa, len
);
359 qtest_memwrite(qts
, guest_buf
, buf
, len
);
361 status
= send_dma_request(qts
, CMD_WRITE_DMA
, 1, 1, prdt
,
362 ARRAY_SIZE(prdt
), NULL
);
363 g_assert_cmphex(status
, ==, BM_STS_INTR
);
364 assert_bit_clear(qpci_io_readb(dev
, ide_bar
, reg_status
), DF
| ERR
);
366 /* Read and verify 0x55 pattern in sector 0 */
367 memset(cmpbuf
, 0x55, len
);
369 status
= send_dma_request(qts
, CMD_READ_DMA
, 0, 1, prdt
, ARRAY_SIZE(prdt
),
371 g_assert_cmphex(status
, ==, BM_STS_INTR
);
372 assert_bit_clear(qpci_io_readb(dev
, ide_bar
, reg_status
), DF
| ERR
);
374 qtest_memread(qts
, guest_buf
, buf
, len
);
375 g_assert(memcmp(buf
, cmpbuf
, len
) == 0);
377 /* Read and verify 0xaa pattern in sector 1 */
378 memset(cmpbuf
, 0xaa, len
);
380 status
= send_dma_request(qts
, CMD_READ_DMA
, 1, 1, prdt
, ARRAY_SIZE(prdt
),
382 g_assert_cmphex(status
, ==, BM_STS_INTR
);
383 assert_bit_clear(qpci_io_readb(dev
, ide_bar
, reg_status
), DF
| ERR
);
385 qtest_memread(qts
, guest_buf
, buf
, len
);
386 g_assert(memcmp(buf
, cmpbuf
, len
) == 0);
388 free_pci_device(dev
);
392 test_bmdma_teardown(qts
);
395 static void test_bmdma_trim(void)
399 QPCIBar bmdma_bar
, ide_bar
;
401 const uint64_t trim_range
[] = { trim_range_le(0, 2),
403 trim_range_le(10, 1),
405 const uint64_t bad_range
= trim_range_le(TEST_IMAGE_SIZE
/ 512 - 1, 2);
411 qts
= test_bmdma_setup();
413 guest_buf
= guest_alloc(&guest_malloc
, len
);
414 prdt
[0].addr
= cpu_to_le32(guest_buf
),
415 prdt
[0].size
= cpu_to_le32(len
| PRDT_EOT
),
417 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
422 *((uint64_t *)buf
) = trim_range
[0];
423 *((uint64_t *)buf
+ 1) = trim_range
[1];
425 qtest_memwrite(qts
, guest_buf
, buf
, 2 * sizeof(uint64_t));
427 status
= send_dma_request(qts
, CMD_DSM
, 0, 1, prdt
,
428 ARRAY_SIZE(prdt
), NULL
);
429 g_assert_cmphex(status
, ==, BM_STS_INTR
);
430 assert_bit_clear(qpci_io_readb(dev
, ide_bar
, reg_status
), DF
| ERR
);
432 /* Request contains invalid range */
433 *((uint64_t *)buf
) = trim_range
[2];
434 *((uint64_t *)buf
+ 1) = bad_range
;
436 qtest_memwrite(qts
, guest_buf
, buf
, 2 * sizeof(uint64_t));
438 status
= send_dma_request(qts
, CMD_DSM
, 0, 1, prdt
,
439 ARRAY_SIZE(prdt
), NULL
);
440 g_assert_cmphex(status
, ==, BM_STS_INTR
);
441 assert_bit_set(qpci_io_readb(dev
, ide_bar
, reg_status
), ERR
);
442 assert_bit_set(qpci_io_readb(dev
, ide_bar
, reg_error
), ABRT
);
444 free_pci_device(dev
);
446 test_bmdma_teardown(qts
);
450 * This test is developed according to the Programming Interface for
451 * Bus Master IDE Controller (Revision 1.0 5/16/94)
453 static void test_bmdma_various_prdts(void)
458 for (sectors
= 1; sectors
<= 256; sectors
*= 2) {
459 QTestState
*qts
= NULL
;
460 QPCIDevice
*dev
= NULL
;
461 QPCIBar bmdma_bar
, ide_bar
;
463 qts
= test_bmdma_setup();
464 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
466 for (size
= 0; size
< 65536; size
+= 256) {
467 uint32_t req_size
= sectors
* 512;
468 uint32_t prd_size
= size
& 0xfffe; /* bit 0 is always set to 0 */
470 uint8_t req_status
= 0;
471 uint8_t abort_req_status
= 0;
475 .size
= cpu_to_le32(size
| PRDT_EOT
),
479 /* A value of zero in PRD size indicates 64K */
485 * 1. If PRDs specified a smaller size than the IDE transfer
486 * size, then the Interrupt and Active bits in the Controller
487 * status register are not set (Error Condition).
489 * 2. If the size of the physical memory regions was equal to
490 * the IDE device transfer size, the Interrupt bit in the
491 * Controller status register is set to 1, Active bit is set to 0.
493 * 3. If PRDs specified a larger size than the IDE transfer size,
494 * the Interrupt and Active bits in the Controller status register
497 if (prd_size
< req_size
) {
499 abort_req_status
= 0;
500 } else if (prd_size
== req_size
) {
501 req_status
= BM_STS_INTR
;
502 abort_req_status
= BM_STS_INTR
;
504 req_status
= BM_STS_ACTIVE
| BM_STS_INTR
;
505 abort_req_status
= BM_STS_INTR
;
508 /* Test the request */
509 ret
= send_dma_request(qts
, CMD_READ_DMA
, 0, sectors
,
510 prdt
, ARRAY_SIZE(prdt
), NULL
);
511 g_assert_cmphex(ret
, ==, req_status
);
512 assert_bit_clear(qpci_io_readb(dev
, ide_bar
, reg_status
), DF
| ERR
);
514 /* Now test aborting the same request */
515 ret
= send_dma_request(qts
, CMD_READ_DMA
| CMDF_ABORT
, 0,
516 sectors
, prdt
, ARRAY_SIZE(prdt
), NULL
);
517 g_assert_cmphex(ret
, ==, abort_req_status
);
518 assert_bit_clear(qpci_io_readb(dev
, ide_bar
, reg_status
), DF
| ERR
);
521 free_pci_device(dev
);
522 test_bmdma_teardown(qts
);
526 static void test_bmdma_no_busmaster(void)
530 QPCIBar bmdma_bar
, ide_bar
;
533 qts
= test_bmdma_setup();
535 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
537 /* No PRDT_EOT, each entry addr 0/size 64k, and in theory qemu shouldn't be
538 * able to access it anyway because the Bus Master bit in the PCI command
539 * register isn't set. This is complete nonsense, but it used to be pretty
540 * good at confusing and occasionally crashing qemu. */
541 PrdtEntry prdt
[4096] = { };
543 status
= send_dma_request(qts
, CMD_READ_DMA
| CMDF_NO_BM
, 0, 512,
544 prdt
, ARRAY_SIZE(prdt
), NULL
);
546 /* Not entirely clear what the expected result is, but this is what we get
547 * in practice. At least we want to be aware of any changes. */
548 g_assert_cmphex(status
, ==, BM_STS_ACTIVE
| BM_STS_INTR
);
549 assert_bit_clear(qpci_io_readb(dev
, ide_bar
, reg_status
), DF
| ERR
);
550 free_pci_device(dev
);
551 test_bmdma_teardown(qts
);
554 static void string_cpu_to_be16(uint16_t *s
, size_t bytes
)
556 g_assert((bytes
& 1) == 0);
560 *s
= cpu_to_be16(*s
);
565 static void test_identify(void)
569 QPCIBar bmdma_bar
, ide_bar
;
575 qts
= ide_test_start(
576 "-drive file=%s,if=ide,cache=writeback,format=raw "
577 "-global ide-hd.serial=%s -global ide-hd.ver=%s",
578 tmp_path
[0], "testdisk", "version");
580 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
582 /* IDENTIFY command on device 0*/
583 qpci_io_writeb(dev
, ide_bar
, reg_device
, 0);
584 qpci_io_writeb(dev
, ide_bar
, reg_command
, CMD_IDENTIFY
);
586 /* Read in the IDENTIFY buffer and check registers */
587 data
= qpci_io_readb(dev
, ide_bar
, reg_device
);
588 g_assert_cmpint(data
& DEV
, ==, 0);
590 for (i
= 0; i
< 256; i
++) {
591 data
= qpci_io_readb(dev
, ide_bar
, reg_status
);
592 assert_bit_set(data
, DRDY
| DRQ
);
593 assert_bit_clear(data
, BSY
| DF
| ERR
);
595 buf
[i
] = qpci_io_readw(dev
, ide_bar
, reg_data
);
598 data
= qpci_io_readb(dev
, ide_bar
, reg_status
);
599 assert_bit_set(data
, DRDY
);
600 assert_bit_clear(data
, BSY
| DF
| ERR
| DRQ
);
602 /* Check serial number/version in the buffer */
603 string_cpu_to_be16(&buf
[10], 20);
604 ret
= memcmp(&buf
[10], "testdisk ", 20);
607 string_cpu_to_be16(&buf
[23], 8);
608 ret
= memcmp(&buf
[23], "version ", 8);
611 /* Write cache enabled bit */
612 assert_bit_set(buf
[85], 0x20);
615 free_pci_device(dev
);
618 static void test_diagnostic(void)
622 QPCIBar bmdma_bar
, ide_bar
;
625 qts
= ide_test_start(
626 "-blockdev driver=file,node-name=hda,filename=%s "
627 "-blockdev driver=file,node-name=hdb,filename=%s "
628 "-device ide-hd,drive=hda,bus=ide.0,unit=0 "
629 "-device ide-hd,drive=hdb,bus=ide.0,unit=1 ",
630 tmp_path
[0], tmp_path
[1]);
632 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
634 /* DIAGNOSE command on device 1 */
635 qpci_io_writeb(dev
, ide_bar
, reg_device
, DEV
);
636 data
= qpci_io_readb(dev
, ide_bar
, reg_device
);
637 g_assert_cmphex(data
& DEV
, ==, DEV
);
638 qpci_io_writeb(dev
, ide_bar
, reg_command
, CMD_DIAGNOSE
);
640 /* Verify that DEVICE is now 0 */
641 data
= qpci_io_readb(dev
, ide_bar
, reg_device
);
642 g_assert_cmphex(data
& DEV
, ==, 0);
645 free_pci_device(dev
);
649 * Write sector 1 with random data to make IDE storage dirty
650 * Needed for flush tests so that flushes actually go though the block layer
652 static void make_dirty(QTestState
*qts
, uint8_t device
)
655 QPCIBar bmdma_bar
, ide_bar
;
661 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
663 guest_buf
= guest_alloc(&guest_malloc
, len
);
665 memset(buf
, rand() % 255 + 1, len
);
669 qtest_memwrite(qts
, guest_buf
, buf
, len
);
673 .addr
= cpu_to_le32(guest_buf
),
674 .size
= cpu_to_le32(len
| PRDT_EOT
),
678 status
= send_dma_request(qts
, CMD_WRITE_DMA
, 1, 1, prdt
,
679 ARRAY_SIZE(prdt
), NULL
);
680 g_assert_cmphex(status
, ==, BM_STS_INTR
);
681 assert_bit_clear(qpci_io_readb(dev
, ide_bar
, reg_status
), DF
| ERR
);
684 free_pci_device(dev
);
687 static void test_flush(void)
691 QPCIBar bmdma_bar
, ide_bar
;
694 qts
= ide_test_start(
695 "-drive file=blkdebug::%s,if=ide,cache=writeback,format=raw",
698 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
700 qtest_irq_intercept_in(qts
, "ioapic");
702 /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
705 /* Delay the completion of the flush request until we explicitly do it */
706 g_free(qtest_hmp(qts
, "qemu-io ide0-hd0 \"break flush_to_os A\""));
708 /* FLUSH CACHE command on device 0*/
709 qpci_io_writeb(dev
, ide_bar
, reg_device
, 0);
710 qpci_io_writeb(dev
, ide_bar
, reg_command
, CMD_FLUSH_CACHE
);
712 /* Check status while request is in flight*/
713 data
= qpci_io_readb(dev
, ide_bar
, reg_status
);
714 assert_bit_set(data
, BSY
| DRDY
);
715 assert_bit_clear(data
, DF
| ERR
| DRQ
);
717 /* Complete the command */
718 g_free(qtest_hmp(qts
, "qemu-io ide0-hd0 \"resume A\""));
720 /* Check registers */
721 data
= qpci_io_readb(dev
, ide_bar
, reg_device
);
722 g_assert_cmpint(data
& DEV
, ==, 0);
725 data
= qpci_io_readb(dev
, ide_bar
, reg_status
);
726 } while (data
& BSY
);
728 assert_bit_set(data
, DRDY
);
729 assert_bit_clear(data
, BSY
| DF
| ERR
| DRQ
);
732 free_pci_device(dev
);
735 static void test_pci_retry_flush(void)
739 QPCIBar bmdma_bar
, ide_bar
;
742 prepare_blkdebug_script(debug_path
, "flush_to_disk");
744 qts
= ide_test_start(
745 "-drive file=blkdebug:%s:%s,if=ide,cache=writeback,format=raw,"
746 "rerror=stop,werror=stop",
747 debug_path
, tmp_path
[0]);
749 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
751 qtest_irq_intercept_in(qts
, "ioapic");
753 /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */
756 /* FLUSH CACHE command on device 0*/
757 qpci_io_writeb(dev
, ide_bar
, reg_device
, 0);
758 qpci_io_writeb(dev
, ide_bar
, reg_command
, CMD_FLUSH_CACHE
);
760 /* Check status while request is in flight*/
761 data
= qpci_io_readb(dev
, ide_bar
, reg_status
);
762 assert_bit_set(data
, BSY
| DRDY
);
763 assert_bit_clear(data
, DF
| ERR
| DRQ
);
765 qtest_qmp_eventwait(qts
, "STOP");
767 /* Complete the command */
768 qmp_discard_response(qts
, "{'execute':'cont' }");
770 /* Check registers */
771 data
= qpci_io_readb(dev
, ide_bar
, reg_device
);
772 g_assert_cmpint(data
& DEV
, ==, 0);
775 data
= qpci_io_readb(dev
, ide_bar
, reg_status
);
776 } while (data
& BSY
);
778 assert_bit_set(data
, DRDY
);
779 assert_bit_clear(data
, BSY
| DF
| ERR
| DRQ
);
782 free_pci_device(dev
);
785 static void test_flush_nodev(void)
789 QPCIBar bmdma_bar
, ide_bar
;
791 qts
= ide_test_start("");
793 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
795 /* FLUSH CACHE command on device 0*/
796 qpci_io_writeb(dev
, ide_bar
, reg_device
, 0);
797 qpci_io_writeb(dev
, ide_bar
, reg_command
, CMD_FLUSH_CACHE
);
799 /* Just testing that qemu doesn't crash... */
801 free_pci_device(dev
);
805 static void test_flush_empty_drive(void)
809 QPCIBar bmdma_bar
, ide_bar
;
811 qts
= ide_test_start("-device ide-cd,bus=ide.0");
812 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
814 /* FLUSH CACHE command on device 0 */
815 qpci_io_writeb(dev
, ide_bar
, reg_device
, 0);
816 qpci_io_writeb(dev
, ide_bar
, reg_command
, CMD_FLUSH_CACHE
);
818 /* Just testing that qemu doesn't crash... */
820 free_pci_device(dev
);
824 typedef struct Read10CDB
{
832 } __attribute__((__packed__
)) Read10CDB
;
834 static void send_scsi_cdb_read10(QPCIDevice
*dev
, QPCIBar ide_bar
,
835 uint64_t lba
, int nblocks
)
837 Read10CDB pkt
= { .padding
= 0 };
840 g_assert_cmpint(lba
, <=, UINT32_MAX
);
841 g_assert_cmpint(nblocks
, <=, UINT16_MAX
);
842 g_assert_cmpint(nblocks
, >=, 0);
844 /* Construct SCSI CDB packet */
846 pkt
.lba
= cpu_to_be32(lba
);
847 pkt
.nblocks
= cpu_to_be16(nblocks
);
850 for (i
= 0; i
< sizeof(Read10CDB
)/2; i
++) {
851 qpci_io_writew(dev
, ide_bar
, reg_data
,
852 le16_to_cpu(((uint16_t *)&pkt
)[i
]));
856 static void nsleep(QTestState
*qts
, int64_t nsecs
)
858 const struct timespec val
= { .tv_nsec
= nsecs
};
859 nanosleep(&val
, NULL
);
860 qtest_clock_set(qts
, nsecs
);
863 static uint8_t ide_wait_clear(QTestState
*qts
, uint8_t flag
)
866 QPCIBar bmdma_bar
, ide_bar
;
870 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
872 /* Wait with a 5 second timeout */
875 data
= qpci_io_readb(dev
, ide_bar
, reg_status
);
876 if (!(data
& flag
)) {
877 free_pci_device(dev
);
880 if (difftime(time(NULL
), st
) > 5.0) {
885 g_assert_not_reached();
888 static void ide_wait_intr(QTestState
*qts
, int irq
)
895 intr
= qtest_get_irq(qts
, irq
);
899 if (difftime(time(NULL
), st
) > 5.0) {
905 g_assert_not_reached();
908 static void cdrom_pio_impl(int nblocks
)
912 QPCIBar bmdma_bar
, ide_bar
;
914 int patt_blocks
= MAX(16, nblocks
);
915 size_t patt_len
= ATAPI_BLOCK_SIZE
* patt_blocks
;
916 char *pattern
= g_malloc(patt_len
);
917 size_t rxsize
= ATAPI_BLOCK_SIZE
* nblocks
;
918 uint16_t *rx
= g_malloc0(rxsize
);
924 /* Prepopulate the CDROM with an interesting pattern */
925 generate_pattern(pattern
, patt_len
, ATAPI_BLOCK_SIZE
);
926 fh
= fopen(tmp_path
[0], "wb+");
927 ret
= fwrite(pattern
, ATAPI_BLOCK_SIZE
, patt_blocks
, fh
);
928 g_assert_cmpint(ret
, ==, patt_blocks
);
931 qts
= ide_test_start(
932 "-drive if=none,file=%s,media=cdrom,format=raw,id=sr0,index=0 "
933 "-device ide-cd,drive=sr0,bus=ide.0", tmp_path
[0]);
934 dev
= get_pci_device(qts
, &bmdma_bar
, &ide_bar
);
935 qtest_irq_intercept_in(qts
, "ioapic");
937 /* PACKET command on device 0 */
938 qpci_io_writeb(dev
, ide_bar
, reg_device
, 0);
939 qpci_io_writeb(dev
, ide_bar
, reg_lba_middle
, BYTE_COUNT_LIMIT
& 0xFF);
940 qpci_io_writeb(dev
, ide_bar
, reg_lba_high
, (BYTE_COUNT_LIMIT
>> 8 & 0xFF));
941 qpci_io_writeb(dev
, ide_bar
, reg_command
, CMD_PACKET
);
942 /* HP0: Check_Status_A State */
944 data
= ide_wait_clear(qts
, BSY
);
945 /* HP1: Send_Packet State */
946 assert_bit_set(data
, DRQ
| DRDY
);
947 assert_bit_clear(data
, ERR
| DF
| BSY
);
949 /* SCSI CDB (READ10) -- read n*2048 bytes from block 0 */
950 send_scsi_cdb_read10(dev
, ide_bar
, 0, nblocks
);
952 /* Read data back: occurs in bursts of 'BYTE_COUNT_LIMIT' bytes.
953 * If BYTE_COUNT_LIMIT is odd, we transfer BYTE_COUNT_LIMIT - 1 bytes.
954 * We allow an odd limit only when the remaining transfer size is
955 * less than BYTE_COUNT_LIMIT. However, SCSI's read10 command can only
956 * request n blocks, so our request size is always even.
957 * For this reason, we assume there is never a hanging byte to fetch. */
958 g_assert(!(rxsize
& 1));
959 limit
= BYTE_COUNT_LIMIT
& ~1;
960 for (i
= 0; i
< DIV_ROUND_UP(rxsize
, limit
); i
++) {
961 size_t offset
= i
* (limit
/ 2);
962 size_t rem
= (rxsize
/ 2) - offset
;
964 /* HP3: INTRQ_Wait */
965 ide_wait_intr(qts
, IDE_PRIMARY_IRQ
);
967 /* HP2: Check_Status_B (and clear IRQ) */
968 data
= ide_wait_clear(qts
, BSY
);
969 assert_bit_set(data
, DRQ
| DRDY
);
970 assert_bit_clear(data
, ERR
| DF
| BSY
);
972 /* HP4: Transfer_Data */
973 for (j
= 0; j
< MIN((limit
/ 2), rem
); j
++) {
974 rx
[offset
+ j
] = cpu_to_le16(qpci_io_readw(dev
, ide_bar
,
979 /* Check for final completion IRQ */
980 ide_wait_intr(qts
, IDE_PRIMARY_IRQ
);
982 /* Sanity check final state */
983 data
= ide_wait_clear(qts
, DRQ
);
984 assert_bit_set(data
, DRDY
);
985 assert_bit_clear(data
, DRQ
| ERR
| DF
| BSY
);
987 g_assert_cmpint(memcmp(pattern
, rx
, rxsize
), ==, 0);
990 test_bmdma_teardown(qts
);
991 free_pci_device(dev
);
994 static void test_cdrom_pio(void)
999 static void test_cdrom_pio_large(void)
1001 /* Test a few loops of the PIO DRQ mechanism. */
1002 cdrom_pio_impl(BYTE_COUNT_LIMIT
* 4 / ATAPI_BLOCK_SIZE
);
1006 static void test_cdrom_dma(void)
1009 static const size_t len
= ATAPI_BLOCK_SIZE
;
1011 char *pattern
= g_malloc(ATAPI_BLOCK_SIZE
* 16);
1012 char *rx
= g_malloc0(len
);
1013 uintptr_t guest_buf
;
1017 qts
= ide_test_start(
1018 "-drive if=none,file=%s,media=cdrom,format=raw,id=sr0,index=0 "
1019 "-device ide-cd,drive=sr0,bus=ide.0", tmp_path
[0]);
1020 qtest_irq_intercept_in(qts
, "ioapic");
1022 guest_buf
= guest_alloc(&guest_malloc
, len
);
1023 prdt
[0].addr
= cpu_to_le32(guest_buf
);
1024 prdt
[0].size
= cpu_to_le32(len
| PRDT_EOT
);
1026 generate_pattern(pattern
, ATAPI_BLOCK_SIZE
* 16, ATAPI_BLOCK_SIZE
);
1027 fh
= fopen(tmp_path
[0], "wb+");
1028 ret
= fwrite(pattern
, ATAPI_BLOCK_SIZE
, 16, fh
);
1029 g_assert_cmpint(ret
, ==, 16);
1032 send_dma_request(qts
, CMD_PACKET
, 0, 1, prdt
, 1, send_scsi_cdb_read10
);
1034 /* Read back data from guest memory into local qtest memory */
1035 qtest_memread(qts
, guest_buf
, rx
, len
);
1036 g_assert_cmpint(memcmp(pattern
, rx
, len
), ==, 0);
1040 test_bmdma_teardown(qts
);
1043 int main(int argc
, char **argv
)
1051 * "base" stores the starting point where we create temporary files.
1053 * On Windows, this is set to the relative path of current working
1054 * directory, because the absolute path causes the blkdebug filename
1055 * parser fail to parse "blkdebug:path/to/config:path/to/image".
1058 base
= g_get_tmp_dir();
1063 /* Create temporary blkdebug instructions */
1064 debug_path
= g_strdup_printf("%s/qtest-blkdebug.XXXXXX", base
);
1065 fd
= g_mkstemp(debug_path
);
1069 /* Create a temporary raw image */
1070 for (i
= 0; i
< 2; ++i
) {
1071 tmp_path
[i
] = g_strdup_printf("%s/qtest.XXXXXX", base
);
1072 fd
= g_mkstemp(tmp_path
[i
]);
1074 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
1080 g_test_init(&argc
, &argv
, NULL
);
1082 qtest_add_func("/ide/identify", test_identify
);
1084 qtest_add_func("/ide/diagnostic", test_diagnostic
);
1086 qtest_add_func("/ide/bmdma/simple_rw", test_bmdma_simple_rw
);
1087 qtest_add_func("/ide/bmdma/trim", test_bmdma_trim
);
1088 qtest_add_func("/ide/bmdma/various_prdts", test_bmdma_various_prdts
);
1089 qtest_add_func("/ide/bmdma/no_busmaster", test_bmdma_no_busmaster
);
1091 qtest_add_func("/ide/flush", test_flush
);
1092 qtest_add_func("/ide/flush/nodev", test_flush_nodev
);
1093 qtest_add_func("/ide/flush/empty_drive", test_flush_empty_drive
);
1094 qtest_add_func("/ide/flush/retry_pci", test_pci_retry_flush
);
1096 qtest_add_func("/ide/cdrom/pio", test_cdrom_pio
);
1097 qtest_add_func("/ide/cdrom/pio_large", test_cdrom_pio_large
);
1098 qtest_add_func("/ide/cdrom/dma", test_cdrom_dma
);
1103 for (i
= 0; i
< 2; ++i
) {
1104 unlink(tmp_path
[i
]);
1105 g_free(tmp_path
[i
]);