Merge remote-tracking branch 'remotes/bkoppelmann/tags/pull-tricore-20150629' into...
[qemu.git] / tests / ahci-test.c
blobae9415d74c903a81c63be5b5ba553bb5f53ad9cc
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-specific defines -- in MiB */
43 #define TEST_IMAGE_SIZE_MB (200 * 1024)
44 #define TEST_IMAGE_SECTORS ((TEST_IMAGE_SIZE_MB / AHCI_SECTOR_SIZE) \
45 * 1024 * 1024)
47 /*** Globals ***/
48 static char tmp_path[] = "/tmp/qtest.XXXXXX";
49 static char debug_path[] = "/tmp/qtest-blkdebug.XXXXXX";
50 static bool ahci_pedantic;
52 /*** Function Declarations ***/
53 static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port);
54 static void ahci_test_pci_spec(AHCIQState *ahci);
55 static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
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 void string_bswap16(uint16_t *s, size_t bytes)
65 g_assert_cmphex((bytes & 1), ==, 0);
66 bytes /= 2;
68 while (bytes--) {
69 *s = bswap16(*s);
70 s++;
74 static void generate_pattern(void *buffer, size_t len, size_t cycle_len)
76 int i, j;
77 unsigned char *tx = (unsigned char *)buffer;
78 unsigned char p;
79 size_t *sx;
81 /* Write an indicative pattern that varies and is unique per-cycle */
82 p = rand() % 256;
83 for (i = j = 0; i < len; i++, j++) {
84 tx[i] = p;
85 if (j % cycle_len == 0) {
86 p = rand() % 256;
90 /* force uniqueness by writing an id per-cycle */
91 for (i = 0; i < len / cycle_len; i++) {
92 j = i * cycle_len;
93 if (j + sizeof(*sx) <= len) {
94 sx = (size_t *)&tx[j];
95 *sx = i;
101 * Verify that the transfer did not corrupt our state at all.
103 static void verify_state(AHCIQState *ahci)
105 int i, j;
106 uint32_t ahci_fingerprint;
107 uint64_t hba_base;
108 uint64_t hba_stored;
109 AHCICommandHeader cmd;
111 ahci_fingerprint = qpci_config_readl(ahci->dev, PCI_VENDOR_ID);
112 g_assert_cmphex(ahci_fingerprint, ==, ahci->fingerprint);
114 /* If we haven't initialized, this is as much as can be validated. */
115 if (!ahci->hba_base) {
116 return;
119 hba_base = (uint64_t)qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
120 hba_stored = (uint64_t)(uintptr_t)ahci->hba_base;
121 g_assert_cmphex(hba_base, ==, hba_stored);
123 g_assert_cmphex(ahci_rreg(ahci, AHCI_CAP), ==, ahci->cap);
124 g_assert_cmphex(ahci_rreg(ahci, AHCI_CAP2), ==, ahci->cap2);
126 for (i = 0; i < 32; i++) {
127 g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_FB), ==,
128 ahci->port[i].fb);
129 g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_CLB), ==,
130 ahci->port[i].clb);
131 for (j = 0; j < 32; j++) {
132 ahci_get_command_header(ahci, i, j, &cmd);
133 g_assert_cmphex(cmd.prdtl, ==, ahci->port[i].prdtl[j]);
134 g_assert_cmphex(cmd.ctba, ==, ahci->port[i].ctba[j]);
139 static void ahci_migrate(AHCIQState *from, AHCIQState *to, const char *uri)
141 QOSState *tmp = to->parent;
142 QPCIDevice *dev = to->dev;
143 if (uri == NULL) {
144 uri = "tcp:127.0.0.1:1234";
147 /* context will be 'to' after completion. */
148 migrate(from->parent, to->parent, uri);
150 /* We'd like for the AHCIState objects to still point
151 * to information specific to its specific parent
152 * instance, but otherwise just inherit the new data. */
153 memcpy(to, from, sizeof(AHCIQState));
154 to->parent = tmp;
155 to->dev = dev;
157 tmp = from->parent;
158 dev = from->dev;
159 memset(from, 0x00, sizeof(AHCIQState));
160 from->parent = tmp;
161 from->dev = dev;
163 verify_state(to);
166 /*** Test Setup & Teardown ***/
169 * Start a Q35 machine and bookmark a handle to the AHCI device.
171 static AHCIQState *ahci_vboot(const char *cli, va_list ap)
173 AHCIQState *s;
175 s = g_malloc0(sizeof(AHCIQState));
176 s->parent = qtest_pc_vboot(cli, ap);
177 alloc_set_flags(s->parent->alloc, ALLOC_LEAK_ASSERT);
179 /* Verify that we have an AHCI device present. */
180 s->dev = get_ahci_device(&s->fingerprint);
182 return s;
186 * Start a Q35 machine and bookmark a handle to the AHCI device.
188 static AHCIQState *ahci_boot(const char *cli, ...)
190 AHCIQState *s;
191 va_list ap;
193 if (cli) {
194 va_start(ap, cli);
195 s = ahci_vboot(cli, ap);
196 va_end(ap);
197 } else {
198 cli = "-drive if=none,id=drive0,file=%s,cache=writeback,serial=%s"
199 ",format=qcow2"
200 " -M q35 "
201 "-device ide-hd,drive=drive0 "
202 "-global ide-hd.ver=%s";
203 s = ahci_boot(cli, tmp_path, "testdisk", "version");
206 return s;
210 * Clean up the PCI device, then terminate the QEMU instance.
212 static void ahci_shutdown(AHCIQState *ahci)
214 QOSState *qs = ahci->parent;
216 set_context(qs);
217 ahci_clean_mem(ahci);
218 free_ahci_device(ahci->dev);
219 g_free(ahci);
220 qtest_shutdown(qs);
224 * Boot and fully enable the HBA device.
225 * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
227 static AHCIQState *ahci_boot_and_enable(const char *cli, ...)
229 AHCIQState *ahci;
230 va_list ap;
232 if (cli) {
233 va_start(ap, cli);
234 ahci = ahci_vboot(cli, ap);
235 va_end(ap);
236 } else {
237 ahci = ahci_boot(NULL);
240 ahci_pci_enable(ahci);
241 ahci_hba_enable(ahci);
243 return ahci;
246 /*** Specification Adherence Tests ***/
249 * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
251 static void ahci_test_pci_spec(AHCIQState *ahci)
253 uint8_t datab;
254 uint16_t data;
255 uint32_t datal;
257 /* Most of these bits should start cleared until we turn them on. */
258 data = qpci_config_readw(ahci->dev, PCI_COMMAND);
259 ASSERT_BIT_CLEAR(data, PCI_COMMAND_MEMORY);
260 ASSERT_BIT_CLEAR(data, PCI_COMMAND_MASTER);
261 ASSERT_BIT_CLEAR(data, PCI_COMMAND_SPECIAL); /* Reserved */
262 ASSERT_BIT_CLEAR(data, PCI_COMMAND_VGA_PALETTE); /* Reserved */
263 ASSERT_BIT_CLEAR(data, PCI_COMMAND_PARITY);
264 ASSERT_BIT_CLEAR(data, PCI_COMMAND_WAIT); /* Reserved */
265 ASSERT_BIT_CLEAR(data, PCI_COMMAND_SERR);
266 ASSERT_BIT_CLEAR(data, PCI_COMMAND_FAST_BACK);
267 ASSERT_BIT_CLEAR(data, PCI_COMMAND_INTX_DISABLE);
268 ASSERT_BIT_CLEAR(data, 0xF800); /* Reserved */
270 data = qpci_config_readw(ahci->dev, PCI_STATUS);
271 ASSERT_BIT_CLEAR(data, 0x01 | 0x02 | 0x04); /* Reserved */
272 ASSERT_BIT_CLEAR(data, PCI_STATUS_INTERRUPT);
273 ASSERT_BIT_SET(data, PCI_STATUS_CAP_LIST); /* must be set */
274 ASSERT_BIT_CLEAR(data, PCI_STATUS_UDF); /* Reserved */
275 ASSERT_BIT_CLEAR(data, PCI_STATUS_PARITY);
276 ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_TARGET_ABORT);
277 ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_TARGET_ABORT);
278 ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_MASTER_ABORT);
279 ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_SYSTEM_ERROR);
280 ASSERT_BIT_CLEAR(data, PCI_STATUS_DETECTED_PARITY);
282 /* RID occupies the low byte, CCs occupy the high three. */
283 datal = qpci_config_readl(ahci->dev, PCI_CLASS_REVISION);
284 if (ahci_pedantic) {
285 /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
286 * Though in practice this is likely seldom true. */
287 ASSERT_BIT_CLEAR(datal, 0xFF);
290 /* BCC *must* equal 0x01. */
291 g_assert_cmphex(PCI_BCC(datal), ==, 0x01);
292 if (PCI_SCC(datal) == 0x01) {
293 /* IDE */
294 ASSERT_BIT_SET(0x80000000, datal);
295 ASSERT_BIT_CLEAR(0x60000000, datal);
296 } else if (PCI_SCC(datal) == 0x04) {
297 /* RAID */
298 g_assert_cmphex(PCI_PI(datal), ==, 0);
299 } else if (PCI_SCC(datal) == 0x06) {
300 /* AHCI */
301 g_assert_cmphex(PCI_PI(datal), ==, 0x01);
302 } else {
303 g_assert_not_reached();
306 datab = qpci_config_readb(ahci->dev, PCI_CACHE_LINE_SIZE);
307 g_assert_cmphex(datab, ==, 0);
309 datab = qpci_config_readb(ahci->dev, PCI_LATENCY_TIMER);
310 g_assert_cmphex(datab, ==, 0);
312 /* Only the bottom 7 bits must be off. */
313 datab = qpci_config_readb(ahci->dev, PCI_HEADER_TYPE);
314 ASSERT_BIT_CLEAR(datab, 0x7F);
316 /* BIST is optional, but the low 7 bits must always start off regardless. */
317 datab = qpci_config_readb(ahci->dev, PCI_BIST);
318 ASSERT_BIT_CLEAR(datab, 0x7F);
320 /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
321 datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
322 g_assert_cmphex(datal, ==, 0);
324 qpci_config_writel(ahci->dev, PCI_BASE_ADDRESS_5, 0xFFFFFFFF);
325 datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
326 /* ABAR must be 32-bit, memory mapped, non-prefetchable and
327 * must be >= 512 bytes. To that end, bits 0-8 must be off. */
328 ASSERT_BIT_CLEAR(datal, 0xFF);
330 /* Capability list MUST be present, */
331 datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST);
332 /* But these bits are reserved. */
333 ASSERT_BIT_CLEAR(datal, ~0xFF);
334 g_assert_cmphex(datal, !=, 0);
336 /* Check specification adherence for capability extenstions. */
337 data = qpci_config_readw(ahci->dev, datal);
339 switch (ahci->fingerprint) {
340 case AHCI_INTEL_ICH9:
341 /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
342 g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_MSI);
343 break;
344 default:
345 /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
346 g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_PM);
349 ahci_test_pci_caps(ahci, data, (uint8_t)datal);
351 /* Reserved. */
352 datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST + 4);
353 g_assert_cmphex(datal, ==, 0);
355 /* IPIN might vary, but ILINE must be off. */
356 datab = qpci_config_readb(ahci->dev, PCI_INTERRUPT_LINE);
357 g_assert_cmphex(datab, ==, 0);
361 * Test PCI capabilities for AHCI specification adherence.
363 static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
364 uint8_t offset)
366 uint8_t cid = header & 0xFF;
367 uint8_t next = header >> 8;
369 g_test_message("CID: %02x; next: %02x", cid, next);
371 switch (cid) {
372 case PCI_CAP_ID_PM:
373 ahci_test_pmcap(ahci, offset);
374 break;
375 case PCI_CAP_ID_MSI:
376 ahci_test_msicap(ahci, offset);
377 break;
378 case PCI_CAP_ID_SATA:
379 ahci_test_satacap(ahci, offset);
380 break;
382 default:
383 g_test_message("Unknown CAP 0x%02x", cid);
386 if (next) {
387 ahci_test_pci_caps(ahci, qpci_config_readw(ahci->dev, next), next);
392 * Test SATA PCI capabilitity for AHCI specification adherence.
394 static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset)
396 uint16_t dataw;
397 uint32_t datal;
399 g_test_message("Verifying SATACAP");
401 /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
402 dataw = qpci_config_readw(ahci->dev, offset + 2);
403 g_assert_cmphex(dataw, ==, 0x10);
405 /* Grab the SATACR1 register. */
406 datal = qpci_config_readw(ahci->dev, offset + 4);
408 switch (datal & 0x0F) {
409 case 0x04: /* BAR0 */
410 case 0x05: /* BAR1 */
411 case 0x06:
412 case 0x07:
413 case 0x08:
414 case 0x09: /* BAR5 */
415 case 0x0F: /* Immediately following SATACR1 in PCI config space. */
416 break;
417 default:
418 /* Invalid BARLOC for the Index Data Pair. */
419 g_assert_not_reached();
422 /* Reserved. */
423 g_assert_cmphex((datal >> 24), ==, 0x00);
427 * Test MSI PCI capability for AHCI specification adherence.
429 static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset)
431 uint16_t dataw;
432 uint32_t datal;
434 g_test_message("Verifying MSICAP");
436 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_FLAGS);
437 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_ENABLE);
438 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_QSIZE);
439 ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_RESERVED);
441 datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_LO);
442 g_assert_cmphex(datal, ==, 0);
444 if (dataw & PCI_MSI_FLAGS_64BIT) {
445 g_test_message("MSICAP is 64bit");
446 datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_HI);
447 g_assert_cmphex(datal, ==, 0);
448 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_64);
449 g_assert_cmphex(dataw, ==, 0);
450 } else {
451 g_test_message("MSICAP is 32bit");
452 dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_32);
453 g_assert_cmphex(dataw, ==, 0);
458 * Test Power Management PCI capability for AHCI specification adherence.
460 static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset)
462 uint16_t dataw;
464 g_test_message("Verifying PMCAP");
466 dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_PMC);
467 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_PME_CLOCK);
468 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_RESERVED);
469 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D1);
470 ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D2);
472 dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_CTRL);
473 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_STATE_MASK);
474 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_RESERVED);
475 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SEL_MASK);
476 ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SCALE_MASK);
479 static void ahci_test_hba_spec(AHCIQState *ahci)
481 unsigned i;
482 uint32_t reg;
483 uint32_t ports;
484 uint8_t nports_impl;
485 uint8_t maxports;
487 g_assert(ahci != NULL);
490 * Note that the AHCI spec does expect the BIOS to set up a few things:
491 * CAP.SSS - Support for staggered spin-up (t/f)
492 * CAP.SMPS - Support for mechanical presence switches (t/f)
493 * PI - Ports Implemented (1-32)
494 * PxCMD.HPCP - Hot Plug Capable Port
495 * PxCMD.MPSP - Mechanical Presence Switch Present
496 * PxCMD.CPD - Cold Presence Detection support
498 * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
499 * Foreach Port Implemented:
500 * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
501 * -PxCLB/U and PxFB/U are set to valid regions in memory
502 * -PxSUD is set to 1.
503 * -PxSSTS.DET is polled for presence; if detected, we continue:
504 * -PxSERR is cleared with 1's.
505 * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
506 * the device is ready.
509 /* 1 CAP - Capabilities Register */
510 ahci->cap = ahci_rreg(ahci, AHCI_CAP);
511 ASSERT_BIT_CLEAR(ahci->cap, AHCI_CAP_RESERVED);
513 /* 2 GHC - Global Host Control */
514 reg = ahci_rreg(ahci, AHCI_GHC);
515 ASSERT_BIT_CLEAR(reg, AHCI_GHC_HR);
516 ASSERT_BIT_CLEAR(reg, AHCI_GHC_IE);
517 ASSERT_BIT_CLEAR(reg, AHCI_GHC_MRSM);
518 if (BITSET(ahci->cap, AHCI_CAP_SAM)) {
519 g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
520 ASSERT_BIT_SET(reg, AHCI_GHC_AE);
521 } else {
522 g_test_message("Supports AHCI/Legacy mix.");
523 ASSERT_BIT_CLEAR(reg, AHCI_GHC_AE);
526 /* 3 IS - Interrupt Status */
527 reg = ahci_rreg(ahci, AHCI_IS);
528 g_assert_cmphex(reg, ==, 0);
530 /* 4 PI - Ports Implemented */
531 ports = ahci_rreg(ahci, AHCI_PI);
532 /* Ports Implemented must be non-zero. */
533 g_assert_cmphex(ports, !=, 0);
534 /* Ports Implemented must be <= Number of Ports. */
535 nports_impl = ctpopl(ports);
536 g_assert_cmpuint(((AHCI_CAP_NP & ahci->cap) + 1), >=, nports_impl);
538 /* Ports must be within the proper range. Given a mapping of SIZE,
539 * 256 bytes are used for global HBA control, and the rest is used
540 * for ports data, at 0x80 bytes each. */
541 g_assert_cmphex(ahci->barsize, >, 0);
542 maxports = (ahci->barsize - HBA_DATA_REGION_SIZE) / HBA_PORT_DATA_SIZE;
543 /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
544 g_assert_cmphex((reg >> maxports), ==, 0);
546 /* 5 AHCI Version */
547 reg = ahci_rreg(ahci, AHCI_VS);
548 switch (reg) {
549 case AHCI_VERSION_0_95:
550 case AHCI_VERSION_1_0:
551 case AHCI_VERSION_1_1:
552 case AHCI_VERSION_1_2:
553 case AHCI_VERSION_1_3:
554 break;
555 default:
556 g_assert_not_reached();
559 /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
560 reg = ahci_rreg(ahci, AHCI_CCCCTL);
561 if (BITSET(ahci->cap, AHCI_CAP_CCCS)) {
562 ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_EN);
563 ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_RESERVED);
564 ASSERT_BIT_SET(reg, AHCI_CCCCTL_CC);
565 ASSERT_BIT_SET(reg, AHCI_CCCCTL_TV);
566 } else {
567 g_assert_cmphex(reg, ==, 0);
570 /* 7 CCC_PORTS */
571 reg = ahci_rreg(ahci, AHCI_CCCPORTS);
572 /* Must be zeroes initially regardless of CAP.CCCS */
573 g_assert_cmphex(reg, ==, 0);
575 /* 8 EM_LOC */
576 reg = ahci_rreg(ahci, AHCI_EMLOC);
577 if (BITCLR(ahci->cap, AHCI_CAP_EMS)) {
578 g_assert_cmphex(reg, ==, 0);
581 /* 9 EM_CTL */
582 reg = ahci_rreg(ahci, AHCI_EMCTL);
583 if (BITSET(ahci->cap, AHCI_CAP_EMS)) {
584 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_STSMR);
585 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLTM);
586 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLRST);
587 ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_RESERVED);
588 } else {
589 g_assert_cmphex(reg, ==, 0);
592 /* 10 CAP2 -- Capabilities Extended */
593 ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
594 ASSERT_BIT_CLEAR(ahci->cap2, AHCI_CAP2_RESERVED);
596 /* 11 BOHC -- Bios/OS Handoff Control */
597 reg = ahci_rreg(ahci, AHCI_BOHC);
598 g_assert_cmphex(reg, ==, 0);
600 /* 12 -- 23: Reserved */
601 g_test_message("Verifying HBA reserved area is empty.");
602 for (i = AHCI_RESERVED; i < AHCI_NVMHCI; ++i) {
603 reg = ahci_rreg(ahci, i);
604 g_assert_cmphex(reg, ==, 0);
607 /* 24 -- 39: NVMHCI */
608 if (BITCLR(ahci->cap2, AHCI_CAP2_NVMP)) {
609 g_test_message("Verifying HBA/NVMHCI area is empty.");
610 for (i = AHCI_NVMHCI; i < AHCI_VENDOR; ++i) {
611 reg = ahci_rreg(ahci, i);
612 g_assert_cmphex(reg, ==, 0);
616 /* 40 -- 63: Vendor */
617 g_test_message("Verifying HBA/Vendor area is empty.");
618 for (i = AHCI_VENDOR; i < AHCI_PORTS; ++i) {
619 reg = ahci_rreg(ahci, i);
620 g_assert_cmphex(reg, ==, 0);
623 /* 64 -- XX: Port Space */
624 for (i = 0; ports || (i < maxports); ports >>= 1, ++i) {
625 if (BITSET(ports, 0x1)) {
626 g_test_message("Testing port %u for spec", i);
627 ahci_test_port_spec(ahci, i);
628 } else {
629 uint16_t j;
630 uint16_t low = AHCI_PORTS + (32 * i);
631 uint16_t high = AHCI_PORTS + (32 * (i + 1));
632 g_test_message("Asserting unimplemented port %u "
633 "(reg [%u-%u]) is empty.",
634 i, low, high - 1);
635 for (j = low; j < high; ++j) {
636 reg = ahci_rreg(ahci, j);
637 g_assert_cmphex(reg, ==, 0);
644 * Test the memory space for one port for specification adherence.
646 static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
648 uint32_t reg;
649 unsigned i;
651 /* (0) CLB */
652 reg = ahci_px_rreg(ahci, port, AHCI_PX_CLB);
653 ASSERT_BIT_CLEAR(reg, AHCI_PX_CLB_RESERVED);
655 /* (1) CLBU */
656 if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
657 reg = ahci_px_rreg(ahci, port, AHCI_PX_CLBU);
658 g_assert_cmphex(reg, ==, 0);
661 /* (2) FB */
662 reg = ahci_px_rreg(ahci, port, AHCI_PX_FB);
663 ASSERT_BIT_CLEAR(reg, AHCI_PX_FB_RESERVED);
665 /* (3) FBU */
666 if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
667 reg = ahci_px_rreg(ahci, port, AHCI_PX_FBU);
668 g_assert_cmphex(reg, ==, 0);
671 /* (4) IS */
672 reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
673 g_assert_cmphex(reg, ==, 0);
675 /* (5) IE */
676 reg = ahci_px_rreg(ahci, port, AHCI_PX_IE);
677 g_assert_cmphex(reg, ==, 0);
679 /* (6) CMD */
680 reg = ahci_px_rreg(ahci, port, AHCI_PX_CMD);
681 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FRE);
682 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_RESERVED);
683 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CCS);
684 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
685 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
686 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_PMA); /* And RW only if CAP.SPM */
687 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_APSTE); /* RW only if CAP2.APST */
688 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ATAPI);
689 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_DLAE);
690 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ALPE); /* RW only if CAP.SALP */
691 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ASP); /* RW only if CAP.SALP */
692 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ICC);
693 /* If CPDetect support does not exist, CPState must be off. */
694 if (BITCLR(reg, AHCI_PX_CMD_CPD)) {
695 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CPS);
697 /* If MPSPresence is not set, MPSState must be off. */
698 if (BITCLR(reg, AHCI_PX_CMD_MPSP)) {
699 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
701 /* If we do not support MPS, MPSS and MPSP must be off. */
702 if (BITCLR(ahci->cap, AHCI_CAP_SMPS)) {
703 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
704 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSP);
706 /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
707 if (BITANY(reg, AHCI_PX_CMD_CPD | AHCI_PX_CMD_MPSP)) {
708 ASSERT_BIT_SET(reg, AHCI_PX_CMD_HPCP);
710 /* HPCP and ESP cannot both be active. */
711 g_assert(!BITSET(reg, AHCI_PX_CMD_HPCP | AHCI_PX_CMD_ESP));
712 /* If CAP.FBSS is not set, FBSCP must not be set. */
713 if (BITCLR(ahci->cap, AHCI_CAP_FBSS)) {
714 ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FBSCP);
717 /* (7) RESERVED */
718 reg = ahci_px_rreg(ahci, port, AHCI_PX_RES1);
719 g_assert_cmphex(reg, ==, 0);
721 /* (8) TFD */
722 reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
723 /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
724 * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
725 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_ERR);
726 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS1);
727 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_DRQ);
728 ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS2);
729 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
730 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
731 ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_RESERVED);
733 /* (9) SIG */
734 /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
735 * Even when GHC.ST is zero, the AHCI HBA may receive the initial
736 * D2H register FIS and update the signature asynchronously,
737 * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
739 /* (10) SSTS / SCR0: SStatus */
740 reg = ahci_px_rreg(ahci, port, AHCI_PX_SSTS);
741 ASSERT_BIT_CLEAR(reg, AHCI_PX_SSTS_RESERVED);
742 /* Even though the register should be 0 at boot, it is asynchronous and
743 * prone to change, so we cannot test any well known value. */
745 /* (11) SCTL / SCR2: SControl */
746 reg = ahci_px_rreg(ahci, port, AHCI_PX_SCTL);
747 g_assert_cmphex(reg, ==, 0);
749 /* (12) SERR / SCR1: SError */
750 reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
751 g_assert_cmphex(reg, ==, 0);
753 /* (13) SACT / SCR3: SActive */
754 reg = ahci_px_rreg(ahci, port, AHCI_PX_SACT);
755 g_assert_cmphex(reg, ==, 0);
757 /* (14) CI */
758 reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
759 g_assert_cmphex(reg, ==, 0);
761 /* (15) SNTF */
762 reg = ahci_px_rreg(ahci, port, AHCI_PX_SNTF);
763 g_assert_cmphex(reg, ==, 0);
765 /* (16) FBS */
766 reg = ahci_px_rreg(ahci, port, AHCI_PX_FBS);
767 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_EN);
768 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEC);
769 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_SDE);
770 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEV);
771 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DWE);
772 ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_RESERVED);
773 if (BITSET(ahci->cap, AHCI_CAP_FBSS)) {
774 /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
775 g_assert((reg & AHCI_PX_FBS_ADO) >> ctzl(AHCI_PX_FBS_ADO) >= 2);
778 /* [17 -- 27] RESERVED */
779 for (i = AHCI_PX_RES2; i < AHCI_PX_VS; ++i) {
780 reg = ahci_px_rreg(ahci, port, i);
781 g_assert_cmphex(reg, ==, 0);
784 /* [28 -- 31] Vendor-Specific */
785 for (i = AHCI_PX_VS; i < 32; ++i) {
786 reg = ahci_px_rreg(ahci, port, i);
787 if (reg) {
788 g_test_message("INFO: Vendor register %u non-empty", i);
794 * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
795 * device we see, then read and check the response.
797 static void ahci_test_identify(AHCIQState *ahci)
799 uint16_t buff[256];
800 unsigned px;
801 int rc;
802 uint16_t sect_size;
803 const size_t buffsize = 512;
805 g_assert(ahci != NULL);
808 * This serves as a bit of a tutorial on AHCI device programming:
810 * (1) Create a data buffer for the IDENTIFY response to be sent to
811 * (2) Create a Command Table buffer, where we will store the
812 * command and PRDT (Physical Region Descriptor Table)
813 * (3) Construct an FIS host-to-device command structure, and write it to
814 * the top of the Command Table buffer.
815 * (4) Create one or more Physical Region Descriptors (PRDs) that describe
816 * a location in memory where data may be stored/retrieved.
817 * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
818 * (6) Each AHCI port has up to 32 command slots. Each slot contains a
819 * header that points to a Command Table buffer. Pick an unused slot
820 * and update it to point to the Command Table we have built.
821 * (7) Now: Command #n points to our Command Table, and our Command Table
822 * contains the FIS (that describes our command) and the PRDTL, which
823 * describes our buffer.
824 * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
825 * #n is ready for processing.
828 /* Pick the first implemented and running port */
829 px = ahci_port_select(ahci);
830 g_test_message("Selected port %u for test", px);
832 /* Clear out the FIS Receive area and any pending interrupts. */
833 ahci_port_clear(ahci, px);
835 /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
836 ahci_io(ahci, px, CMD_IDENTIFY, &buff, buffsize, 0);
838 /* Check serial number/version in the buffer */
839 /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
840 * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
841 * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
842 * as a consequence, only needs to unchunk the data on LE machines. */
843 string_bswap16(&buff[10], 20);
844 rc = memcmp(&buff[10], "testdisk ", 20);
845 g_assert_cmphex(rc, ==, 0);
847 string_bswap16(&buff[23], 8);
848 rc = memcmp(&buff[23], "version ", 8);
849 g_assert_cmphex(rc, ==, 0);
851 sect_size = le16_to_cpu(*((uint16_t *)(&buff[5])));
852 g_assert_cmphex(sect_size, ==, AHCI_SECTOR_SIZE);
855 static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,
856 uint64_t sector, uint8_t read_cmd,
857 uint8_t write_cmd)
859 uint64_t ptr;
860 uint8_t port;
861 unsigned char *tx = g_malloc(bufsize);
862 unsigned char *rx = g_malloc0(bufsize);
864 g_assert(ahci != NULL);
866 /* Pick the first running port and clear it. */
867 port = ahci_port_select(ahci);
868 ahci_port_clear(ahci, port);
870 /*** Create pattern and transfer to guest ***/
871 /* Data buffer in the guest */
872 ptr = ahci_alloc(ahci, bufsize);
873 g_assert(ptr);
875 /* Write some indicative pattern to our buffer. */
876 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
877 bufwrite(ptr, tx, bufsize);
879 /* Write this buffer to disk, then read it back to the DMA buffer. */
880 ahci_guest_io(ahci, port, write_cmd, ptr, bufsize, sector);
881 qmemset(ptr, 0x00, bufsize);
882 ahci_guest_io(ahci, port, read_cmd, ptr, bufsize, sector);
884 /*** Read back the Data ***/
885 bufread(ptr, rx, bufsize);
886 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
888 ahci_free(ahci, ptr);
889 g_free(tx);
890 g_free(rx);
893 static void ahci_test_nondata(AHCIQState *ahci, uint8_t ide_cmd)
895 uint8_t px;
896 AHCICommand *cmd;
898 /* Sanitize */
899 px = ahci_port_select(ahci);
900 ahci_port_clear(ahci, px);
902 /* Issue Command */
903 cmd = ahci_command_create(ide_cmd);
904 ahci_command_commit(ahci, cmd, px);
905 ahci_command_issue(ahci, cmd);
906 ahci_command_verify(ahci, cmd);
907 ahci_command_free(cmd);
910 static void ahci_test_flush(AHCIQState *ahci)
912 ahci_test_nondata(ahci, CMD_FLUSH_CACHE);
916 /******************************************************************************/
917 /* Test Interfaces */
918 /******************************************************************************/
921 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
923 static void test_sanity(void)
925 AHCIQState *ahci;
926 ahci = ahci_boot(NULL);
927 ahci_shutdown(ahci);
931 * Ensure that the PCI configuration space for the AHCI device is in-line with
932 * the AHCI 1.3 specification for initial values.
934 static void test_pci_spec(void)
936 AHCIQState *ahci;
937 ahci = ahci_boot(NULL);
938 ahci_test_pci_spec(ahci);
939 ahci_shutdown(ahci);
943 * Engage the PCI AHCI device and sanity check the response.
944 * Perform additional PCI config space bringup for the HBA.
946 static void test_pci_enable(void)
948 AHCIQState *ahci;
949 ahci = ahci_boot(NULL);
950 ahci_pci_enable(ahci);
951 ahci_shutdown(ahci);
955 * Investigate the memory mapped regions of the HBA,
956 * and test them for AHCI specification adherence.
958 static void test_hba_spec(void)
960 AHCIQState *ahci;
962 ahci = ahci_boot(NULL);
963 ahci_pci_enable(ahci);
964 ahci_test_hba_spec(ahci);
965 ahci_shutdown(ahci);
969 * Engage the HBA functionality of the AHCI PCI device,
970 * and bring it into a functional idle state.
972 static void test_hba_enable(void)
974 AHCIQState *ahci;
976 ahci = ahci_boot(NULL);
977 ahci_pci_enable(ahci);
978 ahci_hba_enable(ahci);
979 ahci_shutdown(ahci);
983 * Bring up the device and issue an IDENTIFY command.
984 * Inspect the state of the HBA device and the data returned.
986 static void test_identify(void)
988 AHCIQState *ahci;
990 ahci = ahci_boot_and_enable(NULL);
991 ahci_test_identify(ahci);
992 ahci_shutdown(ahci);
996 * Fragmented DMA test: Perform a standard 4K DMA read/write
997 * test, but make sure the physical regions are fragmented to
998 * be very small, each just 32 bytes, to see how AHCI performs
999 * with chunks defined to be much less than a sector.
1001 static void test_dma_fragmented(void)
1003 AHCIQState *ahci;
1004 AHCICommand *cmd;
1005 uint8_t px;
1006 size_t bufsize = 4096;
1007 unsigned char *tx = g_malloc(bufsize);
1008 unsigned char *rx = g_malloc0(bufsize);
1009 uint64_t ptr;
1011 ahci = ahci_boot_and_enable(NULL);
1012 px = ahci_port_select(ahci);
1013 ahci_port_clear(ahci, px);
1015 /* create pattern */
1016 generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
1018 /* Create a DMA buffer in guest memory, and write our pattern to it. */
1019 ptr = guest_alloc(ahci->parent->alloc, bufsize);
1020 g_assert(ptr);
1021 bufwrite(ptr, tx, bufsize);
1023 cmd = ahci_command_create(CMD_WRITE_DMA);
1024 ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
1025 ahci_command_commit(ahci, cmd, px);
1026 ahci_command_issue(ahci, cmd);
1027 ahci_command_verify(ahci, cmd);
1028 g_free(cmd);
1030 cmd = ahci_command_create(CMD_READ_DMA);
1031 ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
1032 ahci_command_commit(ahci, cmd, px);
1033 ahci_command_issue(ahci, cmd);
1034 ahci_command_verify(ahci, cmd);
1035 g_free(cmd);
1037 /* Read back the guest's receive buffer into local memory */
1038 bufread(ptr, rx, bufsize);
1039 guest_free(ahci->parent->alloc, ptr);
1041 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1043 ahci_shutdown(ahci);
1045 g_free(rx);
1046 g_free(tx);
1049 static void test_flush(void)
1051 AHCIQState *ahci;
1053 ahci = ahci_boot_and_enable(NULL);
1054 ahci_test_flush(ahci);
1055 ahci_shutdown(ahci);
1058 static void test_flush_retry(void)
1060 AHCIQState *ahci;
1061 AHCICommand *cmd;
1062 uint8_t port;
1063 const char *s;
1065 prepare_blkdebug_script(debug_path, "flush_to_disk");
1066 ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1067 "format=qcow2,cache=writeback,"
1068 "rerror=stop,werror=stop "
1069 "-M q35 "
1070 "-device ide-hd,drive=drive0 ",
1071 debug_path,
1072 tmp_path);
1074 /* Issue Flush Command and wait for error */
1075 port = ahci_port_select(ahci);
1076 ahci_port_clear(ahci, port);
1077 cmd = ahci_command_create(CMD_FLUSH_CACHE);
1078 ahci_command_commit(ahci, cmd, port);
1079 ahci_command_issue_async(ahci, cmd);
1080 qmp_eventwait("STOP");
1082 /* Complete the command */
1083 s = "{'execute':'cont' }";
1084 qmp_async(s);
1085 qmp_eventwait("RESUME");
1086 ahci_command_wait(ahci, cmd);
1087 ahci_command_verify(ahci, cmd);
1089 ahci_command_free(cmd);
1090 ahci_shutdown(ahci);
1094 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
1096 static void test_migrate_sanity(void)
1098 AHCIQState *src, *dst;
1099 const char *uri = "tcp:127.0.0.1:1234";
1101 src = ahci_boot("-m 1024 -M q35 "
1102 "-hda %s ", tmp_path);
1103 dst = ahci_boot("-m 1024 -M q35 "
1104 "-hda %s "
1105 "-incoming %s", tmp_path, uri);
1107 ahci_migrate(src, dst, uri);
1109 ahci_shutdown(src);
1110 ahci_shutdown(dst);
1114 * DMA Migration test: Write a pattern, migrate, then read.
1116 static void test_migrate_dma(void)
1118 AHCIQState *src, *dst;
1119 uint8_t px;
1120 size_t bufsize = 4096;
1121 unsigned char *tx = g_malloc(bufsize);
1122 unsigned char *rx = g_malloc0(bufsize);
1123 unsigned i;
1124 const char *uri = "tcp:127.0.0.1:1234";
1126 src = ahci_boot_and_enable("-m 1024 -M q35 "
1127 "-hda %s ", tmp_path);
1128 dst = ahci_boot("-m 1024 -M q35 "
1129 "-hda %s "
1130 "-incoming %s", tmp_path, uri);
1132 set_context(src->parent);
1134 /* initialize */
1135 px = ahci_port_select(src);
1136 ahci_port_clear(src, px);
1138 /* create pattern */
1139 for (i = 0; i < bufsize; i++) {
1140 tx[i] = (bufsize - i);
1143 /* Write, migrate, then read. */
1144 ahci_io(src, px, CMD_WRITE_DMA, tx, bufsize, 0);
1145 ahci_migrate(src, dst, uri);
1146 ahci_io(dst, px, CMD_READ_DMA, rx, bufsize, 0);
1148 /* Verify pattern */
1149 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1151 ahci_shutdown(src);
1152 ahci_shutdown(dst);
1153 g_free(rx);
1154 g_free(tx);
1158 * DMA Error Test
1160 * Simulate an error on first write, Try to write a pattern,
1161 * Confirm the VM has stopped, resume the VM, verify command
1162 * has completed, then read back the data and verify.
1164 static void test_halted_dma(void)
1166 AHCIQState *ahci;
1167 uint8_t port;
1168 size_t bufsize = 4096;
1169 unsigned char *tx = g_malloc(bufsize);
1170 unsigned char *rx = g_malloc0(bufsize);
1171 unsigned i;
1172 uint64_t ptr;
1173 AHCICommand *cmd;
1175 prepare_blkdebug_script(debug_path, "write_aio");
1177 ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1178 "format=qcow2,cache=writeback,"
1179 "rerror=stop,werror=stop "
1180 "-M q35 "
1181 "-device ide-hd,drive=drive0 ",
1182 debug_path,
1183 tmp_path);
1185 /* Initialize and prepare */
1186 port = ahci_port_select(ahci);
1187 ahci_port_clear(ahci, port);
1189 for (i = 0; i < bufsize; i++) {
1190 tx[i] = (bufsize - i);
1193 /* create DMA source buffer and write pattern */
1194 ptr = ahci_alloc(ahci, bufsize);
1195 g_assert(ptr);
1196 memwrite(ptr, tx, bufsize);
1198 /* Attempt to write (and fail) */
1199 cmd = ahci_guest_io_halt(ahci, port, CMD_WRITE_DMA,
1200 ptr, bufsize, 0);
1202 /* Attempt to resume the command */
1203 ahci_guest_io_resume(ahci, cmd);
1204 ahci_free(ahci, ptr);
1206 /* Read back and verify */
1207 ahci_io(ahci, port, CMD_READ_DMA, rx, bufsize, 0);
1208 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1210 /* Cleanup and go home */
1211 ahci_shutdown(ahci);
1212 g_free(rx);
1213 g_free(tx);
1217 * DMA Error Migration Test
1219 * Simulate an error on first write, Try to write a pattern,
1220 * Confirm the VM has stopped, migrate, resume the VM,
1221 * verify command has completed, then read back the data and verify.
1223 static void test_migrate_halted_dma(void)
1225 AHCIQState *src, *dst;
1226 uint8_t port;
1227 size_t bufsize = 4096;
1228 unsigned char *tx = g_malloc(bufsize);
1229 unsigned char *rx = g_malloc0(bufsize);
1230 unsigned i;
1231 uint64_t ptr;
1232 AHCICommand *cmd;
1233 const char *uri = "tcp:127.0.0.1:1234";
1235 prepare_blkdebug_script(debug_path, "write_aio");
1237 src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1238 "format=qcow2,cache=writeback,"
1239 "rerror=stop,werror=stop "
1240 "-M q35 "
1241 "-device ide-hd,drive=drive0 ",
1242 debug_path,
1243 tmp_path);
1245 dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
1246 "format=qcow2,cache=writeback,"
1247 "rerror=stop,werror=stop "
1248 "-M q35 "
1249 "-device ide-hd,drive=drive0 "
1250 "-incoming %s",
1251 tmp_path, uri);
1253 set_context(src->parent);
1255 /* Initialize and prepare */
1256 port = ahci_port_select(src);
1257 ahci_port_clear(src, port);
1259 for (i = 0; i < bufsize; i++) {
1260 tx[i] = (bufsize - i);
1263 /* create DMA source buffer and write pattern */
1264 ptr = ahci_alloc(src, bufsize);
1265 g_assert(ptr);
1266 memwrite(ptr, tx, bufsize);
1268 /* Write, trigger the VM to stop, migrate, then resume. */
1269 cmd = ahci_guest_io_halt(src, port, CMD_WRITE_DMA,
1270 ptr, bufsize, 0);
1271 ahci_migrate(src, dst, uri);
1272 ahci_guest_io_resume(dst, cmd);
1273 ahci_free(dst, ptr);
1275 /* Read back */
1276 ahci_io(dst, port, CMD_READ_DMA, rx, bufsize, 0);
1278 /* Verify TX and RX are identical */
1279 g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
1281 /* Cleanup and go home. */
1282 ahci_shutdown(src);
1283 ahci_shutdown(dst);
1284 g_free(rx);
1285 g_free(tx);
1289 * Migration test: Try to flush, migrate, then resume.
1291 static void test_flush_migrate(void)
1293 AHCIQState *src, *dst;
1294 AHCICommand *cmd;
1295 uint8_t px;
1296 const char *s;
1297 const char *uri = "tcp:127.0.0.1:1234";
1299 prepare_blkdebug_script(debug_path, "flush_to_disk");
1301 src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
1302 "cache=writeback,rerror=stop,werror=stop "
1303 "-M q35 "
1304 "-device ide-hd,drive=drive0 ",
1305 debug_path, tmp_path);
1306 dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
1307 "cache=writeback,rerror=stop,werror=stop "
1308 "-M q35 "
1309 "-device ide-hd,drive=drive0 "
1310 "-incoming %s", tmp_path, uri);
1312 set_context(src->parent);
1314 /* Issue Flush Command */
1315 px = ahci_port_select(src);
1316 ahci_port_clear(src, px);
1317 cmd = ahci_command_create(CMD_FLUSH_CACHE);
1318 ahci_command_commit(src, cmd, px);
1319 ahci_command_issue_async(src, cmd);
1320 qmp_eventwait("STOP");
1322 /* Migrate over */
1323 ahci_migrate(src, dst, uri);
1325 /* Complete the command */
1326 s = "{'execute':'cont' }";
1327 qmp_async(s);
1328 qmp_eventwait("RESUME");
1329 ahci_command_wait(dst, cmd);
1330 ahci_command_verify(dst, cmd);
1332 ahci_command_free(cmd);
1333 ahci_shutdown(src);
1334 ahci_shutdown(dst);
1337 /******************************************************************************/
1338 /* AHCI I/O Test Matrix Definitions */
1340 enum BuffLen {
1341 LEN_BEGIN = 0,
1342 LEN_SIMPLE = LEN_BEGIN,
1343 LEN_DOUBLE,
1344 LEN_LONG,
1345 LEN_SHORT,
1346 NUM_LENGTHS
1349 static const char *buff_len_str[NUM_LENGTHS] = { "simple", "double",
1350 "long", "short" };
1352 enum AddrMode {
1353 ADDR_MODE_BEGIN = 0,
1354 ADDR_MODE_LBA28 = ADDR_MODE_BEGIN,
1355 ADDR_MODE_LBA48,
1356 NUM_ADDR_MODES
1359 static const char *addr_mode_str[NUM_ADDR_MODES] = { "lba28", "lba48" };
1361 enum IOMode {
1362 MODE_BEGIN = 0,
1363 MODE_PIO = MODE_BEGIN,
1364 MODE_DMA,
1365 NUM_MODES
1368 static const char *io_mode_str[NUM_MODES] = { "pio", "dma" };
1370 enum IOOps {
1371 IO_BEGIN = 0,
1372 IO_READ = IO_BEGIN,
1373 IO_WRITE,
1374 NUM_IO_OPS
1377 enum OffsetType {
1378 OFFSET_BEGIN = 0,
1379 OFFSET_ZERO = OFFSET_BEGIN,
1380 OFFSET_LOW,
1381 OFFSET_HIGH,
1382 NUM_OFFSETS
1385 static const char *offset_str[NUM_OFFSETS] = { "zero", "low", "high" };
1387 typedef struct AHCIIOTestOptions {
1388 enum BuffLen length;
1389 enum AddrMode address_type;
1390 enum IOMode io_type;
1391 enum OffsetType offset;
1392 } AHCIIOTestOptions;
1394 static uint64_t offset_sector(enum OffsetType ofst,
1395 enum AddrMode addr_type,
1396 uint64_t buffsize)
1398 uint64_t ceil;
1399 uint64_t nsectors;
1401 switch (ofst) {
1402 case OFFSET_ZERO:
1403 return 0;
1404 case OFFSET_LOW:
1405 return 1;
1406 case OFFSET_HIGH:
1407 ceil = (addr_type == ADDR_MODE_LBA28) ? 0xfffffff : 0xffffffffffff;
1408 ceil = MIN(ceil, TEST_IMAGE_SECTORS - 1);
1409 nsectors = buffsize / AHCI_SECTOR_SIZE;
1410 return ceil - nsectors + 1;
1411 default:
1412 g_assert_not_reached();
1417 * Table of possible I/O ATA commands given a set of enumerations.
1419 static const uint8_t io_cmds[NUM_MODES][NUM_ADDR_MODES][NUM_IO_OPS] = {
1420 [MODE_PIO] = {
1421 [ADDR_MODE_LBA28] = {
1422 [IO_READ] = CMD_READ_PIO,
1423 [IO_WRITE] = CMD_WRITE_PIO },
1424 [ADDR_MODE_LBA48] = {
1425 [IO_READ] = CMD_READ_PIO_EXT,
1426 [IO_WRITE] = CMD_WRITE_PIO_EXT }
1428 [MODE_DMA] = {
1429 [ADDR_MODE_LBA28] = {
1430 [IO_READ] = CMD_READ_DMA,
1431 [IO_WRITE] = CMD_WRITE_DMA },
1432 [ADDR_MODE_LBA48] = {
1433 [IO_READ] = CMD_READ_DMA_EXT,
1434 [IO_WRITE] = CMD_WRITE_DMA_EXT }
1439 * Test a Read/Write pattern using various commands, addressing modes,
1440 * transfer modes, and buffer sizes.
1442 static void test_io_rw_interface(enum AddrMode lba48, enum IOMode dma,
1443 unsigned bufsize, uint64_t sector)
1445 AHCIQState *ahci;
1447 ahci = ahci_boot_and_enable(NULL);
1448 ahci_test_io_rw_simple(ahci, bufsize, sector,
1449 io_cmds[dma][lba48][IO_READ],
1450 io_cmds[dma][lba48][IO_WRITE]);
1451 ahci_shutdown(ahci);
1455 * Demultiplex the test data and invoke the actual test routine.
1457 static void test_io_interface(gconstpointer opaque)
1459 AHCIIOTestOptions *opts = (AHCIIOTestOptions *)opaque;
1460 unsigned bufsize;
1461 uint64_t sector;
1463 switch (opts->length) {
1464 case LEN_SIMPLE:
1465 bufsize = 4096;
1466 break;
1467 case LEN_DOUBLE:
1468 bufsize = 8192;
1469 break;
1470 case LEN_LONG:
1471 bufsize = 4096 * 64;
1472 break;
1473 case LEN_SHORT:
1474 bufsize = 512;
1475 break;
1476 default:
1477 g_assert_not_reached();
1480 sector = offset_sector(opts->offset, opts->address_type, bufsize);
1481 test_io_rw_interface(opts->address_type, opts->io_type, bufsize, sector);
1482 g_free(opts);
1483 return;
1486 static void create_ahci_io_test(enum IOMode type, enum AddrMode addr,
1487 enum BuffLen len, enum OffsetType offset)
1489 char *name;
1490 AHCIIOTestOptions *opts = g_malloc(sizeof(AHCIIOTestOptions));
1492 opts->length = len;
1493 opts->address_type = addr;
1494 opts->io_type = type;
1495 opts->offset = offset;
1497 name = g_strdup_printf("ahci/io/%s/%s/%s/%s",
1498 io_mode_str[type],
1499 addr_mode_str[addr],
1500 buff_len_str[len],
1501 offset_str[offset]);
1503 qtest_add_data_func(name, opts, test_io_interface);
1504 g_free(name);
1507 /******************************************************************************/
1509 int main(int argc, char **argv)
1511 const char *arch;
1512 int ret;
1513 int fd;
1514 int c;
1515 int i, j, k, m;
1517 static struct option long_options[] = {
1518 {"pedantic", no_argument, 0, 'p' },
1519 {0, 0, 0, 0},
1522 /* Should be first to utilize g_test functionality, So we can see errors. */
1523 g_test_init(&argc, &argv, NULL);
1525 while (1) {
1526 c = getopt_long(argc, argv, "", long_options, NULL);
1527 if (c == -1) {
1528 break;
1530 switch (c) {
1531 case -1:
1532 break;
1533 case 'p':
1534 ahci_pedantic = 1;
1535 break;
1536 default:
1537 fprintf(stderr, "Unrecognized ahci_test option.\n");
1538 g_assert_not_reached();
1542 /* Check architecture */
1543 arch = qtest_get_arch();
1544 if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
1545 g_test_message("Skipping test for non-x86");
1546 return 0;
1549 /* Create a temporary qcow2 image */
1550 close(mkstemp(tmp_path));
1551 mkqcow2(tmp_path, TEST_IMAGE_SIZE_MB);
1553 /* Create temporary blkdebug instructions */
1554 fd = mkstemp(debug_path);
1555 g_assert(fd >= 0);
1556 close(fd);
1558 /* Run the tests */
1559 qtest_add_func("/ahci/sanity", test_sanity);
1560 qtest_add_func("/ahci/pci_spec", test_pci_spec);
1561 qtest_add_func("/ahci/pci_enable", test_pci_enable);
1562 qtest_add_func("/ahci/hba_spec", test_hba_spec);
1563 qtest_add_func("/ahci/hba_enable", test_hba_enable);
1564 qtest_add_func("/ahci/identify", test_identify);
1566 for (i = MODE_BEGIN; i < NUM_MODES; i++) {
1567 for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
1568 for (k = LEN_BEGIN; k < NUM_LENGTHS; k++) {
1569 for (m = OFFSET_BEGIN; m < NUM_OFFSETS; m++) {
1570 create_ahci_io_test(i, j, k, m);
1576 qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented);
1578 qtest_add_func("/ahci/flush/simple", test_flush);
1579 qtest_add_func("/ahci/flush/retry", test_flush_retry);
1580 qtest_add_func("/ahci/flush/migrate", test_flush_migrate);
1582 qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity);
1583 qtest_add_func("/ahci/migrate/dma/simple", test_migrate_dma);
1584 qtest_add_func("/ahci/io/dma/lba28/retry", test_halted_dma);
1585 qtest_add_func("/ahci/migrate/dma/halted", test_migrate_halted_dma);
1587 ret = g_test_run();
1589 /* Cleanup */
1590 unlink(tmp_path);
1591 unlink(debug_path);
1593 return ret;