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