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