hw/ppc/spapr: Add some missing hcall function set strings
[qemu.git] / tests / ahci-test.c
blob57dc44cf3b2adce7e231c3563d786ed96dcbe837
1 /*
2 * AHCI test cases
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include <getopt.h>
28 #include "libqtest.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
43 /*** Globals ***/
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,
55 uint8_t offset);
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);
60 /*** Utilities ***/
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);
70 bytes /= 2;
72 while (bytes--) {
73 *s = bswap16(*s);
74 s++;
78 /**
79 * Verify that the transfer did not corrupt our state at all.
81 static void verify_state(AHCIQState *ahci)
83 int i, j;
84 uint32_t ahci_fingerprint;
85 uint64_t hba_base;
86 uint64_t hba_stored;
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) {
94 return;
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), ==,
106 ahci->port[i].fb);
107 g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_CLB), ==,
108 ahci->port[i].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;
123 if (uri == NULL) {
124 uri_local = g_strdup_printf("%s%s", "unix:", mig_socket);
125 uri = uri_local;
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));
135 to->parent = tmp;
136 to->dev = dev;
138 tmp = from->parent;
139 dev = from->dev;
140 memset(from, 0x00, sizeof(AHCIQState));
141 from->parent = tmp;
142 from->dev = dev;
144 verify_state(to);
145 g_free(uri_local);
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)
155 AHCIQState *s;
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);
164 return s;
168 * Start a Q35 machine and bookmark a handle to the AHCI device.
170 static AHCIQState *ahci_boot(const char *cli, ...)
172 AHCIQState *s;
173 va_list ap;
175 if (cli) {
176 va_start(ap, cli);
177 s = ahci_vboot(cli, ap);
178 va_end(ap);
179 } else {
180 cli = "-drive if=none,id=drive0,file=%s,cache=writeback,serial=%s"
181 ",format=%s"
182 " -M q35 "
183 "-device ide-hd,drive=drive0 "
184 "-global ide-hd.ver=%s";
185 s = ahci_boot(cli, tmp_path, "testdisk", imgfmt, "version");
188 return s;
192 * Clean up the PCI device, then terminate the QEMU instance.
194 static void ahci_shutdown(AHCIQState *ahci)
196 QOSState *qs = ahci->parent;
198 set_context(qs);
199 ahci_clean_mem(ahci);
200 free_ahci_device(ahci->dev);
201 g_free(ahci);
202 qtest_shutdown(qs);
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, ...)
211 AHCIQState *ahci;
212 va_list ap;
213 uint16_t buff[256];
214 uint8_t port;
215 uint8_t hello;
217 if (cli) {
218 va_start(ap, cli);
219 ahci = ahci_vboot(cli, ap);
220 va_end(ap);
221 } else {
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;
232 } else {
233 hello = CMD_IDENTIFY;
235 ahci_io(ahci, port, hello, &buff, sizeof(buff), 0);
237 return ahci;
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)
247 uint8_t datab;
248 uint16_t data;
249 uint32_t datal;
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);
278 if (ahci_pedantic) {
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) {
287 /* IDE */
288 ASSERT_BIT_SET(0x80000000, datal);
289 ASSERT_BIT_CLEAR(0x60000000, datal);
290 } else if (PCI_SCC(datal) == 0x04) {
291 /* RAID */
292 g_assert_cmphex(PCI_PI(datal), ==, 0);
293 } else if (PCI_SCC(datal) == 0x06) {
294 /* AHCI */
295 g_assert_cmphex(PCI_PI(datal), ==, 0x01);
296 } else {
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);
337 break;
338 default:
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);
345 /* Reserved. */
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,
358 uint8_t offset)
360 uint8_t cid = header & 0xFF;
361 uint8_t next = header >> 8;
363 g_test_message("CID: %02x; next: %02x", cid, next);
365 switch (cid) {
366 case PCI_CAP_ID_PM:
367 ahci_test_pmcap(ahci, offset);
368 break;
369 case PCI_CAP_ID_MSI:
370 ahci_test_msicap(ahci, offset);
371 break;
372 case PCI_CAP_ID_SATA:
373 ahci_test_satacap(ahci, offset);
374 break;
376 default:
377 g_test_message("Unknown CAP 0x%02x", cid);
380 if (next) {
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)
390 uint16_t dataw;
391 uint32_t datal;
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 */
405 case 0x06:
406 case 0x07:
407 case 0x08:
408 case 0x09: /* BAR5 */
409 case 0x0F: /* Immediately following SATACR1 in PCI config space. */
410 break;
411 default:
412 /* Invalid BARLOC for the Index Data Pair. */
413 g_assert_not_reached();
416 /* Reserved. */
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)
425 uint16_t dataw;
426 uint32_t datal;
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);
444 } else {
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)
456 uint16_t dataw;
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)
475 unsigned i;
476 uint32_t reg;
477 uint32_t ports;
478 uint8_t nports_impl;
479 uint8_t maxports;
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);
515 } else {
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);
540 /* 5 AHCI Version */
541 reg = ahci_rreg(ahci, AHCI_VS);
542 switch (reg) {
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:
548 break;
549 default:
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);
560 } else {
561 g_assert_cmphex(reg, ==, 0);
564 /* 7 CCC_PORTS */
565 reg = ahci_rreg(ahci, AHCI_CCCPORTS);
566 /* Must be zeroes initially regardless of CAP.CCCS */
567 g_assert_cmphex(reg, ==, 0);
569 /* 8 EM_LOC */
570 reg = ahci_rreg(ahci, AHCI_EMLOC);
571 if (BITCLR(ahci->cap, AHCI_CAP_EMS)) {
572 g_assert_cmphex(reg, ==, 0);
575 /* 9 EM_CTL */
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);
582 } else {
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);
622 } else {
623 uint16_t j;
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.",
628 i, low, high - 1);
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)
642 uint32_t reg;
643 unsigned i;
645 /* (0) CLB */
646 reg = ahci_px_rreg(ahci, port, AHCI_PX_CLB);
647 ASSERT_BIT_CLEAR(reg, AHCI_PX_CLB_RESERVED);
649 /* (1) CLBU */
650 if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
651 reg = ahci_px_rreg(ahci, port, AHCI_PX_CLBU);
652 g_assert_cmphex(reg, ==, 0);
655 /* (2) FB */
656 reg = ahci_px_rreg(ahci, port, AHCI_PX_FB);
657 ASSERT_BIT_CLEAR(reg, AHCI_PX_FB_RESERVED);
659 /* (3) FBU */
660 if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
661 reg = ahci_px_rreg(ahci, port, AHCI_PX_FBU);
662 g_assert_cmphex(reg, ==, 0);
665 /* (4) IS */
666 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
667 g_assert_cmphex(reg, ==, 0);
669 /* (5) IE */
670 reg = ahci_px_rreg(ahci, port, AHCI_PX_IE);
671 g_assert_cmphex(reg, ==, 0);
673 /* (6) CMD */
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);
711 /* (7) RESERVED */
712 reg = ahci_px_rreg(ahci, port, AHCI_PX_RES1);
713 g_assert_cmphex(reg, ==, 0);
715 /* (8) TFD */
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);
727 /* (9) SIG */
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);
751 /* (14) CI */
752 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
753 g_assert_cmphex(reg, ==, 0);
755 /* (15) SNTF */
756 reg = ahci_px_rreg(ahci, port, AHCI_PX_SNTF);
757 g_assert_cmphex(reg, ==, 0);
759 /* (16) FBS */
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);
781 if (reg) {
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)
793 uint16_t buff[256];
794 unsigned px;
795 int rc;
796 uint16_t sect_size;
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,
851 uint8_t write_cmd)
853 uint64_t ptr;
854 uint8_t port;
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);
867 g_assert(ptr);
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);
883 g_free(tx);
884 g_free(rx);
887 static uint8_t ahci_test_nondata(AHCIQState *ahci, uint8_t ide_cmd)
889 uint8_t port;
891 /* Sanitize */
892 port = ahci_port_select(ahci);
893 ahci_port_clear(ahci, port);
895 ahci_io(ahci, port, ide_cmd, NULL, 0, 0);
897 return port;
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);
908 uint64_t nsect;
909 uint8_t port;
910 uint8_t cmd;
911 uint64_t config_sect = mb_to_sectors(test_image_size_mb) - 1;
913 if (config_sect > 0xFFFFFF) {
914 cmd = CMD_READ_MAX_EXT;
915 } else {
916 cmd = CMD_READ_MAX;
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);
929 g_free(d2h);
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)
942 AHCIQState *ahci;
943 ahci = ahci_boot(NULL);
944 ahci_shutdown(ahci);
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)
953 AHCIQState *ahci;
954 ahci = ahci_boot(NULL);
955 ahci_test_pci_spec(ahci);
956 ahci_shutdown(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)
965 AHCIQState *ahci;
966 ahci = ahci_boot(NULL);
967 ahci_pci_enable(ahci);
968 ahci_shutdown(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)
977 AHCIQState *ahci;
979 ahci = ahci_boot(NULL);
980 ahci_pci_enable(ahci);
981 ahci_test_hba_spec(ahci);
982 ahci_shutdown(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)
991 AHCIQState *ahci;
993 ahci = ahci_boot(NULL);
994 ahci_pci_enable(ahci);
995 ahci_hba_enable(ahci);
996 ahci_shutdown(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)
1005 AHCIQState *ahci;
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)
1020 AHCIQState *ahci;
1021 AHCICommand *cmd;
1022 uint8_t px;
1023 size_t bufsize = 4096;
1024 unsigned char *tx = g_malloc(bufsize);
1025 unsigned char *rx = g_malloc0(bufsize);
1026 uint64_t ptr;
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);
1037 g_assert(ptr);
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);
1062 g_free(rx);
1063 g_free(tx);
1066 static void test_flush(void)
1068 AHCIQState *ahci;
1070 ahci = ahci_boot_and_enable(NULL);
1071 ahci_test_flush(ahci);
1072 ahci_shutdown(ahci);
1075 static void test_flush_retry(void)
1077 AHCIQState *ahci;
1078 AHCICommand *cmd;
1079 uint8_t port;
1081 prepare_blkdebug_script(debug_path, "flush_to_disk");
1082 ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1083 "format=%s,cache=writeback,"
1084 "rerror=stop,werror=stop "
1085 "-M q35 "
1086 "-device ide-hd,drive=drive0 ",
1087 debug_path,
1088 tmp_path, imgfmt);
1090 /* Issue Flush Command and wait for error */
1091 port = ahci_port_select(ahci);
1092 ahci_port_clear(ahci, port);
1094 cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
1095 ahci_guest_io_resume(ahci, cmd);
1097 ahci_shutdown(ahci);
1101 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
1103 static void test_migrate_sanity(void)
1105 AHCIQState *src, *dst;
1106 char *uri = g_strdup_printf("unix:%s", mig_socket);
1108 src = ahci_boot("-m 1024 -M q35 "
1109 "-drive if=ide,file=%s,format=%s ", tmp_path, imgfmt);
1110 dst = ahci_boot("-m 1024 -M q35 "
1111 "-drive if=ide,file=%s,format=%s "
1112 "-incoming %s", tmp_path, imgfmt, uri);
1114 ahci_migrate(src, dst, uri);
1116 ahci_shutdown(src);
1117 ahci_shutdown(dst);
1118 g_free(uri);
1122 * Simple migration test: Write a pattern, migrate, then read.
1124 static void ahci_migrate_simple(uint8_t cmd_read, uint8_t cmd_write)
1126 AHCIQState *src, *dst;
1127 uint8_t px;
1128 size_t bufsize = 4096;
1129 unsigned char *tx = g_malloc(bufsize);
1130 unsigned char *rx = g_malloc0(bufsize);
1131 char *uri = g_strdup_printf("unix:%s", mig_socket);
1133 src = ahci_boot_and_enable("-m 1024 -M q35 "
1134 "-drive if=ide,format=%s,file=%s ",
1135 imgfmt, tmp_path);
1136 dst = ahci_boot("-m 1024 -M q35 "
1137 "-drive if=ide,format=%s,file=%s "
1138 "-incoming %s", imgfmt, tmp_path, uri);
1140 set_context(src->parent);
1142 /* initialize */
1143 px = ahci_port_select(src);
1144 ahci_port_clear(src, px);
1146 /* create pattern */
1147 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1149 /* Write, migrate, then read. */
1150 ahci_io(src, px, cmd_write, tx, bufsize, 0);
1151 ahci_migrate(src, dst, uri);
1152 ahci_io(dst, px, cmd_read, rx, bufsize, 0);
1154 /* Verify pattern */
1155 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1157 ahci_shutdown(src);
1158 ahci_shutdown(dst);
1159 g_free(rx);
1160 g_free(tx);
1161 g_free(uri);
1164 static void test_migrate_dma(void)
1166 ahci_migrate_simple(CMD_READ_DMA, CMD_WRITE_DMA);
1169 static void test_migrate_ncq(void)
1171 ahci_migrate_simple(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
1175 * Halted IO Error Test
1177 * Simulate an error on first write, Try to write a pattern,
1178 * Confirm the VM has stopped, resume the VM, verify command
1179 * has completed, then read back the data and verify.
1181 static void ahci_halted_io_test(uint8_t cmd_read, uint8_t cmd_write)
1183 AHCIQState *ahci;
1184 uint8_t port;
1185 size_t bufsize = 4096;
1186 unsigned char *tx = g_malloc(bufsize);
1187 unsigned char *rx = g_malloc0(bufsize);
1188 uint64_t ptr;
1189 AHCICommand *cmd;
1191 prepare_blkdebug_script(debug_path, "write_aio");
1193 ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1194 "format=%s,cache=writeback,"
1195 "rerror=stop,werror=stop "
1196 "-M q35 "
1197 "-device ide-hd,drive=drive0 ",
1198 debug_path,
1199 tmp_path, imgfmt);
1201 /* Initialize and prepare */
1202 port = ahci_port_select(ahci);
1203 ahci_port_clear(ahci, port);
1205 /* create DMA source buffer and write pattern */
1206 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1207 ptr = ahci_alloc(ahci, bufsize);
1208 g_assert(ptr);
1209 memwrite(ptr, tx, bufsize);
1211 /* Attempt to write (and fail) */
1212 cmd = ahci_guest_io_halt(ahci, port, cmd_write,
1213 ptr, bufsize, 0);
1215 /* Attempt to resume the command */
1216 ahci_guest_io_resume(ahci, cmd);
1217 ahci_free(ahci, ptr);
1219 /* Read back and verify */
1220 ahci_io(ahci, port, cmd_read, rx, bufsize, 0);
1221 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1223 /* Cleanup and go home */
1224 ahci_shutdown(ahci);
1225 g_free(rx);
1226 g_free(tx);
1229 static void test_halted_dma(void)
1231 ahci_halted_io_test(CMD_READ_DMA, CMD_WRITE_DMA);
1234 static void test_halted_ncq(void)
1236 ahci_halted_io_test(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
1240 * IO Error Migration Test
1242 * Simulate an error on first write, Try to write a pattern,
1243 * Confirm the VM has stopped, migrate, resume the VM,
1244 * verify command has completed, then read back the data and verify.
1246 static void ahci_migrate_halted_io(uint8_t cmd_read, uint8_t cmd_write)
1248 AHCIQState *src, *dst;
1249 uint8_t port;
1250 size_t bufsize = 4096;
1251 unsigned char *tx = g_malloc(bufsize);
1252 unsigned char *rx = g_malloc0(bufsize);
1253 uint64_t ptr;
1254 AHCICommand *cmd;
1255 char *uri = g_strdup_printf("unix:%s", mig_socket);
1257 prepare_blkdebug_script(debug_path, "write_aio");
1259 src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1260 "format=%s,cache=writeback,"
1261 "rerror=stop,werror=stop "
1262 "-M q35 "
1263 "-device ide-hd,drive=drive0 ",
1264 debug_path,
1265 tmp_path, imgfmt);
1267 dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
1268 "format=%s,cache=writeback,"
1269 "rerror=stop,werror=stop "
1270 "-M q35 "
1271 "-device ide-hd,drive=drive0 "
1272 "-incoming %s",
1273 tmp_path, imgfmt, uri);
1275 set_context(src->parent);
1277 /* Initialize and prepare */
1278 port = ahci_port_select(src);
1279 ahci_port_clear(src, port);
1280 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1282 /* create DMA source buffer and write pattern */
1283 ptr = ahci_alloc(src, bufsize);
1284 g_assert(ptr);
1285 memwrite(ptr, tx, bufsize);
1287 /* Write, trigger the VM to stop, migrate, then resume. */
1288 cmd = ahci_guest_io_halt(src, port, cmd_write,
1289 ptr, bufsize, 0);
1290 ahci_migrate(src, dst, uri);
1291 ahci_guest_io_resume(dst, cmd);
1292 ahci_free(dst, ptr);
1294 /* Read back */
1295 ahci_io(dst, port, cmd_read, rx, bufsize, 0);
1297 /* Verify TX and RX are identical */
1298 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1300 /* Cleanup and go home. */
1301 ahci_shutdown(src);
1302 ahci_shutdown(dst);
1303 g_free(rx);
1304 g_free(tx);
1305 g_free(uri);
1308 static void test_migrate_halted_dma(void)
1310 ahci_migrate_halted_io(CMD_READ_DMA, CMD_WRITE_DMA);
1313 static void test_migrate_halted_ncq(void)
1315 ahci_migrate_halted_io(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
1319 * Migration test: Try to flush, migrate, then resume.
1321 static void test_flush_migrate(void)
1323 AHCIQState *src, *dst;
1324 AHCICommand *cmd;
1325 uint8_t px;
1326 const char *s;
1327 char *uri = g_strdup_printf("unix:%s", mig_socket);
1329 prepare_blkdebug_script(debug_path, "flush_to_disk");
1331 src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1332 "cache=writeback,rerror=stop,werror=stop,"
1333 "format=%s "
1334 "-M q35 "
1335 "-device ide-hd,drive=drive0 ",
1336 debug_path, tmp_path, imgfmt);
1337 dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
1338 "cache=writeback,rerror=stop,werror=stop,"
1339 "format=%s "
1340 "-M q35 "
1341 "-device ide-hd,drive=drive0 "
1342 "-incoming %s", tmp_path, imgfmt, uri);
1344 set_context(src->parent);
1346 /* Issue Flush Command */
1347 px = ahci_port_select(src);
1348 ahci_port_clear(src, px);
1349 cmd = ahci_command_create(CMD_FLUSH_CACHE);
1350 ahci_command_commit(src, cmd, px);
1351 ahci_command_issue_async(src, cmd);
1352 qmp_eventwait("STOP");
1354 /* Migrate over */
1355 ahci_migrate(src, dst, uri);
1357 /* Complete the command */
1358 s = "{'execute':'cont' }";
1359 qmp_async(s);
1360 qmp_eventwait("RESUME");
1361 ahci_command_wait(dst, cmd);
1362 ahci_command_verify(dst, cmd);
1364 ahci_command_free(cmd);
1365 ahci_shutdown(src);
1366 ahci_shutdown(dst);
1367 g_free(uri);
1370 static void test_max(void)
1372 AHCIQState *ahci;
1374 ahci = ahci_boot_and_enable(NULL);
1375 ahci_test_max(ahci);
1376 ahci_shutdown(ahci);
1379 static void test_reset(void)
1381 AHCIQState *ahci;
1382 int i;
1384 ahci = ahci_boot(NULL);
1385 ahci_test_pci_spec(ahci);
1386 ahci_pci_enable(ahci);
1388 for (i = 0; i < 2; i++) {
1389 ahci_test_hba_spec(ahci);
1390 ahci_hba_enable(ahci);
1391 ahci_test_identify(ahci);
1392 ahci_test_io_rw_simple(ahci, 4096, 0,
1393 CMD_READ_DMA_EXT,
1394 CMD_WRITE_DMA_EXT);
1395 ahci_set(ahci, AHCI_GHC, AHCI_GHC_HR);
1396 ahci_clean_mem(ahci);
1399 ahci_shutdown(ahci);
1402 static void test_ncq_simple(void)
1404 AHCIQState *ahci;
1406 ahci = ahci_boot_and_enable(NULL);
1407 ahci_test_io_rw_simple(ahci, 4096, 0,
1408 READ_FPDMA_QUEUED,
1409 WRITE_FPDMA_QUEUED);
1410 ahci_shutdown(ahci);
1413 static int prepare_iso(size_t size, unsigned char **buf, char **name)
1415 char cdrom_path[] = "/tmp/qtest.iso.XXXXXX";
1416 unsigned char *patt;
1417 ssize_t ret;
1418 int fd = mkstemp(cdrom_path);
1420 g_assert(buf);
1421 g_assert(name);
1422 patt = g_malloc(size);
1424 /* Generate a pattern and build a CDROM image to read from */
1425 generate_pattern(patt, size, ATAPI_SECTOR_SIZE);
1426 ret = write(fd, patt, size);
1427 g_assert(ret == size);
1429 *name = g_strdup(cdrom_path);
1430 *buf = patt;
1431 return fd;
1434 static void remove_iso(int fd, char *name)
1436 unlink(name);
1437 g_free(name);
1438 close(fd);
1441 static int ahci_cb_cmp_buff(AHCIQState *ahci, AHCICommand *cmd,
1442 const AHCIOpts *opts)
1444 unsigned char *tx = opts->opaque;
1445 unsigned char *rx = g_malloc0(opts->size);
1447 bufread(opts->buffer, rx, opts->size);
1448 g_assert_cmphex(memcmp(tx, rx, opts->size), ==, 0);
1449 g_free(rx);
1451 return 0;
1454 static void ahci_test_cdrom(int nsectors, bool dma)
1456 AHCIQState *ahci;
1457 unsigned char *tx;
1458 char *iso;
1459 int fd;
1460 AHCIOpts opts = {
1461 .size = (ATAPI_SECTOR_SIZE * nsectors),
1462 .atapi = true,
1463 .atapi_dma = dma,
1464 .post_cb = ahci_cb_cmp_buff,
1467 /* Prepare ISO and fill 'tx' buffer */
1468 fd = prepare_iso(1024 * 1024, &tx, &iso);
1469 opts.opaque = tx;
1471 /* Standard startup wonkery, but use ide-cd and our special iso file */
1472 ahci = ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,format=raw "
1473 "-M q35 "
1474 "-device ide-cd,drive=drive0 ", iso);
1476 /* Build & Send AHCI command */
1477 ahci_exec(ahci, ahci_port_select(ahci), CMD_ATAPI_READ_10, &opts);
1479 /* Cleanup */
1480 g_free(tx);
1481 ahci_shutdown(ahci);
1482 remove_iso(fd, iso);
1485 static void test_cdrom_dma(void)
1487 ahci_test_cdrom(1, true);
1490 static void test_cdrom_dma_multi(void)
1492 ahci_test_cdrom(3, true);
1495 static void test_cdrom_pio(void)
1497 ahci_test_cdrom(1, false);
1500 static void test_cdrom_pio_multi(void)
1502 ahci_test_cdrom(3, false);
1505 /******************************************************************************/
1506 /* AHCI I/O Test Matrix Definitions */
1508 enum BuffLen {
1509 LEN_BEGIN = 0,
1510 LEN_SIMPLE = LEN_BEGIN,
1511 LEN_DOUBLE,
1512 LEN_LONG,
1513 LEN_SHORT,
1514 NUM_LENGTHS
1517 static const char *buff_len_str[NUM_LENGTHS] = { "simple", "double",
1518 "long", "short" };
1520 enum AddrMode {
1521 ADDR_MODE_BEGIN = 0,
1522 ADDR_MODE_LBA28 = ADDR_MODE_BEGIN,
1523 ADDR_MODE_LBA48,
1524 NUM_ADDR_MODES
1527 static const char *addr_mode_str[NUM_ADDR_MODES] = { "lba28", "lba48" };
1529 enum IOMode {
1530 MODE_BEGIN = 0,
1531 MODE_PIO = MODE_BEGIN,
1532 MODE_DMA,
1533 NUM_MODES
1536 static const char *io_mode_str[NUM_MODES] = { "pio", "dma" };
1538 enum IOOps {
1539 IO_BEGIN = 0,
1540 IO_READ = IO_BEGIN,
1541 IO_WRITE,
1542 NUM_IO_OPS
1545 enum OffsetType {
1546 OFFSET_BEGIN = 0,
1547 OFFSET_ZERO = OFFSET_BEGIN,
1548 OFFSET_LOW,
1549 OFFSET_HIGH,
1550 NUM_OFFSETS
1553 static const char *offset_str[NUM_OFFSETS] = { "zero", "low", "high" };
1555 typedef struct AHCIIOTestOptions {
1556 enum BuffLen length;
1557 enum AddrMode address_type;
1558 enum IOMode io_type;
1559 enum OffsetType offset;
1560 } AHCIIOTestOptions;
1562 static uint64_t offset_sector(enum OffsetType ofst,
1563 enum AddrMode addr_type,
1564 uint64_t buffsize)
1566 uint64_t ceil;
1567 uint64_t nsectors;
1569 switch (ofst) {
1570 case OFFSET_ZERO:
1571 return 0;
1572 case OFFSET_LOW:
1573 return 1;
1574 case OFFSET_HIGH:
1575 ceil = (addr_type == ADDR_MODE_LBA28) ? 0xfffffff : 0xffffffffffff;
1576 ceil = MIN(ceil, mb_to_sectors(test_image_size_mb) - 1);
1577 nsectors = buffsize / AHCI_SECTOR_SIZE;
1578 return ceil - nsectors + 1;
1579 default:
1580 g_assert_not_reached();
1585 * Table of possible I/O ATA commands given a set of enumerations.
1587 static const uint8_t io_cmds[NUM_MODES][NUM_ADDR_MODES][NUM_IO_OPS] = {
1588 [MODE_PIO] = {
1589 [ADDR_MODE_LBA28] = {
1590 [IO_READ] = CMD_READ_PIO,
1591 [IO_WRITE] = CMD_WRITE_PIO },
1592 [ADDR_MODE_LBA48] = {
1593 [IO_READ] = CMD_READ_PIO_EXT,
1594 [IO_WRITE] = CMD_WRITE_PIO_EXT }
1596 [MODE_DMA] = {
1597 [ADDR_MODE_LBA28] = {
1598 [IO_READ] = CMD_READ_DMA,
1599 [IO_WRITE] = CMD_WRITE_DMA },
1600 [ADDR_MODE_LBA48] = {
1601 [IO_READ] = CMD_READ_DMA_EXT,
1602 [IO_WRITE] = CMD_WRITE_DMA_EXT }
1607 * Test a Read/Write pattern using various commands, addressing modes,
1608 * transfer modes, and buffer sizes.
1610 static void test_io_rw_interface(enum AddrMode lba48, enum IOMode dma,
1611 unsigned bufsize, uint64_t sector)
1613 AHCIQState *ahci;
1615 ahci = ahci_boot_and_enable(NULL);
1616 ahci_test_io_rw_simple(ahci, bufsize, sector,
1617 io_cmds[dma][lba48][IO_READ],
1618 io_cmds[dma][lba48][IO_WRITE]);
1619 ahci_shutdown(ahci);
1623 * Demultiplex the test data and invoke the actual test routine.
1625 static void test_io_interface(gconstpointer opaque)
1627 AHCIIOTestOptions *opts = (AHCIIOTestOptions *)opaque;
1628 unsigned bufsize;
1629 uint64_t sector;
1631 switch (opts->length) {
1632 case LEN_SIMPLE:
1633 bufsize = 4096;
1634 break;
1635 case LEN_DOUBLE:
1636 bufsize = 8192;
1637 break;
1638 case LEN_LONG:
1639 bufsize = 4096 * 64;
1640 break;
1641 case LEN_SHORT:
1642 bufsize = 512;
1643 break;
1644 default:
1645 g_assert_not_reached();
1648 sector = offset_sector(opts->offset, opts->address_type, bufsize);
1649 test_io_rw_interface(opts->address_type, opts->io_type, bufsize, sector);
1650 g_free(opts);
1651 return;
1654 static void create_ahci_io_test(enum IOMode type, enum AddrMode addr,
1655 enum BuffLen len, enum OffsetType offset)
1657 char *name;
1658 AHCIIOTestOptions *opts;
1660 opts = g_malloc(sizeof(AHCIIOTestOptions));
1661 opts->length = len;
1662 opts->address_type = addr;
1663 opts->io_type = type;
1664 opts->offset = offset;
1666 name = g_strdup_printf("ahci/io/%s/%s/%s/%s",
1667 io_mode_str[type],
1668 addr_mode_str[addr],
1669 buff_len_str[len],
1670 offset_str[offset]);
1672 if ((addr == ADDR_MODE_LBA48) && (offset == OFFSET_HIGH) &&
1673 (mb_to_sectors(test_image_size_mb) <= 0xFFFFFFF)) {
1674 g_test_message("%s: skipped; test image too small", name);
1675 g_free(name);
1676 return;
1679 qtest_add_data_func(name, opts, test_io_interface);
1680 g_free(name);
1683 /******************************************************************************/
1685 int main(int argc, char **argv)
1687 const char *arch;
1688 int ret;
1689 int fd;
1690 int c;
1691 int i, j, k, m;
1693 static struct option long_options[] = {
1694 {"pedantic", no_argument, 0, 'p' },
1695 {0, 0, 0, 0},
1698 /* Should be first to utilize g_test functionality, So we can see errors. */
1699 g_test_init(&argc, &argv, NULL);
1701 while (1) {
1702 c = getopt_long(argc, argv, "", long_options, NULL);
1703 if (c == -1) {
1704 break;
1706 switch (c) {
1707 case -1:
1708 break;
1709 case 'p':
1710 ahci_pedantic = 1;
1711 break;
1712 default:
1713 fprintf(stderr, "Unrecognized ahci_test option.\n");
1714 g_assert_not_reached();
1718 /* Check architecture */
1719 arch = qtest_get_arch();
1720 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
1721 g_test_message("Skipping test for non-x86");
1722 return 0;
1725 /* Create a temporary image */
1726 fd = mkstemp(tmp_path);
1727 g_assert(fd >= 0);
1728 if (have_qemu_img()) {
1729 imgfmt = "qcow2";
1730 test_image_size_mb = TEST_IMAGE_SIZE_MB_LARGE;
1731 mkqcow2(tmp_path, TEST_IMAGE_SIZE_MB_LARGE);
1732 } else {
1733 g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
1734 "skipping LBA48 high-sector tests");
1735 imgfmt = "raw";
1736 test_image_size_mb = TEST_IMAGE_SIZE_MB_SMALL;
1737 ret = ftruncate(fd, test_image_size_mb * 1024 * 1024);
1738 g_assert(ret == 0);
1740 close(fd);
1742 /* Create temporary blkdebug instructions */
1743 fd = mkstemp(debug_path);
1744 g_assert(fd >= 0);
1745 close(fd);
1747 /* Reserve a hollow file to use as a socket for migration tests */
1748 fd = mkstemp(mig_socket);
1749 g_assert(fd >= 0);
1750 close(fd);
1752 /* Run the tests */
1753 qtest_add_func("/ahci/sanity", test_sanity);
1754 qtest_add_func("/ahci/pci_spec", test_pci_spec);
1755 qtest_add_func("/ahci/pci_enable", test_pci_enable);
1756 qtest_add_func("/ahci/hba_spec", test_hba_spec);
1757 qtest_add_func("/ahci/hba_enable", test_hba_enable);
1758 qtest_add_func("/ahci/identify", test_identify);
1760 for (i = MODE_BEGIN; i < NUM_MODES; i++) {
1761 for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
1762 for (k = LEN_BEGIN; k < NUM_LENGTHS; k++) {
1763 for (m = OFFSET_BEGIN; m < NUM_OFFSETS; m++) {
1764 create_ahci_io_test(i, j, k, m);
1770 qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented);
1772 qtest_add_func("/ahci/flush/simple", test_flush);
1773 qtest_add_func("/ahci/flush/retry", test_flush_retry);
1774 qtest_add_func("/ahci/flush/migrate", test_flush_migrate);
1776 qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity);
1777 qtest_add_func("/ahci/migrate/dma/simple", test_migrate_dma);
1778 qtest_add_func("/ahci/io/dma/lba28/retry", test_halted_dma);
1779 qtest_add_func("/ahci/migrate/dma/halted", test_migrate_halted_dma);
1781 qtest_add_func("/ahci/max", test_max);
1782 qtest_add_func("/ahci/reset", test_reset);
1784 qtest_add_func("/ahci/io/ncq/simple", test_ncq_simple);
1785 qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq);
1786 qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq);
1787 qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq);
1789 qtest_add_func("/ahci/cdrom/dma/single", test_cdrom_dma);
1790 qtest_add_func("/ahci/cdrom/dma/multi", test_cdrom_dma_multi);
1791 qtest_add_func("/ahci/cdrom/pio/single", test_cdrom_pio);
1792 qtest_add_func("/ahci/cdrom/pio/multi", test_cdrom_pio_multi);
1794 ret = g_test_run();
1796 /* Cleanup */
1797 unlink(tmp_path);
1798 unlink(debug_path);
1799 unlink(mig_socket);
1801 return ret;