Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / tests / ahci-test.c
blob28ccd166a02725d6f5007220b066e0547f1760d0
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 "qapi/qmp/qdict.h"
35 #include "qemu/host-utils.h"
37 #include "hw/pci/pci_ids.h"
38 #include "hw/pci/pci_regs.h"
40 /* Test images sizes in MB */
41 #define TEST_IMAGE_SIZE_MB_LARGE (200 * 1024)
42 #define TEST_IMAGE_SIZE_MB_SMALL 64
44 /*** Globals ***/
45 static char tmp_path[] = "/tmp/qtest.XXXXXX";
46 static char debug_path[] = "/tmp/qtest-blkdebug.XXXXXX";
47 static char mig_socket[] = "/tmp/qtest-migration.XXXXXX";
48 static bool ahci_pedantic;
49 static const char *imgfmt;
50 static unsigned test_image_size_mb;
52 /*** Function Declarations ***/
53 static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port);
54 static void ahci_test_pci_spec(AHCIQState *ahci);
55 static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
56 uint8_t offset);
57 static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset);
58 static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset);
59 static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset);
61 /*** Utilities ***/
63 static uint64_t mb_to_sectors(uint64_t image_size_mb)
65 return (image_size_mb * 1024 * 1024) / AHCI_SECTOR_SIZE;
68 static void string_bswap16(uint16_t *s, size_t bytes)
70 g_assert_cmphex((bytes & 1), ==, 0);
71 bytes /= 2;
73 while (bytes--) {
74 *s = bswap16(*s);
75 s++;
79 /**
80 * Verify that the transfer did not corrupt our state at all.
82 static void verify_state(AHCIQState *ahci, uint64_t hba_old)
84 int i, j;
85 uint32_t ahci_fingerprint;
86 uint64_t hba_base;
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->enabled) {
94 return;
97 hba_base = (uint64_t)qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
98 g_assert_cmphex(hba_base, ==, hba_old);
100 g_assert_cmphex(ahci_rreg(ahci, AHCI_CAP), ==, ahci->cap);
101 g_assert_cmphex(ahci_rreg(ahci, AHCI_CAP2), ==, ahci->cap2);
103 for (i = 0; i < 32; i++) {
104 g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_FB), ==,
105 ahci->port[i].fb);
106 g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_CLB), ==,
107 ahci->port[i].clb);
108 for (j = 0; j < 32; j++) {
109 ahci_get_command_header(ahci, i, j, &cmd);
110 g_assert_cmphex(cmd.prdtl, ==, ahci->port[i].prdtl[j]);
111 g_assert_cmphex(cmd.ctba, ==, ahci->port[i].ctba[j]);
116 static void ahci_migrate(AHCIQState *from, AHCIQState *to, const char *uri)
118 QOSState *tmp = to->parent;
119 QPCIDevice *dev = to->dev;
120 char *uri_local = NULL;
121 uint64_t hba_old;
123 if (uri == NULL) {
124 uri_local = g_strdup_printf("%s%s", "unix:", mig_socket);
125 uri = uri_local;
128 hba_old = (uint64_t)qpci_config_readl(from->dev, PCI_BASE_ADDRESS_5);
130 /* context will be 'to' after completion. */
131 migrate(from->parent, to->parent, uri);
133 /* We'd like for the AHCIState objects to still point
134 * to information specific to its specific parent
135 * instance, but otherwise just inherit the new data. */
136 memcpy(to, from, sizeof(AHCIQState));
137 to->parent = tmp;
138 to->dev = dev;
140 tmp = from->parent;
141 dev = from->dev;
142 memset(from, 0x00, sizeof(AHCIQState));
143 from->parent = tmp;
144 from->dev = dev;
146 verify_state(to, hba_old);
147 g_free(uri_local);
150 /*** Test Setup & Teardown ***/
153 * Start a Q35 machine and bookmark a handle to the AHCI device.
155 static GCC_FMT_ATTR(1, 0)
156 AHCIQState *ahci_vboot(const char *cli, va_list ap)
158 AHCIQState *s;
160 s = g_new0(AHCIQState, 1);
161 s->parent = qtest_pc_vboot(cli, ap);
162 alloc_set_flags(s->parent->alloc, ALLOC_LEAK_ASSERT);
164 /* Verify that we have an AHCI device present. */
165 s->dev = get_ahci_device(&s->fingerprint);
167 return s;
171 * Start a Q35 machine and bookmark a handle to the AHCI device.
173 static GCC_FMT_ATTR(1, 0)
174 AHCIQState *ahci_boot(const char *cli, ...)
176 AHCIQState *s;
177 va_list ap;
179 if (cli) {
180 va_start(ap, cli);
181 s = ahci_vboot(cli, ap);
182 va_end(ap);
183 } else {
184 cli = "-drive if=none,id=drive0,file=%s,cache=writeback,serial=%s"
185 ",format=%s"
186 " -M q35 "
187 "-device ide-hd,drive=drive0 "
188 "-global ide-hd.ver=%s";
189 s = ahci_boot(cli, tmp_path, "testdisk", imgfmt, "version");
192 return s;
196 * Clean up the PCI device, then terminate the QEMU instance.
198 static void ahci_shutdown(AHCIQState *ahci)
200 QOSState *qs = ahci->parent;
202 set_context(qs);
203 ahci_clean_mem(ahci);
204 free_ahci_device(ahci->dev);
205 g_free(ahci);
206 qtest_shutdown(qs);
210 * Boot and fully enable the HBA device.
211 * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
213 static GCC_FMT_ATTR(1, 0)
214 AHCIQState *ahci_boot_and_enable(const char *cli, ...)
216 AHCIQState *ahci;
217 va_list ap;
218 uint16_t buff[256];
219 uint8_t port;
220 uint8_t hello;
222 if (cli) {
223 va_start(ap, cli);
224 ahci = ahci_vboot(cli, ap);
225 va_end(ap);
226 } else {
227 ahci = ahci_boot(NULL);
230 ahci_pci_enable(ahci);
231 ahci_hba_enable(ahci);
232 /* Initialize test device */
233 port = ahci_port_select(ahci);
234 ahci_port_clear(ahci, port);
235 if (is_atapi(ahci, port)) {
236 hello = CMD_PACKET_ID;
237 } else {
238 hello = CMD_IDENTIFY;
240 ahci_io(ahci, port, hello, &buff, sizeof(buff), 0);
242 return ahci;
245 /*** Specification Adherence Tests ***/
248 * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
250 static void ahci_test_pci_spec(AHCIQState *ahci)
252 uint8_t datab;
253 uint16_t data;
254 uint32_t datal;
256 /* Most of these bits should start cleared until we turn them on. */
257 data = qpci_config_readw(ahci->dev, PCI_COMMAND);
258 ASSERT_BIT_CLEAR(data, PCI_COMMAND_MEMORY);
259 ASSERT_BIT_CLEAR(data, PCI_COMMAND_MASTER);
260 ASSERT_BIT_CLEAR(data, PCI_COMMAND_SPECIAL); /* Reserved */
261 ASSERT_BIT_CLEAR(data, PCI_COMMAND_VGA_PALETTE); /* Reserved */
262 ASSERT_BIT_CLEAR(data, PCI_COMMAND_PARITY);
263 ASSERT_BIT_CLEAR(data, PCI_COMMAND_WAIT); /* Reserved */
264 ASSERT_BIT_CLEAR(data, PCI_COMMAND_SERR);
265 ASSERT_BIT_CLEAR(data, PCI_COMMAND_FAST_BACK);
266 ASSERT_BIT_CLEAR(data, PCI_COMMAND_INTX_DISABLE);
267 ASSERT_BIT_CLEAR(data, 0xF800); /* Reserved */
269 data = qpci_config_readw(ahci->dev, PCI_STATUS);
270 ASSERT_BIT_CLEAR(data, 0x01 | 0x02 | 0x04); /* Reserved */
271 ASSERT_BIT_CLEAR(data, PCI_STATUS_INTERRUPT);
272 ASSERT_BIT_SET(data, PCI_STATUS_CAP_LIST); /* must be set */
273 ASSERT_BIT_CLEAR(data, PCI_STATUS_UDF); /* Reserved */
274 ASSERT_BIT_CLEAR(data, PCI_STATUS_PARITY);
275 ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_TARGET_ABORT);
276 ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_TARGET_ABORT);
277 ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_MASTER_ABORT);
278 ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_SYSTEM_ERROR);
279 ASSERT_BIT_CLEAR(data, PCI_STATUS_DETECTED_PARITY);
281 /* RID occupies the low byte, CCs occupy the high three. */
282 datal = qpci_config_readl(ahci->dev, PCI_CLASS_REVISION);
283 if (ahci_pedantic) {
284 /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
285 * Though in practice this is likely seldom true. */
286 ASSERT_BIT_CLEAR(datal, 0xFF);
289 /* BCC *must* equal 0x01. */
290 g_assert_cmphex(PCI_BCC(datal), ==, 0x01);
291 if (PCI_SCC(datal) == 0x01) {
292 /* IDE */
293 ASSERT_BIT_SET(0x80000000, datal);
294 ASSERT_BIT_CLEAR(0x60000000, datal);
295 } else if (PCI_SCC(datal) == 0x04) {
296 /* RAID */
297 g_assert_cmphex(PCI_PI(datal), ==, 0);
298 } else if (PCI_SCC(datal) == 0x06) {
299 /* AHCI */
300 g_assert_cmphex(PCI_PI(datal), ==, 0x01);
301 } else {
302 g_assert_not_reached();
305 datab = qpci_config_readb(ahci->dev, PCI_CACHE_LINE_SIZE);
306 g_assert_cmphex(datab, ==, 0);
308 datab = qpci_config_readb(ahci->dev, PCI_LATENCY_TIMER);
309 g_assert_cmphex(datab, ==, 0);
311 /* Only the bottom 7 bits must be off. */
312 datab = qpci_config_readb(ahci->dev, PCI_HEADER_TYPE);
313 ASSERT_BIT_CLEAR(datab, 0x7F);
315 /* BIST is optional, but the low 7 bits must always start off regardless. */
316 datab = qpci_config_readb(ahci->dev, PCI_BIST);
317 ASSERT_BIT_CLEAR(datab, 0x7F);
319 /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
320 datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
321 g_assert_cmphex(datal, ==, 0);
323 qpci_config_writel(ahci->dev, PCI_BASE_ADDRESS_5, 0xFFFFFFFF);
324 datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
325 /* ABAR must be 32-bit, memory mapped, non-prefetchable and
326 * must be >= 512 bytes. To that end, bits 0-8 must be off. */
327 ASSERT_BIT_CLEAR(datal, 0xFF);
329 /* Capability list MUST be present, */
330 datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST);
331 /* But these bits are reserved. */
332 ASSERT_BIT_CLEAR(datal, ~0xFF);
333 g_assert_cmphex(datal, !=, 0);
335 /* Check specification adherence for capability extenstions. */
336 data = qpci_config_readw(ahci->dev, datal);
338 switch (ahci->fingerprint) {
339 case AHCI_INTEL_ICH9:
340 /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
341 g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_MSI);
342 break;
343 default:
344 /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
345 g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_PM);
348 ahci_test_pci_caps(ahci, data, (uint8_t)datal);
350 /* Reserved. */
351 datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST + 4);
352 g_assert_cmphex(datal, ==, 0);
354 /* IPIN might vary, but ILINE must be off. */
355 datab = qpci_config_readb(ahci->dev, PCI_INTERRUPT_LINE);
356 g_assert_cmphex(datab, ==, 0);
360 * Test PCI capabilities for AHCI specification adherence.
362 static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
363 uint8_t offset)
365 uint8_t cid = header & 0xFF;
366 uint8_t next = header >> 8;
368 g_test_message("CID: %02x; next: %02x", cid, next);
370 switch (cid) {
371 case PCI_CAP_ID_PM:
372 ahci_test_pmcap(ahci, offset);
373 break;
374 case PCI_CAP_ID_MSI:
375 ahci_test_msicap(ahci, offset);
376 break;
377 case PCI_CAP_ID_SATA:
378 ahci_test_satacap(ahci, offset);
379 break;
381 default:
382 g_test_message("Unknown CAP 0x%02x", cid);
385 if (next) {
386 ahci_test_pci_caps(ahci, qpci_config_readw(ahci->dev, next), next);
391 * Test SATA PCI capabilitity for AHCI specification adherence.
393 static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset)
395 uint16_t dataw;
396 uint32_t datal;
398 g_test_message("Verifying SATACAP");
400 /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
401 dataw = qpci_config_readw(ahci->dev, offset + 2);
402 g_assert_cmphex(dataw, ==, 0x10);
404 /* Grab the SATACR1 register. */
405 datal = qpci_config_readw(ahci->dev, offset + 4);
407 switch (datal & 0x0F) {
408 case 0x04: /* BAR0 */
409 case 0x05: /* BAR1 */
410 case 0x06:
411 case 0x07:
412 case 0x08:
413 case 0x09: /* BAR5 */
414 case 0x0F: /* Immediately following SATACR1 in PCI config space. */
415 break;
416 default:
417 /* Invalid BARLOC for the Index Data Pair. */
418 g_assert_not_reached();
421 /* Reserved. */
422 g_assert_cmphex((datal >> 24), ==, 0x00);
426 * Test MSI PCI capability for AHCI specification adherence.
428 static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset)
430 uint16_t dataw;
431 uint32_t datal;
433 g_test_message("Verifying MSICAP");
435 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_FLAGS);
436 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_ENABLE);
437 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_QSIZE);
438 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_RESERVED);
440 datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_LO);
441 g_assert_cmphex(datal, ==, 0);
443 if (dataw & PCI_MSI_FLAGS_64BIT) {
444 g_test_message("MSICAP is 64bit");
445 datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_HI);
446 g_assert_cmphex(datal, ==, 0);
447 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_64);
448 g_assert_cmphex(dataw, ==, 0);
449 } else {
450 g_test_message("MSICAP is 32bit");
451 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_32);
452 g_assert_cmphex(dataw, ==, 0);
457 * Test Power Management PCI capability for AHCI specification adherence.
459 static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset)
461 uint16_t dataw;
463 g_test_message("Verifying PMCAP");
465 dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_PMC);
466 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_PME_CLOCK);
467 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_RESERVED);
468 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D1);
469 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D2);
471 dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_CTRL);
472 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_STATE_MASK);
473 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_RESERVED);
474 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SEL_MASK);
475 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SCALE_MASK);
478 static void ahci_test_hba_spec(AHCIQState *ahci)
480 unsigned i;
481 uint32_t reg;
482 uint32_t ports;
483 uint8_t nports_impl;
484 uint8_t maxports;
486 g_assert(ahci != NULL);
489 * Note that the AHCI spec does expect the BIOS to set up a few things:
490 * CAP.SSS - Support for staggered spin-up (t/f)
491 * CAP.SMPS - Support for mechanical presence switches (t/f)
492 * PI - Ports Implemented (1-32)
493 * PxCMD.HPCP - Hot Plug Capable Port
494 * PxCMD.MPSP - Mechanical Presence Switch Present
495 * PxCMD.CPD - Cold Presence Detection support
497 * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
498 * Foreach Port Implemented:
499 * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
500 * -PxCLB/U and PxFB/U are set to valid regions in memory
501 * -PxSUD is set to 1.
502 * -PxSSTS.DET is polled for presence; if detected, we continue:
503 * -PxSERR is cleared with 1's.
504 * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
505 * the device is ready.
508 /* 1 CAP - Capabilities Register */
509 ahci->cap = ahci_rreg(ahci, AHCI_CAP);
510 ASSERT_BIT_CLEAR(ahci->cap, AHCI_CAP_RESERVED);
512 /* 2 GHC - Global Host Control */
513 reg = ahci_rreg(ahci, AHCI_GHC);
514 ASSERT_BIT_CLEAR(reg, AHCI_GHC_HR);
515 ASSERT_BIT_CLEAR(reg, AHCI_GHC_IE);
516 ASSERT_BIT_CLEAR(reg, AHCI_GHC_MRSM);
517 if (BITSET(ahci->cap, AHCI_CAP_SAM)) {
518 g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
519 ASSERT_BIT_SET(reg, AHCI_GHC_AE);
520 } else {
521 g_test_message("Supports AHCI/Legacy mix.");
522 ASSERT_BIT_CLEAR(reg, AHCI_GHC_AE);
525 /* 3 IS - Interrupt Status */
526 reg = ahci_rreg(ahci, AHCI_IS);
527 g_assert_cmphex(reg, ==, 0);
529 /* 4 PI - Ports Implemented */
530 ports = ahci_rreg(ahci, AHCI_PI);
531 /* Ports Implemented must be non-zero. */
532 g_assert_cmphex(ports, !=, 0);
533 /* Ports Implemented must be <= Number of Ports. */
534 nports_impl = ctpopl(ports);
535 g_assert_cmpuint(((AHCI_CAP_NP & ahci->cap) + 1), >=, nports_impl);
537 /* Ports must be within the proper range. Given a mapping of SIZE,
538 * 256 bytes are used for global HBA control, and the rest is used
539 * for ports data, at 0x80 bytes each. */
540 g_assert_cmphex(ahci->barsize, >, 0);
541 maxports = (ahci->barsize - HBA_DATA_REGION_SIZE) / HBA_PORT_DATA_SIZE;
542 /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
543 g_assert_cmphex((reg >> maxports), ==, 0);
545 /* 5 AHCI Version */
546 reg = ahci_rreg(ahci, AHCI_VS);
547 switch (reg) {
548 case AHCI_VERSION_0_95:
549 case AHCI_VERSION_1_0:
550 case AHCI_VERSION_1_1:
551 case AHCI_VERSION_1_2:
552 case AHCI_VERSION_1_3:
553 break;
554 default:
555 g_assert_not_reached();
558 /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
559 reg = ahci_rreg(ahci, AHCI_CCCCTL);
560 if (BITSET(ahci->cap, AHCI_CAP_CCCS)) {
561 ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_EN);
562 ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_RESERVED);
563 ASSERT_BIT_SET(reg, AHCI_CCCCTL_CC);
564 ASSERT_BIT_SET(reg, AHCI_CCCCTL_TV);
565 } else {
566 g_assert_cmphex(reg, ==, 0);
569 /* 7 CCC_PORTS */
570 reg = ahci_rreg(ahci, AHCI_CCCPORTS);
571 /* Must be zeroes initially regardless of CAP.CCCS */
572 g_assert_cmphex(reg, ==, 0);
574 /* 8 EM_LOC */
575 reg = ahci_rreg(ahci, AHCI_EMLOC);
576 if (BITCLR(ahci->cap, AHCI_CAP_EMS)) {
577 g_assert_cmphex(reg, ==, 0);
580 /* 9 EM_CTL */
581 reg = ahci_rreg(ahci, AHCI_EMCTL);
582 if (BITSET(ahci->cap, AHCI_CAP_EMS)) {
583 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_STSMR);
584 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLTM);
585 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLRST);
586 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_RESERVED);
587 } else {
588 g_assert_cmphex(reg, ==, 0);
591 /* 10 CAP2 -- Capabilities Extended */
592 ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
593 ASSERT_BIT_CLEAR(ahci->cap2, AHCI_CAP2_RESERVED);
595 /* 11 BOHC -- Bios/OS Handoff Control */
596 reg = ahci_rreg(ahci, AHCI_BOHC);
597 g_assert_cmphex(reg, ==, 0);
599 /* 12 -- 23: Reserved */
600 g_test_message("Verifying HBA reserved area is empty.");
601 for (i = AHCI_RESERVED; i < AHCI_NVMHCI; ++i) {
602 reg = ahci_rreg(ahci, i);
603 g_assert_cmphex(reg, ==, 0);
606 /* 24 -- 39: NVMHCI */
607 if (BITCLR(ahci->cap2, AHCI_CAP2_NVMP)) {
608 g_test_message("Verifying HBA/NVMHCI area is empty.");
609 for (i = AHCI_NVMHCI; i < AHCI_VENDOR; ++i) {
610 reg = ahci_rreg(ahci, i);
611 g_assert_cmphex(reg, ==, 0);
615 /* 40 -- 63: Vendor */
616 g_test_message("Verifying HBA/Vendor area is empty.");
617 for (i = AHCI_VENDOR; i < AHCI_PORTS; ++i) {
618 reg = ahci_rreg(ahci, i);
619 g_assert_cmphex(reg, ==, 0);
622 /* 64 -- XX: Port Space */
623 for (i = 0; ports || (i < maxports); ports >>= 1, ++i) {
624 if (BITSET(ports, 0x1)) {
625 g_test_message("Testing port %u for spec", i);
626 ahci_test_port_spec(ahci, i);
627 } else {
628 uint16_t j;
629 uint16_t low = AHCI_PORTS + (32 * i);
630 uint16_t high = AHCI_PORTS + (32 * (i + 1));
631 g_test_message("Asserting unimplemented port %u "
632 "(reg [%u-%u]) is empty.",
633 i, low, high - 1);
634 for (j = low; j < high; ++j) {
635 reg = ahci_rreg(ahci, j);
636 g_assert_cmphex(reg, ==, 0);
643 * Test the memory space for one port for specification adherence.
645 static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
647 uint32_t reg;
648 unsigned i;
650 /* (0) CLB */
651 reg = ahci_px_rreg(ahci, port, AHCI_PX_CLB);
652 ASSERT_BIT_CLEAR(reg, AHCI_PX_CLB_RESERVED);
654 /* (1) CLBU */
655 if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
656 reg = ahci_px_rreg(ahci, port, AHCI_PX_CLBU);
657 g_assert_cmphex(reg, ==, 0);
660 /* (2) FB */
661 reg = ahci_px_rreg(ahci, port, AHCI_PX_FB);
662 ASSERT_BIT_CLEAR(reg, AHCI_PX_FB_RESERVED);
664 /* (3) FBU */
665 if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
666 reg = ahci_px_rreg(ahci, port, AHCI_PX_FBU);
667 g_assert_cmphex(reg, ==, 0);
670 /* (4) IS */
671 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
672 g_assert_cmphex(reg, ==, 0);
674 /* (5) IE */
675 reg = ahci_px_rreg(ahci, port, AHCI_PX_IE);
676 g_assert_cmphex(reg, ==, 0);
678 /* (6) CMD */
679 reg = ahci_px_rreg(ahci, port, AHCI_PX_CMD);
680 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FRE);
681 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_RESERVED);
682 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CCS);
683 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
684 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
685 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_PMA); /* And RW only if CAP.SPM */
686 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_APSTE); /* RW only if CAP2.APST */
687 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ATAPI);
688 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_DLAE);
689 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ALPE); /* RW only if CAP.SALP */
690 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ASP); /* RW only if CAP.SALP */
691 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ICC);
692 /* If CPDetect support does not exist, CPState must be off. */
693 if (BITCLR(reg, AHCI_PX_CMD_CPD)) {
694 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CPS);
696 /* If MPSPresence is not set, MPSState must be off. */
697 if (BITCLR(reg, AHCI_PX_CMD_MPSP)) {
698 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
700 /* If we do not support MPS, MPSS and MPSP must be off. */
701 if (BITCLR(ahci->cap, AHCI_CAP_SMPS)) {
702 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
703 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSP);
705 /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
706 if (BITANY(reg, AHCI_PX_CMD_CPD | AHCI_PX_CMD_MPSP)) {
707 ASSERT_BIT_SET(reg, AHCI_PX_CMD_HPCP);
709 /* HPCP and ESP cannot both be active. */
710 g_assert(!BITSET(reg, AHCI_PX_CMD_HPCP | AHCI_PX_CMD_ESP));
711 /* If CAP.FBSS is not set, FBSCP must not be set. */
712 if (BITCLR(ahci->cap, AHCI_CAP_FBSS)) {
713 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FBSCP);
716 /* (7) RESERVED */
717 reg = ahci_px_rreg(ahci, port, AHCI_PX_RES1);
718 g_assert_cmphex(reg, ==, 0);
720 /* (8) TFD */
721 reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
722 /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
723 * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
724 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_ERR);
725 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS1);
726 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_DRQ);
727 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS2);
728 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
729 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
730 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_RESERVED);
732 /* (9) SIG */
733 /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
734 * Even when GHC.ST is zero, the AHCI HBA may receive the initial
735 * D2H register FIS and update the signature asynchronously,
736 * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
738 /* (10) SSTS / SCR0: SStatus */
739 reg = ahci_px_rreg(ahci, port, AHCI_PX_SSTS);
740 ASSERT_BIT_CLEAR(reg, AHCI_PX_SSTS_RESERVED);
741 /* Even though the register should be 0 at boot, it is asynchronous and
742 * prone to change, so we cannot test any well known value. */
744 /* (11) SCTL / SCR2: SControl */
745 reg = ahci_px_rreg(ahci, port, AHCI_PX_SCTL);
746 g_assert_cmphex(reg, ==, 0);
748 /* (12) SERR / SCR1: SError */
749 reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
750 g_assert_cmphex(reg, ==, 0);
752 /* (13) SACT / SCR3: SActive */
753 reg = ahci_px_rreg(ahci, port, AHCI_PX_SACT);
754 g_assert_cmphex(reg, ==, 0);
756 /* (14) CI */
757 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
758 g_assert_cmphex(reg, ==, 0);
760 /* (15) SNTF */
761 reg = ahci_px_rreg(ahci, port, AHCI_PX_SNTF);
762 g_assert_cmphex(reg, ==, 0);
764 /* (16) FBS */
765 reg = ahci_px_rreg(ahci, port, AHCI_PX_FBS);
766 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_EN);
767 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEC);
768 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_SDE);
769 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEV);
770 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DWE);
771 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_RESERVED);
772 if (BITSET(ahci->cap, AHCI_CAP_FBSS)) {
773 /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
774 g_assert((reg & AHCI_PX_FBS_ADO) >> ctzl(AHCI_PX_FBS_ADO) >= 2);
777 /* [17 -- 27] RESERVED */
778 for (i = AHCI_PX_RES2; i < AHCI_PX_VS; ++i) {
779 reg = ahci_px_rreg(ahci, port, i);
780 g_assert_cmphex(reg, ==, 0);
783 /* [28 -- 31] Vendor-Specific */
784 for (i = AHCI_PX_VS; i < 32; ++i) {
785 reg = ahci_px_rreg(ahci, port, i);
786 if (reg) {
787 g_test_message("INFO: Vendor register %u non-empty", i);
793 * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
794 * device we see, then read and check the response.
796 static void ahci_test_identify(AHCIQState *ahci)
798 uint16_t buff[256];
799 unsigned px;
800 int rc;
801 uint16_t sect_size;
802 const size_t buffsize = 512;
804 g_assert(ahci != NULL);
807 * This serves as a bit of a tutorial on AHCI device programming:
809 * (1) Create a data buffer for the IDENTIFY response to be sent to
810 * (2) Create a Command Table buffer, where we will store the
811 * command and PRDT (Physical Region Descriptor Table)
812 * (3) Construct an FIS host-to-device command structure, and write it to
813 * the top of the Command Table buffer.
814 * (4) Create one or more Physical Region Descriptors (PRDs) that describe
815 * a location in memory where data may be stored/retrieved.
816 * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
817 * (6) Each AHCI port has up to 32 command slots. Each slot contains a
818 * header that points to a Command Table buffer. Pick an unused slot
819 * and update it to point to the Command Table we have built.
820 * (7) Now: Command #n points to our Command Table, and our Command Table
821 * contains the FIS (that describes our command) and the PRDTL, which
822 * describes our buffer.
823 * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
824 * #n is ready for processing.
827 /* Pick the first implemented and running port */
828 px = ahci_port_select(ahci);
829 g_test_message("Selected port %u for test", px);
831 /* Clear out the FIS Receive area and any pending interrupts. */
832 ahci_port_clear(ahci, px);
834 /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
835 ahci_io(ahci, px, CMD_IDENTIFY, &buff, buffsize, 0);
837 /* Check serial number/version in the buffer */
838 /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
839 * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
840 * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
841 * as a consequence, only needs to unchunk the data on LE machines. */
842 string_bswap16(&buff[10], 20);
843 rc = memcmp(&buff[10], "testdisk ", 20);
844 g_assert_cmphex(rc, ==, 0);
846 string_bswap16(&buff[23], 8);
847 rc = memcmp(&buff[23], "version ", 8);
848 g_assert_cmphex(rc, ==, 0);
850 sect_size = le16_to_cpu(*((uint16_t *)(&buff[5])));
851 g_assert_cmphex(sect_size, ==, AHCI_SECTOR_SIZE);
854 static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,
855 uint64_t sector, uint8_t read_cmd,
856 uint8_t write_cmd)
858 uint64_t ptr;
859 uint8_t port;
860 unsigned char *tx = g_malloc(bufsize);
861 unsigned char *rx = g_malloc0(bufsize);
863 g_assert(ahci != NULL);
865 /* Pick the first running port and clear it. */
866 port = ahci_port_select(ahci);
867 ahci_port_clear(ahci, port);
869 /*** Create pattern and transfer to guest ***/
870 /* Data buffer in the guest */
871 ptr = ahci_alloc(ahci, bufsize);
872 g_assert(ptr);
874 /* Write some indicative pattern to our buffer. */
875 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
876 bufwrite(ptr, tx, bufsize);
878 /* Write this buffer to disk, then read it back to the DMA buffer. */
879 ahci_guest_io(ahci, port, write_cmd, ptr, bufsize, sector);
880 qmemset(ptr, 0x00, bufsize);
881 ahci_guest_io(ahci, port, read_cmd, ptr, bufsize, sector);
883 /*** Read back the Data ***/
884 bufread(ptr, rx, bufsize);
885 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
887 ahci_free(ahci, ptr);
888 g_free(tx);
889 g_free(rx);
892 static uint8_t ahci_test_nondata(AHCIQState *ahci, uint8_t ide_cmd)
894 uint8_t port;
896 /* Sanitize */
897 port = ahci_port_select(ahci);
898 ahci_port_clear(ahci, port);
900 ahci_io(ahci, port, ide_cmd, NULL, 0, 0);
902 return port;
905 static void ahci_test_flush(AHCIQState *ahci)
907 ahci_test_nondata(ahci, CMD_FLUSH_CACHE);
910 static void ahci_test_max(AHCIQState *ahci)
912 RegD2HFIS *d2h = g_malloc0(0x20);
913 uint64_t nsect;
914 uint8_t port;
915 uint8_t cmd;
916 uint64_t config_sect = mb_to_sectors(test_image_size_mb) - 1;
918 if (config_sect > 0xFFFFFF) {
919 cmd = CMD_READ_MAX_EXT;
920 } else {
921 cmd = CMD_READ_MAX;
924 port = ahci_test_nondata(ahci, cmd);
925 memread(ahci->port[port].fb + 0x40, d2h, 0x20);
926 nsect = (uint64_t)d2h->lba_hi[2] << 40 |
927 (uint64_t)d2h->lba_hi[1] << 32 |
928 (uint64_t)d2h->lba_hi[0] << 24 |
929 (uint64_t)d2h->lba_lo[2] << 16 |
930 (uint64_t)d2h->lba_lo[1] << 8 |
931 (uint64_t)d2h->lba_lo[0];
933 g_assert_cmphex(nsect, ==, config_sect);
934 g_free(d2h);
938 /******************************************************************************/
939 /* Test Interfaces */
940 /******************************************************************************/
943 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
945 static void test_sanity(void)
947 AHCIQState *ahci;
948 ahci = ahci_boot(NULL);
949 ahci_shutdown(ahci);
953 * Ensure that the PCI configuration space for the AHCI device is in-line with
954 * the AHCI 1.3 specification for initial values.
956 static void test_pci_spec(void)
958 AHCIQState *ahci;
959 ahci = ahci_boot(NULL);
960 ahci_test_pci_spec(ahci);
961 ahci_shutdown(ahci);
965 * Engage the PCI AHCI device and sanity check the response.
966 * Perform additional PCI config space bringup for the HBA.
968 static void test_pci_enable(void)
970 AHCIQState *ahci;
971 ahci = ahci_boot(NULL);
972 ahci_pci_enable(ahci);
973 ahci_shutdown(ahci);
977 * Investigate the memory mapped regions of the HBA,
978 * and test them for AHCI specification adherence.
980 static void test_hba_spec(void)
982 AHCIQState *ahci;
984 ahci = ahci_boot(NULL);
985 ahci_pci_enable(ahci);
986 ahci_test_hba_spec(ahci);
987 ahci_shutdown(ahci);
991 * Engage the HBA functionality of the AHCI PCI device,
992 * and bring it into a functional idle state.
994 static void test_hba_enable(void)
996 AHCIQState *ahci;
998 ahci = ahci_boot(NULL);
999 ahci_pci_enable(ahci);
1000 ahci_hba_enable(ahci);
1001 ahci_shutdown(ahci);
1005 * Bring up the device and issue an IDENTIFY command.
1006 * Inspect the state of the HBA device and the data returned.
1008 static void test_identify(void)
1010 AHCIQState *ahci;
1012 ahci = ahci_boot_and_enable(NULL);
1013 ahci_test_identify(ahci);
1014 ahci_shutdown(ahci);
1018 * Fragmented DMA test: Perform a standard 4K DMA read/write
1019 * test, but make sure the physical regions are fragmented to
1020 * be very small, each just 32 bytes, to see how AHCI performs
1021 * with chunks defined to be much less than a sector.
1023 static void test_dma_fragmented(void)
1025 AHCIQState *ahci;
1026 AHCICommand *cmd;
1027 uint8_t px;
1028 size_t bufsize = 4096;
1029 unsigned char *tx = g_malloc(bufsize);
1030 unsigned char *rx = g_malloc0(bufsize);
1031 uint64_t ptr;
1033 ahci = ahci_boot_and_enable(NULL);
1034 px = ahci_port_select(ahci);
1035 ahci_port_clear(ahci, px);
1037 /* create pattern */
1038 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1040 /* Create a DMA buffer in guest memory, and write our pattern to it. */
1041 ptr = guest_alloc(ahci->parent->alloc, bufsize);
1042 g_assert(ptr);
1043 bufwrite(ptr, tx, bufsize);
1045 cmd = ahci_command_create(CMD_WRITE_DMA);
1046 ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
1047 ahci_command_commit(ahci, cmd, px);
1048 ahci_command_issue(ahci, cmd);
1049 ahci_command_verify(ahci, cmd);
1050 ahci_command_free(cmd);
1052 cmd = ahci_command_create(CMD_READ_DMA);
1053 ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
1054 ahci_command_commit(ahci, cmd, px);
1055 ahci_command_issue(ahci, cmd);
1056 ahci_command_verify(ahci, cmd);
1057 ahci_command_free(cmd);
1059 /* Read back the guest's receive buffer into local memory */
1060 bufread(ptr, rx, bufsize);
1061 guest_free(ahci->parent->alloc, ptr);
1063 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1065 ahci_shutdown(ahci);
1067 g_free(rx);
1068 g_free(tx);
1072 * Write sector 1 with random data to make AHCI storage dirty
1073 * Needed for flush tests so that flushes actually go though the block layer
1075 static void make_dirty(AHCIQState* ahci, uint8_t port)
1077 uint64_t ptr;
1078 unsigned bufsize = 512;
1080 ptr = ahci_alloc(ahci, bufsize);
1081 g_assert(ptr);
1083 ahci_guest_io(ahci, port, CMD_WRITE_DMA, ptr, bufsize, 1);
1084 ahci_free(ahci, ptr);
1087 static void test_flush(void)
1089 AHCIQState *ahci;
1090 uint8_t port;
1092 ahci = ahci_boot_and_enable(NULL);
1094 port = ahci_port_select(ahci);
1095 ahci_port_clear(ahci, port);
1097 make_dirty(ahci, port);
1099 ahci_test_flush(ahci);
1100 ahci_shutdown(ahci);
1103 static void test_flush_retry(void)
1105 AHCIQState *ahci;
1106 AHCICommand *cmd;
1107 uint8_t port;
1109 prepare_blkdebug_script(debug_path, "flush_to_disk");
1110 ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1111 "format=%s,cache=writeback,"
1112 "rerror=stop,werror=stop "
1113 "-M q35 "
1114 "-device ide-hd,drive=drive0 ",
1115 debug_path,
1116 tmp_path, imgfmt);
1118 port = ahci_port_select(ahci);
1119 ahci_port_clear(ahci, port);
1121 /* Issue write so that flush actually goes to disk */
1122 make_dirty(ahci, port);
1124 /* Issue Flush Command and wait for error */
1125 cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
1126 ahci_guest_io_resume(ahci, cmd);
1128 ahci_shutdown(ahci);
1132 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
1134 static void test_migrate_sanity(void)
1136 AHCIQState *src, *dst;
1137 char *uri = g_strdup_printf("unix:%s", mig_socket);
1139 src = ahci_boot("-m 384 -M q35 "
1140 "-drive if=ide,file=%s,format=%s ", tmp_path, imgfmt);
1141 dst = ahci_boot("-m 384 -M q35 "
1142 "-drive if=ide,file=%s,format=%s "
1143 "-incoming %s", tmp_path, imgfmt, uri);
1145 ahci_migrate(src, dst, uri);
1147 ahci_shutdown(src);
1148 ahci_shutdown(dst);
1149 g_free(uri);
1153 * Simple migration test: Write a pattern, migrate, then read.
1155 static void ahci_migrate_simple(uint8_t cmd_read, uint8_t cmd_write)
1157 AHCIQState *src, *dst;
1158 uint8_t px;
1159 size_t bufsize = 4096;
1160 unsigned char *tx = g_malloc(bufsize);
1161 unsigned char *rx = g_malloc0(bufsize);
1162 char *uri = g_strdup_printf("unix:%s", mig_socket);
1164 src = ahci_boot_and_enable("-m 384 -M q35 "
1165 "-drive if=ide,format=%s,file=%s ",
1166 imgfmt, tmp_path);
1167 dst = ahci_boot("-m 384 -M q35 "
1168 "-drive if=ide,format=%s,file=%s "
1169 "-incoming %s", imgfmt, tmp_path, uri);
1171 set_context(src->parent);
1173 /* initialize */
1174 px = ahci_port_select(src);
1175 ahci_port_clear(src, px);
1177 /* create pattern */
1178 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1180 /* Write, migrate, then read. */
1181 ahci_io(src, px, cmd_write, tx, bufsize, 0);
1182 ahci_migrate(src, dst, uri);
1183 ahci_io(dst, px, cmd_read, rx, bufsize, 0);
1185 /* Verify pattern */
1186 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1188 ahci_shutdown(src);
1189 ahci_shutdown(dst);
1190 g_free(rx);
1191 g_free(tx);
1192 g_free(uri);
1195 static void test_migrate_dma(void)
1197 ahci_migrate_simple(CMD_READ_DMA, CMD_WRITE_DMA);
1200 static void test_migrate_ncq(void)
1202 ahci_migrate_simple(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
1206 * Halted IO Error Test
1208 * Simulate an error on first write, Try to write a pattern,
1209 * Confirm the VM has stopped, resume the VM, verify command
1210 * has completed, then read back the data and verify.
1212 static void ahci_halted_io_test(uint8_t cmd_read, uint8_t cmd_write)
1214 AHCIQState *ahci;
1215 uint8_t port;
1216 size_t bufsize = 4096;
1217 unsigned char *tx = g_malloc(bufsize);
1218 unsigned char *rx = g_malloc0(bufsize);
1219 uint64_t ptr;
1220 AHCICommand *cmd;
1222 prepare_blkdebug_script(debug_path, "write_aio");
1224 ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1225 "format=%s,cache=writeback,"
1226 "rerror=stop,werror=stop "
1227 "-M q35 "
1228 "-device ide-hd,drive=drive0 ",
1229 debug_path,
1230 tmp_path, imgfmt);
1232 /* Initialize and prepare */
1233 port = ahci_port_select(ahci);
1234 ahci_port_clear(ahci, port);
1236 /* create DMA source buffer and write pattern */
1237 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1238 ptr = ahci_alloc(ahci, bufsize);
1239 g_assert(ptr);
1240 memwrite(ptr, tx, bufsize);
1242 /* Attempt to write (and fail) */
1243 cmd = ahci_guest_io_halt(ahci, port, cmd_write,
1244 ptr, bufsize, 0);
1246 /* Attempt to resume the command */
1247 ahci_guest_io_resume(ahci, cmd);
1248 ahci_free(ahci, ptr);
1250 /* Read back and verify */
1251 ahci_io(ahci, port, cmd_read, rx, bufsize, 0);
1252 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1254 /* Cleanup and go home */
1255 ahci_shutdown(ahci);
1256 g_free(rx);
1257 g_free(tx);
1260 static void test_halted_dma(void)
1262 ahci_halted_io_test(CMD_READ_DMA, CMD_WRITE_DMA);
1265 static void test_halted_ncq(void)
1267 ahci_halted_io_test(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
1271 * IO Error Migration Test
1273 * Simulate an error on first write, Try to write a pattern,
1274 * Confirm the VM has stopped, migrate, resume the VM,
1275 * verify command has completed, then read back the data and verify.
1277 static void ahci_migrate_halted_io(uint8_t cmd_read, uint8_t cmd_write)
1279 AHCIQState *src, *dst;
1280 uint8_t port;
1281 size_t bufsize = 4096;
1282 unsigned char *tx = g_malloc(bufsize);
1283 unsigned char *rx = g_malloc0(bufsize);
1284 uint64_t ptr;
1285 AHCICommand *cmd;
1286 char *uri = g_strdup_printf("unix:%s", mig_socket);
1288 prepare_blkdebug_script(debug_path, "write_aio");
1290 src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1291 "format=%s,cache=writeback,"
1292 "rerror=stop,werror=stop "
1293 "-M q35 "
1294 "-device ide-hd,drive=drive0 ",
1295 debug_path,
1296 tmp_path, imgfmt);
1298 dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
1299 "format=%s,cache=writeback,"
1300 "rerror=stop,werror=stop "
1301 "-M q35 "
1302 "-device ide-hd,drive=drive0 "
1303 "-incoming %s",
1304 tmp_path, imgfmt, uri);
1306 set_context(src->parent);
1308 /* Initialize and prepare */
1309 port = ahci_port_select(src);
1310 ahci_port_clear(src, port);
1311 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1313 /* create DMA source buffer and write pattern */
1314 ptr = ahci_alloc(src, bufsize);
1315 g_assert(ptr);
1316 memwrite(ptr, tx, bufsize);
1318 /* Write, trigger the VM to stop, migrate, then resume. */
1319 cmd = ahci_guest_io_halt(src, port, cmd_write,
1320 ptr, bufsize, 0);
1321 ahci_migrate(src, dst, uri);
1322 ahci_guest_io_resume(dst, cmd);
1323 ahci_free(dst, ptr);
1325 /* Read back */
1326 ahci_io(dst, port, cmd_read, rx, bufsize, 0);
1328 /* Verify TX and RX are identical */
1329 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1331 /* Cleanup and go home. */
1332 ahci_shutdown(src);
1333 ahci_shutdown(dst);
1334 g_free(rx);
1335 g_free(tx);
1336 g_free(uri);
1339 static void test_migrate_halted_dma(void)
1341 ahci_migrate_halted_io(CMD_READ_DMA, CMD_WRITE_DMA);
1344 static void test_migrate_halted_ncq(void)
1346 ahci_migrate_halted_io(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
1350 * Migration test: Try to flush, migrate, then resume.
1352 static void test_flush_migrate(void)
1354 AHCIQState *src, *dst;
1355 AHCICommand *cmd;
1356 uint8_t px;
1357 const char *s;
1358 char *uri = g_strdup_printf("unix:%s", mig_socket);
1360 prepare_blkdebug_script(debug_path, "flush_to_disk");
1362 src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1363 "cache=writeback,rerror=stop,werror=stop,"
1364 "format=%s "
1365 "-M q35 "
1366 "-device ide-hd,drive=drive0 ",
1367 debug_path, tmp_path, imgfmt);
1368 dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
1369 "cache=writeback,rerror=stop,werror=stop,"
1370 "format=%s "
1371 "-M q35 "
1372 "-device ide-hd,drive=drive0 "
1373 "-incoming %s", tmp_path, imgfmt, uri);
1375 set_context(src->parent);
1377 px = ahci_port_select(src);
1378 ahci_port_clear(src, px);
1380 /* Dirty device so that flush reaches disk */
1381 make_dirty(src, px);
1383 /* Issue Flush Command */
1384 cmd = ahci_command_create(CMD_FLUSH_CACHE);
1385 ahci_command_commit(src, cmd, px);
1386 ahci_command_issue_async(src, cmd);
1387 qmp_eventwait("STOP");
1389 /* Migrate over */
1390 ahci_migrate(src, dst, uri);
1392 /* Complete the command */
1393 s = "{'execute':'cont' }";
1394 qmp_async(s);
1395 qmp_eventwait("RESUME");
1396 ahci_command_wait(dst, cmd);
1397 ahci_command_verify(dst, cmd);
1399 ahci_command_free(cmd);
1400 ahci_shutdown(src);
1401 ahci_shutdown(dst);
1402 g_free(uri);
1405 static void test_max(void)
1407 AHCIQState *ahci;
1409 ahci = ahci_boot_and_enable(NULL);
1410 ahci_test_max(ahci);
1411 ahci_shutdown(ahci);
1414 static void test_reset(void)
1416 AHCIQState *ahci;
1417 int i;
1419 ahci = ahci_boot(NULL);
1420 ahci_test_pci_spec(ahci);
1421 ahci_pci_enable(ahci);
1423 for (i = 0; i < 2; i++) {
1424 ahci_test_hba_spec(ahci);
1425 ahci_hba_enable(ahci);
1426 ahci_test_identify(ahci);
1427 ahci_test_io_rw_simple(ahci, 4096, 0,
1428 CMD_READ_DMA_EXT,
1429 CMD_WRITE_DMA_EXT);
1430 ahci_set(ahci, AHCI_GHC, AHCI_GHC_HR);
1431 ahci_clean_mem(ahci);
1434 ahci_shutdown(ahci);
1437 static void test_ncq_simple(void)
1439 AHCIQState *ahci;
1441 ahci = ahci_boot_and_enable(NULL);
1442 ahci_test_io_rw_simple(ahci, 4096, 0,
1443 READ_FPDMA_QUEUED,
1444 WRITE_FPDMA_QUEUED);
1445 ahci_shutdown(ahci);
1448 static int prepare_iso(size_t size, unsigned char **buf, char **name)
1450 char cdrom_path[] = "/tmp/qtest.iso.XXXXXX";
1451 unsigned char *patt;
1452 ssize_t ret;
1453 int fd = mkstemp(cdrom_path);
1455 g_assert(buf);
1456 g_assert(name);
1457 patt = g_malloc(size);
1459 /* Generate a pattern and build a CDROM image to read from */
1460 generate_pattern(patt, size, ATAPI_SECTOR_SIZE);
1461 ret = write(fd, patt, size);
1462 g_assert(ret == size);
1464 *name = g_strdup(cdrom_path);
1465 *buf = patt;
1466 return fd;
1469 static void remove_iso(int fd, char *name)
1471 unlink(name);
1472 g_free(name);
1473 close(fd);
1476 static int ahci_cb_cmp_buff(AHCIQState *ahci, AHCICommand *cmd,
1477 const AHCIOpts *opts)
1479 unsigned char *tx = opts->opaque;
1480 unsigned char *rx;
1482 if (!opts->size) {
1483 return 0;
1486 rx = g_malloc0(opts->size);
1487 bufread(opts->buffer, rx, opts->size);
1488 g_assert_cmphex(memcmp(tx, rx, opts->size), ==, 0);
1489 g_free(rx);
1491 return 0;
1494 static void ahci_test_cdrom(int nsectors, bool dma, uint8_t cmd,
1495 bool override_bcl, uint16_t bcl)
1497 AHCIQState *ahci;
1498 unsigned char *tx;
1499 char *iso;
1500 int fd;
1501 AHCIOpts opts = {
1502 .size = (ATAPI_SECTOR_SIZE * nsectors),
1503 .atapi = true,
1504 .atapi_dma = dma,
1505 .post_cb = ahci_cb_cmp_buff,
1506 .set_bcl = override_bcl,
1507 .bcl = bcl,
1509 uint64_t iso_size = ATAPI_SECTOR_SIZE * (nsectors + 1);
1511 /* Prepare ISO and fill 'tx' buffer */
1512 fd = prepare_iso(iso_size, &tx, &iso);
1513 opts.opaque = tx;
1515 /* Standard startup wonkery, but use ide-cd and our special iso file */
1516 ahci = ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,format=raw "
1517 "-M q35 "
1518 "-device ide-cd,drive=drive0 ", iso);
1520 /* Build & Send AHCI command */
1521 ahci_exec(ahci, ahci_port_select(ahci), cmd, &opts);
1523 /* Cleanup */
1524 g_free(tx);
1525 ahci_shutdown(ahci);
1526 remove_iso(fd, iso);
1529 static void ahci_test_cdrom_read10(int nsectors, bool dma)
1531 ahci_test_cdrom(nsectors, dma, CMD_ATAPI_READ_10, false, 0);
1534 static void test_cdrom_dma(void)
1536 ahci_test_cdrom_read10(1, true);
1539 static void test_cdrom_dma_multi(void)
1541 ahci_test_cdrom_read10(3, true);
1544 static void test_cdrom_pio(void)
1546 ahci_test_cdrom_read10(1, false);
1549 static void test_cdrom_pio_multi(void)
1551 ahci_test_cdrom_read10(3, false);
1554 /* Regression test: Test that a READ_CD command with a BCL of 0 but a size of 0
1555 * completes as a NOP instead of erroring out. */
1556 static void test_atapi_bcl(void)
1558 ahci_test_cdrom(0, false, CMD_ATAPI_READ_CD, true, 0);
1562 static void atapi_wait_tray(bool open)
1564 QDict *rsp = qmp_eventwait_ref("DEVICE_TRAY_MOVED");
1565 QDict *data = qdict_get_qdict(rsp, "data");
1566 if (open) {
1567 g_assert(qdict_get_bool(data, "tray-open"));
1568 } else {
1569 g_assert(!qdict_get_bool(data, "tray-open"));
1571 QDECREF(rsp);
1574 static void test_atapi_tray(void)
1576 AHCIQState *ahci;
1577 unsigned char *tx;
1578 char *iso;
1579 int fd;
1580 uint8_t port, sense, asc;
1581 uint64_t iso_size = ATAPI_SECTOR_SIZE;
1582 QDict *rsp;
1584 fd = prepare_iso(iso_size, &tx, &iso);
1585 ahci = ahci_boot_and_enable("-blockdev node-name=drive0,driver=file,filename=%s "
1586 "-M q35 "
1587 "-device ide-cd,id=cd0,drive=drive0 ", iso);
1588 port = ahci_port_select(ahci);
1590 ahci_atapi_eject(ahci, port);
1591 atapi_wait_tray(true);
1593 ahci_atapi_load(ahci, port);
1594 atapi_wait_tray(false);
1596 /* Remove media */
1597 qmp_async("{'execute': 'blockdev-open-tray', "
1598 "'arguments': {'id': 'cd0'}}");
1599 atapi_wait_tray(true);
1600 rsp = qmp_receive();
1601 QDECREF(rsp);
1603 qmp_discard_response("{'execute': 'blockdev-remove-medium', "
1604 "'arguments': {'id': 'cd0'}}");
1606 /* Test the tray without a medium */
1607 ahci_atapi_load(ahci, port);
1608 atapi_wait_tray(false);
1610 ahci_atapi_eject(ahci, port);
1611 atapi_wait_tray(true);
1613 /* Re-insert media */
1614 qmp_discard_response("{'execute': 'blockdev-add', "
1615 "'arguments': {'node-name': 'node0', "
1616 "'driver': 'raw', "
1617 "'file': { 'driver': 'file', "
1618 "'filename': %s }}}", iso);
1619 qmp_discard_response("{'execute': 'blockdev-insert-medium',"
1620 "'arguments': { 'id': 'cd0', "
1621 "'node-name': 'node0' }}");
1623 /* Again, the event shows up first */
1624 qmp_async("{'execute': 'blockdev-close-tray', "
1625 "'arguments': {'id': 'cd0'}}");
1626 atapi_wait_tray(false);
1627 rsp = qmp_receive();
1628 QDECREF(rsp);
1630 /* Now, to convince ATAPI we understand the media has changed... */
1631 ahci_atapi_test_ready(ahci, port, false, SENSE_NOT_READY);
1632 ahci_atapi_get_sense(ahci, port, &sense, &asc);
1633 g_assert_cmpuint(sense, ==, SENSE_NOT_READY);
1634 g_assert_cmpuint(asc, ==, ASC_MEDIUM_NOT_PRESENT);
1636 ahci_atapi_test_ready(ahci, port, false, SENSE_UNIT_ATTENTION);
1637 ahci_atapi_get_sense(ahci, port, &sense, &asc);
1638 g_assert_cmpuint(sense, ==, SENSE_UNIT_ATTENTION);
1639 g_assert_cmpuint(asc, ==, ASC_MEDIUM_MAY_HAVE_CHANGED);
1641 ahci_atapi_test_ready(ahci, port, true, SENSE_NO_SENSE);
1642 ahci_atapi_get_sense(ahci, port, &sense, &asc);
1643 g_assert_cmpuint(sense, ==, SENSE_NO_SENSE);
1645 /* Final tray test. */
1646 ahci_atapi_eject(ahci, port);
1647 atapi_wait_tray(true);
1649 ahci_atapi_load(ahci, port);
1650 atapi_wait_tray(false);
1652 /* Cleanup */
1653 g_free(tx);
1654 ahci_shutdown(ahci);
1655 remove_iso(fd, iso);
1658 /******************************************************************************/
1659 /* AHCI I/O Test Matrix Definitions */
1661 enum BuffLen {
1662 LEN_BEGIN = 0,
1663 LEN_SIMPLE = LEN_BEGIN,
1664 LEN_DOUBLE,
1665 LEN_LONG,
1666 LEN_SHORT,
1667 NUM_LENGTHS
1670 static const char *buff_len_str[NUM_LENGTHS] = { "simple", "double",
1671 "long", "short" };
1673 enum AddrMode {
1674 ADDR_MODE_BEGIN = 0,
1675 ADDR_MODE_LBA28 = ADDR_MODE_BEGIN,
1676 ADDR_MODE_LBA48,
1677 NUM_ADDR_MODES
1680 static const char *addr_mode_str[NUM_ADDR_MODES] = { "lba28", "lba48" };
1682 enum IOMode {
1683 MODE_BEGIN = 0,
1684 MODE_PIO = MODE_BEGIN,
1685 MODE_DMA,
1686 NUM_MODES
1689 static const char *io_mode_str[NUM_MODES] = { "pio", "dma" };
1691 enum IOOps {
1692 IO_BEGIN = 0,
1693 IO_READ = IO_BEGIN,
1694 IO_WRITE,
1695 NUM_IO_OPS
1698 enum OffsetType {
1699 OFFSET_BEGIN = 0,
1700 OFFSET_ZERO = OFFSET_BEGIN,
1701 OFFSET_LOW,
1702 OFFSET_HIGH,
1703 NUM_OFFSETS
1706 static const char *offset_str[NUM_OFFSETS] = { "zero", "low", "high" };
1708 typedef struct AHCIIOTestOptions {
1709 enum BuffLen length;
1710 enum AddrMode address_type;
1711 enum IOMode io_type;
1712 enum OffsetType offset;
1713 } AHCIIOTestOptions;
1715 static uint64_t offset_sector(enum OffsetType ofst,
1716 enum AddrMode addr_type,
1717 uint64_t buffsize)
1719 uint64_t ceil;
1720 uint64_t nsectors;
1722 switch (ofst) {
1723 case OFFSET_ZERO:
1724 return 0;
1725 case OFFSET_LOW:
1726 return 1;
1727 case OFFSET_HIGH:
1728 ceil = (addr_type == ADDR_MODE_LBA28) ? 0xfffffff : 0xffffffffffff;
1729 ceil = MIN(ceil, mb_to_sectors(test_image_size_mb) - 1);
1730 nsectors = buffsize / AHCI_SECTOR_SIZE;
1731 return ceil - nsectors + 1;
1732 default:
1733 g_assert_not_reached();
1738 * Table of possible I/O ATA commands given a set of enumerations.
1740 static const uint8_t io_cmds[NUM_MODES][NUM_ADDR_MODES][NUM_IO_OPS] = {
1741 [MODE_PIO] = {
1742 [ADDR_MODE_LBA28] = {
1743 [IO_READ] = CMD_READ_PIO,
1744 [IO_WRITE] = CMD_WRITE_PIO },
1745 [ADDR_MODE_LBA48] = {
1746 [IO_READ] = CMD_READ_PIO_EXT,
1747 [IO_WRITE] = CMD_WRITE_PIO_EXT }
1749 [MODE_DMA] = {
1750 [ADDR_MODE_LBA28] = {
1751 [IO_READ] = CMD_READ_DMA,
1752 [IO_WRITE] = CMD_WRITE_DMA },
1753 [ADDR_MODE_LBA48] = {
1754 [IO_READ] = CMD_READ_DMA_EXT,
1755 [IO_WRITE] = CMD_WRITE_DMA_EXT }
1760 * Test a Read/Write pattern using various commands, addressing modes,
1761 * transfer modes, and buffer sizes.
1763 static void test_io_rw_interface(enum AddrMode lba48, enum IOMode dma,
1764 unsigned bufsize, uint64_t sector)
1766 AHCIQState *ahci;
1768 ahci = ahci_boot_and_enable(NULL);
1769 ahci_test_io_rw_simple(ahci, bufsize, sector,
1770 io_cmds[dma][lba48][IO_READ],
1771 io_cmds[dma][lba48][IO_WRITE]);
1772 ahci_shutdown(ahci);
1776 * Demultiplex the test data and invoke the actual test routine.
1778 static void test_io_interface(gconstpointer opaque)
1780 AHCIIOTestOptions *opts = (AHCIIOTestOptions *)opaque;
1781 unsigned bufsize;
1782 uint64_t sector;
1784 switch (opts->length) {
1785 case LEN_SIMPLE:
1786 bufsize = 4096;
1787 break;
1788 case LEN_DOUBLE:
1789 bufsize = 8192;
1790 break;
1791 case LEN_LONG:
1792 bufsize = 4096 * 64;
1793 break;
1794 case LEN_SHORT:
1795 bufsize = 512;
1796 break;
1797 default:
1798 g_assert_not_reached();
1801 sector = offset_sector(opts->offset, opts->address_type, bufsize);
1802 test_io_rw_interface(opts->address_type, opts->io_type, bufsize, sector);
1803 g_free(opts);
1804 return;
1807 static void create_ahci_io_test(enum IOMode type, enum AddrMode addr,
1808 enum BuffLen len, enum OffsetType offset)
1810 char *name;
1811 AHCIIOTestOptions *opts;
1813 opts = g_new(AHCIIOTestOptions, 1);
1814 opts->length = len;
1815 opts->address_type = addr;
1816 opts->io_type = type;
1817 opts->offset = offset;
1819 name = g_strdup_printf("ahci/io/%s/%s/%s/%s",
1820 io_mode_str[type],
1821 addr_mode_str[addr],
1822 buff_len_str[len],
1823 offset_str[offset]);
1825 if ((addr == ADDR_MODE_LBA48) && (offset == OFFSET_HIGH) &&
1826 (mb_to_sectors(test_image_size_mb) <= 0xFFFFFFF)) {
1827 g_test_message("%s: skipped; test image too small", name);
1828 g_free(name);
1829 return;
1832 qtest_add_data_func(name, opts, test_io_interface);
1833 g_free(name);
1836 /******************************************************************************/
1838 int main(int argc, char **argv)
1840 const char *arch;
1841 int ret;
1842 int fd;
1843 int c;
1844 int i, j, k, m;
1846 static struct option long_options[] = {
1847 {"pedantic", no_argument, 0, 'p' },
1848 {0, 0, 0, 0},
1851 /* Should be first to utilize g_test functionality, So we can see errors. */
1852 g_test_init(&argc, &argv, NULL);
1854 while (1) {
1855 c = getopt_long(argc, argv, "", long_options, NULL);
1856 if (c == -1) {
1857 break;
1859 switch (c) {
1860 case -1:
1861 break;
1862 case 'p':
1863 ahci_pedantic = 1;
1864 break;
1865 default:
1866 fprintf(stderr, "Unrecognized ahci_test option.\n");
1867 g_assert_not_reached();
1871 /* Check architecture */
1872 arch = qtest_get_arch();
1873 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
1874 g_test_message("Skipping test for non-x86");
1875 return 0;
1878 /* Create a temporary image */
1879 fd = mkstemp(tmp_path);
1880 g_assert(fd >= 0);
1881 if (have_qemu_img()) {
1882 imgfmt = "qcow2";
1883 test_image_size_mb = TEST_IMAGE_SIZE_MB_LARGE;
1884 mkqcow2(tmp_path, TEST_IMAGE_SIZE_MB_LARGE);
1885 } else {
1886 g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
1887 "skipping LBA48 high-sector tests");
1888 imgfmt = "raw";
1889 test_image_size_mb = TEST_IMAGE_SIZE_MB_SMALL;
1890 ret = ftruncate(fd, test_image_size_mb * 1024 * 1024);
1891 g_assert(ret == 0);
1893 close(fd);
1895 /* Create temporary blkdebug instructions */
1896 fd = mkstemp(debug_path);
1897 g_assert(fd >= 0);
1898 close(fd);
1900 /* Reserve a hollow file to use as a socket for migration tests */
1901 fd = mkstemp(mig_socket);
1902 g_assert(fd >= 0);
1903 close(fd);
1905 /* Run the tests */
1906 qtest_add_func("/ahci/sanity", test_sanity);
1907 qtest_add_func("/ahci/pci_spec", test_pci_spec);
1908 qtest_add_func("/ahci/pci_enable", test_pci_enable);
1909 qtest_add_func("/ahci/hba_spec", test_hba_spec);
1910 qtest_add_func("/ahci/hba_enable", test_hba_enable);
1911 qtest_add_func("/ahci/identify", test_identify);
1913 for (i = MODE_BEGIN; i < NUM_MODES; i++) {
1914 for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
1915 for (k = LEN_BEGIN; k < NUM_LENGTHS; k++) {
1916 for (m = OFFSET_BEGIN; m < NUM_OFFSETS; m++) {
1917 create_ahci_io_test(i, j, k, m);
1923 qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented);
1925 qtest_add_func("/ahci/flush/simple", test_flush);
1926 qtest_add_func("/ahci/flush/retry", test_flush_retry);
1927 qtest_add_func("/ahci/flush/migrate", test_flush_migrate);
1929 qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity);
1930 qtest_add_func("/ahci/migrate/dma/simple", test_migrate_dma);
1931 qtest_add_func("/ahci/io/dma/lba28/retry", test_halted_dma);
1932 qtest_add_func("/ahci/migrate/dma/halted", test_migrate_halted_dma);
1934 qtest_add_func("/ahci/max", test_max);
1935 qtest_add_func("/ahci/reset", test_reset);
1937 qtest_add_func("/ahci/io/ncq/simple", test_ncq_simple);
1938 qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq);
1939 qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq);
1940 qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq);
1942 qtest_add_func("/ahci/cdrom/dma/single", test_cdrom_dma);
1943 qtest_add_func("/ahci/cdrom/dma/multi", test_cdrom_dma_multi);
1944 qtest_add_func("/ahci/cdrom/pio/single", test_cdrom_pio);
1945 qtest_add_func("/ahci/cdrom/pio/multi", test_cdrom_pio_multi);
1947 qtest_add_func("/ahci/cdrom/pio/bcl", test_atapi_bcl);
1948 qtest_add_func("/ahci/cdrom/eject", test_atapi_tray);
1950 ret = g_test_run();
1952 /* Cleanup */
1953 unlink(tmp_path);
1954 unlink(debug_path);
1955 unlink(mig_socket);
1957 return ret;