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