]>
Commit | Line | Data |
---|---|---|
acbe4801 KW |
1 | /* |
2 | * IDE test cases | |
3 | * | |
4 | * Copyright (c) 2013 Kevin Wolf <[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" |
acbe4801 | 26 | |
acbe4801 KW |
27 | |
28 | #include "libqtest.h" | |
72c85e94 | 29 | #include "libqos/libqos.h" |
b95739dc KW |
30 | #include "libqos/pci-pc.h" |
31 | #include "libqos/malloc-pc.h" | |
055a1efc | 32 | #include "qapi/qmp/qdict.h" |
acbe4801 | 33 | #include "qemu-common.h" |
58369e22 | 34 | #include "qemu/bswap.h" |
b95739dc KW |
35 | #include "hw/pci/pci_ids.h" |
36 | #include "hw/pci/pci_regs.h" | |
acbe4801 | 37 | |
055a1efc | 38 | /* TODO actually test the results and get rid of this */ |
4a61c3ab | 39 | #define qmp_discard_response(q, ...) qobject_unref(qtest_qmp(q, __VA_ARGS__)) |
055a1efc | 40 | |
acbe4801 KW |
41 | #define TEST_IMAGE_SIZE 64 * 1024 * 1024 |
42 | ||
43 | #define IDE_PCI_DEV 1 | |
44 | #define IDE_PCI_FUNC 1 | |
45 | ||
46 | #define IDE_BASE 0x1f0 | |
47 | #define IDE_PRIMARY_IRQ 14 | |
48 | ||
f7ba8d7f JS |
49 | #define ATAPI_BLOCK_SIZE 2048 |
50 | ||
51 | /* How many bytes to receive via ATAPI PIO at one time. | |
52 | * Must be less than 0xFFFF. */ | |
53 | #define BYTE_COUNT_LIMIT 5120 | |
54 | ||
acbe4801 KW |
55 | enum { |
56 | reg_data = 0x0, | |
00ea63fd | 57 | reg_feature = 0x1, |
29e1d473 | 58 | reg_error = 0x1, |
acbe4801 KW |
59 | reg_nsectors = 0x2, |
60 | reg_lba_low = 0x3, | |
61 | reg_lba_middle = 0x4, | |
62 | reg_lba_high = 0x5, | |
63 | reg_device = 0x6, | |
64 | reg_status = 0x7, | |
65 | reg_command = 0x7, | |
66 | }; | |
67 | ||
68 | enum { | |
69 | BSY = 0x80, | |
70 | DRDY = 0x40, | |
71 | DF = 0x20, | |
72 | DRQ = 0x08, | |
73 | ERR = 0x01, | |
74 | }; | |
75 | ||
29e1d473 AN |
76 | /* Error field */ |
77 | enum { | |
78 | ABRT = 0x04, | |
79 | }; | |
80 | ||
acbe4801 | 81 | enum { |
c27d5656 | 82 | DEV = 0x10, |
b95739dc KW |
83 | LBA = 0x40, |
84 | }; | |
85 | ||
86 | enum { | |
87 | bmreg_cmd = 0x0, | |
88 | bmreg_status = 0x2, | |
89 | bmreg_prdt = 0x4, | |
90 | }; | |
91 | ||
92 | enum { | |
29e1d473 | 93 | CMD_DSM = 0x06, |
b95739dc KW |
94 | CMD_READ_DMA = 0xc8, |
95 | CMD_WRITE_DMA = 0xca, | |
bd07684a | 96 | CMD_FLUSH_CACHE = 0xe7, |
acbe4801 | 97 | CMD_IDENTIFY = 0xec, |
f7ba8d7f | 98 | CMD_PACKET = 0xa0, |
948eaed1 KW |
99 | |
100 | CMDF_ABORT = 0x100, | |
d7b7e580 | 101 | CMDF_NO_BM = 0x200, |
acbe4801 KW |
102 | }; |
103 | ||
b95739dc KW |
104 | enum { |
105 | BM_CMD_START = 0x1, | |
106 | BM_CMD_WRITE = 0x8, /* write = from device to memory */ | |
107 | }; | |
108 | ||
109 | enum { | |
110 | BM_STS_ACTIVE = 0x1, | |
111 | BM_STS_ERROR = 0x2, | |
112 | BM_STS_INTR = 0x4, | |
113 | }; | |
114 | ||
115 | enum { | |
116 | PRDT_EOT = 0x80000000, | |
117 | }; | |
118 | ||
acbe4801 KW |
119 | #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask)) |
120 | #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0) | |
121 | ||
b95739dc | 122 | static QPCIBus *pcibus = NULL; |
eb5937ba | 123 | static QGuestAllocator guest_malloc; |
b95739dc | 124 | |
acbe4801 | 125 | static char tmp_path[] = "/tmp/qtest.XXXXXX"; |
14a92e5f | 126 | static char debug_path[] = "/tmp/qtest-blkdebug.XXXXXX"; |
acbe4801 | 127 | |
4a61c3ab | 128 | static QTestState *ide_test_start(const char *cmdline_fmt, ...) |
acbe4801 | 129 | { |
4a61c3ab | 130 | QTestState *qts; |
acbe4801 | 131 | va_list ap; |
acbe4801 KW |
132 | |
133 | va_start(ap, cmdline_fmt); | |
4a61c3ab | 134 | qts = qtest_vinitf(cmdline_fmt, ap); |
acbe4801 KW |
135 | va_end(ap); |
136 | ||
4a61c3ab | 137 | pc_alloc_init(&guest_malloc, qts, 0); |
e42de189 | 138 | |
4a61c3ab | 139 | return qts; |
acbe4801 KW |
140 | } |
141 | ||
4a61c3ab | 142 | static void ide_test_quit(QTestState *qts) |
acbe4801 | 143 | { |
3b6b0a8a TH |
144 | if (pcibus) { |
145 | qpci_free_pc(pcibus); | |
146 | pcibus = NULL; | |
147 | } | |
eb5937ba | 148 | alloc_destroy(&guest_malloc); |
4a61c3ab | 149 | qtest_quit(qts); |
acbe4801 KW |
150 | } |
151 | ||
4a61c3ab TH |
152 | static QPCIDevice *get_pci_device(QTestState *qts, QPCIBar *bmdma_bar, |
153 | QPCIBar *ide_bar) | |
b95739dc KW |
154 | { |
155 | QPCIDevice *dev; | |
156 | uint16_t vendor_id, device_id; | |
157 | ||
158 | if (!pcibus) { | |
4a61c3ab | 159 | pcibus = qpci_new_pc(qts, NULL); |
b95739dc KW |
160 | } |
161 | ||
162 | /* Find PCI device and verify it's the right one */ | |
163 | dev = qpci_device_find(pcibus, QPCI_DEVFN(IDE_PCI_DEV, IDE_PCI_FUNC)); | |
164 | g_assert(dev != NULL); | |
165 | ||
166 | vendor_id = qpci_config_readw(dev, PCI_VENDOR_ID); | |
167 | device_id = qpci_config_readw(dev, PCI_DEVICE_ID); | |
168 | g_assert(vendor_id == PCI_VENDOR_ID_INTEL); | |
169 | g_assert(device_id == PCI_DEVICE_ID_INTEL_82371SB_1); | |
170 | ||
171 | /* Map bmdma BAR */ | |
b4ba67d9 | 172 | *bmdma_bar = qpci_iomap(dev, 4, NULL); |
9c268f8a | 173 | |
b4ba67d9 | 174 | *ide_bar = qpci_legacy_iomap(dev, IDE_BASE); |
b95739dc KW |
175 | |
176 | qpci_device_enable(dev); | |
177 | ||
178 | return dev; | |
179 | } | |
180 | ||
181 | static void free_pci_device(QPCIDevice *dev) | |
182 | { | |
183 | /* libqos doesn't have a function for this, so free it manually */ | |
184 | g_free(dev); | |
185 | } | |
186 | ||
187 | typedef struct PrdtEntry { | |
188 | uint32_t addr; | |
189 | uint32_t size; | |
190 | } QEMU_PACKED PrdtEntry; | |
191 | ||
192 | #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask)) | |
193 | #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0) | |
194 | ||
29e1d473 AN |
195 | static uint64_t trim_range_le(uint64_t sector, uint16_t count) |
196 | { | |
197 | /* 2-byte range, 6-byte LBA */ | |
198 | return cpu_to_le64(((uint64_t)count << 48) + sector); | |
199 | } | |
200 | ||
4a61c3ab TH |
201 | static int send_dma_request(QTestState *qts, int cmd, uint64_t sector, |
202 | int nb_sectors, PrdtEntry *prdt, int prdt_entries, | |
b4ba67d9 | 203 | void(*post_exec)(QPCIDevice *dev, QPCIBar ide_bar, |
9c268f8a | 204 | uint64_t sector, int nb_sectors)) |
b95739dc KW |
205 | { |
206 | QPCIDevice *dev; | |
b4ba67d9 | 207 | QPCIBar bmdma_bar, ide_bar; |
b95739dc KW |
208 | uintptr_t guest_prdt; |
209 | size_t len; | |
210 | bool from_dev; | |
211 | uint8_t status; | |
948eaed1 | 212 | int flags; |
b95739dc | 213 | |
4a61c3ab | 214 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); |
b95739dc | 215 | |
948eaed1 KW |
216 | flags = cmd & ~0xff; |
217 | cmd &= 0xff; | |
218 | ||
b95739dc KW |
219 | switch (cmd) { |
220 | case CMD_READ_DMA: | |
00ea63fd JS |
221 | case CMD_PACKET: |
222 | /* Assuming we only test data reads w/ ATAPI, otherwise we need to know | |
223 | * the SCSI command being sent in the packet, too. */ | |
b95739dc KW |
224 | from_dev = true; |
225 | break; | |
29e1d473 | 226 | case CMD_DSM: |
b95739dc KW |
227 | case CMD_WRITE_DMA: |
228 | from_dev = false; | |
229 | break; | |
230 | default: | |
231 | g_assert_not_reached(); | |
232 | } | |
233 | ||
d7b7e580 KW |
234 | if (flags & CMDF_NO_BM) { |
235 | qpci_config_writew(dev, PCI_COMMAND, | |
236 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY); | |
237 | } | |
238 | ||
b95739dc | 239 | /* Select device 0 */ |
b4ba67d9 | 240 | qpci_io_writeb(dev, ide_bar, reg_device, 0 | LBA); |
b95739dc KW |
241 | |
242 | /* Stop any running transfer, clear any pending interrupt */ | |
b4ba67d9 DG |
243 | qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, 0); |
244 | qpci_io_writeb(dev, bmdma_bar, bmreg_status, BM_STS_INTR); | |
b95739dc KW |
245 | |
246 | /* Setup PRDT */ | |
247 | len = sizeof(*prdt) * prdt_entries; | |
eb5937ba | 248 | guest_prdt = guest_alloc(&guest_malloc, len); |
4a61c3ab | 249 | qtest_memwrite(qts, guest_prdt, prdt, len); |
b4ba67d9 | 250 | qpci_io_writel(dev, bmdma_bar, bmreg_prdt, guest_prdt); |
b95739dc KW |
251 | |
252 | /* ATA DMA command */ | |
00ea63fd JS |
253 | if (cmd == CMD_PACKET) { |
254 | /* Enables ATAPI DMA; otherwise PIO is attempted */ | |
b4ba67d9 | 255 | qpci_io_writeb(dev, ide_bar, reg_feature, 0x01); |
00ea63fd | 256 | } else { |
29e1d473 AN |
257 | if (cmd == CMD_DSM) { |
258 | /* trim bit */ | |
259 | qpci_io_writeb(dev, ide_bar, reg_feature, 0x01); | |
260 | } | |
b4ba67d9 DG |
261 | qpci_io_writeb(dev, ide_bar, reg_nsectors, nb_sectors); |
262 | qpci_io_writeb(dev, ide_bar, reg_lba_low, sector & 0xff); | |
263 | qpci_io_writeb(dev, ide_bar, reg_lba_middle, (sector >> 8) & 0xff); | |
264 | qpci_io_writeb(dev, ide_bar, reg_lba_high, (sector >> 16) & 0xff); | |
00ea63fd | 265 | } |
b95739dc | 266 | |
b4ba67d9 | 267 | qpci_io_writeb(dev, ide_bar, reg_command, cmd); |
b95739dc | 268 | |
00ea63fd | 269 | if (post_exec) { |
b4ba67d9 | 270 | post_exec(dev, ide_bar, sector, nb_sectors); |
00ea63fd JS |
271 | } |
272 | ||
b95739dc | 273 | /* Start DMA transfer */ |
b4ba67d9 | 274 | qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, |
9c268f8a | 275 | BM_CMD_START | (from_dev ? BM_CMD_WRITE : 0)); |
b95739dc | 276 | |
948eaed1 | 277 | if (flags & CMDF_ABORT) { |
b4ba67d9 | 278 | qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, 0); |
948eaed1 KW |
279 | } |
280 | ||
b95739dc KW |
281 | /* Wait for the DMA transfer to complete */ |
282 | do { | |
b4ba67d9 | 283 | status = qpci_io_readb(dev, bmdma_bar, bmreg_status); |
b95739dc KW |
284 | } while ((status & (BM_STS_ACTIVE | BM_STS_INTR)) == BM_STS_ACTIVE); |
285 | ||
4a61c3ab TH |
286 | g_assert_cmpint(qtest_get_irq(qts, IDE_PRIMARY_IRQ), ==, |
287 | !!(status & BM_STS_INTR)); | |
b95739dc KW |
288 | |
289 | /* Check IDE status code */ | |
b4ba67d9 DG |
290 | assert_bit_set(qpci_io_readb(dev, ide_bar, reg_status), DRDY); |
291 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), BSY | DRQ); | |
b95739dc KW |
292 | |
293 | /* Reading the status register clears the IRQ */ | |
4a61c3ab | 294 | g_assert(!qtest_get_irq(qts, IDE_PRIMARY_IRQ)); |
b95739dc KW |
295 | |
296 | /* Stop DMA transfer if still active */ | |
297 | if (status & BM_STS_ACTIVE) { | |
b4ba67d9 | 298 | qpci_io_writeb(dev, bmdma_bar, bmreg_cmd, 0); |
b95739dc KW |
299 | } |
300 | ||
301 | free_pci_device(dev); | |
302 | ||
303 | return status; | |
304 | } | |
305 | ||
4a61c3ab TH |
306 | static QTestState *test_bmdma_setup(void) |
307 | { | |
308 | QTestState *qts; | |
309 | ||
310 | qts = ide_test_start( | |
311 | "-drive file=%s,if=ide,cache=writeback,format=raw " | |
312 | "-global ide-hd.serial=%s -global ide-hd.ver=%s", | |
313 | tmp_path, "testdisk", "version"); | |
314 | qtest_irq_intercept_in(qts, "ioapic"); | |
315 | ||
316 | return qts; | |
317 | } | |
318 | ||
319 | static void test_bmdma_teardown(QTestState *qts) | |
320 | { | |
321 | ide_test_quit(qts); | |
322 | } | |
323 | ||
b95739dc KW |
324 | static void test_bmdma_simple_rw(void) |
325 | { | |
4a61c3ab | 326 | QTestState *qts; |
9c268f8a | 327 | QPCIDevice *dev; |
b4ba67d9 | 328 | QPCIBar bmdma_bar, ide_bar; |
b95739dc KW |
329 | uint8_t status; |
330 | uint8_t *buf; | |
331 | uint8_t *cmpbuf; | |
332 | size_t len = 512; | |
4a61c3ab TH |
333 | uintptr_t guest_buf; |
334 | PrdtEntry prdt[1]; | |
b95739dc | 335 | |
4a61c3ab TH |
336 | qts = test_bmdma_setup(); |
337 | ||
338 | guest_buf = guest_alloc(&guest_malloc, len); | |
339 | prdt[0].addr = cpu_to_le32(guest_buf); | |
340 | prdt[0].size = cpu_to_le32(len | PRDT_EOT); | |
b95739dc | 341 | |
4a61c3ab | 342 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); |
9c268f8a | 343 | |
b95739dc KW |
344 | buf = g_malloc(len); |
345 | cmpbuf = g_malloc(len); | |
346 | ||
347 | /* Write 0x55 pattern to sector 0 */ | |
348 | memset(buf, 0x55, len); | |
4a61c3ab | 349 | qtest_memwrite(qts, guest_buf, buf, len); |
b95739dc | 350 | |
4a61c3ab | 351 | status = send_dma_request(qts, CMD_WRITE_DMA, 0, 1, prdt, |
00ea63fd | 352 | ARRAY_SIZE(prdt), NULL); |
b95739dc | 353 | g_assert_cmphex(status, ==, BM_STS_INTR); |
b4ba67d9 | 354 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
b95739dc KW |
355 | |
356 | /* Write 0xaa pattern to sector 1 */ | |
357 | memset(buf, 0xaa, len); | |
4a61c3ab | 358 | qtest_memwrite(qts, guest_buf, buf, len); |
b95739dc | 359 | |
4a61c3ab | 360 | status = send_dma_request(qts, CMD_WRITE_DMA, 1, 1, prdt, |
00ea63fd | 361 | ARRAY_SIZE(prdt), NULL); |
b95739dc | 362 | g_assert_cmphex(status, ==, BM_STS_INTR); |
b4ba67d9 | 363 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
b95739dc KW |
364 | |
365 | /* Read and verify 0x55 pattern in sector 0 */ | |
366 | memset(cmpbuf, 0x55, len); | |
367 | ||
4a61c3ab TH |
368 | status = send_dma_request(qts, CMD_READ_DMA, 0, 1, prdt, ARRAY_SIZE(prdt), |
369 | NULL); | |
b95739dc | 370 | g_assert_cmphex(status, ==, BM_STS_INTR); |
b4ba67d9 | 371 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
b95739dc | 372 | |
4a61c3ab | 373 | qtest_memread(qts, guest_buf, buf, len); |
b95739dc KW |
374 | g_assert(memcmp(buf, cmpbuf, len) == 0); |
375 | ||
376 | /* Read and verify 0xaa pattern in sector 1 */ | |
377 | memset(cmpbuf, 0xaa, len); | |
378 | ||
4a61c3ab TH |
379 | status = send_dma_request(qts, CMD_READ_DMA, 1, 1, prdt, ARRAY_SIZE(prdt), |
380 | NULL); | |
b95739dc | 381 | g_assert_cmphex(status, ==, BM_STS_INTR); |
b4ba67d9 | 382 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
b95739dc | 383 | |
4a61c3ab | 384 | qtest_memread(qts, guest_buf, buf, len); |
b95739dc KW |
385 | g_assert(memcmp(buf, cmpbuf, len) == 0); |
386 | ||
f5aa4bdc | 387 | free_pci_device(dev); |
b95739dc KW |
388 | g_free(buf); |
389 | g_free(cmpbuf); | |
4a61c3ab TH |
390 | |
391 | test_bmdma_teardown(qts); | |
b95739dc KW |
392 | } |
393 | ||
29e1d473 AN |
394 | static void test_bmdma_trim(void) |
395 | { | |
4a61c3ab | 396 | QTestState *qts; |
29e1d473 AN |
397 | QPCIDevice *dev; |
398 | QPCIBar bmdma_bar, ide_bar; | |
399 | uint8_t status; | |
400 | const uint64_t trim_range[] = { trim_range_le(0, 2), | |
401 | trim_range_le(6, 8), | |
402 | trim_range_le(10, 1), | |
403 | }; | |
404 | const uint64_t bad_range = trim_range_le(TEST_IMAGE_SIZE / 512 - 1, 2); | |
405 | size_t len = 512; | |
406 | uint8_t *buf; | |
4a61c3ab TH |
407 | uintptr_t guest_buf; |
408 | PrdtEntry prdt[1]; | |
29e1d473 | 409 | |
4a61c3ab TH |
410 | qts = test_bmdma_setup(); |
411 | ||
412 | guest_buf = guest_alloc(&guest_malloc, len); | |
413 | prdt[0].addr = cpu_to_le32(guest_buf), | |
414 | prdt[0].size = cpu_to_le32(len | PRDT_EOT), | |
29e1d473 | 415 | |
4a61c3ab | 416 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); |
29e1d473 AN |
417 | |
418 | buf = g_malloc(len); | |
419 | ||
420 | /* Normal request */ | |
421 | *((uint64_t *)buf) = trim_range[0]; | |
422 | *((uint64_t *)buf + 1) = trim_range[1]; | |
423 | ||
4a61c3ab | 424 | qtest_memwrite(qts, guest_buf, buf, 2 * sizeof(uint64_t)); |
29e1d473 | 425 | |
4a61c3ab | 426 | status = send_dma_request(qts, CMD_DSM, 0, 1, prdt, |
29e1d473 AN |
427 | ARRAY_SIZE(prdt), NULL); |
428 | g_assert_cmphex(status, ==, BM_STS_INTR); | |
429 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); | |
430 | ||
431 | /* Request contains invalid range */ | |
432 | *((uint64_t *)buf) = trim_range[2]; | |
433 | *((uint64_t *)buf + 1) = bad_range; | |
434 | ||
4a61c3ab | 435 | qtest_memwrite(qts, guest_buf, buf, 2 * sizeof(uint64_t)); |
29e1d473 | 436 | |
4a61c3ab | 437 | status = send_dma_request(qts, CMD_DSM, 0, 1, prdt, |
29e1d473 AN |
438 | ARRAY_SIZE(prdt), NULL); |
439 | g_assert_cmphex(status, ==, BM_STS_INTR); | |
440 | assert_bit_set(qpci_io_readb(dev, ide_bar, reg_status), ERR); | |
441 | assert_bit_set(qpci_io_readb(dev, ide_bar, reg_error), ABRT); | |
442 | ||
443 | free_pci_device(dev); | |
444 | g_free(buf); | |
4a61c3ab | 445 | test_bmdma_teardown(qts); |
29e1d473 AN |
446 | } |
447 | ||
948eaed1 KW |
448 | static void test_bmdma_short_prdt(void) |
449 | { | |
4a61c3ab | 450 | QTestState *qts; |
9c268f8a | 451 | QPCIDevice *dev; |
b4ba67d9 | 452 | QPCIBar bmdma_bar, ide_bar; |
948eaed1 KW |
453 | uint8_t status; |
454 | ||
455 | PrdtEntry prdt[] = { | |
262f27b9 KW |
456 | { |
457 | .addr = 0, | |
458 | .size = cpu_to_le32(0x10 | PRDT_EOT), | |
459 | }, | |
948eaed1 KW |
460 | }; |
461 | ||
4a61c3ab TH |
462 | qts = test_bmdma_setup(); |
463 | ||
464 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); | |
9c268f8a | 465 | |
948eaed1 | 466 | /* Normal request */ |
4a61c3ab | 467 | status = send_dma_request(qts, CMD_READ_DMA, 0, 1, |
00ea63fd | 468 | prdt, ARRAY_SIZE(prdt), NULL); |
948eaed1 | 469 | g_assert_cmphex(status, ==, 0); |
b4ba67d9 | 470 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
948eaed1 KW |
471 | |
472 | /* Abort the request before it completes */ | |
4a61c3ab | 473 | status = send_dma_request(qts, CMD_READ_DMA | CMDF_ABORT, 0, 1, |
00ea63fd | 474 | prdt, ARRAY_SIZE(prdt), NULL); |
948eaed1 | 475 | g_assert_cmphex(status, ==, 0); |
b4ba67d9 | 476 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
f5aa4bdc | 477 | free_pci_device(dev); |
4a61c3ab | 478 | test_bmdma_teardown(qts); |
948eaed1 KW |
479 | } |
480 | ||
58732810 SH |
481 | static void test_bmdma_one_sector_short_prdt(void) |
482 | { | |
4a61c3ab | 483 | QTestState *qts; |
9c268f8a | 484 | QPCIDevice *dev; |
b4ba67d9 | 485 | QPCIBar bmdma_bar, ide_bar; |
58732810 SH |
486 | uint8_t status; |
487 | ||
488 | /* Read 2 sectors but only give 1 sector in PRDT */ | |
489 | PrdtEntry prdt[] = { | |
490 | { | |
491 | .addr = 0, | |
492 | .size = cpu_to_le32(0x200 | PRDT_EOT), | |
493 | }, | |
494 | }; | |
495 | ||
4a61c3ab TH |
496 | qts = test_bmdma_setup(); |
497 | ||
498 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); | |
9c268f8a | 499 | |
58732810 | 500 | /* Normal request */ |
4a61c3ab | 501 | status = send_dma_request(qts, CMD_READ_DMA, 0, 2, |
00ea63fd | 502 | prdt, ARRAY_SIZE(prdt), NULL); |
58732810 | 503 | g_assert_cmphex(status, ==, 0); |
b4ba67d9 | 504 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
58732810 SH |
505 | |
506 | /* Abort the request before it completes */ | |
4a61c3ab | 507 | status = send_dma_request(qts, CMD_READ_DMA | CMDF_ABORT, 0, 2, |
00ea63fd | 508 | prdt, ARRAY_SIZE(prdt), NULL); |
58732810 | 509 | g_assert_cmphex(status, ==, 0); |
b4ba67d9 | 510 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
f5aa4bdc | 511 | free_pci_device(dev); |
4a61c3ab | 512 | test_bmdma_teardown(qts); |
58732810 SH |
513 | } |
514 | ||
948eaed1 KW |
515 | static void test_bmdma_long_prdt(void) |
516 | { | |
4a61c3ab | 517 | QTestState *qts; |
9c268f8a | 518 | QPCIDevice *dev; |
b4ba67d9 | 519 | QPCIBar bmdma_bar, ide_bar; |
948eaed1 KW |
520 | uint8_t status; |
521 | ||
522 | PrdtEntry prdt[] = { | |
262f27b9 KW |
523 | { |
524 | .addr = 0, | |
525 | .size = cpu_to_le32(0x1000 | PRDT_EOT), | |
526 | }, | |
948eaed1 KW |
527 | }; |
528 | ||
4a61c3ab TH |
529 | qts = test_bmdma_setup(); |
530 | ||
531 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); | |
9c268f8a | 532 | |
948eaed1 | 533 | /* Normal request */ |
4a61c3ab | 534 | status = send_dma_request(qts, CMD_READ_DMA, 0, 1, |
00ea63fd | 535 | prdt, ARRAY_SIZE(prdt), NULL); |
948eaed1 | 536 | g_assert_cmphex(status, ==, BM_STS_ACTIVE | BM_STS_INTR); |
b4ba67d9 | 537 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
948eaed1 KW |
538 | |
539 | /* Abort the request before it completes */ | |
4a61c3ab | 540 | status = send_dma_request(qts, CMD_READ_DMA | CMDF_ABORT, 0, 1, |
00ea63fd | 541 | prdt, ARRAY_SIZE(prdt), NULL); |
948eaed1 | 542 | g_assert_cmphex(status, ==, BM_STS_INTR); |
b4ba67d9 | 543 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
f5aa4bdc | 544 | free_pci_device(dev); |
4a61c3ab | 545 | test_bmdma_teardown(qts); |
948eaed1 KW |
546 | } |
547 | ||
d7b7e580 KW |
548 | static void test_bmdma_no_busmaster(void) |
549 | { | |
4a61c3ab | 550 | QTestState *qts; |
9c268f8a | 551 | QPCIDevice *dev; |
b4ba67d9 | 552 | QPCIBar bmdma_bar, ide_bar; |
d7b7e580 KW |
553 | uint8_t status; |
554 | ||
4a61c3ab TH |
555 | qts = test_bmdma_setup(); |
556 | ||
557 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); | |
9c268f8a | 558 | |
d7b7e580 KW |
559 | /* No PRDT_EOT, each entry addr 0/size 64k, and in theory qemu shouldn't be |
560 | * able to access it anyway because the Bus Master bit in the PCI command | |
561 | * register isn't set. This is complete nonsense, but it used to be pretty | |
562 | * good at confusing and occasionally crashing qemu. */ | |
563 | PrdtEntry prdt[4096] = { }; | |
564 | ||
4a61c3ab | 565 | status = send_dma_request(qts, CMD_READ_DMA | CMDF_NO_BM, 0, 512, |
00ea63fd | 566 | prdt, ARRAY_SIZE(prdt), NULL); |
d7b7e580 KW |
567 | |
568 | /* Not entirely clear what the expected result is, but this is what we get | |
569 | * in practice. At least we want to be aware of any changes. */ | |
570 | g_assert_cmphex(status, ==, BM_STS_ACTIVE | BM_STS_INTR); | |
b4ba67d9 | 571 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
f5aa4bdc | 572 | free_pci_device(dev); |
4a61c3ab | 573 | test_bmdma_teardown(qts); |
b95739dc KW |
574 | } |
575 | ||
262f27b9 KW |
576 | static void string_cpu_to_be16(uint16_t *s, size_t bytes) |
577 | { | |
578 | g_assert((bytes & 1) == 0); | |
579 | bytes /= 2; | |
580 | ||
581 | while (bytes--) { | |
582 | *s = cpu_to_be16(*s); | |
583 | s++; | |
584 | } | |
585 | } | |
586 | ||
acbe4801 KW |
587 | static void test_identify(void) |
588 | { | |
4a61c3ab | 589 | QTestState *qts; |
9c268f8a | 590 | QPCIDevice *dev; |
b4ba67d9 | 591 | QPCIBar bmdma_bar, ide_bar; |
acbe4801 KW |
592 | uint8_t data; |
593 | uint16_t buf[256]; | |
594 | int i; | |
595 | int ret; | |
596 | ||
4a61c3ab | 597 | qts = ide_test_start( |
572023f7 KW |
598 | "-drive file=%s,if=ide,cache=writeback,format=raw " |
599 | "-global ide-hd.serial=%s -global ide-hd.ver=%s", | |
acbe4801 KW |
600 | tmp_path, "testdisk", "version"); |
601 | ||
4a61c3ab | 602 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); |
9c268f8a | 603 | |
acbe4801 | 604 | /* IDENTIFY command on device 0*/ |
b4ba67d9 DG |
605 | qpci_io_writeb(dev, ide_bar, reg_device, 0); |
606 | qpci_io_writeb(dev, ide_bar, reg_command, CMD_IDENTIFY); | |
acbe4801 KW |
607 | |
608 | /* Read in the IDENTIFY buffer and check registers */ | |
b4ba67d9 | 609 | data = qpci_io_readb(dev, ide_bar, reg_device); |
c27d5656 | 610 | g_assert_cmpint(data & DEV, ==, 0); |
acbe4801 KW |
611 | |
612 | for (i = 0; i < 256; i++) { | |
b4ba67d9 | 613 | data = qpci_io_readb(dev, ide_bar, reg_status); |
acbe4801 KW |
614 | assert_bit_set(data, DRDY | DRQ); |
615 | assert_bit_clear(data, BSY | DF | ERR); | |
616 | ||
b4ba67d9 | 617 | buf[i] = qpci_io_readw(dev, ide_bar, reg_data); |
acbe4801 KW |
618 | } |
619 | ||
b4ba67d9 | 620 | data = qpci_io_readb(dev, ide_bar, reg_status); |
acbe4801 KW |
621 | assert_bit_set(data, DRDY); |
622 | assert_bit_clear(data, BSY | DF | ERR | DRQ); | |
623 | ||
624 | /* Check serial number/version in the buffer */ | |
262f27b9 KW |
625 | string_cpu_to_be16(&buf[10], 20); |
626 | ret = memcmp(&buf[10], "testdisk ", 20); | |
acbe4801 KW |
627 | g_assert(ret == 0); |
628 | ||
262f27b9 KW |
629 | string_cpu_to_be16(&buf[23], 8); |
630 | ret = memcmp(&buf[23], "version ", 8); | |
acbe4801 KW |
631 | g_assert(ret == 0); |
632 | ||
633 | /* Write cache enabled bit */ | |
634 | assert_bit_set(buf[85], 0x20); | |
635 | ||
4a61c3ab | 636 | ide_test_quit(qts); |
f5aa4bdc | 637 | free_pci_device(dev); |
acbe4801 KW |
638 | } |
639 | ||
2dd7e10d EY |
640 | /* |
641 | * Write sector 1 with random data to make IDE storage dirty | |
642 | * Needed for flush tests so that flushes actually go though the block layer | |
643 | */ | |
4a61c3ab | 644 | static void make_dirty(QTestState *qts, uint8_t device) |
2dd7e10d | 645 | { |
9c268f8a | 646 | QPCIDevice *dev; |
b4ba67d9 | 647 | QPCIBar bmdma_bar, ide_bar; |
2dd7e10d EY |
648 | uint8_t status; |
649 | size_t len = 512; | |
650 | uintptr_t guest_buf; | |
651 | void* buf; | |
652 | ||
4a61c3ab | 653 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); |
9c268f8a | 654 | |
eb5937ba | 655 | guest_buf = guest_alloc(&guest_malloc, len); |
2dd7e10d | 656 | buf = g_malloc(len); |
6048018e | 657 | memset(buf, rand() % 255 + 1, len); |
2dd7e10d EY |
658 | g_assert(guest_buf); |
659 | g_assert(buf); | |
660 | ||
4a61c3ab | 661 | qtest_memwrite(qts, guest_buf, buf, len); |
2dd7e10d EY |
662 | |
663 | PrdtEntry prdt[] = { | |
664 | { | |
665 | .addr = cpu_to_le32(guest_buf), | |
666 | .size = cpu_to_le32(len | PRDT_EOT), | |
667 | }, | |
668 | }; | |
669 | ||
4a61c3ab | 670 | status = send_dma_request(qts, CMD_WRITE_DMA, 1, 1, prdt, |
2dd7e10d EY |
671 | ARRAY_SIZE(prdt), NULL); |
672 | g_assert_cmphex(status, ==, BM_STS_INTR); | |
b4ba67d9 | 673 | assert_bit_clear(qpci_io_readb(dev, ide_bar, reg_status), DF | ERR); |
2dd7e10d EY |
674 | |
675 | g_free(buf); | |
f5aa4bdc | 676 | free_pci_device(dev); |
2dd7e10d EY |
677 | } |
678 | ||
bd07684a KW |
679 | static void test_flush(void) |
680 | { | |
4a61c3ab | 681 | QTestState *qts; |
9c268f8a | 682 | QPCIDevice *dev; |
b4ba67d9 | 683 | QPCIBar bmdma_bar, ide_bar; |
bd07684a KW |
684 | uint8_t data; |
685 | ||
4a61c3ab | 686 | qts = ide_test_start( |
b8e665e4 | 687 | "-drive file=blkdebug::%s,if=ide,cache=writeback,format=raw", |
bd07684a KW |
688 | tmp_path); |
689 | ||
4a61c3ab | 690 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); |
9c268f8a | 691 | |
4a61c3ab | 692 | qtest_irq_intercept_in(qts, "ioapic"); |
2dd7e10d EY |
693 | |
694 | /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */ | |
4a61c3ab | 695 | make_dirty(qts, 0); |
2dd7e10d | 696 | |
bd07684a | 697 | /* Delay the completion of the flush request until we explicitly do it */ |
4a61c3ab | 698 | g_free(qtest_hmp(qts, "qemu-io ide0-hd0 \"break flush_to_os A\"")); |
bd07684a KW |
699 | |
700 | /* FLUSH CACHE command on device 0*/ | |
b4ba67d9 DG |
701 | qpci_io_writeb(dev, ide_bar, reg_device, 0); |
702 | qpci_io_writeb(dev, ide_bar, reg_command, CMD_FLUSH_CACHE); | |
bd07684a KW |
703 | |
704 | /* Check status while request is in flight*/ | |
b4ba67d9 | 705 | data = qpci_io_readb(dev, ide_bar, reg_status); |
bd07684a KW |
706 | assert_bit_set(data, BSY | DRDY); |
707 | assert_bit_clear(data, DF | ERR | DRQ); | |
708 | ||
709 | /* Complete the command */ | |
4a61c3ab | 710 | g_free(qtest_hmp(qts, "qemu-io ide0-hd0 \"resume A\"")); |
bd07684a KW |
711 | |
712 | /* Check registers */ | |
b4ba67d9 | 713 | data = qpci_io_readb(dev, ide_bar, reg_device); |
bd07684a KW |
714 | g_assert_cmpint(data & DEV, ==, 0); |
715 | ||
22bfa16e | 716 | do { |
b4ba67d9 | 717 | data = qpci_io_readb(dev, ide_bar, reg_status); |
22bfa16e MR |
718 | } while (data & BSY); |
719 | ||
bd07684a KW |
720 | assert_bit_set(data, DRDY); |
721 | assert_bit_clear(data, BSY | DF | ERR | DRQ); | |
722 | ||
4a61c3ab | 723 | ide_test_quit(qts); |
f5aa4bdc | 724 | free_pci_device(dev); |
bd07684a KW |
725 | } |
726 | ||
baca2b9e | 727 | static void test_retry_flush(const char *machine) |
14a92e5f | 728 | { |
4a61c3ab | 729 | QTestState *qts; |
9c268f8a | 730 | QPCIDevice *dev; |
b4ba67d9 | 731 | QPCIBar bmdma_bar, ide_bar; |
14a92e5f | 732 | uint8_t data; |
14a92e5f PB |
733 | |
734 | prepare_blkdebug_script(debug_path, "flush_to_disk"); | |
735 | ||
4a61c3ab | 736 | qts = ide_test_start( |
b8e665e4 KW |
737 | "-drive file=blkdebug:%s:%s,if=ide,cache=writeback,format=raw," |
738 | "rerror=stop,werror=stop", | |
14a92e5f PB |
739 | debug_path, tmp_path); |
740 | ||
4a61c3ab | 741 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); |
9c268f8a | 742 | |
4a61c3ab | 743 | qtest_irq_intercept_in(qts, "ioapic"); |
2dd7e10d EY |
744 | |
745 | /* Dirty media so that CMD_FLUSH_CACHE will actually go to disk */ | |
4a61c3ab | 746 | make_dirty(qts, 0); |
2dd7e10d | 747 | |
14a92e5f | 748 | /* FLUSH CACHE command on device 0*/ |
b4ba67d9 DG |
749 | qpci_io_writeb(dev, ide_bar, reg_device, 0); |
750 | qpci_io_writeb(dev, ide_bar, reg_command, CMD_FLUSH_CACHE); | |
14a92e5f PB |
751 | |
752 | /* Check status while request is in flight*/ | |
b4ba67d9 | 753 | data = qpci_io_readb(dev, ide_bar, reg_status); |
14a92e5f PB |
754 | assert_bit_set(data, BSY | DRDY); |
755 | assert_bit_clear(data, DF | ERR | DRQ); | |
756 | ||
4a61c3ab | 757 | qtest_qmp_eventwait(qts, "STOP"); |
14a92e5f PB |
758 | |
759 | /* Complete the command */ | |
4a61c3ab | 760 | qmp_discard_response(qts, "{'execute':'cont' }"); |
14a92e5f PB |
761 | |
762 | /* Check registers */ | |
b4ba67d9 | 763 | data = qpci_io_readb(dev, ide_bar, reg_device); |
14a92e5f PB |
764 | g_assert_cmpint(data & DEV, ==, 0); |
765 | ||
766 | do { | |
b4ba67d9 | 767 | data = qpci_io_readb(dev, ide_bar, reg_status); |
14a92e5f PB |
768 | } while (data & BSY); |
769 | ||
770 | assert_bit_set(data, DRDY); | |
771 | assert_bit_clear(data, BSY | DF | ERR | DRQ); | |
772 | ||
4a61c3ab | 773 | ide_test_quit(qts); |
f5aa4bdc | 774 | free_pci_device(dev); |
14a92e5f PB |
775 | } |
776 | ||
f7f3ff1d KW |
777 | static void test_flush_nodev(void) |
778 | { | |
4a61c3ab | 779 | QTestState *qts; |
9c268f8a | 780 | QPCIDevice *dev; |
b4ba67d9 | 781 | QPCIBar bmdma_bar, ide_bar; |
9c268f8a | 782 | |
4a61c3ab | 783 | qts = ide_test_start(""); |
f7f3ff1d | 784 | |
4a61c3ab | 785 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); |
9c268f8a | 786 | |
f7f3ff1d | 787 | /* FLUSH CACHE command on device 0*/ |
b4ba67d9 DG |
788 | qpci_io_writeb(dev, ide_bar, reg_device, 0); |
789 | qpci_io_writeb(dev, ide_bar, reg_command, CMD_FLUSH_CACHE); | |
f7f3ff1d KW |
790 | |
791 | /* Just testing that qemu doesn't crash... */ | |
792 | ||
f5aa4bdc | 793 | free_pci_device(dev); |
4a61c3ab | 794 | ide_test_quit(qts); |
f7f3ff1d KW |
795 | } |
796 | ||
ce317e8d KW |
797 | static void test_flush_empty_drive(void) |
798 | { | |
4a61c3ab | 799 | QTestState *qts; |
ce317e8d KW |
800 | QPCIDevice *dev; |
801 | QPCIBar bmdma_bar, ide_bar; | |
802 | ||
4a61c3ab TH |
803 | qts = ide_test_start("-device ide-cd,bus=ide.0"); |
804 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); | |
ce317e8d KW |
805 | |
806 | /* FLUSH CACHE command on device 0 */ | |
807 | qpci_io_writeb(dev, ide_bar, reg_device, 0); | |
808 | qpci_io_writeb(dev, ide_bar, reg_command, CMD_FLUSH_CACHE); | |
809 | ||
810 | /* Just testing that qemu doesn't crash... */ | |
811 | ||
812 | free_pci_device(dev); | |
4a61c3ab | 813 | ide_test_quit(qts); |
ce317e8d KW |
814 | } |
815 | ||
041088c7 | 816 | static void test_pci_retry_flush(void) |
baca2b9e JS |
817 | { |
818 | test_retry_flush("pc"); | |
819 | } | |
820 | ||
041088c7 | 821 | static void test_isa_retry_flush(void) |
baca2b9e JS |
822 | { |
823 | test_retry_flush("isapc"); | |
824 | } | |
825 | ||
f7ba8d7f JS |
826 | typedef struct Read10CDB { |
827 | uint8_t opcode; | |
828 | uint8_t flags; | |
829 | uint32_t lba; | |
830 | uint8_t reserved; | |
831 | uint16_t nblocks; | |
832 | uint8_t control; | |
833 | uint16_t padding; | |
834 | } __attribute__((__packed__)) Read10CDB; | |
835 | ||
b4ba67d9 | 836 | static void send_scsi_cdb_read10(QPCIDevice *dev, QPCIBar ide_bar, |
9c268f8a | 837 | uint64_t lba, int nblocks) |
f7ba8d7f JS |
838 | { |
839 | Read10CDB pkt = { .padding = 0 }; | |
840 | int i; | |
841 | ||
00ea63fd JS |
842 | g_assert_cmpint(lba, <=, UINT32_MAX); |
843 | g_assert_cmpint(nblocks, <=, UINT16_MAX); | |
844 | g_assert_cmpint(nblocks, >=, 0); | |
845 | ||
f7ba8d7f JS |
846 | /* Construct SCSI CDB packet */ |
847 | pkt.opcode = 0x28; | |
848 | pkt.lba = cpu_to_be32(lba); | |
849 | pkt.nblocks = cpu_to_be16(nblocks); | |
850 | ||
851 | /* Send Packet */ | |
852 | for (i = 0; i < sizeof(Read10CDB)/2; i++) { | |
b4ba67d9 | 853 | qpci_io_writew(dev, ide_bar, reg_data, |
9c268f8a | 854 | le16_to_cpu(((uint16_t *)&pkt)[i])); |
f7ba8d7f JS |
855 | } |
856 | } | |
857 | ||
4a61c3ab | 858 | static void nsleep(QTestState *qts, int64_t nsecs) |
f7ba8d7f JS |
859 | { |
860 | const struct timespec val = { .tv_nsec = nsecs }; | |
861 | nanosleep(&val, NULL); | |
4a61c3ab | 862 | qtest_clock_set(qts, nsecs); |
f7ba8d7f JS |
863 | } |
864 | ||
4a61c3ab | 865 | static uint8_t ide_wait_clear(QTestState *qts, uint8_t flag) |
f7ba8d7f | 866 | { |
9c268f8a | 867 | QPCIDevice *dev; |
b4ba67d9 | 868 | QPCIBar bmdma_bar, ide_bar; |
f7ba8d7f | 869 | uint8_t data; |
9c73517c | 870 | time_t st; |
f7ba8d7f | 871 | |
4a61c3ab | 872 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); |
9c268f8a | 873 | |
f7ba8d7f | 874 | /* Wait with a 5 second timeout */ |
9c73517c JS |
875 | time(&st); |
876 | while (true) { | |
b4ba67d9 | 877 | data = qpci_io_readb(dev, ide_bar, reg_status); |
f7ba8d7f | 878 | if (!(data & flag)) { |
f5aa4bdc | 879 | free_pci_device(dev); |
f7ba8d7f JS |
880 | return data; |
881 | } | |
9c73517c JS |
882 | if (difftime(time(NULL), st) > 5.0) { |
883 | break; | |
884 | } | |
4a61c3ab | 885 | nsleep(qts, 400); |
f7ba8d7f JS |
886 | } |
887 | g_assert_not_reached(); | |
888 | } | |
889 | ||
4a61c3ab | 890 | static void ide_wait_intr(QTestState *qts, int irq) |
f7ba8d7f | 891 | { |
9c73517c | 892 | time_t st; |
f7ba8d7f JS |
893 | bool intr; |
894 | ||
9c73517c JS |
895 | time(&st); |
896 | while (true) { | |
4a61c3ab | 897 | intr = qtest_get_irq(qts, irq); |
f7ba8d7f JS |
898 | if (intr) { |
899 | return; | |
900 | } | |
9c73517c JS |
901 | if (difftime(time(NULL), st) > 5.0) { |
902 | break; | |
903 | } | |
4a61c3ab | 904 | nsleep(qts, 400); |
f7ba8d7f JS |
905 | } |
906 | ||
907 | g_assert_not_reached(); | |
908 | } | |
909 | ||
910 | static void cdrom_pio_impl(int nblocks) | |
911 | { | |
4a61c3ab | 912 | QTestState *qts; |
9c268f8a | 913 | QPCIDevice *dev; |
b4ba67d9 | 914 | QPCIBar bmdma_bar, ide_bar; |
f7ba8d7f JS |
915 | FILE *fh; |
916 | int patt_blocks = MAX(16, nblocks); | |
917 | size_t patt_len = ATAPI_BLOCK_SIZE * patt_blocks; | |
918 | char *pattern = g_malloc(patt_len); | |
919 | size_t rxsize = ATAPI_BLOCK_SIZE * nblocks; | |
920 | uint16_t *rx = g_malloc0(rxsize); | |
921 | int i, j; | |
922 | uint8_t data; | |
923 | uint16_t limit; | |
543f8f13 | 924 | size_t ret; |
f7ba8d7f JS |
925 | |
926 | /* Prepopulate the CDROM with an interesting pattern */ | |
927 | generate_pattern(pattern, patt_len, ATAPI_BLOCK_SIZE); | |
928 | fh = fopen(tmp_path, "w+"); | |
543f8f13 JS |
929 | ret = fwrite(pattern, ATAPI_BLOCK_SIZE, patt_blocks, fh); |
930 | g_assert_cmpint(ret, ==, patt_blocks); | |
f7ba8d7f JS |
931 | fclose(fh); |
932 | ||
4a61c3ab TH |
933 | qts = ide_test_start( |
934 | "-drive if=none,file=%s,media=cdrom,format=raw,id=sr0,index=0 " | |
935 | "-device ide-cd,drive=sr0,bus=ide.0", tmp_path); | |
936 | dev = get_pci_device(qts, &bmdma_bar, &ide_bar); | |
937 | qtest_irq_intercept_in(qts, "ioapic"); | |
f7ba8d7f JS |
938 | |
939 | /* PACKET command on device 0 */ | |
b4ba67d9 DG |
940 | qpci_io_writeb(dev, ide_bar, reg_device, 0); |
941 | qpci_io_writeb(dev, ide_bar, reg_lba_middle, BYTE_COUNT_LIMIT & 0xFF); | |
942 | qpci_io_writeb(dev, ide_bar, reg_lba_high, (BYTE_COUNT_LIMIT >> 8 & 0xFF)); | |
943 | qpci_io_writeb(dev, ide_bar, reg_command, CMD_PACKET); | |
f348daf3 | 944 | /* HP0: Check_Status_A State */ |
4a61c3ab TH |
945 | nsleep(qts, 400); |
946 | data = ide_wait_clear(qts, BSY); | |
f348daf3 | 947 | /* HP1: Send_Packet State */ |
f7ba8d7f JS |
948 | assert_bit_set(data, DRQ | DRDY); |
949 | assert_bit_clear(data, ERR | DF | BSY); | |
950 | ||
951 | /* SCSI CDB (READ10) -- read n*2048 bytes from block 0 */ | |
b4ba67d9 | 952 | send_scsi_cdb_read10(dev, ide_bar, 0, nblocks); |
f7ba8d7f | 953 | |
f7ba8d7f JS |
954 | /* Read data back: occurs in bursts of 'BYTE_COUNT_LIMIT' bytes. |
955 | * If BYTE_COUNT_LIMIT is odd, we transfer BYTE_COUNT_LIMIT - 1 bytes. | |
956 | * We allow an odd limit only when the remaining transfer size is | |
957 | * less than BYTE_COUNT_LIMIT. However, SCSI's read10 command can only | |
958 | * request n blocks, so our request size is always even. | |
959 | * For this reason, we assume there is never a hanging byte to fetch. */ | |
960 | g_assert(!(rxsize & 1)); | |
961 | limit = BYTE_COUNT_LIMIT & ~1; | |
962 | for (i = 0; i < DIV_ROUND_UP(rxsize, limit); i++) { | |
963 | size_t offset = i * (limit / 2); | |
964 | size_t rem = (rxsize / 2) - offset; | |
a421f3c3 JS |
965 | |
966 | /* HP3: INTRQ_Wait */ | |
4a61c3ab | 967 | ide_wait_intr(qts, IDE_PRIMARY_IRQ); |
a421f3c3 JS |
968 | |
969 | /* HP2: Check_Status_B (and clear IRQ) */ | |
4a61c3ab | 970 | data = ide_wait_clear(qts, BSY); |
f348daf3 PL |
971 | assert_bit_set(data, DRQ | DRDY); |
972 | assert_bit_clear(data, ERR | DF | BSY); | |
a421f3c3 | 973 | |
f348daf3 | 974 | /* HP4: Transfer_Data */ |
f7ba8d7f | 975 | for (j = 0; j < MIN((limit / 2), rem); j++) { |
b4ba67d9 DG |
976 | rx[offset + j] = cpu_to_le16(qpci_io_readw(dev, ide_bar, |
977 | reg_data)); | |
f7ba8d7f | 978 | } |
f7ba8d7f | 979 | } |
a421f3c3 JS |
980 | |
981 | /* Check for final completion IRQ */ | |
4a61c3ab | 982 | ide_wait_intr(qts, IDE_PRIMARY_IRQ); |
a421f3c3 JS |
983 | |
984 | /* Sanity check final state */ | |
4a61c3ab | 985 | data = ide_wait_clear(qts, DRQ); |
f7ba8d7f JS |
986 | assert_bit_set(data, DRDY); |
987 | assert_bit_clear(data, DRQ | ERR | DF | BSY); | |
988 | ||
989 | g_assert_cmpint(memcmp(pattern, rx, rxsize), ==, 0); | |
990 | g_free(pattern); | |
991 | g_free(rx); | |
4a61c3ab | 992 | test_bmdma_teardown(qts); |
f5aa4bdc | 993 | free_pci_device(dev); |
f7ba8d7f JS |
994 | } |
995 | ||
996 | static void test_cdrom_pio(void) | |
997 | { | |
998 | cdrom_pio_impl(1); | |
999 | } | |
1000 | ||
1001 | static void test_cdrom_pio_large(void) | |
1002 | { | |
1003 | /* Test a few loops of the PIO DRQ mechanism. */ | |
1004 | cdrom_pio_impl(BYTE_COUNT_LIMIT * 4 / ATAPI_BLOCK_SIZE); | |
1005 | } | |
1006 | ||
00ea63fd JS |
1007 | |
1008 | static void test_cdrom_dma(void) | |
1009 | { | |
4a61c3ab | 1010 | QTestState *qts; |
00ea63fd | 1011 | static const size_t len = ATAPI_BLOCK_SIZE; |
543f8f13 | 1012 | size_t ret; |
00ea63fd JS |
1013 | char *pattern = g_malloc(ATAPI_BLOCK_SIZE * 16); |
1014 | char *rx = g_malloc0(len); | |
1015 | uintptr_t guest_buf; | |
1016 | PrdtEntry prdt[1]; | |
1017 | FILE *fh; | |
1018 | ||
4a61c3ab TH |
1019 | qts = ide_test_start( |
1020 | "-drive if=none,file=%s,media=cdrom,format=raw,id=sr0,index=0 " | |
1021 | "-device ide-cd,drive=sr0,bus=ide.0", tmp_path); | |
1022 | qtest_irq_intercept_in(qts, "ioapic"); | |
00ea63fd | 1023 | |
eb5937ba | 1024 | guest_buf = guest_alloc(&guest_malloc, len); |
00ea63fd JS |
1025 | prdt[0].addr = cpu_to_le32(guest_buf); |
1026 | prdt[0].size = cpu_to_le32(len | PRDT_EOT); | |
1027 | ||
1028 | generate_pattern(pattern, ATAPI_BLOCK_SIZE * 16, ATAPI_BLOCK_SIZE); | |
1029 | fh = fopen(tmp_path, "w+"); | |
543f8f13 JS |
1030 | ret = fwrite(pattern, ATAPI_BLOCK_SIZE, 16, fh); |
1031 | g_assert_cmpint(ret, ==, 16); | |
00ea63fd JS |
1032 | fclose(fh); |
1033 | ||
4a61c3ab | 1034 | send_dma_request(qts, CMD_PACKET, 0, 1, prdt, 1, send_scsi_cdb_read10); |
00ea63fd JS |
1035 | |
1036 | /* Read back data from guest memory into local qtest memory */ | |
4a61c3ab | 1037 | qtest_memread(qts, guest_buf, rx, len); |
00ea63fd JS |
1038 | g_assert_cmpint(memcmp(pattern, rx, len), ==, 0); |
1039 | ||
1040 | g_free(pattern); | |
1041 | g_free(rx); | |
4a61c3ab | 1042 | test_bmdma_teardown(qts); |
00ea63fd JS |
1043 | } |
1044 | ||
acbe4801 KW |
1045 | int main(int argc, char **argv) |
1046 | { | |
acbe4801 KW |
1047 | int fd; |
1048 | int ret; | |
1049 | ||
14a92e5f PB |
1050 | /* Create temporary blkdebug instructions */ |
1051 | fd = mkstemp(debug_path); | |
1052 | g_assert(fd >= 0); | |
1053 | close(fd); | |
1054 | ||
acbe4801 KW |
1055 | /* Create a temporary raw image */ |
1056 | fd = mkstemp(tmp_path); | |
1057 | g_assert(fd >= 0); | |
1058 | ret = ftruncate(fd, TEST_IMAGE_SIZE); | |
1059 | g_assert(ret == 0); | |
1060 | close(fd); | |
1061 | ||
1062 | /* Run the tests */ | |
1063 | g_test_init(&argc, &argv, NULL); | |
1064 | ||
1065 | qtest_add_func("/ide/identify", test_identify); | |
1066 | ||
b95739dc | 1067 | qtest_add_func("/ide/bmdma/simple_rw", test_bmdma_simple_rw); |
29e1d473 | 1068 | qtest_add_func("/ide/bmdma/trim", test_bmdma_trim); |
948eaed1 | 1069 | qtest_add_func("/ide/bmdma/short_prdt", test_bmdma_short_prdt); |
58732810 SH |
1070 | qtest_add_func("/ide/bmdma/one_sector_short_prdt", |
1071 | test_bmdma_one_sector_short_prdt); | |
948eaed1 | 1072 | qtest_add_func("/ide/bmdma/long_prdt", test_bmdma_long_prdt); |
d7b7e580 | 1073 | qtest_add_func("/ide/bmdma/no_busmaster", test_bmdma_no_busmaster); |
b95739dc | 1074 | |
bd07684a | 1075 | qtest_add_func("/ide/flush", test_flush); |
baca2b9e | 1076 | qtest_add_func("/ide/flush/nodev", test_flush_nodev); |
ce317e8d | 1077 | qtest_add_func("/ide/flush/empty_drive", test_flush_empty_drive); |
baca2b9e JS |
1078 | qtest_add_func("/ide/flush/retry_pci", test_pci_retry_flush); |
1079 | qtest_add_func("/ide/flush/retry_isa", test_isa_retry_flush); | |
14a92e5f | 1080 | |
f7ba8d7f JS |
1081 | qtest_add_func("/ide/cdrom/pio", test_cdrom_pio); |
1082 | qtest_add_func("/ide/cdrom/pio_large", test_cdrom_pio_large); | |
00ea63fd | 1083 | qtest_add_func("/ide/cdrom/dma", test_cdrom_dma); |
f7ba8d7f | 1084 | |
acbe4801 KW |
1085 | ret = g_test_run(); |
1086 | ||
1087 | /* Cleanup */ | |
1088 | unlink(tmp_path); | |
14a92e5f | 1089 | unlink(debug_path); |
acbe4801 KW |
1090 | |
1091 | return ret; | |
1092 | } |