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