Skip to contentSkip to conversation

nc53c710 rewrite

PATCH New Helge Deller 1 participant 15 messages Nov 23

lore
Share & tools
lore
#0 lore raw

cover nc53c710 rewrite

From: Helge Deller <[email protected]>

Fifth attempt to replace the existing 53c710 driver by a new driver based on the code of the lsi53c895a.c 🙂

Since v5:
Incorporated feedback from BALATON Zoltan <[email protected]>, specifically:
- added his r-b tag
- use dma_addr_t in lsi_do_dma() as in lsi53c895a driver
- rephrased commit message in patch 11, text from Keith Monahan
- stick to version 2 in vmstate struct

Since v4:
Used LLM to decompose v3 as logical / atomic changes when possible (didn't work well with the rewrite patch).

Fixes extracted first so could be merged for v11.2 (patches 1 to 5). The rest is the rewrite, so up to the maintainers (Helge, Mark) and is probably OK as hobbyist-only code.

Since v3:
- header split, Suggested-by tag (Zoltan)
- I couldn't figure the "hw/misc/lasi.c" change so dropped the comment about it.
- Dropped "move to COMMAND on CLEAR ACK after MESSAGE REJECT"
- Split rewrite patch as separated commits:
- base engine
- asynchronous completions
- synchronous drain
- HP-UX MESSAGE REJECT quirk
- reentrancy guard and reset-time request cancel

hw/scsi/ncr53c710.c is derived from hw/scsi/lsi53c895a.c which is LGPL, the rewrite is now declared GPL-2.0-or-later. hw/scsi/lsi_scsi.h, which is moved verbatim from lsi53c895a.c, keeps the original terms: its header says "This code is licensed under the LGPL." with SPDX-License-Identifier: LGPL-2.1-or-later.

Keith Monahan (9):
hw/scsi/scsi-disk: Add quirk_mode_page_no_flexible_disk, use it on the

    715

hw/scsi: Move shared LSI/NCR definitions to lsi_scsi.h hw/scsi/ncr53c710: Rewrite the 53C710 SCRIPTS engine hw/scsi/ncr53c710: Deliver completions from bottom halves hw/scsi/ncr53c710: Drain asynchronous transfers in the register write hw/scsi/ncr53c710: Append SAVE DATA POINTERS after MESSAGE REJECT for
HP-UX
hw/scsi/ncr53c710: Guard SCRIPTS reentrancy and cancel requests on
soft reset
hw/scsi/lasi_ncr710: Map the full LASI SCSI module page hw/scsi/ncr53c710: Bump the migration version

Philippe Mathieu-Daudé (5):
hw/scsi/ncr53c710: Cancel the in-flight request on soft reset hw/scsi/ncr53c710: Do not load SBCL from DBC after a SCRIPTS phase
move
hw/scsi/lasi_ncr710: Limit max_target and max_lun to 7 hw/scsi/ncr53c710: Keep DCNTL.EA and DMODE.FC1 across an ISTAT.RST
reset
hw/scsi/ncr53c710: Document the SIGP latch deviation

 MAINTAINERS            |    1 +
 hw/hppa/machine.c      |   10 +
 hw/scsi/lasi_ncr710.c  |  135 +-
 hw/scsi/lasi_ncr710.h  |   28 +-
 hw/scsi/lsi53c895a.c   |   79 +-
 hw/scsi/lsi_scsi.h     |  104 ++
 hw/scsi/ncr53c710.c    | 2957 +++++++++++++++-------------------------
 hw/scsi/ncr53c710.h    |  287 ++--
 hw/scsi/scsi-disk.c    |   13 +
 hw/scsi/trace-events   |   43 +-
 include/hw/scsi/scsi.h |    1 +
 11 files changed, 1441 insertions(+), 2217 deletions(-)
 create mode 100644 hw/scsi/lsi_scsi.h
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/14 hw/scsi/ncr53c710: Cancel the in-flight request on soft reset

From: Philippe Mathieu-Daudé <[email protected]>

ncr710_soft_reset() drops s->current and clears the request's hba_private without telling the SCSI layer. The request stays queued on the device and, once it completes or asks for data, the HBA callbacks find a NULL hba_private (ncr710_transfer_data() asserts on it).

Cancel the request first. scsi_req_cancel() invokes ncr710_request_cancelled(), which releases the NCR710Request and the HBA's reference on the request; ncr710_clear_pending_irq() then finds s->current already NULL and has nothing left to do. If the request is not enqueued, scsi_req_cancel() does nothing and the previous behaviour is kept.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/ncr53c710.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/scsi/ncr53c710.c +4 -0
index 57967e7a84..10daa66e84 100644
@@ -364,6 +364,10 @@ void ncr710_soft_reset(NCR710State *s)
     s->sidl = 0;
     s->sfbr = 0;
     qemu_set_irq(s->irq, 0);
+    if (s->current && s->current->req) {
+        /* Drops hba_private via ncr710_request_cancelled() */
+        scsi_req_cancel(s->current->req);
+    }
     ncr710_clear_pending_irq(s);
     ncr710_scsi_fifo_init(&s->scsi_fifo);
 }
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/14 hw/scsi/ncr53c710: Do not load SBCL from DBC after a SCRIPTS phase move

In reply to Helge Deller

From: Philippe Mathieu-Daudé <[email protected]>

After a SCRIPTS Block Move, ncr710_execute_script() copies DBC, the transfer byte count, into SBCL. SBCL (SCSI Bus Control Lines) mirrors the state of the SCSI bus control signals, it is not a byte counter, so this overwrites it with unrelated data. The preceding line already updates CTEST5 from the DBC bits that the chip exposes there.

Remove the assignment. The effect on guests is inferred from the register definition; it has not been reproduced with a failing guest.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/ncr53c710.c | 1 -
 1 file changed, 1 deletion(-)
hw/scsi/ncr53c710.c +0 -1
index 10daa66e84..9ea2c47744 100644
@@ -1400,7 +1400,6 @@ again:
             BADF("Unimplemented phase %d\n", s->sstat2 & PHASE_MASK);
         }
         s->ctest5 = (s->ctest5 & 0xfc) | ((s->dbc >> 8) & 3);
-        s->sbcl = s->dbc;
         break;
 
     case 1: /* IO or Read/Write instruction.  */
Signature · 1 line
2.54.0
#3 lore raw

PATCH 3/14 hw/scsi/scsi-disk: Add quirk_mode_page_no_flexible_disk, use it on the 715

In reply to Helge Deller

From: Keith Monahan <[email protected]>

scsi-disk answers MODE SENSE for page 05h (flexible disk geometry) for every disk. Per the commit author, real SCSI hard disks of that era reject this page with CHECK CONDITION, ILLEGAL REQUEST, INVALID FIELD IN CDB, as only floppy drives implement it. This has not been verified against real hardware or a datasheet here.

Per the commit author, HP-UX 9 on the HP 715 probes page 05h on the open root disk and, when the page is returned, treats the disk as a floppy and leaves it out of the ioscan listing, so SAM shows no hard disk. This guest behaviour has not been reproduced here.

Add the quirk_mode_page_no_flexible_disk property, which makes page 05h an invalid field in the CDB, and enable it for scsi-hd on the HP 715 machine through its compat properties. The property is also defined for scsi-cd, like the other mode page quirks, as both device types share mode_sense_page() and the same quirks bitmap; the 715 does not enable it there.

The array name hw_compat_715 suggested a versioned machine compat table, which it is not; call it hp_715_scsi_quirks.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/hppa/machine.c      | 10 ++++++++++
 hw/scsi/scsi-disk.c    | 13 +++++++++++++
 include/hw/scsi/scsi.h |  1 +
 3 files changed, 24 insertions(+)
hw/hppa/machine.c +10 -0
index 3b83968f33..04c4b2a8d8 100644
@@ -1020,6 +1020,14 @@ static void HP_A400_machine_init_class_init(ObjectClass *oc, const void *data)
     mc->default_ram_size = 1024 * MiB;
 }
 
+/*
+ * HP-UX 9 ioscan sends MODE SENSE page 5 to the open root disk and leaves
+ * the disk out of its listing if the page is returned.
+ */
+static GlobalProperty hp_715_scsi_quirks[] = {
+    { "scsi-hd", "quirk_mode_page_no_flexible_disk", "on" },
+};
+
 static void HP_715_machine_init_class_init(ObjectClass *oc, const void *data)
 {
     static const char * const valid_cpu_types[] = {
@@ -1041,6 +1049,8 @@ static void HP_715_machine_init_class_init(ObjectClass *oc, const void *data)
     mc->max_cpus = MIN_CONST(HPPA_MAX_CPUS, 8);
     mc->default_ram_size = 256 * MiB;
     mc->default_nic = TYPE_LASI_82596;
+    compat_props_add(mc->compat_props, hp_715_scsi_quirks,
+                     G_N_ELEMENTS(hp_715_scsi_quirks));
 }
 
 
hw/scsi/scsi-disk.c +13 -0
index a2df3a4ec4..ce43ddf6c0 100644
@@ -1156,6 +1156,15 @@ static int mode_sense_page(SCSIDiskState *s, int page, uint8_t **p_outbuf,
         return -1;
     }
 
+    /*
+     * The flexible disk page is only implemented by floppy drives.  Some
+     * guests (HP-UX 9 ioscan) take a disk that returns it for a floppy.
+     */
+    if (page == MODE_PAGE_FLEXIBLE_DISK_GEOMETRY &&
+        (s->quirks & (1 << SCSI_DISK_QUIRK_MODE_PAGE_NO_FLEXIBLE_DISK))) {
+        return -1;
+    }
+
     /*
      * If Changeable Values are requested, a mask denoting those mode parameters
      * that are changeable shall be returned. As we currently don't support
@@ -3293,6 +3302,8 @@ static const Property scsi_hd_properties[] = {
                     0),
     DEFINE_PROP_BIT("quirk_mode_page_set_block_size", SCSIDiskState,
                     quirks, SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE, 0),
+    DEFINE_PROP_BIT("quirk_mode_page_no_flexible_disk", SCSIDiskState,
+                    quirks, SCSI_DISK_QUIRK_MODE_PAGE_NO_FLEXIBLE_DISK, 0),
     DEFINE_BLOCK_CHS_PROPERTIES(SCSIDiskState, qdev.conf),
 };
 
@@ -3401,6 +3412,8 @@ static const Property scsi_cd_properties[] = {
                     SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED, 0),
     DEFINE_PROP_BIT("quirk_mode_page_set_block_size", SCSIDiskState,
                     quirks, SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE, 0),
+    DEFINE_PROP_BIT("quirk_mode_page_no_flexible_disk", SCSIDiskState,
+                    quirks, SCSI_DISK_QUIRK_MODE_PAGE_NO_FLEXIBLE_DISK, 0),
 };
 
 static void scsi_cd_class_initfn(ObjectClass *klass, const void *data)
include/hw/scsi/scsi.h +1 -0
index a2f6f0be7f..0efa62554d 100644
@@ -264,5 +264,6 @@ bool scsi_generic_pr_state_cleanup(SCSIDevice *s, Error **errp);
 #define SCSI_DISK_QUIRK_MODE_PAGE_VENDOR_SPECIFIC_APPLE    2
 #define SCSI_DISK_QUIRK_MODE_PAGE_TRUNCATED                3
 #define SCSI_DISK_QUIRK_MODE_PAGE_SET_BLOCK_SIZE           4
+#define SCSI_DISK_QUIRK_MODE_PAGE_NO_FLEXIBLE_DISK         5
 
 #endif
Signature · 1 line
2.54.0
#4 lore raw

PATCH 4/14 hw/scsi/lasi_ncr710: Limit max_target and max_lun to 7

In reply to Helge Deller

From: Philippe Mathieu-Daudé <[email protected]>

SCSI target IDs and LUNs on this bus are 3-bit values, so the highest valid one is 7. scsi-bus.c treats max_target and max_lun as inclusive limits (id > max_target is rejected), so 8 wrongly allowed an ID and a LUN that the chip cannot address. This matches esp.c and esp-pci.c.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/lasi_ncr710.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/scsi/lasi_ncr710.c +2 -2
index 4fde2265b5..27234aadd3 100644
@@ -137,8 +137,8 @@ static void lasi_ncr710_command_complete(SCSIRequest *req, size_t resid)
 
 static const struct SCSIBusInfo lasi_ncr710_scsi_info = {
     .tcq = true,
-    .max_target = 8,
-    .max_lun = 8,  /* full LUN support */
+    .max_target = 7,
+    .max_lun = 7,
 
     .transfer_data = lasi_ncr710_transfer_data,
     .complete = lasi_ncr710_command_complete,
Signature · 1 line
2.54.0
#5 lore raw

PATCH 5/14 hw/scsi/ncr53c710: Keep DCNTL.EA and DMODE.FC1 across an ISTAT.RST reset

In reply to Helge Deller

From: Philippe Mathieu-Daudé <[email protected]>

The NCR 53C710 data manual, ISTAT bit 6 (RST) description, says the software reset clears all registers except that "This reset will not clear the Enable Acknowledge (EA) or Function Control One (FC1) bits". EA is DCNTL bit 5 and FC1 is DMODE bit 4. ncr710_soft_reset() cleared both registers entirely.

Add named defines for the two bits and preserve them in the soft reset. ncr710_soft_reset() is also used for device reset and realize, which must still clear both bits, so those callers (the LASI reset and realize handlers and the sysbus reset handler) now zero DCNTL and DMODE after calling it.

The effect on real guest drivers has not been tested.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/lasi_ncr710.c | 6 ++++++
 hw/scsi/ncr53c710.c   | 7 +++++--
 hw/scsi/ncr53c710.h   | 5 +++++
 3 files changed, 16 insertions(+), 2 deletions(-)
hw/scsi/lasi_ncr710.c +6 -0
index 27234aadd3..27d8b5c749 100644
@@ -186,6 +186,9 @@ static void lasi_ncr710_realize(DeviceState *dev, Error **errp)
                      &s->ncr710);
 
     ncr710_soft_reset(&s->ncr710);
+    /* A device reset clears EA and FC1; only ISTAT.RST keeps them. */
+    s->ncr710.dcntl = 0;
+    s->ncr710.dmode = 0;
 
     trace_lasi_ncr710_timers_initialized(
         (uint64_t)s->ncr710.reselection_retry_timer);
@@ -246,6 +249,9 @@ static void lasi_ncr710_reset(DeviceState *dev)
     LasiNCR710State *s = LASI_NCR710(dev);
     trace_lasi_ncr710_device_reset();
     ncr710_soft_reset(&s->ncr710);
+    /* A device reset clears EA and FC1; only ISTAT.RST keeps them. */
+    s->ncr710.dcntl = 0;
+    s->ncr710.dmode = 0;
 }
 
 static void lasi_ncr710_instance_init(Object *obj)
hw/scsi/ncr53c710.c +5 -2
index 9ea2c47744..55153418e4 100644
@@ -349,8 +349,8 @@ void ncr710_soft_reset(NCR710State *s)
     s->ctest5 = 0;
     s->dsp = 0;
     s->dsps = 0;
-    s->dmode = 0;
-    s->dcntl = 0;
+    s->dmode &= NCR710_DMODE_FC1;
+    s->dcntl &= NCR710_DCNTL_EA;
     s->scntl0 = 0xc0;
     s->scntl1 = 0;
     s->sstat0 = 0;
@@ -2266,6 +2266,9 @@ static void ncr710_device_reset(DeviceState *dev)
     NCR710State *s = &sysbus_dev->ncr710;
 
     ncr710_soft_reset(s);
+    /* A device reset clears EA and FC1; only ISTAT.RST keeps them. */
+    s->dcntl = 0;
+    s->dmode = 0;
 }
 
 static const struct SCSIBusInfo ncr710_scsi_info = {
hw/scsi/ncr53c710.h +5 -0
index 00b6a01577..7dd0fe5f75 100644
@@ -85,6 +85,11 @@
 #define NCR710_DCNTL_REG        0x3B    /* DMA Control */
 #define NCR710_ADDER_REG        0x3C    /* Adder Sum Output (32-bit, LE) */
 
+/* DMODE bit 4: Function Code 1 (FC1), kept by an ISTAT.RST soft reset. */
+#define NCR710_DMODE_FC1  0x10
+/* DCNTL bit 5: Enable Acknowledge (EA), kept by an ISTAT.RST soft reset. */
+#define NCR710_DCNTL_EA   0x20
+
 #define NCR710_REG_SIZE         0x100
 
 #define NCR710_BUF_SIZE         4096
Signature · 1 line
2.54.0
#6 lore raw

PATCH 6/14 hw/scsi: Move shared LSI/NCR definitions to lsi_scsi.h

In reply to Helge Deller

From: Keith Monahan <[email protected]>

Move common defines and structures of the lsi53c895a driver to lsi_scsi.h so that the 53c710 driver can share them.

Pure code movement, no functional change: nothing is renamed and the lsi53c895a device is unchanged.

Licence: the moved code comes from lsi53c895a.c, which is licensed under the LGPL (Copyright (c) 2006 CodeSourcery, written by Paul Brook), so the new header keeps that licence (SPDX LGPL-2.1-or-later) rather than the preferred GPL-2.0-or-later; it is not relicensed.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 MAINTAINERS          |   1 +
 hw/scsi/lsi53c895a.c |  79 +-------------------------------
 hw/scsi/lsi_scsi.h   | 104 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+), 78 deletions(-)
 create mode 100644 hw/scsi/lsi_scsi.h
MAINTAINERS +1 -0
index 40bf63651c..9e001a85ee 100644
@@ -2432,6 +2432,7 @@ R: Fam Zheng <[email protected]>
 S: Supported
 F: include/hw/scsi/*
 F: hw/scsi/*
+F: hw/scsi/lsi_scsi.h
 F: tests/qtest/virtio-scsi-test.c
 F: tests/qtest/fuzz-virtio-scsi-test.c
 F: tests/qtest/am53c974-test.c
hw/scsi/lsi53c895a.c +1 -78

143 lines not on this page · raw message

hw/scsi/lsi_scsi.h new file +104 -0

104 lines not on this page · raw message

Signature · 1 line
2.54.0
#7 lore raw

PATCH 7/14 hw/scsi/ncr53c710: Rewrite the 53C710 SCRIPTS engine

In reply to Helge Deller

From: Keith Monahan <[email protected]>

Rewrite the NCR 53C710 model with a SCRIPTS engine derived from QEMU's LSI53C895A model (hw/scsi/lsi53c895a.c), an actively maintained sibling in the same NCR/LSI SCRIPTS family, adapted to the 53C710: its register map, 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 counts. Behaviour follows the NCR 53C710 Data Manual (Jun 1992) and Programmer's Guide (Oct 1990).

The LASI glue (hw/scsi/lasi_ncr710.c) is the only instantiated device and embeds one NCR710State by value. It gains the free_request SCSIBusInfo callback and request queue init the new core needs, renames the reselection timer to scripts_timer, and drops the old model's dead scaffolding (the unused SCNR define, the DPRINTF macros, and unused or shadowing register macros).

Disconnect and reselect, adapted from lsi53c895a, let a driver overlap tagged commands: a disconnecting command's request moves onto a per device queue and is reselected once its data is ready and the engine is parked at Wait Reselect. Reselection follows the 53C700 family rather than the 8xx parts: gated on the (re)selection interrupt enable (SIEN.SEL) alone, one hot SDID, an SFBR id bitmask in 700 compatibility mode, and IDENTIFY (plus SIMPLE QUEUE TAG when tagged) replayed on reconnect. A disconnect is fabricated only for a tagged command under a driver that parks on Wait Reselect with SIEN.SEL clear, which today means HP-UX; untagged transfers and reselection interrupt drivers (e.g. Linux 53c700) keep the existing connected completion path.

SDTR/WDTR/PPR negotiation is declined with MESSAGE REJECT.

Completion deferral, the synchronous drain, the HP-UX MESSAGE REJECT quirk, the reentrancy guard and the soft reset request cancel are added by the following patches, as is the migration version bump: this patch does not preserve the migration stream of the model it replaces.

Derived from hw/scsi/lsi53c895a.c (Copyright (c) 2006 CodeSourcery, written by Paul Brook), which is LGPL; this derived work is distributed under GPL-2.0-or-later, as the LGPL permits. Replaces the 53C710 model written for Google Summer of Code 2025 by Soumyajyotii Ssarkar, itself based on earlier NCR 53C710 work by Helge Deller and by Toni Wilen (for UAE).

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/lasi_ncr710.c |  108 +-
 hw/scsi/lasi_ncr710.h |   28 +-
 hw/scsi/ncr53c710.c   | 2828 ++++++++++++++---------------------------
 hw/scsi/ncr53c710.h   |  265 ++--
 hw/scsi/trace-events  |   42 +-
 5 files changed, 1125 insertions(+), 2146 deletions(-)
hw/scsi/lasi_ncr710.c +41 -67
index 27d8b5c749..cbb04017ce 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 mapping the 53C710 core in ncr53c710.c onto the HP 715
+ * (-M 715) LASI bus.
  *
  * SPDX-License-Identifier: GPL-2.0-or-later
  */
@@ -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 */
 
@@ -34,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);
@@ -51,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);
@@ -84,7 +81,11 @@ 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);
+     * writes to it are ignored.
+     */
+    if (addr <= 0x0f) {
         return;
     }
 
@@ -93,16 +94,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);
             }
         }
@@ -113,10 +108,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);
@@ -129,20 +121,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,
+    /* 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 = {
@@ -160,9 +159,6 @@ static const VMStateDescription vmstate_lasi_ncr710 = {
     .version_id = 1,
     .minimum_version_id = 1,
     .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()
     }
@@ -179,10 +175,11 @@ 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);
@@ -191,40 +188,26 @@ static void lasi_ncr710_realize(DeviceState *dev, Error **errp)
     s->ncr710.dmode = 0;
 
     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,
@@ -254,20 +237,12 @@ static void lasi_ncr710_reset(DeviceState *dev)
     s->ncr710.dmode = 0;
 }
 
-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";
@@ -280,7 +255,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 -25
index 450fb7e1c3..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
  */
@@ -22,29 +19,10 @@
 #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;
     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 +964 -1864

2,727 lines not on this page · raw message

hw/scsi/ncr53c710.h +86 -179

265 lines not on this page · raw message

hw/scsi/trace-events +31 -11

42 lines not on this page · raw message

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

Signature · 1 line
2.54.0

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

#8 lore raw

PATCH 8/14 hw/scsi/ncr53c710: Deliver completions from bottom halves

In reply to Helge Deller

From: Keith Monahan <[email protected]>

Completions are delivered asynchronously, as the real chip delivers them, rather than reentrantly inside the interpreter: a reselect reconnect and a tagged completion off host cached media (the install CD-ROM) are deferred to a bottom half. A reentrant completion would collapse the disconnect and the completion interrupt into the one guest MMIO write driving the engine and race the HP-UX 10.20 c720 driver's completion bookkeeping. Untagged and polled transfers keep inline completion.

A SIGP kick that lands on a stopped engine, rather than one parked at Wait Reselect, is held in sigp_pending_resume and replayed once the engine parks.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/ncr53c710.c | 92 ++++++++++++++++++++++++++++++++++++++++-----
 hw/scsi/ncr53c710.h |  8 ++++
 2 files changed, 90 insertions(+), 10 deletions(-)
hw/scsi/ncr53c710.c +82 -10
index 75060f5d99..5522dedee8 100644
@@ -74,6 +74,7 @@ static const char *lsi_reg_name(int offset)
 static uint8_t lsi_reg_readb(NCR710State *s, int offset);
 static void lsi_reg_writeb(NCR710State *s, int offset, uint8_t val);
 static void lsi_execute_script(NCR710State *s);
+static void lsi_issue_bh(void *opaque);
 static void lsi_set_phase(NCR710State *s, int phase);
 
 /*
@@ -107,6 +108,7 @@ void ncr710_soft_reset(NCR710State *s)
     s->select_tag = 0;
     s->command_complete = 0;
     s->script_running = false;
+    s->sigp_pending_resume = false;
 
     s->scntl0 = 0xc0;       /* full arbitration */
     s->scntl1 = 0;
@@ -273,6 +275,25 @@ static void lsi_bad_selection(NCR710State *s, uint32_t id)
     lsi_disconnect(s);
 }
 
+/*
+ * Deferred half of lsi_do_dma()'s SCSI continue.  It runs on a clean stack,
+ * so a cached backend's completion cannot arrive reentrantly inside the
+ * SCRIPTS interpreter.
+ */
+static void lsi_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 lsi_do_dma(NCR710State *s, int out)
 {
     uint32_t count;
@@ -315,7 +336,21 @@ static void lsi_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 (dbc == 0) of a tagged transfer to a
+         * bottom half; the engine parks at DMA_IN_PROGRESS (set by the caller)
+         * meanwhile.
+         */
+        if ((p->tag & LSI_TAG_VALID) && s->dbc == 0) {
+            s->issue_pending = true;
+            aio_bh_schedule_oneshot(qemu_get_aio_context(), lsi_issue_bh, s);
+        } else {
+            /*
+             * Complete inline for untagged transfers and mid-transfer
+             * continues (dbc > 0).
+             */
+            scsi_req_continue(req);
+        }
     } else {
         p->dma_buf += count;
         lsi_resume_script(s);
@@ -418,20 +453,43 @@ static int lsi_queue_req(NCR710State *s, SCSIRequest *req, uint32_t len)
     return 1;
 }
 
-/* SCRIPTS Wait Reselect: reconnect a ready queued command, else park. */
-static void lsi_wait_reselect(NCR710State *s)
+/*
+ * Deferred half of lsi_wait_reselect(): reconnect a disconnected command
+ * whose data is ready and resume the engine.  Does nothing if the engine moved
+ * on while this was pending.
+ */
+static void lsi_reselect_bh(void *opaque)
 {
+    NCR710State *s = opaque;
     lsi_request *p;
 
-    if (s->current) {
+    if (s->current != NULL || s->waiting != LSI_WAIT_RESELECT) {
         return;
     }
     p = lsi_get_pending_req(s);
-    if (p) {
-        lsi_reselect(s, p);
+    if (p == NULL) {
+        return;
+    }
+    lsi_reselect(s, p);
+    s->waiting = LSI_NOWAIT;
+    lsi_execute_script(s);
+}
+
+/* SCRIPTS Wait Reselect: reconnect a ready queued command, else park. */
+static void lsi_wait_reselect(NCR710State *s)
+{
+    if (s->current) {
+        return;
     }
-    if (s->current == NULL) {
-        s->waiting = LSI_WAIT_RESELECT;
+    /*
+     * Reselecting synchronously here, inside the guest register write that
+     * drove the engine to this Wait Reselect, would deliver the command's
+     * completion interrupt reentrantly.  Defer the reconnect to a bottom half;
+     * the engine parks meanwhile.
+     */
+    s->waiting = LSI_WAIT_RESELECT;
+    if (lsi_get_pending_req(s)) {
+        aio_bh_schedule_oneshot(qemu_get_aio_context(), lsi_reselect_bh, s);
     }
 }
 
@@ -931,9 +989,10 @@ again:
                 /*
                  * Two things release a Wait Reselect: SIGP (jump to the
                  * dispatch address latched in DNAD), or a disconnected command
-                 * becoming ready to reselect.
+                 * becoming ready to reselect.  A SIGP that arrived while the
+                 * engine was halted is held in sigp_pending_resume.
                  */
-                if (s->istat & LSI_ISTAT0_SIGP) {
+                if ((s->istat & LSI_ISTAT0_SIGP) || s->sigp_pending_resume) {
                     /*
                      * The SIGP divert is a one-shot: clear the latch so a
                      * later Wait Reselect does not re-fire on a stale SIGP.
@@ -941,6 +1000,7 @@ again:
                      * auto-switching.
                      */
                     s->istat &= ~LSI_ISTAT0_SIGP;
+                    s->sigp_pending_resume = false;
                     s->dsp = s->dnad;
                 } else if (!lsi_irq_on_rsl(s)) {
                     lsi_wait_reselect(s);
@@ -1405,6 +1465,17 @@ static void lsi_reg_writeb(NCR710State *s, int offset, uint8_t val)
             s->waiting = LSI_NOWAIT;
             s->dsp = s->dnad;
             lsi_execute_script(s);
+        } else if ((val & LSI_ISTAT0_SIGP) && !s->script_running &&
+                   s->waiting == LSI_NOWAIT) {
+            /*
+             * SIGP while the engine is halted on a just completed command,
+             * not parked at Wait Reselect.  Clear the SIGP latch, as a Wait
+             * Reselect consumption would, without disturbing the pending
+             * completion.  Remember the kick so the next command is selected
+             * once the engine parks again at Wait Reselect.
+             */
+            s->istat &= ~LSI_ISTAT0_SIGP;
+            s->sigp_pending_resume = true;
         }
         break;
     case NCR710_CTEST8:
@@ -1536,6 +1607,7 @@ const VMStateDescription vmstate_ncr710 = {
         VMSTATE_UINT32(select_tag, NCR710State),
         VMSTATE_INT32(command_complete, NCR710State),
         VMSTATE_BOOL(script_running, NCR710State),
+        VMSTATE_BOOL(sigp_pending_resume, NCR710State),
 
         VMSTATE_UINT8(scntl0, NCR710State),
         VMSTATE_UINT8(scntl1, NCR710State),
hw/scsi/ncr53c710.h +8 -0
index 82202940ca..8b5cbfc135 100644
@@ -93,6 +93,14 @@ typedef struct NCR710State {
     uint32_t select_tag;
     int command_complete;
     bool script_running;
+    /*
+     * A SIGP arrived while the engine was halted instead of parked at Wait
+     * Reselect; the Wait Reselect handler consumes it when the engine next
+     * parks.
+     */
+    bool sigp_pending_resume;
+    /* A SCSI continue was deferred to lsi_issue_bh. */
+    bool issue_pending;
     lsi_request *current;
     /* Disconnected tagged commands awaiting reselection. */
     QTAILQ_HEAD(, lsi_request) queue;
Signature · 1 line
2.54.0
#9 lore raw

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

In reply to Helge Deller

From: Keith Monahan <[email protected]>

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

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/ncr53c710.c  | 29 +++++++++++++++++++++++++++++
 hw/scsi/trace-events |  1 +
 2 files changed, 30 insertions(+)
hw/scsi/ncr53c710.c +29 -0
index 5522dedee8..7558d5b4b0 100644
@@ -54,6 +54,12 @@
 
 #define LSI_BUF_SIZE     4096
 
+/*
+ * Safety bound on the synchronous completion drain loop in ncr710_reg_write();
+ * it then falls back to asynchronous completion.
+ */
+#define LSI_DRAIN_MAX_ITERS  100000
+
 static const char *const lsi_reg_names[64] = {
     "SCNTL0", "SCNTL1", "SDID", "SIEN", "SCID", "SXFER", "SODL", "SOCL",
     "SFBR", "SIDL", "SBDL", "SBCL", "DSTAT", "SSTAT0", "SSTAT1", "SSTAT2",
@@ -1553,7 +1559,30 @@ uint64_t ncr710_reg_read(NCR710State *s, hwaddr addr, unsigned size)
 
 void ncr710_reg_write(NCR710State *s, hwaddr addr, uint64_t val, unsigned size)
 {
+    unsigned drained = 0;
+
     lsi_reg_writeb(s, addr & 0xff, val & 0xff);
+
+    /*
+     * Drive any asynchronous SCSI transfer the register write kicked off to
+     * completion before returning to the guest, so its completion interrupt is
+     * raised first.  A deferred continue (lsi_issue_bh) has not issued its
+     * backing I/O yet, so pump it inline; otherwise blk_drain the backend.
+     */
+    while (s->waiting == LSI_DMA_IN_PROGRESS && s->current != NULL &&
+           s->current->req != NULL) {
+        if (drained++ >= LSI_DRAIN_MAX_ITERS) {
+            trace_ncr710_drain_exhausted(drained);
+            break;
+        }
+        if (s->issue_pending) {
+            lsi_issue_bh(s);
+        } else if (s->current->req->dev->conf.blk != NULL) {
+            blk_drain(s->current->req->dev->conf.blk);
+        } else {
+            break;      /* nothing deferred and no backend to drain */
+        }
+    }
 }
 
 static int lsi_pre_save(void *opaque)
hw/scsi/trace-events +1 -0
index f0eb145702..843d8f1a1e 100644
@@ -340,6 +340,7 @@ ncr710_memmove(uint32_t dest, uint32_t src, int count) "MOVE MEMORY dest=0x%08x
 ncr710_do_dma_unavailable(void) "DMA: data not yet available"
 ncr710_wait_disconnect(void) "Wait Disconnect"
 ncr710_illegal_insn(uint32_t insn) "ILLEGAL instruction 0x%08x"
+ncr710_drain_exhausted(unsigned iters) "synchronous completion drain gave up after %u iters"
 
 # lasi_ncr710.c
 lasi_ncr710_device_realize(void) "Device realized"
Signature · 1 line
2.54.0
#10 lore raw

PATCH 10/14 hw/scsi/ncr53c710: Append SAVE DATA POINTERS after MESSAGE REJECT for HP-UX

In reply to Helge Deller

From: Keith Monahan <[email protected]>

The HP-UX 10.20 install kernel is nonconformant: its self modifying SCRIPTS reads a second message byte after the MESSAGE REJECT that declines SDTR/WDTR/PPR negotiation 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.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/ncr53c710.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
hw/scsi/ncr53c710.c +10 -1
index 7558d5b4b0..d6bd1c95fc 100644
@@ -700,6 +700,10 @@ static void lsi_do_msgin(NCR710State *s)
     if (!s->msg_len) {
         switch (s->msg_action) {
         case LSI_MSG_ACTION_COMMAND:
+            /* A MESSAGE REJECT declining negotiation was just consumed. */
+            if (s->sfbr == 7 && !(s->socl & LSI_SOCL_ATN)) {
+                lsi_add_msg_byte(s, 2);   /* SAVE DATA POINTERS (HP-UX) */
+            }
             /* Stay in MSG IN until the next block move requests COMMAND. */
             break;
         case LSI_MSG_ACTION_DISCONNECT:
@@ -795,7 +799,12 @@ static void lsi_do_msgout(NCR710State *s)
     }
     return;
 reject:
-    /* Decline SDTR/WDTR/PPR negotiation with a single MESSAGE REJECT. */
+    /*
+     * Decline SDTR/WDTR/PPR negotiation with a single MESSAGE REJECT.  HP-UX
+     * requires a trailing SAVE DATA POINTERS (0x02), which other initiators
+     * reject.  HP-UX reads the reject with ATN deasserted, so lsi_do_msgin
+     * appends the 0x02 only then.
+     */
     lsi_set_phase(s, PHASE_MI);
     lsi_add_msg_byte(s, 7);           /* MESSAGE REJECT */
     s->msg_action = LSI_MSG_ACTION_COMMAND;
Signature · 1 line
2.54.0
#11 lore raw

PATCH 11/14 hw/scsi/ncr53c710: Guard SCRIPTS reentrancy and cancel requests on soft reset

In reply to Helge Deller

From: Keith Monahan <[email protected]>

The SCRIPTS interpreter guards against CVE-2023-0330. When a command finishes while the interpreter is running, the interpreter moves on to the next instruction without restarting itself. If a script keeps triggering itself, the interpreter stops it after 8 nested restarts.

A guest ISTAT.RST soft reset cancels the connected and any queued tagged requests, so a stale s->current cannot dangle into the next command and queued requests are not leaked, mirroring the SCNTL1.RST bus reset teardown.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/ncr53c710.c | 41 +++++++++++++++++++++++++++++++++++++++--
 hw/scsi/ncr53c710.h |  5 +++++
 2 files changed, 44 insertions(+), 2 deletions(-)
hw/scsi/ncr53c710.c +39 -2
index d6bd1c95fc..b7fc97b93b 100644
@@ -104,8 +104,23 @@ static int lsi_id_from_bits(unsigned bits)
 
 void ncr710_soft_reset(NCR710State *s)
 {
+    lsi_request *p, *p_next;
+
     trace_ncr710_reset();
 
+    /*
+     * A guest ISTAT.RST soft reset can land with commands still in flight.
+     * Cancel them so no stale pointer survives the reset.
+     */
+    if (s->current && s->current->req) {
+        scsi_req_cancel(s->current->req);
+    }
+    QTAILQ_FOREACH_SAFE(p, &s->queue, next, p_next) {
+        if (p->req) {
+            scsi_req_cancel(p->req);
+        }
+    }
+
     s->carry = 0;
     s->msg_action = LSI_MSG_ACTION_COMMAND;
     s->msg_len = 0;
@@ -115,6 +130,11 @@ void ncr710_soft_reset(NCR710State *s)
     s->command_complete = 0;
     s->script_running = false;
     s->sigp_pending_resume = false;
+    /*
+     * Do not reset reentrancy_level here.  A SCRIPTS driven soft reset can run
+     * while lsi_execute_script frames are still on the stack; those frames
+     * decrement the counter as they unwind.
+     */
 
     s->scntl0 = 0xc0;       /* full arbitration */
     s->scntl1 = 0;
@@ -258,6 +278,16 @@ static int lsi_bad_phase(NCR710State *s, int new_phase)
 static void lsi_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.  Do not recurse: release the wait so the outer
+         * loop continues to the next instruction, which the completion has
+         * already armed.
+         */
+        s->waiting = LSI_NOWAIT;
+        return;
+    }
     if (s->waiting != LSI_DMA_SCRIPTS) {
         s->waiting = LSI_NOWAIT;
         lsi_execute_script(s);
@@ -858,13 +888,18 @@ static void lsi_execute_script(NCR710State *s)
         s->waiting = LSI_NOWAIT;
     }
 
+    s->reentrancy_level++;
     s->script_running = true;
 again:
-    /* Yield to the CPU after LSI_MAX_INSN instructions. */
-    if (++insn_processed > LSI_MAX_INSN) {
+    /*
+     * Yield to the CPU after LSI_MAX_INSN instructions, and guard against a
+     * script that retriggers itself (CVE-2023-0330).
+     */
+    if (++insn_processed > LSI_MAX_INSN || s->reentrancy_level > 8) {
         trace_ncr710_script_yield(insn_processed);
         s->waiting = LSI_WAIT_SCRIPTS;
         lsi_scripts_timer_start(s);
+        s->reentrancy_level--;
         return;
     }
 
@@ -1210,6 +1245,8 @@ again:
             goto again;
         }
     }
+
+    s->reentrancy_level--;
 }
 
 static uint8_t lsi_reg_readb(NCR710State *s, int offset)
hw/scsi/ncr53c710.h +5 -0
index 8b5cbfc135..9a4816794e 100644
@@ -101,6 +101,11 @@ typedef struct NCR710State {
     bool sigp_pending_resume;
     /* A SCSI continue was deferred to lsi_issue_bh. */
     bool issue_pending;
+    /*
+     * SCRIPTS interpreter recursion depth; always 0 outside
+     * lsi_execute_script, so it is not migrated.
+     */
+    int reentrancy_level;
     lsi_request *current;
     /* Disconnected tagged commands awaiting reselection. */
     QTAILQ_HEAD(, lsi_request) queue;
Signature · 1 line
2.54.0
#12 lore raw

PATCH 12/14 hw/scsi/lasi_ncr710: Map the full LASI SCSI module page

In reply to Helge Deller

From: Keith Monahan <[email protected]>

0x100..0x1ff goes to the chip core as before. The rest of the module page reads as 0: HP-UX reads a board strap at 0xd85 (bit 0x20:
differential bus, 25 MHz clock); 0 is single ended, 50 MHz.

0x10..0xff and 0x200..0xfff are unmapped module space. Writes to all of them are ignored.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/lasi_ncr710.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
hw/scsi/lasi_ncr710.c +11 -4
index cbb04017ce..c8a28cd987 100644
@@ -45,7 +45,8 @@ static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr,
         return val;
     }
 
-    if (addr >= 0x100) {
+    /* 0x100..0x1ff goes to the chip core. */
+    if (addr >= 0x100 && addr + size <= 0x200) {
         hwaddr ncr_addr = addr - 0x100;
         if (size == 1) {
             /*
@@ -68,6 +69,11 @@ static uint64_t lasi_ncr710_reg_read(void *opaque, hwaddr addr,
 
         trace_lasi_ncr710_reg_forward_read(addr, val);
     } else {
+        /*
+         * The rest of the module page reads as 0.  HP-UX reads a board strap
+         * at 0xd85 (bit 0x20: differential bus, 25 MHz clock); 0 is single
+         * ended, 50 MHz.
+         */
         val = 0;
         trace_lasi_ncr710_reg_read(addr, val, size);
     }
@@ -83,13 +89,14 @@ static void lasi_ncr710_reg_write(void *opaque, hwaddr addr,
 
     /*
      * 0x00..0x0f is the read only LASI module header (ID/SVERSION/HVERSION);
-     * writes to it are ignored.
+     * 0x10..0xff and 0x200..0xfff are unmapped module space.  Writes to all
+     * of them are ignored.
      */
     if (addr <= 0x0f) {
         return;
     }
 
-    if (addr >= 0x100) {
+    if (addr >= 0x100 && addr + size <= 0x200) {
         hwaddr ncr_addr = addr - 0x100;
 
         if (size == 1) {
@@ -191,7 +198,7 @@ static void lasi_ncr710_realize(DeviceState *dev, Error **errp)
         (uint64_t)s->ncr710.scripts_timer);
 
     memory_region_init_io(&s->mmio, OBJECT(dev), &lasi_ncr710_mmio_ops, s,
-                          "lasi-ncr710", 0x200);
+                          "lasi-ncr710", 0x1000);
     sysbus_init_mmio(sbd, &s->mmio);
 }
 
Signature · 1 line
2.54.0
#13 lore raw

PATCH 13/14 hw/scsi/ncr53c710: Document the SIGP latch deviation

In reply to Helge Deller

From: Philippe Mathieu-Daudé <[email protected]>

Comment only, no functional change. The data manual describes SIGP (ISTAT bit 5) as a read/write flag that stays set until reset by the CPU, either through ISTAT or by reading CTEST2. The emulation drops the bit at once when it is set while the engine is halted and remembers the kick in sigp_pending_resume instead. Note the deviation at the code. The reason is not documented in the history.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/ncr53c710.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/scsi/ncr53c710.c +8 -0
index b7fc97b93b..a77705eeda 100644
@@ -1525,6 +1525,14 @@ static void lsi_reg_writeb(NCR710State *s, int offset, uint8_t val)
              * Reselect consumption would, without disturbing the pending
              * completion.  Remember the kick so the next command is selected
              * once the engine parks again at Wait Reselect.
+             *
+             * This deviates from the data manual, which describes SIGP as a
+             * read/write flag that stays set until the CPU resets it, either
+             * via ISTAT or by reading CTEST2: here a halted engine drops the
+             * bit at once, so a read of ISTAT or CTEST2 no longer sees it.
+             * The reason is not documented beyond the commit that added
+             * sigp_pending_resume (6bf09c62fa), which only says that a kick
+             * on a stopped engine is held and replayed once the engine parks.
              */
             s->istat &= ~LSI_ISTAT0_SIGP;
             s->sigp_pending_resume = true;
Signature · 1 line
2.54.0
#14 lore raw

PATCH 14/14 hw/scsi/ncr53c710: Bump the migration version

In reply to Helge Deller

From: Keith Monahan <[email protected]>

The migration format is incompatible with the model this replaces, so the ncr710 and lasi_ncr710 vmstate descriptions raise minimum_version_id in lockstep: an old stream is rejected rather than silently misparsed.

AI-used-for: Patch series decomposition (commit splitting and ordering)

---
 hw/scsi/lasi_ncr710.c | 4 ++--
 hw/scsi/ncr53c710.c   | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
hw/scsi/lasi_ncr710.c +2 -2
index c8a28cd987..62608d039c 100644
@@ -163,8 +163,8 @@ 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_STRUCT(ncr710, LasiNCR710State, 1, vmstate_ncr710, NCR710State),
         VMSTATE_END_OF_LIST()
hw/scsi/ncr53c710.c +2 -2
index a77705eeda..2d3c52667c 100644
@@ -1675,8 +1675,8 @@ static int lsi_post_load(void *opaque, int version_id)
 
 const VMStateDescription vmstate_ncr710 = {
     .name = "ncr710",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .pre_save = lsi_pre_save,
     .post_load = lsi_post_load,
     .fields = (const VMStateField[]) {
Signature · 1 line
2.54.0