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