xref: /openbmc/qemu/tests/qtest/ahci-test.c (revision cc610857)
11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth  * AHCI test cases
31e8a1faeSThomas Huth  *
41e8a1faeSThomas Huth  * Copyright (c) 2014 John Snow <jsnow@redhat.com>
51e8a1faeSThomas Huth  *
61e8a1faeSThomas Huth  * Permission is hereby granted, free of charge, to any person obtaining a copy
71e8a1faeSThomas Huth  * of this software and associated documentation files (the "Software"), to deal
81e8a1faeSThomas Huth  * in the Software without restriction, including without limitation the rights
91e8a1faeSThomas Huth  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
101e8a1faeSThomas Huth  * copies of the Software, and to permit persons to whom the Software is
111e8a1faeSThomas Huth  * furnished to do so, subject to the following conditions:
121e8a1faeSThomas Huth  *
131e8a1faeSThomas Huth  * The above copyright notice and this permission notice shall be included in
141e8a1faeSThomas Huth  * all copies or substantial portions of the Software.
151e8a1faeSThomas Huth  *
161e8a1faeSThomas Huth  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171e8a1faeSThomas Huth  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181e8a1faeSThomas Huth  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
191e8a1faeSThomas Huth  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201e8a1faeSThomas Huth  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211e8a1faeSThomas Huth  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
221e8a1faeSThomas Huth  * THE SOFTWARE.
231e8a1faeSThomas Huth  */
241e8a1faeSThomas Huth 
251e8a1faeSThomas Huth #include "qemu/osdep.h"
261e8a1faeSThomas Huth #include <getopt.h>
271e8a1faeSThomas Huth 
28907b5105SMarc-André Lureau #include "libqtest.h"
291e8a1faeSThomas Huth #include "libqos/libqos-pc.h"
301e8a1faeSThomas Huth #include "libqos/ahci.h"
311e8a1faeSThomas Huth #include "libqos/pci-pc.h"
321e8a1faeSThomas Huth 
331e8a1faeSThomas Huth #include "qapi/qmp/qdict.h"
341e8a1faeSThomas Huth #include "qemu/host-utils.h"
351e8a1faeSThomas Huth 
361e8a1faeSThomas Huth #include "hw/pci/pci_ids.h"
371e8a1faeSThomas Huth #include "hw/pci/pci_regs.h"
381e8a1faeSThomas Huth 
391e8a1faeSThomas Huth /* Test images sizes in MB */
401e8a1faeSThomas Huth #define TEST_IMAGE_SIZE_MB_LARGE (200 * 1024)
411e8a1faeSThomas Huth #define TEST_IMAGE_SIZE_MB_SMALL 64
421e8a1faeSThomas Huth 
431e8a1faeSThomas Huth /*** Globals ***/
44d9eefd35SBin Meng static char *tmp_path;
45d9eefd35SBin Meng static char *debug_path;
46d9eefd35SBin Meng static char *mig_socket;
471e8a1faeSThomas Huth static bool ahci_pedantic;
481e8a1faeSThomas Huth static const char *imgfmt;
491e8a1faeSThomas Huth static unsigned test_image_size_mb;
501e8a1faeSThomas Huth 
511e8a1faeSThomas Huth /*** Function Declarations ***/
521e8a1faeSThomas Huth static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port);
531e8a1faeSThomas Huth static void ahci_test_pci_spec(AHCIQState *ahci);
541e8a1faeSThomas Huth static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
551e8a1faeSThomas Huth                                uint8_t offset);
561e8a1faeSThomas Huth static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset);
571e8a1faeSThomas Huth static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset);
581e8a1faeSThomas Huth static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset);
591e8a1faeSThomas Huth 
601e8a1faeSThomas Huth /*** Utilities ***/
611e8a1faeSThomas Huth 
mb_to_sectors(uint64_t image_size_mb)621e8a1faeSThomas Huth static uint64_t mb_to_sectors(uint64_t image_size_mb)
631e8a1faeSThomas Huth {
641e8a1faeSThomas Huth     return (image_size_mb * 1024 * 1024) / AHCI_SECTOR_SIZE;
651e8a1faeSThomas Huth }
661e8a1faeSThomas Huth 
string_bswap16(uint16_t * s,size_t bytes)671e8a1faeSThomas Huth static void string_bswap16(uint16_t *s, size_t bytes)
681e8a1faeSThomas Huth {
691e8a1faeSThomas Huth     g_assert_cmphex((bytes & 1), ==, 0);
701e8a1faeSThomas Huth     bytes /= 2;
711e8a1faeSThomas Huth 
721e8a1faeSThomas Huth     while (bytes--) {
731e8a1faeSThomas Huth         *s = bswap16(*s);
741e8a1faeSThomas Huth         s++;
751e8a1faeSThomas Huth     }
761e8a1faeSThomas Huth }
771e8a1faeSThomas Huth 
781e8a1faeSThomas Huth /**
791e8a1faeSThomas Huth  * Verify that the transfer did not corrupt our state at all.
801e8a1faeSThomas Huth  */
verify_state(AHCIQState * ahci,uint64_t hba_old)811e8a1faeSThomas Huth static void verify_state(AHCIQState *ahci, uint64_t hba_old)
821e8a1faeSThomas Huth {
831e8a1faeSThomas Huth     int i, j;
841e8a1faeSThomas Huth     uint32_t ahci_fingerprint;
851e8a1faeSThomas Huth     uint64_t hba_base;
861e8a1faeSThomas Huth     AHCICommandHeader cmd;
871e8a1faeSThomas Huth 
881e8a1faeSThomas Huth     ahci_fingerprint = qpci_config_readl(ahci->dev, PCI_VENDOR_ID);
891e8a1faeSThomas Huth     g_assert_cmphex(ahci_fingerprint, ==, ahci->fingerprint);
901e8a1faeSThomas Huth 
911e8a1faeSThomas Huth     /* If we haven't initialized, this is as much as can be validated. */
921e8a1faeSThomas Huth     if (!ahci->enabled) {
931e8a1faeSThomas Huth         return;
941e8a1faeSThomas Huth     }
951e8a1faeSThomas Huth 
961e8a1faeSThomas Huth     hba_base = (uint64_t)qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
971e8a1faeSThomas Huth     g_assert_cmphex(hba_base, ==, hba_old);
981e8a1faeSThomas Huth 
991e8a1faeSThomas Huth     g_assert_cmphex(ahci_rreg(ahci, AHCI_CAP), ==, ahci->cap);
1001e8a1faeSThomas Huth     g_assert_cmphex(ahci_rreg(ahci, AHCI_CAP2), ==, ahci->cap2);
1011e8a1faeSThomas Huth 
1021e8a1faeSThomas Huth     for (i = 0; i < 32; i++) {
1031e8a1faeSThomas Huth         g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_FB), ==,
1041e8a1faeSThomas Huth                         ahci->port[i].fb);
1051e8a1faeSThomas Huth         g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_CLB), ==,
1061e8a1faeSThomas Huth                         ahci->port[i].clb);
1071e8a1faeSThomas Huth         for (j = 0; j < 32; j++) {
1081e8a1faeSThomas Huth             ahci_get_command_header(ahci, i, j, &cmd);
1091e8a1faeSThomas Huth             g_assert_cmphex(cmd.prdtl, ==, ahci->port[i].prdtl[j]);
1101e8a1faeSThomas Huth             g_assert_cmphex(cmd.ctba, ==, ahci->port[i].ctba[j]);
1111e8a1faeSThomas Huth         }
1121e8a1faeSThomas Huth     }
1131e8a1faeSThomas Huth }
1141e8a1faeSThomas Huth 
ahci_migrate(AHCIQState * from,AHCIQState * to,const char * uri)1151e8a1faeSThomas Huth static void ahci_migrate(AHCIQState *from, AHCIQState *to, const char *uri)
1161e8a1faeSThomas Huth {
1171e8a1faeSThomas Huth     QOSState *tmp = to->parent;
1181e8a1faeSThomas Huth     QPCIDevice *dev = to->dev;
1191e8a1faeSThomas Huth     char *uri_local = NULL;
1201e8a1faeSThomas Huth     uint64_t hba_old;
1211e8a1faeSThomas Huth 
1221e8a1faeSThomas Huth     if (uri == NULL) {
1231e8a1faeSThomas Huth         uri_local = g_strdup_printf("%s%s", "unix:", mig_socket);
1241e8a1faeSThomas Huth         uri = uri_local;
1251e8a1faeSThomas Huth     }
1261e8a1faeSThomas Huth 
1271e8a1faeSThomas Huth     hba_old = (uint64_t)qpci_config_readl(from->dev, PCI_BASE_ADDRESS_5);
1281e8a1faeSThomas Huth 
1291e8a1faeSThomas Huth     /* context will be 'to' after completion. */
1301e8a1faeSThomas Huth     migrate(from->parent, to->parent, uri);
1311e8a1faeSThomas Huth 
1321e8a1faeSThomas Huth     /* We'd like for the AHCIState objects to still point
1331e8a1faeSThomas Huth      * to information specific to its specific parent
1341e8a1faeSThomas Huth      * instance, but otherwise just inherit the new data. */
1351e8a1faeSThomas Huth     memcpy(to, from, sizeof(AHCIQState));
1361e8a1faeSThomas Huth     to->parent = tmp;
1371e8a1faeSThomas Huth     to->dev = dev;
1381e8a1faeSThomas Huth 
1391e8a1faeSThomas Huth     tmp = from->parent;
1401e8a1faeSThomas Huth     dev = from->dev;
1411e8a1faeSThomas Huth     memset(from, 0x00, sizeof(AHCIQState));
1421e8a1faeSThomas Huth     from->parent = tmp;
1431e8a1faeSThomas Huth     from->dev = dev;
1441e8a1faeSThomas Huth 
1451e8a1faeSThomas Huth     verify_state(to, hba_old);
1461e8a1faeSThomas Huth     g_free(uri_local);
1471e8a1faeSThomas Huth }
1481e8a1faeSThomas Huth 
1491e8a1faeSThomas Huth /*** Test Setup & Teardown ***/
1501e8a1faeSThomas Huth 
1511e8a1faeSThomas Huth /**
1521e8a1faeSThomas Huth  * Start a Q35 machine and bookmark a handle to the AHCI device.
1531e8a1faeSThomas Huth  */
1540472b2e5SDaniel P. Berrangé G_GNUC_PRINTF(1, 0)
ahci_vboot(const char * cli,va_list ap)1551e8a1faeSThomas Huth static AHCIQState *ahci_vboot(const char *cli, va_list ap)
1561e8a1faeSThomas Huth {
1571e8a1faeSThomas Huth     AHCIQState *s;
1581e8a1faeSThomas Huth 
1591e8a1faeSThomas Huth     s = g_new0(AHCIQState, 1);
1601e8a1faeSThomas Huth     s->parent = qtest_pc_vboot(cli, ap);
1611e8a1faeSThomas Huth     alloc_set_flags(&s->parent->alloc, ALLOC_LEAK_ASSERT);
1621e8a1faeSThomas Huth 
1631e8a1faeSThomas Huth     /* Verify that we have an AHCI device present. */
1641e8a1faeSThomas Huth     s->dev = get_ahci_device(s->parent->qts, &s->fingerprint);
1651e8a1faeSThomas Huth 
1661e8a1faeSThomas Huth     return s;
1671e8a1faeSThomas Huth }
1681e8a1faeSThomas Huth 
1691e8a1faeSThomas Huth /**
1701e8a1faeSThomas Huth  * Start a Q35 machine and bookmark a handle to the AHCI device.
1711e8a1faeSThomas Huth  */
1720472b2e5SDaniel P. Berrangé G_GNUC_PRINTF(1, 2)
ahci_boot(const char * cli,...)1731e8a1faeSThomas Huth static AHCIQState *ahci_boot(const char *cli, ...)
1741e8a1faeSThomas Huth {
1751e8a1faeSThomas Huth     AHCIQState *s;
1761e8a1faeSThomas Huth     va_list ap;
1771e8a1faeSThomas Huth 
1781e8a1faeSThomas Huth     if (cli) {
1791e8a1faeSThomas Huth         va_start(ap, cli);
1801e8a1faeSThomas Huth         s = ahci_vboot(cli, ap);
1811e8a1faeSThomas Huth         va_end(ap);
1821e8a1faeSThomas Huth     } else {
1831e8a1faeSThomas Huth         cli = "-drive if=none,id=drive0,file=%s,cache=writeback,format=%s"
1841e8a1faeSThomas Huth             " -M q35 "
1851e8a1faeSThomas Huth             "-device ide-hd,drive=drive0 "
1861e8a1faeSThomas Huth             "-global ide-hd.serial=%s "
1871e8a1faeSThomas Huth             "-global ide-hd.ver=%s";
1881e8a1faeSThomas Huth         s = ahci_boot(cli, tmp_path, imgfmt, "testdisk", "version");
1891e8a1faeSThomas Huth     }
1901e8a1faeSThomas Huth 
1911e8a1faeSThomas Huth     return s;
1921e8a1faeSThomas Huth }
1931e8a1faeSThomas Huth 
1941e8a1faeSThomas Huth /**
1951e8a1faeSThomas Huth  * Clean up the PCI device, then terminate the QEMU instance.
1961e8a1faeSThomas Huth  */
ahci_shutdown(AHCIQState * ahci)1971e8a1faeSThomas Huth static void ahci_shutdown(AHCIQState *ahci)
1981e8a1faeSThomas Huth {
1991e8a1faeSThomas Huth     QOSState *qs = ahci->parent;
2001e8a1faeSThomas Huth 
2011e8a1faeSThomas Huth     ahci_clean_mem(ahci);
2021e8a1faeSThomas Huth     free_ahci_device(ahci->dev);
2031e8a1faeSThomas Huth     g_free(ahci);
2041e8a1faeSThomas Huth     qtest_shutdown(qs);
2051e8a1faeSThomas Huth }
2061e8a1faeSThomas Huth 
2071e8a1faeSThomas Huth /**
2081e8a1faeSThomas Huth  * Boot and fully enable the HBA device.
2091e8a1faeSThomas Huth  * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
2101e8a1faeSThomas Huth  */
2110472b2e5SDaniel P. Berrangé G_GNUC_PRINTF(1, 2)
ahci_boot_and_enable(const char * cli,...)2121e8a1faeSThomas Huth static AHCIQState *ahci_boot_and_enable(const char *cli, ...)
2131e8a1faeSThomas Huth {
2141e8a1faeSThomas Huth     AHCIQState *ahci;
2151e8a1faeSThomas Huth     va_list ap;
2161e8a1faeSThomas Huth     uint16_t buff[256];
2171e8a1faeSThomas Huth     uint8_t port;
2181e8a1faeSThomas Huth     uint8_t hello;
2191e8a1faeSThomas Huth 
2201e8a1faeSThomas Huth     if (cli) {
2211e8a1faeSThomas Huth         va_start(ap, cli);
2221e8a1faeSThomas Huth         ahci = ahci_vboot(cli, ap);
2231e8a1faeSThomas Huth         va_end(ap);
2241e8a1faeSThomas Huth     } else {
2251e8a1faeSThomas Huth         ahci = ahci_boot(NULL);
2261e8a1faeSThomas Huth     }
2271e8a1faeSThomas Huth 
2281e8a1faeSThomas Huth     ahci_pci_enable(ahci);
2291e8a1faeSThomas Huth     ahci_hba_enable(ahci);
2301e8a1faeSThomas Huth     /* Initialize test device */
2311e8a1faeSThomas Huth     port = ahci_port_select(ahci);
2321e8a1faeSThomas Huth     ahci_port_clear(ahci, port);
2331e8a1faeSThomas Huth     if (is_atapi(ahci, port)) {
2341e8a1faeSThomas Huth         hello = CMD_PACKET_ID;
2351e8a1faeSThomas Huth     } else {
2361e8a1faeSThomas Huth         hello = CMD_IDENTIFY;
2371e8a1faeSThomas Huth     }
2381e8a1faeSThomas Huth     ahci_io(ahci, port, hello, &buff, sizeof(buff), 0);
2391e8a1faeSThomas Huth 
2401e8a1faeSThomas Huth     return ahci;
2411e8a1faeSThomas Huth }
2421e8a1faeSThomas Huth 
2431e8a1faeSThomas Huth /*** Specification Adherence Tests ***/
2441e8a1faeSThomas Huth 
2451e8a1faeSThomas Huth /**
2461e8a1faeSThomas Huth  * Implementation for test_pci_spec. Ensures PCI configuration space is sane.
2471e8a1faeSThomas Huth  */
ahci_test_pci_spec(AHCIQState * ahci)2481e8a1faeSThomas Huth static void ahci_test_pci_spec(AHCIQState *ahci)
2491e8a1faeSThomas Huth {
2501e8a1faeSThomas Huth     uint8_t datab;
2511e8a1faeSThomas Huth     uint16_t data;
2521e8a1faeSThomas Huth     uint32_t datal;
2531e8a1faeSThomas Huth 
2541e8a1faeSThomas Huth     /* Most of these bits should start cleared until we turn them on. */
2551e8a1faeSThomas Huth     data = qpci_config_readw(ahci->dev, PCI_COMMAND);
2561e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_COMMAND_MEMORY);
2571e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_COMMAND_MASTER);
2581e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_COMMAND_SPECIAL);     /* Reserved */
2591e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_COMMAND_VGA_PALETTE); /* Reserved */
2601e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_COMMAND_PARITY);
2611e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_COMMAND_WAIT);        /* Reserved */
2621e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_COMMAND_SERR);
2631e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_COMMAND_FAST_BACK);
2641e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_COMMAND_INTX_DISABLE);
2651e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, 0xF800);                  /* Reserved */
2661e8a1faeSThomas Huth 
2671e8a1faeSThomas Huth     data = qpci_config_readw(ahci->dev, PCI_STATUS);
2681e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, 0x01 | 0x02 | 0x04);     /* Reserved */
2691e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_STATUS_INTERRUPT);
2701e8a1faeSThomas Huth     ASSERT_BIT_SET(data, PCI_STATUS_CAP_LIST);      /* must be set */
2711e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_STATUS_UDF);         /* Reserved */
2721e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_STATUS_PARITY);
2731e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_TARGET_ABORT);
2741e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_TARGET_ABORT);
2751e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_STATUS_REC_MASTER_ABORT);
2761e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_STATUS_SIG_SYSTEM_ERROR);
2771e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(data, PCI_STATUS_DETECTED_PARITY);
2781e8a1faeSThomas Huth 
2791e8a1faeSThomas Huth     /* RID occupies the low byte, CCs occupy the high three. */
2801e8a1faeSThomas Huth     datal = qpci_config_readl(ahci->dev, PCI_CLASS_REVISION);
2811e8a1faeSThomas Huth     if (ahci_pedantic) {
2821e8a1faeSThomas Huth         /* AHCI 1.3 specifies that at-boot, the RID should reset to 0x00,
2831e8a1faeSThomas Huth          * Though in practice this is likely seldom true. */
2841e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(datal, 0xFF);
2851e8a1faeSThomas Huth     }
2861e8a1faeSThomas Huth 
2871e8a1faeSThomas Huth     /* BCC *must* equal 0x01. */
2881e8a1faeSThomas Huth     g_assert_cmphex(PCI_BCC(datal), ==, 0x01);
2891e8a1faeSThomas Huth     if (PCI_SCC(datal) == 0x01) {
2901e8a1faeSThomas Huth         /* IDE */
2911e8a1faeSThomas Huth         ASSERT_BIT_SET(0x80000000, datal);
2921e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(0x60000000, datal);
2931e8a1faeSThomas Huth     } else if (PCI_SCC(datal) == 0x04) {
2941e8a1faeSThomas Huth         /* RAID */
2951e8a1faeSThomas Huth         g_assert_cmphex(PCI_PI(datal), ==, 0);
2961e8a1faeSThomas Huth     } else if (PCI_SCC(datal) == 0x06) {
2971e8a1faeSThomas Huth         /* AHCI */
2981e8a1faeSThomas Huth         g_assert_cmphex(PCI_PI(datal), ==, 0x01);
2991e8a1faeSThomas Huth     } else {
3001e8a1faeSThomas Huth         g_assert_not_reached();
3011e8a1faeSThomas Huth     }
3021e8a1faeSThomas Huth 
3031e8a1faeSThomas Huth     datab = qpci_config_readb(ahci->dev, PCI_CACHE_LINE_SIZE);
3041e8a1faeSThomas Huth     g_assert_cmphex(datab, ==, 0);
3051e8a1faeSThomas Huth 
3061e8a1faeSThomas Huth     datab = qpci_config_readb(ahci->dev, PCI_LATENCY_TIMER);
3071e8a1faeSThomas Huth     g_assert_cmphex(datab, ==, 0);
3081e8a1faeSThomas Huth 
3091e8a1faeSThomas Huth     /* Only the bottom 7 bits must be off. */
3101e8a1faeSThomas Huth     datab = qpci_config_readb(ahci->dev, PCI_HEADER_TYPE);
3111e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(datab, 0x7F);
3121e8a1faeSThomas Huth 
3131e8a1faeSThomas Huth     /* BIST is optional, but the low 7 bits must always start off regardless. */
3141e8a1faeSThomas Huth     datab = qpci_config_readb(ahci->dev, PCI_BIST);
3151e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(datab, 0x7F);
3161e8a1faeSThomas Huth 
3171e8a1faeSThomas Huth     /* BARS 0-4 do not have a boot spec, but ABAR/BAR5 must be clean. */
3181e8a1faeSThomas Huth     datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
3191e8a1faeSThomas Huth     g_assert_cmphex(datal, ==, 0);
3201e8a1faeSThomas Huth 
3211e8a1faeSThomas Huth     qpci_config_writel(ahci->dev, PCI_BASE_ADDRESS_5, 0xFFFFFFFF);
3221e8a1faeSThomas Huth     datal = qpci_config_readl(ahci->dev, PCI_BASE_ADDRESS_5);
3231e8a1faeSThomas Huth     /* ABAR must be 32-bit, memory mapped, non-prefetchable and
3241e8a1faeSThomas Huth      * must be >= 512 bytes. To that end, bits 0-8 must be off. */
3251e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(datal, 0xFF);
3261e8a1faeSThomas Huth 
3271e8a1faeSThomas Huth     /* Capability list MUST be present, */
3281e8a1faeSThomas Huth     datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST);
3291e8a1faeSThomas Huth     /* But these bits are reserved. */
3301e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(datal, ~0xFF);
3311e8a1faeSThomas Huth     g_assert_cmphex(datal, !=, 0);
3321e8a1faeSThomas Huth 
33396420a30SMichael Tokarev     /* Check specification adherence for capability extensions. */
3341e8a1faeSThomas Huth     data = qpci_config_readw(ahci->dev, datal);
3351e8a1faeSThomas Huth 
3361e8a1faeSThomas Huth     switch (ahci->fingerprint) {
3371e8a1faeSThomas Huth     case AHCI_INTEL_ICH9:
3381e8a1faeSThomas Huth         /* Intel ICH9 Family Datasheet 14.1.19 p.550 */
3391e8a1faeSThomas Huth         g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_MSI);
3401e8a1faeSThomas Huth         break;
3411e8a1faeSThomas Huth     default:
3421e8a1faeSThomas Huth         /* AHCI 1.3, Section 2.1.14 -- CAP must point to PMCAP. */
3431e8a1faeSThomas Huth         g_assert_cmphex((data & 0xFF), ==, PCI_CAP_ID_PM);
3441e8a1faeSThomas Huth     }
3451e8a1faeSThomas Huth 
3461e8a1faeSThomas Huth     ahci_test_pci_caps(ahci, data, (uint8_t)datal);
3471e8a1faeSThomas Huth 
3481e8a1faeSThomas Huth     /* Reserved. */
3491e8a1faeSThomas Huth     datal = qpci_config_readl(ahci->dev, PCI_CAPABILITY_LIST + 4);
3501e8a1faeSThomas Huth     g_assert_cmphex(datal, ==, 0);
3511e8a1faeSThomas Huth 
3521e8a1faeSThomas Huth     /* IPIN might vary, but ILINE must be off. */
3531e8a1faeSThomas Huth     datab = qpci_config_readb(ahci->dev, PCI_INTERRUPT_LINE);
3541e8a1faeSThomas Huth     g_assert_cmphex(datab, ==, 0);
3551e8a1faeSThomas Huth }
3561e8a1faeSThomas Huth 
3571e8a1faeSThomas Huth /**
3581e8a1faeSThomas Huth  * Test PCI capabilities for AHCI specification adherence.
3591e8a1faeSThomas Huth  */
ahci_test_pci_caps(AHCIQState * ahci,uint16_t header,uint8_t offset)3601e8a1faeSThomas Huth static void ahci_test_pci_caps(AHCIQState *ahci, uint16_t header,
3611e8a1faeSThomas Huth                                uint8_t offset)
3621e8a1faeSThomas Huth {
3631e8a1faeSThomas Huth     uint8_t cid = header & 0xFF;
3641e8a1faeSThomas Huth     uint8_t next = header >> 8;
3651e8a1faeSThomas Huth 
3661e8a1faeSThomas Huth     g_test_message("CID: %02x; next: %02x", cid, next);
3671e8a1faeSThomas Huth 
3681e8a1faeSThomas Huth     switch (cid) {
3691e8a1faeSThomas Huth     case PCI_CAP_ID_PM:
3701e8a1faeSThomas Huth         ahci_test_pmcap(ahci, offset);
3711e8a1faeSThomas Huth         break;
3721e8a1faeSThomas Huth     case PCI_CAP_ID_MSI:
3731e8a1faeSThomas Huth         ahci_test_msicap(ahci, offset);
3741e8a1faeSThomas Huth         break;
3751e8a1faeSThomas Huth     case PCI_CAP_ID_SATA:
3761e8a1faeSThomas Huth         ahci_test_satacap(ahci, offset);
3771e8a1faeSThomas Huth         break;
3781e8a1faeSThomas Huth 
3791e8a1faeSThomas Huth     default:
3801e8a1faeSThomas Huth         g_test_message("Unknown CAP 0x%02x", cid);
3811e8a1faeSThomas Huth     }
3821e8a1faeSThomas Huth 
3831e8a1faeSThomas Huth     if (next) {
3841e8a1faeSThomas Huth         ahci_test_pci_caps(ahci, qpci_config_readw(ahci->dev, next), next);
3851e8a1faeSThomas Huth     }
3861e8a1faeSThomas Huth }
3871e8a1faeSThomas Huth 
3881e8a1faeSThomas Huth /**
3891e8a1faeSThomas Huth  * Test SATA PCI capabilitity for AHCI specification adherence.
3901e8a1faeSThomas Huth  */
ahci_test_satacap(AHCIQState * ahci,uint8_t offset)3911e8a1faeSThomas Huth static void ahci_test_satacap(AHCIQState *ahci, uint8_t offset)
3921e8a1faeSThomas Huth {
3931e8a1faeSThomas Huth     uint16_t dataw;
3941e8a1faeSThomas Huth     uint32_t datal;
3951e8a1faeSThomas Huth 
3961e8a1faeSThomas Huth     g_test_message("Verifying SATACAP");
3971e8a1faeSThomas Huth 
3981e8a1faeSThomas Huth     /* Assert that the SATACAP version is 1.0, And reserved bits are empty. */
3991e8a1faeSThomas Huth     dataw = qpci_config_readw(ahci->dev, offset + 2);
4001e8a1faeSThomas Huth     g_assert_cmphex(dataw, ==, 0x10);
4011e8a1faeSThomas Huth 
4021e8a1faeSThomas Huth     /* Grab the SATACR1 register. */
4031e8a1faeSThomas Huth     datal = qpci_config_readw(ahci->dev, offset + 4);
4041e8a1faeSThomas Huth 
4051e8a1faeSThomas Huth     switch (datal & 0x0F) {
4061e8a1faeSThomas Huth     case 0x04: /* BAR0 */
4071e8a1faeSThomas Huth     case 0x05: /* BAR1 */
4081e8a1faeSThomas Huth     case 0x06:
4091e8a1faeSThomas Huth     case 0x07:
4101e8a1faeSThomas Huth     case 0x08:
4111e8a1faeSThomas Huth     case 0x09: /* BAR5 */
4121e8a1faeSThomas Huth     case 0x0F: /* Immediately following SATACR1 in PCI config space. */
4131e8a1faeSThomas Huth         break;
4141e8a1faeSThomas Huth     default:
4151e8a1faeSThomas Huth         /* Invalid BARLOC for the Index Data Pair. */
4161e8a1faeSThomas Huth         g_assert_not_reached();
4171e8a1faeSThomas Huth     }
4181e8a1faeSThomas Huth 
4191e8a1faeSThomas Huth     /* Reserved. */
4201e8a1faeSThomas Huth     g_assert_cmphex((datal >> 24), ==, 0x00);
4211e8a1faeSThomas Huth }
4221e8a1faeSThomas Huth 
4231e8a1faeSThomas Huth /**
4241e8a1faeSThomas Huth  * Test MSI PCI capability for AHCI specification adherence.
4251e8a1faeSThomas Huth  */
ahci_test_msicap(AHCIQState * ahci,uint8_t offset)4261e8a1faeSThomas Huth static void ahci_test_msicap(AHCIQState *ahci, uint8_t offset)
4271e8a1faeSThomas Huth {
4281e8a1faeSThomas Huth     uint16_t dataw;
4291e8a1faeSThomas Huth     uint32_t datal;
4301e8a1faeSThomas Huth 
4311e8a1faeSThomas Huth     g_test_message("Verifying MSICAP");
4321e8a1faeSThomas Huth 
4331e8a1faeSThomas Huth     dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_FLAGS);
4341e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_ENABLE);
4351e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_QSIZE);
4361e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_MSI_FLAGS_RESERVED);
4371e8a1faeSThomas Huth 
4381e8a1faeSThomas Huth     datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_LO);
4391e8a1faeSThomas Huth     g_assert_cmphex(datal, ==, 0);
4401e8a1faeSThomas Huth 
4411e8a1faeSThomas Huth     if (dataw & PCI_MSI_FLAGS_64BIT) {
4421e8a1faeSThomas Huth         g_test_message("MSICAP is 64bit");
4431e8a1faeSThomas Huth         datal = qpci_config_readl(ahci->dev, offset + PCI_MSI_ADDRESS_HI);
4441e8a1faeSThomas Huth         g_assert_cmphex(datal, ==, 0);
4451e8a1faeSThomas Huth         dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_64);
4461e8a1faeSThomas Huth         g_assert_cmphex(dataw, ==, 0);
4471e8a1faeSThomas Huth     } else {
4481e8a1faeSThomas Huth         g_test_message("MSICAP is 32bit");
4491e8a1faeSThomas Huth         dataw = qpci_config_readw(ahci->dev, offset + PCI_MSI_DATA_32);
4501e8a1faeSThomas Huth         g_assert_cmphex(dataw, ==, 0);
4511e8a1faeSThomas Huth     }
4521e8a1faeSThomas Huth }
4531e8a1faeSThomas Huth 
4541e8a1faeSThomas Huth /**
4551e8a1faeSThomas Huth  * Test Power Management PCI capability for AHCI specification adherence.
4561e8a1faeSThomas Huth  */
ahci_test_pmcap(AHCIQState * ahci,uint8_t offset)4571e8a1faeSThomas Huth static void ahci_test_pmcap(AHCIQState *ahci, uint8_t offset)
4581e8a1faeSThomas Huth {
4591e8a1faeSThomas Huth     uint16_t dataw;
4601e8a1faeSThomas Huth 
4611e8a1faeSThomas Huth     g_test_message("Verifying PMCAP");
4621e8a1faeSThomas Huth 
4631e8a1faeSThomas Huth     dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_PMC);
4641e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_PME_CLOCK);
4651e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_RESERVED);
4661e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D1);
4671e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_PM_CAP_D2);
4681e8a1faeSThomas Huth 
4691e8a1faeSThomas Huth     dataw = qpci_config_readw(ahci->dev, offset + PCI_PM_CTRL);
4701e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_STATE_MASK);
4711e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_RESERVED);
4721e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SEL_MASK);
4731e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(dataw, PCI_PM_CTRL_DATA_SCALE_MASK);
4741e8a1faeSThomas Huth }
4751e8a1faeSThomas Huth 
ahci_test_hba_spec(AHCIQState * ahci)4761e8a1faeSThomas Huth static void ahci_test_hba_spec(AHCIQState *ahci)
4771e8a1faeSThomas Huth {
4781e8a1faeSThomas Huth     unsigned i;
4791e8a1faeSThomas Huth     uint32_t reg;
4801e8a1faeSThomas Huth     uint32_t ports;
4811e8a1faeSThomas Huth     uint8_t nports_impl;
4821e8a1faeSThomas Huth     uint8_t maxports;
4831e8a1faeSThomas Huth 
4841e8a1faeSThomas Huth     g_assert(ahci != NULL);
4851e8a1faeSThomas Huth 
4861e8a1faeSThomas Huth     /*
4871e8a1faeSThomas Huth      * Note that the AHCI spec does expect the BIOS to set up a few things:
4881e8a1faeSThomas Huth      * CAP.SSS    - Support for staggered spin-up            (t/f)
4891e8a1faeSThomas Huth      * CAP.SMPS   - Support for mechanical presence switches (t/f)
4901e8a1faeSThomas Huth      * PI         - Ports Implemented                        (1-32)
4911e8a1faeSThomas Huth      * PxCMD.HPCP - Hot Plug Capable Port
4921e8a1faeSThomas Huth      * PxCMD.MPSP - Mechanical Presence Switch Present
4931e8a1faeSThomas Huth      * PxCMD.CPD  - Cold Presence Detection support
4941e8a1faeSThomas Huth      *
4951e8a1faeSThomas Huth      * Additional items are touched if CAP.SSS is on, see AHCI 10.1.1 p.97:
4961e8a1faeSThomas Huth      * Foreach Port Implemented:
4971e8a1faeSThomas Huth      * -PxCMD.ST, PxCMD.CR, PxCMD.FRE, PxCMD.FR, PxSCTL.DET are 0
4981e8a1faeSThomas Huth      * -PxCLB/U and PxFB/U are set to valid regions in memory
4991e8a1faeSThomas Huth      * -PxSUD is set to 1.
5001e8a1faeSThomas Huth      * -PxSSTS.DET is polled for presence; if detected, we continue:
5011e8a1faeSThomas Huth      * -PxSERR is cleared with 1's.
5021e8a1faeSThomas Huth      * -If PxTFD.STS.BSY, PxTFD.STS.DRQ, and PxTFD.STS.ERR are all zero,
5031e8a1faeSThomas Huth      *  the device is ready.
5041e8a1faeSThomas Huth      */
5051e8a1faeSThomas Huth 
5061e8a1faeSThomas Huth     /* 1 CAP - Capabilities Register */
5071e8a1faeSThomas Huth     ahci->cap = ahci_rreg(ahci, AHCI_CAP);
5081e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(ahci->cap, AHCI_CAP_RESERVED);
5091e8a1faeSThomas Huth 
5101e8a1faeSThomas Huth     /* 2 GHC - Global Host Control */
5111e8a1faeSThomas Huth     reg = ahci_rreg(ahci, AHCI_GHC);
5121e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_GHC_HR);
5131e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_GHC_IE);
5141e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_GHC_MRSM);
5151e8a1faeSThomas Huth     if (BITSET(ahci->cap, AHCI_CAP_SAM)) {
5161e8a1faeSThomas Huth         g_test_message("Supports AHCI-Only Mode: GHC_AE is Read-Only.");
5171e8a1faeSThomas Huth         ASSERT_BIT_SET(reg, AHCI_GHC_AE);
5181e8a1faeSThomas Huth     } else {
5191e8a1faeSThomas Huth         g_test_message("Supports AHCI/Legacy mix.");
5201e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_GHC_AE);
5211e8a1faeSThomas Huth     }
5221e8a1faeSThomas Huth 
5231e8a1faeSThomas Huth     /* 3 IS - Interrupt Status */
5241e8a1faeSThomas Huth     reg = ahci_rreg(ahci, AHCI_IS);
5251e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
5261e8a1faeSThomas Huth 
5271e8a1faeSThomas Huth     /* 4 PI - Ports Implemented */
5281e8a1faeSThomas Huth     ports = ahci_rreg(ahci, AHCI_PI);
5291e8a1faeSThomas Huth     /* Ports Implemented must be non-zero. */
5301e8a1faeSThomas Huth     g_assert_cmphex(ports, !=, 0);
5311e8a1faeSThomas Huth     /* Ports Implemented must be <= Number of Ports. */
5321e8a1faeSThomas Huth     nports_impl = ctpopl(ports);
5331e8a1faeSThomas Huth     g_assert_cmpuint(((AHCI_CAP_NP & ahci->cap) + 1), >=, nports_impl);
5341e8a1faeSThomas Huth 
5351e8a1faeSThomas Huth     /* Ports must be within the proper range. Given a mapping of SIZE,
5361e8a1faeSThomas Huth      * 256 bytes are used for global HBA control, and the rest is used
5371e8a1faeSThomas Huth      * for ports data, at 0x80 bytes each. */
5381e8a1faeSThomas Huth     g_assert_cmphex(ahci->barsize, >, 0);
5391e8a1faeSThomas Huth     maxports = (ahci->barsize - HBA_DATA_REGION_SIZE) / HBA_PORT_DATA_SIZE;
5401e8a1faeSThomas Huth     /* e.g, 30 ports for 4K of memory. (4096 - 256) / 128 = 30 */
5411e8a1faeSThomas Huth     g_assert_cmphex((reg >> maxports), ==, 0);
5421e8a1faeSThomas Huth 
5431e8a1faeSThomas Huth     /* 5 AHCI Version */
5441e8a1faeSThomas Huth     reg = ahci_rreg(ahci, AHCI_VS);
5451e8a1faeSThomas Huth     switch (reg) {
5461e8a1faeSThomas Huth     case AHCI_VERSION_0_95:
5471e8a1faeSThomas Huth     case AHCI_VERSION_1_0:
5481e8a1faeSThomas Huth     case AHCI_VERSION_1_1:
5491e8a1faeSThomas Huth     case AHCI_VERSION_1_2:
5501e8a1faeSThomas Huth     case AHCI_VERSION_1_3:
5511e8a1faeSThomas Huth         break;
5521e8a1faeSThomas Huth     default:
5531e8a1faeSThomas Huth         g_assert_not_reached();
5541e8a1faeSThomas Huth     }
5551e8a1faeSThomas Huth 
5561e8a1faeSThomas Huth     /* 6 Command Completion Coalescing Control: depends on CAP.CCCS. */
5571e8a1faeSThomas Huth     reg = ahci_rreg(ahci, AHCI_CCCCTL);
5581e8a1faeSThomas Huth     if (BITSET(ahci->cap, AHCI_CAP_CCCS)) {
5591e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_EN);
5601e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_CCCCTL_RESERVED);
5611e8a1faeSThomas Huth         ASSERT_BIT_SET(reg, AHCI_CCCCTL_CC);
5621e8a1faeSThomas Huth         ASSERT_BIT_SET(reg, AHCI_CCCCTL_TV);
5631e8a1faeSThomas Huth     } else {
5641e8a1faeSThomas Huth         g_assert_cmphex(reg, ==, 0);
5651e8a1faeSThomas Huth     }
5661e8a1faeSThomas Huth 
5671e8a1faeSThomas Huth     /* 7 CCC_PORTS */
5681e8a1faeSThomas Huth     reg = ahci_rreg(ahci, AHCI_CCCPORTS);
5691e8a1faeSThomas Huth     /* Must be zeroes initially regardless of CAP.CCCS */
5701e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
5711e8a1faeSThomas Huth 
5721e8a1faeSThomas Huth     /* 8 EM_LOC */
5731e8a1faeSThomas Huth     reg = ahci_rreg(ahci, AHCI_EMLOC);
5741e8a1faeSThomas Huth     if (BITCLR(ahci->cap, AHCI_CAP_EMS)) {
5751e8a1faeSThomas Huth         g_assert_cmphex(reg, ==, 0);
5761e8a1faeSThomas Huth     }
5771e8a1faeSThomas Huth 
5781e8a1faeSThomas Huth     /* 9 EM_CTL */
5791e8a1faeSThomas Huth     reg = ahci_rreg(ahci, AHCI_EMCTL);
5801e8a1faeSThomas Huth     if (BITSET(ahci->cap, AHCI_CAP_EMS)) {
5811e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_STSMR);
5821e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLTM);
5831e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_CTLRST);
5841e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_EMCTL_RESERVED);
5851e8a1faeSThomas Huth     } else {
5861e8a1faeSThomas Huth         g_assert_cmphex(reg, ==, 0);
5871e8a1faeSThomas Huth     }
5881e8a1faeSThomas Huth 
5891e8a1faeSThomas Huth     /* 10 CAP2 -- Capabilities Extended */
5901e8a1faeSThomas Huth     ahci->cap2 = ahci_rreg(ahci, AHCI_CAP2);
5911e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(ahci->cap2, AHCI_CAP2_RESERVED);
5921e8a1faeSThomas Huth 
5931e8a1faeSThomas Huth     /* 11 BOHC -- Bios/OS Handoff Control */
5941e8a1faeSThomas Huth     reg = ahci_rreg(ahci, AHCI_BOHC);
5951e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
5961e8a1faeSThomas Huth 
5971e8a1faeSThomas Huth     /* 12 -- 23: Reserved */
5981e8a1faeSThomas Huth     g_test_message("Verifying HBA reserved area is empty.");
5991e8a1faeSThomas Huth     for (i = AHCI_RESERVED; i < AHCI_NVMHCI; ++i) {
6001e8a1faeSThomas Huth         reg = ahci_rreg(ahci, i);
6011e8a1faeSThomas Huth         g_assert_cmphex(reg, ==, 0);
6021e8a1faeSThomas Huth     }
6031e8a1faeSThomas Huth 
6041e8a1faeSThomas Huth     /* 24 -- 39: NVMHCI */
6051e8a1faeSThomas Huth     if (BITCLR(ahci->cap2, AHCI_CAP2_NVMP)) {
6061e8a1faeSThomas Huth         g_test_message("Verifying HBA/NVMHCI area is empty.");
6071e8a1faeSThomas Huth         for (i = AHCI_NVMHCI; i < AHCI_VENDOR; ++i) {
6081e8a1faeSThomas Huth             reg = ahci_rreg(ahci, i);
6091e8a1faeSThomas Huth             g_assert_cmphex(reg, ==, 0);
6101e8a1faeSThomas Huth         }
6111e8a1faeSThomas Huth     }
6121e8a1faeSThomas Huth 
6131e8a1faeSThomas Huth     /* 40 -- 63: Vendor */
6141e8a1faeSThomas Huth     g_test_message("Verifying HBA/Vendor area is empty.");
6151e8a1faeSThomas Huth     for (i = AHCI_VENDOR; i < AHCI_PORTS; ++i) {
6161e8a1faeSThomas Huth         reg = ahci_rreg(ahci, i);
6171e8a1faeSThomas Huth         g_assert_cmphex(reg, ==, 0);
6181e8a1faeSThomas Huth     }
6191e8a1faeSThomas Huth 
6201e8a1faeSThomas Huth     /* 64 -- XX: Port Space */
6211e8a1faeSThomas Huth     for (i = 0; ports || (i < maxports); ports >>= 1, ++i) {
6221e8a1faeSThomas Huth         if (BITSET(ports, 0x1)) {
6231e8a1faeSThomas Huth             g_test_message("Testing port %u for spec", i);
6241e8a1faeSThomas Huth             ahci_test_port_spec(ahci, i);
6251e8a1faeSThomas Huth         } else {
6261e8a1faeSThomas Huth             uint16_t j;
6271e8a1faeSThomas Huth             uint16_t low = AHCI_PORTS + (32 * i);
6281e8a1faeSThomas Huth             uint16_t high = AHCI_PORTS + (32 * (i + 1));
6291e8a1faeSThomas Huth             g_test_message("Asserting unimplemented port %u "
6301e8a1faeSThomas Huth                            "(reg [%u-%u]) is empty.",
6311e8a1faeSThomas Huth                            i, low, high - 1);
6321e8a1faeSThomas Huth             for (j = low; j < high; ++j) {
6331e8a1faeSThomas Huth                 reg = ahci_rreg(ahci, j);
6341e8a1faeSThomas Huth                 g_assert_cmphex(reg, ==, 0);
6351e8a1faeSThomas Huth             }
6361e8a1faeSThomas Huth         }
6371e8a1faeSThomas Huth     }
6381e8a1faeSThomas Huth }
6391e8a1faeSThomas Huth 
6401e8a1faeSThomas Huth /**
6411e8a1faeSThomas Huth  * Test the memory space for one port for specification adherence.
6421e8a1faeSThomas Huth  */
ahci_test_port_spec(AHCIQState * ahci,uint8_t port)6431e8a1faeSThomas Huth static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
6441e8a1faeSThomas Huth {
6451e8a1faeSThomas Huth     uint32_t reg;
6461e8a1faeSThomas Huth     unsigned i;
6471e8a1faeSThomas Huth 
6481e8a1faeSThomas Huth     /* (0) CLB */
6491e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_CLB);
6501e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CLB_RESERVED);
6511e8a1faeSThomas Huth 
6521e8a1faeSThomas Huth     /* (1) CLBU */
6531e8a1faeSThomas Huth     if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
6541e8a1faeSThomas Huth         reg = ahci_px_rreg(ahci, port, AHCI_PX_CLBU);
6551e8a1faeSThomas Huth         g_assert_cmphex(reg, ==, 0);
6561e8a1faeSThomas Huth     }
6571e8a1faeSThomas Huth 
6581e8a1faeSThomas Huth     /* (2) FB */
6591e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_FB);
6601e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_FB_RESERVED);
6611e8a1faeSThomas Huth 
6621e8a1faeSThomas Huth     /* (3) FBU */
6631e8a1faeSThomas Huth     if (BITCLR(ahci->cap, AHCI_CAP_S64A)) {
6641e8a1faeSThomas Huth         reg = ahci_px_rreg(ahci, port, AHCI_PX_FBU);
6651e8a1faeSThomas Huth         g_assert_cmphex(reg, ==, 0);
6661e8a1faeSThomas Huth     }
6671e8a1faeSThomas Huth 
6681e8a1faeSThomas Huth     /* (4) IS */
6691e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_IS);
6701e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
6711e8a1faeSThomas Huth 
6721e8a1faeSThomas Huth     /* (5) IE */
6731e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_IE);
6741e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
6751e8a1faeSThomas Huth 
6761e8a1faeSThomas Huth     /* (6) CMD */
6771e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_CMD);
6781e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FRE);
6791e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_RESERVED);
6801e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CCS);
6811e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FR);
6821e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CR);
6831e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_PMA); /* And RW only if CAP.SPM */
6841e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_APSTE); /* RW only if CAP2.APST */
6851e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ATAPI);
6861e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_DLAE);
6871e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ALPE);  /* RW only if CAP.SALP */
6881e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ASP);   /* RW only if CAP.SALP */
6891e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_ICC);
6901e8a1faeSThomas Huth     /* If CPDetect support does not exist, CPState must be off. */
6911e8a1faeSThomas Huth     if (BITCLR(reg, AHCI_PX_CMD_CPD)) {
6921e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_CPS);
6931e8a1faeSThomas Huth     }
6941e8a1faeSThomas Huth     /* If MPSPresence is not set, MPSState must be off. */
6951e8a1faeSThomas Huth     if (BITCLR(reg, AHCI_PX_CMD_MPSP)) {
6961e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
6971e8a1faeSThomas Huth     }
6981e8a1faeSThomas Huth     /* If we do not support MPS, MPSS and MPSP must be off. */
6991e8a1faeSThomas Huth     if (BITCLR(ahci->cap, AHCI_CAP_SMPS)) {
7001e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSS);
7011e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_MPSP);
7021e8a1faeSThomas Huth     }
7031e8a1faeSThomas Huth     /* If, via CPD or MPSP we detect a drive, HPCP must be on. */
7041e8a1faeSThomas Huth     if (BITANY(reg, AHCI_PX_CMD_CPD | AHCI_PX_CMD_MPSP)) {
7051e8a1faeSThomas Huth         ASSERT_BIT_SET(reg, AHCI_PX_CMD_HPCP);
7061e8a1faeSThomas Huth     }
7071e8a1faeSThomas Huth     /* HPCP and ESP cannot both be active. */
7081e8a1faeSThomas Huth     g_assert(!BITSET(reg, AHCI_PX_CMD_HPCP | AHCI_PX_CMD_ESP));
7091e8a1faeSThomas Huth     /* If CAP.FBSS is not set, FBSCP must not be set. */
7101e8a1faeSThomas Huth     if (BITCLR(ahci->cap, AHCI_CAP_FBSS)) {
7111e8a1faeSThomas Huth         ASSERT_BIT_CLEAR(reg, AHCI_PX_CMD_FBSCP);
7121e8a1faeSThomas Huth     }
7131e8a1faeSThomas Huth 
7141e8a1faeSThomas Huth     /* (7) RESERVED */
7151e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_RES1);
7161e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
7171e8a1faeSThomas Huth 
7181e8a1faeSThomas Huth     /* (8) TFD */
7191e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_TFD);
7201e8a1faeSThomas Huth     /* At boot, prior to an FIS being received, the TFD register should be 0x7F,
7211e8a1faeSThomas Huth      * which breaks down as follows, as seen in AHCI 1.3 sec 3.3.8, p. 27. */
7221e8a1faeSThomas Huth     ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_ERR);
7231e8a1faeSThomas Huth     ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS1);
7241e8a1faeSThomas Huth     ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_DRQ);
7251e8a1faeSThomas Huth     ASSERT_BIT_SET(reg, AHCI_PX_TFD_STS_CS2);
7261e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
7271e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
7281e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_RESERVED);
7291e8a1faeSThomas Huth 
7301e8a1faeSThomas Huth     /* (9) SIG */
7311e8a1faeSThomas Huth     /* Though AHCI specifies the boot value should be 0xFFFFFFFF,
7321e8a1faeSThomas Huth      * Even when GHC.ST is zero, the AHCI HBA may receive the initial
7331e8a1faeSThomas Huth      * D2H register FIS and update the signature asynchronously,
7341e8a1faeSThomas Huth      * so we cannot expect a value here. AHCI 1.3, sec 3.3.9, pp 27-28 */
7351e8a1faeSThomas Huth 
7361e8a1faeSThomas Huth     /* (10) SSTS / SCR0: SStatus */
7371e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_SSTS);
7381e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_SSTS_RESERVED);
7391e8a1faeSThomas Huth     /* Even though the register should be 0 at boot, it is asynchronous and
7401e8a1faeSThomas Huth      * prone to change, so we cannot test any well known value. */
7411e8a1faeSThomas Huth 
7421e8a1faeSThomas Huth     /* (11) SCTL / SCR2: SControl */
7431e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_SCTL);
7441e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
7451e8a1faeSThomas Huth 
7461e8a1faeSThomas Huth     /* (12) SERR / SCR1: SError */
7471e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_SERR);
7481e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
7491e8a1faeSThomas Huth 
7501e8a1faeSThomas Huth     /* (13) SACT / SCR3: SActive */
7511e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_SACT);
7521e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
7531e8a1faeSThomas Huth 
7541e8a1faeSThomas Huth     /* (14) CI */
7551e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_CI);
7561e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
7571e8a1faeSThomas Huth 
7581e8a1faeSThomas Huth     /* (15) SNTF */
7591e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_SNTF);
7601e8a1faeSThomas Huth     g_assert_cmphex(reg, ==, 0);
7611e8a1faeSThomas Huth 
7621e8a1faeSThomas Huth     /* (16) FBS */
7631e8a1faeSThomas Huth     reg = ahci_px_rreg(ahci, port, AHCI_PX_FBS);
7641e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_EN);
7651e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEC);
7661e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_SDE);
7671e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DEV);
7681e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_DWE);
7691e8a1faeSThomas Huth     ASSERT_BIT_CLEAR(reg, AHCI_PX_FBS_RESERVED);
7701e8a1faeSThomas Huth     if (BITSET(ahci->cap, AHCI_CAP_FBSS)) {
7711e8a1faeSThomas Huth         /* if Port-Multiplier FIS-based switching avail, ADO must >= 2 */
7721e8a1faeSThomas Huth         g_assert((reg & AHCI_PX_FBS_ADO) >> ctzl(AHCI_PX_FBS_ADO) >= 2);
7731e8a1faeSThomas Huth     }
7741e8a1faeSThomas Huth 
7751e8a1faeSThomas Huth     /* [17 -- 27] RESERVED */
7761e8a1faeSThomas Huth     for (i = AHCI_PX_RES2; i < AHCI_PX_VS; ++i) {
7771e8a1faeSThomas Huth         reg = ahci_px_rreg(ahci, port, i);
7781e8a1faeSThomas Huth         g_assert_cmphex(reg, ==, 0);
7791e8a1faeSThomas Huth     }
7801e8a1faeSThomas Huth 
7811e8a1faeSThomas Huth     /* [28 -- 31] Vendor-Specific */
7821e8a1faeSThomas Huth     for (i = AHCI_PX_VS; i < 32; ++i) {
7831e8a1faeSThomas Huth         reg = ahci_px_rreg(ahci, port, i);
7841e8a1faeSThomas Huth         if (reg) {
7851e8a1faeSThomas Huth             g_test_message("INFO: Vendor register %u non-empty", i);
7861e8a1faeSThomas Huth         }
7871e8a1faeSThomas Huth     }
7881e8a1faeSThomas Huth }
7891e8a1faeSThomas Huth 
7901e8a1faeSThomas Huth /**
7911e8a1faeSThomas Huth  * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
7921e8a1faeSThomas Huth  * device we see, then read and check the response.
7931e8a1faeSThomas Huth  */
ahci_test_identify(AHCIQState * ahci)7941e8a1faeSThomas Huth static void ahci_test_identify(AHCIQState *ahci)
7951e8a1faeSThomas Huth {
7961e8a1faeSThomas Huth     uint16_t buff[256];
7971e8a1faeSThomas Huth     unsigned px;
7981e8a1faeSThomas Huth     int rc;
7991e8a1faeSThomas Huth     uint16_t sect_size;
8001e8a1faeSThomas Huth     const size_t buffsize = 512;
8011e8a1faeSThomas Huth 
8021e8a1faeSThomas Huth     g_assert(ahci != NULL);
8031e8a1faeSThomas Huth 
8041e8a1faeSThomas Huth     /**
8051e8a1faeSThomas Huth      * This serves as a bit of a tutorial on AHCI device programming:
8061e8a1faeSThomas Huth      *
8071e8a1faeSThomas Huth      * (1) Create a data buffer for the IDENTIFY response to be sent to
8081e8a1faeSThomas Huth      * (2) Create a Command Table buffer, where we will store the
8091e8a1faeSThomas Huth      *     command and PRDT (Physical Region Descriptor Table)
8101e8a1faeSThomas Huth      * (3) Construct an FIS host-to-device command structure, and write it to
8111e8a1faeSThomas Huth      *     the top of the Command Table buffer.
8121e8a1faeSThomas Huth      * (4) Create one or more Physical Region Descriptors (PRDs) that describe
8131e8a1faeSThomas Huth      *     a location in memory where data may be stored/retrieved.
8141e8a1faeSThomas Huth      * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
8151e8a1faeSThomas Huth      * (6) Each AHCI port has up to 32 command slots. Each slot contains a
8161e8a1faeSThomas Huth      *     header that points to a Command Table buffer. Pick an unused slot
8171e8a1faeSThomas Huth      *     and update it to point to the Command Table we have built.
8181e8a1faeSThomas Huth      * (7) Now: Command #n points to our Command Table, and our Command Table
8191e8a1faeSThomas Huth      *     contains the FIS (that describes our command) and the PRDTL, which
8201e8a1faeSThomas Huth      *     describes our buffer.
8211e8a1faeSThomas Huth      * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
8221e8a1faeSThomas Huth      *     #n is ready for processing.
8231e8a1faeSThomas Huth      */
8241e8a1faeSThomas Huth 
8251e8a1faeSThomas Huth     /* Pick the first implemented and running port */
8261e8a1faeSThomas Huth     px = ahci_port_select(ahci);
8271e8a1faeSThomas Huth     g_test_message("Selected port %u for test", px);
8281e8a1faeSThomas Huth 
8291e8a1faeSThomas Huth     /* Clear out the FIS Receive area and any pending interrupts. */
8301e8a1faeSThomas Huth     ahci_port_clear(ahci, px);
8311e8a1faeSThomas Huth 
8321e8a1faeSThomas Huth     /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
8331e8a1faeSThomas Huth     ahci_io(ahci, px, CMD_IDENTIFY, &buff, buffsize, 0);
8341e8a1faeSThomas Huth 
8351e8a1faeSThomas Huth     /* Check serial number/version in the buffer */
8361e8a1faeSThomas Huth     /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
8371e8a1faeSThomas Huth      * Since we copy byte-for-byte in ahci-test, on both LE and BE, we need to
8381e8a1faeSThomas Huth      * unchunk this data. By contrast, ide-test copies 2 bytes at a time, and
8391e8a1faeSThomas Huth      * as a consequence, only needs to unchunk the data on LE machines. */
8401e8a1faeSThomas Huth     string_bswap16(&buff[10], 20);
8411e8a1faeSThomas Huth     rc = memcmp(&buff[10], "testdisk            ", 20);
8421e8a1faeSThomas Huth     g_assert_cmphex(rc, ==, 0);
8431e8a1faeSThomas Huth 
8441e8a1faeSThomas Huth     string_bswap16(&buff[23], 8);
8451e8a1faeSThomas Huth     rc = memcmp(&buff[23], "version ", 8);
8461e8a1faeSThomas Huth     g_assert_cmphex(rc, ==, 0);
8471e8a1faeSThomas Huth 
8481e8a1faeSThomas Huth     sect_size = le16_to_cpu(*((uint16_t *)(&buff[5])));
8491e8a1faeSThomas Huth     g_assert_cmphex(sect_size, ==, AHCI_SECTOR_SIZE);
8501e8a1faeSThomas Huth }
8511e8a1faeSThomas Huth 
ahci_test_io_rw_simple(AHCIQState * ahci,unsigned bufsize,uint64_t sector,uint8_t read_cmd,uint8_t write_cmd)8521e8a1faeSThomas Huth static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,
8531e8a1faeSThomas Huth                                    uint64_t sector, uint8_t read_cmd,
8541e8a1faeSThomas Huth                                    uint8_t write_cmd)
8551e8a1faeSThomas Huth {
8561e8a1faeSThomas Huth     uint64_t ptr;
8571e8a1faeSThomas Huth     uint8_t port;
8581e8a1faeSThomas Huth     unsigned char *tx = g_malloc(bufsize);
8591e8a1faeSThomas Huth     unsigned char *rx = g_malloc0(bufsize);
8601e8a1faeSThomas Huth 
8611e8a1faeSThomas Huth     g_assert(ahci != NULL);
8621e8a1faeSThomas Huth 
8631e8a1faeSThomas Huth     /* Pick the first running port and clear it. */
8641e8a1faeSThomas Huth     port = ahci_port_select(ahci);
8651e8a1faeSThomas Huth     ahci_port_clear(ahci, port);
8661e8a1faeSThomas Huth 
8671e8a1faeSThomas Huth     /*** Create pattern and transfer to guest ***/
8681e8a1faeSThomas Huth     /* Data buffer in the guest */
8691e8a1faeSThomas Huth     ptr = ahci_alloc(ahci, bufsize);
8701e8a1faeSThomas Huth     g_assert(ptr);
8711e8a1faeSThomas Huth 
8721e8a1faeSThomas Huth     /* Write some indicative pattern to our buffer. */
8731e8a1faeSThomas Huth     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
8741e8a1faeSThomas Huth     qtest_bufwrite(ahci->parent->qts, ptr, tx, bufsize);
8751e8a1faeSThomas Huth 
8761e8a1faeSThomas Huth     /* Write this buffer to disk, then read it back to the DMA buffer. */
8771e8a1faeSThomas Huth     ahci_guest_io(ahci, port, write_cmd, ptr, bufsize, sector);
8781e8a1faeSThomas Huth     qtest_memset(ahci->parent->qts, ptr, 0x00, bufsize);
8791e8a1faeSThomas Huth     ahci_guest_io(ahci, port, read_cmd, ptr, bufsize, sector);
8801e8a1faeSThomas Huth 
8811e8a1faeSThomas Huth     /*** Read back the Data ***/
8821e8a1faeSThomas Huth     qtest_bufread(ahci->parent->qts, ptr, rx, bufsize);
8831e8a1faeSThomas Huth     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
8841e8a1faeSThomas Huth 
8851e8a1faeSThomas Huth     ahci_free(ahci, ptr);
8861e8a1faeSThomas Huth     g_free(tx);
8871e8a1faeSThomas Huth     g_free(rx);
8881e8a1faeSThomas Huth }
8891e8a1faeSThomas Huth 
ahci_test_nondata(AHCIQState * ahci,uint8_t ide_cmd)8901e8a1faeSThomas Huth static uint8_t ahci_test_nondata(AHCIQState *ahci, uint8_t ide_cmd)
8911e8a1faeSThomas Huth {
8921e8a1faeSThomas Huth     uint8_t port;
8931e8a1faeSThomas Huth 
8941e8a1faeSThomas Huth     /* Sanitize */
8951e8a1faeSThomas Huth     port = ahci_port_select(ahci);
8961e8a1faeSThomas Huth     ahci_port_clear(ahci, port);
8971e8a1faeSThomas Huth 
8981e8a1faeSThomas Huth     ahci_io(ahci, port, ide_cmd, NULL, 0, 0);
8991e8a1faeSThomas Huth 
9001e8a1faeSThomas Huth     return port;
9011e8a1faeSThomas Huth }
9021e8a1faeSThomas Huth 
ahci_test_flush(AHCIQState * ahci)9031e8a1faeSThomas Huth static void ahci_test_flush(AHCIQState *ahci)
9041e8a1faeSThomas Huth {
9051e8a1faeSThomas Huth     ahci_test_nondata(ahci, CMD_FLUSH_CACHE);
9061e8a1faeSThomas Huth }
9071e8a1faeSThomas Huth 
ahci_test_max(AHCIQState * ahci)9081e8a1faeSThomas Huth static void ahci_test_max(AHCIQState *ahci)
9091e8a1faeSThomas Huth {
9101e8a1faeSThomas Huth     RegD2HFIS *d2h = g_malloc0(0x20);
9111e8a1faeSThomas Huth     uint64_t nsect;
9121e8a1faeSThomas Huth     uint8_t port;
9131e8a1faeSThomas Huth     uint8_t cmd;
9141e8a1faeSThomas Huth     uint64_t config_sect = mb_to_sectors(test_image_size_mb) - 1;
9151e8a1faeSThomas Huth 
9161e8a1faeSThomas Huth     if (config_sect > 0xFFFFFF) {
9171e8a1faeSThomas Huth         cmd = CMD_READ_MAX_EXT;
9181e8a1faeSThomas Huth     } else {
9191e8a1faeSThomas Huth         cmd = CMD_READ_MAX;
9201e8a1faeSThomas Huth     }
9211e8a1faeSThomas Huth 
9221e8a1faeSThomas Huth     port = ahci_test_nondata(ahci, cmd);
9231e8a1faeSThomas Huth     qtest_memread(ahci->parent->qts, ahci->port[port].fb + 0x40, d2h, 0x20);
9241e8a1faeSThomas Huth     nsect = (uint64_t)d2h->lba_hi[2] << 40 |
9251e8a1faeSThomas Huth         (uint64_t)d2h->lba_hi[1] << 32 |
9261e8a1faeSThomas Huth         (uint64_t)d2h->lba_hi[0] << 24 |
9271e8a1faeSThomas Huth         (uint64_t)d2h->lba_lo[2] << 16 |
9281e8a1faeSThomas Huth         (uint64_t)d2h->lba_lo[1] << 8 |
9291e8a1faeSThomas Huth         (uint64_t)d2h->lba_lo[0];
9301e8a1faeSThomas Huth 
9311e8a1faeSThomas Huth     g_assert_cmphex(nsect, ==, config_sect);
9321e8a1faeSThomas Huth     g_free(d2h);
9331e8a1faeSThomas Huth }
9341e8a1faeSThomas Huth 
9351e8a1faeSThomas Huth 
9361e8a1faeSThomas Huth /******************************************************************************/
9371e8a1faeSThomas Huth /* Test Interfaces                                                            */
9381e8a1faeSThomas Huth /******************************************************************************/
9391e8a1faeSThomas Huth 
9401e8a1faeSThomas Huth /**
9411e8a1faeSThomas Huth  * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
9421e8a1faeSThomas Huth  */
test_sanity(void)9431e8a1faeSThomas Huth static void test_sanity(void)
9441e8a1faeSThomas Huth {
9451e8a1faeSThomas Huth     AHCIQState *ahci;
9461e8a1faeSThomas Huth     ahci = ahci_boot(NULL);
9471e8a1faeSThomas Huth     ahci_shutdown(ahci);
9481e8a1faeSThomas Huth }
9491e8a1faeSThomas Huth 
9501e8a1faeSThomas Huth /**
9511e8a1faeSThomas Huth  * Ensure that the PCI configuration space for the AHCI device is in-line with
9521e8a1faeSThomas Huth  * the AHCI 1.3 specification for initial values.
9531e8a1faeSThomas Huth  */
test_pci_spec(void)9541e8a1faeSThomas Huth static void test_pci_spec(void)
9551e8a1faeSThomas Huth {
9561e8a1faeSThomas Huth     AHCIQState *ahci;
9571e8a1faeSThomas Huth     ahci = ahci_boot(NULL);
9581e8a1faeSThomas Huth     ahci_test_pci_spec(ahci);
9591e8a1faeSThomas Huth     ahci_shutdown(ahci);
9601e8a1faeSThomas Huth }
9611e8a1faeSThomas Huth 
9621e8a1faeSThomas Huth /**
9631e8a1faeSThomas Huth  * Engage the PCI AHCI device and sanity check the response.
9641e8a1faeSThomas Huth  * Perform additional PCI config space bringup for the HBA.
9651e8a1faeSThomas Huth  */
test_pci_enable(void)9661e8a1faeSThomas Huth static void test_pci_enable(void)
9671e8a1faeSThomas Huth {
9681e8a1faeSThomas Huth     AHCIQState *ahci;
9691e8a1faeSThomas Huth     ahci = ahci_boot(NULL);
9701e8a1faeSThomas Huth     ahci_pci_enable(ahci);
9711e8a1faeSThomas Huth     ahci_shutdown(ahci);
9721e8a1faeSThomas Huth }
9731e8a1faeSThomas Huth 
9741e8a1faeSThomas Huth /**
9751e8a1faeSThomas Huth  * Investigate the memory mapped regions of the HBA,
9761e8a1faeSThomas Huth  * and test them for AHCI specification adherence.
9771e8a1faeSThomas Huth  */
test_hba_spec(void)9781e8a1faeSThomas Huth static void test_hba_spec(void)
9791e8a1faeSThomas Huth {
9801e8a1faeSThomas Huth     AHCIQState *ahci;
9811e8a1faeSThomas Huth 
9821e8a1faeSThomas Huth     ahci = ahci_boot(NULL);
9831e8a1faeSThomas Huth     ahci_pci_enable(ahci);
9841e8a1faeSThomas Huth     ahci_test_hba_spec(ahci);
9851e8a1faeSThomas Huth     ahci_shutdown(ahci);
9861e8a1faeSThomas Huth }
9871e8a1faeSThomas Huth 
9881e8a1faeSThomas Huth /**
9891e8a1faeSThomas Huth  * Engage the HBA functionality of the AHCI PCI device,
9901e8a1faeSThomas Huth  * and bring it into a functional idle state.
9911e8a1faeSThomas Huth  */
test_hba_enable(void)9921e8a1faeSThomas Huth static void test_hba_enable(void)
9931e8a1faeSThomas Huth {
9941e8a1faeSThomas Huth     AHCIQState *ahci;
9951e8a1faeSThomas Huth 
9961e8a1faeSThomas Huth     ahci = ahci_boot(NULL);
9971e8a1faeSThomas Huth     ahci_pci_enable(ahci);
9981e8a1faeSThomas Huth     ahci_hba_enable(ahci);
9991e8a1faeSThomas Huth     ahci_shutdown(ahci);
10001e8a1faeSThomas Huth }
10011e8a1faeSThomas Huth 
10021e8a1faeSThomas Huth /**
10031e8a1faeSThomas Huth  * Bring up the device and issue an IDENTIFY command.
10041e8a1faeSThomas Huth  * Inspect the state of the HBA device and the data returned.
10051e8a1faeSThomas Huth  */
test_identify(void)10061e8a1faeSThomas Huth static void test_identify(void)
10071e8a1faeSThomas Huth {
10081e8a1faeSThomas Huth     AHCIQState *ahci;
10091e8a1faeSThomas Huth 
10101e8a1faeSThomas Huth     ahci = ahci_boot_and_enable(NULL);
10111e8a1faeSThomas Huth     ahci_test_identify(ahci);
10121e8a1faeSThomas Huth     ahci_shutdown(ahci);
10131e8a1faeSThomas Huth }
10141e8a1faeSThomas Huth 
10151e8a1faeSThomas Huth /**
10161e8a1faeSThomas Huth  * Fragmented DMA test: Perform a standard 4K DMA read/write
10171e8a1faeSThomas Huth  * test, but make sure the physical regions are fragmented to
10181e8a1faeSThomas Huth  * be very small, each just 32 bytes, to see how AHCI performs
10191e8a1faeSThomas Huth  * with chunks defined to be much less than a sector.
10201e8a1faeSThomas Huth  */
test_dma_fragmented(void)10211e8a1faeSThomas Huth static void test_dma_fragmented(void)
10221e8a1faeSThomas Huth {
10231e8a1faeSThomas Huth     AHCIQState *ahci;
10241e8a1faeSThomas Huth     AHCICommand *cmd;
10251e8a1faeSThomas Huth     uint8_t px;
10261e8a1faeSThomas Huth     size_t bufsize = 4096;
10271e8a1faeSThomas Huth     unsigned char *tx = g_malloc(bufsize);
10281e8a1faeSThomas Huth     unsigned char *rx = g_malloc0(bufsize);
10291e8a1faeSThomas Huth     uint64_t ptr;
10301e8a1faeSThomas Huth 
10311e8a1faeSThomas Huth     ahci = ahci_boot_and_enable(NULL);
10321e8a1faeSThomas Huth     px = ahci_port_select(ahci);
10331e8a1faeSThomas Huth     ahci_port_clear(ahci, px);
10341e8a1faeSThomas Huth 
10351e8a1faeSThomas Huth     /* create pattern */
10361e8a1faeSThomas Huth     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
10371e8a1faeSThomas Huth 
10381e8a1faeSThomas Huth     /* Create a DMA buffer in guest memory, and write our pattern to it. */
10391e8a1faeSThomas Huth     ptr = guest_alloc(&ahci->parent->alloc, bufsize);
10401e8a1faeSThomas Huth     g_assert(ptr);
10411e8a1faeSThomas Huth     qtest_bufwrite(ahci->parent->qts, ptr, tx, bufsize);
10421e8a1faeSThomas Huth 
10431e8a1faeSThomas Huth     cmd = ahci_command_create(CMD_WRITE_DMA);
10441e8a1faeSThomas Huth     ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
10451e8a1faeSThomas Huth     ahci_command_commit(ahci, cmd, px);
10461e8a1faeSThomas Huth     ahci_command_issue(ahci, cmd);
10471e8a1faeSThomas Huth     ahci_command_verify(ahci, cmd);
10481e8a1faeSThomas Huth     ahci_command_free(cmd);
10491e8a1faeSThomas Huth 
10501e8a1faeSThomas Huth     cmd = ahci_command_create(CMD_READ_DMA);
10511e8a1faeSThomas Huth     ahci_command_adjust(cmd, 0, ptr, bufsize, 32);
10521e8a1faeSThomas Huth     ahci_command_commit(ahci, cmd, px);
10531e8a1faeSThomas Huth     ahci_command_issue(ahci, cmd);
10541e8a1faeSThomas Huth     ahci_command_verify(ahci, cmd);
10551e8a1faeSThomas Huth     ahci_command_free(cmd);
10561e8a1faeSThomas Huth 
10571e8a1faeSThomas Huth     /* Read back the guest's receive buffer into local memory */
10581e8a1faeSThomas Huth     qtest_bufread(ahci->parent->qts, ptr, rx, bufsize);
10591e8a1faeSThomas Huth     guest_free(&ahci->parent->alloc, ptr);
10601e8a1faeSThomas Huth 
10611e8a1faeSThomas Huth     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
10621e8a1faeSThomas Huth 
10631e8a1faeSThomas Huth     ahci_shutdown(ahci);
10641e8a1faeSThomas Huth 
10651e8a1faeSThomas Huth     g_free(rx);
10661e8a1faeSThomas Huth     g_free(tx);
10671e8a1faeSThomas Huth }
10681e8a1faeSThomas Huth 
10691e8a1faeSThomas Huth /*
10701e8a1faeSThomas Huth  * Write sector 1 with random data to make AHCI storage dirty
10711e8a1faeSThomas Huth  * Needed for flush tests so that flushes actually go though the block layer
10721e8a1faeSThomas Huth  */
make_dirty(AHCIQState * ahci,uint8_t port)10731e8a1faeSThomas Huth static void make_dirty(AHCIQState* ahci, uint8_t port)
10741e8a1faeSThomas Huth {
10751e8a1faeSThomas Huth     uint64_t ptr;
10761e8a1faeSThomas Huth     unsigned bufsize = 512;
10771e8a1faeSThomas Huth 
10781e8a1faeSThomas Huth     ptr = ahci_alloc(ahci, bufsize);
10791e8a1faeSThomas Huth     g_assert(ptr);
10801e8a1faeSThomas Huth 
10811e8a1faeSThomas Huth     ahci_guest_io(ahci, port, CMD_WRITE_DMA, ptr, bufsize, 1);
10821e8a1faeSThomas Huth     ahci_free(ahci, ptr);
10831e8a1faeSThomas Huth }
10841e8a1faeSThomas Huth 
test_flush(void)10851e8a1faeSThomas Huth static void test_flush(void)
10861e8a1faeSThomas Huth {
10871e8a1faeSThomas Huth     AHCIQState *ahci;
10881e8a1faeSThomas Huth     uint8_t port;
10891e8a1faeSThomas Huth 
10901e8a1faeSThomas Huth     ahci = ahci_boot_and_enable(NULL);
10911e8a1faeSThomas Huth 
10921e8a1faeSThomas Huth     port = ahci_port_select(ahci);
10931e8a1faeSThomas Huth     ahci_port_clear(ahci, port);
10941e8a1faeSThomas Huth 
10951e8a1faeSThomas Huth     make_dirty(ahci, port);
10961e8a1faeSThomas Huth 
10971e8a1faeSThomas Huth     ahci_test_flush(ahci);
10981e8a1faeSThomas Huth     ahci_shutdown(ahci);
10991e8a1faeSThomas Huth }
11001e8a1faeSThomas Huth 
test_flush_retry(void)11011e8a1faeSThomas Huth static void test_flush_retry(void)
11021e8a1faeSThomas Huth {
11031e8a1faeSThomas Huth     AHCIQState *ahci;
11041e8a1faeSThomas Huth     AHCICommand *cmd;
11051e8a1faeSThomas Huth     uint8_t port;
11061e8a1faeSThomas Huth 
11071e8a1faeSThomas Huth     prepare_blkdebug_script(debug_path, "flush_to_disk");
11081e8a1faeSThomas Huth     ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
11091e8a1faeSThomas Huth                                 "format=%s,cache=writeback,"
11101e8a1faeSThomas Huth                                 "rerror=stop,werror=stop "
11111e8a1faeSThomas Huth                                 "-M q35 "
11121e8a1faeSThomas Huth                                 "-device ide-hd,drive=drive0 ",
11131e8a1faeSThomas Huth                                 debug_path,
11141e8a1faeSThomas Huth                                 tmp_path, imgfmt);
11151e8a1faeSThomas Huth 
11161e8a1faeSThomas Huth     port = ahci_port_select(ahci);
11171e8a1faeSThomas Huth     ahci_port_clear(ahci, port);
11181e8a1faeSThomas Huth 
11191e8a1faeSThomas Huth     /* Issue write so that flush actually goes to disk */
11201e8a1faeSThomas Huth     make_dirty(ahci, port);
11211e8a1faeSThomas Huth 
11221e8a1faeSThomas Huth     /* Issue Flush Command and wait for error */
11231e8a1faeSThomas Huth     cmd = ahci_guest_io_halt(ahci, port, CMD_FLUSH_CACHE, 0, 0, 0);
11241e8a1faeSThomas Huth     ahci_guest_io_resume(ahci, cmd);
11251e8a1faeSThomas Huth 
11261e8a1faeSThomas Huth     ahci_shutdown(ahci);
11271e8a1faeSThomas Huth }
11281e8a1faeSThomas Huth 
11291e8a1faeSThomas Huth /**
11301e8a1faeSThomas Huth  * Basic sanity test to boot a machine, find an AHCI device, and shutdown.
11311e8a1faeSThomas Huth  */
test_migrate_sanity(void)11321e8a1faeSThomas Huth static void test_migrate_sanity(void)
11331e8a1faeSThomas Huth {
11341e8a1faeSThomas Huth     AHCIQState *src, *dst;
11351e8a1faeSThomas Huth     char *uri = g_strdup_printf("unix:%s", mig_socket);
11361e8a1faeSThomas Huth 
11371e8a1faeSThomas Huth     src = ahci_boot("-m 384 -M q35 "
11381e8a1faeSThomas Huth                     "-drive if=ide,file=%s,format=%s ", tmp_path, imgfmt);
11391e8a1faeSThomas Huth     dst = ahci_boot("-m 384 -M q35 "
11401e8a1faeSThomas Huth                     "-drive if=ide,file=%s,format=%s "
11411e8a1faeSThomas Huth                     "-incoming %s", tmp_path, imgfmt, uri);
11421e8a1faeSThomas Huth 
11431e8a1faeSThomas Huth     ahci_migrate(src, dst, uri);
11441e8a1faeSThomas Huth 
11451e8a1faeSThomas Huth     ahci_shutdown(src);
11461e8a1faeSThomas Huth     ahci_shutdown(dst);
11471e8a1faeSThomas Huth     g_free(uri);
11481e8a1faeSThomas Huth }
11491e8a1faeSThomas Huth 
11501e8a1faeSThomas Huth /**
11511e8a1faeSThomas Huth  * Simple migration test: Write a pattern, migrate, then read.
11521e8a1faeSThomas Huth  */
ahci_migrate_simple(uint8_t cmd_read,uint8_t cmd_write)11531e8a1faeSThomas Huth static void ahci_migrate_simple(uint8_t cmd_read, uint8_t cmd_write)
11541e8a1faeSThomas Huth {
11551e8a1faeSThomas Huth     AHCIQState *src, *dst;
11561e8a1faeSThomas Huth     uint8_t px;
11571e8a1faeSThomas Huth     size_t bufsize = 4096;
11581e8a1faeSThomas Huth     unsigned char *tx = g_malloc(bufsize);
11591e8a1faeSThomas Huth     unsigned char *rx = g_malloc0(bufsize);
11601e8a1faeSThomas Huth     char *uri = g_strdup_printf("unix:%s", mig_socket);
11611e8a1faeSThomas Huth 
11621e8a1faeSThomas Huth     src = ahci_boot_and_enable("-m 384 -M q35 "
11631e8a1faeSThomas Huth                                "-drive if=ide,format=%s,file=%s ",
11641e8a1faeSThomas Huth                                imgfmt, tmp_path);
11651e8a1faeSThomas Huth     dst = ahci_boot("-m 384 -M q35 "
11661e8a1faeSThomas Huth                     "-drive if=ide,format=%s,file=%s "
11671e8a1faeSThomas Huth                     "-incoming %s", imgfmt, tmp_path, uri);
11681e8a1faeSThomas Huth 
11691e8a1faeSThomas Huth     /* initialize */
11701e8a1faeSThomas Huth     px = ahci_port_select(src);
11711e8a1faeSThomas Huth     ahci_port_clear(src, px);
11721e8a1faeSThomas Huth 
11731e8a1faeSThomas Huth     /* create pattern */
11741e8a1faeSThomas Huth     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
11751e8a1faeSThomas Huth 
11761e8a1faeSThomas Huth     /* Write, migrate, then read. */
11771e8a1faeSThomas Huth     ahci_io(src, px, cmd_write, tx, bufsize, 0);
11781e8a1faeSThomas Huth     ahci_migrate(src, dst, uri);
11791e8a1faeSThomas Huth     ahci_io(dst, px, cmd_read, rx, bufsize, 0);
11801e8a1faeSThomas Huth 
11811e8a1faeSThomas Huth     /* Verify pattern */
11821e8a1faeSThomas Huth     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
11831e8a1faeSThomas Huth 
11841e8a1faeSThomas Huth     ahci_shutdown(src);
11851e8a1faeSThomas Huth     ahci_shutdown(dst);
11861e8a1faeSThomas Huth     g_free(rx);
11871e8a1faeSThomas Huth     g_free(tx);
11881e8a1faeSThomas Huth     g_free(uri);
11891e8a1faeSThomas Huth }
11901e8a1faeSThomas Huth 
test_migrate_dma(void)11911e8a1faeSThomas Huth static void test_migrate_dma(void)
11921e8a1faeSThomas Huth {
11931e8a1faeSThomas Huth     ahci_migrate_simple(CMD_READ_DMA, CMD_WRITE_DMA);
11941e8a1faeSThomas Huth }
11951e8a1faeSThomas Huth 
test_migrate_ncq(void)11961e8a1faeSThomas Huth static void test_migrate_ncq(void)
11971e8a1faeSThomas Huth {
11981e8a1faeSThomas Huth     ahci_migrate_simple(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
11991e8a1faeSThomas Huth }
12001e8a1faeSThomas Huth 
12011e8a1faeSThomas Huth /**
12021e8a1faeSThomas Huth  * Halted IO Error Test
12031e8a1faeSThomas Huth  *
12041e8a1faeSThomas Huth  * Simulate an error on first write, Try to write a pattern,
12051e8a1faeSThomas Huth  * Confirm the VM has stopped, resume the VM, verify command
12061e8a1faeSThomas Huth  * has completed, then read back the data and verify.
12071e8a1faeSThomas Huth  */
ahci_halted_io_test(uint8_t cmd_read,uint8_t cmd_write)12081e8a1faeSThomas Huth static void ahci_halted_io_test(uint8_t cmd_read, uint8_t cmd_write)
12091e8a1faeSThomas Huth {
12101e8a1faeSThomas Huth     AHCIQState *ahci;
12111e8a1faeSThomas Huth     uint8_t port;
12121e8a1faeSThomas Huth     size_t bufsize = 4096;
12131e8a1faeSThomas Huth     unsigned char *tx = g_malloc(bufsize);
12141e8a1faeSThomas Huth     unsigned char *rx = g_malloc0(bufsize);
12151e8a1faeSThomas Huth     uint64_t ptr;
12161e8a1faeSThomas Huth     AHCICommand *cmd;
12171e8a1faeSThomas Huth 
12181e8a1faeSThomas Huth     prepare_blkdebug_script(debug_path, "write_aio");
12191e8a1faeSThomas Huth 
12201e8a1faeSThomas Huth     ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
12211e8a1faeSThomas Huth                                 "format=%s,cache=writeback,"
12221e8a1faeSThomas Huth                                 "rerror=stop,werror=stop "
12231e8a1faeSThomas Huth                                 "-M q35 "
12241e8a1faeSThomas Huth                                 "-device ide-hd,drive=drive0 ",
12251e8a1faeSThomas Huth                                 debug_path,
12261e8a1faeSThomas Huth                                 tmp_path, imgfmt);
12271e8a1faeSThomas Huth 
12281e8a1faeSThomas Huth     /* Initialize and prepare */
12291e8a1faeSThomas Huth     port = ahci_port_select(ahci);
12301e8a1faeSThomas Huth     ahci_port_clear(ahci, port);
12311e8a1faeSThomas Huth 
12321e8a1faeSThomas Huth     /* create DMA source buffer and write pattern */
12331e8a1faeSThomas Huth     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
12341e8a1faeSThomas Huth     ptr = ahci_alloc(ahci, bufsize);
12351e8a1faeSThomas Huth     g_assert(ptr);
12361e8a1faeSThomas Huth     qtest_memwrite(ahci->parent->qts, ptr, tx, bufsize);
12371e8a1faeSThomas Huth 
12381e8a1faeSThomas Huth     /* Attempt to write (and fail) */
12391e8a1faeSThomas Huth     cmd = ahci_guest_io_halt(ahci, port, cmd_write,
12401e8a1faeSThomas Huth                              ptr, bufsize, 0);
12411e8a1faeSThomas Huth 
12421e8a1faeSThomas Huth     /* Attempt to resume the command */
12431e8a1faeSThomas Huth     ahci_guest_io_resume(ahci, cmd);
12441e8a1faeSThomas Huth     ahci_free(ahci, ptr);
12451e8a1faeSThomas Huth 
12461e8a1faeSThomas Huth     /* Read back and verify */
12471e8a1faeSThomas Huth     ahci_io(ahci, port, cmd_read, rx, bufsize, 0);
12481e8a1faeSThomas Huth     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
12491e8a1faeSThomas Huth 
12501e8a1faeSThomas Huth     /* Cleanup and go home */
12511e8a1faeSThomas Huth     ahci_shutdown(ahci);
12521e8a1faeSThomas Huth     g_free(rx);
12531e8a1faeSThomas Huth     g_free(tx);
12541e8a1faeSThomas Huth }
12551e8a1faeSThomas Huth 
test_halted_dma(void)12561e8a1faeSThomas Huth static void test_halted_dma(void)
12571e8a1faeSThomas Huth {
12581e8a1faeSThomas Huth     ahci_halted_io_test(CMD_READ_DMA, CMD_WRITE_DMA);
12591e8a1faeSThomas Huth }
12601e8a1faeSThomas Huth 
test_halted_ncq(void)12611e8a1faeSThomas Huth static void test_halted_ncq(void)
12621e8a1faeSThomas Huth {
12631e8a1faeSThomas Huth     ahci_halted_io_test(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
12641e8a1faeSThomas Huth }
12651e8a1faeSThomas Huth 
12661e8a1faeSThomas Huth /**
12671e8a1faeSThomas Huth  * IO Error Migration Test
12681e8a1faeSThomas Huth  *
12691e8a1faeSThomas Huth  * Simulate an error on first write, Try to write a pattern,
12701e8a1faeSThomas Huth  * Confirm the VM has stopped, migrate, resume the VM,
12711e8a1faeSThomas Huth  * verify command has completed, then read back the data and verify.
12721e8a1faeSThomas Huth  */
ahci_migrate_halted_io(uint8_t cmd_read,uint8_t cmd_write)12731e8a1faeSThomas Huth static void ahci_migrate_halted_io(uint8_t cmd_read, uint8_t cmd_write)
12741e8a1faeSThomas Huth {
12751e8a1faeSThomas Huth     AHCIQState *src, *dst;
12761e8a1faeSThomas Huth     uint8_t port;
12771e8a1faeSThomas Huth     size_t bufsize = 4096;
12781e8a1faeSThomas Huth     unsigned char *tx = g_malloc(bufsize);
12791e8a1faeSThomas Huth     unsigned char *rx = g_malloc0(bufsize);
12801e8a1faeSThomas Huth     uint64_t ptr;
12811e8a1faeSThomas Huth     AHCICommand *cmd;
12821e8a1faeSThomas Huth     char *uri = g_strdup_printf("unix:%s", mig_socket);
12831e8a1faeSThomas Huth 
12841e8a1faeSThomas Huth     prepare_blkdebug_script(debug_path, "write_aio");
12851e8a1faeSThomas Huth 
12861e8a1faeSThomas Huth     src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
12871e8a1faeSThomas Huth                                "format=%s,cache=writeback,"
12881e8a1faeSThomas Huth                                "rerror=stop,werror=stop "
12891e8a1faeSThomas Huth                                "-M q35 "
12901e8a1faeSThomas Huth                                "-device ide-hd,drive=drive0 ",
12911e8a1faeSThomas Huth                                debug_path,
12921e8a1faeSThomas Huth                                tmp_path, imgfmt);
12931e8a1faeSThomas Huth 
12941e8a1faeSThomas Huth     dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
12951e8a1faeSThomas Huth                     "format=%s,cache=writeback,"
12961e8a1faeSThomas Huth                     "rerror=stop,werror=stop "
12971e8a1faeSThomas Huth                     "-M q35 "
12981e8a1faeSThomas Huth                     "-device ide-hd,drive=drive0 "
12991e8a1faeSThomas Huth                     "-incoming %s",
13001e8a1faeSThomas Huth                     tmp_path, imgfmt, uri);
13011e8a1faeSThomas Huth 
13021e8a1faeSThomas Huth     /* Initialize and prepare */
13031e8a1faeSThomas Huth     port = ahci_port_select(src);
13041e8a1faeSThomas Huth     ahci_port_clear(src, port);
13051e8a1faeSThomas Huth     generate_pattern(tx, bufsize, AHCI_SECTOR_SIZE);
13061e8a1faeSThomas Huth 
13071e8a1faeSThomas Huth     /* create DMA source buffer and write pattern */
13081e8a1faeSThomas Huth     ptr = ahci_alloc(src, bufsize);
13091e8a1faeSThomas Huth     g_assert(ptr);
13101e8a1faeSThomas Huth     qtest_memwrite(src->parent->qts, ptr, tx, bufsize);
13111e8a1faeSThomas Huth 
13121e8a1faeSThomas Huth     /* Write, trigger the VM to stop, migrate, then resume. */
13131e8a1faeSThomas Huth     cmd = ahci_guest_io_halt(src, port, cmd_write,
13141e8a1faeSThomas Huth                              ptr, bufsize, 0);
13151e8a1faeSThomas Huth     ahci_migrate(src, dst, uri);
13161e8a1faeSThomas Huth     ahci_guest_io_resume(dst, cmd);
13171e8a1faeSThomas Huth     ahci_free(dst, ptr);
13181e8a1faeSThomas Huth 
13191e8a1faeSThomas Huth     /* Read back */
13201e8a1faeSThomas Huth     ahci_io(dst, port, cmd_read, rx, bufsize, 0);
13211e8a1faeSThomas Huth 
13221e8a1faeSThomas Huth     /* Verify TX and RX are identical */
13231e8a1faeSThomas Huth     g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
13241e8a1faeSThomas Huth 
13251e8a1faeSThomas Huth     /* Cleanup and go home. */
13261e8a1faeSThomas Huth     ahci_shutdown(src);
13271e8a1faeSThomas Huth     ahci_shutdown(dst);
13281e8a1faeSThomas Huth     g_free(rx);
13291e8a1faeSThomas Huth     g_free(tx);
13301e8a1faeSThomas Huth     g_free(uri);
13311e8a1faeSThomas Huth }
13321e8a1faeSThomas Huth 
test_migrate_halted_dma(void)13331e8a1faeSThomas Huth static void test_migrate_halted_dma(void)
13341e8a1faeSThomas Huth {
13351e8a1faeSThomas Huth     ahci_migrate_halted_io(CMD_READ_DMA, CMD_WRITE_DMA);
13361e8a1faeSThomas Huth }
13371e8a1faeSThomas Huth 
test_migrate_halted_ncq(void)13381e8a1faeSThomas Huth static void test_migrate_halted_ncq(void)
13391e8a1faeSThomas Huth {
13401e8a1faeSThomas Huth     ahci_migrate_halted_io(READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED);
13411e8a1faeSThomas Huth }
13421e8a1faeSThomas Huth 
13431e8a1faeSThomas Huth /**
13441e8a1faeSThomas Huth  * Migration test: Try to flush, migrate, then resume.
13451e8a1faeSThomas Huth  */
test_flush_migrate(void)13461e8a1faeSThomas Huth static void test_flush_migrate(void)
13471e8a1faeSThomas Huth {
13481e8a1faeSThomas Huth     AHCIQState *src, *dst;
13491e8a1faeSThomas Huth     AHCICommand *cmd;
13501e8a1faeSThomas Huth     uint8_t px;
13511e8a1faeSThomas Huth     char *uri = g_strdup_printf("unix:%s", mig_socket);
13521e8a1faeSThomas Huth 
13531e8a1faeSThomas Huth     prepare_blkdebug_script(debug_path, "flush_to_disk");
13541e8a1faeSThomas Huth 
13551e8a1faeSThomas Huth     src = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
13561e8a1faeSThomas Huth                                "cache=writeback,rerror=stop,werror=stop,"
13571e8a1faeSThomas Huth                                "format=%s "
13581e8a1faeSThomas Huth                                "-M q35 "
13591e8a1faeSThomas Huth                                "-device ide-hd,drive=drive0 ",
13601e8a1faeSThomas Huth                                debug_path, tmp_path, imgfmt);
13611e8a1faeSThomas Huth     dst = ahci_boot("-drive file=%s,if=none,id=drive0,"
13621e8a1faeSThomas Huth                     "cache=writeback,rerror=stop,werror=stop,"
13631e8a1faeSThomas Huth                     "format=%s "
13641e8a1faeSThomas Huth                     "-M q35 "
13651e8a1faeSThomas Huth                     "-device ide-hd,drive=drive0 "
13661e8a1faeSThomas Huth                     "-incoming %s", tmp_path, imgfmt, uri);
13671e8a1faeSThomas Huth 
13681e8a1faeSThomas Huth     px = ahci_port_select(src);
13691e8a1faeSThomas Huth     ahci_port_clear(src, px);
13701e8a1faeSThomas Huth 
13711e8a1faeSThomas Huth     /* Dirty device so that flush reaches disk */
13721e8a1faeSThomas Huth     make_dirty(src, px);
13731e8a1faeSThomas Huth 
13741e8a1faeSThomas Huth     /* Issue Flush Command */
13751e8a1faeSThomas Huth     cmd = ahci_command_create(CMD_FLUSH_CACHE);
13761e8a1faeSThomas Huth     ahci_command_commit(src, cmd, px);
13771e8a1faeSThomas Huth     ahci_command_issue_async(src, cmd);
13781e8a1faeSThomas Huth     qtest_qmp_eventwait(src->parent->qts, "STOP");
13791e8a1faeSThomas Huth 
13801e8a1faeSThomas Huth     /* Migrate over */
13811e8a1faeSThomas Huth     ahci_migrate(src, dst, uri);
13821e8a1faeSThomas Huth 
13831e8a1faeSThomas Huth     /* Complete the command */
13841e8a1faeSThomas Huth     qtest_qmp_send(dst->parent->qts, "{'execute':'cont' }");
13851e8a1faeSThomas Huth     qtest_qmp_eventwait(dst->parent->qts, "RESUME");
13861e8a1faeSThomas Huth     ahci_command_wait(dst, cmd);
13871e8a1faeSThomas Huth     ahci_command_verify(dst, cmd);
13881e8a1faeSThomas Huth 
13891e8a1faeSThomas Huth     ahci_command_free(cmd);
13901e8a1faeSThomas Huth     ahci_shutdown(src);
13911e8a1faeSThomas Huth     ahci_shutdown(dst);
13921e8a1faeSThomas Huth     g_free(uri);
13931e8a1faeSThomas Huth }
13941e8a1faeSThomas Huth 
test_max(void)13951e8a1faeSThomas Huth static void test_max(void)
13961e8a1faeSThomas Huth {
13971e8a1faeSThomas Huth     AHCIQState *ahci;
13981e8a1faeSThomas Huth 
13991e8a1faeSThomas Huth     ahci = ahci_boot_and_enable(NULL);
14001e8a1faeSThomas Huth     ahci_test_max(ahci);
14011e8a1faeSThomas Huth     ahci_shutdown(ahci);
14021e8a1faeSThomas Huth }
14031e8a1faeSThomas Huth 
test_reset(void)14041e8a1faeSThomas Huth static void test_reset(void)
14051e8a1faeSThomas Huth {
14061e8a1faeSThomas Huth     AHCIQState *ahci;
14071e8a1faeSThomas Huth     int i;
14081e8a1faeSThomas Huth 
14091e8a1faeSThomas Huth     ahci = ahci_boot(NULL);
14101e8a1faeSThomas Huth     ahci_test_pci_spec(ahci);
14111e8a1faeSThomas Huth     ahci_pci_enable(ahci);
14121e8a1faeSThomas Huth 
14131e8a1faeSThomas Huth     for (i = 0; i < 2; i++) {
14141e8a1faeSThomas Huth         ahci_test_hba_spec(ahci);
14151e8a1faeSThomas Huth         ahci_hba_enable(ahci);
14161e8a1faeSThomas Huth         ahci_test_identify(ahci);
14171e8a1faeSThomas Huth         ahci_test_io_rw_simple(ahci, 4096, 0,
14181e8a1faeSThomas Huth                                CMD_READ_DMA_EXT,
14191e8a1faeSThomas Huth                                CMD_WRITE_DMA_EXT);
14201e8a1faeSThomas Huth         ahci_set(ahci, AHCI_GHC, AHCI_GHC_HR);
14211e8a1faeSThomas Huth         ahci_clean_mem(ahci);
14221e8a1faeSThomas Huth     }
14231e8a1faeSThomas Huth 
14241e8a1faeSThomas Huth     ahci_shutdown(ahci);
14251e8a1faeSThomas Huth }
14261e8a1faeSThomas Huth 
test_reset_pending_callback(void)1427*cc610857SFiona Ebner static void test_reset_pending_callback(void)
1428*cc610857SFiona Ebner {
1429*cc610857SFiona Ebner     AHCIQState *ahci;
1430*cc610857SFiona Ebner     AHCICommand *cmd;
1431*cc610857SFiona Ebner     uint8_t port;
1432*cc610857SFiona Ebner     uint64_t ptr1;
1433*cc610857SFiona Ebner     uint64_t ptr2;
1434*cc610857SFiona Ebner 
1435*cc610857SFiona Ebner     int bufsize = 4 * 1024;
1436*cc610857SFiona Ebner     int speed = bufsize + (bufsize / 2);
1437*cc610857SFiona Ebner     int offset1 = 0;
1438*cc610857SFiona Ebner     int offset2 = bufsize / AHCI_SECTOR_SIZE;
1439*cc610857SFiona Ebner 
1440*cc610857SFiona Ebner     g_autofree unsigned char *tx1 = g_malloc(bufsize);
1441*cc610857SFiona Ebner     g_autofree unsigned char *tx2 = g_malloc(bufsize);
1442*cc610857SFiona Ebner     g_autofree unsigned char *rx1 = g_malloc0(bufsize);
1443*cc610857SFiona Ebner     g_autofree unsigned char *rx2 = g_malloc0(bufsize);
1444*cc610857SFiona Ebner 
1445*cc610857SFiona Ebner     /* Uses throttling to make test independent of specific environment. */
1446*cc610857SFiona Ebner     ahci = ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,"
1447*cc610857SFiona Ebner                                 "cache=writeback,format=%s,"
1448*cc610857SFiona Ebner                                 "throttling.bps-write=%d "
1449*cc610857SFiona Ebner                                 "-M q35 "
1450*cc610857SFiona Ebner                                 "-device ide-hd,drive=drive0 ",
1451*cc610857SFiona Ebner                                 tmp_path, imgfmt, speed);
1452*cc610857SFiona Ebner 
1453*cc610857SFiona Ebner     port = ahci_port_select(ahci);
1454*cc610857SFiona Ebner     ahci_port_clear(ahci, port);
1455*cc610857SFiona Ebner 
1456*cc610857SFiona Ebner     ptr1 = ahci_alloc(ahci, bufsize);
1457*cc610857SFiona Ebner     ptr2 = ahci_alloc(ahci, bufsize);
1458*cc610857SFiona Ebner 
1459*cc610857SFiona Ebner     g_assert(ptr1 && ptr2);
1460*cc610857SFiona Ebner 
1461*cc610857SFiona Ebner     /* Need two different patterns. */
1462*cc610857SFiona Ebner     do {
1463*cc610857SFiona Ebner         generate_pattern(tx1, bufsize, AHCI_SECTOR_SIZE);
1464*cc610857SFiona Ebner         generate_pattern(tx2, bufsize, AHCI_SECTOR_SIZE);
1465*cc610857SFiona Ebner     } while (memcmp(tx1, tx2, bufsize) == 0);
1466*cc610857SFiona Ebner 
1467*cc610857SFiona Ebner     qtest_bufwrite(ahci->parent->qts, ptr1, tx1, bufsize);
1468*cc610857SFiona Ebner     qtest_bufwrite(ahci->parent->qts, ptr2, tx2, bufsize);
1469*cc610857SFiona Ebner 
1470*cc610857SFiona Ebner     /* Write to beginning of disk to check it wasn't overwritten later. */
1471*cc610857SFiona Ebner     ahci_guest_io(ahci, port, CMD_WRITE_DMA_EXT, ptr1, bufsize, offset1);
1472*cc610857SFiona Ebner 
1473*cc610857SFiona Ebner     /* Issue asynchronously to get a pending callback during reset. */
1474*cc610857SFiona Ebner     cmd = ahci_command_create(CMD_WRITE_DMA_EXT);
1475*cc610857SFiona Ebner     ahci_command_adjust(cmd, offset2, ptr2, bufsize, 0);
1476*cc610857SFiona Ebner     ahci_command_commit(ahci, cmd, port);
1477*cc610857SFiona Ebner     ahci_command_issue_async(ahci, cmd);
1478*cc610857SFiona Ebner 
1479*cc610857SFiona Ebner     ahci_set(ahci, AHCI_GHC, AHCI_GHC_HR);
1480*cc610857SFiona Ebner 
1481*cc610857SFiona Ebner     ahci_command_free(cmd);
1482*cc610857SFiona Ebner 
1483*cc610857SFiona Ebner     /* Wait for throttled write to finish. */
1484*cc610857SFiona Ebner     sleep(1);
1485*cc610857SFiona Ebner 
1486*cc610857SFiona Ebner     /* Start again. */
1487*cc610857SFiona Ebner     ahci_clean_mem(ahci);
1488*cc610857SFiona Ebner     ahci_pci_enable(ahci);
1489*cc610857SFiona Ebner     ahci_hba_enable(ahci);
1490*cc610857SFiona Ebner     port = ahci_port_select(ahci);
1491*cc610857SFiona Ebner     ahci_port_clear(ahci, port);
1492*cc610857SFiona Ebner 
1493*cc610857SFiona Ebner     /* Read and verify. */
1494*cc610857SFiona Ebner     ahci_guest_io(ahci, port, CMD_READ_DMA_EXT, ptr1, bufsize, offset1);
1495*cc610857SFiona Ebner     qtest_bufread(ahci->parent->qts, ptr1, rx1, bufsize);
1496*cc610857SFiona Ebner     g_assert_cmphex(memcmp(tx1, rx1, bufsize), ==, 0);
1497*cc610857SFiona Ebner 
1498*cc610857SFiona Ebner     ahci_guest_io(ahci, port, CMD_READ_DMA_EXT, ptr2, bufsize, offset2);
1499*cc610857SFiona Ebner     qtest_bufread(ahci->parent->qts, ptr2, rx2, bufsize);
1500*cc610857SFiona Ebner     g_assert_cmphex(memcmp(tx2, rx2, bufsize), ==, 0);
1501*cc610857SFiona Ebner 
1502*cc610857SFiona Ebner     ahci_free(ahci, ptr1);
1503*cc610857SFiona Ebner     ahci_free(ahci, ptr2);
1504*cc610857SFiona Ebner 
1505*cc610857SFiona Ebner     ahci_clean_mem(ahci);
1506*cc610857SFiona Ebner 
1507*cc610857SFiona Ebner     ahci_shutdown(ahci);
1508*cc610857SFiona Ebner }
1509*cc610857SFiona Ebner 
test_ncq_simple(void)15101e8a1faeSThomas Huth static void test_ncq_simple(void)
15111e8a1faeSThomas Huth {
15121e8a1faeSThomas Huth     AHCIQState *ahci;
15131e8a1faeSThomas Huth 
15141e8a1faeSThomas Huth     ahci = ahci_boot_and_enable(NULL);
15151e8a1faeSThomas Huth     ahci_test_io_rw_simple(ahci, 4096, 0,
15161e8a1faeSThomas Huth                            READ_FPDMA_QUEUED,
15171e8a1faeSThomas Huth                            WRITE_FPDMA_QUEUED);
15181e8a1faeSThomas Huth     ahci_shutdown(ahci);
15191e8a1faeSThomas Huth }
15201e8a1faeSThomas Huth 
prepare_iso(size_t size,unsigned char ** buf,char ** name)15211e8a1faeSThomas Huth static int prepare_iso(size_t size, unsigned char **buf, char **name)
15221e8a1faeSThomas Huth {
1523d9eefd35SBin Meng     g_autofree char *cdrom_path = NULL;
15241e8a1faeSThomas Huth     unsigned char *patt;
15251e8a1faeSThomas Huth     ssize_t ret;
1526d9eefd35SBin Meng     int fd = g_file_open_tmp("qtest.iso.XXXXXX", &cdrom_path, NULL);
15271e8a1faeSThomas Huth 
1528df1a312fSPhilippe Mathieu-Daudé     g_assert(fd != -1);
15291e8a1faeSThomas Huth     g_assert(buf);
15301e8a1faeSThomas Huth     g_assert(name);
15311e8a1faeSThomas Huth     patt = g_malloc(size);
15321e8a1faeSThomas Huth 
15331e8a1faeSThomas Huth     /* Generate a pattern and build a CDROM image to read from */
15341e8a1faeSThomas Huth     generate_pattern(patt, size, ATAPI_SECTOR_SIZE);
15351e8a1faeSThomas Huth     ret = write(fd, patt, size);
15361e8a1faeSThomas Huth     g_assert(ret == size);
15371e8a1faeSThomas Huth 
15381e8a1faeSThomas Huth     *name = g_strdup(cdrom_path);
15391e8a1faeSThomas Huth     *buf = patt;
15401e8a1faeSThomas Huth     return fd;
15411e8a1faeSThomas Huth }
15421e8a1faeSThomas Huth 
remove_iso(int fd,char * name)15431e8a1faeSThomas Huth static void remove_iso(int fd, char *name)
15441e8a1faeSThomas Huth {
15451e8a1faeSThomas Huth     unlink(name);
15461e8a1faeSThomas Huth     g_free(name);
15471e8a1faeSThomas Huth     close(fd);
15481e8a1faeSThomas Huth }
15491e8a1faeSThomas Huth 
ahci_cb_cmp_buff(AHCIQState * ahci,AHCICommand * cmd,const AHCIOpts * opts)15501e8a1faeSThomas Huth static int ahci_cb_cmp_buff(AHCIQState *ahci, AHCICommand *cmd,
15511e8a1faeSThomas Huth                             const AHCIOpts *opts)
15521e8a1faeSThomas Huth {
15531e8a1faeSThomas Huth     unsigned char *tx = opts->opaque;
15541e8a1faeSThomas Huth     unsigned char *rx;
15551e8a1faeSThomas Huth 
15561e8a1faeSThomas Huth     if (!opts->size) {
15571e8a1faeSThomas Huth         return 0;
15581e8a1faeSThomas Huth     }
15591e8a1faeSThomas Huth 
15601e8a1faeSThomas Huth     rx = g_malloc0(opts->size);
15611e8a1faeSThomas Huth     qtest_bufread(ahci->parent->qts, opts->buffer, rx, opts->size);
15621e8a1faeSThomas Huth     g_assert_cmphex(memcmp(tx, rx, opts->size), ==, 0);
15631e8a1faeSThomas Huth     g_free(rx);
15641e8a1faeSThomas Huth 
15651e8a1faeSThomas Huth     return 0;
15661e8a1faeSThomas Huth }
15671e8a1faeSThomas Huth 
ahci_test_cdrom(int nsectors,bool dma,uint8_t cmd,bool override_bcl,uint16_t bcl)15681e8a1faeSThomas Huth static void ahci_test_cdrom(int nsectors, bool dma, uint8_t cmd,
15691e8a1faeSThomas Huth                             bool override_bcl, uint16_t bcl)
15701e8a1faeSThomas Huth {
15711e8a1faeSThomas Huth     AHCIQState *ahci;
15721e8a1faeSThomas Huth     unsigned char *tx;
15731e8a1faeSThomas Huth     char *iso;
15741e8a1faeSThomas Huth     int fd;
15751e8a1faeSThomas Huth     AHCIOpts opts = {
1576423dbce5SPeter Maydell         .size = ((uint64_t)ATAPI_SECTOR_SIZE * nsectors),
15771e8a1faeSThomas Huth         .atapi = true,
15781e8a1faeSThomas Huth         .atapi_dma = dma,
15791e8a1faeSThomas Huth         .post_cb = ahci_cb_cmp_buff,
15801e8a1faeSThomas Huth         .set_bcl = override_bcl,
15811e8a1faeSThomas Huth         .bcl = bcl,
15821e8a1faeSThomas Huth     };
1583423dbce5SPeter Maydell     uint64_t iso_size = (uint64_t)ATAPI_SECTOR_SIZE * (nsectors + 1);
15841e8a1faeSThomas Huth 
15851e8a1faeSThomas Huth     /* Prepare ISO and fill 'tx' buffer */
15861e8a1faeSThomas Huth     fd = prepare_iso(iso_size, &tx, &iso);
15871e8a1faeSThomas Huth     opts.opaque = tx;
15881e8a1faeSThomas Huth 
15891e8a1faeSThomas Huth     /* Standard startup wonkery, but use ide-cd and our special iso file */
15901e8a1faeSThomas Huth     ahci = ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,format=raw "
15911e8a1faeSThomas Huth                                 "-M q35 "
15921e8a1faeSThomas Huth                                 "-device ide-cd,drive=drive0 ", iso);
15931e8a1faeSThomas Huth 
15941e8a1faeSThomas Huth     /* Build & Send AHCI command */
15951e8a1faeSThomas Huth     ahci_exec(ahci, ahci_port_select(ahci), cmd, &opts);
15961e8a1faeSThomas Huth 
15971e8a1faeSThomas Huth     /* Cleanup */
15981e8a1faeSThomas Huth     g_free(tx);
15991e8a1faeSThomas Huth     ahci_shutdown(ahci);
16001e8a1faeSThomas Huth     remove_iso(fd, iso);
16011e8a1faeSThomas Huth }
16021e8a1faeSThomas Huth 
ahci_test_cdrom_read10(int nsectors,bool dma)16031e8a1faeSThomas Huth static void ahci_test_cdrom_read10(int nsectors, bool dma)
16041e8a1faeSThomas Huth {
16051e8a1faeSThomas Huth     ahci_test_cdrom(nsectors, dma, CMD_ATAPI_READ_10, false, 0);
16061e8a1faeSThomas Huth }
16071e8a1faeSThomas Huth 
test_cdrom_dma(void)16081e8a1faeSThomas Huth static void test_cdrom_dma(void)
16091e8a1faeSThomas Huth {
16101e8a1faeSThomas Huth     ahci_test_cdrom_read10(1, true);
16111e8a1faeSThomas Huth }
16121e8a1faeSThomas Huth 
test_cdrom_dma_multi(void)16131e8a1faeSThomas Huth static void test_cdrom_dma_multi(void)
16141e8a1faeSThomas Huth {
16151e8a1faeSThomas Huth     ahci_test_cdrom_read10(3, true);
16161e8a1faeSThomas Huth }
16171e8a1faeSThomas Huth 
test_cdrom_pio(void)16181e8a1faeSThomas Huth static void test_cdrom_pio(void)
16191e8a1faeSThomas Huth {
16201e8a1faeSThomas Huth     ahci_test_cdrom_read10(1, false);
16211e8a1faeSThomas Huth }
16221e8a1faeSThomas Huth 
test_cdrom_pio_multi(void)16231e8a1faeSThomas Huth static void test_cdrom_pio_multi(void)
16241e8a1faeSThomas Huth {
16251e8a1faeSThomas Huth     ahci_test_cdrom_read10(3, false);
16261e8a1faeSThomas Huth }
16271e8a1faeSThomas Huth 
16281e8a1faeSThomas Huth /* Regression test: Test that a READ_CD command with a BCL of 0 but a size of 0
16291e8a1faeSThomas Huth  * completes as a NOP instead of erroring out. */
test_atapi_bcl(void)16301e8a1faeSThomas Huth static void test_atapi_bcl(void)
16311e8a1faeSThomas Huth {
16321e8a1faeSThomas Huth     ahci_test_cdrom(0, false, CMD_ATAPI_READ_CD, true, 0);
16331e8a1faeSThomas Huth }
16341e8a1faeSThomas Huth 
16351e8a1faeSThomas Huth 
atapi_wait_tray(AHCIQState * ahci,bool open)16361e8a1faeSThomas Huth static void atapi_wait_tray(AHCIQState *ahci, bool open)
16371e8a1faeSThomas Huth {
16381e8a1faeSThomas Huth     QDict *rsp = qtest_qmp_eventwait_ref(ahci->parent->qts,
16391e8a1faeSThomas Huth                                          "DEVICE_TRAY_MOVED");
16401e8a1faeSThomas Huth     QDict *data = qdict_get_qdict(rsp, "data");
16411e8a1faeSThomas Huth     if (open) {
16421e8a1faeSThomas Huth         g_assert(qdict_get_bool(data, "tray-open"));
16431e8a1faeSThomas Huth     } else {
16441e8a1faeSThomas Huth         g_assert(!qdict_get_bool(data, "tray-open"));
16451e8a1faeSThomas Huth     }
16461e8a1faeSThomas Huth     qobject_unref(rsp);
16471e8a1faeSThomas Huth }
16481e8a1faeSThomas Huth 
test_atapi_tray(void)16491e8a1faeSThomas Huth static void test_atapi_tray(void)
16501e8a1faeSThomas Huth {
16511e8a1faeSThomas Huth     AHCIQState *ahci;
16521e8a1faeSThomas Huth     unsigned char *tx;
16531e8a1faeSThomas Huth     char *iso;
16541e8a1faeSThomas Huth     int fd;
16551e8a1faeSThomas Huth     uint8_t port, sense, asc;
16561e8a1faeSThomas Huth     uint64_t iso_size = ATAPI_SECTOR_SIZE;
16571e8a1faeSThomas Huth     QDict *rsp;
16581e8a1faeSThomas Huth 
16591e8a1faeSThomas Huth     fd = prepare_iso(iso_size, &tx, &iso);
16601e8a1faeSThomas Huth     ahci = ahci_boot_and_enable("-blockdev node-name=drive0,driver=file,filename=%s "
16611e8a1faeSThomas Huth                                 "-M q35 "
16621e8a1faeSThomas Huth                                 "-device ide-cd,id=cd0,drive=drive0 ", iso);
16631e8a1faeSThomas Huth     port = ahci_port_select(ahci);
16641e8a1faeSThomas Huth 
16651e8a1faeSThomas Huth     ahci_atapi_eject(ahci, port);
16661e8a1faeSThomas Huth     atapi_wait_tray(ahci, true);
16671e8a1faeSThomas Huth 
16681e8a1faeSThomas Huth     ahci_atapi_load(ahci, port);
16691e8a1faeSThomas Huth     atapi_wait_tray(ahci, false);
16701e8a1faeSThomas Huth 
16711e8a1faeSThomas Huth     /* Remove media */
16721e8a1faeSThomas Huth     qtest_qmp_send(ahci->parent->qts, "{'execute': 'blockdev-open-tray', "
16731e8a1faeSThomas Huth                     "'arguments': {'id': 'cd0'}}");
16741e8a1faeSThomas Huth     atapi_wait_tray(ahci, true);
16751e8a1faeSThomas Huth     rsp = qtest_qmp_receive(ahci->parent->qts);
16761e8a1faeSThomas Huth     qobject_unref(rsp);
16771e8a1faeSThomas Huth 
1678855436dbSDaniel P. Berrangé     qtest_qmp_assert_success(ahci->parent->qts,
16791e8a1faeSThomas Huth                              "{'execute': 'blockdev-remove-medium', "
16801e8a1faeSThomas Huth                              "'arguments': {'id': 'cd0'}}");
16811e8a1faeSThomas Huth 
16821e8a1faeSThomas Huth     /* Test the tray without a medium */
16831e8a1faeSThomas Huth     ahci_atapi_load(ahci, port);
16841e8a1faeSThomas Huth     atapi_wait_tray(ahci, false);
16851e8a1faeSThomas Huth 
16861e8a1faeSThomas Huth     ahci_atapi_eject(ahci, port);
16871e8a1faeSThomas Huth     atapi_wait_tray(ahci, true);
16881e8a1faeSThomas Huth 
16891e8a1faeSThomas Huth     /* Re-insert media */
1690855436dbSDaniel P. Berrangé     qtest_qmp_assert_success(
1691855436dbSDaniel P. Berrangé         ahci->parent->qts,
16921e8a1faeSThomas Huth         "{'execute': 'blockdev-add', "
16931e8a1faeSThomas Huth         "'arguments': {'node-name': 'node0', "
16941e8a1faeSThomas Huth                       "'driver': 'raw', "
16951e8a1faeSThomas Huth                       "'file': { 'driver': 'file', "
16961e8a1faeSThomas Huth                                 "'filename': %s }}}", iso);
1697855436dbSDaniel P. Berrangé     qtest_qmp_assert_success(
1698855436dbSDaniel P. Berrangé         ahci->parent->qts,
16991e8a1faeSThomas Huth         "{'execute': 'blockdev-insert-medium',"
17001e8a1faeSThomas Huth         "'arguments': { 'id': 'cd0', "
17011e8a1faeSThomas Huth                        "'node-name': 'node0' }}");
17021e8a1faeSThomas Huth 
17031e8a1faeSThomas Huth     /* Again, the event shows up first */
17041e8a1faeSThomas Huth     qtest_qmp_send(ahci->parent->qts, "{'execute': 'blockdev-close-tray', "
17051e8a1faeSThomas Huth                    "'arguments': {'id': 'cd0'}}");
17061e8a1faeSThomas Huth     atapi_wait_tray(ahci, false);
17071e8a1faeSThomas Huth     rsp = qtest_qmp_receive(ahci->parent->qts);
17081e8a1faeSThomas Huth     qobject_unref(rsp);
17091e8a1faeSThomas Huth 
17101e8a1faeSThomas Huth     /* Now, to convince ATAPI we understand the media has changed... */
17111e8a1faeSThomas Huth     ahci_atapi_test_ready(ahci, port, false, SENSE_NOT_READY);
17121e8a1faeSThomas Huth     ahci_atapi_get_sense(ahci, port, &sense, &asc);
17131e8a1faeSThomas Huth     g_assert_cmpuint(sense, ==, SENSE_NOT_READY);
17141e8a1faeSThomas Huth     g_assert_cmpuint(asc, ==, ASC_MEDIUM_NOT_PRESENT);
17151e8a1faeSThomas Huth 
17161e8a1faeSThomas Huth     ahci_atapi_test_ready(ahci, port, false, SENSE_UNIT_ATTENTION);
17171e8a1faeSThomas Huth     ahci_atapi_get_sense(ahci, port, &sense, &asc);
17181e8a1faeSThomas Huth     g_assert_cmpuint(sense, ==, SENSE_UNIT_ATTENTION);
17191e8a1faeSThomas Huth     g_assert_cmpuint(asc, ==, ASC_MEDIUM_MAY_HAVE_CHANGED);
17201e8a1faeSThomas Huth 
17211e8a1faeSThomas Huth     ahci_atapi_test_ready(ahci, port, true, SENSE_NO_SENSE);
17221e8a1faeSThomas Huth     ahci_atapi_get_sense(ahci, port, &sense, &asc);
17231e8a1faeSThomas Huth     g_assert_cmpuint(sense, ==, SENSE_NO_SENSE);
17241e8a1faeSThomas Huth 
17251e8a1faeSThomas Huth     /* Final tray test. */
17261e8a1faeSThomas Huth     ahci_atapi_eject(ahci, port);
17271e8a1faeSThomas Huth     atapi_wait_tray(ahci, true);
17281e8a1faeSThomas Huth 
17291e8a1faeSThomas Huth     ahci_atapi_load(ahci, port);
17301e8a1faeSThomas Huth     atapi_wait_tray(ahci, false);
17311e8a1faeSThomas Huth 
17321e8a1faeSThomas Huth     /* Cleanup */
17331e8a1faeSThomas Huth     g_free(tx);
17341e8a1faeSThomas Huth     ahci_shutdown(ahci);
17351e8a1faeSThomas Huth     remove_iso(fd, iso);
17361e8a1faeSThomas Huth }
17371e8a1faeSThomas Huth 
17381e8a1faeSThomas Huth /******************************************************************************/
17391e8a1faeSThomas Huth /* AHCI I/O Test Matrix Definitions                                           */
17401e8a1faeSThomas Huth 
17411e8a1faeSThomas Huth enum BuffLen {
17421e8a1faeSThomas Huth     LEN_BEGIN = 0,
17431e8a1faeSThomas Huth     LEN_SIMPLE = LEN_BEGIN,
17441e8a1faeSThomas Huth     LEN_DOUBLE,
17451e8a1faeSThomas Huth     LEN_LONG,
17461e8a1faeSThomas Huth     LEN_SHORT,
17471e8a1faeSThomas Huth     NUM_LENGTHS
17481e8a1faeSThomas Huth };
17491e8a1faeSThomas Huth 
17501e8a1faeSThomas Huth static const char *buff_len_str[NUM_LENGTHS] = { "simple", "double",
17511e8a1faeSThomas Huth                                                  "long", "short" };
17521e8a1faeSThomas Huth 
17531e8a1faeSThomas Huth enum AddrMode {
17541e8a1faeSThomas Huth     ADDR_MODE_BEGIN = 0,
17551e8a1faeSThomas Huth     ADDR_MODE_LBA28 = ADDR_MODE_BEGIN,
17561e8a1faeSThomas Huth     ADDR_MODE_LBA48,
17571e8a1faeSThomas Huth     NUM_ADDR_MODES
17581e8a1faeSThomas Huth };
17591e8a1faeSThomas Huth 
17601e8a1faeSThomas Huth static const char *addr_mode_str[NUM_ADDR_MODES] = { "lba28", "lba48" };
17611e8a1faeSThomas Huth 
17621e8a1faeSThomas Huth enum IOMode {
17631e8a1faeSThomas Huth     MODE_BEGIN = 0,
17641e8a1faeSThomas Huth     MODE_PIO = MODE_BEGIN,
17651e8a1faeSThomas Huth     MODE_DMA,
17661e8a1faeSThomas Huth     NUM_MODES
17671e8a1faeSThomas Huth };
17681e8a1faeSThomas Huth 
17691e8a1faeSThomas Huth static const char *io_mode_str[NUM_MODES] = { "pio", "dma" };
17701e8a1faeSThomas Huth 
17711e8a1faeSThomas Huth enum IOOps {
17721e8a1faeSThomas Huth     IO_BEGIN = 0,
17731e8a1faeSThomas Huth     IO_READ = IO_BEGIN,
17741e8a1faeSThomas Huth     IO_WRITE,
17751e8a1faeSThomas Huth     NUM_IO_OPS
17761e8a1faeSThomas Huth };
17771e8a1faeSThomas Huth 
17781e8a1faeSThomas Huth enum OffsetType {
17791e8a1faeSThomas Huth     OFFSET_BEGIN = 0,
17801e8a1faeSThomas Huth     OFFSET_ZERO = OFFSET_BEGIN,
17811e8a1faeSThomas Huth     OFFSET_LOW,
17821e8a1faeSThomas Huth     OFFSET_HIGH,
17831e8a1faeSThomas Huth     NUM_OFFSETS
17841e8a1faeSThomas Huth };
17851e8a1faeSThomas Huth 
17861e8a1faeSThomas Huth static const char *offset_str[NUM_OFFSETS] = { "zero", "low", "high" };
17871e8a1faeSThomas Huth 
17881e8a1faeSThomas Huth typedef struct AHCIIOTestOptions {
17891e8a1faeSThomas Huth     enum BuffLen length;
17901e8a1faeSThomas Huth     enum AddrMode address_type;
17911e8a1faeSThomas Huth     enum IOMode io_type;
17921e8a1faeSThomas Huth     enum OffsetType offset;
17931e8a1faeSThomas Huth } AHCIIOTestOptions;
17941e8a1faeSThomas Huth 
offset_sector(enum OffsetType ofst,enum AddrMode addr_type,uint64_t buffsize)17951e8a1faeSThomas Huth static uint64_t offset_sector(enum OffsetType ofst,
17961e8a1faeSThomas Huth                               enum AddrMode addr_type,
17971e8a1faeSThomas Huth                               uint64_t buffsize)
17981e8a1faeSThomas Huth {
17991e8a1faeSThomas Huth     uint64_t ceil;
18001e8a1faeSThomas Huth     uint64_t nsectors;
18011e8a1faeSThomas Huth 
18021e8a1faeSThomas Huth     switch (ofst) {
18031e8a1faeSThomas Huth     case OFFSET_ZERO:
18041e8a1faeSThomas Huth         return 0;
18051e8a1faeSThomas Huth     case OFFSET_LOW:
18061e8a1faeSThomas Huth         return 1;
18071e8a1faeSThomas Huth     case OFFSET_HIGH:
18081e8a1faeSThomas Huth         ceil = (addr_type == ADDR_MODE_LBA28) ? 0xfffffff : 0xffffffffffff;
18091e8a1faeSThomas Huth         ceil = MIN(ceil, mb_to_sectors(test_image_size_mb) - 1);
18101e8a1faeSThomas Huth         nsectors = buffsize / AHCI_SECTOR_SIZE;
18111e8a1faeSThomas Huth         return ceil - nsectors + 1;
18121e8a1faeSThomas Huth     default:
18131e8a1faeSThomas Huth         g_assert_not_reached();
18141e8a1faeSThomas Huth     }
18151e8a1faeSThomas Huth }
18161e8a1faeSThomas Huth 
18171e8a1faeSThomas Huth /**
18181e8a1faeSThomas Huth  * Table of possible I/O ATA commands given a set of enumerations.
18191e8a1faeSThomas Huth  */
18201e8a1faeSThomas Huth static const uint8_t io_cmds[NUM_MODES][NUM_ADDR_MODES][NUM_IO_OPS] = {
18211e8a1faeSThomas Huth     [MODE_PIO] = {
18221e8a1faeSThomas Huth         [ADDR_MODE_LBA28] = {
18231e8a1faeSThomas Huth             [IO_READ] = CMD_READ_PIO,
18241e8a1faeSThomas Huth             [IO_WRITE] = CMD_WRITE_PIO },
18251e8a1faeSThomas Huth         [ADDR_MODE_LBA48] = {
18261e8a1faeSThomas Huth             [IO_READ] = CMD_READ_PIO_EXT,
18271e8a1faeSThomas Huth             [IO_WRITE] = CMD_WRITE_PIO_EXT }
18281e8a1faeSThomas Huth     },
18291e8a1faeSThomas Huth     [MODE_DMA] = {
18301e8a1faeSThomas Huth         [ADDR_MODE_LBA28] = {
18311e8a1faeSThomas Huth             [IO_READ] = CMD_READ_DMA,
18321e8a1faeSThomas Huth             [IO_WRITE] = CMD_WRITE_DMA },
18331e8a1faeSThomas Huth         [ADDR_MODE_LBA48] = {
18341e8a1faeSThomas Huth             [IO_READ] = CMD_READ_DMA_EXT,
18351e8a1faeSThomas Huth             [IO_WRITE] = CMD_WRITE_DMA_EXT }
18361e8a1faeSThomas Huth     }
18371e8a1faeSThomas Huth };
18381e8a1faeSThomas Huth 
18391e8a1faeSThomas Huth /**
18401e8a1faeSThomas Huth  * Test a Read/Write pattern using various commands, addressing modes,
18411e8a1faeSThomas Huth  * transfer modes, and buffer sizes.
18421e8a1faeSThomas Huth  */
test_io_rw_interface(enum AddrMode lba48,enum IOMode dma,unsigned bufsize,uint64_t sector)18431e8a1faeSThomas Huth static void test_io_rw_interface(enum AddrMode lba48, enum IOMode dma,
18441e8a1faeSThomas Huth                                  unsigned bufsize, uint64_t sector)
18451e8a1faeSThomas Huth {
18461e8a1faeSThomas Huth     AHCIQState *ahci;
18471e8a1faeSThomas Huth 
18481e8a1faeSThomas Huth     ahci = ahci_boot_and_enable(NULL);
18491e8a1faeSThomas Huth     ahci_test_io_rw_simple(ahci, bufsize, sector,
18501e8a1faeSThomas Huth                            io_cmds[dma][lba48][IO_READ],
18511e8a1faeSThomas Huth                            io_cmds[dma][lba48][IO_WRITE]);
18521e8a1faeSThomas Huth     ahci_shutdown(ahci);
18531e8a1faeSThomas Huth }
18541e8a1faeSThomas Huth 
18551e8a1faeSThomas Huth /**
18561e8a1faeSThomas Huth  * Demultiplex the test data and invoke the actual test routine.
18571e8a1faeSThomas Huth  */
test_io_interface(gconstpointer opaque)18581e8a1faeSThomas Huth static void test_io_interface(gconstpointer opaque)
18591e8a1faeSThomas Huth {
18601e8a1faeSThomas Huth     AHCIIOTestOptions *opts = (AHCIIOTestOptions *)opaque;
18611e8a1faeSThomas Huth     unsigned bufsize;
18621e8a1faeSThomas Huth     uint64_t sector;
18631e8a1faeSThomas Huth 
18641e8a1faeSThomas Huth     switch (opts->length) {
18651e8a1faeSThomas Huth     case LEN_SIMPLE:
18661e8a1faeSThomas Huth         bufsize = 4096;
18671e8a1faeSThomas Huth         break;
18681e8a1faeSThomas Huth     case LEN_DOUBLE:
18691e8a1faeSThomas Huth         bufsize = 8192;
18701e8a1faeSThomas Huth         break;
18711e8a1faeSThomas Huth     case LEN_LONG:
18721e8a1faeSThomas Huth         bufsize = 4096 * 64;
18731e8a1faeSThomas Huth         break;
18741e8a1faeSThomas Huth     case LEN_SHORT:
18751e8a1faeSThomas Huth         bufsize = 512;
18761e8a1faeSThomas Huth         break;
18771e8a1faeSThomas Huth     default:
18781e8a1faeSThomas Huth         g_assert_not_reached();
18791e8a1faeSThomas Huth     }
18801e8a1faeSThomas Huth 
18811e8a1faeSThomas Huth     sector = offset_sector(opts->offset, opts->address_type, bufsize);
18821e8a1faeSThomas Huth     test_io_rw_interface(opts->address_type, opts->io_type, bufsize, sector);
18831e8a1faeSThomas Huth     g_free(opts);
18841e8a1faeSThomas Huth     return;
18851e8a1faeSThomas Huth }
18861e8a1faeSThomas Huth 
create_ahci_io_test(enum IOMode type,enum AddrMode addr,enum BuffLen len,enum OffsetType offset)18871e8a1faeSThomas Huth static void create_ahci_io_test(enum IOMode type, enum AddrMode addr,
18881e8a1faeSThomas Huth                                 enum BuffLen len, enum OffsetType offset)
18891e8a1faeSThomas Huth {
18901e8a1faeSThomas Huth     char *name;
18911e8a1faeSThomas Huth     AHCIIOTestOptions *opts;
18921e8a1faeSThomas Huth 
18931e8a1faeSThomas Huth     opts = g_new(AHCIIOTestOptions, 1);
18941e8a1faeSThomas Huth     opts->length = len;
18951e8a1faeSThomas Huth     opts->address_type = addr;
18961e8a1faeSThomas Huth     opts->io_type = type;
18971e8a1faeSThomas Huth     opts->offset = offset;
18981e8a1faeSThomas Huth 
18991e8a1faeSThomas Huth     name = g_strdup_printf("ahci/io/%s/%s/%s/%s",
19001e8a1faeSThomas Huth                            io_mode_str[type],
19011e8a1faeSThomas Huth                            addr_mode_str[addr],
19021e8a1faeSThomas Huth                            buff_len_str[len],
19031e8a1faeSThomas Huth                            offset_str[offset]);
19041e8a1faeSThomas Huth 
19051e8a1faeSThomas Huth     if ((addr == ADDR_MODE_LBA48) && (offset == OFFSET_HIGH) &&
19061e8a1faeSThomas Huth         (mb_to_sectors(test_image_size_mb) <= 0xFFFFFFF)) {
19071e8a1faeSThomas Huth         g_test_message("%s: skipped; test image too small", name);
19081e8a1faeSThomas Huth         g_free(opts);
19091e8a1faeSThomas Huth         g_free(name);
19101e8a1faeSThomas Huth         return;
19111e8a1faeSThomas Huth     }
19121e8a1faeSThomas Huth 
19131e8a1faeSThomas Huth     qtest_add_data_func(name, opts, test_io_interface);
19141e8a1faeSThomas Huth     g_free(name);
19151e8a1faeSThomas Huth }
19161e8a1faeSThomas Huth 
19171e8a1faeSThomas Huth /******************************************************************************/
19181e8a1faeSThomas Huth 
main(int argc,char ** argv)19191e8a1faeSThomas Huth int main(int argc, char **argv)
19201e8a1faeSThomas Huth {
1921be181f87SBin Meng     const char *arch, *base;
19221e8a1faeSThomas Huth     int ret;
19231e8a1faeSThomas Huth     int fd;
19241e8a1faeSThomas Huth     int c;
19251e8a1faeSThomas Huth     int i, j, k, m;
19261e8a1faeSThomas Huth 
19271e8a1faeSThomas Huth     static struct option long_options[] = {
19281e8a1faeSThomas Huth         {"pedantic", no_argument, 0, 'p' },
19291e8a1faeSThomas Huth         {0, 0, 0, 0},
19301e8a1faeSThomas Huth     };
19311e8a1faeSThomas Huth 
19321e8a1faeSThomas Huth     /* Should be first to utilize g_test functionality, So we can see errors. */
19331e8a1faeSThomas Huth     g_test_init(&argc, &argv, NULL);
19341e8a1faeSThomas Huth 
19351e8a1faeSThomas Huth     while (1) {
19361e8a1faeSThomas Huth         c = getopt_long(argc, argv, "", long_options, NULL);
19371e8a1faeSThomas Huth         if (c == -1) {
19381e8a1faeSThomas Huth             break;
19391e8a1faeSThomas Huth         }
19401e8a1faeSThomas Huth         switch (c) {
19411e8a1faeSThomas Huth         case -1:
19421e8a1faeSThomas Huth             break;
19431e8a1faeSThomas Huth         case 'p':
19441e8a1faeSThomas Huth             ahci_pedantic = 1;
19451e8a1faeSThomas Huth             break;
19461e8a1faeSThomas Huth         default:
19471e8a1faeSThomas Huth             fprintf(stderr, "Unrecognized ahci_test option.\n");
19481e8a1faeSThomas Huth             g_assert_not_reached();
19491e8a1faeSThomas Huth         }
19501e8a1faeSThomas Huth     }
19511e8a1faeSThomas Huth 
19521e8a1faeSThomas Huth     /* Check architecture */
19531e8a1faeSThomas Huth     arch = qtest_get_arch();
19541e8a1faeSThomas Huth     if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) {
19551e8a1faeSThomas Huth         g_test_message("Skipping test for non-x86");
19561e8a1faeSThomas Huth         return 0;
19571e8a1faeSThomas Huth     }
19581e8a1faeSThomas Huth 
1959be181f87SBin Meng     /*
1960be181f87SBin Meng      * "base" stores the starting point where we create temporary files.
1961be181f87SBin Meng      *
1962be181f87SBin Meng      * On Windows, this is set to the relative path of current working
1963be181f87SBin Meng      * directory, because the absolute path causes the blkdebug filename
1964be181f87SBin Meng      * parser fail to parse "blkdebug:path/to/config:path/to/image".
1965be181f87SBin Meng      */
1966be181f87SBin Meng #ifndef _WIN32
1967be181f87SBin Meng     base = g_get_tmp_dir();
1968be181f87SBin Meng #else
1969be181f87SBin Meng     base = ".";
1970be181f87SBin Meng #endif
1971be181f87SBin Meng 
19721e8a1faeSThomas Huth     /* Create a temporary image */
1973be181f87SBin Meng     tmp_path = g_strdup_printf("%s/qtest.XXXXXX", base);
1974be181f87SBin Meng     fd = g_mkstemp(tmp_path);
19751e8a1faeSThomas Huth     g_assert(fd >= 0);
19761e8a1faeSThomas Huth     if (have_qemu_img()) {
19771e8a1faeSThomas Huth         imgfmt = "qcow2";
19781e8a1faeSThomas Huth         test_image_size_mb = TEST_IMAGE_SIZE_MB_LARGE;
19791e8a1faeSThomas Huth         mkqcow2(tmp_path, TEST_IMAGE_SIZE_MB_LARGE);
19801e8a1faeSThomas Huth     } else {
19811e8a1faeSThomas Huth         g_test_message("QTEST_QEMU_IMG not set or qemu-img missing; "
19821e8a1faeSThomas Huth                        "skipping LBA48 high-sector tests");
19831e8a1faeSThomas Huth         imgfmt = "raw";
19841e8a1faeSThomas Huth         test_image_size_mb = TEST_IMAGE_SIZE_MB_SMALL;
19851e8a1faeSThomas Huth         ret = ftruncate(fd, test_image_size_mb * 1024 * 1024);
19861e8a1faeSThomas Huth         g_assert(ret == 0);
19871e8a1faeSThomas Huth     }
19881e8a1faeSThomas Huth     close(fd);
19891e8a1faeSThomas Huth 
19901e8a1faeSThomas Huth     /* Create temporary blkdebug instructions */
1991be181f87SBin Meng     debug_path = g_strdup_printf("%s/qtest-blkdebug.XXXXXX", base);
1992be181f87SBin Meng     fd = g_mkstemp(debug_path);
19931e8a1faeSThomas Huth     g_assert(fd >= 0);
19941e8a1faeSThomas Huth     close(fd);
19951e8a1faeSThomas Huth 
19961e8a1faeSThomas Huth     /* Reserve a hollow file to use as a socket for migration tests */
1997d9eefd35SBin Meng     fd = g_file_open_tmp("qtest-migration.XXXXXX", &mig_socket, NULL);
19981e8a1faeSThomas Huth     g_assert(fd >= 0);
19991e8a1faeSThomas Huth     close(fd);
20001e8a1faeSThomas Huth 
20011e8a1faeSThomas Huth     /* Run the tests */
20021e8a1faeSThomas Huth     qtest_add_func("/ahci/sanity",     test_sanity);
20031e8a1faeSThomas Huth     qtest_add_func("/ahci/pci_spec",   test_pci_spec);
20041e8a1faeSThomas Huth     qtest_add_func("/ahci/pci_enable", test_pci_enable);
20051e8a1faeSThomas Huth     qtest_add_func("/ahci/hba_spec",   test_hba_spec);
20061e8a1faeSThomas Huth     qtest_add_func("/ahci/hba_enable", test_hba_enable);
20071e8a1faeSThomas Huth     qtest_add_func("/ahci/identify",   test_identify);
20081e8a1faeSThomas Huth 
20091e8a1faeSThomas Huth     for (i = MODE_BEGIN; i < NUM_MODES; i++) {
20101e8a1faeSThomas Huth         for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
20111e8a1faeSThomas Huth             for (k = LEN_BEGIN; k < NUM_LENGTHS; k++) {
20121e8a1faeSThomas Huth                 for (m = OFFSET_BEGIN; m < NUM_OFFSETS; m++) {
20131e8a1faeSThomas Huth                     create_ahci_io_test(i, j, k, m);
20141e8a1faeSThomas Huth                 }
20151e8a1faeSThomas Huth             }
20161e8a1faeSThomas Huth         }
20171e8a1faeSThomas Huth     }
20181e8a1faeSThomas Huth 
20191e8a1faeSThomas Huth     qtest_add_func("/ahci/io/dma/lba28/fragmented", test_dma_fragmented);
20201e8a1faeSThomas Huth 
20211e8a1faeSThomas Huth     qtest_add_func("/ahci/flush/simple", test_flush);
20221e8a1faeSThomas Huth     qtest_add_func("/ahci/flush/retry", test_flush_retry);
20231e8a1faeSThomas Huth     qtest_add_func("/ahci/flush/migrate", test_flush_migrate);
20241e8a1faeSThomas Huth 
20251e8a1faeSThomas Huth     qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity);
20261e8a1faeSThomas Huth     qtest_add_func("/ahci/migrate/dma/simple", test_migrate_dma);
20271e8a1faeSThomas Huth     qtest_add_func("/ahci/io/dma/lba28/retry", test_halted_dma);
20281e8a1faeSThomas Huth     qtest_add_func("/ahci/migrate/dma/halted", test_migrate_halted_dma);
20291e8a1faeSThomas Huth 
20301e8a1faeSThomas Huth     qtest_add_func("/ahci/max", test_max);
2031*cc610857SFiona Ebner     qtest_add_func("/ahci/reset/simple", test_reset);
2032*cc610857SFiona Ebner     qtest_add_func("/ahci/reset/pending_callback", test_reset_pending_callback);
20331e8a1faeSThomas Huth 
20341e8a1faeSThomas Huth     qtest_add_func("/ahci/io/ncq/simple", test_ncq_simple);
20351e8a1faeSThomas Huth     qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq);
20361e8a1faeSThomas Huth     qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq);
20371e8a1faeSThomas Huth     qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq);
20381e8a1faeSThomas Huth 
20391e8a1faeSThomas Huth     qtest_add_func("/ahci/cdrom/dma/single", test_cdrom_dma);
20401e8a1faeSThomas Huth     qtest_add_func("/ahci/cdrom/dma/multi", test_cdrom_dma_multi);
20411e8a1faeSThomas Huth     qtest_add_func("/ahci/cdrom/pio/single", test_cdrom_pio);
20421e8a1faeSThomas Huth     qtest_add_func("/ahci/cdrom/pio/multi", test_cdrom_pio_multi);
20431e8a1faeSThomas Huth 
20441e8a1faeSThomas Huth     qtest_add_func("/ahci/cdrom/pio/bcl", test_atapi_bcl);
20451e8a1faeSThomas Huth     qtest_add_func("/ahci/cdrom/eject", test_atapi_tray);
20461e8a1faeSThomas Huth 
20471e8a1faeSThomas Huth     ret = g_test_run();
20481e8a1faeSThomas Huth 
20491e8a1faeSThomas Huth     /* Cleanup */
20501e8a1faeSThomas Huth     unlink(tmp_path);
2051d9eefd35SBin Meng     g_free(tmp_path);
20521e8a1faeSThomas Huth     unlink(debug_path);
2053d9eefd35SBin Meng     g_free(debug_path);
20541e8a1faeSThomas Huth     unlink(mig_socket);
2055d9eefd35SBin Meng     g_free(mig_socket);
20561e8a1faeSThomas Huth 
20571e8a1faeSThomas Huth     return ret;
20581e8a1faeSThomas Huth }
2059