Skip to contentSkip to conversation

hw/scsi/ncr53c710: rewrite to support HP-UX, Linux and NetBSD

PATCH In review Keith Monahan 5 participants 20 messages Jul 1 – Jul 18

lore
Share & tools
lore
#0 lore raw

v2 cover hw/scsi/ncr53c710: rewrite to support HP-UX, Linux and NetBSD

This series replaces the NCR 53C710 model so HP-UX 10.20 (including the Ignite/16700A media) installs and boots on the HP 715 (-M 715), while keeping the Linux lasi700 and NetBSD osiop drivers working.

I first tried to fix the existing model, but it took about a dozen fixes just to get the kernel's own driver to negotiate its first command, and the real problems were structural. It's connected-only and runs SCRIPTS synchronously inside the guest register write, so it can't do the disconnect/reselect or async completion HP-UX needs. Fixing that meant rewriting the interpreter, so the new engine is derived from lsi53c895a.c and adapted to the 710: big-endian SCRIPTS and table-indirect fetch, 24-bit DMA, the 710 register map, and the single-byte interrupt model.

v1 was one big rewrite patch; v2 splits it:

  1  hw/misc/lasi: independent LASI IRR fix (unchanged from v1).
  2  hw/scsi/lasi_ncr710: drop dead defines from the glue (no behaviour change).
  3  hw/scsi/ncr53c710: the base engine, enough to install and boot Debian
     (53c700) and boot NetBSD (osiop).

4 hw/scsi/ncr53c710: the HP-UX bits on top (overlapped tagged
disconnect/reselect, async completion, and the ATN message-reject tail).

Patch 3 is still the large one. It's a from-scratch rewrite on lsi53c895a.c rather than an edit of the old model, so it doesn't diff usefully against it, and splitting the engine itself would leave non-building intermediate states.

On factoring a shared core out of lsi53c895a.c (raised in review of v1):
only ~14% of the 710 is shared code, and the interrupt model and reselection differ in the interpreter's hot path, so the win is small while the regression and testing risk lands on lsi53c895a.c and its guests. I've kept them as sibling models.

Tested on -M 715: HP-UX 10.20 install and reboot (Ignite and an overlapping tagged software install), Debian install and boot, NetBSD 9.4 and 10.1 boot and device enumeration.

v1 -> v2: split the rewrite into reviewable pieces; no functional change.

Keith Monahan (4):
hw/misc/lasi: derive IRR from pending and unmasked requests hw/scsi/lasi_ncr710: remove dead and shadowing defines hw/scsi/ncr53c710: rewrite the SCRIPTS engine hw/scsi/ncr53c710: support HP-UX overlapped tagged commands

 hw/misc/lasi.c        |   25 +-
 hw/scsi/lasi_ncr710.c |  118 +-
 hw/scsi/lasi_ncr710.h |   28 +-
 hw/scsi/ncr53c710.c   | 3042 +++++++++++++++++------------------------
 hw/scsi/ncr53c710.h   |  295 ++--
 hw/scsi/trace-events  |   43 +-
 6 files changed, 1481 insertions(+), 2070 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/4 hw/misc/lasi: derive IRR from pending and unmasked requests

The LASI interrupt request register (IRR) was latched: set when a source asserted and then never cleared or re-evaluated against the mask, so a masked or dropped request stayed set forever. The parisc core I/O dispatcher reads IRR to find its interrupt source, so the stuck bit was returned on every later interrupt as an "unexpected core I/O interrupt". On an installed HP-UX system an unacknowledged i82596 LAN interrupt latched this way and the flood wedged the boot.

Derive IRR as (pending & unmasked) with IPR tracking each source's level, matching the hardware and the in-tree parisc gsc/lasi driver.

---
 hw/misc/lasi.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
hw/misc/lasi.c +19 -6
index e8e1578b75..f0dfd78b4d 100644
@@ -62,7 +62,15 @@ static MemTxResult lasi_chip_read_with_attrs(void *opaque, hwaddr addr,
 
     switch (addr) {
     case LASI_IRR:
-        val = s->irr;
+        /*
+         * The interrupt request register reports the interrupts that are both
+         * pending and unmasked, derived live from IPR and IMR rather than
+         * latched, so masking or deasserting a source removes it immediately.
+         * The parisc core I/O interrupt dispatcher reads IRR; a latched bit
+         * that never cleared would be redelivered forever as a phantom
+         * "unexpected" interrupt.
+         */
+        val = s->ipr & s->imr;
         break;
     case LASI_IMR:
         val = s->imr;
@@ -234,13 +242,18 @@ static void lasi_set_irq(void *opaque, int irq, int level)
 
     if (level) {
         s->ipr |= bit;
-        if (bit & s->imr) {
+        if ((bit & s->imr) && (s->icr & ICR_BUS_ERROR_BIT) == 0) {
             uint32_t iar = s->iar;
-            s->irr |= bit;
-            if ((s->icr & ICR_BUS_ERROR_BIT) == 0) {
-                stl_be_phys(&address_space_memory, iar & -32, iar & 31);
-            }
+            stl_be_phys(&address_space_memory, iar & -32, iar & 31);
         }
+    } else {
+        /*
+         * The interrupt sources are level triggered, so a source that drops
+         * its request must clear its pending bit.  Otherwise the bit stays set
+         * in IPR (and hence IRR) and is redelivered as a phantom "unexpected"
+         * core I/O interrupt on every later interrupt.
+         */
+        s->ipr &= ~bit;
     }
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] hw/misc/lasi: derive IRR from pending and unmasked requests

Quoted 8 lines · Keith Monahan

The LASI interrupt request register (IRR) was latched: set when a source asserted and then never cleared or re-evaluated against the mask, so a masked or dropped request stayed set forever. The parisc core I/O dispatcher reads IRR to find its interrupt source, so the stuck bit was returned on every later interrupt as an "unexpected core I/O interrupt". On an installed HP-UX system an unacknowledged i82596 LAN interrupt latched this way and the flood wedged the boot.

Quoted 4 lines

Derive IRR as (pending & unmasked) with IPR tracking each source's level, matching the hardware and the in-tree parisc gsc/lasi driver.

Thanks Keith!
I'll pick it up in my upcoming hppa git tree, so soon you don't need to carry it extra. I hope this is OK?

Helge

#3 lore raw

Re: [PATCH v2 1/4] hw/misc/lasi: derive IRR from pending and unmasked requests

Perfect, thanks for the help.

Keith

Quoted 21 lines · Helge Deller
Quoted 8 lines · Keith Monahan

The LASI interrupt request register (IRR) was latched: set when a source asserted and then never cleared or re-evaluated against the mask, so a masked or dropped request stayed set forever. The parisc core I/O dispatcher reads IRR to find its interrupt source, so the stuck bit was returned on every later interrupt as an "unexpected core I/O interrupt". On an installed HP-UX system an unacknowledged i82596 LAN interrupt latched this way and the flood wedged the boot.

Quoted 4 lines

Derive IRR as (pending & unmasked) with IPR tracking each source's level, matching the hardware and the in-tree parisc gsc/lasi driver.

Thanks Keith!
I'll pick it up in my upcoming hppa git tree, so soon you don't need to carry it extra. I hope this is OK?

Helge

#4 lore raw

PATCH v2 2/4 hw/scsi/lasi_ncr710: remove dead and shadowing defines

In reply to Keith Monahan

Drop unused register and phase defines that duplicate the authoritative definitions in ncr53c710.h (LASI_SCSI_RESET, PHASE_MASK, NCR710_SCNTL1_RST and friends), and the leftover SCNR debug constant. None are referenced.

---
 hw/scsi/lasi_ncr710.c |  1 -
 hw/scsi/lasi_ncr710.h | 16 ----------------
 2 files changed, 17 deletions(-)
hw/scsi/lasi_ncr710.c +0 -1
index 4fde2265b5..3fccda9e4e 100644
@@ -24,7 +24,6 @@
 #include "system/dma.h"
 
 #define LASI_710_SVERSION    0x00082
-#define SCNR                 0xBEEFBABE
 #define LASI_710_HVERSION    0x3D
 #define HPHW_FIO             5        /* Fixed I/O module */
 
hw/scsi/lasi_ncr710.h +0 -16
index 450fb7e1c3..75d3e77d20 100644
@@ -22,22 +22,6 @@
 #define TYPE_LASI_NCR710 "lasi-ncr710"
 OBJECT_DECLARE_SIMPLE_TYPE(LasiNCR710State, LASI_NCR710)
 
-#define LASI_SCSI_RESET         0x000   /* SCSI Reset Register */
-#define LASI_SCSI_NCR710_BASE   0x100   /* NCR710 Base Register Offset */
-
-#define PARISC_DEVICE_ID_OFF    0x00    /* HW type, HVERSION, SVERSION */
-#define PARISC_DEVICE_CONFIG_OFF 0x04   /* Configuration data */
-
-#define PHASE_MASK              7
-#define PHASE_DO                0
-
-#define NCR710_SCNTL1_RST       0x08    /* SCSI Reset */
-#define NCR710_ISTAT_RST        0x40    /* Device Reset */
-#define NCR710_ISTAT_ABRT       0x80    /* Script Abort */
-#define NCR710_ISTAT_CON        0x08    /* ISTAT_Connected */
-#define NCR710_DSTAT_DFE        0x80    /* DMA FIFO Empty */
-#define NCR710_CTEST2_DACK      0x01    /* DMA Acknowledge */
-
 typedef struct LasiNCR710State {
     SysBusDevice parent_obj;
     MemoryRegion mmio;
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 3/4 hw/scsi/ncr53c710: rewrite the SCRIPTS engine

In reply to Keith Monahan

Replace the NCR 53C710 model with a SCRIPTS engine derived from QEMU's LSI53C895A model (hw/scsi/lsi53c895a.c), another device in the same NCR/LSI SCRIPTS family, adapted to the 53C710: its register map, the single-byte DSTAT/SSTAT0 interrupt model, big-endian SCRIPTS and table-indirect fetch (the part sits on the big-endian PA-RISC LASI bus), and 24-bit DMA byte counts. Behaviour follows the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide (Oct 1990).

The base engine supports connected I/O and reselection-interrupt disconnect, enough to install and boot Debian off the 710 with the Linux lasi700/53c700 driver and to boot NetBSD/hppa 9.4 and 10.1 (osiop) with disk and CD-ROM enumeration. Reselection follows the 53C700 family:
gated on the (re)selection interrupt enable (SIEN.SEL) alone, one-hot SDID, and an SFBR id bitmask in 700-compatibility mode. SDTR/WDTR/PPR negotiation is declined with MESSAGE REJECT, which the 700-family drivers require to proceed: unlike the 8xx parts they leave the target to drive the phase after the reject.

The LASI glue (hw/scsi/lasi_ncr710.c) embeds one NCR710State by value. It gains the free_request SCSIBusInfo callback and request-queue init the core needs, renames the reselection timer to scripts_timer, and narrows the SCSI bus to the 8-bit inclusive maxima.

The migration format is incompatible with the model this replaces, so the vmstate version and minimum version are both raised.

---
 hw/scsi/lasi_ncr710.c |  117 +-
 hw/scsi/lasi_ncr710.h |   12 +-
 hw/scsi/ncr53c710.c   | 2795 +++++++++++++++--------------------------
 hw/scsi/ncr53c710.h   |  276 ++--
 hw/scsi/trace-events  |   43 +-
 5 files changed, 1191 insertions(+), 2052 deletions(-)
hw/scsi/lasi_ncr710.c +47 -70
index 3fccda9e4e..bca2695104 100644
@@ -1,12 +1,12 @@
 /*
- * LASI Wrapper for NCR710 SCSI Controller
+ * LASI wrapper for the NCR 53C710 SCSI controller
  *
  * Copyright (c) 2025 Soumyajyotii Ssarkar <[email protected]>
- * This driver was developed during the Google Summer of Code 2025 program.
- * Mentored by Helge Deller <[email protected]>
+ * Developed during Google Summer of Code 2025, mentored by
+ * Helge Deller <[email protected]>.
  *
- * NCR710 SCSI Controller implementation
- * Based on the NCR53C710 Technical Manual Version 3.2, December 2000
+ * LASI module glue around the 53C710 core in ncr53c710.c: it embeds one
+ * NCR710State by value and maps it onto the HP 715 (-M 715) LASI bus.
  *
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
@@ -33,8 +33,6 @@ static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr,
     LasiNCR710State *s = LASI_NCR710(opaque);
     uint64_t val = 0;
 
-    trace_lasi_ncr710_reg_read(addr, 0, size);
-
     if (addr == 0x00) {  /* Device ID */
         val = (HPHW_FIO << 24) | LASI_710_SVERSION;
         trace_lasi_ncr710_reg_read_id(HPHW_FIO, LASI_710_SVERSION, val);
@@ -50,22 +48,22 @@ static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr,
     if (addr >= 0x100) {
         hwaddr ncr_addr = addr - 0x100;
         if (size == 1) {
+            /*
+             * Single byte access: flip the byte lane (PA-RISC big endian
+             * access -> the chip's little endian register order).
+             */
             ncr_addr ^= 3;
-            NCR710_DPRINTF("Reading value to LASI WRAPPER == 0x%lx%s, "
-                           "val=0x%lx, size=%u\n",
-                           addr - 0x100, size == 1 ? " (XORed)" : "",
-                           val, size);
             val = ncr710_reg_read(&s->ncr710, ncr_addr, size);
         } else {
+            /*
+             * Multibyte access: gather little endian, no lane flip, so
+             * the 24/32 bit registers assemble correctly.
+             */
             val = 0;
             for (unsigned i = 0; i < size; i++) {
                 uint8_t byte_val = ncr710_reg_read(&s->ncr710, ncr_addr + i, 1);
                 val |= ((uint64_t)byte_val) << (i * 8);
-                NCR710_DPRINTF("  Read byte %u from NCR addr 0x%lx: "
-                               "0x%02x\n", i, ncr_addr + i, byte_val);
             }
-            NCR710_DPRINTF("  Reconstructed %u-byte value: 0x%lx\n",
-                           size, val);
         }
 
         trace_lasi_ncr710_reg_forward_read(addr, val);
@@ -83,7 +81,13 @@ static void lasi_ncr710_reg_write(void *opaque, hwaddr addr,
 
     trace_lasi_ncr710_reg_write(addr, val, size);
 
-    if (addr <= 0x0F) {
+    /*
+     * 0x00..0x0f is the read only LASI module header (ID/SVERSION/HVERSION) and
+     * 0x10..0xff is unmapped module space; writes to both are ignored.  The
+     * SCSI bus reset is driven through the chip's SCNTL1.RST at forwarded
+     * offset 0x101 (see ncr53c710.c), not through this window.
+     */
+    if (addr <= 0x0f) {
         return;
     }
 
@@ -92,16 +96,10 @@ static void lasi_ncr710_reg_write(void *opaque, hwaddr addr,
 
         if (size == 1) {
             ncr_addr ^= 3;
-            NCR710_DPRINTF("Writing value to LASI WRAPPER == 0x%lx%s, "
-                           "val=0x%lx, size=%u\n",
-                           addr - 0x100, size == 1 ? " (XORed)" : "",
-                           val, size);
             ncr710_reg_write(&s->ncr710, ncr_addr, val, size);
         } else {
             for (unsigned i = 0; i < size; i++) {
                 uint8_t byte_val = (val >> (i * 8)) & 0xff;
-                 NCR710_DPRINTF("  Writing byte %u to NCR addr 0x%lx: 0x%02x\n",
-                       i, ncr_addr + i, byte_val);
                 ncr710_reg_write(&s->ncr710, ncr_addr + i, byte_val, 1);
             }
         }
@@ -112,10 +110,7 @@ static void lasi_ncr710_reg_write(void *opaque, hwaddr addr,
     }
 }
 
-/*
- * req_cancelled, command_complete, transfer_data forwards
- * commands to its core counterparts.
- */
+/* SCSIBusInfo callbacks: trace, then forward to the ncr53c710 core. */
 static void lasi_ncr710_request_cancelled(SCSIRequest *req)
 {
     trace_lasi_ncr710_request_cancelled(req);
@@ -128,20 +123,27 @@ static void lasi_ncr710_command_complete(SCSIRequest *req, size_t resid)
     ncr710_command_complete(req, resid);
 }
 
- static void lasi_ncr710_transfer_data(SCSIRequest *req, uint32_t len)
+static void lasi_ncr710_transfer_data(SCSIRequest *req, uint32_t len)
 {
     trace_lasi_ncr710_transfer_data(len);
     ncr710_transfer_data(req, len);
 }
 
+static void lasi_ncr710_free_request(SCSIBus *bus, void *priv)
+{
+    g_free(priv);
+}
+
 static const struct SCSIBusInfo lasi_ncr710_scsi_info = {
     .tcq = true,
-    .max_target = 8,
-    .max_lun = 8,  /* full LUN support */
+    /* 8-bit bus: targets and LUNs 0-7 (scsi-bus.c maxima are inclusive). */
+    .max_target = 7,
+    .max_lun = 7,
 
     .transfer_data = lasi_ncr710_transfer_data,
     .complete = lasi_ncr710_command_complete,
     .cancel = lasi_ncr710_request_cancelled,
+    .free_request = lasi_ncr710_free_request,
 };
 
 static const MemoryRegionOps lasi_ncr710_mmio_ops = {
@@ -156,12 +158,9 @@ static const MemoryRegionOps lasi_ncr710_mmio_ops = {
 
 static const VMStateDescription vmstate_lasi_ncr710 = {
     .name = "lasi-ncr710",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .fields = (const VMStateField[]) {
-        VMSTATE_UINT32(hw_type, LasiNCR710State),
-        VMSTATE_UINT32(sversion, LasiNCR710State),
-        VMSTATE_UINT32(hversion, LasiNCR710State),
         VMSTATE_STRUCT(ncr710, LasiNCR710State, 1, vmstate_ncr710, NCR710State),
         VMSTATE_END_OF_LIST()
     }
@@ -178,49 +177,36 @@ static void lasi_ncr710_realize(DeviceState *dev, Error **errp)
                   &lasi_ncr710_scsi_info);
     s->ncr710.as = &address_space_memory;
     s->ncr710.irq = s->lasi_irq;
+    QTAILQ_INIT(&s->ncr710.queue);
 
-    s->ncr710.reselection_retry_timer =
+    s->ncr710.scripts_timer =
         timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                     ncr710_reselection_retry_callback,
+                     ncr710_scripts_timer_callback,
                      &s->ncr710);
 
     ncr710_soft_reset(&s->ncr710);
 
     trace_lasi_ncr710_timers_initialized(
-        (uint64_t)s->ncr710.reselection_retry_timer);
+        (uint64_t)s->ncr710.scripts_timer);
 
-    /* Initialize memory region */
     memory_region_init_io(&s->mmio, OBJECT(dev), &lasi_ncr710_mmio_ops, s,
                           "lasi-ncr710", 0x200);
     sysbus_init_mmio(sbd, &s->mmio);
 }
 
-void lasi_ncr710_handle_legacy_cmdline(DeviceState *lasi_dev)
+static void lasi_ncr710_unrealize(DeviceState *dev)
 {
-    LasiNCR710State *s = LASI_NCR710(lasi_dev);
-    SCSIBus *bus = &s->ncr710.bus;
-    int found_drives = 0;
-
-    if (!bus) {
-        return;
-    }
+    LasiNCR710State *s = LASI_NCR710(dev);
 
-    for (int unit = 0; unit <= 7; unit++) {
-        DriveInfo *dinfo = drive_get(IF_SCSI, bus->busnr, unit);
-        if (dinfo) {
-            trace_lasi_ncr710_legacy_drive_found(bus->busnr, unit);
-            found_drives++;
-        }
-    }
+    timer_free(s->ncr710.scripts_timer);
+    s->ncr710.scripts_timer = NULL;
+}
 
-    trace_lasi_ncr710_handle_legacy_cmdline(bus->busnr, found_drives);
+void lasi_ncr710_handle_legacy_cmdline(DeviceState *lasi_dev)
+{
+    LasiNCR710State *s = LASI_NCR710(lasi_dev);
 
-    scsi_bus_legacy_handle_cmdline(bus);
-    BusChild *kid;
-    QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
-        trace_lasi_ncr710_scsi_device_created(
-            object_get_typename(OBJECT(kid->child)));
-    }
+    scsi_bus_legacy_handle_cmdline(&s->ncr710.bus);
 }
 
 DeviceState *lasi_ncr710_init(MemoryRegion *addr_space, hwaddr hpa,
@@ -247,20 +233,12 @@ static void lasi_ncr710_reset(DeviceState *dev)
     ncr710_soft_reset(&s->ncr710);
 }
 
-static void lasi_ncr710_instance_init(Object *obj)
-{
-    LasiNCR710State *s = LASI_NCR710(obj);
-
-    s->hw_type = HPHW_FIO;
-    s->sversion = LASI_710_SVERSION;
-    s->hversion = LASI_710_HVERSION;
-}
-
 static void lasi_ncr710_class_init(ObjectClass *klass, const void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
 
     dc->realize = lasi_ncr710_realize;
+    dc->unrealize = lasi_ncr710_unrealize;
     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
     dc->fw_name = "scsi";
     dc->desc = "HP-PARISC LASI NCR710 SCSI adapter";
@@ -273,7 +251,6 @@ static const TypeInfo lasi_ncr710_info = {
     .name          = TYPE_LASI_NCR710,
     .parent        = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(LasiNCR710State),
-    .instance_init = lasi_ncr710_instance_init,
     .class_init    = lasi_ncr710_class_init,
 };
 
hw/scsi/lasi_ncr710.h +3 -9
index 75d3e77d20..5168f61715 100644
@@ -1,12 +1,9 @@
 /*
- * LASI Wrapper for NCR710 SCSI Controller
+ * LASI wrapper for the NCR 53C710 SCSI controller
  *
  * Copyright (c) 2025 Soumyajyotii Ssarkar <[email protected]>
- * This driver was developed during the Google Summer of Code 2025 program.
- * Mentored by Helge Deller <[email protected]>
- *
- * NCR710 SCSI Controller implementation
- * Based on the NCR53C710 Technical Manual Version 3.2, December 2000
+ * Developed during Google Summer of Code 2025, mentored by
+ * Helge Deller <[email protected]>.
  *
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
@@ -26,9 +23,6 @@ typedef struct LasiNCR710State {
     SysBusDevice parent_obj;
     MemoryRegion mmio;
     qemu_irq lasi_irq;       /* IRQ line to LASI controller */
-    uint32_t hw_type;        /* Hardware type (HPHW_*) */
-    uint32_t sversion;       /* Software version */
-    uint32_t hversion;       /* Hardware version */
     NCR710State ncr710;
 } LasiNCR710State;
 
hw/scsi/ncr53c710.c +1012 -1783

2,746 lines not on this page · raw message

hw/scsi/ncr53c710.h +97 -179

276 lines not on this page · raw message

hw/scsi/trace-events +32 -11

43 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.43.0

Part of the diff is not shown here. View the raw message.

#6 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Quoted 27 lines · Keith Monahan

Replace the NCR 53C710 model with a SCRIPTS engine derived from QEMU's LSI53C895A model (hw/scsi/lsi53c895a.c), another device in the same NCR/LSI SCRIPTS family, adapted to the 53C710: its register map, the single-byte DSTAT/SSTAT0 interrupt model, big-endian SCRIPTS and table-indirect fetch (the part sits on the big-endian PA-RISC LASI bus), and 24-bit DMA byte counts. Behaviour follows the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide (Oct 1990).

The base engine supports connected I/O and reselection-interrupt disconnect, enough to install and boot Debian off the 710 with the Linux lasi700/53c700 driver and to boot NetBSD/hppa 9.4 and 10.1 (osiop) with disk and CD-ROM enumeration. Reselection follows the 53C700 family:
gated on the (re)selection interrupt enable (SIEN.SEL) alone, one-hot SDID, and an SFBR id bitmask in 700-compatibility mode. SDTR/WDTR/PPR negotiation is declined with MESSAGE REJECT, which the 700-family drivers require to proceed: unlike the 8xx parts they leave the target to drive the phase after the reject.

The LASI glue (hw/scsi/lasi_ncr710.c) embeds one NCR710State by value. It gains the free_request SCSIBusInfo callback and request-queue init the core needs, renames the reselection timer to scripts_timer, and narrows the SCSI bus to the 8-bit inclusive maxima.

The migration format is incompatible with the model this replaces, so the vmstate version and minimum version are both raised.

Quoted 8 lines
---
 hw/scsi/lasi_ncr710.c |  117 +-
 hw/scsi/lasi_ncr710.h |   12 +-
 hw/scsi/ncr53c710.c   | 2795 +++++++++++++++--------------------------
 hw/scsi/ncr53c710.h   |  276 ++--
 hw/scsi/trace-events  |   43 +-
 5 files changed, 1191 insertions(+), 2052 deletions(-)

I'm likely not going to be the one doing detailed review here, so my opinion here is not necessarily 100% controlling, but from my perspective this is still significantly too large to review, both in raw size terms and because it has a lot of changes in it that do not all need or want to be in the same patch.

Just looking through the very early stuff to show some examples of changes that don't need to be in this patch:

Quoted 10 lines
hw/scsi/lasi_ncr710.c +4 -1
@@ -50,22 +48,22 @@ static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr,
     if (addr >= 0x100) {
         hwaddr ncr_addr = addr - 0x100;
         if (size == 1) {
+            /*
+             * Single byte access: flip the byte lane (PA-RISC big endian
+             * access -> the chip's little endian register order).
+             */
             ncr_addr ^= 3;
-            NCR710_DPRINTF("Reading value to LASI WRAPPER == 0x%lx%s, "

hw/scsi/lasi_ncr710.c static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr, · line 62

Quoted 10 lines
hw/scsi/lasi_ncr710.c +4 -3
-                           "val=0x%lx, size=%u\n",
-                           addr - 0x100, size == 1 ? " (XORed)" : "",
-                           val, size);
             val = ncr710_reg_read(&s->ncr710, ncr_addr, size);
         } else {
+            /*
+             * Multibyte access: gather little endian, no lane flip, so
+             * the 24/32 bit registers assemble correctly.
+             */
             val = 0;

These are comment improvements to existing code. They don't need to be in the big patch.

hw/scsi/lasi_ncr710.c static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr, · line 68

Quoted 8 lines
hw/scsi/lasi_ncr710.c +0 -4
             for (unsigned i = 0; i < size; i++) {
                 uint8_t byte_val = ncr710_reg_read(&s->ncr710, ncr_addr + i, 1);
                 val |= ((uint64_t)byte_val) << (i * 8);
-                NCR710_DPRINTF("  Read byte %u from NCR addr 0x%lx: "
-                               "0x%02x\n", i, ncr_addr + i, byte_val);
             }
-            NCR710_DPRINTF("  Reconstructed %u-byte value: 0x%lx\n",
-                           size, val);

Here we drop some debug printfs. That can be done separately.

Quoted 7 lines
hw/scsi/lasi_ncr710.c +0 -0
         }
 
         trace_lasi_ncr710_reg_forward_read(addr, val);
@@ -83,7 +81,13 @@ static void lasi_ncr710_reg_write(void *opaque, hwaddr addr,
 
     trace_lasi_ncr710_reg_write(addr, val, size);
 

hw/scsi/lasi_ncr710.c static void lasi_ncr710_reg_write(void *opaque, hwaddr addr, · line 92

Quoted 10 lines
hw/scsi/lasi_ncr710.c +7 -1
-    if (addr <= 0x0F) {
+    /*
+     * 0x00..0x0f is the read only LASI module header (ID/SVERSION/HVERSION) and
+     * 0x10..0xff is unmapped module space; writes to both are ignored.  The
+     * SCSI bus reset is driven through the chip's SCNTL1.RST at forwarded
+     * offset 0x101 (see ncr53c710.c), not through this window.
+     */
+    if (addr <= 0x0f) {
         return;
     }

This is a better explanatory comment. That's not something that needs to be in the big patch.

Quoted 8 lines
hw/scsi/lasi_ncr710.c +0 -3
@@ -92,16 +96,10 @@ static void lasi_ncr710_reg_write(void *opaque, hwaddr addr,
 
         if (size == 1) {
             ncr_addr ^= 3;
-            NCR710_DPRINTF("Writing value to LASI WRAPPER == 0x%lx%s, "
-                           "val=0x%lx, size=%u\n",
-                           addr - 0x100, size == 1 ? " (XORed)" : "",

hw/scsi/lasi_ncr710.c static void lasi_ncr710_reg_write(void *opaque, hwaddr addr, · line 105

Quoted 10 lines
hw/scsi/lasi_ncr710.c +0 -3
-                           val, size);
             ncr710_reg_write(&s->ncr710, ncr_addr, val, size);
         } else {
             for (unsigned i = 0; i < size; i++) {
                 uint8_t byte_val = (val >> (i * 8)) & 0xff;
-                 NCR710_DPRINTF("  Writing byte %u to NCR addr 0x%lx: 0x%02x\n",
-                       i, ncr_addr + i, byte_val);
                 ncr710_reg_write(&s->ncr710, ncr_addr + i, byte_val, 1);
             }
         }

Debug printfs being removed again.

hw/scsi/ncr53c710.c line 5

Quoted 11 lines
hw/scsi/ncr53c710.c +1 -3
index 57967e7a84..57cd7d91a4 100644
@@ -1,599 +1,308 @@
 /*
- * QEMU NCR710 SCSI Controller
+ * QEMU NCR 53C710 SCSI I/O Processor emulation
  *
- * Copyright (c) 2025 Soumyajyotii Ssarkar <[email protected]>
- * This driver was developed during the Google Summer of Code 2025 program.

Generally speaking you shouldn't drop somebody's Copyright line. Even if the code has been mostly rewritten, there's still some of Soumyajyotii's code here, I expect.

Quoted 46 lines
hw/scsi/ncr53c710.c +16 -23
+ * Copyright (c) 2026 Keith Monahan <[email protected]>
+ * Copyright (c) 2006 CodeSourcery (lsi53c895a.c, written by Paul Brook)
  *
- * NCR710 SCSI Controller implementation
- * Based on the NCR53C710 Technical Manual Version 3.2, December 2000
+ * A rewrite of the QEMU NCR 53C710 model.  The 53C710 is an early member of
+ * the NCR/LSI SCRIPTS processor family, so the SCRIPTS engine here is derived
+ * from QEMU's LSI53C895A model and adapted to the 53C710's register map,
+ * single byte interrupt model, big endian SCRIPTS fetch (PA-RISC) and 24 bit
+ * DMA, per the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide
+ * (Oct 1990).
  *
- * Developed from an implementation of NCR53C710 by Helge Deller
- * which was interim based on the implementation by Toni Wilen for UAE.
+ * Derived from hw/scsi/lsi53c895a.c (Copyright (c) 2006 CodeSourcery, written
+ * by Paul Brook).
  *
- * SPDX-License-Identifier: GPL-2.0-or-later
+ * Replaces the 53C710 model written for Google Summer of Code 2025 by
+ * Soumyajyotii Ssarkar <[email protected]>, itself based on
+ * earlier NCR 53C710 work by Helge Deller and by Toni Wilen (for UAE).
  *
- * Contents:
- * 1. Register Definitions
- * 2. Register name functions
- * 3. Parity functions
- * 4. SCSI FIFO Structures
- * 5. Scripts Misc functions
- * 6. DMA functions
- * 7. Scripts functions
- * 8. Read and Write functions
- * 9. QEMU Device model functions
+ * The only instantiated device is the LASI wrapper (lasi_ncr710.c), which
+ * embeds one NCR710State by value and drives it through this file.
  *
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
-/* SCNTL0 (0x00) - SCSI Control Register 0 */
-#define NCR710_SCNTL0_TRG    0x01
-#define NCR710_SCNTL0_AAP    0x02
-#define NCR710_SCNTL0_EPG    0x04
-#define NCR710_SCNTL0_EPC    0x08
-#define NCR710_SCNTL0_WATN   0x10
-#define NCR710_SCNTL0_START  0x20
-#define NCR710_SCNTL0_ARB0   0x40

hw/scsi/ncr53c710.c

Quoted 10 lines
hw/scsi/ncr53c710.c +7 -1
-#define NCR710_SCNTL0_ARB1   0x80
 
 
+/* SCNTL0 (0x00) */
+#define NCR710_SCNTL0_TRG       0x01
+#define NCR710_SCNTL0_AAP       0x02
+#define NCR710_SCNTL0_EPG       0x04
+#define NCR710_SCNTL0_EPC       0x08
+#define NCR710_SCNTL0_WATN      0x10
+#define NCR710_SCNTL0_START     0x20

These defines haven't actually changed, except that you've reformatted the whitespace. If you want to adjust the whitespace, that's a separate patch, but better still don't do that. Then all this stuff doesn't appear in this patch at all because it doesn't change.

Quoted 7 lines
hw/scsi/ncr53c710.c +7 -0
+static const char *const ncr710_reg_names[64] = {
+    "SCNTL0", "SCNTL1", "SDID", "SIEN", "SCID", "SXFER", "SODL", "SOCL",
+    "SFBR", "SIDL", "SBDL", "SBCL", "DSTAT", "SSTAT0", "SSTAT1", "SSTAT2",
+    "DSA0", "DSA1", "DSA2", "DSA3", "CTEST0", "CTEST1", "CTEST2", "CTEST3",
+    "CTEST4", "CTEST5", "CTEST6", "CTEST7", "TEMP0", "TEMP1", "TEMP2", "TEMP3",
+    "DFIFO", "ISTAT", "CTEST8", "LCRC", "DBC0", "DBC1", "DBC2", "DCMD",
+    "DNAD0", "DNAD1", "DNAD2", "DNAD3", "DSP0", "DSP1", "DSP2", "DSP3",

hw/scsi/ncr53c710.c line 164

Quoted 10 lines
hw/scsi/ncr53c710.c +7 -1
+    "DSPS0", "DSPS1", "DSPS2", "DSPS3", "SCRATCH0", "SCRATCH1", "SCRATCH2",
+    "SCRATCH3", "DMODE", "DIEN", "DWT", "DCNTL", "ADDER0", "ADDER1", "ADDER2",
+    "ADDER3",
+};
+
+static const char *ncr710_reg_name(int offset)
 {
-    return (s->sien0 & NCR710_SSTAT0_SEL) != 0;
+    return (offset >= 0 && offset < 64) ? ncr710_reg_names[offset] : "???";
 }

This is a rewrite of the old version of this function, to use an array of strings instead of a big switch. It is definitely nicer code, but this change can be done as its own patch.

Personally my experience is that it is much easier to structure big changes into smaller pieces to start with, rather than doing the big change first and then having to break it up into smaller pieces later. Doing things in smaller pieces makes it easier to isolate where bugs or regressions are, both during development and later if we need to git bisect once a change has landed.

thanks
-- PMM

#7 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Peter,

Thanks for going through it. I raised this originally in the v1 thread and I am not sure it reached you, so let me lay it out here.

The size is real, but the more useful thing to flag is that reading this as a diff against the old model will not give you an accurate view of the change, because these are not two versions of one model. They are two different implementations, and git is aligning text between them that never shared any code.

Three concrete ways they differ:

1. Different origin. The old model descends from Toni Wilen's UAE implementation by way of Helge Deller. The new engine is derived from QEMU's own lsi53c895a.c. They are unrelated codebases, so the delta is not one evolving into the other, it is one file removed and a different one added.

2. Different execution model. The old model runs SCRIPTS synchronously inside the guest's register write, so completion interrupts come back reentrantly. The new one runs the engine asynchronously and delivers completion the way the chip actually does. That is a different
control flow from top to bottom, not a local change.

3. Different connection model. The old model is connected only, with no target initiated disconnect or reselect. The new one implements disconnect and reselect and the state machine around them. That is a different device state machine, not a modified one.

Because of those three, the two files do not line up function to function or line to line. The diff is large and noisy precisely because git has almost nothing to match, so it renders a wholesale replacement as a wall of edits. You will not learn what actually changed by reading the delta; the way to review this is to read ncr53c710.c as a new file against the 53C710 manual, not as a patch on the old one.

For what it is worth, I did try to get there incrementally first. It took about a dozen fixes to the old model just to get the chip's own driver to negotiate its first command, and it still would not boot reliably, because the two blockers, connected only and synchronous SCRIPTS, are structural and cannot be patched out in small local steps. That is why I stopped treating the old code as a base: it has no path to an HP-UX boot to build on.

I do appreciate that this is a large and complicated model, and that reviewing it will take real time; I am not trying to wave that away. But the incremental path would be worse on exactly that front. A series that converted the old model into this one would be a long chain of patches to write and a long chain to review, most of them touching code that is thrown away by the end. Reviewing one coherent new file, even a big one, is less work than reviewing however many steps it would take to get there.

You really can't get THERE from HERE, which is why I took this approach.

Thanks,
Keith

Quoted 235 lines · Peter Maydell
Quoted 27 lines · Keith Monahan

Replace the NCR 53C710 model with a SCRIPTS engine derived from QEMU's LSI53C895A model (hw/scsi/lsi53c895a.c), another device in the same NCR/LSI SCRIPTS family, adapted to the 53C710: its register map, the single-byte DSTAT/SSTAT0 interrupt model, big-endian SCRIPTS and table-indirect fetch (the part sits on the big-endian PA-RISC LASI bus), and 24-bit DMA byte counts. Behaviour follows the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide (Oct 1990).

The base engine supports connected I/O and reselection-interrupt disconnect, enough to install and boot Debian off the 710 with the Linux lasi700/53c700 driver and to boot NetBSD/hppa 9.4 and 10.1 (osiop) with disk and CD-ROM enumeration. Reselection follows the 53C700 family:
gated on the (re)selection interrupt enable (SIEN.SEL) alone, one-hot SDID, and an SFBR id bitmask in 700-compatibility mode. SDTR/WDTR/PPR negotiation is declined with MESSAGE REJECT, which the 700-family drivers require to proceed: unlike the 8xx parts they leave the target to drive the phase after the reject.

The LASI glue (hw/scsi/lasi_ncr710.c) embeds one NCR710State by value. It gains the free_request SCSIBusInfo callback and request-queue init the core needs, renames the reselection timer to scripts_timer, and narrows the SCSI bus to the 8-bit inclusive maxima.

The migration format is incompatible with the model this replaces, so the vmstate version and minimum version are both raised.

Quoted 8 lines
---
  hw/scsi/lasi_ncr710.c |  117 +-
  hw/scsi/lasi_ncr710.h |   12 +-
  hw/scsi/ncr53c710.c   | 2795 +++++++++++++++--------------------------
  hw/scsi/ncr53c710.h   |  276 ++--
  hw/scsi/trace-events  |   43 +-
  5 files changed, 1191 insertions(+), 2052 deletions(-)

I'm likely not going to be the one doing detailed review here, so my opinion here is not necessarily 100% controlling, but from my perspective this is still significantly too large to review, both in raw size terms and because it has a lot of changes in it that do not all need or want to be in the same patch.

Just looking through the very early stuff to show some examples of changes that don't need to be in this patch:

Quoted 10 lines
hw/scsi/lasi_ncr710.c +4 -1
@@ -50,22 +48,22 @@ static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr,
      if (addr >= 0x100) {
          hwaddr ncr_addr = addr - 0x100;
          if (size == 1) {
+            /*
+             * Single byte access: flip the byte lane (PA-RISC big endian
+             * access -> the chip's little endian register order).
+             */
              ncr_addr ^= 3;
-            NCR710_DPRINTF("Reading value to LASI WRAPPER == 0x%lx%s, "

hw/scsi/lasi_ncr710.c static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr, · line 62

Quoted 10 lines
hw/scsi/lasi_ncr710.c +4 -3
-                           "val=0x%lx, size=%u\n",
-                           addr - 0x100, size == 1 ? " (XORed)" : "",
-                           val, size);
              val = ncr710_reg_read(&s->ncr710, ncr_addr, size);
          } else {
+            /*
+             * Multibyte access: gather little endian, no lane flip, so
+             * the 24/32 bit registers assemble correctly.
+             */
              val = 0;

These are comment improvements to existing code. They don't need to be in the big patch.

hw/scsi/lasi_ncr710.c static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr, · line 68

Quoted 8 lines
hw/scsi/lasi_ncr710.c +0 -4
              for (unsigned i = 0; i < size; i++) {
                  uint8_t byte_val = ncr710_reg_read(&s->ncr710, ncr_addr + i, 1);
                  val |= ((uint64_t)byte_val) << (i * 8);
-                NCR710_DPRINTF("  Read byte %u from NCR addr 0x%lx: "
-                               "0x%02x\n", i, ncr_addr + i, byte_val);
              }
-            NCR710_DPRINTF("  Reconstructed %u-byte value: 0x%lx\n",
-                           size, val);

Here we drop some debug printfs. That can be done separately.

Quoted 7 lines
hw/scsi/lasi_ncr710.c +0 -0
          }
 
          trace_lasi_ncr710_reg_forward_read(addr, val);
@@ -83,7 +81,13 @@ static void lasi_ncr710_reg_write(void *opaque, hwaddr addr,
 
      trace_lasi_ncr710_reg_write(addr, val, size);
 

hw/scsi/lasi_ncr710.c static void lasi_ncr710_reg_write(void *opaque, hwaddr addr, · line 92

Quoted 10 lines
hw/scsi/lasi_ncr710.c +7 -1
-    if (addr <= 0x0F) {
+    /*
+     * 0x00..0x0f is the read only LASI module header (ID/SVERSION/HVERSION) and
+     * 0x10..0xff is unmapped module space; writes to both are ignored.  The
+     * SCSI bus reset is driven through the chip's SCNTL1.RST at forwarded
+     * offset 0x101 (see ncr53c710.c), not through this window.
+     */
+    if (addr <= 0x0f) {
          return;
      }

This is a better explanatory comment. That's not something that needs to be in the big patch.

Quoted 8 lines
hw/scsi/lasi_ncr710.c +0 -3
@@ -92,16 +96,10 @@ static void lasi_ncr710_reg_write(void *opaque, hwaddr addr,
 
          if (size == 1) {
              ncr_addr ^= 3;
-            NCR710_DPRINTF("Writing value to LASI WRAPPER == 0x%lx%s, "
-                           "val=0x%lx, size=%u\n",
-                           addr - 0x100, size == 1 ? " (XORed)" : "",

hw/scsi/lasi_ncr710.c static void lasi_ncr710_reg_write(void *opaque, hwaddr addr, · line 105

Quoted 10 lines
hw/scsi/lasi_ncr710.c +0 -3
-                           val, size);
              ncr710_reg_write(&s->ncr710, ncr_addr, val, size);
          } else {
              for (unsigned i = 0; i < size; i++) {
                  uint8_t byte_val = (val >> (i * 8)) & 0xff;
-                 NCR710_DPRINTF("  Writing byte %u to NCR addr 0x%lx: 0x%02x\n",
-                       i, ncr_addr + i, byte_val);
                  ncr710_reg_write(&s->ncr710, ncr_addr + i, byte_val, 1);
              }
          }

Debug printfs being removed again.

hw/scsi/ncr53c710.c line 5

Quoted 11 lines
hw/scsi/ncr53c710.c +1 -3
index 57967e7a84..57cd7d91a4 100644
@@ -1,599 +1,308 @@
  /*
- * QEMU NCR710 SCSI Controller
+ * QEMU NCR 53C710 SCSI I/O Processor emulation
   *
- * Copyright (c) 2025 Soumyajyotii Ssarkar <[email protected]>
- * This driver was developed during the Google Summer of Code 2025 program.

Generally speaking you shouldn't drop somebody's Copyright line. Even if the code has been mostly rewritten, there's still some of Soumyajyotii's code here, I expect.

Quoted 46 lines
hw/scsi/ncr53c710.c +16 -23
+ * Copyright (c) 2026 Keith Monahan <[email protected]>
+ * Copyright (c) 2006 CodeSourcery (lsi53c895a.c, written by Paul Brook)
   *
- * NCR710 SCSI Controller implementation
- * Based on the NCR53C710 Technical Manual Version 3.2, December 2000
+ * A rewrite of the QEMU NCR 53C710 model.  The 53C710 is an early member of
+ * the NCR/LSI SCRIPTS processor family, so the SCRIPTS engine here is derived
+ * from QEMU's LSI53C895A model and adapted to the 53C710's register map,
+ * single byte interrupt model, big endian SCRIPTS fetch (PA-RISC) and 24 bit
+ * DMA, per the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide
+ * (Oct 1990).
   *
- * Developed from an implementation of NCR53C710 by Helge Deller
- * which was interim based on the implementation by Toni Wilen for UAE.
+ * Derived from hw/scsi/lsi53c895a.c (Copyright (c) 2006 CodeSourcery, written
+ * by Paul Brook).
   *
- * SPDX-License-Identifier: GPL-2.0-or-later
+ * Replaces the 53C710 model written for Google Summer of Code 2025 by
+ * Soumyajyotii Ssarkar <[email protected]>, itself based on
+ * earlier NCR 53C710 work by Helge Deller and by Toni Wilen (for UAE).
   *
- * Contents:
- * 1. Register Definitions
- * 2. Register name functions
- * 3. Parity functions
- * 4. SCSI FIFO Structures
- * 5. Scripts Misc functions
- * 6. DMA functions
- * 7. Scripts functions
- * 8. Read and Write functions
- * 9. QEMU Device model functions
+ * The only instantiated device is the LASI wrapper (lasi_ncr710.c), which
+ * embeds one NCR710State by value and drives it through this file.
   *
+ * SPDX-License-Identifier: GPL-2.0-or-later
   */
 
-/* SCNTL0 (0x00) - SCSI Control Register 0 */
-#define NCR710_SCNTL0_TRG    0x01
-#define NCR710_SCNTL0_AAP    0x02
-#define NCR710_SCNTL0_EPG    0x04
-#define NCR710_SCNTL0_EPC    0x08
-#define NCR710_SCNTL0_WATN   0x10
-#define NCR710_SCNTL0_START  0x20
-#define NCR710_SCNTL0_ARB0   0x40

hw/scsi/ncr53c710.c

Quoted 10 lines
hw/scsi/ncr53c710.c +7 -1
-#define NCR710_SCNTL0_ARB1   0x80
 
 
+/* SCNTL0 (0x00) */
+#define NCR710_SCNTL0_TRG       0x01
+#define NCR710_SCNTL0_AAP       0x02
+#define NCR710_SCNTL0_EPG       0x04
+#define NCR710_SCNTL0_EPC       0x08
+#define NCR710_SCNTL0_WATN      0x10
+#define NCR710_SCNTL0_START     0x20

These defines haven't actually changed, except that you've reformatted the whitespace. If you want to adjust the whitespace, that's a separate patch, but better still don't do that. Then all this stuff doesn't appear in this patch at all because it doesn't change.

Quoted 7 lines
hw/scsi/ncr53c710.c +7 -0
+static const char *const ncr710_reg_names[64] = {
+    "SCNTL0", "SCNTL1", "SDID", "SIEN", "SCID", "SXFER", "SODL", "SOCL",
+    "SFBR", "SIDL", "SBDL", "SBCL", "DSTAT", "SSTAT0", "SSTAT1", "SSTAT2",
+    "DSA0", "DSA1", "DSA2", "DSA3", "CTEST0", "CTEST1", "CTEST2", "CTEST3",
+    "CTEST4", "CTEST5", "CTEST6", "CTEST7", "TEMP0", "TEMP1", "TEMP2", "TEMP3",
+    "DFIFO", "ISTAT", "CTEST8", "LCRC", "DBC0", "DBC1", "DBC2", "DCMD",
+    "DNAD0", "DNAD1", "DNAD2", "DNAD3", "DSP0", "DSP1", "DSP2", "DSP3",

hw/scsi/ncr53c710.c line 164

Quoted 10 lines
hw/scsi/ncr53c710.c +7 -1
+    "DSPS0", "DSPS1", "DSPS2", "DSPS3", "SCRATCH0", "SCRATCH1", "SCRATCH2",
+    "SCRATCH3", "DMODE", "DIEN", "DWT", "DCNTL", "ADDER0", "ADDER1", "ADDER2",
+    "ADDER3",
+};
+
+static const char *ncr710_reg_name(int offset)
  {
-    return (s->sien0 & NCR710_SSTAT0_SEL) != 0;
+    return (offset >= 0 && offset < 64) ? ncr710_reg_names[offset] : "???";
  }

This is a rewrite of the old version of this function, to use an array of strings instead of a big switch. It is definitely nicer code, but this change can be done as its own patch.

Personally my experience is that it is much easier to structure big changes into smaller pieces to start with, rather than doing the big change first and then having to break it up into smaller pieces later. Doing things in smaller pieces makes it easier to isolate where bugs or regressions are, both during development and later if we need to git bisect once a change has landed.

thanks
-- PMM

#8 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Quoted 30 lines · Keith Monahan

Peter,

Thanks for going through it. I raised this originally in the v1 thread and I am not sure it reached you, so let me lay it out here.

The size is real, but the more useful thing to flag is that reading this as a diff against the old model will not give you an accurate view of the change, because these are not two versions of one model. They are two different implementations, and git is aligning text between them that never shared any code.

Three concrete ways they differ:

1. Different origin. The old model descends from Toni Wilen's UAE implementation by way of Helge Deller. The new engine is derived from QEMU's own lsi53c895a.c. They are unrelated codebases, so the delta is not one evolving into the other, it is one file removed and a different one added.

2. Different execution model. The old model runs SCRIPTS synchronously inside the guest's register write, so completion interrupts come back reentrantly. The new one runs the engine asynchronously and delivers completion the way the chip actually does. That is a different control
flow from top to bottom, not a local change.

3. Different connection model. The old model is connected only, with no target initiated disconnect or reselect. The new one implements disconnect and reselect and the state machine around them. That is a different device state machine, not a modified one.

Quoted 6 lines

Because of those three, the two files do not line up function to function or line to line. The diff is large and noisy precisely because git has almost nothing to match, so it renders a wholesale replacement as a wall of edits. You will not learn what actually changed by reading the delta; the way to review this is to read ncr53c710.c as a new file against the 53C710 manual, not as a patch on the old one.

In that case maybe another approach could be to have a patch that replaces old 710 model with a copy of lsi53c895a.c unchanged first (of course this would remove it from compile so it can still be bisected or only include this patch as interim for review that can then be squashed with next patch on merge) then have the patch that rewrites the lsi53c895a.c model to 710 which would be a more useful patch to look at to show what's really changed than the patch that's unreadable because it's diffing unrelated codebase?

Regards,
BALATON Zoltan

Quoted 281 lines

For what it is worth, I did try to get there incrementally first. It took about a dozen fixes to the old model just to get the chip's own driver to negotiate its first command, and it still would not boot reliably, because the two blockers, connected only and synchronous SCRIPTS, are structural and cannot be patched out in small local steps. That is why I stopped treating the old code as a base: it has no path to an HP-UX boot to build on.

I do appreciate that this is a large and complicated model, and that reviewing it will take real time; I am not trying to wave that away. But the incremental path would be worse on exactly that front. A series that converted the old model into this one would be a long chain of patches to write and a long chain to review, most of them touching code that is thrown away by the end. Reviewing one coherent new file, even a big one, is less work than reviewing however many steps it would take to get there.

You really can't get THERE from HERE, which is why I took this approach.

Thanks,
Keith

Quoted 256 lines · Peter Maydell
Quoted 27 lines · Keith Monahan

Replace the NCR 53C710 model with a SCRIPTS engine derived from QEMU's LSI53C895A model (hw/scsi/lsi53c895a.c), another device in the same NCR/LSI SCRIPTS family, adapted to the 53C710: its register map, the single-byte DSTAT/SSTAT0 interrupt model, big-endian SCRIPTS and table-indirect fetch (the part sits on the big-endian PA-RISC LASI bus), and 24-bit DMA byte counts. Behaviour follows the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide (Oct 1990).

The base engine supports connected I/O and reselection-interrupt disconnect, enough to install and boot Debian off the 710 with the Linux lasi700/53c700 driver and to boot NetBSD/hppa 9.4 and 10.1 (osiop) with disk and CD-ROM enumeration. Reselection follows the 53C700 family:
gated on the (re)selection interrupt enable (SIEN.SEL) alone, one-hot SDID, and an SFBR id bitmask in 700-compatibility mode. SDTR/WDTR/PPR negotiation is declined with MESSAGE REJECT, which the 700-family drivers require to proceed: unlike the 8xx parts they leave the target to drive the phase after the reject.

The LASI glue (hw/scsi/lasi_ncr710.c) embeds one NCR710State by value. It gains the free_request SCSIBusInfo callback and request-queue init the core needs, renames the reselection timer to scripts_timer, and narrows the SCSI bus to the 8-bit inclusive maxima.

The migration format is incompatible with the model this replaces, so the vmstate version and minimum version are both raised.

Quoted 8 lines
---
  hw/scsi/lasi_ncr710.c |  117 +-
  hw/scsi/lasi_ncr710.h |   12 +-
  hw/scsi/ncr53c710.c   | 2795 +++++++++++++++--------------------------
  hw/scsi/ncr53c710.h   |  276 ++--
  hw/scsi/trace-events  |   43 +-
  5 files changed, 1191 insertions(+), 2052 deletions(-)

I'm likely not going to be the one doing detailed review here, so my opinion here is not necessarily 100% controlling, but from my perspective this is still significantly too large to review, both in raw size terms and because it has a lot of changes in it that do not all need or want to be in the same patch.

Just looking through the very early stuff to show some examples of changes that don't need to be in this patch:

Quoted 11 lines
hw/scsi/lasi_ncr710.c +4 -1
@@ -50,22 +48,22 @@ static uint64_t lasi_ncr710_reg_read(void *opaque, 
 hwaddr addr,
      if (addr >= 0x100) {
          hwaddr ncr_addr = addr - 0x100;
          if (size == 1) {
+            /*
+             * Single byte access: flip the byte lane (PA-RISC big endian
+             * access -> the chip's little endian register order).
+             */
              ncr_addr ^= 3;
-            NCR710_DPRINTF("Reading value to LASI WRAPPER == 0x%lx%s, "

hw/scsi/lasi_ncr710.c static uint64_t lasi_ncr710_reg_read(void *opaque, · line 63

Quoted 10 lines
hw/scsi/lasi_ncr710.c +4 -3
-                           "val=0x%lx, size=%u\n",
-                           addr - 0x100, size == 1 ? " (XORed)" : "",
-                           val, size);
              val = ncr710_reg_read(&s->ncr710, ncr_addr, size);
          } else {
+            /*
+             * Multibyte access: gather little endian, no lane flip, so
+             * the 24/32 bit registers assemble correctly.
+             */
              val = 0;

These are comment improvements to existing code. They don't need to be in the big patch.

hw/scsi/lasi_ncr710.c static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr, · line 68

Quoted 9 lines
hw/scsi/lasi_ncr710.c +1 -4
              for (unsigned i = 0; i < size; i++) {
                  uint8_t byte_val = ncr710_reg_read(&s->ncr710, ncr_addr 
+ i, 1);
                  val |= ((uint64_t)byte_val) << (i * 8);
-                NCR710_DPRINTF("  Read byte %u from NCR addr 0x%lx: "
-                               "0x%02x\n", i, ncr_addr + i, byte_val);
              }
-            NCR710_DPRINTF("  Reconstructed %u-byte value: 0x%lx\n",
-                           size, val);

Here we drop some debug printfs. That can be done separately.

Quoted 16 lines
hw/scsi/lasi_ncr710.c +2 -1
          }
 
          trace_lasi_ncr710_reg_forward_read(addr, val);
@@ -83,7 +81,13 @@ static void lasi_ncr710_reg_write(void *opaque, hwaddr 
 addr,
 
      trace_lasi_ncr710_reg_write(addr, val, size);
 
-    if (addr <= 0x0F) {
+    /*
+     * 0x00..0x0f is the read only LASI module header 

(ID/SVERSION/HVERSION) and
+ * 0x10..0xff is unmapped module space; writes to both are ignored.
The
+ * SCSI bus reset is driven through the chip's SCNTL1.RST at
forwarded

hw/scsi/lasi_ncr710.c static void lasi_ncr710_reg_write(void *opaque, hwaddr addr, · line 92

Quoted 5 lines
hw/scsi/lasi_ncr710.c +3 -0
+     * offset 0x101 (see ncr53c710.c), not through this window.
+     */
+    if (addr <= 0x0f) {
          return;
      }

This is a better explanatory comment. That's not something that needs to be in the big patch.

Quoted 18 lines
hw/scsi/lasi_ncr710.c +0 -6
@@ -92,16 +96,10 @@ static void lasi_ncr710_reg_write(void *opaque, hwaddr 
 addr,
 
          if (size == 1) {
              ncr_addr ^= 3;
-            NCR710_DPRINTF("Writing value to LASI WRAPPER == 0x%lx%s, "
-                           "val=0x%lx, size=%u\n",
-                           addr - 0x100, size == 1 ? " (XORed)" : "",
-                           val, size);
              ncr710_reg_write(&s->ncr710, ncr_addr, val, size);
          } else {
              for (unsigned i = 0; i < size; i++) {
                  uint8_t byte_val = (val >> (i * 8)) & 0xff;
-                 NCR710_DPRINTF("  Writing byte %u to NCR addr 0x%lx: 
 0x%02x\n",
-                       i, ncr_addr + i, byte_val);
                  ncr710_reg_write(&s->ncr710, ncr_addr + i, byte_val, 1);
Quoted 2 lines
              }
          }

Debug printfs being removed again.

Quoted 10 lines
hw/scsi/ncr53c710.c +1 -2
index 57967e7a84..57cd7d91a4 100644
@@ -1,599 +1,308 @@
  /*
- * QEMU NCR710 SCSI Controller
+ * QEMU NCR 53C710 SCSI I/O Processor emulation
   *
- * Copyright (c) 2025 Soumyajyotii Ssarkar 
Quoted 3 lines

<[email protected]>
- * This driver was developed during the Google Summer of Code 2025 program.

Generally speaking you shouldn't drop somebody's Copyright line. Even if the code has been mostly rewritten, there's still some of Soumyajyotii's code here, I expect.

Quoted 51 lines

+ * Copyright (c) 2026 Keith Monahan <[email protected]>
+ * Copyright (c) 2006 CodeSourcery (lsi53c895a.c, written by Paul Brook) *
- * NCR710 SCSI Controller implementation
- * Based on the NCR53C710 Technical Manual Version 3.2, December 2000
+ * A rewrite of the QEMU NCR 53C710 model. The 53C710 is an early member of
+ * the NCR/LSI SCRIPTS processor family, so the SCRIPTS engine here is derived
+ * from QEMU's LSI53C895A model and adapted to the 53C710's register map,
+ * single byte interrupt model, big endian SCRIPTS fetch (PA-RISC) and 24 bit
+ * DMA, per the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide
+ * (Oct 1990).
*
- * Developed from an implementation of NCR53C710 by Helge Deller
- * which was interim based on the implementation by Toni Wilen for UAE.
+ * Derived from hw/scsi/lsi53c895a.c (Copyright (c) 2006 CodeSourcery, written
+ * by Paul Brook).
*
- * SPDX-License-Identifier: GPL-2.0-or-later
+ * Replaces the 53C710 model written for Google Summer of Code 2025 by
+ * Soumyajyotii Ssarkar <[email protected]>, itself based on
+ * earlier NCR 53C710 work by Helge Deller and by Toni Wilen (for UAE). *
- * Contents:
- * 1. Register Definitions
- * 2. Register name functions
- * 3. Parity functions
- * 4. SCSI FIFO Structures
- * 5. Scripts Misc functions
- * 6. DMA functions
- * 7. Scripts functions
- * 8. Read and Write functions
- * 9. QEMU Device model functions
+ * The only instantiated device is the LASI wrapper (lasi_ncr710.c), which
+ * embeds one NCR710State by value and drives it through this file. *
+ * SPDX-License-Identifier: GPL-2.0-or-later
*/

hw/scsi/ncr53c710.c +0 -8
-/* SCNTL0 (0x00) - SCSI Control Register 0 */
-#define NCR710_SCNTL0_TRG    0x01
-#define NCR710_SCNTL0_AAP    0x02
-#define NCR710_SCNTL0_EPG    0x04
-#define NCR710_SCNTL0_EPC    0x08
-#define NCR710_SCNTL0_WATN   0x10
-#define NCR710_SCNTL0_START  0x20
-#define NCR710_SCNTL0_ARB0   0x40

hw/scsi/ncr53c710.c

Quoted 10 lines
hw/scsi/ncr53c710.c +7 -1
-#define NCR710_SCNTL0_ARB1   0x80
 
 
+/* SCNTL0 (0x00) */
+#define NCR710_SCNTL0_TRG       0x01
+#define NCR710_SCNTL0_AAP       0x02
+#define NCR710_SCNTL0_EPG       0x04
+#define NCR710_SCNTL0_EPC       0x08
+#define NCR710_SCNTL0_WATN      0x10
+#define NCR710_SCNTL0_START     0x20

These defines haven't actually changed, except that you've reformatted the whitespace. If you want to adjust the whitespace, that's a separate patch, but better still don't do that. Then all this stuff doesn't appear in this patch at all because it doesn't change.

Quoted 21 lines
hw/scsi/ncr53c710.c +3 -0
+static const char *const ncr710_reg_names[64] = {
+    "SCNTL0", "SCNTL1", "SDID", "SIEN", "SCID", "SXFER", "SODL", "SOCL",
+    "SFBR", "SIDL", "SBDL", "SBCL", "DSTAT", "SSTAT0", "SSTAT1", 

"SSTAT2",
+ "DSA0", "DSA1", "DSA2", "DSA3", "CTEST0", "CTEST1", "CTEST2",
"CTEST3",
+ "CTEST4", "CTEST5", "CTEST6", "CTEST7", "TEMP0", "TEMP1", "TEMP2",
"TEMP3",

hw/scsi/ncr53c710.c +3 -0
+    "DFIFO", "ISTAT", "CTEST8", "LCRC", "DBC0", "DBC1", "DBC2", "DCMD",
+    "DNAD0", "DNAD1", "DNAD2", "DNAD3", "DSP0", "DSP1", "DSP2", "DSP3",
+    "DSPS0", "DSPS1", "DSPS2", "DSPS3", "SCRATCH0", "SCRATCH1", 

"SCRATCH2",
+ "SCRATCH3", "DMODE", "DIEN", "DWT", "DCNTL", "ADDER0", "ADDER1",
"ADDER2",

hw/scsi/ncr53c710.c +5 -1
+    "ADDER3",
+};
+
+static const char *ncr710_reg_name(int offset)
  {
-    return (s->sien0 & NCR710_SSTAT0_SEL) != 0;
+    return (offset >= 0 && offset < 64) ? ncr710_reg_names[offset] : 
Quoted 2 lines

"???";
}

This is a rewrite of the old version of this function, to use an array of strings instead of a big switch. It is definitely nicer code, but this change can be done as its own patch.

Personally my experience is that it is much easier to structure big changes into smaller pieces to start with, rather than doing the big change first and then having to break it up into smaller pieces later. Doing things in smaller pieces makes it easier to isolate where bugs or regressions are, both during development and later if we need to git bisect once a change has landed.

thanks
-- PMM

#9 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

In reply to Keith Monahan
Quoted 11 lines · Keith Monahan

Peter,

Thanks for going through it. I raised this originally in the v1 thread and I am not sure it reached you, so let me lay it out here.

The size is real, but the more useful thing to flag is that reading this as a diff against the old model will not give you an accurate view of the change, because these are not two versions of one model. They are two different implementations, and git is aligning text between them that never shared any code.

And yet, when I look at the diff I see clear things that are just individual fixes or changes to the existing code. Yes, there's a big change to the core, but it would be much easier to see what that core change is if it was not all thrown in with all the other parts too. (Just to check I had another scroll through and there really is a ton of stuff in there which is changes like "variable name changed from 'operator' to 'xoperator' or "the constant names in all the case statements lost their _REG suffix".)

Incidentally, even for "here is a completely new device model" I would recommend structuring it as multiple patches, not a single "here is the device" patch, when the device is this complex.

-- PMM

#10 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Peter,

As you know, I did not start with a rewrite. I started exactly where you are asking me to end up. I spent a large amount of effort trying to morph the existing model into something correct one reviewable change at a time: identifying, debugging, remediating, and reapplying fixes for something on the order of a dozen distinct bugs, testing after each round. That path still did not converge. Only after that did I pivot to rebuilding from scratch, and that was its own effort: tracing, debugging, and running hundreds of test cases before it actually worked.

So reconstructing an incremental series now means manufacturing the exact path I already tried and abandoned, except the intermediate commits are throwaway by construction. They exist only to satisfy review, not to reach a solution, since the working solution already exists as the final commit.

I am investigating the feasibility of creating a series of incremental patches to morph the original 710 model into the new one, but I do not yet know how cleanly that will map onto the current result, since the structure is now quite different and some parts may not have a useful intermediate form.

The original 710 model likely borrowed from the 895 model, as did I when I rebuilt it. So you are right: some of what I claim to be new code in the diff could very well be the same 895 pattern the original author already used as well. This may indeed reduce how much is genuinely new, and I can look for other places where the same thing is true.

There is also a real tradeoff in how the series can be sliced, and I think it comes down to a choice I need you to make. I can aim for smaller steps that each build but do not necessarily boot the guest, or I can aim for steps that keep the model bootable at each stage, which forces large chunks of the central core to land together as one atomic commit. I do not think I can give you both at once.

Which do you want? If every commit has to boot, some changes simply cannot be separated, because the model likely only becomes functional once the central control flow change lands.

To be clear, choosing option 2 means the central patch is going to be large!

Thanks,
Keith

Quoted 29 lines · Peter Maydell
Quoted 11 lines · Keith Monahan

Peter,

Thanks for going through it. I raised this originally in the v1 thread and I am not sure it reached you, so let me lay it out here.

The size is real, but the more useful thing to flag is that reading this as a diff against the old model will not give you an accurate view of the change, because these are not two versions of one model. They are two different implementations, and git is aligning text between them that never shared any code.

And yet, when I look at the diff I see clear things that are just individual fixes or changes to the existing code. Yes, there's a big change to the core, but it would be much easier to see what that core change is if it was not all thrown in with all the other parts too. (Just to check I had another scroll through and there really is a ton of stuff in there which is changes like "variable name changed from 'operator' to 'xoperator' or "the constant names in all the case statements lost their _REG suffix".)

Incidentally, even for "here is a completely new device model" I would recommend structuring it as multiple patches, not a single "here is the device" patch, when the device is this complex.

-- PMM

#11 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Can I get some eyes on this approach please?

Here's my proposal to break up this large rewrite in reviewable sized chunks:

1. lasi_ncr710: remove dead and shadowing defines
2. lasi_ncr710: compute hw identifiers instead of caching them
3. ncr53c710: remove the redundant standalone SysBus device
4. ncr53c710: prepare state, register names and tracepoints
5. ncr53c710: rewrite the status and message phase handlers
6. ncr53c710: rewrite reselection, queueing and the data completion path
7. ncr53c710: rewrite the register read path
8. ncr53c710: rewrite the register write path and soft reset
9. ncr53c710: rewrite the SCRIPTS execution engine
10. ncr53c710: complete cached-backend transfers asynchronously
11. ncr53c710: support HP-UX overlapped tagged commands
12. ncr53c710: extend MESSAGE REJECT handling for HP-UX

To be clear, I will be modifying ncr53c710.c in place. And, of course, modifying the other files as necessary. Those changes are minimal.

Every patch will buildable. There is a non-booting window between patches 5-8 as the new helpers must land before the new loop that matches them, so the device compiles but can't drive I/O until they reconverge.

How does this sound?

Keith

Quoted 49 lines · Keith Monahan

Peter,

As you know, I did not start with a rewrite. I started exactly where you are asking me to end up. I spent a large amount of effort trying to morph the existing model into something correct one reviewable change at a time: identifying, debugging, remediating, and reapplying fixes for something on the order of a dozen distinct bugs, testing after each round. That path still did not converge. Only after that did I pivot to rebuilding from scratch, and that was its own effort: tracing, debugging, and running hundreds of test cases before it actually worked.

So reconstructing an incremental series now means manufacturing the exact path I already tried and abandoned, except the intermediate commits are throwaway by construction. They exist only to satisfy review, not to reach a solution, since the working solution already exists as the final commit.

I am investigating the feasibility of creating a series of incremental patches to morph the original 710 model into the new one, but I do not yet know how cleanly that will map onto the current result, since the structure is now quite different and some parts may not have a useful intermediate form.

The original 710 model likely borrowed from the 895 model, as did I when I rebuilt it. So you are right: some of what I claim to be new code in the diff could very well be the same 895 pattern the original author already used as well. This may indeed reduce how much is genuinely new, and I can look for other places where the same thing is true.

There is also a real tradeoff in how the series can be sliced, and I think it comes down to a choice I need you to make. I can aim for smaller steps that each build but do not necessarily boot the guest, or I can aim for steps that keep the model bootable at each stage, which forces large chunks of the central core to land together as one atomic commit. I do not think I can give you both at once.

Which do you want? If every commit has to boot, some changes simply cannot be separated, because the model likely only becomes functional once the central control flow change lands.

To be clear, choosing option 2 means the central patch is going to be large!

Thanks,
Keith

Quoted 29 lines · Peter Maydell
Quoted 11 lines · Keith Monahan

Peter,

Thanks for going through it. I raised this originally in the v1 thread and I am not sure it reached you, so let me lay it out here.

The size is real, but the more useful thing to flag is that reading this as a diff against the old model will not give you an accurate view of the change, because these are not two versions of one model. They are two different implementations, and git is aligning text between them that never shared any code.

And yet, when I look at the diff I see clear things that are just individual fixes or changes to the existing code. Yes, there's a big change to the core, but it would be much easier to see what that core change is if it was not all thrown in with all the other parts too. (Just to check I had another scroll through and there really is a ton of stuff in there which is changes like "variable name changed from 'operator' to 'xoperator' or "the constant names in all the case statements lost their _REG suffix".)

Incidentally, even for "here is a completely new device model" I would recommend structuring it as multiple patches, not a single "here is the device" patch, when the device is this complex.

-- PMM

#12 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Quoted 17 lines · Keith Monahan

Can I get some eyes on this approach please?

Here's my proposal to break up this large rewrite in reviewable sized chunks:

1. lasi_ncr710: remove dead and shadowing defines
2. lasi_ncr710: compute hw identifiers instead of caching them
3. ncr53c710: remove the redundant standalone SysBus device
4. ncr53c710: prepare state, register names and tracepoints
5. ncr53c710: rewrite the status and message phase handlers
6. ncr53c710: rewrite reselection, queueing and the data completion path
7. ncr53c710: rewrite the register read path
8. ncr53c710: rewrite the register write path and soft reset
9. ncr53c710: rewrite the SCRIPTS execution engine
10. ncr53c710: complete cached-backend transfers asynchronously
11. ncr53c710: support HP-UX overlapped tagged commands
12. ncr53c710: extend MESSAGE REJECT handling for HP-UX

Quoted 7 lines

To be clear, I will be modifying ncr53c710.c in place. And, of course, modifying the other files as necessary. Those changes are minimal.

Every patch will buildable. There is a non-booting window between patches 5-8 as the new helpers must land before the new loop that matches them, so the device compiles but can't drive I/O until they reconverge.

How does this sound?

Keith

Hi Keith,

Thank you for your contribution to QEMU. I found myself in a similar situation a couple of years back when I ended up reworking a large part of the ESP SCSI emulation to fix some fairly obscure edge cases booting Classic MacOS, so I understand how difficult a task this can be.

If you would like to take a look at my series and cover letter then you can find it at https://patchew.org/QEMU/[email protected]/, but I've summarised some thoughts below based upon my experience:

- I focused on using a small set of reference images and made sure they would boot upon every commit. This was really helpful as sometimes even a subtle code movement would cause something to break unexpectedly, and helped me to find extra edge cases I had missed.

- Keep changes to individual devices in separate commits where possible, and also the same goes for refactoring/renaming commits and changes to the device logic/functionality. Often these commits can be merged earlier than later patches in the series where the reviewer may have more questions.

- Not all reviewers are familiar with SCSI, so writing a good cover letter explaining each part of the series will help reviewers understand the changes during review.

- Run your local tests in both a normal and --enable-debug build, and also with asan (--enable-asan) to catch any memory errors

- Adding a functional test if possible would help prevent future regressions.

Similarly for reviewers I should note that the LSI SCRIPTS engine is quite complex: in effect it is almost like a separate processor executing its own programs (SCRIPTS) to perform I/O.

As I remarked in my ESP series cover letter, it is almost impossible for someone to perform a comprehensive review without being very familiar with the controller and its datasheet, so when attempting to review the series, I would probably focus on looking how it compares with the existing lsi53c895a code which is mature and working well. For me this would mean that adding notes explaining any unusual differences between the two devices on the commit message would be extremely helpful during review.

You mentioned above the problem with implementing new helpers causing boot to break between patches 5 and 8: is it possible to bring in the new logic/helpers a phase at a time (like I did with ESP) to help keep things working? Perhaps a temporary lock to enable the async helpers to work in a sync manner temporarily could also help?

ATB,

Mark.

#13 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Mark,

Thanks for engaging me on this rewrite.

I built an automated harness for each OS reference disk. It mounts the disk as a CD-ROM, creates a blank HDD, and uses pyte and regex to drive installation, reboot, and login.

Multiple HP-UX versions, Debian Linux, and NetBSD 9.4/10.1 all boot, install, reboot, and run successfully. That provides strong high-level confidence, though it does not rule out bugs.

I have run the tests hundreds of times, both sequentially and concurrently, to expose timing or race issues. The full suite takes about an hour.

I do not plan to distribute the images or tests because of licensing concerns and required preprocessing.

Thanks for mentioning those debug flags. I'm not using them now.

It wasn't lost on me that I wasn't including any relevant executable tests. When you say functional tests, are you talking specifically /tests/functional/hppa? So similar to test_cdboot.py, changing machine type to -M 715, downloadable asset/image? This does bring up where we point this to? Like is archive.org acceptable reference? Whether it's my harness or the other, we still have a few challenges.

Finally, I overstated the not bootable case, and this should be not be problematic. Linux/Debian and Netbsd will work at ANY patch level, but HP-UX (which to be clear doesn't work at all on the released existing 710 model right now) will only start functioning once everything is in place.

Thanks,
Keith

Quoted 45 lines · Mark Cave-Ayland

Hi Keith,

Thank you for your contribution to QEMU. I found myself in a similar situation a couple of years back when I ended up reworking a large part of the ESP SCSI emulation to fix some fairly obscure edge cases booting Classic MacOS, so I understand how difficult a task this can be.

If you would like to take a look at my series and cover letter then you can find it at https://patchew.org/QEMU/20240112125420.514425-1- [email protected]/, but I've summarised some thoughts below based upon my experience:

  - I focused on using a small set of reference images and made sure
they would
    boot upon every commit. This was really helpful as sometimes even a
subtle code
    movement would cause something to break unexpectedly, and helped me
to find
    extra edge cases I had missed.

  - Keep changes to individual devices in separate commits where
possible, and also
    the same goes for refactoring/renaming commits and changes to the
device
    logic/functionality. Often these commits can be merged earlier than
later
    patches in the series where the reviewer may have more questions.

  - Not all reviewers are familiar with SCSI, so writing a good cover
letter
    explaining each part of the series will help reviewers understand
the changes
    during review.

  - Run your local tests in both a normal and --enable-debug build, and
also with
    asan (--enable-asan) to catch any memory errors

  - Adding a functional test if possible would help prevent future
regressions.

Similarly for reviewers I should note that the LSI SCRIPTS engine is quite complex: in effect it is almost like a separate processor executing its own programs (SCRIPTS) to perform I/O.

As I remarked in my ESP series cover letter, it is almost impossible for someone to perform a comprehensive review without being very familiar with the controller and its datasheet, so when attempting to review the series, I would probably focus on looking how it compares with the existing lsi53c895a code which is mature and working well. For me this would mean that adding notes explaining any unusual differences between the two devices on the commit message would be extremely helpful during review.

You mentioned above the problem with implementing new helpers causing boot to break between patches 5 and 8: is it possible to bring in the new logic/helpers a phase at a time (like I did with ESP) to help keep things working? Perhaps a temporary lock to enable the async helpers to work in a sync manner temporarily could also help?

ATB,

Mark.

#14 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

In reply to Keith Monahan
Quoted 34 lines · Keith Monahan

Replace the NCR 53C710 model with a SCRIPTS engine derived from QEMU's LSI53C895A model (hw/scsi/lsi53c895a.c), another device in the same NCR/LSI SCRIPTS family, adapted to the 53C710: its register map, the single-byte DSTAT/SSTAT0 interrupt model, big-endian SCRIPTS and table-indirect fetch (the part sits on the big-endian PA-RISC LASI bus), and 24-bit DMA byte counts. Behaviour follows the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide (Oct 1990).

The base engine supports connected I/O and reselection-interrupt disconnect, enough to install and boot Debian off the 710 with the Linux lasi700/53c700 driver and to boot NetBSD/hppa 9.4 and 10.1 (osiop) with disk and CD-ROM enumeration. Reselection follows the 53C700 family:
gated on the (re)selection interrupt enable (SIEN.SEL) alone, one-hot SDID, and an SFBR id bitmask in 700-compatibility mode. SDTR/WDTR/PPR negotiation is declined with MESSAGE REJECT, which the 700-family drivers require to proceed: unlike the 8xx parts they leave the target to drive the phase after the reject.

The LASI glue (hw/scsi/lasi_ncr710.c) embeds one NCR710State by value. It gains the free_request SCSIBusInfo callback and request-queue init the core needs, renames the reselection timer to scripts_timer, and narrows the SCSI bus to the 8-bit inclusive maxima.

The migration format is incompatible with the model this replaces, so the vmstate version and minimum version are both raised.

---

hw/scsi/lasi_ncr710.c |  117 +-
hw/scsi/lasi_ncr710.h |   12 +-
hw/scsi/ncr53c710.c   | 2795 +++++++++++++++--------------------------
hw/scsi/ncr53c710.h   |  276 ++--
hw/scsi/trace-events  |   43 +-
5 files changed, 1191 insertions(+), 2052 deletions(-)

[...]

Quoted 438 lines
hw/scsi/ncr53c710.c +122 -296
index 57967e7a84..57cd7d91a4 100644
@@ -1,599 +1,308 @@
 /*
- * QEMU NCR710 SCSI Controller
+ * QEMU NCR 53C710 SCSI I/O Processor emulation
 *
- * Copyright (c) 2025 Soumyajyotii Ssarkar <[email protected]>
- * This driver was developed during the Google Summer of Code 2025 program.
+ * Copyright (c) 2026 Keith Monahan <[email protected]>
+ * Copyright (c) 2006 CodeSourcery (lsi53c895a.c, written by Paul Brook)
 *
- * NCR710 SCSI Controller implementation
- * Based on the NCR53C710 Technical Manual Version 3.2, December 2000
+ * A rewrite of the QEMU NCR 53C710 model.  The 53C710 is an early member of
+ * the NCR/LSI SCRIPTS processor family, so the SCRIPTS engine here is derived
+ * from QEMU's LSI53C895A model and adapted to the 53C710's register map,
+ * single byte interrupt model, big endian SCRIPTS fetch (PA-RISC) and 24 bit
+ * DMA, per the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide
+ * (Oct 1990).
 *
- * Developed from an implementation of NCR53C710 by Helge Deller
- * which was interim based on the implementation by Toni Wilen for UAE.
+ * Derived from hw/scsi/lsi53c895a.c (Copyright (c) 2006 CodeSourcery, written
+ * by Paul Brook).
 *
- * SPDX-License-Identifier: GPL-2.0-or-later
+ * Replaces the 53C710 model written for Google Summer of Code 2025 by
+ * Soumyajyotii Ssarkar <[email protected]>, itself based on
+ * earlier NCR 53C710 work by Helge Deller and by Toni Wilen (for UAE).
 *
- * Contents:
- * 1. Register Definitions
- * 2. Register name functions
- * 3. Parity functions
- * 4. SCSI FIFO Structures
- * 5. Scripts Misc functions
- * 6. DMA functions
- * 7. Scripts functions
- * 8. Read and Write functions
- * 9. QEMU Device model functions
+ * The only instantiated device is the LASI wrapper (lasi_ncr710.c), which
+ * embeds one NCR710State by value and drives it through this file.
 *
+ * SPDX-License-Identifier: GPL-2.0-or-later
 */
 
 #include "qemu/osdep.h"
-#include "qapi/error.h"
-#include "qemu/timer.h"
+#include "qemu/bitops.h"
+#include "qemu/log.h"
+#include "qemu/main-loop.h"
 #include "hw/core/irq.h"
-#include "hw/core/sysbus.h"
-#include "hw/scsi/scsi.h"
 #include "hw/scsi/ncr53c710.h"
-#include "migration/vmstate.h"
-#include "system/dma.h"
-#include "qemu/log.h"
-#include "qemu/module.h"
+#include "system/block-backend.h"
 #include "trace.h"
-#include "qom/object.h"
-
-#define NCR710_MAX_DEVS 7
-
-/* SCNTL0 (0x00) - SCSI Control Register 0 */
-#define NCR710_SCNTL0_TRG    0x01
-#define NCR710_SCNTL0_AAP    0x02
-#define NCR710_SCNTL0_EPG    0x04
-#define NCR710_SCNTL0_EPC    0x08
-#define NCR710_SCNTL0_WATN   0x10
-#define NCR710_SCNTL0_START  0x20
-#define NCR710_SCNTL0_ARB0   0x40
-#define NCR710_SCNTL0_ARB1   0x80
-
-/* SCNTL1 (0x01) - SCSI Control Register 1 */
-#define NCR710_SCNTL1_RES0   0x01
-#define NCR710_SCNTL1_RES1   0x02
-#define NCR710_SCNTL1_AESP   0x04
-#define NCR710_SCNTL1_RST    0x08
-#define NCR710_SCNTL1_CON    0x10
-#define NCR710_SCNTL1_ESR    0x20
-#define NCR710_SCNTL1_ADB    0x40
-#define NCR710_SCNTL1_EXC    0x80
-
-/* ISTAT (0x21) - Interrupt Status Register */
-#define NCR710_ISTAT_DIP    0x01
-#define NCR710_ISTAT_SIP    0x02
-#define NCR710_ISTAT_CON    0x08
-#define NCR710_ISTAT_SIGP   0x20
-#define NCR710_ISTAT_RST    0x40
-#define NCR710_ISTAT_ABRT   0x80
-
-/* SSTAT0 (0x0D) - SCSI Status Register 0 */
-#define NCR710_SSTAT0_PAR    0x01
-#define NCR710_SSTAT0_RST    0x02
-#define NCR710_SSTAT0_UDC    0x04
-#define NCR710_SSTAT0_SGE    0x08
-#define NCR710_SSTAT0_SEL    0x10
-#define NCR710_SSTAT0_STO    0x20
-#define NCR710_SSTAT0_FCMP   0x40
-#define NCR710_SSTAT0_MA     0x80
-
-/* SSTAT1 (0x0E) - SCSI Status Register 1 */
-#define NCR710_SSTAT1_ORF    0x02
-#define NCR710_SSTAT1_ILF    0x04
-
-/* SSTAT2 (0x0F) - SCSI Status Register 2 */
-#define NCR710_SSTAT2_FF0    0x01
-#define NCR710_SSTAT2_FF1    0x02
-#define NCR710_SSTAT2_FF2    0x04
-#define NCR710_SSTAT2_FF3    0x08
-
-/* SOCL (0x07) / SBCL (0x0B) - SCSI Output/Bus Control Lines */
-#define NCR710_SOCL_IO       0x01
-#define NCR710_SOCL_CD       0x02
-#define NCR710_SOCL_MSG      0x04
-#define NCR710_SOCL_ATN      0x08
-#define NCR710_SOCL_SEL      0x10
-#define NCR710_SOCL_BSY      0x20
-#define NCR710_SOCL_ACK      0x40
-#define NCR710_SOCL_REQ      0x80
-
-/* SBCL bits same as SOCL */
-#define NCR710_SBCL_IO       0x01
-#define NCR710_SBCL_CD       0x02
-#define NCR710_SBCL_MSG      0x04
-#define NCR710_SBCL_ATN      0x08
-#define NCR710_SBCL_SEL      0x10
-#define NCR710_SBCL_BSY      0x20
-#define NCR710_SBCL_ACK      0x40
-#define NCR710_SBCL_REQ      0x80
-
-/* DSTAT (0x0C) - DMA Status Register */
-#define NCR710_DSTAT_IID     0x01
-#define NCR710_DSTAT_SIR     0x04
-#define NCR710_DSTAT_SSI     0x08
-#define NCR710_DSTAT_ABRT    0x10
-#define NCR710_DSTAT_BF      0x20
-#define NCR710_DSTAT_MDPE    0x40
-#define NCR710_DSTAT_DFE     0x80
-
-/* DCNTL (0x3B) - DMA Control Register */
-#define NCR710_DCNTL_COM     0x01
-#define NCR710_DCNTL_IRQD    0x02
-#define NCR710_DCNTL_STD     0x04
-#define NCR710_DCNTL_IRQM    0x08
-#define NCR710_DCNTL_SSM     0x10
-#define NCR710_DCNTL_PFEN    0x20
-#define NCR710_DCNTL_PFF     0x40
-
-/* DMODE (0x38) - DMA Mode Register */
-#define NCR710_DMODE_MAN     0x01
-#define NCR710_DMODE_BOF     0x02
-#define NCR710_DMODE_ERMP    0x04
-#define NCR710_DMODE_ERL     0x08
-#define NCR710_DMODE_DIOM    0x10
-#define NCR710_DMODE_SIOM    0x20
-#define NCR710_DMODE_BL_MASK 0xC0
-#define NCR710_DMODE_BL_1    0x00
-#define NCR710_DMODE_BL_2    0x40
-#define NCR710_DMODE_BL_4    0x80
-#define NCR710_DMODE_BL_8    0xC0
-
-/* CTEST2 (0x16) - Chip Test Register 2 */
-#define NCR710_CTEST2_DACK   0x01
-#define NCR710_CTEST2_DREQ   0x02
-#define NCR710_CTEST2_TEOP   0x04
-#define NCR710_CTEST2_PCICIE 0x08
-#define NCR710_CTEST2_CM     0x10
-#define NCR710_CTEST2_CIO    0x20
-#define NCR710_CTEST2_SIGP   0x40
-#define NCR710_CTEST2_DDIR   0x80
-
-/* CTEST5 (0x19) - Chip Test Register 5 */
-#define NCR710_CTEST5_BL2    0x04
-#define NCR710_CTEST5_DDIR   0x08
-#define NCR710_CTEST5_MASR   0x10
-#define NCR710_CTEST5_DFSN   0x20
-#define NCR710_CTEST5_BBCK   0x40
-#define NCR710_CTEST5_ADCK   0x80
-
-/* SCID (0x04) - SCSI Chip ID Register */
-#define NCR710_SCID_RRE      0x60
-#define NCR710_SCID_ID_MASK  0x07
-
-#define NCR710_HOST_ID       7
-
-/* NCR53C710 has 8-byte SCSI FIFO */
-#define NCR710_MAX_MSGIN_LEN 8
-#define NCR710_BUF_SIZE         4096
-
-/* Standard SCSI Message Byte Constants */
-#define SCSI_MSG_ABORT                      0x06
-#define SCSI_MSG_BUS_DEVICE_RESET           0x0c
-#define SCSI_MSG_COMMAND_COMPLETE           0x00
-#define SCSI_MSG_DISCONNECT                 0x04
-#define SCSI_MSG_EXTENDED_MESSAGE           0x01
-#define SCSI_MSG_IDENTIFY                   0x80
-#define SCSI_MSG_IGNORE_WIDE_RESIDUE        0x23
-#define SCSI_MSG_MESSAGE_PARITY_ERROR       0x09
-#define SCSI_MSG_MESSAGE_REJECT             0x07
-#define SCSI_MSG_NO_OPERATION               0x08
-#define SCSI_MSG_RELEASE_RECOVERY           0x10
-#define SCSI_MSG_RESTORE_POINTERS           0x03
-#define SCSI_MSG_SAVE_DATA_POINTER          0x02
-#define SCSI_MSG_SYNCHRONOUS_DATA_TRANSFER  0x01
-#define SCSI_MSG_WIDE_DATA_TRANSFER         0x03
-
-/* Script interrupt codes */
-#define A_GOOD_STATUS_AFTER_STATUS          0x401
-#define A_DISCONNECT_AFTER_CMD              0x380
-#define A_DISCONNECT_AFTER_DATA             0x580
-#define A_DISCONNECT_DURING_DATA            0x780
-#define A_RESELECTION_IDENTIFIED            0x1003
-#define A_UNEXPECTED_PHASE                  0x20
-#define A_FATAL                             0x2000
-#define A_DEBUG_INTERRUPT                   0x3000
-
-/* SCSI Script execution states */
-#define SCRIPT_STATE_IDLE                  0
-#define SCRIPT_STATE_SELECTING             1
-#define SCRIPT_STATE_COMMAND               2
-#define SCRIPT_STATE_DATA                  3
-#define SCRIPT_STATE_STATUS                4
-#define SCRIPT_STATE_MESSAGE               5
-#define SCRIPT_STATE_DISCONNECTED          6
-
-#define AFTER_SELECTION         0x100
-#define BEFORE_CMD              0x200
-#define AFTER_CMD               0x300
-#define AFTER_STATUS            0x400
-#define AFTER_DATA_IN           0x500
-#define AFTER_DATA_OUT          0x600
-#define DURING_DATA_IN          0x700
-
-#define NOT_MSG_OUT             0x10
-#define UNEXPECTED_PHASE        0x20
-#define NOT_MSG_IN              0x30
-#define UNEXPECTED_MSG          0x40
-#define MSG_IN                  0x50
-#define SDTR_MSG_R              0x60
-#define REJECT_MSG_R            0x70
-#define DISCONNECT              0x80
-#define MSG_OUT                 0x90
-#define WDTR_MSG_R              0xA0
-
-#define GOOD_STATUS             0x1
-
-#define NOT_MSG_OUT_AFTER_SELECTION         0x110
-#define UNEXPECTED_PHASE_BEFORE_CMD         0x220
-#define UNEXPECTED_PHASE_AFTER_CMD          0x320
-#define NOT_MSG_IN_AFTER_STATUS             0x430
-#define GOOD_STATUS_AFTER_STATUS            0x401
-#define UNEXPECTED_PHASE_AFTER_DATA_IN      0x520
-#define UNEXPECTED_PHASE_AFTER_DATA_OUT     0x620
-#define UNEXPECTED_MSG_BEFORE_CMD           0x240
-#define MSG_IN_BEFORE_CMD                   0x250
-#define MSG_IN_AFTER_CMD                    0x350
-#define SDTR_MSG_BEFORE_CMD                 0x260
-#define REJECT_MSG_BEFORE_CMD               0x270
-#define DISCONNECT_AFTER_CMD                0x380
-#define SDTR_MSG_AFTER_CMD                  0x360
-#define WDTR_MSG_AFTER_CMD                  0x3A0
-#define MSG_IN_AFTER_STATUS                 0x440
-#define DISCONNECT_AFTER_DATA               0x580
-#define MSG_IN_AFTER_DATA_IN                0x550
-#define MSG_IN_AFTER_DATA_OUT               0x650
-#define MSG_OUT_AFTER_DATA_IN               0x590
-#define DATA_IN_AFTER_DATA_IN               0x5a0
-#define MSG_IN_DURING_DATA_IN               0x750
-#define DISCONNECT_DURING_DATA              0x780
-
-#define RESELECTED_DURING_SELECTION      0x1000
-#define COMPLETED_SELECTION_AS_TARGET    0x1001
-#define RESELECTION_IDENTIFIED           0x1003
-
-#define FATAL                            0x2000
-#define FATAL_UNEXPECTED_RESELECTION_MSG 0x2000
-#define FATAL_SEND_MSG                   0x2001
-#define FATAL_NOT_MSG_IN_AFTER_SELECTION 0x2002
-#define FATAL_ILLEGAL_MSG_LENGTH         0x2003
-
-#define DEBUG_INTERRUPT     0x3000
-#define DEBUG_INTERRUPT1    0x3001
-#define DEBUG_INTERRUPT2    0x3002
-#define DEBUG_INTERRUPT3    0x3003
-#define DEBUG_INTERRUPT4    0x3004
-#define DEBUG_INTERRUPT5    0x3005
-#define DEBUG_INTERRUPT6    0x3006
-
-#define COMMAND_COMPLETE_MSG      0x00
-#define EXTENDED_MSG              0x01
-#define SDTR_MSG                  0x01
-#define SAVE_DATA_PTRS_MSG        0x02
-#define RESTORE_DATA_PTRS_MSG     0x03
-#define WDTR_MSG                  0x03
-#define DISCONNECT_MSG            0x04
-#define REJECT_MSG                0x07
-#define PARITY_ERROR_MSG          0x09
-#define SIMPLE_TAG_MSG            0x20
-#define IDENTIFY_MSG              0x80
-#define IDENTIFY_MSG_MASK         0x7F
-#define TWO_BYTE_MSG              0x20
-#define TWO_BYTE_MSG_MASK         0x0F
-
-/* SCSI phases */
-#define PHASE_DO   0  /* Data out phase */
-#define PHASE_DI   1  /* Data in phase */
-#define PHASE_CO   2  /* Command phase */
-#define PHASE_SI   3  /* Status phase */
-#define PHASE_ST   3  /* Status phase (alias) */
-#define PHASE_MO   6  /* Message out phase */
-#define PHASE_MI   7  /* Message in phase */
-#define PHASE_MASK 7  /* Mask for phase bits */
-
-#define NCR710_TAG_VALID     (1 << 16)
-
-static void ncr710_scsi_fifo_init(NCR710_SCSI_FIFO *fifo);
-static const char *ncr710_reg_name(int offset);
-static void ncr710_script_scsi_interrupt(NCR710State *s, int stat0);
-static void ncr710_update_irq(NCR710State *s);
-static void ncr710_script_dma_interrupt(NCR710State *s, int stat);
-static void ncr710_request_free(NCR710State *s, NCR710Request *p);
-static inline void ncr710_dma_read(NCR710State *s, uint32_t addr,
-                                   void *buf, uint32_t len);
-static inline void ncr710_dma_write(NCR710State *s, uint32_t addr,
-                                    const void *buf, uint32_t len);
-static uint8_t ncr710_reg_readb(NCR710State *s, int offset);
-static void ncr710_reg_writeb(NCR710State *s, int offset, uint8_t val);
 
+/* SCNTL0 (0x00) */
+#define NCR710_SCNTL0_TRG       0x01
+#define NCR710_SCNTL0_AAP       0x02
+#define NCR710_SCNTL0_EPG       0x04
+#define NCR710_SCNTL0_EPC       0x08
+#define NCR710_SCNTL0_WATN      0x10
+#define NCR710_SCNTL0_START     0x20
+/* ARB1|ARB0 = 0xc0 (full arbitration) at reset */
+
+/* SCNTL1 (0x01) */
+#define NCR710_SCNTL1_AESP      0x04
+#define NCR710_SCNTL1_RST       0x08
+#define NCR710_SCNTL1_CON       0x10
+#define NCR710_SCNTL1_ESR       0x20
+#define NCR710_SCNTL1_ADB       0x40
+#define NCR710_SCNTL1_EXC       0x80
+
+/* SSTAT0 (0x0d) and SIEN (0x03) share this bit layout. */
+#define NCR710_STAT0_PAR        0x01
+#define NCR710_STAT0_RST        0x02
+#define NCR710_STAT0_UDC        0x04
+#define NCR710_STAT0_SGE        0x08
+#define NCR710_STAT0_SEL        0x10
+#define NCR710_STAT0_STO        0x20
+#define NCR710_STAT0_FCMP       0x40
+#define NCR710_STAT0_MA         0x80
+
+/* DSTAT (0x0c) and DIEN (0x39) share this bit layout. */
+#define NCR710_DSTAT_IID        0x01
+#define NCR710_DSTAT_WTD        0x02
+#define NCR710_DSTAT_SIR        0x04
+#define NCR710_DSTAT_SSI        0x08
+#define NCR710_DSTAT_ABRT       0x10
+#define NCR710_DSTAT_BF         0x20
+#define NCR710_DSTAT_DFE        0x80   /* pure status; never raises an IRQ */
+
+/* ISTAT (0x21) */
+#define NCR710_ISTAT_DIP        0x01
+#define NCR710_ISTAT_SIP        0x02
+#define NCR710_ISTAT_CON        0x08
+#define NCR710_ISTAT_SIGP       0x20
+#define NCR710_ISTAT_RST        0x40
+#define NCR710_ISTAT_ABRT       0x80
+
+/* SBCL (0x0b): low 3 bits mirror the SCSI phase, upper bits are bus signals. */
+#define NCR710_SBCL_ATN         0x08
+#define NCR710_SBCL_SEL         0x10
+#define NCR710_SBCL_BSY         0x20
+#define NCR710_SBCL_ACK         0x40
+#define NCR710_SBCL_REQ         0x80
+
+/* SOCL (0x07) */
+#define NCR710_SOCL_ATN         0x08
+
+/* CTEST2 (0x16) */
+#define NCR710_CTEST2_DACK      0x01
+#define NCR710_CTEST2_SIGP      0x40
+
+/* DMODE (0x38) */
+#define NCR710_DMODE_MAN        0x01
+
+/* DCNTL (0x3b) */
+#define NCR710_DCNTL_COM        0x01
+#define NCR710_DCNTL_FA         0x02
+#define NCR710_DCNTL_STD        0x04
+#define NCR710_DCNTL_LLM        0x08
+#define NCR710_DCNTL_SSM        0x10
+#define NCR710_DCNTL_EA         0x20
+
+/* SCSI phase (MSG,C/D,I/O); matches SSTAT2[2:0]/SBCL[2:0]/DCMD[26:24]. */
+#define PHASE_DO        0
+#define PHASE_DI        1
+#define PHASE_CMD       2
+#define PHASE_ST        3
+#define PHASE_MO        6
+#define PHASE_MI        7
+#define PHASE_MASK      7
+
+/* Flag bit OR'd into select_tag/tag when a queue tag is valid. */
+#define NCR710_TAG_VALID    (1 << 16)
+
+/* Maximum SCRIPTS instructions to process before yielding to the CPU. */
+#define NCR710_MAX_INSN     500
+
+#define NCR710_BUF_SIZE     4096
+
+/* waiting state machine (mirrors lsi53c895a). */
+enum {
+    NCR710_NOWAIT = 0,        /* SCRIPTS running or stopped */
+    NCR710_WAIT_RESELECT,     /* Wait Reselect instruction issued */
+    NCR710_DMA_SCRIPTS,       /* DMA invoked from within execute_script */
+    NCR710_DMA_IN_PROGRESS,   /* asynchronous DMA in progress */
+    NCR710_WAIT_SCRIPTS,      /* stopped on the instruction count limit */
+};
 
-static inline int ncr710_irq_on_rsl(NCR710State *s)
+enum {
+    NCR710_MSG_ACTION_COMMAND = 0,
+    NCR710_MSG_ACTION_DISCONNECT = 1,
+    NCR710_MSG_ACTION_DOUT = 2,
+    NCR710_MSG_ACTION_DIN = 3,
+};
+

hw/scsi/ncr53c710.c line 147

Quoted 10 lines
hw/scsi/ncr53c710.c +10 -0
+struct NCR710Request {
+    SCSIRequest *req;
+    uint32_t tag;
+    uint32_t dma_len;
+    uint8_t *dma_buf;
+    uint32_t pending;     /* bytes the SCSI layer has ready while queued */
+    int out;              /* nonzero if the queued transfer is DATA OUT */
+    bool orphan;
+    QTAILQ_ENTRY(NCR710Request) next;
+};

This struct is exactly the same as lsi_request in lsi53c895a.c. As a start this and helper functions working on it should be split off in a shared file and header from lsi53c895a.c and reused here. That needs changing lsi53c895a.c but it's trivial changes easy to review and does not break it. That with more small chnages split off as suggested by Peter should reduce the size of this patch to a more managable size so we can see what else could be shared.

Regards,
BALATON Zoltan

#15 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Quoted 485 lines · BALATON Zoltan
Quoted 34 lines · Keith Monahan

Replace the NCR 53C710 model with a SCRIPTS engine derived from QEMU's LSI53C895A model (hw/scsi/lsi53c895a.c), another device in the same NCR/LSI SCRIPTS family, adapted to the 53C710: its register map, the single-byte DSTAT/SSTAT0 interrupt model, big-endian SCRIPTS and table-indirect fetch (the part sits on the big-endian PA-RISC LASI bus), and 24-bit DMA byte counts.  Behaviour follows the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide (Oct 1990).

The base engine supports connected I/O and reselection-interrupt disconnect, enough to install and boot Debian off the 710 with the Linux lasi700/53c700 driver and to boot NetBSD/hppa 9.4 and 10.1 (osiop) with disk and CD-ROM enumeration.  Reselection follows the 53C700 family:
gated on the (re)selection interrupt enable (SIEN.SEL) alone, one-hot SDID, and an SFBR id bitmask in 700-compatibility mode.  SDTR/WDTR/PPR negotiation is declined with MESSAGE REJECT, which the 700-family drivers require to proceed: unlike the 8xx parts they leave the target to drive the phase after the reject.

The LASI glue (hw/scsi/lasi_ncr710.c) embeds one NCR710State by value. It gains the free_request SCSIBusInfo callback and request-queue init the core needs, renames the reselection timer to scripts_timer, and narrows the SCSI bus to the 8-bit inclusive maxima.

The migration format is incompatible with the model this replaces, so the vmstate version and minimum version are both raised.

---
hw/scsi/lasi_ncr710.c |  117 +-
hw/scsi/lasi_ncr710.h |   12 +-
hw/scsi/ncr53c710.c   | 2795 +++++++++++++++-------------------------- hw/scsi/ncr53c710.h   |  276 ++--
hw/scsi/trace-events  |   43 +-

5 files changed, 1191 insertions(+), 2052 deletions(-)

[...]

Quoted 438 lines
hw/scsi/ncr53c710.c +122 -296
index 57967e7a84..57cd7d91a4 100644
@@ -1,599 +1,308 @@
 /*
- * QEMU NCR710 SCSI Controller
+ * QEMU NCR 53C710 SCSI I/O Processor emulation
  *
- * Copyright (c) 2025 Soumyajyotii Ssarkar <[email protected]>
- * This driver was developed during the Google Summer of Code 2025 program.
+ * Copyright (c) 2026 Keith Monahan <[email protected]>
+ * Copyright (c) 2006 CodeSourcery (lsi53c895a.c, written by Paul Brook)
  *
- * NCR710 SCSI Controller implementation
- * Based on the NCR53C710 Technical Manual Version 3.2, December 2000
+ * A rewrite of the QEMU NCR 53C710 model.  The 53C710 is an early member of
+ * the NCR/LSI SCRIPTS processor family, so the SCRIPTS engine here is derived
+ * from QEMU's LSI53C895A model and adapted to the 53C710's register map,
+ * single byte interrupt model, big endian SCRIPTS fetch (PA-RISC) and 24 bit
+ * DMA, per the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide
+ * (Oct 1990).
  *
- * Developed from an implementation of NCR53C710 by Helge Deller
- * which was interim based on the implementation by Toni Wilen for UAE.
+ * Derived from hw/scsi/lsi53c895a.c (Copyright (c) 2006 CodeSourcery, written
+ * by Paul Brook).
  *
- * SPDX-License-Identifier: GPL-2.0-or-later
+ * Replaces the 53C710 model written for Google Summer of Code 2025 by
+ * Soumyajyotii Ssarkar <[email protected]>, itself based on
+ * earlier NCR 53C710 work by Helge Deller and by Toni Wilen (for UAE).
  *
- * Contents:
- * 1. Register Definitions
- * 2. Register name functions
- * 3. Parity functions
- * 4. SCSI FIFO Structures
- * 5. Scripts Misc functions
- * 6. DMA functions
- * 7. Scripts functions
- * 8. Read and Write functions
- * 9. QEMU Device model functions
+ * The only instantiated device is the LASI wrapper (lasi_ncr710.c), which
+ * embeds one NCR710State by value and drives it through this file.
  *
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include "qemu/osdep.h"
-#include "qapi/error.h"
-#include "qemu/timer.h"
+#include "qemu/bitops.h"
+#include "qemu/log.h"
+#include "qemu/main-loop.h"
 #include "hw/core/irq.h"
-#include "hw/core/sysbus.h"
-#include "hw/scsi/scsi.h"
 #include "hw/scsi/ncr53c710.h"
-#include "migration/vmstate.h"
-#include "system/dma.h"
-#include "qemu/log.h"
-#include "qemu/module.h"
+#include "system/block-backend.h"
 #include "trace.h"
-#include "qom/object.h"
-
-#define NCR710_MAX_DEVS 7
-
-/* SCNTL0 (0x00) - SCSI Control Register 0 */
-#define NCR710_SCNTL0_TRG    0x01
-#define NCR710_SCNTL0_AAP    0x02
-#define NCR710_SCNTL0_EPG    0x04
-#define NCR710_SCNTL0_EPC    0x08
-#define NCR710_SCNTL0_WATN   0x10
-#define NCR710_SCNTL0_START  0x20
-#define NCR710_SCNTL0_ARB0   0x40
-#define NCR710_SCNTL0_ARB1   0x80
-
-/* SCNTL1 (0x01) - SCSI Control Register 1 */
-#define NCR710_SCNTL1_RES0   0x01
-#define NCR710_SCNTL1_RES1   0x02
-#define NCR710_SCNTL1_AESP   0x04
-#define NCR710_SCNTL1_RST    0x08
-#define NCR710_SCNTL1_CON    0x10
-#define NCR710_SCNTL1_ESR    0x20
-#define NCR710_SCNTL1_ADB    0x40
-#define NCR710_SCNTL1_EXC    0x80
-
-/* ISTAT (0x21) - Interrupt Status Register */
-#define NCR710_ISTAT_DIP    0x01
-#define NCR710_ISTAT_SIP    0x02
-#define NCR710_ISTAT_CON    0x08
-#define NCR710_ISTAT_SIGP   0x20
-#define NCR710_ISTAT_RST    0x40
-#define NCR710_ISTAT_ABRT   0x80
-
-/* SSTAT0 (0x0D) - SCSI Status Register 0 */
-#define NCR710_SSTAT0_PAR    0x01
-#define NCR710_SSTAT0_RST    0x02
-#define NCR710_SSTAT0_UDC    0x04
-#define NCR710_SSTAT0_SGE    0x08
-#define NCR710_SSTAT0_SEL    0x10
-#define NCR710_SSTAT0_STO    0x20
-#define NCR710_SSTAT0_FCMP   0x40
-#define NCR710_SSTAT0_MA     0x80
-
-/* SSTAT1 (0x0E) - SCSI Status Register 1 */
-#define NCR710_SSTAT1_ORF    0x02
-#define NCR710_SSTAT1_ILF    0x04
-
-/* SSTAT2 (0x0F) - SCSI Status Register 2 */
-#define NCR710_SSTAT2_FF0    0x01
-#define NCR710_SSTAT2_FF1    0x02
-#define NCR710_SSTAT2_FF2    0x04
-#define NCR710_SSTAT2_FF3    0x08
-
-/* SOCL (0x07) / SBCL (0x0B) - SCSI Output/Bus Control Lines */
-#define NCR710_SOCL_IO       0x01
-#define NCR710_SOCL_CD       0x02
-#define NCR710_SOCL_MSG      0x04
-#define NCR710_SOCL_ATN      0x08
-#define NCR710_SOCL_SEL      0x10
-#define NCR710_SOCL_BSY      0x20
-#define NCR710_SOCL_ACK      0x40
-#define NCR710_SOCL_REQ      0x80
-
-/* SBCL bits same as SOCL */
-#define NCR710_SBCL_IO       0x01
-#define NCR710_SBCL_CD       0x02
-#define NCR710_SBCL_MSG      0x04
-#define NCR710_SBCL_ATN      0x08
-#define NCR710_SBCL_SEL      0x10
-#define NCR710_SBCL_BSY      0x20
-#define NCR710_SBCL_ACK      0x40
-#define NCR710_SBCL_REQ      0x80
-
-/* DSTAT (0x0C) - DMA Status Register */
-#define NCR710_DSTAT_IID     0x01
-#define NCR710_DSTAT_SIR     0x04
-#define NCR710_DSTAT_SSI     0x08
-#define NCR710_DSTAT_ABRT    0x10
-#define NCR710_DSTAT_BF      0x20
-#define NCR710_DSTAT_MDPE    0x40
-#define NCR710_DSTAT_DFE     0x80
-
-/* DCNTL (0x3B) - DMA Control Register */
-#define NCR710_DCNTL_COM     0x01
-#define NCR710_DCNTL_IRQD    0x02
-#define NCR710_DCNTL_STD     0x04
-#define NCR710_DCNTL_IRQM    0x08
-#define NCR710_DCNTL_SSM     0x10
-#define NCR710_DCNTL_PFEN    0x20
-#define NCR710_DCNTL_PFF     0x40
-
-/* DMODE (0x38) - DMA Mode Register */
-#define NCR710_DMODE_MAN     0x01
-#define NCR710_DMODE_BOF     0x02
-#define NCR710_DMODE_ERMP    0x04
-#define NCR710_DMODE_ERL     0x08
-#define NCR710_DMODE_DIOM    0x10
-#define NCR710_DMODE_SIOM    0x20
-#define NCR710_DMODE_BL_MASK 0xC0
-#define NCR710_DMODE_BL_1    0x00
-#define NCR710_DMODE_BL_2    0x40
-#define NCR710_DMODE_BL_4    0x80
-#define NCR710_DMODE_BL_8    0xC0
-
-/* CTEST2 (0x16) - Chip Test Register 2 */
-#define NCR710_CTEST2_DACK   0x01
-#define NCR710_CTEST2_DREQ   0x02
-#define NCR710_CTEST2_TEOP   0x04
-#define NCR710_CTEST2_PCICIE 0x08
-#define NCR710_CTEST2_CM     0x10
-#define NCR710_CTEST2_CIO    0x20
-#define NCR710_CTEST2_SIGP   0x40
-#define NCR710_CTEST2_DDIR   0x80
-
-/* CTEST5 (0x19) - Chip Test Register 5 */
-#define NCR710_CTEST5_BL2    0x04
-#define NCR710_CTEST5_DDIR   0x08
-#define NCR710_CTEST5_MASR   0x10
-#define NCR710_CTEST5_DFSN   0x20
-#define NCR710_CTEST5_BBCK   0x40
-#define NCR710_CTEST5_ADCK   0x80
-
-/* SCID (0x04) - SCSI Chip ID Register */
-#define NCR710_SCID_RRE      0x60
-#define NCR710_SCID_ID_MASK  0x07
-
-#define NCR710_HOST_ID       7
-
-/* NCR53C710 has 8-byte SCSI FIFO */
-#define NCR710_MAX_MSGIN_LEN 8
-#define NCR710_BUF_SIZE         4096
-
-/* Standard SCSI Message Byte Constants */
-#define SCSI_MSG_ABORT                      0x06
-#define SCSI_MSG_BUS_DEVICE_RESET           0x0c
-#define SCSI_MSG_COMMAND_COMPLETE           0x00
-#define SCSI_MSG_DISCONNECT                 0x04
-#define SCSI_MSG_EXTENDED_MESSAGE           0x01
-#define SCSI_MSG_IDENTIFY                   0x80
-#define SCSI_MSG_IGNORE_WIDE_RESIDUE        0x23
-#define SCSI_MSG_MESSAGE_PARITY_ERROR       0x09
-#define SCSI_MSG_MESSAGE_REJECT             0x07
-#define SCSI_MSG_NO_OPERATION               0x08
-#define SCSI_MSG_RELEASE_RECOVERY           0x10
-#define SCSI_MSG_RESTORE_POINTERS           0x03
-#define SCSI_MSG_SAVE_DATA_POINTER          0x02
-#define SCSI_MSG_SYNCHRONOUS_DATA_TRANSFER  0x01
-#define SCSI_MSG_WIDE_DATA_TRANSFER         0x03
-
-/* Script interrupt codes */
-#define A_GOOD_STATUS_AFTER_STATUS          0x401
-#define A_DISCONNECT_AFTER_CMD              0x380
-#define A_DISCONNECT_AFTER_DATA             0x580
-#define A_DISCONNECT_DURING_DATA            0x780
-#define A_RESELECTION_IDENTIFIED            0x1003
-#define A_UNEXPECTED_PHASE                  0x20
-#define A_FATAL                             0x2000
-#define A_DEBUG_INTERRUPT                   0x3000
-
-/* SCSI Script execution states */
-#define SCRIPT_STATE_IDLE                  0
-#define SCRIPT_STATE_SELECTING             1
-#define SCRIPT_STATE_COMMAND               2
-#define SCRIPT_STATE_DATA                  3
-#define SCRIPT_STATE_STATUS                4
-#define SCRIPT_STATE_MESSAGE               5
-#define SCRIPT_STATE_DISCONNECTED          6
-
-#define AFTER_SELECTION         0x100
-#define BEFORE_CMD              0x200
-#define AFTER_CMD               0x300
-#define AFTER_STATUS            0x400
-#define AFTER_DATA_IN           0x500
-#define AFTER_DATA_OUT          0x600
-#define DURING_DATA_IN          0x700
-
-#define NOT_MSG_OUT             0x10
-#define UNEXPECTED_PHASE        0x20
-#define NOT_MSG_IN              0x30
-#define UNEXPECTED_MSG          0x40
-#define MSG_IN                  0x50
-#define SDTR_MSG_R              0x60
-#define REJECT_MSG_R            0x70
-#define DISCONNECT              0x80
-#define MSG_OUT                 0x90
-#define WDTR_MSG_R              0xA0
-
-#define GOOD_STATUS             0x1
-
-#define NOT_MSG_OUT_AFTER_SELECTION         0x110
-#define UNEXPECTED_PHASE_BEFORE_CMD         0x220
-#define UNEXPECTED_PHASE_AFTER_CMD          0x320
-#define NOT_MSG_IN_AFTER_STATUS             0x430
-#define GOOD_STATUS_AFTER_STATUS            0x401
-#define UNEXPECTED_PHASE_AFTER_DATA_IN      0x520
-#define UNEXPECTED_PHASE_AFTER_DATA_OUT     0x620
-#define UNEXPECTED_MSG_BEFORE_CMD           0x240
-#define MSG_IN_BEFORE_CMD                   0x250
-#define MSG_IN_AFTER_CMD                    0x350
-#define SDTR_MSG_BEFORE_CMD                 0x260
-#define REJECT_MSG_BEFORE_CMD               0x270
-#define DISCONNECT_AFTER_CMD                0x380
-#define SDTR_MSG_AFTER_CMD                  0x360
-#define WDTR_MSG_AFTER_CMD                  0x3A0
-#define MSG_IN_AFTER_STATUS                 0x440
-#define DISCONNECT_AFTER_DATA               0x580
-#define MSG_IN_AFTER_DATA_IN                0x550
-#define MSG_IN_AFTER_DATA_OUT               0x650
-#define MSG_OUT_AFTER_DATA_IN               0x590
-#define DATA_IN_AFTER_DATA_IN               0x5a0
-#define MSG_IN_DURING_DATA_IN               0x750
-#define DISCONNECT_DURING_DATA              0x780
-
-#define RESELECTED_DURING_SELECTION      0x1000
-#define COMPLETED_SELECTION_AS_TARGET    0x1001
-#define RESELECTION_IDENTIFIED           0x1003
-
-#define FATAL                            0x2000
-#define FATAL_UNEXPECTED_RESELECTION_MSG 0x2000
-#define FATAL_SEND_MSG                   0x2001
-#define FATAL_NOT_MSG_IN_AFTER_SELECTION 0x2002
-#define FATAL_ILLEGAL_MSG_LENGTH         0x2003
-
-#define DEBUG_INTERRUPT     0x3000
-#define DEBUG_INTERRUPT1    0x3001
-#define DEBUG_INTERRUPT2    0x3002
-#define DEBUG_INTERRUPT3    0x3003
-#define DEBUG_INTERRUPT4    0x3004
-#define DEBUG_INTERRUPT5    0x3005
-#define DEBUG_INTERRUPT6    0x3006
-
-#define COMMAND_COMPLETE_MSG      0x00
-#define EXTENDED_MSG              0x01
-#define SDTR_MSG                  0x01
-#define SAVE_DATA_PTRS_MSG        0x02
-#define RESTORE_DATA_PTRS_MSG     0x03
-#define WDTR_MSG                  0x03
-#define DISCONNECT_MSG            0x04
-#define REJECT_MSG                0x07
-#define PARITY_ERROR_MSG          0x09
-#define SIMPLE_TAG_MSG            0x20
-#define IDENTIFY_MSG              0x80
-#define IDENTIFY_MSG_MASK         0x7F
-#define TWO_BYTE_MSG              0x20
-#define TWO_BYTE_MSG_MASK         0x0F
-
-/* SCSI phases */
-#define PHASE_DO   0  /* Data out phase */
-#define PHASE_DI   1  /* Data in phase */
-#define PHASE_CO   2  /* Command phase */
-#define PHASE_SI   3  /* Status phase */
-#define PHASE_ST   3  /* Status phase (alias) */
-#define PHASE_MO   6  /* Message out phase */
-#define PHASE_MI   7  /* Message in phase */
-#define PHASE_MASK 7  /* Mask for phase bits */
-
-#define NCR710_TAG_VALID     (1 << 16)
-
-static void ncr710_scsi_fifo_init(NCR710_SCSI_FIFO *fifo);
-static const char *ncr710_reg_name(int offset);
-static void ncr710_script_scsi_interrupt(NCR710State *s, int stat0);
-static void ncr710_update_irq(NCR710State *s);
-static void ncr710_script_dma_interrupt(NCR710State *s, int stat);
-static void ncr710_request_free(NCR710State *s, NCR710Request *p);
-static inline void ncr710_dma_read(NCR710State *s, uint32_t addr,
-                                   void *buf, uint32_t len);
-static inline void ncr710_dma_write(NCR710State *s, uint32_t addr,
-                                    const void *buf, uint32_t len);
-static uint8_t ncr710_reg_readb(NCR710State *s, int offset);
-static void ncr710_reg_writeb(NCR710State *s, int offset, uint8_t val);
 
+/* SCNTL0 (0x00) */
+#define NCR710_SCNTL0_TRG       0x01
+#define NCR710_SCNTL0_AAP       0x02
+#define NCR710_SCNTL0_EPG       0x04
+#define NCR710_SCNTL0_EPC       0x08
+#define NCR710_SCNTL0_WATN      0x10
+#define NCR710_SCNTL0_START     0x20
+/* ARB1|ARB0 = 0xc0 (full arbitration) at reset */
+
+/* SCNTL1 (0x01) */
+#define NCR710_SCNTL1_AESP      0x04
+#define NCR710_SCNTL1_RST       0x08
+#define NCR710_SCNTL1_CON       0x10
+#define NCR710_SCNTL1_ESR       0x20
+#define NCR710_SCNTL1_ADB       0x40
+#define NCR710_SCNTL1_EXC       0x80
+
+/* SSTAT0 (0x0d) and SIEN (0x03) share this bit layout. */
+#define NCR710_STAT0_PAR        0x01
+#define NCR710_STAT0_RST        0x02
+#define NCR710_STAT0_UDC        0x04
+#define NCR710_STAT0_SGE        0x08
+#define NCR710_STAT0_SEL        0x10
+#define NCR710_STAT0_STO        0x20
+#define NCR710_STAT0_FCMP       0x40
+#define NCR710_STAT0_MA         0x80
+
+/* DSTAT (0x0c) and DIEN (0x39) share this bit layout. */
+#define NCR710_DSTAT_IID        0x01
+#define NCR710_DSTAT_WTD        0x02
+#define NCR710_DSTAT_SIR        0x04
+#define NCR710_DSTAT_SSI        0x08
+#define NCR710_DSTAT_ABRT       0x10
+#define NCR710_DSTAT_BF         0x20
+#define NCR710_DSTAT_DFE        0x80   /* pure status; never raises an IRQ */
+
+/* ISTAT (0x21) */
+#define NCR710_ISTAT_DIP        0x01
+#define NCR710_ISTAT_SIP        0x02
+#define NCR710_ISTAT_CON        0x08
+#define NCR710_ISTAT_SIGP       0x20
+#define NCR710_ISTAT_RST        0x40
+#define NCR710_ISTAT_ABRT       0x80
+
+/* SBCL (0x0b): low 3 bits mirror the SCSI phase, upper bits are bus signals. */
+#define NCR710_SBCL_ATN         0x08
+#define NCR710_SBCL_SEL         0x10
+#define NCR710_SBCL_BSY         0x20
+#define NCR710_SBCL_ACK         0x40
+#define NCR710_SBCL_REQ         0x80
+
+/* SOCL (0x07) */
+#define NCR710_SOCL_ATN         0x08
+
+/* CTEST2 (0x16) */
+#define NCR710_CTEST2_DACK      0x01
+#define NCR710_CTEST2_SIGP      0x40
+
+/* DMODE (0x38) */
+#define NCR710_DMODE_MAN        0x01
+
+/* DCNTL (0x3b) */
+#define NCR710_DCNTL_COM        0x01
+#define NCR710_DCNTL_FA         0x02
+#define NCR710_DCNTL_STD        0x04
+#define NCR710_DCNTL_LLM        0x08
+#define NCR710_DCNTL_SSM        0x10
+#define NCR710_DCNTL_EA         0x20
+
+/* SCSI phase (MSG,C/D,I/O); matches SSTAT2[2:0]/SBCL[2:0]/DCMD[26:24]. */
+#define PHASE_DO        0
+#define PHASE_DI        1
+#define PHASE_CMD       2
+#define PHASE_ST        3
+#define PHASE_MO        6
+#define PHASE_MI        7
+#define PHASE_MASK      7
+
+/* Flag bit OR'd into select_tag/tag when a queue tag is valid. */
+#define NCR710_TAG_VALID    (1 << 16)
+
+/* Maximum SCRIPTS instructions to process before yielding to the CPU. */
+#define NCR710_MAX_INSN     500
+
+#define NCR710_BUF_SIZE     4096
+
+/* waiting state machine (mirrors lsi53c895a). */
+enum {
+    NCR710_NOWAIT = 0,        /* SCRIPTS running or stopped */
+    NCR710_WAIT_RESELECT,     /* Wait Reselect instruction issued */
+    NCR710_DMA_SCRIPTS,       /* DMA invoked from within execute_script */
+    NCR710_DMA_IN_PROGRESS,   /* asynchronous DMA in progress */
+    NCR710_WAIT_SCRIPTS,      /* stopped on the instruction count limit */
+};
 
-static inline int ncr710_irq_on_rsl(NCR710State *s)
+enum {
+    NCR710_MSG_ACTION_COMMAND = 0,
+    NCR710_MSG_ACTION_DISCONNECT = 1,
+    NCR710_MSG_ACTION_DOUT = 2,
+    NCR710_MSG_ACTION_DIN = 3,
+};
+

hw/scsi/ncr53c710.c line 147

Quoted 10 lines
hw/scsi/ncr53c710.c +10 -0
+struct NCR710Request {
+    SCSIRequest *req;
+    uint32_t tag;
+    uint32_t dma_len;
+    uint8_t *dma_buf;
+    uint32_t pending;     /* bytes the SCSI layer has ready while queued */
+    int out;              /* nonzero if the queued transfer is DATA OUT */
+    bool orphan;
+    QTAILQ_ENTRY(NCR710Request) next;
+};
Quoted 7 lines

This struct is exactly the same as lsi_request in lsi53c895a.c. As a start this and helper functions working on it should be split off in a shared file and header from lsi53c895a.c and reused here. That needs changing lsi53c895a.c but it's trivial changes easy to review and does not break it. That with more small chnages split off as suggested by Peter should reduce the size of this patch to a more managable size so we can see what else could be shared.

I fully understand and generally support the wish to merge and share things in the hope for easier review and possible maintanance reduction.

But even in the Linux kernel those drivers don't share code. Will we really gain much if we try to split the existing working drivers and try share code between those?
I mean, the 710 and 89x chips are quite different, and so will be the code.

In this specific case IMHO it's better to keep them seperate.

Helge

#16 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Quoted 508 lines · Helge Deller
Quoted 499 lines · BALATON Zoltan
Quoted 34 lines · Keith Monahan

Replace the NCR 53C710 model with a SCRIPTS engine derived from QEMU's LSI53C895A model (hw/scsi/lsi53c895a.c), another device in the same NCR/LSI SCRIPTS family, adapted to the 53C710: its register map, the single-byte DSTAT/SSTAT0 interrupt model, big-endian SCRIPTS and table-indirect fetch (the part sits on the big-endian PA-RISC LASI bus), and 24-bit DMA byte counts.  Behaviour follows the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide (Oct 1990).

The base engine supports connected I/O and reselection-interrupt disconnect, enough to install and boot Debian off the 710 with the Linux lasi700/53c700 driver and to boot NetBSD/hppa 9.4 and 10.1 (osiop) with disk and CD-ROM enumeration.  Reselection follows the 53C700 family:
gated on the (re)selection interrupt enable (SIEN.SEL) alone, one-hot SDID, and an SFBR id bitmask in 700-compatibility mode.  SDTR/WDTR/PPR negotiation is declined with MESSAGE REJECT, which the 700-family drivers require to proceed: unlike the 8xx parts they leave the target to drive the phase after the reject.

The LASI glue (hw/scsi/lasi_ncr710.c) embeds one NCR710State by value. It gains the free_request SCSIBusInfo callback and request-queue init the core needs, renames the reselection timer to scripts_timer, and narrows the SCSI bus to the 8-bit inclusive maxima.

The migration format is incompatible with the model this replaces, so the vmstate version and minimum version are both raised.

---
hw/scsi/lasi_ncr710.c |  117 +-
hw/scsi/lasi_ncr710.h |   12 +-
hw/scsi/ncr53c710.c   | 2795 +++++++++++++++-------------------------- hw/scsi/ncr53c710.h   |  276 ++--
hw/scsi/trace-events  |   43 +-

5 files changed, 1191 insertions(+), 2052 deletions(-)

[...]

Quoted 456 lines
hw/scsi/ncr53c710.c +1 -2
index 57967e7a84..57cd7d91a4 100644
@@ -1,599 +1,308 @@
 /*
- * QEMU NCR710 SCSI Controller
+ * QEMU NCR 53C710 SCSI I/O Processor emulation
  *
- * Copyright (c) 2025 Soumyajyotii Ssarkar 

<[email protected]>
- * This driver was developed during the Google Summer of Code 2025 program.
+ * Copyright (c) 2026 Keith Monahan <[email protected]>
+ * Copyright (c) 2006 CodeSourcery (lsi53c895a.c, written by Paul Brook) *
- * NCR710 SCSI Controller implementation
- * Based on the NCR53C710 Technical Manual Version 3.2, December 2000
+ * A rewrite of the QEMU NCR 53C710 model.  The 53C710 is an early member of
+ * the NCR/LSI SCRIPTS processor family, so the SCRIPTS engine here is derived
+ * from QEMU's LSI53C895A model and adapted to the 53C710's register map,
+ * single byte interrupt model, big endian SCRIPTS fetch (PA-RISC) and 24 bit
+ * DMA, per the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide
+ * (Oct 1990).
 *
- * Developed from an implementation of NCR53C710 by Helge Deller
- * which was interim based on the implementation by Toni Wilen for UAE.
+ * Derived from hw/scsi/lsi53c895a.c (Copyright (c) 2006 CodeSourcery, written
+ * by Paul Brook).
 *
- * SPDX-License-Identifier: GPL-2.0-or-later
+ * Replaces the 53C710 model written for Google Summer of Code 2025 by
+ * Soumyajyotii Ssarkar <[email protected]>, itself based on
+ * earlier NCR 53C710 work by Helge Deller and by Toni Wilen (for UAE). *
- * Contents:
- * 1. Register Definitions
- * 2. Register name functions
- * 3. Parity functions
- * 4. SCSI FIFO Structures
- * 5. Scripts Misc functions
- * 6. DMA functions
- * 7. Scripts functions
- * 8. Read and Write functions
- * 9. QEMU Device model functions
+ * The only instantiated device is the LASI wrapper (lasi_ncr710.c), which
+ * embeds one NCR710State by value and drives it through this file. *
+ * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "qemu/osdep.h"

hw/scsi/ncr53c710.c +3 -2
-#include "qapi/error.h"
-#include "qemu/timer.h"
+#include "qemu/bitops.h"
+#include "qemu/log.h"
+#include "qemu/main-loop.h"

#include "hw/core/irq.h"

hw/scsi/ncr53c710.c +0 -2
-#include "hw/core/sysbus.h"
-#include "hw/scsi/scsi.h"

#include "hw/scsi/ncr53c710.h"

hw/scsi/ncr53c710.c +1 -4
-#include "migration/vmstate.h"
-#include "system/dma.h"
-#include "qemu/log.h"
-#include "qemu/module.h"
+#include "system/block-backend.h"

#include "trace.h"

hw/scsi/ncr53c710.c +35 -269
-#include "qom/object.h"
-
-#define NCR710_MAX_DEVS 7
-
-/* SCNTL0 (0x00) - SCSI Control Register 0 */
-#define NCR710_SCNTL0_TRG    0x01
-#define NCR710_SCNTL0_AAP    0x02
-#define NCR710_SCNTL0_EPG    0x04
-#define NCR710_SCNTL0_EPC    0x08
-#define NCR710_SCNTL0_WATN   0x10
-#define NCR710_SCNTL0_START  0x20
-#define NCR710_SCNTL0_ARB0   0x40
-#define NCR710_SCNTL0_ARB1   0x80
-
-/* SCNTL1 (0x01) - SCSI Control Register 1 */
-#define NCR710_SCNTL1_RES0   0x01
-#define NCR710_SCNTL1_RES1   0x02
-#define NCR710_SCNTL1_AESP   0x04
-#define NCR710_SCNTL1_RST    0x08
-#define NCR710_SCNTL1_CON    0x10
-#define NCR710_SCNTL1_ESR    0x20
-#define NCR710_SCNTL1_ADB    0x40
-#define NCR710_SCNTL1_EXC    0x80
-
-/* ISTAT (0x21) - Interrupt Status Register */
-#define NCR710_ISTAT_DIP    0x01
-#define NCR710_ISTAT_SIP    0x02
-#define NCR710_ISTAT_CON    0x08
-#define NCR710_ISTAT_SIGP   0x20
-#define NCR710_ISTAT_RST    0x40
-#define NCR710_ISTAT_ABRT   0x80
-
-/* SSTAT0 (0x0D) - SCSI Status Register 0 */
-#define NCR710_SSTAT0_PAR    0x01
-#define NCR710_SSTAT0_RST    0x02
-#define NCR710_SSTAT0_UDC    0x04
-#define NCR710_SSTAT0_SGE    0x08
-#define NCR710_SSTAT0_SEL    0x10
-#define NCR710_SSTAT0_STO    0x20
-#define NCR710_SSTAT0_FCMP   0x40
-#define NCR710_SSTAT0_MA     0x80
-
-/* SSTAT1 (0x0E) - SCSI Status Register 1 */
-#define NCR710_SSTAT1_ORF    0x02
-#define NCR710_SSTAT1_ILF    0x04
-
-/* SSTAT2 (0x0F) - SCSI Status Register 2 */
-#define NCR710_SSTAT2_FF0    0x01
-#define NCR710_SSTAT2_FF1    0x02
-#define NCR710_SSTAT2_FF2    0x04
-#define NCR710_SSTAT2_FF3    0x08
-
-/* SOCL (0x07) / SBCL (0x0B) - SCSI Output/Bus Control Lines */
-#define NCR710_SOCL_IO       0x01
-#define NCR710_SOCL_CD       0x02
-#define NCR710_SOCL_MSG      0x04
-#define NCR710_SOCL_ATN      0x08
-#define NCR710_SOCL_SEL      0x10
-#define NCR710_SOCL_BSY      0x20
-#define NCR710_SOCL_ACK      0x40
-#define NCR710_SOCL_REQ      0x80
-
-/* SBCL bits same as SOCL */
-#define NCR710_SBCL_IO       0x01
-#define NCR710_SBCL_CD       0x02
-#define NCR710_SBCL_MSG      0x04
-#define NCR710_SBCL_ATN      0x08
-#define NCR710_SBCL_SEL      0x10
-#define NCR710_SBCL_BSY      0x20
-#define NCR710_SBCL_ACK      0x40
-#define NCR710_SBCL_REQ      0x80
-
-/* DSTAT (0x0C) - DMA Status Register */
-#define NCR710_DSTAT_IID     0x01
-#define NCR710_DSTAT_SIR     0x04
-#define NCR710_DSTAT_SSI     0x08
-#define NCR710_DSTAT_ABRT    0x10
-#define NCR710_DSTAT_BF      0x20
-#define NCR710_DSTAT_MDPE    0x40
-#define NCR710_DSTAT_DFE     0x80
-
-/* DCNTL (0x3B) - DMA Control Register */
-#define NCR710_DCNTL_COM     0x01
-#define NCR710_DCNTL_IRQD    0x02
-#define NCR710_DCNTL_STD     0x04
-#define NCR710_DCNTL_IRQM    0x08
-#define NCR710_DCNTL_SSM     0x10
-#define NCR710_DCNTL_PFEN    0x20
-#define NCR710_DCNTL_PFF     0x40
-
-/* DMODE (0x38) - DMA Mode Register */
-#define NCR710_DMODE_MAN     0x01
-#define NCR710_DMODE_BOF     0x02
-#define NCR710_DMODE_ERMP    0x04
-#define NCR710_DMODE_ERL     0x08
-#define NCR710_DMODE_DIOM    0x10
-#define NCR710_DMODE_SIOM    0x20
-#define NCR710_DMODE_BL_MASK 0xC0
-#define NCR710_DMODE_BL_1    0x00
-#define NCR710_DMODE_BL_2    0x40
-#define NCR710_DMODE_BL_4    0x80
-#define NCR710_DMODE_BL_8    0xC0
-
-/* CTEST2 (0x16) - Chip Test Register 2 */
-#define NCR710_CTEST2_DACK   0x01
-#define NCR710_CTEST2_DREQ   0x02
-#define NCR710_CTEST2_TEOP   0x04
-#define NCR710_CTEST2_PCICIE 0x08
-#define NCR710_CTEST2_CM     0x10
-#define NCR710_CTEST2_CIO    0x20
-#define NCR710_CTEST2_SIGP   0x40
-#define NCR710_CTEST2_DDIR   0x80
-
-/* CTEST5 (0x19) - Chip Test Register 5 */
-#define NCR710_CTEST5_BL2    0x04
-#define NCR710_CTEST5_DDIR   0x08
-#define NCR710_CTEST5_MASR   0x10
-#define NCR710_CTEST5_DFSN   0x20
-#define NCR710_CTEST5_BBCK   0x40
-#define NCR710_CTEST5_ADCK   0x80
-
-/* SCID (0x04) - SCSI Chip ID Register */
-#define NCR710_SCID_RRE      0x60
-#define NCR710_SCID_ID_MASK  0x07
-
-#define NCR710_HOST_ID       7
-
-/* NCR53C710 has 8-byte SCSI FIFO */
-#define NCR710_MAX_MSGIN_LEN 8
-#define NCR710_BUF_SIZE         4096
-
-/* Standard SCSI Message Byte Constants */
-#define SCSI_MSG_ABORT                      0x06
-#define SCSI_MSG_BUS_DEVICE_RESET           0x0c
-#define SCSI_MSG_COMMAND_COMPLETE           0x00
-#define SCSI_MSG_DISCONNECT                 0x04
-#define SCSI_MSG_EXTENDED_MESSAGE           0x01
-#define SCSI_MSG_IDENTIFY                   0x80
-#define SCSI_MSG_IGNORE_WIDE_RESIDUE        0x23
-#define SCSI_MSG_MESSAGE_PARITY_ERROR       0x09
-#define SCSI_MSG_MESSAGE_REJECT             0x07
-#define SCSI_MSG_NO_OPERATION               0x08
-#define SCSI_MSG_RELEASE_RECOVERY           0x10
-#define SCSI_MSG_RESTORE_POINTERS           0x03
-#define SCSI_MSG_SAVE_DATA_POINTER          0x02
-#define SCSI_MSG_SYNCHRONOUS_DATA_TRANSFER  0x01
-#define SCSI_MSG_WIDE_DATA_TRANSFER         0x03
-
-/* Script interrupt codes */
-#define A_GOOD_STATUS_AFTER_STATUS          0x401
-#define A_DISCONNECT_AFTER_CMD              0x380
-#define A_DISCONNECT_AFTER_DATA             0x580
-#define A_DISCONNECT_DURING_DATA            0x780
-#define A_RESELECTION_IDENTIFIED            0x1003
-#define A_UNEXPECTED_PHASE                  0x20
-#define A_FATAL                             0x2000
-#define A_DEBUG_INTERRUPT                   0x3000
-
-/* SCSI Script execution states */
-#define SCRIPT_STATE_IDLE                  0
-#define SCRIPT_STATE_SELECTING             1
-#define SCRIPT_STATE_COMMAND               2
-#define SCRIPT_STATE_DATA                  3
-#define SCRIPT_STATE_STATUS                4
-#define SCRIPT_STATE_MESSAGE               5
-#define SCRIPT_STATE_DISCONNECTED          6
-
-#define AFTER_SELECTION         0x100
-#define BEFORE_CMD              0x200
-#define AFTER_CMD               0x300
-#define AFTER_STATUS            0x400
-#define AFTER_DATA_IN           0x500
-#define AFTER_DATA_OUT          0x600
-#define DURING_DATA_IN          0x700
-
-#define NOT_MSG_OUT             0x10
-#define UNEXPECTED_PHASE        0x20
-#define NOT_MSG_IN              0x30
-#define UNEXPECTED_MSG          0x40
-#define MSG_IN                  0x50
-#define SDTR_MSG_R              0x60
-#define REJECT_MSG_R            0x70
-#define DISCONNECT              0x80
-#define MSG_OUT                 0x90
-#define WDTR_MSG_R              0xA0
-
-#define GOOD_STATUS             0x1
-
-#define NOT_MSG_OUT_AFTER_SELECTION         0x110
-#define UNEXPECTED_PHASE_BEFORE_CMD         0x220
-#define UNEXPECTED_PHASE_AFTER_CMD          0x320
-#define NOT_MSG_IN_AFTER_STATUS             0x430
-#define GOOD_STATUS_AFTER_STATUS            0x401
-#define UNEXPECTED_PHASE_AFTER_DATA_IN      0x520
-#define UNEXPECTED_PHASE_AFTER_DATA_OUT     0x620
-#define UNEXPECTED_MSG_BEFORE_CMD           0x240
-#define MSG_IN_BEFORE_CMD                   0x250
-#define MSG_IN_AFTER_CMD                    0x350
-#define SDTR_MSG_BEFORE_CMD                 0x260
-#define REJECT_MSG_BEFORE_CMD               0x270
-#define DISCONNECT_AFTER_CMD                0x380
-#define SDTR_MSG_AFTER_CMD                  0x360
-#define WDTR_MSG_AFTER_CMD                  0x3A0
-#define MSG_IN_AFTER_STATUS                 0x440
-#define DISCONNECT_AFTER_DATA               0x580
-#define MSG_IN_AFTER_DATA_IN                0x550
-#define MSG_IN_AFTER_DATA_OUT               0x650
-#define MSG_OUT_AFTER_DATA_IN               0x590
-#define DATA_IN_AFTER_DATA_IN               0x5a0
-#define MSG_IN_DURING_DATA_IN               0x750
-#define DISCONNECT_DURING_DATA              0x780
-
-#define RESELECTED_DURING_SELECTION      0x1000
-#define COMPLETED_SELECTION_AS_TARGET    0x1001
-#define RESELECTION_IDENTIFIED           0x1003
-
-#define FATAL                            0x2000
-#define FATAL_UNEXPECTED_RESELECTION_MSG 0x2000
-#define FATAL_SEND_MSG                   0x2001
-#define FATAL_NOT_MSG_IN_AFTER_SELECTION 0x2002
-#define FATAL_ILLEGAL_MSG_LENGTH         0x2003
-
-#define DEBUG_INTERRUPT     0x3000
-#define DEBUG_INTERRUPT1    0x3001
-#define DEBUG_INTERRUPT2    0x3002
-#define DEBUG_INTERRUPT3    0x3003
-#define DEBUG_INTERRUPT4    0x3004
-#define DEBUG_INTERRUPT5    0x3005
-#define DEBUG_INTERRUPT6    0x3006
-
-#define COMMAND_COMPLETE_MSG      0x00
-#define EXTENDED_MSG              0x01
-#define SDTR_MSG                  0x01
-#define SAVE_DATA_PTRS_MSG        0x02
-#define RESTORE_DATA_PTRS_MSG     0x03
-#define WDTR_MSG                  0x03
-#define DISCONNECT_MSG            0x04
-#define REJECT_MSG                0x07
-#define PARITY_ERROR_MSG          0x09
-#define SIMPLE_TAG_MSG            0x20
-#define IDENTIFY_MSG              0x80
-#define IDENTIFY_MSG_MASK         0x7F
-#define TWO_BYTE_MSG              0x20
-#define TWO_BYTE_MSG_MASK         0x0F
-
-/* SCSI phases */
-#define PHASE_DO   0  /* Data out phase */
-#define PHASE_DI   1  /* Data in phase */
-#define PHASE_CO   2  /* Command phase */
-#define PHASE_SI   3  /* Status phase */
-#define PHASE_ST   3  /* Status phase (alias) */
-#define PHASE_MO   6  /* Message out phase */
-#define PHASE_MI   7  /* Message in phase */
-#define PHASE_MASK 7  /* Mask for phase bits */
-
-#define NCR710_TAG_VALID     (1 << 16)
-
-static void ncr710_scsi_fifo_init(NCR710_SCSI_FIFO *fifo);
-static const char *ncr710_reg_name(int offset);
-static void ncr710_script_scsi_interrupt(NCR710State *s, int stat0);
-static void ncr710_update_irq(NCR710State *s);
-static void ncr710_script_dma_interrupt(NCR710State *s, int stat);
-static void ncr710_request_free(NCR710State *s, NCR710Request *p);
-static inline void ncr710_dma_read(NCR710State *s, uint32_t addr,
-                                   void *buf, uint32_t len);
-static inline void ncr710_dma_write(NCR710State *s, uint32_t addr,
-                                    const void *buf, uint32_t len);
-static uint8_t ncr710_reg_readb(NCR710State *s, int offset);
-static void ncr710_reg_writeb(NCR710State *s, int offset, uint8_t val);
 
+/* SCNTL0 (0x00) */
+#define NCR710_SCNTL0_TRG       0x01
+#define NCR710_SCNTL0_AAP       0x02
+#define NCR710_SCNTL0_EPG       0x04
+#define NCR710_SCNTL0_EPC       0x08
+#define NCR710_SCNTL0_WATN      0x10
+#define NCR710_SCNTL0_START     0x20
+/* ARB1|ARB0 = 0xc0 (full arbitration) at reset */
+
+/* SCNTL1 (0x01) */
+#define NCR710_SCNTL1_AESP      0x04
+#define NCR710_SCNTL1_RST       0x08
+#define NCR710_SCNTL1_CON       0x10
+#define NCR710_SCNTL1_ESR       0x20
+#define NCR710_SCNTL1_ADB       0x40
+#define NCR710_SCNTL1_EXC       0x80
+
+/* SSTAT0 (0x0d) and SIEN (0x03) share this bit layout. */
+#define NCR710_STAT0_PAR        0x01
+#define NCR710_STAT0_RST        0x02
+#define NCR710_STAT0_UDC        0x04
+#define NCR710_STAT0_SGE        0x08
+#define NCR710_STAT0_SEL        0x10
+#define NCR710_STAT0_STO        0x20
+#define NCR710_STAT0_FCMP       0x40
+#define NCR710_STAT0_MA         0x80
+
+/* DSTAT (0x0c) and DIEN (0x39) share this bit layout. */
+#define NCR710_DSTAT_IID        0x01
+#define NCR710_DSTAT_WTD        0x02
+#define NCR710_DSTAT_SIR        0x04
+#define NCR710_DSTAT_SSI        0x08
+#define NCR710_DSTAT_ABRT       0x10
+#define NCR710_DSTAT_BF         0x20
+#define NCR710_DSTAT_DFE        0x80   /* pure status; never raises an 

IRQ */

hw/scsi/ncr53c710.c +10 -0
+
+/* ISTAT (0x21) */
+#define NCR710_ISTAT_DIP        0x01
+#define NCR710_ISTAT_SIP        0x02
+#define NCR710_ISTAT_CON        0x08
+#define NCR710_ISTAT_SIGP       0x20
+#define NCR710_ISTAT_RST        0x40
+#define NCR710_ISTAT_ABRT       0x80
+
+/* SBCL (0x0b): low 3 bits mirror the SCSI phase, upper bits are bus 

signals. */

hw/scsi/ncr53c710.c +25 -0
+#define NCR710_SBCL_ATN         0x08
+#define NCR710_SBCL_SEL         0x10
+#define NCR710_SBCL_BSY         0x20
+#define NCR710_SBCL_ACK         0x40
+#define NCR710_SBCL_REQ         0x80
+
+/* SOCL (0x07) */
+#define NCR710_SOCL_ATN         0x08
+
+/* CTEST2 (0x16) */
+#define NCR710_CTEST2_DACK      0x01
+#define NCR710_CTEST2_SIGP      0x40
+
+/* DMODE (0x38) */
+#define NCR710_DMODE_MAN        0x01
+
+/* DCNTL (0x3b) */
+#define NCR710_DCNTL_COM        0x01
+#define NCR710_DCNTL_FA         0x02
+#define NCR710_DCNTL_STD        0x04
+#define NCR710_DCNTL_LLM        0x08
+#define NCR710_DCNTL_SSM        0x10
+#define NCR710_DCNTL_EA         0x20
+
+/* SCSI phase (MSG,C/D,I/O); matches SSTAT2[2:0]/SBCL[2:0]/DCMD[26:24]. 

*/

hw/scsi/ncr53c710.c +21 -0
+#define PHASE_DO        0
+#define PHASE_DI        1
+#define PHASE_CMD       2
+#define PHASE_ST        3
+#define PHASE_MO        6
+#define PHASE_MI        7
+#define PHASE_MASK      7
+
+/* Flag bit OR'd into select_tag/tag when a queue tag is valid. */
+#define NCR710_TAG_VALID    (1 << 16)
+
+/* Maximum SCRIPTS instructions to process before yielding to the CPU. */
+#define NCR710_MAX_INSN     500
+
+#define NCR710_BUF_SIZE     4096
+
+/* waiting state machine (mirrors lsi53c895a). */
+enum {
+    NCR710_NOWAIT = 0,        /* SCRIPTS running or stopped */
+    NCR710_WAIT_RESELECT,     /* Wait Reselect instruction issued */
+    NCR710_DMA_SCRIPTS,       /* DMA invoked from within execute_script 

*/
+    NCR710_DMA_IN_PROGRESS,   /* asynchronous DMA in progress */ +    NCR710_WAIT_SCRIPTS,      /* stopped on the instruction count limit */

hw/scsi/ncr53c710.c +14 -1
+};
 
-static inline int ncr710_irq_on_rsl(NCR710State *s)
+enum {
+    NCR710_MSG_ACTION_COMMAND = 0,
+    NCR710_MSG_ACTION_DISCONNECT = 1,
+    NCR710_MSG_ACTION_DOUT = 2,
+    NCR710_MSG_ACTION_DIN = 3,
+};
+
+struct NCR710Request {
+    SCSIRequest *req;
+    uint32_t tag;
+    uint32_t dma_len;
+    uint8_t *dma_buf;
+    uint32_t pending;     /* bytes the SCSI layer has ready while queued 
Quoted 6 lines

*/
+    int out;              /* nonzero if the queued transfer is DATA OUT */
+    bool orphan;
+    QTAILQ_ENTRY(NCR710Request) next;
+};

Quoted 7 lines

This struct is exactly the same as lsi_request in lsi53c895a.c. As a start this and helper functions working on it should be split off in a shared file and header from lsi53c895a.c and reused here. That needs changing lsi53c895a.c but it's trivial changes easy to review and does not break it. That with more small chnages split off as suggested by Peter should reduce the size of this patch to a more managable size so we can see what else could be shared.

Quoted 6 lines

I fully understand and generally support the wish to merge and share things in the hope for easier review and possible maintanance reduction.

But even in the Linux kernel those drivers don't share code. Will we really gain much if we try to split the existing working drivers and try share code between those?

Yes, reduced duplicated code and avoiding divergence with bugs only introduced or fixed in one of them in the future. IMO that's worth some restructuring.

Quoted 3 lines

I mean, the 710 and 89x chips are quite different, and so will be the code.

In this specific case IMHO it's better to keep them seperate.

I'm not asking for merging the two models and rewrite the 53c895a much but trivially shareable code such as request queue handling that seems to be just reused here should not be duplicated and this seems to be easily split from exsting code and reused here.

I'm not the maintainer so whatever the one responsible here prefers but I think trying to split this patch up and share some easily shared code is reasonable to ask to avoid adding duplicated code. Then the rest where these are really different can be separate without sharing more code but currently there are a lot of defines, types and some functions duplicated in this patch that could be split off and trivially shared.

Regards,
BALATON Zoltan

#17 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Balaton,

I raised this in the v1 thread, but here it is again with more numbers. Against ncr53c710.c's 1,243 significant lines, textually identical code is 15.4 percent. If I first rename the symbols, lsi_ to ncr710_ and LSIState to NCR710State, and then count every line that merely resembles an 895 line, it reaches 32.8 percent. But that is a resemblance ceiling, not a measure of code that can be cleanly factored out.

What actually matters for a refactor is how much could move into a shared core without changing 895 behavior. I reviewed all 44 functions:
5.5 percent, about 58 lines, ten small helpers. In line counts, the gap is the point: 32.8 percent looks like a shared core could absorb about 400 lines, but only about 58 come out cleanly. The rest is interleaved line by line with the 710 differences: single byte interrupt model, register map, big endian fetch, 24 bit DMA, and bottom half reselect. Those differences live inside execute_script, reg_readb, and reg_writeb, which are about half the file. Sharing that code means splitting every hot path function or adding is_710 branches into the 895.

The macros make the same point, and they are worth calling out because they are the first thing you see when you open the file. Of 102 macros in the 710, only seven are identical to the 895, and those are just the standard SCSI 2 bus phases. Another 38 match once you strip the prefix:
chip family register bit positions, the same value under a different name. The 42 register offset macros are specific to the 710. So the most visible overlap, that block of defines up top, is also the most trivial:
one liners, not logic. What you hit first is not representative of the rest of the file, where the interpreters actually diverge!

So the genuinely shared code is about 12 to 15 percent, and the cleanly factorable part is about 5 percent. I kept the models as siblings because folding the interpreters together adds cross model coupling and 895 regression risk for very little deduplication.

My conclusion is the same as in my previous message, and the numbers only make it firmer: the refactor work, the testing burden, and above all the regression risk it forces onto lsi53c895a.c, a model that far more guests and architectures depend on than this one, cannot be justified by deduplicating roughly 5 percent of the file, the ~58 lines that actually move cleanly.

That is trading real risk to a proven, widely used model for a cosmetic win on code that is largely not duplicated to begin with. The 33 percent resemblance figure does not change that, because none of that resemblance is separable without cutting into the 895 hot path. I do not think it is a good trade at any reading of the numbers.

Thanks,
Keith

Quoted 536 lines · BALATON Zoltan
Quoted 516 lines · Helge Deller
Quoted 507 lines · BALATON Zoltan
Quoted 34 lines · Keith Monahan

Replace the NCR 53C710 model with a SCRIPTS engine derived from QEMU's LSI53C895A model (hw/scsi/lsi53c895a.c), another device in the same NCR/LSI SCRIPTS family, adapted to the 53C710: its register map, the single-byte DSTAT/SSTAT0 interrupt model, big-endian SCRIPTS and table-indirect fetch (the part sits on the big-endian PA-RISC LASI bus), and 24-bit DMA byte counts.  Behaviour follows the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide (Oct 1990).

The base engine supports connected I/O and reselection-interrupt disconnect, enough to install and boot Debian off the 710 with the Linux lasi700/53c700 driver and to boot NetBSD/hppa 9.4 and 10.1 (osiop) with disk and CD-ROM enumeration.  Reselection follows the 53C700 family:
gated on the (re)selection interrupt enable (SIEN.SEL) alone, one-hot SDID, and an SFBR id bitmask in 700-compatibility mode.  SDTR/WDTR/PPR negotiation is declined with MESSAGE REJECT, which the 700-family drivers require to proceed: unlike the 8xx parts they leave the target to drive the phase after the reject.

The LASI glue (hw/scsi/lasi_ncr710.c) embeds one NCR710State by value. It gains the free_request SCSIBusInfo callback and request-queue init the core needs, renames the reselection timer to scripts_timer, and narrows the SCSI bus to the 8-bit inclusive maxima.

The migration format is incompatible with the model this replaces, so the vmstate version and minimum version are both raised.

---
hw/scsi/lasi_ncr710.c |  117 +-
hw/scsi/lasi_ncr710.h |   12 +-
hw/scsi/ncr53c710.c   | 2795 +++++++++++++++-------------------------- hw/scsi/ncr53c710.h   |  276 ++--
hw/scsi/trace-events  |   43 +-

5 files changed, 1191 insertions(+), 2052 deletions(-)

[...]

Quoted 464 lines
hw/scsi/ncr53c710.c +1 -2
index 57967e7a84..57cd7d91a4 100644
@@ -1,599 +1,308 @@
 /*
- * QEMU NCR710 SCSI Controller
+ * QEMU NCR 53C710 SCSI I/O Processor emulation
  *
- * Copyright (c) 2025 Soumyajyotii Ssarkar 

<[email protected]>
- * This driver was developed during the Google Summer of Code 2025 program.
+ * Copyright (c) 2026 Keith Monahan <[email protected]>
+ * Copyright (c) 2006 CodeSourcery (lsi53c895a.c, written by Paul Brook)
 *
- * NCR710 SCSI Controller implementation
- * Based on the NCR53C710 Technical Manual Version 3.2, December 2000
+ * A rewrite of the QEMU NCR 53C710 model.  The 53C710 is an early member of
+ * the NCR/LSI SCRIPTS processor family, so the SCRIPTS engine here is derived
+ * from QEMU's LSI53C895A model and adapted to the 53C710's register map,
+ * single byte interrupt model, big endian SCRIPTS fetch (PA-RISC) and 24 bit
+ * DMA, per the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide
+ * (Oct 1990).
 *
- * Developed from an implementation of NCR53C710 by Helge Deller
- * which was interim based on the implementation by Toni Wilen for UAE.
+ * Derived from hw/scsi/lsi53c895a.c (Copyright (c) 2006 CodeSourcery, written
+ * by Paul Brook).
 *
- * SPDX-License-Identifier: GPL-2.0-or-later
+ * Replaces the 53C710 model written for Google Summer of Code 2025 by
+ * Soumyajyotii Ssarkar <[email protected]>, itself based on
+ * earlier NCR 53C710 work by Helge Deller and by Toni Wilen (for UAE).
 *
- * Contents:
- * 1. Register Definitions
- * 2. Register name functions
- * 3. Parity functions
- * 4. SCSI FIFO Structures
- * 5. Scripts Misc functions
- * 6. DMA functions
- * 7. Scripts functions
- * 8. Read and Write functions
- * 9. QEMU Device model functions
+ * The only instantiated device is the LASI wrapper (lasi_ncr710.c), which
+ * embeds one NCR710State by value and drives it through this file. *
+ * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "qemu/osdep.h"

hw/scsi/ncr53c710.c +3 -2
-#include "qapi/error.h"
-#include "qemu/timer.h"
+#include "qemu/bitops.h"
+#include "qemu/log.h"
+#include "qemu/main-loop.h"

#include "hw/core/irq.h"

hw/scsi/ncr53c710.c +0 -2
-#include "hw/core/sysbus.h"
-#include "hw/scsi/scsi.h"

#include "hw/scsi/ncr53c710.h"

hw/scsi/ncr53c710.c +1 -4
-#include "migration/vmstate.h"
-#include "system/dma.h"
-#include "qemu/log.h"
-#include "qemu/module.h"
+#include "system/block-backend.h"

#include "trace.h"

hw/scsi/ncr53c710.c +0 -269
-#include "qom/object.h"
-
-#define NCR710_MAX_DEVS 7
-
-/* SCNTL0 (0x00) - SCSI Control Register 0 */
-#define NCR710_SCNTL0_TRG    0x01
-#define NCR710_SCNTL0_AAP    0x02
-#define NCR710_SCNTL0_EPG    0x04
-#define NCR710_SCNTL0_EPC    0x08
-#define NCR710_SCNTL0_WATN   0x10
-#define NCR710_SCNTL0_START  0x20
-#define NCR710_SCNTL0_ARB0   0x40
-#define NCR710_SCNTL0_ARB1   0x80
-
-/* SCNTL1 (0x01) - SCSI Control Register 1 */
-#define NCR710_SCNTL1_RES0   0x01
-#define NCR710_SCNTL1_RES1   0x02
-#define NCR710_SCNTL1_AESP   0x04
-#define NCR710_SCNTL1_RST    0x08
-#define NCR710_SCNTL1_CON    0x10
-#define NCR710_SCNTL1_ESR    0x20
-#define NCR710_SCNTL1_ADB    0x40
-#define NCR710_SCNTL1_EXC    0x80
-
-/* ISTAT (0x21) - Interrupt Status Register */
-#define NCR710_ISTAT_DIP    0x01
-#define NCR710_ISTAT_SIP    0x02
-#define NCR710_ISTAT_CON    0x08
-#define NCR710_ISTAT_SIGP   0x20
-#define NCR710_ISTAT_RST    0x40
-#define NCR710_ISTAT_ABRT   0x80
-
-/* SSTAT0 (0x0D) - SCSI Status Register 0 */
-#define NCR710_SSTAT0_PAR    0x01
-#define NCR710_SSTAT0_RST    0x02
-#define NCR710_SSTAT0_UDC    0x04
-#define NCR710_SSTAT0_SGE    0x08
-#define NCR710_SSTAT0_SEL    0x10
-#define NCR710_SSTAT0_STO    0x20
-#define NCR710_SSTAT0_FCMP   0x40
-#define NCR710_SSTAT0_MA     0x80
-
-/* SSTAT1 (0x0E) - SCSI Status Register 1 */
-#define NCR710_SSTAT1_ORF    0x02
-#define NCR710_SSTAT1_ILF    0x04
-
-/* SSTAT2 (0x0F) - SCSI Status Register 2 */
-#define NCR710_SSTAT2_FF0    0x01
-#define NCR710_SSTAT2_FF1    0x02
-#define NCR710_SSTAT2_FF2    0x04
-#define NCR710_SSTAT2_FF3    0x08
-
-/* SOCL (0x07) / SBCL (0x0B) - SCSI Output/Bus Control Lines */
-#define NCR710_SOCL_IO       0x01
-#define NCR710_SOCL_CD       0x02
-#define NCR710_SOCL_MSG      0x04
-#define NCR710_SOCL_ATN      0x08
-#define NCR710_SOCL_SEL      0x10
-#define NCR710_SOCL_BSY      0x20
-#define NCR710_SOCL_ACK      0x40
-#define NCR710_SOCL_REQ      0x80
-
-/* SBCL bits same as SOCL */
-#define NCR710_SBCL_IO       0x01
-#define NCR710_SBCL_CD       0x02
-#define NCR710_SBCL_MSG      0x04
-#define NCR710_SBCL_ATN      0x08
-#define NCR710_SBCL_SEL      0x10
-#define NCR710_SBCL_BSY      0x20
-#define NCR710_SBCL_ACK      0x40
-#define NCR710_SBCL_REQ      0x80
-
-/* DSTAT (0x0C) - DMA Status Register */
-#define NCR710_DSTAT_IID     0x01
-#define NCR710_DSTAT_SIR     0x04
-#define NCR710_DSTAT_SSI     0x08
-#define NCR710_DSTAT_ABRT    0x10
-#define NCR710_DSTAT_BF      0x20
-#define NCR710_DSTAT_MDPE    0x40
-#define NCR710_DSTAT_DFE     0x80
-
-/* DCNTL (0x3B) - DMA Control Register */
-#define NCR710_DCNTL_COM     0x01
-#define NCR710_DCNTL_IRQD    0x02
-#define NCR710_DCNTL_STD     0x04
-#define NCR710_DCNTL_IRQM    0x08
-#define NCR710_DCNTL_SSM     0x10
-#define NCR710_DCNTL_PFEN    0x20
-#define NCR710_DCNTL_PFF     0x40
-
-/* DMODE (0x38) - DMA Mode Register */
-#define NCR710_DMODE_MAN     0x01
-#define NCR710_DMODE_BOF     0x02
-#define NCR710_DMODE_ERMP    0x04
-#define NCR710_DMODE_ERL     0x08
-#define NCR710_DMODE_DIOM    0x10
-#define NCR710_DMODE_SIOM    0x20
-#define NCR710_DMODE_BL_MASK 0xC0
-#define NCR710_DMODE_BL_1    0x00
-#define NCR710_DMODE_BL_2    0x40
-#define NCR710_DMODE_BL_4    0x80
-#define NCR710_DMODE_BL_8    0xC0
-
-/* CTEST2 (0x16) - Chip Test Register 2 */
-#define NCR710_CTEST2_DACK   0x01
-#define NCR710_CTEST2_DREQ   0x02
-#define NCR710_CTEST2_TEOP   0x04
-#define NCR710_CTEST2_PCICIE 0x08
-#define NCR710_CTEST2_CM     0x10
-#define NCR710_CTEST2_CIO    0x20
-#define NCR710_CTEST2_SIGP   0x40
-#define NCR710_CTEST2_DDIR   0x80
-
-/* CTEST5 (0x19) - Chip Test Register 5 */
-#define NCR710_CTEST5_BL2    0x04
-#define NCR710_CTEST5_DDIR   0x08
-#define NCR710_CTEST5_MASR   0x10
-#define NCR710_CTEST5_DFSN   0x20
-#define NCR710_CTEST5_BBCK   0x40
-#define NCR710_CTEST5_ADCK   0x80
-
-/* SCID (0x04) - SCSI Chip ID Register */
-#define NCR710_SCID_RRE      0x60
-#define NCR710_SCID_ID_MASK  0x07
-
-#define NCR710_HOST_ID       7
-
-/* NCR53C710 has 8-byte SCSI FIFO */
-#define NCR710_MAX_MSGIN_LEN 8
-#define NCR710_BUF_SIZE         4096
-
-/* Standard SCSI Message Byte Constants */
-#define SCSI_MSG_ABORT                      0x06
-#define SCSI_MSG_BUS_DEVICE_RESET           0x0c
-#define SCSI_MSG_COMMAND_COMPLETE           0x00
-#define SCSI_MSG_DISCONNECT                 0x04
-#define SCSI_MSG_EXTENDED_MESSAGE           0x01
-#define SCSI_MSG_IDENTIFY                   0x80
-#define SCSI_MSG_IGNORE_WIDE_RESIDUE        0x23
-#define SCSI_MSG_MESSAGE_PARITY_ERROR       0x09
-#define SCSI_MSG_MESSAGE_REJECT             0x07
-#define SCSI_MSG_NO_OPERATION               0x08
-#define SCSI_MSG_RELEASE_RECOVERY           0x10
-#define SCSI_MSG_RESTORE_POINTERS           0x03
-#define SCSI_MSG_SAVE_DATA_POINTER          0x02
-#define SCSI_MSG_SYNCHRONOUS_DATA_TRANSFER  0x01
-#define SCSI_MSG_WIDE_DATA_TRANSFER         0x03
-
-/* Script interrupt codes */
-#define A_GOOD_STATUS_AFTER_STATUS          0x401
-#define A_DISCONNECT_AFTER_CMD              0x380
-#define A_DISCONNECT_AFTER_DATA             0x580
-#define A_DISCONNECT_DURING_DATA            0x780
-#define A_RESELECTION_IDENTIFIED            0x1003
-#define A_UNEXPECTED_PHASE                  0x20
-#define A_FATAL                             0x2000
-#define A_DEBUG_INTERRUPT                   0x3000
-
-/* SCSI Script execution states */
-#define SCRIPT_STATE_IDLE                  0
-#define SCRIPT_STATE_SELECTING             1
-#define SCRIPT_STATE_COMMAND               2
-#define SCRIPT_STATE_DATA                  3
-#define SCRIPT_STATE_STATUS                4
-#define SCRIPT_STATE_MESSAGE               5
-#define SCRIPT_STATE_DISCONNECTED          6
-
-#define AFTER_SELECTION         0x100
-#define BEFORE_CMD              0x200
-#define AFTER_CMD               0x300
-#define AFTER_STATUS            0x400
-#define AFTER_DATA_IN           0x500
-#define AFTER_DATA_OUT          0x600
-#define DURING_DATA_IN          0x700
-
-#define NOT_MSG_OUT             0x10
-#define UNEXPECTED_PHASE        0x20
-#define NOT_MSG_IN              0x30
-#define UNEXPECTED_MSG          0x40
-#define MSG_IN                  0x50
-#define SDTR_MSG_R              0x60
-#define REJECT_MSG_R            0x70
-#define DISCONNECT              0x80
-#define MSG_OUT                 0x90
-#define WDTR_MSG_R              0xA0
-
-#define GOOD_STATUS             0x1
-
-#define NOT_MSG_OUT_AFTER_SELECTION         0x110
-#define UNEXPECTED_PHASE_BEFORE_CMD         0x220
-#define UNEXPECTED_PHASE_AFTER_CMD          0x320
-#define NOT_MSG_IN_AFTER_STATUS             0x430
-#define GOOD_STATUS_AFTER_STATUS            0x401
-#define UNEXPECTED_PHASE_AFTER_DATA_IN      0x520
-#define UNEXPECTED_PHASE_AFTER_DATA_OUT     0x620
-#define UNEXPECTED_MSG_BEFORE_CMD           0x240
-#define MSG_IN_BEFORE_CMD                   0x250
-#define MSG_IN_AFTER_CMD                    0x350
-#define SDTR_MSG_BEFORE_CMD                 0x260
-#define REJECT_MSG_BEFORE_CMD               0x270
-#define DISCONNECT_AFTER_CMD                0x380
-#define SDTR_MSG_AFTER_CMD                  0x360
-#define WDTR_MSG_AFTER_CMD                  0x3A0
-#define MSG_IN_AFTER_STATUS                 0x440
-#define DISCONNECT_AFTER_DATA               0x580
-#define MSG_IN_AFTER_DATA_IN                0x550
-#define MSG_IN_AFTER_DATA_OUT               0x650
-#define MSG_OUT_AFTER_DATA_IN               0x590
-#define DATA_IN_AFTER_DATA_IN               0x5a0
-#define MSG_IN_DURING_DATA_IN               0x750
-#define DISCONNECT_DURING_DATA              0x780
-
-#define RESELECTED_DURING_SELECTION      0x1000
-#define COMPLETED_SELECTION_AS_TARGET    0x1001
-#define RESELECTION_IDENTIFIED           0x1003
-
-#define FATAL                            0x2000
-#define FATAL_UNEXPECTED_RESELECTION_MSG 0x2000
-#define FATAL_SEND_MSG                   0x2001
-#define FATAL_NOT_MSG_IN_AFTER_SELECTION 0x2002
-#define FATAL_ILLEGAL_MSG_LENGTH         0x2003
-
-#define DEBUG_INTERRUPT     0x3000
-#define DEBUG_INTERRUPT1    0x3001
-#define DEBUG_INTERRUPT2    0x3002
-#define DEBUG_INTERRUPT3    0x3003
-#define DEBUG_INTERRUPT4    0x3004
-#define DEBUG_INTERRUPT5    0x3005
-#define DEBUG_INTERRUPT6    0x3006
-
-#define COMMAND_COMPLETE_MSG      0x00
-#define EXTENDED_MSG              0x01
-#define SDTR_MSG                  0x01
-#define SAVE_DATA_PTRS_MSG        0x02
-#define RESTORE_DATA_PTRS_MSG     0x03
-#define WDTR_MSG                  0x03
-#define DISCONNECT_MSG            0x04
-#define REJECT_MSG                0x07
-#define PARITY_ERROR_MSG          0x09
-#define SIMPLE_TAG_MSG            0x20
-#define IDENTIFY_MSG              0x80
-#define IDENTIFY_MSG_MASK         0x7F
-#define TWO_BYTE_MSG              0x20
-#define TWO_BYTE_MSG_MASK         0x0F
-
-/* SCSI phases */
-#define PHASE_DO   0  /* Data out phase */
-#define PHASE_DI   1  /* Data in phase */
-#define PHASE_CO   2  /* Command phase */
-#define PHASE_SI   3  /* Status phase */
-#define PHASE_ST   3  /* Status phase (alias) */
-#define PHASE_MO   6  /* Message out phase */
-#define PHASE_MI   7  /* Message in phase */
-#define PHASE_MASK 7  /* Mask for phase bits */
-
-#define NCR710_TAG_VALID     (1 << 16)
-
-static void ncr710_scsi_fifo_init(NCR710_SCSI_FIFO *fifo);
-static const char *ncr710_reg_name(int offset);
-static void ncr710_script_scsi_interrupt(NCR710State *s, int stat0);
-static void ncr710_update_irq(NCR710State *s);
-static void ncr710_script_dma_interrupt(NCR710State *s, int stat);
-static void ncr710_request_free(NCR710State *s, NCR710Request *p);
-static inline void ncr710_dma_read(NCR710State *s, uint32_t addr,
-                                   void *buf, uint32_t len);
-static inline void ncr710_dma_write(NCR710State *s, uint32_t addr,
-                                    const void *buf, uint32_t len);
-static uint8_t ncr710_reg_readb(NCR710State *s, int offset);
-static void ncr710_reg_writeb(NCR710State *s, int offset, uint8_t 

val);

hw/scsi/ncr53c710.c +35 -0
+/* SCNTL0 (0x00) */
+#define NCR710_SCNTL0_TRG       0x01
+#define NCR710_SCNTL0_AAP       0x02
+#define NCR710_SCNTL0_EPG       0x04
+#define NCR710_SCNTL0_EPC       0x08
+#define NCR710_SCNTL0_WATN      0x10
+#define NCR710_SCNTL0_START     0x20
+/* ARB1|ARB0 = 0xc0 (full arbitration) at reset */
+
+/* SCNTL1 (0x01) */
+#define NCR710_SCNTL1_AESP      0x04
+#define NCR710_SCNTL1_RST       0x08
+#define NCR710_SCNTL1_CON       0x10
+#define NCR710_SCNTL1_ESR       0x20
+#define NCR710_SCNTL1_ADB       0x40
+#define NCR710_SCNTL1_EXC       0x80
+
+/* SSTAT0 (0x0d) and SIEN (0x03) share this bit layout. */
+#define NCR710_STAT0_PAR        0x01
+#define NCR710_STAT0_RST        0x02
+#define NCR710_STAT0_UDC        0x04
+#define NCR710_STAT0_SGE        0x08
+#define NCR710_STAT0_SEL        0x10
+#define NCR710_STAT0_STO        0x20
+#define NCR710_STAT0_FCMP       0x40
+#define NCR710_STAT0_MA         0x80
+
+/* DSTAT (0x0c) and DIEN (0x39) share this bit layout. */
+#define NCR710_DSTAT_IID        0x01
+#define NCR710_DSTAT_WTD        0x02
+#define NCR710_DSTAT_SIR        0x04
+#define NCR710_DSTAT_SSI        0x08
+#define NCR710_DSTAT_ABRT       0x10
+#define NCR710_DSTAT_BF         0x20
+#define NCR710_DSTAT_DFE        0x80   /* pure status; never raises 

an IRQ */

hw/scsi/ncr53c710.c +10 -0
+
+/* ISTAT (0x21) */
+#define NCR710_ISTAT_DIP        0x01
+#define NCR710_ISTAT_SIP        0x02
+#define NCR710_ISTAT_CON        0x08
+#define NCR710_ISTAT_SIGP       0x20
+#define NCR710_ISTAT_RST        0x40
+#define NCR710_ISTAT_ABRT       0x80
+
+/* SBCL (0x0b): low 3 bits mirror the SCSI phase, upper bits are 

bus signals. */

hw/scsi/ncr53c710.c +25 -0
+#define NCR710_SBCL_ATN         0x08
+#define NCR710_SBCL_SEL         0x10
+#define NCR710_SBCL_BSY         0x20
+#define NCR710_SBCL_ACK         0x40
+#define NCR710_SBCL_REQ         0x80
+
+/* SOCL (0x07) */
+#define NCR710_SOCL_ATN         0x08
+
+/* CTEST2 (0x16) */
+#define NCR710_CTEST2_DACK      0x01
+#define NCR710_CTEST2_SIGP      0x40
+
+/* DMODE (0x38) */
+#define NCR710_DMODE_MAN        0x01
+
+/* DCNTL (0x3b) */
+#define NCR710_DCNTL_COM        0x01
+#define NCR710_DCNTL_FA         0x02
+#define NCR710_DCNTL_STD        0x04
+#define NCR710_DCNTL_LLM        0x08
+#define NCR710_DCNTL_SSM        0x10
+#define NCR710_DCNTL_EA         0x20
+
+/* SCSI phase (MSG,C/D,I/O); matches SSTAT2[2:0]/SBCL[2:0]/ 

DCMD[26:24]. */

hw/scsi/ncr53c710.c +12 -0
+#define PHASE_DO        0
+#define PHASE_DI        1
+#define PHASE_CMD       2
+#define PHASE_ST        3
+#define PHASE_MO        6
+#define PHASE_MI        7
+#define PHASE_MASK      7
+
+/* Flag bit OR'd into select_tag/tag when a queue tag is valid. */
+#define NCR710_TAG_VALID    (1 << 16)
+
+/* Maximum SCRIPTS instructions to process before yielding to the 

CPU. */

hw/scsi/ncr53c710.c +9 -0
+#define NCR710_MAX_INSN     500
+
+#define NCR710_BUF_SIZE     4096
+
+/* waiting state machine (mirrors lsi53c895a). */
+enum {
+    NCR710_NOWAIT = 0,        /* SCRIPTS running or stopped */
+    NCR710_WAIT_RESELECT,     /* Wait Reselect instruction issued */
+    NCR710_DMA_SCRIPTS,       /* DMA invoked from within 

execute_script */
+    NCR710_DMA_IN_PROGRESS,   /* asynchronous DMA in progress */ +    NCR710_WAIT_SCRIPTS,      /* stopped on the instruction count limit */

hw/scsi/ncr53c710.c +14 -1
+};
 
-static inline int ncr710_irq_on_rsl(NCR710State *s)
+enum {
+    NCR710_MSG_ACTION_COMMAND = 0,
+    NCR710_MSG_ACTION_DISCONNECT = 1,
+    NCR710_MSG_ACTION_DOUT = 2,
+    NCR710_MSG_ACTION_DIN = 3,
+};
+
+struct NCR710Request {
+    SCSIRequest *req;
+    uint32_t tag;
+    uint32_t dma_len;
+    uint8_t *dma_buf;
+    uint32_t pending;     /* bytes the SCSI layer has ready while 
Quoted 6 lines

queued */
+    int out;              /* nonzero if the queued transfer is DATA OUT */
+    bool orphan;
+    QTAILQ_ENTRY(NCR710Request) next;
+};

Quoted 7 lines

This struct is exactly the same as lsi_request in lsi53c895a.c. As a start this and helper functions working on it should be split off in a shared file and header from lsi53c895a.c and reused here. That needs changing lsi53c895a.c but it's trivial changes easy to review and does not break it. That with more small chnages split off as suggested by Peter should reduce the size of this patch to a more managable size so we can see what else could be shared.

Quoted 6 lines

I fully understand and generally support the wish to merge and share things in the hope for easier review and possible maintanance reduction.

But even in the Linux kernel those drivers don't share code. Will we really gain much if we try to split the existing working drivers and try share code between those?

Yes, reduced duplicated code and avoiding divergence with bugs only introduced or fixed in one of them in the future. IMO that's worth some restructuring.

Quoted 3 lines

I mean, the 710 and 89x chips are quite different, and so will be the code.

In this specific case IMHO it's better to keep them seperate.

I'm not asking for merging the two models and rewrite the 53c895a much but trivially shareable code such as request queue handling that seems to be just reused here should not be duplicated and this seems to be easily split from exsting code and reused here.

I'm not the maintainer so whatever the one responsible here prefers but I think trying to split this patch up and share some easily shared code is reasonable to ask to avoid adding duplicated code. Then the rest where these are really different can be separate without sharing more code but currently there are a lot of defines, types and some functions duplicated in this patch that could be split off and trivially shared.

Regards,
BALATON Zoltan

#18 lore raw

Re: [PATCH v2 3/4] hw/scsi/ncr53c710: rewrite the SCRIPTS engine

Quoted 30 lines · Keith Monahan

Balaton,

I raised this in the v1 thread, but here it is again with more numbers. Against ncr53c710.c's 1,243 significant lines, textually identical code is 15.4 percent. If I first rename the symbols, lsi_ to ncr710_ and LSIState to NCR710State, and then count every line that merely resembles an 895 line, it reaches 32.8 percent. But that is a resemblance ceiling, not a measure of code that can be cleanly factored out.

What actually matters for a refactor is how much could move into a shared core without changing 895 behavior. I reviewed all 44 functions:
5.5 percent, about 58 lines, ten small helpers. In line counts, the gap is the point: 32.8 percent looks like a shared core could absorb about 400 lines, but only about 58 come out cleanly. The rest is interleaved line by line with the 710 differences: single byte interrupt model, register map, big endian fetch, 24 bit DMA, and bottom half reselect. Those differences live inside execute_script, reg_readb, and reg_writeb, which are about half the file. Sharing that code means splitting every hot path function or adding is_710 branches into the 895.

The macros make the same point, and they are worth calling out because they are the first thing you see when you open the file. Of 102 macros in the 710, only seven are identical to the 895, and those are just the standard SCSI 2 bus phases. Another 38 match once you strip the prefix:
chip family register bit positions, the same value under a different name. The 42 register offset macros are specific to the 710. So the most visible overlap, that block of defines up top, is also the most trivial:
one liners, not logic. What you hit first is not representative of the rest of the file, where the interpreters actually diverge!

Quoted 4 lines

So the genuinely shared code is about 12 to 15 percent, and the cleanly factorable part is about 5 percent. I kept the models as siblings because folding the interpreters together adds cross model coupling and 895 regression risk for very little deduplication.

I did not suggest to rewrite 895 model to implement both devices by same code if that's not trivial to do. As you say there may be too many differences for that. But I still think it's worth splitting out at least the lsi_request struct and its helpers (i.e. request queue handling) and the few shared macros into some common lsiscsi.{ch} or similar and use that in both 710 and 895 models where these are exactly the same to avoid code duplication. Even if this is only 5-10 percent of the whole model, sharing it makes sure they won't diverge and get different bugs and fixes separately in the future. But as I said earlier whatever the scsi subsystem maintainers prefer, if they think it's OK to have code duplication I don't care.

Regards,
BALATON Zoltan

Quoted 552 lines

My conclusion is the same as in my previous message, and the numbers only make it firmer: the refactor work, the testing burden, and above all the regression risk it forces onto lsi53c895a.c, a model that far more guests and architectures depend on than this one, cannot be justified by deduplicating roughly 5 percent of the file, the ~58 lines that actually move cleanly.

That is trading real risk to a proven, widely used model for a cosmetic win on code that is largely not duplicated to begin with. The 33 percent resemblance figure does not change that, because none of that resemblance is separable without cutting into the 895 hot path. I do not think it is a good trade at any reading of the numbers.

Thanks,
Keith

Quoted 533 lines · BALATON Zoltan
Quoted 513 lines · Helge Deller
Quoted 504 lines · BALATON Zoltan
Quoted 34 lines · Keith Monahan

Replace the NCR 53C710 model with a SCRIPTS engine derived from QEMU's LSI53C895A model (hw/scsi/lsi53c895a.c), another device in the same NCR/LSI SCRIPTS family, adapted to the 53C710: its register map, the single-byte DSTAT/SSTAT0 interrupt model, big-endian SCRIPTS and table-indirect fetch (the part sits on the big-endian PA-RISC LASI bus), and 24-bit DMA byte counts.  Behaviour follows the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide (Oct 1990).

The base engine supports connected I/O and reselection-interrupt disconnect, enough to install and boot Debian off the 710 with the Linux lasi700/53c700 driver and to boot NetBSD/hppa 9.4 and 10.1 (osiop) with disk and CD-ROM enumeration.  Reselection follows the 53C700 family:
gated on the (re)selection interrupt enable (SIEN.SEL) alone, one-hot SDID, and an SFBR id bitmask in 700-compatibility mode.  SDTR/WDTR/PPR negotiation is declined with MESSAGE REJECT, which the 700-family drivers require to proceed: unlike the 8xx parts they leave the target to drive the phase after the reject.

The LASI glue (hw/scsi/lasi_ncr710.c) embeds one NCR710State by value. It gains the free_request SCSIBusInfo callback and request-queue init the core needs, renames the reselection timer to scripts_timer, and narrows the SCSI bus to the 8-bit inclusive maxima.

The migration format is incompatible with the model this replaces, so the vmstate version and minimum version are both raised.

---
hw/scsi/lasi_ncr710.c |  117 +-
hw/scsi/lasi_ncr710.h |   12 +-
hw/scsi/ncr53c710.c   | 2795 +++++++++++++++-------------------------- hw/scsi/ncr53c710.h   |  276 ++--
hw/scsi/trace-events  |   43 +-

5 files changed, 1191 insertions(+), 2052 deletions(-)

[...]

Quoted 461 lines
hw/scsi/ncr53c710.c +1 -2
index 57967e7a84..57cd7d91a4 100644
@@ -1,599 +1,308 @@
 /*
- * QEMU NCR710 SCSI Controller
+ * QEMU NCR 53C710 SCSI I/O Processor emulation
  *
- * Copyright (c) 2025 Soumyajyotii Ssarkar 

<[email protected]>
- * This driver was developed during the Google Summer of Code 2025 program.
+ * Copyright (c) 2026 Keith Monahan <[email protected]>
+ * Copyright (c) 2006 CodeSourcery (lsi53c895a.c, written by Paul Brook)
 *
- * NCR710 SCSI Controller implementation
- * Based on the NCR53C710 Technical Manual Version 3.2, December 2000
+ * A rewrite of the QEMU NCR 53C710 model.  The 53C710 is an early member of
+ * the NCR/LSI SCRIPTS processor family, so the SCRIPTS engine here is derived
+ * from QEMU's LSI53C895A model and adapted to the 53C710's register map,
+ * single byte interrupt model, big endian SCRIPTS fetch (PA-RISC) and 24 bit
+ * DMA, per the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide
+ * (Oct 1990).
 *
- * Developed from an implementation of NCR53C710 by Helge Deller
- * which was interim based on the implementation by Toni Wilen for UAE.
+ * Derived from hw/scsi/lsi53c895a.c (Copyright (c) 2006 CodeSourcery, written
+ * by Paul Brook).
 *
- * SPDX-License-Identifier: GPL-2.0-or-later
+ * Replaces the 53C710 model written for Google Summer of Code 2025 by
+ * Soumyajyotii Ssarkar <[email protected]>, itself based on
+ * earlier NCR 53C710 work by Helge Deller and by Toni Wilen (for UAE). *
- * Contents:
- * 1. Register Definitions
- * 2. Register name functions
- * 3. Parity functions
- * 4. SCSI FIFO Structures
- * 5. Scripts Misc functions
- * 6. DMA functions
- * 7. Scripts functions
- * 8. Read and Write functions
- * 9. QEMU Device model functions
+ * The only instantiated device is the LASI wrapper (lasi_ncr710.c), which
+ * embeds one NCR710State by value and drives it through this file. *
+ * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "qemu/osdep.h"

hw/scsi/ncr53c710.c +3 -2
-#include "qapi/error.h"
-#include "qemu/timer.h"
+#include "qemu/bitops.h"
+#include "qemu/log.h"
+#include "qemu/main-loop.h"

#include "hw/core/irq.h"

hw/scsi/ncr53c710.c +0 -2
-#include "hw/core/sysbus.h"
-#include "hw/scsi/scsi.h"

#include "hw/scsi/ncr53c710.h"

hw/scsi/ncr53c710.c +1 -4
-#include "migration/vmstate.h"
-#include "system/dma.h"
-#include "qemu/log.h"
-#include "qemu/module.h"
+#include "system/block-backend.h"

#include "trace.h"

hw/scsi/ncr53c710.c +35 -269
-#include "qom/object.h"
-
-#define NCR710_MAX_DEVS 7
-
-/* SCNTL0 (0x00) - SCSI Control Register 0 */
-#define NCR710_SCNTL0_TRG    0x01
-#define NCR710_SCNTL0_AAP    0x02
-#define NCR710_SCNTL0_EPG    0x04
-#define NCR710_SCNTL0_EPC    0x08
-#define NCR710_SCNTL0_WATN   0x10
-#define NCR710_SCNTL0_START  0x20
-#define NCR710_SCNTL0_ARB0   0x40
-#define NCR710_SCNTL0_ARB1   0x80
-
-/* SCNTL1 (0x01) - SCSI Control Register 1 */
-#define NCR710_SCNTL1_RES0   0x01
-#define NCR710_SCNTL1_RES1   0x02
-#define NCR710_SCNTL1_AESP   0x04
-#define NCR710_SCNTL1_RST    0x08
-#define NCR710_SCNTL1_CON    0x10
-#define NCR710_SCNTL1_ESR    0x20
-#define NCR710_SCNTL1_ADB    0x40
-#define NCR710_SCNTL1_EXC    0x80
-
-/* ISTAT (0x21) - Interrupt Status Register */
-#define NCR710_ISTAT_DIP    0x01
-#define NCR710_ISTAT_SIP    0x02
-#define NCR710_ISTAT_CON    0x08
-#define NCR710_ISTAT_SIGP   0x20
-#define NCR710_ISTAT_RST    0x40
-#define NCR710_ISTAT_ABRT   0x80
-
-/* SSTAT0 (0x0D) - SCSI Status Register 0 */
-#define NCR710_SSTAT0_PAR    0x01
-#define NCR710_SSTAT0_RST    0x02
-#define NCR710_SSTAT0_UDC    0x04
-#define NCR710_SSTAT0_SGE    0x08
-#define NCR710_SSTAT0_SEL    0x10
-#define NCR710_SSTAT0_STO    0x20
-#define NCR710_SSTAT0_FCMP   0x40
-#define NCR710_SSTAT0_MA     0x80
-
-/* SSTAT1 (0x0E) - SCSI Status Register 1 */
-#define NCR710_SSTAT1_ORF    0x02
-#define NCR710_SSTAT1_ILF    0x04
-
-/* SSTAT2 (0x0F) - SCSI Status Register 2 */
-#define NCR710_SSTAT2_FF0    0x01
-#define NCR710_SSTAT2_FF1    0x02
-#define NCR710_SSTAT2_FF2    0x04
-#define NCR710_SSTAT2_FF3    0x08
-
-/* SOCL (0x07) / SBCL (0x0B) - SCSI Output/Bus Control Lines */
-#define NCR710_SOCL_IO       0x01
-#define NCR710_SOCL_CD       0x02
-#define NCR710_SOCL_MSG      0x04
-#define NCR710_SOCL_ATN      0x08
-#define NCR710_SOCL_SEL      0x10
-#define NCR710_SOCL_BSY      0x20
-#define NCR710_SOCL_ACK      0x40
-#define NCR710_SOCL_REQ      0x80
-
-/* SBCL bits same as SOCL */
-#define NCR710_SBCL_IO       0x01
-#define NCR710_SBCL_CD       0x02
-#define NCR710_SBCL_MSG      0x04
-#define NCR710_SBCL_ATN      0x08
-#define NCR710_SBCL_SEL      0x10
-#define NCR710_SBCL_BSY      0x20
-#define NCR710_SBCL_ACK      0x40
-#define NCR710_SBCL_REQ      0x80
-
-/* DSTAT (0x0C) - DMA Status Register */
-#define NCR710_DSTAT_IID     0x01
-#define NCR710_DSTAT_SIR     0x04
-#define NCR710_DSTAT_SSI     0x08
-#define NCR710_DSTAT_ABRT    0x10
-#define NCR710_DSTAT_BF      0x20
-#define NCR710_DSTAT_MDPE    0x40
-#define NCR710_DSTAT_DFE     0x80
-
-/* DCNTL (0x3B) - DMA Control Register */
-#define NCR710_DCNTL_COM     0x01
-#define NCR710_DCNTL_IRQD    0x02
-#define NCR710_DCNTL_STD     0x04
-#define NCR710_DCNTL_IRQM    0x08
-#define NCR710_DCNTL_SSM     0x10
-#define NCR710_DCNTL_PFEN    0x20
-#define NCR710_DCNTL_PFF     0x40
-
-/* DMODE (0x38) - DMA Mode Register */
-#define NCR710_DMODE_MAN     0x01
-#define NCR710_DMODE_BOF     0x02
-#define NCR710_DMODE_ERMP    0x04
-#define NCR710_DMODE_ERL     0x08
-#define NCR710_DMODE_DIOM    0x10
-#define NCR710_DMODE_SIOM    0x20
-#define NCR710_DMODE_BL_MASK 0xC0
-#define NCR710_DMODE_BL_1    0x00
-#define NCR710_DMODE_BL_2    0x40
-#define NCR710_DMODE_BL_4    0x80
-#define NCR710_DMODE_BL_8    0xC0
-
-/* CTEST2 (0x16) - Chip Test Register 2 */
-#define NCR710_CTEST2_DACK   0x01
-#define NCR710_CTEST2_DREQ   0x02
-#define NCR710_CTEST2_TEOP   0x04
-#define NCR710_CTEST2_PCICIE 0x08
-#define NCR710_CTEST2_CM     0x10
-#define NCR710_CTEST2_CIO    0x20
-#define NCR710_CTEST2_SIGP   0x40
-#define NCR710_CTEST2_DDIR   0x80
-
-/* CTEST5 (0x19) - Chip Test Register 5 */
-#define NCR710_CTEST5_BL2    0x04
-#define NCR710_CTEST5_DDIR   0x08
-#define NCR710_CTEST5_MASR   0x10
-#define NCR710_CTEST5_DFSN   0x20
-#define NCR710_CTEST5_BBCK   0x40
-#define NCR710_CTEST5_ADCK   0x80
-
-/* SCID (0x04) - SCSI Chip ID Register */
-#define NCR710_SCID_RRE      0x60
-#define NCR710_SCID_ID_MASK  0x07
-
-#define NCR710_HOST_ID       7
-
-/* NCR53C710 has 8-byte SCSI FIFO */
-#define NCR710_MAX_MSGIN_LEN 8
-#define NCR710_BUF_SIZE         4096
-
-/* Standard SCSI Message Byte Constants */
-#define SCSI_MSG_ABORT                      0x06
-#define SCSI_MSG_BUS_DEVICE_RESET           0x0c
-#define SCSI_MSG_COMMAND_COMPLETE           0x00
-#define SCSI_MSG_DISCONNECT                 0x04
-#define SCSI_MSG_EXTENDED_MESSAGE           0x01
-#define SCSI_MSG_IDENTIFY                   0x80
-#define SCSI_MSG_IGNORE_WIDE_RESIDUE        0x23
-#define SCSI_MSG_MESSAGE_PARITY_ERROR       0x09
-#define SCSI_MSG_MESSAGE_REJECT             0x07
-#define SCSI_MSG_NO_OPERATION               0x08
-#define SCSI_MSG_RELEASE_RECOVERY           0x10
-#define SCSI_MSG_RESTORE_POINTERS           0x03
-#define SCSI_MSG_SAVE_DATA_POINTER          0x02
-#define SCSI_MSG_SYNCHRONOUS_DATA_TRANSFER  0x01
-#define SCSI_MSG_WIDE_DATA_TRANSFER         0x03
-
-/* Script interrupt codes */
-#define A_GOOD_STATUS_AFTER_STATUS          0x401
-#define A_DISCONNECT_AFTER_CMD              0x380
-#define A_DISCONNECT_AFTER_DATA             0x580
-#define A_DISCONNECT_DURING_DATA            0x780
-#define A_RESELECTION_IDENTIFIED            0x1003
-#define A_UNEXPECTED_PHASE                  0x20
-#define A_FATAL                             0x2000
-#define A_DEBUG_INTERRUPT                   0x3000
-
-/* SCSI Script execution states */
-#define SCRIPT_STATE_IDLE                  0
-#define SCRIPT_STATE_SELECTING             1
-#define SCRIPT_STATE_COMMAND               2
-#define SCRIPT_STATE_DATA                  3
-#define SCRIPT_STATE_STATUS                4
-#define SCRIPT_STATE_MESSAGE               5
-#define SCRIPT_STATE_DISCONNECTED          6
-
-#define AFTER_SELECTION         0x100
-#define BEFORE_CMD              0x200
-#define AFTER_CMD               0x300
-#define AFTER_STATUS            0x400
-#define AFTER_DATA_IN           0x500
-#define AFTER_DATA_OUT          0x600
-#define DURING_DATA_IN          0x700
-
-#define NOT_MSG_OUT             0x10
-#define UNEXPECTED_PHASE        0x20
-#define NOT_MSG_IN              0x30
-#define UNEXPECTED_MSG          0x40
-#define MSG_IN                  0x50
-#define SDTR_MSG_R              0x60
-#define REJECT_MSG_R            0x70
-#define DISCONNECT              0x80
-#define MSG_OUT                 0x90
-#define WDTR_MSG_R              0xA0
-
-#define GOOD_STATUS             0x1
-
-#define NOT_MSG_OUT_AFTER_SELECTION         0x110
-#define UNEXPECTED_PHASE_BEFORE_CMD         0x220
-#define UNEXPECTED_PHASE_AFTER_CMD          0x320
-#define NOT_MSG_IN_AFTER_STATUS             0x430
-#define GOOD_STATUS_AFTER_STATUS            0x401
-#define UNEXPECTED_PHASE_AFTER_DATA_IN      0x520
-#define UNEXPECTED_PHASE_AFTER_DATA_OUT     0x620
-#define UNEXPECTED_MSG_BEFORE_CMD           0x240
-#define MSG_IN_BEFORE_CMD                   0x250
-#define MSG_IN_AFTER_CMD                    0x350
-#define SDTR_MSG_BEFORE_CMD                 0x260
-#define REJECT_MSG_BEFORE_CMD               0x270
-#define DISCONNECT_AFTER_CMD                0x380
-#define SDTR_MSG_AFTER_CMD                  0x360
-#define WDTR_MSG_AFTER_CMD                  0x3A0
-#define MSG_IN_AFTER_STATUS                 0x440
-#define DISCONNECT_AFTER_DATA               0x580
-#define MSG_IN_AFTER_DATA_IN                0x550
-#define MSG_IN_AFTER_DATA_OUT               0x650
-#define MSG_OUT_AFTER_DATA_IN               0x590
-#define DATA_IN_AFTER_DATA_IN               0x5a0
-#define MSG_IN_DURING_DATA_IN               0x750
-#define DISCONNECT_DURING_DATA              0x780
-
-#define RESELECTED_DURING_SELECTION      0x1000
-#define COMPLETED_SELECTION_AS_TARGET    0x1001
-#define RESELECTION_IDENTIFIED           0x1003
-
-#define FATAL                            0x2000
-#define FATAL_UNEXPECTED_RESELECTION_MSG 0x2000
-#define FATAL_SEND_MSG                   0x2001
-#define FATAL_NOT_MSG_IN_AFTER_SELECTION 0x2002
-#define FATAL_ILLEGAL_MSG_LENGTH         0x2003
-
-#define DEBUG_INTERRUPT     0x3000
-#define DEBUG_INTERRUPT1    0x3001
-#define DEBUG_INTERRUPT2    0x3002
-#define DEBUG_INTERRUPT3    0x3003
-#define DEBUG_INTERRUPT4    0x3004
-#define DEBUG_INTERRUPT5    0x3005
-#define DEBUG_INTERRUPT6    0x3006
-
-#define COMMAND_COMPLETE_MSG      0x00
-#define EXTENDED_MSG              0x01
-#define SDTR_MSG                  0x01
-#define SAVE_DATA_PTRS_MSG        0x02
-#define RESTORE_DATA_PTRS_MSG     0x03
-#define WDTR_MSG                  0x03
-#define DISCONNECT_MSG            0x04
-#define REJECT_MSG                0x07
-#define PARITY_ERROR_MSG          0x09
-#define SIMPLE_TAG_MSG            0x20
-#define IDENTIFY_MSG              0x80
-#define IDENTIFY_MSG_MASK         0x7F
-#define TWO_BYTE_MSG              0x20
-#define TWO_BYTE_MSG_MASK         0x0F
-
-/* SCSI phases */
-#define PHASE_DO   0  /* Data out phase */
-#define PHASE_DI   1  /* Data in phase */
-#define PHASE_CO   2  /* Command phase */
-#define PHASE_SI   3  /* Status phase */
-#define PHASE_ST   3  /* Status phase (alias) */
-#define PHASE_MO   6  /* Message out phase */
-#define PHASE_MI   7  /* Message in phase */
-#define PHASE_MASK 7  /* Mask for phase bits */
-
-#define NCR710_TAG_VALID     (1 << 16)
-
-static void ncr710_scsi_fifo_init(NCR710_SCSI_FIFO *fifo);
-static const char *ncr710_reg_name(int offset);
-static void ncr710_script_scsi_interrupt(NCR710State *s, int stat0);
-static void ncr710_update_irq(NCR710State *s);
-static void ncr710_script_dma_interrupt(NCR710State *s, int stat);
-static void ncr710_request_free(NCR710State *s, NCR710Request *p);
-static inline void ncr710_dma_read(NCR710State *s, uint32_t addr,
-                                   void *buf, uint32_t len);
-static inline void ncr710_dma_write(NCR710State *s, uint32_t addr,
-                                    const void *buf, uint32_t len);
-static uint8_t ncr710_reg_readb(NCR710State *s, int offset);
-static void ncr710_reg_writeb(NCR710State *s, int offset, uint8_t val);
 
+/* SCNTL0 (0x00) */
+#define NCR710_SCNTL0_TRG       0x01
+#define NCR710_SCNTL0_AAP       0x02
+#define NCR710_SCNTL0_EPG       0x04
+#define NCR710_SCNTL0_EPC       0x08
+#define NCR710_SCNTL0_WATN      0x10
+#define NCR710_SCNTL0_START     0x20
+/* ARB1|ARB0 = 0xc0 (full arbitration) at reset */
+
+/* SCNTL1 (0x01) */
+#define NCR710_SCNTL1_AESP      0x04
+#define NCR710_SCNTL1_RST       0x08
+#define NCR710_SCNTL1_CON       0x10
+#define NCR710_SCNTL1_ESR       0x20
+#define NCR710_SCNTL1_ADB       0x40
+#define NCR710_SCNTL1_EXC       0x80
+
+/* SSTAT0 (0x0d) and SIEN (0x03) share this bit layout. */
+#define NCR710_STAT0_PAR        0x01
+#define NCR710_STAT0_RST        0x02
+#define NCR710_STAT0_UDC        0x04
+#define NCR710_STAT0_SGE        0x08
+#define NCR710_STAT0_SEL        0x10
+#define NCR710_STAT0_STO        0x20
+#define NCR710_STAT0_FCMP       0x40
+#define NCR710_STAT0_MA         0x80
+
+/* DSTAT (0x0c) and DIEN (0x39) share this bit layout. */
+#define NCR710_DSTAT_IID        0x01
+#define NCR710_DSTAT_WTD        0x02
+#define NCR710_DSTAT_SIR        0x04
+#define NCR710_DSTAT_SSI        0x08
+#define NCR710_DSTAT_ABRT       0x10
+#define NCR710_DSTAT_BF         0x20
+#define NCR710_DSTAT_DFE        0x80   /* pure status; never raises an 

IRQ */

hw/scsi/ncr53c710.c +10 -0
+
+/* ISTAT (0x21) */
+#define NCR710_ISTAT_DIP        0x01
+#define NCR710_ISTAT_SIP        0x02
+#define NCR710_ISTAT_CON        0x08
+#define NCR710_ISTAT_SIGP       0x20
+#define NCR710_ISTAT_RST        0x40
+#define NCR710_ISTAT_ABRT       0x80
+
+/* SBCL (0x0b): low 3 bits mirror the SCSI phase, upper bits are bus 

signals. */

hw/scsi/ncr53c710.c +25 -0
+#define NCR710_SBCL_ATN         0x08
+#define NCR710_SBCL_SEL         0x10
+#define NCR710_SBCL_BSY         0x20
+#define NCR710_SBCL_ACK         0x40
+#define NCR710_SBCL_REQ         0x80
+
+/* SOCL (0x07) */
+#define NCR710_SOCL_ATN         0x08
+
+/* CTEST2 (0x16) */
+#define NCR710_CTEST2_DACK      0x01
+#define NCR710_CTEST2_SIGP      0x40
+
+/* DMODE (0x38) */
+#define NCR710_DMODE_MAN        0x01
+
+/* DCNTL (0x3b) */
+#define NCR710_DCNTL_COM        0x01
+#define NCR710_DCNTL_FA         0x02
+#define NCR710_DCNTL_STD        0x04
+#define NCR710_DCNTL_LLM        0x08
+#define NCR710_DCNTL_SSM        0x10
+#define NCR710_DCNTL_EA         0x20
+
+/* SCSI phase (MSG,C/D,I/O); matches SSTAT2[2:0]/SBCL[2:0]/ 

DCMD[26:24]. */

hw/scsi/ncr53c710.c +12 -0
+#define PHASE_DO        0
+#define PHASE_DI        1
+#define PHASE_CMD       2
+#define PHASE_ST        3
+#define PHASE_MO        6
+#define PHASE_MI        7
+#define PHASE_MASK      7
+
+/* Flag bit OR'd into select_tag/tag when a queue tag is valid. */
+#define NCR710_TAG_VALID    (1 << 16)
+
+/* Maximum SCRIPTS instructions to process before yielding to the CPU. 

*/

hw/scsi/ncr53c710.c +9 -0
+#define NCR710_MAX_INSN     500
+
+#define NCR710_BUF_SIZE     4096
+
+/* waiting state machine (mirrors lsi53c895a). */
+enum {
+    NCR710_NOWAIT = 0,        /* SCRIPTS running or stopped */
+    NCR710_WAIT_RESELECT,     /* Wait Reselect instruction issued */
+    NCR710_DMA_SCRIPTS,       /* DMA invoked from within execute_script 

*/
+    NCR710_DMA_IN_PROGRESS,   /* asynchronous DMA in progress */ +    NCR710_WAIT_SCRIPTS,      /* stopped on the instruction count limit */

hw/scsi/ncr53c710.c +14 -1
+};
 
-static inline int ncr710_irq_on_rsl(NCR710State *s)
+enum {
+    NCR710_MSG_ACTION_COMMAND = 0,
+    NCR710_MSG_ACTION_DISCONNECT = 1,
+    NCR710_MSG_ACTION_DOUT = 2,
+    NCR710_MSG_ACTION_DIN = 3,
+};
+
+struct NCR710Request {
+    SCSIRequest *req;
+    uint32_t tag;
+    uint32_t dma_len;
+    uint8_t *dma_buf;
+    uint32_t pending;     /* bytes the SCSI layer has ready while 
Quoted 6 lines

queued */
+    int out;              /* nonzero if the queued transfer is DATA OUT */
+    bool orphan;
+    QTAILQ_ENTRY(NCR710Request) next;
+};

Quoted 7 lines

This struct is exactly the same as lsi_request in lsi53c895a.c. As a start this and helper functions working on it should be split off in a shared file and header from lsi53c895a.c and reused here. That needs changing lsi53c895a.c but it's trivial changes easy to review and does not break it. That with more small chnages split off as suggested by Peter should reduce the size of this patch to a more managable size so we can see what else could be shared.

Quoted 6 lines

I fully understand and generally support the wish to merge and share things in the hope for easier review and possible maintanance reduction.

But even in the Linux kernel those drivers don't share code. Will we really gain much if we try to split the existing working drivers and try share code between those?

Yes, reduced duplicated code and avoiding divergence with bugs only introduced or fixed in one of them in the future. IMO that's worth some restructuring.

Quoted 3 lines

I mean, the 710 and 89x chips are quite different, and so will be the code.

In this specific case IMHO it's better to keep them seperate.

I'm not asking for merging the two models and rewrite the 53c895a much but trivially shareable code such as request queue handling that seems to be just reused here should not be duplicated and this seems to be easily split from exsting code and reused here.

I'm not the maintainer so whatever the one responsible here prefers but I think trying to split this patch up and share some easily shared code is reasonable to ask to avoid adding duplicated code. Then the rest where these are really different can be separate without sharing more code but currently there are a lot of defines, types and some functions duplicated in this patch that could be split off and trivially shared.

Regards,
BALATON Zoltan

#19 lore raw

PATCH v2 4/4 hw/scsi/ncr53c710: support HP-UX overlapped tagged commands

In reply to Keith Monahan

The HP-UX 10.20 c720 driver overlaps tagged commands and recovers a disconnected command by parking the SCRIPTS engine on a Wait Reselect instruction with the reselection interrupt disabled (SIEN.SEL clear), which the base engine cannot service. Add the target-initiated disconnect/reselect and asynchronous completion path it needs.

A tagged command whose data is not yet ready is disconnected and queued (only under a Wait Reselect driver; untagged transfers and reselection-interrupt drivers keep the connected path), and reselected once its data is ready and the engine is parked at Wait Reselect. Completions are delivered asynchronously through a bottom half rather than reentrantly inside the interpreter, and the interpreter guards the reentrancy class behind CVE-2023-0330.

The c720 driver also issues its "start next command" SIGP kick with CPU interrupts masked and then spins, 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), and a SIGP kick that lands on a stopped engine is held and replayed once it parks.

The HP-UX 10.20 install kernel is also nonconformant in negotiation: its self-modifying SCRIPTS reads a second message byte after a MESSAGE REJECT and hangs unless it is SAVE DATA POINTERS, while NetBSD's osiop resets the bus if it sees that byte. The two are told apart by ATN state as the reject is consumed (HP-UX has deasserted ATN, a conformant initiator has not), so the extra byte is appended only for HP-UX.

With this, HP-UX 10.20 installs and reboots into the installed system on both the Ignite (16700A) media and a software install that overlaps tagged commands.

---
 hw/scsi/ncr53c710.c | 295 +++++++++++++++++++++++++++++++++++++++++---
 hw/scsi/ncr53c710.h |  19 +++
 2 files changed, 295 insertions(+), 19 deletions(-)
hw/scsi/ncr53c710.c +276 -19
index 57cd7d91a4..1dcc111cc9 100644
@@ -119,6 +119,14 @@
 
 #define NCR710_BUF_SIZE     4096
 
+/*
+ * Safety bound on the synchronous completion drain loop in ncr710_reg_write().
+ * A real transfer needs only a handful of iterations; this cap prevents an
+ * unbounded spin if a drain ever fails to progress (it then falls back to
+ * asynchronous completion).
+ */
+#define NCR710_DRAIN_MAX_ITERS  100000
+
 /* waiting state machine (mirrors lsi53c895a). */
 enum {
     NCR710_NOWAIT = 0,        /* SCRIPTS running or stopped */
@@ -167,6 +175,7 @@ static const char *ncr710_reg_name(int offset)
 static uint8_t ncr710_reg_readb(NCR710State *s, int offset);
 static void ncr710_reg_writeb(NCR710State *s, int offset, uint8_t val);
 static void ncr710_execute_script(NCR710State *s);
+static void ncr710_issue_bh(void *opaque);
 static void ncr710_set_phase(NCR710State *s, int phase);
 
 /*
@@ -225,6 +234,14 @@ void ncr710_soft_reset(NCR710State *s)
     s->select_tag = 0;
     s->command_complete = 0;
     s->script_running = false;
+    s->sigp_pending_resume = false;
+    /*
+     * Do not reset reentrancy_level here.  A SCRIPTS driven soft reset (a guest
+     * write of ISTAT.RST from a register move) can run while execute_script
+     * frames are still on the stack; those frames decrement the counter as they
+     * unwind, so zeroing it would underflow negative and defeat the reentrancy
+     * guard.  It is zero at device init and balances itself to zero.
+     */
 
     s->scntl0 = 0xc0;       /* full arbitration */
     s->scntl1 = 0;
@@ -371,6 +388,21 @@ static int ncr710_bad_phase(NCR710State *s, int new_phase)
 static void ncr710_resume_script(NCR710State *s)
 {
     trace_ncr710_resume_script(s->waiting);
+    if (s->reentrancy_level > 0) {
+        /*
+         * A completion fired synchronously while the SCRIPTS interpreter is
+         * already on the stack, e.g. inline GOOD status for a no data
+         * command (TEST UNIT READY, START STOP UNIT, ...) completing inside
+         * scsi_req_enqueue().  Reentering ncr710_execute_script() here would
+         * advance s->dsp underneath the outer interpreter frame and can
+         * double execute SCRIPTS (the reentrancy class behind CVE-2023-0330
+         * in lsi53c895a).  Do not recurse: release the wait so the outer
+         * loop continues to the next instruction, which the completion has
+         * already armed (PHASE_ST, via ncr710_command_complete()).
+         */
+        s->waiting = NCR710_NOWAIT;
+        return;
+    }
     if (s->waiting != NCR710_DMA_SCRIPTS) {
         s->waiting = NCR710_NOWAIT;
         ncr710_execute_script(s);
@@ -394,6 +426,26 @@ static void ncr710_bad_selection(NCR710State *s, uint32_t id)
     ncr710_disconnect(s);
 }
 
+/*
+ * Deferred half of ncr710_do_dma()'s SCSI continue: runs on a clean stack so a
+ * cached backend's completion arrives asynchronously (reentrancy_level 0)
+ * instead of reentrantly inside the SCRIPTS interpreter.  Guarded against an
+ * orphaned/cancelled request like ncr710_reselect_bh.
+ */
+static void ncr710_issue_bh(void *opaque)
+{
+    NCR710State *s = opaque;
+
+    if (!s->issue_pending) {
+        return;
+    }
+    s->issue_pending = false;
+    if (s->current == NULL || s->current->orphan) {
+        return;
+    }
+    scsi_req_continue(s->current->req);
+}
+
 static void ncr710_do_dma(NCR710State *s, int out)
 {
     uint32_t count;
@@ -436,7 +488,30 @@ static void ncr710_do_dma(NCR710State *s, int out)
     p->dma_len -= count;
     if (p->dma_len == 0) {
         p->dma_buf = NULL;
-        scsi_req_continue(req);
+        /*
+         * Defer the completing continue (Block Move satisfied, dbc == 0) of a
+         * tagged disconnect/reselect transfer to a bottom half: its
+         * scsi_req_continue can trigger a cached backend (CD-ROM) completion
+         * reentrantly inside execute_script, collapsing the disconnect and
+         * completion INT into the guest MMIO write driving the engine.  That
+         * lets the HP-UX c720 driver's next-command SIGP kick race ahead of its
+         * completion ISR and wedge the install.  Deferring makes completion an
+         * autonomous async event, like the disk-backed path;
+         * the engine parks at DMA_IN_PROGRESS (set by the caller) meanwhile.
+         */
+        if ((p->tag & NCR710_TAG_VALID) && s->dbc == 0) {
+            s->issue_pending = true;
+            aio_bh_schedule_oneshot(qemu_get_aio_context(), ncr710_issue_bh, s);
+        } else {
+            /*
+             * Complete inline for untagged transfers (the connected, polled
+             * early-boot and PDC path: SeaBIOS, ISL, pre-driver probe) and
+             * mid-transfer continues (dbc > 0) that fetch more data without
+             * completing the command (deferring every chunk would stall large
+             * multi-chunk reads).
+             */
+            scsi_req_continue(req);
+        }
     } else {
         p->dma_buf += count;
         ncr710_resume_script(s);
@@ -478,6 +553,20 @@ static NCR710Request *ncr710_get_pending_req(NCR710State *s)
     return NULL;
 }
 
+/* Move the in-flight command to the disconnected queue (target disconnect). */
+static void ncr710_queue_command(NCR710State *s)
+{
+    NCR710Request *p = s->current;
+
+    trace_ncr710_queue_command(p->tag);
+    assert(s->current != NULL);
+    assert(s->current->dma_len == 0);
+    QTAILQ_INSERT_TAIL(&s->queue, s->current, next);
+    s->current = NULL;
+    p->pending = 0;
+    p->out = (s->sstat2 & PHASE_MASK) == PHASE_DO;
+}
+
 /* Reselect a disconnected command to resume (continue) its data transfer. */
 static void ncr710_reselect(NCR710State *s, NCR710Request *p)
 {
@@ -528,20 +617,51 @@ static int ncr710_queue_req(NCR710State *s, SCSIRequest *req, uint32_t len)
     return 1;
 }
 
-/* SCRIPTS Wait Reselect: reconnect a ready queued command, else park. */
-static void ncr710_wait_reselect(NCR710State *s)
+/*
+ * Deferred half of ncr710_wait_reselect(): reconnect a disconnected command
+ * whose data is ready and resume the engine, on a clean (bottom half) stack.
+ * Guarded so it does nothing if a guest SIGP wake or an inline transfer_data
+ * reselection moved the engine on while this was pending.
+ */
+static void ncr710_reselect_bh(void *opaque)
 {
+    NCR710State *s = opaque;
     NCR710Request *p;
 
-    if (s->current) {
+    if (s->current != NULL || s->waiting != NCR710_WAIT_RESELECT) {
         return;
     }
     p = ncr710_get_pending_req(s);
-    if (p) {
-        ncr710_reselect(s, p);
+    if (p == NULL) {
+        return;
     }
-    if (s->current == NULL) {
-        s->waiting = NCR710_WAIT_RESELECT;
+    ncr710_reselect(s, p);
+    s->waiting = NCR710_NOWAIT;
+    ncr710_execute_script(s);
+}
+
+/* SCRIPTS Wait Reselect: reconnect a ready queued command, else park. */
+static void ncr710_wait_reselect(NCR710State *s)
+{
+    if (s->current) {
+        return;
+    }
+    /*
+     * A disconnected command whose data is ready must reconnect to resume its
+     * transfer.  Reselecting synchronously here, inside the guest register
+     * write that drove the engine to this Wait Reselect, would deliver the
+     * command's completion interrupt reentrantly in the middle of driver flow,
+     * racing the HP-UX 10.20 c720 driver's completion bookkeeping and dropping
+     * a biodone/wakeup (the install then sleeps forever; the model idles
+     * with the queue empty).  Defer the reconnect to a bottom half so it
+     * lands on a clean stack after the register write returns, as a real target
+     * initiated reselection would arrive.  The engine parks meanwhile; if more
+     * data arrives first, transfer_data's own reselection (or a SIGP wake)
+     * handles it and the bottom half does nothing.
+     */
+    s->waiting = NCR710_WAIT_RESELECT;
+    if (ncr710_get_pending_req(s)) {
+        aio_bh_schedule_oneshot(qemu_get_aio_context(), ncr710_reselect_bh, s);
     }
 }
 
@@ -667,7 +787,36 @@ static void ncr710_do_command(NCR710State *s)
     }
     if (!s->command_complete) {
         if (n) {
-            /* Stay connected; the block move waits for transfer_data. */
+            if ((s->current->tag & NCR710_TAG_VALID) &&
+                !ncr710_irq_on_rsl(s)) {
+                /*
+                 * Tagged command whose data is not yet ready (typically a read
+                 * whose media access has not completed), issued by a driver
+                 * that recovers a disconnected command by parking the SCRIPTS
+                 * engine on a Wait Reselect instruction (SIEN.SEL clear, e.g.
+                 * HP-UX). Disconnect and queue it so the bus is free for
+                 * further tagged commands; ncr710_wait_reselect() reselects it
+                 * once its data is ready.
+                 *
+                 * Do not disconnect for:
+                 *   - untagged transfers (SeaBIOS/PDC firmware, simple
+                 *     drivers), which have no Wait Reselect handler; and
+                 *   - reselection-interrupt drivers (SIEN.SEL set, e.g. Linux's
+                 *     53c700), which expect a target-reselect interrupt with
+                 *     the 700-family reselection handshake rather than a script
+                 *     park.
+                 * Both keep the connected behaviour and complete inline.
+                 */
+                ncr710_add_msg_byte(s, 2);   /* SAVE DATA POINTER */
+                ncr710_add_msg_byte(s, 4);   /* DISCONNECT */
+                ncr710_set_phase(s, PHASE_MI);
+                s->msg_action = NCR710_MSG_ACTION_DISCONNECT;
+                ncr710_queue_command(s);
+            }
+            /*
+             * Otherwise stay connected and let the block move wait for
+             * transfer_data; no disconnect is fabricated (see policy above).
+             */
         } else {
             /*
              * Async no data command (e.g. SYNCHRONIZE CACHE from Linux
@@ -741,6 +890,17 @@ static void ncr710_do_msgin(NCR710State *s)
     if (!s->msg_len) {
         switch (s->msg_action) {
         case NCR710_MSG_ACTION_COMMAND:
+            /*
+             * A MESSAGE REJECT declining negotiation was just consumed.
+             * The HP-UX install template reads it with ATN deasserted and
+             * requires a trailing SAVE DATA POINTERS (0x02); a conformant
+             * initiator (NetBSD, Linux) reads it with ATN still asserted and
+             * wants no second byte.  See ncr710_do_msgout for the
+             * discriminator.
+             */
+            if (s->sfbr == 7 && !(s->socl & NCR710_SOCL_ATN)) {
+                ncr710_add_msg_byte(s, 2);   /* SAVE DATA POINTERS (HP-UX) */
+            }
             /*
              * Stay in MSG IN: SSTAT2 latches the last REQ phase, so the driver
              * samples MSG IN at the script interrupt and the lazy MSG IN ->
@@ -899,14 +1059,13 @@ static void ncr710_execute_script(NCR710State *s)
     uint32_t addr;
     int opcode;
     int insn_processed = 0;
-    static int reentrancy_level;
 
     if (s->waiting == NCR710_WAIT_SCRIPTS) {
         timer_del(s->scripts_timer);
         s->waiting = NCR710_NOWAIT;
     }
 
-    reentrancy_level++;
+    s->reentrancy_level++;
     s->script_running = true;
 again:
     /*
@@ -914,11 +1073,11 @@ again:
      * waiting on a memory change make progress), and guard against a script
      * that retriggers itself (CVE-2023-0330) via the reentrancy counter.
      */
-    if (++insn_processed > NCR710_MAX_INSN || reentrancy_level > 8) {
+    if (++insn_processed > NCR710_MAX_INSN || s->reentrancy_level > 8) {
         trace_ncr710_script_yield(insn_processed);
         s->waiting = NCR710_WAIT_SCRIPTS;
         ncr710_scripts_timer_start(s);
-        reentrancy_level--;
+        s->reentrancy_level--;
         return;
     }
 
@@ -1065,11 +1224,23 @@ again:
                 break;
             case 2: /* Wait Reselect */
                 /*
-                 * A SIGP "start next command" kick jumps to the dispatch
-                 * address latched in DNAD; otherwise reconnect a ready queued
-                 * command, or park.
+                 * Two things release a Wait Reselect: the driver's SIGP "start
+                 * next command" kick (jump to the dispatch address latched in
+                 * DNAD), or a disconnected tagged command becoming ready to
+                 * reselect.  A SIGP that arrived while the engine was halted at
+                 * a completion INT is held in sigp_pending_resume and consumed
+                 * here.  Otherwise reconnect a ready queued command, or park.
                  */
-                if (s->istat & NCR710_ISTAT_SIGP) {
+                if ((s->istat & NCR710_ISTAT_SIGP) || s->sigp_pending_resume) {
+                    /*
+                     * The SIGP divert is a one-shot: clear the latch as it is
+                     * consumed so a subsequent Wait Reselect does not re-fire
+                     * on a stale SIGP, and so SIGP is never left set across a
+                     * real reselection (Jun92 p.53: an active SIGP at
+                     * reselection disables target-mode auto-switching).
+                     */
+                    s->istat &= ~NCR710_ISTAT_SIGP;
+                    s->sigp_pending_resume = false;
                     s->dsp = s->dnad;
                 } else if (!ncr710_irq_on_rsl(s)) {
                     ncr710_wait_reselect(s);
@@ -1266,7 +1437,7 @@ again:
         }
     }
 
-    reentrancy_level--;
+    s->reentrancy_level--;
 }
 
 #define CASE_GET_REG24(name, addr) \
@@ -1528,6 +1699,52 @@ static void ncr710_reg_writeb(NCR710State *s, int offset, uint8_t val)
         if (val & NCR710_ISTAT_ABRT) {
             ncr710_script_dma_interrupt(s, NCR710_DSTAT_ABRT);
         }
+        if (s->waiting == NCR710_WAIT_RESELECT && (val & NCR710_ISTAT_SIGP)) {
+            /*
+             * SIGP "start next command" wake of a parked Wait Reselect.  Run
+             * the engine synchronously: the Wait Reselect opcode, on SIGP,
+             * jumps to the DNAD dispatch and SELECTs the next command (Jun92
+             * p.69 step 3).  This SELECT must take priority over any
+             * disconnected command pending reselection: on real silicon the
+             * SIGP jump wins and the pending target reselects later, when the
+             * engine is next parked.  Deferring this to a bottom half let
+             * ncr710_reselect_bh win the race and reconnect a pending command
+             * instead of selecting the new one, stranding the driver's
+             * start-next kick (it polls, sees no progress, and resets the bus).
+             * qemu_set_irq only raises the IRQ line here; the guest takes it at
+             * the next instruction boundary, after this store returns, so this
+             * is not a reentrant interrupt delivery.
+             */
+            trace_ncr710_awoken();
+            s->waiting = NCR710_NOWAIT;
+            s->dsp = s->dnad;
+            ncr710_execute_script(s);
+        } else if ((val & NCR710_ISTAT_SIGP) && !s->script_running &&
+                   s->waiting == NCR710_NOWAIT) {
+            /*
+             * The driver's "start next command" SIGP kick raced ahead of the
+             * prior command's completion ISR: the engine is halted (script
+             * stopped, not parked at Wait Reselect) on a just completed
+             * command, either at its completion INT or in the brief window
+             * after the ISR has read cleared DSTAT but before it restarts the
+             * engine.  The wake above is skipped, so the kick only latches
+             * SIGP.  On real silicon the completion ISR runs before this lower
+             * priority kick, so the kick always lands on a parked Wait
+             * Reselect; under TCG it can land in the completion window.  The
+             * driver then polls the engine, sees no progress (SIGP still
+             * latched, DCMD/bus unchanged) and resets the SCSI bus, destroying
+             * the in flight command and hanging the install.
+             *
+             * Present "kick consumed" to that poll without disturbing any
+             * pending completion: clear the SIGP latch, exactly as a real Wait
+             * Reselect consumption would.  DSTAT/DSPS/DIP and the asserted IRQ
+             * are untouched, so the real completion ISR still services the
+             * command. Remember the kick so the next command is selected once
+             * the engine parks again at Wait Reselect (execute_script, case 2).
+             */
+            s->istat &= ~NCR710_ISTAT_SIGP;
+            s->sigp_pending_resume = true;
+        }
         break;
     case NCR710_CTEST8:
         /*
@@ -1599,7 +1816,40 @@ 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;
+
     ncr710_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.  The HP-UX 10.20 c720 driver issues its "start next
+     * command" SIGP kick with CPU interrupts masked and then spins; a still
+     * pending asynchronous completion (its bottom half has not run) would land
+     * in that masked window unserviced, and the driver's watchdog would reset
+     * the bus.  Real hardware takes the higher priority completion interrupt
+     * before that poll.
+     *
+     * A deferred continue (ncr710_issue_bh, for a tagged reselect transfer) has
+     * not issued its backing I/O yet, so pump it inline first; otherwise
+     * blk_drain the backend.  The loop is bounded by NCR710_DRAIN_MAX_ITERS and
+     * falls back to asynchronous completion (trace_ncr710_drain_exhausted) if it
+     * cannot make progress.
+     */
+    while (s->waiting == NCR710_DMA_IN_PROGRESS && s->current != NULL &&
+           s->current->req != NULL) {
+        if (drained++ >= NCR710_DRAIN_MAX_ITERS) {
+            trace_ncr710_drain_exhausted(drained);
+            break;
+        }
+        if (s->issue_pending) {
+            ncr710_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 ncr710_pre_save(void *opaque)
@@ -1644,7 +1894,7 @@ const VMStateDescription vmstate_ncr710 = {
      * raised in lockstep so a legacy stream is rejected rather than parsed
      * positionally into the new layout.
      */
-    .version_id = 2,
+    .version_id = 3,
     .minimum_version_id = 2,
     .pre_save = ncr710_pre_save,
     .post_load = ncr710_post_load,
@@ -1659,6 +1909,13 @@ const VMStateDescription vmstate_ncr710 = {
         VMSTATE_UINT32(select_tag, NCR710State),
         VMSTATE_INT32(command_complete, NCR710State),
         VMSTATE_BOOL(script_running, NCR710State),
+        /*
+         * A deferred "start next command" SIGP kick (version 3+).  Unlike
+         * reentrancy_level it can be live across a migration point (set while
+         * the engine is halted at a completion INT awaiting its ISR), so it is
+         * migrated; absent from a v2 stream it defaults to false.
+         */
+        VMSTATE_BOOL_V(sigp_pending_resume, NCR710State, 3),
 
         VMSTATE_UINT8(scntl0, NCR710State),
         VMSTATE_UINT8(scntl1, NCR710State),
hw/scsi/ncr53c710.h +19 -0
index 7bcefae902..4e15c9d0c1 100644
@@ -95,6 +95,25 @@ typedef struct NCR710State {
     uint32_t select_tag;
     int command_complete;
     bool script_running;
+    /*
+     * The driver's "start next command" SIGP kick landed while the engine was
+     * halted at a completion INT (waiting==NOWAIT) instead of parked at Wait
+     * Reselect, so the normal SIGP wake is skipped.  The ISTAT write clears the
+     * SIGP latch (presenting the kick as "consumed" to the driver's poll) and
+     * sets this flag instead; the Wait Reselect handler (execute_script case 2)
+     * consumes it when the engine next parks, selecting the next command.
+     */
+    bool sigp_pending_resume;
+    /*
+     * A cached-backend SCSI continue was deferred to ncr710_issue_bh so its
+     * completion arrives asynchronously, not reentrantly inside the engine.
+     */
+    bool issue_pending;
+    /*
+     * SCRIPTS interpreter on-stack recursion depth; transient (always 0
+     * outside execute_script), so it is not migrated in vmstate.
+     */
+    int reentrancy_level;
     NCR710Request *current;
     /*
      * Disconnected tagged commands awaiting target-initiated reselection
Signature · 1 line
2.43.0