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 27/28] ahci: Add test_hba_enable to ahci-test.
Date: Mon,  7 Jul 2014 14:18:08 -0400	[thread overview]
Message-ID: <1404757089-4836-28-git-send-email-jsnow@redhat.com> (raw)
In-Reply-To: <1404757089-4836-1-git-send-email-jsnow@redhat.com>

This test engages the HBA functionality and initializes
values to sane defaults to allow for minimal HBA functionality.

Buffers are allocated and pointers are updated to allow minimal
I/O commands to complete as expected. Error registers and responses
are sanity checked for specification adherence.

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

diff --git a/tests/ahci-test.c b/tests/ahci-test.c
index 727c939..f342e2c 100644
--- a/tests/ahci-test.c
+++ b/tests/ahci-test.c
@@ -460,6 +460,140 @@ static QPCIDevice *start_ahci_device(QPCIDevice *ahci, HBA **hba_base)
     return ahci;
 }
 
+/**
+ * Test and initialize the AHCI's HBA memory areas.
+ * Initialize and start any ports with devices attached.
+ * Bring the HBA into the idle state.
+ */
+static void ahci_hba_enable(QPCIDevice *ahci, HBA *hba_base)
+{
+    /* Bits of interest in this section:
+     * GHC.AE     Global Host Control / AHCI Enable
+     * PxCMD.ST   Port Command: Start
+     * PxCMD.SUD  "Spin Up Device"
+     * PxCMD.POD  "Power On Device"
+     * PxCMD.FRE  "FIS Receive Enable"
+     * PxCMD.FR   "FIS Receive Running"
+     * PxCMD.CR   "Command List Running"
+     */
+
+    g_assert(ahci != NULL);
+    g_assert(hba_base != NULL);
+
+    uint32_t reg, ports_impl, clb, fb;
+    uint16_t i;
+    uint8_t num_cmd_slots;
+
+    g_assert(hba_base != 0);
+
+    /* Set GHC.AE to 1 */
+    ahci_set(AHCI_GHC, AHCI_GHC_AE);
+    reg = ahci_rreg(AHCI_GHC);
+    assert_bit_set(reg, AHCI_GHC_AE);
+
+    /* Read CAP.NCS, how many command slots do we have? */
+    reg = ahci_rreg(AHCI_CAP);
+    num_cmd_slots = ((reg & AHCI_CAP_NCS) >> ctzl(AHCI_CAP_NCS)) + 1;
+    g_test_message("Number of Command Slots: %u", num_cmd_slots);
+
+    /* Determine which ports are implemented. */
+    ports_impl = ahci_rreg(AHCI_PI);
+
+    for (i = 0; ports_impl; ports_impl >>= 1, ++i) {
+        if (!(ports_impl & 0x01)) {
+            continue;
+        }
+
+        g_test_message("Initializing port %u", i);
+
+        reg = px_rreg(i, AHCI_PX_CMD);
+        if (bitclr(reg, AHCI_PX_CMD_ST | AHCI_PX_CMD_CR |
+                   AHCI_PX_CMD_FRE | AHCI_PX_CMD_FR)) {
+            g_test_message("port is idle");
+        } else {
+            g_test_message("port needs to be idled");
+            px_clr(i, AHCI_PX_CMD, (AHCI_PX_CMD_ST | AHCI_PX_CMD_FRE));
+            /* The port has 500ms to disengage. */
+            usleep(500000);
+            reg = px_rreg(i, AHCI_PX_CMD);
+            assert_bit_clear(reg, AHCI_PX_CMD_CR);
+            assert_bit_clear(reg, AHCI_PX_CMD_FR);
+            g_test_message("port is now idle");
+            /* The spec does allow for possibly needing a PORT RESET
+             * or HBA reset if we fail to idle the port. */
+        }
+
+        /* Allocate Memory for the Command List Buffer & FIS Buffer */
+        /* PxCLB space ... 0x20 per command, as in 4.2.2 p 36 */
+        clb = guest_alloc(guest_malloc, num_cmd_slots * 0x20);
+        g_test_message("CLB: 0x%08x", clb);
+        px_wreg(i, AHCI_PX_CLB, clb);
+        g_assert_cmphex(clb, ==, px_rreg(i, AHCI_PX_CLB));
+
+        /* PxFB space ... 0x100, as in 4.2.1 p 35 */
+        fb = guest_alloc(guest_malloc, 0x100);
+        g_test_message("FB: 0x%08x", fb);
+        px_wreg(i, AHCI_PX_FB, fb);
+        g_assert_cmphex(fb, ==, px_rreg(i, AHCI_PX_FB));
+
+        /* Clear PxSERR, PxIS, then IS.IPS[x] by writing '1's. */
+        px_wreg(i, AHCI_PX_SERR, 0xFFFFFFFF);
+        px_wreg(i, AHCI_PX_IS, 0xFFFFFFFF);
+        ahci_wreg(AHCI_IS, (1 << i));
+
+        /* Verify Interrupts Cleared */
+        reg = px_rreg(i, AHCI_PX_SERR);
+        g_assert(reg == 0);
+
+        reg = px_rreg(i, AHCI_PX_IS);
+        g_assert(reg == 0);
+
+        reg = ahci_rreg(AHCI_IS);
+        assert_bit_clear(reg, (1 << i));
+
+        /* Enable All Interrupts: */
+        px_wreg(i, AHCI_PX_IE, 0xFFFFFFFF);
+        reg = px_rreg(i, AHCI_PX_IE);
+        g_assert_cmphex(reg, ==, ~((uint32_t)AHCI_PX_IE_RESERVED));
+
+        /* Enable the FIS Receive Engine. */
+        px_set(i, AHCI_PX_CMD, AHCI_PX_CMD_FRE);
+        reg = px_rreg(i, AHCI_PX_CMD);
+        assert_bit_set(reg, AHCI_PX_CMD_FR);
+
+        /* AHCI 1.3 spec: if !STS.BSY, !STS.DRQ and PxSSTS.DET indicates
+         * physical presence, a device is present and may be started. However,
+         * PxSERR.DIAG.X /may/ need to be cleared a priori. */
+        reg = px_rreg(i, AHCI_PX_SERR);
+        if (bitset(reg, AHCI_PX_SERR_DIAG_X)) {
+            px_set(i, AHCI_PX_SERR, AHCI_PX_SERR_DIAG_X);
+        }
+
+        reg = px_rreg(i, AHCI_PX_TFD);
+        if (bitclr(reg, AHCI_PX_TFD_STS_BSY | AHCI_PX_TFD_STS_DRQ)) {
+            reg = px_rreg(i, AHCI_PX_SSTS);
+            if ((reg & AHCI_PX_SSTS_DET) == SSTS_DET_ESTABLISHED) {
+                /* Device Found: set PxCMD.ST := 1 */
+                px_set(i, AHCI_PX_CMD, AHCI_PX_CMD_ST);
+                assert_bit_set(px_rreg(i, AHCI_PX_CMD), AHCI_PX_CMD_CR);
+                g_test_message("Started Device %u", i);
+            } else if ((reg & AHCI_PX_SSTS_DET)) {
+                /* Device present, but in some unknown state. */
+                g_assert_not_reached();
+            }
+        }
+    }
+
+    /* Enable GHC.IE */
+    ahci_set(AHCI_GHC, AHCI_GHC_IE);
+    reg = ahci_rreg(AHCI_GHC);
+    assert_bit_set(reg, AHCI_GHC_IE);
+
+    /* TODO: The device should now be idling and waiting for commands.
+     * In the future, a small test-case to inspect the Register D2H FIS
+     * and clear the initial interrupts might be good. */
+}
+
 /*** Specification Adherence Tests ***/
 
 /**
@@ -1093,6 +1227,21 @@ static void test_hba_spec(void)
     ahci_shutdown(ahci);
 }
 
+/**
+ * Engage the HBA functionality of the AHCI PCI device,
+ * and bring it into a functional idle state.
+ */
+static void test_hba_enable(void)
+{
+    QPCIDevice *ahci;
+    HBA *hba_base;
+
+    ahci_boot(&ahci);
+    ahci_pci_enable(ahci, &hba_base);
+    ahci_hba_enable(ahci, hba_base);
+    ahci_shutdown(ahci);
+}
+
 /******************************************************************************/
 
 int main(int argc, char **argv)
@@ -1123,6 +1272,7 @@ int main(int argc, char **argv)
     qtest_add_func("/ahci/pci_spec",   test_pci_spec);
     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);
 
     ret = g_test_run();
 
-- 
1.9.3

  parent reply	other threads:[~2014-07-07 18:18 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 ` John Snow [this message]
2014-07-31 15:24   ` [Qemu-devel] [PATCH 27/28] ahci: Add test_hba_enable " Stefan Hajnoczi
2014-07-07 18:18 ` [Qemu-devel] [PATCH 28/28] ahci: Add test_identify case " John Snow
2014-07-31 15:28   ` 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-28-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).