* [PATCH 01/13] hw/ide: report the SMART state in IDENTIFY DEVICE
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
@ 2026-11-27 19:12 ` 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
` (11 subsequent siblings)
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
Currently, IDENTIFY DEVICE word 85 bit 0 always reports the SMART feature
set as enabled, regardless if it has been disabled using SET FEATURES or
SMART DISABLE OPERATIONS.
Add a helper to build word85, such that the SMART feature set and the
write cache is reported in IDENTIFY DEVICE correctly.
Also, just like the write cache state is re-initialized after migration,
re-initialize the SMART state after migration.
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/core.c | 26 ++++++++++++++------------
1 file changed, 14 insertions(+), 12 deletions(-)
diff --git a/hw/ide/core.c b/hw/ide/core.c
index 06e6bb6067..7e3f882969 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -138,6 +138,14 @@ static void ide_identify_size(IDEState *s)
put_le16(p + 103, s->nb_sectors >> 48);
}
+/* IDENTIFY DEVICE word 85: the commands and feature sets that are enabled */
+static uint16_t ide_identify_word85(IDEState *s)
+{
+ /* 14 = NOP supported, 5=WCACHE enabled, 0=SMART feature set enabled */
+ return (1 << 14) | (blk_enable_write_cache(s->blk) ? 1 << 5 : 0) |
+ (s->smart_enabled ? 1 : 0);
+}
+
static void ide_identify(IDEState *s)
{
uint16_t *p;
@@ -204,12 +212,7 @@ static void ide_identify(IDEState *s)
} else {
put_le16(p + 84, (1 << 14) | 0);
}
- /* 14 = NOP supported, 5=WCACHE enabled, 0=SMART feature set enabled */
- if (blk_enable_write_cache(s->blk)) {
- put_le16(p + 85, (1 << 14) | (1 << 5) | 1);
- } else {
- put_le16(p + 85, (1 << 14) | 1);
- }
+ put_le16(p + 85, ide_identify_word85(s));
/* 13=flush_cache_ext,12=flush_cache,10=lba48 */
put_le16(p + 86, (1 << 13) | (1 <<12) | (1 << 10));
/* 14=set to 1, 8=has WWN, 1=SMART self test, 0=SMART error logging */
@@ -1714,12 +1717,12 @@ static bool cmd_set_features(IDEState *s, uint8_t cmd)
case 0x02: /* write cache enable */
blk_set_enable_write_cache(s->blk, true);
identify_data = (uint16_t *)s->identify_data;
- put_le16(identify_data + 85, (1 << 14) | (1 << 5) | 1);
+ put_le16(identify_data + 85, ide_identify_word85(s));
return true;
case 0x82: /* write cache disable */
blk_set_enable_write_cache(s->blk, false);
identify_data = (uint16_t *)s->identify_data;
- put_le16(identify_data + 85, (1 << 14) | 1);
+ put_le16(identify_data + 85, ide_identify_word85(s));
ide_flush_cache(s);
return false;
case 0xcc: /* reverting to power-on defaults enable */
@@ -1942,11 +1945,9 @@ static bool cmd_smart(IDEState *s, uint8_t cmd)
switch (s->feature) {
case SMART_DISABLE:
- s->smart_enabled = 0;
- return true;
-
case SMART_ENABLE:
- s->smart_enabled = 1;
+ s->smart_enabled = s->feature == SMART_ENABLE;
+ put_le16((uint16_t *)s->identify_data + 85, ide_identify_word85(s));
return true;
case SMART_ATTR_AUTOSAVE:
@@ -2901,6 +2902,7 @@ static int ide_drive_post_load(void *opaque, int version_id)
if (s->blk && s->identify_set) {
blk_set_enable_write_cache(s->blk, !!(s->identify_data[85] & (1 << 5)));
+ s->smart_enabled = s->identify_data[85] & 1;
}
return 0;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 01/13] hw/ide: report the SMART state in IDENTIFY DEVICE
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:10 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev; +Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> Currently, IDENTIFY DEVICE word 85 bit 0 always reports the SMART feature
> set as enabled, regardless if it has been disabled using SET FEATURES or
> SMART DISABLE OPERATIONS.
>
> Add a helper to build word85, such that the SMART feature set and the
> write cache is reported in IDENTIFY DEVICE correctly.
>
> Also, just like the write cache state is re-initialized after migration,
> re-initialize the SMART state after migration.
>
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
> ---
> hw/ide/core.c | 26 ++++++++++++++------------
> 1 file changed, 14 insertions(+), 12 deletions(-)
>
> diff --git a/hw/ide/core.c b/hw/ide/core.c
> index 06e6bb6067..7e3f882969 100644
> --- a/hw/ide/core.c
> +++ b/hw/ide/core.c
> @@ -138,6 +138,14 @@ static void ide_identify_size(IDEState *s)
> put_le16(p + 103, s->nb_sectors >> 48);
> }
>
> +/* IDENTIFY DEVICE word 85: the commands and feature sets that are enabled */
> +static uint16_t ide_identify_word85(IDEState *s)
> +{
> + /* 14 = NOP supported, 5=WCACHE enabled, 0=SMART feature set enabled */
> + return (1 << 14) | (blk_enable_write_cache(s->blk) ? 1 << 5 : 0) |
> + (s->smart_enabled ? 1 : 0);
Can we make this a bit more readable and easier to extend?
Something like:
/* IDENTIFY DEVICE word 85: the commands and feature sets that are enabled */
static uint16_t ide_identify_word85(IDEState *s)
{
uint16_t w = 0;
/* 0=SMART feature set enabled */
if ((s->smart_enabled)
w |= 1 << 0;
/* 5=WCACHE enabled */
if (blk_enable_write_cache(s->blk))
w |= 1 << 5;
/* 14 = NOP supported. */
w |= 1 << 14;
return w;
}
Other than that,
Looks good to me.
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 02/13] hw/ide/ahci: stop processing commands after a fatal error
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-27 19:12 ` Niklas Cassel
2026-11-30 5:13 ` 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
` (10 subsequent siblings)
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
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
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 02/13] hw/ide/ahci: stop processing commands after a fatal error
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:13 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev, Fabiano Rosas,
Laurent Vivier, Paolo Bonzini
Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> 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.
double "both".
>
> 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>
Other than the nit above, looks OK to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 03/13] hw/ide/ahci: clear a stale error status when an NCQ command is queued
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-27 19:12 ` [PATCH 02/13] hw/ide/ahci: stop processing commands after a fatal error Niklas Cassel
@ 2026-11-27 19:12 ` 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
` (9 subsequent siblings)
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
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
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 03/13] hw/ide/ahci: clear a stale error status when an NCQ command is queued
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:15 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev, Fabiano Rosas,
Laurent Vivier, Paolo Bonzini
Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> 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>
Looks good to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 04/13] hw/ide/ahci: complete unsupported NCQ commands
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
` (2 preceding siblings ...)
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-27 19:12 ` 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
` (8 subsequent siblings)
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
execute_ncq_command() aborts an NCQ command that it does not implement
(e.g. SEND FPDMA QUEUED) with ncq_err(), but does not call ncq_finish().
This means that no Set Device Bits FIS is sent, so the command never
completes. Linux will eventually notice a timeout and reset the port,
however, the the proper thing to do is to send a SDB FIS after calling
ncq_err() such that the host gets an IRQ.
Fixes: 72a065dbb13d ("ahci: add is_ncq predicate helper")
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/ahci.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index e3ae46e869..e14b298ea6 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -1127,6 +1127,7 @@ static void execute_ncq_command(NCQTransferState *ncq_tfs)
trace_execute_ncq_command_unsup(ad->hba, port,
ncq_tfs->tag, ncq_tfs->cmd);
ncq_err(ncq_tfs);
+ ncq_finish(ncq_tfs);
}
}
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 04/13] hw/ide/ahci: complete unsupported NCQ commands
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:16 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev; +Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> execute_ncq_command() aborts an NCQ command that it does not implement
> (e.g. SEND FPDMA QUEUED) with ncq_err(), but does not call ncq_finish().
>
> This means that no Set Device Bits FIS is sent, so the command never
> completes. Linux will eventually notice a timeout and reset the port,
> however, the the proper thing to do is to send a SDB FIS after calling
double "the"
> ncq_err() such that the host gets an IRQ.
>
> Fixes: 72a065dbb13d ("ahci: add is_ncq predicate helper")
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
Looks good to me, modulo the above nit.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 05/13] hw/ide: add General Purpose Logging (GPL) feature set support
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
` (3 preceding siblings ...)
2026-11-27 19:12 ` [PATCH 04/13] hw/ide/ahci: complete unsupported NCQ commands Niklas Cassel
@ 2026-11-27 19:12 ` 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
` (7 subsequent siblings)
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
Implement the General Purpose Logging feature set (ACS-6 4.11).
Commands READ LOG EXT (PIO) and READ LOG DMA EXT (DMA) are implemented.
WRITE LOG EXT and WRITE LOG DMA EXT are not implemented.
(E.g. Linux never writes to any log.)
In this base commit, the only log implemented is the General Purpose Log
Directory (00h), which lists no other log yet.
As with NCQ, only a disk behind AHCI exposes this feature set.
On other controllers, a disk neither advertises it nor accepts the new
commands, i.e. they work as before. The main reason to add GPL support
is to add the NCQ Command Error log, the SATA NCQ Send and Receive log,
the Serial ATA page of the IDENTIFY DEVICE data log, and RECEIVE FPDMA
QUEUED. Neither of these features/logs makes sense for non-NCQ devices.
Add qtests that read the log directory over AHCI, with PIO and DMA, and
check that an IDE disk does not advertise the feature set.
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/core.c | 134 ++++++++++++++++++++++++++++++++++----
hw/ide/ide-internal.h | 10 ++-
hw/ide/trace-events | 1 +
include/hw/ide/ide-dev.h | 1 +
tests/qtest/ahci-test.c | 51 +++++++++++++++
tests/qtest/ide-test.c | 3 +
tests/qtest/libqos/ahci.c | 4 ++
tests/qtest/libqos/ahci.h | 2 +
8 files changed, 192 insertions(+), 14 deletions(-)
diff --git a/hw/ide/core.c b/hw/ide/core.c
index 7e3f882969..d09b0fa736 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -68,7 +68,8 @@ const char *IDE_DMA_CMD_lookup[IDE_DMA__COUNT] = {
[IDE_DMA_READ] = "DMA READ",
[IDE_DMA_WRITE] = "DMA WRITE",
[IDE_DMA_TRIM] = "DMA TRIM",
- [IDE_DMA_ATAPI] = "DMA ATAPI"
+ [IDE_DMA_ATAPI] = "DMA ATAPI",
+ [IDE_DMA_READ_LOG] = "DMA READ LOG"
};
static const char *IDE_DMA_CMD_str(enum ide_dma_cmd enval)
@@ -150,6 +151,7 @@ static void ide_identify(IDEState *s)
{
uint16_t *p;
IDEDevice *dev = s->unit ? s->bus->slave : s->bus->master;
+ uint16_t gpl = s->ncq_queues ? 1 << 5 : 0;
p = (uint16_t *)s->identify_data;
if (s->identify_set) {
@@ -196,8 +198,8 @@ static void ide_identify(IDEState *s)
if (s->ncq_queues) {
put_le16(p + 75, s->ncq_queues - 1);
- /* NCQ supported */
- put_le16(p + 76, (1 << 8));
+ /* 15=READ LOG DMA EXT equivalent to READ LOG EXT, 8=NCQ supported */
+ put_le16(p + 76, (1 << 15) | (1 << 8));
}
put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
@@ -206,20 +208,21 @@ static void ide_identify(IDEState *s)
put_le16(p + 82, (1 << 14) | (1 << 5) | 1);
/* 13=flush_cache_ext,12=flush_cache,10=lba48 */
put_le16(p + 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
- /* 14=set to 1, 8=has WWN, 1=SMART self test, 0=SMART error logging */
+ /* 14=set to 1, 8=has WWN, 5=GPL, 1=SMART self test, 0=SMART error log */
if (s->wwn) {
- put_le16(p + 84, (1 << 14) | (1 << 8) | 0);
+ put_le16(p + 84, (1 << 14) | (1 << 8) | gpl | 0);
} else {
- put_le16(p + 84, (1 << 14) | 0);
+ put_le16(p + 84, (1 << 14) | gpl | 0);
}
put_le16(p + 85, ide_identify_word85(s));
- /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
- put_le16(p + 86, (1 << 13) | (1 <<12) | (1 << 10));
- /* 14=set to 1, 8=has WWN, 1=SMART self test, 0=SMART error logging */
+ /* 15=words 119-120 valid,13=flush_cache_ext,12=flush_cache,10=lba48 */
+ put_le16(p + 86, (s->ncq_queues ? 1 << 15 : 0) |
+ (1 << 13) | (1 << 12) | (1 << 10));
+ /* 14=set to 1, 8=has WWN, 5=GPL, 1=SMART self test, 0=SMART error log */
if (s->wwn) {
- put_le16(p + 87, (1 << 14) | (1 << 8) | 0);
+ put_le16(p + 87, (1 << 14) | (1 << 8) | gpl | 0);
} else {
- put_le16(p + 87, (1 << 14) | 0);
+ put_le16(p + 87, (1 << 14) | gpl | 0);
}
put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
put_le16(p + 93, 1 | (1 << 14) | 0x2000);
@@ -237,6 +240,15 @@ static void ide_identify(IDEState *s)
put_le16(p + 110, s->wwn >> 16);
put_le16(p + 111, s->wwn);
}
+ if (s->ncq_queues) {
+ /*
+ * 14=set to 1,
+ * 3=READ LOG DMA EXT/WRITE LOG DMA EXT (GPL DMA) supported
+ */
+ put_le16(p + 119, (1 << 14) | (1 << 3));
+ /* 14=set to 1, 3=GPL DMA enabled */
+ put_le16(p + 120, (1 << 14) | (1 << 3));
+ }
if (dev && dev->conf.discard_granularity) {
put_le16(p + 169, 1); /* TRIM support */
}
@@ -1034,6 +1046,20 @@ void ide_start_dma(IDEState *s, BlockCompletionFunc *cb)
}
}
+static void ide_buffer_dma_cb(void *opaque, int ret)
+{
+ IDEState *s = opaque;
+
+ /*
+ * The data was generated synchronously into the I/O buffer before the
+ * transfer was started, so there is never a block I/O error to handle.
+ */
+ s->bus->dma->ops->rw_buf(s->bus->dma, 1);
+ s->status = READY_STAT | SEEK_STAT;
+ ide_bus_set_irq(s->bus);
+ ide_set_inactive(s, false);
+}
+
static void ide_sector_write(IDEState *s);
static void ide_sector_write_timer_cb(void *opaque)
@@ -1626,6 +1652,86 @@ static bool cmd_write_dma(IDEState *s, uint8_t cmd)
return false;
}
+/*
+ * Fill the I/O buffer with the General Purpose Logging data requested by a
+ * READ LOG EXT / READ LOG DMA EXT command and return the number of bytes to
+ * transfer, or a negative value if the command must be aborted (ACS-6 7.25).
+ */
+static int ide_read_log(IDEState *s)
+{
+ uint8_t log_address = s->sector;
+ uint16_t page = s->lcyl | ((uint16_t)s->hob_lcyl << 8);
+ uint32_t count = s->nsector; /* LOG PAGE COUNT, in 512-byte pages */
+ uint16_t log_size;
+
+ switch (log_address) {
+ case IDE_GPL_LOG_DIRECTORY:
+ log_size = 1;
+ break;
+ default:
+ /* Log (or its associated feature set) not supported (ACS-6 7.25.5). */
+ return -1;
+ }
+
+ /*
+ * A LOG PAGE COUNT of zero is invalid, and the requested pages must lie
+ * within the log as reported by the General Purpose Log Directory
+ * (ACS-6 7.25.5). The callers turn a zero COUNT into 65536, which the
+ * range check rejects; checking for zero guards a caller that does not.
+ */
+ if (count == 0 || page >= log_size || count > log_size - page) {
+ return -1;
+ }
+
+ switch (log_address) {
+ case IDE_GPL_LOG_DIRECTORY:
+ /* General Purpose Log Directory (ACS-6 9.2) */
+ memset(s->io_buffer, 0, count * 512);
+ /* word 0: General Purpose Logging Version */
+ put_le16((uint16_t *)s->io_buffer, 0x0001);
+ break;
+ }
+
+ return count * 512;
+}
+
+/* READ LOG EXT (PIO) and READ LOG DMA EXT, both 48-bit GPL commands */
+static bool cmd_read_log(IDEState *s, uint8_t cmd)
+{
+ bool dma = (cmd == WIN_READ_LOG_DMA_EXT);
+ int size;
+
+ if (!s->blk) {
+ ide_abort_command(s);
+ return true;
+ }
+
+ /* These commands are always 48-bit; COUNT holds the LOG PAGE COUNT. */
+ ide_cmd_lba48_transform(s, 1);
+
+ trace_ide_read_log(s, s->sector, s->lcyl | ((uint16_t)s->hob_lcyl << 8),
+ s->nsector, dma ? "dma" : "pio");
+
+ size = ide_read_log(s);
+ if (size < 0) {
+ ide_abort_command(s);
+ return true;
+ }
+
+ if (dma) {
+ s->dma_cmd = IDE_DMA_READ_LOG;
+ s->io_buffer_index = 0;
+ s->io_buffer_size = size;
+ ide_start_dma(s, ide_buffer_dma_cb);
+ return false;
+ }
+
+ s->status = READY_STAT | SEEK_STAT;
+ ide_transfer_start(s, s->io_buffer, size, ide_transfer_stop);
+ ide_bus_set_irq(s->bus);
+ return false;
+}
+
static bool cmd_flush_cache(IDEState *s, uint8_t cmd)
{
ide_flush_cache(s);
@@ -2107,6 +2213,7 @@ abort_cmd:
/* Set the Disk Seek Completed status bit during completion */
#define SET_DSC (1u << 8)
+#define AHCI_ONLY (1u << 9)
/* See ACS-2 T13/2015-D Table B.2 Command codes */
static const struct {
@@ -2125,6 +2232,8 @@ static const struct {
[WIN_READDMA_EXT] = { cmd_read_dma, HD_CFA_OK },
[WIN_READ_NATIVE_MAX_EXT] = { cmd_read_native_max, HD_CFA_OK | SET_DSC },
[WIN_MULTREAD_EXT] = { cmd_read_multiple, HD_CFA_OK },
+ [WIN_READ_LOG_EXT] = { cmd_read_log, HD_OK | AHCI_ONLY },
+ [WIN_READ_LOG_DMA_EXT] = { cmd_read_log, HD_OK | AHCI_ONLY },
[WIN_WRITE] = { cmd_write_pio, HD_CFA_OK },
[WIN_WRITE_ONCE] = { cmd_write_pio, HD_CFA_OK },
[WIN_WRITE_EXT] = { cmd_write_pio, HD_CFA_OK },
@@ -2176,7 +2285,8 @@ static const struct {
static bool ide_cmd_permitted(IDEState *s, uint32_t cmd)
{
return cmd < ARRAY_SIZE(ide_cmd_table)
- && (ide_cmd_table[cmd].flags & (1u << s->drive_kind));
+ && (ide_cmd_table[cmd].flags & (1u << s->drive_kind))
+ && (s->ncq_queues || !(ide_cmd_table[cmd].flags & AHCI_ONLY));
}
void ide_bus_exec_cmd(IDEBus *bus, uint32_t val)
diff --git a/hw/ide/ide-internal.h b/hw/ide/ide-internal.h
index 094772209d..389e15049e 100644
--- a/hw/ide/ide-internal.h
+++ b/hw/ide/ide-internal.h
@@ -81,7 +81,7 @@
/* READ STREAM DMA EXT 0x2A */
/* READ STREAM EXT 0x2B */
/* reserved 0x2C..0x2E */
-/* READ LOG EXT 0x2F */
+#define WIN_READ_LOG_EXT 0x2F /* 48-Bit */
#define WIN_WRITE 0x30 /* 28-Bit */
#define WIN_WRITE_ONCE 0x31 /* 28-Bit w/o retries, obsolete since ATA5 */
/* obsolete since ATA4 0x32..0x33 */
@@ -104,7 +104,7 @@
/* reserved 0x43..0x44 */
/* WRITE UNCORRECTABLE EXT 0x45 */
/* reserved 0x46 */
-/* READ LOG DMA EXT 0x47 */
+#define WIN_READ_LOG_DMA_EXT 0x47 /* 48-Bit */
/* reserved 0x48..0x4F */
/* obsolete since ATA4 0x50 */
/* CONFIGURE STREAM 0x51 */
@@ -207,6 +207,12 @@
/* feature values for Data Set Management */
#define DSM_TRIM 0x01
+/*
+ * Log addresses for the General Purpose Logging feature set, accessed via
+ * READ LOG EXT / READ LOG DMA EXT (see ACS-6 9.1 table 222).
+ */
+#define IDE_GPL_LOG_DIRECTORY 0x00 /* General Purpose Log Directory */
+
#if (IDE_DMA_BUF_SECTORS < MAX_MULT_SECTORS)
#error "IDE_DMA_BUF_SECTORS must be bigger or equal to MAX_MULT_SECTORS"
#endif
diff --git a/hw/ide/trace-events b/hw/ide/trace-events
index c44eec6c58..102f39436c 100644
--- a/hw/ide/trace-events
+++ b/hw/ide/trace-events
@@ -17,6 +17,7 @@ ide_cancel_dma_sync_buffered(void *fn, void *req) "invoking cb %p of buffered re
ide_cancel_dma_sync_remaining(void) "draining all remaining requests"
ide_sector_read(int64_t sector_num, int nsectors) "sector=%"PRId64" nsectors=%d"
ide_sector_write(int64_t sector_num, int nsectors) "sector=%"PRId64" nsectors=%d"
+ide_read_log(void *s, uint8_t log, uint16_t page, uint32_t count, const char *kind) "IDEState %p; log=0x%02x page=%u count=%u %s"
ide_reset(void *s) "IDEstate %p"
ide_bus_reset_aio(void) "aio_cancel"
ide_dma_cb(void *s, int64_t sector_num, int n, const char *dma) "IDEState %p; sector_num=%"PRId64" n=%d cmd=%s"
diff --git a/include/hw/ide/ide-dev.h b/include/hw/ide/ide-dev.h
index f5eaaf68e6..258b216b44 100644
--- a/include/hw/ide/ide-dev.h
+++ b/include/hw/ide/ide-dev.h
@@ -47,6 +47,7 @@ enum ide_dma_cmd {
IDE_DMA_WRITE,
IDE_DMA_TRIM,
IDE_DMA_ATAPI,
+ IDE_DMA_READ_LOG,
IDE_DMA__COUNT
};
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index 2b668ea708..8ff30616d5 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1055,6 +1055,55 @@ static void test_specify(void)
ahci_shutdown(ahci);
}
+/*
+ * Exercise the General Purpose Logging feature set via READ LOG EXT (PIO) or
+ * READ LOG DMA EXT. Reads are performed one log page at a time so the same
+ * helper can drive both the PIO and DMA variants.
+ */
+static void ahci_test_read_log(AHCIQState *ahci, uint8_t read_log_cmd)
+{
+ uint16_t id[256];
+ uint16_t dir[256];
+ unsigned px;
+
+ px = ahci_port_select(ahci);
+ ahci_port_clear(ahci, px);
+
+ /*
+ * The device must advertise the GPL feature set (IDENTIFY word 84/87) and
+ * READ LOG DMA EXT support (word 86 bit 15 makes word 119 valid; word 119
+ * bit 3 = GPL DMA supported).
+ */
+ ahci_io(ahci, px, CMD_IDENTIFY, &id, 512, 0);
+ g_assert_cmphex(le16_to_cpu(id[84]) & (1 << 5), ==, (1 << 5));
+ g_assert_cmphex(le16_to_cpu(id[87]) & (1 << 5), ==, (1 << 5));
+ g_assert_cmphex(le16_to_cpu(id[86]) & (1 << 15), ==, (1 << 15));
+ g_assert_cmphex(le16_to_cpu(id[119]) & (1 << 3), ==, (1 << 3));
+
+ /* General Purpose Log Directory (log address 00h, page 0). */
+ ahci_io(ahci, px, read_log_cmd, &dir, 512, 0x00);
+ /* word 0: General Purpose Logging Version */
+ g_assert_cmphex(le16_to_cpu(dir[0]), ==, 0x0001);
+}
+
+static void test_read_log_dma(void)
+{
+ AHCIQState *ahci;
+
+ ahci = ahci_boot_and_enable(NULL);
+ ahci_test_read_log(ahci, CMD_READ_LOG_DMA_EXT);
+ ahci_shutdown(ahci);
+}
+
+static void test_read_log_pio(void)
+{
+ AHCIQState *ahci;
+
+ ahci = ahci_boot_and_enable(NULL);
+ ahci_test_read_log(ahci, CMD_READ_LOG_EXT);
+ ahci_shutdown(ahci);
+}
+
/**
* Fragmented DMA test: Perform a standard 4K DMA read/write
* test, but make sure the physical regions are fragmented to
@@ -2511,6 +2560,8 @@ 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/io/dma/read_log", test_read_log_dma);
+ qtest_add_func("/ahci/io/pio/read_log", test_read_log_pio);
for (i = MODE_BEGIN; i < NUM_MODES; i++) {
for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
diff --git a/tests/qtest/ide-test.c b/tests/qtest/ide-test.c
index cf109b0799..d82efdfdda 100644
--- a/tests/qtest/ide-test.c
+++ b/tests/qtest/ide-test.c
@@ -746,6 +746,9 @@ static void test_identify(void)
/* Write cache enabled bit */
assert_bit_set(buf[85], 0x20);
+ assert_bit_clear(buf[84], 0x20);
+ assert_bit_clear(buf[86], 0x8000);
+ g_assert_cmphex(buf[119], ==, 0);
ide_test_quit(qts);
free_pci_device(dev);
diff --git a/tests/qtest/libqos/ahci.c b/tests/qtest/libqos/ahci.c
index 53a98bd6cb..e64fcade36 100644
--- a/tests/qtest/libqos/ahci.c
+++ b/tests/qtest/libqos/ahci.c
@@ -67,6 +67,10 @@ AHCICommandProp ahci_command_properties[] = {
.lba48 = true, .write = true },
{ .cmd = CMD_IDENTIFY, .data = true, .pio = true,
.size = 512, .read = true },
+ { .cmd = CMD_READ_LOG_EXT, .data = true, .pio = true,
+ .lba48 = true, .read = true },
+ { .cmd = CMD_READ_LOG_DMA_EXT, .data = true, .dma = true,
+ .lba48 = true, .read = true },
{ .cmd = READ_FPDMA_QUEUED, .data = true, .dma = true,
.lba48 = true, .read = true, .ncq = true },
{ .cmd = WRITE_FPDMA_QUEUED, .data = true, .dma = true,
diff --git a/tests/qtest/libqos/ahci.h b/tests/qtest/libqos/ahci.h
index f098c91b47..9c4195eee9 100644
--- a/tests/qtest/libqos/ahci.h
+++ b/tests/qtest/libqos/ahci.h
@@ -280,6 +280,8 @@ enum {
CMD_FLUSH_CACHE = 0xE7,
CMD_INIT_DP = 0x91, /* INITIALIZE DEVICE PARAMETERS */
CMD_IDENTIFY = 0xEC,
+ CMD_READ_LOG_EXT = 0x2F,
+ CMD_READ_LOG_DMA_EXT = 0x47,
CMD_PACKET = 0xA0,
CMD_PACKET_ID = 0xA1,
/* NCQ */
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 05/13] hw/ide: add General Purpose Logging (GPL) feature set support
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:20 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev, Fabiano Rosas,
Laurent Vivier, Paolo Bonzini
Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> Implement the General Purpose Logging feature set (ACS-6 4.11).
>
> Commands READ LOG EXT (PIO) and READ LOG DMA EXT (DMA) are implemented.
> WRITE LOG EXT and WRITE LOG DMA EXT are not implemented.
> (E.g. Linux never writes to any log.)
>
> In this base commit, the only log implemented is the General Purpose Log
> Directory (00h), which lists no other log yet.
>
> As with NCQ, only a disk behind AHCI exposes this feature set.
> On other controllers, a disk neither advertises it nor accepts the new
> commands, i.e. they work as before. The main reason to add GPL support
> is to add the NCQ Command Error log, the SATA NCQ Send and Receive log,
> the Serial ATA page of the IDENTIFY DEVICE data log, and RECEIVE FPDMA
> QUEUED. Neither of these features/logs makes sense for non-NCQ devices.
>
> Add qtests that read the log directory over AHCI, with PIO and DMA, and
> check that an IDE disk does not advertise the feature set.
>
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
Looks good to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 06/13] hw/ide: add the IDENTIFY DEVICE data log
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
` (4 preceding siblings ...)
2026-11-27 19:12 ` [PATCH 05/13] hw/ide: add General Purpose Logging (GPL) feature set support Niklas Cassel
@ 2026-11-27 19:12 ` 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
` (6 subsequent siblings)
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
Add the IDENTIFY DEVICE data log (30h).
The IDENTIFY DEVICE data log is a mandatory log in the GPL feature set.
Implement it with pages 00h to 06h and 08h, which are all the pages that
ACS-6 9.11.1 makes mandatory for a SATA device, and list it in the General
Purpose Log Directory.
The Supported Capabilities (03h), Current Settings (04h) and Serial ATA
(08h) pages copy their bits from the IDENTIFY DEVICE data, so that they
cannot disagree with it.
Extend the read log qtests to check the pages, and to read two pages
with one READ LOG DMA EXT.
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/core.c | 195 ++++++++++++++++++++++++++++++++++++++++
hw/ide/ide-internal.h | 9 ++
tests/qtest/ahci-test.c | 70 +++++++++++++++
3 files changed, 274 insertions(+)
diff --git a/hw/ide/core.c b/hw/ide/core.c
index d09b0fa736..1304202ea2 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -1652,6 +1652,192 @@ static bool cmd_write_dma(IDEState *s, uint8_t cmd)
return false;
}
+/*
+ * An IDENTIFY DEVICE data bit, and the bit of an IDENTIFY DEVICE data log QWord
+ * that holds a copy of it.
+ */
+typedef struct IDEIdentifyBit {
+ uint8_t word;
+ uint8_t bit;
+ uint8_t qword_bit;
+} IDEIdentifyBit;
+
+/* The Supported Capabilities QWord of page 03h, terminated by a zero word */
+static const IDEIdentifyBit ide_supported_capabilities[] = {
+ { 69, 14, 39 }, /* DRAT SUPPORTED */
+ { 69, 5, 31 }, /* RZAT SUPPORTED */
+ { 82, 14, 29 }, /* NOP SUPPORTED */
+ { 82, 5, 24 }, /* VOLATILE WRITE CACHE SUPPORTED */
+ { 82, 0, 23 }, /* SMART */
+ { 83, 13, 22 }, /* FLUSH CACHE EXT SUPPORTED */
+ { 83, 10, 20 }, /* 48-BIT SUPPORTED */
+ { 84, 6, 12 }, /* WRITE FUA EXT SUPPORTED */
+ { 84, 5, 11 }, /* GPL SUPPORTED */
+ { 84, 1, 8 }, /* SMART SELF-TEST SUPPORTED */
+ { 84, 0, 7 }, /* SMART ERROR LOGGING SUPPORTED */
+ { 119, 6, 5 }, /* SENSE DATA SUPPORTED */
+ { 119, 3, 2 }, /* GPL DMA SUPPORTED */
+ { 0 }
+};
+
+/* The SATA Capabilities QWord of page 08h, terminated by a zero word */
+static const IDEIdentifyBit ide_sata_capabilities[] = {
+ { 76, 1, 0 }, /* SATA GEN1 SIGNALING SPEED SUPPORTED */
+ { 76, 2, 1 }, /* SATA GEN2 SIGNALING SPEED SUPPORTED */
+ { 76, 3, 2 }, /* SATA GEN3 SIGNALING SPEED SUPPORTED */
+ { 76, 8, 7 }, /* NCQ FEATURE SET SUPPORTED */
+ { 76, 9, 8 }, /* RECEIPT OF HOST INITIATED POWER MANAGEMENT ... */
+ { 76, 10, 9 }, /* SATA PHY EVENT COUNTERS LOG SUPPORTED */
+ { 76, 11, 10 }, /* UNLOAD WHILE NCQ COMMANDS ARE OUTSTANDING ... */
+ { 76, 12, 11 }, /* NCQ PRIORITY INFORMATION SUPPORTED */
+ { 76, 13, 12 }, /* HOST AUTOMATIC PARTIAL TO SLUMBER ... */
+ { 76, 14, 13 }, /* DEVICE AUTOMATIC PARTIAL TO SLUMBER ... */
+ { 76, 15, 14 }, /* READ LOG DMA EXT AS EQUIVALENT TO READ LOG EXT ... */
+ { 77, 4, 15 }, /* NCQ STREAMING SUPPORTED */
+ { 77, 5, 16 }, /* NCQ QUEUE MANAGEMENT COMMAND SUPPORTED */
+ { 77, 6, 17 }, /* SEND AND RECEIVE QUEUED COMMANDS SUPPORTED */
+ { 78, 1, 18 }, /* NONZERO BUFFER OFFSETS SUPPORTED */
+ { 78, 2, 19 }, /* DMA SETUP AUTO-ACTIVATION SUPPORTED */
+ { 78, 3, 20 }, /* DEVICE INITIATED POWER MANAGEMENT SUPPORTED */
+ { 78, 4, 21 }, /* IN-ORDER DATA DELIVERY SUPPORTED */
+ { 78, 5, 22 }, /* HARDWARE FEATURE CONTROL SUPPORTED */
+ { 78, 6, 23 }, /* SOFTWARE SETTINGS PRESERVATION SUPPORTED */
+ { 78, 7, 24 }, /* NCQ AUTOSENSE SUPPORTED */
+ { 78, 8, 25 }, /* DEVICE SLEEP SUPPORTED */
+ { 0 }
+};
+
+/* The Current Settings QWord of page 04h, terminated by a zero word */
+static const IDEIdentifyBit ide_current_settings[] = {
+ { 85, 5, 13 }, /* VOLATILE WRITE CACHE ENABLED */
+ { 85, 0, 6 }, /* SMART ENABLED */
+ { 0 }
+};
+
+/* A QWord with bit 63 set, and a copy of the IDENTIFY DEVICE data @bits */
+static uint64_t ide_identify_bits(IDEState *s, const IDEIdentifyBit *bits)
+{
+ uint64_t qword = 1ULL << 63;
+
+ for (; bits->word; bits++) {
+ if (lduw_le_p(s->identify_data + 2 * bits->word) & (1 << bits->bit)) {
+ qword |= 1ULL << bits->qword_bit;
+ }
+ }
+ return qword;
+}
+
+/*
+ * Build the IDENTIFY DEVICE data log (log address 30h, ACS-6 9.11) pages
+ * [page, page + count) into the I/O buffer. Page 00h (List of Supported
+ * pages), page 01h (a copy of the IDENTIFY DEVICE data), page 02h (Capacity),
+ * page 03h (Supported Capabilities), page 04h (Current Settings), page 05h
+ * (Strings), page 06h (Security) and page 08h (Serial ATA) are implemented;
+ * any other page is returned as zeros (ACS-6 9.11.1).
+ */
+static void ide_fill_identify_device_log(IDEState *s, uint16_t page,
+ uint16_t count)
+{
+ uint16_t i;
+
+ /* Update the cached IDENTIFY DEVICE data; this clobbers io_buffer. */
+ ide_identify(s);
+ memset(s->io_buffer, 0, count * 512);
+
+ for (i = 0; i < count; i++) {
+ uint8_t *buf = s->io_buffer + i * 512;
+
+ switch (page + i) {
+ case 0x00:
+ /*
+ * List of Supported IDENTIFY DEVICE data log pages (ACS-6 9.11.2)
+ */
+ put_le16((uint16_t *)buf, 0x0001); /* REVISION NUMBER field */
+ buf[8] = 8; /* number of entries */
+ buf[9] = 0x00; /* page 00h is supported */
+ buf[10] = 0x01; /* page 01h is supported */
+ buf[11] = 0x02; /* page 02h is supported */
+ buf[12] = 0x03; /* page 03h is supported */
+ buf[13] = 0x04; /* page 04h is supported */
+ buf[14] = 0x05; /* page 05h is supported */
+ buf[15] = 0x06; /* page 06h is supported */
+ buf[16] = 0x08; /* page 08h is supported */
+ break;
+ case 0x01:
+ /* Copy of IDENTIFY DEVICE data, words 0..255 (ACS-6 9.11.3) */
+ memcpy(buf, s->identify_data, sizeof(s->identify_data));
+ break;
+ case 0x02:
+ /* Capacity (ACS-6 9.11.4) */
+ /* header QWord: 63=set to one, PAGE NUMBER=02h, REVISION=0001h */
+ stq_le_p(buf, (1ULL << 63) | (0x02ULL << 16) | 0x0001);
+ /*
+ * Device Capacity QWord: 63=set to one, 47:0=ACCESSIBLE CAPACITY
+ * (one greater than the maximum user-accessible LBA)
+ */
+ stq_le_p(buf + 8, (1ULL << 63) |
+ ((uint64_t)s->nb_sectors & 0xffffffffffffULL));
+ break;
+ case 0x03:
+ /* Supported Capabilities (ACS-6 9.11.5) */
+ /* header QWord: 63=set to one, PAGE NUMBER=03h, REVISION=0001h */
+ stq_le_p(buf, (1ULL << 63) | (0x03ULL << 16) | 0x0001);
+ /* capabilities QWord: 63=valid, the rest copies IDENTIFY data */
+ stq_le_p(buf + 8, ide_identify_bits(s, ide_supported_capabilities));
+ break;
+ case 0x04:
+ /* Current Settings (ACS-6 9.11.6) */
+ /* header QWord: 63=set to one, PAGE NUMBER=04h, REVISION=0001h */
+ stq_le_p(buf, (1ULL << 63) | (0x04ULL << 16) | 0x0001);
+ /* Current Settings: 63=set to one, rest copies IDENTIFY data */
+ stq_le_p(buf + 8, ide_identify_bits(s, ide_current_settings));
+ break;
+ case 0x05:
+ /* Strings */
+ /* header QWord: 63=set to one, PAGE NUMBER=05h, REVISION=0001h */
+ stq_le_p(buf, (1ULL << 63) | (0x05ULL << 16) | 0x0001);
+ /*
+ * SERIAL NUMBER, FIRMWARE REVISION, MODEL NUMBER and ADDITIONAL
+ * PRODUCT IDENTIFIER: copies of IDENTIFY DEVICE data words 10..19,
+ * 23..26, 27..46 and 170..173
+ */
+ memcpy(buf + 8, s->identify_data + 2 * 10, 20);
+ memcpy(buf + 32, s->identify_data + 2 * 23, 8);
+ memcpy(buf + 48, s->identify_data + 2 * 27, 40);
+ memcpy(buf + 96, s->identify_data + 2 * 170, 8);
+ break;
+ case 0x06:
+ /* Security: the Security feature set is not supported */
+ /* header QWord: 63=set to one, PAGE NUMBER=06h, REVISION=0001h */
+ stq_le_p(buf, (1ULL << 63) | (0x06ULL << 16) | 0x0001);
+ /*
+ * Master Password Identifier, Security Status, the times required
+ * for an Enhanced and a Normal Erase, Trusted Computing feature
+ * set and Security Capabilities: valid, with nothing supported
+ */
+ stq_le_p(buf + 8, 1ULL << 63);
+ stq_le_p(buf + 16, 1ULL << 63);
+ stq_le_p(buf + 24, 1ULL << 63);
+ stq_le_p(buf + 32, 1ULL << 63);
+ stq_le_p(buf + 40, 1ULL << 63);
+ stq_le_p(buf + 48, 1ULL << 63);
+ break;
+ case 0x08:
+ /* Serial ATA */
+ /* header QWord: 63=set to one, PAGE NUMBER=08h, REVISION=0001h */
+ stq_le_p(buf, (1ULL << 63) | (0x08ULL << 16) | 0x0001);
+ /* SATA Capabilities: 63=set to one, rest copies IDENTIFY data */
+ stq_le_p(buf + 8, ide_identify_bits(s, ide_sata_capabilities));
+ /* Current SATA Settings: 63=set to one, nothing enabled */
+ stq_le_p(buf + 16, 1ULL << 63);
+ break;
+ default:
+ /* Unsupported page: returned as all zeros. */
+ break;
+ }
+ }
+}
+
/*
* Fill the I/O buffer with the General Purpose Logging data requested by a
* READ LOG EXT / READ LOG DMA EXT command and return the number of bytes to
@@ -1668,6 +1854,9 @@ static int ide_read_log(IDEState *s)
case IDE_GPL_LOG_DIRECTORY:
log_size = 1;
break;
+ case IDE_GPL_LOG_IDENTIFY_DEVICE:
+ log_size = IDE_GPL_LOG_IDENTIFY_DEVICE_LEN;
+ break;
default:
/* Log (or its associated feature set) not supported (ACS-6 7.25.5). */
return -1;
@@ -1689,6 +1878,12 @@ static int ide_read_log(IDEState *s)
memset(s->io_buffer, 0, count * 512);
/* word 0: General Purpose Logging Version */
put_le16((uint16_t *)s->io_buffer, 0x0001);
+ /* word N: number of log pages at log address N */
+ put_le16((uint16_t *)s->io_buffer + IDE_GPL_LOG_IDENTIFY_DEVICE,
+ IDE_GPL_LOG_IDENTIFY_DEVICE_LEN);
+ break;
+ case IDE_GPL_LOG_IDENTIFY_DEVICE:
+ ide_fill_identify_device_log(s, page, count);
break;
}
diff --git a/hw/ide/ide-internal.h b/hw/ide/ide-internal.h
index 389e15049e..8da6d72450 100644
--- a/hw/ide/ide-internal.h
+++ b/hw/ide/ide-internal.h
@@ -212,6 +212,15 @@
* READ LOG EXT / READ LOG DMA EXT (see ACS-6 9.1 table 222).
*/
#define IDE_GPL_LOG_DIRECTORY 0x00 /* General Purpose Log Directory */
+#define IDE_GPL_LOG_IDENTIFY_DEVICE 0x30 /* IDENTIFY DEVICE data log */
+
+/*
+ * Number of 512-byte pages in the IDENTIFY DEVICE data log. Pages 00h (list
+ * of supported pages), 01h (copy of IDENTIFY DEVICE data), 02h (Capacity),
+ * 03h (Supported Capabilities), 04h (Current Settings), 05h (Strings), 06h
+ * (Security) and 08h (Serial ATA) are implemented.
+ */
+#define IDE_GPL_LOG_IDENTIFY_DEVICE_LEN 9
#if (IDE_DMA_BUF_SECTORS < MAX_MULT_SECTORS)
#error "IDE_DMA_BUF_SECTORS must be bigger or equal to MAX_MULT_SECTORS"
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index 8ff30616d5..ba9ea5c435 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1064,6 +1064,9 @@ static void ahci_test_read_log(AHCIQState *ahci, uint8_t read_log_cmd)
{
uint16_t id[256];
uint16_t dir[256];
+ uint16_t page[256];
+ uint8_t *page8 = (uint8_t *)page;
+ uint64_t nb, cap;
unsigned px;
px = ahci_port_select(ahci);
@@ -1084,14 +1087,81 @@ static void ahci_test_read_log(AHCIQState *ahci, uint8_t read_log_cmd)
ahci_io(ahci, px, read_log_cmd, &dir, 512, 0x00);
/* word 0: General Purpose Logging Version */
g_assert_cmphex(le16_to_cpu(dir[0]), ==, 0x0001);
+ /* word 30h: number of pages in the IDENTIFY DEVICE data log */
+ g_assert_cmphex(le16_to_cpu(dir[0x30]), ==, 9);
+
+ /* IDENTIFY DEVICE data log (log address 30h), page 0: list of pages. */
+ ahci_io(ahci, px, read_log_cmd, &page, 512, 0x30);
+ g_assert_cmphex(le16_to_cpu(page[0]), ==, 0x0001); /* REVISION NUMBER */
+ g_assert_cmpint(page8[8], ==, 8); /* number of entries */
+ g_assert_cmpint(page8[9], ==, 0x00); /* page 00h supported */
+ g_assert_cmpint(page8[10], ==, 0x01); /* page 01h supported */
+ g_assert_cmpint(page8[11], ==, 0x02); /* page 02h supported */
+ g_assert_cmpint(page8[12], ==, 0x03); /* page 03h supported */
+ g_assert_cmpint(page8[13], ==, 0x04); /* page 04h supported */
+ g_assert_cmpint(page8[14], ==, 0x05); /* page 05h supported */
+ g_assert_cmpint(page8[15], ==, 0x06); /* page 06h supported */
+ g_assert_cmpint(page8[16], ==, 0x08); /* page 08h supported */
+
+ /* IDENTIFY DEVICE data log page 1: a copy of the IDENTIFY data. */
+ ahci_io(ahci, px, read_log_cmd, &page, 512, 0x30 | (1 << 8));
+ g_assert_cmphex(memcmp(page, id, 512), ==, 0);
+
+ /* IDENTIFY DEVICE data log page 2 (page number 2): Capacity. */
+ ahci_io(ahci, px, read_log_cmd, &page, 512, 0x30 | (2 << 8));
+ g_assert_cmphex(le16_to_cpu(page[0]), ==, 0x0001); /* REVISION NUMBER */
+ g_assert_cmpint(page8[2], ==, 0x02); /* PAGE NUMBER */
+ g_assert_cmpint(page8[7] & (1 << 7), ==, (1 << 7)); /* header bit 63 */
+ g_assert_cmpint(page8[15] & (1 << 7), ==, (1 << 7)); /* capacity valid */
+ /* ACCESSIBLE CAPACITY (bits 47:0) must match IDENTIFY words 100..103 */
+ nb = (uint64_t)le16_to_cpu(id[100]) |
+ ((uint64_t)le16_to_cpu(id[101]) << 16) |
+ ((uint64_t)le16_to_cpu(id[102]) << 32) |
+ ((uint64_t)le16_to_cpu(id[103]) << 48);
+ cap = (uint64_t)page8[8] | ((uint64_t)page8[9] << 8) |
+ ((uint64_t)page8[10] << 16) | ((uint64_t)page8[11] << 24) |
+ ((uint64_t)page8[12] << 32) | ((uint64_t)page8[13] << 40);
+ g_assert_cmpuint(cap, ==, nb);
+
+ /* IDENTIFY DEVICE data log page 3: Supported Capabilities. */
+ ahci_io(ahci, px, read_log_cmd, &page, 512, 0x30 | (3 << 8));
+ g_assert_cmphex(le16_to_cpu(page[0]), ==, 0x0001); /* REVISION NUMBER */
+ g_assert_cmpint(page8[2], ==, 0x03); /* PAGE NUMBER */
+ g_assert_cmpint(page8[7] & (1 << 7), ==, (1 << 7)); /* header bit 63 */
+ g_assert_cmpint(page8[8] & (1 << 2), ==, (1 << 2)); /* GPL DMA supported */
+ g_assert_cmpint(page8[9] & (1 << 3), ==, (1 << 3)); /* GPL (bit 11) */
+ g_assert_cmpint(page8[10] & (1 << 4), ==, (1 << 4)); /* 48-BIT (bit 20) */
+
+ /* IDENTIFY DEVICE data log page 5: Strings, as in the IDENTIFY data. */
+ ahci_io(ahci, px, read_log_cmd, &page, 512, 0x30 | (5 << 8));
+ g_assert_cmpint(page8[2], ==, 0x05); /* PAGE NUMBER */
+ g_assert_cmphex(memcmp(page8 + 8, &id[10], 20), ==, 0); /* SERIAL */
+ g_assert_cmphex(memcmp(page8 + 48, &id[27], 40), ==, 0); /* MODEL */
+
+ /* IDENTIFY DEVICE data log page 8: Serial ATA. */
+ ahci_io(ahci, px, read_log_cmd, &page, 512, 0x30 | (8 << 8));
+ g_assert_cmpint(page8[2], ==, 0x08); /* PAGE NUMBER */
+ g_assert_cmpint(page8[8] & (1 << 7), ==, (1 << 7)); /* NCQ supported */
}
static void test_read_log_dma(void)
{
AHCIQState *ahci;
+ uint16_t id[256];
+ uint16_t idlog[512]; /* two consecutive 512-byte log pages */
+ unsigned px;
ahci = ahci_boot_and_enable(NULL);
ahci_test_read_log(ahci, CMD_READ_LOG_DMA_EXT);
+
+ /* Also exercise a multi-page DMA transfer: both pages of log 30h. */
+ px = ahci_port_select(ahci);
+ ahci_port_clear(ahci, px);
+ ahci_io(ahci, px, CMD_IDENTIFY, &id, 512, 0);
+ ahci_io(ahci, px, CMD_READ_LOG_DMA_EXT, &idlog, 1024, 0x30);
+ g_assert_cmphex(le16_to_cpu(idlog[0]), ==, 0x0001); /* page 0 header */
+ g_assert_cmphex(memcmp(&idlog[256], id, 512), ==, 0); /* page 1: IDENTIFY */
+
ahci_shutdown(ahci);
}
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 06/13] hw/ide: add the IDENTIFY DEVICE data log
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:23 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev, Fabiano Rosas,
Laurent Vivier, Paolo Bonzini
Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> Add the IDENTIFY DEVICE data log (30h).
> The IDENTIFY DEVICE data log is a mandatory log in the GPL feature set.
>
> Implement it with pages 00h to 06h and 08h, which are all the pages that
> ACS-6 9.11.1 makes mandatory for a SATA device, and list it in the General
> Purpose Log Directory.
>
> The Supported Capabilities (03h), Current Settings (04h) and Serial ATA
> (08h) pages copy their bits from the IDENTIFY DEVICE data, so that they
> cannot disagree with it.
>
> Extend the read log qtests to check the pages, and to read two pages
> with one READ LOG DMA EXT.
>
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
Looks good to me.
Note that I did not check the specs to see if all the bits are correct. I trust
you did that :)
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 07/13] hw/ide/ahci: add RECEIVE FPDMA QUEUED support
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
` (5 preceding siblings ...)
2026-11-27 19:12 ` [PATCH 06/13] hw/ide: add the IDENTIFY DEVICE data log Niklas Cassel
@ 2026-11-27 19:12 ` 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
` (5 subsequent siblings)
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
The command RECEIVE FPDMA QUEUED allows the device to encapsulate a
non-NCQ command in a the RECEIVE FPDMA QUEUED command.
Add support for subcommand 01h only: the NCQ encapsulation of READ LOG
DMA EXT. It shares the log generation with READ LOG (DMA) EXT, and an
invalid log or subcommand aborts the command.
Add a qtest that reads logs over NCQ, and the libqos support for RECEIVE
FPDMA QUEUED.
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/ahci-internal.h | 4 ++
hw/ide/ahci.c | 44 ++++++++++++++++++++++
hw/ide/core.c | 39 ++++++++++++++++----
hw/ide/ide-internal.h | 10 +++++
hw/ide/trace-events | 1 +
tests/qtest/ahci-test.c | 77 +++++++++++++++++++++++++++++++++++++++
tests/qtest/libqos/ahci.c | 15 ++++++++
tests/qtest/libqos/ahci.h | 11 +++++-
8 files changed, 191 insertions(+), 10 deletions(-)
diff --git a/hw/ide/ahci-internal.h b/hw/ide/ahci-internal.h
index ca11a370c7..5f51341d87 100644
--- a/hw/ide/ahci-internal.h
+++ b/hw/ide/ahci-internal.h
@@ -246,6 +246,9 @@ enum AHCIPortIRQ {
#define RECEIVE_FPDMA_QUEUED 0x65
#define SEND_FPDMA_QUEUED 0x64
+/* RECEIVE FPDMA QUEUED subcommands (ACS-6 table 95) */
+#define RECEIVE_FPDMA_READ_LOG_DMA_EXT 0x01
+
#define NCQ_FIS_FUA_MASK 0x80
#define NCQ_FIS_RARC_MASK 0x01
@@ -302,6 +305,7 @@ typedef struct NCQTransferState {
uint64_t lba;
uint8_t tag;
uint8_t cmd;
+ uint8_t subcmd; /* SUBCOMMAND field, for RECEIVE/SEND FPDMA QUEUED */
uint8_t slot;
bool used;
bool halt;
diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index e14b298ea6..21938703da 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -1093,6 +1093,43 @@ static int is_ncq(uint8_t ata_cmd)
}
}
+/*
+ * RECEIVE FPDMA QUEUED (ACS-6 7.33) transfers device-generated data to the
+ * host. Only subcommand 01h (the NCQ encapsulation of READ LOG DMA EXT, see
+ * ACS-6 7.26.6) is supported. The log data is generated synchronously into
+ * the I/O buffer and copied into the command's scatter/gather list, so there
+ * is no block backend I/O involved.
+ */
+static void execute_ncq_receive_fpdma_queued(NCQTransferState *ncq_tfs)
+{
+ IDEState *ide_state = &ncq_tfs->drive->port.ifs[0];
+
+ switch (ncq_tfs->subcmd) {
+ case RECEIVE_FPDMA_READ_LOG_DMA_EXT: {
+ /* Encapsulated READ LOG DMA EXT inputs (ACS-6 table 87). */
+ uint8_t log_address = ncq_tfs->lba & 0xff;
+ uint16_t page = ((ncq_tfs->lba >> 8) & 0xff) |
+ (((ncq_tfs->lba >> 32) & 0xff) << 8);
+ int size = ide_read_log(ide_state, log_address, page,
+ ncq_tfs->sector_count);
+
+ if (size < 0) {
+ ncq_err(ncq_tfs);
+ break;
+ }
+ dma_buf_read(ide_state->io_buffer, size, NULL, &ncq_tfs->sglist,
+ MEMTXATTRS_UNSPECIFIED);
+ ide_state->status = READY_STAT | SEEK_STAT;
+ break;
+ }
+ default:
+ ncq_err(ncq_tfs);
+ break;
+ }
+
+ ncq_finish(ncq_tfs);
+}
+
static void execute_ncq_command(NCQTransferState *ncq_tfs)
{
AHCIDevice *ad = ncq_tfs->drive;
@@ -1123,6 +1160,11 @@ static void execute_ncq_command(NCQTransferState *ncq_tfs)
BDRV_SECTOR_SIZE,
ncq_cb, ncq_tfs);
break;
+ case RECEIVE_FPDMA_QUEUED:
+ trace_execute_ncq_command_receive(ad->hba, port, ncq_tfs->tag,
+ ncq_tfs->subcmd);
+ execute_ncq_receive_fpdma_queued(ncq_tfs);
+ break;
default:
trace_execute_ncq_command_unsup(ad->hba, port,
ncq_tfs->tag, ncq_tfs->cmd);
@@ -1175,6 +1217,8 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
ncq_tfs->slot = slot;
ncq_tfs->cmdh = &((AHCICmdHdr *)ad->lst)[slot];
ncq_tfs->cmd = ncq_fis->command;
+ /* SUBCOMMAND field is COUNT bits 12:8, i.e. the low 5 bits of "prio" */
+ ncq_tfs->subcmd = ncq_fis->prio & 0x1f;
ncq_tfs->lba = ((uint64_t)ncq_fis->lba5 << 40) |
((uint64_t)ncq_fis->lba4 << 32) |
((uint64_t)ncq_fis->lba3 << 24) |
diff --git a/hw/ide/core.c b/hw/ide/core.c
index 1304202ea2..0ccac398b2 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -200,6 +200,8 @@ static void ide_identify(IDEState *s)
put_le16(p + 75, s->ncq_queues - 1);
/* 15=READ LOG DMA EXT equivalent to READ LOG EXT, 8=NCQ supported */
put_le16(p + 76, (1 << 15) | (1 << 8));
+ /* 6=SEND AND RECEIVE QUEUED COMMANDS supported (SATA NCQ log 13h) */
+ put_le16(p + 77, (1 << 6));
}
put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
@@ -1839,21 +1841,28 @@ static void ide_fill_identify_device_log(IDEState *s, uint16_t page,
}
/*
- * Fill the I/O buffer with the General Purpose Logging data requested by a
- * READ LOG EXT / READ LOG DMA EXT command and return the number of bytes to
- * transfer, or a negative value if the command must be aborted (ACS-6 7.25).
+ * Fill the I/O buffer with the General Purpose Logging data selected by
+ * log_address, page and count (the LOG PAGE COUNT, in 512-byte pages) and
+ * return the number of bytes to transfer, or a negative value if the command
+ * must be aborted (ACS-6 7.25). Shared by the READ LOG EXT / READ LOG DMA EXT
+ * commands and by the RECEIVE FPDMA QUEUED NCQ encapsulation (ACS-6 7.26.6).
*/
-static int ide_read_log(IDEState *s)
+int ide_read_log(IDEState *s, uint8_t log_address, uint16_t page,
+ uint32_t count)
{
- uint8_t log_address = s->sector;
- uint16_t page = s->lcyl | ((uint16_t)s->hob_lcyl << 8);
- uint32_t count = s->nsector; /* LOG PAGE COUNT, in 512-byte pages */
uint16_t log_size;
switch (log_address) {
case IDE_GPL_LOG_DIRECTORY:
log_size = 1;
break;
+ case IDE_GPL_LOG_NCQ_SEND_RECV:
+ /* Only meaningful for devices that support the NCQ feature set. */
+ if (!s->ncq_queues) {
+ return -1;
+ }
+ log_size = 1;
+ break;
case IDE_GPL_LOG_IDENTIFY_DEVICE:
log_size = IDE_GPL_LOG_IDENTIFY_DEVICE_LEN;
break;
@@ -1881,6 +1890,19 @@ static int ide_read_log(IDEState *s)
/* word N: number of log pages at log address N */
put_le16((uint16_t *)s->io_buffer + IDE_GPL_LOG_IDENTIFY_DEVICE,
IDE_GPL_LOG_IDENTIFY_DEVICE_LEN);
+ if (s->ncq_queues) {
+ put_le16((uint16_t *)s->io_buffer + IDE_GPL_LOG_NCQ_SEND_RECV, 1);
+ }
+ break;
+ case IDE_GPL_LOG_NCQ_SEND_RECV:
+ /* SATA NCQ Send and Receive log (ACS-6 9.19) */
+ memset(s->io_buffer, 0, count * 512);
+ /*
+ * "Supports Read Log" DWord (offset 8): advertise READ LOG DMA EXT as
+ * RECEIVE FPDMA QUEUED subcommand 01h. Bit 0 = QUEUED READ LOG DMA EXT
+ * SUPPORTED, bit 1 = SEQUENTIAL QUEUED READ LOG DMA EXT SUPPORTED.
+ */
+ stl_le_p(s->io_buffer + 8, (1 << 1) | (1 << 0));
break;
case IDE_GPL_LOG_IDENTIFY_DEVICE:
ide_fill_identify_device_log(s, page, count);
@@ -1907,7 +1929,8 @@ static bool cmd_read_log(IDEState *s, uint8_t cmd)
trace_ide_read_log(s, s->sector, s->lcyl | ((uint16_t)s->hob_lcyl << 8),
s->nsector, dma ? "dma" : "pio");
- size = ide_read_log(s);
+ size = ide_read_log(s, s->sector, s->lcyl | ((uint16_t)s->hob_lcyl << 8),
+ s->nsector);
if (size < 0) {
ide_abort_command(s);
return true;
diff --git a/hw/ide/ide-internal.h b/hw/ide/ide-internal.h
index 8da6d72450..382e2a4915 100644
--- a/hw/ide/ide-internal.h
+++ b/hw/ide/ide-internal.h
@@ -212,6 +212,7 @@
* READ LOG EXT / READ LOG DMA EXT (see ACS-6 9.1 table 222).
*/
#define IDE_GPL_LOG_DIRECTORY 0x00 /* General Purpose Log Directory */
+#define IDE_GPL_LOG_NCQ_SEND_RECV 0x13 /* SATA NCQ Send and Receive log */
#define IDE_GPL_LOG_IDENTIFY_DEVICE 0x30 /* IDENTIFY DEVICE data log */
/*
@@ -445,6 +446,15 @@ void ide_bus_register_restart_cb(IDEBus *bus);
void ide_bus_exec_cmd(IDEBus *bus, uint32_t val);
+/*
+ * Fill the IDEState I/O buffer with a General Purpose Logging log page.
+ * Returns the number of bytes generated, or a negative value if the request
+ * is invalid. Shared between the READ LOG (DMA) EXT commands and the
+ * RECEIVE FPDMA QUEUED NCQ encapsulation.
+ */
+int ide_read_log(IDEState *s, uint8_t log_address, uint16_t page,
+ uint32_t count);
+
void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
EndTransferFunc *end_transfer_func);
bool ide_transfer_start_norecurse(IDEState *s, uint8_t *buf, int size,
diff --git a/hw/ide/trace-events b/hw/ide/trace-events
index 102f39436c..8b98b11d23 100644
--- a/hw/ide/trace-events
+++ b/hw/ide/trace-events
@@ -98,6 +98,7 @@ ahci_populate_sglist_bad_offset(void *s, int port, int off_idx, int64_t off_pos)
ncq_finish(void *s, int port, uint8_t tag) "ahci(%p)[%d][tag:%d]: NCQ transfer finished"
execute_ncq_command_read(void *s, int port, uint8_t tag, int count, int64_t lba) "ahci(%p)[%d][tag:%d]: NCQ reading %d sectors from LBA %"PRId64
execute_ncq_command_write(void *s, int port, uint8_t tag, int count, int64_t lba) "ahci(%p)[%d][tag:%d]: NCQ writing %d sectors to LBA %"PRId64
+execute_ncq_command_receive(void *s, int port, uint8_t tag, uint8_t subcmd) "ahci(%p)[%d][tag:%d]: NCQ RECEIVE FPDMA QUEUED subcommand 0x%02x"
execute_ncq_command_unsup(void *s, int port, uint8_t tag, uint8_t cmd) "ahci(%p)[%d][tag:%d]: error: unsupported NCQ command (0x%02x) received"
process_ncq_command_mismatch(void *s, int port, uint8_t tag, uint8_t slot) "ahci(%p)[%d][tag:%d]: Warning: NCQ slot (%d) did not match the given tag"
process_ncq_command_aux(void *s, int port, uint8_t tag) "ahci(%p)[%d][tag:%d]: Warn: Attempt to use NCQ auxiliary fields"
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index ba9ea5c435..5e924010f0 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1805,6 +1805,82 @@ static void test_ncq_simple(void)
ahci_shutdown(ahci);
}
+/*
+ * Read one 512-byte General Purpose Logging log page into @buffer using
+ * RECEIVE FPDMA QUEUED subcommand 01h (the NCQ encapsulation of
+ * READ LOG DMA EXT).
+ */
+static void ahci_ncq_read_log(AHCIQState *ahci, uint8_t port, uint8_t log,
+ uint16_t page, void *buffer)
+{
+ AHCICommand *cmd;
+ uint64_t ptr;
+ uint64_t lba = log | ((uint64_t)(page & 0xff) << 8) |
+ ((uint64_t)(page >> 8) << 32);
+
+ ptr = ahci_alloc(ahci, 512);
+ g_assert(ptr);
+ qtest_memset(ahci->parent->qts, ptr, 0x00, 512);
+
+ cmd = ahci_command_create(RECEIVE_FPDMA_QUEUED);
+ ahci_command_set_buffer(cmd, ptr);
+ ahci_command_set_size(cmd, 512);
+ ahci_command_set_offset(cmd, lba);
+ ahci_command_set_ncq_subcmd(cmd, NCQ_RECEIVE_READ_LOG_DMA_EXT);
+ ahci_command_commit(ahci, cmd, port);
+ ahci_command_issue(ahci, cmd);
+ ahci_command_verify(ahci, cmd);
+ ahci_command_free(cmd);
+
+ qtest_bufread(ahci->parent->qts, ptr, buffer, 512);
+ ahci_free(ahci, ptr);
+}
+
+static void test_ncq_read_log(void)
+{
+ AHCIQState *ahci;
+ uint16_t id[256];
+ uint16_t dir[256];
+ uint16_t page[256];
+ uint8_t *page8 = (uint8_t *)page;
+ unsigned px;
+
+ ahci = ahci_boot_and_enable(NULL);
+ px = ahci_port_select(ahci);
+ ahci_port_clear(ahci, px);
+
+ /* Fetch IDENTIFY DEVICE data (PIO) to compare against the log copy. */
+ ahci_io(ahci, px, CMD_IDENTIFY, &id, 512, 0);
+ /* word 77 bit 6: SEND AND RECEIVE QUEUED COMMANDS supported */
+ g_assert_cmphex(le16_to_cpu(id[77]) & (1 << 6), ==, (1 << 6));
+
+ /* General Purpose Log Directory (log 00h) over NCQ. */
+ ahci_ncq_read_log(ahci, px, 0x00, 0, dir);
+ g_assert_cmphex(le16_to_cpu(dir[0]), ==, 0x0001); /* GPL version */
+ g_assert_cmphex(le16_to_cpu(dir[0x13]), ==, 1); /* NCQ Send/Recv log */
+ g_assert_cmphex(le16_to_cpu(dir[0x30]), ==, 9); /* IDENTIFY log size */
+
+ /* SATA NCQ Send and Receive log (log 13h). */
+ ahci_ncq_read_log(ahci, px, 0x13, 0, page);
+ /*
+ * "Supports Read Log" DWord (offset 8): bit 0 QUEUED READ LOG DMA EXT and
+ * bit 1 SEQUENTIAL QUEUED READ LOG DMA EXT (subcommand 01h).
+ */
+ g_assert_cmpuint(page8[8] | (page8[9] << 8) | (page8[10] << 16) |
+ ((uint32_t)page8[11] << 24), ==, 0x3);
+
+ /* IDENTIFY DEVICE data log (log 30h) page 0: list of supported pages. */
+ ahci_ncq_read_log(ahci, px, 0x30, 0, page);
+ g_assert_cmphex(le16_to_cpu(page[0]), ==, 0x0001); /* REVISION NUMBER */
+ g_assert_cmpint(page8[8], ==, 8); /* number of entries */
+
+ /* IDENTIFY DEVICE data log (log 30h) page 1: copy of IDENTIFY data. */
+ ahci_ncq_read_log(ahci, px, 0x30, 1, page);
+ g_assert_cmphex(memcmp(page, id, 512), ==, 0);
+
+ ahci_shutdown(ahci);
+}
+
static int prepare_iso(size_t size, unsigned char **buf, char **name)
{
g_autofree char *cdrom_path = NULL;
@@ -2662,6 +2738,7 @@ int main(int argc, char **argv)
qtest_add_func("/ahci/reset/pending_callback", test_reset_pending_callback);
qtest_add_func("/ahci/io/ncq/simple", test_ncq_simple);
+ qtest_add_func("/ahci/io/ncq/read_log", test_ncq_read_log);
qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq);
qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq);
qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq);
diff --git a/tests/qtest/libqos/ahci.c b/tests/qtest/libqos/ahci.c
index e64fcade36..d50ff4767b 100644
--- a/tests/qtest/libqos/ahci.c
+++ b/tests/qtest/libqos/ahci.c
@@ -75,6 +75,8 @@ AHCICommandProp ahci_command_properties[] = {
.lba48 = true, .read = true, .ncq = true },
{ .cmd = WRITE_FPDMA_QUEUED, .data = true, .dma = true,
.lba48 = true, .write = true, .ncq = true },
+ { .cmd = RECEIVE_FPDMA_QUEUED, .data = true, .dma = true,
+ .lba48 = true, .read = true, .ncq = true },
{ .cmd = CMD_READ_MAX, .lba28 = true },
{ .cmd = CMD_READ_MAX_EXT, .lba48 = true },
{ .cmd = CMD_FLUSH_CACHE, .data = false },
@@ -1204,6 +1206,19 @@ void ahci_command_set_count(AHCICommand *cmd, uint16_t count)
cmd->fis.count = count;
}
+/*
+ * Set the SUBCOMMAND field (COUNT bits 12:8) of an NCQ command, e.g. the
+ * RECEIVE FPDMA QUEUED subcommand. It occupies the low 5 bits of the "prio"
+ * (COUNT 15:8) byte.
+ */
+void ahci_command_set_ncq_subcmd(AHCICommand *cmd, uint8_t subcmd)
+{
+ NCQFIS *nfis = (NCQFIS *)&(cmd->fis);
+
+ g_assert(cmd->props->ncq);
+ nfis->prio = (nfis->prio & ~0x1f) | (subcmd & 0x1f);
+}
+
void ahci_command_expect_error(AHCICommand *cmd, uint8_t err)
{
cmd->interrupts |= AHCI_PX_IS_TFES;
diff --git a/tests/qtest/libqos/ahci.h b/tests/qtest/libqos/ahci.h
index 9c4195eee9..9b40eabe35 100644
--- a/tests/qtest/libqos/ahci.h
+++ b/tests/qtest/libqos/ahci.h
@@ -285,8 +285,14 @@ enum {
CMD_PACKET = 0xA0,
CMD_PACKET_ID = 0xA1,
/* NCQ */
- READ_FPDMA_QUEUED = 0x60,
- WRITE_FPDMA_QUEUED = 0x61,
+ READ_FPDMA_QUEUED = 0x60,
+ WRITE_FPDMA_QUEUED = 0x61,
+ RECEIVE_FPDMA_QUEUED = 0x65,
+};
+
+/* RECEIVE FPDMA QUEUED subcommands */
+enum {
+ NCQ_RECEIVE_READ_LOG_DMA_EXT = 0x01,
};
/* ATAPI Commands */
@@ -647,6 +653,7 @@ void ahci_command_set_prd_size(AHCICommand *cmd, unsigned prd_size);
void ahci_command_set_sizes(AHCICommand *cmd, uint64_t xbytes,
unsigned prd_size);
void ahci_command_set_count(AHCICommand *cmd, uint16_t count);
+void ahci_command_set_ncq_subcmd(AHCICommand *cmd, uint8_t subcmd);
void ahci_command_expect_error(AHCICommand *cmd, uint8_t err);
void ahci_command_set_acmd(AHCICommand *cmd, void *acmd);
void ahci_command_enable_atapi_dma(AHCICommand *cmd);
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 07/13] hw/ide/ahci: add RECEIVE FPDMA QUEUED support
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:25 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev, Fabiano Rosas,
Laurent Vivier, Paolo Bonzini
Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> The command RECEIVE FPDMA QUEUED allows the device to encapsulate a
> non-NCQ command in a the RECEIVE FPDMA QUEUED command.
>
> Add support for subcommand 01h only: the NCQ encapsulation of READ LOG
> DMA EXT. It shares the log generation with READ LOG (DMA) EXT, and an
> invalid log or subcommand aborts the command.
>
> Add a qtest that reads logs over NCQ, and the libqos support for RECEIVE
> FPDMA QUEUED.
>
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
Looks good to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 08/13] hw/ide: add Sense Data Reporting feature set support
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
` (6 preceding siblings ...)
2026-11-27 19:12 ` [PATCH 07/13] hw/ide/ahci: add RECEIVE FPDMA QUEUED support Niklas Cassel
@ 2026-11-27 19:12 ` 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
` (4 subsequent siblings)
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
Implement the Sense Data Reporting feature set (ACS-6 4.25).
When it is enabled, a failed command sets SENSE DATA AVAILABLE, and
REQUEST SENSE DATA EXT (0Bh) returns the sense key, ASC and ASCQ.
The sense data is discarded on a reset and when another command is
accepted.
An unsupported command reports INVALID COMMAND OPERATION CODE (ACS-6
6.8.18), and an invalid input field reports INVALID FIELD IN CDB (6.8.19),
both with ILLEGAL REQUEST. Other errors make no sense data available.
As with NCQ and GPL, only a disk behind AHCI offers the feature set.
This feature will be used to populate the NCQ Command Error log and to
implement NCQ Autosense. Add a qtest that checks the sense data of an
aborted SET FEATURES.
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/ahci.c | 3 +
hw/ide/core.c | 146 +++++++++++++++++++++++++++++++++++---
hw/ide/ide-internal.h | 3 +-
include/hw/ide/ide-dev.h | 4 ++
tests/qtest/ahci-test.c | 69 ++++++++++++++++++
tests/qtest/libqos/ahci.c | 10 +++
tests/qtest/libqos/ahci.h | 5 +-
7 files changed, 230 insertions(+), 10 deletions(-)
diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index 21938703da..d74d185607 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -1225,6 +1225,9 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
((uint64_t)ncq_fis->lba2 << 16) |
((uint64_t)ncq_fis->lba1 << 8) |
(uint64_t)ncq_fis->lba0;
+
+ /* Accepting an NCQ command makes the sense data unavailable (ACS-6 4.25) */
+ ad->port.ifs[0].sense_data_avail = false;
ncq_tfs->tag = tag;
/* Sanity-check the NCQ packet */
diff --git a/hw/ide/core.c b/hw/ide/core.c
index 0ccac398b2..2d8bca0762 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -37,6 +37,8 @@
#include "system/dma.h"
#include "hw/block/block.h"
#include "system/block-backend.h"
+#include "scsi/constants.h"
+#include "scsi/utils.h"
#include "qapi/error.h"
#include "qemu/cutils.h"
#include "system/replay.h"
@@ -244,12 +246,13 @@ static void ide_identify(IDEState *s)
}
if (s->ncq_queues) {
/*
- * 14=set to 1,
+ * 14=set to 1, 6=Sense Data Reporting supported,
* 3=READ LOG DMA EXT/WRITE LOG DMA EXT (GPL DMA) supported
*/
- put_le16(p + 119, (1 << 14) | (1 << 3));
- /* 14=set to 1, 3=GPL DMA enabled */
- put_le16(p + 120, (1 << 14) | (1 << 3));
+ put_le16(p + 119, (1 << 14) | (1 << 6) | (1 << 3));
+ /* 14=set to 1, 6=Sense Data Reporting enabled, 3=GPL DMA enabled */
+ put_le16(p + 120, (1 << 14) | (1 << 3) |
+ (s->sense_data_enabled ? (1 << 6) : 0));
}
if (dev && dev->conf.discard_granularity) {
put_le16(p + 169, 1); /* TRIM support */
@@ -555,11 +558,41 @@ BlockAIOCB *ide_issue_trim(
return &iocb->common;
}
-void ide_abort_command(IDEState *s)
+/*
+ * Record sense data for the Sense Data Reporting feature set (ACS-6 4.25).
+ * When the feature is enabled the SENSE DATA AVAILABLE bit is reported in the
+ * STATUS field so the host can retrieve the sense with REQUEST SENSE DATA EXT.
+ * When it is disabled this is a no-op, so the ATAPI sense_key/asc are left
+ * untouched for packet devices.
+ */
+static void ide_set_sense(IDEState *s, SCSISense sense)
+{
+ /*
+ * NO SENSE means that there is no sense data to report (ACS-6 6.8.31):
+ * none is made available, and the host relies on the ERROR field.
+ */
+ if (!s->sense_data_enabled || sense.key == NO_SENSE) {
+ return;
+ }
+ s->sense_key = sense.key;
+ s->asc = sense.asc;
+ s->ascq = sense.ascq;
+ s->sense_data_avail = true;
+ s->status |= SENSE_DATA_STAT;
+}
+
+/* Abort the command, and record @sense for Sense Data Reporting */
+static void ide_abort_command_with_sense(IDEState *s, SCSISense sense)
{
s->status = READY_STAT | ERR_STAT;
s->error = ABRT_ERR;
ide_transfer_stop(s);
+ ide_set_sense(s, sense);
+}
+
+void ide_abort_command(IDEState *s)
+{
+ ide_abort_command_with_sense(s, SENSE_CODE(NO_SENSE));
}
static void ide_set_retry(IDEState *s)
@@ -1432,6 +1465,14 @@ static void ide_reset(IDEState *s, IDEResetKind kind)
s->select = (ATA_DEV_ALWAYS_ON);
s->status = READY_STAT | SEEK_STAT;
+ /*
+ * Sense data does not survive a reset (ACS-6 4.25), but the enabled
+ * state of the Sense Data Reporting feature set does: a power-on reset
+ * should disable it (ACS-6 9.11.5.2.46), but IDE_RESET_HARDWARE also
+ * covers COMRESET, which must not change it.
+ */
+ s->sense_data_avail = false;
+
s->lba48 = 0;
/* ATAPI specific */
@@ -1712,6 +1753,7 @@ static const IDEIdentifyBit ide_sata_capabilities[] = {
/* The Current Settings QWord of page 04h, terminated by a zero word */
static const IDEIdentifyBit ide_current_settings[] = {
{ 85, 5, 13 }, /* VOLATILE WRITE CACHE ENABLED */
+ { 120, 6, 10 }, /* SENSE DATA ENABLED */
{ 85, 0, 6 }, /* SMART ENABLED */
{ 0 }
};
@@ -1932,7 +1974,7 @@ static bool cmd_read_log(IDEState *s, uint8_t cmd)
size = ide_read_log(s, s->sector, s->lcyl | ((uint16_t)s->hob_lcyl << 8),
s->nsector);
if (size < 0) {
- ide_abort_command(s);
+ ide_abort_command_with_sense(s, SENSE_CODE(INVALID_FIELD));
return true;
}
@@ -2020,6 +2062,38 @@ static bool cmd_specify(IDEState *s, uint8_t cmd)
return true;
}
+/*
+ * REQUEST SENSE DATA EXT (0Bh), Sense Data Reporting feature set (ACS-6 7.35)
+ */
+static bool cmd_request_sense_data_ext(IDEState *s, uint8_t cmd)
+{
+ /*
+ * Return the most recent sense data in the LBA field (ACS-6 table 371):
+ * LBA 7:0 = ASCQ, LBA 15:8 = ASC, LBA 19:16 = SENSE KEY. The COUNT and
+ * upper LBA fields (progress indication / vendor specific) are zeroed.
+ */
+ s->nsector = 0;
+ s->hob_nsector = 0;
+ s->hob_sector = 0;
+ s->hob_lcyl = 0;
+ s->hob_hcyl = 0;
+
+ if (s->sense_data_avail) {
+ s->sector = s->ascq;
+ s->lcyl = s->asc;
+ s->hcyl = s->sense_key & 0x0f;
+ s->status |= SENSE_DATA_STAT;
+ /* The sense data is consumed by reading it (ACS-6 4.25). */
+ s->sense_data_avail = false;
+ } else {
+ s->sector = 0;
+ s->lcyl = 0;
+ s->hcyl = 0;
+ }
+
+ return true;
+}
+
static bool cmd_set_features(IDEState *s, uint8_t cmd)
{
uint16_t *identify_data;
@@ -2049,6 +2123,20 @@ static bool cmd_set_features(IDEState *s, uint8_t cmd)
put_le16(identify_data + 85, ide_identify_word85(s));
ide_flush_cache(s);
return false;
+ case 0xc3: /* enable/disable the Sense Data Reporting feature set */
+ if (!s->ncq_queues) {
+ goto abort_cmd;
+ }
+ s->sense_data_enabled = s->nsector & 0x01;
+ identify_data = (uint16_t *)s->identify_data;
+ if (s->sense_data_enabled) {
+ put_le16(identify_data + 120,
+ le16_to_cpu(identify_data[120]) | (1 << 6));
+ } else {
+ put_le16(identify_data + 120,
+ le16_to_cpu(identify_data[120]) & ~(1 << 6));
+ }
+ return true;
case 0xcc: /* reverting to power-on defaults enable */
s->reset_reverts = true;
return true;
@@ -2101,7 +2189,7 @@ static bool cmd_set_features(IDEState *s, uint8_t cmd)
}
abort_cmd:
- ide_abort_command(s);
+ ide_abort_command_with_sense(s, SENSE_CODE(INVALID_FIELD));
return true;
}
@@ -2443,6 +2531,8 @@ static const struct {
[CFA_REQ_EXT_ERROR_CODE] = { cmd_cfa_req_ext_error_code, CFA_OK },
[WIN_DSM] = { cmd_data_set_management, HD_CFA_OK },
[WIN_DEVICE_RESET] = { cmd_device_reset, CD_OK },
+ [WIN_REQUEST_SENSE_DATA_EXT] = { cmd_request_sense_data_ext,
+ HD_OK | AHCI_ONLY },
[WIN_RECAL] = { cmd_nop, HD_CFA_OK | SET_DSC},
[WIN_READ] = { cmd_read_pio, ALL_OK },
[WIN_READ_ONCE] = { cmd_read_pio, HD_CFA_OK },
@@ -2507,6 +2597,15 @@ static bool ide_cmd_permitted(IDEState *s, uint32_t cmd)
&& (s->ncq_queues || !(ide_cmd_table[cmd].flags & AHCI_ONLY));
}
+/*
+ * Whether the sense data stays available once this command is accepted
+ * (ACS-6 4.25): REQUEST SENSE DATA EXT returns it.
+ */
+static bool ide_cmd_keeps_sense_data(IDEState *s, uint8_t cmd)
+{
+ return cmd == WIN_REQUEST_SENSE_DATA_EXT;
+}
+
void ide_bus_exec_cmd(IDEBus *bus, uint32_t val)
{
IDEState *s;
@@ -2529,7 +2628,7 @@ void ide_bus_exec_cmd(IDEBus *bus, uint32_t val)
}
if (!ide_cmd_permitted(s, val)) {
- ide_abort_command(s);
+ ide_abort_command_with_sense(s, SENSE_CODE(INVALID_OPCODE));
ide_bus_set_irq(s->bus);
return;
}
@@ -2538,6 +2637,15 @@ void ide_bus_exec_cmd(IDEBus *bus, uint32_t val)
s->error = 0;
s->io_buffer_offset = 0;
+ /*
+ * Accepting a command makes the sense data unavailable, unless the command
+ * reads it (ACS-6 4.25); a failing command records fresh sense of its own
+ * below.
+ */
+ if (!ide_cmd_keeps_sense_data(s, val)) {
+ s->sense_data_avail = false;
+ }
+
complete = ide_cmd_table[val].handler(s, val);
if (complete) {
s->status &= ~BUSY_STAT;
@@ -3368,6 +3476,27 @@ static const VMStateDescription vmstate_ide_tray_state = {
}
};
+static bool ide_sense_data_needed(void *opaque)
+{
+ IDEState *s = opaque;
+
+ return s->sense_data_enabled || s->sense_data_avail;
+}
+
+/* sense_key and asc travel with the main structure (ATAPI shares them) */
+static const VMStateDescription vmstate_ide_sense_data = {
+ .name = "ide_drive/sense_data",
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .needed = ide_sense_data_needed,
+ .fields = (const VMStateField[]) {
+ VMSTATE_BOOL(sense_data_enabled, IDEState),
+ VMSTATE_BOOL(sense_data_avail, IDEState),
+ VMSTATE_UINT8(ascq, IDEState),
+ VMSTATE_END_OF_LIST()
+ }
+};
+
static const VMStateDescription vmstate_ide_drive_pio_state = {
.name = "ide_drive/pio_state",
.version_id = 1,
@@ -3423,6 +3552,7 @@ const VMStateDescription vmstate_ide_drive = {
&vmstate_ide_drive_reset_reverts,
&vmstate_ide_tray_state,
&vmstate_ide_atapi_gesn_state,
+ &vmstate_ide_sense_data,
NULL
}
};
diff --git a/hw/ide/ide-internal.h b/hw/ide/ide-internal.h
index 382e2a4915..0fb4776356 100644
--- a/hw/ide/ide-internal.h
+++ b/hw/ide/ide-internal.h
@@ -26,6 +26,7 @@
/* Bits of HD_STATUS */
#define ERR_STAT 0x01
#define INDEX_STAT 0x02
+#define SENSE_DATA_STAT 0x02 /* SENSE DATA AVAILABLE (ACS-6 6.2.9) */
#define ECC_STAT 0x04 /* Corrected error */
#define DRQ_STAT 0x08
#define SEEK_STAT 0x10
@@ -65,7 +66,7 @@
/* reserved 0x07 */
#define WIN_DEVICE_RESET 0x08
/* reserved 0x09..0x0a */
-/* REQUEST SENSE DATA EXT 0x0B */
+#define WIN_REQUEST_SENSE_DATA_EXT 0x0B /* 48-Bit */
/* reserved 0x0C..0x0F */
#define WIN_RECAL 0x10 /* obsolete since ATA4 */
/* obsolete since ATA3, retired in ATA4 0x11..0x1F */
diff --git a/include/hw/ide/ide-dev.h b/include/hw/ide/ide-dev.h
index 258b216b44..01b7952957 100644
--- a/include/hw/ide/ide-dev.h
+++ b/include/hw/ide/ide-dev.h
@@ -96,6 +96,10 @@ struct IDEState {
struct unreported_events events;
uint8_t sense_key;
uint8_t asc;
+ /* Sense Data Reporting feature set (ATA HD) */
+ uint8_t ascq;
+ bool sense_data_enabled;
+ bool sense_data_avail;
bool tray_open;
bool tray_locked;
uint8_t cdrom_changed;
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index 5e924010f0..e7df8c9fc8 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1174,6 +1174,63 @@ static void test_read_log_pio(void)
ahci_shutdown(ahci);
}
+/* Restart command list processing after a task file error (AHCI 6.2.2) */
+static void ahci_port_restart(AHCIQState *ahci, unsigned px)
+{
+ 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);
+}
+
+/* Issue a non-data command; return the D2H Register FIS it completes with */
+static RegD2HFIS ahci_nodata_cmd(AHCIQState *ahci, unsigned px, uint8_t op,
+ uint16_t feature, uint16_t count, uint8_t err)
+{
+ AHCICommand *cmd = ahci_command_create(op);
+ RegD2HFIS d2h;
+
+ ahci_command_set_feature(cmd, feature);
+ ahci_command_set_count(cmd, count);
+ if (err) {
+ ahci_command_expect_error(cmd, err);
+ }
+ ahci_command_commit(ahci, cmd, px);
+ ahci_command_issue(ahci, cmd);
+ ahci_command_verify(ahci, cmd);
+ ahci_command_free(cmd);
+ qtest_memread(ahci->parent->qts, ahci->port[px].fb + 0x40, &d2h,
+ sizeof(d2h));
+ return d2h;
+}
+
+/*
+ * Boot with the Sense Data Reporting feature set enabled, and make sense data
+ * available: SET FEATURES with an unsupported subcommand is aborted, which
+ * records ILLEGAL REQUEST / INVALID FIELD IN CDB.
+ */
+static AHCIQState *ahci_boot_with_sense_data(unsigned *px)
+{
+ AHCIQState *ahci;
+
+ ahci = ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,format=%s "
+ "-M q35 -device ide-hd,drive=drive0",
+ tmp_path, imgfmt);
+ *px = ahci_port_select(ahci);
+ ahci_port_clear(ahci, *px);
+ ahci_nodata_cmd(ahci, *px, CMD_SET_FEATURES, 0xc3, 1, 0);
+ ahci_nodata_cmd(ahci, *px, CMD_SET_FEATURES, 0x47, 0, ATA_ERR_ABRT);
+ ahci_port_restart(ahci, *px);
+ return ahci;
+}
+
+/* REQUEST SENSE DATA EXT: the sense key, ASC and ASCQ as KKAAQQh */
+static uint32_t ahci_sense(AHCIQState *ahci, unsigned px)
+{
+ RegD2HFIS fis = ahci_nodata_cmd(ahci, px, CMD_REQ_SENSE_DATA, 0, 0, 0);
+
+ return (fis.lba_lo[2] & 0x0f) << 16 | fis.lba_lo[1] << 8 | fis.lba_lo[0];
+}
+
/**
* Fragmented DMA test: Perform a standard 4K DMA read/write
* test, but make sure the physical regions are fragmented to
@@ -2617,6 +2674,16 @@ static void create_ahci_io_test(enum IOMode type, enum AddrMode addr,
/******************************************************************************/
+static void test_sense_data_set_features(void)
+{
+ unsigned px;
+ AHCIQState *ahci = ahci_boot_with_sense_data(&px);
+
+ ahci_nodata_cmd(ahci, px, CMD_SET_FEATURES, 0x47, 0, ATA_ERR_ABRT);
+ g_assert_cmphex(ahci_sense(ahci, px), ==, 0x052400);
+ ahci_shutdown(ahci);
+}
+
int main(int argc, char **argv)
{
const char *arch, *base;
@@ -2708,6 +2775,8 @@ int main(int argc, char **argv)
qtest_add_func("/ahci/identify", test_identify);
qtest_add_func("/ahci/io/dma/read_log", test_read_log_dma);
qtest_add_func("/ahci/io/pio/read_log", test_read_log_pio);
+ qtest_add_func("/ahci/io/sense_data/set_features",
+ test_sense_data_set_features);
for (i = MODE_BEGIN; i < NUM_MODES; i++) {
for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
diff --git a/tests/qtest/libqos/ahci.c b/tests/qtest/libqos/ahci.c
index d50ff4767b..a4790c2f0c 100644
--- a/tests/qtest/libqos/ahci.c
+++ b/tests/qtest/libqos/ahci.c
@@ -71,6 +71,8 @@ AHCICommandProp ahci_command_properties[] = {
.lba48 = true, .read = true },
{ .cmd = CMD_READ_LOG_DMA_EXT, .data = true, .dma = true,
.lba48 = true, .read = true },
+ { .cmd = CMD_SET_FEATURES, .data = false },
+ { .cmd = CMD_REQ_SENSE_DATA, .lba48 = true },
{ .cmd = READ_FPDMA_QUEUED, .data = true, .dma = true,
.lba48 = true, .read = true, .ncq = true },
{ .cmd = WRITE_FPDMA_QUEUED, .data = true, .dma = true,
@@ -1219,6 +1221,14 @@ void ahci_command_set_ncq_subcmd(AHCICommand *cmd, uint8_t subcmd)
nfis->prio = (nfis->prio & ~0x1f) | (subcmd & 0x1f);
}
+/* Set the 16-bit FEATURE field of a (non-NCQ) command. */
+void ahci_command_set_feature(AHCICommand *cmd, uint16_t feature)
+{
+ g_assert(!cmd->props->ncq);
+ cmd->fis.feature_low = feature & 0xff;
+ cmd->fis.feature_high = (feature >> 8) & 0xff;
+}
+
void ahci_command_expect_error(AHCICommand *cmd, uint8_t err)
{
cmd->interrupts |= AHCI_PX_IS_TFES;
diff --git a/tests/qtest/libqos/ahci.h b/tests/qtest/libqos/ahci.h
index 9b40eabe35..a23ea8b2f0 100644
--- a/tests/qtest/libqos/ahci.h
+++ b/tests/qtest/libqos/ahci.h
@@ -282,6 +282,8 @@ enum {
CMD_IDENTIFY = 0xEC,
CMD_READ_LOG_EXT = 0x2F,
CMD_READ_LOG_DMA_EXT = 0x47,
+ CMD_SET_FEATURES = 0xEF,
+ CMD_REQ_SENSE_DATA = 0x0B,
CMD_PACKET = 0xA0,
CMD_PACKET_ID = 0xA1,
/* NCQ */
@@ -654,9 +656,10 @@ void ahci_command_set_sizes(AHCICommand *cmd, uint64_t xbytes,
unsigned prd_size);
void ahci_command_set_count(AHCICommand *cmd, uint16_t count);
void ahci_command_set_ncq_subcmd(AHCICommand *cmd, uint8_t subcmd);
-void ahci_command_expect_error(AHCICommand *cmd, uint8_t err);
void ahci_command_set_acmd(AHCICommand *cmd, void *acmd);
void ahci_command_enable_atapi_dma(AHCICommand *cmd);
+void ahci_command_set_feature(AHCICommand *cmd, uint16_t feature);
+void ahci_command_expect_error(AHCICommand *cmd, uint8_t err);
void ahci_command_adjust(AHCICommand *cmd, uint64_t lba_sect, uint64_t gbuffer,
uint64_t xbytes, unsigned prd_size);
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 08/13] hw/ide: add Sense Data Reporting feature set support
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:30 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev, Fabiano Rosas,
Laurent Vivier, Paolo Bonzini
Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> Implement the Sense Data Reporting feature set (ACS-6 4.25).
>
> When it is enabled, a failed command sets SENSE DATA AVAILABLE, and
> REQUEST SENSE DATA EXT (0Bh) returns the sense key, ASC and ASCQ.
> The sense data is discarded on a reset and when another command is
> accepted.
The sense data must also be reset after the execution of REQUEST SENSE DATA EXT.
> An unsupported command reports INVALID COMMAND OPERATION CODE (ACS-6
> 6.8.18), and an invalid input field reports INVALID FIELD IN CDB (6.8.19),
> both with ILLEGAL REQUEST. Other errors make no sense data available.
>
> As with NCQ and GPL, only a disk behind AHCI offers the feature set.
> This feature will be used to populate the NCQ Command Error log and to
> implement NCQ Autosense. Add a qtest that checks the sense data of an
> aborted SET FEATURES.
>
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 09/13] hw/ide: implement the NCQ Command Error log (10h)
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
` (7 preceding siblings ...)
2026-11-27 19:12 ` [PATCH 08/13] hw/ide: add Sense Data Reporting feature set support Niklas Cassel
@ 2026-11-27 19:12 ` 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
` (3 subsequent siblings)
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
As per ACS-6 9.15, when an NCQ command fails, the device enter the NCQ
error state, and the host shall read the NCQ Command Error log (10h) to
find out which command that failed.
The NCQ Command Error log is a mandatory part of the NCQ feature set.
QEMU did not implement this mandatory part, making it impossible for the
host to know which command that failed.
Record the failed command in the log, with its sense data through NCQ
Autosense. Report INVALID COMMAND OPERATION CODE for an unsupported NCQ
command, INVALID FIELD IN CDB for an invalid RECEIVE FPDMA QUEUED, and
none for a failed read or write.
A PRDT that is too short for the transfer is detected by the HBA, not
the device (AHCI 1.3.1 6.1.5), so for that case, the command is only
released and OFS raised, without recording an error.
Once the device has entered the NCQ error state (see DFPDMAQ11 to
DFPDMAQ15), the outstanding NCQ commands neither complete nor fail, and
new commands are aborted (except for a READ LOG of the NCQ Command Error
log) or the port is reset.
Add qtests for the log contents, the error state and the sense data.
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/ahci.c | 87 ++++++++++++---
hw/ide/core.c | 145 +++++++++++++++++++++++-
hw/ide/ide-internal.h | 9 ++
include/hw/ide/ide-dev.h | 17 +++
tests/qtest/ahci-test.c | 231 +++++++++++++++++++++++++++++++++++++++
5 files changed, 473 insertions(+), 16 deletions(-)
diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index d74d185607..a1fe0cc72d 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -779,9 +779,9 @@ static void ahci_unmap_clb_address(AHCIDevice *ad)
ad->lst = NULL;
}
-static void ahci_write_fis_sdb(AHCIState *s, NCQTransferState *ncq_tfs)
+static void ahci_write_fis_sdb(AHCIDevice *ad, bool irq)
{
- AHCIDevice *ad = ncq_tfs->drive;
+ AHCIState *s = ad->hba;
AHCIPortRegs *pr = &ad->port_regs;
IDEState *ide_state;
SDBFIS *sdb_fis;
@@ -796,7 +796,7 @@ static void ahci_write_fis_sdb(AHCIState *s, NCQTransferState *ncq_tfs)
sdb_fis->type = SATA_FIS_TYPE_SDB;
/* Interrupt pending & Notification bit */
- sdb_fis->flags = 0x40; /* Interrupt bit, always 1 for NCQ */
+ sdb_fis->flags = irq ? 0x40 : 0; /* Interrupt bit */
sdb_fis->status = ide_state->status & 0x77;
sdb_fis->error = ide_state->error;
/* update SAct field in SDB_FIS */
@@ -1021,12 +1021,17 @@ out:
return r;
}
-static void ncq_err(NCQTransferState *ncq_tfs)
+static void ncq_err(NCQTransferState *ncq_tfs, SCSISense sense)
{
IDEState *ide_state = &ncq_tfs->drive->port.ifs[0];
- ide_state->error = ABRT_ERR;
- ide_state->status = READY_STAT | ERR_STAT;
+ /*
+ * Record the failure (status/error registers and the NCQ Command Error
+ * log, 10h) so that the host can retrieve it after being notified of the
+ * error via the Set Device Bits FIS.
+ */
+ ide_ncq_cmd_error(ide_state, ncq_tfs->tag, ncq_tfs->lba,
+ ncq_tfs->sector_count, sense);
qemu_sglist_destroy(&ncq_tfs->sglist);
ncq_tfs->used = 0;
}
@@ -1040,7 +1045,7 @@ static void ncq_finish(NCQTransferState *ncq_tfs)
ncq_tfs->drive->finished |= (1 << ncq_tfs->tag);
}
- ahci_write_fis_sdb(ncq_tfs->drive->hba, ncq_tfs);
+ ahci_write_fis_sdb(ncq_tfs->drive, true);
trace_ncq_finish(ncq_tfs->drive->hba, ncq_tfs->drive->port_no,
ncq_tfs->tag);
@@ -1058,6 +1063,17 @@ static void ncq_cb(void *opaque, int ret)
ncq_tfs->aiocb = NULL;
+ /*
+ * The device stopped processing NCQ commands when another one failed
+ * (SATA 3.5a DFPDMAQ11): this one neither completes nor fails.
+ */
+ if (ide_state->ncq_err.active) {
+ block_acct_failed(blk_get_stats(ide_state->blk), &ncq_tfs->acct);
+ qemu_sglist_destroy(&ncq_tfs->sglist);
+ ncq_tfs->used = 0;
+ return;
+ }
+
if (ret < 0) {
bool is_read = ncq_tfs->cmd == READ_FPDMA_QUEUED;
BlockErrorAction action = blk_get_error_action(ide_state->blk,
@@ -1066,7 +1082,7 @@ static void ncq_cb(void *opaque, int ret)
ncq_tfs->halt = true;
ide_state->bus->error_status = IDE_RETRY_HBA;
} else if (action == BLOCK_ERROR_ACTION_REPORT) {
- ncq_err(ncq_tfs);
+ ncq_err(ncq_tfs, SENSE_CODE(NO_SENSE));
}
blk_error_action(ide_state->blk, action, is_read, -ret);
} else {
@@ -1114,7 +1130,7 @@ static void execute_ncq_receive_fpdma_queued(NCQTransferState *ncq_tfs)
ncq_tfs->sector_count);
if (size < 0) {
- ncq_err(ncq_tfs);
+ ncq_err(ncq_tfs, SENSE_CODE(INVALID_FIELD));
break;
}
dma_buf_read(ide_state->io_buffer, size, NULL, &ncq_tfs->sglist,
@@ -1123,7 +1139,7 @@ static void execute_ncq_receive_fpdma_queued(NCQTransferState *ncq_tfs)
break;
}
default:
- ncq_err(ncq_tfs);
+ ncq_err(ncq_tfs, SENSE_CODE(INVALID_FIELD));
break;
}
@@ -1168,7 +1184,7 @@ static void execute_ncq_command(NCQTransferState *ncq_tfs)
default:
trace_execute_ncq_command_unsup(ad->hba, port,
ncq_tfs->tag, ncq_tfs->cmd);
- ncq_err(ncq_tfs);
+ ncq_err(ncq_tfs, SENSE_CODE(INVALID_OPCODE));
ncq_finish(ncq_tfs);
}
}
@@ -1184,6 +1200,18 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
size_t size;
g_assert(is_ncq(ncq_fis->command));
+
+ /*
+ * After an NCQ command failed, any other command is aborted (SATA 3.5a
+ * DFPDMAQ12, DFPDMAQ13), until the host reads the NCQ Command Error log.
+ */
+ if (ad->port.ifs[0].ncq_err.active) {
+ ad->port.ifs[0].status = READY_STAT | ERR_STAT;
+ ad->port.ifs[0].error = ABRT_ERR;
+ ahci_write_fis_d2h(ad, false);
+ return;
+ }
+
if (ncq_tfs->used) {
/* error - already in use */
qemu_log_mask(LOG_GUEST_ERROR, "%s: tag %d already used\n",
@@ -1226,8 +1254,15 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
((uint64_t)ncq_fis->lba1 << 8) |
(uint64_t)ncq_fis->lba0;
- /* Accepting an NCQ command makes the sense data unavailable (ACS-6 4.25) */
- ad->port.ifs[0].sense_data_avail = false;
+ /*
+ * Accepting an NCQ command makes the sense data unavailable, unless it
+ * reads the NCQ Command Error log (ACS-6 4.25).
+ */
+ if (!(ncq_fis->command == RECEIVE_FPDMA_QUEUED &&
+ ncq_tfs->subcmd == RECEIVE_FPDMA_READ_LOG_DMA_EXT &&
+ (ncq_tfs->lba & 0xff) == IDE_GPL_LOG_NCQ_CMD_ERROR)) {
+ ad->port.ifs[0].sense_data_avail = false;
+ }
ncq_tfs->tag = tag;
/* Sanity-check the NCQ packet */
@@ -1260,7 +1295,12 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
error_report("ahci: PRDT length for NCQ command (0x" DMA_ADDR_FMT ") "
"is smaller than the requested size (0x%zx)",
ncq_tfs->sglist.size, size);
- ncq_err(ncq_tfs);
+ /*
+ * The HBA detected the overflow, not the device (AHCI 1.3.1 6.1.5):
+ * release the command without recording an NCQ error.
+ */
+ qemu_sglist_destroy(&ncq_tfs->sglist);
+ ncq_tfs->used = 0;
ahci_trigger_irq(ad->hba, ad, AHCI_PORT_IRQ_BIT_OFS);
return;
} else if (ncq_tfs->sglist.size != size) {
@@ -1394,6 +1434,25 @@ static void handle_reg_h2d_fis(AHCIState *s, int port,
*/
ad->busy_slot = slot;
+ /*
+ * Reading the NCQ Command Error log ends the NCQ error state: the device
+ * discards the queued commands, and reports this with a Set Device Bits
+ * FIS that has ERR cleared and all ACT bits set, but no interrupt (SATA
+ * 3.5a DFPDMAQ14, DFPDMAQ15; IDENTIFY DEVICE word 76 bit 15 is set for
+ * NCQ devices, which allows READ LOG DMA EXT).
+ */
+ if (ide_state->ncq_err.active &&
+ (cmd_fis[2] == WIN_READ_LOG_EXT ||
+ cmd_fis[2] == WIN_READ_LOG_DMA_EXT) &&
+ cmd_fis[4] == IDE_GPL_LOG_NCQ_CMD_ERROR) {
+ ahci_cancel_ncq_requests(ad);
+ ide_state->ncq_err.active = false;
+ ide_state->status = READY_STAT | SEEK_STAT;
+ ide_state->error = 0;
+ ad->finished = 0xffffffff;
+ ahci_write_fis_sdb(ad, false);
+ }
+
/* We're ready to process the command in FIS byte 2. */
ide_bus_exec_cmd(&s->dev[port].port, cmd_fis[2]);
}
diff --git a/hw/ide/core.c b/hw/ide/core.c
index 2d8bca0762..cc38a3ef65 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -204,6 +204,8 @@ static void ide_identify(IDEState *s)
put_le16(p + 76, (1 << 15) | (1 << 8));
/* 6=SEND AND RECEIVE QUEUED COMMANDS supported (SATA NCQ log 13h) */
put_le16(p + 77, (1 << 6));
+ /* SATA features supported: 7=NCQ Autosense (sense in NCQ error log) */
+ put_le16(p + 78, (1 << 7));
}
put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
@@ -595,6 +597,42 @@ void ide_abort_command(IDEState *s)
ide_abort_command_with_sense(s, SENSE_CODE(NO_SENSE));
}
+/*
+ * Record the most recent NCQ command failure so that it can be reported
+ * through the NCQ Command Error log (10h) when the host reads it. Unlike the
+ * Sense Data Reporting feature set, the NCQ Autosense sense data is always
+ * recorded (its presence in the log is gated on NCQ Autosense support).
+ */
+void ide_ncq_cmd_error(IDEState *s, uint8_t tag, uint64_t lba,
+ uint16_t count, SCSISense sense)
+{
+ s->status = READY_STAT | ERR_STAT;
+ s->error = ABRT_ERR;
+
+ /*
+ * The device processes no other command until the host reads the log or
+ * resets the device (SATA 3.5a DFPDMAQ11, DFPDMAQ13).
+ */
+ s->ncq_err.active = true;
+ s->ncq_err.valid = true;
+ s->ncq_err.tag = tag;
+ /*
+ * The STATUS field in the log advertises SENSE DATA AVAILABLE (ACS-6 6.2.9)
+ * if there is sense data, so that a host supporting NCQ Autosense reads the
+ * sense key / ASC / ASCQ from the log (libata only does so when this bit
+ * is set). NO SENSE means that there is none (ACS-6 6.8.31).
+ */
+ s->ncq_err.status = s->status |
+ (sense.key != NO_SENSE ? SENSE_DATA_STAT : 0);
+ s->ncq_err.error = s->error;
+ s->ncq_err.device = 1 << 6; /* LBA addressing */
+ s->ncq_err.lba = lba;
+ s->ncq_err.count = count;
+ s->ncq_err.sense_key = sense.key;
+ s->ncq_err.asc = sense.asc;
+ s->ncq_err.ascq = sense.ascq;
+}
+
static void ide_set_retry(IDEState *s)
{
s->bus->retry_unit = s->unit;
@@ -1472,6 +1510,7 @@ static void ide_reset(IDEState *s, IDEResetKind kind)
* covers COMRESET, which must not change it.
*/
s->sense_data_avail = false;
+ s->ncq_err.active = false;
s->lba48 = 0;
@@ -1882,6 +1921,54 @@ static void ide_fill_identify_device_log(IDEState *s, uint16_t page,
}
}
+/*
+ * Fill the NCQ Command Error log (Log Address 10h) - ACS-6 9.15, SATA 3.5a
+ * 13.7.4. Page 0 reports the most recent NCQ command failure.
+ */
+static void ide_fill_ncq_cmd_error_log(IDEState *s, uint16_t page,
+ uint32_t count)
+{
+ uint32_t p;
+
+ memset(s->io_buffer, 0, count * 512);
+
+ for (p = 0; p < count; p++) {
+ uint8_t *buf = s->io_buffer + p * 512;
+ unsigned int sum = 0;
+ int i;
+
+ if (page + p == 0) {
+ if (s->ncq_err.valid) {
+ /* NQ=0, DEFERRED=0, NCQ TAG field valid (ACS-6 9.15.2). */
+ buf[0] = s->ncq_err.tag & 0x1f;
+ buf[2] = s->ncq_err.status;
+ buf[3] = s->ncq_err.error;
+ buf[4] = s->ncq_err.lba;
+ buf[5] = s->ncq_err.lba >> 8;
+ buf[6] = s->ncq_err.lba >> 16;
+ buf[7] = s->ncq_err.device;
+ buf[8] = s->ncq_err.lba >> 24;
+ buf[9] = s->ncq_err.lba >> 32;
+ buf[10] = s->ncq_err.lba >> 40;
+ buf[12] = s->ncq_err.count;
+ buf[13] = s->ncq_err.count >> 8;
+ buf[14] = s->ncq_err.sense_key;
+ buf[15] = s->ncq_err.asc;
+ buf[16] = s->ncq_err.ascq;
+ } else {
+ /* NQ=1: the NCQ TAG field is not valid (ACS-6 9.15.3). */
+ buf[0] = 1 << 7;
+ }
+ }
+
+ /* Each page ends with a checksum making its bytes sum to zero. */
+ for (i = 0; i < 511; i++) {
+ sum += buf[i];
+ }
+ buf[511] = -sum;
+ }
+}
+
/*
* Fill the I/O buffer with the General Purpose Logging data selected by
* log_address, page and count (the LOG PAGE COUNT, in 512-byte pages) and
@@ -1898,6 +1985,12 @@ int ide_read_log(IDEState *s, uint8_t log_address, uint16_t page,
case IDE_GPL_LOG_DIRECTORY:
log_size = 1;
break;
+ case IDE_GPL_LOG_NCQ_CMD_ERROR:
+ if (!s->ncq_queues) {
+ return -1;
+ }
+ log_size = 1;
+ break;
case IDE_GPL_LOG_NCQ_SEND_RECV:
/* Only meaningful for devices that support the NCQ feature set. */
if (!s->ncq_queues) {
@@ -1933,6 +2026,7 @@ int ide_read_log(IDEState *s, uint8_t log_address, uint16_t page,
put_le16((uint16_t *)s->io_buffer + IDE_GPL_LOG_IDENTIFY_DEVICE,
IDE_GPL_LOG_IDENTIFY_DEVICE_LEN);
if (s->ncq_queues) {
+ put_le16((uint16_t *)s->io_buffer + IDE_GPL_LOG_NCQ_CMD_ERROR, 1);
put_le16((uint16_t *)s->io_buffer + IDE_GPL_LOG_NCQ_SEND_RECV, 1);
}
break;
@@ -1946,6 +2040,9 @@ int ide_read_log(IDEState *s, uint8_t log_address, uint16_t page,
*/
stl_le_p(s->io_buffer + 8, (1 << 1) | (1 << 0));
break;
+ case IDE_GPL_LOG_NCQ_CMD_ERROR:
+ ide_fill_ncq_cmd_error_log(s, page, count);
+ break;
case IDE_GPL_LOG_IDENTIFY_DEVICE:
ide_fill_identify_device_log(s, page, count);
break;
@@ -2599,11 +2696,14 @@ static bool ide_cmd_permitted(IDEState *s, uint32_t cmd)
/*
* Whether the sense data stays available once this command is accepted
- * (ACS-6 4.25): REQUEST SENSE DATA EXT returns it.
+ * (ACS-6 4.25): REQUEST SENSE DATA EXT returns it, and reading the NCQ
+ * Command Error log does not discard it.
*/
static bool ide_cmd_keeps_sense_data(IDEState *s, uint8_t cmd)
{
- return cmd == WIN_REQUEST_SENSE_DATA_EXT;
+ return cmd == WIN_REQUEST_SENSE_DATA_EXT ||
+ ((cmd == WIN_READ_LOG_EXT || cmd == WIN_READ_LOG_DMA_EXT) &&
+ s->sector == IDE_GPL_LOG_NCQ_CMD_ERROR);
}
void ide_bus_exec_cmd(IDEBus *bus, uint32_t val)
@@ -2627,6 +2727,18 @@ void ide_bus_exec_cmd(IDEBus *bus, uint32_t val)
}
}
+ /*
+ * After an NCQ command failed, any other command is aborted (SATA 3.5a
+ * DFPDMAQ13). The HBA ends that state before issuing a read of the NCQ
+ * Command Error log.
+ */
+ if (s->ncq_err.active) {
+ ide_abort_command(s);
+ ide_cmd_done(s);
+ ide_bus_set_irq(s->bus);
+ return;
+ }
+
if (!ide_cmd_permitted(s, val)) {
ide_abort_command_with_sense(s, SENSE_CODE(INVALID_OPCODE));
ide_bus_set_irq(s->bus);
@@ -3497,6 +3609,34 @@ static const VMStateDescription vmstate_ide_sense_data = {
}
};
+static bool ide_ncq_err_needed(void *opaque)
+{
+ IDEState *s = opaque;
+
+ return s->ncq_err.valid;
+}
+
+static const VMStateDescription vmstate_ide_ncq_err = {
+ .name = "ide_drive/ncq_err",
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .needed = ide_ncq_err_needed,
+ .fields = (const VMStateField[]) {
+ VMSTATE_BOOL(ncq_err.valid, IDEState),
+ VMSTATE_BOOL(ncq_err.active, IDEState),
+ VMSTATE_UINT8(ncq_err.tag, IDEState),
+ VMSTATE_UINT8(ncq_err.status, IDEState),
+ VMSTATE_UINT8(ncq_err.error, IDEState),
+ VMSTATE_UINT8(ncq_err.device, IDEState),
+ VMSTATE_UINT8(ncq_err.sense_key, IDEState),
+ VMSTATE_UINT8(ncq_err.asc, IDEState),
+ VMSTATE_UINT8(ncq_err.ascq, IDEState),
+ VMSTATE_UINT16(ncq_err.count, IDEState),
+ VMSTATE_UINT64(ncq_err.lba, IDEState),
+ VMSTATE_END_OF_LIST()
+ }
+};
+
static const VMStateDescription vmstate_ide_drive_pio_state = {
.name = "ide_drive/pio_state",
.version_id = 1,
@@ -3553,6 +3693,7 @@ const VMStateDescription vmstate_ide_drive = {
&vmstate_ide_tray_state,
&vmstate_ide_atapi_gesn_state,
&vmstate_ide_sense_data,
+ &vmstate_ide_ncq_err,
NULL
}
};
diff --git a/hw/ide/ide-internal.h b/hw/ide/ide-internal.h
index 0fb4776356..b39b5460c3 100644
--- a/hw/ide/ide-internal.h
+++ b/hw/ide/ide-internal.h
@@ -8,6 +8,7 @@
*/
#include "hw/ide/ide-bus.h"
+#include "scsi/utils.h"
/* debug IDE devices */
#define USE_DMA_CDROM
@@ -213,6 +214,7 @@
* READ LOG EXT / READ LOG DMA EXT (see ACS-6 9.1 table 222).
*/
#define IDE_GPL_LOG_DIRECTORY 0x00 /* General Purpose Log Directory */
+#define IDE_GPL_LOG_NCQ_CMD_ERROR 0x10 /* NCQ Command Error log */
#define IDE_GPL_LOG_NCQ_SEND_RECV 0x13 /* SATA NCQ Send and Receive log */
#define IDE_GPL_LOG_IDENTIFY_DEVICE 0x30 /* IDENTIFY DEVICE data log */
@@ -456,6 +458,13 @@ void ide_bus_exec_cmd(IDEBus *bus, uint32_t val);
int ide_read_log(IDEState *s, uint8_t log_address, uint16_t page,
uint32_t count);
+/*
+ * Record the most recent NCQ command failure, with its sense data, so that it
+ * can be reported through the NCQ Command Error log (10h).
+ */
+void ide_ncq_cmd_error(IDEState *s, uint8_t tag, uint64_t lba,
+ uint16_t count, SCSISense sense);
+
void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
EndTransferFunc *end_transfer_func);
bool ide_transfer_start_norecurse(IDEState *s, uint8_t *buf, int size,
diff --git a/include/hw/ide/ide-dev.h b/include/hw/ide/ide-dev.h
index 01b7952957..8cdce3b517 100644
--- a/include/hw/ide/ide-dev.h
+++ b/include/hw/ide/ide-dev.h
@@ -51,6 +51,22 @@ enum ide_dma_cmd {
IDE_DMA__COUNT
};
+/* The NCQ Command Error log (10h) and the NCQ error state */
+typedef struct IDENCQError {
+ bool valid; /* the log holds a failed NCQ command */
+ /* NCQ error: waiting for log 10h or a reset (SATA 3.5a DFPDMAQ13) */
+ bool active;
+ uint8_t tag;
+ uint8_t status;
+ uint8_t error;
+ uint8_t device;
+ uint8_t sense_key;
+ uint8_t asc;
+ uint8_t ascq;
+ uint16_t count;
+ uint64_t lba;
+} IDENCQError;
+
/* NOTE: IDEState represents in fact one drive */
struct IDEState {
IDEBus *bus;
@@ -100,6 +116,7 @@ struct IDEState {
uint8_t ascq;
bool sense_data_enabled;
bool sense_data_avail;
+ IDENCQError ncq_err;
bool tray_open;
bool tray_locked;
uint8_t cdrom_changed;
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index e7df8c9fc8..df9295906c 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1938,6 +1938,231 @@ static void test_ncq_read_log(void)
ahci_shutdown(ahci);
}
+/*
+ * A failed NCQ command is reported through the NCQ Command Error log (10h):
+ * the host is notified via the Set Device Bits FIS and then reads the log to
+ * learn which tag failed and why. Here an injected I/O error fails a WRITE
+ * FPDMA QUEUED. The error has no more specific reason, so the log has no
+ * sense data.
+ */
+static void test_ncq_cmd_error(void)
+{
+ AHCIQState *ahci;
+ AHCICommand *cmd;
+ uint16_t dir[256];
+ uint8_t *log;
+ uint64_t ptr;
+ unsigned px;
+ unsigned int sum = 0;
+ 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,cache=writeback,"
+ "rerror=report,werror=report "
+ "-M q35 -device ide-hd,drive=drive0",
+ debug_path, tmp_path, imgfmt);
+ px = ahci_port_select(ahci);
+ ahci_port_clear(ahci, px);
+
+ /* The NCQ Command Error log (10h) is advertised, one page long. */
+ ahci_io(ahci, px, CMD_READ_LOG_DMA_EXT, dir, 512, 0x00);
+ g_assert_cmphex(le16_to_cpu(dir[0x10]), ==, 1);
+
+ /* An NCQ write of one sector at LBA 8 that fails with an I/O error. */
+ ptr = ahci_alloc(ahci, 512);
+ g_assert(ptr);
+ cmd = ahci_command_create(WRITE_FPDMA_QUEUED);
+ ahci_command_set_buffer(cmd, ptr);
+ ahci_command_set_size(cmd, 512);
+ ahci_command_set_offset(cmd, 8);
+ ahci_command_expect_error(cmd, ATA_ERR_ABRT);
+ ahci_command_commit(ahci, cmd, px);
+ ahci_command_issue(ahci, cmd);
+ ahci_command_verify(ahci, cmd);
+ ahci_command_free(cmd);
+ ahci_free(ahci, ptr);
+
+ /* Read the NCQ Command Error log; page 0 records the failure. */
+ log = g_malloc0(512);
+ ahci_io(ahci, px, CMD_READ_LOG_DMA_EXT, log, 512, 0x10);
+
+ g_assert_cmphex(log[0] & (1 << 7), ==, 0); /* NQ=0: NCQ tag is valid */
+ g_assert_cmphex(log[2], ==, 0x41); /* STATUS: DRDY | ERR */
+ g_assert_cmphex(log[3], ==, 0x04); /* ERROR: ABRT */
+ g_assert_cmphex(log[4], ==, 8); /* LBA (7:0) */
+ g_assert_cmphex(log[12], ==, 1); /* COUNT (7:0) */
+ /* No sense data: SENSE KEY, ASC and ASCQ are zero. */
+ g_assert_cmphex(log[14], ==, 0x00);
+ g_assert_cmphex(log[15], ==, 0x00);
+ g_assert_cmphex(log[16], ==, 0x00);
+ for (i = 0; i < 512; i++) {
+ sum += log[i];
+ }
+ g_assert_cmphex(sum & 0xff, ==, 0); /* checksum: bytes sum to zero */
+
+ g_free(log);
+ ahci_shutdown(ahci);
+}
+
+/* The sense key that REQUEST SENSE DATA EXT returns (LBA 19:16) */
+static uint8_t ahci_sense_key(AHCIQState *ahci, unsigned px)
+{
+ return ahci_nodata_cmd(ahci, px, CMD_REQ_SENSE_DATA, 0, 0, 0).lba_lo[2] &
+ 0x0f;
+}
+
+/* READ LOG DMA EXT of the NCQ Command Error log (10h) */
+static void ahci_read_ncq_log(AHCIQState *ahci, unsigned px)
+{
+ uint8_t log[512];
+
+ ahci_io(ahci, px, CMD_READ_LOG_DMA_EXT, log, 512, 0x10);
+}
+
+/*
+ * Reading the NCQ Command Error log does not make the sense data unavailable
+ * (ACS-6 4.25), with READ LOG DMA EXT or with RECEIVE FPDMA QUEUED.
+ */
+static void test_sense_data_ncq_log(void)
+{
+ AHCICommand *cmd;
+ uint64_t ptr;
+ unsigned px;
+ AHCIQState *ahci = ahci_boot_with_sense_data(&px);
+
+ ahci_read_ncq_log(ahci, px);
+ g_assert_cmphex(ahci_sense_key(ahci, px), ==, 0x05); /* ILLEGAL REQUEST */
+
+ ahci_nodata_cmd(ahci, px, CMD_SET_FEATURES, 0x47, 0, ATA_ERR_ABRT);
+ ahci_port_restart(ahci, px);
+ ptr = ahci_alloc(ahci, 512);
+ cmd = ahci_command_create(RECEIVE_FPDMA_QUEUED);
+ ahci_command_set_ncq_subcmd(cmd, 0x01); /* READ LOG DMA EXT */
+ ahci_command_set_buffer(cmd, ptr);
+ ahci_command_set_size(cmd, 512);
+ ahci_command_set_offset(cmd, 0x10); /* LOG ADDRESS 10h */
+ ahci_command_commit(ahci, cmd, px);
+ ahci_command_issue(ahci, cmd);
+ ahci_command_verify(ahci, cmd);
+ ahci_command_free(cmd);
+ ahci_free(ahci, ptr);
+ g_assert_cmphex(ahci_sense_key(ahci, px), ==, 0x05);
+
+ ahci_shutdown(ahci);
+}
+
+/* Accepting an NCQ command makes the sense data unavailable (ACS-6 4.25). */
+static void test_sense_data_ncq_cmd(void)
+{
+ uint8_t buf[512];
+ unsigned px;
+ AHCIQState *ahci = ahci_boot_with_sense_data(&px);
+
+ ahci_io(ahci, px, READ_FPDMA_QUEUED, buf, 512, 0);
+ g_assert_cmphex(ahci_sense_key(ahci, px), ==, 0);
+
+ ahci_shutdown(ahci);
+}
+
+/* Issue a 512-byte NCQ command that the device is expected to abort */
+static void ahci_ncq_expect_abort(AHCIQState *ahci, unsigned px, uint8_t op,
+ uint8_t subcmd, uint64_t lba, uint64_t ptr)
+{
+ AHCICommand *cmd = ahci_command_create(op);
+
+ if (subcmd) {
+ ahci_command_set_ncq_subcmd(cmd, subcmd);
+ }
+ ahci_command_set_buffer(cmd, ptr);
+ ahci_command_set_size(cmd, 512);
+ ahci_command_set_offset(cmd, lba);
+ ahci_command_expect_error(cmd, ATA_ERR_ABRT);
+ ahci_command_commit(ahci, cmd, px);
+ ahci_command_issue(ahci, cmd);
+ ahci_command_verify(ahci, cmd);
+ ahci_command_free(cmd);
+}
+
+/*
+ * After an NCQ command fails, the device processes no other command until the
+ * host reads the NCQ Command Error log or resets the device (SATA 3.5a
+ * DFPDMAQ11, DFPDMAQ13). Reading the log discards the queued commands with a
+ * Set Device Bits FIS that has ERR cleared and ACT set to FFFFFFFFh
+ * (DFPDMAQ15), after which commands are processed again.
+ */
+static void test_ncq_error_state(void)
+{
+ uint8_t buf[512], sdb[8];
+ AHCIQState *ahci;
+ 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,rerror=report,werror=report "
+ "-M q35 -device ide-hd,drive=drive0",
+ debug_path, tmp_path, imgfmt);
+ px = ahci_port_select(ahci);
+ ahci_port_clear(ahci, px);
+ ptr = ahci_alloc(ahci, 512);
+
+ /* A WRITE FPDMA QUEUED that fails with the injected I/O error */
+ ahci_ncq_expect_abort(ahci, px, WRITE_FPDMA_QUEUED, 0, 0, ptr);
+
+ /*
+ * Any other command is aborted: a non-NCQ command, an NCQ command, and
+ * an NCQ read of the NCQ Command Error log itself.
+ */
+ ahci_nodata_cmd(ahci, px, CMD_SET_FEATURES, 0x02, 0, ATA_ERR_ABRT);
+ ahci_ncq_expect_abort(ahci, px, READ_FPDMA_QUEUED, 0, 0, ptr);
+ ahci_ncq_expect_abort(ahci, px, RECEIVE_FPDMA_QUEUED, 0x01, 0x10, ptr);
+
+ /* READ LOG DMA EXT of the log reports the failed command ... */
+ ahci_io(ahci, px, CMD_READ_LOG_DMA_EXT, buf, 512, 0x10);
+ g_assert_cmphex(buf[0] & (1 << 7), ==, 0); /* NQ=0 */
+ /* ... after discarding the queued commands */
+ qtest_memread(ahci->parent->qts, ahci->port[px].fb + 0x58, sdb,
+ sizeof(sdb));
+ g_assert_cmphex(sdb[0], ==, 0xa1); /* Set Device Bits FIS */
+ g_assert_cmphex(sdb[1] & 0x40, ==, 0); /* Interrupt bit */
+ g_assert_cmphex(sdb[2] & 0x01, ==, 0); /* STATUS: ERR */
+ g_assert_cmphex(sdb[3], ==, 0); /* ERROR */
+ g_assert_cmphex((uint32_t)ldl_le_p(sdb + 4), ==, 0xffffffff); /* ACT */
+
+ /* Commands are processed again */
+ ahci_io(ahci, px, READ_FPDMA_QUEUED, buf, 512, 0);
+ ahci_nodata_cmd(ahci, px, CMD_SET_FEATURES, 0x02, 0, 0);
+
+ ahci_free(ahci, ptr);
+ ahci_shutdown(ahci);
+}
+
+/*
+ * IDENTIFY DEVICE data log page 04h (Current Settings) is supported, and
+ * reports whether the Sense Data Reporting feature set is enabled.
+ */
+static void test_sense_data_current_settings(void)
+{
+ uint8_t page[512];
+ uint64_t settings;
+ unsigned px;
+ AHCIQState *ahci = ahci_boot_with_sense_data(&px);
+
+ ahci_io(ahci, px, CMD_READ_LOG_DMA_EXT, page, 512, 0x30);
+ g_assert_cmphex(page[8], ==, 8); /* number of entries */
+ g_assert_cmphex(page[13], ==, 0x04); /* page 04h listed */
+
+ ahci_io(ahci, px, CMD_READ_LOG_DMA_EXT, page, 512, 0x30 | (0x04 << 8));
+ g_assert_cmphex(page[2], ==, 0x04); /* PAGE NUMBER */
+ memcpy(&settings, page + 8, sizeof(settings));
+ /* 63=valid, 10=SENSE DATA ENABLED */
+ g_assert_cmphex(le64_to_cpu(settings) & (1ULL << 63), ==, 1ULL << 63);
+ g_assert_cmphex(le64_to_cpu(settings) & (1ULL << 10), ==, 1ULL << 10);
+
+ ahci_shutdown(ahci);
+}
+
static int prepare_iso(size_t size, unsigned char **buf, char **name)
{
g_autofree char *cdrom_path = NULL;
@@ -2808,6 +3033,12 @@ int main(int argc, char **argv)
qtest_add_func("/ahci/io/ncq/simple", test_ncq_simple);
qtest_add_func("/ahci/io/ncq/read_log", test_ncq_read_log);
+ qtest_add_func("/ahci/io/ncq/error", test_ncq_cmd_error);
+ qtest_add_func("/ahci/io/ncq/error_state", test_ncq_error_state);
+ qtest_add_func("/ahci/io/ncq/sense_data/log", test_sense_data_ncq_log);
+ qtest_add_func("/ahci/io/ncq/sense_data/cmd", test_sense_data_ncq_cmd);
+ qtest_add_func("/ahci/io/sense_data/current_settings",
+ test_sense_data_current_settings);
qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq);
qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq);
qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq);
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 09/13] hw/ide: implement the NCQ Command Error log (10h)
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:34 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev, Fabiano Rosas,
Laurent Vivier, Paolo Bonzini
Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> As per ACS-6 9.15, when an NCQ command fails, the device enter the NCQ
> error state, and the host shall read the NCQ Command Error log (10h) to
> find out which command that failed.
>
> The NCQ Command Error log is a mandatory part of the NCQ feature set.
> QEMU did not implement this mandatory part, making it impossible for the
> host to know which command that failed.
>
> Record the failed command in the log, with its sense data through NCQ
> Autosense. Report INVALID COMMAND OPERATION CODE for an unsupported NCQ
> command, INVALID FIELD IN CDB for an invalid RECEIVE FPDMA QUEUED, and
> none for a failed read or write.
>
> A PRDT that is too short for the transfer is detected by the HBA, not
> the device (AHCI 1.3.1 6.1.5), so for that case, the command is only
> released and OFS raised, without recording an error.
>
> Once the device has entered the NCQ error state (see DFPDMAQ11 to
> DFPDMAQ15), the outstanding NCQ commands neither complete nor fail, and
> new commands are aborted (except for a READ LOG of the NCQ Command Error
> log) or the port is reset.
>
> Add qtests for the log contents, the error state and the sense data.
>
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
Looks OK to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 10/13] tests/qtest/libqos: skip some checks for commands expected to fail
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
` (8 preceding siblings ...)
2026-11-27 19:12 ` [PATCH 09/13] hw/ide: implement the NCQ Command Error log (10h) Niklas Cassel
@ 2026-11-27 19:12 ` Niklas Cassel
2026-11-27 19:12 ` [PATCH 11/13] hw/ide: report out-of-range accesses as ILLEGAL REQUEST Niklas Cassel
` (2 subsequent siblings)
12 siblings, 0 replies; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
A command that fails with an error may transfer fewer bytes than
requested, or none at all. In this case the device sends no PIO Setup
FIS. For example, a READ SECTORS EXT or READ DMA EXT of a sector past
the end of the disk is aborted before any data is transferred.
Thus PRDBC stays 0, and the PIO read gets no PIO Setup FIS, so
ahci_command_verify() fails although the command failed as expected.
Skip the PRDBC and PIO Setup FIS checks for commands that are expected
to fail.
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
tests/qtest/libqos/ahci.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/tests/qtest/libqos/ahci.c b/tests/qtest/libqos/ahci.c
index a4790c2f0c..cd6f75331e 100644
--- a/tests/qtest/libqos/ahci.c
+++ b/tests/qtest/libqos/ahci.c
@@ -586,8 +586,12 @@ void ahci_port_check_cmd_sanity(AHCIQState *ahci, AHCICommand *cmd)
AHCICommandHeader cmdh;
ahci_get_command_header(ahci, cmd->port, cmd->slot, &cmdh);
- /* Physical Region Descriptor Byte Count is not required to work for NCQ. */
- if (!cmd->props->ncq) {
+ /*
+ * Physical Region Descriptor Byte Count is not required to work for NCQ.
+ * A command that failed with an error may also have transferred fewer
+ * bytes than requested, so skip the check when an error is expected.
+ */
+ if (!cmd->props->ncq && !cmd->errors) {
g_assert_cmphex(cmd->xbytes, ==, cmdh.prdbc);
}
}
@@ -1347,7 +1351,8 @@ void ahci_command_verify(AHCIQState *ahci, AHCICommand *cmd)
if (cmd->interrupts & AHCI_PX_IS_DHRS) {
ahci_port_check_d2h_sanity(ahci, port, slot);
}
- if (cmd->props->pio) {
+ /* A command aborted before any data transfer gets no PIO Setup FIS */
+ if (cmd->props->pio && !cmd->errors) {
ahci_port_check_pio_sanity(ahci, cmd);
}
}
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* [PATCH 11/13] hw/ide: report out-of-range accesses as ILLEGAL REQUEST
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
` (9 preceding siblings ...)
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 ` 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-27 19:12 ` [PATCH 13/13] hw/ide: report ACS-6 compliance behind AHCI Niklas Cassel
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
A non-queued read or write beyond the last logical sector is aborted by
ide_sect_range_ok() before any block I/O is done, without sense data.
Report sense data ILLEGAL REQUEST / LOGICAL BLOCK ADDRESS OUT OF RANGE
for non-NCQ commands trying to do this. Add a qtest for it.
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/core.c | 22 ++++++++++++++--------
tests/qtest/ahci-test.c | 32 ++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 8 deletions(-)
diff --git a/hw/ide/core.c b/hw/ide/core.c
index cc38a3ef65..a8c8a104b6 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -757,8 +757,9 @@ void ide_set_sector(IDEState *s, int64_t sector_num)
}
}
-static void ide_rw_error(IDEState *s) {
- ide_abort_command(s);
+static void ide_rw_error(IDEState *s, SCSISense sense)
+{
+ ide_abort_command_with_sense(s, sense);
ide_bus_set_irq(s->bus);
}
@@ -913,7 +914,7 @@ static void ide_sector_read(IDEState *s)
trace_ide_sector_read(sector_num, n);
if (!ide_sect_range_ok(s, sector_num, n)) {
- ide_rw_error(s);
+ ide_rw_error(s, SENSE_CODE(LBA_OUT_OF_RANGE));
block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
return;
}
@@ -945,14 +946,19 @@ void ide_set_inactive(IDEState *s, bool more)
ide_cmd_done(s);
}
-void ide_dma_error(IDEState *s)
+static void ide_dma_error_with_sense(IDEState *s, SCSISense sense)
{
ide_dma_buf_commit(s, 0);
- ide_abort_command(s);
+ ide_abort_command_with_sense(s, sense);
ide_set_inactive(s, false);
ide_bus_set_irq(s->bus);
}
+void ide_dma_error(IDEState *s)
+{
+ ide_dma_error_with_sense(s, SENSE_CODE(NO_SENSE));
+}
+
int ide_handle_rw_error(IDEState *s, int error, int op)
{
bool is_read = (op & IDE_RETRY_READ) != 0;
@@ -968,7 +974,7 @@ int ide_handle_rw_error(IDEState *s, int error, int op)
} else if (IS_IDE_RETRY_ATAPI(op)) {
ide_atapi_io_error(s, -error);
} else {
- ide_rw_error(s);
+ ide_rw_error(s, SENSE_CODE(NO_SENSE));
}
}
blk_error_action(s->blk, action, is_read, error);
@@ -1056,7 +1062,7 @@ static void ide_dma_cb(void *opaque, int ret)
if ((s->dma_cmd == IDE_DMA_READ || s->dma_cmd == IDE_DMA_WRITE) &&
!ide_sect_range_ok(s, sector_num, n)) {
- ide_dma_error(s);
+ ide_dma_error_with_sense(s, SENSE_CODE(LBA_OUT_OF_RANGE));
block_acct_failed(blk_get_stats(s->blk), &s->acct);
return;
}
@@ -1206,7 +1212,7 @@ static void ide_sector_write(IDEState *s)
trace_ide_sector_write(sector_num, n);
if (!ide_sect_range_ok(s, sector_num, n)) {
- ide_rw_error(s);
+ ide_rw_error(s, SENSE_CODE(LBA_OUT_OF_RANGE));
block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_WRITE);
return;
}
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index df9295906c..c95a95540d 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -1231,6 +1231,24 @@ static uint32_t ahci_sense(AHCIQState *ahci, unsigned px)
return (fis.lba_lo[2] & 0x0f) << 16 | fis.lba_lo[1] << 8 | fis.lba_lo[0];
}
+/* Issue a 512-byte data-in command at @lba that is expected to be aborted */
+static void ahci_read_expect_abort(AHCIQState *ahci, unsigned px, uint8_t op,
+ uint64_t lba)
+{
+ uint64_t ptr = ahci_alloc(ahci, 512);
+ AHCICommand *cmd = ahci_command_create(op);
+
+ ahci_command_set_buffer(cmd, ptr);
+ ahci_command_set_size(cmd, 512);
+ ahci_command_set_offset(cmd, lba);
+ ahci_command_expect_error(cmd, ATA_ERR_ABRT);
+ ahci_command_commit(ahci, cmd, px);
+ ahci_command_issue(ahci, cmd);
+ ahci_command_verify(ahci, cmd);
+ ahci_command_free(cmd);
+ ahci_free(ahci, ptr);
+}
+
/**
* Fragmented DMA test: Perform a standard 4K DMA read/write
* test, but make sure the physical regions are fragmented to
@@ -2909,6 +2927,19 @@ static void test_sense_data_set_features(void)
ahci_shutdown(ahci);
}
+static void test_sense_data_lba_range(void)
+{
+ const uint64_t lba = (uint64_t)test_image_size_mb * 1024 * 1024 / 512;
+ unsigned px;
+ AHCIQState *ahci = ahci_boot_with_sense_data(&px);
+
+ ahci_read_expect_abort(ahci, px, CMD_READ_PIO_EXT, lba);
+ g_assert_cmphex(ahci_sense(ahci, px), ==, 0x052100);
+ ahci_read_expect_abort(ahci, px, CMD_READ_DMA_EXT, lba);
+ g_assert_cmphex(ahci_sense(ahci, px), ==, 0x052100);
+ ahci_shutdown(ahci);
+}
+
int main(int argc, char **argv)
{
const char *arch, *base;
@@ -3002,6 +3033,7 @@ int main(int argc, char **argv)
qtest_add_func("/ahci/io/pio/read_log", test_read_log_pio);
qtest_add_func("/ahci/io/sense_data/set_features",
test_sense_data_set_features);
+ qtest_add_func("/ahci/io/sense_data/lba_range", test_sense_data_lba_range);
for (i = MODE_BEGIN; i < NUM_MODES; i++) {
for (j = ADDR_MODE_BEGIN; j < NUM_ADDR_MODES; j++) {
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 11/13] hw/ide: report out-of-range accesses as ILLEGAL REQUEST
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:35 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev, Fabiano Rosas,
Laurent Vivier, Paolo Bonzini
Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> A non-queued read or write beyond the last logical sector is aborted by
> ide_sect_range_ok() before any block I/O is done, without sense data.
>
> Report sense data ILLEGAL REQUEST / LOGICAL BLOCK ADDRESS OUT OF RANGE
> for non-NCQ commands trying to do this. Add a qtest for it.
>
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
Looks OK to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 12/13] hw/ide/ahci: check the LBA range of NCQ commands
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
` (10 preceding siblings ...)
2026-11-27 19:12 ` [PATCH 11/13] hw/ide: report out-of-range accesses as ILLEGAL REQUEST Niklas Cassel
@ 2026-11-27 19:12 ` 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
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
A NCQ command were not range checked before being accepted.
The NCQ command was queued and then failed with EIO by the block layer.
Check the range with ide_sect_range_ok() when the command is accepted,
and abort it there instead of queuing it (ACS-6 7.24.6). Report sense data
LOGICAL BLOCK ADDRESS OUT OF RANGE in the NCQ Command Error log, and
account the request as invalid. Add a test.
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/ahci.c | 44 +++++++++++++++++++++++++---------
hw/ide/core.c | 3 +--
hw/ide/ide-internal.h | 3 +++
tests/qtest/ahci-test.c | 52 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 89 insertions(+), 13 deletions(-)
diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c
index a1fe0cc72d..9f9d2a070d 100644
--- a/hw/ide/ahci.c
+++ b/hw/ide/ahci.c
@@ -1219,6 +1219,39 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
return;
}
+ ncq_tfs->lba = ((uint64_t)ncq_fis->lba5 << 40) |
+ ((uint64_t)ncq_fis->lba4 << 32) |
+ ((uint64_t)ncq_fis->lba3 << 24) |
+ ((uint64_t)ncq_fis->lba2 << 16) |
+ ((uint64_t)ncq_fis->lba1 << 8) |
+ (uint64_t)ncq_fis->lba0;
+ ncq_tfs->sector_count = ((ncq_fis->sector_count_high << 8) |
+ ncq_fis->sector_count_low);
+ if (!ncq_tfs->sector_count) {
+ ncq_tfs->sector_count = 0x10000;
+ }
+
+ /*
+ * A READ or WRITE FPDMA QUEUED beyond the last logical sector is not
+ * queued: the device aborts it when accepting it (ACS-6 7.24.6, table
+ * 398), and then waits for the host to read the NCQ Command Error log
+ * (SATA 3.5a DFPDMAQ12, DFPDMAQ13). Like the non-queued reads and writes
+ * (see ide_sect_range_ok()), it thus never reaches the block layer, so
+ * the rerror/werror policy does not apply to it.
+ */
+ if ((ncq_fis->command == READ_FPDMA_QUEUED ||
+ ncq_fis->command == WRITE_FPDMA_QUEUED) &&
+ !ide_sect_range_ok(&ad->port.ifs[0], ncq_tfs->lba,
+ ncq_tfs->sector_count)) {
+ block_acct_invalid(blk_get_stats(ad->port.ifs[0].blk),
+ ncq_fis->command == READ_FPDMA_QUEUED ?
+ BLOCK_ACCT_READ : BLOCK_ACCT_WRITE);
+ ide_ncq_cmd_error(&ad->port.ifs[0], tag, ncq_tfs->lba,
+ ncq_tfs->sector_count, SENSE_CODE(LBA_OUT_OF_RANGE));
+ ahci_write_fis_d2h(ad, true);
+ return;
+ }
+
/*
* A NCQ command clears the bit in PxCI after the command has been QUEUED
* successfully (ERROR not set, BUSY and DRQ cleared). A device reports
@@ -1247,12 +1280,6 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
ncq_tfs->cmd = ncq_fis->command;
/* SUBCOMMAND field is COUNT bits 12:8, i.e. the low 5 bits of "prio" */
ncq_tfs->subcmd = ncq_fis->prio & 0x1f;
- ncq_tfs->lba = ((uint64_t)ncq_fis->lba5 << 40) |
- ((uint64_t)ncq_fis->lba4 << 32) |
- ((uint64_t)ncq_fis->lba3 << 24) |
- ((uint64_t)ncq_fis->lba2 << 16) |
- ((uint64_t)ncq_fis->lba1 << 8) |
- (uint64_t)ncq_fis->lba0;
/*
* Accepting an NCQ command makes the sense data unavailable, unless it
@@ -1283,11 +1310,6 @@ static void process_ncq_command(AHCIState *s, int port, const uint8_t *cmd_fis,
trace_process_ncq_command_rarc(s, port, tag);
}
- ncq_tfs->sector_count = ((ncq_fis->sector_count_high << 8) |
- ncq_fis->sector_count_low);
- if (!ncq_tfs->sector_count) {
- ncq_tfs->sector_count = 0x10000;
- }
size = ncq_tfs->sector_count * BDRV_SECTOR_SIZE;
ahci_populate_sglist(ad, &ncq_tfs->sglist, ncq_tfs->cmdh, size, 0);
diff --git a/hw/ide/core.c b/hw/ide/core.c
index a8c8a104b6..31bb617390 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -429,8 +429,7 @@ static void ide_set_signature(IDEState *s)
}
}
-static bool ide_sect_range_ok(IDEState *s,
- uint64_t sector, uint64_t nb_sectors)
+bool ide_sect_range_ok(IDEState *s, uint64_t sector, uint64_t nb_sectors)
{
uint64_t total_sectors;
diff --git a/hw/ide/ide-internal.h b/hw/ide/ide-internal.h
index b39b5460c3..2de12c8105 100644
--- a/hw/ide/ide-internal.h
+++ b/hw/ide/ide-internal.h
@@ -465,6 +465,9 @@ int ide_read_log(IDEState *s, uint8_t log_address, uint16_t page,
void ide_ncq_cmd_error(IDEState *s, uint8_t tag, uint64_t lba,
uint16_t count, SCSISense sense);
+/* Whether a command may access nb_sectors sectors from sector */
+bool ide_sect_range_ok(IDEState *s, uint64_t sector, uint64_t nb_sectors);
+
void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
EndTransferFunc *end_transfer_func);
bool ide_transfer_start_norecurse(IDEState *s, uint8_t *buf, int size,
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index c95a95540d..06d090cc04 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -2181,6 +2181,57 @@ static void test_sense_data_current_settings(void)
ahci_shutdown(ahci);
}
+/*
+ * An NCQ command beyond the last logical sector is aborted with ILLEGAL
+ * REQUEST / LOGICAL BLOCK ADDRESS OUT OF RANGE in the NCQ Command Error log,
+ * without reaching the block layer: even with rerror=stop and werror=stop, it
+ * is reported to the guest rather than stopping the VM.
+ */
+static void test_ncq_lba_range(void)
+{
+ const uint64_t lba = mb_to_sectors(test_image_size_mb); /* past the end */
+ const uint8_t cmds[] = { READ_FPDMA_QUEUED, WRITE_FPDMA_QUEUED };
+ AHCIQState *ahci;
+ uint8_t log[512];
+ uint64_t ptr;
+ unsigned px;
+ int i;
+
+ ahci = ahci_boot_and_enable("-drive if=none,id=drive0,file=%s,format=%s,"
+ "rerror=stop,werror=stop "
+ "-M q35 -device ide-hd,drive=drive0",
+ tmp_path, imgfmt);
+ px = ahci_port_select(ahci);
+ ahci_port_clear(ahci, px);
+ ptr = ahci_alloc(ahci, 512);
+ g_assert(ptr);
+
+ for (i = 0; i < ARRAY_SIZE(cmds); i++) {
+ AHCICommand *cmd = ahci_command_create(cmds[i]);
+
+ ahci_command_set_buffer(cmd, ptr);
+ ahci_command_set_size(cmd, 512);
+ ahci_command_set_offset(cmd, lba);
+ ahci_command_expect_error(cmd, ATA_ERR_ABRT);
+ ahci_command_commit(ahci, cmd, px);
+ ahci_command_issue(ahci, cmd);
+ ahci_command_verify(ahci, cmd);
+ ahci_command_free(cmd);
+
+ ahci_io(ahci, px, CMD_READ_LOG_DMA_EXT, log, 512, 0x10);
+ g_assert_cmphex(log[3], ==, 0x04); /* ERROR: ABRT */
+ g_assert_cmphex(log[4] | log[5] << 8 | log[6] << 16 |
+ (uint64_t)log[8] << 24 | (uint64_t)log[9] << 32 |
+ (uint64_t)log[10] << 40, ==, lba);
+ g_assert_cmphex(log[14], ==, 0x05); /* ILLEGAL REQUEST */
+ g_assert_cmphex(log[15], ==, 0x21); /* LBA OUT OF RANGE */
+ g_assert_cmphex(log[16], ==, 0x00);
+ }
+
+ ahci_free(ahci, ptr);
+ ahci_shutdown(ahci);
+}
+
static int prepare_iso(size_t size, unsigned char **buf, char **name)
{
g_autofree char *cdrom_path = NULL;
@@ -3071,6 +3122,7 @@ int main(int argc, char **argv)
qtest_add_func("/ahci/io/ncq/sense_data/cmd", test_sense_data_ncq_cmd);
qtest_add_func("/ahci/io/sense_data/current_settings",
test_sense_data_current_settings);
+ qtest_add_func("/ahci/io/ncq/lba_range", test_ncq_lba_range);
qtest_add_func("/ahci/migrate/ncq/simple", test_migrate_ncq);
qtest_add_func("/ahci/io/ncq/retry", test_halted_ncq);
qtest_add_func("/ahci/migrate/ncq/halted", test_migrate_halted_ncq);
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 12/13] hw/ide/ahci: check the LBA range of NCQ commands
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:36 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev, Fabiano Rosas,
Laurent Vivier, Paolo Bonzini
Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> A NCQ command were not range checked before being accepted.
>
> The NCQ command was queued and then failed with EIO by the block layer.
>
> Check the range with ide_sect_range_ok() when the command is accepted,
> and abort it there instead of queuing it (ACS-6 7.24.6). Report sense data
> LOGICAL BLOCK ADDRESS OUT OF RANGE in the NCQ Command Error log, and
> account the request as invalid. Add a test.
>
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
Looks OK to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 13/13] hw/ide: report ACS-6 compliance behind AHCI
2026-11-27 19:12 [PATCH 00/13] hw/ide: add missing features required for SATA Niklas Cassel
` (11 preceding siblings ...)
2026-11-27 19:12 ` [PATCH 12/13] hw/ide/ahci: check the LBA range of NCQ commands Niklas Cassel
@ 2026-11-27 19:12 ` Niklas Cassel
2026-11-30 5:37 ` Damien Le Moal
12 siblings, 1 reply; 26+ messages in thread
From: Niklas Cassel @ 2026-11-27 19:12 UTC (permalink / raw)
To: John Snow, Denis V. Lunev, Fabiano Rosas, Laurent Vivier,
Paolo Bonzini
Cc: Damien Le Moal, Niklas Cassel, qemu-block, qemu-devel
Behind AHCI, a disk now implements the General Purpose Logging and
Sense Data Reporting feature sets, the NCQ logs and RECEIVE FPDMA
QUEUED.
Report compliance with ACS-6 in IDENTIFY DEVICE: word 80 sets
every major version bit from ATA/ATAPI-4 to ACS-6 (bits 13:4), and
word 81 reports the latest ACS-6 minor version, ACS-6 version 11
(0070h).
A disk on another controller has none of these, and keeps reporting
ATA/ATAPI-4 to ATA/ATAPI-7, with the ATA/ATAPI-5 minor version (0016h).
Correct the comment of that major version, which named ATA-3 to
ATA/ATAPI-6. Check both in the identify qtests.
Signed-off-by: Niklas Cassel <cassel@kernel.org>
---
hw/ide/core.c | 9 +++++++--
tests/qtest/ahci-test.c | 2 ++
tests/qtest/ide-test.c | 2 ++
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/hw/ide/core.c b/hw/ide/core.c
index 31bb617390..7890780b90 100644
--- a/hw/ide/core.c
+++ b/hw/ide/core.c
@@ -208,8 +208,13 @@ static void ide_identify(IDEState *s)
put_le16(p + 78, (1 << 7));
}
- put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
- put_le16(p + 81, 0x16); /* conforms to ata5 */
+ if (s->ncq_queues) {
+ put_le16(p + 80, 0x3ff0); /* ATA/ATAPI-4 -> ACS-6 supported */
+ put_le16(p + 81, 0x70); /* conforms to ACS-6 version 11 */
+ } else {
+ put_le16(p + 80, 0xf0); /* ATA/ATAPI-4 -> ATA/ATAPI-7 supported */
+ put_le16(p + 81, 0x16); /* conforms to ata5 */
+ }
/* 14=NOP supported, 5=WCACHE supported, 0=SMART supported */
put_le16(p + 82, (1 << 14) | (1 << 5) | 1);
/* 13=flush_cache_ext,12=flush_cache,10=lba48 */
diff --git a/tests/qtest/ahci-test.c b/tests/qtest/ahci-test.c
index 06d090cc04..c34c98d5cb 100644
--- a/tests/qtest/ahci-test.c
+++ b/tests/qtest/ahci-test.c
@@ -851,6 +851,8 @@ static void ahci_test_identify(AHCIQState *ahci)
sect_size = le16_to_cpu(*((uint16_t *)(&buff[5])));
g_assert_cmphex(sect_size, ==, AHCI_SECTOR_SIZE);
+ g_assert_cmphex(le16_to_cpu(buff[80]), ==, 0x3ff0);
+ g_assert_cmphex(le16_to_cpu(buff[81]), ==, 0x70);
}
static void ahci_test_io_rw_simple(AHCIQState *ahci, unsigned bufsize,
diff --git a/tests/qtest/ide-test.c b/tests/qtest/ide-test.c
index d82efdfdda..c6438326ea 100644
--- a/tests/qtest/ide-test.c
+++ b/tests/qtest/ide-test.c
@@ -749,6 +749,8 @@ static void test_identify(void)
assert_bit_clear(buf[84], 0x20);
assert_bit_clear(buf[86], 0x8000);
g_assert_cmphex(buf[119], ==, 0);
+ g_assert_cmphex(buf[80], ==, 0xf0);
+ g_assert_cmphex(buf[81], ==, 0x16);
ide_test_quit(qts);
free_pci_device(dev);
--
2.56.1
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 13/13] hw/ide: report ACS-6 compliance behind AHCI
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
0 siblings, 0 replies; 26+ messages in thread
From: Damien Le Moal @ 2026-11-30 5:37 UTC (permalink / raw)
To: Niklas Cassel, John Snow, Denis V. Lunev, Fabiano Rosas,
Laurent Vivier, Paolo Bonzini
Cc: qemu-block, qemu-devel
On 11/28/26 04:12, Niklas Cassel wrote:
> Behind AHCI, a disk now implements the General Purpose Logging and
> Sense Data Reporting feature sets, the NCQ logs and RECEIVE FPDMA
> QUEUED.
>
> Report compliance with ACS-6 in IDENTIFY DEVICE: word 80 sets
> every major version bit from ATA/ATAPI-4 to ACS-6 (bits 13:4), and
> word 81 reports the latest ACS-6 minor version, ACS-6 version 11
> (0070h).
>
> A disk on another controller has none of these, and keeps reporting
> ATA/ATAPI-4 to ATA/ATAPI-7, with the ATA/ATAPI-5 minor version (0016h).
>
> Correct the comment of that major version, which named ATA-3 to
> ATA/ATAPI-6. Check both in the identify qtests.
>
> Signed-off-by: Niklas Cassel <cassel@kernel.org>
Looks OK to me.
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 26+ messages in thread