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
];
100 /*** Test Setup & Teardown ***/
103 * Start a Q35 machine and bookmark a handle to the AHCI device.
105 static AHCIQState
*ahci_vboot(const char *cli
, va_list ap
)
109 s
= g_malloc0(sizeof(AHCIQState
));
110 s
->parent
= qtest_pc_vboot(cli
, ap
);
111 alloc_set_flags(s
->parent
->alloc
, ALLOC_LEAK_ASSERT
);
113 /* Verify that we have an AHCI device present. */
114 s
->dev
= get_ahci_device(&s
->fingerprint
);
120 * Start a Q35 machine and bookmark a handle to the AHCI device.
122 static AHCIQState
*ahci_boot(const char *cli
, ...)
129 s
= ahci_vboot(cli
, ap
);
132 cli
= "-drive if=none,id=drive0,file=%s,cache=writeback,serial=%s"
135 "-device ide-hd,drive=drive0 "
136 "-global ide-hd.ver=%s";
137 s
= ahci_boot(cli
, tmp_path
, "testdisk", "version");
144 * Clean up the PCI device, then terminate the QEMU instance.
146 static void ahci_shutdown(AHCIQState
*ahci
)
148 QOSState
*qs
= ahci
->parent
;
149 ahci_clean_mem(ahci
);
150 free_ahci_device(ahci
->dev
);
156 * Boot and fully enable the HBA device.
157 * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
159 static AHCIQState
*ahci_boot_and_enable(const char *cli
, ...)
166 ahci
= ahci_vboot(cli
, ap
);
169 ahci
= ahci_boot(NULL
);
172 ahci_pci_enable(ahci
);
173 ahci_hba_enable(ahci
);
178 /*** Specification Adherence Tests ***/
181 * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
183 static void ahci_test_pci_spec(AHCIQState
*ahci
)
189 /* Most of these bits should start cleared until we turn them on. */
190 data
= qpci_config_readw(ahci
->dev
, PCI_COMMAND
);
191 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_MEMORY
);
192 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_MASTER
);
193 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_SPECIAL
); /* Reserved */
194 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_VGA_PALETTE
); /* Reserved */
195 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_PARITY
);
196 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_WAIT
); /* Reserved */
197 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_SERR
);
198 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_FAST_BACK
);
199 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_INTX_DISABLE
);
200 ASSERT_BIT_CLEAR(data
, 0xF800); /* Reserved */
202 data
= qpci_config_readw(ahci
->dev
, PCI_STATUS
);
203 ASSERT_BIT_CLEAR(data
, 0x01 | 0x02 | 0x04); /* Reserved */
204 ASSERT_BIT_CLEAR(data
, PCI_STATUS_INTERRUPT
);
205 ASSERT_BIT_SET(data
, PCI_STATUS_CAP_LIST
); /* must be set */
206 ASSERT_BIT_CLEAR(data
, PCI_STATUS_UDF
); /* Reserved */
207 ASSERT_BIT_CLEAR(data
, PCI_STATUS_PARITY
);
208 ASSERT_BIT_CLEAR(data
, PCI_STATUS_SIG_TARGET_ABORT
);
209 ASSERT_BIT_CLEAR(data
, PCI_STATUS_REC_TARGET_ABORT
);
210 ASSERT_BIT_CLEAR(data
, PCI_STATUS_REC_MASTER_ABORT
);
211 ASSERT_BIT_CLEAR(data
, PCI_STATUS_SIG_SYSTEM_ERROR
);
212 ASSERT_BIT_CLEAR(data
, PCI_STATUS_DETECTED_PARITY
);
214 /* RID occupies the low byte, CCs occupy the high three. */
215 datal
= qpci_config_readl(ahci
->dev
, PCI_CLASS_REVISION
);
217 /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
218 * Though in practice this is likely seldom true. */
219 ASSERT_BIT_CLEAR(datal
, 0xFF);
222 /* BCC *must* equal 0x01. */
223 g_assert_cmphex(PCI_BCC(datal
), ==, 0x01);
224 if (PCI_SCC(datal
) == 0x01) {
226 ASSERT_BIT_SET(0x80000000, datal
);
227 ASSERT_BIT_CLEAR(0x60000000, datal
);
228 } else if (PCI_SCC(datal
) == 0x04) {
230 g_assert_cmphex(PCI_PI(datal
), ==, 0);
231 } else if (PCI_SCC(datal
) == 0x06) {
233 g_assert_cmphex(PCI_PI(datal
), ==, 0x01);
235 g_assert_not_reached();
238 datab
= qpci_config_readb(ahci
->dev
, PCI_CACHE_LINE_SIZE
);
239 g_assert_cmphex(datab
, ==, 0);
241 datab
= qpci_config_readb(ahci
->dev
, PCI_LATENCY_TIMER
);
242 g_assert_cmphex(datab
, ==, 0);
244 /* Only the bottom 7 bits must be off. */
245 datab
= qpci_config_readb(ahci
->dev
, PCI_HEADER_TYPE
);
246 ASSERT_BIT_CLEAR(datab
, 0x7F);
248 /* BIST is optional, but the low 7 bits must always start off regardless. */
249 datab
= qpci_config_readb(ahci
->dev
, PCI_BIST
);
250 ASSERT_BIT_CLEAR(datab
, 0x7F);
252 /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
253 datal
= qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
254 g_assert_cmphex(datal
, ==, 0);
256 qpci_config_writel(ahci
->dev
, PCI_BASE_ADDRESS_5
, 0xFFFFFFFF);
257 datal
= qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
258 /* ABAR must be 32-bit, memory mapped, non-prefetchable and
259 * must be >= 512 bytes. To that end, bits 0-8 must be off. */
260 ASSERT_BIT_CLEAR(datal
, 0xFF);
262 /* Capability list MUST be present, */
263 datal
= qpci_config_readl(ahci
->dev
, PCI_CAPABILITY_LIST
);
264 /* But these bits are reserved. */
265 ASSERT_BIT_CLEAR(datal
, ~0xFF);
266 g_assert_cmphex(datal
, !=, 0);
268 /* Check specification adherence for capability extenstions. */
269 data
= qpci_config_readw(ahci
->dev
, datal
);
271 switch (ahci
->fingerprint
) {
272 case AHCI_INTEL_ICH9
:
273 /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
274 g_assert_cmphex((data
& 0xFF), ==, PCI_CAP_ID_MSI
);
277 /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
278 g_assert_cmphex((data
& 0xFF), ==, PCI_CAP_ID_PM
);
281 ahci_test_pci_caps(ahci
, data
, (uint8_t)datal
);
284 datal
= qpci_config_readl(ahci
->dev
, PCI_CAPABILITY_LIST
+ 4);
285 g_assert_cmphex(datal
, ==, 0);
287 /* IPIN might vary, but ILINE must be off. */
288 datab
= qpci_config_readb(ahci
->dev
, PCI_INTERRUPT_LINE
);
289 g_assert_cmphex(datab
, ==, 0);
293 * Test PCI capabilities for AHCI specification adherence.
295 static void ahci_test_pci_caps(AHCIQState
*ahci
, uint16_t header
,
298 uint8_t cid
= header
& 0xFF;
299 uint8_t next
= header
>> 8;
301 g_test_message("CID: %02x; next: %02x", cid
, next
);
305 ahci_test_pmcap(ahci
, offset
);
308 ahci_test_msicap(ahci
, offset
);
310 case PCI_CAP_ID_SATA
:
311 ahci_test_satacap(ahci
, offset
);
315 g_test_message("Unknown CAP 0x%02x", cid
);
319 ahci_test_pci_caps(ahci
, qpci_config_readw(ahci
->dev
, next
), next
);
324 * Test SATA PCI capabilitity for AHCI specification adherence.
326 static void ahci_test_satacap(AHCIQState
*ahci
, uint8_t offset
)
331 g_test_message("Verifying SATACAP");
333 /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
334 dataw
= qpci_config_readw(ahci
->dev
, offset
+ 2);
335 g_assert_cmphex(dataw
, ==, 0x10);
337 /* Grab the SATACR1 register. */
338 datal
= qpci_config_readw(ahci
->dev
, offset
+ 4);
340 switch (datal
& 0x0F) {
341 case 0x04: /* BAR0 */
342 case 0x05: /* BAR1 */
346 case 0x09: /* BAR5 */
347 case 0x0F: /* Immediately following SATACR1 in PCI config space. */
350 /* Invalid BARLOC for the Index Data Pair. */
351 g_assert_not_reached();
355 g_assert_cmphex((datal
>> 24), ==, 0x00);
359 * Test MSI PCI capability for AHCI specification adherence.
361 static void ahci_test_msicap(AHCIQState
*ahci
, uint8_t offset
)
366 g_test_message("Verifying MSICAP");
368 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_FLAGS
);
369 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_ENABLE
);
370 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_QSIZE
);
371 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_RESERVED
);
373 datal
= qpci_config_readl(ahci
->dev
, offset
+ PCI_MSI_ADDRESS_LO
);
374 g_assert_cmphex(datal
, ==, 0);
376 if (dataw
& PCI_MSI_FLAGS_64BIT
) {
377 g_test_message("MSICAP is 64bit");
378 datal
= qpci_config_readl(ahci
->dev
, offset
+ PCI_MSI_ADDRESS_HI
);
379 g_assert_cmphex(datal
, ==, 0);
380 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_DATA_64
);
381 g_assert_cmphex(dataw
, ==, 0);
383 g_test_message("MSICAP is 32bit");
384 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_DATA_32
);
385 g_assert_cmphex(dataw
, ==, 0);
390 * Test Power Management PCI capability for AHCI specification adherence.
392 static void ahci_test_pmcap(AHCIQState
*ahci
, uint8_t offset
)
396 g_test_message("Verifying PMCAP");
398 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_PM_PMC
);
399 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_PME_CLOCK
);
400 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_RESERVED
);
401 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_D1
);
402 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_D2
);
404 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_PM_CTRL
);
405 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_STATE_MASK
);
406 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_RESERVED
);
407 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_DATA_SEL_MASK
);
408 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_DATA_SCALE_MASK
);
411 static void ahci_test_hba_spec(AHCIQState
*ahci
)
419 g_assert(ahci
!= NULL
);
422 * Note that the AHCI spec does expect the BIOS to set up a few things:
423 * CAP.SSS - Support for staggered spin-up (t/f)
424 * CAP.SMPS - Support for mechanical presence switches (t/f)
425 * PI - Ports Implemented (1-32)
426 * PxCMD.HPCP - Hot Plug Capable Port
427 * PxCMD.MPSP - Mechanical Presence Switch Present
428 * PxCMD.CPD - Cold Presence Detection support
430 * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
431 * Foreach Port Implemented:
432 * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
433 * -PxCLB/U and PxFB/U are set to valid regions in memory
434 * -PxSUD is set to 1.
435 * -PxSSTS.DET is polled for presence; if detected, we continue:
436 * -PxSERR is cleared with 1's.
437 * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
438 * the device is ready.
441 /* 1 CAP - Capabilities Register */
442 ahci
->cap
= ahci_rreg(ahci
, AHCI_CAP
);
443 ASSERT_BIT_CLEAR(ahci
->cap
, AHCI_CAP_RESERVED
);
445 /* 2 GHC - Global Host Control */
446 reg
= ahci_rreg(ahci
, AHCI_GHC
);
447 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_HR
);
448 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_IE
);
449 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_MRSM
);
450 if (BITSET(ahci
->cap
, AHCI_CAP_SAM
)) {
451 g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
452 ASSERT_BIT_SET(reg
, AHCI_GHC_AE
);
454 g_test_message("Supports AHCI/Legacy mix.");
455 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_AE
);
458 /* 3 IS - Interrupt Status */
459 reg
= ahci_rreg(ahci
, AHCI_IS
);
460 g_assert_cmphex(reg
, ==, 0);
462 /* 4 PI - Ports Implemented */
463 ports
= ahci_rreg(ahci
, AHCI_PI
);
464 /* Ports Implemented must be non-zero. */
465 g_assert_cmphex(ports
, !=, 0);
466 /* Ports Implemented must be <= Number of Ports. */
467 nports_impl
= ctpopl(ports
);
468 g_assert_cmpuint(((AHCI_CAP_NP
& ahci
->cap
) + 1), >=, nports_impl
);
470 /* Ports must be within the proper range. Given a mapping of SIZE,
471 * 256 bytes are used for global HBA control, and the rest is used
472 * for ports data, at 0x80 bytes each. */
473 g_assert_cmphex(ahci
->barsize
, >, 0);
474 maxports
= (ahci
->barsize
- HBA_DATA_REGION_SIZE
) / HBA_PORT_DATA_SIZE
;
475 /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
476 g_assert_cmphex((reg
>> maxports
), ==, 0);
479 reg
= ahci_rreg(ahci
, AHCI_VS
);
481 case AHCI_VERSION_0_95
:
482 case AHCI_VERSION_1_0
:
483 case AHCI_VERSION_1_1
:
484 case AHCI_VERSION_1_2
:
485 case AHCI_VERSION_1_3
:
488 g_assert_not_reached();
491 /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
492 reg
= ahci_rreg(ahci
, AHCI_CCCCTL
);
493 if (BITSET(ahci
->cap
, AHCI_CAP_CCCS
)) {
494 ASSERT_BIT_CLEAR(reg
, AHCI_CCCCTL_EN
);
495 ASSERT_BIT_CLEAR(reg
, AHCI_CCCCTL_RESERVED
);
496 ASSERT_BIT_SET(reg
, AHCI_CCCCTL_CC
);
497 ASSERT_BIT_SET(reg
, AHCI_CCCCTL_TV
);
499 g_assert_cmphex(reg
, ==, 0);
503 reg
= ahci_rreg(ahci
, AHCI_CCCPORTS
);
504 /* Must be zeroes initially regardless of CAP.CCCS */
505 g_assert_cmphex(reg
, ==, 0);
508 reg
= ahci_rreg(ahci
, AHCI_EMLOC
);
509 if (BITCLR(ahci
->cap
, AHCI_CAP_EMS
)) {
510 g_assert_cmphex(reg
, ==, 0);
514 reg
= ahci_rreg(ahci
, AHCI_EMCTL
);
515 if (BITSET(ahci
->cap
, AHCI_CAP_EMS
)) {
516 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_STSMR
);
517 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_CTLTM
);
518 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_CTLRST
);
519 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_RESERVED
);
521 g_assert_cmphex(reg
, ==, 0);
524 /* 10 CAP2 -- Capabilities Extended */
525 ahci
->cap2
= ahci_rreg(ahci
, AHCI_CAP2
);
526 ASSERT_BIT_CLEAR(ahci
->cap2
, AHCI_CAP2_RESERVED
);
528 /* 11 BOHC -- Bios/OS Handoff Control */
529 reg
= ahci_rreg(ahci
, AHCI_BOHC
);
530 g_assert_cmphex(reg
, ==, 0);
532 /* 12 -- 23: Reserved */
533 g_test_message("Verifying HBA reserved area is empty.");
534 for (i
= AHCI_RESERVED
; i
< AHCI_NVMHCI
; ++i
) {
535 reg
= ahci_rreg(ahci
, i
);
536 g_assert_cmphex(reg
, ==, 0);
539 /* 24 -- 39: NVMHCI */
540 if (BITCLR(ahci
->cap2
, AHCI_CAP2_NVMP
)) {
541 g_test_message("Verifying HBA/NVMHCI area is empty.");
542 for (i
= AHCI_NVMHCI
; i
< AHCI_VENDOR
; ++i
) {
543 reg
= ahci_rreg(ahci
, i
);
544 g_assert_cmphex(reg
, ==, 0);
548 /* 40 -- 63: Vendor */
549 g_test_message("Verifying HBA/Vendor area is empty.");
550 for (i
= AHCI_VENDOR
; i
< AHCI_PORTS
; ++i
) {
551 reg
= ahci_rreg(ahci
, i
);
552 g_assert_cmphex(reg
, ==, 0);
555 /* 64 -- XX: Port Space */
556 for (i
= 0; ports
|| (i
< maxports
); ports
>>= 1, ++i
) {
557 if (BITSET(ports
, 0x1)) {
558 g_test_message("Testing port %u for spec", i
);
559 ahci_test_port_spec(ahci
, i
);
562 uint16_t low
= AHCI_PORTS
+ (32 * i
);
563 uint16_t high
= AHCI_PORTS
+ (32 * (i
+ 1));
564 g_test_message("Asserting unimplemented port %u "
565 "(reg [%u-%u]) is empty.",
567 for (j
= low
; j
< high
; ++j
) {
568 reg
= ahci_rreg(ahci
, j
);
569 g_assert_cmphex(reg
, ==, 0);
576 * Test the memory space for one port for specification adherence.
578 static void ahci_test_port_spec(AHCIQState
*ahci
, uint8_t port
)
584 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CLB
);
585 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CLB_RESERVED
);
588 if (BITCLR(ahci
->cap
, AHCI_CAP_S64A
)) {
589 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CLBU
);
590 g_assert_cmphex(reg
, ==, 0);
594 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FB
);
595 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FB_RESERVED
);
598 if (BITCLR(ahci
->cap
, AHCI_CAP_S64A
)) {
599 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FBU
);
600 g_assert_cmphex(reg
, ==, 0);
604 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_IS
);
605 g_assert_cmphex(reg
, ==, 0);
608 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_IE
);
609 g_assert_cmphex(reg
, ==, 0);
612 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CMD
);
613 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FRE
);
614 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_RESERVED
);
615 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CCS
);
616 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FR
);
617 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CR
);
618 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_PMA
); /* And RW only if CAP.SPM */
619 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_APSTE
); /* RW only if CAP2.APST */
620 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ATAPI
);
621 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_DLAE
);
622 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ALPE
); /* RW only if CAP.SALP */
623 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ASP
); /* RW only if CAP.SALP */
624 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ICC
);
625 /* If CPDetect support does not exist, CPState must be off. */
626 if (BITCLR(reg
, AHCI_PX_CMD_CPD
)) {
627 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CPS
);
629 /* If MPSPresence is not set, MPSState must be off. */
630 if (BITCLR(reg
, AHCI_PX_CMD_MPSP
)) {
631 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSS
);
633 /* If we do not support MPS, MPSS and MPSP must be off. */
634 if (BITCLR(ahci
->cap
, AHCI_CAP_SMPS
)) {
635 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSS
);
636 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSP
);
638 /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
639 if (BITANY(reg
, AHCI_PX_CMD_CPD
| AHCI_PX_CMD_MPSP
)) {
640 ASSERT_BIT_SET(reg
, AHCI_PX_CMD_HPCP
);
642 /* HPCP and ESP cannot both be active. */
643 g_assert(!BITSET(reg
, AHCI_PX_CMD_HPCP
| AHCI_PX_CMD_ESP
));
644 /* If CAP.FBSS is not set, FBSCP must not be set. */
645 if (BITCLR(ahci
->cap
, AHCI_CAP_FBSS
)) {
646 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FBSCP
);
650 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_RES1
);
651 g_assert_cmphex(reg
, ==, 0);
654 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_TFD
);
655 /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
656 * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
657 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_ERR
);
658 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_CS1
);
659 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_DRQ
);
660 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_CS2
);
661 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_STS_BSY
);
662 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_ERR
);
663 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_RESERVED
);
666 /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
667 * Even when GHC.ST is zero, the AHCI HBA may receive the initial
668 * D2H register FIS and update the signature asynchronously,
669 * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
671 /* (10) SSTS / SCR0: SStatus */
672 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SSTS
);
673 ASSERT_BIT_CLEAR(reg
, AHCI_PX_SSTS_RESERVED
);
674 /* Even though the register should be 0 at boot, it is asynchronous and
675 * prone to change, so we cannot test any well known value. */
677 /* (11) SCTL / SCR2: SControl */
678 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SCTL
);
679 g_assert_cmphex(reg
, ==, 0);
681 /* (12) SERR / SCR1: SError */
682 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SERR
);
683 g_assert_cmphex(reg
, ==, 0);
685 /* (13) SACT / SCR3: SActive */
686 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SACT
);
687 g_assert_cmphex(reg
, ==, 0);
690 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CI
);
691 g_assert_cmphex(reg
, ==, 0);
694 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SNTF
);
695 g_assert_cmphex(reg
, ==, 0);
698 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FBS
);
699 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_EN
);
700 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DEC
);
701 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_SDE
);
702 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DEV
);
703 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DWE
);
704 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_RESERVED
);
705 if (BITSET(ahci
->cap
, AHCI_CAP_FBSS
)) {
706 /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
707 g_assert((reg
& AHCI_PX_FBS_ADO
) >> ctzl(AHCI_PX_FBS_ADO
) >= 2);
710 /* [17 -- 27] RESERVED */
711 for (i
= AHCI_PX_RES2
; i
< AHCI_PX_VS
; ++i
) {
712 reg
= ahci_px_rreg(ahci
, port
, i
);
713 g_assert_cmphex(reg
, ==, 0);
716 /* [28 -- 31] Vendor-Specific */
717 for (i
= AHCI_PX_VS
; i
< 32; ++i
) {
718 reg
= ahci_px_rreg(ahci
, port
, i
);
720 g_test_message("INFO: Vendor register %u non-empty", i
);
726 * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
727 * device we see, then read and check the response.
729 static void ahci_test_identify(AHCIQState
*ahci
)
735 const size_t buffsize
= 512;
737 g_assert(ahci
!= NULL
);
740 * This serves as a bit of a tutorial on AHCI device programming:
742 * (1) Create a data buffer for the IDENTIFY response to be sent to
743 * (2) Create a Command Table buffer, where we will store the
744 * command and PRDT (Physical Region Descriptor Table)
745 * (3) Construct an FIS host-to-device command structure, and write it to
746 * the top of the Command Table buffer.
747 * (4) Create one or more Physical Region Descriptors (PRDs) that describe
748 * a location in memory where data may be stored/retrieved.
749 * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
750 * (6) Each AHCI port has up to 32 command slots. Each slot contains a
751 * header that points to a Command Table buffer. Pick an unused slot
752 * and update it to point to the Command Table we have built.
753 * (7) Now: Command #n points to our Command Table, and our Command Table
754 * contains the FIS (that describes our command) and the PRDTL, which
755 * describes our buffer.
756 * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
757 * #n is ready for processing.
760 /* Pick the first implemented and running port */
761 px
= ahci_port_select(ahci
);
762 g_test_message("Selected port %u for test", px
);
764 /* Clear out the FIS Receive area and any pending interrupts. */
765 ahci_port_clear(ahci
, px
);
767 /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
768 ahci_io(ahci
, px
, CMD_IDENTIFY
, &buff
, buffsize
, 0);
770 /* Check serial number/version in the buffer */
771 /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
772 * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
773 * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
774 * as a consequence, only needs to unchunk the data on LE machines. */
775 string_bswap16(&buff
[10], 20);
776 rc
= memcmp(&buff
[10], "testdisk ", 20);
777 g_assert_cmphex(rc
, ==, 0);
779 string_bswap16(&buff
[23], 8);
780 rc
= memcmp(&buff
[23], "version ", 8);
781 g_assert_cmphex(rc
, ==, 0);
783 sect_size
= le16_to_cpu(*((uint16_t *)(&buff
[5])));
784 g_assert_cmphex(sect_size
, ==, AHCI_SECTOR_SIZE
);
787 static void ahci_test_io_rw_simple(AHCIQState
*ahci
, unsigned bufsize
,
788 uint64_t sector
, uint8_t read_cmd
,
793 unsigned char *tx
= g_malloc(bufsize
);
794 unsigned char *rx
= g_malloc0(bufsize
);
796 g_assert(ahci
!= NULL
);
798 /* Pick the first running port and clear it. */
799 port
= ahci_port_select(ahci
);
800 ahci_port_clear(ahci
, port
);
802 /*** Create pattern and transfer to guest ***/
803 /* Data buffer in the guest */
804 ptr
= ahci_alloc(ahci
, bufsize
);
807 /* Write some indicative pattern to our buffer. */
808 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
809 memwrite(ptr
, tx
, bufsize
);
811 /* Write this buffer to disk, then read it back to the DMA buffer. */
812 ahci_guest_io(ahci
, port
, write_cmd
, ptr
, bufsize
, sector
);
813 qmemset(ptr
, 0x00, bufsize
);
814 ahci_guest_io(ahci
, port
, read_cmd
, ptr
, bufsize
, sector
);
816 /*** Read back the Data ***/
817 memread(ptr
, rx
, bufsize
);
818 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
820 ahci_free(ahci
, ptr
);
825 static void ahci_test_nondata(AHCIQState
*ahci
, uint8_t ide_cmd
)
831 px
= ahci_port_select(ahci
);
832 ahci_port_clear(ahci
, px
);
835 cmd
= ahci_command_create(ide_cmd
);
836 ahci_command_commit(ahci
, cmd
, px
);
837 ahci_command_issue(ahci
, cmd
);
838 ahci_command_verify(ahci
, cmd
);
839 ahci_command_free(cmd
);
842 static void ahci_test_flush(AHCIQState
*ahci
)
844 ahci_test_nondata(ahci
, CMD_FLUSH_CACHE
);
848 /******************************************************************************/
849 /* Test Interfaces */
850 /******************************************************************************/
853 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
855 static void test_sanity(void)
858 ahci
= ahci_boot(NULL
);
863 * Ensure that the PCI configuration space for the AHCI device is in-line with
864 * the AHCI 1.3 specification for initial values.
866 static void test_pci_spec(void)
869 ahci
= ahci_boot(NULL
);
870 ahci_test_pci_spec(ahci
);
875 * Engage the PCI AHCI device and sanity check the response.
876 * Perform additional PCI config space bringup for the HBA.
878 static void test_pci_enable(void)
881 ahci
= ahci_boot(NULL
);
882 ahci_pci_enable(ahci
);
887 * Investigate the memory mapped regions of the HBA,
888 * and test them for AHCI specification adherence.
890 static void test_hba_spec(void)
894 ahci
= ahci_boot(NULL
);
895 ahci_pci_enable(ahci
);
896 ahci_test_hba_spec(ahci
);
901 * Engage the HBA functionality of the AHCI PCI device,
902 * and bring it into a functional idle state.
904 static void test_hba_enable(void)
908 ahci
= ahci_boot(NULL
);
909 ahci_pci_enable(ahci
);
910 ahci_hba_enable(ahci
);
915 * Bring up the device and issue an IDENTIFY command.
916 * Inspect the state of the HBA device and the data returned.
918 static void test_identify(void)
922 ahci
= ahci_boot_and_enable(NULL
);
923 ahci_test_identify(ahci
);
928 * Fragmented DMA test: Perform a standard 4K DMA read/write
929 * test, but make sure the physical regions are fragmented to
930 * be very small, each just 32 bytes, to see how AHCI performs
931 * with chunks defined to be much less than a sector.
933 static void test_dma_fragmented(void)
938 size_t bufsize
= 4096;
939 unsigned char *tx
= g_malloc(bufsize
);
940 unsigned char *rx
= g_malloc0(bufsize
);
943 ahci
= ahci_boot_and_enable(NULL
);
944 px
= ahci_port_select(ahci
);
945 ahci_port_clear(ahci
, px
);
948 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
950 /* Create a DMA buffer in guest memory, and write our pattern to it. */
951 ptr
= guest_alloc(ahci
->parent
->alloc
, bufsize
);
953 memwrite(ptr
, tx
, bufsize
);
955 cmd
= ahci_command_create(CMD_WRITE_DMA
);
956 ahci_command_adjust(cmd
, 0, ptr
, bufsize
, 32);
957 ahci_command_commit(ahci
, cmd
, px
);
958 ahci_command_issue(ahci
, cmd
);
959 ahci_command_verify(ahci
, cmd
);
962 cmd
= ahci_command_create(CMD_READ_DMA
);
963 ahci_command_adjust(cmd
, 0, ptr
, bufsize
, 32);
964 ahci_command_commit(ahci
, cmd
, px
);
965 ahci_command_issue(ahci
, cmd
);
966 ahci_command_verify(ahci
, cmd
);
969 /* Read back the guest's receive buffer into local memory */
970 memread(ptr
, rx
, bufsize
);
971 guest_free(ahci
->parent
->alloc
, ptr
);
973 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
981 static void test_flush(void)
985 ahci
= ahci_boot_and_enable(NULL
);
986 ahci_test_flush(ahci
);
990 static void test_flush_retry(void)
997 prepare_blkdebug_script(debug_path
, "flush_to_disk");
998 ahci
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
999 "format=qcow2,cache=writeback,"
1000 "rerror=stop,werror=stop "
1002 "-device ide-hd,drive=drive0 ",
1006 /* Issue Flush Command */
1007 port
= ahci_port_select(ahci
);
1008 ahci_port_clear(ahci
, port
);
1009 cmd
= ahci_command_create(CMD_FLUSH_CACHE
);
1010 ahci_command_commit(ahci
, cmd
, port
);
1011 ahci_command_issue_async(ahci
, cmd
);
1012 qmp_eventwait("STOP");
1014 /* Complete the command */
1015 s
= "{'execute':'cont' }";
1017 qmp_eventwait("RESUME");
1018 ahci_command_wait(ahci
, cmd
);
1019 ahci_command_verify(ahci
, cmd
);
1021 ahci_command_free(cmd
);
1022 ahci_shutdown(ahci
);
1025 /******************************************************************************/
1026 /* AHCI I/O Test Matrix Definitions */
1030 LEN_SIMPLE
= LEN_BEGIN
,
1037 static const char *buff_len_str
[NUM_LENGTHS
] = { "simple", "double",
1041 ADDR_MODE_BEGIN
= 0,
1042 ADDR_MODE_LBA28
= ADDR_MODE_BEGIN
,
1047 static const char *addr_mode_str
[NUM_ADDR_MODES
] = { "lba28", "lba48" };
1051 MODE_PIO
= MODE_BEGIN
,
1056 static const char *io_mode_str
[NUM_MODES
] = { "pio", "dma" };
1067 OFFSET_ZERO
= OFFSET_BEGIN
,
1073 static const char *offset_str
[NUM_OFFSETS
] = { "zero", "low", "high" };
1075 typedef struct AHCIIOTestOptions
{
1076 enum BuffLen length
;
1077 enum AddrMode address_type
;
1078 enum IOMode io_type
;
1079 enum OffsetType offset
;
1080 } AHCIIOTestOptions
;
1082 static uint64_t offset_sector(enum OffsetType ofst
,
1083 enum AddrMode addr_type
,
1095 ceil
= (addr_type
== ADDR_MODE_LBA28
) ? 0xfffffff : 0xffffffffffff;
1096 ceil
= MIN(ceil
, TEST_IMAGE_SECTORS
- 1);
1097 nsectors
= buffsize
/ AHCI_SECTOR_SIZE
;
1098 return ceil
- nsectors
+ 1;
1100 g_assert_not_reached();
1105 * Table of possible I/O ATA commands given a set of enumerations.
1107 static const uint8_t io_cmds
[NUM_MODES
][NUM_ADDR_MODES
][NUM_IO_OPS
] = {
1109 [ADDR_MODE_LBA28
] = {
1110 [IO_READ
] = CMD_READ_PIO
,
1111 [IO_WRITE
] = CMD_WRITE_PIO
},
1112 [ADDR_MODE_LBA48
] = {
1113 [IO_READ
] = CMD_READ_PIO_EXT
,
1114 [IO_WRITE
] = CMD_WRITE_PIO_EXT
}
1117 [ADDR_MODE_LBA28
] = {
1118 [IO_READ
] = CMD_READ_DMA
,
1119 [IO_WRITE
] = CMD_WRITE_DMA
},
1120 [ADDR_MODE_LBA48
] = {
1121 [IO_READ
] = CMD_READ_DMA_EXT
,
1122 [IO_WRITE
] = CMD_WRITE_DMA_EXT
}
1127 * Test a Read/Write pattern using various commands, addressing modes,
1128 * transfer modes, and buffer sizes.
1130 static void test_io_rw_interface(enum AddrMode lba48
, enum IOMode dma
,
1131 unsigned bufsize
, uint64_t sector
)
1135 ahci
= ahci_boot_and_enable(NULL
);
1136 ahci_test_io_rw_simple(ahci
, bufsize
, sector
,
1137 io_cmds
[dma
][lba48
][IO_READ
],
1138 io_cmds
[dma
][lba48
][IO_WRITE
]);
1139 ahci_shutdown(ahci
);
1143 * Demultiplex the test data and invoke the actual test routine.
1145 static void test_io_interface(gconstpointer opaque
)
1147 AHCIIOTestOptions
*opts
= (AHCIIOTestOptions
*)opaque
;
1151 switch (opts
->length
) {
1159 bufsize
= 4096 * 64;
1165 g_assert_not_reached();
1168 sector
= offset_sector(opts
->offset
, opts
->address_type
, bufsize
);
1169 test_io_rw_interface(opts
->address_type
, opts
->io_type
, bufsize
, sector
);
1174 static void create_ahci_io_test(enum IOMode type
, enum AddrMode addr
,
1175 enum BuffLen len
, enum OffsetType offset
)
1177 static const char *arch
;
1179 AHCIIOTestOptions
*opts
= g_malloc(sizeof(AHCIIOTestOptions
));
1182 opts
->address_type
= addr
;
1183 opts
->io_type
= type
;
1184 opts
->offset
= offset
;
1187 arch
= qtest_get_arch();
1190 name
= g_strdup_printf("/%s/ahci/io/%s/%s/%s/%s", arch
,
1192 addr_mode_str
[addr
],
1194 offset_str
[offset
]);
1196 g_test_add_data_func(name
, opts
, test_io_interface
);
1200 /******************************************************************************/
1202 int main(int argc
, char **argv
)
1210 static struct option long_options
[] = {
1211 {"pedantic", no_argument
, 0, 'p' },
1215 /* Should be first to utilize g_test functionality, So we can see errors. */
1216 g_test_init(&argc
, &argv
, NULL
);
1219 c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
1230 fprintf(stderr
, "Unrecognized ahci_test option.\n");
1231 g_assert_not_reached();
1235 /* Check architecture */
1236 arch
= qtest_get_arch();
1237 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
1238 g_test_message("Skipping test for non-x86");
1242 /* Create a temporary qcow2 image */
1243 close(mkstemp(tmp_path
));
1244 mkqcow2(tmp_path
, TEST_IMAGE_SIZE_MB
);
1246 /* Create temporary blkdebug instructions */
1247 fd
= mkstemp(debug_path
);
1252 qtest_add_func("/ahci/sanity", test_sanity
);
1253 qtest_add_func("/ahci/pci_spec", test_pci_spec
);
1254 qtest_add_func("/ahci/pci_enable", test_pci_enable
);
1255 qtest_add_func("/ahci/hba_spec", test_hba_spec
);
1256 qtest_add_func("/ahci/hba_enable", test_hba_enable
);
1257 qtest_add_func("/ahci/identify", test_identify
);
1259 for (i
= MODE_BEGIN
; i
< NUM_MODES
; i
++) {
1260 for (j
= ADDR_MODE_BEGIN
; j
< NUM_ADDR_MODES
; j
++) {
1261 for (k
= LEN_BEGIN
; k
< NUM_LENGTHS
; k
++) {
1262 for (m
= OFFSET_BEGIN
; m
< NUM_OFFSETS
; m
++) {
1263 create_ahci_io_test(i
, j
, k
, m
);
1269 qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented
);
1271 qtest_add_func("/ahci/flush/simple", test_flush
);
1272 qtest_add_func("/ahci/flush/retry", test_flush_retry
);