Statistics
| Branch: | Revision:

root / tests / ide-test.c @ 7edd9ddc

History | View | Annotate | Download (14 kB)

1 acbe4801 Kevin Wolf
/*
2 acbe4801 Kevin Wolf
 * IDE test cases
3 acbe4801 Kevin Wolf
 *
4 acbe4801 Kevin Wolf
 * Copyright (c) 2013 Kevin Wolf <kwolf@redhat.com>
5 acbe4801 Kevin Wolf
 *
6 acbe4801 Kevin Wolf
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 acbe4801 Kevin Wolf
 * of this software and associated documentation files (the "Software"), to deal
8 acbe4801 Kevin Wolf
 * in the Software without restriction, including without limitation the rights
9 acbe4801 Kevin Wolf
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 acbe4801 Kevin Wolf
 * copies of the Software, and to permit persons to whom the Software is
11 acbe4801 Kevin Wolf
 * furnished to do so, subject to the following conditions:
12 acbe4801 Kevin Wolf
 *
13 acbe4801 Kevin Wolf
 * The above copyright notice and this permission notice shall be included in
14 acbe4801 Kevin Wolf
 * all copies or substantial portions of the Software.
15 acbe4801 Kevin Wolf
 *
16 acbe4801 Kevin Wolf
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 acbe4801 Kevin Wolf
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 acbe4801 Kevin Wolf
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 acbe4801 Kevin Wolf
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 acbe4801 Kevin Wolf
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 acbe4801 Kevin Wolf
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 acbe4801 Kevin Wolf
 * THE SOFTWARE.
23 acbe4801 Kevin Wolf
 */
24 acbe4801 Kevin Wolf
25 acbe4801 Kevin Wolf
#include <stdint.h>
26 acbe4801 Kevin Wolf
#include <string.h>
27 acbe4801 Kevin Wolf
#include <stdio.h>
28 acbe4801 Kevin Wolf
29 acbe4801 Kevin Wolf
#include <glib.h>
30 acbe4801 Kevin Wolf
31 acbe4801 Kevin Wolf
#include "libqtest.h"
32 b95739dc Kevin Wolf
#include "libqos/pci-pc.h"
33 b95739dc Kevin Wolf
#include "libqos/malloc-pc.h"
34 acbe4801 Kevin Wolf
35 acbe4801 Kevin Wolf
#include "qemu-common.h"
36 b95739dc Kevin Wolf
#include "hw/pci/pci_ids.h"
37 b95739dc Kevin Wolf
#include "hw/pci/pci_regs.h"
38 acbe4801 Kevin Wolf
39 acbe4801 Kevin Wolf
#define TEST_IMAGE_SIZE 64 * 1024 * 1024
40 acbe4801 Kevin Wolf
41 acbe4801 Kevin Wolf
#define IDE_PCI_DEV     1
42 acbe4801 Kevin Wolf
#define IDE_PCI_FUNC    1
43 acbe4801 Kevin Wolf
44 acbe4801 Kevin Wolf
#define IDE_BASE 0x1f0
45 acbe4801 Kevin Wolf
#define IDE_PRIMARY_IRQ 14
46 acbe4801 Kevin Wolf
47 acbe4801 Kevin Wolf
enum {
48 acbe4801 Kevin Wolf
    reg_data        = 0x0,
49 acbe4801 Kevin Wolf
    reg_nsectors    = 0x2,
50 acbe4801 Kevin Wolf
    reg_lba_low     = 0x3,
51 acbe4801 Kevin Wolf
    reg_lba_middle  = 0x4,
52 acbe4801 Kevin Wolf
    reg_lba_high    = 0x5,
53 acbe4801 Kevin Wolf
    reg_device      = 0x6,
54 acbe4801 Kevin Wolf
    reg_status      = 0x7,
55 acbe4801 Kevin Wolf
    reg_command     = 0x7,
56 acbe4801 Kevin Wolf
};
57 acbe4801 Kevin Wolf
58 acbe4801 Kevin Wolf
enum {
59 acbe4801 Kevin Wolf
    BSY     = 0x80,
60 acbe4801 Kevin Wolf
    DRDY    = 0x40,
61 acbe4801 Kevin Wolf
    DF      = 0x20,
62 acbe4801 Kevin Wolf
    DRQ     = 0x08,
63 acbe4801 Kevin Wolf
    ERR     = 0x01,
64 acbe4801 Kevin Wolf
};
65 acbe4801 Kevin Wolf
66 acbe4801 Kevin Wolf
enum {
67 c27d5656 Kevin Wolf
    DEV     = 0x10,
68 b95739dc Kevin Wolf
    LBA     = 0x40,
69 b95739dc Kevin Wolf
};
70 b95739dc Kevin Wolf
71 b95739dc Kevin Wolf
enum {
72 b95739dc Kevin Wolf
    bmreg_cmd       = 0x0,
73 b95739dc Kevin Wolf
    bmreg_status    = 0x2,
74 b95739dc Kevin Wolf
    bmreg_prdt      = 0x4,
75 b95739dc Kevin Wolf
};
76 b95739dc Kevin Wolf
77 b95739dc Kevin Wolf
enum {
78 b95739dc Kevin Wolf
    CMD_READ_DMA    = 0xc8,
79 b95739dc Kevin Wolf
    CMD_WRITE_DMA   = 0xca,
80 bd07684a Kevin Wolf
    CMD_FLUSH_CACHE = 0xe7,
81 acbe4801 Kevin Wolf
    CMD_IDENTIFY    = 0xec,
82 948eaed1 Kevin Wolf
83 948eaed1 Kevin Wolf
    CMDF_ABORT      = 0x100,
84 d7b7e580 Kevin Wolf
    CMDF_NO_BM      = 0x200,
85 acbe4801 Kevin Wolf
};
86 acbe4801 Kevin Wolf
87 b95739dc Kevin Wolf
enum {
88 b95739dc Kevin Wolf
    BM_CMD_START    =  0x1,
89 b95739dc Kevin Wolf
    BM_CMD_WRITE    =  0x8, /* write = from device to memory */
90 b95739dc Kevin Wolf
};
91 b95739dc Kevin Wolf
92 b95739dc Kevin Wolf
enum {
93 b95739dc Kevin Wolf
    BM_STS_ACTIVE   =  0x1,
94 b95739dc Kevin Wolf
    BM_STS_ERROR    =  0x2,
95 b95739dc Kevin Wolf
    BM_STS_INTR     =  0x4,
96 b95739dc Kevin Wolf
};
97 b95739dc Kevin Wolf
98 b95739dc Kevin Wolf
enum {
99 b95739dc Kevin Wolf
    PRDT_EOT        = 0x80000000,
100 b95739dc Kevin Wolf
};
101 b95739dc Kevin Wolf
102 acbe4801 Kevin Wolf
#define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
103 acbe4801 Kevin Wolf
#define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
104 acbe4801 Kevin Wolf
105 b95739dc Kevin Wolf
static QPCIBus *pcibus = NULL;
106 b95739dc Kevin Wolf
static QGuestAllocator *guest_malloc;
107 b95739dc Kevin Wolf
108 acbe4801 Kevin Wolf
static char tmp_path[] = "/tmp/qtest.XXXXXX";
109 acbe4801 Kevin Wolf
110 acbe4801 Kevin Wolf
static void ide_test_start(const char *cmdline_fmt, ...)
111 acbe4801 Kevin Wolf
{
112 acbe4801 Kevin Wolf
    va_list ap;
113 acbe4801 Kevin Wolf
    char *cmdline;
114 acbe4801 Kevin Wolf
115 acbe4801 Kevin Wolf
    va_start(ap, cmdline_fmt);
116 acbe4801 Kevin Wolf
    cmdline = g_strdup_vprintf(cmdline_fmt, ap);
117 acbe4801 Kevin Wolf
    va_end(ap);
118 acbe4801 Kevin Wolf
119 acbe4801 Kevin Wolf
    qtest_start(cmdline);
120 acbe4801 Kevin Wolf
    qtest_irq_intercept_in(global_qtest, "ioapic");
121 b95739dc Kevin Wolf
    guest_malloc = pc_alloc_init();
122 acbe4801 Kevin Wolf
}
123 acbe4801 Kevin Wolf
124 acbe4801 Kevin Wolf
static void ide_test_quit(void)
125 acbe4801 Kevin Wolf
{
126 1d9358e6 Markus Armbruster
    qtest_end();
127 acbe4801 Kevin Wolf
}
128 acbe4801 Kevin Wolf
129 b95739dc Kevin Wolf
static QPCIDevice *get_pci_device(uint16_t *bmdma_base)
130 b95739dc Kevin Wolf
{
131 b95739dc Kevin Wolf
    QPCIDevice *dev;
132 b95739dc Kevin Wolf
    uint16_t vendor_id, device_id;
133 b95739dc Kevin Wolf
134 b95739dc Kevin Wolf
    if (!pcibus) {
135 b95739dc Kevin Wolf
        pcibus = qpci_init_pc();
136 b95739dc Kevin Wolf
    }
137 b95739dc Kevin Wolf
138 b95739dc Kevin Wolf
    /* Find PCI device and verify it's the right one */
139 b95739dc Kevin Wolf
    dev = qpci_device_find(pcibus, QPCI_DEVFN(IDE_PCI_DEV, IDE_PCI_FUNC));
140 b95739dc Kevin Wolf
    g_assert(dev != NULL);
141 b95739dc Kevin Wolf
142 b95739dc Kevin Wolf
    vendor_id = qpci_config_readw(dev, PCI_VENDOR_ID);
143 b95739dc Kevin Wolf
    device_id = qpci_config_readw(dev, PCI_DEVICE_ID);
144 b95739dc Kevin Wolf
    g_assert(vendor_id == PCI_VENDOR_ID_INTEL);
145 b95739dc Kevin Wolf
    g_assert(device_id == PCI_DEVICE_ID_INTEL_82371SB_1);
146 b95739dc Kevin Wolf
147 b95739dc Kevin Wolf
    /* Map bmdma BAR */
148 b95739dc Kevin Wolf
    *bmdma_base = (uint16_t)(uintptr_t) qpci_iomap(dev, 4);
149 b95739dc Kevin Wolf
150 b95739dc Kevin Wolf
    qpci_device_enable(dev);
151 b95739dc Kevin Wolf
152 b95739dc Kevin Wolf
    return dev;
153 b95739dc Kevin Wolf
}
154 b95739dc Kevin Wolf
155 b95739dc Kevin Wolf
static void free_pci_device(QPCIDevice *dev)
156 b95739dc Kevin Wolf
{
157 b95739dc Kevin Wolf
    /* libqos doesn't have a function for this, so free it manually */
158 b95739dc Kevin Wolf
    g_free(dev);
159 b95739dc Kevin Wolf
}
160 b95739dc Kevin Wolf
161 b95739dc Kevin Wolf
typedef struct PrdtEntry {
162 b95739dc Kevin Wolf
    uint32_t addr;
163 b95739dc Kevin Wolf
    uint32_t size;
164 b95739dc Kevin Wolf
} QEMU_PACKED PrdtEntry;
165 b95739dc Kevin Wolf
166 b95739dc Kevin Wolf
#define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
167 b95739dc Kevin Wolf
#define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
168 b95739dc Kevin Wolf
169 b95739dc Kevin Wolf
static int send_dma_request(int cmd, uint64_t sector, int nb_sectors,
170 b95739dc Kevin Wolf
                            PrdtEntry *prdt, int prdt_entries)
171 b95739dc Kevin Wolf
{
172 b95739dc Kevin Wolf
    QPCIDevice *dev;
173 b95739dc Kevin Wolf
    uint16_t bmdma_base;
174 b95739dc Kevin Wolf
    uintptr_t guest_prdt;
175 b95739dc Kevin Wolf
    size_t len;
176 b95739dc Kevin Wolf
    bool from_dev;
177 b95739dc Kevin Wolf
    uint8_t status;
178 948eaed1 Kevin Wolf
    int flags;
179 b95739dc Kevin Wolf
180 b95739dc Kevin Wolf
    dev = get_pci_device(&bmdma_base);
181 b95739dc Kevin Wolf
182 948eaed1 Kevin Wolf
    flags = cmd & ~0xff;
183 948eaed1 Kevin Wolf
    cmd &= 0xff;
184 948eaed1 Kevin Wolf
185 b95739dc Kevin Wolf
    switch (cmd) {
186 b95739dc Kevin Wolf
    case CMD_READ_DMA:
187 b95739dc Kevin Wolf
        from_dev = true;
188 b95739dc Kevin Wolf
        break;
189 b95739dc Kevin Wolf
    case CMD_WRITE_DMA:
190 b95739dc Kevin Wolf
        from_dev = false;
191 b95739dc Kevin Wolf
        break;
192 b95739dc Kevin Wolf
    default:
193 b95739dc Kevin Wolf
        g_assert_not_reached();
194 b95739dc Kevin Wolf
    }
195 b95739dc Kevin Wolf
196 d7b7e580 Kevin Wolf
    if (flags & CMDF_NO_BM) {
197 d7b7e580 Kevin Wolf
        qpci_config_writew(dev, PCI_COMMAND,
198 d7b7e580 Kevin Wolf
                           PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
199 d7b7e580 Kevin Wolf
    }
200 d7b7e580 Kevin Wolf
201 b95739dc Kevin Wolf
    /* Select device 0 */
202 b95739dc Kevin Wolf
    outb(IDE_BASE + reg_device, 0 | LBA);
203 b95739dc Kevin Wolf
204 b95739dc Kevin Wolf
    /* Stop any running transfer, clear any pending interrupt */
205 b95739dc Kevin Wolf
    outb(bmdma_base + bmreg_cmd, 0);
206 b95739dc Kevin Wolf
    outb(bmdma_base + bmreg_status, BM_STS_INTR);
207 b95739dc Kevin Wolf
208 b95739dc Kevin Wolf
    /* Setup PRDT */
209 b95739dc Kevin Wolf
    len = sizeof(*prdt) * prdt_entries;
210 b95739dc Kevin Wolf
    guest_prdt = guest_alloc(guest_malloc, len);
211 b95739dc Kevin Wolf
    memwrite(guest_prdt, prdt, len);
212 b95739dc Kevin Wolf
    outl(bmdma_base + bmreg_prdt, guest_prdt);
213 b95739dc Kevin Wolf
214 b95739dc Kevin Wolf
    /* ATA DMA command */
215 b95739dc Kevin Wolf
    outb(IDE_BASE + reg_nsectors, nb_sectors);
216 b95739dc Kevin Wolf
217 b95739dc Kevin Wolf
    outb(IDE_BASE + reg_lba_low,    sector & 0xff);
218 b95739dc Kevin Wolf
    outb(IDE_BASE + reg_lba_middle, (sector >> 8) & 0xff);
219 b95739dc Kevin Wolf
    outb(IDE_BASE + reg_lba_high,   (sector >> 16) & 0xff);
220 b95739dc Kevin Wolf
221 b95739dc Kevin Wolf
    outb(IDE_BASE + reg_command, cmd);
222 b95739dc Kevin Wolf
223 b95739dc Kevin Wolf
    /* Start DMA transfer */
224 b95739dc Kevin Wolf
    outb(bmdma_base + bmreg_cmd, BM_CMD_START | (from_dev ? BM_CMD_WRITE : 0));
225 b95739dc Kevin Wolf
226 948eaed1 Kevin Wolf
    if (flags & CMDF_ABORT) {
227 948eaed1 Kevin Wolf
        outb(bmdma_base + bmreg_cmd, 0);
228 948eaed1 Kevin Wolf
    }
229 948eaed1 Kevin Wolf
230 b95739dc Kevin Wolf
    /* Wait for the DMA transfer to complete */
231 b95739dc Kevin Wolf
    do {
232 b95739dc Kevin Wolf
        status = inb(bmdma_base + bmreg_status);
233 b95739dc Kevin Wolf
    } while ((status & (BM_STS_ACTIVE | BM_STS_INTR)) == BM_STS_ACTIVE);
234 b95739dc Kevin Wolf
235 b95739dc Kevin Wolf
    g_assert_cmpint(get_irq(IDE_PRIMARY_IRQ), ==, !!(status & BM_STS_INTR));
236 b95739dc Kevin Wolf
237 b95739dc Kevin Wolf
    /* Check IDE status code */
238 b95739dc Kevin Wolf
    assert_bit_set(inb(IDE_BASE + reg_status), DRDY);
239 b95739dc Kevin Wolf
    assert_bit_clear(inb(IDE_BASE + reg_status), BSY | DRQ);
240 b95739dc Kevin Wolf
241 b95739dc Kevin Wolf
    /* Reading the status register clears the IRQ */
242 b95739dc Kevin Wolf
    g_assert(!get_irq(IDE_PRIMARY_IRQ));
243 b95739dc Kevin Wolf
244 b95739dc Kevin Wolf
    /* Stop DMA transfer if still active */
245 b95739dc Kevin Wolf
    if (status & BM_STS_ACTIVE) {
246 b95739dc Kevin Wolf
        outb(bmdma_base + bmreg_cmd, 0);
247 b95739dc Kevin Wolf
    }
248 b95739dc Kevin Wolf
249 b95739dc Kevin Wolf
    free_pci_device(dev);
250 b95739dc Kevin Wolf
251 b95739dc Kevin Wolf
    return status;
252 b95739dc Kevin Wolf
}
253 b95739dc Kevin Wolf
254 b95739dc Kevin Wolf
static void test_bmdma_simple_rw(void)
255 b95739dc Kevin Wolf
{
256 b95739dc Kevin Wolf
    uint8_t status;
257 b95739dc Kevin Wolf
    uint8_t *buf;
258 b95739dc Kevin Wolf
    uint8_t *cmpbuf;
259 b95739dc Kevin Wolf
    size_t len = 512;
260 b95739dc Kevin Wolf
    uintptr_t guest_buf = guest_alloc(guest_malloc, len);
261 b95739dc Kevin Wolf
262 b95739dc Kevin Wolf
    PrdtEntry prdt[] = {
263 262f27b9 Kevin Wolf
        {
264 262f27b9 Kevin Wolf
            .addr = cpu_to_le32(guest_buf),
265 262f27b9 Kevin Wolf
            .size = cpu_to_le32(len | PRDT_EOT),
266 262f27b9 Kevin Wolf
        },
267 b95739dc Kevin Wolf
    };
268 b95739dc Kevin Wolf
269 b95739dc Kevin Wolf
    buf = g_malloc(len);
270 b95739dc Kevin Wolf
    cmpbuf = g_malloc(len);
271 b95739dc Kevin Wolf
272 b95739dc Kevin Wolf
    /* Write 0x55 pattern to sector 0 */
273 b95739dc Kevin Wolf
    memset(buf, 0x55, len);
274 b95739dc Kevin Wolf
    memwrite(guest_buf, buf, len);
275 b95739dc Kevin Wolf
276 b95739dc Kevin Wolf
    status = send_dma_request(CMD_WRITE_DMA, 0, 1, prdt, ARRAY_SIZE(prdt));
277 b95739dc Kevin Wolf
    g_assert_cmphex(status, ==, BM_STS_INTR);
278 b95739dc Kevin Wolf
    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
279 b95739dc Kevin Wolf
280 b95739dc Kevin Wolf
    /* Write 0xaa pattern to sector 1 */
281 b95739dc Kevin Wolf
    memset(buf, 0xaa, len);
282 b95739dc Kevin Wolf
    memwrite(guest_buf, buf, len);
283 b95739dc Kevin Wolf
284 b95739dc Kevin Wolf
    status = send_dma_request(CMD_WRITE_DMA, 1, 1, prdt, ARRAY_SIZE(prdt));
285 b95739dc Kevin Wolf
    g_assert_cmphex(status, ==, BM_STS_INTR);
286 b95739dc Kevin Wolf
    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
287 b95739dc Kevin Wolf
288 b95739dc Kevin Wolf
    /* Read and verify 0x55 pattern in sector 0 */
289 b95739dc Kevin Wolf
    memset(cmpbuf, 0x55, len);
290 b95739dc Kevin Wolf
291 b95739dc Kevin Wolf
    status = send_dma_request(CMD_READ_DMA, 0, 1, prdt, ARRAY_SIZE(prdt));
292 b95739dc Kevin Wolf
    g_assert_cmphex(status, ==, BM_STS_INTR);
293 b95739dc Kevin Wolf
    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
294 b95739dc Kevin Wolf
295 b95739dc Kevin Wolf
    memread(guest_buf, buf, len);
296 b95739dc Kevin Wolf
    g_assert(memcmp(buf, cmpbuf, len) == 0);
297 b95739dc Kevin Wolf
298 b95739dc Kevin Wolf
    /* Read and verify 0xaa pattern in sector 1 */
299 b95739dc Kevin Wolf
    memset(cmpbuf, 0xaa, len);
300 b95739dc Kevin Wolf
301 b95739dc Kevin Wolf
    status = send_dma_request(CMD_READ_DMA, 1, 1, prdt, ARRAY_SIZE(prdt));
302 b95739dc Kevin Wolf
    g_assert_cmphex(status, ==, BM_STS_INTR);
303 b95739dc Kevin Wolf
    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
304 b95739dc Kevin Wolf
305 b95739dc Kevin Wolf
    memread(guest_buf, buf, len);
306 b95739dc Kevin Wolf
    g_assert(memcmp(buf, cmpbuf, len) == 0);
307 b95739dc Kevin Wolf
308 b95739dc Kevin Wolf
309 b95739dc Kevin Wolf
    g_free(buf);
310 b95739dc Kevin Wolf
    g_free(cmpbuf);
311 b95739dc Kevin Wolf
}
312 b95739dc Kevin Wolf
313 948eaed1 Kevin Wolf
static void test_bmdma_short_prdt(void)
314 948eaed1 Kevin Wolf
{
315 948eaed1 Kevin Wolf
    uint8_t status;
316 948eaed1 Kevin Wolf
317 948eaed1 Kevin Wolf
    PrdtEntry prdt[] = {
318 262f27b9 Kevin Wolf
        {
319 262f27b9 Kevin Wolf
            .addr = 0,
320 262f27b9 Kevin Wolf
            .size = cpu_to_le32(0x10 | PRDT_EOT),
321 262f27b9 Kevin Wolf
        },
322 948eaed1 Kevin Wolf
    };
323 948eaed1 Kevin Wolf
324 948eaed1 Kevin Wolf
    /* Normal request */
325 948eaed1 Kevin Wolf
    status = send_dma_request(CMD_READ_DMA, 0, 1,
326 948eaed1 Kevin Wolf
                              prdt, ARRAY_SIZE(prdt));
327 948eaed1 Kevin Wolf
    g_assert_cmphex(status, ==, 0);
328 948eaed1 Kevin Wolf
    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
329 948eaed1 Kevin Wolf
330 948eaed1 Kevin Wolf
    /* Abort the request before it completes */
331 948eaed1 Kevin Wolf
    status = send_dma_request(CMD_READ_DMA | CMDF_ABORT, 0, 1,
332 948eaed1 Kevin Wolf
                              prdt, ARRAY_SIZE(prdt));
333 948eaed1 Kevin Wolf
    g_assert_cmphex(status, ==, 0);
334 948eaed1 Kevin Wolf
    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
335 948eaed1 Kevin Wolf
}
336 948eaed1 Kevin Wolf
337 948eaed1 Kevin Wolf
static void test_bmdma_long_prdt(void)
338 948eaed1 Kevin Wolf
{
339 948eaed1 Kevin Wolf
    uint8_t status;
340 948eaed1 Kevin Wolf
341 948eaed1 Kevin Wolf
    PrdtEntry prdt[] = {
342 262f27b9 Kevin Wolf
        {
343 262f27b9 Kevin Wolf
            .addr = 0,
344 262f27b9 Kevin Wolf
            .size = cpu_to_le32(0x1000 | PRDT_EOT),
345 262f27b9 Kevin Wolf
        },
346 948eaed1 Kevin Wolf
    };
347 948eaed1 Kevin Wolf
348 948eaed1 Kevin Wolf
    /* Normal request */
349 948eaed1 Kevin Wolf
    status = send_dma_request(CMD_READ_DMA, 0, 1,
350 948eaed1 Kevin Wolf
                              prdt, ARRAY_SIZE(prdt));
351 948eaed1 Kevin Wolf
    g_assert_cmphex(status, ==, BM_STS_ACTIVE | BM_STS_INTR);
352 948eaed1 Kevin Wolf
    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
353 948eaed1 Kevin Wolf
354 948eaed1 Kevin Wolf
    /* Abort the request before it completes */
355 948eaed1 Kevin Wolf
    status = send_dma_request(CMD_READ_DMA | CMDF_ABORT, 0, 1,
356 948eaed1 Kevin Wolf
                              prdt, ARRAY_SIZE(prdt));
357 948eaed1 Kevin Wolf
    g_assert_cmphex(status, ==, BM_STS_INTR);
358 948eaed1 Kevin Wolf
    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
359 948eaed1 Kevin Wolf
}
360 948eaed1 Kevin Wolf
361 d7b7e580 Kevin Wolf
static void test_bmdma_no_busmaster(void)
362 d7b7e580 Kevin Wolf
{
363 d7b7e580 Kevin Wolf
    uint8_t status;
364 d7b7e580 Kevin Wolf
365 d7b7e580 Kevin Wolf
    /* No PRDT_EOT, each entry addr 0/size 64k, and in theory qemu shouldn't be
366 d7b7e580 Kevin Wolf
     * able to access it anyway because the Bus Master bit in the PCI command
367 d7b7e580 Kevin Wolf
     * register isn't set. This is complete nonsense, but it used to be pretty
368 d7b7e580 Kevin Wolf
     * good at confusing and occasionally crashing qemu. */
369 d7b7e580 Kevin Wolf
    PrdtEntry prdt[4096] = { };
370 d7b7e580 Kevin Wolf
371 d7b7e580 Kevin Wolf
    status = send_dma_request(CMD_READ_DMA | CMDF_NO_BM, 0, 512,
372 d7b7e580 Kevin Wolf
                              prdt, ARRAY_SIZE(prdt));
373 d7b7e580 Kevin Wolf
374 d7b7e580 Kevin Wolf
    /* Not entirely clear what the expected result is, but this is what we get
375 d7b7e580 Kevin Wolf
     * in practice. At least we want to be aware of any changes. */
376 d7b7e580 Kevin Wolf
    g_assert_cmphex(status, ==, BM_STS_ACTIVE | BM_STS_INTR);
377 d7b7e580 Kevin Wolf
    assert_bit_clear(inb(IDE_BASE + reg_status), DF | ERR);
378 d7b7e580 Kevin Wolf
}
379 d7b7e580 Kevin Wolf
380 b95739dc Kevin Wolf
static void test_bmdma_setup(void)
381 b95739dc Kevin Wolf
{
382 b95739dc Kevin Wolf
    ide_test_start(
383 b95739dc Kevin Wolf
        "-drive file=%s,if=ide,serial=%s,cache=writeback "
384 b95739dc Kevin Wolf
        "-global ide-hd.ver=%s",
385 b95739dc Kevin Wolf
        tmp_path, "testdisk", "version");
386 b95739dc Kevin Wolf
}
387 b95739dc Kevin Wolf
388 b95739dc Kevin Wolf
static void test_bmdma_teardown(void)
389 b95739dc Kevin Wolf
{
390 b95739dc Kevin Wolf
    ide_test_quit();
391 b95739dc Kevin Wolf
}
392 b95739dc Kevin Wolf
393 262f27b9 Kevin Wolf
static void string_cpu_to_be16(uint16_t *s, size_t bytes)
394 262f27b9 Kevin Wolf
{
395 262f27b9 Kevin Wolf
    g_assert((bytes & 1) == 0);
396 262f27b9 Kevin Wolf
    bytes /= 2;
397 262f27b9 Kevin Wolf
398 262f27b9 Kevin Wolf
    while (bytes--) {
399 262f27b9 Kevin Wolf
        *s = cpu_to_be16(*s);
400 262f27b9 Kevin Wolf
        s++;
401 262f27b9 Kevin Wolf
    }
402 262f27b9 Kevin Wolf
}
403 262f27b9 Kevin Wolf
404 acbe4801 Kevin Wolf
static void test_identify(void)
405 acbe4801 Kevin Wolf
{
406 acbe4801 Kevin Wolf
    uint8_t data;
407 acbe4801 Kevin Wolf
    uint16_t buf[256];
408 acbe4801 Kevin Wolf
    int i;
409 acbe4801 Kevin Wolf
    int ret;
410 acbe4801 Kevin Wolf
411 acbe4801 Kevin Wolf
    ide_test_start(
412 acbe4801 Kevin Wolf
        "-drive file=%s,if=ide,serial=%s,cache=writeback "
413 acbe4801 Kevin Wolf
        "-global ide-hd.ver=%s",
414 acbe4801 Kevin Wolf
        tmp_path, "testdisk", "version");
415 acbe4801 Kevin Wolf
416 acbe4801 Kevin Wolf
    /* IDENTIFY command on device 0*/
417 acbe4801 Kevin Wolf
    outb(IDE_BASE + reg_device, 0);
418 acbe4801 Kevin Wolf
    outb(IDE_BASE + reg_command, CMD_IDENTIFY);
419 acbe4801 Kevin Wolf
420 acbe4801 Kevin Wolf
    /* Read in the IDENTIFY buffer and check registers */
421 acbe4801 Kevin Wolf
    data = inb(IDE_BASE + reg_device);
422 c27d5656 Kevin Wolf
    g_assert_cmpint(data & DEV, ==, 0);
423 acbe4801 Kevin Wolf
424 acbe4801 Kevin Wolf
    for (i = 0; i < 256; i++) {
425 acbe4801 Kevin Wolf
        data = inb(IDE_BASE + reg_status);
426 acbe4801 Kevin Wolf
        assert_bit_set(data, DRDY | DRQ);
427 acbe4801 Kevin Wolf
        assert_bit_clear(data, BSY | DF | ERR);
428 acbe4801 Kevin Wolf
429 acbe4801 Kevin Wolf
        ((uint16_t*) buf)[i] = inw(IDE_BASE + reg_data);
430 acbe4801 Kevin Wolf
    }
431 acbe4801 Kevin Wolf
432 acbe4801 Kevin Wolf
    data = inb(IDE_BASE + reg_status);
433 acbe4801 Kevin Wolf
    assert_bit_set(data, DRDY);
434 acbe4801 Kevin Wolf
    assert_bit_clear(data, BSY | DF | ERR | DRQ);
435 acbe4801 Kevin Wolf
436 acbe4801 Kevin Wolf
    /* Check serial number/version in the buffer */
437 262f27b9 Kevin Wolf
    string_cpu_to_be16(&buf[10], 20);
438 262f27b9 Kevin Wolf
    ret = memcmp(&buf[10], "testdisk            ", 20);
439 acbe4801 Kevin Wolf
    g_assert(ret == 0);
440 acbe4801 Kevin Wolf
441 262f27b9 Kevin Wolf
    string_cpu_to_be16(&buf[23], 8);
442 262f27b9 Kevin Wolf
    ret = memcmp(&buf[23], "version ", 8);
443 acbe4801 Kevin Wolf
    g_assert(ret == 0);
444 acbe4801 Kevin Wolf
445 acbe4801 Kevin Wolf
    /* Write cache enabled bit */
446 acbe4801 Kevin Wolf
    assert_bit_set(buf[85], 0x20);
447 acbe4801 Kevin Wolf
448 acbe4801 Kevin Wolf
    ide_test_quit();
449 acbe4801 Kevin Wolf
}
450 acbe4801 Kevin Wolf
451 bd07684a Kevin Wolf
static void test_flush(void)
452 bd07684a Kevin Wolf
{
453 bd07684a Kevin Wolf
    uint8_t data;
454 bd07684a Kevin Wolf
455 bd07684a Kevin Wolf
    ide_test_start(
456 bd07684a Kevin Wolf
        "-drive file=blkdebug::%s,if=ide,cache=writeback",
457 bd07684a Kevin Wolf
        tmp_path);
458 bd07684a Kevin Wolf
459 bd07684a Kevin Wolf
    /* Delay the completion of the flush request until we explicitly do it */
460 0d1aa05e Stefan Hajnoczi
    qmp_discard_response("{'execute':'human-monitor-command', 'arguments': {"
461 0d1aa05e Stefan Hajnoczi
                         " 'command-line':"
462 0d1aa05e Stefan Hajnoczi
                         " 'qemu-io ide0-hd0 \"break flush_to_os A\"'} }");
463 bd07684a Kevin Wolf
464 bd07684a Kevin Wolf
    /* FLUSH CACHE command on device 0*/
465 bd07684a Kevin Wolf
    outb(IDE_BASE + reg_device, 0);
466 bd07684a Kevin Wolf
    outb(IDE_BASE + reg_command, CMD_FLUSH_CACHE);
467 bd07684a Kevin Wolf
468 bd07684a Kevin Wolf
    /* Check status while request is in flight*/
469 bd07684a Kevin Wolf
    data = inb(IDE_BASE + reg_status);
470 bd07684a Kevin Wolf
    assert_bit_set(data, BSY | DRDY);
471 bd07684a Kevin Wolf
    assert_bit_clear(data, DF | ERR | DRQ);
472 bd07684a Kevin Wolf
473 bd07684a Kevin Wolf
    /* Complete the command */
474 0d1aa05e Stefan Hajnoczi
    qmp_discard_response("{'execute':'human-monitor-command', 'arguments': {"
475 0d1aa05e Stefan Hajnoczi
                         " 'command-line':"
476 0d1aa05e Stefan Hajnoczi
                         " 'qemu-io ide0-hd0 \"resume A\"'} }");
477 bd07684a Kevin Wolf
478 bd07684a Kevin Wolf
    /* Check registers */
479 bd07684a Kevin Wolf
    data = inb(IDE_BASE + reg_device);
480 bd07684a Kevin Wolf
    g_assert_cmpint(data & DEV, ==, 0);
481 bd07684a Kevin Wolf
482 22bfa16e Michael Roth
    do {
483 22bfa16e Michael Roth
        data = inb(IDE_BASE + reg_status);
484 22bfa16e Michael Roth
    } while (data & BSY);
485 22bfa16e Michael Roth
486 bd07684a Kevin Wolf
    assert_bit_set(data, DRDY);
487 bd07684a Kevin Wolf
    assert_bit_clear(data, BSY | DF | ERR | DRQ);
488 bd07684a Kevin Wolf
489 bd07684a Kevin Wolf
    ide_test_quit();
490 bd07684a Kevin Wolf
}
491 bd07684a Kevin Wolf
492 acbe4801 Kevin Wolf
int main(int argc, char **argv)
493 acbe4801 Kevin Wolf
{
494 acbe4801 Kevin Wolf
    const char *arch = qtest_get_arch();
495 acbe4801 Kevin Wolf
    int fd;
496 acbe4801 Kevin Wolf
    int ret;
497 acbe4801 Kevin Wolf
498 acbe4801 Kevin Wolf
    /* Check architecture */
499 acbe4801 Kevin Wolf
    if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
500 acbe4801 Kevin Wolf
        g_test_message("Skipping test for non-x86\n");
501 acbe4801 Kevin Wolf
        return 0;
502 acbe4801 Kevin Wolf
    }
503 acbe4801 Kevin Wolf
504 acbe4801 Kevin Wolf
    /* Create a temporary raw image */
505 acbe4801 Kevin Wolf
    fd = mkstemp(tmp_path);
506 acbe4801 Kevin Wolf
    g_assert(fd >= 0);
507 acbe4801 Kevin Wolf
    ret = ftruncate(fd, TEST_IMAGE_SIZE);
508 acbe4801 Kevin Wolf
    g_assert(ret == 0);
509 acbe4801 Kevin Wolf
    close(fd);
510 acbe4801 Kevin Wolf
511 acbe4801 Kevin Wolf
    /* Run the tests */
512 acbe4801 Kevin Wolf
    g_test_init(&argc, &argv, NULL);
513 acbe4801 Kevin Wolf
514 acbe4801 Kevin Wolf
    qtest_add_func("/ide/identify", test_identify);
515 acbe4801 Kevin Wolf
516 b95739dc Kevin Wolf
    qtest_add_func("/ide/bmdma/setup", test_bmdma_setup);
517 b95739dc Kevin Wolf
    qtest_add_func("/ide/bmdma/simple_rw", test_bmdma_simple_rw);
518 948eaed1 Kevin Wolf
    qtest_add_func("/ide/bmdma/short_prdt", test_bmdma_short_prdt);
519 948eaed1 Kevin Wolf
    qtest_add_func("/ide/bmdma/long_prdt", test_bmdma_long_prdt);
520 d7b7e580 Kevin Wolf
    qtest_add_func("/ide/bmdma/no_busmaster", test_bmdma_no_busmaster);
521 b95739dc Kevin Wolf
    qtest_add_func("/ide/bmdma/teardown", test_bmdma_teardown);
522 b95739dc Kevin Wolf
523 bd07684a Kevin Wolf
    qtest_add_func("/ide/flush", test_flush);
524 bd07684a Kevin Wolf
525 acbe4801 Kevin Wolf
    ret = g_test_run();
526 acbe4801 Kevin Wolf
527 acbe4801 Kevin Wolf
    /* Cleanup */
528 acbe4801 Kevin Wolf
    unlink(tmp_path);
529 acbe4801 Kevin Wolf
530 acbe4801 Kevin Wolf
    return ret;
531 acbe4801 Kevin Wolf
}