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