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 02/13] hw/ide/ahci: stop processing commands after a fatal error
Date: Fri, 27 Nov 2026 20:12:37 +0100 [thread overview]
Message-ID: <20261127191250.3298004-3-cassel@kernel.org> (raw)
In-Reply-To: <20261127191250.3298004-1-cassel@kernel.org>
As per AHCI 1.3.1, 6.2.2, after a fatal error, the HBA should not
process any further commands until software clears PxCMD.ST.
This was previously missing in the QEMU model, which emulates both
both the drive and the HBA.
Previously, QEMU went on processing commands, and as a failed command
keeps its bit in PxCI, check_cmd() executed it again when the host issued
another command without restarting the port.
Add a new state that halts the port after a fatal error.
Linux, SeaBIOS and libqos already restart the port using PxCMD.ST after
an error.
Add a qtest that checks that a command issued after a failed WRITE DMA
EXT, without restarting the port, is not processed, and that the failed
write is not executed again.
Fixes: 1a16ce64fda1 ("hw/ide/ahci: PxCI should not get cleared when ERR_STAT is set")
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/ahci-internal.h | 4 +++
hw/ide/ahci.c | 39 ++++++++++++++++++++++++-
tests/qtest/ahci-test.c | 63 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 105 insertions(+), 1 deletion(-)
diff --git a/hw/ide/ahci-internal.h b/hw/ide/ahci-internal.h
index a318f36811..ca11a370c7 100644
--- a/hw/ide/ahci-internal.h
+++ b/hw/ide/ahci-internal.h
@@ -165,6 +165,9 @@ enum AHCIPortIRQ {
#define DEF_PORT_IRQ (PORT_IRQ_ERROR | PORT_IRQ_SG_DONE | \
PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS | \
PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
+/* The fatal errors, after which the HBA stops (AHCI 1.3.1 6.2.2) */
+#define PORT_IRQ_FATAL (PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_ERR | \
+ PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_ERR)
/* PORT_CMD bits */
#define PORT_CMD_ATAPI (1 << 24) /* Device is ATAPI */
@@ -318,6 +321,7 @@ struct AHCIDevice {
bool done_first_drq;
int32_t busy_slot;
bool init_d2h_sent;
+ bool halted; /* fatal error (PxIS.TFES), until PxCMD.ST is cleared */
AHCICmdHdr *cur_cmd;
NCQTransferState ncq_tfs[AHCI_MAX_CMDS];
MemReentrancyGuard mem_reentrancy_guard;
diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index 86f6297dc9..3b872c6cf1 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -208,6 +208,13 @@ static void ahci_trigger_irq(AHCIState *s, AHCIDevice *d,
irqstat & d->port_regs.irq_mask);
d->port_regs.irq_stat = irqstat;
+ /*
+ * A fatal error halts the port until PxCMD.ST is cleared
+ * (AHCI 1.3.1 6.2.2).
+ */
+ if (irq & PORT_IRQ_FATAL) {
+ d->halted = true;
+ }
ahci_check_irq(s);
}
@@ -301,6 +308,7 @@ static void ahci_port_write(AHCIState *s, int port, int offset, uint32_t val)
if ((pr->cmd & PORT_CMD_START) && !(val & PORT_CMD_START)) {
pr->scr_act = 0;
pr->cmd_issue = 0;
+ s->dev[port].halted = false;
}
/* Block any Read-only fields from being set;
@@ -565,7 +573,13 @@ static void check_cmd(AHCIState *s, int port)
uint8_t slot;
if ((pr->cmd & PORT_CMD_START) && pr->cmd_issue) {
- for (slot = 0; (slot < 32) && pr->cmd_issue; slot++) {
+ /*
+ * After a fatal error the HBA processes no commands until software
+ * clears PxCMD.ST (AHCI 1.3.1 6.2.2). A command can fail before
+ * handle_cmd() returns, so check before each slot.
+ */
+ for (slot = 0; (slot < 32) && pr->cmd_issue && !s->dev[port].halted;
+ slot++) {
if (pr->cmd_issue & (1U << slot)) {
handle_cmd(s, port, slot);
}
@@ -664,6 +678,7 @@ static void ahci_reset_port(AHCIState *s, int port, IDEResetKind kind)
pr->cmd_issue = 0;
d->busy_slot = -1;
d->init_d2h_sent = false;
+ d->halted = false;
ide_state = &s->dev[port].port.ifs[0];
if (!ide_state->blk) {
@@ -1738,6 +1753,24 @@ static const VMStateDescription vmstate_ncq_tfs = {
},
};
+static bool ahci_device_halted_needed(void *opaque)
+{
+ AHCIDevice *ad = opaque;
+
+ return ad->halted;
+}
+
+static const VMStateDescription vmstate_ahci_device_halted = {
+ .name = "ahci port/halted",
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .needed = ahci_device_halted_needed,
+ .fields = (const VMStateField[]) {
+ VMSTATE_BOOL(halted, AHCIDevice),
+ VMSTATE_END_OF_LIST()
+ },
+};
+
static const VMStateDescription vmstate_ahci_device = {
.name = "ahci port",
.version_id = 1,
@@ -1767,6 +1800,10 @@ static const VMStateDescription vmstate_ahci_device = {
1, vmstate_ncq_tfs, NCQTransferState),
VMSTATE_END_OF_LIST()
},
+ .subsections = (const VMStateDescription * const []) {
+ &vmstate_ahci_device_halted,
+ NULL
+ },
};
static int ahci_state_post_load(void *opaque, int version_id)
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index 291f29e762..6acb2327e3 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1169,6 +1169,68 @@ static void test_flush_retry(void)
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
+ * command again either.
+ */
+static void test_halt_after_error(void)
+{
+ g_autofree unsigned char *before = g_malloc(512);
+ g_autofree unsigned char *tx = g_malloc(512);
+ g_autofree unsigned char *rx = g_malloc(512);
+ AHCIQState *ahci;
+ AHCICommand *cmd;
+ uint32_t pending;
+ uint64_t ptr;
+ unsigned px;
+
+ 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);
+ ahci_io(ahci, px, CMD_READ_DMA_EXT, before, 512, 0);
+ memset(tx, before[0] ^ 0xff, 512);
+
+ /* A WRITE DMA EXT that fails with the injected I/O error */
+ ptr = ahci_alloc(ahci, 512);
+ qtest_bufwrite(ahci->parent->qts, ptr, tx, 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);
+ /* Not ahci_command_verify(), which would restart the port */
+ g_assert_cmphex(ahci_px_rreg(ahci, px, AHCI_PX_IS) & AHCI_PX_IS_TFES,
+ !=, 0);
+ ahci_command_free(cmd);
+
+ /* A command issued without restarting the port is not processed */
+ cmd = ahci_command_create(CMD_READ_DMA_EXT);
+ ahci_command_set_buffer(cmd, ptr);
+ ahci_command_set_size(cmd, 512);
+ ahci_command_commit(ahci, cmd, px);
+ ahci_command_issue_async(ahci, cmd);
+ /* Nothing to poll for: give the command time to be processed */
+ g_usleep(50 * 1000);
+ pending = ahci_px_rreg(ahci, px, AHCI_PX_CI) &
+ (1u << ahci_command_slot(cmd));
+ ahci_command_free(cmd);
+ ahci_free(ahci, ptr);
+
+ /* Restarting the port drops it; the sector still has its old data */
+ 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);
+ ahci_io(ahci, px, CMD_READ_DMA_EXT, rx, 512, 0);
+ g_assert_cmphex(memcmp(rx, before, 512), ==, 0);
+ g_assert_cmphex(pending, !=, 0);
+
+ ahci_shutdown(ahci);
+}
+
/**
* Basic sanity test to boot a machine, find an AHCI device, and shutdown.
*/
@@ -2404,6 +2466,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/dma/halt_after_error", test_halt_after_error);
qtest_add_func("/ahci/flush/migrate", test_flush_migrate);
qtest_add_func("/ahci/migrate/sanity", test_migrate_sanity);
--
2.56.1
next prev 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 ` Niklas Cassel [this message]
2026-11-30 5:13 ` [PATCH 02/13] hw/ide/ahci: stop processing commands after a fatal error Damien Le Moal
2026-11-27 19:12 ` [PATCH 03/13] hw/ide/ahci: clear a stale error status when an NCQ command is queued Niklas Cassel
2026-11-30 5:15 ` 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-3-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.