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
32 #include "libqos/libqos.h"
33 #include "libqos/pci-pc.h"
34 #include "libqos/malloc-pc.h"
36 #include "qemu-common.h"
37 #include "hw/pci/pci_ids.h"
38 #include "hw/pci/pci_regs.h"
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
81 CMD_FLUSH_CACHE
= 0xe7,
90 BM_CMD_WRITE
= 0x8, /* write = from device to memory */
100 PRDT_EOT
= 0x80000000,
103 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
104 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
106 static QPCIBus
*pcibus
= NULL
;
107 static QGuestAllocator
*guest_malloc
;
109 static char tmp_path
[] = "/tmp/qtest.XXXXXX";
110 static char debug_path
[] = "/tmp/qtest-blkdebug.XXXXXX";
112 static void ide_test_start(const char *cmdline_fmt
, ...)
117 va_start(ap
, cmdline_fmt
);
118 cmdline
= g_strdup_vprintf(cmdline_fmt
, ap
);
121 qtest_start(cmdline
);
122 guest_malloc
= pc_alloc_init();
127 static void ide_test_quit(void)
129 pc_alloc_uninit(guest_malloc
);
134 static QPCIDevice
*get_pci_device(uint16_t *bmdma_base
)
137 uint16_t vendor_id
, device_id
;
140 pcibus
= qpci_init_pc();
143 /* Find PCI device and verify it's the right one */
144 dev
= qpci_device_find(pcibus
, QPCI_DEVFN(IDE_PCI_DEV
, IDE_PCI_FUNC
));
145 g_assert(dev
!= NULL
);
147 vendor_id
= qpci_config_readw(dev
, PCI_VENDOR_ID
);
148 device_id
= qpci_config_readw(dev
, PCI_DEVICE_ID
);
149 g_assert(vendor_id
== PCI_VENDOR_ID_INTEL
);
150 g_assert(device_id
== PCI_DEVICE_ID_INTEL_82371SB_1
);
153 *bmdma_base
= (uint16_t)(uintptr_t) qpci_iomap(dev
, 4, NULL
);
155 qpci_device_enable(dev
);
160 static void free_pci_device(QPCIDevice
*dev
)
162 /* libqos doesn't have a function for this, so free it manually */
166 typedef struct PrdtEntry
{
169 } QEMU_PACKED PrdtEntry
;
171 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
172 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
174 static int send_dma_request(int cmd
, uint64_t sector
, int nb_sectors
,
175 PrdtEntry
*prdt
, int prdt_entries
)
179 uintptr_t guest_prdt
;
185 dev
= get_pci_device(&bmdma_base
);
198 g_assert_not_reached();
201 if (flags
& CMDF_NO_BM
) {
202 qpci_config_writew(dev
, PCI_COMMAND
,
203 PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
);
206 /* Select device 0 */
207 outb(IDE_BASE
+ reg_device
, 0 | LBA
);
209 /* Stop any running transfer, clear any pending interrupt */
210 outb(bmdma_base
+ bmreg_cmd
, 0);
211 outb(bmdma_base
+ bmreg_status
, BM_STS_INTR
);
214 len
= sizeof(*prdt
) * prdt_entries
;
215 guest_prdt
= guest_alloc(guest_malloc
, len
);
216 memwrite(guest_prdt
, prdt
, len
);
217 outl(bmdma_base
+ bmreg_prdt
, guest_prdt
);
219 /* ATA DMA command */
220 outb(IDE_BASE
+ reg_nsectors
, nb_sectors
);
222 outb(IDE_BASE
+ reg_lba_low
, sector
& 0xff);
223 outb(IDE_BASE
+ reg_lba_middle
, (sector
>> 8) & 0xff);
224 outb(IDE_BASE
+ reg_lba_high
, (sector
>> 16) & 0xff);
226 outb(IDE_BASE
+ reg_command
, cmd
);
228 /* Start DMA transfer */
229 outb(bmdma_base
+ bmreg_cmd
, BM_CMD_START
| (from_dev
? BM_CMD_WRITE
: 0));
231 if (flags
& CMDF_ABORT
) {
232 outb(bmdma_base
+ bmreg_cmd
, 0);
235 /* Wait for the DMA transfer to complete */
237 status
= inb(bmdma_base
+ bmreg_status
);
238 } while ((status
& (BM_STS_ACTIVE
| BM_STS_INTR
)) == BM_STS_ACTIVE
);
240 g_assert_cmpint(get_irq(IDE_PRIMARY_IRQ
), ==, !!(status
& BM_STS_INTR
));
242 /* Check IDE status code */
243 assert_bit_set(inb(IDE_BASE
+ reg_status
), DRDY
);
244 assert_bit_clear(inb(IDE_BASE
+ reg_status
), BSY
| DRQ
);
246 /* Reading the status register clears the IRQ */
247 g_assert(!get_irq(IDE_PRIMARY_IRQ
));
249 /* Stop DMA transfer if still active */
250 if (status
& BM_STS_ACTIVE
) {
251 outb(bmdma_base
+ bmreg_cmd
, 0);
254 free_pci_device(dev
);
259 static void test_bmdma_simple_rw(void)
265 uintptr_t guest_buf
= guest_alloc(guest_malloc
, len
);
269 .addr
= cpu_to_le32(guest_buf
),
270 .size
= cpu_to_le32(len
| PRDT_EOT
),
275 cmpbuf
= g_malloc(len
);
277 /* Write 0x55 pattern to sector 0 */
278 memset(buf
, 0x55, len
);
279 memwrite(guest_buf
, buf
, len
);
281 status
= send_dma_request(CMD_WRITE_DMA
, 0, 1, prdt
, ARRAY_SIZE(prdt
));
282 g_assert_cmphex(status
, ==, BM_STS_INTR
);
283 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
285 /* Write 0xaa pattern to sector 1 */
286 memset(buf
, 0xaa, len
);
287 memwrite(guest_buf
, buf
, len
);
289 status
= send_dma_request(CMD_WRITE_DMA
, 1, 1, prdt
, ARRAY_SIZE(prdt
));
290 g_assert_cmphex(status
, ==, BM_STS_INTR
);
291 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
293 /* Read and verify 0x55 pattern in sector 0 */
294 memset(cmpbuf
, 0x55, len
);
296 status
= send_dma_request(CMD_READ_DMA
, 0, 1, prdt
, ARRAY_SIZE(prdt
));
297 g_assert_cmphex(status
, ==, BM_STS_INTR
);
298 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
300 memread(guest_buf
, buf
, len
);
301 g_assert(memcmp(buf
, cmpbuf
, len
) == 0);
303 /* Read and verify 0xaa pattern in sector 1 */
304 memset(cmpbuf
, 0xaa, len
);
306 status
= send_dma_request(CMD_READ_DMA
, 1, 1, prdt
, ARRAY_SIZE(prdt
));
307 g_assert_cmphex(status
, ==, BM_STS_INTR
);
308 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
310 memread(guest_buf
, buf
, len
);
311 g_assert(memcmp(buf
, cmpbuf
, len
) == 0);
318 static void test_bmdma_short_prdt(void)
325 .size
= cpu_to_le32(0x10 | PRDT_EOT
),
330 status
= send_dma_request(CMD_READ_DMA
, 0, 1,
331 prdt
, ARRAY_SIZE(prdt
));
332 g_assert_cmphex(status
, ==, 0);
333 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
335 /* Abort the request before it completes */
336 status
= send_dma_request(CMD_READ_DMA
| CMDF_ABORT
, 0, 1,
337 prdt
, ARRAY_SIZE(prdt
));
338 g_assert_cmphex(status
, ==, 0);
339 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
342 static void test_bmdma_long_prdt(void)
349 .size
= cpu_to_le32(0x1000 | PRDT_EOT
),
354 status
= send_dma_request(CMD_READ_DMA
, 0, 1,
355 prdt
, ARRAY_SIZE(prdt
));
356 g_assert_cmphex(status
, ==, BM_STS_ACTIVE
| BM_STS_INTR
);
357 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
359 /* Abort the request before it completes */
360 status
= send_dma_request(CMD_READ_DMA
| CMDF_ABORT
, 0, 1,
361 prdt
, ARRAY_SIZE(prdt
));
362 g_assert_cmphex(status
, ==, BM_STS_INTR
);
363 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
366 static void test_bmdma_no_busmaster(void)
370 /* No PRDT_EOT, each entry addr 0/size 64k, and in theory qemu shouldn't be
371 * able to access it anyway because the Bus Master bit in the PCI command
372 * register isn't set. This is complete nonsense, but it used to be pretty
373 * good at confusing and occasionally crashing qemu. */
374 PrdtEntry prdt
[4096] = { };
376 status
= send_dma_request(CMD_READ_DMA
| CMDF_NO_BM
, 0, 512,
377 prdt
, ARRAY_SIZE(prdt
));
379 /* Not entirely clear what the expected result is, but this is what we get
380 * in practice. At least we want to be aware of any changes. */
381 g_assert_cmphex(status
, ==, BM_STS_ACTIVE
| BM_STS_INTR
);
382 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
385 static void test_bmdma_setup(void)
388 "-drive file=%s,if=ide,serial=%s,cache=writeback,format=raw "
389 "-global ide-hd.ver=%s",
390 tmp_path
, "testdisk", "version");
391 qtest_irq_intercept_in(global_qtest
, "ioapic");
394 static void test_bmdma_teardown(void)
399 static void string_cpu_to_be16(uint16_t *s
, size_t bytes
)
401 g_assert((bytes
& 1) == 0);
405 *s
= cpu_to_be16(*s
);
410 static void test_identify(void)
418 "-drive file=%s,if=ide,serial=%s,cache=writeback,format=raw "
419 "-global ide-hd.ver=%s",
420 tmp_path
, "testdisk", "version");
422 /* IDENTIFY command on device 0*/
423 outb(IDE_BASE
+ reg_device
, 0);
424 outb(IDE_BASE
+ reg_command
, CMD_IDENTIFY
);
426 /* Read in the IDENTIFY buffer and check registers */
427 data
= inb(IDE_BASE
+ reg_device
);
428 g_assert_cmpint(data
& DEV
, ==, 0);
430 for (i
= 0; i
< 256; i
++) {
431 data
= inb(IDE_BASE
+ reg_status
);
432 assert_bit_set(data
, DRDY
| DRQ
);
433 assert_bit_clear(data
, BSY
| DF
| ERR
);
435 ((uint16_t*) buf
)[i
] = inw(IDE_BASE
+ reg_data
);
438 data
= inb(IDE_BASE
+ reg_status
);
439 assert_bit_set(data
, DRDY
);
440 assert_bit_clear(data
, BSY
| DF
| ERR
| DRQ
);
442 /* Check serial number/version in the buffer */
443 string_cpu_to_be16(&buf
[10], 20);
444 ret
= memcmp(&buf
[10], "testdisk ", 20);
447 string_cpu_to_be16(&buf
[23], 8);
448 ret
= memcmp(&buf
[23], "version ", 8);
451 /* Write cache enabled bit */
452 assert_bit_set(buf
[85], 0x20);
457 static void test_flush(void)
462 "-drive file=blkdebug::%s,if=ide,cache=writeback,format=raw",
465 /* Delay the completion of the flush request until we explicitly do it */
466 qmp_discard_response("{'execute':'human-monitor-command', 'arguments': {"
468 " 'qemu-io ide0-hd0 \"break flush_to_os A\"'} }");
470 /* FLUSH CACHE command on device 0*/
471 outb(IDE_BASE
+ reg_device
, 0);
472 outb(IDE_BASE
+ reg_command
, CMD_FLUSH_CACHE
);
474 /* Check status while request is in flight*/
475 data
= inb(IDE_BASE
+ reg_status
);
476 assert_bit_set(data
, BSY
| DRDY
);
477 assert_bit_clear(data
, DF
| ERR
| DRQ
);
479 /* Complete the command */
480 qmp_discard_response("{'execute':'human-monitor-command', 'arguments': {"
482 " 'qemu-io ide0-hd0 \"resume A\"'} }");
484 /* Check registers */
485 data
= inb(IDE_BASE
+ reg_device
);
486 g_assert_cmpint(data
& DEV
, ==, 0);
489 data
= inb(IDE_BASE
+ reg_status
);
490 } while (data
& BSY
);
492 assert_bit_set(data
, DRDY
);
493 assert_bit_clear(data
, BSY
| DF
| ERR
| DRQ
);
498 static void test_retry_flush(const char *machine
)
503 prepare_blkdebug_script(debug_path
, "flush_to_disk");
507 "-drive file=blkdebug:%s:%s,if=ide,cache=writeback,format=raw,"
508 "rerror=stop,werror=stop",
509 debug_path
, tmp_path
);
511 /* FLUSH CACHE command on device 0*/
512 outb(IDE_BASE
+ reg_device
, 0);
513 outb(IDE_BASE
+ reg_command
, CMD_FLUSH_CACHE
);
515 /* Check status while request is in flight*/
516 data
= inb(IDE_BASE
+ reg_status
);
517 assert_bit_set(data
, BSY
| DRDY
);
518 assert_bit_clear(data
, DF
| ERR
| DRQ
);
520 qmp_eventwait("STOP");
522 /* Complete the command */
523 s
= "{'execute':'cont' }";
524 qmp_discard_response(s
);
526 /* Check registers */
527 data
= inb(IDE_BASE
+ reg_device
);
528 g_assert_cmpint(data
& DEV
, ==, 0);
531 data
= inb(IDE_BASE
+ reg_status
);
532 } while (data
& BSY
);
534 assert_bit_set(data
, DRDY
);
535 assert_bit_clear(data
, BSY
| DF
| ERR
| DRQ
);
540 static void test_flush_nodev(void)
544 /* FLUSH CACHE command on device 0*/
545 outb(IDE_BASE
+ reg_device
, 0);
546 outb(IDE_BASE
+ reg_command
, CMD_FLUSH_CACHE
);
548 /* Just testing that qemu doesn't crash... */
553 static void test_pci_retry_flush(const char *machine
)
555 test_retry_flush("pc");
558 static void test_isa_retry_flush(const char *machine
)
560 test_retry_flush("isapc");
563 int main(int argc
, char **argv
)
565 const char *arch
= qtest_get_arch();
569 /* Check architecture */
570 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
571 g_test_message("Skipping test for non-x86\n");
575 /* Create temporary blkdebug instructions */
576 fd
= mkstemp(debug_path
);
580 /* Create a temporary raw image */
581 fd
= mkstemp(tmp_path
);
583 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
588 g_test_init(&argc
, &argv
, NULL
);
590 qtest_add_func("/ide/identify", test_identify
);
592 qtest_add_func("/ide/bmdma/setup", test_bmdma_setup
);
593 qtest_add_func("/ide/bmdma/simple_rw", test_bmdma_simple_rw
);
594 qtest_add_func("/ide/bmdma/short_prdt", test_bmdma_short_prdt
);
595 qtest_add_func("/ide/bmdma/long_prdt", test_bmdma_long_prdt
);
596 qtest_add_func("/ide/bmdma/no_busmaster", test_bmdma_no_busmaster
);
597 qtest_add_func("/ide/bmdma/teardown", test_bmdma_teardown
);
599 qtest_add_func("/ide/flush", test_flush
);
600 qtest_add_func("/ide/flush/nodev", test_flush_nodev
);
601 qtest_add_func("/ide/flush/retry_pci", test_pci_retry_flush
);
602 qtest_add_func("/ide/flush/retry_isa", test_isa_retry_flush
);