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-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
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
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
[] = "/tmp/qtest.XXXXXX";
126 static char debug_path
[] = "/tmp/qtest-blkdebug.XXXXXX";
128 static void ide_test_start(const char *cmdline_fmt
, ...)
133 va_start(ap
, cmdline_fmt
);
134 cmdline
= g_strdup_vprintf(cmdline_fmt
, ap
);
137 qtest_start(cmdline
);
138 guest_malloc
= pc_alloc_init(global_qtest
);
143 static void ide_test_quit(void)
145 pc_alloc_uninit(guest_malloc
);
150 static QPCIDevice
*get_pci_device(QPCIBar
*bmdma_bar
, QPCIBar
*ide_bar
)
153 uint16_t vendor_id
, device_id
;
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
);
169 *bmdma_bar
= qpci_iomap(dev
, 4, NULL
);
171 *ide_bar
= qpci_legacy_iomap(dev
, IDE_BASE
);
173 qpci_device_enable(dev
);
178 static void free_pci_device(QPCIDevice
*dev
)
180 /* libqos doesn't have a function for this, so free it manually */
184 typedef struct PrdtEntry
{
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
))
204 QPCIBar bmdma_bar
, ide_bar
;
205 uintptr_t guest_prdt
;
211 dev
= get_pci_device(&bmdma_bar
, &ide_bar
);
219 /* Assuming we only test data reads w/ ATAPI, otherwise we need to know
220 * the SCSI command being sent in the packet, too. */
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
);
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);
254 if (cmd
== CMD_DSM
) {
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
);
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 */
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
);
302 static void test_bmdma_simple_rw(void)
305 QPCIBar bmdma_bar
, ide_bar
;
310 uintptr_t guest_buf
= guest_alloc(guest_malloc
, len
);
314 .addr
= cpu_to_le32(guest_buf
),
315 .size
= cpu_to_le32(len
| PRDT_EOT
),
319 dev
= get_pci_device(&bmdma_bar
, &ide_bar
);
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
);
368 static void test_bmdma_trim(void)
371 QPCIBar bmdma_bar
, ide_bar
;
373 const uint64_t trim_range
[] = { trim_range_le(0, 2),
375 trim_range_le(10, 1),
377 const uint64_t bad_range
= trim_range_le(TEST_IMAGE_SIZE
/ 512 - 1, 2);
380 uintptr_t guest_buf
= guest_alloc(guest_malloc
, len
);
384 .addr
= cpu_to_le32(guest_buf
),
385 .size
= cpu_to_le32(len
| PRDT_EOT
),
389 dev
= get_pci_device(&bmdma_bar
, &ide_bar
);
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
);
420 static void test_bmdma_short_prdt(void)
423 QPCIBar bmdma_bar
, ide_bar
;
429 .size
= cpu_to_le32(0x10 | PRDT_EOT
),
433 dev
= get_pci_device(&bmdma_bar
, &ide_bar
);
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)
452 QPCIBar bmdma_bar
, ide_bar
;
455 /* Read 2 sectors but only give 1 sector in PRDT */
459 .size
= cpu_to_le32(0x200 | PRDT_EOT
),
463 dev
= get_pci_device(&bmdma_bar
, &ide_bar
);
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)
482 QPCIBar bmdma_bar
, ide_bar
;
488 .size
= cpu_to_le32(0x1000 | PRDT_EOT
),
492 dev
= get_pci_device(&bmdma_bar
, &ide_bar
);
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)
511 QPCIBar bmdma_bar
, ide_bar
;
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)
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)
546 static void string_cpu_to_be16(uint16_t *s
, size_t bytes
)
548 g_assert((bytes
& 1) == 0);
552 *s
= cpu_to_be16(*s
);
557 static void test_identify(void)
560 QPCIBar bmdma_bar
, ide_bar
;
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);
598 string_cpu_to_be16(&buf
[23], 8);
599 ret
= memcmp(&buf
[23], "version ", 8);
602 /* Write cache enabled bit */
603 assert_bit_set(buf
[85], 0x20);
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
)
616 QPCIBar bmdma_bar
, ide_bar
;
622 dev
= get_pci_device(&bmdma_bar
, &ide_bar
);
624 guest_buf
= guest_alloc(guest_malloc
, len
);
626 memset(buf
, rand() % 255 + 1, len
);
630 memwrite(guest_buf
, buf
, len
);
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
);
645 free_pci_device(dev
);
648 static void test_flush(void)
651 QPCIBar bmdma_bar
, ide_bar
;
655 "-drive file=blkdebug::%s,if=ide,cache=writeback,format=raw",
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 */
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);
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
);
692 free_pci_device(dev
);
695 static void test_retry_flush(const char *machine
)
698 QPCIBar bmdma_bar
, ide_bar
;
701 prepare_blkdebug_script(debug_path
, "flush_to_disk");
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 */
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);
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
);
741 free_pci_device(dev
);
744 static void test_flush_nodev(void)
747 QPCIBar bmdma_bar
, ide_bar
;
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
);
763 static void test_flush_empty_drive(void)
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
);
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
{
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 };
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 */
813 pkt
.lba
= cpu_to_be32(lba
);
814 pkt
.nblocks
= cpu_to_be16(nblocks
);
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
);
830 static uint8_t ide_wait_clear(uint8_t flag
)
833 QPCIBar bmdma_bar
, ide_bar
;
837 dev
= get_pci_device(&bmdma_bar
, &ide_bar
);
839 /* Wait with a 5 second timeout */
842 data
= qpci_io_readb(dev
, ide_bar
, reg_status
);
843 if (!(data
& flag
)) {
844 free_pci_device(dev
);
847 if (difftime(time(NULL
), st
) > 5.0) {
852 g_assert_not_reached();
855 static void ide_wait_intr(int irq
)
866 if (difftime(time(NULL
), st
) > 5.0) {
872 g_assert_not_reached();
875 static void cdrom_pio_impl(int nblocks
)
878 QPCIBar bmdma_bar
, ide_bar
;
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
);
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
);
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 */
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
,
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);
955 test_bmdma_teardown();
956 free_pci_device(dev
);
959 static void test_cdrom_pio(void)
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
;
975 char *pattern
= g_malloc(ATAPI_BLOCK_SIZE
* 16);
976 char *rx
= g_malloc0(len
);
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);
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);
1003 test_bmdma_teardown();
1006 int main(int argc
, char **argv
)
1008 const char *arch
= qtest_get_arch();
1012 /* Check architecture */
1013 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
1014 g_test_message("Skipping test for non-x86\n");
1018 /* Create temporary blkdebug instructions */
1019 fd
= mkstemp(debug_path
);
1023 /* Create a temporary raw image */
1024 fd
= mkstemp(tmp_path
);
1026 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
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
);