Commit 06f5331cc1 for qemu.org

commit 06f5331cc1743bbdb4abab53ac49bc07a13bd166
Author: Keith Monahan <keith@techtravels.org>
Date:   Fri Nov 20 18:12:29 2026 +0100

    hw/scsi/ncr53c710: Drain asynchronous transfers in the register write

    The c720 driver of HP-UX 10.20 needs the completion interrupt ordering
    hardware provides, starting a command with interrupts masked and then
    spinning.  So after a register write starts an asynchronous transfer
    the model drives it to completion before returning to the guest (a
    bounded synchronous drain under the BQL, as IDE and MegaSAS do, falling
    back to asynchronous completion if it cannot advance).

    Signed-off-by: Keith Monahan <keith@techtravels.org>
    AI-used-for: Patch series decomposition (commit splitting and ordering)
    Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
    Signed-off-by: Helge Deller <deller@gmx.de>

diff --git a/hw/scsi/ncr53c710.c b/hw/scsi/ncr53c710.c
index 9a7b76a597..25469c7d7d 100644
--- a/hw/scsi/ncr53c710.c
+++ b/hw/scsi/ncr53c710.c
@@ -54,6 +54,12 @@

 #define LSI_BUF_SIZE     4096

+/*
+ * Safety bound on the synchronous completion drain loop in ncr710_reg_write();
+ * it then falls back to asynchronous completion.
+ */
+#define LSI_DRAIN_MAX_ITERS  100000
+
 static const char *const lsi_reg_names[64] = {
     "SCNTL0", "SCNTL1", "SDID", "SIEN", "SCID", "SXFER", "SODL", "SOCL",
     "SFBR", "SIDL", "SBDL", "SBCL", "DSTAT", "SSTAT0", "SSTAT1", "SSTAT2",
@@ -1561,7 +1567,30 @@ uint64_t ncr710_reg_read(NCR710State *s, hwaddr addr, unsigned size)

 void ncr710_reg_write(NCR710State *s, hwaddr addr, uint64_t val, unsigned size)
 {
+    unsigned drained = 0;
+
     lsi_reg_writeb(s, addr & 0xff, val & 0xff);
+
+    /*
+     * Drive any asynchronous SCSI transfer the register write kicked off to
+     * completion before returning to the guest, so its completion interrupt is
+     * raised first.  A deferred continue (lsi_issue_bh) has not issued its
+     * backing I/O yet, so pump it inline; otherwise blk_drain the backend.
+     */
+    while (s->waiting == LSI_DMA_IN_PROGRESS && s->current != NULL &&
+           s->current->req != NULL) {
+        if (drained++ >= LSI_DRAIN_MAX_ITERS) {
+            trace_ncr710_drain_exhausted(drained);
+            break;
+        }
+        if (s->issue_pending) {
+            lsi_issue_bh(s);
+        } else if (s->current->req->dev->conf.blk != NULL) {
+            blk_drain(s->current->req->dev->conf.blk);
+        } else {
+            break;      /* nothing deferred and no backend to drain */
+        }
+    }
 }

 static int lsi_pre_save(void *opaque)
diff --git a/hw/scsi/trace-events b/hw/scsi/trace-events
index f0eb145702..843d8f1a1e 100644
--- a/hw/scsi/trace-events
+++ b/hw/scsi/trace-events
@@ -340,6 +340,7 @@ ncr710_memmove(uint32_t dest, uint32_t src, int count) "MOVE MEMORY dest=0x%08x
 ncr710_do_dma_unavailable(void) "DMA: data not yet available"
 ncr710_wait_disconnect(void) "Wait Disconnect"
 ncr710_illegal_insn(uint32_t insn) "ILLEGAL instruction 0x%08x"
+ncr710_drain_exhausted(unsigned iters) "synchronous completion drain gave up after %u iters"

 # lasi_ncr710.c
 lasi_ncr710_device_realize(void) "Device realized"