Skip to contentSkip to conversation

AHCI Device improvements

RFC In review John Snow 2 participants 25 messages Sep 13, 2014 – Sep 16, 2014

lore
Share & tools
lore

John Snow

author
#0 lore raw

cover AHCI Device improvements

This patch series collects a number of fixes centered around improving the AHCI device.

A number of them used to be tied to as-yet unposted ahci-test patches, but I have separated them out in order to post them standalone and collect feedback.

This series as a whole fixes a number of crashes, bugs, and some specification issues that were a problem mostly in unit testing, though several observable problems with real guests are fixed by this series:

(1) Byte count after DMA completion fixes Windows 7 hibernate as well as non-ncq BSODs.

(2) FIS decomposition fixes prevent corruption when reading from / writing to sectors located beyond the LBA28 limit.
(Reported by Eniac Zhang <[email protected]>)

(3) SDB_FIS construction issues may be partly responsible for unreliable NCQ operation within windows.

John Snow (10):
ide: add is_write() macro for semantic consistency
AHCI: Update byte count after DMA completion
AHCI: Add PRD interrupt
ide: Correct handling of malformed/short PRDTs
AHCI: Rename NCQFIS structure fields
AHCI: Fix FIS decomposition
ide/ahci: Reorder error cases in handle_cmd
ahci: Check cmd_fis[1] more explicitly
ahci: factor out FIS decomposition
AHCI: Fix SDB FIS Construction

 dma-helpers.c        |   8 ++
 hw/ide/ahci.c        | 337 +++++++++++++++++++++++++++++++--------------------
 hw/ide/ahci.h        |  51 ++++++--
 hw/ide/core.c        |  17 ++-
 hw/ide/internal.h    |   3 +
 hw/ide/pci.c         |   5 +-
 include/sysemu/dma.h |   1 +
 7 files changed, 270 insertions(+), 152 deletions(-)
Signature · 1 line
1.9.3
author
#1 lore raw

RFC 1/10 ide: add is_write() macro for semantic consistency

The prepare_buf callback takes an argument named /is_write/, however in core.c we are checking to see if this DMA command is /is_read/. I am adding a small macro to correct this oversight.

Impact: Nothing, yet.
-The prepare_buf callback is only used in ahci and pci, and both
versions of this callback name the incoming argument is_write.
-Both functions ignore this hint currently, anyway.

This is therefore a simple patch to avoid future mistakes.

---
 hw/ide/core.c     | 2 +-
 hw/ide/internal.h | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
hw/ide/core.c +1 -1
index 191f893..3d682e2 100644
@@ -718,7 +718,7 @@ void ide_dma_cb(void *opaque, int ret)
     n = s->nsector;
     s->io_buffer_index = 0;
     s->io_buffer_size = n * 512;
-    if (s->bus->dma->ops->prepare_buf(s->bus->dma, ide_cmd_is_read(s)) == 0) {
+    if (s->bus->dma->ops->prepare_buf(s->bus->dma, ide_cmd_is_write(s)) == 0) {
         /* The PRDs were too short. Reset the Active bit, but don't raise an
          * interrupt. */
         s->status = READY_STAT | SEEK_STAT;
hw/ide/internal.h +2 -0
index 5c19f79..72d0147 100644
@@ -338,6 +338,8 @@ enum ide_dma_cmd {
 
 #define ide_cmd_is_read(s) \
 	((s)->dma_cmd == IDE_DMA_READ)
+#define ide_cmd_is_write(s) \
+    ((s)->dma_cmd == IDE_DMA_WRITE)
 
 /* NOTE: IDEState represents in fact one drive */
 struct IDEState {
Signature · 1 line
1.9.3
#2 lore raw

Re: [Qemu-devel] [RFC 01/10] ide: add is_write() macro for semantic consistency

Il 13/09/2014 06:34, John Snow ha scritto:

Quoted 36 lines

The prepare_buf callback takes an argument named /is_write/, however in core.c we are checking to see if this DMA command is /is_read/. I am adding a small macro to correct this oversight.

Impact: Nothing, yet.

(quoted diff) +0 -2
-The prepare_buf callback is only used in ahci and pci, and both
 versions of this callback name the incoming argument is_write.
-Both functions ignore this hint currently, anyway.

This is therefore a simple patch to avoid future mistakes.

---
 hw/ide/core.c     | 2 +-
 hw/ide/internal.h | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
hw/ide/core.c +1 -1
index 191f893..3d682e2 100644
@@ -718,7 +718,7 @@ void ide_dma_cb(void *opaque, int ret)
     n = s->nsector;
     s->io_buffer_index = 0;
     s->io_buffer_size = n * 512;
-    if (s->bus->dma->ops->prepare_buf(s->bus->dma, ide_cmd_is_read(s)) == 0) {
+    if (s->bus->dma->ops->prepare_buf(s->bus->dma, ide_cmd_is_write(s)) == 0) {
         /* The PRDs were too short. Reset the Active bit, but don't raise an
          * interrupt. */
         s->status = READY_STAT | SEEK_STAT;
hw/ide/internal.h +0 -0
index 5c19f79..72d0147 100644

hw/ide/internal.h enum ide_dma_cmd { · line 345

Quoted 8 lines
hw/ide/internal.h +2 -0
@@ -338,6 +338,8 @@ enum ide_dma_cmd {
 
 #define ide_cmd_is_read(s) \
 	((s)->dma_cmd == IDE_DMA_READ)
+#define ide_cmd_is_write(s) \
+    ((s)->dma_cmd == IDE_DMA_WRITE)
 
 /* NOTE: IDEState represents in fact one drive */
 struct IDEState {

Actually the code is right (!).

A read command corresponds to a DMA write. A write or trim will read data from memory, so it is a DMA read.

See for example how rw_buf is only used from the READ CD command, but passes 1 for the second argument of prepare_buf.

Paolo

author
#3 lore raw

Re: [Qemu-devel] [RFC 01/10] ide: add is_write() macro for semantic consistency

Quoted 48 lines · Paolo Bonzini

Il 13/09/2014 06:34, John Snow ha scritto:

Quoted 36 lines

The prepare_buf callback takes an argument named /is_write/, however in core.c we are checking to see if this DMA command is /is_read/. I am adding a small macro to correct this oversight.

Impact: Nothing, yet.

(quoted diff) +0 -2
-The prepare_buf callback is only used in ahci and pci, and both
  versions of this callback name the incoming argument is_write.
-Both functions ignore this hint currently, anyway.

This is therefore a simple patch to avoid future mistakes.

---
  hw/ide/core.c     | 2 +-
  hw/ide/internal.h | 2 ++
  2 files changed, 3 insertions(+), 1 deletion(-)
hw/ide/core.c +1 -1
index 191f893..3d682e2 100644
@@ -718,7 +718,7 @@ void ide_dma_cb(void *opaque, int ret)
      n = s->nsector;
      s->io_buffer_index = 0;
      s->io_buffer_size = n * 512;
-    if (s->bus->dma->ops->prepare_buf(s->bus->dma, ide_cmd_is_read(s)) == 0) {
+    if (s->bus->dma->ops->prepare_buf(s->bus->dma, ide_cmd_is_write(s)) == 0) {
          /* The PRDs were too short. Reset the Active bit, but don't raise an
           * interrupt. */
          s->status = READY_STAT | SEEK_STAT;
hw/ide/internal.h +0 -0
index 5c19f79..72d0147 100644

hw/ide/internal.h enum ide_dma_cmd { · line 345

Quoted 8 lines
hw/ide/internal.h +2 -0
@@ -338,6 +338,8 @@ enum ide_dma_cmd {
 
  #define ide_cmd_is_read(s) \
  	((s)->dma_cmd == IDE_DMA_READ)
+#define ide_cmd_is_write(s) \
+    ((s)->dma_cmd == IDE_DMA_WRITE)
 
  /* NOTE: IDEState represents in fact one drive */
  struct IDEState {

Actually the code is right (!).

Quoted 8 lines

A read command corresponds to a DMA write. A write or trim will read data from memory, so it is a DMA read.

See for example how rw_buf is only used from the READ CD command, but passes 1 for the second argument of prepare_buf.

Paolo

Ah, okay ... It didn't seem as if this argument was actually getting /used/ anywhere yet, so it wasn't really clear what it was intended to mean or for whom. None of the prepare_buf implementations actually seem to use this bit.

If you think this is correct the way it is, we should probably add some documentation to the prepare_buf callback to explain the nature of the argument to prevent someone from being helpful and trying to fix it again in the future.

Thanks for reviewing, this series is RFC for a reason :)

author
#4 lore raw

RFC 2/10 AHCI: Update byte count after DMA completion

In reply to John Snow

Currently, DMA read/write operations neglect to update the byte count after a successful transfer like ATAPI DMA read or PIO read/write operations do.

We correct this oversight by adding another callback into the IDEDMAOps structure. The commit callback is called whenever we are cleaning up a scatter-gather list. AHCI can register this callback in order to update post- transfer information such as byte count updates.

We use this callback in AHCI to consolidate where we delete the SGlist as generated from the PRDT, as well as update the byte count after the transfer is complete.

The QEMUSGList structure has an init flag added to it in order to make qemu_sglist_destroy a nop if it is called when there is no sglist, which simplifies cleanup and error paths.

This patch fixes several AHCI problems, notably Non-NCQ modes of operation for Windows 7 as well as Hibernate support for Windows 7.

---
 dma-helpers.c        |  5 +++++
 hw/ide/ahci.c        | 47 +++++++++++++++++++++++++++++++++++++----------
 hw/ide/core.c        | 14 +++++++++-----
 hw/ide/internal.h    |  1 +
 include/sysemu/dma.h |  1 +
 5 files changed, 53 insertions(+), 15 deletions(-)
dma-helpers.c +5 -0
index 499b52b..ba965a3 100644
@@ -45,6 +45,7 @@ void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
     qsg->as = as;
     qsg->dev = dev;
     object_ref(OBJECT(dev));
+    qsg->init = true;
 }
 
 void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
@@ -61,6 +62,10 @@ void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
 
 void qemu_sglist_destroy(QEMUSGList *qsg)
 {
+    if (!qsg->init) {
+        return;
+    }
+
     object_unref(OBJECT(qsg->dev));
     g_free(qsg->sg);
     memset(qsg, 0, sizeof(*qsg));
hw/ide/ahci.c +37 -10

88 lines not on this page · raw message

hw/ide/core.c +9 -5
index 3d682e2..b2980e9 100644
@@ -629,9 +629,13 @@ void ide_sector_read(IDEState *s)
                                   ide_sector_read_cb, s);
 }
 
-static void dma_buf_commit(IDEState *s)
+static void dma_buf_commit(IDEState *s, int xfer_ok)
 {
-    qemu_sglist_destroy(&s->sg);
+    if (s->bus->dma->ops->commit_buf) {
+        s->bus->dma->ops->commit_buf(s->bus->dma, xfer_ok);
+    } else {
+        qemu_sglist_destroy(&s->sg);
+    }
 }
 
 void ide_set_inactive(IDEState *s, bool more)
@@ -660,7 +664,7 @@ static int ide_handle_rw_error(IDEState *s, int error, int op)
         s->bus->error_status = op;
     } else if (action == BLOCK_ERROR_ACTION_REPORT) {
         if (op & IDE_RETRY_DMA) {
-            dma_buf_commit(s);
+            dma_buf_commit(s, false);
             ide_dma_error(s);
         } else {
             ide_rw_error(s);
@@ -701,7 +705,7 @@ void ide_dma_cb(void *opaque, int ret)
 
     sector_num = ide_get_sector(s);
     if (n > 0) {
-        dma_buf_commit(s);
+        dma_buf_commit(s, true);
         sector_num += n;
         ide_set_sector(s, sector_num);
         s->nsector -= n;
@@ -732,7 +736,7 @@ 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)) {
-        dma_buf_commit(s);
+        dma_buf_commit(s, false);
         ide_dma_error(s);
         return;
     }
hw/ide/internal.h +1 -0
index 72d0147..f190e8c 100644
@@ -432,6 +432,7 @@ struct IDEDMAOps {
     DMAStartFunc *start_dma;
     DMAVoidFunc *start_transfer;
     DMAIntFunc *prepare_buf;
+    DMAIntFunc *commit_buf;
     DMAIntFunc *rw_buf;
     DMAIntFunc *set_unit;
     DMAStopFunc *set_inactive;
include/sysemu/dma.h +1 -0
index 00f21f3..8d2c4d6 100644
@@ -31,6 +31,7 @@ struct QEMUSGList {
     size_t size;
     DeviceState *dev;
     AddressSpace *as;
+    bool init;
 };
 
 #ifndef CONFIG_USER_ONLY
Signature · 1 line
1.9.3
#5 lore raw

Re: [Qemu-devel] [RFC 02/10] AHCI: Update byte count after DMA completion

Il 13/09/2014 06:34, John Snow ha scritto:

Quoted 14 lines

Currently, DMA read/write operations neglect to update the byte count after a successful transfer like ATAPI DMA read or PIO read/write operations do.

We correct this oversight by adding another callback into the IDEDMAOps structure. The commit callback is called whenever we are cleaning up a scatter-gather list. AHCI can register this callback in order to update post- transfer information such as byte count updates.

We use this callback in AHCI to consolidate where we delete the SGlist as generated from the PRDT, as well as update the byte count after the transfer is complete.

Quoted 6 lines

The QEMUSGList structure has an init flag added to it in order to make qemu_sglist_destroy a nop if it is called when there is no sglist, which simplifies cleanup and error paths.

This patch fixes several AHCI problems, notably Non-NCQ modes of operation for Windows 7 as well as Hibernate support for Windows 7.

I think this patch is touching the internals more than it should. There's obviously some good stuff here, but the abstractions needed to fix the problem are already there.

Quoted 22 lines
---
 dma-helpers.c        |  5 +++++
 hw/ide/ahci.c        | 47 +++++++++++++++++++++++++++++++++++++----------
 hw/ide/core.c        | 14 +++++++++-----
 hw/ide/internal.h    |  1 +
 include/sysemu/dma.h |  1 +
 5 files changed, 53 insertions(+), 15 deletions(-)
dma-helpers.c +1 -0
index 499b52b..ba965a3 100644
@@ -45,6 +45,7 @@ void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
     qsg->as = as;
     qsg->dev = dev;
     object_ref(OBJECT(dev));
+    qsg->init = true;
 }
 
 void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)

dma-helpers.c void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len) · line 71

Quoted 10 lines
dma-helpers.c +4 -0
@@ -61,6 +62,10 @@ void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
 
 void qemu_sglist_destroy(QEMUSGList *qsg)
 {
+    if (!qsg->init) {
+        return;
+    }
+
     object_unref(OBJECT(qsg->dev));
     g_free(qsg->sg);
     memset(qsg, 0, sizeof(*qsg));

Why do you need this? qemu_sglist_destroy is idempotent (neither free nor unref of NULL cause problems).

Quoted 57 lines
hw/ide/ahci.c +18 -6
index 0ee713b..d3ece78 100644
@@ -48,6 +48,9 @@ static int handle_cmd(AHCIState *s,int port,int slot);
 static void ahci_reset_port(AHCIState *s, int port);
 static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis);
 static void ahci_init_d2h(AHCIDevice *ad);
+static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write);
+static int ahci_commit_buf(IDEDMA *dma, int xfer_ok);
+
 
 static uint32_t  ahci_port_read(AHCIState *s, int port, int offset)
 {
@@ -1084,16 +1087,12 @@ static void ahci_start_transfer(IDEDMA *dma)
         }
     }
 
-    /* update number of transferred bytes */
-    ad->cur_cmd->status = cpu_to_le32(le32_to_cpu(ad->cur_cmd->status) + size);
-
 out:
     /* declare that we processed everything */
     s->data_ptr = s->data_end;
 
-    if (has_sglist) {
-        qemu_sglist_destroy(&s->sg);
-    }
+    /* Update number of transferred bytes, destroy sglist */
+    ahci_commit_buf(dma, true);
 
     s->end_transfer_func(s);
 
@@ -1114,6 +1113,11 @@ static void ahci_start_dma(IDEDMA *dma, IDEState *s,
     dma_cb(s, 0);
 }
 
+/**
+ * Called in DMA R/W chains to read the PRDTL, utilizing ahci_populate_sglist.
+ * Not currently invoked by PIO R/W chains,
+ * which invoke ahci_populate_sglist via ahci_start_transfer.
+ */
 static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write)
 {
     AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
@@ -1126,6 +1130,30 @@ static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write)
     return s->io_buffer_size != 0;
 }
 
+/**
+ * Destroys the scatter-gather list,
+ * and updates the command header with a bytes-read value.
+ * called explicitly via ahci_dma_rw_buf (ATAPI DMA),
+ * and ahci_start_transfer (PIO R/W),
+ * and called via callback from ide_dma_cb for DMA R/W paths.
+ */
+static int ahci_commit_buf(IDEDMA *dma, int xfer_ok)

hw/ide/ahci.c static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write) · line 1150

Quoted 10 lines
hw/ide/ahci.c +10 -0
+{
+    AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
+    IDEState *s = &ad->port.ifs[0];
+    uint32_t byte_count;
+
+    if (xfer_ok) {
+        byte_count = le32_to_cpu(ad->cur_cmd->status);
+        byte_count += s->sg.size;
+        ad->cur_cmd->status = cpu_to_le32(byte_count);
+    }

If you leave qemu_sglist_destroy in the caller, you do not need to call ahci_commit_buf in the error cases. Maybe I'm wrong, but I have a feeling that it would be much simpler to me if your commit hook is simply

       byte_count += le32_to_cpu(ad->cur_cmd->status);
       ad->cur_cmd->status = cpu_to_le32(byte_count);

where byte_count is the second argument to the hook. The callers can pass 0 in the error case, s->sg.size otherwise.

hw/ide/ahci.c static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write) · line 1154

Quoted 3 lines
hw/ide/ahci.c +3 -0
+    qemu_sglist_destroy(&s->sg);
+
+    return s->sg.size != 0;

Also, there's no reason to keep the return value (which is always false).

hw/ide/ahci.c static int ahci_dma_rw_buf(IDEDMA *dma, int is_write) · line 1175

Quoted 13 lines
hw/ide/ahci.c +4 -2
+}
+
 static int ahci_dma_rw_buf(IDEDMA *dma, int is_write)
 {
     AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
@@ -1143,11 +1171,9 @@ static int ahci_dma_rw_buf(IDEDMA *dma, int is_write)
         dma_buf_write(p, l, &s->sg);
     }
 
-    /* free sglist that was created in ahci_populate_sglist() */
-    qemu_sglist_destroy(&s->sg);
+    /* free sglist, update byte count */
+    ahci_commit_buf(dma, true);

Perhaps you should make dma_buf_commit public (and add the call to the hook), so that ahci_commit_buf does not have to mess with &s->sg.

Quoted 18 lines
hw/ide/ahci.c +1 -2
-    /* update number of transferred bytes */
-    ad->cur_cmd->status = cpu_to_le32(le32_to_cpu(ad->cur_cmd->status) + l);
     s->io_buffer_index += l;
     s->io_buffer_offset += l;
 
@@ -1190,6 +1216,7 @@ static const IDEDMAOps ahci_dma_ops = {
     .start_dma = ahci_start_dma,
     .start_transfer = ahci_start_transfer,
     .prepare_buf = ahci_dma_prepare_buf,
+    .commit_buf = ahci_commit_buf,
     .rw_buf = ahci_dma_rw_buf,
     .set_unit = ahci_dma_set_unit,
     .cmd_done = ahci_cmd_done,
hw/ide/core.c +0 -0
index 3d682e2..b2980e9 100644

hw/ide/core.c void ide_sector_read(IDEState *s) · line 636

Quoted 10 lines
hw/ide/core.c +4 -2
@@ -629,9 +629,13 @@ void ide_sector_read(IDEState *s)
                                   ide_sector_read_cb, s);
 }
 
-static void dma_buf_commit(IDEState *s)
+static void dma_buf_commit(IDEState *s, int xfer_ok)
 {
-    qemu_sglist_destroy(&s->sg);
+    if (s->bus->dma->ops->commit_buf) {
+        s->bus->dma->ops->commit_buf(s->bus->dma, xfer_ok);
+    } else {

The "else" is not necessary, I think.

hw/ide/core.c static int ide_handle_rw_error(IDEState *s, int error, int op) · line 667

Quoted 11 lines
hw/ide/core.c +3 -1
+        qemu_sglist_destroy(&s->sg);
+    }
 }
 
 void ide_set_inactive(IDEState *s, bool more)
@@ -660,7 +664,7 @@ static int ide_handle_rw_error(IDEState *s, int error, int op)
         s->bus->error_status = op;
     } else if (action == BLOCK_ERROR_ACTION_REPORT) {
         if (op & IDE_RETRY_DMA) {
-            dma_buf_commit(s);
+            dma_buf_commit(s, false);

For the two error cases, it makes sense to move the call to dma_buf_commit from the callers to ide_dma_error.

Quoted 46 lines
hw/ide/core.c +2 -2
             ide_dma_error(s);
         } else {
             ide_rw_error(s);
@@ -701,7 +705,7 @@ void ide_dma_cb(void *opaque, int ret)
 
     sector_num = ide_get_sector(s);
     if (n > 0) {
-        dma_buf_commit(s);
+        dma_buf_commit(s, true);
         sector_num += n;
         ide_set_sector(s, sector_num);
         s->nsector -= n;
@@ -732,7 +736,7 @@ 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)) {
-        dma_buf_commit(s);
+        dma_buf_commit(s, false);
         ide_dma_error(s);
         return;
     }
hw/ide/internal.h +1 -0
index 72d0147..f190e8c 100644
@@ -432,6 +432,7 @@ struct IDEDMAOps {
     DMAStartFunc *start_dma;
     DMAVoidFunc *start_transfer;
     DMAIntFunc *prepare_buf;
+    DMAIntFunc *commit_buf;
     DMAIntFunc *rw_buf;
     DMAIntFunc *set_unit;
     DMAStopFunc *set_inactive;
include/sysemu/dma.h +1 -0
index 00f21f3..8d2c4d6 100644
@@ -31,6 +31,7 @@ struct QEMUSGList {
     size_t size;
     DeviceState *dev;
     AddressSpace *as;
+    bool init;
 };
 
 #ifndef CONFIG_USER_ONLY
author
#6 lore raw

Re: [Qemu-devel] [RFC 02/10] AHCI: Update byte count after DMA completion

Quoted 19 lines · Paolo Bonzini

Il 13/09/2014 06:34, John Snow ha scritto:

Quoted 18 lines

Currently, DMA read/write operations neglect to update the byte count after a successful transfer like ATAPI DMA read or PIO read/write operations do.

We correct this oversight by adding another callback into the IDEDMAOps structure. The commit callback is called whenever we are cleaning up a scatter-gather list. AHCI can register this callback in order to update post- transfer information such as byte count updates.

We use this callback in AHCI to consolidate where we delete the SGlist as generated from the PRDT, as well as update the byte count after the transfer is complete.

The QEMUSGList structure has an init flag added to it in order to make qemu_sglist_destroy a nop if it is called when there is no sglist, which simplifies cleanup and error paths.

Quoted 6 lines
Quoted 2 lines

This patch fixes several AHCI problems, notably Non-NCQ modes of operation for Windows 7 as well as Hibernate support for Windows 7.

I think this patch is touching the internals more than it should. There's obviously some good stuff here, but the abstractions needed to fix the problem are already there.

OK. I tried to clarify below with what I was trying to do, please let me know if you want me to take it in a different direction.

Quoted 24 lines
Quoted 24 lines
---
  dma-helpers.c        |  5 +++++
  hw/ide/ahci.c        | 47 +++++++++++++++++++++++++++++++++++++----------
  hw/ide/core.c        | 14 +++++++++-----
  hw/ide/internal.h    |  1 +
  include/sysemu/dma.h |  1 +
  5 files changed, 53 insertions(+), 15 deletions(-)
dma-helpers.c +1 -0
index 499b52b..ba965a3 100644
@@ -45,6 +45,7 @@ void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
      qsg->as = as;
      qsg->dev = dev;
      object_ref(OBJECT(dev));
+    qsg->init = true;
  }
 
  void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
@@ -61,6 +62,10 @@ void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len)
 
Quoted 12 lines

dma-helpers.c void qemu_sglist_add(QEMUSGList *qsg, dma_addr_t base, dma_addr_t len) · line 71

Quoted 9 lines
dma-helpers.c +4 -0
  void qemu_sglist_destroy(QEMUSGList *qsg)
  {
+    if (!qsg->init) {
+        return;
+    }
+
      object_unref(OBJECT(qsg->dev));
      g_free(qsg->sg);
      memset(qsg, 0, sizeof(*qsg));

Why do you need this? qemu_sglist_destroy is idempotent (neither free nor unref of NULL cause problems).

No reason other than I wasn't completely sure of how unref operated. Was just being very conservative in my PoC. I wanted to be able to be lazy and call sglist_destroy with or without "has_sglist" elsewhere in the DMA code, so the bool just got kicked elsewhere.

I will delete it.

Quoted 73 lines
Quoted 58 lines
hw/ide/ahci.c +18 -6
index 0ee713b..d3ece78 100644
@@ -48,6 +48,9 @@ static int handle_cmd(AHCIState *s,int port,int slot);
  static void ahci_reset_port(AHCIState *s, int port);
  static void ahci_write_fis_d2h(AHCIDevice *ad, uint8_t *cmd_fis);
  static void ahci_init_d2h(AHCIDevice *ad);
+static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write);
+static int ahci_commit_buf(IDEDMA *dma, int xfer_ok);
+
 
  static uint32_t  ahci_port_read(AHCIState *s, int port, int offset)
  {
@@ -1084,16 +1087,12 @@ static void ahci_start_transfer(IDEDMA *dma)
          }
      }
 
-    /* update number of transferred bytes */
-    ad->cur_cmd->status = cpu_to_le32(le32_to_cpu(ad->cur_cmd->status) + size);
-
  out:
      /* declare that we processed everything */
      s->data_ptr = s->data_end;
 
-    if (has_sglist) {
-        qemu_sglist_destroy(&s->sg);
-    }
+    /* Update number of transferred bytes, destroy sglist */
+    ahci_commit_buf(dma, true);
 
      s->end_transfer_func(s);
 
@@ -1114,6 +1113,11 @@ static void ahci_start_dma(IDEDMA *dma, IDEState *s,
      dma_cb(s, 0);
  }
 
+/**
+ * Called in DMA R/W chains to read the PRDTL, utilizing ahci_populate_sglist.
+ * Not currently invoked by PIO R/W chains,
+ * which invoke ahci_populate_sglist via ahci_start_transfer.
+ */
  static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write)
  {
      AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
@@ -1126,6 +1130,30 @@ static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write)
      return s->io_buffer_size != 0;
  }
 
+/**
+ * Destroys the scatter-gather list,
+ * and updates the command header with a bytes-read value.
+ * called explicitly via ahci_dma_rw_buf (ATAPI DMA),
+ * and ahci_start_transfer (PIO R/W),
+ * and called via callback from ide_dma_cb for DMA R/W paths.
+ */
+static int ahci_commit_buf(IDEDMA *dma, int xfer_ok)

hw/ide/ahci.c static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write) · line 1150

Quoted 10 lines
hw/ide/ahci.c +10 -0
+{
+    AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
+    IDEState *s = &ad->port.ifs[0];
+    uint32_t byte_count;
+
+    if (xfer_ok) {
+        byte_count = le32_to_cpu(ad->cur_cmd->status);
+        byte_count += s->sg.size;
+        ad->cur_cmd->status = cpu_to_le32(byte_count);
+    }

If you leave qemu_sglist_destroy in the caller, you do not need to call ahci_commit_buf in the error cases. Maybe I'm wrong, but I have a feeling that it would be much simpler to me if your commit hook is simply

Quoted 6 lines
        byte_count += le32_to_cpu(ad->cur_cmd->status);
        ad->cur_cmd->status = cpu_to_le32(byte_count);

where byte_count is the second argument to the hook. The callers can pass 0 in the error case, s->sg.size otherwise.

In my mind, I was trying to create an analog to "prepare_buf," where prepare_buf is responsible for instantiating the sglist, and "commit_buf" is responsible for tearing it down.

I felt that since the preparation callback instantiated it, it would make sense to keep the teardown in the same layer.

Quoted 6 lines

hw/ide/ahci.c static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write) · line 1154

Quoted 3 lines
hw/ide/ahci.c +3 -0
+    qemu_sglist_destroy(&s->sg);
+
+    return s->sg.size != 0;

Also, there's no reason to keep the return value (which is always false).

Yes, not strictly meaningful. Just a holdover from hunting down the short PRDT issues. I will just get rid of the return value.

Quoted 9 lines
Quoted 9 lines
hw/ide/ahci.c +2 -0
+}
+
  static int ahci_dma_rw_buf(IDEDMA *dma, int is_write)
  {
      AHCIDevice *ad = DO_UPCAST(AHCIDevice, dma, dma);
@@ -1143,11 +1171,9 @@ static int ahci_dma_rw_buf(IDEDMA *dma, int is_write)
          dma_buf_write(p, l, &s->sg);
      }
 
Quoted 8 lines

hw/ide/ahci.c static int ahci_dma_rw_buf(IDEDMA *dma, int is_write) · line 1175

Quoted 4 lines
hw/ide/ahci.c +2 -2
-    /* free sglist that was created in ahci_populate_sglist() */
-    qemu_sglist_destroy(&s->sg);
+    /* free sglist, update byte count */
+    ahci_commit_buf(dma, true);

Perhaps you should make dma_buf_commit public (and add the call to the hook), so that ahci_commit_buf does not have to mess with &s->sg.

As above, the ahci layer is already messing around with &s->sg during preparation, so I don't see why I'd try to abstract this away from parts of that file.

Unless I'm misunderstanding you.

Quoted 21 lines
Quoted 21 lines
hw/ide/ahci.c +1 -2
-    /* update number of transferred bytes */
-    ad->cur_cmd->status = cpu_to_le32(le32_to_cpu(ad->cur_cmd->status) + l);
      s->io_buffer_index += l;
      s->io_buffer_offset += l;
 
@@ -1190,6 +1216,7 @@ static const IDEDMAOps ahci_dma_ops = {
      .start_dma = ahci_start_dma,
      .start_transfer = ahci_start_transfer,
      .prepare_buf = ahci_dma_prepare_buf,
+    .commit_buf = ahci_commit_buf,
      .rw_buf = ahci_dma_rw_buf,
      .set_unit = ahci_dma_set_unit,
      .cmd_done = ahci_cmd_done,
hw/ide/core.c +0 -0
index 3d682e2..b2980e9 100644
@@ -629,9 +629,13 @@ void ide_sector_read(IDEState *s)
                                    ide_sector_read_cb, s);
  }
 
Quoted 10 lines

hw/ide/core.c void ide_sector_read(IDEState *s) · line 636

Quoted 7 lines
hw/ide/core.c +4 -2
-static void dma_buf_commit(IDEState *s)
+static void dma_buf_commit(IDEState *s, int xfer_ok)
  {
-    qemu_sglist_destroy(&s->sg);
+    if (s->bus->dma->ops->commit_buf) {
+        s->bus->dma->ops->commit_buf(s->bus->dma, xfer_ok);
+    } else {

The "else" is not necessary, I think.

As long as I add the callback to the PCI BMDMA layer, you're right. If we follow along with my "create" and "destroy" analogy above, we can just do the dispatch here and let PCI or AHCI do the teardown without having a fallback.

Quoted 4 lines
Quoted 4 lines
hw/ide/core.c +2 -0
+        qemu_sglist_destroy(&s->sg);
+    }
  }
 
Quoted 11 lines

hw/ide/core.c static int ide_handle_rw_error(IDEState *s, int error, int op) · line 667

Quoted 7 lines
hw/ide/core.c +1 -1
  void ide_set_inactive(IDEState *s, bool more)
@@ -660,7 +664,7 @@ static int ide_handle_rw_error(IDEState *s, int error, int op)
          s->bus->error_status = op;
      } else if (action == BLOCK_ERROR_ACTION_REPORT) {
          if (op & IDE_RETRY_DMA) {
-            dma_buf_commit(s);
+            dma_buf_commit(s, false);

For the two error cases, it makes sense to move the call to dma_buf_commit from the callers to ide_dma_error.

Sure, I can add that small cleanup.

Quoted 49 lines
Quoted 46 lines
hw/ide/core.c +2 -2
              ide_dma_error(s);
          } else {
              ide_rw_error(s);
@@ -701,7 +705,7 @@ void ide_dma_cb(void *opaque, int ret)
 
      sector_num = ide_get_sector(s);
      if (n > 0) {
-        dma_buf_commit(s);
+        dma_buf_commit(s, true);
          sector_num += n;
          ide_set_sector(s, sector_num);
          s->nsector -= n;
@@ -732,7 +736,7 @@ 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)) {
-        dma_buf_commit(s);
+        dma_buf_commit(s, false);
          ide_dma_error(s);
          return;
      }
hw/ide/internal.h +1 -0
index 72d0147..f190e8c 100644
@@ -432,6 +432,7 @@ struct IDEDMAOps {
      DMAStartFunc *start_dma;
      DMAVoidFunc *start_transfer;
      DMAIntFunc *prepare_buf;
+    DMAIntFunc *commit_buf;
      DMAIntFunc *rw_buf;
      DMAIntFunc *set_unit;
      DMAStopFunc *set_inactive;
include/sysemu/dma.h +1 -0
index 00f21f3..8d2c4d6 100644
@@ -31,6 +31,7 @@ struct QEMUSGList {
      size_t size;
      DeviceState *dev;
      AddressSpace *as;
+    bool init;
  };
 
  #ifndef CONFIG_USER_ONLY
#7 lore raw

Re: [Qemu-devel] [RFC 02/10] AHCI: Update byte count after DMA completion

Il 15/09/2014 22:07, John Snow ha scritto:

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
hw/ide/ahci.c +2 -2
-    /* free sglist that was created in ahci_populate_sglist() */
-    qemu_sglist_destroy(&s->sg);
+    /* free sglist, update byte count */
+    ahci_commit_buf(dma, true);
Quoted 7 lines
Quoted 3 lines

Perhaps you should make dma_buf_commit public (and add the call to the hook), so that ahci_commit_buf does not have to mess with &s->sg.

As above, the ahci layer is already messing around with &s->sg during preparation, so I don't see why I'd try to abstract this away from parts of that file.

I think it would make sense if you moved the QEMUSGList to the DMA layer instead of the IDE layer. However, if you leave it in the IDE layer, it makes sense to leave qemu_sglist_destroy within the IDE layer as well. That's because qemu_sglist_destroy is idempotent.

Moving the QEMUSGList to the DMA layer would be a relatively large change, so I would keep it where it is now.

Paolo

author
#8 lore raw

RFC 3/10 AHCI: Add PRD interrupt

In reply to John Snow

AHCI devices support a feature where individual entries in the scatter-gather list may have interrupt request bits set, in order to receive notification partway through a command that a portion of a transfer has completed. AHCI specs refer to this as the DPS bit or Descriptor Processed Status. It is named the "Interrupt on Completion" bit inside the PRDT.

This is not currently feasible in QEMU without reworking the inner DMA loop extensively, but we can at least record if we saw such a bit in advance and sent the interrupt at the end of the transfer, in case a guest is expecting to see the PRD/DPS interrupt bit set.

---
 hw/ide/ahci.c | 11 +++++++++++
 hw/ide/ahci.h |  5 ++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
hw/ide/ahci.c +11 -0
index d3ece78..8e6a352 100644
@@ -501,6 +501,7 @@ static void ahci_reset_port(AHCIState *s, int port)
     pr->scr_err = 0;
     pr->scr_act = 0;
     d->busy_slot = -1;
+    d->dp_intr_req = false;
     d->init_d2h_sent = false;
 
     ide_state = &s->dev[port].port.ifs[0];
@@ -775,11 +776,15 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist, int offset)
                          ad->hba->as);
         qemu_sglist_add(sglist, le64_to_cpu(tbl[off_idx].addr + off_pos),
                         prdt_tbl_entry_size(&tbl[off_idx]) - off_pos);
+        ad->dp_intr_req = le32_to_cpu(tbl[off_idx].flags_size) & AHCI_PRDT_INTR;
 
         for (i = off_idx + 1; i < sglist_alloc_hint; i++) {
             /* flags_size is zero-based */
             qemu_sglist_add(sglist, le64_to_cpu(tbl[i].addr),
                             prdt_tbl_entry_size(&tbl[i]));
+            if (le32_to_cpu(tbl[i].flags_size) & AHCI_PRDT_INTR) {
+                ad->dp_intr_req = 1;
+            }
         }
     }
 
@@ -1151,6 +1156,11 @@ static int ahci_commit_buf(IDEDMA *dma, int xfer_ok)
 
     qemu_sglist_destroy(&s->sg);
 
+    if (ad->dp_intr_req) {
+        ahci_trigger_irq(ad->hba, ad, PORT_IRQ_SG_DONE);
+        ad->dp_intr_req = 0;
+    }
+
     return s->sg.size != 0;
 }
 
@@ -1307,6 +1317,7 @@ static const VMStateDescription vmstate_ahci_device = {
         VMSTATE_UINT32(port_regs.cmd_issue, AHCIDevice),
         VMSTATE_BOOL(done_atapi_packet, AHCIDevice),
         VMSTATE_INT32(busy_slot, AHCIDevice),
+        VMSTATE_BOOL(dp_intr_req, AHCIDevice),
         VMSTATE_BOOL(init_d2h_sent, AHCIDevice),
         VMSTATE_END_OF_LIST()
     },
hw/ide/ahci.h +4 -1
index 1543df7..31f32d3 100644
@@ -180,7 +180,9 @@
 
 #define AHCI_COMMAND_TABLE_ACMD            0x40
 
-#define AHCI_PRDT_SIZE_MASK                0x3fffff
+#define AHCI_PRDT_SIZE_MASK                0x003fffff
+#define AHCI_PRDT_RESERVED                 0x7fc00000
+#define AHCI_PRDT_INTR                     0x80000000
 
 #define IDE_FEATURE_DMA                    1
 
@@ -265,6 +267,7 @@ struct AHCIDevice {
     bool done_atapi_packet;
     int32_t busy_slot;
     bool init_d2h_sent;
+    bool dp_intr_req;
     AHCICmdHdr *cur_cmd;
     NCQTransferState ncq_tfs[AHCI_MAX_CMDS];
 };
Signature · 1 line
1.9.3
#9 lore raw

Re: [Qemu-devel] [RFC 03/10] AHCI: Add PRD interrupt

Il 13/09/2014 06:34, John Snow ha scritto:

Quoted 31 lines

AHCI devices support a feature where individual entries in the scatter-gather list may have interrupt request bits set, in order to receive notification partway through a command that a portion of a transfer has completed. AHCI specs refer to this as the DPS bit or Descriptor Processed Status. It is named the "Interrupt on Completion" bit inside the PRDT.

This is not currently feasible in QEMU without reworking the inner DMA loop extensively, but we can at least record if we saw such a bit in advance and sent the interrupt at the end of the transfer, in case a guest is expecting to see the PRD/DPS interrupt bit set.

---
 hw/ide/ahci.c | 11 +++++++++++
 hw/ide/ahci.h |  5 ++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
hw/ide/ahci.c +1 -0
index d3ece78..8e6a352 100644
@@ -501,6 +501,7 @@ static void ahci_reset_port(AHCIState *s, int port)
     pr->scr_err = 0;
     pr->scr_act = 0;
     d->busy_slot = -1;
+    d->dp_intr_req = false;
     d->init_d2h_sent = false;
 
     ide_state = &s->dev[port].port.ifs[0];

hw/ide/ahci.c static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist, int offset) · line 779

Quoted 4 lines
hw/ide/ahci.c +1 -0
@@ -775,11 +776,15 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist, int offset)
                          ad->hba->as);
         qemu_sglist_add(sglist, le64_to_cpu(tbl[off_idx].addr + off_pos),
                         prdt_tbl_entry_size(&tbl[off_idx]) - off_pos);
+        ad->dp_intr_req = le32_to_cpu(tbl[off_idx].flags_size) & AHCI_PRDT_INTR;

Why is this also not an "OR"? It feels safer that way.

Quoted 11 lines
hw/ide/ahci.c +3 -0
         for (i = off_idx + 1; i < sglist_alloc_hint; i++) {
             /* flags_size is zero-based */
             qemu_sglist_add(sglist, le64_to_cpu(tbl[i].addr),
                             prdt_tbl_entry_size(&tbl[i]));
+            if (le32_to_cpu(tbl[i].flags_size) & AHCI_PRDT_INTR) {
+                ad->dp_intr_req = 1;
+            }
         }
     }
 

hw/ide/ahci.c static int ahci_commit_buf(IDEDMA *dma, int xfer_ok) · line 1162

Quoted 7 lines
hw/ide/ahci.c +4 -0
@@ -1151,6 +1156,11 @@ static int ahci_commit_buf(IDEDMA *dma, int xfer_ok)
 
     qemu_sglist_destroy(&s->sg);
 
+    if (ad->dp_intr_req) {
+        ahci_trigger_irq(ad->hba, ad, PORT_IRQ_SG_DONE);
+        ad->dp_intr_req = 0;
+    }

Is it also needed in the error case? Especially the short-PRDT case that you are adding in the next patch.

Quoted 35 lines
hw/ide/ahci.c +1 -0
     return s->sg.size != 0;
 }
 
@@ -1307,6 +1317,7 @@ static const VMStateDescription vmstate_ahci_device = {
         VMSTATE_UINT32(port_regs.cmd_issue, AHCIDevice),
         VMSTATE_BOOL(done_atapi_packet, AHCIDevice),
         VMSTATE_INT32(busy_slot, AHCIDevice),
+        VMSTATE_BOOL(dp_intr_req, AHCIDevice),
         VMSTATE_BOOL(init_d2h_sent, AHCIDevice),
         VMSTATE_END_OF_LIST()
     },
hw/ide/ahci.h +4 -1
index 1543df7..31f32d3 100644
@@ -180,7 +180,9 @@
 
 #define AHCI_COMMAND_TABLE_ACMD            0x40
 
-#define AHCI_PRDT_SIZE_MASK                0x3fffff
+#define AHCI_PRDT_SIZE_MASK                0x003fffff
+#define AHCI_PRDT_RESERVED                 0x7fc00000
+#define AHCI_PRDT_INTR                     0x80000000
 
 #define IDE_FEATURE_DMA                    1
 
@@ -265,6 +267,7 @@ struct AHCIDevice {
     bool done_atapi_packet;
     int32_t busy_slot;
     bool init_d2h_sent;
+    bool dp_intr_req;
     AHCICmdHdr *cur_cmd;
     NCQTransferState ncq_tfs[AHCI_MAX_CMDS];
 };
#10 lore raw

Re: [Qemu-devel] [RFC 03/10] AHCI: Add PRD interrupt

Il 13/09/2014 15:26, Paolo Bonzini ha scritto:

Quoted 8 lines
Quoted 5 lines

hw/ide/ahci.c static int ahci_commit_buf(IDEDMA *dma, int xfer_ok) · line 1162

Quoted 5 lines
hw/ide/ahci.c +4 -0
+    if (ad->dp_intr_req) {
+        ahci_trigger_irq(ad->hba, ad, PORT_IRQ_SG_DONE);
+        ad->dp_intr_req = 0;
+    }

Is it also needed in the error case? Especially the short-PRDT case that you are adding in the next patch.

... and is it also needed for NCQ, albeit with a separate flag?

Paolo

author
#11 lore raw

Re: [Qemu-devel] [RFC 03/10] AHCI: Add PRD interrupt

Quoted 6 lines · Paolo Bonzini

Il 13/09/2014 15:26, Paolo Bonzini ha scritto:

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
hw/ide/ahci.c +4 -0
+    if (ad->dp_intr_req) {
+        ahci_trigger_irq(ad->hba, ad, PORT_IRQ_SG_DONE);
+        ad->dp_intr_req = 0;
+    }
Quoted 8 lines
Quoted 3 lines

Is it also needed in the error case? Especially the short-PRDT case that you are adding in the next patch.

... and is it also needed for NCQ, albeit with a separate flag?

Paolo

I had actually thought that it wasn't; due to the more asynchronous nature of NCQ. But... looking back at the specification, I can't find anywhere that explicitly says you can't use these bits for NCQ.

#12 lore raw

Re: [Qemu-devel] [RFC 03/10] AHCI: Add PRD interrupt

Il 15/09/2014 18:31, John Snow ha scritto:

Quoted 13 lines · Paolo Bonzini
Quoted 10 lines

Il 13/09/2014 15:26, Paolo Bonzini ha scritto:

Quoted 8 lines
Quoted 5 lines

hw/ide/ahci.c static int ahci_commit_buf(IDEDMA *dma, int xfer_ok) · line 1162

Quoted 5 lines
hw/ide/ahci.c +4 -0
+    if (ad->dp_intr_req) {
+        ahci_trigger_irq(ad->hba, ad, PORT_IRQ_SG_DONE);
+        ad->dp_intr_req = 0;
+    }

Is it also needed in the error case? Especially the short-PRDT case that you are adding in the next patch.

Quoted 8 lines
Quoted 4 lines

... and is it also needed for NCQ, albeit with a separate flag?

Paolo

I had actually thought that it wasn't; due to the more asynchronous nature of NCQ. But... looking back at the specification, I can't find anywhere that explicitly says you can't use these bits for NCQ.

I suggest that you leave out this bit for a moment, so that we can get Windows fixed.

Paolo

author
#13 lore raw

Re: [Qemu-devel] [RFC 03/10] AHCI: Add PRD interrupt

In reply to Paolo Bonzini
Quoted 30 lines · Paolo Bonzini

Il 13/09/2014 06:34, John Snow ha scritto:

Quoted 29 lines

AHCI devices support a feature where individual entries in the scatter-gather list may have interrupt request bits set, in order to receive notification partway through a command that a portion of a transfer has completed. AHCI specs refer to this as the DPS bit or Descriptor Processed Status. It is named the "Interrupt on Completion" bit inside the PRDT.

This is not currently feasible in QEMU without reworking the inner DMA loop extensively, but we can at least record if we saw such a bit in advance and sent the interrupt at the end of the transfer, in case a guest is expecting to see the PRD/DPS interrupt bit set.

---
  hw/ide/ahci.c | 11 +++++++++++
  hw/ide/ahci.h |  5 ++++-
  2 files changed, 15 insertions(+), 1 deletion(-)
hw/ide/ahci.c +1 -0
index d3ece78..8e6a352 100644
@@ -501,6 +501,7 @@ static void ahci_reset_port(AHCIState *s, int port)
      pr->scr_err = 0;
      pr->scr_act = 0;
      d->busy_slot = -1;
+    d->dp_intr_req = false;
      d->init_d2h_sent = false;
 
Quoted 8 lines

hw/ide/ahci.c static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist, int offset) · line 779

Quoted 6 lines
hw/ide/ahci.c +1 -0
      ide_state = &s->dev[port].port.ifs[0];
@@ -775,11 +776,15 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGList *sglist, int offset)
                           ad->hba->as);
          qemu_sglist_add(sglist, le64_to_cpu(tbl[off_idx].addr + off_pos),
                          prdt_tbl_entry_size(&tbl[off_idx]) - off_pos);
+        ad->dp_intr_req = le32_to_cpu(tbl[off_idx].flags_size) & AHCI_PRDT_INTR;

Why is this also not an "OR"? It feels safer that way.

It's being added after sglist initialization; I didn't think there was any reason to preserve the previous value if there was one. I suspect that if the internal interrupt request bit is still one here, it's because I goofed up elsewhere.

Quoted 15 lines
Quoted 15 lines
hw/ide/ahci.c +3 -0
          for (i = off_idx + 1; i < sglist_alloc_hint; i++) {
              /* flags_size is zero-based */
              qemu_sglist_add(sglist, le64_to_cpu(tbl[i].addr),
                              prdt_tbl_entry_size(&tbl[i]));
+            if (le32_to_cpu(tbl[i].flags_size) & AHCI_PRDT_INTR) {
+                ad->dp_intr_req = 1;
+            }
          }
      }
 
@@ -1151,6 +1156,11 @@ static int ahci_commit_buf(IDEDMA *dma, int xfer_ok)
 
      qemu_sglist_destroy(&s->sg);
 
Quoted 7 lines

hw/ide/ahci.c static int ahci_commit_buf(IDEDMA *dma, int xfer_ok) · line 1162

Quoted 4 lines
hw/ide/ahci.c +4 -0
+    if (ad->dp_intr_req) {
+        ahci_trigger_irq(ad->hba, ad, PORT_IRQ_SG_DONE);
+        ad->dp_intr_req = 0;
+    }

Is it also needed in the error case? Especially the short-PRDT case that you are adding in the next patch.

Aah! Good catch... No, we should only be interrupting if we are 100% sure that the segment that requested the DPS bit was transferred.

Quoted 37 lines
Quoted 36 lines
hw/ide/ahci.c +1 -0
      return s->sg.size != 0;
  }
 
@@ -1307,6 +1317,7 @@ static const VMStateDescription vmstate_ahci_device = {
          VMSTATE_UINT32(port_regs.cmd_issue, AHCIDevice),
          VMSTATE_BOOL(done_atapi_packet, AHCIDevice),
          VMSTATE_INT32(busy_slot, AHCIDevice),
+        VMSTATE_BOOL(dp_intr_req, AHCIDevice),
          VMSTATE_BOOL(init_d2h_sent, AHCIDevice),
          VMSTATE_END_OF_LIST()
      },
hw/ide/ahci.h +4 -1
index 1543df7..31f32d3 100644
@@ -180,7 +180,9 @@
 
  #define AHCI_COMMAND_TABLE_ACMD            0x40
 
-#define AHCI_PRDT_SIZE_MASK                0x3fffff
+#define AHCI_PRDT_SIZE_MASK                0x003fffff
+#define AHCI_PRDT_RESERVED                 0x7fc00000
+#define AHCI_PRDT_INTR                     0x80000000
 
  #define IDE_FEATURE_DMA                    1
 
@@ -265,6 +267,7 @@ struct AHCIDevice {
      bool done_atapi_packet;
      int32_t busy_slot;
      bool init_d2h_sent;
+    bool dp_intr_req;
      AHCICmdHdr *cur_cmd;
      NCQTransferState ncq_tfs[AHCI_MAX_CMDS];
  };
Signature · 1 line
—js
author
#14 lore raw

RFC 4/10 ide: Correct handling of malformed/short PRDTs

In reply to John Snow

This impacts both BMDMA and AHCI HBA interfaces for IDE. Currently, we confuse the difference between a PRD having "0 bytes" and a PRD having "0 complete sectors."

This leads to, in the BMDMA case, leaked memory for short PRDTs, and infinite loops in the AHCI case.

the "prepare_buf" callback is reworked to return 0 if it could not allocate a full sector's worth of buffer space, instead of returning non-zero if it allocated any number of bytes.

ide_dma_cb adds a call to commit_buf in order to delete the short PRDT that it will not attempt to use to finish the DMA operation.

This patch corrects both occurrences and adds an assertion to prevent future regression. This assertion is tested in the existing ide-test, and is covered in a forthcoming AHCI test.

---
 dma-helpers.c | 3 +++
 hw/ide/ahci.c | 2 +-
 hw/ide/core.c | 1 +
 hw/ide/pci.c  | 5 +++--
 4 files changed, 8 insertions(+), 3 deletions(-)
dma-helpers.c +3 -0
index ba965a3..3f9766d 100644
@@ -38,6 +38,9 @@ int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len)
 void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
                       AddressSpace *as)
 {
+    /* If this is true, you're leaking memory. */
+    assert(qsg->sg == NULL);
+
     qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
     qsg->nsg = 0;
     qsg->nalloc = alloc_hint;
hw/ide/ahci.c +1 -1
index 8e6a352..42a77c4 100644
@@ -1132,7 +1132,7 @@ static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write)
     s->io_buffer_size = s->sg.size;
 
     DPRINTF(ad->port_no, "len=%#x\n", s->io_buffer_size);
-    return s->io_buffer_size != 0;
+    return s->io_buffer_size / 512 != 0;
 }
 
 /**
hw/ide/core.c +1 -0
index b2980e9..1685f6d 100644
@@ -726,6 +726,7 @@ void ide_dma_cb(void *opaque, int ret)
         /* The PRDs were too short. Reset the Active bit, but don't raise an
          * interrupt. */
         s->status = READY_STAT | SEEK_STAT;
+        dma_buf_commit(s, false);
         goto eot;
     }
 
hw/ide/pci.c +3 -2
index 2397f35..3f643c2 100644
@@ -74,8 +74,9 @@ static int bmdma_prepare_buf(IDEDMA *dma, int is_write)
         if (bm->cur_prd_len == 0) {
             /* end of table (with a fail safe of one page) */
             if (bm->cur_prd_last ||
-                (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE)
-                return s->io_buffer_size != 0;
+                (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE) {
+                return (s->io_buffer_size / 512) != 0;
+            }
             pci_dma_read(pci_dev, bm->cur_addr, &prd, 8);
             bm->cur_addr += 8;
             prd.addr = le32_to_cpu(prd.addr);
Signature · 1 line
1.9.3
Reviewed-by on v1 4/10
#15 lore raw

Re: [Qemu-devel] [RFC 04/10] ide: Correct handling of malformed/short PRDTs

Il 13/09/2014 06:34, John Snow ha scritto:

Quoted 73 lines

This impacts both BMDMA and AHCI HBA interfaces for IDE. Currently, we confuse the difference between a PRD having "0 bytes" and a PRD having "0 complete sectors."

This leads to, in the BMDMA case, leaked memory for short PRDTs, and infinite loops in the AHCI case.

the "prepare_buf" callback is reworked to return 0 if it could not allocate a full sector's worth of buffer space, instead of returning non-zero if it allocated any number of bytes.

ide_dma_cb adds a call to commit_buf in order to delete the short PRDT that it will not attempt to use to finish the DMA operation.

This patch corrects both occurrences and adds an assertion to prevent future regression. This assertion is tested in the existing ide-test, and is covered in a forthcoming AHCI test.

---
 dma-helpers.c | 3 +++
 hw/ide/ahci.c | 2 +-
 hw/ide/core.c | 1 +
 hw/ide/pci.c  | 5 +++--
 4 files changed, 8 insertions(+), 3 deletions(-)
dma-helpers.c +3 -0
index ba965a3..3f9766d 100644
@@ -38,6 +38,9 @@ int dma_memory_set(AddressSpace *as, dma_addr_t addr, uint8_t c, dma_addr_t len)
 void qemu_sglist_init(QEMUSGList *qsg, DeviceState *dev, int alloc_hint,
                       AddressSpace *as)
 {
+    /* If this is true, you're leaking memory. */
+    assert(qsg->sg == NULL);
+
     qsg->sg = g_malloc(alloc_hint * sizeof(ScatterGatherEntry));
     qsg->nsg = 0;
     qsg->nalloc = alloc_hint;
hw/ide/ahci.c +1 -1
index 8e6a352..42a77c4 100644
@@ -1132,7 +1132,7 @@ static int ahci_dma_prepare_buf(IDEDMA *dma, int is_write)
     s->io_buffer_size = s->sg.size;
 
     DPRINTF(ad->port_no, "len=%#x\n", s->io_buffer_size);
-    return s->io_buffer_size != 0;
+    return s->io_buffer_size / 512 != 0;
 }
 
 /**
hw/ide/core.c +1 -0
index b2980e9..1685f6d 100644
@@ -726,6 +726,7 @@ void ide_dma_cb(void *opaque, int ret)
         /* The PRDs were too short. Reset the Active bit, but don't raise an
          * interrupt. */
         s->status = READY_STAT | SEEK_STAT;
+        dma_buf_commit(s, false);
         goto eot;
     }
 
hw/ide/pci.c +0 -0
index 2397f35..3f643c2 100644
@@ -74,8 +74,9 @@ static int bmdma_prepare_buf(IDEDMA *dma, int is_write)
         if (bm->cur_prd_len == 0) {

hw/ide/pci.c static int bmdma_prepare_buf(IDEDMA *dma, int is_write) · line 82

Quoted 10 lines
hw/ide/pci.c +3 -2
             /* end of table (with a fail safe of one page) */
             if (bm->cur_prd_last ||
-                (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE)
-                return s->io_buffer_size != 0;
+                (bm->cur_addr - bm->addr) >= BMDMA_PAGE_SIZE) {
+                return (s->io_buffer_size / 512) != 0;
+            }
             pci_dma_read(pci_dev, bm->cur_addr, &prd, 8);
             bm->cur_addr += 8;
             prd.addr = le32_to_cpu(prd.addr);

The changes I suggested in patch 2 shouldn't be a hurdle here.

Paolo

author
#16 lore raw

RFC 5/10 AHCI: Rename NCQFIS structure fields

In reply to John Snow

Several fields of the NCQFIS structure are ambiguously named. This patch clarifies the intended (if unsupported) usage of the NCQ fields to aid in creating more meaningful debug messages through the NCQ codepaths.

This patch also adds an is_ncq() helper to avoid repetition for checking this property.

---
 hw/ide/ahci.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++----
 hw/ide/ahci.h | 38 ++++++++++++++++++++++++++++----------
 2 files changed, 74 insertions(+), 14 deletions(-)
hw/ide/ahci.c +46 -4
index 42a77c4..4a1af3b 100644
@@ -822,6 +822,21 @@ static void ncq_cb(void *opaque, int ret)
     ncq_tfs->used = 0;
 }
 
+static int is_ncq(uint8_t ata_cmd)
+{
+    /* Based on SATA 3.2 section 13.6.3.2 */
+    switch (ata_cmd) {
+    case READ_FPDMA_QUEUED:
+    case WRITE_FPDMA_QUEUED:
+    case NCQ_NON_DATA:
+    case RECEIVE_FPDMA_QUEUED:
+    case SEND_FPDMA_QUEUED:
+        return 1;
+    default:
+        return 0;
+    }
+}
+
 static void process_ncq_command(AHCIState *s, int port, uint8_t *cmd_fis,
                                 int slot)
 {
@@ -845,6 +860,22 @@ static void process_ncq_command(AHCIState *s, int port, uint8_t *cmd_fis,
                    ((uint64_t)ncq_fis->lba1 << 8) |
                    (uint64_t)ncq_fis->lba0;
 
+#ifdef DEBUG_AHCI
+    /* Sanity-check the NCQ packet */
+    if (ncq_fis->aux0 || ncq_fis->aux1 || ncq_fis->aux2 || ncq_fis->aux3) {
+        DPRINTF(port, "Warn: Attempt to use NCQ auxiliary fields.\n");
+    }
+    if (ncq_fis->prio || ncq_fis->icc) {
+        DPRINTF(port, "Warn: Unsupported attempt to use PRIO/ICC fields\n");
+    }
+    if (ncq_fis->fua & NCQ_FIS_FUA_MASK) {
+        DPRINTF(port, "Warn: Unsupported attempt to use Force Unit Access\n");
+    }
+    if (ncq_fis->tag & NCQ_FIS_RARC_MASK) {
+        DPRINTF(port, "Warn: Unsupported attempt to use Rebuild Assist\n");
+    }
+#endif
+
     /* Note: We calculate the sector count, but don't currently rely on it.
      * The total size of the DMA buffer tells us the transfer size instead. */
     ncq_tfs->sector_count = ((uint16_t)ncq_fis->sector_count_high << 8) |
@@ -856,6 +887,12 @@ static void process_ncq_command(AHCIState *s, int port, uint8_t *cmd_fis,
             s->dev[port].port.ifs[0].nb_sectors - 1);
 
     ahci_populate_sglist(&s->dev[port], &ncq_tfs->sglist, 0);
+    if (ncq_tfs->sglist.size != ncq_tfs->sector_count * 512) {
+        DPRINTF(port, "NCQ header hint (%u) doesn't match DMA buffer size"
+                " (%u)\n", ncq_tfs->sector_count * 512, ncq_tfs->sglist.size);
+        qemu_sglist_destroy(&ncq_tfs->sglist);
+        return;
+    }
     ncq_tfs->tag = tag;
 
     switch(ncq_fis->command) {
@@ -887,9 +924,15 @@ static void process_ncq_command(AHCIState *s, int port, uint8_t *cmd_fis,
                                             ncq_cb, ncq_tfs);
             break;
         default:
-            DPRINTF(port, "error: tried to process non-NCQ command as NCQ\n");
+            if (is_ncq(cmd_fis[2])) {
+                DPRINTF(port,
+                        "error: unsupported NCQ command (0x%02x) received\n",
+                        cmd_fis[2]);
+            } else {
+                DPRINTF(port,
+                        "error: tried to process non-NCQ command as NCQ\n");
+            }
             qemu_sglist_destroy(&ncq_tfs->sglist);
-            break;
     }
 }
 
@@ -981,8 +1024,7 @@ static int handle_cmd(AHCIState *s, int port, int slot)
     if (cmd_fis[1] == SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER) {
 
         /* Check for NCQ command */
-        if ((cmd_fis[2] == READ_FPDMA_QUEUED) ||
-            (cmd_fis[2] == WRITE_FPDMA_QUEUED)) {
+        if (is_ncq(cmd_fis[2])) {
             process_ncq_command(s, port, cmd_fis, slot);
             goto out;
         }
hw/ide/ahci.h +28 -10
index 31f32d3..8745848 100644
@@ -188,6 +188,12 @@
 
 #define READ_FPDMA_QUEUED                  0x60
 #define WRITE_FPDMA_QUEUED                 0x61
+#define NCQ_NON_DATA                       0x63
+#define RECEIVE_FPDMA_QUEUED               0x65
+#define SEND_FPDMA_QUEUED                  0x64
+
+#define NCQ_FIS_FUA_MASK                   0x80
+#define NCQ_FIS_RARC_MASK                  0x01
 
 #define RES_FIS_DSFIS                      0x00
 #define RES_FIS_PSFIS                      0x20
@@ -307,27 +313,39 @@ extern const VMStateDescription vmstate_ahci;
     .offset     = vmstate_offset_value(_state, _field, AHCIState),   \
 }
 
+/**
+ * NCQFrame is the same as a Register H2D FIS (described in SATA 3.2),
+ * but some fields have been re-mapped and re-purposed, as seen in
+ * SATA 3.2 section 13.6.4.1 ("READ FPDMA QUEUED")
+ *
+ * cmd_fis[3], feature 7:0, becomes sector count 7:0.
+ * cmd_fis[7], device 7:0, uses bit 7 as the Force Unit Access bit.
+ * cmd_fis[11], feature 15:8, becomes sector count 15:8.
+ * cmd_fis[12], count 7:0, becomes the NCQ TAG (7:3) and RARC bit (0)
+ * cmd_fis[13], count 15:8, becomes the priority value (7:6)
+ * bytes 16-19 become an le32 "auxiliary" field.
+ */
 typedef struct NCQFrame {
     uint8_t fis_type;
     uint8_t c;
     uint8_t command;
-    uint8_t sector_count_low;
+    uint8_t sector_count_low;  /* (feature 7:0) */
     uint8_t lba0;
     uint8_t lba1;
     uint8_t lba2;
-    uint8_t fua;
+    uint8_t fua;               /* (device 7:0) */
     uint8_t lba3;
     uint8_t lba4;
     uint8_t lba5;
-    uint8_t sector_count_high;
-    uint8_t tag;
-    uint8_t reserved5;
-    uint8_t reserved6;
+    uint8_t sector_count_high; /* (feature 15:8) */
+    uint8_t tag;               /* (count 0:7) */
+    uint8_t prio;              /* (count 15:8) */
+    uint8_t icc;
     uint8_t control;
-    uint8_t reserved7;
-    uint8_t reserved8;
-    uint8_t reserved9;
-    uint8_t reserved10;
+    uint8_t aux0;
+    uint8_t aux1;
+    uint8_t aux2;
+    uint8_t aux3;
 } QEMU_PACKED NCQFrame;
 
 void ahci_init(AHCIState *s, DeviceState *qdev, AddressSpace *as, int ports);
Signature · 1 line
1.9.3
author
#17 lore raw

RFC 6/10 AHCI: Fix FIS decomposition

In reply to John Snow

This patch introduces a few changes to how FIS packets are deciphered in the AHCI virtual device.

(1) Packets may now either update the Control register or the Command register, but not both. This is according to the SATA 3.2 specification which states:
"...the device either initiates processing of the command indicated in the Command register or initiates processing of the control request indicated [...] depending on the state of the C bit in the FIS."

(2) Instead of trying to extract the sector number out of the FIS from bytes 4-10 and setting it with ide_set_sector, we set the appropriate IDEState registers and trust that ide_get_sector can retrieve the correct sector later.

(3) We save cmd_fis[7] as ide_state->select, which informs decisions about if we are using LBA or CHS.
This corrects a bug in AHCI wherein we attempt to set and/or retrieve the sector number by using ide_set_sector and ide_get_sector, which depend on the select register to determine if we are using LBA or CHS.

(4) Save cmd_fis[11] as ide_state->hob_feature, as defined in AHCI.

(5) For several ATA commands, the sector count register set to 0 is a magic number that means 256 sectors. For LBA48 commands, this means 65,536 sectors. We drop the magic sector correction here, and trust the ide core layer to handle the conversion appropriately, in ide_cmd_lba48_transform

(6) We expand FIS decomposition to include both ATAPI and IDE devices. We leave the logic of determining if the fields are valid or not to the respective layers.

(7) Forcefully setting the feature, hcyl and lcyl registers for ATAPI commands is removed.
- The hcyl and lcyl magic present here is valid at boot only, and should not be overridden for every PACKET command.
- The feature register is defined as valid for the PACKET command, so we should not suppress it. The ATAPI layer does not even currently depend on or require 0x01 as mandatory.

---
 hw/ide/ahci.c | 64 ++++++++++++++++++++++++-----------------------------------
 1 file changed, 26 insertions(+), 38 deletions(-)
hw/ide/ahci.c +26 -38
index 4a1af3b..c2fa733 100644
@@ -1008,7 +1008,8 @@ static int handle_cmd(AHCIState *s, int port, int slot)
             break;
     }
 
-    switch (s->dev[port].port_state) {
+    if (!(cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER)) {
+        switch (s->dev[port].port_state) {
         case STATE_RUN:
             if (cmd_fis[15] & ATA_SRST) {
                 s->dev[port].port_state = STATE_RESET;
@@ -1019,9 +1020,10 @@ static int handle_cmd(AHCIState *s, int port, int slot)
                 ahci_reset_port(s, port);
             }
             break;
+        }
     }
 
-    if (cmd_fis[1] == SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER) {
+    else if (cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER) {
 
         /* Check for NCQ command */
         if (is_ncq(cmd_fis[2])) {
@@ -1029,50 +1031,36 @@ static int handle_cmd(AHCIState *s, int port, int slot)
             goto out;
         }
 
-        /* Decompose the FIS  */
-        ide_state->nsector = (int64_t)((cmd_fis[13] << 8) | cmd_fis[12]);
+        /* Decompose the FIS:
+         * AHCI does not interpret FIS packets, it only forwards them.
+         * SATA 1.0 describes how to decode LBA28 and CHS FIS packets.
+         * Later specifications, e.g, SATA 3.2, describe LBA48 FIS packets.
+         *
+         * ATA4 describes sector number for LBA28/CHS commands.
+         * ATA6 describes sector number for LBA48 commands.
+         * ATA8 deprecates CHS fully, describing only LBA28/48.
+         *
+         * We dutifully convert the FIS into IDE registers, and allow the
+         * core layer to interpret them as needed. */
         ide_state->feature = cmd_fis[3];
-        if (!ide_state->nsector) {
-            ide_state->nsector = 256;
-        }
-
-        if (ide_state->drive_kind != IDE_CD) {
-            /*
-             * We set the sector depending on the sector defined in the FIS.
-             * Unfortunately, the spec isn't exactly obvious on this one.
-             *
-             * Apparently LBA48 commands set fis bytes 10,9,8,6,5,4 to the
-             * 48 bit sector number. ATA_CMD_READ_DMA_EXT is an example for
-             * such a command.
-             *
-             * Non-LBA48 commands however use 7[lower 4 bits],6,5,4 to define a
-             * 28-bit sector number. ATA_CMD_READ_DMA is an example for such
-             * a command.
-             *
-             * Since the spec doesn't explicitly state what each field should
-             * do, I simply assume non-used fields as reserved and OR everything
-             * together, independent of the command.
-             */
-            ide_set_sector(ide_state, ((uint64_t)cmd_fis[10] << 40)
-                                    | ((uint64_t)cmd_fis[9] << 32)
-                                    /* This is used for LBA48 commands */
-                                    | ((uint64_t)cmd_fis[8] << 24)
-                                    /* This is used for non-LBA48 commands */
-                                    | ((uint64_t)(cmd_fis[7] & 0xf) << 24)
-                                    | ((uint64_t)cmd_fis[6] << 16)
-                                    | ((uint64_t)cmd_fis[5] << 8)
-                                    | cmd_fis[4]);
-        }
+        ide_state->sector = cmd_fis[4];     /* LBA 7:0 */
+        ide_state->lcyl = cmd_fis[5];       /* LBA 15:8  */
+        ide_state->hcyl = cmd_fis[6];       /* LBA 23:16 */
+        ide_state->select = cmd_fis[7];     /* LBA 27:24 (LBA28) */
+        ide_state->hob_sector = cmd_fis[8]; /* LBA 31:24 */
+        ide_state->hob_lcyl = cmd_fis[9];   /* LBA 39:32 */
+        ide_state->hob_hcyl = cmd_fis[10];  /* LBA 47:40 */
+        ide_state->hob_feature = cmd_fis[11];
+        ide_state->nsector = (int64_t)((cmd_fis[13] << 8) | cmd_fis[12]);
+        /* 14, 16, 17, 18, 19: Reserved (SATA 1.0) */
+        /* 15: Only valid when UPDATE_COMMAND not set. */
 
         /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command
          * table to ide_state->io_buffer
          */
         if (opts & AHCI_CMD_ATAPI) {
             memcpy(ide_state->io_buffer, &cmd_fis[AHCI_COMMAND_TABLE_ACMD], 0x10);
-            ide_state->lcyl = 0x14;
-            ide_state->hcyl = 0xeb;
             debug_print_fis(ide_state->io_buffer, 0x10);
-            ide_state->feature = IDE_FEATURE_DMA;
             s->dev[port].done_atapi_packet = false;
             /* XXX send PIO setup FIS */
         }
Signature · 1 line
1.9.3
author
#18 lore raw

RFC 7/10 ide/ahci: Reorder error cases in handle_cmd

In reply to John Snow

Error checking in ahci's handle_cmd is re-ordered so that we initialize as few things as possible before we've done our sanity checking. This simplifies returning from this call in case of an error.

A check to make sure the DMA memory map succeeds with the correct size is also added, and the debug print of the command fis is cleaned up with its size corrected.

---
 hw/ide/ahci.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
hw/ide/ahci.c +13 -15
index c2fa733..1153ce9 100644
@@ -951,38 +951,36 @@ static int handle_cmd(AHCIState *s, int port, int slot)
         return -1;
     }
 
-    cmd = &((AHCICmdHdr *)s->dev[port].lst)[slot];
-
     if (!s->dev[port].lst) {
         DPRINTF(port, "error: lst not given but cmd handled");
         return -1;
     }
-
+    cmd = &((AHCICmdHdr *)s->dev[port].lst)[slot];
     /* remember current slot handle for later */
     s->dev[port].cur_cmd = cmd;
 
+    /* The device we are working for */
+    ide_state = &s->dev[port].port.ifs[0];
+    if (!ide_state->bs) {
+        DPRINTF(port, "error: guest accessed unused port");
+        return -1;
+    }
+
     opts = le32_to_cpu(cmd->opts);
     tbl_addr = le64_to_cpu(cmd->tbl_addr);
-
     cmd_len = 0x80;
     cmd_fis = dma_memory_map(s->as, tbl_addr, &cmd_len,
                              DMA_DIRECTION_FROM_DEVICE);
-
     if (!cmd_fis) {
         DPRINTF(port, "error: guest passed us an invalid cmd fis\n");
         return -1;
-    }
-
-    /* The device we are working for */
-    ide_state = &s->dev[port].port.ifs[0];
-
-    if (!ide_state->bs) {
-        DPRINTF(port, "error: guest accessed unused port");
+    } else if (cmd_len != 0x80) {
+        ahci_trigger_irq(s, &s->dev[port], PORT_IRQ_HBUS_ERR);
+        DPRINTF(port, "error: dma_memory_map failed (len (%02x) < 0x80)\n",
+                cmd_len);
         goto out;
     }
-
-    debug_print_fis(cmd_fis, 0x90);
-    //debug_print_fis(cmd_fis, (opts & AHCI_CMD_HDR_CMD_FIS_LEN) * 4);
+    debug_print_fis(cmd_fis, 0x80);
 
     switch (cmd_fis[0]) {
         case SATA_FIS_TYPE_REGISTER_H2D:
Signature · 1 line
1.9.3
Reviewed-by on v1 7/10
#19 lore raw

Re: [Qemu-devel] [RFC 07/10] ide/ahci: Reorder error cases in handle_cmd

Il 13/09/2014 06:34, John Snow ha scritto:

Quoted 61 lines

Error checking in ahci's handle_cmd is re-ordered so that we initialize as few things as possible before we've done our sanity checking. This simplifies returning from this call in case of an error.

A check to make sure the DMA memory map succeeds with the correct size is also added, and the debug print of the command fis is cleaned up with its size corrected.

---
 hw/ide/ahci.c | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)
hw/ide/ahci.c +11 -12
index c2fa733..1153ce9 100644
@@ -951,38 +951,36 @@ static int handle_cmd(AHCIState *s, int port, int slot)
         return -1;
     }
 
-    cmd = &((AHCICmdHdr *)s->dev[port].lst)[slot];
-
     if (!s->dev[port].lst) {
         DPRINTF(port, "error: lst not given but cmd handled");
         return -1;
     }
-
+    cmd = &((AHCICmdHdr *)s->dev[port].lst)[slot];
     /* remember current slot handle for later */
     s->dev[port].cur_cmd = cmd;
 
+    /* The device we are working for */
+    ide_state = &s->dev[port].port.ifs[0];
+    if (!ide_state->bs) {
+        DPRINTF(port, "error: guest accessed unused port");
+        return -1;
+    }
+
     opts = le32_to_cpu(cmd->opts);
     tbl_addr = le64_to_cpu(cmd->tbl_addr);
-
     cmd_len = 0x80;
     cmd_fis = dma_memory_map(s->as, tbl_addr, &cmd_len,
                              DMA_DIRECTION_FROM_DEVICE);
-
     if (!cmd_fis) {
         DPRINTF(port, "error: guest passed us an invalid cmd fis\n");
         return -1;
-    }
-
-    /* The device we are working for */
-    ide_state = &s->dev[port].port.ifs[0];
-
-    if (!ide_state->bs) {
-        DPRINTF(port, "error: guest accessed unused port");
+    } else if (cmd_len != 0x80) {
+        ahci_trigger_irq(s, &s->dev[port], PORT_IRQ_HBUS_ERR);
+        DPRINTF(port, "error: dma_memory_map failed (len (%02x) < 0x80)\n",

hw/ide/ahci.c static int handle_cmd(AHCIState *s, int port, int slot) · line 986

Quoted 10 lines
hw/ide/ahci.c +2 -3
+                cmd_len);
         goto out;
     }
-
-    debug_print_fis(cmd_fis, 0x90);
-    //debug_print_fis(cmd_fis, (opts & AHCI_CMD_HDR_CMD_FIS_LEN) * 4);
+    debug_print_fis(cmd_fis, 0x80);
 
     switch (cmd_fis[0]) {
         case SATA_FIS_TYPE_REGISTER_H2D:
author
#20 lore raw

RFC 8/10 ahci: Check cmd_fis[1] more explicitly

In reply to John Snow

Instead of checking for a known byte, inspect the fields of this byte explicitly to produce more meaningful error messages and improve the readability of this section.

---
 hw/ide/ahci.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
hw/ide/ahci.c +12 -11
index 1153ce9..e4eae0c 100644
@@ -993,17 +993,18 @@ static int handle_cmd(AHCIState *s, int port, int slot)
             break;
     }
 
-    switch (cmd_fis[1]) {
-        case SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER:
-            break;
-        case 0:
-            break;
-        default:
-            DPRINTF(port, "unknown command cmd_fis[0]=%02x cmd_fis[1]=%02x "
-                          "cmd_fis[2]=%02x\n", cmd_fis[0], cmd_fis[1],
-                          cmd_fis[2]);
-            goto out;
-            break;
+    if (cmd_fis[1] & 0x0F) {
+        DPRINTF(port, "Port Multiplier not supported."
+                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
+                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
+        goto out;
+    }
+
+    if (cmd_fis[1] & 0x70) {
+        DPRINTF(port, "Reserved flags set in H2D Register FIS."
+                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
+                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
+        goto out;
     }
 
     if (!(cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER)) {
Signature · 1 line
1.9.3
Reviewed-by on v1 8/10
#21 lore raw

Re: [Qemu-devel] [RFC 08/10] ahci: Check cmd_fis[1] more explicitly

Il 13/09/2014 06:34, John Snow ha scritto:

Quoted 34 lines

Instead of checking for a known byte, inspect the fields of this byte explicitly to produce more meaningful error messages and improve the readability of this section.

---
 hw/ide/ahci.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
hw/ide/ahci.c +5 -11
index 1153ce9..e4eae0c 100644
@@ -993,17 +993,18 @@ static int handle_cmd(AHCIState *s, int port, int slot)
             break;
     }
 
-    switch (cmd_fis[1]) {
-        case SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER:
-            break;
-        case 0:
-            break;
-        default:
-            DPRINTF(port, "unknown command cmd_fis[0]=%02x cmd_fis[1]=%02x "
-                          "cmd_fis[2]=%02x\n", cmd_fis[0], cmd_fis[1],
-                          cmd_fis[2]);
-            goto out;
-            break;
+    if (cmd_fis[1] & 0x0F) {
+        DPRINTF(port, "Port Multiplier not supported."
+                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
+                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
+        goto out;

hw/ide/ahci.c static int handle_cmd(AHCIState *s, int port, int slot) · line 1010

Quoted 10 lines
hw/ide/ahci.c +7 -0
+    }
+
+    if (cmd_fis[1] & 0x70) {
+        DPRINTF(port, "Reserved flags set in H2D Register FIS."
+                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
+                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
+        goto out;
     }
 
     if (!(cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER)) {
author
#22 lore raw

RFC 9/10 ahci: factor out FIS decomposition

In reply to John Snow
---
 hw/ide/ahci.c | 169 ++++++++++++++++++++++++++++++----------------------------
 1 file changed, 86 insertions(+), 83 deletions(-)
hw/ide/ahci.c +86 -83
index e4eae0c..5bc5a92 100644
@@ -936,10 +936,94 @@ static void process_ncq_command(AHCIState *s, int port, uint8_t *cmd_fis,
     }
 }
 
+static void handle_reg_h2d_fis(AHCIState *s, int port,
+                               int slot, uint8_t *cmd_fis)
+{
+    IDEState *ide_state = &s->dev[port].port.ifs[0];
+    AHCICmdHdr *cmd = s->dev[port].cur_cmd;
+    uint32_t opts = le32_to_cpu(cmd->opts);
+
+    if (cmd_fis[1] & 0x0F) {
+        DPRINTF(port, "Port Multiplier not supported."
+                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
+                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
+        return;
+    }
+
+    if (cmd_fis[1] & 0x70) {
+        DPRINTF(port, "Reserved flags set in H2D Register FIS."
+                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
+                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
+        return;
+    }
+
+    if (!(cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER)) {
+        switch (s->dev[port].port_state) {
+        case STATE_RUN:
+            if (cmd_fis[15] & ATA_SRST) {
+                s->dev[port].port_state = STATE_RESET;
+            }
+            break;
+        case STATE_RESET:
+            if (!(cmd_fis[15] & ATA_SRST)) {
+                ahci_reset_port(s, port);
+            }
+            break;
+        }
+        return;
+    }
+
+    /* Check for NCQ command */
+    if (is_ncq(cmd_fis[2])) {
+        process_ncq_command(s, port, cmd_fis, slot);
+        return;
+    }
+
+    /* Decompose the FIS:
+     * AHCI does not interpret FIS packets, it only forwards them.
+     * SATA 1.0 describes how to decode LBA28 and CHS FIS packets.
+     * Later specifications, e.g, SATA 3.2, describe LBA48 FIS packets.
+     *
+     * ATA4 describes sector number for LBA28/CHS commands.
+     * ATA6 describes sector number for LBA48 commands.
+     * ATA8 deprecates CHS fully, describing only LBA28/48.
+     *
+     * We dutifully convert the FIS into IDE registers, and allow the
+     * core layer to interpret them as needed. */
+    ide_state->feature = cmd_fis[3];
+    ide_state->sector = cmd_fis[4];      /* LBA 7:0 */
+    ide_state->lcyl = cmd_fis[5];        /* LBA 15:8  */
+    ide_state->hcyl = cmd_fis[6];        /* LBA 23:16 */
+    ide_state->select = cmd_fis[7];      /* LBA 27:24 (LBA28) */
+    ide_state->hob_sector = cmd_fis[8];  /* LBA 31:24 */
+    ide_state->hob_lcyl = cmd_fis[9];    /* LBA 39:32 */
+    ide_state->hob_hcyl = cmd_fis[10];   /* LBA 47:40 */
+    ide_state->hob_feature = cmd_fis[11];
+    ide_state->nsector = (int64_t)((cmd_fis[13] << 8) | cmd_fis[12]);
+    /* 14, 16, 17, 18, 19: Reserved (SATA 1.0) */
+    /* 15: Only valid when UPDATE_COMMAND not set. */
+
+    /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command
+     * table to ide_state->io_buffer */
+    if (opts & AHCI_CMD_ATAPI) {
+        memcpy(ide_state->io_buffer, &cmd_fis[AHCI_COMMAND_TABLE_ACMD], 0x10);
+        debug_print_fis(ide_state->io_buffer, 0x10);
+        s->dev[port].done_atapi_packet = false;
+        /* XXX send PIO setup FIS */
+    }
+
+    ide_state->error = 0;
+
+    /* Reset transferred byte counter */
+    cmd->status = 0;
+
+    /* We're ready to process the command in FIS byte 2. */
+    ide_exec_cmd(&s->dev[port].port, cmd_fis[2]);
+}
+
 static int handle_cmd(AHCIState *s, int port, int slot)
 {
     IDEState *ide_state;
-    uint32_t opts;
     uint64_t tbl_addr;
     AHCICmdHdr *cmd;
     uint8_t *cmd_fis;
@@ -966,7 +1050,6 @@ static int handle_cmd(AHCIState *s, int port, int slot)
         return -1;
     }
 
-    opts = le32_to_cpu(cmd->opts);
     tbl_addr = le64_to_cpu(cmd->tbl_addr);
     cmd_len = 0x80;
     cmd_fis = dma_memory_map(s->as, tbl_addr, &cmd_len,
@@ -984,95 +1067,15 @@ static int handle_cmd(AHCIState *s, int port, int slot)
 
     switch (cmd_fis[0]) {
         case SATA_FIS_TYPE_REGISTER_H2D:
+            handle_reg_h2d_fis(s, port, slot, cmd_fis);
             break;
         default:
             DPRINTF(port, "unknown command cmd_fis[0]=%02x cmd_fis[1]=%02x "
                           "cmd_fis[2]=%02x\n", cmd_fis[0], cmd_fis[1],
                           cmd_fis[2]);
-            goto out;
             break;
     }
 
-    if (cmd_fis[1] & 0x0F) {
-        DPRINTF(port, "Port Multiplier not supported."
-                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
-                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
-        goto out;
-    }
-
-    if (cmd_fis[1] & 0x70) {
-        DPRINTF(port, "Reserved flags set in H2D Register FIS."
-                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
-                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
-        goto out;
-    }
-
-    if (!(cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER)) {
-        switch (s->dev[port].port_state) {
-        case STATE_RUN:
-            if (cmd_fis[15] & ATA_SRST) {
-                s->dev[port].port_state = STATE_RESET;
-            }
-            break;
-        case STATE_RESET:
-            if (!(cmd_fis[15] & ATA_SRST)) {
-                ahci_reset_port(s, port);
-            }
-            break;
-        }
-    }
-
-    else if (cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER) {
-
-        /* Check for NCQ command */
-        if (is_ncq(cmd_fis[2])) {
-            process_ncq_command(s, port, cmd_fis, slot);
-            goto out;
-        }
-
-        /* Decompose the FIS:
-         * AHCI does not interpret FIS packets, it only forwards them.
-         * SATA 1.0 describes how to decode LBA28 and CHS FIS packets.
-         * Later specifications, e.g, SATA 3.2, describe LBA48 FIS packets.
-         *
-         * ATA4 describes sector number for LBA28/CHS commands.
-         * ATA6 describes sector number for LBA48 commands.
-         * ATA8 deprecates CHS fully, describing only LBA28/48.
-         *
-         * We dutifully convert the FIS into IDE registers, and allow the
-         * core layer to interpret them as needed. */
-        ide_state->feature = cmd_fis[3];
-        ide_state->sector = cmd_fis[4];     /* LBA 7:0 */
-        ide_state->lcyl = cmd_fis[5];       /* LBA 15:8  */
-        ide_state->hcyl = cmd_fis[6];       /* LBA 23:16 */
-        ide_state->select = cmd_fis[7];     /* LBA 27:24 (LBA28) */
-        ide_state->hob_sector = cmd_fis[8]; /* LBA 31:24 */
-        ide_state->hob_lcyl = cmd_fis[9];   /* LBA 39:32 */
-        ide_state->hob_hcyl = cmd_fis[10];  /* LBA 47:40 */
-        ide_state->hob_feature = cmd_fis[11];
-        ide_state->nsector = (int64_t)((cmd_fis[13] << 8) | cmd_fis[12]);
-        /* 14, 16, 17, 18, 19: Reserved (SATA 1.0) */
-        /* 15: Only valid when UPDATE_COMMAND not set. */
-
-        /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command
-         * table to ide_state->io_buffer
-         */
-        if (opts & AHCI_CMD_ATAPI) {
-            memcpy(ide_state->io_buffer, &cmd_fis[AHCI_COMMAND_TABLE_ACMD], 0x10);
-            debug_print_fis(ide_state->io_buffer, 0x10);
-            s->dev[port].done_atapi_packet = false;
-            /* XXX send PIO setup FIS */
-        }
-
-        ide_state->error = 0;
-
-        /* Reset transferred byte counter */
-        cmd->status = 0;
-
-        /* We're ready to process the command in FIS byte 2. */
-        ide_exec_cmd(&s->dev[port].port, cmd_fis[2]);
-    }
-
 out:
     dma_memory_unmap(s->as, cmd_fis, cmd_len, DMA_DIRECTION_FROM_DEVICE,
                      cmd_len);
Signature · 1 line
1.9.3
Reviewed-by on v1 9/10
#23 lore raw

Re: [Qemu-devel] [RFC 09/10] ahci: factor out FIS decomposition

Il 13/09/2014 06:34, John Snow ha scritto:

Quoted 4 lines
---
 hw/ide/ahci.c | 169 ++++++++++++++++++++++++++++++----------------------------
 1 file changed, 86 insertions(+), 83 deletions(-)
Quoted 206 lines
hw/ide/ahci.c +86 -83
index e4eae0c..5bc5a92 100644
@@ -936,10 +936,94 @@ static void process_ncq_command(AHCIState *s, int port, uint8_t *cmd_fis,
     }
 }
 
+static void handle_reg_h2d_fis(AHCIState *s, int port,
+                               int slot, uint8_t *cmd_fis)
+{
+    IDEState *ide_state = &s->dev[port].port.ifs[0];
+    AHCICmdHdr *cmd = s->dev[port].cur_cmd;
+    uint32_t opts = le32_to_cpu(cmd->opts);
+
+    if (cmd_fis[1] & 0x0F) {
+        DPRINTF(port, "Port Multiplier not supported."
+                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
+                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
+        return;
+    }
+
+    if (cmd_fis[1] & 0x70) {
+        DPRINTF(port, "Reserved flags set in H2D Register FIS."
+                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
+                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
+        return;
+    }
+
+    if (!(cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER)) {
+        switch (s->dev[port].port_state) {
+        case STATE_RUN:
+            if (cmd_fis[15] & ATA_SRST) {
+                s->dev[port].port_state = STATE_RESET;
+            }
+            break;
+        case STATE_RESET:
+            if (!(cmd_fis[15] & ATA_SRST)) {
+                ahci_reset_port(s, port);
+            }
+            break;
+        }
+        return;
+    }
+
+    /* Check for NCQ command */
+    if (is_ncq(cmd_fis[2])) {
+        process_ncq_command(s, port, cmd_fis, slot);
+        return;
+    }
+
+    /* Decompose the FIS:
+     * AHCI does not interpret FIS packets, it only forwards them.
+     * SATA 1.0 describes how to decode LBA28 and CHS FIS packets.
+     * Later specifications, e.g, SATA 3.2, describe LBA48 FIS packets.
+     *
+     * ATA4 describes sector number for LBA28/CHS commands.
+     * ATA6 describes sector number for LBA48 commands.
+     * ATA8 deprecates CHS fully, describing only LBA28/48.
+     *
+     * We dutifully convert the FIS into IDE registers, and allow the
+     * core layer to interpret them as needed. */
+    ide_state->feature = cmd_fis[3];
+    ide_state->sector = cmd_fis[4];      /* LBA 7:0 */
+    ide_state->lcyl = cmd_fis[5];        /* LBA 15:8  */
+    ide_state->hcyl = cmd_fis[6];        /* LBA 23:16 */
+    ide_state->select = cmd_fis[7];      /* LBA 27:24 (LBA28) */
+    ide_state->hob_sector = cmd_fis[8];  /* LBA 31:24 */
+    ide_state->hob_lcyl = cmd_fis[9];    /* LBA 39:32 */
+    ide_state->hob_hcyl = cmd_fis[10];   /* LBA 47:40 */
+    ide_state->hob_feature = cmd_fis[11];
+    ide_state->nsector = (int64_t)((cmd_fis[13] << 8) | cmd_fis[12]);
+    /* 14, 16, 17, 18, 19: Reserved (SATA 1.0) */
+    /* 15: Only valid when UPDATE_COMMAND not set. */
+
+    /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command
+     * table to ide_state->io_buffer */
+    if (opts & AHCI_CMD_ATAPI) {
+        memcpy(ide_state->io_buffer, &cmd_fis[AHCI_COMMAND_TABLE_ACMD], 0x10);
+        debug_print_fis(ide_state->io_buffer, 0x10);
+        s->dev[port].done_atapi_packet = false;
+        /* XXX send PIO setup FIS */
+    }
+
+    ide_state->error = 0;
+
+    /* Reset transferred byte counter */
+    cmd->status = 0;
+
+    /* We're ready to process the command in FIS byte 2. */
+    ide_exec_cmd(&s->dev[port].port, cmd_fis[2]);
+}
+
 static int handle_cmd(AHCIState *s, int port, int slot)
 {
     IDEState *ide_state;
-    uint32_t opts;
     uint64_t tbl_addr;
     AHCICmdHdr *cmd;
     uint8_t *cmd_fis;
@@ -966,7 +1050,6 @@ static int handle_cmd(AHCIState *s, int port, int slot)
         return -1;
     }
 
-    opts = le32_to_cpu(cmd->opts);
     tbl_addr = le64_to_cpu(cmd->tbl_addr);
     cmd_len = 0x80;
     cmd_fis = dma_memory_map(s->as, tbl_addr, &cmd_len,
@@ -984,95 +1067,15 @@ static int handle_cmd(AHCIState *s, int port, int slot)
 
     switch (cmd_fis[0]) {
         case SATA_FIS_TYPE_REGISTER_H2D:
+            handle_reg_h2d_fis(s, port, slot, cmd_fis);
             break;
         default:
             DPRINTF(port, "unknown command cmd_fis[0]=%02x cmd_fis[1]=%02x "
                           "cmd_fis[2]=%02x\n", cmd_fis[0], cmd_fis[1],
                           cmd_fis[2]);
-            goto out;
             break;
     }
 
-    if (cmd_fis[1] & 0x0F) {
-        DPRINTF(port, "Port Multiplier not supported."
-                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
-                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
-        goto out;
-    }
-
-    if (cmd_fis[1] & 0x70) {
-        DPRINTF(port, "Reserved flags set in H2D Register FIS."
-                " cmd_fis[0]=%02x cmd_fis[1]=%02x cmd_fis[2]=%02x\n",
-                cmd_fis[0], cmd_fis[1], cmd_fis[2]);
-        goto out;
-    }
-
-    if (!(cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER)) {
-        switch (s->dev[port].port_state) {
-        case STATE_RUN:
-            if (cmd_fis[15] & ATA_SRST) {
-                s->dev[port].port_state = STATE_RESET;
-            }
-            break;
-        case STATE_RESET:
-            if (!(cmd_fis[15] & ATA_SRST)) {
-                ahci_reset_port(s, port);
-            }
-            break;
-        }
-    }
-
-    else if (cmd_fis[1] & SATA_FIS_REG_H2D_UPDATE_COMMAND_REGISTER) {
-
-        /* Check for NCQ command */
-        if (is_ncq(cmd_fis[2])) {
-            process_ncq_command(s, port, cmd_fis, slot);
-            goto out;
-        }
-
-        /* Decompose the FIS:
-         * AHCI does not interpret FIS packets, it only forwards them.
-         * SATA 1.0 describes how to decode LBA28 and CHS FIS packets.
-         * Later specifications, e.g, SATA 3.2, describe LBA48 FIS packets.
-         *
-         * ATA4 describes sector number for LBA28/CHS commands.
-         * ATA6 describes sector number for LBA48 commands.
-         * ATA8 deprecates CHS fully, describing only LBA28/48.
-         *
-         * We dutifully convert the FIS into IDE registers, and allow the
-         * core layer to interpret them as needed. */
-        ide_state->feature = cmd_fis[3];
-        ide_state->sector = cmd_fis[4];     /* LBA 7:0 */
-        ide_state->lcyl = cmd_fis[5];       /* LBA 15:8  */
-        ide_state->hcyl = cmd_fis[6];       /* LBA 23:16 */
-        ide_state->select = cmd_fis[7];     /* LBA 27:24 (LBA28) */
-        ide_state->hob_sector = cmd_fis[8]; /* LBA 31:24 */
-        ide_state->hob_lcyl = cmd_fis[9];   /* LBA 39:32 */
-        ide_state->hob_hcyl = cmd_fis[10];  /* LBA 47:40 */
-        ide_state->hob_feature = cmd_fis[11];
-        ide_state->nsector = (int64_t)((cmd_fis[13] << 8) | cmd_fis[12]);
-        /* 14, 16, 17, 18, 19: Reserved (SATA 1.0) */
-        /* 15: Only valid when UPDATE_COMMAND not set. */
-
-        /* Copy the ACMD field (ATAPI packet, if any) from the AHCI command
-         * table to ide_state->io_buffer
-         */
-        if (opts & AHCI_CMD_ATAPI) {
-            memcpy(ide_state->io_buffer, &cmd_fis[AHCI_COMMAND_TABLE_ACMD], 0x10);
-            debug_print_fis(ide_state->io_buffer, 0x10);
-            s->dev[port].done_atapi_packet = false;
-            /* XXX send PIO setup FIS */
-        }
-
-        ide_state->error = 0;
-
-        /* Reset transferred byte counter */
-        cmd->status = 0;
-
-        /* We're ready to process the command in FIS byte 2. */
-        ide_exec_cmd(&s->dev[port].port, cmd_fis[2]);
-    }
-
 out:
     dma_memory_unmap(s->as, cmd_fis, cmd_len, DMA_DIRECTION_FROM_DEVICE,
                      cmd_len);
author
#24 lore raw

RFC 10/10 AHCI: Fix SDB FIS Construction

In reply to John Snow

The SDB FIS creation was mangled;
We were writing the error byte to byte 0, and omitting the SDB FIS magic byte.

Though the SDB packet layout states that:
byte 0: Must be 0xA1 to indicate SDB FIS. byte 1: Port multiplier select & other flags byte 2: status byte.
byte 3: error byte.

This patch adds an SDB FIS structure with human-readable names, and ensures that we are filling the structure appropriately.

---
 hw/ide/ahci.c | 23 ++++++++++++-----------
 hw/ide/ahci.h |  8 ++++++++
 2 files changed, 20 insertions(+), 11 deletions(-)
hw/ide/ahci.c +12 -11
index 5bc5a92..c2662fa 100644
@@ -567,26 +567,27 @@ static void debug_print_fis(uint8_t *fis, int cmd_len)
 
 static void ahci_write_fis_sdb(AHCIState *s, int port, uint32_t finished)
 {
-    AHCIPortRegs *pr = &s->dev[port].port_regs;
+    AHCIDevice *ad = &s->dev[port];
+    AHCIPortRegs *pr = &ad->port_regs;
     IDEState *ide_state;
-    uint8_t *sdb_fis;
+    SDBFIS *sdb_fis;
 
     if (!s->dev[port].res_fis ||
         !(pr->cmd & PORT_CMD_FIS_RX)) {
         return;
     }
 
-    sdb_fis = &s->dev[port].res_fis[RES_FIS_SDBFIS];
-    ide_state = &s->dev[port].port.ifs[0];
-
-    /* clear memory */
-    *(uint32_t*)sdb_fis = 0;
+    sdb_fis = (SDBFIS *)&ad->res_fis[RES_FIS_SDBFIS];
+    ide_state = &ad->port.ifs[0];
 
-    /* write values */
-    sdb_fis[0] = ide_state->error;
-    sdb_fis[2] = ide_state->status & 0x77;
+    sdb_fis->type = 0xA1;
+    /* Interrupt pending & Notification bit */
+    sdb_fis->flags = (ad->hba->control_regs.irqstatus ? (1 << 6) : 0);
+    sdb_fis->status = ide_state->status & 0x77;
+    sdb_fis->error = ide_state->error;
+    /* update SAct field in SDB_FIS */
     s->dev[port].finished |= finished;
-    *(uint32_t*)(sdb_fis + 4) = cpu_to_le32(s->dev[port].finished);
+    sdb_fis->payload = cpu_to_le32(ad->finished);
 
     ahci_trigger_irq(s, &s->dev[port], PORT_IRQ_SDB_FIS);
 }
hw/ide/ahci.h +8 -0
index 8745848..a1c107e 100644
@@ -348,6 +348,14 @@ typedef struct NCQFrame {
     uint8_t aux3;
 } QEMU_PACKED NCQFrame;
 
+typedef struct SDBFIS {
+    uint8_t type;
+    uint8_t flags;
+    uint8_t status;
+    uint8_t error;
+    uint32_t payload;
+} QEMU_PACKED SDBFIS;
+
 void ahci_init(AHCIState *s, DeviceState *qdev, AddressSpace *as, int ports);
 void ahci_uninit(AHCIState *s);
 
Signature · 1 line
1.9.3