Skip to contentSkip to conversation

qtest/ahci: add dma test

PATCH Superseded John Snow 2 participants 35 messages Jan 30, 2015 – Feb 3, 2015

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

John Snow

author
#0 lore raw

cover qtest/ahci: add dma test

Add a simple DMA r/w test to ahci-test.

Oh, and for the first 18 patches, refactor everything into helpers so that each ahci_test isn't a thousand lines long.

This patch depends upon the "ahci test preliminary refactoring" series upstream, which shuffled a lot of libqos and malloc facilities to support this series.

This patchset is a necessary step in checking in AHCI/DMA migration tests that I will later use as proof as suitability of enabling the ICH9 and AHCI migration flags.

~John

John Snow (19):
libqos/ahci: Add ahci_port_select helper libqos/ahci: Add ahci_port_clear helper qtest/ahci: rename 'Command' to 'CommandHeader' libqos/ahci: Add command header helpers libqos/ahci: Add ahci_port_check_error helper libqos/ahci: Add ahci_port_check_interrupts helper libqos/ahci: Add port_check_nonbusy helper libqos/ahci: Add cmd response sanity check helpers qtest/ahci: Demagic ahci tests. libqos/ahci: Add ide cmd properties libqos/ahci: add ahci command functions libqos/ahci: add ahci command verify libqos/ahci: add ahci command size setters libqos/ahci: Add ahci_guest_io libqos/ahci: add ahci_io
libqos/ahci: Add ahci_clean_mem qtest/ahci: Add a macro bootup routine qtest/ahci: Assert sector size in identify test qtest/ahci: Adding simple dma read-write test

 tests/ahci-test.c     | 246 +++++++++-------------
 tests/libqos/ahci.c   | 554 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/ahci.h   | 163 ++++++++++++---
 tests/libqos/malloc.c |   5 +
 tests/libqos/malloc.h |   1 +
 5 files changed, 798 insertions(+), 171 deletions(-)
Signature · 1 line
1.9.3
author
#1 lore raw

PATCH 1/19 libqos/ahci: Add ahci_port_select helper

This helper identifies which port of the
AHCI HBA has a device we may run tests on.

---
 tests/ahci-test.c   | 19 ++-----------------
 tests/libqos/ahci.c | 27 +++++++++++++++++++++++++++
 tests/libqos/ahci.h |  1 +
 3 files changed, 30 insertions(+), 17 deletions(-)
tests/ahci-test.c +2 -17
index fca33d2..c689b62 100644
@@ -662,7 +662,7 @@ static void ahci_test_identify(AHCIQState *ahci)
     RegH2DFIS fis;
     AHCICommand cmd;
     PRD prd;
-    uint32_t ports, reg, table, data_ptr;
+    uint32_t reg, table, data_ptr;
     uint16_t buff[256];
     unsigned i;
     int rc;
@@ -684,22 +684,7 @@ static void ahci_test_identify(AHCIQState *ahci)
      */
 
     /* Pick the first implemented and running port */
-    ports = ahci_rreg(ahci, AHCI_PI);
-    for (i = 0; i < 32; ports >>= 1, ++i) {
-        if (ports == 0) {
-            i = 32;
-        }
-
-        if (!(ports & 0x01)) {
-            continue;
-        }
-
-        reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
-        if (BITSET(reg, AHCI_PX_CMD_ST)) {
-            break;
-        }
-    }
-    g_assert_cmphex(i, <, 32);
+    i = ahci_port_select(ahci);
     g_test_message("Selected port %u for test", i);
 
     /* Clear out this port's interrupts (ignore the init register d2h fis) */
tests/libqos/ahci.c +27 -0
index 3056fb1..8874790 100644
@@ -267,3 +267,30 @@ void ahci_hba_enable(AHCIQState *ahci)
      * In the future, a small test-case to inspect the Register D2H FIS
      * and clear the initial interrupts might be good. */
 }
+
+/**
+ * Pick the first implemented and running port
+ */
+unsigned ahci_port_select(AHCIQState *ahci)
+{
+    uint32_t ports, reg;
+    unsigned i;
+
+    ports = ahci_rreg(ahci, AHCI_PI);
+    for (i = 0; i < 32; ports >>= 1, ++i) {
+        if (ports == 0) {
+            i = 32;
+        }
+
+        if (!(ports & 0x01)) {
+            continue;
+        }
+
+        reg = ahci_px_rreg(ahci, i, AHCI_PX_CMD);
+        if (BITSET(reg, AHCI_PX_CMD_ST)) {
+            break;
+        }
+    }
+    g_assert(i < 32);
+    return i;
+}
tests/libqos/ahci.h +1 -0
index 77f2055..b3992e1 100644
@@ -431,5 +431,6 @@ void free_ahci_device(QPCIDevice *dev);
 void ahci_pci_enable(AHCIQState *ahci);
 void start_ahci_device(AHCIQState *ahci);
 void ahci_hba_enable(AHCIQState *ahci);
+unsigned ahci_port_select(AHCIQState *ahci);
 
 #endif
Signature · 1 line
1.9.3
author
#2 lore raw

PATCH 2/19 libqos/ahci: Add ahci_port_clear helper

In reply to John Snow

Add a helper that assists in clearing out potentially old error and FIS information from an AHCI port's data structures. This ensures we always start with a blank slate for interrupt and FIS receipt information.

---
 tests/ahci-test.c   |  9 ++-------
 tests/libqos/ahci.c | 16 ++++++++++++++++
 tests/libqos/ahci.h |  1 +
 3 files changed, 19 insertions(+), 7 deletions(-)
tests/ahci-test.c +2 -7
index c689b62..90647f2 100644
@@ -687,13 +687,8 @@ static void ahci_test_identify(AHCIQState *ahci)
     i = ahci_port_select(ahci);
     g_test_message("Selected port %u for test", i);
 
-    /* Clear out this port's interrupts (ignore the init register d2h fis) */
-    reg = ahci_px_rreg(ahci, i, AHCI_PX_IS);
-    ahci_px_wreg(ahci, i, AHCI_PX_IS, reg);
-    g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
-
-    /* Wipe the FIS-Receive Buffer */
-    qmemset(ahci->port[i].fb, 0x00, 0x100);
+    /* Clear out the FIS Receive area and any pending interrupts. */
+    ahci_port_clear(ahci, i);
 
     /* 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.    */
tests/libqos/ahci.c +16 -0
index 8874790..9195bd6 100644
@@ -294,3 +294,19 @@ unsigned ahci_port_select(AHCIQState *ahci)
     g_assert(i < 32);
     return i;
 }
+
+/**
+ * Clear a port's interrupts and status information prior to a test.
+ */
+void ahci_port_clear(AHCIQState *ahci, uint8_t px)
+{
+    uint32_t reg;
+
+    /* Clear out this port's interrupts (ignore the init register d2h fis) */
+    reg = ahci_px_rreg(ahci, px, AHCI_PX_IS);
+    ahci_px_wreg(ahci, px, AHCI_PX_IS, reg);
+    g_assert_cmphex(ahci_px_rreg(ahci, px, AHCI_PX_IS), ==, 0);
+
+    /* Wipe the FIS-Recieve Buffer */
+    qmemset(ahci->port[px].fb, 0x00, 0x100);
+}
tests/libqos/ahci.h +1 -0
index b3992e1..49ddee5 100644
@@ -432,5 +432,6 @@ void ahci_pci_enable(AHCIQState *ahci);
 void start_ahci_device(AHCIQState *ahci);
 void ahci_hba_enable(AHCIQState *ahci);
 unsigned ahci_port_select(AHCIQState *ahci);
+void ahci_port_clear(AHCIQState *ahci, uint8_t px);
 
 #endif
Signature · 1 line
1.9.3
author
#3 lore raw

PATCH 3/19 qtest/ahci: rename 'Command' to 'CommandHeader'

In reply to John Snow

The structure name is a bit of a misnomer; the structure currently named command is actually the commandheader. A future patch in this series will add an actual "Command" structure, so we'll rename it now before the rest of the functions in this series try to use it.

In addition, rename the "b1" and "b2" fields
to be a unified uint16_t named "flags."

---
 tests/ahci-test.c   | 6 +++---
 tests/libqos/ahci.h | 7 +++----
 2 files changed, 6 insertions(+), 7 deletions(-)
tests/ahci-test.c +3 -3
index 90647f2..85e5761 100644
@@ -660,7 +660,7 @@ static void ahci_test_identify(AHCIQState *ahci)
     RegD2HFIS *d2h = g_malloc0(0x20);
     RegD2HFIS *pio = g_malloc0(0x20);
     RegH2DFIS fis;
-    AHCICommand cmd;
+    AHCICommandHeader cmd;
     PRD prd;
     uint32_t reg, table, data_ptr;
     uint16_t buff[256];
@@ -703,8 +703,8 @@ static void ahci_test_identify(AHCIQState *ahci)
     /* Copy the existing Command #0 structure from the CLB into local memory,
      * and build a new command #0. */
     memread(ahci->port[i].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.flags = 5;    /* reg_h2d_fis is 5 double-words long */
+    cmd.flags |= 0x400; /* clear PxTFD.STS.BSY when done */
     cmd.prdtl = cpu_to_le16(1); /* One PRD table entry. */
     cmd.prdbc = 0;
     cmd.ctba = cpu_to_le32(table);
tests/libqos/ahci.h +3 -4
index 49ddee5..e3ada6e 100644
@@ -329,15 +329,14 @@ typedef struct RegH2DFIS {
  * Command List entry structure.
  * The command list contains between 1-32 of these structures.
  */
-typedef struct AHCICommand {
-    uint8_t b1;
-    uint8_t b2;
+typedef struct AHCICommandHeader {
+    uint16_t flags; /* Cmd-Fis-Len, PMP#, and flags. */
     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__)) AHCICommand;
+} __attribute__((__packed__)) AHCICommandHeader;
 
 /**
  * Physical Region Descriptor; pointed to by the Command List Header,
Signature · 1 line
1.9.3
#4 lore raw

Re: [Qemu-devel] [PATCH 03/19] qtest/ahci: rename 'Command' to 'CommandHeader'

tests/ahci-test.c static void ahci_test_identify(AHCIQState *ahci) · line 707

Quoted 8 lines · John Snow
tests/ahci-test.c +2 -2
@@ -703,8 +703,8 @@ static void ahci_test_identify(AHCIQState *ahci)
     /* Copy the existing Command #0 structure from the CLB into local memory,
      * and build a new command #0. */
     memread(ahci->port[i].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.flags = 5;    /* reg_h2d_fis is 5 double-words long */
+    cmd.flags |= 0x400; /* clear PxTFD.STS.BSY when done */

Missing cpu_to_le16, I think.

Paolo

author
#5 lore raw

PATCH 4/19 libqos/ahci: Add command header helpers

In reply to John Snow

Adds command header helper functions:
-ahci_command_header_set
-ahci_command_header_get,
-ahci_command_destroy, and
-ahci_cmd_pick

These helpers help to quickly manage the command header information in the AHCI device.

ahci_command_header_set and get will store or retrieve an AHCI command header, respectively.

ahci_cmd_pick chooses the first available but least recently used command slot to allow us to cycle through the available command slots.

ahci_command_destroy obliterates all information contained within a given slot's command header, and frees its associated command table, but not its DMA buffer!

Lastly, the command table pointer fields (dba and dbau) are merged into a single 64bit value to make managing 64bit tests simpler.

---
 tests/ahci-test.c   | 43 ++++++++++++++++---------------
 tests/libqos/ahci.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/ahci.h | 17 ++++++++----
 3 files changed, 109 insertions(+), 25 deletions(-)
tests/ahci-test.c +23 -20
index 85e5761..17aee37 100644
@@ -662,10 +662,12 @@ static void ahci_test_identify(AHCIQState *ahci)
     RegH2DFIS fis;
     AHCICommandHeader cmd;
     PRD prd;
-    uint32_t reg, table, data_ptr;
+    uint32_t reg, data_ptr;
     uint16_t buff[256];
     unsigned i;
     int rc;
+    uint8_t cx;
+    uint64_t table;
 
     g_assert(ahci != NULL);
 
@@ -700,19 +702,19 @@ static void ahci_test_identify(AHCIQState *ahci)
     data_ptr = ahci_alloc(ahci, 512);
     g_assert(data_ptr);
 
-    /* Copy the existing Command #0 structure from the CLB into local memory,
-     * and build a new command #0. */
-    memread(ahci->port[i].clb, &cmd, sizeof(cmd));
-    cmd.flags = 5;    /* reg_h2d_fis is 5 double-words long */
-    cmd.flags |= 0x400; /* clear PxTFD.STS.BSY when done */
-    cmd.prdtl = cpu_to_le16(1); /* One PRD table entry. */
+    /* pick a command slot (should be 0!) */
+    cx = ahci_pick_cmd(ahci, i);
+
+    /* Construct our Command Header (set_command_header handles endianness.) */
+    memset(&cmd, 0x00, sizeof(cmd));
+    cmd.flags = 5;     /* reg_h2d_fis is 5 double-words long */
+    cmd.flags = 0x400; /* clear PxTFD.STS.BSY when done */
+    cmd.prdtl = 1;     /* One PRD table entry. */
     cmd.prdbc = 0;
-    cmd.ctba = cpu_to_le32(table);
-    cmd.ctbau = 0;
+    cmd.ctba = table;
 
     /* Construct our PRD, noting that DBC is 0-indexed. */
-    prd.dba = cpu_to_le32(data_ptr);
-    prd.dbau = 0;
+    prd.dba = cpu_to_le64(data_ptr);
     prd.res = 0;
     /* 511+1 bytes, request DPS interrupt */
     prd.dbc = cpu_to_le32(511 | 0x80000000);
@@ -733,14 +735,15 @@ static void ahci_test_identify(AHCIQState *ahci)
     /* 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(ahci->port[i].clb, &cmd, sizeof(cmd));
+    /* Commit Command #cx, pointing to the Table, to the Command List Buffer. */
+    ahci_set_command_header(ahci, i, cx, &cmd);
 
-    /* Everything is in place, but we haven't given the go-ahead yet. */
+    /* Everything is in place, but we haven't given the go-ahead yet,
+     * so we should find that there are no pending interrupts yet. */
     g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
 
-    /* Issue Command #0 via PxCI */
-    ahci_px_wreg(ahci, i, AHCI_PX_CI, (1 << 0));
+    /* Issue Command #cx via PxCI */
+    ahci_px_wreg(ahci, i, AHCI_PX_CI, (1 << cx));
     while (BITSET(ahci_px_rreg(ahci, i, AHCI_PX_TFD), AHCI_PX_TFD_STS_BSY)) {
         usleep(50);
     }
@@ -764,9 +767,9 @@ static void ahci_test_identify(AHCIQState *ahci)
     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(ahci->port[i].clb, &cmd, sizeof(cmd));
-    g_assert_cmphex(512, ==, le32_to_cpu(cmd.prdbc));
+    /* Investigate the CMD, assert that we read 512 bytes */
+    ahci_get_command_header(ahci, i, cx, &cmd);
+    g_assert_cmphex(512, ==, cmd.prdbc);
 
     /* Investigate FIS responses */
     memread(ahci->port[i].fb + 0x20, pio, 0x20);
@@ -783,7 +786,7 @@ static void ahci_test_identify(AHCIQState *ahci)
     /* The PIO Setup FIS contains a "bytes read" field, which is a
      * 16-bit value. The Physical Region Descriptor Byte Count is
      * 32-bit, but for small transfers using one PRD, it should match. */
-    g_assert_cmphex(le16_to_cpu(pio->res4), ==, le32_to_cpu(cmd.prdbc));
+    g_assert_cmphex(le16_to_cpu(pio->res4), ==, cmd.prdbc);
 
     /* Last, but not least: Investigate the IDENTIFY response data. */
     memread(data_ptr, &buff, 512);
tests/libqos/ahci.c +74 -0

77 lines not on this page · raw message

tests/libqos/ahci.h +12 -5
index e3ada6e..d27e75f 100644
@@ -248,6 +248,9 @@
 typedef struct AHCIPortQState {
     uint64_t fb;
     uint64_t clb;
+    uint64_t ctba[32];
+    uint16_t prdtl[32];
+    uint8_t next; /** Next Command Slot to Use **/
 } AHCIPortQState;
 
 typedef struct AHCIQState {
@@ -333,8 +336,7 @@ typedef struct AHCICommandHeader {
     uint16_t flags; /* Cmd-Fis-Len, PMP#, and flags. */
     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 */
+    uint64_t ctba;  /* Command Table Descriptor Base Address */
     uint32_t res[4];
 } __attribute__((__packed__)) AHCICommandHeader;
 
@@ -343,11 +345,10 @@ typedef struct AHCICommandHeader {
  * struct ahci_command.
  */
 typedef struct PRD {
-    uint32_t dba;  /* Data Base Address */
-    uint32_t dbau; /* Data Base Address Upper */
+    uint64_t dba;  /* Data Base Address */
     uint32_t res;  /* Reserved */
     uint32_t dbc;  /* Data Byte Count (0-indexed) & Interrupt Flag (bit 2^31) */
-} PRD;
+} __attribute__((__packed__)) PRD;
 
 /*** Macro Utilities ***/
 #define BITANY(data, mask) (((data) & (mask)) != 0)
@@ -432,5 +433,11 @@ void start_ahci_device(AHCIQState *ahci);
 void ahci_hba_enable(AHCIQState *ahci);
 unsigned ahci_port_select(AHCIQState *ahci);
 void ahci_port_clear(AHCIQState *ahci, uint8_t px);
+void ahci_get_command_header(AHCIQState *ahci, uint8_t px,
+                             uint8_t cx, AHCICommandHeader *cmd);
+void ahci_set_command_header(AHCIQState *ahci, uint8_t px,
+                             uint8_t cx, AHCICommandHeader *cmd);
+void ahci_destroy_command(AHCIQState *ahci, uint8_t px, uint8_t cx);
+unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t px);
 
 #endif
Signature · 1 line
1.9.3
#6 lore raw

Re: [Qemu-devel] [PATCH 04/19] libqos/ahci: Add command header helpers

tests/ahci-test.c static void ahci_test_identify(AHCIQState *ahci) · line 711

Quoted 4 lines · John Snow
tests/ahci-test.c +4 -0
+    /* Construct our Command Header (set_command_header handles endianness.) */
+    memset(&cmd, 0x00, sizeof(cmd));
+    cmd.flags = 5;     /* reg_h2d_fis is 5 double-words long */
+    cmd.flags = 0x400; /* clear PxTFD.STS.BSY when done */

And here |= became = ?

Paolo

Quoted 1 line

+ cmd.prdtl = 1; /* One PRD table entry. */

author
#7 lore raw

Re: [Qemu-devel] [PATCH 04/19] libqos/ahci: Add command header helpers

Quoted 14 lines · Paolo Bonzini

tests/ahci-test.c static void ahci_test_identify(AHCIQState *ahci) · line 711

Quoted 4 lines · John Snow
tests/ahci-test.c +4 -0
+    /* Construct our Command Header (set_command_header handles endianness.) */
+    memset(&cmd, 0x00, sizeof(cmd));
+    cmd.flags = 5;     /* reg_h2d_fis is 5 double-words long */
+    cmd.flags = 0x400; /* clear PxTFD.STS.BSY when done */

And here |= became = ?

Paolo

Quoted 1 line

+ cmd.prdtl = 1; /* One PRD table entry. */

A typo that testing didn't pick up on. (I wonder how?) Thank you.

#8 lore raw

Re: [Qemu-devel] [PATCH 04/19] libqos/ahci: Add command header helpers

In reply to John Snow

tests/libqos/ahci.c void ahci_port_clear(AHCIQState *ahci, uint8_t px) · line 338

Quoted 11 lines · John Snow
tests/libqos/ahci.c +11 -0
+/* Set the #cx'th command of port #px. */
+void ahci_set_command_header(AHCIQState *ahci, uint8_t px,
+                             uint8_t cx, AHCICommandHeader *cmd)
+{
+    uint64_t ba = ahci->port[px].clb;
+    ba += cx * sizeof(AHCICommandHeader);
+
+    cmd->flags = cpu_to_le16(cmd->flags);
+    cmd->prdtl = cpu_to_le16(cmd->prdtl);
+    cmd->prdbc = cpu_to_le32(cmd->prdbc);
+    cmd->ctba = cpu_to_le64(cmd->ctba);

Modifying cmd is uglyish, and especially it might hide bugs that only happen on big endian system. Please copy to a local AHCICommandHeader variable before adjusting for host endianness.

Paolo

Quoted 2 lines
(quoted diff) +2 -0
+    memwrite(ba, cmd, sizeof(AHCICommandHeader));
+}
author
#9 lore raw

Re: [Qemu-devel] [PATCH 04/19] libqos/ahci: Add command header helpers

Quoted 19 lines · Paolo Bonzini

tests/libqos/ahci.c void ahci_port_clear(AHCIQState *ahci, uint8_t px) · line 338

Quoted 11 lines · John Snow
tests/libqos/ahci.c +11 -0
+/* Set the #cx'th command of port #px. */
+void ahci_set_command_header(AHCIQState *ahci, uint8_t px,
+                             uint8_t cx, AHCICommandHeader *cmd)
+{
+    uint64_t ba = ahci->port[px].clb;
+    ba += cx * sizeof(AHCICommandHeader);
+
+    cmd->flags = cpu_to_le16(cmd->flags);
+    cmd->prdtl = cpu_to_le16(cmd->prdtl);
+    cmd->prdbc = cpu_to_le32(cmd->prdbc);
+    cmd->ctba = cpu_to_le64(cmd->ctba);

Modifying cmd is uglyish, and especially it might hide bugs that only happen on big endian system. Please copy to a local AHCICommandHeader variable before adjusting for host endianness.

Quoted 5 lines

Paolo

Quoted 2 lines
(quoted diff) +2 -0
+    memwrite(ba, cmd, sizeof(AHCICommandHeader));
+}

You're right. I had just never actually thought to reference the command after committing it before, so I hadn't considered it in that light.

If I intend to share goodies with other tests it would be nice if they didn't do secretly evil things :)

author
#10 lore raw

PATCH 5/19 libqos/ahci: Add ahci_port_check_error helper

In reply to John Snow

ahci_port_check_error checks a given port's error registers and asserts that everything from the port-level view is still OK.

---
 tests/ahci-test.c   |  8 +-------
 tests/libqos/ahci.c | 22 ++++++++++++++++++++++
 tests/libqos/ahci.h |  1 +
 3 files changed, 24 insertions(+), 7 deletions(-)
tests/ahci-test.c +1 -7
index 17aee37..63ecf73 100644
@@ -747,6 +747,7 @@ static void ahci_test_identify(AHCIQState *ahci)
     while (BITSET(ahci_px_rreg(ahci, i, AHCI_PX_TFD), AHCI_PX_TFD_STS_BSY)) {
         usleep(50);
     }
+    ahci_port_check_error(ahci, i);
 
     /* Check for expected interrupts */
     reg = ahci_px_rreg(ahci, i, AHCI_PX_IS);
@@ -760,13 +761,6 @@ static void ahci_test_identify(AHCIQState *ahci)
                  AHCI_PX_IS_DHRS | AHCI_PX_IS_PSS | AHCI_PX_IS_DPS);
     g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
 
-    /* Check for errors. */
-    reg = ahci_px_rreg(ahci, i, AHCI_PX_SERR);
-    g_assert_cmphex(reg, ==, 0);
-    reg = ahci_px_rreg(ahci, i, AHCI_PX_TFD);
-    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_ERR);
-    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
-
     /* Investigate the CMD, assert that we read 512 bytes */
     ahci_get_command_header(ahci, i, cx, &cmd);
     g_assert_cmphex(512, ==, cmd.prdbc);
tests/libqos/ahci.c +22 -0
index f5e8864..35de9af 100644
@@ -311,6 +311,28 @@ void ahci_port_clear(AHCIQState *ahci, uint8_t px)
     qmemset(ahci->port[px].fb, 0x00, 0x100);
 }
 
+/**
+ * Check a port for errors.
+ */
+void ahci_port_check_error(AHCIQState *ahci, uint8_t px)
+{
+    uint32_t reg;
+
+    /* The upper 9 bits of the IS register all indicate errors. */
+    reg = ahci_px_rreg(ahci, px, AHCI_PX_IS);
+    reg >>= 23;
+    g_assert_cmphex(reg, ==, 0);
+
+    /* The Sata Error Register should be empty. */
+    reg = ahci_px_rreg(ahci, px, AHCI_PX_SERR);
+    g_assert_cmphex(reg, ==, 0);
+
+    /* The TFD also has two error sections. */
+    reg = ahci_px_rreg(ahci, px, AHCI_PX_TFD);
+    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_ERR);
+    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
+}
+
 /* Get the #cx'th command of port #px. */
 void ahci_get_command_header(AHCIQState *ahci, uint8_t px,
                              uint8_t cx, AHCICommandHeader *cmd)
tests/libqos/ahci.h +1 -0
index d27e75f..b384cbc 100644
@@ -433,6 +433,7 @@ void start_ahci_device(AHCIQState *ahci);
 void ahci_hba_enable(AHCIQState *ahci);
 unsigned ahci_port_select(AHCIQState *ahci);
 void ahci_port_clear(AHCIQState *ahci, uint8_t px);
+void ahci_port_check_error(AHCIQState *ahci, uint8_t px);
 void ahci_get_command_header(AHCIQState *ahci, uint8_t px,
                              uint8_t cx, AHCICommandHeader *cmd);
 void ahci_set_command_header(AHCIQState *ahci, uint8_t px,
Signature · 1 line
1.9.3
author
#11 lore raw

PATCH 6/19 libqos/ahci: Add ahci_port_check_interrupts helper

In reply to John Snow

A helper that compares a given port's current interrupts and checks them against a supplied list of expected interrupt bits, and throws an error if they do not match.

The helper then resets the requested interrupts on this port, and asserts that the interrupt register is now empty.

---
 tests/ahci-test.c   | 13 ++-----------
 tests/libqos/ahci.c | 14 ++++++++++++++
 tests/libqos/ahci.h |  2 ++
 3 files changed, 18 insertions(+), 11 deletions(-)
tests/ahci-test.c +2 -11
index 63ecf73..e744f44 100644
@@ -747,19 +747,10 @@ static void ahci_test_identify(AHCIQState *ahci)
     while (BITSET(ahci_px_rreg(ahci, i, AHCI_PX_TFD), AHCI_PX_TFD_STS_BSY)) {
         usleep(50);
     }
+    /* Check registers for post-command consistency */
     ahci_port_check_error(ahci, i);
-
-    /* Check for expected interrupts */
-    reg = ahci_px_rreg(ahci, i, AHCI_PX_IS);
-    ASSERT_BIT_SET(reg, AHCI_PX_IS_DHRS);
-    ASSERT_BIT_SET(reg, AHCI_PX_IS_PSS);
     /* BUG: we expect AHCI_PX_IS_DPS to be set. */
-    ASSERT_BIT_CLEAR(reg, AHCI_PX_IS_DPS);
-
-    /* Clear expected interrupts and assert all interrupts now cleared. */
-    ahci_px_wreg(ahci, i, AHCI_PX_IS,
-                 AHCI_PX_IS_DHRS | AHCI_PX_IS_PSS | AHCI_PX_IS_DPS);
-    g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
+    ahci_port_check_interrupts(ahci, i, AHCI_PX_IS_DHRS | AHCI_PX_IS_PSS);
 
     /* Investigate the CMD, assert that we read 512 bytes */
     ahci_get_command_header(ahci, i, cx, &cmd);
tests/libqos/ahci.c +14 -0
index 35de9af..3318a54 100644
@@ -333,6 +333,20 @@ void ahci_port_check_error(AHCIQState *ahci, uint8_t px)
     ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_ERR);
 }
 
+void ahci_port_check_interrupts(AHCIQState *ahci, uint8_t px,
+                                uint32_t intr_mask)
+{
+    uint32_t reg;
+
+    /* Check for expected interrupts */
+    reg = ahci_px_rreg(ahci, px, AHCI_PX_IS);
+    ASSERT_BIT_SET(reg, intr_mask);
+
+    /* Clear expected interrupts and assert all interrupts now cleared. */
+    ahci_px_wreg(ahci, px, AHCI_PX_IS, intr_mask);
+    g_assert_cmphex(ahci_px_rreg(ahci, px, AHCI_PX_IS), ==, 0);
+}
+
 /* Get the #cx'th command of port #px. */
 void ahci_get_command_header(AHCIQState *ahci, uint8_t px,
                              uint8_t cx, AHCICommandHeader *cmd)
tests/libqos/ahci.h +2 -0
index b384cbc..4b6696a 100644
@@ -434,6 +434,8 @@ void ahci_hba_enable(AHCIQState *ahci);
 unsigned ahci_port_select(AHCIQState *ahci);
 void ahci_port_clear(AHCIQState *ahci, uint8_t px);
 void ahci_port_check_error(AHCIQState *ahci, uint8_t px);
+void ahci_port_check_interrupts(AHCIQState *ahci, uint8_t px,
+                                uint32_t intr_mask);
 void ahci_get_command_header(AHCIQState *ahci, uint8_t px,
                              uint8_t cx, AHCICommandHeader *cmd);
 void ahci_set_command_header(AHCIQState *ahci, uint8_t px,
Signature · 1 line
1.9.3
author
#12 lore raw

PATCH 7/19 libqos/ahci: Add port_check_nonbusy helper

In reply to John Snow

A simple helper that asserts a given port is not busy processing any commands via the TFD, Command Issue and SACT registers.

---
 tests/ahci-test.c   |  1 +
 tests/libqos/ahci.c | 18 ++++++++++++++++++
 tests/libqos/ahci.h |  1 +
 3 files changed, 20 insertions(+)
tests/ahci-test.c +1 -0
index e744f44..77d0f97 100644
@@ -751,6 +751,7 @@ static void ahci_test_identify(AHCIQState *ahci)
     ahci_port_check_error(ahci, i);
     /* BUG: we expect AHCI_PX_IS_DPS to be set. */
     ahci_port_check_interrupts(ahci, i, AHCI_PX_IS_DHRS | AHCI_PX_IS_PSS);
+    ahci_port_check_nonbusy(ahci, i, cx);
 
     /* Investigate the CMD, assert that we read 512 bytes */
     ahci_get_command_header(ahci, i, cx, &cmd);
tests/libqos/ahci.c +18 -0
index 3318a54..0016aad 100644
@@ -347,6 +347,24 @@ void ahci_port_check_interrupts(AHCIQState *ahci, uint8_t px,
     g_assert_cmphex(ahci_px_rreg(ahci, px, AHCI_PX_IS), ==, 0);
 }
 
+void ahci_port_check_nonbusy(AHCIQState *ahci, uint8_t px, uint8_t cx)
+{
+    uint32_t reg;
+
+    /* Assert that the command slot is no longer busy (NCQ) */
+    reg = ahci_px_rreg(ahci, px, AHCI_PX_SACT);
+    ASSERT_BIT_CLEAR(reg, (1 << cx));
+
+    /* Non-NCQ */
+    reg = ahci_px_rreg(ahci, px, AHCI_PX_CI);
+    ASSERT_BIT_CLEAR(reg, (1 << cx));
+
+    /* And assert that we are generally not busy. */
+    reg = ahci_px_rreg(ahci, px, AHCI_PX_TFD);
+    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_BSY);
+    ASSERT_BIT_CLEAR(reg, AHCI_PX_TFD_STS_DRQ);
+}
+
 /* Get the #cx'th command of port #px. */
 void ahci_get_command_header(AHCIQState *ahci, uint8_t px,
                              uint8_t cx, AHCICommandHeader *cmd)
tests/libqos/ahci.h +1 -0
index 4b6696a..7c504a5 100644
@@ -436,6 +436,7 @@ void ahci_port_clear(AHCIQState *ahci, uint8_t px);
 void ahci_port_check_error(AHCIQState *ahci, uint8_t px);
 void ahci_port_check_interrupts(AHCIQState *ahci, uint8_t px,
                                 uint32_t intr_mask);
+void ahci_port_check_nonbusy(AHCIQState *ahci, uint8_t px, uint8_t cx);
 void ahci_get_command_header(AHCIQState *ahci, uint8_t px,
                              uint8_t cx, AHCICommandHeader *cmd);
 void ahci_set_command_header(AHCIQState *ahci, uint8_t px,
Signature · 1 line
1.9.3
author
#13 lore raw

PATCH 8/19 libqos/ahci: Add cmd response sanity check helpers

In reply to John Snow

This patch adds a few helpers to help sanity-check the response of the AHCI device after a command.

ahci_d2h_check_sanity inspects the D2H Register FIS, ahci_pio_check_sanity inspects the PIO Setup FIS, and ahci_cmd_check_sanity inspects the command header.

To support the PIO sanity check, a new structure is added for the PIO Setup FIS type. Existing FIS types (H2D and D2H) have had their members renamed slightly to condense reserved members into fewer fields; and LBA fields are now represented by arrays of 8 byte chunks instead of independent variables.

---
 tests/ahci-test.c   | 29 ++++------------------------
 tests/libqos/ahci.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/ahci.h | 54 ++++++++++++++++++++++++++++++++++++-----------------
 3 files changed, 88 insertions(+), 42 deletions(-)
tests/ahci-test.c +4 -25
index 77d0f97..b21d4ad 100644
@@ -657,12 +657,10 @@ static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
  */
 static void ahci_test_identify(AHCIQState *ahci)
 {
-    RegD2HFIS *d2h = g_malloc0(0x20);
-    RegD2HFIS *pio = g_malloc0(0x20);
     RegH2DFIS fis;
     AHCICommandHeader cmd;
     PRD prd;
-    uint32_t reg, data_ptr;
+    uint32_t data_ptr;
     uint16_t buff[256];
     unsigned i;
     int rc;
@@ -752,27 +750,11 @@ static void ahci_test_identify(AHCIQState *ahci)
     /* BUG: we expect AHCI_PX_IS_DPS to be set. */
     ahci_port_check_interrupts(ahci, i, AHCI_PX_IS_DHRS | AHCI_PX_IS_PSS);
     ahci_port_check_nonbusy(ahci, i, cx);
-
     /* Investigate the CMD, assert that we read 512 bytes */
-    ahci_get_command_header(ahci, i, cx, &cmd);
-    g_assert_cmphex(512, ==, cmd.prdbc);
-
+    ahci_port_check_cmd_sanity(ahci, i, cx, 512);
     /* Investigate FIS responses */
-    memread(ahci->port[i].fb + 0x20, pio, 0x20);
-    memread(ahci->port[i].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 = ahci_px_rreg(ahci, i, AHCI_PX_TFD);
-    g_assert_cmphex((reg & AHCI_PX_TFD_ERR), ==, pio->error);
-    g_assert_cmphex((reg & AHCI_PX_TFD_STS), ==, pio->status);
-    /* The PIO Setup FIS contains a "bytes read" field, which is a
-     * 16-bit value. The Physical Region Descriptor Byte Count is
-     * 32-bit, but for small transfers using one PRD, it should match. */
-    g_assert_cmphex(le16_to_cpu(pio->res4), ==, cmd.prdbc);
+    ahci_port_check_d2h_sanity(ahci, i, cx);
+    ahci_port_check_pio_sanity(ahci, i, cx, 512);
 
     /* Last, but not least: Investigate the IDENTIFY response data. */
     memread(data_ptr, &buff, 512);
@@ -789,9 +771,6 @@ static void ahci_test_identify(AHCIQState *ahci)
     string_bswap16(&buff[23], 8);
     rc = memcmp(&buff[23], "version ", 8);
     g_assert_cmphex(rc, ==, 0);
-
-    g_free(d2h);
-    g_free(pio);
 }
 
 /******************************************************************************/
tests/libqos/ahci.c +47 -0

53 lines not on this page · raw message

tests/libqos/ahci.h +37 -17

82 lines not on this page · raw message

Signature · 1 line
1.9.3
author
#14 lore raw

PATCH 9/19 qtest/ahci: Demagic ahci tests.

In reply to John Snow

Add human-readable command names and other miscellaneous #defines to help make the code more readable.

Some of these definitions are not yet used in this current series, but for convenience and sanity they have been lumped together here, as it's more trouble than it is worth in a test suite to hand-pick, one-by-one, which preprocessor definitions are useful per-each test.

These definitions include:

ATA Command Mnemonics
Current expected AHCI sector size
FIS magic bytes
REG_H2D_FIS flags
Command Header flags

---
 tests/ahci-test.c   | 12 ++++++------
 tests/libqos/ahci.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 6 deletions(-)
tests/ahci-test.c +6 -6
index b21d4ad..c862525 100644
@@ -705,9 +705,9 @@ static void ahci_test_identify(AHCIQState *ahci)
 
     /* Construct our Command Header (set_command_header handles endianness.) */
     memset(&cmd, 0x00, sizeof(cmd));
-    cmd.flags = 5;     /* reg_h2d_fis is 5 double-words long */
-    cmd.flags = 0x400; /* clear PxTFD.STS.BSY when done */
-    cmd.prdtl = 1;     /* One PRD table entry. */
+    cmd.flags = 5;            /* reg_h2d_fis is 5 double-words long */
+    cmd.flags = CMDH_CLR_BSY; /* clear PxTFD.STS.BSY when done */
+    cmd.prdtl = 1;            /* One PRD table entry. */
     cmd.prdbc = 0;
     cmd.ctba = table;
 
@@ -719,10 +719,10 @@ static void ahci_test_identify(AHCIQState *ahci)
 
     /* 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.fis_type = REG_H2D_FIS;  /* Register Host-to-Device FIS */
+    fis.command = CMD_IDENTIFY;
     fis.device = 0;
-    fis.flags = 0x80;    /* Indicate this is a command FIS */
+    fis.flags = REG_H2D_FIS_CMD; /* Indicate this is a command FIS */
 
     /* We've committed nothing yet, no interrupts should be posted yet. */
     g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
tests/libqos/ahci.h +53 -0

59 lines not on this page · raw message

Signature · 1 line
1.9.3
#15 lore raw

Re: [Qemu-devel] [PATCH 09/19] qtest/ahci: Demagic ahci tests.

tests/ahci-test.c static void ahci_test_identify(AHCIQState *ahci) · line 709

Quoted 6 lines · John Snow
     memset(&cmd, 0x00, sizeof(cmd));
tests/ahci-test.c +2 -3
-    cmd.flags = 5;     /* reg_h2d_fis is 5 double-words long */
-    cmd.flags = 0x400; /* clear PxTFD.STS.BSY when done */
-    cmd.prdtl = 1;     /* One PRD table entry. */
+    cmd.flags = 5;            /* reg_h2d_fis is 5 double-words long */
+    cmd.flags = CMDH_CLR_BSY; /* clear PxTFD.STS.BSY when done */

Same = vs. |= note as before.

Paolo

author
#16 lore raw

PATCH 10/19 libqos/ahci: Add ide cmd properties

In reply to John Snow

Add a structure that defines some properties of various IDE commands. These will be used to simplify the interface to the libqos AHCI calls, lessening the redundancy of specifying and respecifying properties of commands to various helper functions.

The "Invalid Command Sentinel" here that caps the property array is an invalid ATA command, namely 0x01. 0x00 is NOP and 0xFF is reserved for vendor usage, so I chose the first invalid one instead.

---
 tests/libqos/ahci.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
tests/libqos/ahci.c +42 -0
index 924b9f0..f0e2a27 100644
@@ -34,6 +34,48 @@
 #include "hw/pci/pci_ids.h"
 #include "hw/pci/pci_regs.h"
 
+typedef struct AHCICommandProp {
+    uint8_t  cmd;        /* Command Code */
+    bool     data;       /* Data transfer command? */
+    bool     pio;
+    bool     dma;
+    bool     lba28;
+    bool     lba48;
+    bool     read;
+    bool     write;
+    bool     atapi;
+    bool     ncq;
+    uint64_t size;       /* Static transfer size, for commands like IDENTIFY. */
+    uint32_t interrupts; /* Expected interrupts for this command. */
+} AHCICommandProp;
+
+#define CMD_INVALID_SENTINEL 0x01
+
+AHCICommandProp ahci_command_properties[] = {
+    { .cmd = CMD_READ_PIO,      .data = true,  .pio = true,
+                                .lba28 = true, .read = true },
+    { .cmd = CMD_WRITE_PIO,     .data = true,  .pio = true,
+                                .lba28 = true, .write = true },
+    { .cmd = CMD_READ_PIO_EXT,  .data = true,  .pio = true,
+                                .lba48 = true, .read = true },
+    { .cmd = CMD_WRITE_PIO_EXT, .data = true,  .pio = true,
+                                .lba48 = true, .write = true },
+    { .cmd = CMD_READ_DMA,      .data = true,  .dma = true,
+                                .lba28 = true, .read = true },
+    { .cmd = CMD_WRITE_DMA,     .data = true,  .dma = true,
+                                .lba28 = true, .write = true },
+    { .cmd = CMD_READ_DMA_EXT,  .data = true,  .dma = true,
+                                .lba48 = true, .read = true },
+    { .cmd = CMD_WRITE_DMA_EXT, .data = true,  .dma = true,
+                                .lba48 = true, .write = true },
+    { .cmd = CMD_IDENTIFY,      .data = true,  .pio = true,
+                                .size = 512,   .read = true },
+    { .cmd = CMD_READ_MAX,      .lba28 = true },
+    { .cmd = CMD_READ_MAX_EXT,  .lba48 = true },
+    { .cmd = CMD_FLUSH_CACHE,   .data = false },
+    { .cmd = CMD_INVALID_SENTINEL }
+};
+
 /**
  * Allocate space in the guest using information in the AHCIQState object.
  */
Signature · 1 line
1.9.3
#17 lore raw

Re: [Qemu-devel] [PATCH 10/19] libqos/ahci: Add ide cmd properties

Quoted 3 lines · John Snow

The "Invalid Command Sentinel" here that caps the property array is an invalid ATA command, namely 0x01. 0x00 is NOP and 0xFF is reserved for vendor usage, so I chose the first invalid one instead.

You can use ARRAY_SIZE instead.

Paolo

author
#18 lore raw

PATCH 11/19 libqos/ahci: add ahci command functions

In reply to John Snow

This patch adds the AHCICommand structure, and a set of functions to operate on the structure.

ahci_command_create - Initialize and create a new AHCICommand in memory ahci_command_free - Destroy this object. ahci_command_set_buffer - Set where the guest memory DMA buffer is. ahci_command_commit - Write this command to the AHCI HBA. ahci_command_issue - Issue the committed command synchronously. ahci_command_issue_async - Issue the committed command asynchronously. ahci_command_wait - Wait for an asynchronous command to finish. ahci_command_slot - Get the number of the command slot we committed to.

Helpers:
size_to_prdtl - Calculate the required minimum PRDTL size from
a buffer size.
ahci_command_find - Given an ATA command mnemonic, look it up in the
properties table to obtain info about the command.
command_header_init - Initialize the command header with sane values. command_table_init - Initialize the command table with sane values.

---
 tests/ahci-test.c   |  73 +++++--------------
 tests/libqos/ahci.c | 202 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/ahci.h |  15 ++++
 3 files changed, 234 insertions(+), 56 deletions(-)
tests/ahci-test.c +17 -56
index c862525..0834020 100644
@@ -657,30 +657,28 @@ static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
  */
 static void ahci_test_identify(AHCIQState *ahci)
 {
-    RegH2DFIS fis;
-    AHCICommandHeader cmd;
-    PRD prd;
     uint32_t data_ptr;
     uint16_t buff[256];
     unsigned i;
     int rc;
+    AHCICommand *cmd;
     uint8_t cx;
-    uint64_t table;
 
     g_assert(ahci != NULL);
 
     /* We need to:
-     * (1) Create a Command Table Buffer and update the Command List Slot #0
-     *     to point to this buffer.
-     * (2) Construct an FIS host-to-device command structure, and write it to
+     * (1) Create a data buffer for the IDENTIFY response to be sent to,
+     * (2) Create a Command Table Buffer
+     * (3) 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
+     * (5) Obtain a Command List slot, and update this header to point to
+     *     the Command Table we built above.
+     * (6) 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.
+     * (7) We inform the HBA via PxCI that there is a command ready in slot #0.
      */
 
     /* Pick the first implemented and running port */
@@ -690,61 +688,24 @@ static void ahci_test_identify(AHCIQState *ahci)
     /* Clear out the FIS Receive area and any pending interrupts. */
     ahci_port_clear(ahci, i);
 
-    /* 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 = ahci_alloc(ahci, CMD_TBL_SIZ(1));
-    g_assert(table);
-    ASSERT_BIT_CLEAR(table, 0x7F);
-
-    /* Create a data buffer ... where we will dump the IDENTIFY data to. */
+    /* Create a data buffer where we will dump the IDENTIFY data to. */
     data_ptr = ahci_alloc(ahci, 512);
     g_assert(data_ptr);
 
-    /* pick a command slot (should be 0!) */
-    cx = ahci_pick_cmd(ahci, i);
-
-    /* Construct our Command Header (set_command_header handles endianness.) */
-    memset(&cmd, 0x00, sizeof(cmd));
-    cmd.flags = 5;            /* reg_h2d_fis is 5 double-words long */
-    cmd.flags = CMDH_CLR_BSY; /* clear PxTFD.STS.BSY when done */
-    cmd.prdtl = 1;            /* One PRD table entry. */
-    cmd.prdbc = 0;
-    cmd.ctba = table;
-
-    /* Construct our PRD, noting that DBC is 0-indexed. */
-    prd.dba = cpu_to_le64(data_ptr);
-    prd.res = 0;
-    /* 511+1 bytes, request DPS interrupt */
-    prd.dbc = cpu_to_le32(511 | 0x80000000);
-
-    /* Construct our Command FIS, Based on http://wiki.osdev.org/AHCI */
-    memset(&fis, 0x00, sizeof(fis));
-    fis.fis_type = REG_H2D_FIS;  /* Register Host-to-Device FIS */
-    fis.command = CMD_IDENTIFY;
-    fis.device = 0;
-    fis.flags = REG_H2D_FIS_CMD; /* Indicate this is a command FIS */
-
-    /* We've committed nothing yet, no interrupts should be posted yet. */
-    g_assert_cmphex(ahci_px_rreg(ahci, 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 #cx, pointing to the Table, to the Command List Buffer. */
-    ahci_set_command_header(ahci, i, cx, &cmd);
+    /* Construct the Command Table (FIS and PRDT) and Command Header */
+    cmd = ahci_command_create(CMD_IDENTIFY);
+    ahci_command_set_buffer(cmd, data_ptr);
+    /* Write the command header and PRDT to guest memory */
+    ahci_command_commit(ahci, cmd, i);
 
     /* Everything is in place, but we haven't given the go-ahead yet,
      * so we should find that there are no pending interrupts yet. */
     g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
 
     /* Issue Command #cx via PxCI */
-    ahci_px_wreg(ahci, i, AHCI_PX_CI, (1 << cx));
-    while (BITSET(ahci_px_rreg(ahci, i, AHCI_PX_TFD), AHCI_PX_TFD_STS_BSY)) {
-        usleep(50);
-    }
+    ahci_command_issue(ahci, cmd);
+    cx = ahci_command_slot(cmd);
+
     /* Check registers for post-command consistency */
     ahci_port_check_error(ahci, i);
     /* BUG: we expect AHCI_PX_IS_DPS to be set. */
tests/libqos/ahci.c +202 -0

205 lines not on this page · raw message

tests/libqos/ahci.h +15 -0
index 0d12582..99ac820 100644
@@ -418,6 +418,9 @@ typedef struct PRD {
     uint32_t dbc;  /* Data Byte Count (0-indexed) & Interrupt Flag (bit 2^31) */
 } __attribute__((__packed__)) PRD;
 
+/* Opaque, defined within ahci.c */
+typedef struct AHCICommand AHCICommand;
+
 /*** Macro Utilities ***/
 #define BITANY(data, mask) (((data) & (mask)) != 0)
 #define BITSET(data, mask) (((data) & (mask)) == (mask))
@@ -516,5 +519,17 @@ void ahci_set_command_header(AHCIQState *ahci, uint8_t px,
                              uint8_t cx, AHCICommandHeader *cmd);
 void ahci_destroy_command(AHCIQState *ahci, uint8_t px, uint8_t cx);
 unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t px);
+unsigned size_to_prdtl(unsigned bytes, unsigned bytes_per_prd);
 
+/* Command Lifecycle */
+AHCICommand *ahci_command_create(uint8_t command_name);
+void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t px);
+void ahci_command_issue(AHCIQState *ahci, AHCICommand *cmd);
+void ahci_command_issue_async(AHCIQState *ahci, AHCICommand *cmd);
+void ahci_command_wait(AHCIQState *ahci, AHCICommand *cmd);
+void ahci_command_free(AHCICommand *cmd);
+/* Command adjustments */
+void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer);
+
+uint8_t ahci_command_slot(AHCICommand *cmd);
 #endif
Signature · 1 line
1.9.3
author
#19 lore raw

PATCH 12/19 libqos/ahci: add ahci command verify

In reply to John Snow

Helps to verify that a command completed successfully.

---
 tests/ahci-test.c   | 16 ++--------------
 tests/libqos/ahci.c | 12 ++++++++++++
 tests/libqos/ahci.h |  1 +
 3 files changed, 15 insertions(+), 14 deletions(-)
tests/ahci-test.c +2 -14
index 0834020..6e7b765 100644
@@ -662,7 +662,6 @@ static void ahci_test_identify(AHCIQState *ahci)
     unsigned i;
     int rc;
     AHCICommand *cmd;
-    uint8_t cx;
 
     g_assert(ahci != NULL);
 
@@ -702,20 +701,9 @@ static void ahci_test_identify(AHCIQState *ahci)
      * so we should find that there are no pending interrupts yet. */
     g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
 
-    /* Issue Command #cx via PxCI */
+    /* Issue command and sanity check response. */
     ahci_command_issue(ahci, cmd);
-    cx = ahci_command_slot(cmd);
-
-    /* Check registers for post-command consistency */
-    ahci_port_check_error(ahci, i);
-    /* BUG: we expect AHCI_PX_IS_DPS to be set. */
-    ahci_port_check_interrupts(ahci, i, AHCI_PX_IS_DHRS | AHCI_PX_IS_PSS);
-    ahci_port_check_nonbusy(ahci, i, cx);
-    /* Investigate the CMD, assert that we read 512 bytes */
-    ahci_port_check_cmd_sanity(ahci, i, cx, 512);
-    /* Investigate FIS responses */
-    ahci_port_check_d2h_sanity(ahci, i, cx);
-    ahci_port_check_pio_sanity(ahci, i, cx, 512);
+    ahci_command_verify(ahci, cmd);
 
     /* Last, but not least: Investigate the IDENTIFY response data. */
     memread(data_ptr, &buff, 512);
tests/libqos/ahci.c +12 -0
index 87cc9dc..594c821 100644
@@ -725,6 +725,18 @@ void ahci_command_issue(AHCIQState *ahci, AHCICommand *cmd)
     ahci_command_wait(ahci, cmd);
 }
 
+void ahci_command_verify(AHCIQState *ahci, AHCICommand *cmd)
+{
+    ahci_port_check_error(ahci, cmd->px);
+    ahci_port_check_interrupts(ahci, cmd->px, cmd->interrupts);
+    ahci_port_check_nonbusy(ahci, cmd->px, cmd->cx);
+    ahci_port_check_cmd_sanity(ahci, cmd->px, cmd->cx, cmd->xbytes);
+    ahci_port_check_d2h_sanity(ahci, cmd->px, cmd->cx);
+    if (cmd->props->pio) {
+        ahci_port_check_pio_sanity(ahci, cmd->px, cmd->cx, cmd->xbytes);
+    }
+}
+
 uint8_t ahci_command_slot(AHCICommand *cmd)
 {
     return cmd->cx;
tests/libqos/ahci.h +1 -0
index 99ac820..46b7bf3 100644
@@ -527,6 +527,7 @@ void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t px);
 void ahci_command_issue(AHCIQState *ahci, AHCICommand *cmd);
 void ahci_command_issue_async(AHCIQState *ahci, AHCICommand *cmd);
 void ahci_command_wait(AHCIQState *ahci, AHCICommand *cmd);
+void ahci_command_verify(AHCIQState *ahci, AHCICommand *cmd);
 void ahci_command_free(AHCICommand *cmd);
 /* Command adjustments */
 void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer);
Signature · 1 line
1.9.3
author
#20 lore raw

PATCH 13/19 libqos/ahci: add ahci command size setters

In reply to John Snow

Adds setters for size, prd_size and both via set_sizes.

---
 tests/libqos/ahci.c | 22 ++++++++++++++++++++++
 tests/libqos/ahci.h |  5 +++++
 2 files changed, 27 insertions(+)
tests/libqos/ahci.c +22 -0
index 594c821..3375d54 100644
@@ -648,6 +648,28 @@ void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer)
     cmd->buffer = buffer;
 }
 
+void ahci_command_set_sizes(AHCICommand *cmd, uint64_t xbytes,
+                            unsigned prd_size)
+{
+    /* Each PRD can describe up to 4MiB, and must not be odd. */
+    g_assert_cmphex(prd_size, <=, 4096 * 1024);
+    g_assert_cmphex(prd_size & 0x01, ==, 0x00);
+    cmd->prd_size = prd_size;
+    cmd->xbytes = xbytes;
+    cmd->fis.count = cpu_to_le16(cmd->xbytes / AHCI_SECTOR_SIZE);
+    cmd->header.prdtl = size_to_prdtl(cmd->xbytes, cmd->prd_size);
+}
+
+void ahci_command_set_size(AHCICommand *cmd, uint64_t xbytes)
+{
+    ahci_command_set_sizes(cmd, xbytes, cmd->prd_size);
+}
+
+void ahci_command_set_prd_size(AHCICommand *cmd, unsigned prd_size)
+{
+    ahci_command_set_sizes(cmd, cmd->xbytes, prd_size);
+}
+
 void ahci_command_commit(AHCIQState *ahci, AHCICommand *cmd, uint8_t px)
 {
     uint16_t i, prdtl;
tests/libqos/ahci.h +5 -0
index 46b7bf3..1300967 100644
@@ -531,6 +531,11 @@ void ahci_command_verify(AHCIQState *ahci, AHCICommand *cmd);
 void ahci_command_free(AHCICommand *cmd);
 /* Command adjustments */
 void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer);
+void ahci_command_set_size(AHCICommand *cmd, uint64_t xbytes);
+void ahci_command_set_prd_size(AHCICommand *cmd, unsigned prd_size);
+void ahci_command_set_sizes(AHCICommand *cmd, uint64_t xbytes,
+                            unsigned prd_size);
 
 uint8_t ahci_command_slot(AHCICommand *cmd);
+
 #endif
Signature · 1 line
1.9.3
#21 lore raw

Re: [Qemu-devel] [PATCH 13/19] libqos/ahci: add ahci command size setters

tests/libqos/ahci.c void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer) · line 659

Quoted 9 lines · John Snow
tests/libqos/ahci.c +9 -0
+void ahci_command_set_sizes(AHCICommand *cmd, uint64_t xbytes,
+                            unsigned prd_size)
+{
+    /* Each PRD can describe up to 4MiB, and must not be odd. */
+    g_assert_cmphex(prd_size, <=, 4096 * 1024);
+    g_assert_cmphex(prd_size & 0x01, ==, 0x00);
+    cmd->prd_size = prd_size;
+    cmd->xbytes = xbytes;
+    cmd->fis.count = cpu_to_le16(cmd->xbytes / AHCI_SECTOR_SIZE);

Why do you need cpu_to_le16 here, instead of having it in the function that writes the command to guest memory?

Paolo

Quoted 3 lines
tests/libqos/ahci.c +3 -0
+    cmd->header.prdtl = size_to_prdtl(cmd->xbytes, cmd->prd_size);
+}
+
author
#22 lore raw

Re: [Qemu-devel] [PATCH 13/19] libqos/ahci: add ahci command size setters

Quoted 16 lines · Paolo Bonzini

tests/libqos/ahci.c void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer) · line 659

Quoted 9 lines · John Snow
tests/libqos/ahci.c +9 -0
+void ahci_command_set_sizes(AHCICommand *cmd, uint64_t xbytes,
+                            unsigned prd_size)
+{
+    /* Each PRD can describe up to 4MiB, and must not be odd. */
+    g_assert_cmphex(prd_size, <=, 4096 * 1024);
+    g_assert_cmphex(prd_size & 0x01, ==, 0x00);
+    cmd->prd_size = prd_size;
+    cmd->xbytes = xbytes;
+    cmd->fis.count = cpu_to_le16(cmd->xbytes / AHCI_SECTOR_SIZE);

Why do you need cpu_to_le16 here, instead of having it in the function that writes the command to guest memory?

Quoted 6 lines

Paolo

tests/libqos/ahci.c void ahci_command_set_buffer(AHCICommand *cmd, uint64_t buffer) · line 662

Quoted 3 lines
tests/libqos/ahci.c +3 -0
+    cmd->header.prdtl = size_to_prdtl(cmd->xbytes, cmd->prd_size);
+}
+

In this case, only the command header had a utility written for it to flip the bits for me. This is part of the FIS, instead, which has no explicit flip-on-write mechanism inside of commit.

So, it's correct, but not terribly consistent.

I can write a fis write helper to make this more internally consistent about when we handle it for the user and when we don't.

#23 lore raw

Re: [Qemu-devel] [PATCH 13/19] libqos/ahci: add ahci command size setters

Quoted 9 lines · John Snow

In this case, only the command header had a utility written for it to flip the bits for me. This is part of the FIS, instead, which has no explicit flip-on-write mechanism inside of commit.

So, it's correct, but not terribly consistent.

I can write a fis write helper to make this more internally consistent about when we handle it for the user and when we don't.

Please do. :)

Paolo

author
#24 lore raw

PATCH 14/19 libqos/ahci: Add ahci_guest_io

In reply to John Snow

ahci_guest_io is a shorthand function that will, in one shot, execute a data command on the guest to the specified guest buffer location, in the requested amount.

---
 tests/libqos/ahci.c | 15 +++++++++++++++
 tests/libqos/ahci.h |  2 ++
 2 files changed, 17 insertions(+)
tests/libqos/ahci.c +15 -0
index 3375d54..d55dab8 100644
@@ -536,6 +536,21 @@ inline unsigned size_to_prdtl(unsigned bytes, unsigned bytes_per_prd)
     return (bytes + bytes_per_prd - 1) / bytes_per_prd;
 }
 
+/* Given a guest buffer address, perform an IO operation */
+void ahci_guest_io(AHCIQState *ahci, uint8_t px, uint8_t ide_cmd,
+                   uint64_t buffer, size_t bufsize)
+{
+    AHCICommand *cmd;
+
+    cmd = ahci_command_create(ide_cmd);
+    ahci_command_set_buffer(cmd, buffer);
+    ahci_command_set_size(cmd, bufsize);
+    ahci_command_commit(ahci, cmd, px);
+    ahci_command_issue(ahci, cmd);
+    ahci_command_verify(ahci, cmd);
+    ahci_command_free(cmd);
+}
+
 typedef struct AHCICommand {
     /* Test Management Data */
     uint8_t name;
tests/libqos/ahci.h +2 -0
index 1300967..cbde141 100644
@@ -520,6 +520,8 @@ void ahci_set_command_header(AHCIQState *ahci, uint8_t px,
 void ahci_destroy_command(AHCIQState *ahci, uint8_t px, uint8_t cx);
 unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t px);
 unsigned size_to_prdtl(unsigned bytes, unsigned bytes_per_prd);
+void ahci_guest_io(AHCIQState *ahci, uint8_t px, uint8_t ide_cmd,
+                   uint64_t gbuffer, size_t size);
 
 /* Command Lifecycle */
 AHCICommand *ahci_command_create(uint8_t command_name);
Signature · 1 line
1.9.3
author
#25 lore raw

PATCH 15/19 libqos/ahci: add ahci_io

In reply to John Snow

ahci_io is a wrapper around ahci_guest_io that takes a pointer to host memory instead, and will create a guest memory buffer and copy the data to/from as needed and as appropriate for a read/write command, such that after a read, the guest data will be in a host buffer, and for a write, the data will be transmitted to guest memory prior to the block operation.

Now that we have all the syntactic sugar functions in place for AHCI, we can convert the identify test to be very, very short.

---
 tests/ahci-test.c   | 63 +++++++++++++++++++++--------------------------------
 tests/libqos/ahci.c | 25 +++++++++++++++++++++
 tests/libqos/ahci.h |  2 ++
 3 files changed, 52 insertions(+), 38 deletions(-)
tests/ahci-test.c +25 -38
index 6e7b765..47491fe 100644
@@ -657,56 +657,43 @@ static void ahci_test_port_spec(AHCIQState *ahci, uint8_t port)
  */
 static void ahci_test_identify(AHCIQState *ahci)
 {
-    uint32_t data_ptr;
     uint16_t buff[256];
-    unsigned i;
+    unsigned px;
     int rc;
-    AHCICommand *cmd;
+    const size_t buffsize = 512;
 
     g_assert(ahci != NULL);
 
-    /* We need to:
-     * (1) Create a data buffer for the IDENTIFY response to be sent to,
-     * (2) Create a Command Table Buffer
+    /**
+     * This serves as a bit of a tutorial on AHCI device programming:
+     *
+     * (1) Create a data buffer for the IDENTIFY response to be sent to
+     * (2) Create a Command Table buffer, where we will store the
+     *     command and PRDT (Physical Region Descriptor Table)
      * (3) Construct an FIS host-to-device command structure, and write it to
-     *     the top of the command table buffer.
-     * (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) Obtain a Command List slot, and update this header to point to
-     *     the Command Table we built above.
-     * (6) 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.
-     * (7) We inform the HBA via PxCI that there is a command ready in slot #0.
+     *     the top of the Command Table buffer.
+     * (4) Create one or more Physical Region Descriptors (PRDs) that describe
+     *     a location in memory where data may be stored/retrieved.
+     * (5) Write these PRDTs to the bottom (offset 0x80) of the Command Table.
+     * (6) Each AHCI port has up to 32 command slots. Each slot contains a
+     *     header that points to a Command Table buffer. Pick an unused slot
+     *     and update it to point to the Command Table we have built.
+     * (7) Now: Command #n points to our Command Table, and our Command Table
+     *     contains the FIS (that describes our command) and the PRDTL, which
+     *     describes our buffer.
+     * (8) We inform the HBA via PxCI (Command Issue) that the command in slot
+     *     #n is ready for processing.
      */
 
     /* Pick the first implemented and running port */
-    i = ahci_port_select(ahci);
-    g_test_message("Selected port %u for test", i);
+    px = ahci_port_select(ahci);
+    g_test_message("Selected port %u for test", px);
 
     /* Clear out the FIS Receive area and any pending interrupts. */
-    ahci_port_clear(ahci, i);
+    ahci_port_clear(ahci, px);
 
-    /* Create a data buffer where we will dump the IDENTIFY data to. */
-    data_ptr = ahci_alloc(ahci, 512);
-    g_assert(data_ptr);
-
-    /* Construct the Command Table (FIS and PRDT) and Command Header */
-    cmd = ahci_command_create(CMD_IDENTIFY);
-    ahci_command_set_buffer(cmd, data_ptr);
-    /* Write the command header and PRDT to guest memory */
-    ahci_command_commit(ahci, cmd, i);
-
-    /* Everything is in place, but we haven't given the go-ahead yet,
-     * so we should find that there are no pending interrupts yet. */
-    g_assert_cmphex(ahci_px_rreg(ahci, i, AHCI_PX_IS), ==, 0);
-
-    /* Issue command and sanity check response. */
-    ahci_command_issue(ahci, cmd);
-    ahci_command_verify(ahci, cmd);
-
-    /* Last, but not least: Investigate the IDENTIFY response data. */
-    memread(data_ptr, &buff, 512);
+    /* "Read" 512 bytes using CMD_IDENTIFY into the host buffer. */
+    ahci_io(ahci, px, CMD_IDENTIFY, &buff, buffsize);
 
     /* Check serial number/version in the buffer */
     /* NB: IDENTIFY strings are packed in 16bit little endian chunks.
tests/libqos/ahci.c +25 -0
index d55dab8..e73205b 100644
@@ -580,6 +580,31 @@ static AHCICommandProp *ahci_command_find(uint8_t command_name)
     }
 }
 
+/* Given a HOST buffer, create a buffer address and perform an IO operation. */
+void ahci_io(AHCIQState *ahci, uint8_t px, uint8_t ide_cmd,
+             void *buffer, size_t bufsize)
+{
+    uint64_t ptr;
+    AHCICommandProp *props;
+
+    props = ahci_command_find(ide_cmd);
+    g_assert(props);
+    ptr = ahci_alloc(ahci, bufsize);
+    g_assert(ptr);
+
+    if (props->write) {
+        memwrite(ptr, buffer, bufsize);
+    }
+
+    ahci_guest_io(ahci, px, ide_cmd, ptr, bufsize);
+
+    if (props->read) {
+        memread(ptr, buffer, bufsize);
+    }
+
+    ahci_free(ahci, ptr);
+}
+
 /**
  * Initializes a basic command header in memory.
  * We assume that this is for an ATA command using RegH2DFIS.
tests/libqos/ahci.h +2 -0
index cbde141..3c4f478 100644
@@ -522,6 +522,8 @@ unsigned ahci_pick_cmd(AHCIQState *ahci, uint8_t px);
 unsigned size_to_prdtl(unsigned bytes, unsigned bytes_per_prd);
 void ahci_guest_io(AHCIQState *ahci, uint8_t px, uint8_t ide_cmd,
                    uint64_t gbuffer, size_t size);
+void ahci_io(AHCIQState *ahci, uint8_t px, uint8_t ide_cmd,
+             void *buffer, size_t bufsize);
 
 /* Command Lifecycle */
 AHCICommand *ahci_command_create(uint8_t command_name);
Signature · 1 line
1.9.3
author
#26 lore raw

PATCH 16/19 libqos/ahci: Add ahci_clean_mem

In reply to John Snow

Clean up guest memory being used in ahci_clean_mem, to be called during ahci_shutdown. With all guest memory leaks removed, add an option to the allocator to throw an assertion if a leak occurs.

This test adds some sanity to both the AHCI library and the allocator.

---
 tests/ahci-test.c     |  3 +++
 tests/libqos/ahci.c   | 18 ++++++++++++++++++
 tests/libqos/ahci.h   |  1 +
 tests/libqos/malloc.c |  5 +++++
 tests/libqos/malloc.h |  1 +
 5 files changed, 28 insertions(+)
tests/ahci-test.c +3 -0
index 47491fe..3a0131a 100644
@@ -86,6 +86,7 @@ static AHCIQState *ahci_boot(void)
         "-device ide-hd,drive=drive0 "
         "-global ide-hd.ver=%s";
     s->parent = qtest_pc_boot(cli, tmp_path, "testdisk", "version");
+    alloc_set_flags(s->parent->alloc, ALLOC_LEAK_ASSERT);
 
     /* Verify that we have an AHCI device present. */
     s->dev = get_ahci_device(&s->fingerprint);
@@ -99,6 +100,8 @@ static AHCIQState *ahci_boot(void)
 static void ahci_shutdown(AHCIQState *ahci)
 {
     QOSState *qs = ahci->parent;
+
+    ahci_clean_mem(ahci);
     free_ahci_device(ahci->dev);
     g_free(ahci);
     qtest_shutdown(qs);
tests/libqos/ahci.c +18 -0
index e73205b..87a9e62 100644
@@ -133,6 +133,24 @@ void free_ahci_device(QPCIDevice *dev)
     qpci_free_pc(pcibus);
 }
 
+/* Free all memory in-use by the AHCI device. */
+void ahci_clean_mem(AHCIQState *ahci)
+{
+    uint8_t px, cx;
+
+    for (px = 0; px < 32; ++px) {
+        if (ahci->port[px].fb) {
+            ahci_free(ahci, ahci->port[px].fb);
+        }
+        if (ahci->port[px].clb) {
+            for (cx = 0; cx < 32; cx++) {
+                ahci_destroy_command(ahci, px, cx);
+            }
+            ahci_free(ahci, ahci->port[px].clb);
+        }
+    }
+}
+
 /*** Logical Device Initialization ***/
 
 /**
tests/libqos/ahci.h +1 -0
index 3c4f478..2d25957 100644
@@ -499,6 +499,7 @@ uint64_t ahci_alloc(AHCIQState *ahci, size_t bytes);
 void ahci_free(AHCIQState *ahci, uint64_t addr);
 QPCIDevice *get_ahci_device(uint32_t *fingerprint);
 void free_ahci_device(QPCIDevice *dev);
+void ahci_clean_mem(AHCIQState *ahci);
 void ahci_pci_enable(AHCIQState *ahci);
 void start_ahci_device(AHCIQState *ahci);
 void ahci_hba_enable(AHCIQState *ahci);
tests/libqos/malloc.c +5 -0
index 8cce1ba..ad283f6 100644
@@ -324,3 +324,8 @@ void alloc_set_page_size(QGuestAllocator *allocator, size_t page_size)
     g_assert(is_power_of_2(page_size));
     allocator->page_size = page_size;
 }
+
+void alloc_set_flags(QGuestAllocator *allocator, QAllocOpts opts)
+{
+    allocator->opts |= opts;
+}
tests/libqos/malloc.h +1 -0
index a39dba4..71ac407 100644
@@ -36,5 +36,6 @@ QGuestAllocator *alloc_init(uint64_t start, uint64_t end);
 QGuestAllocator *alloc_init_flags(QAllocOpts flags,
                                   uint64_t start, uint64_t end);
 void alloc_set_page_size(QGuestAllocator *allocator, size_t page_size);
+void alloc_set_flags(QGuestAllocator *allocator, QAllocOpts opts);
 
 #endif
Signature · 1 line
1.9.3
author
#27 lore raw

PATCH 17/19 qtest/ahci: Add a macro bootup routine

In reply to John Snow

Add a routine that can be used to engage the AHCI device at a not-granular level so that bringing up the functionality of the HBA is easy in future tests that are not concerned with testing the bring-up process.

---
 tests/ahci-test.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
tests/ahci-test.c +16 -3
index 3a0131a..9207e73 100644
@@ -107,6 +107,21 @@ static void ahci_shutdown(AHCIQState *ahci)
     qtest_shutdown(qs);
 }
 
+/**
+ * Boot and fully enable the HBA device.
+ * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
+ */
+static AHCIQState *ahci_macro_bootup(void)
+{
+    AHCIQState *ahci;
+    ahci = ahci_boot();
+
+    ahci_pci_enable(ahci);
+    ahci_hba_enable(ahci);
+
+    return ahci;
+}
+
 /*** Specification Adherence Tests ***/
 
 /**
@@ -787,9 +802,7 @@ static void test_identify(void)
 {
     AHCIQState *ahci;
 
-    ahci = ahci_boot();
-    ahci_pci_enable(ahci);
-    ahci_hba_enable(ahci);
+    ahci = ahci_macro_bootup();
     ahci_test_identify(ahci);
     ahci_shutdown(ahci);
 }
Signature · 1 line
1.9.3
#28 lore raw

Re: [Qemu-devel] [PATCH 17/19] qtest/ahci: Add a macro bootup routine

tests/ahci-test.c static void ahci_shutdown(AHCIQState *ahci) · line 114

Quoted 5 lines · John Snow
tests/ahci-test.c +5 -0
+/**
+ * Boot and fully enable the HBA device.
+ * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
+ */
+static AHCIQState *ahci_macro_bootup(void)

Ugly name... I would just leave out this patch.

Paolo

Quoted 9 lines
tests/ahci-test.c +9 -0
+{
+    AHCIQState *ahci;
+    ahci = ahci_boot();
+
+    ahci_pci_enable(ahci);
+    ahci_hba_enable(ahci);
+
+    return ahci;
+}
author
#29 lore raw

Re: [Qemu-devel] [PATCH 17/19] qtest/ahci: Add a macro bootup routine

Quoted 13 lines · Paolo Bonzini

tests/ahci-test.c static void ahci_shutdown(AHCIQState *ahci) · line 114

Quoted 5 lines · John Snow
tests/ahci-test.c +5 -0
+/**
+ * Boot and fully enable the HBA device.
+ * @see ahci_boot, ahci_pci_enable and ahci_hba_enable.
+ */
+static AHCIQState *ahci_macro_bootup(void)

Ugly name... I would just leave out this patch.

Paolo

Quoted 10 lines

tests/ahci-test.c static void ahci_shutdown(AHCIQState *ahci) · line 123

Quoted 9 lines
tests/ahci-test.c +9 -0
+{
+    AHCIQState *ahci;
+    ahci = ahci_boot();
+
+    ahci_pci_enable(ahci);
+    ahci_hba_enable(ahci);
+
+    return ahci;
+}

It comes in handy later for testing migration so I don't have to do a lot of boilerplate for each instance, though it is just a convenience subroutine with no logic of its own.

I like to cut down on boilerplate as much as possible to expose the logic of the test as much as possible.

Have a suggestion for a better name, or are you very adamant about culling it?

--js

#30 lore raw

Re: [Qemu-devel] [PATCH 17/19] qtest/ahci: Add a macro bootup routine

Quoted 10 lines · John Snow

It comes in handy later for testing migration so I don't have to do a lot of boilerplate for each instance, though it is just a convenience subroutine with no logic of its own.

I like to cut down on boilerplate as much as possible to expose the logic of the test as much as possible.

Have a suggestion for a better name, or are you very adamant about culling it?

I'm adamant about culling it because I don't have a suggestion for a better name.

In the long run, I think we should just have a qos_boot function that does everything including PCI scanning, mapping BARs and initializing devices. But we're of course very far from that.

Paolo

author
#31 lore raw

PATCH 18/19 qtest/ahci: Assert sector size in identify test

In reply to John Snow

A minor sanity check to assert that the sector size is 512. The current block layer code deeply assumes that the IDE sector size will be 512 bytes, so we carry forward that assumption here.

This is useful for the DMA tests, which currently assume that a sector will always be 512 bytes.

---
 tests/ahci-test.c | 4 ++++
 1 file changed, 4 insertions(+)
tests/ahci-test.c +4 -0
index 9207e73..cae94b5 100644
@@ -678,6 +678,7 @@ static void ahci_test_identify(AHCIQState *ahci)
     uint16_t buff[256];
     unsigned px;
     int rc;
+    uint16_t sect_size;
     const size_t buffsize = 512;
 
     g_assert(ahci != NULL);
@@ -725,6 +726,9 @@ static void ahci_test_identify(AHCIQState *ahci)
     string_bswap16(&buff[23], 8);
     rc = memcmp(&buff[23], "version ", 8);
     g_assert_cmphex(rc, ==, 0);
+
+    sect_size = le16_to_cpu(*((uint16_t *)(&buff[5])));
+    g_assert_cmphex(sect_size, ==, 0x200);
 }
 
 /******************************************************************************/
Signature · 1 line
1.9.3
author
#32 lore raw

PATCH 19/19 qtest/ahci: Adding simple dma read-write test

In reply to John Snow

Adds a test case for AHCI wherein we write a 4K
block of a changing pattern to sector 0, then
read back that 4K and compare the transmit and
receive buffers.

---
 tests/ahci-test.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
tests/ahci-test.c +52 -0
index cae94b5..1f02fa8 100644
@@ -731,6 +731,45 @@ static void ahci_test_identify(AHCIQState *ahci)
     g_assert_cmphex(sect_size, ==, 0x200);
 }
 
+static void ahci_test_dma_rw_simple(AHCIQState *ahci)
+{
+    uint64_t ptr;
+    uint8_t px;
+    unsigned i;
+    const unsigned bufsize = 4096;
+    unsigned char *tx = g_malloc(bufsize);
+    unsigned char *rx = g_malloc0(bufsize);
+
+    g_assert(ahci != NULL);
+
+    /* Pick the first running port and clear it. */
+    px = ahci_port_select(ahci);
+    ahci_port_clear(ahci, px);
+
+    /*** Create pattern and transfer to guest ***/
+    /* Data buffer in the guest */
+    ptr = ahci_alloc(ahci, bufsize);
+    g_assert(ptr);
+
+    /* Write some indicative pattern to our 4K buffer. */
+    for (i = 0; i < bufsize; i++) {
+        tx[i] = (bufsize - i);
+    }
+    memwrite(ptr, tx, bufsize);
+
+    /* Write this buffer to disk, then read it back to the DMA buffer. */
+    ahci_guest_io(ahci, px, CMD_WRITE_DMA, ptr, bufsize);
+    ahci_guest_io(ahci, px, CMD_READ_DMA, ptr, bufsize);
+
+    /*** Read back the Data ***/
+    memread(ptr, rx, bufsize);
+    g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
+
+    ahci_free(ahci, ptr);
+    g_free(tx);
+    g_free(rx);
+}
+
 /******************************************************************************/
 /* Test Interfaces                                                            */
 /******************************************************************************/
@@ -811,6 +850,18 @@ static void test_identify(void)
     ahci_shutdown(ahci);
 }
 
+/**
+ * Perform a simple DMA R/W test, using a single PRD and non-NCQ commands.
+ */
+static void test_dma_rw_simple(void)
+{
+    AHCIQState *ahci;
+
+    ahci = ahci_macro_bootup();
+    ahci_test_dma_rw_simple(ahci);
+    ahci_shutdown(ahci);
+}
+
 /******************************************************************************/
 
 int main(int argc, char **argv)
@@ -866,6 +917,7 @@ int main(int argc, char **argv)
     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);
+    qtest_add_func("/ahci/dma/simple", test_dma_rw_simple);
 
     ret = g_test_run();
 
Signature · 1 line
1.9.3
#33 lore raw

Re: [Qemu-devel] [PATCH 19/19] qtest/ahci: Adding simple dma read-write test

tests/ahci-test.c static void ahci_test_identify(AHCIQState *ahci) · line 761

Quoted 4 lines · John Snow
tests/ahci-test.c +4 -0
+    memwrite(ptr, tx, bufsize);
+
+    /* Write this buffer to disk, then read it back to the DMA buffer. */
+    ahci_guest_io(ahci, px, CMD_WRITE_DMA, ptr, bufsize);

I would qmemset the buffer here.

Paolo

Quoted 5 lines
tests/ahci-test.c +5 -0
+    ahci_guest_io(ahci, px, CMD_READ_DMA, ptr, bufsize);
+
+    /*** Read back the Data ***/
+    memread(ptr, rx, bufsize);
+    g_assert_cmphex(memcmp(tx, rx, bufsize), ==, 0);
#34 lore raw
In reply to John Snow
Quoted 5 lines · John Snow

Add a simple DMA r/w test to ahci-test.

Oh, and for the first 18 patches, refactor everything into helpers so that each ahci_test isn't a thousand lines long.

Quoted 7 lines

This patch depends upon the "ahci test preliminary refactoring" series upstream, which shuffled a lot of libqos and malloc facilities to support this series.

This patchset is a necessary step in checking in AHCI/DMA migration tests that I will later use as proof as suitability of enabling the ICH9 and AHCI migration flags.

Sent a few comments, everything that I didn't reply to has my Reviewed-by.

Paolo

Quoted 28 lines

John Snow (19):
libqos/ahci: Add ahci_port_select helper libqos/ahci: Add ahci_port_clear helper qtest/ahci: rename 'Command' to 'CommandHeader' libqos/ahci: Add command header helpers libqos/ahci: Add ahci_port_check_error helper libqos/ahci: Add ahci_port_check_interrupts helper libqos/ahci: Add port_check_nonbusy helper libqos/ahci: Add cmd response sanity check helpers qtest/ahci: Demagic ahci tests. libqos/ahci: Add ide cmd properties libqos/ahci: add ahci command functions libqos/ahci: add ahci command verify libqos/ahci: add ahci command size setters libqos/ahci: Add ahci_guest_io libqos/ahci: add ahci_io
libqos/ahci: Add ahci_clean_mem qtest/ahci: Add a macro bootup routine qtest/ahci: Assert sector size in identify test qtest/ahci: Adding simple dma read-write test

 tests/ahci-test.c     | 246 +++++++++-------------
 tests/libqos/ahci.c   | 554 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/ahci.h   | 163 ++++++++++++---
 tests/libqos/malloc.c |   5 +
 tests/libqos/malloc.h |   1 +
 5 files changed, 798 insertions(+), 171 deletions(-)