[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 28/28] ahci: Add test_identify case to ahci-test.
From: |
John Snow |
Subject: |
[Qemu-devel] [PATCH 28/28] ahci: Add test_identify case to ahci-test. |
Date: |
Mon, 7 Jul 2014 14:18:09 -0400 |
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 <address@hidden>
---
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
- [Qemu-devel] [PATCH 27/28] ahci: Add test_hba_enable to ahci-test., (continued)
- [Qemu-devel] [PATCH 27/28] ahci: Add test_hba_enable to ahci-test., John Snow, 2014/07/07
- [Qemu-devel] [PATCH 22/28] libqos: Fixes a small memory leak., John Snow, 2014/07/07
- [Qemu-devel] [PATCH 20/28] libqos: Correct memory leak, John Snow, 2014/07/07
- [Qemu-devel] [PATCH 19/28] qtest: Adding qtest_memset and qmemset., John Snow, 2014/07/07
- [Qemu-devel] [PATCH 23/28] ahci: Adding basic functionality qtest., John Snow, 2014/07/07
- [Qemu-devel] [PATCH 28/28] ahci: Add test_identify case to ahci-test.,
John Snow <=
- [Qemu-devel] [PATCH 24/28] ahci: Add test_pci_spec to ahci-test., John Snow, 2014/07/07
- [Qemu-devel] [PATCH 25/28] ahci: add test_pci_enable to ahci-test., John Snow, 2014/07/07
- [Qemu-devel] [PATCH 26/28] ahci: Add test_hba_spec to ahci-test., John Snow, 2014/07/07