SCSI device: fix to incomplete QOMify
[qemu/kevin.git] / tests / ahci-test.c
blob31fb1f947610cc860c3c2440e005eacdf08d2c43
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 <stdint.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <glib.h>
31 #include "libqtest.h"
32 #include "libqos/libqos-pc.h"
33 #include "libqos/ahci.h"
34 #include "libqos/pci-pc.h"
36 #include "qemu-common.h"
37 #include "qemu/host-utils.h"
39 #include "hw/pci/pci_ids.h"
40 #include "hw/pci/pci_regs.h"
42 /* Test images sizes in MB */
43 #define TEST_IMAGE_SIZE_MB_LARGE (200 * 1024)
44 #define TEST_IMAGE_SIZE_MB_SMALL 64
46 /*** Globals ***/
47 static char tmp_path[] = "/tmp/qtest.XXXXXX";
48 static char debug_path[] = "/tmp/qtest-blkdebug.XXXXXX";
49 static char mig_socket[] = "/tmp/qtest-migration.XXXXXX";
50 static bool ahci_pedantic;
51 static const char *imgfmt;
52 static unsigned test_image_size_mb;
54 /*** Function Declarations ***/
55 static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port);
56 static void ahci_test_pci_spec(AHCIQState *ahci);
57 static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
58 uint8_t offset);
59 static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset);
60 static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset);
61 static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset);
63 /*** Utilities ***/
65 static uint64_t mb_to_sectors(uint64_t image_size_mb)
67 return (image_size_mb * 1024 * 1024) / AHCI_SECTOR_SIZE;
70 static void string_bswap16(uint16_t *s, size_t bytes)
72 g_assert_cmphex((bytes & 1), ==, 0);
73 bytes /= 2;
75 while (bytes--) {
76 *s = bswap16(*s);
77 s++;
81 /**
82 * Verify that the transfer did not corrupt our state at all.
84 static void verify_state(AHCIQState *ahci)
86 int i, j;
87 uint32_t ahci_fingerprint;
88 uint64_t hba_base;
89 uint64_t hba_stored;
90 AHCICommandHeader cmd;
92 ahci_fingerprint = qpci_config_readl(ahci->dev, PCI_VENDOR_ID);
93 g_assert_cmphex(ahci_fingerprint, ==, ahci->fingerprint);
95 /* If we haven't initialized, this is as much as can be validated. */
96 if (!ahci->hba_base) {
97 return;
100 hba_base = (uint64_t)qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
101 hba_stored = (uint64_t)(uintptr_t)ahci->hba_base;
102 g_assert_cmphex(hba_base, ==, hba_stored);
104 g_assert_cmphex(ahci_rreg(ahci, AHCI_CAP), ==, ahci->cap);
105 g_assert_cmphex(ahci_rreg(ahci, AHCI_CAP2), ==, ahci->cap2);
107 for (i = 0; i < 32; i++) {
108 g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_FB), ==,
109 ahci->port[i].fb);
110 g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_CLB), ==,
111 ahci->port[i].clb);
112 for (j = 0; j < 32; j++) {
113 ahci_get_command_header(ahci, i, j, &cmd);
114 g_assert_cmphex(cmd.prdtl, ==, ahci->port[i].prdtl[j]);
115 g_assert_cmphex(cmd.ctba, ==, ahci->port[i].ctba[j]);
120 static void ahci_migrate(AHCIQState *from, AHCIQState *to, const char *uri)
122 QOSState *tmp = to->parent;
123 QPCIDevice *dev = to->dev;
124 char *uri_local = NULL;
126 if (uri == NULL) {
127 uri_local = g_strdup_printf("%s%s", "unix:", mig_socket);
128 uri = uri_local;
131 /* context will be 'to' after completion. */
132 migrate(from->parent, to->parent, uri);
134 /* We'd like for the AHCIState objects to still point
135 * to information specific to its specific parent
136 * instance, but otherwise just inherit the new data. */
137 memcpy(to, from, sizeof(AHCIQState));
138 to->parent = tmp;
139 to->dev = dev;
141 tmp = from->parent;
142 dev = from->dev;
143 memset(from, 0x00, sizeof(AHCIQState));
144 from->parent = tmp;
145 from->dev = dev;
147 verify_state(to);
148 g_free(uri_local);
151 /*** Test Setup & Teardown ***/
154 * Start a Q35 machine and bookmark a handle to the AHCI device.
156 static AHCIQState *ahci_vboot(const char *cli, va_list ap)
158 AHCIQState *s;
160 s = g_malloc0(sizeof(AHCIQState));
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 AHCIQState *ahci_boot(const char *cli, ...)
175 AHCIQState *s;
176 va_list ap;
178 if (cli) {
179 va_start(ap, cli);
180 s = ahci_vboot(cli, ap);
181 va_end(ap);
182 } else {
183 cli = "-drive if=none,id=drive0,file=%s,cache=writeback,serial=%s"
184 ",format=%s"
185 " -M q35 "
186 "-device ide-hd,drive=drive0 "
187 "-global ide-hd.ver=%s";
188 s = ahci_boot(cli, tmp_path, "testdisk", imgfmt, "version");
191 return s;
195 * Clean up the PCI device, then terminate the QEMU instance.
197 static void ahci_shutdown(AHCIQState *ahci)
199 QOSState *qs = ahci->parent;
201 set_context(qs);
202 ahci_clean_mem(ahci);
203 free_ahci_device(ahci->dev);
204 g_free(ahci);
205 qtest_shutdown(qs);
209 * Boot and fully enable the HBA device.
210 * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
212 static AHCIQState *ahci_boot_and_enable(const char *cli, ...)
214 AHCIQState *ahci;
215 va_list ap;
216 uint16_t buff[256];
217 uint8_t port;
218 uint8_t hello;
220 if (cli) {
221 va_start(ap, cli);
222 ahci = ahci_vboot(cli, ap);
223 va_end(ap);
224 } else {
225 ahci = ahci_boot(NULL);
228 ahci_pci_enable(ahci);
229 ahci_hba_enable(ahci);
230 /* Initialize test device */
231 port = ahci_port_select(ahci);
232 ahci_port_clear(ahci, port);
233 if (is_atapi(ahci, port)) {
234 hello = CMD_PACKET_ID;
235 } else {
236 hello = CMD_IDENTIFY;
238 ahci_io(ahci, port, hello, &buff, sizeof(buff), 0);
240 return ahci;
243 /*** Specification Adherence Tests ***/
246 * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
248 static void ahci_test_pci_spec(AHCIQState *ahci)
250 uint8_t datab;
251 uint16_t data;
252 uint32_t datal;
254 /* Most of these bits should start cleared until we turn them on. */
255 data = qpci_config_readw(ahci->dev, PCI_COMMAND);
256 ASSERT_BIT_CLEAR(data, PCI_COMMAND_MEMORY);
257 ASSERT_BIT_CLEAR(data, PCI_COMMAND_MASTER);
258 ASSERT_BIT_CLEAR(data, PCI_COMMAND_SPECIAL); /* Reserved */
259 ASSERT_BIT_CLEAR(data, PCI_COMMAND_VGA_PALETTE); /* Reserved */
260 ASSERT_BIT_CLEAR(data, PCI_COMMAND_PARITY);
261 ASSERT_BIT_CLEAR(data, PCI_COMMAND_WAIT); /* Reserved */
262 ASSERT_BIT_CLEAR(data, PCI_COMMAND_SERR);
263 ASSERT_BIT_CLEAR(data, PCI_COMMAND_FAST_BACK);
264 ASSERT_BIT_CLEAR(data, PCI_COMMAND_INTX_DISABLE);
265 ASSERT_BIT_CLEAR(data, 0xF800); /* Reserved */
267 data = qpci_config_readw(ahci->dev, PCI_STATUS);
268 ASSERT_BIT_CLEAR(data, 0x01 | 0x02 | 0x04); /* Reserved */
269 ASSERT_BIT_CLEAR(data, PCI_STATUS_INTERRUPT);
270 ASSERT_BIT_SET(data, PCI_STATUS_CAP_LIST); /* must be set */
271 ASSERT_BIT_CLEAR(data, PCI_STATUS_UDF); /* Reserved */
272 ASSERT_BIT_CLEAR(data, PCI_STATUS_PARITY);
273 ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_TARGET_ABORT);
274 ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_TARGET_ABORT);
275 ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_MASTER_ABORT);
276 ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_SYSTEM_ERROR);
277 ASSERT_BIT_CLEAR(data, PCI_STATUS_DETECTED_PARITY);
279 /* RID occupies the low byte, CCs occupy the high three. */
280 datal = qpci_config_readl(ahci->dev, PCI_CLASS_REVISION);
281 if (ahci_pedantic) {
282 /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
283 * Though in practice this is likely seldom true. */
284 ASSERT_BIT_CLEAR(datal, 0xFF);
287 /* BCC *must* equal 0x01. */
288 g_assert_cmphex(PCI_BCC(datal), ==, 0x01);
289 if (PCI_SCC(datal) == 0x01) {
290 /* IDE */
291 ASSERT_BIT_SET(0x80000000, datal);
292 ASSERT_BIT_CLEAR(0x60000000, datal);
293 } else if (PCI_SCC(datal) == 0x04) {
294 /* RAID */
295 g_assert_cmphex(PCI_PI(datal), ==, 0);
296 } else if (PCI_SCC(datal) == 0x06) {
297 /* AHCI */
298 g_assert_cmphex(PCI_PI(datal), ==, 0x01);
299 } else {
300 g_assert_not_reached();
303 datab = qpci_config_readb(ahci->dev, PCI_CACHE_LINE_SIZE);
304 g_assert_cmphex(datab, ==, 0);
306 datab = qpci_config_readb(ahci->dev, PCI_LATENCY_TIMER);
307 g_assert_cmphex(datab, ==, 0);
309 /* Only the bottom 7 bits must be off. */
310 datab = qpci_config_readb(ahci->dev, PCI_HEADER_TYPE);
311 ASSERT_BIT_CLEAR(datab, 0x7F);
313 /* BIST is optional, but the low 7 bits must always start off regardless. */
314 datab = qpci_config_readb(ahci->dev, PCI_BIST);
315 ASSERT_BIT_CLEAR(datab, 0x7F);
317 /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
318 datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
319 g_assert_cmphex(datal, ==, 0);
321 qpci_config_writel(ahci->dev, PCI_BASE_ADDRESS_5, 0xFFFFFFFF);
322 datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
323 /* ABAR must be 32-bit, memory mapped, non-prefetchable and
324 * must be >= 512 bytes. To that end, bits 0-8 must be off. */
325 ASSERT_BIT_CLEAR(datal, 0xFF);
327 /* Capability list MUST be present, */
328 datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST);
329 /* But these bits are reserved. */
330 ASSERT_BIT_CLEAR(datal, ~0xFF);
331 g_assert_cmphex(datal, !=, 0);
333 /* Check specification adherence for capability extenstions. */
334 data = qpci_config_readw(ahci->dev, datal);
336 switch (ahci->fingerprint) {
337 case AHCI_INTEL_ICH9:
338 /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
339 g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_MSI);
340 break;
341 default:
342 /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
343 g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_PM);
346 ahci_test_pci_caps(ahci, data, (uint8_t)datal);
348 /* Reserved. */
349 datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST + 4);
350 g_assert_cmphex(datal, ==, 0);
352 /* IPIN might vary, but ILINE must be off. */
353 datab = qpci_config_readb(ahci->dev, PCI_INTERRUPT_LINE);
354 g_assert_cmphex(datab, ==, 0);
358 * Test PCI capabilities for AHCI specification adherence.
360 static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
361 uint8_t offset)
363 uint8_t cid = header & 0xFF;
364 uint8_t next = header >> 8;
366 g_test_message("CID: %02x; next: %02x", cid, next);
368 switch (cid) {
369 case PCI_CAP_ID_PM:
370 ahci_test_pmcap(ahci, offset);
371 break;
372 case PCI_CAP_ID_MSI:
373 ahci_test_msicap(ahci, offset);
374 break;
375 case PCI_CAP_ID_SATA:
376 ahci_test_satacap(ahci, offset);
377 break;
379 default:
380 g_test_message("Unknown CAP 0x%02x", cid);
383 if (next) {
384 ahci_test_pci_caps(ahci, qpci_config_readw(ahci->dev, next), next);
389 * Test SATA PCI capabilitity for AHCI specification adherence.
391 static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset)
393 uint16_t dataw;
394 uint32_t datal;
396 g_test_message("Verifying SATACAP");
398 /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
399 dataw = qpci_config_readw(ahci->dev, offset + 2);
400 g_assert_cmphex(dataw, ==, 0x10);
402 /* Grab the SATACR1 register. */
403 datal = qpci_config_readw(ahci->dev, offset + 4);
405 switch (datal & 0x0F) {
406 case 0x04: /* BAR0 */
407 case 0x05: /* BAR1 */
408 case 0x06:
409 case 0x07:
410 case 0x08:
411 case 0x09: /* BAR5 */
412 case 0x0F: /* Immediately following SATACR1 in PCI config space. */
413 break;
414 default:
415 /* Invalid BARLOC for the Index Data Pair. */
416 g_assert_not_reached();
419 /* Reserved. */
420 g_assert_cmphex((datal >> 24), ==, 0x00);
424 * Test MSI PCI capability for AHCI specification adherence.
426 static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset)
428 uint16_t dataw;
429 uint32_t datal;
431 g_test_message("Verifying MSICAP");
433 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_FLAGS);
434 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_ENABLE);
435 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_QSIZE);
436 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_RESERVED);
438 datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_LO);
439 g_assert_cmphex(datal, ==, 0);
441 if (dataw & PCI_MSI_FLAGS_64BIT) {
442 g_test_message("MSICAP is 64bit");
443 datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_HI);
444 g_assert_cmphex(datal, ==, 0);
445 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_64);
446 g_assert_cmphex(dataw, ==, 0);
447 } else {
448 g_test_message("MSICAP is 32bit");
449 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_32);
450 g_assert_cmphex(dataw, ==, 0);
455 * Test Power Management PCI capability for AHCI specification adherence.
457 static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset)
459 uint16_t dataw;
461 g_test_message("Verifying PMCAP");
463 dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_PMC);
464 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_PME_CLOCK);
465 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_RESERVED);
466 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D1);
467 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D2);
469 dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_CTRL);
470 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_STATE_MASK);
471 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_RESERVED);
472 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SEL_MASK);
473 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SCALE_MASK);
476 static void ahci_test_hba_spec(AHCIQState *ahci)
478 unsigned i;
479 uint32_t reg;
480 uint32_t ports;
481 uint8_t nports_impl;
482 uint8_t maxports;
484 g_assert(ahci != NULL);
487 * Note that the AHCI spec does expect the BIOS to set up a few things:
488 * CAP.SSS - Support for staggered spin-up (t/f)
489 * CAP.SMPS - Support for mechanical presence switches (t/f)
490 * PI - Ports Implemented (1-32)
491 * PxCMD.HPCP - Hot Plug Capable Port
492 * PxCMD.MPSP - Mechanical Presence Switch Present
493 * PxCMD.CPD - Cold Presence Detection support
495 * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
496 * Foreach Port Implemented:
497 * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
498 * -PxCLB/U and PxFB/U are set to valid regions in memory
499 * -PxSUD is set to 1.
500 * -PxSSTS.DET is polled for presence; if detected, we continue:
501 * -PxSERR is cleared with 1's.
502 * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
503 * the device is ready.
506 /* 1 CAP - Capabilities Register */
507 ahci->cap = ahci_rreg(ahci, AHCI_CAP);
508 ASSERT_BIT_CLEAR(ahci->cap, AHCI_CAP_RESERVED);
510 /* 2 GHC - Global Host Control */
511 reg = ahci_rreg(ahci, AHCI_GHC);
512 ASSERT_BIT_CLEAR(reg, AHCI_GHC_HR);
513 ASSERT_BIT_CLEAR(reg, AHCI_GHC_IE);
514 ASSERT_BIT_CLEAR(reg, AHCI_GHC_MRSM);
515 if (BITSET(ahci->cap, AHCI_CAP_SAM)) {
516 g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
517 ASSERT_BIT_SET(reg, AHCI_GHC_AE);
518 } else {
519 g_test_message("Supports AHCI/Legacy mix.");
520 ASSERT_BIT_CLEAR(reg, AHCI_GHC_AE);
523 /* 3 IS - Interrupt Status */
524 reg = ahci_rreg(ahci, AHCI_IS);
525 g_assert_cmphex(reg, ==, 0);
527 /* 4 PI - Ports Implemented */
528 ports = ahci_rreg(ahci, AHCI_PI);
529 /* Ports Implemented must be non-zero. */
530 g_assert_cmphex(ports, !=, 0);
531 /* Ports Implemented must be <= Number of Ports. */
532 nports_impl = ctpopl(ports);
533 g_assert_cmpuint(((AHCI_CAP_NP & ahci->cap) + 1), >=, nports_impl);
535 /* Ports must be within the proper range. Given a mapping of SIZE,
536 * 256 bytes are used for global HBA control, and the rest is used
537 * for ports data, at 0x80 bytes each. */
538 g_assert_cmphex(ahci->barsize, >, 0);
539 maxports = (ahci->barsize - HBA_DATA_REGION_SIZE) / HBA_PORT_DATA_SIZE;
540 /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
541 g_assert_cmphex((reg >> maxports), ==, 0);
543 /* 5 AHCI Version */
544 reg = ahci_rreg(ahci, AHCI_VS);
545 switch (reg) {
546 case AHCI_VERSION_0_95:
547 case AHCI_VERSION_1_0:
548 case AHCI_VERSION_1_1:
549 case AHCI_VERSION_1_2:
550 case AHCI_VERSION_1_3:
551 break;
552 default:
553 g_assert_not_reached();
556 /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
557 reg = ahci_rreg(ahci, AHCI_CCCCTL);
558 if (BITSET(ahci->cap, AHCI_CAP_CCCS)) {
559 ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_EN);
560 ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_RESERVED);
561 ASSERT_BIT_SET(reg, AHCI_CCCCTL_CC);
562 ASSERT_BIT_SET(reg, AHCI_CCCCTL_TV);
563 } else {
564 g_assert_cmphex(reg, ==, 0);
567 /* 7 CCC_PORTS */
568 reg = ahci_rreg(ahci, AHCI_CCCPORTS);
569 /* Must be zeroes initially regardless of CAP.CCCS */
570 g_assert_cmphex(reg, ==, 0);
572 /* 8 EM_LOC */
573 reg = ahci_rreg(ahci, AHCI_EMLOC);
574 if (BITCLR(ahci->cap, AHCI_CAP_EMS)) {
575 g_assert_cmphex(reg, ==, 0);
578 /* 9 EM_CTL */
579 reg = ahci_rreg(ahci, AHCI_EMCTL);
580 if (BITSET(ahci->cap, AHCI_CAP_EMS)) {
581 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_STSMR);
582 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLTM);
583 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLRST);
584 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_RESERVED);
585 } else {
586 g_assert_cmphex(reg, ==, 0);
589 /* 10 CAP2 -- Capabilities Extended */
590 ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
591 ASSERT_BIT_CLEAR(ahci->cap2, AHCI_CAP2_RESERVED);
593 /* 11 BOHC -- Bios/OS Handoff Control */
594 reg = ahci_rreg(ahci, AHCI_BOHC);
595 g_assert_cmphex(reg, ==, 0);
597 /* 12 -- 23: Reserved */
598 g_test_message("Verifying HBA reserved area is empty.");
599 for (i = AHCI_RESERVED; i < AHCI_NVMHCI; ++i) {
600 reg = ahci_rreg(ahci, i);
601 g_assert_cmphex(reg, ==, 0);
604 /* 24 -- 39: NVMHCI */
605 if (BITCLR(ahci->cap2, AHCI_CAP2_NVMP)) {
606 g_test_message("Verifying HBA/NVMHCI area is empty.");
607 for (i = AHCI_NVMHCI; i < AHCI_VENDOR; ++i) {
608 reg = ahci_rreg(ahci, i);
609 g_assert_cmphex(reg, ==, 0);
613 /* 40 -- 63: Vendor */
614 g_test_message("Verifying HBA/Vendor area is empty.");
615 for (i = AHCI_VENDOR; i < AHCI_PORTS; ++i) {
616 reg = ahci_rreg(ahci, i);
617 g_assert_cmphex(reg, ==, 0);
620 /* 64 -- XX: Port Space */
621 for (i = 0; ports || (i < maxports); ports >>= 1, ++i) {
622 if (BITSET(ports, 0x1)) {
623 g_test_message("Testing port %u for spec", i);
624 ahci_test_port_spec(ahci, i);
625 } else {
626 uint16_t j;
627 uint16_t low = AHCI_PORTS + (32 * i);
628 uint16_t high = AHCI_PORTS + (32 * (i + 1));
629 g_test_message("Asserting unimplemented port %u "
630 "(reg [%u-%u]) is empty.",
631 i, low, high - 1);
632 for (j = low; j < high; ++j) {
633 reg = ahci_rreg(ahci, j);
634 g_assert_cmphex(reg, ==, 0);
641 * Test the memory space for one port for specification adherence.
643 static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
645 uint32_t reg;
646 unsigned i;
648 /* (0) CLB */
649 reg = ahci_px_rreg(ahci, port, AHCI_PX_CLB);
650 ASSERT_BIT_CLEAR(reg, AHCI_PX_CLB_RESERVED);
652 /* (1) CLBU */
653 if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
654 reg = ahci_px_rreg(ahci, port, AHCI_PX_CLBU);
655 g_assert_cmphex(reg, ==, 0);
658 /* (2) FB */
659 reg = ahci_px_rreg(ahci, port, AHCI_PX_FB);
660 ASSERT_BIT_CLEAR(reg, AHCI_PX_FB_RESERVED);
662 /* (3) FBU */
663 if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
664 reg = ahci_px_rreg(ahci, port, AHCI_PX_FBU);
665 g_assert_cmphex(reg, ==, 0);
668 /* (4) IS */
669 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
670 g_assert_cmphex(reg, ==, 0);
672 /* (5) IE */
673 reg = ahci_px_rreg(ahci, port, AHCI_PX_IE);
674 g_assert_cmphex(reg, ==, 0);
676 /* (6) CMD */
677 reg = ahci_px_rreg(ahci, port, AHCI_PX_CMD);
678 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FRE);
679 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_RESERVED);
680 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CCS);
681 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
682 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
683 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_PMA); /* And RW only if CAP.SPM */
684 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_APSTE); /* RW only if CAP2.APST */
685 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ATAPI);
686 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_DLAE);
687 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ALPE); /* RW only if CAP.SALP */
688 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ASP); /* RW only if CAP.SALP */
689 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ICC);
690 /* If CPDetect support does not exist, CPState must be off. */
691 if (BITCLR(reg, AHCI_PX_CMD_CPD)) {
692 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CPS);
694 /* If MPSPresence is not set, MPSState must be off. */
695 if (BITCLR(reg, AHCI_PX_CMD_MPSP)) {
696 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
698 /* If we do not support MPS, MPSS and MPSP must be off. */
699 if (BITCLR(ahci->cap, AHCI_CAP_SMPS)) {
700 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
701 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSP);
703 /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
704 if (BITANY(reg, AHCI_PX_CMD_CPD | AHCI_PX_CMD_MPSP)) {
705 ASSERT_BIT_SET(reg, AHCI_PX_CMD_HPCP);
707 /* HPCP and ESP cannot both be active. */
708 g_assert(!BITSET(reg, AHCI_PX_CMD_HPCP | AHCI_PX_CMD_ESP));
709 /* If CAP.FBSS is not set, FBSCP must not be set. */
710 if (BITCLR(ahci->cap, AHCI_CAP_FBSS)) {
711 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FBSCP);
714 /* (7) RESERVED */
715 reg = ahci_px_rreg(ahci, port, AHCI_PX_RES1);
716 g_assert_cmphex(reg, ==, 0);
718 /* (8) TFD */
719 reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
720 /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
721 * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
722 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_ERR);
723 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS1);
724 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_DRQ);
725 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS2);
726 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
727 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
728 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_RESERVED);
730 /* (9) SIG */
731 /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
732 * Even when GHC.ST is zero, the AHCI HBA may receive the initial
733 * D2H register FIS and update the signature asynchronously,
734 * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
736 /* (10) SSTS / SCR0: SStatus */
737 reg = ahci_px_rreg(ahci, port, AHCI_PX_SSTS);
738 ASSERT_BIT_CLEAR(reg, AHCI_PX_SSTS_RESERVED);
739 /* Even though the register should be 0 at boot, it is asynchronous and
740 * prone to change, so we cannot test any well known value. */
742 /* (11) SCTL / SCR2: SControl */
743 reg = ahci_px_rreg(ahci, port, AHCI_PX_SCTL);
744 g_assert_cmphex(reg, ==, 0);
746 /* (12) SERR / SCR1: SError */
747 reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
748 g_assert_cmphex(reg, ==, 0);
750 /* (13) SACT / SCR3: SActive */
751 reg = ahci_px_rreg(ahci, port, AHCI_PX_SACT);
752 g_assert_cmphex(reg, ==, 0);
754 /* (14) CI */
755 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
756 g_assert_cmphex(reg, ==, 0);
758 /* (15) SNTF */
759 reg = ahci_px_rreg(ahci, port, AHCI_PX_SNTF);
760 g_assert_cmphex(reg, ==, 0);
762 /* (16) FBS */
763 reg = ahci_px_rreg(ahci, port, AHCI_PX_FBS);
764 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_EN);
765 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEC);
766 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_SDE);
767 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEV);
768 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DWE);
769 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_RESERVED);
770 if (BITSET(ahci->cap, AHCI_CAP_FBSS)) {
771 /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
772 g_assert((reg & AHCI_PX_FBS_ADO) >> ctzl(AHCI_PX_FBS_ADO) >= 2);
775 /* [17 -- 27] RESERVED */
776 for (i = AHCI_PX_RES2; i < AHCI_PX_VS; ++i) {
777 reg = ahci_px_rreg(ahci, port, i);
778 g_assert_cmphex(reg, ==, 0);
781 /* [28 -- 31] Vendor-Specific */
782 for (i = AHCI_PX_VS; i < 32; ++i) {
783 reg = ahci_px_rreg(ahci, port, i);
784 if (reg) {
785 g_test_message("INFO: Vendor register %u non-empty", i);
791 * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
792 * device we see, then read and check the response.
794 static void ahci_test_identify(AHCIQState *ahci)
796 uint16_t buff[256];
797 unsigned px;
798 int rc;
799 uint16_t sect_size;
800 const size_t buffsize = 512;
802 g_assert(ahci != NULL);
805 * This serves as a bit of a tutorial on AHCI device programming:
807 * (1) Create a data buffer for the IDENTIFY response to be sent to
808 * (2) Create a Command Table buffer, where we will store the
809 * command and PRDT (Physical Region Descriptor Table)
810 * (3) Construct an FIS host-to-device command structure, and write it to
811 * the top of the Command Table buffer.
812 * (4) Create one or more Physical Region Descriptors (PRDs) that describe
813 * a location in memory where data may be stored/retrieved.
814 * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
815 * (6) Each AHCI port has up to 32 command slots. Each slot contains a
816 * header that points to a Command Table buffer. Pick an unused slot
817 * and update it to point to the Command Table we have built.
818 * (7) Now: Command #n points to our Command Table, and our Command Table
819 * contains the FIS (that describes our command) and the PRDTL, which
820 * describes our buffer.
821 * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
822 * #n is ready for processing.
825 /* Pick the first implemented and running port */
826 px = ahci_port_select(ahci);
827 g_test_message("Selected port %u for test", px);
829 /* Clear out the FIS Receive area and any pending interrupts. */
830 ahci_port_clear(ahci, px);
832 /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
833 ahci_io(ahci, px, CMD_IDENTIFY, &buff, buffsize, 0);
835 /* Check serial number/version in the buffer */
836 /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
837 * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
838 * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
839 * as a consequence, only needs to unchunk the data on LE machines. */
840 string_bswap16(&buff[10], 20);
841 rc = memcmp(&buff[10], "testdisk ", 20);
842 g_assert_cmphex(rc, ==, 0);
844 string_bswap16(&buff[23], 8);
845 rc = memcmp(&buff[23], "version ", 8);
846 g_assert_cmphex(rc, ==, 0);
848 sect_size = le16_to_cpu(*((uint16_t *)(&buff[5])));
849 g_assert_cmphex(sect_size, ==, AHCI_SECTOR_SIZE);
852 static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,
853 uint64_t sector, uint8_t read_cmd,
854 uint8_t write_cmd)
856 uint64_t ptr;
857 uint8_t port;
858 unsigned char *tx = g_malloc(bufsize);
859 unsigned char *rx = g_malloc0(bufsize);
861 g_assert(ahci != NULL);
863 /* Pick the first running port and clear it. */
864 port = ahci_port_select(ahci);
865 ahci_port_clear(ahci, port);
867 /*** Create pattern and transfer to guest ***/
868 /* Data buffer in the guest */
869 ptr = ahci_alloc(ahci, bufsize);
870 g_assert(ptr);
872 /* Write some indicative pattern to our buffer. */
873 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
874 bufwrite(ptr, tx, bufsize);
876 /* Write this buffer to disk, then read it back to the DMA buffer. */
877 ahci_guest_io(ahci, port, write_cmd, ptr, bufsize, sector);
878 qmemset(ptr, 0x00, bufsize);
879 ahci_guest_io(ahci, port, read_cmd, ptr, bufsize, sector);
881 /*** Read back the Data ***/
882 bufread(ptr, rx, bufsize);
883 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
885 ahci_free(ahci, ptr);
886 g_free(tx);
887 g_free(rx);
890 static uint8_t ahci_test_nondata(AHCIQState *ahci, uint8_t ide_cmd)
892 uint8_t port;
894 /* Sanitize */
895 port = ahci_port_select(ahci);
896 ahci_port_clear(ahci, port);
898 ahci_io(ahci, port, ide_cmd, NULL, 0, 0);
900 return port;
903 static void ahci_test_flush(AHCIQState *ahci)
905 ahci_test_nondata(ahci, CMD_FLUSH_CACHE);
908 static void ahci_test_max(AHCIQState *ahci)
910 RegD2HFIS *d2h = g_malloc0(0x20);
911 uint64_t nsect;
912 uint8_t port;
913 uint8_t cmd;
914 uint64_t config_sect = mb_to_sectors(test_image_size_mb) - 1;
916 if (config_sect > 0xFFFFFF) {
917 cmd = CMD_READ_MAX_EXT;
918 } else {
919 cmd = CMD_READ_MAX;
922 port = ahci_test_nondata(ahci, cmd);
923 memread(ahci->port[port].fb + 0x40, d2h, 0x20);
924 nsect = (uint64_t)d2h->lba_hi[2] << 40 |
925 (uint64_t)d2h->lba_hi[1] << 32 |
926 (uint64_t)d2h->lba_hi[0] << 24 |
927 (uint64_t)d2h->lba_lo[2] << 16 |
928 (uint64_t)d2h->lba_lo[1] << 8 |
929 (uint64_t)d2h->lba_lo[0];
931 g_assert_cmphex(nsect, ==, config_sect);
932 g_free(d2h);
936 /******************************************************************************/
937 /* Test Interfaces */
938 /******************************************************************************/
941 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
943 static void test_sanity(void)
945 AHCIQState *ahci;
946 ahci = ahci_boot(NULL);
947 ahci_shutdown(ahci);
951 * Ensure that the PCI configuration space for the AHCI device is in-line with
952 * the AHCI 1.3 specification for initial values.
954 static void test_pci_spec(void)
956 AHCIQState *ahci;
957 ahci = ahci_boot(NULL);
958 ahci_test_pci_spec(ahci);
959 ahci_shutdown(ahci);
963 * Engage the PCI AHCI device and sanity check the response.
964 * Perform additional PCI config space bringup for the HBA.
966 static void test_pci_enable(void)
968 AHCIQState *ahci;
969 ahci = ahci_boot(NULL);
970 ahci_pci_enable(ahci);
971 ahci_shutdown(ahci);
975 * Investigate the memory mapped regions of the HBA,
976 * and test them for AHCI specification adherence.
978 static void test_hba_spec(void)
980 AHCIQState *ahci;
982 ahci = ahci_boot(NULL);
983 ahci_pci_enable(ahci);
984 ahci_test_hba_spec(ahci);
985 ahci_shutdown(ahci);
989 * Engage the HBA functionality of the AHCI PCI device,
990 * and bring it into a functional idle state.
992 static void test_hba_enable(void)
994 AHCIQState *ahci;
996 ahci = ahci_boot(NULL);
997 ahci_pci_enable(ahci);
998 ahci_hba_enable(ahci);
999 ahci_shutdown(ahci);
1003 * Bring up the device and issue an IDENTIFY command.
1004 * Inspect the state of the HBA device and the data returned.
1006 static void test_identify(void)
1008 AHCIQState *ahci;
1010 ahci = ahci_boot_and_enable(NULL);
1011 ahci_test_identify(ahci);
1012 ahci_shutdown(ahci);
1016 * Fragmented DMA test: Perform a standard 4K DMA read/write
1017 * test, but make sure the physical regions are fragmented to
1018 * be very small, each just 32 bytes, to see how AHCI performs
1019 * with chunks defined to be much less than a sector.
1021 static void test_dma_fragmented(void)
1023 AHCIQState *ahci;
1024 AHCICommand *cmd;
1025 uint8_t px;
1026 size_t bufsize = 4096;
1027 unsigned char *tx = g_malloc(bufsize);
1028 unsigned char *rx = g_malloc0(bufsize);
1029 uint64_t ptr;
1031 ahci = ahci_boot_and_enable(NULL);
1032 px = ahci_port_select(ahci);
1033 ahci_port_clear(ahci, px);
1035 /* create pattern */
1036 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1038 /* Create a DMA buffer in guest memory, and write our pattern to it. */
1039 ptr = guest_alloc(ahci->parent->alloc, bufsize);
1040 g_assert(ptr);
1041 bufwrite(ptr, tx, bufsize);
1043 cmd = ahci_command_create(CMD_WRITE_DMA);
1044 ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
1045 ahci_command_commit(ahci, cmd, px);
1046 ahci_command_issue(ahci, cmd);
1047 ahci_command_verify(ahci, cmd);
1048 ahci_command_free(cmd);
1050 cmd = ahci_command_create(CMD_READ_DMA);
1051 ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
1052 ahci_command_commit(ahci, cmd, px);
1053 ahci_command_issue(ahci, cmd);
1054 ahci_command_verify(ahci, cmd);
1055 ahci_command_free(cmd);
1057 /* Read back the guest's receive buffer into local memory */
1058 bufread(ptr, rx, bufsize);
1059 guest_free(ahci->parent->alloc, ptr);
1061 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1063 ahci_shutdown(ahci);
1065 g_free(rx);
1066 g_free(tx);
1069 static void test_flush(void)
1071 AHCIQState *ahci;
1073 ahci = ahci_boot_and_enable(NULL);
1074 ahci_test_flush(ahci);
1075 ahci_shutdown(ahci);
1078 static void test_flush_retry(void)
1080 AHCIQState *ahci;
1081 AHCICommand *cmd;
1082 uint8_t port;
1084 prepare_blkdebug_script(debug_path, "flush_to_disk");
1085 ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1086 "format=%s,cache=writeback,"
1087 "rerror=stop,werror=stop "
1088 "-M q35 "
1089 "-device ide-hd,drive=drive0 ",
1090 debug_path,
1091 tmp_path, imgfmt);
1093 /* Issue Flush Command and wait for error */
1094 port = ahci_port_select(ahci);
1095 ahci_port_clear(ahci, port);
1097 cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
1098 ahci_guest_io_resume(ahci, cmd);
1100 ahci_shutdown(ahci);
1104 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
1106 static void test_migrate_sanity(void)
1108 AHCIQState *src, *dst;
1109 char *uri = g_strdup_printf("unix:%s", mig_socket);
1111 src = ahci_boot("-m 1024 -M q35 "
1112 "-drive if=ide,file=%s,format=%s ", tmp_path, imgfmt);
1113 dst = ahci_boot("-m 1024 -M q35 "
1114 "-drive if=ide,file=%s,format=%s "
1115 "-incoming %s", tmp_path, imgfmt, uri);
1117 ahci_migrate(src, dst, uri);
1119 ahci_shutdown(src);
1120 ahci_shutdown(dst);
1121 g_free(uri);
1125 * Simple migration test: Write a pattern, migrate, then read.
1127 static void ahci_migrate_simple(uint8_t cmd_read, uint8_t cmd_write)
1129 AHCIQState *src, *dst;
1130 uint8_t px;
1131 size_t bufsize = 4096;
1132 unsigned char *tx = g_malloc(bufsize);
1133 unsigned char *rx = g_malloc0(bufsize);
1134 char *uri = g_strdup_printf("unix:%s", mig_socket);
1136 src = ahci_boot_and_enable("-m 1024 -M q35 "
1137 "-drive if=ide,format=%s,file=%s ",
1138 imgfmt, tmp_path);
1139 dst = ahci_boot("-m 1024 -M q35 "
1140 "-drive if=ide,format=%s,file=%s "
1141 "-incoming %s", imgfmt, tmp_path, uri);
1143 set_context(src->parent);
1145 /* initialize */
1146 px = ahci_port_select(src);
1147 ahci_port_clear(src, px);
1149 /* create pattern */
1150 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1152 /* Write, migrate, then read. */
1153 ahci_io(src, px, cmd_write, tx, bufsize, 0);
1154 ahci_migrate(src, dst, uri);
1155 ahci_io(dst, px, cmd_read, rx, bufsize, 0);
1157 /* Verify pattern */
1158 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1160 ahci_shutdown(src);
1161 ahci_shutdown(dst);
1162 g_free(rx);
1163 g_free(tx);
1164 g_free(uri);
1167 static void test_migrate_dma(void)
1169 ahci_migrate_simple(CMD_READ_DMA, CMD_WRITE_DMA);
1172 static void test_migrate_ncq(void)
1174 ahci_migrate_simple(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
1178 * Halted IO Error Test
1180 * Simulate an error on first write, Try to write a pattern,
1181 * Confirm the VM has stopped, resume the VM, verify command
1182 * has completed, then read back the data and verify.
1184 static void ahci_halted_io_test(uint8_t cmd_read, uint8_t cmd_write)
1186 AHCIQState *ahci;
1187 uint8_t port;
1188 size_t bufsize = 4096;
1189 unsigned char *tx = g_malloc(bufsize);
1190 unsigned char *rx = g_malloc0(bufsize);
1191 uint64_t ptr;
1192 AHCICommand *cmd;
1194 prepare_blkdebug_script(debug_path, "write_aio");
1196 ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1197 "format=%s,cache=writeback,"
1198 "rerror=stop,werror=stop "
1199 "-M q35 "
1200 "-device ide-hd,drive=drive0 ",
1201 debug_path,
1202 tmp_path, imgfmt);
1204 /* Initialize and prepare */
1205 port = ahci_port_select(ahci);
1206 ahci_port_clear(ahci, port);
1208 /* create DMA source buffer and write pattern */
1209 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1210 ptr = ahci_alloc(ahci, bufsize);
1211 g_assert(ptr);
1212 memwrite(ptr, tx, bufsize);
1214 /* Attempt to write (and fail) */
1215 cmd = ahci_guest_io_halt(ahci, port, cmd_write,
1216 ptr, bufsize, 0);
1218 /* Attempt to resume the command */
1219 ahci_guest_io_resume(ahci, cmd);
1220 ahci_free(ahci, ptr);
1222 /* Read back and verify */
1223 ahci_io(ahci, port, cmd_read, rx, bufsize, 0);
1224 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1226 /* Cleanup and go home */
1227 ahci_shutdown(ahci);
1228 g_free(rx);
1229 g_free(tx);
1232 static void test_halted_dma(void)
1234 ahci_halted_io_test(CMD_READ_DMA, CMD_WRITE_DMA);
1237 static void test_halted_ncq(void)
1239 ahci_halted_io_test(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
1243 * IO Error Migration Test
1245 * Simulate an error on first write, Try to write a pattern,
1246 * Confirm the VM has stopped, migrate, resume the VM,
1247 * verify command has completed, then read back the data and verify.
1249 static void ahci_migrate_halted_io(uint8_t cmd_read, uint8_t cmd_write)
1251 AHCIQState *src, *dst;
1252 uint8_t port;
1253 size_t bufsize = 4096;
1254 unsigned char *tx = g_malloc(bufsize);
1255 unsigned char *rx = g_malloc0(bufsize);
1256 uint64_t ptr;
1257 AHCICommand *cmd;
1258 char *uri = g_strdup_printf("unix:%s", mig_socket);
1260 prepare_blkdebug_script(debug_path, "write_aio");
1262 src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1263 "format=%s,cache=writeback,"
1264 "rerror=stop,werror=stop "
1265 "-M q35 "
1266 "-device ide-hd,drive=drive0 ",
1267 debug_path,
1268 tmp_path, imgfmt);
1270 dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
1271 "format=%s,cache=writeback,"
1272 "rerror=stop,werror=stop "
1273 "-M q35 "
1274 "-device ide-hd,drive=drive0 "
1275 "-incoming %s",
1276 tmp_path, imgfmt, uri);
1278 set_context(src->parent);
1280 /* Initialize and prepare */
1281 port = ahci_port_select(src);
1282 ahci_port_clear(src, port);
1283 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1285 /* create DMA source buffer and write pattern */
1286 ptr = ahci_alloc(src, bufsize);
1287 g_assert(ptr);
1288 memwrite(ptr, tx, bufsize);
1290 /* Write, trigger the VM to stop, migrate, then resume. */
1291 cmd = ahci_guest_io_halt(src, port, cmd_write,
1292 ptr, bufsize, 0);
1293 ahci_migrate(src, dst, uri);
1294 ahci_guest_io_resume(dst, cmd);
1295 ahci_free(dst, ptr);
1297 /* Read back */
1298 ahci_io(dst, port, cmd_read, rx, bufsize, 0);
1300 /* Verify TX and RX are identical */
1301 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1303 /* Cleanup and go home. */
1304 ahci_shutdown(src);
1305 ahci_shutdown(dst);
1306 g_free(rx);
1307 g_free(tx);
1308 g_free(uri);
1311 static void test_migrate_halted_dma(void)
1313 ahci_migrate_halted_io(CMD_READ_DMA, CMD_WRITE_DMA);
1316 static void test_migrate_halted_ncq(void)
1318 ahci_migrate_halted_io(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
1322 * Migration test: Try to flush, migrate, then resume.
1324 static void test_flush_migrate(void)
1326 AHCIQState *src, *dst;
1327 AHCICommand *cmd;
1328 uint8_t px;
1329 const char *s;
1330 char *uri = g_strdup_printf("unix:%s", mig_socket);
1332 prepare_blkdebug_script(debug_path, "flush_to_disk");
1334 src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1335 "cache=writeback,rerror=stop,werror=stop,"
1336 "format=%s "
1337 "-M q35 "
1338 "-device ide-hd,drive=drive0 ",
1339 debug_path, tmp_path, imgfmt);
1340 dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
1341 "cache=writeback,rerror=stop,werror=stop,"
1342 "format=%s "
1343 "-M q35 "
1344 "-device ide-hd,drive=drive0 "
1345 "-incoming %s", tmp_path, imgfmt, uri);
1347 set_context(src->parent);
1349 /* Issue Flush Command */
1350 px = ahci_port_select(src);
1351 ahci_port_clear(src, px);
1352 cmd = ahci_command_create(CMD_FLUSH_CACHE);
1353 ahci_command_commit(src, cmd, px);
1354 ahci_command_issue_async(src, cmd);
1355 qmp_eventwait("STOP");
1357 /* Migrate over */
1358 ahci_migrate(src, dst, uri);
1360 /* Complete the command */
1361 s = "{'execute':'cont' }";
1362 qmp_async(s);
1363 qmp_eventwait("RESUME");
1364 ahci_command_wait(dst, cmd);
1365 ahci_command_verify(dst, cmd);
1367 ahci_command_free(cmd);
1368 ahci_shutdown(src);
1369 ahci_shutdown(dst);
1370 g_free(uri);
1373 static void test_max(void)
1375 AHCIQState *ahci;
1377 ahci = ahci_boot_and_enable(NULL);
1378 ahci_test_max(ahci);
1379 ahci_shutdown(ahci);
1382 static void test_reset(void)
1384 AHCIQState *ahci;
1385 int i;
1387 ahci = ahci_boot(NULL);
1388 ahci_test_pci_spec(ahci);
1389 ahci_pci_enable(ahci);
1391 for (i = 0; i < 2; i++) {
1392 ahci_test_hba_spec(ahci);
1393 ahci_hba_enable(ahci);
1394 ahci_test_identify(ahci);
1395 ahci_test_io_rw_simple(ahci, 4096, 0,
1396 CMD_READ_DMA_EXT,
1397 CMD_WRITE_DMA_EXT);
1398 ahci_set(ahci, AHCI_GHC, AHCI_GHC_HR);
1399 ahci_clean_mem(ahci);
1402 ahci_shutdown(ahci);
1405 static void test_ncq_simple(void)
1407 AHCIQState *ahci;
1409 ahci = ahci_boot_and_enable(NULL);
1410 ahci_test_io_rw_simple(ahci, 4096, 0,
1411 READ_FPDMA_QUEUED,
1412 WRITE_FPDMA_QUEUED);
1413 ahci_shutdown(ahci);
1416 static int prepare_iso(size_t size, unsigned char **buf, char **name)
1418 char cdrom_path[] = "/tmp/qtest.iso.XXXXXX";
1419 unsigned char *patt;
1420 ssize_t ret;
1421 int fd = mkstemp(cdrom_path);
1423 g_assert(buf);
1424 g_assert(name);
1425 patt = g_malloc(size);
1427 /* Generate a pattern and build a CDROM image to read from */
1428 generate_pattern(patt, size, ATAPI_SECTOR_SIZE);
1429 ret = write(fd, patt, size);
1430 g_assert(ret == size);
1432 *name = g_strdup(cdrom_path);
1433 *buf = patt;
1434 return fd;
1437 static void remove_iso(int fd, char *name)
1439 unlink(name);
1440 g_free(name);
1441 close(fd);
1444 static int ahci_cb_cmp_buff(AHCIQState *ahci, AHCICommand *cmd,
1445 const AHCIOpts *opts)
1447 unsigned char *tx = opts->opaque;
1448 unsigned char *rx = g_malloc0(opts->size);
1450 bufread(opts->buffer, rx, opts->size);
1451 g_assert_cmphex(memcmp(tx, rx, opts->size), ==, 0);
1452 g_free(rx);
1454 return 0;
1457 static void ahci_test_cdrom(int nsectors, bool dma)
1459 AHCIQState *ahci;
1460 unsigned char *tx;
1461 char *iso;
1462 int fd;
1463 AHCIOpts opts = {
1464 .size = (ATAPI_SECTOR_SIZE * nsectors),
1465 .atapi = true,
1466 .atapi_dma = dma,
1467 .post_cb = ahci_cb_cmp_buff,
1470 /* Prepare ISO and fill 'tx' buffer */
1471 fd = prepare_iso(1024 * 1024, &tx, &iso);
1472 opts.opaque = tx;
1474 /* Standard startup wonkery, but use ide-cd and our special iso file */
1475 ahci = ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,format=raw "
1476 "-M q35 "
1477 "-device ide-cd,drive=drive0 ", iso);
1479 /* Build & Send AHCI command */
1480 ahci_exec(ahci, ahci_port_select(ahci), CMD_ATAPI_READ_10, &opts);
1482 /* Cleanup */
1483 g_free(tx);
1484 ahci_shutdown(ahci);
1485 remove_iso(fd, iso);
1488 static void test_cdrom_dma(void)
1490 ahci_test_cdrom(1, true);
1493 static void test_cdrom_dma_multi(void)
1495 ahci_test_cdrom(3, true);
1498 static void test_cdrom_pio(void)
1500 ahci_test_cdrom(1, false);
1503 static void test_cdrom_pio_multi(void)
1505 ahci_test_cdrom(3, false);
1508 /******************************************************************************/
1509 /* AHCI I/O Test Matrix Definitions */
1511 enum BuffLen {
1512 LEN_BEGIN = 0,
1513 LEN_SIMPLE = LEN_BEGIN,
1514 LEN_DOUBLE,
1515 LEN_LONG,
1516 LEN_SHORT,
1517 NUM_LENGTHS
1520 static const char *buff_len_str[NUM_LENGTHS] = { "simple", "double",
1521 "long", "short" };
1523 enum AddrMode {
1524 ADDR_MODE_BEGIN = 0,
1525 ADDR_MODE_LBA28 = ADDR_MODE_BEGIN,
1526 ADDR_MODE_LBA48,
1527 NUM_ADDR_MODES
1530 static const char *addr_mode_str[NUM_ADDR_MODES] = { "lba28", "lba48" };
1532 enum IOMode {
1533 MODE_BEGIN = 0,
1534 MODE_PIO = MODE_BEGIN,
1535 MODE_DMA,
1536 NUM_MODES
1539 static const char *io_mode_str[NUM_MODES] = { "pio", "dma" };
1541 enum IOOps {
1542 IO_BEGIN = 0,
1543 IO_READ = IO_BEGIN,
1544 IO_WRITE,
1545 NUM_IO_OPS
1548 enum OffsetType {
1549 OFFSET_BEGIN = 0,
1550 OFFSET_ZERO = OFFSET_BEGIN,
1551 OFFSET_LOW,
1552 OFFSET_HIGH,
1553 NUM_OFFSETS
1556 static const char *offset_str[NUM_OFFSETS] = { "zero", "low", "high" };
1558 typedef struct AHCIIOTestOptions {
1559 enum BuffLen length;
1560 enum AddrMode address_type;
1561 enum IOMode io_type;
1562 enum OffsetType offset;
1563 } AHCIIOTestOptions;
1565 static uint64_t offset_sector(enum OffsetType ofst,
1566 enum AddrMode addr_type,
1567 uint64_t buffsize)
1569 uint64_t ceil;
1570 uint64_t nsectors;
1572 switch (ofst) {
1573 case OFFSET_ZERO:
1574 return 0;
1575 case OFFSET_LOW:
1576 return 1;
1577 case OFFSET_HIGH:
1578 ceil = (addr_type == ADDR_MODE_LBA28) ? 0xfffffff : 0xffffffffffff;
1579 ceil = MIN(ceil, mb_to_sectors(test_image_size_mb) - 1);
1580 nsectors = buffsize / AHCI_SECTOR_SIZE;
1581 return ceil - nsectors + 1;
1582 default:
1583 g_assert_not_reached();
1588 * Table of possible I/O ATA commands given a set of enumerations.
1590 static const uint8_t io_cmds[NUM_MODES][NUM_ADDR_MODES][NUM_IO_OPS] = {
1591 [MODE_PIO] = {
1592 [ADDR_MODE_LBA28] = {
1593 [IO_READ] = CMD_READ_PIO,
1594 [IO_WRITE] = CMD_WRITE_PIO },
1595 [ADDR_MODE_LBA48] = {
1596 [IO_READ] = CMD_READ_PIO_EXT,
1597 [IO_WRITE] = CMD_WRITE_PIO_EXT }
1599 [MODE_DMA] = {
1600 [ADDR_MODE_LBA28] = {
1601 [IO_READ] = CMD_READ_DMA,
1602 [IO_WRITE] = CMD_WRITE_DMA },
1603 [ADDR_MODE_LBA48] = {
1604 [IO_READ] = CMD_READ_DMA_EXT,
1605 [IO_WRITE] = CMD_WRITE_DMA_EXT }
1610 * Test a Read/Write pattern using various commands, addressing modes,
1611 * transfer modes, and buffer sizes.
1613 static void test_io_rw_interface(enum AddrMode lba48, enum IOMode dma,
1614 unsigned bufsize, uint64_t sector)
1616 AHCIQState *ahci;
1618 ahci = ahci_boot_and_enable(NULL);
1619 ahci_test_io_rw_simple(ahci, bufsize, sector,
1620 io_cmds[dma][lba48][IO_READ],
1621 io_cmds[dma][lba48][IO_WRITE]);
1622 ahci_shutdown(ahci);
1626 * Demultiplex the test data and invoke the actual test routine.
1628 static void test_io_interface(gconstpointer opaque)
1630 AHCIIOTestOptions *opts = (AHCIIOTestOptions *)opaque;
1631 unsigned bufsize;
1632 uint64_t sector;
1634 switch (opts->length) {
1635 case LEN_SIMPLE:
1636 bufsize = 4096;
1637 break;
1638 case LEN_DOUBLE:
1639 bufsize = 8192;
1640 break;
1641 case LEN_LONG:
1642 bufsize = 4096 * 64;
1643 break;
1644 case LEN_SHORT:
1645 bufsize = 512;
1646 break;
1647 default:
1648 g_assert_not_reached();
1651 sector = offset_sector(opts->offset, opts->address_type, bufsize);
1652 test_io_rw_interface(opts->address_type, opts->io_type, bufsize, sector);
1653 g_free(opts);
1654 return;
1657 static void create_ahci_io_test(enum IOMode type, enum AddrMode addr,
1658 enum BuffLen len, enum OffsetType offset)
1660 char *name;
1661 AHCIIOTestOptions *opts;
1663 opts = g_malloc(sizeof(AHCIIOTestOptions));
1664 opts->length = len;
1665 opts->address_type = addr;
1666 opts->io_type = type;
1667 opts->offset = offset;
1669 name = g_strdup_printf("ahci/io/%s/%s/%s/%s",
1670 io_mode_str[type],
1671 addr_mode_str[addr],
1672 buff_len_str[len],
1673 offset_str[offset]);
1675 if ((addr == ADDR_MODE_LBA48) && (offset == OFFSET_HIGH) &&
1676 (mb_to_sectors(test_image_size_mb) <= 0xFFFFFFF)) {
1677 g_test_message("%s: skipped; test image too small", name);
1678 g_free(name);
1679 return;
1682 qtest_add_data_func(name, opts, test_io_interface);
1683 g_free(name);
1686 /******************************************************************************/
1688 int main(int argc, char **argv)
1690 const char *arch;
1691 int ret;
1692 int fd;
1693 int c;
1694 int i, j, k, m;
1696 static struct option long_options[] = {
1697 {"pedantic", no_argument, 0, 'p' },
1698 {0, 0, 0, 0},
1701 /* Should be first to utilize g_test functionality, So we can see errors. */
1702 g_test_init(&argc, &argv, NULL);
1704 while (1) {
1705 c = getopt_long(argc, argv, "", long_options, NULL);
1706 if (c == -1) {
1707 break;
1709 switch (c) {
1710 case -1:
1711 break;
1712 case 'p':
1713 ahci_pedantic = 1;
1714 break;
1715 default:
1716 fprintf(stderr, "Unrecognized ahci_test option.\n");
1717 g_assert_not_reached();
1721 /* Check architecture */
1722 arch = qtest_get_arch();
1723 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
1724 g_test_message("Skipping test for non-x86");
1725 return 0;
1728 /* Create a temporary image */
1729 fd = mkstemp(tmp_path);
1730 g_assert(fd >= 0);
1731 if (have_qemu_img()) {
1732 imgfmt = "qcow2";
1733 test_image_size_mb = TEST_IMAGE_SIZE_MB_LARGE;
1734 mkqcow2(tmp_path, TEST_IMAGE_SIZE_MB_LARGE);
1735 } else {
1736 g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
1737 "skipping LBA48 high-sector tests");
1738 imgfmt = "raw";
1739 test_image_size_mb = TEST_IMAGE_SIZE_MB_SMALL;
1740 ret = ftruncate(fd, test_image_size_mb * 1024 * 1024);
1741 g_assert(ret == 0);
1743 close(fd);
1745 /* Create temporary blkdebug instructions */
1746 fd = mkstemp(debug_path);
1747 g_assert(fd >= 0);
1748 close(fd);
1750 /* Reserve a hollow file to use as a socket for migration tests */
1751 fd = mkstemp(mig_socket);
1752 g_assert(fd >= 0);
1753 close(fd);
1755 /* Run the tests */
1756 qtest_add_func("/ahci/sanity", test_sanity);
1757 qtest_add_func("/ahci/pci_spec", test_pci_spec);
1758 qtest_add_func("/ahci/pci_enable", test_pci_enable);
1759 qtest_add_func("/ahci/hba_spec", test_hba_spec);
1760 qtest_add_func("/ahci/hba_enable", test_hba_enable);
1761 qtest_add_func("/ahci/identify", test_identify);
1763 for (i = MODE_BEGIN; i < NUM_MODES; i++) {
1764 for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
1765 for (k = LEN_BEGIN; k < NUM_LENGTHS; k++) {
1766 for (m = OFFSET_BEGIN; m < NUM_OFFSETS; m++) {
1767 create_ahci_io_test(i, j, k, m);
1773 qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented);
1775 qtest_add_func("/ahci/flush/simple", test_flush);
1776 qtest_add_func("/ahci/flush/retry", test_flush_retry);
1777 qtest_add_func("/ahci/flush/migrate", test_flush_migrate);
1779 qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity);
1780 qtest_add_func("/ahci/migrate/dma/simple", test_migrate_dma);
1781 qtest_add_func("/ahci/io/dma/lba28/retry", test_halted_dma);
1782 qtest_add_func("/ahci/migrate/dma/halted", test_migrate_halted_dma);
1784 qtest_add_func("/ahci/max", test_max);
1785 qtest_add_func("/ahci/reset", test_reset);
1787 qtest_add_func("/ahci/io/ncq/simple", test_ncq_simple);
1788 qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq);
1789 qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq);
1790 qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq);
1792 qtest_add_func("/ahci/cdrom/dma/single", test_cdrom_dma);
1793 qtest_add_func("/ahci/cdrom/dma/multi", test_cdrom_dma_multi);
1794 qtest_add_func("/ahci/cdrom/pio/single", test_cdrom_pio);
1795 qtest_add_func("/ahci/cdrom/pio/multi", test_cdrom_pio_multi);
1797 ret = g_test_run();
1799 /* Cleanup */
1800 unlink(tmp_path);
1801 unlink(debug_path);
1802 unlink(mig_socket);
1804 return ret;