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-specific defines -- in MiB */
43 #define TEST_IMAGE_SIZE_MB (200 * 1024)
44 #define TEST_IMAGE_SECTORS ((TEST_IMAGE_SIZE_MB / AHCI_SECTOR_SIZE) \
48 static char tmp_path
[] = "/tmp/qtest.XXXXXX";
49 static char debug_path
[] = "/tmp/qtest-blkdebug.XXXXXX";
50 static bool ahci_pedantic
;
52 /*** Function Declarations ***/
53 static void ahci_test_port_spec(AHCIQState
*ahci
, uint8_t port
);
54 static void ahci_test_pci_spec(AHCIQState
*ahci
);
55 static void ahci_test_pci_caps(AHCIQState
*ahci
, uint16_t header
,
57 static void ahci_test_satacap(AHCIQState
*ahci
, uint8_t offset
);
58 static void ahci_test_msicap(AHCIQState
*ahci
, uint8_t offset
);
59 static void ahci_test_pmcap(AHCIQState
*ahci
, uint8_t offset
);
63 static void string_bswap16(uint16_t *s
, size_t bytes
)
65 g_assert_cmphex((bytes
& 1), ==, 0);
74 static void generate_pattern(void *buffer
, size_t len
, size_t cycle_len
)
77 unsigned char *tx
= (unsigned char *)buffer
;
81 /* Write an indicative pattern that varies and is unique per-cycle */
83 for (i
= j
= 0; i
< len
; i
++, j
++) {
85 if (j
% cycle_len
== 0) {
90 /* force uniqueness by writing an id per-cycle */
91 for (i
= 0; i
< len
/ cycle_len
; i
++) {
93 if (j
+ sizeof(*sx
) <= len
) {
94 sx
= (size_t *)&tx
[j
];
101 * Verify that the transfer did not corrupt our state at all.
103 static void verify_state(AHCIQState
*ahci
)
106 uint32_t ahci_fingerprint
;
109 AHCICommandHeader cmd
;
111 ahci_fingerprint
= qpci_config_readl(ahci
->dev
, PCI_VENDOR_ID
);
112 g_assert_cmphex(ahci_fingerprint
, ==, ahci
->fingerprint
);
114 /* If we haven't initialized, this is as much as can be validated. */
115 if (!ahci
->hba_base
) {
119 hba_base
= (uint64_t)qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
120 hba_stored
= (uint64_t)(uintptr_t)ahci
->hba_base
;
121 g_assert_cmphex(hba_base
, ==, hba_stored
);
123 g_assert_cmphex(ahci_rreg(ahci
, AHCI_CAP
), ==, ahci
->cap
);
124 g_assert_cmphex(ahci_rreg(ahci
, AHCI_CAP2
), ==, ahci
->cap2
);
126 for (i
= 0; i
< 32; i
++) {
127 g_assert_cmphex(ahci_px_rreg(ahci
, i
, AHCI_PX_FB
), ==,
129 g_assert_cmphex(ahci_px_rreg(ahci
, i
, AHCI_PX_CLB
), ==,
131 for (j
= 0; j
< 32; j
++) {
132 ahci_get_command_header(ahci
, i
, j
, &cmd
);
133 g_assert_cmphex(cmd
.prdtl
, ==, ahci
->port
[i
].prdtl
[j
]);
134 g_assert_cmphex(cmd
.ctba
, ==, ahci
->port
[i
].ctba
[j
]);
139 static void ahci_migrate(AHCIQState
*from
, AHCIQState
*to
, const char *uri
)
141 QOSState
*tmp
= to
->parent
;
142 QPCIDevice
*dev
= to
->dev
;
144 uri
= "tcp:127.0.0.1:1234";
147 /* context will be 'to' after completion. */
148 migrate(from
->parent
, to
->parent
, uri
);
150 /* We'd like for the AHCIState objects to still point
151 * to information specific to its specific parent
152 * instance, but otherwise just inherit the new data. */
153 memcpy(to
, from
, sizeof(AHCIQState
));
159 memset(from
, 0x00, sizeof(AHCIQState
));
166 /*** Test Setup & Teardown ***/
169 * Start a Q35 machine and bookmark a handle to the AHCI device.
171 static AHCIQState
*ahci_vboot(const char *cli
, va_list ap
)
175 s
= g_malloc0(sizeof(AHCIQState
));
176 s
->parent
= qtest_pc_vboot(cli
, ap
);
177 alloc_set_flags(s
->parent
->alloc
, ALLOC_LEAK_ASSERT
);
179 /* Verify that we have an AHCI device present. */
180 s
->dev
= get_ahci_device(&s
->fingerprint
);
186 * Start a Q35 machine and bookmark a handle to the AHCI device.
188 static AHCIQState
*ahci_boot(const char *cli
, ...)
195 s
= ahci_vboot(cli
, ap
);
198 cli
= "-drive if=none,id=drive0,file=%s,cache=writeback,serial=%s"
201 "-device ide-hd,drive=drive0 "
202 "-global ide-hd.ver=%s";
203 s
= ahci_boot(cli
, tmp_path
, "testdisk", "version");
210 * Clean up the PCI device, then terminate the QEMU instance.
212 static void ahci_shutdown(AHCIQState
*ahci
)
214 QOSState
*qs
= ahci
->parent
;
217 ahci_clean_mem(ahci
);
218 free_ahci_device(ahci
->dev
);
224 * Boot and fully enable the HBA device.
225 * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
227 static AHCIQState
*ahci_boot_and_enable(const char *cli
, ...)
236 ahci
= ahci_vboot(cli
, ap
);
239 ahci
= ahci_boot(NULL
);
242 ahci_pci_enable(ahci
);
243 ahci_hba_enable(ahci
);
244 /* Initialize test device */
245 port
= ahci_port_select(ahci
);
246 ahci_port_clear(ahci
, port
);
247 ahci_io(ahci
, port
, CMD_IDENTIFY
, &buff
, sizeof(buff
), 0);
252 /*** Specification Adherence Tests ***/
255 * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
257 static void ahci_test_pci_spec(AHCIQState
*ahci
)
263 /* Most of these bits should start cleared until we turn them on. */
264 data
= qpci_config_readw(ahci
->dev
, PCI_COMMAND
);
265 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_MEMORY
);
266 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_MASTER
);
267 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_SPECIAL
); /* Reserved */
268 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_VGA_PALETTE
); /* Reserved */
269 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_PARITY
);
270 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_WAIT
); /* Reserved */
271 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_SERR
);
272 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_FAST_BACK
);
273 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_INTX_DISABLE
);
274 ASSERT_BIT_CLEAR(data
, 0xF800); /* Reserved */
276 data
= qpci_config_readw(ahci
->dev
, PCI_STATUS
);
277 ASSERT_BIT_CLEAR(data
, 0x01 | 0x02 | 0x04); /* Reserved */
278 ASSERT_BIT_CLEAR(data
, PCI_STATUS_INTERRUPT
);
279 ASSERT_BIT_SET(data
, PCI_STATUS_CAP_LIST
); /* must be set */
280 ASSERT_BIT_CLEAR(data
, PCI_STATUS_UDF
); /* Reserved */
281 ASSERT_BIT_CLEAR(data
, PCI_STATUS_PARITY
);
282 ASSERT_BIT_CLEAR(data
, PCI_STATUS_SIG_TARGET_ABORT
);
283 ASSERT_BIT_CLEAR(data
, PCI_STATUS_REC_TARGET_ABORT
);
284 ASSERT_BIT_CLEAR(data
, PCI_STATUS_REC_MASTER_ABORT
);
285 ASSERT_BIT_CLEAR(data
, PCI_STATUS_SIG_SYSTEM_ERROR
);
286 ASSERT_BIT_CLEAR(data
, PCI_STATUS_DETECTED_PARITY
);
288 /* RID occupies the low byte, CCs occupy the high three. */
289 datal
= qpci_config_readl(ahci
->dev
, PCI_CLASS_REVISION
);
291 /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
292 * Though in practice this is likely seldom true. */
293 ASSERT_BIT_CLEAR(datal
, 0xFF);
296 /* BCC *must* equal 0x01. */
297 g_assert_cmphex(PCI_BCC(datal
), ==, 0x01);
298 if (PCI_SCC(datal
) == 0x01) {
300 ASSERT_BIT_SET(0x80000000, datal
);
301 ASSERT_BIT_CLEAR(0x60000000, datal
);
302 } else if (PCI_SCC(datal
) == 0x04) {
304 g_assert_cmphex(PCI_PI(datal
), ==, 0);
305 } else if (PCI_SCC(datal
) == 0x06) {
307 g_assert_cmphex(PCI_PI(datal
), ==, 0x01);
309 g_assert_not_reached();
312 datab
= qpci_config_readb(ahci
->dev
, PCI_CACHE_LINE_SIZE
);
313 g_assert_cmphex(datab
, ==, 0);
315 datab
= qpci_config_readb(ahci
->dev
, PCI_LATENCY_TIMER
);
316 g_assert_cmphex(datab
, ==, 0);
318 /* Only the bottom 7 bits must be off. */
319 datab
= qpci_config_readb(ahci
->dev
, PCI_HEADER_TYPE
);
320 ASSERT_BIT_CLEAR(datab
, 0x7F);
322 /* BIST is optional, but the low 7 bits must always start off regardless. */
323 datab
= qpci_config_readb(ahci
->dev
, PCI_BIST
);
324 ASSERT_BIT_CLEAR(datab
, 0x7F);
326 /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
327 datal
= qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
328 g_assert_cmphex(datal
, ==, 0);
330 qpci_config_writel(ahci
->dev
, PCI_BASE_ADDRESS_5
, 0xFFFFFFFF);
331 datal
= qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
332 /* ABAR must be 32-bit, memory mapped, non-prefetchable and
333 * must be >= 512 bytes. To that end, bits 0-8 must be off. */
334 ASSERT_BIT_CLEAR(datal
, 0xFF);
336 /* Capability list MUST be present, */
337 datal
= qpci_config_readl(ahci
->dev
, PCI_CAPABILITY_LIST
);
338 /* But these bits are reserved. */
339 ASSERT_BIT_CLEAR(datal
, ~0xFF);
340 g_assert_cmphex(datal
, !=, 0);
342 /* Check specification adherence for capability extenstions. */
343 data
= qpci_config_readw(ahci
->dev
, datal
);
345 switch (ahci
->fingerprint
) {
346 case AHCI_INTEL_ICH9
:
347 /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
348 g_assert_cmphex((data
& 0xFF), ==, PCI_CAP_ID_MSI
);
351 /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
352 g_assert_cmphex((data
& 0xFF), ==, PCI_CAP_ID_PM
);
355 ahci_test_pci_caps(ahci
, data
, (uint8_t)datal
);
358 datal
= qpci_config_readl(ahci
->dev
, PCI_CAPABILITY_LIST
+ 4);
359 g_assert_cmphex(datal
, ==, 0);
361 /* IPIN might vary, but ILINE must be off. */
362 datab
= qpci_config_readb(ahci
->dev
, PCI_INTERRUPT_LINE
);
363 g_assert_cmphex(datab
, ==, 0);
367 * Test PCI capabilities for AHCI specification adherence.
369 static void ahci_test_pci_caps(AHCIQState
*ahci
, uint16_t header
,
372 uint8_t cid
= header
& 0xFF;
373 uint8_t next
= header
>> 8;
375 g_test_message("CID: %02x; next: %02x", cid
, next
);
379 ahci_test_pmcap(ahci
, offset
);
382 ahci_test_msicap(ahci
, offset
);
384 case PCI_CAP_ID_SATA
:
385 ahci_test_satacap(ahci
, offset
);
389 g_test_message("Unknown CAP 0x%02x", cid
);
393 ahci_test_pci_caps(ahci
, qpci_config_readw(ahci
->dev
, next
), next
);
398 * Test SATA PCI capabilitity for AHCI specification adherence.
400 static void ahci_test_satacap(AHCIQState
*ahci
, uint8_t offset
)
405 g_test_message("Verifying SATACAP");
407 /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
408 dataw
= qpci_config_readw(ahci
->dev
, offset
+ 2);
409 g_assert_cmphex(dataw
, ==, 0x10);
411 /* Grab the SATACR1 register. */
412 datal
= qpci_config_readw(ahci
->dev
, offset
+ 4);
414 switch (datal
& 0x0F) {
415 case 0x04: /* BAR0 */
416 case 0x05: /* BAR1 */
420 case 0x09: /* BAR5 */
421 case 0x0F: /* Immediately following SATACR1 in PCI config space. */
424 /* Invalid BARLOC for the Index Data Pair. */
425 g_assert_not_reached();
429 g_assert_cmphex((datal
>> 24), ==, 0x00);
433 * Test MSI PCI capability for AHCI specification adherence.
435 static void ahci_test_msicap(AHCIQState
*ahci
, uint8_t offset
)
440 g_test_message("Verifying MSICAP");
442 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_FLAGS
);
443 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_ENABLE
);
444 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_QSIZE
);
445 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_RESERVED
);
447 datal
= qpci_config_readl(ahci
->dev
, offset
+ PCI_MSI_ADDRESS_LO
);
448 g_assert_cmphex(datal
, ==, 0);
450 if (dataw
& PCI_MSI_FLAGS_64BIT
) {
451 g_test_message("MSICAP is 64bit");
452 datal
= qpci_config_readl(ahci
->dev
, offset
+ PCI_MSI_ADDRESS_HI
);
453 g_assert_cmphex(datal
, ==, 0);
454 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_DATA_64
);
455 g_assert_cmphex(dataw
, ==, 0);
457 g_test_message("MSICAP is 32bit");
458 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_DATA_32
);
459 g_assert_cmphex(dataw
, ==, 0);
464 * Test Power Management PCI capability for AHCI specification adherence.
466 static void ahci_test_pmcap(AHCIQState
*ahci
, uint8_t offset
)
470 g_test_message("Verifying PMCAP");
472 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_PM_PMC
);
473 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_PME_CLOCK
);
474 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_RESERVED
);
475 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_D1
);
476 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_D2
);
478 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_PM_CTRL
);
479 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_STATE_MASK
);
480 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_RESERVED
);
481 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_DATA_SEL_MASK
);
482 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_DATA_SCALE_MASK
);
485 static void ahci_test_hba_spec(AHCIQState
*ahci
)
493 g_assert(ahci
!= NULL
);
496 * Note that the AHCI spec does expect the BIOS to set up a few things:
497 * CAP.SSS - Support for staggered spin-up (t/f)
498 * CAP.SMPS - Support for mechanical presence switches (t/f)
499 * PI - Ports Implemented (1-32)
500 * PxCMD.HPCP - Hot Plug Capable Port
501 * PxCMD.MPSP - Mechanical Presence Switch Present
502 * PxCMD.CPD - Cold Presence Detection support
504 * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
505 * Foreach Port Implemented:
506 * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
507 * -PxCLB/U and PxFB/U are set to valid regions in memory
508 * -PxSUD is set to 1.
509 * -PxSSTS.DET is polled for presence; if detected, we continue:
510 * -PxSERR is cleared with 1's.
511 * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
512 * the device is ready.
515 /* 1 CAP - Capabilities Register */
516 ahci
->cap
= ahci_rreg(ahci
, AHCI_CAP
);
517 ASSERT_BIT_CLEAR(ahci
->cap
, AHCI_CAP_RESERVED
);
519 /* 2 GHC - Global Host Control */
520 reg
= ahci_rreg(ahci
, AHCI_GHC
);
521 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_HR
);
522 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_IE
);
523 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_MRSM
);
524 if (BITSET(ahci
->cap
, AHCI_CAP_SAM
)) {
525 g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
526 ASSERT_BIT_SET(reg
, AHCI_GHC_AE
);
528 g_test_message("Supports AHCI/Legacy mix.");
529 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_AE
);
532 /* 3 IS - Interrupt Status */
533 reg
= ahci_rreg(ahci
, AHCI_IS
);
534 g_assert_cmphex(reg
, ==, 0);
536 /* 4 PI - Ports Implemented */
537 ports
= ahci_rreg(ahci
, AHCI_PI
);
538 /* Ports Implemented must be non-zero. */
539 g_assert_cmphex(ports
, !=, 0);
540 /* Ports Implemented must be <= Number of Ports. */
541 nports_impl
= ctpopl(ports
);
542 g_assert_cmpuint(((AHCI_CAP_NP
& ahci
->cap
) + 1), >=, nports_impl
);
544 /* Ports must be within the proper range. Given a mapping of SIZE,
545 * 256 bytes are used for global HBA control, and the rest is used
546 * for ports data, at 0x80 bytes each. */
547 g_assert_cmphex(ahci
->barsize
, >, 0);
548 maxports
= (ahci
->barsize
- HBA_DATA_REGION_SIZE
) / HBA_PORT_DATA_SIZE
;
549 /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
550 g_assert_cmphex((reg
>> maxports
), ==, 0);
553 reg
= ahci_rreg(ahci
, AHCI_VS
);
555 case AHCI_VERSION_0_95
:
556 case AHCI_VERSION_1_0
:
557 case AHCI_VERSION_1_1
:
558 case AHCI_VERSION_1_2
:
559 case AHCI_VERSION_1_3
:
562 g_assert_not_reached();
565 /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
566 reg
= ahci_rreg(ahci
, AHCI_CCCCTL
);
567 if (BITSET(ahci
->cap
, AHCI_CAP_CCCS
)) {
568 ASSERT_BIT_CLEAR(reg
, AHCI_CCCCTL_EN
);
569 ASSERT_BIT_CLEAR(reg
, AHCI_CCCCTL_RESERVED
);
570 ASSERT_BIT_SET(reg
, AHCI_CCCCTL_CC
);
571 ASSERT_BIT_SET(reg
, AHCI_CCCCTL_TV
);
573 g_assert_cmphex(reg
, ==, 0);
577 reg
= ahci_rreg(ahci
, AHCI_CCCPORTS
);
578 /* Must be zeroes initially regardless of CAP.CCCS */
579 g_assert_cmphex(reg
, ==, 0);
582 reg
= ahci_rreg(ahci
, AHCI_EMLOC
);
583 if (BITCLR(ahci
->cap
, AHCI_CAP_EMS
)) {
584 g_assert_cmphex(reg
, ==, 0);
588 reg
= ahci_rreg(ahci
, AHCI_EMCTL
);
589 if (BITSET(ahci
->cap
, AHCI_CAP_EMS
)) {
590 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_STSMR
);
591 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_CTLTM
);
592 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_CTLRST
);
593 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_RESERVED
);
595 g_assert_cmphex(reg
, ==, 0);
598 /* 10 CAP2 -- Capabilities Extended */
599 ahci
->cap2
= ahci_rreg(ahci
, AHCI_CAP2
);
600 ASSERT_BIT_CLEAR(ahci
->cap2
, AHCI_CAP2_RESERVED
);
602 /* 11 BOHC -- Bios/OS Handoff Control */
603 reg
= ahci_rreg(ahci
, AHCI_BOHC
);
604 g_assert_cmphex(reg
, ==, 0);
606 /* 12 -- 23: Reserved */
607 g_test_message("Verifying HBA reserved area is empty.");
608 for (i
= AHCI_RESERVED
; i
< AHCI_NVMHCI
; ++i
) {
609 reg
= ahci_rreg(ahci
, i
);
610 g_assert_cmphex(reg
, ==, 0);
613 /* 24 -- 39: NVMHCI */
614 if (BITCLR(ahci
->cap2
, AHCI_CAP2_NVMP
)) {
615 g_test_message("Verifying HBA/NVMHCI area is empty.");
616 for (i
= AHCI_NVMHCI
; i
< AHCI_VENDOR
; ++i
) {
617 reg
= ahci_rreg(ahci
, i
);
618 g_assert_cmphex(reg
, ==, 0);
622 /* 40 -- 63: Vendor */
623 g_test_message("Verifying HBA/Vendor area is empty.");
624 for (i
= AHCI_VENDOR
; i
< AHCI_PORTS
; ++i
) {
625 reg
= ahci_rreg(ahci
, i
);
626 g_assert_cmphex(reg
, ==, 0);
629 /* 64 -- XX: Port Space */
630 for (i
= 0; ports
|| (i
< maxports
); ports
>>= 1, ++i
) {
631 if (BITSET(ports
, 0x1)) {
632 g_test_message("Testing port %u for spec", i
);
633 ahci_test_port_spec(ahci
, i
);
636 uint16_t low
= AHCI_PORTS
+ (32 * i
);
637 uint16_t high
= AHCI_PORTS
+ (32 * (i
+ 1));
638 g_test_message("Asserting unimplemented port %u "
639 "(reg [%u-%u]) is empty.",
641 for (j
= low
; j
< high
; ++j
) {
642 reg
= ahci_rreg(ahci
, j
);
643 g_assert_cmphex(reg
, ==, 0);
650 * Test the memory space for one port for specification adherence.
652 static void ahci_test_port_spec(AHCIQState
*ahci
, uint8_t port
)
658 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CLB
);
659 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CLB_RESERVED
);
662 if (BITCLR(ahci
->cap
, AHCI_CAP_S64A
)) {
663 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CLBU
);
664 g_assert_cmphex(reg
, ==, 0);
668 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FB
);
669 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FB_RESERVED
);
672 if (BITCLR(ahci
->cap
, AHCI_CAP_S64A
)) {
673 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FBU
);
674 g_assert_cmphex(reg
, ==, 0);
678 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_IS
);
679 g_assert_cmphex(reg
, ==, 0);
682 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_IE
);
683 g_assert_cmphex(reg
, ==, 0);
686 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CMD
);
687 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FRE
);
688 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_RESERVED
);
689 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CCS
);
690 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FR
);
691 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CR
);
692 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_PMA
); /* And RW only if CAP.SPM */
693 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_APSTE
); /* RW only if CAP2.APST */
694 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ATAPI
);
695 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_DLAE
);
696 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ALPE
); /* RW only if CAP.SALP */
697 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ASP
); /* RW only if CAP.SALP */
698 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ICC
);
699 /* If CPDetect support does not exist, CPState must be off. */
700 if (BITCLR(reg
, AHCI_PX_CMD_CPD
)) {
701 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CPS
);
703 /* If MPSPresence is not set, MPSState must be off. */
704 if (BITCLR(reg
, AHCI_PX_CMD_MPSP
)) {
705 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSS
);
707 /* If we do not support MPS, MPSS and MPSP must be off. */
708 if (BITCLR(ahci
->cap
, AHCI_CAP_SMPS
)) {
709 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSS
);
710 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSP
);
712 /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
713 if (BITANY(reg
, AHCI_PX_CMD_CPD
| AHCI_PX_CMD_MPSP
)) {
714 ASSERT_BIT_SET(reg
, AHCI_PX_CMD_HPCP
);
716 /* HPCP and ESP cannot both be active. */
717 g_assert(!BITSET(reg
, AHCI_PX_CMD_HPCP
| AHCI_PX_CMD_ESP
));
718 /* If CAP.FBSS is not set, FBSCP must not be set. */
719 if (BITCLR(ahci
->cap
, AHCI_CAP_FBSS
)) {
720 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FBSCP
);
724 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_RES1
);
725 g_assert_cmphex(reg
, ==, 0);
728 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_TFD
);
729 /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
730 * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
731 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_ERR
);
732 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_CS1
);
733 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_DRQ
);
734 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_CS2
);
735 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_STS_BSY
);
736 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_ERR
);
737 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_RESERVED
);
740 /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
741 * Even when GHC.ST is zero, the AHCI HBA may receive the initial
742 * D2H register FIS and update the signature asynchronously,
743 * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
745 /* (10) SSTS / SCR0: SStatus */
746 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SSTS
);
747 ASSERT_BIT_CLEAR(reg
, AHCI_PX_SSTS_RESERVED
);
748 /* Even though the register should be 0 at boot, it is asynchronous and
749 * prone to change, so we cannot test any well known value. */
751 /* (11) SCTL / SCR2: SControl */
752 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SCTL
);
753 g_assert_cmphex(reg
, ==, 0);
755 /* (12) SERR / SCR1: SError */
756 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SERR
);
757 g_assert_cmphex(reg
, ==, 0);
759 /* (13) SACT / SCR3: SActive */
760 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SACT
);
761 g_assert_cmphex(reg
, ==, 0);
764 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CI
);
765 g_assert_cmphex(reg
, ==, 0);
768 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SNTF
);
769 g_assert_cmphex(reg
, ==, 0);
772 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FBS
);
773 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_EN
);
774 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DEC
);
775 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_SDE
);
776 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DEV
);
777 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DWE
);
778 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_RESERVED
);
779 if (BITSET(ahci
->cap
, AHCI_CAP_FBSS
)) {
780 /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
781 g_assert((reg
& AHCI_PX_FBS_ADO
) >> ctzl(AHCI_PX_FBS_ADO
) >= 2);
784 /* [17 -- 27] RESERVED */
785 for (i
= AHCI_PX_RES2
; i
< AHCI_PX_VS
; ++i
) {
786 reg
= ahci_px_rreg(ahci
, port
, i
);
787 g_assert_cmphex(reg
, ==, 0);
790 /* [28 -- 31] Vendor-Specific */
791 for (i
= AHCI_PX_VS
; i
< 32; ++i
) {
792 reg
= ahci_px_rreg(ahci
, port
, i
);
794 g_test_message("INFO: Vendor register %u non-empty", i
);
800 * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
801 * device we see, then read and check the response.
803 static void ahci_test_identify(AHCIQState
*ahci
)
809 const size_t buffsize
= 512;
811 g_assert(ahci
!= NULL
);
814 * This serves as a bit of a tutorial on AHCI device programming:
816 * (1) Create a data buffer for the IDENTIFY response to be sent to
817 * (2) Create a Command Table buffer, where we will store the
818 * command and PRDT (Physical Region Descriptor Table)
819 * (3) Construct an FIS host-to-device command structure, and write it to
820 * the top of the Command Table buffer.
821 * (4) Create one or more Physical Region Descriptors (PRDs) that describe
822 * a location in memory where data may be stored/retrieved.
823 * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
824 * (6) Each AHCI port has up to 32 command slots. Each slot contains a
825 * header that points to a Command Table buffer. Pick an unused slot
826 * and update it to point to the Command Table we have built.
827 * (7) Now: Command #n points to our Command Table, and our Command Table
828 * contains the FIS (that describes our command) and the PRDTL, which
829 * describes our buffer.
830 * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
831 * #n is ready for processing.
834 /* Pick the first implemented and running port */
835 px
= ahci_port_select(ahci
);
836 g_test_message("Selected port %u for test", px
);
838 /* Clear out the FIS Receive area and any pending interrupts. */
839 ahci_port_clear(ahci
, px
);
841 /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
842 ahci_io(ahci
, px
, CMD_IDENTIFY
, &buff
, buffsize
, 0);
844 /* Check serial number/version in the buffer */
845 /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
846 * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
847 * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
848 * as a consequence, only needs to unchunk the data on LE machines. */
849 string_bswap16(&buff
[10], 20);
850 rc
= memcmp(&buff
[10], "testdisk ", 20);
851 g_assert_cmphex(rc
, ==, 0);
853 string_bswap16(&buff
[23], 8);
854 rc
= memcmp(&buff
[23], "version ", 8);
855 g_assert_cmphex(rc
, ==, 0);
857 sect_size
= le16_to_cpu(*((uint16_t *)(&buff
[5])));
858 g_assert_cmphex(sect_size
, ==, AHCI_SECTOR_SIZE
);
861 static void ahci_test_io_rw_simple(AHCIQState
*ahci
, unsigned bufsize
,
862 uint64_t sector
, uint8_t read_cmd
,
867 unsigned char *tx
= g_malloc(bufsize
);
868 unsigned char *rx
= g_malloc0(bufsize
);
870 g_assert(ahci
!= NULL
);
872 /* Pick the first running port and clear it. */
873 port
= ahci_port_select(ahci
);
874 ahci_port_clear(ahci
, port
);
876 /*** Create pattern and transfer to guest ***/
877 /* Data buffer in the guest */
878 ptr
= ahci_alloc(ahci
, bufsize
);
881 /* Write some indicative pattern to our buffer. */
882 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
883 bufwrite(ptr
, tx
, bufsize
);
885 /* Write this buffer to disk, then read it back to the DMA buffer. */
886 ahci_guest_io(ahci
, port
, write_cmd
, ptr
, bufsize
, sector
);
887 qmemset(ptr
, 0x00, bufsize
);
888 ahci_guest_io(ahci
, port
, read_cmd
, ptr
, bufsize
, sector
);
890 /*** Read back the Data ***/
891 bufread(ptr
, rx
, bufsize
);
892 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
894 ahci_free(ahci
, ptr
);
899 static uint8_t ahci_test_nondata(AHCIQState
*ahci
, uint8_t ide_cmd
)
905 port
= ahci_port_select(ahci
);
906 ahci_port_clear(ahci
, port
);
909 cmd
= ahci_command_create(ide_cmd
);
910 ahci_command_commit(ahci
, cmd
, port
);
911 ahci_command_issue(ahci
, cmd
);
912 ahci_command_verify(ahci
, cmd
);
913 ahci_command_free(cmd
);
918 static void ahci_test_flush(AHCIQState
*ahci
)
920 ahci_test_nondata(ahci
, CMD_FLUSH_CACHE
);
923 static void ahci_test_max(AHCIQState
*ahci
)
925 RegD2HFIS
*d2h
= g_malloc0(0x20);
929 uint64_t config_sect
= TEST_IMAGE_SECTORS
- 1;
931 if (config_sect
> 0xFFFFFF) {
932 cmd
= CMD_READ_MAX_EXT
;
937 port
= ahci_test_nondata(ahci
, cmd
);
938 memread(ahci
->port
[port
].fb
+ 0x40, d2h
, 0x20);
939 nsect
= (uint64_t)d2h
->lba_hi
[2] << 40 |
940 (uint64_t)d2h
->lba_hi
[1] << 32 |
941 (uint64_t)d2h
->lba_hi
[0] << 24 |
942 (uint64_t)d2h
->lba_lo
[2] << 16 |
943 (uint64_t)d2h
->lba_lo
[1] << 8 |
944 (uint64_t)d2h
->lba_lo
[0];
946 g_assert_cmphex(nsect
, ==, config_sect
);
951 /******************************************************************************/
952 /* Test Interfaces */
953 /******************************************************************************/
956 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
958 static void test_sanity(void)
961 ahci
= ahci_boot(NULL
);
966 * Ensure that the PCI configuration space for the AHCI device is in-line with
967 * the AHCI 1.3 specification for initial values.
969 static void test_pci_spec(void)
972 ahci
= ahci_boot(NULL
);
973 ahci_test_pci_spec(ahci
);
978 * Engage the PCI AHCI device and sanity check the response.
979 * Perform additional PCI config space bringup for the HBA.
981 static void test_pci_enable(void)
984 ahci
= ahci_boot(NULL
);
985 ahci_pci_enable(ahci
);
990 * Investigate the memory mapped regions of the HBA,
991 * and test them for AHCI specification adherence.
993 static void test_hba_spec(void)
997 ahci
= ahci_boot(NULL
);
998 ahci_pci_enable(ahci
);
999 ahci_test_hba_spec(ahci
);
1000 ahci_shutdown(ahci
);
1004 * Engage the HBA functionality of the AHCI PCI device,
1005 * and bring it into a functional idle state.
1007 static void test_hba_enable(void)
1011 ahci
= ahci_boot(NULL
);
1012 ahci_pci_enable(ahci
);
1013 ahci_hba_enable(ahci
);
1014 ahci_shutdown(ahci
);
1018 * Bring up the device and issue an IDENTIFY command.
1019 * Inspect the state of the HBA device and the data returned.
1021 static void test_identify(void)
1025 ahci
= ahci_boot_and_enable(NULL
);
1026 ahci_test_identify(ahci
);
1027 ahci_shutdown(ahci
);
1031 * Fragmented DMA test: Perform a standard 4K DMA read/write
1032 * test, but make sure the physical regions are fragmented to
1033 * be very small, each just 32 bytes, to see how AHCI performs
1034 * with chunks defined to be much less than a sector.
1036 static void test_dma_fragmented(void)
1041 size_t bufsize
= 4096;
1042 unsigned char *tx
= g_malloc(bufsize
);
1043 unsigned char *rx
= g_malloc0(bufsize
);
1046 ahci
= ahci_boot_and_enable(NULL
);
1047 px
= ahci_port_select(ahci
);
1048 ahci_port_clear(ahci
, px
);
1050 /* create pattern */
1051 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
1053 /* Create a DMA buffer in guest memory, and write our pattern to it. */
1054 ptr
= guest_alloc(ahci
->parent
->alloc
, bufsize
);
1056 bufwrite(ptr
, tx
, bufsize
);
1058 cmd
= ahci_command_create(CMD_WRITE_DMA
);
1059 ahci_command_adjust(cmd
, 0, ptr
, bufsize
, 32);
1060 ahci_command_commit(ahci
, cmd
, px
);
1061 ahci_command_issue(ahci
, cmd
);
1062 ahci_command_verify(ahci
, cmd
);
1065 cmd
= ahci_command_create(CMD_READ_DMA
);
1066 ahci_command_adjust(cmd
, 0, ptr
, bufsize
, 32);
1067 ahci_command_commit(ahci
, cmd
, px
);
1068 ahci_command_issue(ahci
, cmd
);
1069 ahci_command_verify(ahci
, cmd
);
1072 /* Read back the guest's receive buffer into local memory */
1073 bufread(ptr
, rx
, bufsize
);
1074 guest_free(ahci
->parent
->alloc
, ptr
);
1076 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1078 ahci_shutdown(ahci
);
1084 static void test_flush(void)
1088 ahci
= ahci_boot_and_enable(NULL
);
1089 ahci_test_flush(ahci
);
1090 ahci_shutdown(ahci
);
1093 static void test_flush_retry(void)
1100 prepare_blkdebug_script(debug_path
, "flush_to_disk");
1101 ahci
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1102 "format=qcow2,cache=writeback,"
1103 "rerror=stop,werror=stop "
1105 "-device ide-hd,drive=drive0 ",
1109 /* Issue Flush Command and wait for error */
1110 port
= ahci_port_select(ahci
);
1111 ahci_port_clear(ahci
, port
);
1112 cmd
= ahci_command_create(CMD_FLUSH_CACHE
);
1113 ahci_command_commit(ahci
, cmd
, port
);
1114 ahci_command_issue_async(ahci
, cmd
);
1115 qmp_eventwait("STOP");
1117 /* Complete the command */
1118 s
= "{'execute':'cont' }";
1120 qmp_eventwait("RESUME");
1121 ahci_command_wait(ahci
, cmd
);
1122 ahci_command_verify(ahci
, cmd
);
1124 ahci_command_free(cmd
);
1125 ahci_shutdown(ahci
);
1129 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
1131 static void test_migrate_sanity(void)
1133 AHCIQState
*src
, *dst
;
1134 const char *uri
= "tcp:127.0.0.1:1234";
1136 src
= ahci_boot("-m 1024 -M q35 "
1137 "-hda %s ", tmp_path
);
1138 dst
= ahci_boot("-m 1024 -M q35 "
1140 "-incoming %s", tmp_path
, uri
);
1142 ahci_migrate(src
, dst
, uri
);
1149 * Simple migration test: Write a pattern, migrate, then read.
1151 static void ahci_migrate_simple(uint8_t cmd_read
, uint8_t cmd_write
)
1153 AHCIQState
*src
, *dst
;
1155 size_t bufsize
= 4096;
1156 unsigned char *tx
= g_malloc(bufsize
);
1157 unsigned char *rx
= g_malloc0(bufsize
);
1159 const char *uri
= "tcp:127.0.0.1:1234";
1161 src
= ahci_boot_and_enable("-m 1024 -M q35 "
1162 "-hda %s ", tmp_path
);
1163 dst
= ahci_boot("-m 1024 -M q35 "
1165 "-incoming %s", tmp_path
, uri
);
1167 set_context(src
->parent
);
1170 px
= ahci_port_select(src
);
1171 ahci_port_clear(src
, px
);
1173 /* create pattern */
1174 for (i
= 0; i
< bufsize
; i
++) {
1175 tx
[i
] = (bufsize
- i
);
1178 /* Write, migrate, then read. */
1179 ahci_io(src
, px
, cmd_write
, tx
, bufsize
, 0);
1180 ahci_migrate(src
, dst
, uri
);
1181 ahci_io(dst
, px
, cmd_read
, rx
, bufsize
, 0);
1183 /* Verify pattern */
1184 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1192 static void test_migrate_dma(void)
1194 ahci_migrate_simple(CMD_READ_DMA
, CMD_WRITE_DMA
);
1197 static void test_migrate_ncq(void)
1199 ahci_migrate_simple(READ_FPDMA_QUEUED
, WRITE_FPDMA_QUEUED
);
1203 * Halted IO Error Test
1205 * Simulate an error on first write, Try to write a pattern,
1206 * Confirm the VM has stopped, resume the VM, verify command
1207 * has completed, then read back the data and verify.
1209 static void ahci_halted_io_test(uint8_t cmd_read
, uint8_t cmd_write
)
1213 size_t bufsize
= 4096;
1214 unsigned char *tx
= g_malloc(bufsize
);
1215 unsigned char *rx
= g_malloc0(bufsize
);
1220 prepare_blkdebug_script(debug_path
, "write_aio");
1222 ahci
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1223 "format=qcow2,cache=writeback,"
1224 "rerror=stop,werror=stop "
1226 "-device ide-hd,drive=drive0 ",
1230 /* Initialize and prepare */
1231 port
= ahci_port_select(ahci
);
1232 ahci_port_clear(ahci
, port
);
1234 for (i
= 0; i
< bufsize
; i
++) {
1235 tx
[i
] = (bufsize
- i
);
1238 /* create DMA source buffer and write pattern */
1239 ptr
= ahci_alloc(ahci
, bufsize
);
1241 memwrite(ptr
, tx
, bufsize
);
1243 /* Attempt to write (and fail) */
1244 cmd
= ahci_guest_io_halt(ahci
, port
, cmd_write
,
1247 /* Attempt to resume the command */
1248 ahci_guest_io_resume(ahci
, cmd
);
1249 ahci_free(ahci
, ptr
);
1251 /* Read back and verify */
1252 ahci_io(ahci
, port
, cmd_read
, rx
, bufsize
, 0);
1253 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1255 /* Cleanup and go home */
1256 ahci_shutdown(ahci
);
1261 static void test_halted_dma(void)
1263 ahci_halted_io_test(CMD_READ_DMA
, CMD_WRITE_DMA
);
1266 static void test_halted_ncq(void)
1268 ahci_halted_io_test(READ_FPDMA_QUEUED
, WRITE_FPDMA_QUEUED
);
1272 * IO Error Migration Test
1274 * Simulate an error on first write, Try to write a pattern,
1275 * Confirm the VM has stopped, migrate, resume the VM,
1276 * verify command has completed, then read back the data and verify.
1278 static void ahci_migrate_halted_io(uint8_t cmd_read
, uint8_t cmd_write
)
1280 AHCIQState
*src
, *dst
;
1282 size_t bufsize
= 4096;
1283 unsigned char *tx
= g_malloc(bufsize
);
1284 unsigned char *rx
= g_malloc0(bufsize
);
1288 const char *uri
= "tcp:127.0.0.1:1234";
1290 prepare_blkdebug_script(debug_path
, "write_aio");
1292 src
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1293 "format=qcow2,cache=writeback,"
1294 "rerror=stop,werror=stop "
1296 "-device ide-hd,drive=drive0 ",
1300 dst
= ahci_boot("-drive file=%s,if=none,id=drive0,"
1301 "format=qcow2,cache=writeback,"
1302 "rerror=stop,werror=stop "
1304 "-device ide-hd,drive=drive0 "
1308 set_context(src
->parent
);
1310 /* Initialize and prepare */
1311 port
= ahci_port_select(src
);
1312 ahci_port_clear(src
, port
);
1314 for (i
= 0; i
< bufsize
; i
++) {
1315 tx
[i
] = (bufsize
- i
);
1318 /* create DMA source buffer and write pattern */
1319 ptr
= ahci_alloc(src
, bufsize
);
1321 memwrite(ptr
, tx
, bufsize
);
1323 /* Write, trigger the VM to stop, migrate, then resume. */
1324 cmd
= ahci_guest_io_halt(src
, port
, cmd_write
,
1326 ahci_migrate(src
, dst
, uri
);
1327 ahci_guest_io_resume(dst
, cmd
);
1328 ahci_free(dst
, ptr
);
1331 ahci_io(dst
, port
, cmd_read
, rx
, bufsize
, 0);
1333 /* Verify TX and RX are identical */
1334 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1336 /* Cleanup and go home. */
1343 static void test_migrate_halted_dma(void)
1345 ahci_migrate_halted_io(CMD_READ_DMA
, CMD_WRITE_DMA
);
1348 static void test_migrate_halted_ncq(void)
1350 ahci_migrate_halted_io(READ_FPDMA_QUEUED
, WRITE_FPDMA_QUEUED
);
1354 * Migration test: Try to flush, migrate, then resume.
1356 static void test_flush_migrate(void)
1358 AHCIQState
*src
, *dst
;
1362 const char *uri
= "tcp:127.0.0.1:1234";
1364 prepare_blkdebug_script(debug_path
, "flush_to_disk");
1366 src
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1367 "cache=writeback,rerror=stop,werror=stop "
1369 "-device ide-hd,drive=drive0 ",
1370 debug_path
, tmp_path
);
1371 dst
= ahci_boot("-drive file=%s,if=none,id=drive0,"
1372 "cache=writeback,rerror=stop,werror=stop "
1374 "-device ide-hd,drive=drive0 "
1375 "-incoming %s", tmp_path
, uri
);
1377 set_context(src
->parent
);
1379 /* Issue Flush Command */
1380 px
= ahci_port_select(src
);
1381 ahci_port_clear(src
, px
);
1382 cmd
= ahci_command_create(CMD_FLUSH_CACHE
);
1383 ahci_command_commit(src
, cmd
, px
);
1384 ahci_command_issue_async(src
, cmd
);
1385 qmp_eventwait("STOP");
1388 ahci_migrate(src
, dst
, uri
);
1390 /* Complete the command */
1391 s
= "{'execute':'cont' }";
1393 qmp_eventwait("RESUME");
1394 ahci_command_wait(dst
, cmd
);
1395 ahci_command_verify(dst
, cmd
);
1397 ahci_command_free(cmd
);
1402 static void test_max(void)
1406 ahci
= ahci_boot_and_enable(NULL
);
1407 ahci_test_max(ahci
);
1408 ahci_shutdown(ahci
);
1411 static void test_reset(void)
1416 ahci
= ahci_boot(NULL
);
1417 ahci_test_pci_spec(ahci
);
1418 ahci_pci_enable(ahci
);
1420 for (i
= 0; i
< 2; i
++) {
1421 ahci_test_hba_spec(ahci
);
1422 ahci_hba_enable(ahci
);
1423 ahci_test_identify(ahci
);
1424 ahci_test_io_rw_simple(ahci
, 4096, 0,
1427 ahci_set(ahci
, AHCI_GHC
, AHCI_GHC_HR
);
1428 ahci_clean_mem(ahci
);
1431 ahci_shutdown(ahci
);
1434 static void test_ncq_simple(void)
1438 ahci
= ahci_boot_and_enable(NULL
);
1439 ahci_test_io_rw_simple(ahci
, 4096, 0,
1441 WRITE_FPDMA_QUEUED
);
1442 ahci_shutdown(ahci
);
1445 /******************************************************************************/
1446 /* AHCI I/O Test Matrix Definitions */
1450 LEN_SIMPLE
= LEN_BEGIN
,
1457 static const char *buff_len_str
[NUM_LENGTHS
] = { "simple", "double",
1461 ADDR_MODE_BEGIN
= 0,
1462 ADDR_MODE_LBA28
= ADDR_MODE_BEGIN
,
1467 static const char *addr_mode_str
[NUM_ADDR_MODES
] = { "lba28", "lba48" };
1471 MODE_PIO
= MODE_BEGIN
,
1476 static const char *io_mode_str
[NUM_MODES
] = { "pio", "dma" };
1487 OFFSET_ZERO
= OFFSET_BEGIN
,
1493 static const char *offset_str
[NUM_OFFSETS
] = { "zero", "low", "high" };
1495 typedef struct AHCIIOTestOptions
{
1496 enum BuffLen length
;
1497 enum AddrMode address_type
;
1498 enum IOMode io_type
;
1499 enum OffsetType offset
;
1500 } AHCIIOTestOptions
;
1502 static uint64_t offset_sector(enum OffsetType ofst
,
1503 enum AddrMode addr_type
,
1515 ceil
= (addr_type
== ADDR_MODE_LBA28
) ? 0xfffffff : 0xffffffffffff;
1516 ceil
= MIN(ceil
, TEST_IMAGE_SECTORS
- 1);
1517 nsectors
= buffsize
/ AHCI_SECTOR_SIZE
;
1518 return ceil
- nsectors
+ 1;
1520 g_assert_not_reached();
1525 * Table of possible I/O ATA commands given a set of enumerations.
1527 static const uint8_t io_cmds
[NUM_MODES
][NUM_ADDR_MODES
][NUM_IO_OPS
] = {
1529 [ADDR_MODE_LBA28
] = {
1530 [IO_READ
] = CMD_READ_PIO
,
1531 [IO_WRITE
] = CMD_WRITE_PIO
},
1532 [ADDR_MODE_LBA48
] = {
1533 [IO_READ
] = CMD_READ_PIO_EXT
,
1534 [IO_WRITE
] = CMD_WRITE_PIO_EXT
}
1537 [ADDR_MODE_LBA28
] = {
1538 [IO_READ
] = CMD_READ_DMA
,
1539 [IO_WRITE
] = CMD_WRITE_DMA
},
1540 [ADDR_MODE_LBA48
] = {
1541 [IO_READ
] = CMD_READ_DMA_EXT
,
1542 [IO_WRITE
] = CMD_WRITE_DMA_EXT
}
1547 * Test a Read/Write pattern using various commands, addressing modes,
1548 * transfer modes, and buffer sizes.
1550 static void test_io_rw_interface(enum AddrMode lba48
, enum IOMode dma
,
1551 unsigned bufsize
, uint64_t sector
)
1555 ahci
= ahci_boot_and_enable(NULL
);
1556 ahci_test_io_rw_simple(ahci
, bufsize
, sector
,
1557 io_cmds
[dma
][lba48
][IO_READ
],
1558 io_cmds
[dma
][lba48
][IO_WRITE
]);
1559 ahci_shutdown(ahci
);
1563 * Demultiplex the test data and invoke the actual test routine.
1565 static void test_io_interface(gconstpointer opaque
)
1567 AHCIIOTestOptions
*opts
= (AHCIIOTestOptions
*)opaque
;
1571 switch (opts
->length
) {
1579 bufsize
= 4096 * 64;
1585 g_assert_not_reached();
1588 sector
= offset_sector(opts
->offset
, opts
->address_type
, bufsize
);
1589 test_io_rw_interface(opts
->address_type
, opts
->io_type
, bufsize
, sector
);
1594 static void create_ahci_io_test(enum IOMode type
, enum AddrMode addr
,
1595 enum BuffLen len
, enum OffsetType offset
)
1598 AHCIIOTestOptions
*opts
= g_malloc(sizeof(AHCIIOTestOptions
));
1601 opts
->address_type
= addr
;
1602 opts
->io_type
= type
;
1603 opts
->offset
= offset
;
1605 name
= g_strdup_printf("ahci/io/%s/%s/%s/%s",
1607 addr_mode_str
[addr
],
1609 offset_str
[offset
]);
1611 qtest_add_data_func(name
, opts
, test_io_interface
);
1615 /******************************************************************************/
1617 int main(int argc
, char **argv
)
1625 static struct option long_options
[] = {
1626 {"pedantic", no_argument
, 0, 'p' },
1630 /* Should be first to utilize g_test functionality, So we can see errors. */
1631 g_test_init(&argc
, &argv
, NULL
);
1634 c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
1645 fprintf(stderr
, "Unrecognized ahci_test option.\n");
1646 g_assert_not_reached();
1650 /* Check architecture */
1651 arch
= qtest_get_arch();
1652 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
1653 g_test_message("Skipping test for non-x86");
1657 /* Create a temporary qcow2 image */
1658 close(mkstemp(tmp_path
));
1659 mkqcow2(tmp_path
, TEST_IMAGE_SIZE_MB
);
1661 /* Create temporary blkdebug instructions */
1662 fd
= mkstemp(debug_path
);
1667 qtest_add_func("/ahci/sanity", test_sanity
);
1668 qtest_add_func("/ahci/pci_spec", test_pci_spec
);
1669 qtest_add_func("/ahci/pci_enable", test_pci_enable
);
1670 qtest_add_func("/ahci/hba_spec", test_hba_spec
);
1671 qtest_add_func("/ahci/hba_enable", test_hba_enable
);
1672 qtest_add_func("/ahci/identify", test_identify
);
1674 for (i
= MODE_BEGIN
; i
< NUM_MODES
; i
++) {
1675 for (j
= ADDR_MODE_BEGIN
; j
< NUM_ADDR_MODES
; j
++) {
1676 for (k
= LEN_BEGIN
; k
< NUM_LENGTHS
; k
++) {
1677 for (m
= OFFSET_BEGIN
; m
< NUM_OFFSETS
; m
++) {
1678 create_ahci_io_test(i
, j
, k
, m
);
1684 qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented
);
1686 qtest_add_func("/ahci/flush/simple", test_flush
);
1687 qtest_add_func("/ahci/flush/retry", test_flush_retry
);
1688 qtest_add_func("/ahci/flush/migrate", test_flush_migrate
);
1690 qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity
);
1691 qtest_add_func("/ahci/migrate/dma/simple", test_migrate_dma
);
1692 qtest_add_func("/ahci/io/dma/lba28/retry", test_halted_dma
);
1693 qtest_add_func("/ahci/migrate/dma/halted", test_migrate_halted_dma
);
1695 qtest_add_func("/ahci/max", test_max
);
1696 qtest_add_func("/ahci/reset", test_reset
);
1698 qtest_add_func("/ahci/io/ncq/simple", test_ncq_simple
);
1699 qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq
);
1700 qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq
);
1701 qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq
);