]> Git Repo - qemu.git/blob - tests/ahci-test.c
qtest/ahci: Allow override of default CLI options
[qemu.git] / tests / ahci-test.c
1 /*
2  * AHCI test cases
3  *
4  * Copyright (c) 2014 John Snow <[email protected]>
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 <stdint.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <glib.h>
30
31 #include "libqtest.h"
32 #include "libqos/libqos-pc.h"
33 #include "libqos/ahci.h"
34 #include "libqos/pci-pc.h"
35
36 #include "qemu-common.h"
37 #include "qemu/host-utils.h"
38
39 #include "hw/pci/pci_ids.h"
40 #include "hw/pci/pci_regs.h"
41
42 /* Test-specific defines -- in MiB */
43 #define TEST_IMAGE_SIZE_MB (200 * 1024)
44 #define TEST_IMAGE_SECTORS ((TEST_IMAGE_SIZE_MB / AHCI_SECTOR_SIZE)     \
45                             * 1024 * 1024)
46
47 /*** Globals ***/
48 static char tmp_path[] = "/tmp/qtest.XXXXXX";
49 static bool ahci_pedantic;
50
51 /*** Function Declarations ***/
52 static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port);
53 static void ahci_test_pci_spec(AHCIQState *ahci);
54 static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
55                                uint8_t offset);
56 static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset);
57 static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset);
58 static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset);
59
60 /*** Utilities ***/
61
62 static void string_bswap16(uint16_t *s, size_t bytes)
63 {
64     g_assert_cmphex((bytes & 1), ==, 0);
65     bytes /= 2;
66
67     while (bytes--) {
68         *s = bswap16(*s);
69         s++;
70     }
71 }
72
73 static void generate_pattern(void *buffer, size_t len, size_t cycle_len)
74 {
75     int i, j;
76     unsigned char *tx = (unsigned char *)buffer;
77     unsigned char p;
78     size_t *sx;
79
80     /* Write an indicative pattern that varies and is unique per-cycle */
81     p = rand() % 256;
82     for (i = j = 0; i < len; i++, j++) {
83         tx[i] = p;
84         if (j % cycle_len == 0) {
85             p = rand() % 256;
86         }
87     }
88
89     /* force uniqueness by writing an id per-cycle */
90     for (i = 0; i < len / cycle_len; i++) {
91         j = i * cycle_len;
92         if (j + sizeof(*sx) <= len) {
93             sx = (size_t *)&tx[j];
94             *sx = i;
95         }
96     }
97 }
98
99 /*** Test Setup & Teardown ***/
100
101 /**
102  * Start a Q35 machine and bookmark a handle to the AHCI device.
103  */
104 static AHCIQState *ahci_vboot(const char *cli, va_list ap)
105 {
106     AHCIQState *s;
107
108     s = g_malloc0(sizeof(AHCIQState));
109     s->parent = qtest_pc_vboot(cli, ap);
110     alloc_set_flags(s->parent->alloc, ALLOC_LEAK_ASSERT);
111
112     /* Verify that we have an AHCI device present. */
113     s->dev = get_ahci_device(&s->fingerprint);
114
115     return s;
116 }
117
118 /**
119  * Start a Q35 machine and bookmark a handle to the AHCI device.
120  */
121 static AHCIQState *ahci_boot(const char *cli, ...)
122 {
123     AHCIQState *s;
124     va_list ap;
125
126     if (cli) {
127         va_start(ap, cli);
128         s = ahci_vboot(cli, ap);
129         va_end(ap);
130     } else {
131         cli = "-drive if=none,id=drive0,file=%s,cache=writeback,serial=%s"
132             ",format=qcow2"
133             " -M q35 "
134             "-device ide-hd,drive=drive0 "
135             "-global ide-hd.ver=%s";
136         s = ahci_boot(cli, tmp_path, "testdisk", "version");
137     }
138
139     return s;
140 }
141
142 /**
143  * Clean up the PCI device, then terminate the QEMU instance.
144  */
145 static void ahci_shutdown(AHCIQState *ahci)
146 {
147     QOSState *qs = ahci->parent;
148     ahci_clean_mem(ahci);
149     free_ahci_device(ahci->dev);
150     g_free(ahci);
151     qtest_shutdown(qs);
152 }
153
154 /**
155  * Boot and fully enable the HBA device.
156  * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
157  */
158 static AHCIQState *ahci_boot_and_enable(const char *cli, ...)
159 {
160     AHCIQState *ahci;
161     va_list ap;
162
163     if (cli) {
164         va_start(ap, cli);
165         ahci = ahci_vboot(cli, ap);
166         va_end(ap);
167     } else {
168         ahci = ahci_boot(NULL);
169     }
170
171     ahci_pci_enable(ahci);
172     ahci_hba_enable(ahci);
173
174     return ahci;
175 }
176
177 /*** Specification Adherence Tests ***/
178
179 /**
180  * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
181  */
182 static void ahci_test_pci_spec(AHCIQState *ahci)
183 {
184     uint8_t datab;
185     uint16_t data;
186     uint32_t datal;
187
188     /* Most of these bits should start cleared until we turn them on. */
189     data = qpci_config_readw(ahci->dev, PCI_COMMAND);
190     ASSERT_BIT_CLEAR(data, PCI_COMMAND_MEMORY);
191     ASSERT_BIT_CLEAR(data, PCI_COMMAND_MASTER);
192     ASSERT_BIT_CLEAR(data, PCI_COMMAND_SPECIAL);     /* Reserved */
193     ASSERT_BIT_CLEAR(data, PCI_COMMAND_VGA_PALETTE); /* Reserved */
194     ASSERT_BIT_CLEAR(data, PCI_COMMAND_PARITY);
195     ASSERT_BIT_CLEAR(data, PCI_COMMAND_WAIT);        /* Reserved */
196     ASSERT_BIT_CLEAR(data, PCI_COMMAND_SERR);
197     ASSERT_BIT_CLEAR(data, PCI_COMMAND_FAST_BACK);
198     ASSERT_BIT_CLEAR(data, PCI_COMMAND_INTX_DISABLE);
199     ASSERT_BIT_CLEAR(data, 0xF800);                  /* Reserved */
200
201     data = qpci_config_readw(ahci->dev, PCI_STATUS);
202     ASSERT_BIT_CLEAR(data, 0x01 | 0x02 | 0x04);     /* Reserved */
203     ASSERT_BIT_CLEAR(data, PCI_STATUS_INTERRUPT);
204     ASSERT_BIT_SET(data, PCI_STATUS_CAP_LIST);      /* must be set */
205     ASSERT_BIT_CLEAR(data, PCI_STATUS_UDF);         /* Reserved */
206     ASSERT_BIT_CLEAR(data, PCI_STATUS_PARITY);
207     ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_TARGET_ABORT);
208     ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_TARGET_ABORT);
209     ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_MASTER_ABORT);
210     ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_SYSTEM_ERROR);
211     ASSERT_BIT_CLEAR(data, PCI_STATUS_DETECTED_PARITY);
212
213     /* RID occupies the low byte, CCs occupy the high three. */
214     datal = qpci_config_readl(ahci->dev, PCI_CLASS_REVISION);
215     if (ahci_pedantic) {
216         /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
217          * Though in practice this is likely seldom true. */
218         ASSERT_BIT_CLEAR(datal, 0xFF);
219     }
220
221     /* BCC *must* equal 0x01. */
222     g_assert_cmphex(PCI_BCC(datal), ==, 0x01);
223     if (PCI_SCC(datal) == 0x01) {
224         /* IDE */
225         ASSERT_BIT_SET(0x80000000, datal);
226         ASSERT_BIT_CLEAR(0x60000000, datal);
227     } else if (PCI_SCC(datal) == 0x04) {
228         /* RAID */
229         g_assert_cmphex(PCI_PI(datal), ==, 0);
230     } else if (PCI_SCC(datal) == 0x06) {
231         /* AHCI */
232         g_assert_cmphex(PCI_PI(datal), ==, 0x01);
233     } else {
234         g_assert_not_reached();
235     }
236
237     datab = qpci_config_readb(ahci->dev, PCI_CACHE_LINE_SIZE);
238     g_assert_cmphex(datab, ==, 0);
239
240     datab = qpci_config_readb(ahci->dev, PCI_LATENCY_TIMER);
241     g_assert_cmphex(datab, ==, 0);
242
243     /* Only the bottom 7 bits must be off. */
244     datab = qpci_config_readb(ahci->dev, PCI_HEADER_TYPE);
245     ASSERT_BIT_CLEAR(datab, 0x7F);
246
247     /* BIST is optional, but the low 7 bits must always start off regardless. */
248     datab = qpci_config_readb(ahci->dev, PCI_BIST);
249     ASSERT_BIT_CLEAR(datab, 0x7F);
250
251     /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
252     datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
253     g_assert_cmphex(datal, ==, 0);
254
255     qpci_config_writel(ahci->dev, PCI_BASE_ADDRESS_5, 0xFFFFFFFF);
256     datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
257     /* ABAR must be 32-bit, memory mapped, non-prefetchable and
258      * must be >= 512 bytes. To that end, bits 0-8 must be off. */
259     ASSERT_BIT_CLEAR(datal, 0xFF);
260
261     /* Capability list MUST be present, */
262     datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST);
263     /* But these bits are reserved. */
264     ASSERT_BIT_CLEAR(datal, ~0xFF);
265     g_assert_cmphex(datal, !=, 0);
266
267     /* Check specification adherence for capability extenstions. */
268     data = qpci_config_readw(ahci->dev, datal);
269
270     switch (ahci->fingerprint) {
271     case AHCI_INTEL_ICH9:
272         /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
273         g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_MSI);
274         break;
275     default:
276         /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
277         g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_PM);
278     }
279
280     ahci_test_pci_caps(ahci, data, (uint8_t)datal);
281
282     /* Reserved. */
283     datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST + 4);
284     g_assert_cmphex(datal, ==, 0);
285
286     /* IPIN might vary, but ILINE must be off. */
287     datab = qpci_config_readb(ahci->dev, PCI_INTERRUPT_LINE);
288     g_assert_cmphex(datab, ==, 0);
289 }
290
291 /**
292  * Test PCI capabilities for AHCI specification adherence.
293  */
294 static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
295                                uint8_t offset)
296 {
297     uint8_t cid = header & 0xFF;
298     uint8_t next = header >> 8;
299
300     g_test_message("CID: %02x; next: %02x", cid, next);
301
302     switch (cid) {
303     case PCI_CAP_ID_PM:
304         ahci_test_pmcap(ahci, offset);
305         break;
306     case PCI_CAP_ID_MSI:
307         ahci_test_msicap(ahci, offset);
308         break;
309     case PCI_CAP_ID_SATA:
310         ahci_test_satacap(ahci, offset);
311         break;
312
313     default:
314         g_test_message("Unknown CAP 0x%02x", cid);
315     }
316
317     if (next) {
318         ahci_test_pci_caps(ahci, qpci_config_readw(ahci->dev, next), next);
319     }
320 }
321
322 /**
323  * Test SATA PCI capabilitity for AHCI specification adherence.
324  */
325 static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset)
326 {
327     uint16_t dataw;
328     uint32_t datal;
329
330     g_test_message("Verifying SATACAP");
331
332     /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
333     dataw = qpci_config_readw(ahci->dev, offset + 2);
334     g_assert_cmphex(dataw, ==, 0x10);
335
336     /* Grab the SATACR1 register. */
337     datal = qpci_config_readw(ahci->dev, offset + 4);
338
339     switch (datal & 0x0F) {
340     case 0x04: /* BAR0 */
341     case 0x05: /* BAR1 */
342     case 0x06:
343     case 0x07:
344     case 0x08:
345     case 0x09: /* BAR5 */
346     case 0x0F: /* Immediately following SATACR1 in PCI config space. */
347         break;
348     default:
349         /* Invalid BARLOC for the Index Data Pair. */
350         g_assert_not_reached();
351     }
352
353     /* Reserved. */
354     g_assert_cmphex((datal >> 24), ==, 0x00);
355 }
356
357 /**
358  * Test MSI PCI capability for AHCI specification adherence.
359  */
360 static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset)
361 {
362     uint16_t dataw;
363     uint32_t datal;
364
365     g_test_message("Verifying MSICAP");
366
367     dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_FLAGS);
368     ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_ENABLE);
369     ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_QSIZE);
370     ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_RESERVED);
371
372     datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_LO);
373     g_assert_cmphex(datal, ==, 0);
374
375     if (dataw & PCI_MSI_FLAGS_64BIT) {
376         g_test_message("MSICAP is 64bit");
377         datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_HI);
378         g_assert_cmphex(datal, ==, 0);
379         dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_64);
380         g_assert_cmphex(dataw, ==, 0);
381     } else {
382         g_test_message("MSICAP is 32bit");
383         dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_32);
384         g_assert_cmphex(dataw, ==, 0);
385     }
386 }
387
388 /**
389  * Test Power Management PCI capability for AHCI specification adherence.
390  */
391 static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset)
392 {
393     uint16_t dataw;
394
395     g_test_message("Verifying PMCAP");
396
397     dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_PMC);
398     ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_PME_CLOCK);
399     ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_RESERVED);
400     ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D1);
401     ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D2);
402
403     dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_CTRL);
404     ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_STATE_MASK);
405     ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_RESERVED);
406     ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SEL_MASK);
407     ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SCALE_MASK);
408 }
409
410 static void ahci_test_hba_spec(AHCIQState *ahci)
411 {
412     unsigned i;
413     uint32_t reg;
414     uint32_t ports;
415     uint8_t nports_impl;
416     uint8_t maxports;
417
418     g_assert(ahci != NULL);
419
420     /*
421      * Note that the AHCI spec does expect the BIOS to set up a few things:
422      * CAP.SSS    - Support for staggered spin-up            (t/f)
423      * CAP.SMPS   - Support for mechanical presence switches (t/f)
424      * PI         - Ports Implemented                        (1-32)
425      * PxCMD.HPCP - Hot Plug Capable Port
426      * PxCMD.MPSP - Mechanical Presence Switch Present
427      * PxCMD.CPD  - Cold Presence Detection support
428      *
429      * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
430      * Foreach Port Implemented:
431      * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
432      * -PxCLB/U and PxFB/U are set to valid regions in memory
433      * -PxSUD is set to 1.
434      * -PxSSTS.DET is polled for presence; if detected, we continue:
435      * -PxSERR is cleared with 1's.
436      * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
437      *  the device is ready.
438      */
439
440     /* 1 CAP - Capabilities Register */
441     ahci->cap = ahci_rreg(ahci, AHCI_CAP);
442     ASSERT_BIT_CLEAR(ahci->cap, AHCI_CAP_RESERVED);
443
444     /* 2 GHC - Global Host Control */
445     reg = ahci_rreg(ahci, AHCI_GHC);
446     ASSERT_BIT_CLEAR(reg, AHCI_GHC_HR);
447     ASSERT_BIT_CLEAR(reg, AHCI_GHC_IE);
448     ASSERT_BIT_CLEAR(reg, AHCI_GHC_MRSM);
449     if (BITSET(ahci->cap, AHCI_CAP_SAM)) {
450         g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
451         ASSERT_BIT_SET(reg, AHCI_GHC_AE);
452     } else {
453         g_test_message("Supports AHCI/Legacy mix.");
454         ASSERT_BIT_CLEAR(reg, AHCI_GHC_AE);
455     }
456
457     /* 3 IS - Interrupt Status */
458     reg = ahci_rreg(ahci, AHCI_IS);
459     g_assert_cmphex(reg, ==, 0);
460
461     /* 4 PI - Ports Implemented */
462     ports = ahci_rreg(ahci, AHCI_PI);
463     /* Ports Implemented must be non-zero. */
464     g_assert_cmphex(ports, !=, 0);
465     /* Ports Implemented must be <= Number of Ports. */
466     nports_impl = ctpopl(ports);
467     g_assert_cmpuint(((AHCI_CAP_NP & ahci->cap) + 1), >=, nports_impl);
468
469     /* Ports must be within the proper range. Given a mapping of SIZE,
470      * 256 bytes are used for global HBA control, and the rest is used
471      * for ports data, at 0x80 bytes each. */
472     g_assert_cmphex(ahci->barsize, >, 0);
473     maxports = (ahci->barsize - HBA_DATA_REGION_SIZE) / HBA_PORT_DATA_SIZE;
474     /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
475     g_assert_cmphex((reg >> maxports), ==, 0);
476
477     /* 5 AHCI Version */
478     reg = ahci_rreg(ahci, AHCI_VS);
479     switch (reg) {
480     case AHCI_VERSION_0_95:
481     case AHCI_VERSION_1_0:
482     case AHCI_VERSION_1_1:
483     case AHCI_VERSION_1_2:
484     case AHCI_VERSION_1_3:
485         break;
486     default:
487         g_assert_not_reached();
488     }
489
490     /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
491     reg = ahci_rreg(ahci, AHCI_CCCCTL);
492     if (BITSET(ahci->cap, AHCI_CAP_CCCS)) {
493         ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_EN);
494         ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_RESERVED);
495         ASSERT_BIT_SET(reg, AHCI_CCCCTL_CC);
496         ASSERT_BIT_SET(reg, AHCI_CCCCTL_TV);
497     } else {
498         g_assert_cmphex(reg, ==, 0);
499     }
500
501     /* 7 CCC_PORTS */
502     reg = ahci_rreg(ahci, AHCI_CCCPORTS);
503     /* Must be zeroes initially regardless of CAP.CCCS */
504     g_assert_cmphex(reg, ==, 0);
505
506     /* 8 EM_LOC */
507     reg = ahci_rreg(ahci, AHCI_EMLOC);
508     if (BITCLR(ahci->cap, AHCI_CAP_EMS)) {
509         g_assert_cmphex(reg, ==, 0);
510     }
511
512     /* 9 EM_CTL */
513     reg = ahci_rreg(ahci, AHCI_EMCTL);
514     if (BITSET(ahci->cap, AHCI_CAP_EMS)) {
515         ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_STSMR);
516         ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLTM);
517         ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLRST);
518         ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_RESERVED);
519     } else {
520         g_assert_cmphex(reg, ==, 0);
521     }
522
523     /* 10 CAP2 -- Capabilities Extended */
524     ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
525     ASSERT_BIT_CLEAR(ahci->cap2, AHCI_CAP2_RESERVED);
526
527     /* 11 BOHC -- Bios/OS Handoff Control */
528     reg = ahci_rreg(ahci, AHCI_BOHC);
529     g_assert_cmphex(reg, ==, 0);
530
531     /* 12 -- 23: Reserved */
532     g_test_message("Verifying HBA reserved area is empty.");
533     for (i = AHCI_RESERVED; i < AHCI_NVMHCI; ++i) {
534         reg = ahci_rreg(ahci, i);
535         g_assert_cmphex(reg, ==, 0);
536     }
537
538     /* 24 -- 39: NVMHCI */
539     if (BITCLR(ahci->cap2, AHCI_CAP2_NVMP)) {
540         g_test_message("Verifying HBA/NVMHCI area is empty.");
541         for (i = AHCI_NVMHCI; i < AHCI_VENDOR; ++i) {
542             reg = ahci_rreg(ahci, i);
543             g_assert_cmphex(reg, ==, 0);
544         }
545     }
546
547     /* 40 -- 63: Vendor */
548     g_test_message("Verifying HBA/Vendor area is empty.");
549     for (i = AHCI_VENDOR; i < AHCI_PORTS; ++i) {
550         reg = ahci_rreg(ahci, i);
551         g_assert_cmphex(reg, ==, 0);
552     }
553
554     /* 64 -- XX: Port Space */
555     for (i = 0; ports || (i < maxports); ports >>= 1, ++i) {
556         if (BITSET(ports, 0x1)) {
557             g_test_message("Testing port %u for spec", i);
558             ahci_test_port_spec(ahci, i);
559         } else {
560             uint16_t j;
561             uint16_t low = AHCI_PORTS + (32 * i);
562             uint16_t high = AHCI_PORTS + (32 * (i + 1));
563             g_test_message("Asserting unimplemented port %u "
564                            "(reg [%u-%u]) is empty.",
565                            i, low, high - 1);
566             for (j = low; j < high; ++j) {
567                 reg = ahci_rreg(ahci, j);
568                 g_assert_cmphex(reg, ==, 0);
569             }
570         }
571     }
572 }
573
574 /**
575  * Test the memory space for one port for specification adherence.
576  */
577 static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
578 {
579     uint32_t reg;
580     unsigned i;
581
582     /* (0) CLB */
583     reg = ahci_px_rreg(ahci, port, AHCI_PX_CLB);
584     ASSERT_BIT_CLEAR(reg, AHCI_PX_CLB_RESERVED);
585
586     /* (1) CLBU */
587     if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
588         reg = ahci_px_rreg(ahci, port, AHCI_PX_CLBU);
589         g_assert_cmphex(reg, ==, 0);
590     }
591
592     /* (2) FB */
593     reg = ahci_px_rreg(ahci, port, AHCI_PX_FB);
594     ASSERT_BIT_CLEAR(reg, AHCI_PX_FB_RESERVED);
595
596     /* (3) FBU */
597     if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
598         reg = ahci_px_rreg(ahci, port, AHCI_PX_FBU);
599         g_assert_cmphex(reg, ==, 0);
600     }
601
602     /* (4) IS */
603     reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
604     g_assert_cmphex(reg, ==, 0);
605
606     /* (5) IE */
607     reg = ahci_px_rreg(ahci, port, AHCI_PX_IE);
608     g_assert_cmphex(reg, ==, 0);
609
610     /* (6) CMD */
611     reg = ahci_px_rreg(ahci, port, AHCI_PX_CMD);
612     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FRE);
613     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_RESERVED);
614     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CCS);
615     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
616     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
617     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_PMA); /* And RW only if CAP.SPM */
618     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_APSTE); /* RW only if CAP2.APST */
619     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ATAPI);
620     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_DLAE);
621     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ALPE);  /* RW only if CAP.SALP */
622     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ASP);   /* RW only if CAP.SALP */
623     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ICC);
624     /* If CPDetect support does not exist, CPState must be off. */
625     if (BITCLR(reg, AHCI_PX_CMD_CPD)) {
626         ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CPS);
627     }
628     /* If MPSPresence is not set, MPSState must be off. */
629     if (BITCLR(reg, AHCI_PX_CMD_MPSP)) {
630         ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
631     }
632     /* If we do not support MPS, MPSS and MPSP must be off. */
633     if (BITCLR(ahci->cap, AHCI_CAP_SMPS)) {
634         ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
635         ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSP);
636     }
637     /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
638     if (BITANY(reg, AHCI_PX_CMD_CPD | AHCI_PX_CMD_MPSP)) {
639         ASSERT_BIT_SET(reg, AHCI_PX_CMD_HPCP);
640     }
641     /* HPCP and ESP cannot both be active. */
642     g_assert(!BITSET(reg, AHCI_PX_CMD_HPCP | AHCI_PX_CMD_ESP));
643     /* If CAP.FBSS is not set, FBSCP must not be set. */
644     if (BITCLR(ahci->cap, AHCI_CAP_FBSS)) {
645         ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FBSCP);
646     }
647
648     /* (7) RESERVED */
649     reg = ahci_px_rreg(ahci, port, AHCI_PX_RES1);
650     g_assert_cmphex(reg, ==, 0);
651
652     /* (8) TFD */
653     reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
654     /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
655      * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
656     ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_ERR);
657     ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS1);
658     ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_DRQ);
659     ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS2);
660     ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
661     ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
662     ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_RESERVED);
663
664     /* (9) SIG */
665     /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
666      * Even when GHC.ST is zero, the AHCI HBA may receive the initial
667      * D2H register FIS and update the signature asynchronously,
668      * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
669
670     /* (10) SSTS / SCR0: SStatus */
671     reg = ahci_px_rreg(ahci, port, AHCI_PX_SSTS);
672     ASSERT_BIT_CLEAR(reg, AHCI_PX_SSTS_RESERVED);
673     /* Even though the register should be 0 at boot, it is asynchronous and
674      * prone to change, so we cannot test any well known value. */
675
676     /* (11) SCTL / SCR2: SControl */
677     reg = ahci_px_rreg(ahci, port, AHCI_PX_SCTL);
678     g_assert_cmphex(reg, ==, 0);
679
680     /* (12) SERR / SCR1: SError */
681     reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
682     g_assert_cmphex(reg, ==, 0);
683
684     /* (13) SACT / SCR3: SActive */
685     reg = ahci_px_rreg(ahci, port, AHCI_PX_SACT);
686     g_assert_cmphex(reg, ==, 0);
687
688     /* (14) CI */
689     reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
690     g_assert_cmphex(reg, ==, 0);
691
692     /* (15) SNTF */
693     reg = ahci_px_rreg(ahci, port, AHCI_PX_SNTF);
694     g_assert_cmphex(reg, ==, 0);
695
696     /* (16) FBS */
697     reg = ahci_px_rreg(ahci, port, AHCI_PX_FBS);
698     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_EN);
699     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEC);
700     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_SDE);
701     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEV);
702     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DWE);
703     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_RESERVED);
704     if (BITSET(ahci->cap, AHCI_CAP_FBSS)) {
705         /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
706         g_assert((reg & AHCI_PX_FBS_ADO) >> ctzl(AHCI_PX_FBS_ADO) >= 2);
707     }
708
709     /* [17 -- 27] RESERVED */
710     for (i = AHCI_PX_RES2; i < AHCI_PX_VS; ++i) {
711         reg = ahci_px_rreg(ahci, port, i);
712         g_assert_cmphex(reg, ==, 0);
713     }
714
715     /* [28 -- 31] Vendor-Specific */
716     for (i = AHCI_PX_VS; i < 32; ++i) {
717         reg = ahci_px_rreg(ahci, port, i);
718         if (reg) {
719             g_test_message("INFO: Vendor register %u non-empty", i);
720         }
721     }
722 }
723
724 /**
725  * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
726  * device we see, then read and check the response.
727  */
728 static void ahci_test_identify(AHCIQState *ahci)
729 {
730     uint16_t buff[256];
731     unsigned px;
732     int rc;
733     uint16_t sect_size;
734     const size_t buffsize = 512;
735
736     g_assert(ahci != NULL);
737
738     /**
739      * This serves as a bit of a tutorial on AHCI device programming:
740      *
741      * (1) Create a data buffer for the IDENTIFY response to be sent to
742      * (2) Create a Command Table buffer, where we will store the
743      *     command and PRDT (Physical Region Descriptor Table)
744      * (3) Construct an FIS host-to-device command structure, and write it to
745      *     the top of the Command Table buffer.
746      * (4) Create one or more Physical Region Descriptors (PRDs) that describe
747      *     a location in memory where data may be stored/retrieved.
748      * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
749      * (6) Each AHCI port has up to 32 command slots. Each slot contains a
750      *     header that points to a Command Table buffer. Pick an unused slot
751      *     and update it to point to the Command Table we have built.
752      * (7) Now: Command #n points to our Command Table, and our Command Table
753      *     contains the FIS (that describes our command) and the PRDTL, which
754      *     describes our buffer.
755      * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
756      *     #n is ready for processing.
757      */
758
759     /* Pick the first implemented and running port */
760     px = ahci_port_select(ahci);
761     g_test_message("Selected port %u for test", px);
762
763     /* Clear out the FIS Receive area and any pending interrupts. */
764     ahci_port_clear(ahci, px);
765
766     /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
767     ahci_io(ahci, px, CMD_IDENTIFY, &buff, buffsize, 0);
768
769     /* Check serial number/version in the buffer */
770     /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
771      * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
772      * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
773      * as a consequence, only needs to unchunk the data on LE machines. */
774     string_bswap16(&buff[10], 20);
775     rc = memcmp(&buff[10], "testdisk            ", 20);
776     g_assert_cmphex(rc, ==, 0);
777
778     string_bswap16(&buff[23], 8);
779     rc = memcmp(&buff[23], "version ", 8);
780     g_assert_cmphex(rc, ==, 0);
781
782     sect_size = le16_to_cpu(*((uint16_t *)(&buff[5])));
783     g_assert_cmphex(sect_size, ==, AHCI_SECTOR_SIZE);
784 }
785
786 static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,
787                                    uint64_t sector, uint8_t read_cmd,
788                                    uint8_t write_cmd)
789 {
790     uint64_t ptr;
791     uint8_t port;
792     unsigned char *tx = g_malloc(bufsize);
793     unsigned char *rx = g_malloc0(bufsize);
794
795     g_assert(ahci != NULL);
796
797     /* Pick the first running port and clear it. */
798     port = ahci_port_select(ahci);
799     ahci_port_clear(ahci, port);
800
801     /*** Create pattern and transfer to guest ***/
802     /* Data buffer in the guest */
803     ptr = ahci_alloc(ahci, bufsize);
804     g_assert(ptr);
805
806     /* Write some indicative pattern to our buffer. */
807     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
808     memwrite(ptr, tx, bufsize);
809
810     /* Write this buffer to disk, then read it back to the DMA buffer. */
811     ahci_guest_io(ahci, port, write_cmd, ptr, bufsize, sector);
812     qmemset(ptr, 0x00, bufsize);
813     ahci_guest_io(ahci, port, read_cmd, ptr, bufsize, sector);
814
815     /*** Read back the Data ***/
816     memread(ptr, rx, bufsize);
817     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
818
819     ahci_free(ahci, ptr);
820     g_free(tx);
821     g_free(rx);
822 }
823
824 static void ahci_test_nondata(AHCIQState *ahci, uint8_t ide_cmd)
825 {
826     uint8_t px;
827     AHCICommand *cmd;
828
829     /* Sanitize */
830     px = ahci_port_select(ahci);
831     ahci_port_clear(ahci, px);
832
833     /* Issue Command */
834     cmd = ahci_command_create(ide_cmd);
835     ahci_command_commit(ahci, cmd, px);
836     ahci_command_issue(ahci, cmd);
837     ahci_command_verify(ahci, cmd);
838     ahci_command_free(cmd);
839 }
840
841 static void ahci_test_flush(AHCIQState *ahci)
842 {
843     ahci_test_nondata(ahci, CMD_FLUSH_CACHE);
844 }
845
846
847 /******************************************************************************/
848 /* Test Interfaces                                                            */
849 /******************************************************************************/
850
851 /**
852  * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
853  */
854 static void test_sanity(void)
855 {
856     AHCIQState *ahci;
857     ahci = ahci_boot(NULL);
858     ahci_shutdown(ahci);
859 }
860
861 /**
862  * Ensure that the PCI configuration space for the AHCI device is in-line with
863  * the AHCI 1.3 specification for initial values.
864  */
865 static void test_pci_spec(void)
866 {
867     AHCIQState *ahci;
868     ahci = ahci_boot(NULL);
869     ahci_test_pci_spec(ahci);
870     ahci_shutdown(ahci);
871 }
872
873 /**
874  * Engage the PCI AHCI device and sanity check the response.
875  * Perform additional PCI config space bringup for the HBA.
876  */
877 static void test_pci_enable(void)
878 {
879     AHCIQState *ahci;
880     ahci = ahci_boot(NULL);
881     ahci_pci_enable(ahci);
882     ahci_shutdown(ahci);
883 }
884
885 /**
886  * Investigate the memory mapped regions of the HBA,
887  * and test them for AHCI specification adherence.
888  */
889 static void test_hba_spec(void)
890 {
891     AHCIQState *ahci;
892
893     ahci = ahci_boot(NULL);
894     ahci_pci_enable(ahci);
895     ahci_test_hba_spec(ahci);
896     ahci_shutdown(ahci);
897 }
898
899 /**
900  * Engage the HBA functionality of the AHCI PCI device,
901  * and bring it into a functional idle state.
902  */
903 static void test_hba_enable(void)
904 {
905     AHCIQState *ahci;
906
907     ahci = ahci_boot(NULL);
908     ahci_pci_enable(ahci);
909     ahci_hba_enable(ahci);
910     ahci_shutdown(ahci);
911 }
912
913 /**
914  * Bring up the device and issue an IDENTIFY command.
915  * Inspect the state of the HBA device and the data returned.
916  */
917 static void test_identify(void)
918 {
919     AHCIQState *ahci;
920
921     ahci = ahci_boot_and_enable(NULL);
922     ahci_test_identify(ahci);
923     ahci_shutdown(ahci);
924 }
925
926 /**
927  * Fragmented DMA test: Perform a standard 4K DMA read/write
928  * test, but make sure the physical regions are fragmented to
929  * be very small, each just 32 bytes, to see how AHCI performs
930  * with chunks defined to be much less than a sector.
931  */
932 static void test_dma_fragmented(void)
933 {
934     AHCIQState *ahci;
935     AHCICommand *cmd;
936     uint8_t px;
937     size_t bufsize = 4096;
938     unsigned char *tx = g_malloc(bufsize);
939     unsigned char *rx = g_malloc0(bufsize);
940     uint64_t ptr;
941
942     ahci = ahci_boot_and_enable(NULL);
943     px = ahci_port_select(ahci);
944     ahci_port_clear(ahci, px);
945
946     /* create pattern */
947     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
948
949     /* Create a DMA buffer in guest memory, and write our pattern to it. */
950     ptr = guest_alloc(ahci->parent->alloc, bufsize);
951     g_assert(ptr);
952     memwrite(ptr, tx, bufsize);
953
954     cmd = ahci_command_create(CMD_WRITE_DMA);
955     ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
956     ahci_command_commit(ahci, cmd, px);
957     ahci_command_issue(ahci, cmd);
958     ahci_command_verify(ahci, cmd);
959     g_free(cmd);
960
961     cmd = ahci_command_create(CMD_READ_DMA);
962     ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
963     ahci_command_commit(ahci, cmd, px);
964     ahci_command_issue(ahci, cmd);
965     ahci_command_verify(ahci, cmd);
966     g_free(cmd);
967
968     /* Read back the guest's receive buffer into local memory */
969     memread(ptr, rx, bufsize);
970     guest_free(ahci->parent->alloc, ptr);
971
972     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
973
974     ahci_shutdown(ahci);
975
976     g_free(rx);
977     g_free(tx);
978 }
979
980 static void test_flush(void)
981 {
982     AHCIQState *ahci;
983
984     ahci = ahci_boot_and_enable(NULL);
985     ahci_test_flush(ahci);
986     ahci_shutdown(ahci);
987 }
988
989 /******************************************************************************/
990 /* AHCI I/O Test Matrix Definitions                                           */
991
992 enum BuffLen {
993     LEN_BEGIN = 0,
994     LEN_SIMPLE = LEN_BEGIN,
995     LEN_DOUBLE,
996     LEN_LONG,
997     LEN_SHORT,
998     NUM_LENGTHS
999 };
1000
1001 static const char *buff_len_str[NUM_LENGTHS] = { "simple", "double",
1002                                                  "long", "short" };
1003
1004 enum AddrMode {
1005     ADDR_MODE_BEGIN = 0,
1006     ADDR_MODE_LBA28 = ADDR_MODE_BEGIN,
1007     ADDR_MODE_LBA48,
1008     NUM_ADDR_MODES
1009 };
1010
1011 static const char *addr_mode_str[NUM_ADDR_MODES] = { "lba28", "lba48" };
1012
1013 enum IOMode {
1014     MODE_BEGIN = 0,
1015     MODE_PIO = MODE_BEGIN,
1016     MODE_DMA,
1017     NUM_MODES
1018 };
1019
1020 static const char *io_mode_str[NUM_MODES] = { "pio", "dma" };
1021
1022 enum IOOps {
1023     IO_BEGIN = 0,
1024     IO_READ = IO_BEGIN,
1025     IO_WRITE,
1026     NUM_IO_OPS
1027 };
1028
1029 enum OffsetType {
1030     OFFSET_BEGIN = 0,
1031     OFFSET_ZERO = OFFSET_BEGIN,
1032     OFFSET_LOW,
1033     OFFSET_HIGH,
1034     NUM_OFFSETS
1035 };
1036
1037 static const char *offset_str[NUM_OFFSETS] = { "zero", "low", "high" };
1038
1039 typedef struct AHCIIOTestOptions {
1040     enum BuffLen length;
1041     enum AddrMode address_type;
1042     enum IOMode io_type;
1043     enum OffsetType offset;
1044 } AHCIIOTestOptions;
1045
1046 static uint64_t offset_sector(enum OffsetType ofst,
1047                               enum AddrMode addr_type,
1048                               uint64_t buffsize)
1049 {
1050     uint64_t ceil;
1051     uint64_t nsectors;
1052
1053     switch (ofst) {
1054     case OFFSET_ZERO:
1055         return 0;
1056     case OFFSET_LOW:
1057         return 1;
1058     case OFFSET_HIGH:
1059         ceil = (addr_type == ADDR_MODE_LBA28) ? 0xfffffff : 0xffffffffffff;
1060         ceil = MIN(ceil, TEST_IMAGE_SECTORS - 1);
1061         nsectors = buffsize / AHCI_SECTOR_SIZE;
1062         return ceil - nsectors + 1;
1063     default:
1064         g_assert_not_reached();
1065     }
1066 }
1067
1068 /**
1069  * Table of possible I/O ATA commands given a set of enumerations.
1070  */
1071 static const uint8_t io_cmds[NUM_MODES][NUM_ADDR_MODES][NUM_IO_OPS] = {
1072     [MODE_PIO] = {
1073         [ADDR_MODE_LBA28] = {
1074             [IO_READ] = CMD_READ_PIO,
1075             [IO_WRITE] = CMD_WRITE_PIO },
1076         [ADDR_MODE_LBA48] = {
1077             [IO_READ] = CMD_READ_PIO_EXT,
1078             [IO_WRITE] = CMD_WRITE_PIO_EXT }
1079     },
1080     [MODE_DMA] = {
1081         [ADDR_MODE_LBA28] = {
1082             [IO_READ] = CMD_READ_DMA,
1083             [IO_WRITE] = CMD_WRITE_DMA },
1084         [ADDR_MODE_LBA48] = {
1085             [IO_READ] = CMD_READ_DMA_EXT,
1086             [IO_WRITE] = CMD_WRITE_DMA_EXT }
1087     }
1088 };
1089
1090 /**
1091  * Test a Read/Write pattern using various commands, addressing modes,
1092  * transfer modes, and buffer sizes.
1093  */
1094 static void test_io_rw_interface(enum AddrMode lba48, enum IOMode dma,
1095                                  unsigned bufsize, uint64_t sector)
1096 {
1097     AHCIQState *ahci;
1098
1099     ahci = ahci_boot_and_enable(NULL);
1100     ahci_test_io_rw_simple(ahci, bufsize, sector,
1101                            io_cmds[dma][lba48][IO_READ],
1102                            io_cmds[dma][lba48][IO_WRITE]);
1103     ahci_shutdown(ahci);
1104 }
1105
1106 /**
1107  * Demultiplex the test data and invoke the actual test routine.
1108  */
1109 static void test_io_interface(gconstpointer opaque)
1110 {
1111     AHCIIOTestOptions *opts = (AHCIIOTestOptions *)opaque;
1112     unsigned bufsize;
1113     uint64_t sector;
1114
1115     switch (opts->length) {
1116     case LEN_SIMPLE:
1117         bufsize = 4096;
1118         break;
1119     case LEN_DOUBLE:
1120         bufsize = 8192;
1121         break;
1122     case LEN_LONG:
1123         bufsize = 4096 * 64;
1124         break;
1125     case LEN_SHORT:
1126         bufsize = 512;
1127         break;
1128     default:
1129         g_assert_not_reached();
1130     }
1131
1132     sector = offset_sector(opts->offset, opts->address_type, bufsize);
1133     test_io_rw_interface(opts->address_type, opts->io_type, bufsize, sector);
1134     g_free(opts);
1135     return;
1136 }
1137
1138 static void create_ahci_io_test(enum IOMode type, enum AddrMode addr,
1139                                 enum BuffLen len, enum OffsetType offset)
1140 {
1141     static const char *arch;
1142     char *name;
1143     AHCIIOTestOptions *opts = g_malloc(sizeof(AHCIIOTestOptions));
1144
1145     opts->length = len;
1146     opts->address_type = addr;
1147     opts->io_type = type;
1148     opts->offset = offset;
1149
1150     if (!arch) {
1151         arch = qtest_get_arch();
1152     }
1153
1154     name = g_strdup_printf("/%s/ahci/io/%s/%s/%s/%s", arch,
1155                            io_mode_str[type],
1156                            addr_mode_str[addr],
1157                            buff_len_str[len],
1158                            offset_str[offset]);
1159
1160     g_test_add_data_func(name, opts, test_io_interface);
1161     g_free(name);
1162 }
1163
1164 /******************************************************************************/
1165
1166 int main(int argc, char **argv)
1167 {
1168     const char *arch;
1169     int ret;
1170     int c;
1171     int i, j, k, m;
1172
1173     static struct option long_options[] = {
1174         {"pedantic", no_argument, 0, 'p' },
1175         {0, 0, 0, 0},
1176     };
1177
1178     /* Should be first to utilize g_test functionality, So we can see errors. */
1179     g_test_init(&argc, &argv, NULL);
1180
1181     while (1) {
1182         c = getopt_long(argc, argv, "", long_options, NULL);
1183         if (c == -1) {
1184             break;
1185         }
1186         switch (c) {
1187         case -1:
1188             break;
1189         case 'p':
1190             ahci_pedantic = 1;
1191             break;
1192         default:
1193             fprintf(stderr, "Unrecognized ahci_test option.\n");
1194             g_assert_not_reached();
1195         }
1196     }
1197
1198     /* Check architecture */
1199     arch = qtest_get_arch();
1200     if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
1201         g_test_message("Skipping test for non-x86");
1202         return 0;
1203     }
1204
1205     /* Create a temporary qcow2 image */
1206     close(mkstemp(tmp_path));
1207     mkqcow2(tmp_path, TEST_IMAGE_SIZE_MB);
1208
1209     /* Run the tests */
1210     qtest_add_func("/ahci/sanity",     test_sanity);
1211     qtest_add_func("/ahci/pci_spec",   test_pci_spec);
1212     qtest_add_func("/ahci/pci_enable", test_pci_enable);
1213     qtest_add_func("/ahci/hba_spec",   test_hba_spec);
1214     qtest_add_func("/ahci/hba_enable", test_hba_enable);
1215     qtest_add_func("/ahci/identify",   test_identify);
1216
1217     for (i = MODE_BEGIN; i < NUM_MODES; i++) {
1218         for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
1219             for (k = LEN_BEGIN; k < NUM_LENGTHS; k++) {
1220                 for (m = OFFSET_BEGIN; m < NUM_OFFSETS; m++) {
1221                     create_ahci_io_test(i, j, k, m);
1222                 }
1223             }
1224         }
1225     }
1226
1227     qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented);
1228
1229     qtest_add_func("/ahci/flush/simple", test_flush);
1230
1231     ret = g_test_run();
1232
1233     /* Cleanup */
1234     unlink(tmp_path);
1235
1236     return ret;
1237 }
This page took 0.090195 seconds and 4 git commands to generate.