4 * Copyright (c) 2014 John Snow <jsnow@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-pc.h"
33 #include "libqos/ahci.h"
34 #include "libqos/pci-pc.h"
36 #include "qemu-common.h"
37 #include "qemu/host-utils.h"
39 #include "hw/pci/pci_ids.h"
40 #include "hw/pci/pci_regs.h"
42 /* Test images sizes in MB */
43 #define TEST_IMAGE_SIZE_MB_LARGE (200 * 1024)
44 #define TEST_IMAGE_SIZE_MB_SMALL 64
47 static char tmp_path
[] = "/tmp/qtest.XXXXXX";
48 static char debug_path
[] = "/tmp/qtest-blkdebug.XXXXXX";
49 static char mig_socket
[] = "/tmp/qtest-migration.XXXXXX";
50 static bool ahci_pedantic
;
51 static const char *imgfmt
;
52 static unsigned test_image_size_mb
;
54 /*** Function Declarations ***/
55 static void ahci_test_port_spec(AHCIQState
*ahci
, uint8_t port
);
56 static void ahci_test_pci_spec(AHCIQState
*ahci
);
57 static void ahci_test_pci_caps(AHCIQState
*ahci
, uint16_t header
,
59 static void ahci_test_satacap(AHCIQState
*ahci
, uint8_t offset
);
60 static void ahci_test_msicap(AHCIQState
*ahci
, uint8_t offset
);
61 static void ahci_test_pmcap(AHCIQState
*ahci
, uint8_t offset
);
65 static uint64_t mb_to_sectors(uint64_t image_size_mb
)
67 return (image_size_mb
* 1024 * 1024) / AHCI_SECTOR_SIZE
;
70 static void string_bswap16(uint16_t *s
, size_t bytes
)
72 g_assert_cmphex((bytes
& 1), ==, 0);
82 * Verify that the transfer did not corrupt our state at all.
84 static void verify_state(AHCIQState
*ahci
)
87 uint32_t ahci_fingerprint
;
90 AHCICommandHeader cmd
;
92 ahci_fingerprint
= qpci_config_readl(ahci
->dev
, PCI_VENDOR_ID
);
93 g_assert_cmphex(ahci_fingerprint
, ==, ahci
->fingerprint
);
95 /* If we haven't initialized, this is as much as can be validated. */
96 if (!ahci
->hba_base
) {
100 hba_base
= (uint64_t)qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
101 hba_stored
= (uint64_t)(uintptr_t)ahci
->hba_base
;
102 g_assert_cmphex(hba_base
, ==, hba_stored
);
104 g_assert_cmphex(ahci_rreg(ahci
, AHCI_CAP
), ==, ahci
->cap
);
105 g_assert_cmphex(ahci_rreg(ahci
, AHCI_CAP2
), ==, ahci
->cap2
);
107 for (i
= 0; i
< 32; i
++) {
108 g_assert_cmphex(ahci_px_rreg(ahci
, i
, AHCI_PX_FB
), ==,
110 g_assert_cmphex(ahci_px_rreg(ahci
, i
, AHCI_PX_CLB
), ==,
112 for (j
= 0; j
< 32; j
++) {
113 ahci_get_command_header(ahci
, i
, j
, &cmd
);
114 g_assert_cmphex(cmd
.prdtl
, ==, ahci
->port
[i
].prdtl
[j
]);
115 g_assert_cmphex(cmd
.ctba
, ==, ahci
->port
[i
].ctba
[j
]);
120 static void ahci_migrate(AHCIQState
*from
, AHCIQState
*to
, const char *uri
)
122 QOSState
*tmp
= to
->parent
;
123 QPCIDevice
*dev
= to
->dev
;
124 char *uri_local
= NULL
;
127 uri_local
= g_strdup_printf("%s%s", "unix:", mig_socket
);
131 /* context will be 'to' after completion. */
132 migrate(from
->parent
, to
->parent
, uri
);
134 /* We'd like for the AHCIState objects to still point
135 * to information specific to its specific parent
136 * instance, but otherwise just inherit the new data. */
137 memcpy(to
, from
, sizeof(AHCIQState
));
143 memset(from
, 0x00, sizeof(AHCIQState
));
151 /*** Test Setup & Teardown ***/
154 * Start a Q35 machine and bookmark a handle to the AHCI device.
156 static AHCIQState
*ahci_vboot(const char *cli
, va_list ap
)
160 s
= g_malloc0(sizeof(AHCIQState
));
161 s
->parent
= qtest_pc_vboot(cli
, ap
);
162 alloc_set_flags(s
->parent
->alloc
, ALLOC_LEAK_ASSERT
);
164 /* Verify that we have an AHCI device present. */
165 s
->dev
= get_ahci_device(&s
->fingerprint
);
171 * Start a Q35 machine and bookmark a handle to the AHCI device.
173 static AHCIQState
*ahci_boot(const char *cli
, ...)
180 s
= ahci_vboot(cli
, ap
);
183 cli
= "-drive if=none,id=drive0,file=%s,cache=writeback,serial=%s"
186 "-device ide-hd,drive=drive0 "
187 "-global ide-hd.ver=%s";
188 s
= ahci_boot(cli
, tmp_path
, "testdisk", imgfmt
, "version");
195 * Clean up the PCI device, then terminate the QEMU instance.
197 static void ahci_shutdown(AHCIQState
*ahci
)
199 QOSState
*qs
= ahci
->parent
;
202 ahci_clean_mem(ahci
);
203 free_ahci_device(ahci
->dev
);
209 * Boot and fully enable the HBA device.
210 * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
212 static AHCIQState
*ahci_boot_and_enable(const char *cli
, ...)
221 ahci
= ahci_vboot(cli
, ap
);
224 ahci
= ahci_boot(NULL
);
227 ahci_pci_enable(ahci
);
228 ahci_hba_enable(ahci
);
229 /* Initialize test device */
230 port
= ahci_port_select(ahci
);
231 ahci_port_clear(ahci
, port
);
232 ahci_io(ahci
, port
, CMD_IDENTIFY
, &buff
, sizeof(buff
), 0);
237 /*** Specification Adherence Tests ***/
240 * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
242 static void ahci_test_pci_spec(AHCIQState
*ahci
)
248 /* Most of these bits should start cleared until we turn them on. */
249 data
= qpci_config_readw(ahci
->dev
, PCI_COMMAND
);
250 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_MEMORY
);
251 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_MASTER
);
252 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_SPECIAL
); /* Reserved */
253 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_VGA_PALETTE
); /* Reserved */
254 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_PARITY
);
255 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_WAIT
); /* Reserved */
256 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_SERR
);
257 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_FAST_BACK
);
258 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_INTX_DISABLE
);
259 ASSERT_BIT_CLEAR(data
, 0xF800); /* Reserved */
261 data
= qpci_config_readw(ahci
->dev
, PCI_STATUS
);
262 ASSERT_BIT_CLEAR(data
, 0x01 | 0x02 | 0x04); /* Reserved */
263 ASSERT_BIT_CLEAR(data
, PCI_STATUS_INTERRUPT
);
264 ASSERT_BIT_SET(data
, PCI_STATUS_CAP_LIST
); /* must be set */
265 ASSERT_BIT_CLEAR(data
, PCI_STATUS_UDF
); /* Reserved */
266 ASSERT_BIT_CLEAR(data
, PCI_STATUS_PARITY
);
267 ASSERT_BIT_CLEAR(data
, PCI_STATUS_SIG_TARGET_ABORT
);
268 ASSERT_BIT_CLEAR(data
, PCI_STATUS_REC_TARGET_ABORT
);
269 ASSERT_BIT_CLEAR(data
, PCI_STATUS_REC_MASTER_ABORT
);
270 ASSERT_BIT_CLEAR(data
, PCI_STATUS_SIG_SYSTEM_ERROR
);
271 ASSERT_BIT_CLEAR(data
, PCI_STATUS_DETECTED_PARITY
);
273 /* RID occupies the low byte, CCs occupy the high three. */
274 datal
= qpci_config_readl(ahci
->dev
, PCI_CLASS_REVISION
);
276 /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
277 * Though in practice this is likely seldom true. */
278 ASSERT_BIT_CLEAR(datal
, 0xFF);
281 /* BCC *must* equal 0x01. */
282 g_assert_cmphex(PCI_BCC(datal
), ==, 0x01);
283 if (PCI_SCC(datal
) == 0x01) {
285 ASSERT_BIT_SET(0x80000000, datal
);
286 ASSERT_BIT_CLEAR(0x60000000, datal
);
287 } else if (PCI_SCC(datal
) == 0x04) {
289 g_assert_cmphex(PCI_PI(datal
), ==, 0);
290 } else if (PCI_SCC(datal
) == 0x06) {
292 g_assert_cmphex(PCI_PI(datal
), ==, 0x01);
294 g_assert_not_reached();
297 datab
= qpci_config_readb(ahci
->dev
, PCI_CACHE_LINE_SIZE
);
298 g_assert_cmphex(datab
, ==, 0);
300 datab
= qpci_config_readb(ahci
->dev
, PCI_LATENCY_TIMER
);
301 g_assert_cmphex(datab
, ==, 0);
303 /* Only the bottom 7 bits must be off. */
304 datab
= qpci_config_readb(ahci
->dev
, PCI_HEADER_TYPE
);
305 ASSERT_BIT_CLEAR(datab
, 0x7F);
307 /* BIST is optional, but the low 7 bits must always start off regardless. */
308 datab
= qpci_config_readb(ahci
->dev
, PCI_BIST
);
309 ASSERT_BIT_CLEAR(datab
, 0x7F);
311 /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
312 datal
= qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
313 g_assert_cmphex(datal
, ==, 0);
315 qpci_config_writel(ahci
->dev
, PCI_BASE_ADDRESS_5
, 0xFFFFFFFF);
316 datal
= qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
317 /* ABAR must be 32-bit, memory mapped, non-prefetchable and
318 * must be >= 512 bytes. To that end, bits 0-8 must be off. */
319 ASSERT_BIT_CLEAR(datal
, 0xFF);
321 /* Capability list MUST be present, */
322 datal
= qpci_config_readl(ahci
->dev
, PCI_CAPABILITY_LIST
);
323 /* But these bits are reserved. */
324 ASSERT_BIT_CLEAR(datal
, ~0xFF);
325 g_assert_cmphex(datal
, !=, 0);
327 /* Check specification adherence for capability extenstions. */
328 data
= qpci_config_readw(ahci
->dev
, datal
);
330 switch (ahci
->fingerprint
) {
331 case AHCI_INTEL_ICH9
:
332 /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
333 g_assert_cmphex((data
& 0xFF), ==, PCI_CAP_ID_MSI
);
336 /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
337 g_assert_cmphex((data
& 0xFF), ==, PCI_CAP_ID_PM
);
340 ahci_test_pci_caps(ahci
, data
, (uint8_t)datal
);
343 datal
= qpci_config_readl(ahci
->dev
, PCI_CAPABILITY_LIST
+ 4);
344 g_assert_cmphex(datal
, ==, 0);
346 /* IPIN might vary, but ILINE must be off. */
347 datab
= qpci_config_readb(ahci
->dev
, PCI_INTERRUPT_LINE
);
348 g_assert_cmphex(datab
, ==, 0);
352 * Test PCI capabilities for AHCI specification adherence.
354 static void ahci_test_pci_caps(AHCIQState
*ahci
, uint16_t header
,
357 uint8_t cid
= header
& 0xFF;
358 uint8_t next
= header
>> 8;
360 g_test_message("CID: %02x; next: %02x", cid
, next
);
364 ahci_test_pmcap(ahci
, offset
);
367 ahci_test_msicap(ahci
, offset
);
369 case PCI_CAP_ID_SATA
:
370 ahci_test_satacap(ahci
, offset
);
374 g_test_message("Unknown CAP 0x%02x", cid
);
378 ahci_test_pci_caps(ahci
, qpci_config_readw(ahci
->dev
, next
), next
);
383 * Test SATA PCI capabilitity for AHCI specification adherence.
385 static void ahci_test_satacap(AHCIQState
*ahci
, uint8_t offset
)
390 g_test_message("Verifying SATACAP");
392 /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
393 dataw
= qpci_config_readw(ahci
->dev
, offset
+ 2);
394 g_assert_cmphex(dataw
, ==, 0x10);
396 /* Grab the SATACR1 register. */
397 datal
= qpci_config_readw(ahci
->dev
, offset
+ 4);
399 switch (datal
& 0x0F) {
400 case 0x04: /* BAR0 */
401 case 0x05: /* BAR1 */
405 case 0x09: /* BAR5 */
406 case 0x0F: /* Immediately following SATACR1 in PCI config space. */
409 /* Invalid BARLOC for the Index Data Pair. */
410 g_assert_not_reached();
414 g_assert_cmphex((datal
>> 24), ==, 0x00);
418 * Test MSI PCI capability for AHCI specification adherence.
420 static void ahci_test_msicap(AHCIQState
*ahci
, uint8_t offset
)
425 g_test_message("Verifying MSICAP");
427 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_FLAGS
);
428 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_ENABLE
);
429 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_QSIZE
);
430 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_RESERVED
);
432 datal
= qpci_config_readl(ahci
->dev
, offset
+ PCI_MSI_ADDRESS_LO
);
433 g_assert_cmphex(datal
, ==, 0);
435 if (dataw
& PCI_MSI_FLAGS_64BIT
) {
436 g_test_message("MSICAP is 64bit");
437 datal
= qpci_config_readl(ahci
->dev
, offset
+ PCI_MSI_ADDRESS_HI
);
438 g_assert_cmphex(datal
, ==, 0);
439 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_DATA_64
);
440 g_assert_cmphex(dataw
, ==, 0);
442 g_test_message("MSICAP is 32bit");
443 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_DATA_32
);
444 g_assert_cmphex(dataw
, ==, 0);
449 * Test Power Management PCI capability for AHCI specification adherence.
451 static void ahci_test_pmcap(AHCIQState
*ahci
, uint8_t offset
)
455 g_test_message("Verifying PMCAP");
457 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_PM_PMC
);
458 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_PME_CLOCK
);
459 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_RESERVED
);
460 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_D1
);
461 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_D2
);
463 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_PM_CTRL
);
464 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_STATE_MASK
);
465 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_RESERVED
);
466 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_DATA_SEL_MASK
);
467 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_DATA_SCALE_MASK
);
470 static void ahci_test_hba_spec(AHCIQState
*ahci
)
478 g_assert(ahci
!= NULL
);
481 * Note that the AHCI spec does expect the BIOS to set up a few things:
482 * CAP.SSS - Support for staggered spin-up (t/f)
483 * CAP.SMPS - Support for mechanical presence switches (t/f)
484 * PI - Ports Implemented (1-32)
485 * PxCMD.HPCP - Hot Plug Capable Port
486 * PxCMD.MPSP - Mechanical Presence Switch Present
487 * PxCMD.CPD - Cold Presence Detection support
489 * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
490 * Foreach Port Implemented:
491 * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
492 * -PxCLB/U and PxFB/U are set to valid regions in memory
493 * -PxSUD is set to 1.
494 * -PxSSTS.DET is polled for presence; if detected, we continue:
495 * -PxSERR is cleared with 1's.
496 * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
497 * the device is ready.
500 /* 1 CAP - Capabilities Register */
501 ahci
->cap
= ahci_rreg(ahci
, AHCI_CAP
);
502 ASSERT_BIT_CLEAR(ahci
->cap
, AHCI_CAP_RESERVED
);
504 /* 2 GHC - Global Host Control */
505 reg
= ahci_rreg(ahci
, AHCI_GHC
);
506 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_HR
);
507 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_IE
);
508 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_MRSM
);
509 if (BITSET(ahci
->cap
, AHCI_CAP_SAM
)) {
510 g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
511 ASSERT_BIT_SET(reg
, AHCI_GHC_AE
);
513 g_test_message("Supports AHCI/Legacy mix.");
514 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_AE
);
517 /* 3 IS - Interrupt Status */
518 reg
= ahci_rreg(ahci
, AHCI_IS
);
519 g_assert_cmphex(reg
, ==, 0);
521 /* 4 PI - Ports Implemented */
522 ports
= ahci_rreg(ahci
, AHCI_PI
);
523 /* Ports Implemented must be non-zero. */
524 g_assert_cmphex(ports
, !=, 0);
525 /* Ports Implemented must be <= Number of Ports. */
526 nports_impl
= ctpopl(ports
);
527 g_assert_cmpuint(((AHCI_CAP_NP
& ahci
->cap
) + 1), >=, nports_impl
);
529 /* Ports must be within the proper range. Given a mapping of SIZE,
530 * 256 bytes are used for global HBA control, and the rest is used
531 * for ports data, at 0x80 bytes each. */
532 g_assert_cmphex(ahci
->barsize
, >, 0);
533 maxports
= (ahci
->barsize
- HBA_DATA_REGION_SIZE
) / HBA_PORT_DATA_SIZE
;
534 /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
535 g_assert_cmphex((reg
>> maxports
), ==, 0);
538 reg
= ahci_rreg(ahci
, AHCI_VS
);
540 case AHCI_VERSION_0_95
:
541 case AHCI_VERSION_1_0
:
542 case AHCI_VERSION_1_1
:
543 case AHCI_VERSION_1_2
:
544 case AHCI_VERSION_1_3
:
547 g_assert_not_reached();
550 /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
551 reg
= ahci_rreg(ahci
, AHCI_CCCCTL
);
552 if (BITSET(ahci
->cap
, AHCI_CAP_CCCS
)) {
553 ASSERT_BIT_CLEAR(reg
, AHCI_CCCCTL_EN
);
554 ASSERT_BIT_CLEAR(reg
, AHCI_CCCCTL_RESERVED
);
555 ASSERT_BIT_SET(reg
, AHCI_CCCCTL_CC
);
556 ASSERT_BIT_SET(reg
, AHCI_CCCCTL_TV
);
558 g_assert_cmphex(reg
, ==, 0);
562 reg
= ahci_rreg(ahci
, AHCI_CCCPORTS
);
563 /* Must be zeroes initially regardless of CAP.CCCS */
564 g_assert_cmphex(reg
, ==, 0);
567 reg
= ahci_rreg(ahci
, AHCI_EMLOC
);
568 if (BITCLR(ahci
->cap
, AHCI_CAP_EMS
)) {
569 g_assert_cmphex(reg
, ==, 0);
573 reg
= ahci_rreg(ahci
, AHCI_EMCTL
);
574 if (BITSET(ahci
->cap
, AHCI_CAP_EMS
)) {
575 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_STSMR
);
576 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_CTLTM
);
577 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_CTLRST
);
578 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_RESERVED
);
580 g_assert_cmphex(reg
, ==, 0);
583 /* 10 CAP2 -- Capabilities Extended */
584 ahci
->cap2
= ahci_rreg(ahci
, AHCI_CAP2
);
585 ASSERT_BIT_CLEAR(ahci
->cap2
, AHCI_CAP2_RESERVED
);
587 /* 11 BOHC -- Bios/OS Handoff Control */
588 reg
= ahci_rreg(ahci
, AHCI_BOHC
);
589 g_assert_cmphex(reg
, ==, 0);
591 /* 12 -- 23: Reserved */
592 g_test_message("Verifying HBA reserved area is empty.");
593 for (i
= AHCI_RESERVED
; i
< AHCI_NVMHCI
; ++i
) {
594 reg
= ahci_rreg(ahci
, i
);
595 g_assert_cmphex(reg
, ==, 0);
598 /* 24 -- 39: NVMHCI */
599 if (BITCLR(ahci
->cap2
, AHCI_CAP2_NVMP
)) {
600 g_test_message("Verifying HBA/NVMHCI area is empty.");
601 for (i
= AHCI_NVMHCI
; i
< AHCI_VENDOR
; ++i
) {
602 reg
= ahci_rreg(ahci
, i
);
603 g_assert_cmphex(reg
, ==, 0);
607 /* 40 -- 63: Vendor */
608 g_test_message("Verifying HBA/Vendor area is empty.");
609 for (i
= AHCI_VENDOR
; i
< AHCI_PORTS
; ++i
) {
610 reg
= ahci_rreg(ahci
, i
);
611 g_assert_cmphex(reg
, ==, 0);
614 /* 64 -- XX: Port Space */
615 for (i
= 0; ports
|| (i
< maxports
); ports
>>= 1, ++i
) {
616 if (BITSET(ports
, 0x1)) {
617 g_test_message("Testing port %u for spec", i
);
618 ahci_test_port_spec(ahci
, i
);
621 uint16_t low
= AHCI_PORTS
+ (32 * i
);
622 uint16_t high
= AHCI_PORTS
+ (32 * (i
+ 1));
623 g_test_message("Asserting unimplemented port %u "
624 "(reg [%u-%u]) is empty.",
626 for (j
= low
; j
< high
; ++j
) {
627 reg
= ahci_rreg(ahci
, j
);
628 g_assert_cmphex(reg
, ==, 0);
635 * Test the memory space for one port for specification adherence.
637 static void ahci_test_port_spec(AHCIQState
*ahci
, uint8_t port
)
643 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CLB
);
644 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CLB_RESERVED
);
647 if (BITCLR(ahci
->cap
, AHCI_CAP_S64A
)) {
648 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CLBU
);
649 g_assert_cmphex(reg
, ==, 0);
653 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FB
);
654 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FB_RESERVED
);
657 if (BITCLR(ahci
->cap
, AHCI_CAP_S64A
)) {
658 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FBU
);
659 g_assert_cmphex(reg
, ==, 0);
663 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_IS
);
664 g_assert_cmphex(reg
, ==, 0);
667 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_IE
);
668 g_assert_cmphex(reg
, ==, 0);
671 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CMD
);
672 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FRE
);
673 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_RESERVED
);
674 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CCS
);
675 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FR
);
676 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CR
);
677 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_PMA
); /* And RW only if CAP.SPM */
678 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_APSTE
); /* RW only if CAP2.APST */
679 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ATAPI
);
680 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_DLAE
);
681 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ALPE
); /* RW only if CAP.SALP */
682 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ASP
); /* RW only if CAP.SALP */
683 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ICC
);
684 /* If CPDetect support does not exist, CPState must be off. */
685 if (BITCLR(reg
, AHCI_PX_CMD_CPD
)) {
686 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CPS
);
688 /* If MPSPresence is not set, MPSState must be off. */
689 if (BITCLR(reg
, AHCI_PX_CMD_MPSP
)) {
690 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSS
);
692 /* If we do not support MPS, MPSS and MPSP must be off. */
693 if (BITCLR(ahci
->cap
, AHCI_CAP_SMPS
)) {
694 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSS
);
695 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSP
);
697 /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
698 if (BITANY(reg
, AHCI_PX_CMD_CPD
| AHCI_PX_CMD_MPSP
)) {
699 ASSERT_BIT_SET(reg
, AHCI_PX_CMD_HPCP
);
701 /* HPCP and ESP cannot both be active. */
702 g_assert(!BITSET(reg
, AHCI_PX_CMD_HPCP
| AHCI_PX_CMD_ESP
));
703 /* If CAP.FBSS is not set, FBSCP must not be set. */
704 if (BITCLR(ahci
->cap
, AHCI_CAP_FBSS
)) {
705 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FBSCP
);
709 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_RES1
);
710 g_assert_cmphex(reg
, ==, 0);
713 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_TFD
);
714 /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
715 * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
716 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_ERR
);
717 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_CS1
);
718 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_DRQ
);
719 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_CS2
);
720 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_STS_BSY
);
721 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_ERR
);
722 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_RESERVED
);
725 /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
726 * Even when GHC.ST is zero, the AHCI HBA may receive the initial
727 * D2H register FIS and update the signature asynchronously,
728 * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
730 /* (10) SSTS / SCR0: SStatus */
731 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SSTS
);
732 ASSERT_BIT_CLEAR(reg
, AHCI_PX_SSTS_RESERVED
);
733 /* Even though the register should be 0 at boot, it is asynchronous and
734 * prone to change, so we cannot test any well known value. */
736 /* (11) SCTL / SCR2: SControl */
737 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SCTL
);
738 g_assert_cmphex(reg
, ==, 0);
740 /* (12) SERR / SCR1: SError */
741 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SERR
);
742 g_assert_cmphex(reg
, ==, 0);
744 /* (13) SACT / SCR3: SActive */
745 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SACT
);
746 g_assert_cmphex(reg
, ==, 0);
749 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CI
);
750 g_assert_cmphex(reg
, ==, 0);
753 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SNTF
);
754 g_assert_cmphex(reg
, ==, 0);
757 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FBS
);
758 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_EN
);
759 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DEC
);
760 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_SDE
);
761 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DEV
);
762 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DWE
);
763 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_RESERVED
);
764 if (BITSET(ahci
->cap
, AHCI_CAP_FBSS
)) {
765 /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
766 g_assert((reg
& AHCI_PX_FBS_ADO
) >> ctzl(AHCI_PX_FBS_ADO
) >= 2);
769 /* [17 -- 27] RESERVED */
770 for (i
= AHCI_PX_RES2
; i
< AHCI_PX_VS
; ++i
) {
771 reg
= ahci_px_rreg(ahci
, port
, i
);
772 g_assert_cmphex(reg
, ==, 0);
775 /* [28 -- 31] Vendor-Specific */
776 for (i
= AHCI_PX_VS
; i
< 32; ++i
) {
777 reg
= ahci_px_rreg(ahci
, port
, i
);
779 g_test_message("INFO: Vendor register %u non-empty", i
);
785 * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
786 * device we see, then read and check the response.
788 static void ahci_test_identify(AHCIQState
*ahci
)
794 const size_t buffsize
= 512;
796 g_assert(ahci
!= NULL
);
799 * This serves as a bit of a tutorial on AHCI device programming:
801 * (1) Create a data buffer for the IDENTIFY response to be sent to
802 * (2) Create a Command Table buffer, where we will store the
803 * command and PRDT (Physical Region Descriptor Table)
804 * (3) Construct an FIS host-to-device command structure, and write it to
805 * the top of the Command Table buffer.
806 * (4) Create one or more Physical Region Descriptors (PRDs) that describe
807 * a location in memory where data may be stored/retrieved.
808 * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
809 * (6) Each AHCI port has up to 32 command slots. Each slot contains a
810 * header that points to a Command Table buffer. Pick an unused slot
811 * and update it to point to the Command Table we have built.
812 * (7) Now: Command #n points to our Command Table, and our Command Table
813 * contains the FIS (that describes our command) and the PRDTL, which
814 * describes our buffer.
815 * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
816 * #n is ready for processing.
819 /* Pick the first implemented and running port */
820 px
= ahci_port_select(ahci
);
821 g_test_message("Selected port %u for test", px
);
823 /* Clear out the FIS Receive area and any pending interrupts. */
824 ahci_port_clear(ahci
, px
);
826 /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
827 ahci_io(ahci
, px
, CMD_IDENTIFY
, &buff
, buffsize
, 0);
829 /* Check serial number/version in the buffer */
830 /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
831 * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
832 * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
833 * as a consequence, only needs to unchunk the data on LE machines. */
834 string_bswap16(&buff
[10], 20);
835 rc
= memcmp(&buff
[10], "testdisk ", 20);
836 g_assert_cmphex(rc
, ==, 0);
838 string_bswap16(&buff
[23], 8);
839 rc
= memcmp(&buff
[23], "version ", 8);
840 g_assert_cmphex(rc
, ==, 0);
842 sect_size
= le16_to_cpu(*((uint16_t *)(&buff
[5])));
843 g_assert_cmphex(sect_size
, ==, AHCI_SECTOR_SIZE
);
846 static void ahci_test_io_rw_simple(AHCIQState
*ahci
, unsigned bufsize
,
847 uint64_t sector
, uint8_t read_cmd
,
852 unsigned char *tx
= g_malloc(bufsize
);
853 unsigned char *rx
= g_malloc0(bufsize
);
855 g_assert(ahci
!= NULL
);
857 /* Pick the first running port and clear it. */
858 port
= ahci_port_select(ahci
);
859 ahci_port_clear(ahci
, port
);
861 /*** Create pattern and transfer to guest ***/
862 /* Data buffer in the guest */
863 ptr
= ahci_alloc(ahci
, bufsize
);
866 /* Write some indicative pattern to our buffer. */
867 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
868 bufwrite(ptr
, tx
, bufsize
);
870 /* Write this buffer to disk, then read it back to the DMA buffer. */
871 ahci_guest_io(ahci
, port
, write_cmd
, ptr
, bufsize
, sector
);
872 qmemset(ptr
, 0x00, bufsize
);
873 ahci_guest_io(ahci
, port
, read_cmd
, ptr
, bufsize
, sector
);
875 /*** Read back the Data ***/
876 bufread(ptr
, rx
, bufsize
);
877 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
879 ahci_free(ahci
, ptr
);
884 static uint8_t ahci_test_nondata(AHCIQState
*ahci
, uint8_t ide_cmd
)
890 port
= ahci_port_select(ahci
);
891 ahci_port_clear(ahci
, port
);
894 cmd
= ahci_command_create(ide_cmd
);
895 ahci_command_commit(ahci
, cmd
, port
);
896 ahci_command_issue(ahci
, cmd
);
897 ahci_command_verify(ahci
, cmd
);
898 ahci_command_free(cmd
);
903 static void ahci_test_flush(AHCIQState
*ahci
)
905 ahci_test_nondata(ahci
, CMD_FLUSH_CACHE
);
908 static void ahci_test_max(AHCIQState
*ahci
)
910 RegD2HFIS
*d2h
= g_malloc0(0x20);
914 uint64_t config_sect
= mb_to_sectors(test_image_size_mb
) - 1;
916 if (config_sect
> 0xFFFFFF) {
917 cmd
= CMD_READ_MAX_EXT
;
922 port
= ahci_test_nondata(ahci
, cmd
);
923 memread(ahci
->port
[port
].fb
+ 0x40, d2h
, 0x20);
924 nsect
= (uint64_t)d2h
->lba_hi
[2] << 40 |
925 (uint64_t)d2h
->lba_hi
[1] << 32 |
926 (uint64_t)d2h
->lba_hi
[0] << 24 |
927 (uint64_t)d2h
->lba_lo
[2] << 16 |
928 (uint64_t)d2h
->lba_lo
[1] << 8 |
929 (uint64_t)d2h
->lba_lo
[0];
931 g_assert_cmphex(nsect
, ==, config_sect
);
936 /******************************************************************************/
937 /* Test Interfaces */
938 /******************************************************************************/
941 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
943 static void test_sanity(void)
946 ahci
= ahci_boot(NULL
);
951 * Ensure that the PCI configuration space for the AHCI device is in-line with
952 * the AHCI 1.3 specification for initial values.
954 static void test_pci_spec(void)
957 ahci
= ahci_boot(NULL
);
958 ahci_test_pci_spec(ahci
);
963 * Engage the PCI AHCI device and sanity check the response.
964 * Perform additional PCI config space bringup for the HBA.
966 static void test_pci_enable(void)
969 ahci
= ahci_boot(NULL
);
970 ahci_pci_enable(ahci
);
975 * Investigate the memory mapped regions of the HBA,
976 * and test them for AHCI specification adherence.
978 static void test_hba_spec(void)
982 ahci
= ahci_boot(NULL
);
983 ahci_pci_enable(ahci
);
984 ahci_test_hba_spec(ahci
);
989 * Engage the HBA functionality of the AHCI PCI device,
990 * and bring it into a functional idle state.
992 static void test_hba_enable(void)
996 ahci
= ahci_boot(NULL
);
997 ahci_pci_enable(ahci
);
998 ahci_hba_enable(ahci
);
1003 * Bring up the device and issue an IDENTIFY command.
1004 * Inspect the state of the HBA device and the data returned.
1006 static void test_identify(void)
1010 ahci
= ahci_boot_and_enable(NULL
);
1011 ahci_test_identify(ahci
);
1012 ahci_shutdown(ahci
);
1016 * Fragmented DMA test: Perform a standard 4K DMA read/write
1017 * test, but make sure the physical regions are fragmented to
1018 * be very small, each just 32 bytes, to see how AHCI performs
1019 * with chunks defined to be much less than a sector.
1021 static void test_dma_fragmented(void)
1026 size_t bufsize
= 4096;
1027 unsigned char *tx
= g_malloc(bufsize
);
1028 unsigned char *rx
= g_malloc0(bufsize
);
1031 ahci
= ahci_boot_and_enable(NULL
);
1032 px
= ahci_port_select(ahci
);
1033 ahci_port_clear(ahci
, px
);
1035 /* create pattern */
1036 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
1038 /* Create a DMA buffer in guest memory, and write our pattern to it. */
1039 ptr
= guest_alloc(ahci
->parent
->alloc
, bufsize
);
1041 bufwrite(ptr
, tx
, bufsize
);
1043 cmd
= ahci_command_create(CMD_WRITE_DMA
);
1044 ahci_command_adjust(cmd
, 0, ptr
, bufsize
, 32);
1045 ahci_command_commit(ahci
, cmd
, px
);
1046 ahci_command_issue(ahci
, cmd
);
1047 ahci_command_verify(ahci
, cmd
);
1050 cmd
= ahci_command_create(CMD_READ_DMA
);
1051 ahci_command_adjust(cmd
, 0, ptr
, bufsize
, 32);
1052 ahci_command_commit(ahci
, cmd
, px
);
1053 ahci_command_issue(ahci
, cmd
);
1054 ahci_command_verify(ahci
, cmd
);
1057 /* Read back the guest's receive buffer into local memory */
1058 bufread(ptr
, rx
, bufsize
);
1059 guest_free(ahci
->parent
->alloc
, ptr
);
1061 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1063 ahci_shutdown(ahci
);
1069 static void test_flush(void)
1073 ahci
= ahci_boot_and_enable(NULL
);
1074 ahci_test_flush(ahci
);
1075 ahci_shutdown(ahci
);
1078 static void test_flush_retry(void)
1085 prepare_blkdebug_script(debug_path
, "flush_to_disk");
1086 ahci
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1087 "format=%s,cache=writeback,"
1088 "rerror=stop,werror=stop "
1090 "-device ide-hd,drive=drive0 ",
1094 /* Issue Flush Command and wait for error */
1095 port
= ahci_port_select(ahci
);
1096 ahci_port_clear(ahci
, port
);
1097 cmd
= ahci_command_create(CMD_FLUSH_CACHE
);
1098 ahci_command_commit(ahci
, cmd
, port
);
1099 ahci_command_issue_async(ahci
, cmd
);
1100 qmp_eventwait("STOP");
1102 /* Complete the command */
1103 s
= "{'execute':'cont' }";
1105 qmp_eventwait("RESUME");
1106 ahci_command_wait(ahci
, cmd
);
1107 ahci_command_verify(ahci
, cmd
);
1109 ahci_command_free(cmd
);
1110 ahci_shutdown(ahci
);
1114 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
1116 static void test_migrate_sanity(void)
1118 AHCIQState
*src
, *dst
;
1119 char *uri
= g_strdup_printf("unix:%s", mig_socket
);
1121 src
= ahci_boot("-m 1024 -M q35 "
1122 "-drive if=ide,file=%s,format=%s ", tmp_path
, imgfmt
);
1123 dst
= ahci_boot("-m 1024 -M q35 "
1124 "-drive if=ide,file=%s,format=%s "
1125 "-incoming %s", tmp_path
, imgfmt
, uri
);
1127 ahci_migrate(src
, dst
, uri
);
1135 * Simple migration test: Write a pattern, migrate, then read.
1137 static void ahci_migrate_simple(uint8_t cmd_read
, uint8_t cmd_write
)
1139 AHCIQState
*src
, *dst
;
1141 size_t bufsize
= 4096;
1142 unsigned char *tx
= g_malloc(bufsize
);
1143 unsigned char *rx
= g_malloc0(bufsize
);
1144 char *uri
= g_strdup_printf("unix:%s", mig_socket
);
1146 src
= ahci_boot_and_enable("-m 1024 -M q35 "
1147 "-drive if=ide,format=%s,file=%s ",
1149 dst
= ahci_boot("-m 1024 -M q35 "
1150 "-drive if=ide,format=%s,file=%s "
1151 "-incoming %s", imgfmt
, tmp_path
, uri
);
1153 set_context(src
->parent
);
1156 px
= ahci_port_select(src
);
1157 ahci_port_clear(src
, px
);
1159 /* create pattern */
1160 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
1162 /* Write, migrate, then read. */
1163 ahci_io(src
, px
, cmd_write
, tx
, bufsize
, 0);
1164 ahci_migrate(src
, dst
, uri
);
1165 ahci_io(dst
, px
, cmd_read
, rx
, bufsize
, 0);
1167 /* Verify pattern */
1168 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1177 static void test_migrate_dma(void)
1179 ahci_migrate_simple(CMD_READ_DMA
, CMD_WRITE_DMA
);
1182 static void test_migrate_ncq(void)
1184 ahci_migrate_simple(READ_FPDMA_QUEUED
, WRITE_FPDMA_QUEUED
);
1188 * Halted IO Error Test
1190 * Simulate an error on first write, Try to write a pattern,
1191 * Confirm the VM has stopped, resume the VM, verify command
1192 * has completed, then read back the data and verify.
1194 static void ahci_halted_io_test(uint8_t cmd_read
, uint8_t cmd_write
)
1198 size_t bufsize
= 4096;
1199 unsigned char *tx
= g_malloc(bufsize
);
1200 unsigned char *rx
= g_malloc0(bufsize
);
1204 prepare_blkdebug_script(debug_path
, "write_aio");
1206 ahci
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1207 "format=%s,cache=writeback,"
1208 "rerror=stop,werror=stop "
1210 "-device ide-hd,drive=drive0 ",
1214 /* Initialize and prepare */
1215 port
= ahci_port_select(ahci
);
1216 ahci_port_clear(ahci
, port
);
1218 /* create DMA source buffer and write pattern */
1219 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
1220 ptr
= ahci_alloc(ahci
, bufsize
);
1222 memwrite(ptr
, tx
, bufsize
);
1224 /* Attempt to write (and fail) */
1225 cmd
= ahci_guest_io_halt(ahci
, port
, cmd_write
,
1228 /* Attempt to resume the command */
1229 ahci_guest_io_resume(ahci
, cmd
);
1230 ahci_free(ahci
, ptr
);
1232 /* Read back and verify */
1233 ahci_io(ahci
, port
, cmd_read
, rx
, bufsize
, 0);
1234 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1236 /* Cleanup and go home */
1237 ahci_shutdown(ahci
);
1242 static void test_halted_dma(void)
1244 ahci_halted_io_test(CMD_READ_DMA
, CMD_WRITE_DMA
);
1247 static void test_halted_ncq(void)
1249 ahci_halted_io_test(READ_FPDMA_QUEUED
, WRITE_FPDMA_QUEUED
);
1253 * IO Error Migration Test
1255 * Simulate an error on first write, Try to write a pattern,
1256 * Confirm the VM has stopped, migrate, resume the VM,
1257 * verify command has completed, then read back the data and verify.
1259 static void ahci_migrate_halted_io(uint8_t cmd_read
, uint8_t cmd_write
)
1261 AHCIQState
*src
, *dst
;
1263 size_t bufsize
= 4096;
1264 unsigned char *tx
= g_malloc(bufsize
);
1265 unsigned char *rx
= g_malloc0(bufsize
);
1268 char *uri
= g_strdup_printf("unix:%s", mig_socket
);
1270 prepare_blkdebug_script(debug_path
, "write_aio");
1272 src
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1273 "format=%s,cache=writeback,"
1274 "rerror=stop,werror=stop "
1276 "-device ide-hd,drive=drive0 ",
1280 dst
= ahci_boot("-drive file=%s,if=none,id=drive0,"
1281 "format=%s,cache=writeback,"
1282 "rerror=stop,werror=stop "
1284 "-device ide-hd,drive=drive0 "
1286 tmp_path
, imgfmt
, uri
);
1288 set_context(src
->parent
);
1290 /* Initialize and prepare */
1291 port
= ahci_port_select(src
);
1292 ahci_port_clear(src
, port
);
1293 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
1295 /* create DMA source buffer and write pattern */
1296 ptr
= ahci_alloc(src
, bufsize
);
1298 memwrite(ptr
, tx
, bufsize
);
1300 /* Write, trigger the VM to stop, migrate, then resume. */
1301 cmd
= ahci_guest_io_halt(src
, port
, cmd_write
,
1303 ahci_migrate(src
, dst
, uri
);
1304 ahci_guest_io_resume(dst
, cmd
);
1305 ahci_free(dst
, ptr
);
1308 ahci_io(dst
, port
, cmd_read
, rx
, bufsize
, 0);
1310 /* Verify TX and RX are identical */
1311 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1313 /* Cleanup and go home. */
1321 static void test_migrate_halted_dma(void)
1323 ahci_migrate_halted_io(CMD_READ_DMA
, CMD_WRITE_DMA
);
1326 static void test_migrate_halted_ncq(void)
1328 ahci_migrate_halted_io(READ_FPDMA_QUEUED
, WRITE_FPDMA_QUEUED
);
1332 * Migration test: Try to flush, migrate, then resume.
1334 static void test_flush_migrate(void)
1336 AHCIQState
*src
, *dst
;
1340 char *uri
= g_strdup_printf("unix:%s", mig_socket
);
1342 prepare_blkdebug_script(debug_path
, "flush_to_disk");
1344 src
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1345 "cache=writeback,rerror=stop,werror=stop,"
1348 "-device ide-hd,drive=drive0 ",
1349 debug_path
, tmp_path
, imgfmt
);
1350 dst
= ahci_boot("-drive file=%s,if=none,id=drive0,"
1351 "cache=writeback,rerror=stop,werror=stop,"
1354 "-device ide-hd,drive=drive0 "
1355 "-incoming %s", tmp_path
, imgfmt
, uri
);
1357 set_context(src
->parent
);
1359 /* Issue Flush Command */
1360 px
= ahci_port_select(src
);
1361 ahci_port_clear(src
, px
);
1362 cmd
= ahci_command_create(CMD_FLUSH_CACHE
);
1363 ahci_command_commit(src
, cmd
, px
);
1364 ahci_command_issue_async(src
, cmd
);
1365 qmp_eventwait("STOP");
1368 ahci_migrate(src
, dst
, uri
);
1370 /* Complete the command */
1371 s
= "{'execute':'cont' }";
1373 qmp_eventwait("RESUME");
1374 ahci_command_wait(dst
, cmd
);
1375 ahci_command_verify(dst
, cmd
);
1377 ahci_command_free(cmd
);
1383 static void test_max(void)
1387 ahci
= ahci_boot_and_enable(NULL
);
1388 ahci_test_max(ahci
);
1389 ahci_shutdown(ahci
);
1392 static void test_reset(void)
1397 ahci
= ahci_boot(NULL
);
1398 ahci_test_pci_spec(ahci
);
1399 ahci_pci_enable(ahci
);
1401 for (i
= 0; i
< 2; i
++) {
1402 ahci_test_hba_spec(ahci
);
1403 ahci_hba_enable(ahci
);
1404 ahci_test_identify(ahci
);
1405 ahci_test_io_rw_simple(ahci
, 4096, 0,
1408 ahci_set(ahci
, AHCI_GHC
, AHCI_GHC_HR
);
1409 ahci_clean_mem(ahci
);
1412 ahci_shutdown(ahci
);
1415 static void test_ncq_simple(void)
1419 ahci
= ahci_boot_and_enable(NULL
);
1420 ahci_test_io_rw_simple(ahci
, 4096, 0,
1422 WRITE_FPDMA_QUEUED
);
1423 ahci_shutdown(ahci
);
1426 /******************************************************************************/
1427 /* AHCI I/O Test Matrix Definitions */
1431 LEN_SIMPLE
= LEN_BEGIN
,
1438 static const char *buff_len_str
[NUM_LENGTHS
] = { "simple", "double",
1442 ADDR_MODE_BEGIN
= 0,
1443 ADDR_MODE_LBA28
= ADDR_MODE_BEGIN
,
1448 static const char *addr_mode_str
[NUM_ADDR_MODES
] = { "lba28", "lba48" };
1452 MODE_PIO
= MODE_BEGIN
,
1457 static const char *io_mode_str
[NUM_MODES
] = { "pio", "dma" };
1468 OFFSET_ZERO
= OFFSET_BEGIN
,
1474 static const char *offset_str
[NUM_OFFSETS
] = { "zero", "low", "high" };
1476 typedef struct AHCIIOTestOptions
{
1477 enum BuffLen length
;
1478 enum AddrMode address_type
;
1479 enum IOMode io_type
;
1480 enum OffsetType offset
;
1481 } AHCIIOTestOptions
;
1483 static uint64_t offset_sector(enum OffsetType ofst
,
1484 enum AddrMode addr_type
,
1496 ceil
= (addr_type
== ADDR_MODE_LBA28
) ? 0xfffffff : 0xffffffffffff;
1497 ceil
= MIN(ceil
, mb_to_sectors(test_image_size_mb
) - 1);
1498 nsectors
= buffsize
/ AHCI_SECTOR_SIZE
;
1499 return ceil
- nsectors
+ 1;
1501 g_assert_not_reached();
1506 * Table of possible I/O ATA commands given a set of enumerations.
1508 static const uint8_t io_cmds
[NUM_MODES
][NUM_ADDR_MODES
][NUM_IO_OPS
] = {
1510 [ADDR_MODE_LBA28
] = {
1511 [IO_READ
] = CMD_READ_PIO
,
1512 [IO_WRITE
] = CMD_WRITE_PIO
},
1513 [ADDR_MODE_LBA48
] = {
1514 [IO_READ
] = CMD_READ_PIO_EXT
,
1515 [IO_WRITE
] = CMD_WRITE_PIO_EXT
}
1518 [ADDR_MODE_LBA28
] = {
1519 [IO_READ
] = CMD_READ_DMA
,
1520 [IO_WRITE
] = CMD_WRITE_DMA
},
1521 [ADDR_MODE_LBA48
] = {
1522 [IO_READ
] = CMD_READ_DMA_EXT
,
1523 [IO_WRITE
] = CMD_WRITE_DMA_EXT
}
1528 * Test a Read/Write pattern using various commands, addressing modes,
1529 * transfer modes, and buffer sizes.
1531 static void test_io_rw_interface(enum AddrMode lba48
, enum IOMode dma
,
1532 unsigned bufsize
, uint64_t sector
)
1536 ahci
= ahci_boot_and_enable(NULL
);
1537 ahci_test_io_rw_simple(ahci
, bufsize
, sector
,
1538 io_cmds
[dma
][lba48
][IO_READ
],
1539 io_cmds
[dma
][lba48
][IO_WRITE
]);
1540 ahci_shutdown(ahci
);
1544 * Demultiplex the test data and invoke the actual test routine.
1546 static void test_io_interface(gconstpointer opaque
)
1548 AHCIIOTestOptions
*opts
= (AHCIIOTestOptions
*)opaque
;
1552 switch (opts
->length
) {
1560 bufsize
= 4096 * 64;
1566 g_assert_not_reached();
1569 sector
= offset_sector(opts
->offset
, opts
->address_type
, bufsize
);
1570 test_io_rw_interface(opts
->address_type
, opts
->io_type
, bufsize
, sector
);
1575 static void create_ahci_io_test(enum IOMode type
, enum AddrMode addr
,
1576 enum BuffLen len
, enum OffsetType offset
)
1579 AHCIIOTestOptions
*opts
;
1581 opts
= g_malloc(sizeof(AHCIIOTestOptions
));
1583 opts
->address_type
= addr
;
1584 opts
->io_type
= type
;
1585 opts
->offset
= offset
;
1587 name
= g_strdup_printf("ahci/io/%s/%s/%s/%s",
1589 addr_mode_str
[addr
],
1591 offset_str
[offset
]);
1593 if ((addr
== ADDR_MODE_LBA48
) && (offset
== OFFSET_HIGH
) &&
1594 (mb_to_sectors(test_image_size_mb
) <= 0xFFFFFFF)) {
1595 g_test_message("%s: skipped; test image too small", name
);
1600 qtest_add_data_func(name
, opts
, test_io_interface
);
1604 /******************************************************************************/
1606 int main(int argc
, char **argv
)
1614 static struct option long_options
[] = {
1615 {"pedantic", no_argument
, 0, 'p' },
1619 /* Should be first to utilize g_test functionality, So we can see errors. */
1620 g_test_init(&argc
, &argv
, NULL
);
1623 c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
1634 fprintf(stderr
, "Unrecognized ahci_test option.\n");
1635 g_assert_not_reached();
1639 /* Check architecture */
1640 arch
= qtest_get_arch();
1641 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
1642 g_test_message("Skipping test for non-x86");
1646 /* Create a temporary image */
1647 fd
= mkstemp(tmp_path
);
1649 if (have_qemu_img()) {
1651 test_image_size_mb
= TEST_IMAGE_SIZE_MB_LARGE
;
1652 mkqcow2(tmp_path
, TEST_IMAGE_SIZE_MB_LARGE
);
1654 g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
1655 "skipping LBA48 high-sector tests");
1657 test_image_size_mb
= TEST_IMAGE_SIZE_MB_SMALL
;
1658 ret
= ftruncate(fd
, test_image_size_mb
* 1024 * 1024);
1663 /* Create temporary blkdebug instructions */
1664 fd
= mkstemp(debug_path
);
1668 /* Reserve a hollow file to use as a socket for migration tests */
1669 fd
= mkstemp(mig_socket
);
1674 qtest_add_func("/ahci/sanity", test_sanity
);
1675 qtest_add_func("/ahci/pci_spec", test_pci_spec
);
1676 qtest_add_func("/ahci/pci_enable", test_pci_enable
);
1677 qtest_add_func("/ahci/hba_spec", test_hba_spec
);
1678 qtest_add_func("/ahci/hba_enable", test_hba_enable
);
1679 qtest_add_func("/ahci/identify", test_identify
);
1681 for (i
= MODE_BEGIN
; i
< NUM_MODES
; i
++) {
1682 for (j
= ADDR_MODE_BEGIN
; j
< NUM_ADDR_MODES
; j
++) {
1683 for (k
= LEN_BEGIN
; k
< NUM_LENGTHS
; k
++) {
1684 for (m
= OFFSET_BEGIN
; m
< NUM_OFFSETS
; m
++) {
1685 create_ahci_io_test(i
, j
, k
, m
);
1691 qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented
);
1693 qtest_add_func("/ahci/flush/simple", test_flush
);
1694 qtest_add_func("/ahci/flush/retry", test_flush_retry
);
1695 qtest_add_func("/ahci/flush/migrate", test_flush_migrate
);
1697 qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity
);
1698 qtest_add_func("/ahci/migrate/dma/simple", test_migrate_dma
);
1699 qtest_add_func("/ahci/io/dma/lba28/retry", test_halted_dma
);
1700 qtest_add_func("/ahci/migrate/dma/halted", test_migrate_halted_dma
);
1702 qtest_add_func("/ahci/max", test_max
);
1703 qtest_add_func("/ahci/reset", test_reset
);
1705 qtest_add_func("/ahci/io/ncq/simple", test_ncq_simple
);
1706 qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq
);
1707 qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq
);
1708 qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq
);