Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-2.8-20161017' into staging
[qemu/ar7.git] / tests / ahci-test.c
blob9c0adce220aec71cbf45f0d71d18e9234e1c2965
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);
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)
1072 uint64_t ptr;
1073 unsigned bufsize = 512;
1075 ptr = ahci_alloc(ahci, bufsize);
1076 g_assert(ptr);
1078 ahci_guest_io(ahci, port, CMD_WRITE_DMA, ptr, bufsize, 1);
1079 ahci_free(ahci, ptr);
1082 static void test_flush(void)
1084 AHCIQState *ahci;
1085 uint8_t port;
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)
1100 AHCIQState *ahci;
1101 AHCICommand *cmd;
1102 uint8_t port;
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 "
1108 "-M q35 "
1109 "-device ide-hd,drive=drive0 ",
1110 debug_path,
1111 tmp_path, imgfmt);
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);
1142 ahci_shutdown(src);
1143 ahci_shutdown(dst);
1144 g_free(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;
1153 uint8_t px;
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 ",
1161 imgfmt, tmp_path);
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);
1168 /* initialize */
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);
1183 ahci_shutdown(src);
1184 ahci_shutdown(dst);
1185 g_free(rx);
1186 g_free(tx);
1187 g_free(uri);
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)
1209 AHCIQState *ahci;
1210 uint8_t port;
1211 size_t bufsize = 4096;
1212 unsigned char *tx = g_malloc(bufsize);
1213 unsigned char *rx = g_malloc0(bufsize);
1214 uint64_t ptr;
1215 AHCICommand *cmd;
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 "
1222 "-M q35 "
1223 "-device ide-hd,drive=drive0 ",
1224 debug_path,
1225 tmp_path, imgfmt);
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);
1234 g_assert(ptr);
1235 memwrite(ptr, tx, bufsize);
1237 /* Attempt to write (and fail) */
1238 cmd = ahci_guest_io_halt(ahci, port, cmd_write,
1239 ptr, bufsize, 0);
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);
1251 g_free(rx);
1252 g_free(tx);
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;
1275 uint8_t port;
1276 size_t bufsize = 4096;
1277 unsigned char *tx = g_malloc(bufsize);
1278 unsigned char *rx = g_malloc0(bufsize);
1279 uint64_t ptr;
1280 AHCICommand *cmd;
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 "
1288 "-M q35 "
1289 "-device ide-hd,drive=drive0 ",
1290 debug_path,
1291 tmp_path, imgfmt);
1293 dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
1294 "format=%s,cache=writeback,"
1295 "rerror=stop,werror=stop "
1296 "-M q35 "
1297 "-device ide-hd,drive=drive0 "
1298 "-incoming %s",
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);
1310 g_assert(ptr);
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,
1315 ptr, bufsize, 0);
1316 ahci_migrate(src, dst, uri);
1317 ahci_guest_io_resume(dst, cmd);
1318 ahci_free(dst, ptr);
1320 /* Read back */
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. */
1327 ahci_shutdown(src);
1328 ahci_shutdown(dst);
1329 g_free(rx);
1330 g_free(tx);
1331 g_free(uri);
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;
1350 AHCICommand *cmd;
1351 uint8_t px;
1352 const char *s;
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,"
1359 "format=%s "
1360 "-M q35 "
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,"
1365 "format=%s "
1366 "-M q35 "
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");
1384 /* Migrate over */
1385 ahci_migrate(src, dst, uri);
1387 /* Complete the command */
1388 s = "{'execute':'cont' }";
1389 qmp_async(s);
1390 qmp_eventwait("RESUME");
1391 ahci_command_wait(dst, cmd);
1392 ahci_command_verify(dst, cmd);
1394 ahci_command_free(cmd);
1395 ahci_shutdown(src);
1396 ahci_shutdown(dst);
1397 g_free(uri);
1400 static void test_max(void)
1402 AHCIQState *ahci;
1404 ahci = ahci_boot_and_enable(NULL);
1405 ahci_test_max(ahci);
1406 ahci_shutdown(ahci);
1409 static void test_reset(void)
1411 AHCIQState *ahci;
1412 int i;
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,
1423 CMD_READ_DMA_EXT,
1424 CMD_WRITE_DMA_EXT);
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)
1434 AHCIQState *ahci;
1436 ahci = ahci_boot_and_enable(NULL);
1437 ahci_test_io_rw_simple(ahci, 4096, 0,
1438 READ_FPDMA_QUEUED,
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;
1447 ssize_t ret;
1448 int fd = mkstemp(cdrom_path);
1450 g_assert(buf);
1451 g_assert(name);
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);
1460 *buf = patt;
1461 return fd;
1464 static void remove_iso(int fd, char *name)
1466 unlink(name);
1467 g_free(name);
1468 close(fd);
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);
1479 g_free(rx);
1481 return 0;
1484 static void ahci_test_cdrom(int nsectors, bool dma)
1486 AHCIQState *ahci;
1487 unsigned char *tx;
1488 char *iso;
1489 int fd;
1490 AHCIOpts opts = {
1491 .size = (ATAPI_SECTOR_SIZE * nsectors),
1492 .atapi = true,
1493 .atapi_dma = dma,
1494 .post_cb = ahci_cb_cmp_buff,
1497 /* Prepare ISO and fill 'tx' buffer */
1498 fd = prepare_iso(1024 * 1024, &tx, &iso);
1499 opts.opaque = tx;
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 "
1503 "-M q35 "
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);
1509 /* Cleanup */
1510 g_free(tx);
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 */
1538 enum BuffLen {
1539 LEN_BEGIN = 0,
1540 LEN_SIMPLE = LEN_BEGIN,
1541 LEN_DOUBLE,
1542 LEN_LONG,
1543 LEN_SHORT,
1544 NUM_LENGTHS
1547 static const char *buff_len_str[NUM_LENGTHS] = { "simple", "double",
1548 "long", "short" };
1550 enum AddrMode {
1551 ADDR_MODE_BEGIN = 0,
1552 ADDR_MODE_LBA28 = ADDR_MODE_BEGIN,
1553 ADDR_MODE_LBA48,
1554 NUM_ADDR_MODES
1557 static const char *addr_mode_str[NUM_ADDR_MODES] = { "lba28", "lba48" };
1559 enum IOMode {
1560 MODE_BEGIN = 0,
1561 MODE_PIO = MODE_BEGIN,
1562 MODE_DMA,
1563 NUM_MODES
1566 static const char *io_mode_str[NUM_MODES] = { "pio", "dma" };
1568 enum IOOps {
1569 IO_BEGIN = 0,
1570 IO_READ = IO_BEGIN,
1571 IO_WRITE,
1572 NUM_IO_OPS
1575 enum OffsetType {
1576 OFFSET_BEGIN = 0,
1577 OFFSET_ZERO = OFFSET_BEGIN,
1578 OFFSET_LOW,
1579 OFFSET_HIGH,
1580 NUM_OFFSETS
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,
1594 uint64_t buffsize)
1596 uint64_t ceil;
1597 uint64_t nsectors;
1599 switch (ofst) {
1600 case OFFSET_ZERO:
1601 return 0;
1602 case OFFSET_LOW:
1603 return 1;
1604 case OFFSET_HIGH:
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;
1609 default:
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] = {
1618 [MODE_PIO] = {
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 }
1626 [MODE_DMA] = {
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)
1643 AHCIQState *ahci;
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;
1658 unsigned bufsize;
1659 uint64_t sector;
1661 switch (opts->length) {
1662 case LEN_SIMPLE:
1663 bufsize = 4096;
1664 break;
1665 case LEN_DOUBLE:
1666 bufsize = 8192;
1667 break;
1668 case LEN_LONG:
1669 bufsize = 4096 * 64;
1670 break;
1671 case LEN_SHORT:
1672 bufsize = 512;
1673 break;
1674 default:
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);
1680 g_free(opts);
1681 return;
1684 static void create_ahci_io_test(enum IOMode type, enum AddrMode addr,
1685 enum BuffLen len, enum OffsetType offset)
1687 char *name;
1688 AHCIIOTestOptions *opts;
1690 opts = g_malloc(sizeof(AHCIIOTestOptions));
1691 opts->length = len;
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",
1697 io_mode_str[type],
1698 addr_mode_str[addr],
1699 buff_len_str[len],
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);
1705 g_free(name);
1706 return;
1709 qtest_add_data_func(name, opts, test_io_interface);
1710 g_free(name);
1713 /******************************************************************************/
1715 int main(int argc, char **argv)
1717 const char *arch;
1718 int ret;
1719 int fd;
1720 int c;
1721 int i, j, k, m;
1723 static struct option long_options[] = {
1724 {"pedantic", no_argument, 0, 'p' },
1725 {0, 0, 0, 0},
1728 /* Should be first to utilize g_test functionality, So we can see errors. */
1729 g_test_init(&argc, &argv, NULL);
1731 while (1) {
1732 c = getopt_long(argc, argv, "", long_options, NULL);
1733 if (c == -1) {
1734 break;
1736 switch (c) {
1737 case -1:
1738 break;
1739 case 'p':
1740 ahci_pedantic = 1;
1741 break;
1742 default:
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");
1752 return 0;
1755 /* Create a temporary image */
1756 fd = mkstemp(tmp_path);
1757 g_assert(fd >= 0);
1758 if (have_qemu_img()) {
1759 imgfmt = "qcow2";
1760 test_image_size_mb = TEST_IMAGE_SIZE_MB_LARGE;
1761 mkqcow2(tmp_path, TEST_IMAGE_SIZE_MB_LARGE);
1762 } else {
1763 g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
1764 "skipping LBA48 high-sector tests");
1765 imgfmt = "raw";
1766 test_image_size_mb = TEST_IMAGE_SIZE_MB_SMALL;
1767 ret = ftruncate(fd, test_image_size_mb * 1024 * 1024);
1768 g_assert(ret == 0);
1770 close(fd);
1772 /* Create temporary blkdebug instructions */
1773 fd = mkstemp(debug_path);
1774 g_assert(fd >= 0);
1775 close(fd);
1777 /* Reserve a hollow file to use as a socket for migration tests */
1778 fd = mkstemp(mig_socket);
1779 g_assert(fd >= 0);
1780 close(fd);
1782 /* Run the tests */
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);
1824 ret = g_test_run();
1826 /* Cleanup */
1827 unlink(tmp_path);
1828 unlink(debug_path);
1829 unlink(mig_socket);
1831 return ret;