ahci-test.c 38.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * AHCI test cases
 *
 * Copyright (c) 2014 John Snow <jsnow@redhat.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <stdint.h>
#include <string.h>
#include <stdio.h>
28
#include <getopt.h>
29 30 31
#include <glib.h>

#include "libqtest.h"
J
John Snow 已提交
32
#include "libqos/libqos-pc.h"
J
John Snow 已提交
33
#include "libqos/ahci.h"
34 35 36 37 38 39 40 41 42 43 44 45 46
#include "libqos/pci-pc.h"

#include "qemu-common.h"
#include "qemu/host-utils.h"

#include "hw/pci/pci_ids.h"
#include "hw/pci/pci_regs.h"

/* Test-specific defines. */
#define TEST_IMAGE_SIZE    (64 * 1024 * 1024)

/*** Globals ***/
static char tmp_path[] = "/tmp/qtest.XXXXXX";
47 48
static bool ahci_pedantic;

49
/*** Function Declarations ***/
50
static QPCIDevice *get_ahci_device(uint32_t *fingerprint);
51
static void start_ahci_device(AHCIQState *ahci);
52
static void free_ahci_device(QPCIDevice *dev);
53

54
static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port);
55 56
static void ahci_test_pci_spec(AHCIQState *ahci);
static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
57
                               uint8_t offset);
58 59 60
static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset);
static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset);
static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset);
61 62 63

/*** Utilities ***/

64 65 66 67 68 69 70 71 72 73 74
static void string_bswap16(uint16_t *s, size_t bytes)
{
    g_assert_cmphex((bytes & 1), ==, 0);
    bytes /= 2;

    while (bytes--) {
        *s = bswap16(*s);
        s++;
    }
}

75 76 77 78 79
static uint64_t ahci_alloc(AHCIQState *ahci, size_t bytes)
{
    return qmalloc(ahci->parent, bytes);
}

80 81 82
/**
 * Locate, verify, and return a handle to the AHCI device.
 */
83
static QPCIDevice *get_ahci_device(uint32_t *fingerprint)
84 85
{
    QPCIDevice *ahci;
86
    uint32_t ahci_fingerprint;
J
John Snow 已提交
87
    QPCIBus *pcibus;
88 89 90 91 92 93 94

    pcibus = qpci_init_pc();

    /* Find the AHCI PCI device and verify it's the right one. */
    ahci = qpci_device_find(pcibus, QPCI_DEVFN(0x1F, 0x02));
    g_assert(ahci != NULL);

95
    ahci_fingerprint = qpci_config_readl(ahci, PCI_VENDOR_ID);
96

97 98 99 100 101 102 103
    switch (ahci_fingerprint) {
    case AHCI_INTEL_ICH9:
        break;
    default:
        /* Unknown device. */
        g_assert_not_reached();
    }
104

105 106 107
    if (fingerprint) {
        *fingerprint = ahci_fingerprint;
    }
108 109 110
    return ahci;
}

J
John Snow 已提交
111
static void free_ahci_device(QPCIDevice *dev)
112
{
J
John Snow 已提交
113
    QPCIBus *pcibus = dev ? dev->bus : NULL;
114

J
John Snow 已提交
115 116 117
    /* libqos doesn't have a function for this, so free it manually */
    g_free(dev);
    qpci_free_pc(pcibus);
118 119 120 121 122
}

/*** Test Setup & Teardown ***/

/**
J
John Snow 已提交
123
 * Start a Q35 machine and bookmark a handle to the AHCI device.
124
 */
J
John Snow 已提交
125
static AHCIQState *ahci_boot(void)
126
{
J
John Snow 已提交
127 128
    AHCIQState *s;
    const char *cli;
129

J
John Snow 已提交
130
    s = g_malloc0(sizeof(AHCIQState));
131

J
John Snow 已提交
132 133 134 135 136
    cli = "-drive if=none,id=drive0,file=%s,cache=writeback,serial=%s"
        ",format=raw"
        " -M q35 "
        "-device ide-hd,drive=drive0 "
        "-global ide-hd.ver=%s";
J
John Snow 已提交
137
    s->parent = qtest_pc_boot(cli, tmp_path, "testdisk", "version");
138

J
John Snow 已提交
139
    /* Verify that we have an AHCI device present. */
140
    s->dev = get_ahci_device(&s->fingerprint);
141

J
John Snow 已提交
142
    return s;
143 144 145 146 147
}

/**
 * Clean up the PCI device, then terminate the QEMU instance.
 */
J
John Snow 已提交
148
static void ahci_shutdown(AHCIQState *ahci)
149
{
J
John Snow 已提交
150 151 152 153
    QOSState *qs = ahci->parent;
    free_ahci_device(ahci->dev);
    g_free(ahci);
    qtest_shutdown(qs);
154 155
}

156 157 158 159 160
/*** Logical Device Initialization ***/

/**
 * Start the PCI device and sanity-check default operation.
 */
161
static void ahci_pci_enable(AHCIQState *ahci)
162 163 164
{
    uint8_t reg;

165
    start_ahci_device(ahci);
166

167
    switch (ahci->fingerprint) {
168 169 170
    case AHCI_INTEL_ICH9:
        /* ICH9 has a register at PCI 0x92 that
         * acts as a master port enabler mask. */
171
        reg = qpci_config_readb(ahci->dev, 0x92);
172
        reg |= 0x3F;
173
        qpci_config_writeb(ahci->dev, 0x92, reg);
174
        /* 0...0111111b -- bit significant, ports 0-5 enabled. */
175
        ASSERT_BIT_SET(qpci_config_readb(ahci->dev, 0x92), 0x3F);
176 177 178 179 180 181 182 183
        break;
    }

}

/**
 * Map BAR5/ABAR, and engage the PCI device.
 */
184
static void start_ahci_device(AHCIQState *ahci)
185 186
{
    /* Map AHCI's ABAR (BAR5) */
187
    ahci->hba_base = qpci_iomap(ahci->dev, 5, &ahci->barsize);
188 189

    /* turns on pci.cmd.iose, pci.cmd.mse and pci.cmd.bme */
190
    qpci_device_enable(ahci->dev);
191 192
}

193 194 195 196 197
/**
 * Test and initialize the AHCI's HBA memory areas.
 * Initialize and start any ports with devices attached.
 * Bring the HBA into the idle state.
 */
198
static void ahci_hba_enable(AHCIQState *ahci)
199 200 201 202 203 204 205 206 207 208 209 210 211 212
{
    /* Bits of interest in this section:
     * GHC.AE     Global Host Control / AHCI Enable
     * PxCMD.ST   Port Command: Start
     * PxCMD.SUD  "Spin Up Device"
     * PxCMD.POD  "Power On Device"
     * PxCMD.FRE  "FIS Receive Enable"
     * PxCMD.FR   "FIS Receive Running"
     * PxCMD.CR   "Command List Running"
     */
    uint32_t reg, ports_impl, clb, fb;
    uint16_t i;
    uint8_t num_cmd_slots;

213 214
    g_assert(ahci != NULL);

215
    /* Set GHC.AE to 1 */
216 217
    ahci_set(ahci, AHCI_GHC, AHCI_GHC_AE);
    reg = ahci_rreg(ahci, AHCI_GHC);
218 219
    ASSERT_BIT_SET(reg, AHCI_GHC_AE);

220
    /* Cache CAP and CAP2. */
221 222
    ahci->cap = ahci_rreg(ahci, AHCI_CAP);
    ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
223

224
    /* Read CAP.NCS, how many command slots do we have? */
225
    num_cmd_slots = ((ahci->cap & AHCI_CAP_NCS) >> ctzl(AHCI_CAP_NCS)) + 1;
226 227 228
    g_test_message("Number of Command Slots: %u", num_cmd_slots);

    /* Determine which ports are implemented. */
229
    ports_impl = ahci_rreg(ahci, AHCI_PI);
230 231 232 233 234 235 236 237

    for (i = 0; ports_impl; ports_impl >>= 1, ++i) {
        if (!(ports_impl & 0x01)) {
            continue;
        }

        g_test_message("Initializing port %u", i);

238
        reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
239 240 241 242 243
        if (BITCLR(reg, AHCI_PX_CMD_ST | AHCI_PX_CMD_CR |
                   AHCI_PX_CMD_FRE | AHCI_PX_CMD_FR)) {
            g_test_message("port is idle");
        } else {
            g_test_message("port needs to be idled");
244 245
            ahci_px_clr(ahci, i, AHCI_PX_CMD,
                        (AHCI_PX_CMD_ST | AHCI_PX_CMD_FRE));
246 247
            /* The port has 500ms to disengage. */
            usleep(500000);
248
            reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
249 250 251 252 253 254 255 256 257
            ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
            ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
            g_test_message("port is now idle");
            /* The spec does allow for possibly needing a PORT RESET
             * or HBA reset if we fail to idle the port. */
        }

        /* Allocate Memory for the Command List Buffer & FIS Buffer */
        /* PxCLB space ... 0x20 per command, as in 4.2.2 p 36 */
258
        clb = ahci_alloc(ahci, num_cmd_slots * 0x20);
259
        g_test_message("CLB: 0x%08x", clb);
260 261
        ahci_px_wreg(ahci, i, AHCI_PX_CLB, clb);
        g_assert_cmphex(clb, ==, ahci_px_rreg(ahci, i, AHCI_PX_CLB));
262 263

        /* PxFB space ... 0x100, as in 4.2.1 p 35 */
264
        fb = ahci_alloc(ahci, 0x100);
265
        g_test_message("FB: 0x%08x", fb);
266 267
        ahci_px_wreg(ahci, i, AHCI_PX_FB, fb);
        g_assert_cmphex(fb, ==, ahci_px_rreg(ahci, i, AHCI_PX_FB));
268 269

        /* Clear PxSERR, PxIS, then IS.IPS[x] by writing '1's. */
270 271 272
        ahci_px_wreg(ahci, i, AHCI_PX_SERR, 0xFFFFFFFF);
        ahci_px_wreg(ahci, i, AHCI_PX_IS, 0xFFFFFFFF);
        ahci_wreg(ahci, AHCI_IS, (1 << i));
273 274

        /* Verify Interrupts Cleared */
275
        reg = ahci_px_rreg(ahci, i, AHCI_PX_SERR);
276 277
        g_assert_cmphex(reg, ==, 0);

278
        reg = ahci_px_rreg(ahci, i, AHCI_PX_IS);
279 280
        g_assert_cmphex(reg, ==, 0);

281
        reg = ahci_rreg(ahci, AHCI_IS);
282 283 284
        ASSERT_BIT_CLEAR(reg, (1 << i));

        /* Enable All Interrupts: */
285 286
        ahci_px_wreg(ahci, i, AHCI_PX_IE, 0xFFFFFFFF);
        reg = ahci_px_rreg(ahci, i, AHCI_PX_IE);
287 288 289
        g_assert_cmphex(reg, ==, ~((uint32_t)AHCI_PX_IE_RESERVED));

        /* Enable the FIS Receive Engine. */
290 291
        ahci_px_set(ahci, i, AHCI_PX_CMD, AHCI_PX_CMD_FRE);
        reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
292 293 294 295 296
        ASSERT_BIT_SET(reg, AHCI_PX_CMD_FR);

        /* AHCI 1.3 spec: if !STS.BSY, !STS.DRQ and PxSSTS.DET indicates
         * physical presence, a device is present and may be started. However,
         * PxSERR.DIAG.X /may/ need to be cleared a priori. */
297
        reg = ahci_px_rreg(ahci, i, AHCI_PX_SERR);
298
        if (BITSET(reg, AHCI_PX_SERR_DIAG_X)) {
299
            ahci_px_set(ahci, i, AHCI_PX_SERR, AHCI_PX_SERR_DIAG_X);
300 301
        }

302
        reg = ahci_px_rreg(ahci, i, AHCI_PX_TFD);
303
        if (BITCLR(reg, AHCI_PX_TFD_STS_BSY | AHCI_PX_TFD_STS_DRQ)) {
304
            reg = ahci_px_rreg(ahci, i, AHCI_PX_SSTS);
305 306
            if ((reg & AHCI_PX_SSTS_DET) == SSTS_DET_ESTABLISHED) {
                /* Device Found: set PxCMD.ST := 1 */
307 308 309
                ahci_px_set(ahci, i, AHCI_PX_CMD, AHCI_PX_CMD_ST);
                ASSERT_BIT_SET(ahci_px_rreg(ahci, i, AHCI_PX_CMD),
                               AHCI_PX_CMD_CR);
310 311 312 313 314 315 316 317 318
                g_test_message("Started Device %u", i);
            } else if ((reg & AHCI_PX_SSTS_DET)) {
                /* Device present, but in some unknown state. */
                g_assert_not_reached();
            }
        }
    }

    /* Enable GHC.IE */
319 320
    ahci_set(ahci, AHCI_GHC, AHCI_GHC_IE);
    reg = ahci_rreg(ahci, AHCI_GHC);
321 322 323 324 325 326 327
    ASSERT_BIT_SET(reg, AHCI_GHC_IE);

    /* TODO: The device should now be idling and waiting for commands.
     * In the future, a small test-case to inspect the Register D2H FIS
     * and clear the initial interrupts might be good. */
}

328 329 330 331 332
/*** Specification Adherence Tests ***/

/**
 * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
 */
333
static void ahci_test_pci_spec(AHCIQState *ahci)
334 335 336 337 338 339
{
    uint8_t datab;
    uint16_t data;
    uint32_t datal;

    /* Most of these bits should start cleared until we turn them on. */
340
    data = qpci_config_readw(ahci->dev, PCI_COMMAND);
341 342 343 344 345 346 347 348 349 350 351
    ASSERT_BIT_CLEAR(data, PCI_COMMAND_MEMORY);
    ASSERT_BIT_CLEAR(data, PCI_COMMAND_MASTER);
    ASSERT_BIT_CLEAR(data, PCI_COMMAND_SPECIAL);     /* Reserved */
    ASSERT_BIT_CLEAR(data, PCI_COMMAND_VGA_PALETTE); /* Reserved */
    ASSERT_BIT_CLEAR(data, PCI_COMMAND_PARITY);
    ASSERT_BIT_CLEAR(data, PCI_COMMAND_WAIT);        /* Reserved */
    ASSERT_BIT_CLEAR(data, PCI_COMMAND_SERR);
    ASSERT_BIT_CLEAR(data, PCI_COMMAND_FAST_BACK);
    ASSERT_BIT_CLEAR(data, PCI_COMMAND_INTX_DISABLE);
    ASSERT_BIT_CLEAR(data, 0xF800);                  /* Reserved */

352
    data = qpci_config_readw(ahci->dev, PCI_STATUS);
353 354 355 356 357 358 359 360 361 362 363 364
    ASSERT_BIT_CLEAR(data, 0x01 | 0x02 | 0x04);     /* Reserved */
    ASSERT_BIT_CLEAR(data, PCI_STATUS_INTERRUPT);
    ASSERT_BIT_SET(data, PCI_STATUS_CAP_LIST);      /* must be set */
    ASSERT_BIT_CLEAR(data, PCI_STATUS_UDF);         /* Reserved */
    ASSERT_BIT_CLEAR(data, PCI_STATUS_PARITY);
    ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_TARGET_ABORT);
    ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_TARGET_ABORT);
    ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_MASTER_ABORT);
    ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_SYSTEM_ERROR);
    ASSERT_BIT_CLEAR(data, PCI_STATUS_DETECTED_PARITY);

    /* RID occupies the low byte, CCs occupy the high three. */
365
    datal = qpci_config_readl(ahci->dev, PCI_CLASS_REVISION);
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    if (ahci_pedantic) {
        /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
         * Though in practice this is likely seldom true. */
        ASSERT_BIT_CLEAR(datal, 0xFF);
    }

    /* BCC *must* equal 0x01. */
    g_assert_cmphex(PCI_BCC(datal), ==, 0x01);
    if (PCI_SCC(datal) == 0x01) {
        /* IDE */
        ASSERT_BIT_SET(0x80000000, datal);
        ASSERT_BIT_CLEAR(0x60000000, datal);
    } else if (PCI_SCC(datal) == 0x04) {
        /* RAID */
        g_assert_cmphex(PCI_PI(datal), ==, 0);
    } else if (PCI_SCC(datal) == 0x06) {
        /* AHCI */
        g_assert_cmphex(PCI_PI(datal), ==, 0x01);
    } else {
        g_assert_not_reached();
    }

388
    datab = qpci_config_readb(ahci->dev, PCI_CACHE_LINE_SIZE);
389 390
    g_assert_cmphex(datab, ==, 0);

391
    datab = qpci_config_readb(ahci->dev, PCI_LATENCY_TIMER);
392 393 394
    g_assert_cmphex(datab, ==, 0);

    /* Only the bottom 7 bits must be off. */
395
    datab = qpci_config_readb(ahci->dev, PCI_HEADER_TYPE);
396 397 398
    ASSERT_BIT_CLEAR(datab, 0x7F);

    /* BIST is optional, but the low 7 bits must always start off regardless. */
399
    datab = qpci_config_readb(ahci->dev, PCI_BIST);
400 401 402
    ASSERT_BIT_CLEAR(datab, 0x7F);

    /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
403
    datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
404 405
    g_assert_cmphex(datal, ==, 0);

406 407
    qpci_config_writel(ahci->dev, PCI_BASE_ADDRESS_5, 0xFFFFFFFF);
    datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
408 409 410 411 412
    /* ABAR must be 32-bit, memory mapped, non-prefetchable and
     * must be >= 512 bytes. To that end, bits 0-8 must be off. */
    ASSERT_BIT_CLEAR(datal, 0xFF);

    /* Capability list MUST be present, */
413
    datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST);
414 415 416 417 418
    /* But these bits are reserved. */
    ASSERT_BIT_CLEAR(datal, ~0xFF);
    g_assert_cmphex(datal, !=, 0);

    /* Check specification adherence for capability extenstions. */
419
    data = qpci_config_readw(ahci->dev, datal);
420

421
    switch (ahci->fingerprint) {
422 423 424 425 426 427 428 429 430 431 432 433
    case AHCI_INTEL_ICH9:
        /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
        g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_MSI);
        break;
    default:
        /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
        g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_PM);
    }

    ahci_test_pci_caps(ahci, data, (uint8_t)datal);

    /* Reserved. */
434
    datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST + 4);
435 436 437
    g_assert_cmphex(datal, ==, 0);

    /* IPIN might vary, but ILINE must be off. */
438
    datab = qpci_config_readb(ahci->dev, PCI_INTERRUPT_LINE);
439 440 441 442 443 444
    g_assert_cmphex(datab, ==, 0);
}

/**
 * Test PCI capabilities for AHCI specification adherence.
 */
445
static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
                               uint8_t offset)
{
    uint8_t cid = header & 0xFF;
    uint8_t next = header >> 8;

    g_test_message("CID: %02x; next: %02x", cid, next);

    switch (cid) {
    case PCI_CAP_ID_PM:
        ahci_test_pmcap(ahci, offset);
        break;
    case PCI_CAP_ID_MSI:
        ahci_test_msicap(ahci, offset);
        break;
    case PCI_CAP_ID_SATA:
        ahci_test_satacap(ahci, offset);
        break;

    default:
        g_test_message("Unknown CAP 0x%02x", cid);
    }

    if (next) {
469
        ahci_test_pci_caps(ahci, qpci_config_readw(ahci->dev, next), next);
470 471 472 473 474 475
    }
}

/**
 * Test SATA PCI capabilitity for AHCI specification adherence.
 */
476
static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset)
477 478 479 480 481 482 483
{
    uint16_t dataw;
    uint32_t datal;

    g_test_message("Verifying SATACAP");

    /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
484
    dataw = qpci_config_readw(ahci->dev, offset + 2);
485 486 487
    g_assert_cmphex(dataw, ==, 0x10);

    /* Grab the SATACR1 register. */
488
    datal = qpci_config_readw(ahci->dev, offset + 4);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510

    switch (datal & 0x0F) {
    case 0x04: /* BAR0 */
    case 0x05: /* BAR1 */
    case 0x06:
    case 0x07:
    case 0x08:
    case 0x09: /* BAR5 */
    case 0x0F: /* Immediately following SATACR1 in PCI config space. */
        break;
    default:
        /* Invalid BARLOC for the Index Data Pair. */
        g_assert_not_reached();
    }

    /* Reserved. */
    g_assert_cmphex((datal >> 24), ==, 0x00);
}

/**
 * Test MSI PCI capability for AHCI specification adherence.
 */
511
static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset)
512 513 514 515 516 517
{
    uint16_t dataw;
    uint32_t datal;

    g_test_message("Verifying MSICAP");

518
    dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_FLAGS);
519 520 521 522
    ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_ENABLE);
    ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_QSIZE);
    ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_RESERVED);

523
    datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_LO);
524 525 526 527
    g_assert_cmphex(datal, ==, 0);

    if (dataw & PCI_MSI_FLAGS_64BIT) {
        g_test_message("MSICAP is 64bit");
528
        datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_HI);
529
        g_assert_cmphex(datal, ==, 0);
530
        dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_64);
531 532 533
        g_assert_cmphex(dataw, ==, 0);
    } else {
        g_test_message("MSICAP is 32bit");
534
        dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_32);
535 536 537 538 539 540 541
        g_assert_cmphex(dataw, ==, 0);
    }
}

/**
 * Test Power Management PCI capability for AHCI specification adherence.
 */
542
static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset)
543 544 545 546 547
{
    uint16_t dataw;

    g_test_message("Verifying PMCAP");

548
    dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_PMC);
549 550 551 552 553
    ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_PME_CLOCK);
    ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_RESERVED);
    ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D1);
    ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D2);

554
    dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_CTRL);
555 556 557 558 559 560
    ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_STATE_MASK);
    ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_RESERVED);
    ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SEL_MASK);
    ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SCALE_MASK);
}

561
static void ahci_test_hba_spec(AHCIQState *ahci)
562 563
{
    unsigned i;
564
    uint32_t reg;
565 566 567 568
    uint32_t ports;
    uint8_t nports_impl;
    uint8_t maxports;

569
    g_assert(ahci != NULL);
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

    /*
     * Note that the AHCI spec does expect the BIOS to set up a few things:
     * CAP.SSS    - Support for staggered spin-up            (t/f)
     * CAP.SMPS   - Support for mechanical presence switches (t/f)
     * PI         - Ports Implemented                        (1-32)
     * PxCMD.HPCP - Hot Plug Capable Port
     * PxCMD.MPSP - Mechanical Presence Switch Present
     * PxCMD.CPD  - Cold Presence Detection support
     *
     * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
     * Foreach Port Implemented:
     * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
     * -PxCLB/U and PxFB/U are set to valid regions in memory
     * -PxSUD is set to 1.
     * -PxSSTS.DET is polled for presence; if detected, we continue:
     * -PxSERR is cleared with 1's.
     * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
     *  the device is ready.
     */

    /* 1 CAP - Capabilities Register */
592
    ahci->cap = ahci_rreg(ahci, AHCI_CAP);
593
    ASSERT_BIT_CLEAR(ahci->cap, AHCI_CAP_RESERVED);
594 595

    /* 2 GHC - Global Host Control */
596
    reg = ahci_rreg(ahci, AHCI_GHC);
597 598 599
    ASSERT_BIT_CLEAR(reg, AHCI_GHC_HR);
    ASSERT_BIT_CLEAR(reg, AHCI_GHC_IE);
    ASSERT_BIT_CLEAR(reg, AHCI_GHC_MRSM);
600
    if (BITSET(ahci->cap, AHCI_CAP_SAM)) {
601 602 603 604 605 606 607 608
        g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
        ASSERT_BIT_SET(reg, AHCI_GHC_AE);
    } else {
        g_test_message("Supports AHCI/Legacy mix.");
        ASSERT_BIT_CLEAR(reg, AHCI_GHC_AE);
    }

    /* 3 IS - Interrupt Status */
609
    reg = ahci_rreg(ahci, AHCI_IS);
610 611 612
    g_assert_cmphex(reg, ==, 0);

    /* 4 PI - Ports Implemented */
613
    ports = ahci_rreg(ahci, AHCI_PI);
614 615 616 617
    /* Ports Implemented must be non-zero. */
    g_assert_cmphex(ports, !=, 0);
    /* Ports Implemented must be <= Number of Ports. */
    nports_impl = ctpopl(ports);
618
    g_assert_cmpuint(((AHCI_CAP_NP & ahci->cap) + 1), >=, nports_impl);
619 620 621 622

    /* Ports must be within the proper range. Given a mapping of SIZE,
     * 256 bytes are used for global HBA control, and the rest is used
     * for ports data, at 0x80 bytes each. */
623 624
    g_assert_cmphex(ahci->barsize, >, 0);
    maxports = (ahci->barsize - HBA_DATA_REGION_SIZE) / HBA_PORT_DATA_SIZE;
625 626 627 628
    /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
    g_assert_cmphex((reg >> maxports), ==, 0);

    /* 5 AHCI Version */
629
    reg = ahci_rreg(ahci, AHCI_VS);
630 631 632 633 634 635 636 637 638 639 640 641
    switch (reg) {
    case AHCI_VERSION_0_95:
    case AHCI_VERSION_1_0:
    case AHCI_VERSION_1_1:
    case AHCI_VERSION_1_2:
    case AHCI_VERSION_1_3:
        break;
    default:
        g_assert_not_reached();
    }

    /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
642
    reg = ahci_rreg(ahci, AHCI_CCCCTL);
643
    if (BITSET(ahci->cap, AHCI_CAP_CCCS)) {
644 645 646 647 648 649 650 651 652
        ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_EN);
        ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_RESERVED);
        ASSERT_BIT_SET(reg, AHCI_CCCCTL_CC);
        ASSERT_BIT_SET(reg, AHCI_CCCCTL_TV);
    } else {
        g_assert_cmphex(reg, ==, 0);
    }

    /* 7 CCC_PORTS */
653
    reg = ahci_rreg(ahci, AHCI_CCCPORTS);
654 655 656 657
    /* Must be zeroes initially regardless of CAP.CCCS */
    g_assert_cmphex(reg, ==, 0);

    /* 8 EM_LOC */
658
    reg = ahci_rreg(ahci, AHCI_EMLOC);
659
    if (BITCLR(ahci->cap, AHCI_CAP_EMS)) {
660 661 662 663
        g_assert_cmphex(reg, ==, 0);
    }

    /* 9 EM_CTL */
664
    reg = ahci_rreg(ahci, AHCI_EMCTL);
665
    if (BITSET(ahci->cap, AHCI_CAP_EMS)) {
666 667 668 669 670 671 672 673 674
        ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_STSMR);
        ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLTM);
        ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLRST);
        ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_RESERVED);
    } else {
        g_assert_cmphex(reg, ==, 0);
    }

    /* 10 CAP2 -- Capabilities Extended */
675
    ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
676
    ASSERT_BIT_CLEAR(ahci->cap2, AHCI_CAP2_RESERVED);
677 678

    /* 11 BOHC -- Bios/OS Handoff Control */
679
    reg = ahci_rreg(ahci, AHCI_BOHC);
680 681 682 683 684
    g_assert_cmphex(reg, ==, 0);

    /* 12 -- 23: Reserved */
    g_test_message("Verifying HBA reserved area is empty.");
    for (i = AHCI_RESERVED; i < AHCI_NVMHCI; ++i) {
685
        reg = ahci_rreg(ahci, i);
686 687 688 689
        g_assert_cmphex(reg, ==, 0);
    }

    /* 24 -- 39: NVMHCI */
690
    if (BITCLR(ahci->cap2, AHCI_CAP2_NVMP)) {
691 692
        g_test_message("Verifying HBA/NVMHCI area is empty.");
        for (i = AHCI_NVMHCI; i < AHCI_VENDOR; ++i) {
693
            reg = ahci_rreg(ahci, i);
694 695 696 697 698 699 700
            g_assert_cmphex(reg, ==, 0);
        }
    }

    /* 40 -- 63: Vendor */
    g_test_message("Verifying HBA/Vendor area is empty.");
    for (i = AHCI_VENDOR; i < AHCI_PORTS; ++i) {
701
        reg = ahci_rreg(ahci, i);
702 703 704 705 706 707 708
        g_assert_cmphex(reg, ==, 0);
    }

    /* 64 -- XX: Port Space */
    for (i = 0; ports || (i < maxports); ports >>= 1, ++i) {
        if (BITSET(ports, 0x1)) {
            g_test_message("Testing port %u for spec", i);
709
            ahci_test_port_spec(ahci, i);
710 711 712 713 714 715 716 717
        } else {
            uint16_t j;
            uint16_t low = AHCI_PORTS + (32 * i);
            uint16_t high = AHCI_PORTS + (32 * (i + 1));
            g_test_message("Asserting unimplemented port %u "
                           "(reg [%u-%u]) is empty.",
                           i, low, high - 1);
            for (j = low; j < high; ++j) {
718
                reg = ahci_rreg(ahci, j);
719 720 721 722 723 724 725 726 727
                g_assert_cmphex(reg, ==, 0);
            }
        }
    }
}

/**
 * Test the memory space for one port for specification adherence.
 */
728
static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
729 730 731 732 733
{
    uint32_t reg;
    unsigned i;

    /* (0) CLB */
734
    reg = ahci_px_rreg(ahci, port, AHCI_PX_CLB);
735 736 737
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CLB_RESERVED);

    /* (1) CLBU */
738
    if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
739
        reg = ahci_px_rreg(ahci, port, AHCI_PX_CLBU);
740 741 742 743
        g_assert_cmphex(reg, ==, 0);
    }

    /* (2) FB */
744
    reg = ahci_px_rreg(ahci, port, AHCI_PX_FB);
745 746 747
    ASSERT_BIT_CLEAR(reg, AHCI_PX_FB_RESERVED);

    /* (3) FBU */
748
    if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
749
        reg = ahci_px_rreg(ahci, port, AHCI_PX_FBU);
750 751 752 753
        g_assert_cmphex(reg, ==, 0);
    }

    /* (4) IS */
754
    reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
755 756 757
    g_assert_cmphex(reg, ==, 0);

    /* (5) IE */
758
    reg = ahci_px_rreg(ahci, port, AHCI_PX_IE);
759 760 761
    g_assert_cmphex(reg, ==, 0);

    /* (6) CMD */
762
    reg = ahci_px_rreg(ahci, port, AHCI_PX_CMD);
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FRE);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_RESERVED);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CCS);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_PMA); /* And RW only if CAP.SPM */
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_APSTE); /* RW only if CAP2.APST */
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ATAPI);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_DLAE);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ALPE);  /* RW only if CAP.SALP */
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ASP);   /* RW only if CAP.SALP */
    ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ICC);
    /* If CPDetect support does not exist, CPState must be off. */
    if (BITCLR(reg, AHCI_PX_CMD_CPD)) {
        ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CPS);
    }
    /* If MPSPresence is not set, MPSState must be off. */
    if (BITCLR(reg, AHCI_PX_CMD_MPSP)) {
        ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
    }
    /* If we do not support MPS, MPSS and MPSP must be off. */
784
    if (BITCLR(ahci->cap, AHCI_CAP_SMPS)) {
785 786 787 788 789 790 791 792 793 794
        ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
        ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSP);
    }
    /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
    if (BITANY(reg, AHCI_PX_CMD_CPD || AHCI_PX_CMD_MPSP)) {
        ASSERT_BIT_SET(reg, AHCI_PX_CMD_HPCP);
    }
    /* HPCP and ESP cannot both be active. */
    g_assert(!BITSET(reg, AHCI_PX_CMD_HPCP | AHCI_PX_CMD_ESP));
    /* If CAP.FBSS is not set, FBSCP must not be set. */
795
    if (BITCLR(ahci->cap, AHCI_CAP_FBSS)) {
796 797 798 799
        ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FBSCP);
    }

    /* (7) RESERVED */
800
    reg = ahci_px_rreg(ahci, port, AHCI_PX_RES1);
801 802 803
    g_assert_cmphex(reg, ==, 0);

    /* (8) TFD */
804
    reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
    /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
     * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
    ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_ERR);
    ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS1);
    ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_DRQ);
    ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS2);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_RESERVED);

    /* (9) SIG */
    /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
     * Even when GHC.ST is zero, the AHCI HBA may receive the initial
     * D2H register FIS and update the signature asynchronously,
     * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */

    /* (10) SSTS / SCR0: SStatus */
822
    reg = ahci_px_rreg(ahci, port, AHCI_PX_SSTS);
823 824 825 826 827
    ASSERT_BIT_CLEAR(reg, AHCI_PX_SSTS_RESERVED);
    /* Even though the register should be 0 at boot, it is asynchronous and
     * prone to change, so we cannot test any well known value. */

    /* (11) SCTL / SCR2: SControl */
828
    reg = ahci_px_rreg(ahci, port, AHCI_PX_SCTL);
829 830 831
    g_assert_cmphex(reg, ==, 0);

    /* (12) SERR / SCR1: SError */
832
    reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
833 834 835
    g_assert_cmphex(reg, ==, 0);

    /* (13) SACT / SCR3: SActive */
836
    reg = ahci_px_rreg(ahci, port, AHCI_PX_SACT);
837 838 839
    g_assert_cmphex(reg, ==, 0);

    /* (14) CI */
840
    reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
841 842 843
    g_assert_cmphex(reg, ==, 0);

    /* (15) SNTF */
844
    reg = ahci_px_rreg(ahci, port, AHCI_PX_SNTF);
845 846 847
    g_assert_cmphex(reg, ==, 0);

    /* (16) FBS */
848
    reg = ahci_px_rreg(ahci, port, AHCI_PX_FBS);
849 850 851 852 853 854
    ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_EN);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEC);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_SDE);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEV);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DWE);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_RESERVED);
855
    if (BITSET(ahci->cap, AHCI_CAP_FBSS)) {
856 857 858 859 860 861
        /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
        g_assert((reg & AHCI_PX_FBS_ADO) >> ctzl(AHCI_PX_FBS_ADO) >= 2);
    }

    /* [17 -- 27] RESERVED */
    for (i = AHCI_PX_RES2; i < AHCI_PX_VS; ++i) {
862
        reg = ahci_px_rreg(ahci, port, i);
863 864 865 866 867
        g_assert_cmphex(reg, ==, 0);
    }

    /* [28 -- 31] Vendor-Specific */
    for (i = AHCI_PX_VS; i < 32; ++i) {
868
        reg = ahci_px_rreg(ahci, port, i);
869 870 871 872 873 874
        if (reg) {
            g_test_message("INFO: Vendor register %u non-empty", i);
        }
    }
}

875 876 877 878
/**
 * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
 * device we see, then read and check the response.
 */
879
static void ahci_test_identify(AHCIQState *ahci)
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
{
    RegD2HFIS *d2h = g_malloc0(0x20);
    RegD2HFIS *pio = g_malloc0(0x20);
    RegH2DFIS fis;
    AHCICommand cmd;
    PRD prd;
    uint32_t ports, reg, clb, table, fb, data_ptr;
    uint16_t buff[256];
    unsigned i;
    int rc;

    g_assert(ahci != NULL);

    /* We need to:
     * (1) Create a Command Table Buffer and update the Command List Slot #0
     *     to point to this buffer.
     * (2) Construct an FIS host-to-device command structure, and write it to
     *     the top of the command table buffer.
     * (3) Create a data buffer for the IDENTIFY response to be sent to
     * (4) Create a Physical Region Descriptor that points to the data buffer,
     *     and write it to the bottom (offset 0x80) of the command table.
     * (5) Now, PxCLB points to the command list, command 0 points to
     *     our table, and our table contains an FIS instruction and a
     *     PRD that points to our rx buffer.
     * (6) We inform the HBA via PxCI that there is a command ready in slot #0.
     */

    /* Pick the first implemented and running port */
908
    ports = ahci_rreg(ahci, AHCI_PI);
909 910 911 912 913 914 915 916 917
    for (i = 0; i < 32; ports >>= 1, ++i) {
        if (ports == 0) {
            i = 32;
        }

        if (!(ports & 0x01)) {
            continue;
        }

918
        reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
919 920 921 922 923 924 925 926
        if (BITSET(reg, AHCI_PX_CMD_ST)) {
            break;
        }
    }
    g_assert_cmphex(i, <, 32);
    g_test_message("Selected port %u for test", i);

    /* Clear out this port's interrupts (ignore the init register d2h fis) */
927 928 929
    reg = ahci_px_rreg(ahci, i, AHCI_PX_IS);
    ahci_px_wreg(ahci, i, AHCI_PX_IS, reg);
    g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
930

S
Stefan Weil 已提交
931
    /* Wipe the FIS-Receive Buffer */
932
    fb = ahci_px_rreg(ahci, i, AHCI_PX_FB);
933 934 935 936 937
    g_assert_cmphex(fb, !=, 0);
    qmemset(fb, 0x00, 0x100);

    /* Create a Command Table buffer. 0x80 is the smallest with a PRDTL of 0. */
    /* We need at least one PRD, so round up to the nearest 0x80 multiple.    */
938
    table = ahci_alloc(ahci, CMD_TBL_SIZ(1));
939 940 941 942
    g_assert(table);
    ASSERT_BIT_CLEAR(table, 0x7F);

    /* Create a data buffer ... where we will dump the IDENTIFY data to. */
943
    data_ptr = ahci_alloc(ahci, 512);
944 945 946
    g_assert(data_ptr);

    /* Grab the Command List Buffer pointer */
947
    clb = ahci_px_rreg(ahci, i, AHCI_PX_CLB);
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
    g_assert(clb);

    /* Copy the existing Command #0 structure from the CLB into local memory,
     * and build a new command #0. */
    memread(clb, &cmd, sizeof(cmd));
    cmd.b1 = 5;    /* reg_h2d_fis is 5 double-words long */
    cmd.b2 = 0x04; /* clear PxTFD.STS.BSY when done */
    cmd.prdtl = cpu_to_le16(1); /* One PRD table entry. */
    cmd.prdbc = 0;
    cmd.ctba = cpu_to_le32(table);
    cmd.ctbau = 0;

    /* Construct our PRD, noting that DBC is 0-indexed. */
    prd.dba = cpu_to_le32(data_ptr);
    prd.dbau = 0;
    prd.res = 0;
    /* 511+1 bytes, request DPS interrupt */
    prd.dbc = cpu_to_le32(511 | 0x80000000);

    /* Construct our Command FIS, Based on http://wiki.osdev.org/AHCI */
    memset(&fis, 0x00, sizeof(fis));
    fis.fis_type = 0x27; /* Register Host-to-Device FIS */
    fis.command = 0xEC;  /* IDENTIFY */
    fis.device = 0;
    fis.flags = 0x80;    /* Indicate this is a command FIS */

    /* We've committed nothing yet, no interrupts should be posted yet. */
975
    g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
976 977 978 979 980 981 982 983 984 985 986

    /* Commit the Command FIS to the Command Table */
    memwrite(table, &fis, sizeof(fis));

    /* Commit the PRD entry to the Command Table */
    memwrite(table + 0x80, &prd, sizeof(prd));

    /* Commit Command #0, pointing to the Table, to the Command List Buffer. */
    memwrite(clb, &cmd, sizeof(cmd));

    /* Everything is in place, but we haven't given the go-ahead yet. */
987
    g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
988 989

    /* Issue Command #0 via PxCI */
990 991
    ahci_px_wreg(ahci, i, AHCI_PX_CI, (1 << 0));
    while (BITSET(ahci_px_rreg(ahci, i, AHCI_PX_TFD), AHCI_PX_TFD_STS_BSY)) {
992 993 994 995
        usleep(50);
    }

    /* Check for expected interrupts */
996
    reg = ahci_px_rreg(ahci, i, AHCI_PX_IS);
997 998 999 1000 1001 1002
    ASSERT_BIT_SET(reg, AHCI_PX_IS_DHRS);
    ASSERT_BIT_SET(reg, AHCI_PX_IS_PSS);
    /* BUG: we expect AHCI_PX_IS_DPS to be set. */
    ASSERT_BIT_CLEAR(reg, AHCI_PX_IS_DPS);

    /* Clear expected interrupts and assert all interrupts now cleared. */
1003 1004 1005
    ahci_px_wreg(ahci, i, AHCI_PX_IS,
                 AHCI_PX_IS_DHRS | AHCI_PX_IS_PSS | AHCI_PX_IS_DPS);
    g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
1006 1007

    /* Check for errors. */
1008
    reg = ahci_px_rreg(ahci, i, AHCI_PX_SERR);
1009
    g_assert_cmphex(reg, ==, 0);
1010
    reg = ahci_px_rreg(ahci, i, AHCI_PX_TFD);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_ERR);
    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);

    /* Investigate CMD #0, assert that we read 512 bytes */
    memread(clb, &cmd, sizeof(cmd));
    g_assert_cmphex(512, ==, le32_to_cpu(cmd.prdbc));

    /* Investigate FIS responses */
    memread(fb + 0x20, pio, 0x20);
    memread(fb + 0x40, d2h, 0x20);
    g_assert_cmphex(pio->fis_type, ==, 0x5f);
    g_assert_cmphex(d2h->fis_type, ==, 0x34);
    g_assert_cmphex(pio->flags, ==, d2h->flags);
    g_assert_cmphex(pio->status, ==, d2h->status);
    g_assert_cmphex(pio->error, ==, d2h->error);

1027
    reg = ahci_px_rreg(ahci, i, AHCI_PX_TFD);
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    g_assert_cmphex((reg & AHCI_PX_TFD_ERR), ==, pio->error);
    g_assert_cmphex((reg & AHCI_PX_TFD_STS), ==, pio->status);
    /* The PIO Setup FIS contains a "bytes read" field, which is a
     * 16-bit value. The Physical Region Descriptor Byte Count is
     * 32-bit, but for small transfers using one PRD, it should match. */
    g_assert_cmphex(le16_to_cpu(pio->res4), ==, le32_to_cpu(cmd.prdbc));

    /* Last, but not least: Investigate the IDENTIFY response data. */
    memread(data_ptr, &buff, 512);

    /* Check serial number/version in the buffer */
    /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
     * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
     * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
     * as a consequence, only needs to unchunk the data on LE machines. */
    string_bswap16(&buff[10], 20);
    rc = memcmp(&buff[10], "testdisk            ", 20);
    g_assert_cmphex(rc, ==, 0);

    string_bswap16(&buff[23], 8);
    rc = memcmp(&buff[23], "version ", 8);
    g_assert_cmphex(rc, ==, 0);

    g_free(d2h);
    g_free(pio);
}

1055 1056 1057 1058 1059 1060 1061 1062 1063
/******************************************************************************/
/* Test Interfaces                                                            */
/******************************************************************************/

/**
 * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
 */
static void test_sanity(void)
{
J
John Snow 已提交
1064
    AHCIQState *ahci;
1065 1066 1067 1068
    ahci = ahci_boot();
    ahci_shutdown(ahci);
}

1069 1070 1071 1072 1073 1074
/**
 * Ensure that the PCI configuration space for the AHCI device is in-line with
 * the AHCI 1.3 specification for initial values.
 */
static void test_pci_spec(void)
{
J
John Snow 已提交
1075
    AHCIQState *ahci;
1076
    ahci = ahci_boot();
1077
    ahci_test_pci_spec(ahci);
1078 1079 1080
    ahci_shutdown(ahci);
}

1081 1082 1083 1084 1085 1086
/**
 * Engage the PCI AHCI device and sanity check the response.
 * Perform additional PCI config space bringup for the HBA.
 */
static void test_pci_enable(void)
{
J
John Snow 已提交
1087
    AHCIQState *ahci;
1088

1089
    ahci = ahci_boot();
1090
    ahci_pci_enable(ahci);
1091 1092 1093
    ahci_shutdown(ahci);
}

1094 1095 1096 1097 1098 1099
/**
 * Investigate the memory mapped regions of the HBA,
 * and test them for AHCI specification adherence.
 */
static void test_hba_spec(void)
{
J
John Snow 已提交
1100
    AHCIQState *ahci;
1101 1102

    ahci = ahci_boot();
1103 1104
    ahci_pci_enable(ahci);
    ahci_test_hba_spec(ahci);
1105 1106 1107
    ahci_shutdown(ahci);
}

1108 1109 1110 1111 1112 1113
/**
 * Engage the HBA functionality of the AHCI PCI device,
 * and bring it into a functional idle state.
 */
static void test_hba_enable(void)
{
J
John Snow 已提交
1114
    AHCIQState *ahci;
1115 1116

    ahci = ahci_boot();
1117 1118
    ahci_pci_enable(ahci);
    ahci_hba_enable(ahci);
1119 1120 1121
    ahci_shutdown(ahci);
}

1122 1123 1124 1125 1126 1127
/**
 * Bring up the device and issue an IDENTIFY command.
 * Inspect the state of the HBA device and the data returned.
 */
static void test_identify(void)
{
J
John Snow 已提交
1128
    AHCIQState *ahci;
1129 1130

    ahci = ahci_boot();
1131 1132 1133
    ahci_pci_enable(ahci);
    ahci_hba_enable(ahci);
    ahci_test_identify(ahci);
1134 1135 1136
    ahci_shutdown(ahci);
}

1137 1138 1139 1140 1141 1142 1143
/******************************************************************************/

int main(int argc, char **argv)
{
    const char *arch;
    int fd;
    int ret;
1144 1145 1146 1147 1148 1149
    int c;

    static struct option long_options[] = {
        {"pedantic", no_argument, 0, 'p' },
        {0, 0, 0, 0},
    };
1150 1151 1152 1153

    /* Should be first to utilize g_test functionality, So we can see errors. */
    g_test_init(&argc, &argv, NULL);

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    while (1) {
        c = getopt_long(argc, argv, "", long_options, NULL);
        if (c == -1) {
            break;
        }
        switch (c) {
        case -1:
            break;
        case 'p':
            ahci_pedantic = 1;
            break;
        default:
            fprintf(stderr, "Unrecognized ahci_test option.\n");
            g_assert_not_reached();
        }
    }

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    /* Check architecture */
    arch = qtest_get_arch();
    if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
        g_test_message("Skipping test for non-x86");
        return 0;
    }

    /* Create a temporary raw image */
    fd = mkstemp(tmp_path);
    g_assert(fd >= 0);
    ret = ftruncate(fd, TEST_IMAGE_SIZE);
    g_assert(ret == 0);
    close(fd);

    /* Run the tests */
    qtest_add_func("/ahci/sanity",     test_sanity);
1187
    qtest_add_func("/ahci/pci_spec",   test_pci_spec);
1188
    qtest_add_func("/ahci/pci_enable", test_pci_enable);
1189
    qtest_add_func("/ahci/hba_spec",   test_hba_spec);
1190
    qtest_add_func("/ahci/hba_enable", test_hba_enable);
1191
    qtest_add_func("/ahci/identify",   test_identify);
1192 1193 1194 1195 1196 1197 1198 1199

    ret = g_test_run();

    /* Cleanup */
    unlink(tmp_path);

    return ret;
}