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