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, ...)
234 ahci = ahci_vboot(cli, ap);
237 ahci = ahci_boot(NULL);
240 ahci_pci_enable(ahci);
241 ahci_hba_enable(ahci);
246 /*** Specification Adherence Tests ***/
249 * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
251 static void ahci_test_pci_spec(AHCIQState *ahci)
257 /* Most of these bits should start cleared until we turn them on. */
258 data = qpci_config_readw(ahci->dev, PCI_COMMAND);
259 ASSERT_BIT_CLEAR(data, PCI_COMMAND_MEMORY);
260 ASSERT_BIT_CLEAR(data, PCI_COMMAND_MASTER);
261 ASSERT_BIT_CLEAR(data, PCI_COMMAND_SPECIAL); /* Reserved */
262 ASSERT_BIT_CLEAR(data, PCI_COMMAND_VGA_PALETTE); /* Reserved */
263 ASSERT_BIT_CLEAR(data, PCI_COMMAND_PARITY);
264 ASSERT_BIT_CLEAR(data, PCI_COMMAND_WAIT); /* Reserved */
265 ASSERT_BIT_CLEAR(data, PCI_COMMAND_SERR);
266 ASSERT_BIT_CLEAR(data, PCI_COMMAND_FAST_BACK);
267 ASSERT_BIT_CLEAR(data, PCI_COMMAND_INTX_DISABLE);
268 ASSERT_BIT_CLEAR(data, 0xF800); /* Reserved */
270 data = qpci_config_readw(ahci->dev, PCI_STATUS);
271 ASSERT_BIT_CLEAR(data, 0x01 | 0x02 | 0x04); /* Reserved */
272 ASSERT_BIT_CLEAR(data, PCI_STATUS_INTERRUPT);
273 ASSERT_BIT_SET(data, PCI_STATUS_CAP_LIST); /* must be set */
274 ASSERT_BIT_CLEAR(data, PCI_STATUS_UDF); /* Reserved */
275 ASSERT_BIT_CLEAR(data, PCI_STATUS_PARITY);
276 ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_TARGET_ABORT);
277 ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_TARGET_ABORT);
278 ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_MASTER_ABORT);
279 ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_SYSTEM_ERROR);
280 ASSERT_BIT_CLEAR(data, PCI_STATUS_DETECTED_PARITY);
282 /* RID occupies the low byte, CCs occupy the high three. */
283 datal = qpci_config_readl(ahci->dev, PCI_CLASS_REVISION);
285 /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
286 * Though in practice this is likely seldom true. */
287 ASSERT_BIT_CLEAR(datal, 0xFF);
290 /* BCC *must* equal 0x01. */
291 g_assert_cmphex(PCI_BCC(datal), ==, 0x01);
292 if (PCI_SCC(datal) == 0x01) {
294 ASSERT_BIT_SET(0x80000000, datal);
295 ASSERT_BIT_CLEAR(0x60000000, datal);
296 } else if (PCI_SCC(datal) == 0x04) {
298 g_assert_cmphex(PCI_PI(datal), ==, 0);
299 } else if (PCI_SCC(datal) == 0x06) {
301 g_assert_cmphex(PCI_PI(datal), ==, 0x01);
303 g_assert_not_reached();
306 datab = qpci_config_readb(ahci->dev, PCI_CACHE_LINE_SIZE);
307 g_assert_cmphex(datab, ==, 0);
309 datab = qpci_config_readb(ahci->dev, PCI_LATENCY_TIMER);
310 g_assert_cmphex(datab, ==, 0);
312 /* Only the bottom 7 bits must be off. */
313 datab = qpci_config_readb(ahci->dev, PCI_HEADER_TYPE);
314 ASSERT_BIT_CLEAR(datab, 0x7F);
316 /* BIST is optional, but the low 7 bits must always start off regardless. */
317 datab = qpci_config_readb(ahci->dev, PCI_BIST);
318 ASSERT_BIT_CLEAR(datab, 0x7F);
320 /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
321 datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
322 g_assert_cmphex(datal, ==, 0);
324 qpci_config_writel(ahci->dev, PCI_BASE_ADDRESS_5, 0xFFFFFFFF);
325 datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
326 /* ABAR must be 32-bit, memory mapped, non-prefetchable and
327 * must be >= 512 bytes. To that end, bits 0-8 must be off. */
328 ASSERT_BIT_CLEAR(datal, 0xFF);
330 /* Capability list MUST be present, */
331 datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST);
332 /* But these bits are reserved. */
333 ASSERT_BIT_CLEAR(datal, ~0xFF);
334 g_assert_cmphex(datal, !=, 0);
336 /* Check specification adherence for capability extenstions. */
337 data = qpci_config_readw(ahci->dev, datal);
339 switch (ahci->fingerprint) {
340 case AHCI_INTEL_ICH9:
341 /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
342 g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_MSI);
345 /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
346 g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_PM);
349 ahci_test_pci_caps(ahci, data, (uint8_t)datal);
352 datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST + 4);
353 g_assert_cmphex(datal, ==, 0);
355 /* IPIN might vary, but ILINE must be off. */
356 datab = qpci_config_readb(ahci->dev, PCI_INTERRUPT_LINE);
357 g_assert_cmphex(datab, ==, 0);
361 * Test PCI capabilities for AHCI specification adherence.
363 static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
366 uint8_t cid = header & 0xFF;
367 uint8_t next = header >> 8;
369 g_test_message("CID: %02x; next: %02x", cid, next);
373 ahci_test_pmcap(ahci, offset);
376 ahci_test_msicap(ahci, offset);
378 case PCI_CAP_ID_SATA:
379 ahci_test_satacap(ahci, offset);
383 g_test_message("Unknown CAP 0x%02x", cid);
387 ahci_test_pci_caps(ahci, qpci_config_readw(ahci->dev, next), next);
392 * Test SATA PCI capabilitity for AHCI specification adherence.
394 static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset)
399 g_test_message("Verifying SATACAP");
401 /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
402 dataw = qpci_config_readw(ahci->dev, offset + 2);
403 g_assert_cmphex(dataw, ==, 0x10);
405 /* Grab the SATACR1 register. */
406 datal = qpci_config_readw(ahci->dev, offset + 4);
408 switch (datal & 0x0F) {
409 case 0x04: /* BAR0 */
410 case 0x05: /* BAR1 */
414 case 0x09: /* BAR5 */
415 case 0x0F: /* Immediately following SATACR1 in PCI config space. */
418 /* Invalid BARLOC for the Index Data Pair. */
419 g_assert_not_reached();
423 g_assert_cmphex((datal >> 24), ==, 0x00);
427 * Test MSI PCI capability for AHCI specification adherence.
429 static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset)
434 g_test_message("Verifying MSICAP");
436 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_FLAGS);
437 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_ENABLE);
438 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_QSIZE);
439 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_RESERVED);
441 datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_LO);
442 g_assert_cmphex(datal, ==, 0);
444 if (dataw & PCI_MSI_FLAGS_64BIT) {
445 g_test_message("MSICAP is 64bit");
446 datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_HI);
447 g_assert_cmphex(datal, ==, 0);
448 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_64);
449 g_assert_cmphex(dataw, ==, 0);
451 g_test_message("MSICAP is 32bit");
452 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_32);
453 g_assert_cmphex(dataw, ==, 0);
458 * Test Power Management PCI capability for AHCI specification adherence.
460 static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset)
464 g_test_message("Verifying PMCAP");
466 dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_PMC);
467 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_PME_CLOCK);
468 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_RESERVED);
469 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D1);
470 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D2);
472 dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_CTRL);
473 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_STATE_MASK);
474 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_RESERVED);
475 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SEL_MASK);
476 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SCALE_MASK);
479 static void ahci_test_hba_spec(AHCIQState *ahci)
487 g_assert(ahci != NULL);
490 * Note that the AHCI spec does expect the BIOS to set up a few things:
491 * CAP.SSS - Support for staggered spin-up (t/f)
492 * CAP.SMPS - Support for mechanical presence switches (t/f)
493 * PI - Ports Implemented (1-32)
494 * PxCMD.HPCP - Hot Plug Capable Port
495 * PxCMD.MPSP - Mechanical Presence Switch Present
496 * PxCMD.CPD - Cold Presence Detection support
498 * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
499 * Foreach Port Implemented:
500 * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
501 * -PxCLB/U and PxFB/U are set to valid regions in memory
502 * -PxSUD is set to 1.
503 * -PxSSTS.DET is polled for presence; if detected, we continue:
504 * -PxSERR is cleared with 1's.
505 * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
506 * the device is ready.
509 /* 1 CAP - Capabilities Register */
510 ahci->cap = ahci_rreg(ahci, AHCI_CAP);
511 ASSERT_BIT_CLEAR(ahci->cap, AHCI_CAP_RESERVED);
513 /* 2 GHC - Global Host Control */
514 reg = ahci_rreg(ahci, AHCI_GHC);
515 ASSERT_BIT_CLEAR(reg, AHCI_GHC_HR);
516 ASSERT_BIT_CLEAR(reg, AHCI_GHC_IE);
517 ASSERT_BIT_CLEAR(reg, AHCI_GHC_MRSM);
518 if (BITSET(ahci->cap, AHCI_CAP_SAM)) {
519 g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
520 ASSERT_BIT_SET(reg, AHCI_GHC_AE);
522 g_test_message("Supports AHCI/Legacy mix.");
523 ASSERT_BIT_CLEAR(reg, AHCI_GHC_AE);
526 /* 3 IS - Interrupt Status */
527 reg = ahci_rreg(ahci, AHCI_IS);
528 g_assert_cmphex(reg, ==, 0);
530 /* 4 PI - Ports Implemented */
531 ports = ahci_rreg(ahci, AHCI_PI);
532 /* Ports Implemented must be non-zero. */
533 g_assert_cmphex(ports, !=, 0);
534 /* Ports Implemented must be <= Number of Ports. */
535 nports_impl = ctpopl(ports);
536 g_assert_cmpuint(((AHCI_CAP_NP & ahci->cap) + 1), >=, nports_impl);
538 /* Ports must be within the proper range. Given a mapping of SIZE,
539 * 256 bytes are used for global HBA control, and the rest is used
540 * for ports data, at 0x80 bytes each. */
541 g_assert_cmphex(ahci->barsize, >, 0);
542 maxports = (ahci->barsize - HBA_DATA_REGION_SIZE) / HBA_PORT_DATA_SIZE;
543 /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
544 g_assert_cmphex((reg >> maxports), ==, 0);
547 reg = ahci_rreg(ahci, AHCI_VS);
549 case AHCI_VERSION_0_95:
550 case AHCI_VERSION_1_0:
551 case AHCI_VERSION_1_1:
552 case AHCI_VERSION_1_2:
553 case AHCI_VERSION_1_3:
556 g_assert_not_reached();
559 /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
560 reg = ahci_rreg(ahci, AHCI_CCCCTL);
561 if (BITSET(ahci->cap, AHCI_CAP_CCCS)) {
562 ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_EN);
563 ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_RESERVED);
564 ASSERT_BIT_SET(reg, AHCI_CCCCTL_CC);
565 ASSERT_BIT_SET(reg, AHCI_CCCCTL_TV);
567 g_assert_cmphex(reg, ==, 0);
571 reg = ahci_rreg(ahci, AHCI_CCCPORTS);
572 /* Must be zeroes initially regardless of CAP.CCCS */
573 g_assert_cmphex(reg, ==, 0);
576 reg = ahci_rreg(ahci, AHCI_EMLOC);
577 if (BITCLR(ahci->cap, AHCI_CAP_EMS)) {
578 g_assert_cmphex(reg, ==, 0);
582 reg = ahci_rreg(ahci, AHCI_EMCTL);
583 if (BITSET(ahci->cap, AHCI_CAP_EMS)) {
584 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_STSMR);
585 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLTM);
586 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLRST);
587 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_RESERVED);
589 g_assert_cmphex(reg, ==, 0);
592 /* 10 CAP2 -- Capabilities Extended */
593 ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
594 ASSERT_BIT_CLEAR(ahci->cap2, AHCI_CAP2_RESERVED);
596 /* 11 BOHC -- Bios/OS Handoff Control */
597 reg = ahci_rreg(ahci, AHCI_BOHC);
598 g_assert_cmphex(reg, ==, 0);
600 /* 12 -- 23: Reserved */
601 g_test_message("Verifying HBA reserved area is empty.");
602 for (i = AHCI_RESERVED; i < AHCI_NVMHCI; ++i) {
603 reg = ahci_rreg(ahci, i);
604 g_assert_cmphex(reg, ==, 0);
607 /* 24 -- 39: NVMHCI */
608 if (BITCLR(ahci->cap2, AHCI_CAP2_NVMP)) {
609 g_test_message("Verifying HBA/NVMHCI area is empty.");
610 for (i = AHCI_NVMHCI; i < AHCI_VENDOR; ++i) {
611 reg = ahci_rreg(ahci, i);
612 g_assert_cmphex(reg, ==, 0);
616 /* 40 -- 63: Vendor */
617 g_test_message("Verifying HBA/Vendor area is empty.");
618 for (i = AHCI_VENDOR; i < AHCI_PORTS; ++i) {
619 reg = ahci_rreg(ahci, i);
620 g_assert_cmphex(reg, ==, 0);
623 /* 64 -- XX: Port Space */
624 for (i = 0; ports || (i < maxports); ports >>= 1, ++i) {
625 if (BITSET(ports, 0x1)) {
626 g_test_message("Testing port %u for spec", i);
627 ahci_test_port_spec(ahci, i);
630 uint16_t low = AHCI_PORTS + (32 * i);
631 uint16_t high = AHCI_PORTS + (32 * (i + 1));
632 g_test_message("Asserting unimplemented port %u "
633 "(reg [%u-%u]) is empty.",
635 for (j = low; j < high; ++j) {
636 reg = ahci_rreg(ahci, j);
637 g_assert_cmphex(reg, ==, 0);
644 * Test the memory space for one port for specification adherence.
646 static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
652 reg = ahci_px_rreg(ahci, port, AHCI_PX_CLB);
653 ASSERT_BIT_CLEAR(reg, AHCI_PX_CLB_RESERVED);
656 if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
657 reg = ahci_px_rreg(ahci, port, AHCI_PX_CLBU);
658 g_assert_cmphex(reg, ==, 0);
662 reg = ahci_px_rreg(ahci, port, AHCI_PX_FB);
663 ASSERT_BIT_CLEAR(reg, AHCI_PX_FB_RESERVED);
666 if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
667 reg = ahci_px_rreg(ahci, port, AHCI_PX_FBU);
668 g_assert_cmphex(reg, ==, 0);
672 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
673 g_assert_cmphex(reg, ==, 0);
676 reg = ahci_px_rreg(ahci, port, AHCI_PX_IE);
677 g_assert_cmphex(reg, ==, 0);
680 reg = ahci_px_rreg(ahci, port, AHCI_PX_CMD);
681 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FRE);
682 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_RESERVED);
683 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CCS);
684 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
685 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
686 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_PMA); /* And RW only if CAP.SPM */
687 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_APSTE); /* RW only if CAP2.APST */
688 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ATAPI);
689 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_DLAE);
690 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ALPE); /* RW only if CAP.SALP */
691 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ASP); /* RW only if CAP.SALP */
692 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ICC);
693 /* If CPDetect support does not exist, CPState must be off. */
694 if (BITCLR(reg, AHCI_PX_CMD_CPD)) {
695 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CPS);
697 /* If MPSPresence is not set, MPSState must be off. */
698 if (BITCLR(reg, AHCI_PX_CMD_MPSP)) {
699 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
701 /* If we do not support MPS, MPSS and MPSP must be off. */
702 if (BITCLR(ahci->cap, AHCI_CAP_SMPS)) {
703 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
704 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSP);
706 /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
707 if (BITANY(reg, AHCI_PX_CMD_CPD | AHCI_PX_CMD_MPSP)) {
708 ASSERT_BIT_SET(reg, AHCI_PX_CMD_HPCP);
710 /* HPCP and ESP cannot both be active. */
711 g_assert(!BITSET(reg, AHCI_PX_CMD_HPCP | AHCI_PX_CMD_ESP));
712 /* If CAP.FBSS is not set, FBSCP must not be set. */
713 if (BITCLR(ahci->cap, AHCI_CAP_FBSS)) {
714 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FBSCP);
718 reg = ahci_px_rreg(ahci, port, AHCI_PX_RES1);
719 g_assert_cmphex(reg, ==, 0);
722 reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
723 /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
724 * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
725 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_ERR);
726 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS1);
727 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_DRQ);
728 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS2);
729 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
730 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
731 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_RESERVED);
734 /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
735 * Even when GHC.ST is zero, the AHCI HBA may receive the initial
736 * D2H register FIS and update the signature asynchronously,
737 * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
739 /* (10) SSTS / SCR0: SStatus */
740 reg = ahci_px_rreg(ahci, port, AHCI_PX_SSTS);
741 ASSERT_BIT_CLEAR(reg, AHCI_PX_SSTS_RESERVED);
742 /* Even though the register should be 0 at boot, it is asynchronous and
743 * prone to change, so we cannot test any well known value. */
745 /* (11) SCTL / SCR2: SControl */
746 reg = ahci_px_rreg(ahci, port, AHCI_PX_SCTL);
747 g_assert_cmphex(reg, ==, 0);
749 /* (12) SERR / SCR1: SError */
750 reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
751 g_assert_cmphex(reg, ==, 0);
753 /* (13) SACT / SCR3: SActive */
754 reg = ahci_px_rreg(ahci, port, AHCI_PX_SACT);
755 g_assert_cmphex(reg, ==, 0);
758 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
759 g_assert_cmphex(reg, ==, 0);
762 reg = ahci_px_rreg(ahci, port, AHCI_PX_SNTF);
763 g_assert_cmphex(reg, ==, 0);
766 reg = ahci_px_rreg(ahci, port, AHCI_PX_FBS);
767 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_EN);
768 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEC);
769 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_SDE);
770 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEV);
771 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DWE);
772 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_RESERVED);
773 if (BITSET(ahci->cap, AHCI_CAP_FBSS)) {
774 /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
775 g_assert((reg & AHCI_PX_FBS_ADO) >> ctzl(AHCI_PX_FBS_ADO) >= 2);
778 /* [17 -- 27] RESERVED */
779 for (i = AHCI_PX_RES2; i < AHCI_PX_VS; ++i) {
780 reg = ahci_px_rreg(ahci, port, i);
781 g_assert_cmphex(reg, ==, 0);
784 /* [28 -- 31] Vendor-Specific */
785 for (i = AHCI_PX_VS; i < 32; ++i) {
786 reg = ahci_px_rreg(ahci, port, i);
788 g_test_message("INFO: Vendor register %u non-empty", i);
794 * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
795 * device we see, then read and check the response.
797 static void ahci_test_identify(AHCIQState *ahci)
803 const size_t buffsize = 512;
805 g_assert(ahci != NULL);
808 * This serves as a bit of a tutorial on AHCI device programming:
810 * (1) Create a data buffer for the IDENTIFY response to be sent to
811 * (2) Create a Command Table buffer, where we will store the
812 * command and PRDT (Physical Region Descriptor Table)
813 * (3) Construct an FIS host-to-device command structure, and write it to
814 * the top of the Command Table buffer.
815 * (4) Create one or more Physical Region Descriptors (PRDs) that describe
816 * a location in memory where data may be stored/retrieved.
817 * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
818 * (6) Each AHCI port has up to 32 command slots. Each slot contains a
819 * header that points to a Command Table buffer. Pick an unused slot
820 * and update it to point to the Command Table we have built.
821 * (7) Now: Command #n points to our Command Table, and our Command Table
822 * contains the FIS (that describes our command) and the PRDTL, which
823 * describes our buffer.
824 * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
825 * #n is ready for processing.
828 /* Pick the first implemented and running port */
829 px = ahci_port_select(ahci);
830 g_test_message("Selected port %u for test", px);
832 /* Clear out the FIS Receive area and any pending interrupts. */
833 ahci_port_clear(ahci, px);
835 /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
836 ahci_io(ahci, px, CMD_IDENTIFY, &buff, buffsize, 0);
838 /* Check serial number/version in the buffer */
839 /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
840 * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
841 * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
842 * as a consequence, only needs to unchunk the data on LE machines. */
843 string_bswap16(&buff[10], 20);
844 rc = memcmp(&buff[10], "testdisk ", 20);
845 g_assert_cmphex(rc, ==, 0);
847 string_bswap16(&buff[23], 8);
848 rc = memcmp(&buff[23], "version ", 8);
849 g_assert_cmphex(rc, ==, 0);
851 sect_size = le16_to_cpu(*((uint16_t *)(&buff[5])));
852 g_assert_cmphex(sect_size, ==, AHCI_SECTOR_SIZE);
855 static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,
856 uint64_t sector, uint8_t read_cmd,
861 unsigned char *tx = g_malloc(bufsize);
862 unsigned char *rx = g_malloc0(bufsize);
864 g_assert(ahci != NULL);
866 /* Pick the first running port and clear it. */
867 port = ahci_port_select(ahci);
868 ahci_port_clear(ahci, port);
870 /*** Create pattern and transfer to guest ***/
871 /* Data buffer in the guest */
872 ptr = ahci_alloc(ahci, bufsize);
875 /* Write some indicative pattern to our buffer. */
876 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
877 memwrite(ptr, tx, bufsize);
879 /* Write this buffer to disk, then read it back to the DMA buffer. */
880 ahci_guest_io(ahci, port, write_cmd, ptr, bufsize, sector);
881 qmemset(ptr, 0x00, bufsize);
882 ahci_guest_io(ahci, port, read_cmd, ptr, bufsize, sector);
884 /*** Read back the Data ***/
885 memread(ptr, rx, bufsize);
886 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
888 ahci_free(ahci, ptr);
893 static void ahci_test_nondata(AHCIQState *ahci, uint8_t ide_cmd)
899 px = ahci_port_select(ahci);
900 ahci_port_clear(ahci, px);
903 cmd = ahci_command_create(ide_cmd);
904 ahci_command_commit(ahci, cmd, px);
905 ahci_command_issue(ahci, cmd);
906 ahci_command_verify(ahci, cmd);
907 ahci_command_free(cmd);
910 static void ahci_test_flush(AHCIQState *ahci)
912 ahci_test_nondata(ahci, CMD_FLUSH_CACHE);
916 /******************************************************************************/
917 /* Test Interfaces */
918 /******************************************************************************/
921 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
923 static void test_sanity(void)
926 ahci = ahci_boot(NULL);
931 * Ensure that the PCI configuration space for the AHCI device is in-line with
932 * the AHCI 1.3 specification for initial values.
934 static void test_pci_spec(void)
937 ahci = ahci_boot(NULL);
938 ahci_test_pci_spec(ahci);
943 * Engage the PCI AHCI device and sanity check the response.
944 * Perform additional PCI config space bringup for the HBA.
946 static void test_pci_enable(void)
949 ahci = ahci_boot(NULL);
950 ahci_pci_enable(ahci);
955 * Investigate the memory mapped regions of the HBA,
956 * and test them for AHCI specification adherence.
958 static void test_hba_spec(void)
962 ahci = ahci_boot(NULL);
963 ahci_pci_enable(ahci);
964 ahci_test_hba_spec(ahci);
969 * Engage the HBA functionality of the AHCI PCI device,
970 * and bring it into a functional idle state.
972 static void test_hba_enable(void)
976 ahci = ahci_boot(NULL);
977 ahci_pci_enable(ahci);
978 ahci_hba_enable(ahci);
983 * Bring up the device and issue an IDENTIFY command.
984 * Inspect the state of the HBA device and the data returned.
986 static void test_identify(void)
990 ahci = ahci_boot_and_enable(NULL);
991 ahci_test_identify(ahci);
996 * Fragmented DMA test: Perform a standard 4K DMA read/write
997 * test, but make sure the physical regions are fragmented to
998 * be very small, each just 32 bytes, to see how AHCI performs
999 * with chunks defined to be much less than a sector.
1001 static void test_dma_fragmented(void)
1006 size_t bufsize = 4096;
1007 unsigned char *tx = g_malloc(bufsize);
1008 unsigned char *rx = g_malloc0(bufsize);
1011 ahci = ahci_boot_and_enable(NULL);
1012 px = ahci_port_select(ahci);
1013 ahci_port_clear(ahci, px);
1015 /* create pattern */
1016 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1018 /* Create a DMA buffer in guest memory, and write our pattern to it. */
1019 ptr = guest_alloc(ahci->parent->alloc, bufsize);
1021 memwrite(ptr, tx, bufsize);
1023 cmd = ahci_command_create(CMD_WRITE_DMA);
1024 ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
1025 ahci_command_commit(ahci, cmd, px);
1026 ahci_command_issue(ahci, cmd);
1027 ahci_command_verify(ahci, cmd);
1030 cmd = ahci_command_create(CMD_READ_DMA);
1031 ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
1032 ahci_command_commit(ahci, cmd, px);
1033 ahci_command_issue(ahci, cmd);
1034 ahci_command_verify(ahci, cmd);
1037 /* Read back the guest's receive buffer into local memory */
1038 memread(ptr, rx, bufsize);
1039 guest_free(ahci->parent->alloc, ptr);
1041 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1043 ahci_shutdown(ahci);
1049 static void test_flush(void)
1053 ahci = ahci_boot_and_enable(NULL);
1054 ahci_test_flush(ahci);
1055 ahci_shutdown(ahci);
1058 static void test_flush_retry(void)
1065 prepare_blkdebug_script(debug_path, "flush_to_disk");
1066 ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1067 "format=qcow2,cache=writeback,"
1068 "rerror=stop,werror=stop "
1070 "-device ide-hd,drive=drive0 ",
1074 /* Issue Flush Command */
1075 port = ahci_port_select(ahci);
1076 ahci_port_clear(ahci, port);
1077 cmd = ahci_command_create(CMD_FLUSH_CACHE);
1078 ahci_command_commit(ahci, cmd, port);
1079 ahci_command_issue_async(ahci, cmd);
1080 qmp_eventwait("STOP");
1082 /* Complete the command */
1083 s = "{'execute':'cont' }";
1085 qmp_eventwait("RESUME");
1086 ahci_command_wait(ahci, cmd);
1087 ahci_command_verify(ahci, cmd);
1089 ahci_command_free(cmd);
1090 ahci_shutdown(ahci);
1094 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
1096 static void test_migrate_sanity(void)
1098 AHCIQState *src, *dst;
1099 const char *uri = "tcp:127.0.0.1:1234";
1101 src = ahci_boot("-m 1024 -M q35 "
1102 "-hda %s ", tmp_path);
1103 dst = ahci_boot("-m 1024 -M q35 "
1105 "-incoming %s", tmp_path, uri);
1107 ahci_migrate(src, dst, uri);
1114 * DMA Migration test: Write a pattern, migrate, then read.
1116 static void test_migrate_dma(void)
1118 AHCIQState *src, *dst;
1120 size_t bufsize = 4096;
1121 unsigned char *tx = g_malloc(bufsize);
1122 unsigned char *rx = g_malloc0(bufsize);
1124 const char *uri = "tcp:127.0.0.1:1234";
1126 src = ahci_boot_and_enable("-m 1024 -M q35 "
1127 "-hda %s ", tmp_path);
1128 dst = ahci_boot("-m 1024 -M q35 "
1130 "-incoming %s", tmp_path, uri);
1132 set_context(src->parent);
1135 px = ahci_port_select(src);
1136 ahci_port_clear(src, px);
1138 /* create pattern */
1139 for (i = 0; i < bufsize; i++) {
1140 tx[i] = (bufsize - i);
1143 /* Write, migrate, then read. */
1144 ahci_io(src, px, CMD_WRITE_DMA, tx, bufsize, 0);
1145 ahci_migrate(src, dst, uri);
1146 ahci_io(dst, px, CMD_READ_DMA, rx, bufsize, 0);
1148 /* Verify pattern */
1149 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1157 /******************************************************************************/
1158 /* AHCI I/O Test Matrix Definitions */
1162 LEN_SIMPLE = LEN_BEGIN,
1169 static const char *buff_len_str[NUM_LENGTHS] = { "simple", "double",
1173 ADDR_MODE_BEGIN = 0,
1174 ADDR_MODE_LBA28 = ADDR_MODE_BEGIN,
1179 static const char *addr_mode_str[NUM_ADDR_MODES] = { "lba28", "lba48" };
1183 MODE_PIO = MODE_BEGIN,
1188 static const char *io_mode_str[NUM_MODES] = { "pio", "dma" };
1199 OFFSET_ZERO = OFFSET_BEGIN,
1205 static const char *offset_str[NUM_OFFSETS] = { "zero", "low", "high" };
1207 typedef struct AHCIIOTestOptions {
1208 enum BuffLen length;
1209 enum AddrMode address_type;
1210 enum IOMode io_type;
1211 enum OffsetType offset;
1212 } AHCIIOTestOptions;
1214 static uint64_t offset_sector(enum OffsetType ofst,
1215 enum AddrMode addr_type,
1227 ceil = (addr_type == ADDR_MODE_LBA28) ? 0xfffffff : 0xffffffffffff;
1228 ceil = MIN(ceil, TEST_IMAGE_SECTORS - 1);
1229 nsectors = buffsize / AHCI_SECTOR_SIZE;
1230 return ceil - nsectors + 1;
1232 g_assert_not_reached();
1237 * Table of possible I/O ATA commands given a set of enumerations.
1239 static const uint8_t io_cmds[NUM_MODES][NUM_ADDR_MODES][NUM_IO_OPS] = {
1241 [ADDR_MODE_LBA28] = {
1242 [IO_READ] = CMD_READ_PIO,
1243 [IO_WRITE] = CMD_WRITE_PIO },
1244 [ADDR_MODE_LBA48] = {
1245 [IO_READ] = CMD_READ_PIO_EXT,
1246 [IO_WRITE] = CMD_WRITE_PIO_EXT }
1249 [ADDR_MODE_LBA28] = {
1250 [IO_READ] = CMD_READ_DMA,
1251 [IO_WRITE] = CMD_WRITE_DMA },
1252 [ADDR_MODE_LBA48] = {
1253 [IO_READ] = CMD_READ_DMA_EXT,
1254 [IO_WRITE] = CMD_WRITE_DMA_EXT }
1259 * Test a Read/Write pattern using various commands, addressing modes,
1260 * transfer modes, and buffer sizes.
1262 static void test_io_rw_interface(enum AddrMode lba48, enum IOMode dma,
1263 unsigned bufsize, uint64_t sector)
1267 ahci = ahci_boot_and_enable(NULL);
1268 ahci_test_io_rw_simple(ahci, bufsize, sector,
1269 io_cmds[dma][lba48][IO_READ],
1270 io_cmds[dma][lba48][IO_WRITE]);
1271 ahci_shutdown(ahci);
1275 * Demultiplex the test data and invoke the actual test routine.
1277 static void test_io_interface(gconstpointer opaque)
1279 AHCIIOTestOptions *opts = (AHCIIOTestOptions *)opaque;
1283 switch (opts->length) {
1291 bufsize = 4096 * 64;
1297 g_assert_not_reached();
1300 sector = offset_sector(opts->offset, opts->address_type, bufsize);
1301 test_io_rw_interface(opts->address_type, opts->io_type, bufsize, sector);
1306 static void create_ahci_io_test(enum IOMode type, enum AddrMode addr,
1307 enum BuffLen len, enum OffsetType offset)
1309 static const char *arch;
1311 AHCIIOTestOptions *opts = g_malloc(sizeof(AHCIIOTestOptions));
1314 opts->address_type = addr;
1315 opts->io_type = type;
1316 opts->offset = offset;
1319 arch = qtest_get_arch();
1322 name = g_strdup_printf("/%s/ahci/io/%s/%s/%s/%s", arch,
1324 addr_mode_str[addr],
1326 offset_str[offset]);
1328 g_test_add_data_func(name, opts, test_io_interface);
1332 /******************************************************************************/
1334 int main(int argc, char **argv)
1342 static struct option long_options[] = {
1343 {"pedantic", no_argument, 0, 'p' },
1347 /* Should be first to utilize g_test functionality, So we can see errors. */
1348 g_test_init(&argc, &argv, NULL);
1351 c = getopt_long(argc, argv, "", long_options, NULL);
1362 fprintf(stderr, "Unrecognized ahci_test option.\n");
1363 g_assert_not_reached();
1367 /* Check architecture */
1368 arch = qtest_get_arch();
1369 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
1370 g_test_message("Skipping test for non-x86");
1374 /* Create a temporary qcow2 image */
1375 close(mkstemp(tmp_path));
1376 mkqcow2(tmp_path, TEST_IMAGE_SIZE_MB);
1378 /* Create temporary blkdebug instructions */
1379 fd = mkstemp(debug_path);
1384 qtest_add_func("/ahci/sanity", test_sanity);
1385 qtest_add_func("/ahci/pci_spec", test_pci_spec);
1386 qtest_add_func("/ahci/pci_enable", test_pci_enable);
1387 qtest_add_func("/ahci/hba_spec", test_hba_spec);
1388 qtest_add_func("/ahci/hba_enable", test_hba_enable);
1389 qtest_add_func("/ahci/identify", test_identify);
1391 for (i = MODE_BEGIN; i < NUM_MODES; i++) {
1392 for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
1393 for (k = LEN_BEGIN; k < NUM_LENGTHS; k++) {
1394 for (m = OFFSET_BEGIN; m < NUM_OFFSETS; m++) {
1395 create_ahci_io_test(i, j, k, m);
1401 qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented);
1403 qtest_add_func("/ahci/flush/simple", test_flush);
1404 qtest_add_func("/ahci/flush/retry", test_flush_retry);
1406 qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity);
1407 qtest_add_func("/ahci/migrate/dma", test_migrate_dma);