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