qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: John Snow <jsnow@redhat.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, John Snow <jsnow@redhat.com>,
	stefanha@redhat.com, mst@redhat.com
Subject: [Qemu-devel] [PATCH 28/28] ahci: Add test_identify case to ahci-test.
Date: Mon,  7 Jul 2014 14:18:09 -0400	[thread overview]
Message-ID: <1404757089-4836-29-git-send-email-jsnow@redhat.com> (raw)
In-Reply-To: <1404757089-4836-1-git-send-email-jsnow@redhat.com>

Utilizing all of the bring-up code in pci_enable and hba_enable,
this test issues a simple IDENTIFY command via the HBA and retrieves
the response via the PIO receive mechanisms of the HBA.

Signed-off-by: John Snow <jsnow@redhat.com>
---
 tests/ahci-test.c | 296 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 296 insertions(+)

diff --git a/tests/ahci-test.c b/tests/ahci-test.c
index f342e2c..a410b97 100644
--- a/tests/ahci-test.c
+++ b/tests/ahci-test.c
@@ -255,6 +255,96 @@
 #define AHCI_VERSION_1_2         (0x00010200)
 #define AHCI_VERSION_1_3         (0x00010300)
 
+/*** Structures ***/
+
+/**
+ * Generic FIS structure.
+ */
+struct fis {
+    uint8_t fis_type;
+    uint8_t flags;
+    char data[0];
+} __attribute__((__packed__));
+
+/**
+ * Register device-to-host FIS structure.
+ */
+struct reg_d2h_fis {
+    /* DW0 */
+    uint8_t fis_type;
+    uint8_t flags;
+    uint8_t status;
+    uint8_t error;
+    /* DW1 */
+    uint8_t lba_low;
+    uint8_t lba_mid;
+    uint8_t lba_high;
+    uint8_t device;
+    /* DW2 */
+    uint8_t lba3;
+    uint8_t lba4;
+    uint8_t lba5;
+    uint8_t res1;
+    /* DW3 */
+    uint16_t count;
+    uint8_t res2;
+    uint8_t res3;
+    /* DW4 */
+    uint16_t res4;
+    uint16_t res5;
+} __attribute__((__packed__));
+
+/**
+ * Register host-to-device FIS structure.
+ */
+struct reg_h2d_fis {
+    /* DW0 */
+    uint8_t fis_type;
+    uint8_t flags;
+    uint8_t command;
+    uint8_t feature_low;
+    /* DW1 */
+    uint8_t lba_low;
+    uint8_t lba_mid;
+    uint8_t lba_high;
+    uint8_t device;
+    /* DW2 */
+    uint8_t lba3;
+    uint8_t lba4;
+    uint8_t lba5;
+    uint8_t feature_high;
+    /* DW3 */
+    uint16_t count;
+    uint8_t icc;
+    uint8_t control;
+    /* DW4 */
+    uint32_t aux;
+} __attribute__((__packed__));
+
+/**
+ * Command List entry structure.
+ * The command list contains between 1-32 of these structures.
+ */
+struct ahci_command {
+    uint8_t b1;
+    uint8_t b2;
+    uint16_t prdtl; /* Phys Region Desc. Table Length */
+    uint32_t prdbc; /* Phys Region Desc. Byte Count */
+    uint32_t ctba;  /* Command Table Descriptor Base Address */
+    uint32_t ctbau; /*                                    '' Upper */
+    uint32_t res[4];
+} __attribute__((__packed__));
+
+/**
+ * Physical Region Descriptor; pointed to by the Command List Header,
+ * struct ahci_command.
+ */
+struct prd {
+    uint32_t dba;  /* Data Base Address */
+    uint32_t dbau; /* Data Base Address Upper */
+    uint32_t res;  /* Reserved */
+    uint32_t dbc;  /* Data Byte Count (0-indexed) & Interrupt Flag (bit 2^31) */
+};
 
 /* To help make it clear that the HBA is not a pointer to local memory. */
 typedef void HBA;
@@ -295,6 +385,10 @@ static char tmp_path[] = "/tmp/qtest.XXXXXX";
 #define px_clr(port, reg, mask)  px_wreg((port), (reg),                 \
                                          px_rreg((port), (reg)) & ~(mask));
 
+/* For calculating how big the PRD table needs to be: */
+#define cmd_tbl_siz(n) ((0x80 + ((n) * sizeof(struct prd)) + 0x7F) & ~0x7F)
+
+
 /*** Function Declarations ***/
 static QPCIDevice *get_ahci_device(void);
 static QPCIDevice *start_ahci_device(QPCIDevice *dev, HBA **hba_base);
@@ -310,6 +404,17 @@ static void ahci_test_pmcap(QPCIDevice *ahci, uint8_t offset);
 
 /*** Utilities ***/
 
+static void string_cpu_to_be16(uint16_t *s, size_t bytes)
+{
+    g_assert((bytes & 1) == 0);
+    bytes /= 2;
+
+    while (bytes--) {
+        *s = cpu_to_be16(*s);
+        s++;
+    }
+}
+
 /**
  * Locate, verify, and return a handle to the AHCI device.
  */
@@ -1173,6 +1278,180 @@ static void ahci_test_port_spec(QPCIDevice *ahci, HBA *hba_base,
     }
 }
 
+/**
+ * Utilizing an initialized AHCI HBA, issue an IDENTIFY command to the first
+ * device we see, then read and check the response.
+ */
+static void ahci_test_identify(QPCIDevice *ahci, HBA *hba_base)
+{
+    struct reg_d2h_fis *d2h = g_malloc0(0x20);
+    struct reg_d2h_fis *pio = g_malloc0(0x20);
+    struct reg_h2d_fis fis;
+    struct ahci_command cmd;
+    struct prd prd;
+    uint32_t ports, reg, clb, table, fb, data_ptr;
+    uint16_t buff[256];
+    unsigned i;
+    int rc;
+
+    g_assert(ahci != NULL);
+    g_assert(hba_base != NULL);
+
+    /* We need to:
+     * (1) Create a Command Table Buffer and update the Command List Slot #0
+     *     to point to this buffer.
+     * (2) Construct an FIS host-to-device command structure, and write it to
+     *     the top of the command table buffer.
+     * (3) Create a data buffer for the IDENTIFY response to be sent to
+     * (4) Create a Physical Region Descriptor that points to the data buffer,
+     *     and write it to the bottom (offset 0x80) of the command table.
+     * (5) Now, PxCLB points to the command list, command 0 points to
+     *     our table, and our table contains an FIS instruction and a
+     *     PRD that points to our rx buffer.
+     * (6) We inform the HBA via PxCI that there is a command ready in slot #0.
+     */
+
+    /* Pick the first implemented and running port */
+    ports = ahci_rreg(AHCI_PI);
+    for (i = 0; i < 32; ports >>= 1, ++i) {
+        if (ports == 0) {
+            i = 32;
+        }
+
+        if (!(ports & 0x01)) {
+            continue;
+        }
+
+        reg = px_rreg(i, AHCI_PX_CMD);
+        if (bitset(reg, AHCI_PX_CMD_ST)) {
+            break;
+        }
+    }
+    g_assert(i < 32);
+    g_test_message("Selected port %u for test", i);
+
+    /* Clear out this port's interrupts (ignore the init register d2h fis) */
+    reg = px_rreg(i, AHCI_PX_IS);
+    px_wreg(i, AHCI_PX_IS, reg);
+    g_assert_cmphex(px_rreg(i, AHCI_PX_IS), ==, 0);
+
+    /* Wipe the FIS-Recieve Buffer */
+    fb = px_rreg(i, AHCI_PX_FB);
+    g_assert_cmphex(fb, !=, 0);
+    qmemset(fb, 0x00, 0x100);
+
+    /* Create a Command Table buffer. 0x80 is the smallest with a PRDTL of 0. */
+    /* We need at least one PRD, so round up to the nearest 0x80 multiple.    */
+    table = guest_alloc(guest_malloc, cmd_tbl_siz(1));
+    g_assert(table);
+    assert_bit_clear(table, 0x7F);
+
+    /* Create a data buffer ... where we will dump the IDENTIFY data to. */
+    data_ptr = guest_alloc(guest_malloc, 512);
+    g_assert(data_ptr);
+
+    /* Grab the Command List Buffer pointer */
+    clb = px_rreg(i, AHCI_PX_CLB);
+    g_assert(clb != 0);
+
+    /* Copy the existing Command #0 structure from the CLB into local memory,
+     * and build a new command #0. */
+    memread(clb, &cmd, sizeof(cmd));
+    cmd.b1 = 5;    /* reg_h2d_fis is 5 double-words long */
+    cmd.b2 = 0x04; /* clear PxTFD.STS.BSY when done */
+    cmd.prdtl = 1; /* One PRD table entry. */
+    cmd.prdbc = 0;
+    cmd.ctba = table;
+    cmd.ctbau = 0;
+
+    /* Construct our PRD, noting that DBC is 0-indexed. */
+    prd.dba = data_ptr;
+    prd.dbau = 0;
+    prd.res = 0;
+    prd.dbc = 511;
+    prd.dbc |= 0x80000000; /* Request DPS Interrupt */
+
+    /* Construct our Command FIS, Based on http://wiki.osdev.org/AHCI */
+    memset(&fis, 0x00, sizeof(fis));
+    fis.fis_type = 0x27; /* Register Host-to-Device FIS */
+    fis.command = 0xEC;  /* IDENTIFY */
+    fis.device = 0;
+    fis.flags = 0x80;    /* Indicate this is a command FIS */
+
+    /* We've committed nothing yet, no interrupts should be posted yet. */
+    g_assert_cmphex(px_rreg(i, AHCI_PX_IS), ==, 0);
+
+    /* Commit the Command FIS to the Command Table */
+    memwrite(table, &fis, sizeof(fis));
+
+    /* Commit the PRD entry to the Command Table */
+    memwrite(table + 0x80, &prd, sizeof(prd));
+
+    /* Commit Command #0, pointing to the Table, to the Command List Buffer. */
+    memwrite(clb, &cmd, sizeof(cmd));
+
+    /* Everything is in place, but we haven't given the go-ahead yet. */
+    g_assert_cmphex(px_rreg(i, AHCI_PX_IS), ==, 0);
+
+    /* Issue Command #0 via PxCI */
+    px_wreg(i, AHCI_PX_CI, (1 << 0));
+    while (bitset(px_rreg(i, AHCI_PX_TFD), AHCI_PX_TFD_STS_BSY)) {
+        usleep(50);
+    }
+
+    /* Check for expected interrupts */
+    reg = px_rreg(i, AHCI_PX_IS);
+    assert_bit_set(reg, AHCI_PX_IS_DHRS);
+    assert_bit_set(reg, AHCI_PX_IS_PSS);
+    if (bitclr(reg, AHCI_PX_IS_DPS)) {
+        g_test_message("WARN: Expected to see DPS bit set in PxIS");
+    }
+    /* Clear expected interrupts and assert all interrupts now cleared. */
+    px_wreg(i, AHCI_PX_IS, AHCI_PX_IS_DHRS | AHCI_PX_IS_PSS | AHCI_PX_IS_DPS);
+    g_assert_cmphex(px_rreg(i, AHCI_PX_IS), ==, 0);
+
+    /* Check for errors. */
+    reg = px_rreg(i, AHCI_PX_SERR);
+    g_assert_cmphex(reg, ==, 0);
+    reg = px_rreg(i, AHCI_PX_TFD);
+    assert_bit_clear(reg, AHCI_PX_TFD_STS_ERR);
+    assert_bit_clear(reg, AHCI_PX_TFD_ERR);
+
+    /* Investigate CMD #0, assert that we read 512 bytes */
+    memread(clb, &cmd, sizeof(cmd));
+    g_assert_cmphex(512, ==, cmd.prdbc);
+
+    /* Investigate FIS responses */
+    memread(fb + 0x20, pio, 0x20);
+    memread(fb + 0x40, d2h, 0x20);
+    g_assert_cmphex(pio->fis_type, ==, 0x5f);
+    g_assert_cmphex(d2h->fis_type, ==, 0x34);
+    g_assert_cmphex(pio->flags, ==, d2h->flags);
+    g_assert_cmphex(pio->status, ==, d2h->status);
+    g_assert_cmphex(pio->error, ==, d2h->error);
+
+    reg = px_rreg(i, AHCI_PX_TFD);
+    g_assert_cmphex((reg & AHCI_PX_TFD_ERR), ==, pio->error);
+    g_assert_cmphex((reg & AHCI_PX_TFD_STS), ==, pio->status);
+    /* PIO FIS contains a "bytes read" field, it should match up. */
+    g_assert_cmphex(pio->res4, ==, cmd.prdbc);
+
+    /* Last, but not least: Investigate the IDENTIFY response data. */
+    memread(data_ptr, &buff, 512);
+
+    /* Check serial number/version in the buffer */
+    string_cpu_to_be16(&buff[10], 20);
+    rc = memcmp(&buff[10], "testdisk            ", 20);
+    g_assert(rc == 0);
+
+    string_cpu_to_be16(&buff[23], 8);
+    rc = memcmp(&buff[23], "version ", 8);
+    g_assert(rc == 0);
+
+    free(d2h);
+    free(pio);
+}
+
 /******************************************************************************/
 /* Test Interfaces                                                            */
 /******************************************************************************/
@@ -1242,6 +1521,22 @@ static void test_hba_enable(void)
     ahci_shutdown(ahci);
 }
 
+/**
+ * Bring up the device and issue an IDENTIFY command.
+ * Inspect the state of the HBA device and the data returned.
+ */
+static void test_identify(void)
+{
+    QPCIDevice *ahci;
+    HBA *hba_base;
+
+    ahci_boot(&ahci);
+    ahci_pci_enable(ahci, &hba_base);
+    ahci_hba_enable(ahci, hba_base);
+    ahci_test_identify(ahci, hba_base);
+    ahci_shutdown(ahci);
+}
+
 /******************************************************************************/
 
 int main(int argc, char **argv)
@@ -1273,6 +1568,7 @@ int main(int argc, char **argv)
     qtest_add_func("/ahci/pci_enable", test_pci_enable);
     qtest_add_func("/ahci/hba_spec",   test_hba_spec);
     qtest_add_func("/ahci/hba_enable", test_hba_enable);
+    qtest_add_func("/ahci/identify",   test_identify);
 
     ret = g_test_run();
 
-- 
1.9.3

  parent reply	other threads:[~2014-07-07 18:19 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-07 18:17 [Qemu-devel] [PATCH 00/28] ahci refactoring to support ahci-test suite John Snow
2014-07-07 18:17 ` [Qemu-devel] [PATCH 01/28] blkdebug: report errors on flush too John Snow
2014-07-17 13:28   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 02/28] libqtest: add QTEST_LOG for debugging qtest testcases John Snow
2014-07-17 13:32   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 03/28] ide-test: add test for werror=stop John Snow
2014-07-31 10:58   ` Stefan Hajnoczi
2014-07-31 22:06     ` John Snow
2014-08-01  7:13       ` Markus Armbruster
2014-07-07 18:17 ` [Qemu-devel] [PATCH 04/28] ide: stash aiocb for flushes John Snow
2014-07-31 11:53   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 05/28] ide: simplify reset callbacks John Snow
2014-07-31 11:54   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 06/28] ide: simplify set_inactive callbacks John Snow
2014-07-31 11:54   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 07/28] ide: simplify async_cmd_done callbacks John Snow
2014-07-31 11:54   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 08/28] ide: simplify start_transfer callbacks John Snow
2014-07-31 11:55   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 09/28] ide: wrap start_dma callback John Snow
2014-07-31 11:55   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 10/28] ide: remove wrong setting of BM_STATUS_INT John Snow
2014-07-31 11:56   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 11/28] ide: fold add_status callback into set_inactive John Snow
2014-07-31 11:57   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 12/28] ide: move BM_STATUS bits to pci.[ch] John Snow
2014-07-31 11:57   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 13/28] ide: move retry constants out of BM_STATUS_* namespace John Snow
2014-07-31 12:06   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 14/28] ahci: remove duplicate PORT_IRQ_* constants John Snow
2014-07-31 12:12   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 15/28] ide: stop PIO transfer on errors John Snow
2014-07-31 12:23   ` Stefan Hajnoczi
2014-07-31 23:32     ` John Snow
2014-08-01  7:15     ` Paolo Bonzini
2014-07-07 18:17 ` [Qemu-devel] [PATCH 16/28] ide: make all commands go through cmd_done John Snow
2014-07-31 12:25   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 17/28] ahci: construct PIO Setup FIS for PIO commands John Snow
2014-07-31 12:32   ` Stefan Hajnoczi
2014-07-07 18:17 ` [Qemu-devel] [PATCH 18/28] q35: Enable the ioapic device to be seen by qtest John Snow
2014-07-31 12:33   ` Stefan Hajnoczi
2014-07-07 18:18 ` [Qemu-devel] [PATCH 19/28] qtest: Adding qtest_memset and qmemset John Snow
2014-07-31 12:37   ` Stefan Hajnoczi
2014-07-07 18:18 ` [Qemu-devel] [PATCH 20/28] libqos: Correct memory leak John Snow
2014-07-31 12:38   ` Stefan Hajnoczi
2014-07-07 18:18 ` [Qemu-devel] [PATCH 21/28] libqtest: Correct small " John Snow
2014-07-31 12:39   ` Stefan Hajnoczi
2014-07-07 18:18 ` [Qemu-devel] [PATCH 22/28] libqos: Fixes a " John Snow
2014-07-31 12:40   ` Stefan Hajnoczi
2014-07-07 18:18 ` [Qemu-devel] [PATCH 23/28] ahci: Adding basic functionality qtest John Snow
2014-07-31 12:54   ` Stefan Hajnoczi
2014-07-07 18:18 ` [Qemu-devel] [PATCH 24/28] ahci: Add test_pci_spec to ahci-test John Snow
2014-07-31 13:19   ` Stefan Hajnoczi
2014-07-31 17:42     ` John Snow
2014-08-01 11:14       ` Stefan Hajnoczi
2014-07-07 18:18 ` [Qemu-devel] [PATCH 25/28] ahci: add test_pci_enable " John Snow
2014-07-31 13:36   ` Stefan Hajnoczi
2014-07-07 18:18 ` [Qemu-devel] [PATCH 26/28] ahci: Add test_hba_spec " John Snow
2014-07-31 14:01   ` Stefan Hajnoczi
2014-07-31 20:03     ` John Snow
2014-08-01 23:27     ` John Snow
2014-08-04  9:51       ` Stefan Hajnoczi
2014-07-07 18:18 ` [Qemu-devel] [PATCH 27/28] ahci: Add test_hba_enable " John Snow
2014-07-31 15:24   ` Stefan Hajnoczi
2014-07-07 18:18 ` John Snow [this message]
2014-07-31 15:28   ` [Qemu-devel] [PATCH 28/28] ahci: Add test_identify case " Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1404757089-4836-29-git-send-email-jsnow@redhat.com \
    --to=jsnow@redhat.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).