All of lore.kernel.org
 help / color / mirror / Atom feed
From: Niklas Cassel <cassel@kernel.org>
To: John Snow <jsnow@redhat.com>, "Denis V. Lunev" <den@openvz.org>,
	Fabiano Rosas <farosas@suse.de>,
	Laurent Vivier <lvivier@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: Damien Le Moal <dlemoal@kernel.org>,
	Niklas Cassel <cassel@kernel.org>,
	qemu-block@nongnu.org, qemu-devel@nongnu.org
Subject: [PATCH 03/13] hw/ide/ahci: clear a stale error status when an NCQ command is queued
Date: Fri, 27 Nov 2026 20:12:38 +0100	[thread overview]
Message-ID: <20261127191250.3298004-4-cassel@kernel.org> (raw)
In-Reply-To: <20261127191250.3298004-1-cassel@kernel.org>

Successfully queueing an NCQ command should clear ERR_STAT, BUSY and
DRQ, see ACS-6 table 369 and SATA 3.5a DFPDMAQ2.

Simply restarting the port after a fatal error does not clear ERR_STAT.
The task file status in PxTFD only changes when the device sends a FIS,
i.e. when queueing a new NCQ command.

Without this, we would incorrectly again enter the HBA stopped state
when queueing a new NCQ command after restarting the port. Add a qtest
that issues an NCQ command after a failed WRITE DMA EXT.

Fixes: 2967dc8209dd ("hw/ide/ahci: write D2H FIS when processing NCQ command")
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
 hw/ide/ahci.c           |  9 ++++--
 tests/qtest/ahci-test.c | 61 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index 3b872c6cf1..e3ae46e869 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -1150,13 +1150,16 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
 
     /*
      * A NCQ command clears the bit in PxCI after the command has been QUEUED
-     * successfully (ERROR not set, BUSY and DRQ cleared).
-     *
-     * For NCQ commands, PxCI will always be cleared here.
+     * successfully (ERROR not set, BUSY and DRQ cleared). A device reports
+     * this status for every command that it queues (SATA 3.5a DFPDMAQ2,
+     * ACS-6 table 369), whatever the status of the previous command was, so
+     * PxCI will always be cleared here.
      *
      * (Once the NCQ command is COMPLETED, the device will send a SDB FIS with
      * the interrupt bit set, which will clear PxSACT and raise an interrupt.)
      */
+    ad->port.ifs[0].status = READY_STAT | SEEK_STAT;
+    ad->port.ifs[0].error = 0;
     ahci_clear_cmd_issue(ad, slot);
 
     /*
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index 6acb2327e3..2b668ea708 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1169,6 +1169,66 @@ static void test_flush_retry(void)
     ahci_shutdown(ahci);
 }
 
+/*
+ * An NCQ command issued after a failed non-NCQ command is queued with BSY,
+ * DRQ and ERR cleared (SATA 3.5a DFPDMAQ2), so its PxCI bit is cleared and
+ * it completes without the error of the earlier command.
+ */
+static void test_ncq_after_error(void)
+{
+    AHCIQState *ahci;
+    AHCICommand *cmd;
+    uint32_t bit;
+    uint64_t ptr;
+    uint8_t px;
+    int i;
+
+    prepare_blkdebug_script(debug_path, "write_aio");
+    ahci = ahci_boot_and_enable("-drive file=blkdebug:%s:%s,if=none,id=drive0,"
+                                "format=%s -M q35 -device ide-hd,drive=drive0",
+                                debug_path, tmp_path, imgfmt);
+    px = ahci_port_select(ahci);
+    ahci_port_clear(ahci, px);
+
+    /* A WRITE DMA EXT that fails with the injected I/O error */
+    ptr = ahci_alloc(ahci, 512);
+    cmd = ahci_command_create(CMD_WRITE_DMA_EXT);
+    ahci_command_set_buffer(cmd, ptr);
+    ahci_command_set_size(cmd, 512);
+    ahci_command_commit(ahci, cmd, px);
+    ahci_command_issue(ahci, cmd);
+    g_assert_cmphex(ahci_px_rreg(ahci, px, AHCI_PX_IS) & AHCI_PX_IS_TFES,
+                    !=, 0);
+    ahci_command_free(cmd);
+
+    /* Restart the port, as after any error (AHCI 1.3.1 6.2.2.1) */
+    ahci_px_clr(ahci, px, AHCI_PX_CMD, AHCI_PX_CMD_ST);
+    ahci_port_clear(ahci, px);
+    ahci_px_set(ahci, px, AHCI_PX_CMD, AHCI_PX_CMD_ST);
+
+    cmd = ahci_command_create(READ_FPDMA_QUEUED);
+    ahci_command_set_buffer(cmd, ptr);
+    ahci_command_set_size(cmd, 512);
+    ahci_command_commit(ahci, cmd, px);
+    ahci_command_issue_async(ahci, cmd);
+    bit = 1u << ahci_command_slot(cmd);
+    for (i = 0; i < 100000 && (ahci_px_rreg(ahci, px, AHCI_PX_SACT) & bit);
+         i++) {
+        usleep(50);
+    }
+    g_assert_cmphex(ahci_px_rreg(ahci, px, AHCI_PX_SACT) & bit, ==, 0);
+    /* The command was acknowledged without an error, and PxCI cleared */
+    g_assert_cmphex(ahci_px_rreg(ahci, px, AHCI_PX_IS) & AHCI_PX_IS_TFES,
+                    ==, 0);
+    g_assert_cmphex(ahci_px_rreg(ahci, px, AHCI_PX_CI) & bit, ==, 0);
+    /* STS.ERR and the ERR field: nothing left from the failed command */
+    g_assert_cmphex(ahci_px_rreg(ahci, px, AHCI_PX_TFD) & 0xff01, ==, 0);
+
+    ahci_command_free(cmd);
+    ahci_free(ahci, ptr);
+    ahci_shutdown(ahci);
+}
+
 /*
  * After a command fails, the HBA processes no further commands until software
  * clears PxCMD.ST (AHCI 1.3.1 6.2.2), so it does not execute the failed
@@ -2466,6 +2526,7 @@ int main(int argc, char **argv)
 
     qtest_add_func("/ahci/flush/simple", test_flush);
     qtest_add_func("/ahci/flush/retry", test_flush_retry);
+    qtest_add_func("/ahci/io/ncq/after_error", test_ncq_after_error);
     qtest_add_func("/ahci/io/dma/halt_after_error", test_halt_after_error);
     qtest_add_func("/ahci/flush/migrate", test_flush_migrate);
 
-- 
2.56.1



  parent reply	other threads:[~2026-11-27 19:14 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
2026-11-27 19:12 ` [PATCH 01/13] hw/ide: report the SMART state in IDENTIFY DEVICE Niklas Cassel
2026-11-30  5:10   ` Damien Le Moal
2026-11-27 19:12 ` [PATCH 02/13] hw/ide/ahci: stop processing commands after a fatal error Niklas Cassel
2026-11-30  5:13   ` Damien Le Moal
2026-11-27 19:12 ` Niklas Cassel [this message]
2026-11-30  5:15   ` [PATCH 03/13] hw/ide/ahci: clear a stale error status when an NCQ command is queued Damien Le Moal
2026-11-27 19:12 ` [PATCH 04/13] hw/ide/ahci: complete unsupported NCQ commands Niklas Cassel
2026-11-30  5:16   ` Damien Le Moal
2026-11-27 19:12 ` [PATCH 05/13] hw/ide: add General Purpose Logging (GPL) feature set support Niklas Cassel
2026-11-30  5:20   ` Damien Le Moal
2026-11-27 19:12 ` [PATCH 06/13] hw/ide: add the IDENTIFY DEVICE data log Niklas Cassel
2026-11-30  5:23   ` Damien Le Moal
2026-11-27 19:12 ` [PATCH 07/13] hw/ide/ahci: add RECEIVE FPDMA QUEUED support Niklas Cassel
2026-11-30  5:25   ` Damien Le Moal
2026-11-27 19:12 ` [PATCH 08/13] hw/ide: add Sense Data Reporting feature set support Niklas Cassel
2026-11-30  5:30   ` Damien Le Moal
2026-11-27 19:12 ` [PATCH 09/13] hw/ide: implement the NCQ Command Error log (10h) Niklas Cassel
2026-11-30  5:34   ` Damien Le Moal
2026-11-27 19:12 ` [PATCH 10/13] tests/qtest/libqos: skip some checks for commands expected to fail Niklas Cassel
2026-11-27 19:12 ` [PATCH 11/13] hw/ide: report out-of-range accesses as ILLEGAL REQUEST Niklas Cassel
2026-11-30  5:35   ` Damien Le Moal
2026-11-27 19:12 ` [PATCH 12/13] hw/ide/ahci: check the LBA range of NCQ commands Niklas Cassel
2026-11-30  5:36   ` Damien Le Moal
2026-11-27 19:12 ` [PATCH 13/13] hw/ide: report ACS-6 compliance behind AHCI Niklas Cassel
2026-11-30  5:37   ` Damien Le Moal

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=20261127191250.3298004-4-cassel@kernel.org \
    --to=cassel@kernel.org \
    --cc=den@openvz.org \
    --cc=dlemoal@kernel.org \
    --cc=farosas@suse.de \
    --cc=jsnow@redhat.com \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.