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
25 #include "qemu/osdep.h"
29 #include "libqos/libqos-pc.h"
30 #include "libqos/ahci.h"
31 #include "libqos/pci-pc.h"
33 #include "qemu-common.h"
34 #include "qemu/host-utils.h"
36 #include "hw/pci/pci_ids.h"
37 #include "hw/pci/pci_regs.h"
39 /* Test images sizes in MB */
40 #define TEST_IMAGE_SIZE_MB_LARGE (200 * 1024)
41 #define TEST_IMAGE_SIZE_MB_SMALL 64
44 static char tmp_path
[] = "/tmp/qtest.XXXXXX";
45 static char debug_path
[] = "/tmp/qtest-blkdebug.XXXXXX";
46 static char mig_socket
[] = "/tmp/qtest-migration.XXXXXX";
47 static bool ahci_pedantic
;
48 static const char *imgfmt
;
49 static unsigned test_image_size_mb
;
51 /*** Function Declarations ***/
52 static void ahci_test_port_spec(AHCIQState
*ahci
, uint8_t port
);
53 static void ahci_test_pci_spec(AHCIQState
*ahci
);
54 static void ahci_test_pci_caps(AHCIQState
*ahci
, uint16_t header
,
56 static void ahci_test_satacap(AHCIQState
*ahci
, uint8_t offset
);
57 static void ahci_test_msicap(AHCIQState
*ahci
, uint8_t offset
);
58 static void ahci_test_pmcap(AHCIQState
*ahci
, uint8_t offset
);
62 static uint64_t mb_to_sectors(uint64_t image_size_mb
)
64 return (image_size_mb
* 1024 * 1024) / AHCI_SECTOR_SIZE
;
67 static void string_bswap16(uint16_t *s
, size_t bytes
)
69 g_assert_cmphex((bytes
& 1), ==, 0);
79 * Verify that the transfer did not corrupt our state at all.
81 static void verify_state(AHCIQState
*ahci
)
84 uint32_t ahci_fingerprint
;
87 AHCICommandHeader cmd
;
89 ahci_fingerprint
= qpci_config_readl(ahci
->dev
, PCI_VENDOR_ID
);
90 g_assert_cmphex(ahci_fingerprint
, ==, ahci
->fingerprint
);
92 /* If we haven't initialized, this is as much as can be validated. */
93 if (!ahci
->hba_base
) {
97 hba_base
= (uint64_t)qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
98 hba_stored
= (uint64_t)(uintptr_t)ahci
->hba_base
;
99 g_assert_cmphex(hba_base
, ==, hba_stored
);
101 g_assert_cmphex(ahci_rreg(ahci
, AHCI_CAP
), ==, ahci
->cap
);
102 g_assert_cmphex(ahci_rreg(ahci
, AHCI_CAP2
), ==, ahci
->cap2
);
104 for (i
= 0; i
< 32; i
++) {
105 g_assert_cmphex(ahci_px_rreg(ahci
, i
, AHCI_PX_FB
), ==,
107 g_assert_cmphex(ahci_px_rreg(ahci
, i
, AHCI_PX_CLB
), ==,
109 for (j
= 0; j
< 32; j
++) {
110 ahci_get_command_header(ahci
, i
, j
, &cmd
);
111 g_assert_cmphex(cmd
.prdtl
, ==, ahci
->port
[i
].prdtl
[j
]);
112 g_assert_cmphex(cmd
.ctba
, ==, ahci
->port
[i
].ctba
[j
]);
117 static void ahci_migrate(AHCIQState
*from
, AHCIQState
*to
, const char *uri
)
119 QOSState
*tmp
= to
->parent
;
120 QPCIDevice
*dev
= to
->dev
;
121 char *uri_local
= NULL
;
124 uri_local
= g_strdup_printf("%s%s", "unix:", mig_socket
);
128 /* context will be 'to' after completion. */
129 migrate(from
->parent
, to
->parent
, uri
);
131 /* We'd like for the AHCIState objects to still point
132 * to information specific to its specific parent
133 * instance, but otherwise just inherit the new data. */
134 memcpy(to
, from
, sizeof(AHCIQState
));
140 memset(from
, 0x00, sizeof(AHCIQState
));
148 /*** Test Setup & Teardown ***/
151 * Start a Q35 machine and bookmark a handle to the AHCI device.
153 static AHCIQState
*ahci_vboot(const char *cli
, va_list ap
)
157 s
= g_malloc0(sizeof(AHCIQState
));
158 s
->parent
= qtest_pc_vboot(cli
, ap
);
159 alloc_set_flags(s
->parent
->alloc
, ALLOC_LEAK_ASSERT
);
161 /* Verify that we have an AHCI device present. */
162 s
->dev
= get_ahci_device(&s
->fingerprint
);
168 * Start a Q35 machine and bookmark a handle to the AHCI device.
170 static AHCIQState
*ahci_boot(const char *cli
, ...)
177 s
= ahci_vboot(cli
, ap
);
180 cli
= "-drive if=none,id=drive0,file=%s,cache=writeback,serial=%s"
183 "-device ide-hd,drive=drive0 "
184 "-global ide-hd.ver=%s";
185 s
= ahci_boot(cli
, tmp_path
, "testdisk", imgfmt
, "version");
192 * Clean up the PCI device, then terminate the QEMU instance.
194 static void ahci_shutdown(AHCIQState
*ahci
)
196 QOSState
*qs
= ahci
->parent
;
199 ahci_clean_mem(ahci
);
200 free_ahci_device(ahci
->dev
);
206 * Boot and fully enable the HBA device.
207 * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
209 static AHCIQState
*ahci_boot_and_enable(const char *cli
, ...)
219 ahci
= ahci_vboot(cli
, ap
);
222 ahci
= ahci_boot(NULL
);
225 ahci_pci_enable(ahci
);
226 ahci_hba_enable(ahci
);
227 /* Initialize test device */
228 port
= ahci_port_select(ahci
);
229 ahci_port_clear(ahci
, port
);
230 if (is_atapi(ahci
, port
)) {
231 hello
= CMD_PACKET_ID
;
233 hello
= CMD_IDENTIFY
;
235 ahci_io(ahci
, port
, hello
, &buff
, sizeof(buff
), 0);
240 /*** Specification Adherence Tests ***/
243 * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
245 static void ahci_test_pci_spec(AHCIQState
*ahci
)
251 /* Most of these bits should start cleared until we turn them on. */
252 data
= qpci_config_readw(ahci
->dev
, PCI_COMMAND
);
253 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_MEMORY
);
254 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_MASTER
);
255 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_SPECIAL
); /* Reserved */
256 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_VGA_PALETTE
); /* Reserved */
257 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_PARITY
);
258 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_WAIT
); /* Reserved */
259 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_SERR
);
260 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_FAST_BACK
);
261 ASSERT_BIT_CLEAR(data
, PCI_COMMAND_INTX_DISABLE
);
262 ASSERT_BIT_CLEAR(data
, 0xF800); /* Reserved */
264 data
= qpci_config_readw(ahci
->dev
, PCI_STATUS
);
265 ASSERT_BIT_CLEAR(data
, 0x01 | 0x02 | 0x04); /* Reserved */
266 ASSERT_BIT_CLEAR(data
, PCI_STATUS_INTERRUPT
);
267 ASSERT_BIT_SET(data
, PCI_STATUS_CAP_LIST
); /* must be set */
268 ASSERT_BIT_CLEAR(data
, PCI_STATUS_UDF
); /* Reserved */
269 ASSERT_BIT_CLEAR(data
, PCI_STATUS_PARITY
);
270 ASSERT_BIT_CLEAR(data
, PCI_STATUS_SIG_TARGET_ABORT
);
271 ASSERT_BIT_CLEAR(data
, PCI_STATUS_REC_TARGET_ABORT
);
272 ASSERT_BIT_CLEAR(data
, PCI_STATUS_REC_MASTER_ABORT
);
273 ASSERT_BIT_CLEAR(data
, PCI_STATUS_SIG_SYSTEM_ERROR
);
274 ASSERT_BIT_CLEAR(data
, PCI_STATUS_DETECTED_PARITY
);
276 /* RID occupies the low byte, CCs occupy the high three. */
277 datal
= qpci_config_readl(ahci
->dev
, PCI_CLASS_REVISION
);
279 /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
280 * Though in practice this is likely seldom true. */
281 ASSERT_BIT_CLEAR(datal
, 0xFF);
284 /* BCC *must* equal 0x01. */
285 g_assert_cmphex(PCI_BCC(datal
), ==, 0x01);
286 if (PCI_SCC(datal
) == 0x01) {
288 ASSERT_BIT_SET(0x80000000, datal
);
289 ASSERT_BIT_CLEAR(0x60000000, datal
);
290 } else if (PCI_SCC(datal
) == 0x04) {
292 g_assert_cmphex(PCI_PI(datal
), ==, 0);
293 } else if (PCI_SCC(datal
) == 0x06) {
295 g_assert_cmphex(PCI_PI(datal
), ==, 0x01);
297 g_assert_not_reached();
300 datab
= qpci_config_readb(ahci
->dev
, PCI_CACHE_LINE_SIZE
);
301 g_assert_cmphex(datab
, ==, 0);
303 datab
= qpci_config_readb(ahci
->dev
, PCI_LATENCY_TIMER
);
304 g_assert_cmphex(datab
, ==, 0);
306 /* Only the bottom 7 bits must be off. */
307 datab
= qpci_config_readb(ahci
->dev
, PCI_HEADER_TYPE
);
308 ASSERT_BIT_CLEAR(datab
, 0x7F);
310 /* BIST is optional, but the low 7 bits must always start off regardless. */
311 datab
= qpci_config_readb(ahci
->dev
, PCI_BIST
);
312 ASSERT_BIT_CLEAR(datab
, 0x7F);
314 /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
315 datal
= qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
316 g_assert_cmphex(datal
, ==, 0);
318 qpci_config_writel(ahci
->dev
, PCI_BASE_ADDRESS_5
, 0xFFFFFFFF);
319 datal
= qpci_config_readl(ahci
->dev
, PCI_BASE_ADDRESS_5
);
320 /* ABAR must be 32-bit, memory mapped, non-prefetchable and
321 * must be >= 512 bytes. To that end, bits 0-8 must be off. */
322 ASSERT_BIT_CLEAR(datal
, 0xFF);
324 /* Capability list MUST be present, */
325 datal
= qpci_config_readl(ahci
->dev
, PCI_CAPABILITY_LIST
);
326 /* But these bits are reserved. */
327 ASSERT_BIT_CLEAR(datal
, ~0xFF);
328 g_assert_cmphex(datal
, !=, 0);
330 /* Check specification adherence for capability extenstions. */
331 data
= qpci_config_readw(ahci
->dev
, datal
);
333 switch (ahci
->fingerprint
) {
334 case AHCI_INTEL_ICH9
:
335 /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
336 g_assert_cmphex((data
& 0xFF), ==, PCI_CAP_ID_MSI
);
339 /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
340 g_assert_cmphex((data
& 0xFF), ==, PCI_CAP_ID_PM
);
343 ahci_test_pci_caps(ahci
, data
, (uint8_t)datal
);
346 datal
= qpci_config_readl(ahci
->dev
, PCI_CAPABILITY_LIST
+ 4);
347 g_assert_cmphex(datal
, ==, 0);
349 /* IPIN might vary, but ILINE must be off. */
350 datab
= qpci_config_readb(ahci
->dev
, PCI_INTERRUPT_LINE
);
351 g_assert_cmphex(datab
, ==, 0);
355 * Test PCI capabilities for AHCI specification adherence.
357 static void ahci_test_pci_caps(AHCIQState
*ahci
, uint16_t header
,
360 uint8_t cid
= header
& 0xFF;
361 uint8_t next
= header
>> 8;
363 g_test_message("CID: %02x; next: %02x", cid
, next
);
367 ahci_test_pmcap(ahci
, offset
);
370 ahci_test_msicap(ahci
, offset
);
372 case PCI_CAP_ID_SATA
:
373 ahci_test_satacap(ahci
, offset
);
377 g_test_message("Unknown CAP 0x%02x", cid
);
381 ahci_test_pci_caps(ahci
, qpci_config_readw(ahci
->dev
, next
), next
);
386 * Test SATA PCI capabilitity for AHCI specification adherence.
388 static void ahci_test_satacap(AHCIQState
*ahci
, uint8_t offset
)
393 g_test_message("Verifying SATACAP");
395 /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
396 dataw
= qpci_config_readw(ahci
->dev
, offset
+ 2);
397 g_assert_cmphex(dataw
, ==, 0x10);
399 /* Grab the SATACR1 register. */
400 datal
= qpci_config_readw(ahci
->dev
, offset
+ 4);
402 switch (datal
& 0x0F) {
403 case 0x04: /* BAR0 */
404 case 0x05: /* BAR1 */
408 case 0x09: /* BAR5 */
409 case 0x0F: /* Immediately following SATACR1 in PCI config space. */
412 /* Invalid BARLOC for the Index Data Pair. */
413 g_assert_not_reached();
417 g_assert_cmphex((datal
>> 24), ==, 0x00);
421 * Test MSI PCI capability for AHCI specification adherence.
423 static void ahci_test_msicap(AHCIQState
*ahci
, uint8_t offset
)
428 g_test_message("Verifying MSICAP");
430 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_FLAGS
);
431 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_ENABLE
);
432 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_QSIZE
);
433 ASSERT_BIT_CLEAR(dataw
, PCI_MSI_FLAGS_RESERVED
);
435 datal
= qpci_config_readl(ahci
->dev
, offset
+ PCI_MSI_ADDRESS_LO
);
436 g_assert_cmphex(datal
, ==, 0);
438 if (dataw
& PCI_MSI_FLAGS_64BIT
) {
439 g_test_message("MSICAP is 64bit");
440 datal
= qpci_config_readl(ahci
->dev
, offset
+ PCI_MSI_ADDRESS_HI
);
441 g_assert_cmphex(datal
, ==, 0);
442 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_DATA_64
);
443 g_assert_cmphex(dataw
, ==, 0);
445 g_test_message("MSICAP is 32bit");
446 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_MSI_DATA_32
);
447 g_assert_cmphex(dataw
, ==, 0);
452 * Test Power Management PCI capability for AHCI specification adherence.
454 static void ahci_test_pmcap(AHCIQState
*ahci
, uint8_t offset
)
458 g_test_message("Verifying PMCAP");
460 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_PM_PMC
);
461 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_PME_CLOCK
);
462 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_RESERVED
);
463 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_D1
);
464 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CAP_D2
);
466 dataw
= qpci_config_readw(ahci
->dev
, offset
+ PCI_PM_CTRL
);
467 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_STATE_MASK
);
468 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_RESERVED
);
469 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_DATA_SEL_MASK
);
470 ASSERT_BIT_CLEAR(dataw
, PCI_PM_CTRL_DATA_SCALE_MASK
);
473 static void ahci_test_hba_spec(AHCIQState
*ahci
)
481 g_assert(ahci
!= NULL
);
484 * Note that the AHCI spec does expect the BIOS to set up a few things:
485 * CAP.SSS - Support for staggered spin-up (t/f)
486 * CAP.SMPS - Support for mechanical presence switches (t/f)
487 * PI - Ports Implemented (1-32)
488 * PxCMD.HPCP - Hot Plug Capable Port
489 * PxCMD.MPSP - Mechanical Presence Switch Present
490 * PxCMD.CPD - Cold Presence Detection support
492 * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
493 * Foreach Port Implemented:
494 * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
495 * -PxCLB/U and PxFB/U are set to valid regions in memory
496 * -PxSUD is set to 1.
497 * -PxSSTS.DET is polled for presence; if detected, we continue:
498 * -PxSERR is cleared with 1's.
499 * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
500 * the device is ready.
503 /* 1 CAP - Capabilities Register */
504 ahci
->cap
= ahci_rreg(ahci
, AHCI_CAP
);
505 ASSERT_BIT_CLEAR(ahci
->cap
, AHCI_CAP_RESERVED
);
507 /* 2 GHC - Global Host Control */
508 reg
= ahci_rreg(ahci
, AHCI_GHC
);
509 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_HR
);
510 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_IE
);
511 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_MRSM
);
512 if (BITSET(ahci
->cap
, AHCI_CAP_SAM
)) {
513 g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
514 ASSERT_BIT_SET(reg
, AHCI_GHC_AE
);
516 g_test_message("Supports AHCI/Legacy mix.");
517 ASSERT_BIT_CLEAR(reg
, AHCI_GHC_AE
);
520 /* 3 IS - Interrupt Status */
521 reg
= ahci_rreg(ahci
, AHCI_IS
);
522 g_assert_cmphex(reg
, ==, 0);
524 /* 4 PI - Ports Implemented */
525 ports
= ahci_rreg(ahci
, AHCI_PI
);
526 /* Ports Implemented must be non-zero. */
527 g_assert_cmphex(ports
, !=, 0);
528 /* Ports Implemented must be <= Number of Ports. */
529 nports_impl
= ctpopl(ports
);
530 g_assert_cmpuint(((AHCI_CAP_NP
& ahci
->cap
) + 1), >=, nports_impl
);
532 /* Ports must be within the proper range. Given a mapping of SIZE,
533 * 256 bytes are used for global HBA control, and the rest is used
534 * for ports data, at 0x80 bytes each. */
535 g_assert_cmphex(ahci
->barsize
, >, 0);
536 maxports
= (ahci
->barsize
- HBA_DATA_REGION_SIZE
) / HBA_PORT_DATA_SIZE
;
537 /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
538 g_assert_cmphex((reg
>> maxports
), ==, 0);
541 reg
= ahci_rreg(ahci
, AHCI_VS
);
543 case AHCI_VERSION_0_95
:
544 case AHCI_VERSION_1_0
:
545 case AHCI_VERSION_1_1
:
546 case AHCI_VERSION_1_2
:
547 case AHCI_VERSION_1_3
:
550 g_assert_not_reached();
553 /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
554 reg
= ahci_rreg(ahci
, AHCI_CCCCTL
);
555 if (BITSET(ahci
->cap
, AHCI_CAP_CCCS
)) {
556 ASSERT_BIT_CLEAR(reg
, AHCI_CCCCTL_EN
);
557 ASSERT_BIT_CLEAR(reg
, AHCI_CCCCTL_RESERVED
);
558 ASSERT_BIT_SET(reg
, AHCI_CCCCTL_CC
);
559 ASSERT_BIT_SET(reg
, AHCI_CCCCTL_TV
);
561 g_assert_cmphex(reg
, ==, 0);
565 reg
= ahci_rreg(ahci
, AHCI_CCCPORTS
);
566 /* Must be zeroes initially regardless of CAP.CCCS */
567 g_assert_cmphex(reg
, ==, 0);
570 reg
= ahci_rreg(ahci
, AHCI_EMLOC
);
571 if (BITCLR(ahci
->cap
, AHCI_CAP_EMS
)) {
572 g_assert_cmphex(reg
, ==, 0);
576 reg
= ahci_rreg(ahci
, AHCI_EMCTL
);
577 if (BITSET(ahci
->cap
, AHCI_CAP_EMS
)) {
578 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_STSMR
);
579 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_CTLTM
);
580 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_CTLRST
);
581 ASSERT_BIT_CLEAR(reg
, AHCI_EMCTL_RESERVED
);
583 g_assert_cmphex(reg
, ==, 0);
586 /* 10 CAP2 -- Capabilities Extended */
587 ahci
->cap2
= ahci_rreg(ahci
, AHCI_CAP2
);
588 ASSERT_BIT_CLEAR(ahci
->cap2
, AHCI_CAP2_RESERVED
);
590 /* 11 BOHC -- Bios/OS Handoff Control */
591 reg
= ahci_rreg(ahci
, AHCI_BOHC
);
592 g_assert_cmphex(reg
, ==, 0);
594 /* 12 -- 23: Reserved */
595 g_test_message("Verifying HBA reserved area is empty.");
596 for (i
= AHCI_RESERVED
; i
< AHCI_NVMHCI
; ++i
) {
597 reg
= ahci_rreg(ahci
, i
);
598 g_assert_cmphex(reg
, ==, 0);
601 /* 24 -- 39: NVMHCI */
602 if (BITCLR(ahci
->cap2
, AHCI_CAP2_NVMP
)) {
603 g_test_message("Verifying HBA/NVMHCI area is empty.");
604 for (i
= AHCI_NVMHCI
; i
< AHCI_VENDOR
; ++i
) {
605 reg
= ahci_rreg(ahci
, i
);
606 g_assert_cmphex(reg
, ==, 0);
610 /* 40 -- 63: Vendor */
611 g_test_message("Verifying HBA/Vendor area is empty.");
612 for (i
= AHCI_VENDOR
; i
< AHCI_PORTS
; ++i
) {
613 reg
= ahci_rreg(ahci
, i
);
614 g_assert_cmphex(reg
, ==, 0);
617 /* 64 -- XX: Port Space */
618 for (i
= 0; ports
|| (i
< maxports
); ports
>>= 1, ++i
) {
619 if (BITSET(ports
, 0x1)) {
620 g_test_message("Testing port %u for spec", i
);
621 ahci_test_port_spec(ahci
, i
);
624 uint16_t low
= AHCI_PORTS
+ (32 * i
);
625 uint16_t high
= AHCI_PORTS
+ (32 * (i
+ 1));
626 g_test_message("Asserting unimplemented port %u "
627 "(reg [%u-%u]) is empty.",
629 for (j
= low
; j
< high
; ++j
) {
630 reg
= ahci_rreg(ahci
, j
);
631 g_assert_cmphex(reg
, ==, 0);
638 * Test the memory space for one port for specification adherence.
640 static void ahci_test_port_spec(AHCIQState
*ahci
, uint8_t port
)
646 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CLB
);
647 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CLB_RESERVED
);
650 if (BITCLR(ahci
->cap
, AHCI_CAP_S64A
)) {
651 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CLBU
);
652 g_assert_cmphex(reg
, ==, 0);
656 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FB
);
657 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FB_RESERVED
);
660 if (BITCLR(ahci
->cap
, AHCI_CAP_S64A
)) {
661 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FBU
);
662 g_assert_cmphex(reg
, ==, 0);
666 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_IS
);
667 g_assert_cmphex(reg
, ==, 0);
670 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_IE
);
671 g_assert_cmphex(reg
, ==, 0);
674 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CMD
);
675 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FRE
);
676 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_RESERVED
);
677 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CCS
);
678 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FR
);
679 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CR
);
680 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_PMA
); /* And RW only if CAP.SPM */
681 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_APSTE
); /* RW only if CAP2.APST */
682 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ATAPI
);
683 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_DLAE
);
684 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ALPE
); /* RW only if CAP.SALP */
685 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ASP
); /* RW only if CAP.SALP */
686 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_ICC
);
687 /* If CPDetect support does not exist, CPState must be off. */
688 if (BITCLR(reg
, AHCI_PX_CMD_CPD
)) {
689 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_CPS
);
691 /* If MPSPresence is not set, MPSState must be off. */
692 if (BITCLR(reg
, AHCI_PX_CMD_MPSP
)) {
693 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSS
);
695 /* If we do not support MPS, MPSS and MPSP must be off. */
696 if (BITCLR(ahci
->cap
, AHCI_CAP_SMPS
)) {
697 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSS
);
698 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_MPSP
);
700 /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
701 if (BITANY(reg
, AHCI_PX_CMD_CPD
| AHCI_PX_CMD_MPSP
)) {
702 ASSERT_BIT_SET(reg
, AHCI_PX_CMD_HPCP
);
704 /* HPCP and ESP cannot both be active. */
705 g_assert(!BITSET(reg
, AHCI_PX_CMD_HPCP
| AHCI_PX_CMD_ESP
));
706 /* If CAP.FBSS is not set, FBSCP must not be set. */
707 if (BITCLR(ahci
->cap
, AHCI_CAP_FBSS
)) {
708 ASSERT_BIT_CLEAR(reg
, AHCI_PX_CMD_FBSCP
);
712 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_RES1
);
713 g_assert_cmphex(reg
, ==, 0);
716 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_TFD
);
717 /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
718 * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
719 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_ERR
);
720 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_CS1
);
721 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_DRQ
);
722 ASSERT_BIT_SET(reg
, AHCI_PX_TFD_STS_CS2
);
723 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_STS_BSY
);
724 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_ERR
);
725 ASSERT_BIT_CLEAR(reg
, AHCI_PX_TFD_RESERVED
);
728 /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
729 * Even when GHC.ST is zero, the AHCI HBA may receive the initial
730 * D2H register FIS and update the signature asynchronously,
731 * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
733 /* (10) SSTS / SCR0: SStatus */
734 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SSTS
);
735 ASSERT_BIT_CLEAR(reg
, AHCI_PX_SSTS_RESERVED
);
736 /* Even though the register should be 0 at boot, it is asynchronous and
737 * prone to change, so we cannot test any well known value. */
739 /* (11) SCTL / SCR2: SControl */
740 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SCTL
);
741 g_assert_cmphex(reg
, ==, 0);
743 /* (12) SERR / SCR1: SError */
744 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SERR
);
745 g_assert_cmphex(reg
, ==, 0);
747 /* (13) SACT / SCR3: SActive */
748 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SACT
);
749 g_assert_cmphex(reg
, ==, 0);
752 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_CI
);
753 g_assert_cmphex(reg
, ==, 0);
756 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_SNTF
);
757 g_assert_cmphex(reg
, ==, 0);
760 reg
= ahci_px_rreg(ahci
, port
, AHCI_PX_FBS
);
761 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_EN
);
762 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DEC
);
763 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_SDE
);
764 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DEV
);
765 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_DWE
);
766 ASSERT_BIT_CLEAR(reg
, AHCI_PX_FBS_RESERVED
);
767 if (BITSET(ahci
->cap
, AHCI_CAP_FBSS
)) {
768 /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
769 g_assert((reg
& AHCI_PX_FBS_ADO
) >> ctzl(AHCI_PX_FBS_ADO
) >= 2);
772 /* [17 -- 27] RESERVED */
773 for (i
= AHCI_PX_RES2
; i
< AHCI_PX_VS
; ++i
) {
774 reg
= ahci_px_rreg(ahci
, port
, i
);
775 g_assert_cmphex(reg
, ==, 0);
778 /* [28 -- 31] Vendor-Specific */
779 for (i
= AHCI_PX_VS
; i
< 32; ++i
) {
780 reg
= ahci_px_rreg(ahci
, port
, i
);
782 g_test_message("INFO: Vendor register %u non-empty", i
);
788 * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
789 * device we see, then read and check the response.
791 static void ahci_test_identify(AHCIQState
*ahci
)
797 const size_t buffsize
= 512;
799 g_assert(ahci
!= NULL
);
802 * This serves as a bit of a tutorial on AHCI device programming:
804 * (1) Create a data buffer for the IDENTIFY response to be sent to
805 * (2) Create a Command Table buffer, where we will store the
806 * command and PRDT (Physical Region Descriptor Table)
807 * (3) Construct an FIS host-to-device command structure, and write it to
808 * the top of the Command Table buffer.
809 * (4) Create one or more Physical Region Descriptors (PRDs) that describe
810 * a location in memory where data may be stored/retrieved.
811 * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
812 * (6) Each AHCI port has up to 32 command slots. Each slot contains a
813 * header that points to a Command Table buffer. Pick an unused slot
814 * and update it to point to the Command Table we have built.
815 * (7) Now: Command #n points to our Command Table, and our Command Table
816 * contains the FIS (that describes our command) and the PRDTL, which
817 * describes our buffer.
818 * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
819 * #n is ready for processing.
822 /* Pick the first implemented and running port */
823 px
= ahci_port_select(ahci
);
824 g_test_message("Selected port %u for test", px
);
826 /* Clear out the FIS Receive area and any pending interrupts. */
827 ahci_port_clear(ahci
, px
);
829 /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
830 ahci_io(ahci
, px
, CMD_IDENTIFY
, &buff
, buffsize
, 0);
832 /* Check serial number/version in the buffer */
833 /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
834 * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
835 * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
836 * as a consequence, only needs to unchunk the data on LE machines. */
837 string_bswap16(&buff
[10], 20);
838 rc
= memcmp(&buff
[10], "testdisk ", 20);
839 g_assert_cmphex(rc
, ==, 0);
841 string_bswap16(&buff
[23], 8);
842 rc
= memcmp(&buff
[23], "version ", 8);
843 g_assert_cmphex(rc
, ==, 0);
845 sect_size
= le16_to_cpu(*((uint16_t *)(&buff
[5])));
846 g_assert_cmphex(sect_size
, ==, AHCI_SECTOR_SIZE
);
849 static void ahci_test_io_rw_simple(AHCIQState
*ahci
, unsigned bufsize
,
850 uint64_t sector
, uint8_t read_cmd
,
855 unsigned char *tx
= g_malloc(bufsize
);
856 unsigned char *rx
= g_malloc0(bufsize
);
858 g_assert(ahci
!= NULL
);
860 /* Pick the first running port and clear it. */
861 port
= ahci_port_select(ahci
);
862 ahci_port_clear(ahci
, port
);
864 /*** Create pattern and transfer to guest ***/
865 /* Data buffer in the guest */
866 ptr
= ahci_alloc(ahci
, bufsize
);
869 /* Write some indicative pattern to our buffer. */
870 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
871 bufwrite(ptr
, tx
, bufsize
);
873 /* Write this buffer to disk, then read it back to the DMA buffer. */
874 ahci_guest_io(ahci
, port
, write_cmd
, ptr
, bufsize
, sector
);
875 qmemset(ptr
, 0x00, bufsize
);
876 ahci_guest_io(ahci
, port
, read_cmd
, ptr
, bufsize
, sector
);
878 /*** Read back the Data ***/
879 bufread(ptr
, rx
, bufsize
);
880 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
882 ahci_free(ahci
, ptr
);
887 static uint8_t ahci_test_nondata(AHCIQState
*ahci
, uint8_t ide_cmd
)
892 port
= ahci_port_select(ahci
);
893 ahci_port_clear(ahci
, port
);
895 ahci_io(ahci
, port
, ide_cmd
, NULL
, 0, 0);
900 static void ahci_test_flush(AHCIQState
*ahci
)
902 ahci_test_nondata(ahci
, CMD_FLUSH_CACHE
);
905 static void ahci_test_max(AHCIQState
*ahci
)
907 RegD2HFIS
*d2h
= g_malloc0(0x20);
911 uint64_t config_sect
= mb_to_sectors(test_image_size_mb
) - 1;
913 if (config_sect
> 0xFFFFFF) {
914 cmd
= CMD_READ_MAX_EXT
;
919 port
= ahci_test_nondata(ahci
, cmd
);
920 memread(ahci
->port
[port
].fb
+ 0x40, d2h
, 0x20);
921 nsect
= (uint64_t)d2h
->lba_hi
[2] << 40 |
922 (uint64_t)d2h
->lba_hi
[1] << 32 |
923 (uint64_t)d2h
->lba_hi
[0] << 24 |
924 (uint64_t)d2h
->lba_lo
[2] << 16 |
925 (uint64_t)d2h
->lba_lo
[1] << 8 |
926 (uint64_t)d2h
->lba_lo
[0];
928 g_assert_cmphex(nsect
, ==, config_sect
);
933 /******************************************************************************/
934 /* Test Interfaces */
935 /******************************************************************************/
938 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
940 static void test_sanity(void)
943 ahci
= ahci_boot(NULL
);
948 * Ensure that the PCI configuration space for the AHCI device is in-line with
949 * the AHCI 1.3 specification for initial values.
951 static void test_pci_spec(void)
954 ahci
= ahci_boot(NULL
);
955 ahci_test_pci_spec(ahci
);
960 * Engage the PCI AHCI device and sanity check the response.
961 * Perform additional PCI config space bringup for the HBA.
963 static void test_pci_enable(void)
966 ahci
= ahci_boot(NULL
);
967 ahci_pci_enable(ahci
);
972 * Investigate the memory mapped regions of the HBA,
973 * and test them for AHCI specification adherence.
975 static void test_hba_spec(void)
979 ahci
= ahci_boot(NULL
);
980 ahci_pci_enable(ahci
);
981 ahci_test_hba_spec(ahci
);
986 * Engage the HBA functionality of the AHCI PCI device,
987 * and bring it into a functional idle state.
989 static void test_hba_enable(void)
993 ahci
= ahci_boot(NULL
);
994 ahci_pci_enable(ahci
);
995 ahci_hba_enable(ahci
);
1000 * Bring up the device and issue an IDENTIFY command.
1001 * Inspect the state of the HBA device and the data returned.
1003 static void test_identify(void)
1007 ahci
= ahci_boot_and_enable(NULL
);
1008 ahci_test_identify(ahci
);
1009 ahci_shutdown(ahci
);
1013 * Fragmented DMA test: Perform a standard 4K DMA read/write
1014 * test, but make sure the physical regions are fragmented to
1015 * be very small, each just 32 bytes, to see how AHCI performs
1016 * with chunks defined to be much less than a sector.
1018 static void test_dma_fragmented(void)
1023 size_t bufsize
= 4096;
1024 unsigned char *tx
= g_malloc(bufsize
);
1025 unsigned char *rx
= g_malloc0(bufsize
);
1028 ahci
= ahci_boot_and_enable(NULL
);
1029 px
= ahci_port_select(ahci
);
1030 ahci_port_clear(ahci
, px
);
1032 /* create pattern */
1033 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
1035 /* Create a DMA buffer in guest memory, and write our pattern to it. */
1036 ptr
= guest_alloc(ahci
->parent
->alloc
, bufsize
);
1038 bufwrite(ptr
, tx
, bufsize
);
1040 cmd
= ahci_command_create(CMD_WRITE_DMA
);
1041 ahci_command_adjust(cmd
, 0, ptr
, bufsize
, 32);
1042 ahci_command_commit(ahci
, cmd
, px
);
1043 ahci_command_issue(ahci
, cmd
);
1044 ahci_command_verify(ahci
, cmd
);
1045 ahci_command_free(cmd
);
1047 cmd
= ahci_command_create(CMD_READ_DMA
);
1048 ahci_command_adjust(cmd
, 0, ptr
, bufsize
, 32);
1049 ahci_command_commit(ahci
, cmd
, px
);
1050 ahci_command_issue(ahci
, cmd
);
1051 ahci_command_verify(ahci
, cmd
);
1052 ahci_command_free(cmd
);
1054 /* Read back the guest's receive buffer into local memory */
1055 bufread(ptr
, rx
, bufsize
);
1056 guest_free(ahci
->parent
->alloc
, ptr
);
1058 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1060 ahci_shutdown(ahci
);
1067 * Write sector 1 with random data to make AHCI storage dirty
1068 * Needed for flush tests so that flushes actually go though the block layer
1070 static void make_dirty(AHCIQState
* ahci
, uint8_t port
)
1073 unsigned bufsize
= 512;
1075 ptr
= ahci_alloc(ahci
, bufsize
);
1078 ahci_guest_io(ahci
, port
, CMD_WRITE_DMA
, ptr
, bufsize
, 1);
1079 ahci_free(ahci
, ptr
);
1082 static void test_flush(void)
1087 ahci
= ahci_boot_and_enable(NULL
);
1089 port
= ahci_port_select(ahci
);
1090 ahci_port_clear(ahci
, port
);
1092 make_dirty(ahci
, port
);
1094 ahci_test_flush(ahci
);
1095 ahci_shutdown(ahci
);
1098 static void test_flush_retry(void)
1104 prepare_blkdebug_script(debug_path
, "flush_to_disk");
1105 ahci
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1106 "format=%s,cache=writeback,"
1107 "rerror=stop,werror=stop "
1109 "-device ide-hd,drive=drive0 ",
1113 port
= ahci_port_select(ahci
);
1114 ahci_port_clear(ahci
, port
);
1116 /* Issue write so that flush actually goes to disk */
1117 make_dirty(ahci
, port
);
1119 /* Issue Flush Command and wait for error */
1120 cmd
= ahci_guest_io_halt(ahci
, port
, CMD_FLUSH_CACHE
, 0, 0, 0);
1121 ahci_guest_io_resume(ahci
, cmd
);
1123 ahci_shutdown(ahci
);
1127 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
1129 static void test_migrate_sanity(void)
1131 AHCIQState
*src
, *dst
;
1132 char *uri
= g_strdup_printf("unix:%s", mig_socket
);
1134 src
= ahci_boot("-m 1024 -M q35 "
1135 "-drive if=ide,file=%s,format=%s ", tmp_path
, imgfmt
);
1136 dst
= ahci_boot("-m 1024 -M q35 "
1137 "-drive if=ide,file=%s,format=%s "
1138 "-incoming %s", tmp_path
, imgfmt
, uri
);
1140 ahci_migrate(src
, dst
, uri
);
1148 * Simple migration test: Write a pattern, migrate, then read.
1150 static void ahci_migrate_simple(uint8_t cmd_read
, uint8_t cmd_write
)
1152 AHCIQState
*src
, *dst
;
1154 size_t bufsize
= 4096;
1155 unsigned char *tx
= g_malloc(bufsize
);
1156 unsigned char *rx
= g_malloc0(bufsize
);
1157 char *uri
= g_strdup_printf("unix:%s", mig_socket
);
1159 src
= ahci_boot_and_enable("-m 1024 -M q35 "
1160 "-drive if=ide,format=%s,file=%s ",
1162 dst
= ahci_boot("-m 1024 -M q35 "
1163 "-drive if=ide,format=%s,file=%s "
1164 "-incoming %s", imgfmt
, tmp_path
, uri
);
1166 set_context(src
->parent
);
1169 px
= ahci_port_select(src
);
1170 ahci_port_clear(src
, px
);
1172 /* create pattern */
1173 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
1175 /* Write, migrate, then read. */
1176 ahci_io(src
, px
, cmd_write
, tx
, bufsize
, 0);
1177 ahci_migrate(src
, dst
, uri
);
1178 ahci_io(dst
, px
, cmd_read
, rx
, bufsize
, 0);
1180 /* Verify pattern */
1181 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1190 static void test_migrate_dma(void)
1192 ahci_migrate_simple(CMD_READ_DMA
, CMD_WRITE_DMA
);
1195 static void test_migrate_ncq(void)
1197 ahci_migrate_simple(READ_FPDMA_QUEUED
, WRITE_FPDMA_QUEUED
);
1201 * Halted IO Error Test
1203 * Simulate an error on first write, Try to write a pattern,
1204 * Confirm the VM has stopped, resume the VM, verify command
1205 * has completed, then read back the data and verify.
1207 static void ahci_halted_io_test(uint8_t cmd_read
, uint8_t cmd_write
)
1211 size_t bufsize
= 4096;
1212 unsigned char *tx
= g_malloc(bufsize
);
1213 unsigned char *rx
= g_malloc0(bufsize
);
1217 prepare_blkdebug_script(debug_path
, "write_aio");
1219 ahci
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1220 "format=%s,cache=writeback,"
1221 "rerror=stop,werror=stop "
1223 "-device ide-hd,drive=drive0 ",
1227 /* Initialize and prepare */
1228 port
= ahci_port_select(ahci
);
1229 ahci_port_clear(ahci
, port
);
1231 /* create DMA source buffer and write pattern */
1232 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
1233 ptr
= ahci_alloc(ahci
, bufsize
);
1235 memwrite(ptr
, tx
, bufsize
);
1237 /* Attempt to write (and fail) */
1238 cmd
= ahci_guest_io_halt(ahci
, port
, cmd_write
,
1241 /* Attempt to resume the command */
1242 ahci_guest_io_resume(ahci
, cmd
);
1243 ahci_free(ahci
, ptr
);
1245 /* Read back and verify */
1246 ahci_io(ahci
, port
, cmd_read
, rx
, bufsize
, 0);
1247 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1249 /* Cleanup and go home */
1250 ahci_shutdown(ahci
);
1255 static void test_halted_dma(void)
1257 ahci_halted_io_test(CMD_READ_DMA
, CMD_WRITE_DMA
);
1260 static void test_halted_ncq(void)
1262 ahci_halted_io_test(READ_FPDMA_QUEUED
, WRITE_FPDMA_QUEUED
);
1266 * IO Error Migration Test
1268 * Simulate an error on first write, Try to write a pattern,
1269 * Confirm the VM has stopped, migrate, resume the VM,
1270 * verify command has completed, then read back the data and verify.
1272 static void ahci_migrate_halted_io(uint8_t cmd_read
, uint8_t cmd_write
)
1274 AHCIQState
*src
, *dst
;
1276 size_t bufsize
= 4096;
1277 unsigned char *tx
= g_malloc(bufsize
);
1278 unsigned char *rx
= g_malloc0(bufsize
);
1281 char *uri
= g_strdup_printf("unix:%s", mig_socket
);
1283 prepare_blkdebug_script(debug_path
, "write_aio");
1285 src
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1286 "format=%s,cache=writeback,"
1287 "rerror=stop,werror=stop "
1289 "-device ide-hd,drive=drive0 ",
1293 dst
= ahci_boot("-drive file=%s,if=none,id=drive0,"
1294 "format=%s,cache=writeback,"
1295 "rerror=stop,werror=stop "
1297 "-device ide-hd,drive=drive0 "
1299 tmp_path
, imgfmt
, uri
);
1301 set_context(src
->parent
);
1303 /* Initialize and prepare */
1304 port
= ahci_port_select(src
);
1305 ahci_port_clear(src
, port
);
1306 generate_pattern(tx
, bufsize
, AHCI_SECTOR_SIZE
);
1308 /* create DMA source buffer and write pattern */
1309 ptr
= ahci_alloc(src
, bufsize
);
1311 memwrite(ptr
, tx
, bufsize
);
1313 /* Write, trigger the VM to stop, migrate, then resume. */
1314 cmd
= ahci_guest_io_halt(src
, port
, cmd_write
,
1316 ahci_migrate(src
, dst
, uri
);
1317 ahci_guest_io_resume(dst
, cmd
);
1318 ahci_free(dst
, ptr
);
1321 ahci_io(dst
, port
, cmd_read
, rx
, bufsize
, 0);
1323 /* Verify TX and RX are identical */
1324 g_assert_cmphex(memcmp(tx
, rx
, bufsize
), ==, 0);
1326 /* Cleanup and go home. */
1334 static void test_migrate_halted_dma(void)
1336 ahci_migrate_halted_io(CMD_READ_DMA
, CMD_WRITE_DMA
);
1339 static void test_migrate_halted_ncq(void)
1341 ahci_migrate_halted_io(READ_FPDMA_QUEUED
, WRITE_FPDMA_QUEUED
);
1345 * Migration test: Try to flush, migrate, then resume.
1347 static void test_flush_migrate(void)
1349 AHCIQState
*src
, *dst
;
1353 char *uri
= g_strdup_printf("unix:%s", mig_socket
);
1355 prepare_blkdebug_script(debug_path
, "flush_to_disk");
1357 src
= ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1358 "cache=writeback,rerror=stop,werror=stop,"
1361 "-device ide-hd,drive=drive0 ",
1362 debug_path
, tmp_path
, imgfmt
);
1363 dst
= ahci_boot("-drive file=%s,if=none,id=drive0,"
1364 "cache=writeback,rerror=stop,werror=stop,"
1367 "-device ide-hd,drive=drive0 "
1368 "-incoming %s", tmp_path
, imgfmt
, uri
);
1370 set_context(src
->parent
);
1372 px
= ahci_port_select(src
);
1373 ahci_port_clear(src
, px
);
1375 /* Dirty device so that flush reaches disk */
1376 make_dirty(src
, px
);
1378 /* Issue Flush Command */
1379 cmd
= ahci_command_create(CMD_FLUSH_CACHE
);
1380 ahci_command_commit(src
, cmd
, px
);
1381 ahci_command_issue_async(src
, cmd
);
1382 qmp_eventwait("STOP");
1385 ahci_migrate(src
, dst
, uri
);
1387 /* Complete the command */
1388 s
= "{'execute':'cont' }";
1390 qmp_eventwait("RESUME");
1391 ahci_command_wait(dst
, cmd
);
1392 ahci_command_verify(dst
, cmd
);
1394 ahci_command_free(cmd
);
1400 static void test_max(void)
1404 ahci
= ahci_boot_and_enable(NULL
);
1405 ahci_test_max(ahci
);
1406 ahci_shutdown(ahci
);
1409 static void test_reset(void)
1414 ahci
= ahci_boot(NULL
);
1415 ahci_test_pci_spec(ahci
);
1416 ahci_pci_enable(ahci
);
1418 for (i
= 0; i
< 2; i
++) {
1419 ahci_test_hba_spec(ahci
);
1420 ahci_hba_enable(ahci
);
1421 ahci_test_identify(ahci
);
1422 ahci_test_io_rw_simple(ahci
, 4096, 0,
1425 ahci_set(ahci
, AHCI_GHC
, AHCI_GHC_HR
);
1426 ahci_clean_mem(ahci
);
1429 ahci_shutdown(ahci
);
1432 static void test_ncq_simple(void)
1436 ahci
= ahci_boot_and_enable(NULL
);
1437 ahci_test_io_rw_simple(ahci
, 4096, 0,
1439 WRITE_FPDMA_QUEUED
);
1440 ahci_shutdown(ahci
);
1443 static int prepare_iso(size_t size
, unsigned char **buf
, char **name
)
1445 char cdrom_path
[] = "/tmp/qtest.iso.XXXXXX";
1446 unsigned char *patt
;
1448 int fd
= mkstemp(cdrom_path
);
1452 patt
= g_malloc(size
);
1454 /* Generate a pattern and build a CDROM image to read from */
1455 generate_pattern(patt
, size
, ATAPI_SECTOR_SIZE
);
1456 ret
= write(fd
, patt
, size
);
1457 g_assert(ret
== size
);
1459 *name
= g_strdup(cdrom_path
);
1464 static void remove_iso(int fd
, char *name
)
1471 static int ahci_cb_cmp_buff(AHCIQState
*ahci
, AHCICommand
*cmd
,
1472 const AHCIOpts
*opts
)
1474 unsigned char *tx
= opts
->opaque
;
1475 unsigned char *rx
= g_malloc0(opts
->size
);
1477 bufread(opts
->buffer
, rx
, opts
->size
);
1478 g_assert_cmphex(memcmp(tx
, rx
, opts
->size
), ==, 0);
1484 static void ahci_test_cdrom(int nsectors
, bool dma
)
1491 .size
= (ATAPI_SECTOR_SIZE
* nsectors
),
1494 .post_cb
= ahci_cb_cmp_buff
,
1497 /* Prepare ISO and fill 'tx' buffer */
1498 fd
= prepare_iso(1024 * 1024, &tx
, &iso
);
1501 /* Standard startup wonkery, but use ide-cd and our special iso file */
1502 ahci
= ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,format=raw "
1504 "-device ide-cd,drive=drive0 ", iso
);
1506 /* Build & Send AHCI command */
1507 ahci_exec(ahci
, ahci_port_select(ahci
), CMD_ATAPI_READ_10
, &opts
);
1511 ahci_shutdown(ahci
);
1512 remove_iso(fd
, iso
);
1515 static void test_cdrom_dma(void)
1517 ahci_test_cdrom(1, true);
1520 static void test_cdrom_dma_multi(void)
1522 ahci_test_cdrom(3, true);
1525 static void test_cdrom_pio(void)
1527 ahci_test_cdrom(1, false);
1530 static void test_cdrom_pio_multi(void)
1532 ahci_test_cdrom(3, false);
1535 /******************************************************************************/
1536 /* AHCI I/O Test Matrix Definitions */
1540 LEN_SIMPLE
= LEN_BEGIN
,
1547 static const char *buff_len_str
[NUM_LENGTHS
] = { "simple", "double",
1551 ADDR_MODE_BEGIN
= 0,
1552 ADDR_MODE_LBA28
= ADDR_MODE_BEGIN
,
1557 static const char *addr_mode_str
[NUM_ADDR_MODES
] = { "lba28", "lba48" };
1561 MODE_PIO
= MODE_BEGIN
,
1566 static const char *io_mode_str
[NUM_MODES
] = { "pio", "dma" };
1577 OFFSET_ZERO
= OFFSET_BEGIN
,
1583 static const char *offset_str
[NUM_OFFSETS
] = { "zero", "low", "high" };
1585 typedef struct AHCIIOTestOptions
{
1586 enum BuffLen length
;
1587 enum AddrMode address_type
;
1588 enum IOMode io_type
;
1589 enum OffsetType offset
;
1590 } AHCIIOTestOptions
;
1592 static uint64_t offset_sector(enum OffsetType ofst
,
1593 enum AddrMode addr_type
,
1605 ceil
= (addr_type
== ADDR_MODE_LBA28
) ? 0xfffffff : 0xffffffffffff;
1606 ceil
= MIN(ceil
, mb_to_sectors(test_image_size_mb
) - 1);
1607 nsectors
= buffsize
/ AHCI_SECTOR_SIZE
;
1608 return ceil
- nsectors
+ 1;
1610 g_assert_not_reached();
1615 * Table of possible I/O ATA commands given a set of enumerations.
1617 static const uint8_t io_cmds
[NUM_MODES
][NUM_ADDR_MODES
][NUM_IO_OPS
] = {
1619 [ADDR_MODE_LBA28
] = {
1620 [IO_READ
] = CMD_READ_PIO
,
1621 [IO_WRITE
] = CMD_WRITE_PIO
},
1622 [ADDR_MODE_LBA48
] = {
1623 [IO_READ
] = CMD_READ_PIO_EXT
,
1624 [IO_WRITE
] = CMD_WRITE_PIO_EXT
}
1627 [ADDR_MODE_LBA28
] = {
1628 [IO_READ
] = CMD_READ_DMA
,
1629 [IO_WRITE
] = CMD_WRITE_DMA
},
1630 [ADDR_MODE_LBA48
] = {
1631 [IO_READ
] = CMD_READ_DMA_EXT
,
1632 [IO_WRITE
] = CMD_WRITE_DMA_EXT
}
1637 * Test a Read/Write pattern using various commands, addressing modes,
1638 * transfer modes, and buffer sizes.
1640 static void test_io_rw_interface(enum AddrMode lba48
, enum IOMode dma
,
1641 unsigned bufsize
, uint64_t sector
)
1645 ahci
= ahci_boot_and_enable(NULL
);
1646 ahci_test_io_rw_simple(ahci
, bufsize
, sector
,
1647 io_cmds
[dma
][lba48
][IO_READ
],
1648 io_cmds
[dma
][lba48
][IO_WRITE
]);
1649 ahci_shutdown(ahci
);
1653 * Demultiplex the test data and invoke the actual test routine.
1655 static void test_io_interface(gconstpointer opaque
)
1657 AHCIIOTestOptions
*opts
= (AHCIIOTestOptions
*)opaque
;
1661 switch (opts
->length
) {
1669 bufsize
= 4096 * 64;
1675 g_assert_not_reached();
1678 sector
= offset_sector(opts
->offset
, opts
->address_type
, bufsize
);
1679 test_io_rw_interface(opts
->address_type
, opts
->io_type
, bufsize
, sector
);
1684 static void create_ahci_io_test(enum IOMode type
, enum AddrMode addr
,
1685 enum BuffLen len
, enum OffsetType offset
)
1688 AHCIIOTestOptions
*opts
;
1690 opts
= g_malloc(sizeof(AHCIIOTestOptions
));
1692 opts
->address_type
= addr
;
1693 opts
->io_type
= type
;
1694 opts
->offset
= offset
;
1696 name
= g_strdup_printf("ahci/io/%s/%s/%s/%s",
1698 addr_mode_str
[addr
],
1700 offset_str
[offset
]);
1702 if ((addr
== ADDR_MODE_LBA48
) && (offset
== OFFSET_HIGH
) &&
1703 (mb_to_sectors(test_image_size_mb
) <= 0xFFFFFFF)) {
1704 g_test_message("%s: skipped; test image too small", name
);
1709 qtest_add_data_func(name
, opts
, test_io_interface
);
1713 /******************************************************************************/
1715 int main(int argc
, char **argv
)
1723 static struct option long_options
[] = {
1724 {"pedantic", no_argument
, 0, 'p' },
1728 /* Should be first to utilize g_test functionality, So we can see errors. */
1729 g_test_init(&argc
, &argv
, NULL
);
1732 c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
1743 fprintf(stderr
, "Unrecognized ahci_test option.\n");
1744 g_assert_not_reached();
1748 /* Check architecture */
1749 arch
= qtest_get_arch();
1750 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
1751 g_test_message("Skipping test for non-x86");
1755 /* Create a temporary image */
1756 fd
= mkstemp(tmp_path
);
1758 if (have_qemu_img()) {
1760 test_image_size_mb
= TEST_IMAGE_SIZE_MB_LARGE
;
1761 mkqcow2(tmp_path
, TEST_IMAGE_SIZE_MB_LARGE
);
1763 g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
1764 "skipping LBA48 high-sector tests");
1766 test_image_size_mb
= TEST_IMAGE_SIZE_MB_SMALL
;
1767 ret
= ftruncate(fd
, test_image_size_mb
* 1024 * 1024);
1772 /* Create temporary blkdebug instructions */
1773 fd
= mkstemp(debug_path
);
1777 /* Reserve a hollow file to use as a socket for migration tests */
1778 fd
= mkstemp(mig_socket
);
1783 qtest_add_func("/ahci/sanity", test_sanity
);
1784 qtest_add_func("/ahci/pci_spec", test_pci_spec
);
1785 qtest_add_func("/ahci/pci_enable", test_pci_enable
);
1786 qtest_add_func("/ahci/hba_spec", test_hba_spec
);
1787 qtest_add_func("/ahci/hba_enable", test_hba_enable
);
1788 qtest_add_func("/ahci/identify", test_identify
);
1790 for (i
= MODE_BEGIN
; i
< NUM_MODES
; i
++) {
1791 for (j
= ADDR_MODE_BEGIN
; j
< NUM_ADDR_MODES
; j
++) {
1792 for (k
= LEN_BEGIN
; k
< NUM_LENGTHS
; k
++) {
1793 for (m
= OFFSET_BEGIN
; m
< NUM_OFFSETS
; m
++) {
1794 create_ahci_io_test(i
, j
, k
, m
);
1800 qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented
);
1802 qtest_add_func("/ahci/flush/simple", test_flush
);
1803 qtest_add_func("/ahci/flush/retry", test_flush_retry
);
1804 qtest_add_func("/ahci/flush/migrate", test_flush_migrate
);
1806 qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity
);
1807 qtest_add_func("/ahci/migrate/dma/simple", test_migrate_dma
);
1808 qtest_add_func("/ahci/io/dma/lba28/retry", test_halted_dma
);
1809 qtest_add_func("/ahci/migrate/dma/halted", test_migrate_halted_dma
);
1811 qtest_add_func("/ahci/max", test_max
);
1812 qtest_add_func("/ahci/reset", test_reset
);
1814 qtest_add_func("/ahci/io/ncq/simple", test_ncq_simple
);
1815 qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq
);
1816 qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq
);
1817 qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq
);
1819 qtest_add_func("/ahci/cdrom/dma/single", test_cdrom_dma
);
1820 qtest_add_func("/ahci/cdrom/dma/multi", test_cdrom_dma_multi
);
1821 qtest_add_func("/ahci/cdrom/pio/single", test_cdrom_pio
);
1822 qtest_add_func("/ahci/cdrom/pio/multi", test_cdrom_pio_multi
);