Skip to contentSkip to conversation

target/hppa patch queue

GIT PULL Richard Henderson 4 participants 20 messages Jan 24, 2020 – Mar 5, 2020

lore
Share & tools
lore
#0 lore raw

Version 3 fixes printf formatting errors, and some indentation errors, in artist.c, lasips2.c, and i82596.c.

r~

The following changes since commit ba2ed84fe6a78f64b2da441750fc6e925d94106a:

Merge remote-tracking branch 'remotes/palmer/tags/riscv-for-master-5.0-sf1' into staging (2020-01-24 12:34:04 +0000)

are available in the Git repository at:

https://github.com/rth7680/qemu.git tags/pull-pa-20200124

for you to fetch changes up to 5b33eecce852658121b1190fee466f01a69e1910:

target/hppa: Allow, but diagnose, LDCW aligned only mod 4 (2020-01-24 14:35:58 -0800)

----------------------------------------------------------------
Improve LASI emulation
Add Artist graphics
Fix main memory allocation
Improve LDCW emulation wrt real hw

----------------------------------------------------------------
Helge Deller (3):
hw/hppa/dino.c: Improve emulation of Dino PCI chip
hppa: Add support for LASI chip with i82596 NIC
hppa: Switch to tulip NIC by default

Philippe Mathieu-Daudé (3):
hw/hppa/machine: Correctly check the firmware is in PDC range hw/hppa/machine: Restrict the total memory size to 3GB hw/hppa/machine: Map the PDC memory region with higher priority

Richard Henderson (1):
target/hppa: Allow, but diagnose, LDCW aligned only mod 4

Sven Schnelle (4):
ps2: accept 'Set Key Make and Break' commands
hppa: add emulation of LASI PS2 controllers
seabios-hppa: update to latest version
hppa: Add emulation of Artist graphics

 hw/hppa/hppa_hardware.h        |    1 +
 hw/hppa/hppa_sys.h             |    2 +
 hw/net/i82596.h                |   55 ++
 include/hw/input/lasips2.h     |   16 +
 include/hw/input/ps2.h         |    1 +
 include/hw/net/lasi_82596.h    |   29 +
 target/hppa/helper.h           |    2 +
 hw/display/artist.c            | 1454 ++++++++++++++++++++++++++++++++++++++++
 hw/hppa/dino.c                 |   97 ++-
 hw/hppa/lasi.c                 |  368 ++++++++++
 hw/hppa/machine.c              |   33 +-
 hw/input/lasips2.c             |  291 ++++++++
 hw/input/ps2.c                 |   15 +
 hw/net/i82596.c                |  734 ++++++++++++++++++++
 hw/net/lasi_i82596.c           |  188 ++++++
 target/hppa/op_helper.c        |    9 +
 target/hppa/translate.c        |   15 +-
 tests/qtest/boot-serial-test.c |    3 +-
 MAINTAINERS                    |    4 +-
 hw/display/Kconfig             |    4 +
 hw/display/Makefile.objs       |    1 +
 hw/display/trace-events        |    9 +
 hw/hppa/Kconfig                |    3 +
 hw/hppa/Makefile.objs          |    2 +-
 hw/hppa/trace-events           |   10 +
 hw/input/Kconfig               |    3 +
 hw/input/Makefile.objs         |    1 +
 hw/input/trace-events          |    5 +
 hw/net/Kconfig                 |    7 +
 hw/net/Makefile.objs           |    2 +
 hw/net/trace-events            |   13 +
 pc-bios/hppa-firmware.img      |  Bin 783724 -> 766136 bytes
 roms/seabios-hppa              |    2 +-
 33 files changed, 3351 insertions(+), 28 deletions(-)
 create mode 100644 hw/net/i82596.h
 create mode 100644 include/hw/input/lasips2.h
 create mode 100644 include/hw/net/lasi_82596.h
 create mode 100644 hw/display/artist.c
 create mode 100644 hw/hppa/lasi.c
 create mode 100644 hw/input/lasips2.c
 create mode 100644 hw/net/i82596.c
 create mode 100644 hw/net/lasi_i82596.c
#1 lore raw

[PULL v3 01/11] hw/hppa/dino.c: Improve emulation of Dino PCI chip

From: Helge Deller <[email protected]>

The tests of the dino chip with the Online-diagnostics CD ("ODE DINOTEST") now succeeds.
Additionally add some qemu trace events.

Message-Id: <[email protected]>

---
 hw/hppa/dino.c       | 97 +++++++++++++++++++++++++++++++++++++-------
 MAINTAINERS          |  2 +-
 hw/hppa/trace-events |  5 +++
 3 files changed, 89 insertions(+), 15 deletions(-)
hw/hppa/dino.c +83 -14
index ab6969b45f..9797a7f0d9 100644
@@ -1,7 +1,7 @@
 /*
- * HP-PARISC Dino PCI chipset emulation.
+ * HP-PARISC Dino PCI chipset emulation, as in B160L and similiar machines
  *
- * (C) 2017 by Helge Deller <[email protected]>
+ * (C) 2017-2019 by Helge Deller <[email protected]>
  *
  * This work is licensed under the GNU GPL license version 2 or later.
  *
@@ -21,6 +21,7 @@
 #include "migration/vmstate.h"
 #include "hppa_sys.h"
 #include "exec/address-spaces.h"
+#include "trace.h"
 
 
 #define TYPE_DINO_PCI_HOST_BRIDGE "dino-pcihost"
@@ -82,11 +83,28 @@
 #define DINO_PCI_HOST_BRIDGE(obj) \
     OBJECT_CHECK(DinoState, (obj), TYPE_DINO_PCI_HOST_BRIDGE)
 
+#define DINO800_REGS ((DINO_TLTIM - DINO_GMASK) / 4)
+static const uint32_t reg800_keep_bits[DINO800_REGS] = {
+            MAKE_64BIT_MASK(0, 1),
+            MAKE_64BIT_MASK(0, 7),
+            MAKE_64BIT_MASK(0, 7),
+            MAKE_64BIT_MASK(0, 8),
+            MAKE_64BIT_MASK(0, 7),
+            MAKE_64BIT_MASK(0, 9),
+            MAKE_64BIT_MASK(0, 32),
+            MAKE_64BIT_MASK(0, 8),
+            MAKE_64BIT_MASK(0, 30),
+            MAKE_64BIT_MASK(0, 25),
+            MAKE_64BIT_MASK(0, 22),
+            MAKE_64BIT_MASK(0, 9),
+};
+
 typedef struct DinoState {
     PCIHostState parent_obj;
 
     /* PCI_CONFIG_ADDR is parent_obj.config_reg, via pci_host_conf_be_ops,
        so that we can map PCI_CONFIG_DATA to pci_host_data_be_ops.  */
+    uint32_t config_reg_dino; /* keep original copy, including 2 lowest bits */
 
     uint32_t iar0;
     uint32_t iar1;
@@ -94,8 +112,12 @@ typedef struct DinoState {
     uint32_t ipr;
     uint32_t icr;
     uint32_t ilr;
+    uint32_t io_fbb_en;
     uint32_t io_addr_en;
     uint32_t io_control;
+    uint32_t toc_addr;
+
+    uint32_t reg800[DINO800_REGS];
 
     MemoryRegion this_mem;
     MemoryRegion pci_mem;
@@ -106,8 +128,6 @@ typedef struct DinoState {
     MemoryRegion bm_ram_alias;
     MemoryRegion bm_pci_alias;
     MemoryRegion bm_cpu_alias;
-
-    MemoryRegion cpu0_eir_mem;
 } DinoState;
 
 /*
@@ -122,6 +142,8 @@ static void gsc_to_pci_forwarding(DinoState *s)
     tmp = extract32(s->io_control, 7, 2);
     enabled = (tmp == 0x01);
     io_addr_en = s->io_addr_en;
+    /* Mask out first (=firmware) and last (=Dino) areas. */
+    io_addr_en &= ~(BIT(31) | BIT(0));
 
     memory_region_transaction_begin();
     for (i = 1; i < 31; i++) {
@@ -142,6 +164,8 @@ static bool dino_chip_mem_valid(void *opaque, hwaddr addr,
                                 unsigned size, bool is_write,
                                 MemTxAttrs attrs)
 {
+    bool ret = false;
+
     switch (addr) {
     case DINO_IAR0:
     case DINO_IAR1:
@@ -152,16 +176,22 @@ static bool dino_chip_mem_valid(void *opaque, hwaddr addr,
     case DINO_ICR:
     case DINO_ILR:
     case DINO_IO_CONTROL:
+    case DINO_IO_FBB_EN:
     case DINO_IO_ADDR_EN:
     case DINO_PCI_IO_DATA:
-        return true;
+    case DINO_TOC_ADDR:
+    case DINO_GMASK ... DINO_TLTIM:
+        ret = true;
+        break;
     case DINO_PCI_IO_DATA + 2:
-        return size <= 2;
+        ret = (size <= 2);
+        break;
     case DINO_PCI_IO_DATA + 1:
     case DINO_PCI_IO_DATA + 3:
-        return size == 1;
+        ret = (size == 1);
     }
-    return false;
+    trace_dino_chip_mem_valid(addr, ret);
+    return ret;
 }
 
 static MemTxResult dino_chip_read_with_attrs(void *opaque, hwaddr addr,
@@ -194,6 +224,9 @@ static MemTxResult dino_chip_read_with_attrs(void *opaque, hwaddr addr,
         }
         break;
 
+    case DINO_IO_FBB_EN:
+        val = s->io_fbb_en;
+        break;
     case DINO_IO_ADDR_EN:
         val = s->io_addr_en;
         break;
@@ -227,12 +260,28 @@ static MemTxResult dino_chip_read_with_attrs(void *opaque, hwaddr addr,
     case DINO_IRR1:
         val = s->ilr & s->imr & s->icr;
         break;
+    case DINO_TOC_ADDR:
+        val = s->toc_addr;
+        break;
+    case DINO_GMASK ... DINO_TLTIM:
+        val = s->reg800[(addr - DINO_GMASK) / 4];
+        if (addr == DINO_PAMR) {
+            val &= ~0x01;  /* LSB is hardwired to 0 */
+        }
+        if (addr == DINO_MLTIM) {
+            val &= ~0x07;  /* 3 LSB are hardwired to 0 */
+        }
+        if (addr == DINO_BRDG_FEAT) {
+            val &= ~(0x10710E0ul | 8); /* bits 5-7, 24 & 15 reserved */
+        }
+        break;
 
     default:
         /* Controlled by dino_chip_mem_valid above.  */
         g_assert_not_reached();
     }
 
+    trace_dino_chip_read(addr, val);
     *data = val;
     return ret;
 }
@@ -245,6 +294,9 @@ static MemTxResult dino_chip_write_with_attrs(void *opaque, hwaddr addr,
     AddressSpace *io;
     MemTxResult ret;
     uint16_t ioaddr;
+    int i;
+
+    trace_dino_chip_write(addr, val);
 
     switch (addr) {
     case DINO_IO_DATA ... DINO_PCI_IO_DATA + 3:
@@ -266,9 +318,11 @@ static MemTxResult dino_chip_write_with_attrs(void *opaque, hwaddr addr,
         }
         return ret;
 
+    case DINO_IO_FBB_EN:
+        s->io_fbb_en = val & 0x03;
+        break;
     case DINO_IO_ADDR_EN:
-        /* Never allow first (=firmware) and last (=Dino) areas.  */
-        s->io_addr_en = val & 0x7ffffffe;
+        s->io_addr_en = val;
         gsc_to_pci_forwarding(s);
         break;
     case DINO_IO_CONTROL:
@@ -292,6 +346,10 @@ static MemTxResult dino_chip_write_with_attrs(void *opaque, hwaddr addr,
         /* Any write to IPR clears the register.  */
         s->ipr = 0;
         break;
+    case DINO_TOC_ADDR:
+        /* IO_COMMAND of CPU with client_id bits */
+        s->toc_addr = 0xFFFA0030 | (val & 0x1e000);
+        break;
 
     case DINO_ILR:
     case DINO_IRR0:
@@ -299,6 +357,12 @@ static MemTxResult dino_chip_write_with_attrs(void *opaque, hwaddr addr,
         /* These registers are read-only.  */
         break;
 
+    case DINO_GMASK ... DINO_TLTIM:
+        i = (addr - DINO_GMASK) / 4;
+        val &= reg800_keep_bits[i];
+        s->reg800[i] = val;
+        break;
+
     default:
         /* Controlled by dino_chip_mem_valid above.  */
         g_assert_not_reached();
@@ -323,7 +387,7 @@ static const MemoryRegionOps dino_chip_ops = {
 
 static const VMStateDescription vmstate_dino = {
     .name = "Dino",
-    .version_id = 1,
+    .version_id = 2,
     .minimum_version_id = 1,
     .fields = (VMStateField[]) {
         VMSTATE_UINT32(iar0, DinoState),
@@ -332,13 +396,14 @@ static const VMStateDescription vmstate_dino = {
         VMSTATE_UINT32(ipr, DinoState),
         VMSTATE_UINT32(icr, DinoState),
         VMSTATE_UINT32(ilr, DinoState),
+        VMSTATE_UINT32(io_fbb_en, DinoState),
         VMSTATE_UINT32(io_addr_en, DinoState),
         VMSTATE_UINT32(io_control, DinoState),
+        VMSTATE_UINT32(toc_addr, DinoState),
         VMSTATE_END_OF_LIST()
     }
 };
 
-
 /* Unlike pci_config_data_le_ops, no check of high bit set in config_reg.  */
 
 static uint64_t dino_config_data_read(void *opaque, hwaddr addr, unsigned len)
@@ -362,14 +427,16 @@ static const MemoryRegionOps dino_config_data_ops = {
 
 static uint64_t dino_config_addr_read(void *opaque, hwaddr addr, unsigned len)
 {
-    PCIHostState *s = opaque;
-    return s->config_reg;
+    DinoState *s = opaque;
+    return s->config_reg_dino;
 }
 
 static void dino_config_addr_write(void *opaque, hwaddr addr,
                                    uint64_t val, unsigned len)
 {
     PCIHostState *s = opaque;
+    DinoState *ds = opaque;
+    ds->config_reg_dino = val; /* keep a copy of original value */
     s->config_reg = val & ~3U;
 }
 
@@ -453,6 +520,8 @@ PCIBus *dino_init(MemoryRegion *addr_space,
 
     dev = qdev_create(NULL, TYPE_DINO_PCI_HOST_BRIDGE);
     s = DINO_PCI_HOST_BRIDGE(dev);
+    s->iar0 = s->iar1 = CPU_HPA + 3;
+    s->toc_addr = 0xFFFA0030; /* IO_COMMAND of CPU */
 
     /* Dino PCI access from main memory.  */
     memory_region_init_io(&s->this_mem, OBJECT(s), &dino_chip_ops,
MAINTAINERS +1 -1
index f6511d5120..ff53f43d5d 100644
@@ -890,7 +890,7 @@ F: hw/*/etraxfs_*.c
 
 HP-PARISC Machines
 ------------------
-Dino
+HP B160L
 M: Richard Henderson <[email protected]>
 R: Helge Deller <[email protected]>
 S: Odd Fixes
hw/hppa/trace-events +5 -0
index 4e2acb6176..f943b16c4e 100644
@@ -2,3 +2,8 @@
 
 # pci.c
 hppa_pci_iack_write(void) ""
+
+# dino.c
+dino_chip_mem_valid(uint64_t addr, uint32_t val) "access to addr 0x%"PRIx64" is %d"
+dino_chip_read(uint64_t addr, uint32_t val) "addr 0x%"PRIx64" val 0x%08x"
+dino_chip_write(uint64_t addr, uint32_t val) "addr 0x%"PRIx64" val 0x%08x"
Signature · 1 line
2.20.1
#2 lore raw

[PULL v3 02/11] hppa: Add support for LASI chip with i82596 NIC

In reply to Richard Henderson

From: Helge Deller <[email protected]>

LASI is a built-in multi-I/O chip which supports serial, parallel, network (Intel i82596 Apricot), sound and other functionalities. LASI has been used in many HP PARISC machines.
This patch adds the necessary parts to allow Linux and HP-UX to detect LASI and the network card.

Message-Id: <[email protected]>

---
 hw/hppa/hppa_sys.h          |   2 +
 hw/net/i82596.h             |  55 +++
 include/hw/net/lasi_82596.h |  29 ++
 hw/hppa/lasi.c              | 360 ++++++++++++++++++
 hw/hppa/machine.c           |   8 +-
 hw/net/i82596.c             | 734 ++++++++++++++++++++++++++++++++++++
 hw/net/lasi_i82596.c        | 188 +++++++++
 MAINTAINERS                 |   2 +
 hw/hppa/Kconfig             |   1 +
 hw/hppa/Makefile.objs       |   2 +-
 hw/hppa/trace-events        |   5 +
 hw/net/Kconfig              |   7 +
 hw/net/Makefile.objs        |   2 +
 hw/net/trace-events         |  13 +
 14 files changed, 1406 insertions(+), 2 deletions(-)
 create mode 100644 hw/net/i82596.h
 create mode 100644 include/hw/net/lasi_82596.h
 create mode 100644 hw/hppa/lasi.c
 create mode 100644 hw/net/i82596.c
 create mode 100644 hw/net/lasi_i82596.c
hw/hppa/hppa_sys.h +2 -0
index 4e5019695e..4d08501464 100644
@@ -12,6 +12,8 @@
 #include "hppa_hardware.h"
 
 PCIBus *dino_init(MemoryRegion *, qemu_irq *, qemu_irq *);
+DeviceState *lasi_init(MemoryRegion *);
+#define enable_lasi_lan()       0
 
 #define TYPE_DINO_PCI_HOST_BRIDGE "dino-pcihost"
 
hw/net/i82596.h new file +55 -0

55 lines not on this page · raw message

include/hw/net/lasi_82596.h new file +29 -0
new file mode 100644
index 0000000000..e76ef8308e
@@ -0,0 +1,29 @@
+/*
+ * QEMU LASI i82596 device emulation
+ *
+ * Copyright (c) 201 Helge Deller <[email protected]>
+ *
+ */
+
+#ifndef LASI_82596_H
+#define LASI_82596_H
+
+#include "net/net.h"
+#include "hw/net/i82596.h"
+
+#define TYPE_LASI_82596 "lasi_82596"
+#define SYSBUS_I82596(obj) \
+    OBJECT_CHECK(SysBusI82596State, (obj), TYPE_LASI_82596)
+
+typedef struct {
+    SysBusDevice parent_obj;
+
+    I82596State state;
+    uint16_t last_val;
+    int val_index:1;
+} SysBusI82596State;
+
+SysBusI82596State *lasi_82596_init(MemoryRegion *addr_space,
+                                    hwaddr hpa, qemu_irq irq);
+
+#endif
hw/hppa/lasi.c new file +360 -0

360 lines not on this page · raw message

hw/hppa/machine.c +7 -1
index 5d0de26140..33e3769d0b 100644
@@ -16,6 +16,7 @@
 #include "hw/ide.h"
 #include "hw/timer/i8254.h"
 #include "hw/char/serial.h"
+#include "hw/net/lasi_82596.h"
 #include "hppa_sys.h"
 #include "qemu/units.h"
 #include "qapi/error.h"
@@ -101,6 +102,9 @@ static void machine_hppa_init(MachineState *machine)
                                          "ram", ram_size);
     memory_region_add_subregion(addr_space, 0, ram_region);
 
+    /* Init Lasi chip */
+    lasi_init(addr_space);
+
     /* Init Dino (PCI host bus chip).  */
     pci_bus = dino_init(addr_space, &rtc_irq, &serial_irq);
     assert(pci_bus);
@@ -125,7 +129,9 @@ static void machine_hppa_init(MachineState *machine)
 
     /* Network setup.  e1000 is good enough, failing Tulip support.  */
     for (i = 0; i < nb_nics; i++) {
-        pci_nic_init_nofail(&nd_table[i], pci_bus, "e1000", NULL);
+        if (!enable_lasi_lan()) {
+            pci_nic_init_nofail(&nd_table[i], pci_bus, "e1000", NULL);
+        }
     }
 
     /* Load firmware.  Given that this is not "real" firmware,
hw/net/i82596.c new file +734 -0

734 lines not on this page · raw message

hw/net/lasi_i82596.c new file +188 -0

188 lines not on this page · raw message

MAINTAINERS +2 -0
index ff53f43d5d..efd3f3875f 100644
@@ -178,6 +178,8 @@ S: Maintained
 F: target/hppa/
 F: hw/hppa/
 F: disas/hppa.c
+F: hw/net/*i82596*
+F: include/hw/net/lasi_82596.h
 
 LM32 TCG CPUs
 M: Michael Walle <[email protected]>
hw/hppa/Kconfig +1 -0
index 6e5d74a825..2a7b38d6d6 100644
@@ -10,3 +10,4 @@ config DINO
     select IDE_CMD646
     select MC146818RTC
     select LSI_SCSI_PCI
+    select LASI_82596
hw/hppa/Makefile.objs +1 -1
index 67838f50a3..eac3467d8a 100644
@@ -1 +1 @@
-obj-$(CONFIG_DINO) += pci.o machine.o dino.o
+obj-$(CONFIG_DINO) += pci.o machine.o dino.o lasi.o
hw/hppa/trace-events +5 -0
index f943b16c4e..3ff620319a 100644
@@ -7,3 +7,8 @@ hppa_pci_iack_write(void) ""
 dino_chip_mem_valid(uint64_t addr, uint32_t val) "access to addr 0x%"PRIx64" is %d"
 dino_chip_read(uint64_t addr, uint32_t val) "addr 0x%"PRIx64" val 0x%08x"
 dino_chip_write(uint64_t addr, uint32_t val) "addr 0x%"PRIx64" val 0x%08x"
+
+# lasi.c
+lasi_chip_mem_valid(uint64_t addr, uint32_t val) "access to addr 0x%"PRIx64" is %d"
+lasi_chip_read(uint64_t addr, uint32_t val) "addr 0x%"PRIx64" val 0x%08x"
+lasi_chip_write(uint64_t addr, uint32_t val) "addr 0x%"PRIx64" val 0x%08x"
hw/net/Kconfig +7 -0
index af6a11baf3..54411d3dcc 100644
@@ -31,6 +31,9 @@ config TULIP
     depends on PCI
     select NMC93XX_EEPROM
 
+config I82596_COMMON
+    bool
+
 config E1000_PCI
     bool
     default y if PCI_DEVICES
@@ -89,6 +92,10 @@ config LANCE
     bool
     select PCNET_COMMON
 
+config LASI_82596
+    bool
+    select I82596_COMMON
+
 config SUNHME
     bool
 
hw/net/Makefile.objs +2 -0
index 7907d2c199..19f13e9fa5 100644
@@ -28,6 +28,8 @@ common-obj-$(CONFIG_IMX_FEC) += imx_fec.o
 common-obj-$(CONFIG_CADENCE) += cadence_gem.o
 common-obj-$(CONFIG_STELLARIS_ENET) += stellaris_enet.o
 common-obj-$(CONFIG_LANCE) += lance.o
+common-obj-$(CONFIG_LASI_82596) += lasi_i82596.o
+common-obj-$(CONFIG_I82596_COMMON) += i82596.o
 common-obj-$(CONFIG_SUNHME) += sunhme.o
 common-obj-$(CONFIG_FTGMAC100) += ftgmac100.o
 common-obj-$(CONFIG_SUNGEM) += sungem.o
hw/net/trace-events +13 -0
index e70f12bee1..42066fcf66 100644
@@ -381,3 +381,16 @@ tulip_mii_read(int phy, int reg, uint16_t data) "phy 0x%x, reg 0x%x data 0x%04x"
 tulip_reset(void) ""
 tulip_setup_frame(void) ""
 tulip_setup_filter(int n, uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f) "%d: %02x:%02x:%02x:%02x:%02x:%02x"
+
+# lasi_i82596.c
+lasi_82596_mem_readw(uint64_t addr, uint32_t ret) "addr=0x%"PRIx64" val=0x%04x"
+lasi_82596_mem_writew(uint64_t addr, uint32_t val) "addr=0x%"PRIx64" val=0x%04x"
+
+# i82596.c
+i82596_s_reset(void *s) "%p Reset chip"
+i82596_transmit(uint32_t size, uint32_t addr) "size %u from addr 0x%04x"
+i82596_receive_analysis(const char *s) "%s"
+i82596_receive_packet(size_t sz) "len=%zu"
+i82596_new_mac(const char *id_with_mac) "New MAC for: %s"
+i82596_set_multicast(uint16_t count) "Added %d multicast entries"
+i82596_channel_attention(void *s) "%p: Received CHANNEL ATTENTION"
Signature · 1 line
2.20.1
#3 lore raw

Re: [PULL v3 02/11] hppa: Add support for LASI chip with i82596 NIC

Quoted 9 lines · Richard Henderson

From: Helge Deller <[email protected]>

LASI is a built-in multi-I/O chip which supports serial, parallel, network (Intel i82596 Apricot), sound and other functionalities. LASI has been used in many HP PARISC machines.
This patch adds the necessary parts to allow Linux and HP-UX to detect LASI and the network card.

Quoted 4 lines

Message-Id: <[email protected]>

Hi; Coverity has an issue with this code (CID 1419396):

Quoted 7 lines
hw/net/i82596.c new file +5 -0
+#define ETHER_TYPE_LEN 2
+#define VLAN_TCI_LEN 2
+#define VLAN_HLEN (ETHER_TYPE_LEN + VLAN_TCI_LEN)
 
 
+#define MIN_BUF_SIZE 60
+

hw/net/i82596.c line 508

Quoted 10 lines
hw/net/i82596.c new file +10 -0
+ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
+{
+    I82596State *s = qemu_get_nic_opaque(nc);
+    uint32_t rfd_p;
+    uint32_t rbd;
+    uint16_t is_broadcast = 0;
+    size_t len = sz;
+    uint32_t crc;
+    uint8_t *crc_ptr;
+    uint8_t buf1[MIN_BUF_SIZE + VLAN_HLEN];

This buffer is 60 + 2 + 2 == 64 bytes large...

hw/net/i82596.c line 598

Quoted 8 lines
hw/net/i82596.c new file +8 -0
+    /* if too small buffer, then expand it */
+    if (len < MIN_BUF_SIZE + VLAN_HLEN) {
+        memcpy(buf1, buf, len);
+        memset(buf1 + len, 0, MIN_BUF_SIZE + VLAN_HLEN - len);
+        buf = buf1;
+        if (len < MIN_BUF_SIZE) {
+            len = MIN_BUF_SIZE;
+        }

...here if we're using the buf1[] buffer then len must be >= MIN_BUF_SIZE (60) and < MIN_BUF_SIZE + VLAN_HLEN (64), so it's in the range 60 to 63...

hw/net/i82596.c line 604

Quoted 6 lines
hw/net/i82596.c new file +6 -0
+    }
+
+    /* Calculate the ethernet checksum (4 bytes) */
+    len += 4;
+    crc = cpu_to_be32(crc32(~0, buf, sz));
+    crc_ptr = (uint8_t *) &crc;

...but then we add 4 to len here, so it's now 64 to 67...

hw/net/i82596.c line 640

Quoted 8 lines
hw/net/i82596.c new file +5 -0
+    while (len) {
 
 
 
+            num = buffer_size & SIZE_MASK;
+            if (num > len) {
+                num = len;
+            }

...before using len as the cap on how many bytes we write...

hw/net/i82596.c line 644

Quoted 4 lines
hw/net/i82596.c new file +4 -0
+            rba = get_uint32(rbd + 8);
+            /* printf("rba is 0x%x\n", rba); */
+            address_space_rw(&address_space_memory, rba,
+                MEMTXATTRS_UNSPECIFIED, (void *)buf, num, 1);

...from the buffer into guest memory here.

So we could be reading off the end of the buffer.

I don't know whether the buffer should be 4 bytes larger to allow for the checksum, or if the len calculation is wrong.

PS: I think calling address_space_write() with a constant final argument is confusing; "address_space_rw(as, addr, attrs, buf, len, 1)" is equivalent to
"address_space_write(as, addr, attrs, buf, len)", except that it's more obvious that it's a write rather than a read, and it avoids an extra layer of function call. We do seem to have a surprisingly large number of places in the codebase that call address_space_rw() with a constant final argument, though...

thanks
-- PMM

#4 lore raw

Re: [PULL v3 02/11] hppa: Add support for LASI chip with i82596 NIC

Quoted 77 lines · Peter Maydell
Quoted 9 lines · Richard Henderson

From: Helge Deller <[email protected]>

LASI is a built-in multi-I/O chip which supports serial, parallel, network (Intel i82596 Apricot), sound and other functionalities. LASI has been used in many HP PARISC machines.
This patch adds the necessary parts to allow Linux and HP-UX to detect LASI and the network card.

Quoted 4 lines

Message-Id: <[email protected]>

Hi; Coverity has an issue with this code (CID 1419396):

Quoted 7 lines
hw/net/i82596.c new file +5 -0
+#define ETHER_TYPE_LEN 2
+#define VLAN_TCI_LEN 2
+#define VLAN_HLEN (ETHER_TYPE_LEN + VLAN_TCI_LEN)
 
 
+#define MIN_BUF_SIZE 60
+

hw/net/i82596.c line 508

Quoted 10 lines
hw/net/i82596.c new file +10 -0
+ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
+{
+    I82596State *s = qemu_get_nic_opaque(nc);
+    uint32_t rfd_p;
+    uint32_t rbd;
+    uint16_t is_broadcast = 0;
+    size_t len = sz;
+    uint32_t crc;
+    uint8_t *crc_ptr;
+    uint8_t buf1[MIN_BUF_SIZE + VLAN_HLEN];

This buffer is 60 + 2 + 2 == 64 bytes large...

hw/net/i82596.c line 598

Quoted 8 lines
hw/net/i82596.c new file +8 -0
+    /* if too small buffer, then expand it */
+    if (len < MIN_BUF_SIZE + VLAN_HLEN) {
+        memcpy(buf1, buf, len);
+        memset(buf1 + len, 0, MIN_BUF_SIZE + VLAN_HLEN - len);
+        buf = buf1;
+        if (len < MIN_BUF_SIZE) {
+            len = MIN_BUF_SIZE;
+        }

...here if we're using the buf1[] buffer then len must be >= MIN_BUF_SIZE (60) and < MIN_BUF_SIZE + VLAN_HLEN (64), so it's in the range 60 to 63...

hw/net/i82596.c line 604

Quoted 6 lines
hw/net/i82596.c new file +6 -0
+    }
+
+    /* Calculate the ethernet checksum (4 bytes) */
+    len += 4;
+    crc = cpu_to_be32(crc32(~0, buf, sz));
+    crc_ptr = (uint8_t *) &crc;

...but then we add 4 to len here, so it's now 64 to 67...

hw/net/i82596.c line 640

Quoted 8 lines
hw/net/i82596.c new file +5 -0
+    while (len) {
 
 
 
+            num = buffer_size & SIZE_MASK;
+            if (num > len) {
+                num = len;
+            }

...before using len as the cap on how many bytes we write...

hw/net/i82596.c line 644

Quoted 4 lines
hw/net/i82596.c new file +4 -0
+            rba = get_uint32(rbd + 8);
+            /* printf("rba is 0x%x\n", rba); */
+            address_space_rw(&address_space_memory, rba,
+                MEMTXATTRS_UNSPECIFIED, (void *)buf, num, 1);
Quoted 7 lines

...from the buffer into guest memory here.

So we could be reading off the end of the buffer.

I don't know whether the buffer should be 4 bytes larger to allow for the checksum, or if the len calculation is wrong.

I'm working on a bigger patch which will improve this driver. It still has some issues with the emulation in Linux and HP-UX. With the patch I will take try to fix those out-of-bounds accesses too.
It will take some time though, until I will send the patch.

Quoted 10 lines

PS: I think calling address_space_write() with a constant final argument is confusing; "address_space_rw(as, addr, attrs, buf, len, 1)" is equivalent to
"address_space_write(as, addr, attrs, buf, len)", except that it's more obvious that it's a write rather than a read, and it avoids an extra layer of function call. We do seem to have a surprisingly large number of places in the codebase that call address_space_rw() with a constant final argument, though...

Thanks for cleaning this up in the meantime!

Helge

#5 lore raw

Re: [PULL v3 02/11] hppa: Add support for LASI chip with i82596 NIC

Quoted 19 lines · Helge Deller
Quoted 11 lines · Peter Maydell
Quoted 8 lines · Richard Henderson

From: Helge Deller <[email protected]>

LASI is a built-in multi-I/O chip which supports serial, parallel, network (Intel i82596 Apricot), sound and other functionalities. LASI has been used in many HP PARISC machines.
This patch adds the necessary parts to allow Linux and HP-UX to detect LASI and the network card.

Quoted 5 lines

So we could be reading off the end of the buffer.

I don't know whether the buffer should be 4 bytes larger to allow for the checksum, or if the len calculation is wrong.

Quoted 5 lines

I'm working on a bigger patch which will improve this driver. It still has some issues with the emulation in Linux and HP-UX. With the patch I will take try to fix those out-of-bounds accesses too.
It will take some time though, until I will send the patch.

As this is a bug fix and in particular a fix for a buffer overrun, I think it would be better if you could send a patch that just fixes this. If you include it in a larger general-improvements patch we'll probably just ask you to split it out when we get to code-review of that patchset, and doing it that way will mean we have the buggy code in QEMU for longer.

thanks
-- PMM

#6 lore raw

Re: [PULL v3 02/11] hppa: Add support for LASI chip with i82596 NIC

* Peter Maydell <[email protected]>:

Quoted 26 lines · Helge Deller
Quoted 19 lines · Peter Maydell
Quoted 11 lines
Quoted 8 lines · Richard Henderson

From: Helge Deller <[email protected]>

LASI is a built-in multi-I/O chip which supports serial, parallel, network (Intel i82596 Apricot), sound and other functionalities. LASI has been used in many HP PARISC machines.
This patch adds the necessary parts to allow Linux and HP-UX to detect LASI and the network card.

Quoted 5 lines

So we could be reading off the end of the buffer.

I don't know whether the buffer should be 4 bytes larger to allow for the checksum, or if the len calculation is wrong.

Quoted 5 lines

I'm working on a bigger patch which will improve this driver. It still has some issues with the emulation in Linux and HP-UX. With the patch I will take try to fix those out-of-bounds accesses too.
It will take some time though, until I will send the patch.

Quoted 3 lines

As this is a bug fix and in particular a fix for a buffer overrun, I think it would be better if you could send a patch that just fixes this.

Of course I usually agree on that, but in this special case the initial patch was more or less the basic functionality that the NIC gets detected. The driver is nevertheless full of bugs, so even if I'd try to get this specific buffer overrun fixed, it will not give you a working NIC emulation. Such a patch would just hide the buffer overrun without any real benefit.

Quoted 4 lines

If you include it in a larger general-improvements patch we'll probably just ask you to split it out when we get to code-review of that patchset, and doing it that way will mean we have the buggy code in QEMU for longer.

I've done big progress with my fixes. Below you will find the current patch against qemu git head which does work with a Linux guest. It's not perfect yet either, but the Linux guest gets an IP address, apt-get update works, same as pings and such. It's still work-in-progress, so please don't look too deep into it yet. I'm aware of the rough edges which I plan to clean up in the next few days, but I attached it here for your reference in the hope that you can see that there is much more work than just trying to fix some static code analysis output.

That said, I would be happy to further clean it up, split it into logically seperate patches and send them upstream in a few days for review if you agree. I think with this approach we gain more than trying to "fix" the old buggy code.

Thoughts?

Helge

hw/net/i82596.c +222 -156
index fe9f2390a9..f8d55601bc 100644
@@ -27,29 +27,29 @@
 #define DBG(x)          do { } while (0)
 #endif
 
-#define USE_TIMER       0
-
 #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
 
-#define PKT_BUF_SZ      1536
 #define MAX_MC_CNT      64
 
 #define ISCP_BUSY       0x0001
 
 #define I596_NULL       ((uint32_t)0xffffffff)
 
-#define SCB_STATUS_CX   0x8000 /* CU finished command with I bit */
-#define SCB_STATUS_FR   0x4000 /* RU finished receiving a frame */
-#define SCB_STATUS_CNA  0x2000 /* CU left active state */
-#define SCB_STATUS_RNR  0x1000 /* RU left active state */
+#define SCB_STAT_CX     0x8000 /* CU finished command with I bit */
+#define SCB_STAT_FR     0x4000 /* RU finished receiving a frame */
+#define SCB_STAT_CNA    0x2000 /* CU left active state */
+#define SCB_STAT_RNR    0x1000 /* RU left active state */
 
-#define CU_IDLE         0
+#define CU_IDLE         0       /* CUS values */
 #define CU_SUSPENDED    1
 #define CU_ACTIVE       2
 
-#define RX_IDLE         0
+#define RX_IDLE         0       /* RUS values */
 #define RX_SUSPENDED    1
+#define RX_NO_RESOURCES 2
 #define RX_READY        4
+#define RX_NO_RESO_RBD  (8 + RX_NO_RESOURCES)
+#define RX_NO_MORE_RBD  (8 + RX_READY)
 
 #define CMD_EOL         0x8000  /* The last command of the list, stop. */
 #define CMD_SUSP        0x4000  /* Suspend after doing cmd. */
@@ -76,10 +76,13 @@ enum commands {
 
 /* various flags in the chip config registers */
 #define I596_PREFETCH   (s->config[0] & 0x80)
+#define I596_NO_SRC_ADD_IN (s->config[3] & 0x08) /* if 1, do not insert MAC in Tx Packet */
+#define I596_LOOPBACK   (s->config[3] >> 6)     /* loopback mode, 3 = external loopback */
 #define I596_PROMISC    (s->config[8] & 0x01)
-#define I596_BC_DISABLE (s->config[8] & 0x02) /* broadcast disable */
-#define I596_NOCRC_INS  (s->config[8] & 0x08)
-#define I596_CRCINM     (s->config[11] & 0x04) /* CRC appended */
+#define I596_BC_DISABLE (s->config[8] & 0x02)   /* broadcast disable */
+#define I596_NOCRC_INS  (s->config[8] & 0x08)   /* do not append CRC to Tx frame */
+#define I596_CRC16_32   (s->config[8] & 0x10)   /* CRC-16 or CRC-32 */
+#define I596_CRCINM     (s->config[11] & 0x04)  /* Rx CRC appended in memory */
 #define I596_MC_ALL     (s->config[11] & 0x20)
 #define I596_MULTIIA    (s->config[13] & 0x40)
 
@@ -134,9 +137,14 @@ struct qemu_ether_header {
 static void i82596_transmit(I82596State *s, uint32_t addr)
 {
     uint32_t tdb_p; /* Transmit Buffer Descriptor */
+    uint16_t cmd;
+    int insert_crc;
 
-    /* TODO: Check flexible mode */
+    cmd = get_uint16(addr + 2);
+    assert(cmd & 8);    /* check flexible mode */
     tdb_p = get_uint32(addr + 8);
+    /* check NC bit and possibly insert CRC */
+    insert_crc = (I596_NOCRC_INS == 0) && ((cmd & 0x10) == 0) && !I596_LOOPBACK;
     while (tdb_p != I596_NULL) {
         uint16_t size, len;
         uint32_t tba;
@@ -147,16 +155,43 @@ static void i82596_transmit(I82596State *s, uint32_t addr)
         trace_i82596_transmit(len, tba);
 
         if (s->nic && len) {
-            assert(len <= sizeof(s->tx_buffer));
+            uint16_t new_len;
+            new_len = len + 4;
+            assert(new_len <= sizeof(s->tx_buffer));
             address_space_read(&address_space_memory, tba,
                                MEMTXATTRS_UNSPECIFIED, s->tx_buffer, len);
+
+            if (I596_NO_SRC_ADD_IN == 0) {
+                /* insert MAC in Tx Packet */
+                memcpy(&s->tx_buffer[ETH_ALEN], s->conf.macaddr.a, ETH_ALEN);
+            }
+
+            DBG(printf("i82596_transmit: insert_crc = %d  insert SRC = %d\n",
+                        insert_crc, I596_NO_SRC_ADD_IN == 0));
+            if (insert_crc) {
+                uint32_t crc = crc32(~0, s->tx_buffer, len);
+                crc = cpu_to_be32(crc);
+                memcpy(&s->tx_buffer[len], &crc, sizeof(crc));
+                len += sizeof(crc);
+            }
+
             DBG(PRINT_PKTHDR("Send", &s->tx_buffer));
-            DBG(printf("Sending %d bytes\n", len));
-            qemu_send_packet(qemu_get_queue(s->nic), s->tx_buffer, len);
+            DBG(printf("Sending %d bytes (crc_inserted=%d)\n", len, insert_crc));
+            switch (I596_LOOPBACK) {
+            case 0:     /* no loopback, send packet */
+                qemu_send_packet_raw(qemu_get_queue(s->nic), s->tx_buffer, len);
+                break;
+            case 1:     /* external loopback enabled */
+                i82596_receive(qemu_get_queue(s->nic), s->tx_buffer, len);
+                break;
+            default:    /* all other loopback modes: ignore! */
+                break;
+            }
         }
 
         /* was this the last package? */
         if (size & I596_EOF) {
+            qemu_flush_queued_packets(qemu_get_queue(s->nic));
             break;
         }
 
@@ -175,6 +210,7 @@ static void set_individual_address(I82596State *s, uint32_t addr)
     address_space_read(&address_space_memory, addr + 8,
                        MEMTXATTRS_UNSPECIFIED, m, ETH_ALEN);
     qemu_format_nic_info_str(nc, m);
+    DBG(printf("MAC addr set to %s\n", nc->info_str));
     trace_i82596_new_mac(nc->info_str);
 }
 
@@ -188,6 +224,7 @@ static void set_multicast_list(I82596State *s, uint32_t addr)
     if (mc_count > MAX_MC_CNT) {
         mc_count = MAX_MC_CNT;
     }
+    DBG(printf("Add %d multicast entries.\n", mc_count));
     for (i = 0; i < mc_count; i++) {
         uint8_t multicast_addr[ETH_ALEN];
         address_space_read(&address_space_memory, addr + i * ETH_ALEN,
@@ -212,7 +249,9 @@ void i82596_set_link_status(NetClientState *nc)
 static void update_scb_status(I82596State *s)
 {
     s->scb_status = (s->scb_status & 0xf000)
-        | (s->cu_status << 8) | (s->rx_status << 4);
+        | (s->CUS << 8) | (s->RUS << 4)
+        | 8 /* 8: bus throttle timers loaded */;
+    DBG(printf("update_scb_status 0x%04x CUS: %d, RUS: %d\n", s->scb_status, s->CUS, s->RUS));
     set_uint16(s->scb, s->scb_status);
 }
 
@@ -220,13 +259,13 @@ static void update_scb_status(I82596State *s)
 static void i82596_s_reset(I82596State *s)
 {
     trace_i82596_s_reset(s);
-    s->scp = 0;
+    DBG(printf("i82596_s_reset()\n"));
+    s->scp = 0x00FFFFF4;
     s->scb_status = 0;
-    s->cu_status = CU_IDLE;
-    s->rx_status = RX_SUSPENDED;
+    s->CUS = CU_IDLE;
+    s->RUS = RX_IDLE;
     s->cmd_p = I596_NULL;
     s->lnkst = 0x8000; /* initial link state: up */
-    s->ca = s->ca_active = 0;
     s->send_irq = 0;
 }
 
@@ -237,7 +276,7 @@ static void command_loop(I82596State *s)
     uint16_t status;
     uint8_t byte_cnt;
 
-    DBG(printf("STARTING COMMAND LOOP cmd_p=%08x\n", s->cmd_p));
+    DBG(printf("STARTING COMMAND LOOP cmd_p=0x%08x\n", s->cmd_p));
 
     while (s->cmd_p != I596_NULL) {
         /* set status */
@@ -246,7 +285,8 @@ static void command_loop(I82596State *s)
         status = STAT_C | STAT_OK; /* update, but write later */
 
         cmd = get_uint16(s->cmd_p + 2);
-        DBG(printf("Running command %04x at %08x\n", cmd, s->cmd_p));
+        DBG(printf("Running command 0x%04x (cmd %d) at 0x%08x\n",
+                cmd, cmd & 7, s->cmd_p));
 
         switch (cmd & 0x07) {
         case CmdNOp:
@@ -264,11 +304,17 @@ static void command_loop(I82596State *s)
             /* config byte according to page 35ff */
             s->config[2] &= 0x82; /* mask valid bits */
             s->config[2] |= 0x40;
+            DBG(printf("I596_CONFIG3 = 0x%02x  LOOPBACK 0x%x\n", s->config[3], I596_LOOPBACK));
+            if (I596_NO_SRC_ADD_IN == 0) {
+                assert((s->config[3] & 0x07) == ETH_ALEN);
+            }
             s->config[7]  &= 0xf7; /* clear zero bit */
-            assert(I596_NOCRC_INS == 0); /* do CRC insertion */
+            assert(I596_CRC16_32 == 0); /* only CRC-32 implemented */
+            DBG(printf("I596_CRCINM = %d\n\n", I596_CRCINM));
             s->config[10] = MAX(s->config[10], 5); /* min frame length */
             s->config[12] &= 0x40; /* only full duplex field valid */
             s->config[13] |= 0x3f; /* set ones in byte 13 */
+            s->scb_status |= SCB_STAT_CX;
             break;
         case CmdTDR:
             /* get signal LINK */
@@ -290,7 +336,7 @@ static void command_loop(I82596State *s)
         set_uint16(s->cmd_p, status);
 
         s->cmd_p = get_uint32(s->cmd_p + 4); /* get link address */
-        DBG(printf("NEXT addr would be %08x\n", s->cmd_p));
+        DBG(printf("NEXT loop addr is 0x%08x\n", s->cmd_p));
         if (s->cmd_p == 0) {
             s->cmd_p = I596_NULL;
         }
@@ -301,71 +347,51 @@ static void command_loop(I82596State *s)
         }
         /* Suspend after doing cmd? */
         if (cmd & CMD_SUSP) {
-            s->cu_status = CU_SUSPENDED;
-            printf("FIXME SUSPEND !!\n");
+            s->CUS = CU_SUSPENDED;
+            printf("FIXME SUSPEND ?\n");
         }
-        /* Interrupt after doing cmd? */
-        if (cmd & CMD_INTR) {
-            s->scb_status |= SCB_STATUS_CX;
-        } else {
-            s->scb_status &= ~SCB_STATUS_CX;
-        }
-        update_scb_status(s);
 
         /* Interrupt after doing cmd? */
         if (cmd & CMD_INTR) {
+            s->scb_status |= SCB_STAT_CX;
             s->send_irq = 1;
         }
 
-        if (s->cu_status != CU_ACTIVE) {
+        if (s->CUS == CU_SUSPENDED) {
             break;
         }
     }
     DBG(printf("FINISHED COMMAND LOOP\n"));
-    qemu_flush_queued_packets(qemu_get_queue(s->nic));
-}
-
-static void i82596_flush_queue_timer(void *opaque)
-{
-    I82596State *s = opaque;
-    if (0) {
-        timer_del(s->flush_queue_timer);
-        qemu_flush_queued_packets(qemu_get_queue(s->nic));
-        timer_mod(s->flush_queue_timer,
-              qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000);
-    }
 }
 
 static void examine_scb(I82596State *s)
 {
-    uint16_t command, cuc, ruc;
+    uint16_t command, cuc, ruc, c;
 
     /* get the scb command word */
     command = get_uint16(s->scb + 2);
+    DBG(printf("COMMAND = 0x%04x\n", command));
     cuc = (command >> 8) & 0x7;
     ruc = (command >> 4) & 0x7;
-    DBG(printf("MAIN COMMAND %04x  cuc %02x ruc %02x\n", command, cuc, ruc));
-    /* and clear the scb command word */
-    set_uint16(s->scb + 2, 0);
+    DBG(printf("MAIN CU COMMAND 0x%04x: stat 0x%02x cuc 0x%02x ruc 0x%02x\n",
+            command, command >> 12,  cuc, ruc));
 
-    if (command & BIT(31))      /* ACK-CX */
-        s->scb_status &= ~SCB_STATUS_CX;
-    if (command & BIT(30))      /*ACK-FR */
-        s->scb_status &= ~SCB_STATUS_FR;
-    if (command & BIT(29))      /*ACK-CNA */
-        s->scb_status &= ~SCB_STATUS_CNA;
-    if (command & BIT(28))      /*ACK-RNR */
-        s->scb_status &= ~SCB_STATUS_RNR;
+    /* toggle the STAT flags in SCB status word */
+    c = command & (SCB_STAT_CX | SCB_STAT_FR | SCB_STAT_CNA | SCB_STAT_RNR);
+    s->scb_status &= ~c;
 
     switch (cuc) {
     case 0:     /* no change */
+    case 5:
+    case 6:
         break;
     case 1:     /* CUC_START */
-        s->cu_status = CU_ACTIVE;
+        s->CUS = CU_ACTIVE;
         break;
     case 4:     /* CUC_ABORT */
-        s->cu_status = CU_SUSPENDED;
-        s->scb_status |= SCB_STATUS_CNA; /* CU left active state */
+        s->CUS = CU_IDLE;
+        s->scb_status |= SCB_STAT_CNA; /* CU left active state */
+        s->send_irq = 1;
         break;
     default:
         printf("WARNING: Unknown CUC %d!\n", cuc);
@@ -376,16 +402,17 @@ static void examine_scb(I82596State *s)
         break;
     case 1:     /* RX_START */
     case 2:     /* RX_RESUME */
-        s->rx_status = RX_IDLE;
-        if (USE_TIMER) {
-            timer_mod(s->flush_queue_timer, qemu_clock_get_ms(
-                                QEMU_CLOCK_VIRTUAL) + 1000);
-        }
+        s->RUS = RX_READY;
         break;
     case 3:     /* RX_SUSPEND */
+        s->RUS = RX_SUSPENDED;
+        s->scb_status |= SCB_STAT_RNR; /* RU left active state */
+        s->send_irq = 1;
+        break;
     case 4:     /* RX_ABORT */
-        s->rx_status = RX_SUSPENDED;
-        s->scb_status |= SCB_STATUS_RNR; /* RU left active state */
+        s->RUS = RX_IDLE;
+        s->scb_status |= SCB_STAT_RNR; /* RU left active state */
+        s->send_irq = 1;
         break;
     default:
         printf("WARNING: Unknown RUC %d!\n", ruc);
@@ -396,66 +423,92 @@ static void examine_scb(I82596State *s)
     }
 
     /* execute commands from SCBL */
-    if (s->cu_status != CU_SUSPENDED) {
+    if (s->CUS == CU_ACTIVE) {
         if (s->cmd_p == I596_NULL) {
             s->cmd_p = get_uint32(s->scb + 4);
         }
+        command_loop(s);
+        s->CUS = CU_IDLE;
+        s->send_irq = 1;
     }
 
-    /* update scb status */
-    update_scb_status(s);
-
-    command_loop(s);
+    qemu_flush_queued_packets(qemu_get_queue(s->nic));
 }
 
 static void signal_ca(I82596State *s)
 {
-    uint32_t iscp = 0;
+    DBG(printf("-- CA start\n"));
 
     /* trace_i82596_channel_attention(s); */
     if (s->scp) {
+        uint32_t iscp;
+        uint8_t sysbus;
+        uint8_t mode;       /* MODE_82586 or MODE_LINEAR */
+
         /* CA after reset -> do init with new scp. */
-        s->sysbus = get_byte(s->scp + 3); /* big endian */
-        DBG(printf("SYSBUS = %08x\n", s->sysbus));
-        if (((s->sysbus >> 1) & 0x03) != 2) {
-            printf("WARNING: NO LINEAR MODE !!\n");
-        }
-        if ((s->sysbus >> 7)) {
+        sysbus = get_byte(s->scp + 3); /* big endian */
+        DBG(printf("SYSBUS = %08x\n", sysbus));
+        mode = (sysbus >> 1) & 0x03;
+        /* Only MODE_LINEAR is currently implemented. */
+        assert(mode == MODE_LINEAR);
+        if ((sysbus >> 7)) {
             printf("WARNING: 32BIT LINMODE IN B-STEPPING NOT SUPPORTED !!\n");
         }
         iscp = get_uint32(s->scp + 8);
         s->scb = get_uint32(iscp + 4);
+        DBG(printf("ISCP = 0x%08x, SCB = 0x%08x\n", iscp,s->scb));
+        /* set_uint32(iscp + 4, 0); NOT: clear SCB pointer */
         set_byte(iscp + 1, 0); /* clear BUSY flag in iscp */
+        /* sets CX and CNR to equal 1 in the SCB, clears the SCB command word,
+         * sends an interrupt to the CPU, and awaits another CA signal */
+        s->scb_status = SCB_STAT_CX | SCB_STAT_CNA;
+        s->CUS = CU_IDLE;
+        s->RUS = RX_IDLE;
         s->scp = 0;
+        s->send_irq = 1;
+        goto _cont;
     }
 
-    s->ca++;    /* count ca() */
-    if (!s->ca_active) {
-        s->ca_active = 1;
-        while (s->ca)   {
-            examine_scb(s);
-            s->ca--;
-        }
-        s->ca_active = 0;
-    }
+    examine_scb(s);
+
+_cont:
+    /* update scb status */
+    update_scb_status(s);
+
+    /* and clear the scb command word */
+    set_uint16(s->scb + 2, 0);
 
     if (s->send_irq) {
         s->send_irq = 0;
+        DBG(printf("Send IRQ\n"));
         qemu_set_irq(s->irq, 1);
     }
+    DBG(printf("-- CA end\n"));
 }
 
 void i82596_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
 {
     I82596State *s = opaque;
-    /* printf("i82596_ioport_writew addr=0x%08x val=0x%04x\n", addr, val); */
-    switch (addr) {
+    uint32_t res, tmp;
+    DBG(printf("i82596_ioport_writew addr=0x%08x val=0x%04x\n", addr, val));
+    switch (addr & PORT_BYTEMASK) {
     case PORT_RESET: /* Reset */
         i82596_s_reset(s);
         break;
+    case PORT_SELFTEST:
+        res = val + sizeof(uint32_t);
+        tmp = get_uint32(res); /* should be -1 */
+        DBG(printf("i82596 SELFTEST at 0x%04x val 0x%04x requested.\n", res, tmp));
+        assert(tmp == I596_NULL);
+        set_uint32(res, 0); /* set to zero */
+        break;
     case PORT_ALTSCP:
+        DBG(printf("i82596 ALTSCP requested.\n"));
         s->scp = val;
         break;
+    case PORT_ALTDUMP:
+        printf("i82596 PORT_ALTDUMP not implemented yet.\n");
+        break;
     case PORT_CA:
         signal_ca(s);
         break;
@@ -478,18 +531,15 @@ int i82596_can_receive(NetClientState *nc)
 {
     I82596State *s = qemu_get_nic_opaque(nc);
 
-    if (s->rx_status == RX_SUSPENDED) {
-        return 0;
+    if (s->RUS != RX_READY) {
+        /* return 0; */
     }
 
+    /* Link down? */
     if (!s->lnkst) {
         return 0;
     }
 
-    if (USE_TIMER && !timer_pending(s->flush_queue_timer)) {
-        return 1;
-    }
-
     return 1;
 }
 
@@ -500,7 +550,7 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
     I82596State *s = qemu_get_nic_opaque(nc);
     uint32_t rfd_p;
     uint32_t rbd;
-    uint16_t is_broadcast = 0;
+    uint16_t status, is_broadcast = 0;
     size_t len = sz;
     uint32_t crc;
     uint8_t *crc_ptr;
@@ -508,14 +558,10 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
     static const uint8_t broadcast_macaddr[6] = {
                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
-    DBG(printf("i82596_receive() start\n"));
-
-    if (USE_TIMER && timer_pending(s->flush_queue_timer)) {
-        return 0;
-    }
+    DBG(printf("i82596_receive() start, sz = %lu\n", sz));
 
     /* first check if receiver is enabled */
-    if (s->rx_status == RX_SUSPENDED) {
+    if (s->RUS == RX_SUSPENDED) {
         trace_i82596_receive_analysis(">>> Receiving suspended");
         return -1;
     }
@@ -527,14 +573,14 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
 
     /* Received frame smaller than configured "min frame len"? */
     if (sz < s->config[10]) {
-        printf("Received frame too small, %zu vs. %u bytes\n",
-               sz, s->config[10]);
-        return -1;
+        if (0) printf("Received frame too small, %lu vs. %u bytes\n",
+            sz, s->config[10]);
+        sz = 60; /* return -1; */
     }
 
     DBG(printf("Received %lu bytes\n", sz));
 
-    if (I596_PROMISC) {
+    if (I596_PROMISC || I596_LOOPBACK) {
 
         /* promiscuous: receive all */
         trace_i82596_receive_analysis(
@@ -598,69 +644,90 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
     }
 
     /* Calculate the ethernet checksum (4 bytes) */
-    len += 4;
-    crc = cpu_to_be32(crc32(~0, buf, sz));
-    crc_ptr = (uint8_t *) &crc;
-
-    rfd_p = get_uint32(s->scb + 8); /* get Receive Frame Descriptor */
-    assert(rfd_p && rfd_p != I596_NULL);
+    if (I596_CRCINM && !I596_LOOPBACK) {
+        len += 4;
+        crc = crc32(~0, buf, sz);
+        crc = cpu_to_be32(crc);
+        crc_ptr = (uint8_t *) &crc;
+    }
 
-    /* get first Receive Buffer Descriptor Address */
-    rbd = get_uint32(rfd_p + 8);
-    assert(rbd && rbd != I596_NULL);
+    rfd_p = get_uint32(s->scb + 8); /* get initial Receive Frame Descriptor */
+    do {
+        assert(rfd_p && rfd_p != I596_NULL);
+        status = get_uint16(rfd_p+0);
+        /* if rfd is filled, get next one from link addr */
+        if (status & STAT_OK)
+            rfd_p = get_uint32(rfd_p+4);
+    } while (status & STAT_OK);
 
     trace_i82596_receive_packet(len);
-    /* PRINT_PKTHDR("Receive", buf); */
+    DBG(PRINT_PKTHDR("Receive", buf));
 
     while (len) {
-        uint16_t command, status;
+        uint16_t command;
         uint32_t next_rfd;
+        uint32_t rba;
+        uint16_t rba_size;
+        uint32_t actual_count_ptr;
 
+        DBG(printf("Receive: rfd is 0x%08x, len = %lu\n", rfd_p, len));
         command = get_uint16(rfd_p + 2);
         assert(command & CMD_FLEX); /* assert Flex Mode */
-        /* get first Receive Buffer Descriptor Address */
+
+        /* get first Receive Buffer Descriptor address */
         rbd = get_uint32(rfd_p + 8);
-        assert(get_uint16(rfd_p + 14) == 0);
+        assert(rbd && rbd != I596_NULL);
 
-        /* printf("Receive: rfd is %08x\n", rfd_p); */
+        /* possibly store first bytes in rfd */
+        rba = rfd_p + 16;       /* 30 ?? data is behind the length field */
+        rba_size = get_uint16(rfd_p + 14); /* count of additional bytes in rfd */
+        actual_count_ptr = rfd_p + 12;
 
         while (len) {
-            uint16_t buffer_size, num;
-            uint32_t rba;
+            uint16_t num, actual_count;
 
-            /* printf("Receive: rbd is %08x\n", rbd); */
-            buffer_size = get_uint16(rbd + 12);
-            /* printf("buffer_size is 0x%x\n", buffer_size); */
-            assert(buffer_size != 0);
+            DBG(printf("rba is at 0x%x, rba_size = %d, cnt_ptr 0x%08x\n", rba, rba_size, actual_count_ptr));
 
-            num = buffer_size & SIZE_MASK;
+            /* store number of received bytes first */
+            num = rba_size & SIZE_MASK;
             if (num > len) {
                 num = len;
             }
-            rba = get_uint32(rbd + 8);
-            /* printf("rba is 0x%x\n", rba); */
-            address_space_write(&address_space_memory, rba,
-                                MEMTXATTRS_UNSPECIFIED, buf, num);
+            actual_count = num;
+            if (num == len) {
+                actual_count |= I596_EOF; /* set EOF BIT */
+            }
+
+            if (num) {
+                actual_count |= 0x4000; /* set F BIT */
+                set_uint16(actual_count_ptr, actual_count); /* write actual count with flags */
+
+                address_space_write(&address_space_memory, rba,
+                                    MEMTXATTRS_UNSPECIFIED, buf, num);
+            }
             rba += num;
             buf += num;
             len -= num;
-            if (len == 0) { /* copy crc */
+            if (len == 0 && I596_CRCINM && !I596_LOOPBACK) { /* copy crc */
                 address_space_write(&address_space_memory, rba - 4,
                                     MEMTXATTRS_UNSPECIFIED, crc_ptr, 4);
             }
 
-            num |= 0x4000; /* set F BIT */
-            if (len == 0) {
-                num |= I596_EOF; /* set EOF BIT */
+            if (len == 0) { // do not get next rbd
+                break;
             }
-            set_uint16(rbd + 0, num); /* write actual count with flags */
+
+            if (rba_size & I596_EOF) /* last entry */
+                break;
+
+            DBG(printf("Receive: rbd is 0x%08x\n", rbd));
+            rba_size = get_uint16(rbd + 12);
+            rba = get_uint32(rbd + 8);
+            actual_count_ptr = rbd + 0;
+            assert(rba_size != 0);
 
             /* get next rbd */
             rbd = get_uint32(rbd + 4);
-            /* printf("Next Receive: rbd is %08x\n", rbd); */
-
-            if (buffer_size & I596_EOF) /* last entry */
-                break;
         }
 
         /* Housekeeping, see pg. 18 */
@@ -671,8 +738,8 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
         set_uint16(rfd_p, status);
 
         if (command & CMD_SUSP) {  /* suspend after command? */
-            s->rx_status = RX_SUSPENDED;
-            s->scb_status |= SCB_STATUS_RNR; /* RU left active state */
+            s->RUS = RX_SUSPENDED;
+            s->scb_status |= SCB_STAT_RNR; /* RU left active state */
             break;
         }
         if (command & CMD_EOL) /* was it last Frame Descriptor? */
@@ -683,24 +750,28 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
 
     assert(len == 0);
 
-    s->scb_status |= SCB_STATUS_FR; /* set "RU finished receiving frame" bit. */
+    s->scb_status |= SCB_STAT_FR; /* set "RU finished receiving frame" bit. */
     update_scb_status(s);
 
     /* send IRQ that we received data */
-    qemu_set_irq(s->irq, 1);
-    /* s->send_irq = 1; */
+    if (I596_LOOPBACK) {
+        s->send_irq = 1;
+    } else {
+        qemu_set_irq(s->irq, 1);
+    }
 
     if (0) {
         DBG(printf("Checking:\n"));
         rfd_p = get_uint32(s->scb + 8); /* get Receive Frame Descriptor */
-        DBG(printf("Next Receive: rfd is %08x\n", rfd_p));
+        DBG(printf("Next Receive: rfd is 0x%08x\n", rfd_p));
         rfd_p = get_uint32(rfd_p + 4); /* get Next Receive Frame Descriptor */
-        DBG(printf("Next Receive: rfd is %08x\n", rfd_p));
+        DBG(printf("Next Receive: rfd is 0x%08x\n", rfd_p));
         /* get first Receive Buffer Descriptor Address */
         rbd = get_uint32(rfd_p + 8);
-        DBG(printf("Next Receive: rbd is %08x\n", rbd));
+        DBG(printf("Next Receive: rbd is 0x%08x\n", rbd));
     }
 
+    DBG(printf("i82596_receive() end sz = %lu\n", sz));
     return sz;
 }
 
@@ -711,7 +782,6 @@ const VMStateDescription vmstate_i82596 = {
     .minimum_version_id = 1,
     .fields = (VMStateField[]) {
         VMSTATE_UINT16(lnkst, I82596State),
-        VMSTATE_TIMER_PTR(flush_queue_timer, I82596State),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -725,9 +795,5 @@ void i82596_common_init(DeviceState *dev, I82596State *s, NetClientInfo *info)
                 dev->id, s);
     qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
 
-    if (USE_TIMER) {
-        s->flush_queue_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                    i82596_flush_queue_timer, s);
-    }
     s->lnkst = 0x8000; /* initial link state: up */
 }
hw/net/i82596.h +12 -8
index 1238ac11f8..0b1c0ce2aa 100644
@@ -10,7 +10,14 @@
 #define PORT_SELFTEST           0x01    /* selftest */
 #define PORT_ALTSCP             0x02    /* alternate SCB address */
 #define PORT_ALTDUMP            0x03    /* Alternate DUMP address */
-#define PORT_CA                 0x10    /* QEMU-internal CA signal */
+#define PORT_CA                 0x04    /* QEMU-internal CA signal */
+#define PORT_BYTEMASK           0x0f    /* all valid bits */
+
+/* modes in which the 82596 can operate */
+#define MODE_82586              0       /* 24 bit address space */
+#define MODE_32BIT_SEGMENTED    1
+#define MODE_LINEAR             2       /* 32 bit address space */
+#define MODE_UNKNOWN            3
 
 typedef struct I82596State_st I82596State;
 
@@ -23,22 +30,19 @@ struct I82596State_st {
     QEMUTimer *flush_queue_timer;
 
     hwaddr scp;         /* pointer to SCP */
-    uint8_t sysbus;
     uint32_t scb;       /* SCB */
     uint16_t scb_status;
-    uint8_t cu_status, rx_status;
+    uint8_t  send_irq;
+    uint8_t  CUS:3;     /* Command Unit status word in SCB */
+    uint8_t  RUS:4;     /* Receive Unit status word in SCB */
     uint16_t lnkst;
-
     uint32_t cmd_p;     /* addr of current command */
-    int ca;
-    int ca_active;
-    int send_irq;
 
     /* Hash register (multicast mask array, multiple individual addresses). */
     uint8_t mult[8];
     uint8_t config[14]; /* config bytes from CONFIGURE command */
 
-    uint8_t tx_buffer[0x4000];
+    uint8_t tx_buffer[1540];
 };
 
 void i82596_h_reset(void *opaque);
hw/net/lasi_i82596.c +2 -2
index 52637a562d..dc37591ad8 100644
@@ -42,8 +42,8 @@ static void lasi_82596_mem_write(void *opaque, hwaddr addr,
         d->val_index++;
         if (d->val_index == 0) {
             uint32_t v = d->last_val | (val << 16);
-            v = v & ~0xff;
-            i82596_ioport_writew(&d->state, d->last_val & 0xff, v);
+            v &= ~PORT_BYTEMASK;
+            i82596_ioport_writew(&d->state, d->last_val & PORT_BYTEMASK, v);
         }
         d->last_val = val;
         break;
#7 lore raw

Re: [PULL v3 02/11] hppa: Add support for LASI chip with i82596 NIC

Quoted 52 lines · Helge Deller

* Peter Maydell <[email protected]>:

Quoted 26 lines · Helge Deller
Quoted 19 lines · Peter Maydell
Quoted 11 lines
Quoted 8 lines · Richard Henderson

From: Helge Deller <[email protected]>

LASI is a built-in multi-I/O chip which supports serial, parallel, network (Intel i82596 Apricot), sound and other functionalities. LASI has been used in many HP PARISC machines.
This patch adds the necessary parts to allow Linux and HP-UX to detect LASI and the network card.

Quoted 5 lines

So we could be reading off the end of the buffer.

I don't know whether the buffer should be 4 bytes larger to allow for the checksum, or if the len calculation is wrong.

Quoted 5 lines

I'm working on a bigger patch which will improve this driver. It still has some issues with the emulation in Linux and HP-UX. With the patch I will take try to fix those out-of-bounds accesses too.
It will take some time though, until I will send the patch.

Quoted 3 lines

As this is a bug fix and in particular a fix for a buffer overrun, I think it would be better if you could send a patch that just fixes this.

Of course I usually agree on that, but in this special case the initial patch was more or less the basic functionality that the NIC gets detected. The driver is nevertheless full of bugs, so even if I'd try to get this specific buffer overrun fixed, it will not give you a working NIC emulation. Such a patch would just hide the buffer overrun without any real benefit.

Quoted 4 lines

If you include it in a larger general-improvements patch we'll probably just ask you to split it out when we get to code-review of that patchset, and doing it that way will mean we have the buggy code in QEMU for longer.

I've done big progress with my fixes. Below you will find the current patch against qemu git head which does work with a Linux guest. It's not perfect yet either, but the Linux guest gets an IP address, apt-get update works, same as pings and such. It's still work-in-progress, so please don't look too deep into it yet. I'm aware of the rough edges which I plan to clean up in the next few days, but I attached it here for your reference in the hope that you can see that there is much more work than just trying to fix some static code analysis output.

Quoted 6 lines

That said, I would be happy to further clean it up, split it into logically seperate patches and send them upstream in a few days for review if you agree. I think with this approach we gain more than trying to "fix" the old buggy code.

Thoughts?

"big progress" -> "big patch" :)

Can you re-post split in smaller logical changes to ease review?

Quoted 768 lines

Helge

hw/net/i82596.c +222 -156
index fe9f2390a9..f8d55601bc 100644
@@ -27,29 +27,29 @@
  #define DBG(x)          do { } while (0)
  #endif
 
-#define USE_TIMER       0
-
  #define BITS(n, m) (((0xffffffffU << (31 - n)) >> (31 - n + m)) << m)
 
-#define PKT_BUF_SZ      1536
  #define MAX_MC_CNT      64
 
  #define ISCP_BUSY       0x0001
 
  #define I596_NULL       ((uint32_t)0xffffffff)
 
-#define SCB_STATUS_CX   0x8000 /* CU finished command with I bit */
-#define SCB_STATUS_FR   0x4000 /* RU finished receiving a frame */
-#define SCB_STATUS_CNA  0x2000 /* CU left active state */
-#define SCB_STATUS_RNR  0x1000 /* RU left active state */
+#define SCB_STAT_CX     0x8000 /* CU finished command with I bit */
+#define SCB_STAT_FR     0x4000 /* RU finished receiving a frame */
+#define SCB_STAT_CNA    0x2000 /* CU left active state */
+#define SCB_STAT_RNR    0x1000 /* RU left active state */
 
-#define CU_IDLE         0
+#define CU_IDLE         0       /* CUS values */
  #define CU_SUSPENDED    1
  #define CU_ACTIVE       2
 
-#define RX_IDLE         0
+#define RX_IDLE         0       /* RUS values */
  #define RX_SUSPENDED    1
+#define RX_NO_RESOURCES 2
  #define RX_READY        4
+#define RX_NO_RESO_RBD  (8 + RX_NO_RESOURCES)
+#define RX_NO_MORE_RBD  (8 + RX_READY)
 
  #define CMD_EOL         0x8000  /* The last command of the list, stop. */
  #define CMD_SUSP        0x4000  /* Suspend after doing cmd. */
@@ -76,10 +76,13 @@ enum commands {
 
  /* various flags in the chip config registers */
  #define I596_PREFETCH   (s->config[0] & 0x80)
+#define I596_NO_SRC_ADD_IN (s->config[3] & 0x08) /* if 1, do not insert MAC in Tx Packet */
+#define I596_LOOPBACK   (s->config[3] >> 6)     /* loopback mode, 3 = external loopback */
  #define I596_PROMISC    (s->config[8] & 0x01)
-#define I596_BC_DISABLE (s->config[8] & 0x02) /* broadcast disable */
-#define I596_NOCRC_INS  (s->config[8] & 0x08)
-#define I596_CRCINM     (s->config[11] & 0x04) /* CRC appended */
+#define I596_BC_DISABLE (s->config[8] & 0x02)   /* broadcast disable */
+#define I596_NOCRC_INS  (s->config[8] & 0x08)   /* do not append CRC to Tx frame */
+#define I596_CRC16_32   (s->config[8] & 0x10)   /* CRC-16 or CRC-32 */
+#define I596_CRCINM     (s->config[11] & 0x04)  /* Rx CRC appended in memory */
  #define I596_MC_ALL     (s->config[11] & 0x20)
  #define I596_MULTIIA    (s->config[13] & 0x40)
 
@@ -134,9 +137,14 @@ struct qemu_ether_header {
  static void i82596_transmit(I82596State *s, uint32_t addr)
  {
      uint32_t tdb_p; /* Transmit Buffer Descriptor */
+    uint16_t cmd;
+    int insert_crc;
 
-    /* TODO: Check flexible mode */
+    cmd = get_uint16(addr + 2);
+    assert(cmd & 8);    /* check flexible mode */
      tdb_p = get_uint32(addr + 8);
+    /* check NC bit and possibly insert CRC */
+    insert_crc = (I596_NOCRC_INS == 0) && ((cmd & 0x10) == 0) && !I596_LOOPBACK;
      while (tdb_p != I596_NULL) {
          uint16_t size, len;
          uint32_t tba;
@@ -147,16 +155,43 @@ static void i82596_transmit(I82596State *s, uint32_t addr)
          trace_i82596_transmit(len, tba);
 
          if (s->nic && len) {
-            assert(len <= sizeof(s->tx_buffer));
+            uint16_t new_len;
+            new_len = len + 4;
+            assert(new_len <= sizeof(s->tx_buffer));
              address_space_read(&address_space_memory, tba,
                                 MEMTXATTRS_UNSPECIFIED, s->tx_buffer, len);
+
+            if (I596_NO_SRC_ADD_IN == 0) {
+                /* insert MAC in Tx Packet */
+                memcpy(&s->tx_buffer[ETH_ALEN], s->conf.macaddr.a, ETH_ALEN);
+            }
+
+            DBG(printf("i82596_transmit: insert_crc = %d  insert SRC = %d\n",
+                        insert_crc, I596_NO_SRC_ADD_IN == 0));
+            if (insert_crc) {
+                uint32_t crc = crc32(~0, s->tx_buffer, len);
+                crc = cpu_to_be32(crc);
+                memcpy(&s->tx_buffer[len], &crc, sizeof(crc));
+                len += sizeof(crc);
+            }
+
              DBG(PRINT_PKTHDR("Send", &s->tx_buffer));
-            DBG(printf("Sending %d bytes\n", len));
-            qemu_send_packet(qemu_get_queue(s->nic), s->tx_buffer, len);
+            DBG(printf("Sending %d bytes (crc_inserted=%d)\n", len, insert_crc));
+            switch (I596_LOOPBACK) {
+            case 0:     /* no loopback, send packet */
+                qemu_send_packet_raw(qemu_get_queue(s->nic), s->tx_buffer, len);
+                break;
+            case 1:     /* external loopback enabled */
+                i82596_receive(qemu_get_queue(s->nic), s->tx_buffer, len);
+                break;
+            default:    /* all other loopback modes: ignore! */
+                break;
+            }
          }
 
          /* was this the last package? */
          if (size & I596_EOF) {
+            qemu_flush_queued_packets(qemu_get_queue(s->nic));
              break;
          }
 
@@ -175,6 +210,7 @@ static void set_individual_address(I82596State *s, uint32_t addr)
      address_space_read(&address_space_memory, addr + 8,
                         MEMTXATTRS_UNSPECIFIED, m, ETH_ALEN);
      qemu_format_nic_info_str(nc, m);
+    DBG(printf("MAC addr set to %s\n", nc->info_str));
      trace_i82596_new_mac(nc->info_str);
  }
 
@@ -188,6 +224,7 @@ static void set_multicast_list(I82596State *s, uint32_t addr)
      if (mc_count > MAX_MC_CNT) {
          mc_count = MAX_MC_CNT;
      }
+    DBG(printf("Add %d multicast entries.\n", mc_count));
      for (i = 0; i < mc_count; i++) {
          uint8_t multicast_addr[ETH_ALEN];
          address_space_read(&address_space_memory, addr + i * ETH_ALEN,
@@ -212,7 +249,9 @@ void i82596_set_link_status(NetClientState *nc)
  static void update_scb_status(I82596State *s)
  {
      s->scb_status = (s->scb_status & 0xf000)
-        | (s->cu_status << 8) | (s->rx_status << 4);
+        | (s->CUS << 8) | (s->RUS << 4)
+        | 8 /* 8: bus throttle timers loaded */;
+    DBG(printf("update_scb_status 0x%04x CUS: %d, RUS: %d\n", s->scb_status, s->CUS, s->RUS));
      set_uint16(s->scb, s->scb_status);
  }
 
@@ -220,13 +259,13 @@ static void update_scb_status(I82596State *s)
  static void i82596_s_reset(I82596State *s)
  {
      trace_i82596_s_reset(s);
-    s->scp = 0;
+    DBG(printf("i82596_s_reset()\n"));
+    s->scp = 0x00FFFFF4;
      s->scb_status = 0;
-    s->cu_status = CU_IDLE;
-    s->rx_status = RX_SUSPENDED;
+    s->CUS = CU_IDLE;
+    s->RUS = RX_IDLE;
      s->cmd_p = I596_NULL;
      s->lnkst = 0x8000; /* initial link state: up */
-    s->ca = s->ca_active = 0;
      s->send_irq = 0;
  }
 
@@ -237,7 +276,7 @@ static void command_loop(I82596State *s)
      uint16_t status;
      uint8_t byte_cnt;
 
-    DBG(printf("STARTING COMMAND LOOP cmd_p=%08x\n", s->cmd_p));
+    DBG(printf("STARTING COMMAND LOOP cmd_p=0x%08x\n", s->cmd_p));
 
      while (s->cmd_p != I596_NULL) {
          /* set status */
@@ -246,7 +285,8 @@ static void command_loop(I82596State *s)
          status = STAT_C | STAT_OK; /* update, but write later */
 
          cmd = get_uint16(s->cmd_p + 2);
-        DBG(printf("Running command %04x at %08x\n", cmd, s->cmd_p));
+        DBG(printf("Running command 0x%04x (cmd %d) at 0x%08x\n",
+                cmd, cmd & 7, s->cmd_p));
 
          switch (cmd & 0x07) {
          case CmdNOp:
@@ -264,11 +304,17 @@ static void command_loop(I82596State *s)
              /* config byte according to page 35ff */
              s->config[2] &= 0x82; /* mask valid bits */
              s->config[2] |= 0x40;
+            DBG(printf("I596_CONFIG3 = 0x%02x  LOOPBACK 0x%x\n", s->config[3], I596_LOOPBACK));
+            if (I596_NO_SRC_ADD_IN == 0) {
+                assert((s->config[3] & 0x07) == ETH_ALEN);
+            }
              s->config[7]  &= 0xf7; /* clear zero bit */
-            assert(I596_NOCRC_INS == 0); /* do CRC insertion */
+            assert(I596_CRC16_32 == 0); /* only CRC-32 implemented */
+            DBG(printf("I596_CRCINM = %d\n\n", I596_CRCINM));
              s->config[10] = MAX(s->config[10], 5); /* min frame length */
              s->config[12] &= 0x40; /* only full duplex field valid */
              s->config[13] |= 0x3f; /* set ones in byte 13 */
+            s->scb_status |= SCB_STAT_CX;
              break;
          case CmdTDR:
              /* get signal LINK */
@@ -290,7 +336,7 @@ static void command_loop(I82596State *s)
          set_uint16(s->cmd_p, status);
 
          s->cmd_p = get_uint32(s->cmd_p + 4); /* get link address */
-        DBG(printf("NEXT addr would be %08x\n", s->cmd_p));
+        DBG(printf("NEXT loop addr is 0x%08x\n", s->cmd_p));
          if (s->cmd_p == 0) {
              s->cmd_p = I596_NULL;
          }
@@ -301,71 +347,51 @@ static void command_loop(I82596State *s)
          }
          /* Suspend after doing cmd? */
          if (cmd & CMD_SUSP) {
-            s->cu_status = CU_SUSPENDED;
-            printf("FIXME SUSPEND !!\n");
+            s->CUS = CU_SUSPENDED;
+            printf("FIXME SUSPEND ?\n");
          }
-        /* Interrupt after doing cmd? */
-        if (cmd & CMD_INTR) {
-            s->scb_status |= SCB_STATUS_CX;
-        } else {
-            s->scb_status &= ~SCB_STATUS_CX;
-        }
-        update_scb_status(s);
 
          /* Interrupt after doing cmd? */
          if (cmd & CMD_INTR) {
+            s->scb_status |= SCB_STAT_CX;
              s->send_irq = 1;
          }
 
-        if (s->cu_status != CU_ACTIVE) {
+        if (s->CUS == CU_SUSPENDED) {
              break;
          }
      }
      DBG(printf("FINISHED COMMAND LOOP\n"));
-    qemu_flush_queued_packets(qemu_get_queue(s->nic));
-}
-
-static void i82596_flush_queue_timer(void *opaque)
-{
-    I82596State *s = opaque;
-    if (0) {
-        timer_del(s->flush_queue_timer);
-        qemu_flush_queued_packets(qemu_get_queue(s->nic));
-        timer_mod(s->flush_queue_timer,
-              qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000);
-    }
  }
 
  static void examine_scb(I82596State *s)
  {
-    uint16_t command, cuc, ruc;
+    uint16_t command, cuc, ruc, c;
 
      /* get the scb command word */
      command = get_uint16(s->scb + 2);
+    DBG(printf("COMMAND = 0x%04x\n", command));
      cuc = (command >> 8) & 0x7;
      ruc = (command >> 4) & 0x7;
-    DBG(printf("MAIN COMMAND %04x  cuc %02x ruc %02x\n", command, cuc, ruc));
-    /* and clear the scb command word */
-    set_uint16(s->scb + 2, 0);
+    DBG(printf("MAIN CU COMMAND 0x%04x: stat 0x%02x cuc 0x%02x ruc 0x%02x\n",
+            command, command >> 12,  cuc, ruc));
 
-    if (command & BIT(31))      /* ACK-CX */
-        s->scb_status &= ~SCB_STATUS_CX;
-    if (command & BIT(30))      /*ACK-FR */
-        s->scb_status &= ~SCB_STATUS_FR;
-    if (command & BIT(29))      /*ACK-CNA */
-        s->scb_status &= ~SCB_STATUS_CNA;
-    if (command & BIT(28))      /*ACK-RNR */
-        s->scb_status &= ~SCB_STATUS_RNR;
+    /* toggle the STAT flags in SCB status word */
+    c = command & (SCB_STAT_CX | SCB_STAT_FR | SCB_STAT_CNA | SCB_STAT_RNR);
+    s->scb_status &= ~c;
 
      switch (cuc) {
      case 0:     /* no change */
+    case 5:
+    case 6:
          break;
      case 1:     /* CUC_START */
-        s->cu_status = CU_ACTIVE;
+        s->CUS = CU_ACTIVE;
          break;
      case 4:     /* CUC_ABORT */
-        s->cu_status = CU_SUSPENDED;
-        s->scb_status |= SCB_STATUS_CNA; /* CU left active state */
+        s->CUS = CU_IDLE;
+        s->scb_status |= SCB_STAT_CNA; /* CU left active state */
+        s->send_irq = 1;
          break;
      default:
          printf("WARNING: Unknown CUC %d!\n", cuc);
@@ -376,16 +402,17 @@ static void examine_scb(I82596State *s)
          break;
      case 1:     /* RX_START */
      case 2:     /* RX_RESUME */
-        s->rx_status = RX_IDLE;
-        if (USE_TIMER) {
-            timer_mod(s->flush_queue_timer, qemu_clock_get_ms(
-                                QEMU_CLOCK_VIRTUAL) + 1000);
-        }
+        s->RUS = RX_READY;
          break;
      case 3:     /* RX_SUSPEND */
+        s->RUS = RX_SUSPENDED;
+        s->scb_status |= SCB_STAT_RNR; /* RU left active state */
+        s->send_irq = 1;
+        break;
      case 4:     /* RX_ABORT */
-        s->rx_status = RX_SUSPENDED;
-        s->scb_status |= SCB_STATUS_RNR; /* RU left active state */
+        s->RUS = RX_IDLE;
+        s->scb_status |= SCB_STAT_RNR; /* RU left active state */
+        s->send_irq = 1;
          break;
      default:
          printf("WARNING: Unknown RUC %d!\n", ruc);
@@ -396,66 +423,92 @@ static void examine_scb(I82596State *s)
      }
 
      /* execute commands from SCBL */
-    if (s->cu_status != CU_SUSPENDED) {
+    if (s->CUS == CU_ACTIVE) {
          if (s->cmd_p == I596_NULL) {
              s->cmd_p = get_uint32(s->scb + 4);
          }
+        command_loop(s);
+        s->CUS = CU_IDLE;
+        s->send_irq = 1;
      }
 
-    /* update scb status */
-    update_scb_status(s);
-
-    command_loop(s);
+    qemu_flush_queued_packets(qemu_get_queue(s->nic));
  }
 
  static void signal_ca(I82596State *s)
  {
-    uint32_t iscp = 0;
+    DBG(printf("-- CA start\n"));
 
      /* trace_i82596_channel_attention(s); */
      if (s->scp) {
+        uint32_t iscp;
+        uint8_t sysbus;
+        uint8_t mode;       /* MODE_82586 or MODE_LINEAR */
+
          /* CA after reset -> do init with new scp. */
-        s->sysbus = get_byte(s->scp + 3); /* big endian */
-        DBG(printf("SYSBUS = %08x\n", s->sysbus));
-        if (((s->sysbus >> 1) & 0x03) != 2) {
-            printf("WARNING: NO LINEAR MODE !!\n");
-        }
-        if ((s->sysbus >> 7)) {
+        sysbus = get_byte(s->scp + 3); /* big endian */
+        DBG(printf("SYSBUS = %08x\n", sysbus));
+        mode = (sysbus >> 1) & 0x03;
+        /* Only MODE_LINEAR is currently implemented. */
+        assert(mode == MODE_LINEAR);
+        if ((sysbus >> 7)) {
              printf("WARNING: 32BIT LINMODE IN B-STEPPING NOT SUPPORTED !!\n");
          }
          iscp = get_uint32(s->scp + 8);
          s->scb = get_uint32(iscp + 4);
+        DBG(printf("ISCP = 0x%08x, SCB = 0x%08x\n", iscp,s->scb));
+        /* set_uint32(iscp + 4, 0); NOT: clear SCB pointer */
          set_byte(iscp + 1, 0); /* clear BUSY flag in iscp */
+        /* sets CX and CNR to equal 1 in the SCB, clears the SCB command word,
+         * sends an interrupt to the CPU, and awaits another CA signal */
+        s->scb_status = SCB_STAT_CX | SCB_STAT_CNA;
+        s->CUS = CU_IDLE;
+        s->RUS = RX_IDLE;
          s->scp = 0;
+        s->send_irq = 1;
+        goto _cont;
      }
 
-    s->ca++;    /* count ca() */
-    if (!s->ca_active) {
-        s->ca_active = 1;
-        while (s->ca)   {
-            examine_scb(s);
-            s->ca--;
-        }
-        s->ca_active = 0;
-    }
+    examine_scb(s);
+
+_cont:
+    /* update scb status */
+    update_scb_status(s);
+
+    /* and clear the scb command word */
+    set_uint16(s->scb + 2, 0);
 
      if (s->send_irq) {
          s->send_irq = 0;
+        DBG(printf("Send IRQ\n"));
          qemu_set_irq(s->irq, 1);
      }
+    DBG(printf("-- CA end\n"));
  }
 
  void i82596_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
  {
      I82596State *s = opaque;
-    /* printf("i82596_ioport_writew addr=0x%08x val=0x%04x\n", addr, val); */
-    switch (addr) {
+    uint32_t res, tmp;
+    DBG(printf("i82596_ioport_writew addr=0x%08x val=0x%04x\n", addr, val));
+    switch (addr & PORT_BYTEMASK) {
      case PORT_RESET: /* Reset */
          i82596_s_reset(s);
          break;
+    case PORT_SELFTEST:
+        res = val + sizeof(uint32_t);
+        tmp = get_uint32(res); /* should be -1 */
+        DBG(printf("i82596 SELFTEST at 0x%04x val 0x%04x requested.\n", res, tmp));
+        assert(tmp == I596_NULL);
+        set_uint32(res, 0); /* set to zero */
+        break;
      case PORT_ALTSCP:
+        DBG(printf("i82596 ALTSCP requested.\n"));
          s->scp = val;
          break;
+    case PORT_ALTDUMP:
+        printf("i82596 PORT_ALTDUMP not implemented yet.\n");
+        break;
      case PORT_CA:
          signal_ca(s);
          break;
@@ -478,18 +531,15 @@ int i82596_can_receive(NetClientState *nc)
  {
      I82596State *s = qemu_get_nic_opaque(nc);
 
-    if (s->rx_status == RX_SUSPENDED) {
-        return 0;
+    if (s->RUS != RX_READY) {
+        /* return 0; */
      }
 
+    /* Link down? */
      if (!s->lnkst) {
          return 0;
      }
 
-    if (USE_TIMER && !timer_pending(s->flush_queue_timer)) {
-        return 1;
-    }
-
      return 1;
  }
 
@@ -500,7 +550,7 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
      I82596State *s = qemu_get_nic_opaque(nc);
      uint32_t rfd_p;
      uint32_t rbd;
-    uint16_t is_broadcast = 0;
+    uint16_t status, is_broadcast = 0;
      size_t len = sz;
      uint32_t crc;
      uint8_t *crc_ptr;
@@ -508,14 +558,10 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
      static const uint8_t broadcast_macaddr[6] = {
                  0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
-    DBG(printf("i82596_receive() start\n"));
-
-    if (USE_TIMER && timer_pending(s->flush_queue_timer)) {
-        return 0;
-    }
+    DBG(printf("i82596_receive() start, sz = %lu\n", sz));
 
      /* first check if receiver is enabled */
-    if (s->rx_status == RX_SUSPENDED) {
+    if (s->RUS == RX_SUSPENDED) {
          trace_i82596_receive_analysis(">>> Receiving suspended");
          return -1;
      }
@@ -527,14 +573,14 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
 
      /* Received frame smaller than configured "min frame len"? */
      if (sz < s->config[10]) {
-        printf("Received frame too small, %zu vs. %u bytes\n",
-               sz, s->config[10]);
-        return -1;
+        if (0) printf("Received frame too small, %lu vs. %u bytes\n",
+            sz, s->config[10]);
+        sz = 60; /* return -1; */
      }
 
      DBG(printf("Received %lu bytes\n", sz));
 
-    if (I596_PROMISC) {
+    if (I596_PROMISC || I596_LOOPBACK) {
 
          /* promiscuous: receive all */
          trace_i82596_receive_analysis(
@@ -598,69 +644,90 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
      }
 
      /* Calculate the ethernet checksum (4 bytes) */
-    len += 4;
-    crc = cpu_to_be32(crc32(~0, buf, sz));
-    crc_ptr = (uint8_t *) &crc;
-
-    rfd_p = get_uint32(s->scb + 8); /* get Receive Frame Descriptor */
-    assert(rfd_p && rfd_p != I596_NULL);
+    if (I596_CRCINM && !I596_LOOPBACK) {
+        len += 4;
+        crc = crc32(~0, buf, sz);
+        crc = cpu_to_be32(crc);
+        crc_ptr = (uint8_t *) &crc;
+    }
 
-    /* get first Receive Buffer Descriptor Address */
-    rbd = get_uint32(rfd_p + 8);
-    assert(rbd && rbd != I596_NULL);
+    rfd_p = get_uint32(s->scb + 8); /* get initial Receive Frame Descriptor */
+    do {
+        assert(rfd_p && rfd_p != I596_NULL);
+        status = get_uint16(rfd_p+0);
+        /* if rfd is filled, get next one from link addr */
+        if (status & STAT_OK)
+            rfd_p = get_uint32(rfd_p+4);
+    } while (status & STAT_OK);
 
      trace_i82596_receive_packet(len);
-    /* PRINT_PKTHDR("Receive", buf); */
+    DBG(PRINT_PKTHDR("Receive", buf));
 
      while (len) {
-        uint16_t command, status;
+        uint16_t command;
          uint32_t next_rfd;
+        uint32_t rba;
+        uint16_t rba_size;
+        uint32_t actual_count_ptr;
 
+        DBG(printf("Receive: rfd is 0x%08x, len = %lu\n", rfd_p, len));
          command = get_uint16(rfd_p + 2);
          assert(command & CMD_FLEX); /* assert Flex Mode */
-        /* get first Receive Buffer Descriptor Address */
+
+        /* get first Receive Buffer Descriptor address */
          rbd = get_uint32(rfd_p + 8);
-        assert(get_uint16(rfd_p + 14) == 0);
+        assert(rbd && rbd != I596_NULL);
 
-        /* printf("Receive: rfd is %08x\n", rfd_p); */
+        /* possibly store first bytes in rfd */
+        rba = rfd_p + 16;       /* 30 ?? data is behind the length field */
+        rba_size = get_uint16(rfd_p + 14); /* count of additional bytes in rfd */
+        actual_count_ptr = rfd_p + 12;
 
          while (len) {
-            uint16_t buffer_size, num;
-            uint32_t rba;
+            uint16_t num, actual_count;
 
-            /* printf("Receive: rbd is %08x\n", rbd); */
-            buffer_size = get_uint16(rbd + 12);
-            /* printf("buffer_size is 0x%x\n", buffer_size); */
-            assert(buffer_size != 0);
+            DBG(printf("rba is at 0x%x, rba_size = %d, cnt_ptr 0x%08x\n", rba, rba_size, actual_count_ptr));
 
-            num = buffer_size & SIZE_MASK;
+            /* store number of received bytes first */
+            num = rba_size & SIZE_MASK;
              if (num > len) {
                  num = len;
              }
-            rba = get_uint32(rbd + 8);
-            /* printf("rba is 0x%x\n", rba); */
-            address_space_write(&address_space_memory, rba,
-                                MEMTXATTRS_UNSPECIFIED, buf, num);
+            actual_count = num;
+            if (num == len) {
+                actual_count |= I596_EOF; /* set EOF BIT */
+            }
+
+            if (num) {
+                actual_count |= 0x4000; /* set F BIT */
+                set_uint16(actual_count_ptr, actual_count); /* write actual count with flags */
+
+                address_space_write(&address_space_memory, rba,
+                                    MEMTXATTRS_UNSPECIFIED, buf, num);
+            }
              rba += num;
              buf += num;
              len -= num;
-            if (len == 0) { /* copy crc */
+            if (len == 0 && I596_CRCINM && !I596_LOOPBACK) { /* copy crc */
                  address_space_write(&address_space_memory, rba - 4,
                                      MEMTXATTRS_UNSPECIFIED, crc_ptr, 4);
              }
 
-            num |= 0x4000; /* set F BIT */
-            if (len == 0) {
-                num |= I596_EOF; /* set EOF BIT */
+            if (len == 0) { // do not get next rbd
+                break;
              }
-            set_uint16(rbd + 0, num); /* write actual count with flags */
+
+            if (rba_size & I596_EOF) /* last entry */
+                break;
+
+            DBG(printf("Receive: rbd is 0x%08x\n", rbd));
+            rba_size = get_uint16(rbd + 12);
+            rba = get_uint32(rbd + 8);
+            actual_count_ptr = rbd + 0;
+            assert(rba_size != 0);
 
              /* get next rbd */
              rbd = get_uint32(rbd + 4);
-            /* printf("Next Receive: rbd is %08x\n", rbd); */
-
-            if (buffer_size & I596_EOF) /* last entry */
-                break;
          }
 
          /* Housekeeping, see pg. 18 */
@@ -671,8 +738,8 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
          set_uint16(rfd_p, status);
 
          if (command & CMD_SUSP) {  /* suspend after command? */
-            s->rx_status = RX_SUSPENDED;
-            s->scb_status |= SCB_STATUS_RNR; /* RU left active state */
+            s->RUS = RX_SUSPENDED;
+            s->scb_status |= SCB_STAT_RNR; /* RU left active state */
              break;
          }
          if (command & CMD_EOL) /* was it last Frame Descriptor? */
@@ -683,24 +750,28 @@ ssize_t i82596_receive(NetClientState *nc, const uint8_t *buf, size_t sz)
 
      assert(len == 0);
 
-    s->scb_status |= SCB_STATUS_FR; /* set "RU finished receiving frame" bit. */
+    s->scb_status |= SCB_STAT_FR; /* set "RU finished receiving frame" bit. */
      update_scb_status(s);
 
      /* send IRQ that we received data */
-    qemu_set_irq(s->irq, 1);
-    /* s->send_irq = 1; */
+    if (I596_LOOPBACK) {
+        s->send_irq = 1;
+    } else {
+        qemu_set_irq(s->irq, 1);
+    }
 
      if (0) {
          DBG(printf("Checking:\n"));
          rfd_p = get_uint32(s->scb + 8); /* get Receive Frame Descriptor */
-        DBG(printf("Next Receive: rfd is %08x\n", rfd_p));
+        DBG(printf("Next Receive: rfd is 0x%08x\n", rfd_p));
          rfd_p = get_uint32(rfd_p + 4); /* get Next Receive Frame Descriptor */
-        DBG(printf("Next Receive: rfd is %08x\n", rfd_p));
+        DBG(printf("Next Receive: rfd is 0x%08x\n", rfd_p));
          /* get first Receive Buffer Descriptor Address */
          rbd = get_uint32(rfd_p + 8);
-        DBG(printf("Next Receive: rbd is %08x\n", rbd));
+        DBG(printf("Next Receive: rbd is 0x%08x\n", rbd));
      }
 
+    DBG(printf("i82596_receive() end sz = %lu\n", sz));
      return sz;
  }
 
@@ -711,7 +782,6 @@ const VMStateDescription vmstate_i82596 = {
      .minimum_version_id = 1,
      .fields = (VMStateField[]) {
          VMSTATE_UINT16(lnkst, I82596State),
-        VMSTATE_TIMER_PTR(flush_queue_timer, I82596State),
          VMSTATE_END_OF_LIST()
      }
  };
@@ -725,9 +795,5 @@ void i82596_common_init(DeviceState *dev, I82596State *s, NetClientInfo *info)
                  dev->id, s);
      qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
 
-    if (USE_TIMER) {
-        s->flush_queue_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                    i82596_flush_queue_timer, s);
-    }
      s->lnkst = 0x8000; /* initial link state: up */
  }
hw/net/i82596.h +12 -8
index 1238ac11f8..0b1c0ce2aa 100644
@@ -10,7 +10,14 @@
  #define PORT_SELFTEST           0x01    /* selftest */
  #define PORT_ALTSCP             0x02    /* alternate SCB address */
  #define PORT_ALTDUMP            0x03    /* Alternate DUMP address */
-#define PORT_CA                 0x10    /* QEMU-internal CA signal */
+#define PORT_CA                 0x04    /* QEMU-internal CA signal */
+#define PORT_BYTEMASK           0x0f    /* all valid bits */
+
+/* modes in which the 82596 can operate */
+#define MODE_82586              0       /* 24 bit address space */
+#define MODE_32BIT_SEGMENTED    1
+#define MODE_LINEAR             2       /* 32 bit address space */
+#define MODE_UNKNOWN            3
 
  typedef struct I82596State_st I82596State;
 
@@ -23,22 +30,19 @@ struct I82596State_st {
      QEMUTimer *flush_queue_timer;
 
      hwaddr scp;         /* pointer to SCP */
-    uint8_t sysbus;
      uint32_t scb;       /* SCB */
      uint16_t scb_status;
-    uint8_t cu_status, rx_status;
+    uint8_t  send_irq;
+    uint8_t  CUS:3;     /* Command Unit status word in SCB */
+    uint8_t  RUS:4;     /* Receive Unit status word in SCB */
      uint16_t lnkst;
-
      uint32_t cmd_p;     /* addr of current command */
-    int ca;
-    int ca_active;
-    int send_irq;
 
      /* Hash register (multicast mask array, multiple individual addresses). */
      uint8_t mult[8];
      uint8_t config[14]; /* config bytes from CONFIGURE command */
 
-    uint8_t tx_buffer[0x4000];
+    uint8_t tx_buffer[1540];
  };
 
  void i82596_h_reset(void *opaque);
hw/net/lasi_i82596.c +2 -2
index 52637a562d..dc37591ad8 100644
@@ -42,8 +42,8 @@ static void lasi_82596_mem_write(void *opaque, hwaddr addr,
          d->val_index++;
          if (d->val_index == 0) {
              uint32_t v = d->last_val | (val << 16);
-            v = v & ~0xff;
-            i82596_ioport_writew(&d->state, d->last_val & 0xff, v);
+            v &= ~PORT_BYTEMASK;
+            i82596_ioport_writew(&d->state, d->last_val & PORT_BYTEMASK, v);
          }
          d->last_val = val;
          break;
#8 lore raw

Re: [PULL v3 02/11] hppa: Add support for LASI chip with i82596 NIC

In reply to Richard Henderson

Cc'ing Jason.

Quoted 13 lines · Richard Henderson

From: Helge Deller <[email protected]>

LASI is a built-in multi-I/O chip which supports serial, parallel, network (Intel i82596 Apricot), sound and other functionalities. LASI has been used in many HP PARISC machines.
This patch adds the necessary parts to allow Linux and HP-UX to detect LASI and the network card.

Message-Id: <[email protected]>

---

[...]

Quoted 4 lines
hw/net/i82596.c new file +4 -0
+int i82596_can_receive(NetClientState *nc)
+{
+    I82596State *s = qemu_get_nic_opaque(nc);
+

hw/net/i82596.c line 491

Quoted 10 lines
hw/net/i82596.c new file +10 -0
+    if (s->rx_status == RX_SUSPENDED) {
+        return 0;
+    }
+
+    if (!s->lnkst) {
+        return 0;
+    }
+
+    if (USE_TIMER && !timer_pending(s->flush_queue_timer)) {
+        return 1;

Something is odd here. Don't you want the opposite?

"if timer pending to flush the queue, do not receive more pkts"

    if (USE_TIMER && timer_pending(s->flush_queue_timer)) {
        return 0;
    }
Quoted 4 lines
hw/net/i82596.c new file +4 -0
+    }
+
+    return 1;
+}

[...]

#9 lore raw

[PULL v3 10/11] hw/hppa/machine: Map the PDC memory region with higher priority

In reply to Richard Henderson

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

The region in range [0xf0000000 - 0xf1000000] is the PDC area (Processor Dependent Code), where the firmware is loaded. This region has higher priority than the main memory.

When the machine has more than 3840MB of RAM, there is an overlap. Since the PDC is closer to the CPU in the bus hierarchy, it gets accessed first, and the CPU does not have access to the RAM in this range.

To model the same behavior and keep a simple memory layout, reduce the priority of the RAM region. The PDC region ends overlapping the RAM.

Message-Id: <[email protected]>

---
 hw/hppa/machine.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/hppa/machine.c +1 -1
index 57cb4e0f34..2d62a248b8 100644
@@ -100,7 +100,7 @@ static void machine_hppa_init(MachineState *machine)
     ram_region = g_new(MemoryRegion, 1);
     memory_region_allocate_system_memory(ram_region, OBJECT(machine),
                                          "ram", ram_size);
-    memory_region_add_subregion(addr_space, 0, ram_region);
+    memory_region_add_subregion_overlap(addr_space, 0, ram_region, -1);
 
     /* Init Lasi chip */
     lasi_init(addr_space);
Signature · 1 line
2.20.1
#10 lore raw

[PULL v3 03/11] ps2: accept 'Set Key Make and Break' commands

In reply to Richard Henderson

From: Sven Schnelle <[email protected]>

HP-UX sends both the 'Set key make and break (0xfc) and 'Set all key typematic make and break' (0xfa). QEMU response with 'Resend' as it doesn't handle these commands. HP-UX than reports an PS/2 max retransmission exceeded error. Add these commands and just reply with ACK.

Message-Id: <[email protected]>

---
 hw/input/ps2.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/input/ps2.c +10 -0
index 67f92f6112..0b671b6339 100644
@@ -49,6 +49,8 @@
 #define KBD_CMD_RESET_DISABLE	0xF5	/* reset and disable scanning */
 #define KBD_CMD_RESET_ENABLE   	0xF6    /* reset and enable scanning */
 #define KBD_CMD_RESET		0xFF	/* Reset */
+#define KBD_CMD_SET_MAKE_BREAK  0xFC    /* Set Make and Break mode */
+#define KBD_CMD_SET_TYPEMATIC   0xFA    /* Set Typematic Make and Break mode */
 
 /* Keyboard Replies */
 #define KBD_REPLY_POR		0xAA	/* Power on reset */
@@ -573,6 +575,7 @@ void ps2_write_keyboard(void *opaque, int val)
         case KBD_CMD_SCANCODE:
         case KBD_CMD_SET_LEDS:
         case KBD_CMD_SET_RATE:
+        case KBD_CMD_SET_MAKE_BREAK:
             s->common.write_cmd = val;
             ps2_queue(&s->common, KBD_REPLY_ACK);
             break;
@@ -592,11 +595,18 @@ void ps2_write_keyboard(void *opaque, int val)
                 KBD_REPLY_ACK,
                 KBD_REPLY_POR);
             break;
+        case KBD_CMD_SET_TYPEMATIC:
+            ps2_queue(&s->common, KBD_REPLY_ACK);
+            break;
         default:
             ps2_queue(&s->common, KBD_REPLY_RESEND);
             break;
         }
         break;
+    case KBD_CMD_SET_MAKE_BREAK:
+        ps2_queue(&s->common, KBD_REPLY_ACK);
+        s->common.write_cmd = -1;
+        break;
     case KBD_CMD_SCANCODE:
         if (val == 0) {
             if (s->common.queue.count <= PS2_QUEUE_SIZE - 2) {
Signature · 1 line
2.20.1
#11 lore raw

[PULL v3 08/11] hw/hppa/machine: Correctly check the firmware is in PDC range

In reply to Richard Henderson

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

The firmware has to reside in the PDC range. If the Elf file expects to load it below FIRMWARE_START, it is incorrect, regardless the RAM size.

Message-Id: <[email protected]>

---
 hw/hppa/machine.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/hppa/machine.c +1 -1
index a35527cfc7..e57530950a 100644
@@ -171,7 +171,7 @@ static void machine_hppa_init(MachineState *machine)
     qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64
                   "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n",
                   firmware_low, firmware_high, firmware_entry);
-    if (firmware_low < ram_size || firmware_high >= FIRMWARE_END) {
+    if (firmware_low < FIRMWARE_START || firmware_high >= FIRMWARE_END) {
         error_report("Firmware overlaps with memory or IO space");
         exit(1);
     }
Signature · 1 line
2.20.1
#12 lore raw

[PULL v3 07/11] hppa: Add emulation of Artist graphics

In reply to Richard Henderson

From: Sven Schnelle <[email protected]>

This adds emulation of Artist graphics good enough to get a text console on both Linux and HP-UX. The X11 server from HP-UX also works.

Adjust boot-serial-test to disable graphics, so that SeaBIOS outputs to the serial port, as expected by the test.

Message-Id: <[email protected]>

[rth: Merge Helge's test for machine->enable_graphics]
---
 hw/hppa/hppa_hardware.h        |    1 +
 hw/display/artist.c            | 1454 ++++++++++++++++++++++++++++++++
 hw/hppa/machine.c              |   10 +
 tests/qtest/boot-serial-test.c |    3 +-
 hw/display/Kconfig             |    4 +
 hw/display/Makefile.objs       |    1 +
 hw/display/trace-events        |    9 +
 hw/hppa/Kconfig                |    1 +
 8 files changed, 1482 insertions(+), 1 deletion(-)
 create mode 100644 hw/display/artist.c
hw/hppa/hppa_hardware.h +1 -0
index 507f91e05d..4a2fe2df60 100644
@@ -22,6 +22,7 @@
 #define LASI_PS2KBD_HPA 0xffd08000
 #define LASI_PS2MOU_HPA 0xffd08100
 #define LASI_GFX_HPA    0xf8000000
+#define ARTIST_FB_ADDR  0xf9000000
 #define CPU_HPA         0xfffb0000
 #define MEMORY_HPA      0xfffbf000
 
hw/display/artist.c new file +1454 -0

1,454 lines not on this page · raw message

hw/hppa/machine.c +10 -0
index c8b1830f88..a35527cfc7 100644
@@ -75,6 +75,7 @@ static void machine_hppa_init(MachineState *machine)
     MemoryRegion *cpu_region;
     long i;
     unsigned int smp_cpus = machine->smp.cpus;
+    SysBusDevice *s;
 
     ram_size = machine->ram_size;
 
@@ -127,6 +128,15 @@ static void machine_hppa_init(MachineState *machine)
     dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a"));
     lsi53c8xx_handle_legacy_cmdline(dev);
 
+    /* Graphics setup. */
+    if (machine->enable_graphics && vga_interface_type != VGA_NONE) {
+        dev = qdev_create(NULL, "artist");
+        qdev_init_nofail(dev);
+        s = SYS_BUS_DEVICE(dev);
+        sysbus_mmio_map(s, 0, LASI_GFX_HPA);
+        sysbus_mmio_map(s, 1, ARTIST_FB_ADDR);
+    }
+
     /* Network setup. */
     for (i = 0; i < nb_nics; i++) {
         if (!enable_lasi_lan()) {
tests/qtest/boot-serial-test.c +2 -1
index 05c7f44457..8e8c5b0a0f 100644
@@ -135,7 +135,8 @@ static testdef_t tests[] = {
       sizeof(kernel_plml605), kernel_plml605 },
     { "moxie", "moxiesim", "", "TT", sizeof(bios_moxiesim), 0, bios_moxiesim },
     { "arm", "raspi2", "", "TT", sizeof(bios_raspi2), 0, bios_raspi2 },
-    { "hppa", "hppa", "", "SeaBIOS wants SYSTEM HALT" },
+    /* For hppa, force bios to output to serial by disabling graphics. */
+    { "hppa", "hppa", "-vga none", "SeaBIOS wants SYSTEM HALT" },
     { "aarch64", "virt", "-cpu cortex-a57", "TT", sizeof(kernel_aarch64),
       kernel_aarch64 },
     { "arm", "microbit", "", "T", sizeof(kernel_nrf51), kernel_nrf51 },
hw/display/Kconfig +4 -0
index c500d1fc6d..15d59e10dc 100644
@@ -91,6 +91,10 @@ config TCX
 config CG3
     bool
 
+config ARTIST
+    bool
+    select FRAMEBUFFER
+
 config VGA
     bool
 
hw/display/Makefile.objs +1 -0
index f2182e3bef..5f03dfdcc4 100644
@@ -40,6 +40,7 @@ common-obj-$(CONFIG_SM501) += sm501.o
 common-obj-$(CONFIG_TCX) += tcx.o
 common-obj-$(CONFIG_CG3) += cg3.o
 common-obj-$(CONFIG_NEXTCUBE) += next-fb.o
+common-obj-$(CONFIG_ARTIST) += artist.o
 
 obj-$(CONFIG_VGA) += vga.o
 
hw/display/trace-events +9 -0
index ba7787b180..e6e22bef88 100644
@@ -142,3 +142,12 @@ sii9022_switch_mode(const char *mode) "mode: %s"
 # ati.c
 ati_mm_read(unsigned int size, uint64_t addr, const char *name, uint64_t val) "%u 0x%"PRIx64 " %s -> 0x%"PRIx64
 ati_mm_write(unsigned int size, uint64_t addr, const char *name, uint64_t val) "%u 0x%"PRIx64 " %s <- 0x%"PRIx64
+
+# artist.c
+artist_reg_read(unsigned int size, uint64_t addr, const char *name, uint64_t val) "%u 0x%"PRIx64 "%s -> 0x%"PRIx64
+artist_reg_write(unsigned int size, uint64_t addr, const char *name, uint64_t val) "%u 0x%"PRIx64 "%s <- 0x%"PRIx64
+artist_vram_read(unsigned int size, uint64_t addr, int posx, int posy, uint64_t val) "%u 0x%"PRIx64 " %ux%u-> 0x%"PRIx64
+artist_vram_write(unsigned int size, uint64_t addr, uint64_t val) "%u 0x%"PRIx64 " <- 0x%"PRIx64
+artist_fill_window(unsigned int start_x, unsigned int start_y, unsigned int width, unsigned int height, uint32_t op, uint32_t ctlpln) "start=%ux%u length=%ux%u op=0x%08x ctlpln=0x%08x"
+artist_block_move(unsigned int start_x, unsigned int start_y, unsigned int dest_x, unsigned int dest_y, unsigned int width, unsigned int height) "source %ux%u -> dest %ux%u size %ux%u"
+artist_draw_line(unsigned int start_x, unsigned int start_y, unsigned int end_x, unsigned int end_y) "%ux%u %ux%u"
hw/hppa/Kconfig +1 -0
index 7f9be7f25c..82178c7dcb 100644
@@ -12,3 +12,4 @@ config DINO
     select LSI_SCSI_PCI
     select LASI_82596
     select LASIPS2
+    select ARTIST
Signature · 1 line
2.20.1
#13 lore raw

[PULL v3 05/11] hppa: Switch to tulip NIC by default

In reply to Richard Henderson

From: Helge Deller <[email protected]>

Most HP PA-RISC machines have a Digital DS21142/43 Tulip network card, only some very latest generation machines have an e1000 NIC. Since qemu now provides an emulated tulip card, use that one instead.

Message-Id: <[email protected]>

---
 hw/hppa/machine.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/hppa/machine.c +2 -2
index 33e3769d0b..c8b1830f88 100644
@@ -127,10 +127,10 @@ static void machine_hppa_init(MachineState *machine)
     dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a"));
     lsi53c8xx_handle_legacy_cmdline(dev);
 
-    /* Network setup.  e1000 is good enough, failing Tulip support.  */
+    /* Network setup. */
     for (i = 0; i < nb_nics; i++) {
         if (!enable_lasi_lan()) {
-            pci_nic_init_nofail(&nd_table[i], pci_bus, "e1000", NULL);
+            pci_nic_init_nofail(&nd_table[i], pci_bus, "tulip", NULL);
         }
     }
 
Signature · 1 line
2.20.1
#14 lore raw

[PULL v3 04/11] hppa: add emulation of LASI PS2 controllers

In reply to Richard Henderson

From: Sven Schnelle <[email protected]>

Message-Id: <[email protected]>

---
 include/hw/input/lasips2.h |  16 ++
 include/hw/input/ps2.h     |   1 +
 hw/hppa/lasi.c             |  10 +-
 hw/input/lasips2.c         | 291 +++++++++++++++++++++++++++++++++++++
 hw/input/ps2.c             |   5 +
 hw/hppa/Kconfig            |   1 +
 hw/input/Kconfig           |   3 +
 hw/input/Makefile.objs     |   1 +
 hw/input/trace-events      |   5 +
 9 files changed, 332 insertions(+), 1 deletion(-)
 create mode 100644 include/hw/input/lasips2.h
 create mode 100644 hw/input/lasips2.c
include/hw/input/lasips2.h new file +16 -0
new file mode 100644
index 0000000000..0cd7b59064
@@ -0,0 +1,16 @@
+/*
+ * QEMU LASI PS/2 emulation
+ *
+ * Copyright (c) 2019 Sven Schnelle
+ *
+ */
+#ifndef HW_INPUT_LASIPS2_H
+#define HW_INPUT_LASIPS2_H
+
+#include "exec/hwaddr.h"
+
+#define TYPE_LASIPS2 "lasips2"
+
+void lasips2_init(MemoryRegion *address_space, hwaddr base, qemu_irq irq);
+
+#endif /* HW_INPUT_LASIPS2_H */
include/hw/input/ps2.h +1 -0
index b60455d4f6..35d983897a 100644
@@ -47,5 +47,6 @@ void ps2_queue_3(PS2State *s, int b1, int b2, int b3);
 void ps2_queue_4(PS2State *s, int b1, int b2, int b3, int b4);
 void ps2_keyboard_set_translation(void *opaque, int mode);
 void ps2_mouse_fake_event(void *opaque);
+int ps2_queue_empty(PS2State *s);
 
 #endif /* HW_PS2_H */
hw/hppa/lasi.c +9 -1
index 51752589f3..d8d03f95c0 100644
@@ -22,6 +22,7 @@
 #include "hw/net/lasi_82596.h"
 #include "hw/char/parallel.h"
 #include "hw/char/serial.h"
+#include "hw/input/lasips2.h"
 #include "exec/address-spaces.h"
 #include "migration/vmstate.h"
 
@@ -324,6 +325,7 @@ DeviceState *lasi_init(MemoryRegion *address_space)
                      lpt_irq, parallel_hds[0]);
 
     /* Real time clock (RTC), it's only one 32-bit counter @9000 */
+
     s->rtc = time(NULL);
     s->rtc_ref = 0;
 
@@ -333,8 +335,14 @@ DeviceState *lasi_init(MemoryRegion *address_space)
                 lasi_get_irq(LASI_UART_HPA));
         serial_mm_init(address_space, LASI_UART_HPA + 0x800, 0,
                 serial_irq, 8000000 / 16,
-                serial_hd(1), DEVICE_NATIVE_ENDIAN);
+                serial_hd(0), DEVICE_NATIVE_ENDIAN);
     }
+
+    /* PS/2 Keyboard/Mouse */
+    qemu_irq ps2kbd_irq = qemu_allocate_irq(lasi_set_irq, s,
+            lasi_get_irq(LASI_PS2KBD_HPA));
+    lasips2_init(address_space, LASI_PS2KBD_HPA,  ps2kbd_irq);
+
     return dev;
 }
 
hw/input/lasips2.c new file +291 -0

291 lines not on this page · raw message

hw/input/ps2.c +5 -0
index 0b671b6339..f8746d2f52 100644
@@ -192,6 +192,11 @@ static void ps2_reset_queue(PS2State *s)
     q->count = 0;
 }
 
+int ps2_queue_empty(PS2State *s)
+{
+    return s->queue.count == 0;
+}
+
 void ps2_queue_noirq(PS2State *s, int b)
 {
     PS2Queue *q = &s->queue;
hw/hppa/Kconfig +1 -0
index 2a7b38d6d6..7f9be7f25c 100644
@@ -11,3 +11,4 @@ config DINO
     select MC146818RTC
     select LSI_SCSI_PCI
     select LASI_82596
+    select LASIPS2
hw/input/Kconfig +3 -0
index 287f08887b..25c77a1b87 100644
@@ -41,3 +41,6 @@ config VHOST_USER_INPUT
 
 config TSC210X
     bool
+
+config LASIPS2
+    select PS2
hw/input/Makefile.objs +1 -0
index a1bc502ed0..f98f635685 100644
@@ -15,3 +15,4 @@ common-obj-$(CONFIG_VHOST_USER_INPUT) += vhost-user-input.o
 obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
 obj-$(CONFIG_PXA2XX) += pxa2xx_keypad.o
 obj-$(CONFIG_TSC210X) += tsc210x.o
+obj-$(CONFIG_LASIPS2) += lasips2.o
hw/input/trace-events +5 -0
index cf072fa2f8..a2888fd10c 100644
@@ -53,3 +53,8 @@ tsc2005_sense(const char *state) "touchscreen sense %s"
 
 # virtio-input.c
 virtio_input_queue_full(void) "queue full"
+
+# lasips2.c
+lasips2_reg_read(unsigned int size, int id, uint64_t addr, const char *name, uint64_t val) "%u %d addr 0x%"PRIx64 "%s -> 0x%"PRIx64
+lasips2_reg_write(unsigned int size, int id, uint64_t addr, const char *name, uint64_t val) "%u %d addr 0x%"PRIx64 "%s <- 0x%"PRIx64
+lasips2_intr(unsigned int val) "%d"
Signature · 1 line
2.20.1
#15 lore raw

Re: [PULL v3 04/11] hppa: add emulation of LASI PS2 controllers

Cc'ing Eduardo/Markus.

Quoted 41 lines · Richard Henderson

From: Sven Schnelle <[email protected]>

Message-Id: <[email protected]>

---
  include/hw/input/lasips2.h |  16 ++
  include/hw/input/ps2.h     |   1 +
  hw/hppa/lasi.c             |  10 +-
  hw/input/lasips2.c         | 291 +++++++++++++++++++++++++++++++++++++
  hw/input/ps2.c             |   5 +
  hw/hppa/Kconfig            |   1 +
  hw/input/Kconfig           |   3 +
  hw/input/Makefile.objs     |   1 +
  hw/input/trace-events      |   5 +
  9 files changed, 332 insertions(+), 1 deletion(-)

create mode 100644 include/hw/input/lasips2.h
create mode 100644 hw/input/lasips2.c

include/hw/input/lasips2.h new file +16 -0
new file mode 100644
index 0000000000..0cd7b59064
@@ -0,0 +1,16 @@
+/*
+ * QEMU LASI PS/2 emulation
+ *
+ * Copyright (c) 2019 Sven Schnelle
+ *
+ */
+#ifndef HW_INPUT_LASIPS2_H
+#define HW_INPUT_LASIPS2_H
+
+#include "exec/hwaddr.h"
+
+#define TYPE_LASIPS2 "lasips2"
+
+void lasips2_init(MemoryRegion *address_space, hwaddr base, qemu_irq irq);
+
+#endif /* HW_INPUT_LASIPS2_H */

[...]

include/hw/input/lasips2.h

Quoted 7 lines
include/hw/input/lasips2.h new file +7 -0
+
+void lasips2_init(MemoryRegion *address_space,
+                  hwaddr base, qemu_irq irq)
+{
+    LASIPS2State *s;
+
+    s = g_malloc0(sizeof(LASIPS2State));

At the current QOM conversion rate, we should have all devices QOM'ified by 2027, so please do not introduce non-QOM devices...

Does anyone have an idea how we could refuse them using checkpatch?

Quoted 78 lines
include/hw/input/lasips2.h new file +19 -0
+
+    s->irq = irq;
+    s->mouse.id = 1;
+    s->kbd.parent = s;
+    s->mouse.parent = s;
+
+    vmstate_register(NULL, base, &vmstate_lasips2, s);
+
+    s->kbd.dev = ps2_kbd_init(ps2dev_update_irq, &s->kbd);
+    s->mouse.dev = ps2_mouse_init(ps2dev_update_irq, &s->mouse);
+
+    memory_region_init_io(&s->kbd.reg, NULL, &lasips2_reg_ops, &s->kbd,
+                          "lasips2-kbd", 0x100);
+    memory_region_add_subregion(address_space, base, &s->kbd.reg);
+
+    memory_region_init_io(&s->mouse.reg, NULL, &lasips2_reg_ops, &s->mouse,
+                          "lasips2-mouse", 0x100);
+    memory_region_add_subregion(address_space, base + 0x100, &s->mouse.reg);
+}
hw/input/ps2.c +5 -0
index 0b671b6339..f8746d2f52 100644
@@ -192,6 +192,11 @@ static void ps2_reset_queue(PS2State *s)
      q->count = 0;
  }
 
+int ps2_queue_empty(PS2State *s)
+{
+    return s->queue.count == 0;
+}
+
  void ps2_queue_noirq(PS2State *s, int b)
  {
      PS2Queue *q = &s->queue;
hw/hppa/Kconfig +1 -0
index 2a7b38d6d6..7f9be7f25c 100644
@@ -11,3 +11,4 @@ config DINO
      select MC146818RTC
      select LSI_SCSI_PCI
      select LASI_82596
+    select LASIPS2
hw/input/Kconfig +3 -0
index 287f08887b..25c77a1b87 100644
@@ -41,3 +41,6 @@ config VHOST_USER_INPUT
 
  config TSC210X
      bool
+
+config LASIPS2
+    select PS2
hw/input/Makefile.objs +1 -0
index a1bc502ed0..f98f635685 100644
@@ -15,3 +15,4 @@ common-obj-$(CONFIG_VHOST_USER_INPUT) += vhost-user-input.o
  obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
  obj-$(CONFIG_PXA2XX) += pxa2xx_keypad.o
  obj-$(CONFIG_TSC210X) += tsc210x.o
+obj-$(CONFIG_LASIPS2) += lasips2.o
hw/input/trace-events +5 -0
index cf072fa2f8..a2888fd10c 100644
@@ -53,3 +53,8 @@ tsc2005_sense(const char *state) "touchscreen sense %s"
 
  # virtio-input.c
  virtio_input_queue_full(void) "queue full"
+
+# lasips2.c
+lasips2_reg_read(unsigned int size, int id, uint64_t addr, const char *name, uint64_t val) "%u %d addr 0x%"PRIx64 "%s -> 0x%"PRIx64
+lasips2_reg_write(unsigned int size, int id, uint64_t addr, const char *name, uint64_t val) "%u %d addr 0x%"PRIx64 "%s <- 0x%"PRIx64
+lasips2_intr(unsigned int val) "%d"
#16 lore raw

[PULL v3 09/11] hw/hppa/machine: Restrict the total memory size to 3GB

In reply to Richard Henderson

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

The hardware expects DIMM slots of 1 or 2 GB, allowing up to 4 GB of memory. We want to accept the same amount of memory the hardware can deal with. DIMMs of 768MB are not available.

However we have to deal with a firmware limitation: currently SeaBIOS only supports 32-bit, and expects the RAM size in a 32-bit register. When using a 4GB configuration, the 32-bit register get truncated and we report a size of 0MB to SeaBIOS, which ends halting the machine:

  $ qemu-system-hppa -m 4g -serial stdio

SeaBIOS: Machine configured with too little memory (0 MB), minimum is 16 MB.

SeaBIOS wants SYSTEM HALT.

The easiest way is to restrict the machine to 3GB of memory.

Message-Id: <[email protected]>

---
 hw/hppa/machine.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
hw/hppa/machine.c +4 -5
index e57530950a..57cb4e0f34 100644
@@ -92,12 +92,11 @@ static void machine_hppa_init(MachineState *machine)
         g_free(name);
     }
 
-    /* Limit main memory. */
-    if (ram_size > FIRMWARE_START) {
-        machine->ram_size = ram_size = FIRMWARE_START;
-    }
-
     /* Main memory region. */
+    if (machine->ram_size > 3 * GiB) {
+        error_report("RAM size is currently restricted to 3GB");
+        exit(EXIT_FAILURE);
+    }
     ram_region = g_new(MemoryRegion, 1);
     memory_region_allocate_system_memory(ram_region, OBJECT(machine),
                                          "ram", ram_size);
Signature · 1 line
2.20.1
#17 lore raw

[PULL v3 11/11] target/hppa: Allow, but diagnose, LDCW aligned only mod 4

In reply to Richard Henderson

The PA-RISC 1.1 specification says that LDCW must be aligned mod 16 or the operation is undefined. However, real hardware only generates an unaligned access trap for unaligned mod 4.

Match real hardware, but diagnose with GUEST_ERROR a violation of the specification.

At the same time fix a bug in the initialization of mop, where the size was specified twice, and another to free the zero temporary.

---
 target/hppa/helper.h    |  2 ++
 target/hppa/op_helper.c |  9 +++++++++
 target/hppa/translate.c | 15 ++++++++++++++-
 3 files changed, 25 insertions(+), 1 deletion(-)
target/hppa/helper.h +2 -0
index 38d834ef6b..2d483aab58 100644
@@ -17,6 +17,8 @@ DEF_HELPER_FLAGS_3(stby_b_parallel, TCG_CALL_NO_WG, void, env, tl, tr)
 DEF_HELPER_FLAGS_3(stby_e, TCG_CALL_NO_WG, void, env, tl, tr)
 DEF_HELPER_FLAGS_3(stby_e_parallel, TCG_CALL_NO_WG, void, env, tl, tr)
 
+DEF_HELPER_FLAGS_1(ldc_check, TCG_CALL_NO_RWG, void, tl)
+
 DEF_HELPER_FLAGS_4(probe, TCG_CALL_NO_WG, tr, env, tl, i32, i32)
 
 DEF_HELPER_FLAGS_1(loaded_fr0, TCG_CALL_NO_RWG, void, env)
target/hppa/op_helper.c +9 -0
index f0516e81f1..7823706e9c 100644
@@ -153,6 +153,15 @@ void HELPER(stby_e_parallel)(CPUHPPAState *env, target_ulong addr,
     do_stby_e(env, addr, val, true, GETPC());
 }
 
+void HELPER(ldc_check)(target_ulong addr)
+{
+    if (unlikely(addr & 0xf)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "Undefined ldc to unaligned address mod 16: "
+                      TARGET_FMT_lx "\n", addr);
+    }
+}
+
 target_ureg HELPER(probe)(CPUHPPAState *env, target_ulong addr,
                           uint32_t level, uint32_t want)
 {
target/hppa/translate.c +14 -1
index f25927aeca..52d7bea1ea 100644
@@ -2942,7 +2942,7 @@ static bool trans_st(DisasContext *ctx, arg_ldst *a)
 
 static bool trans_ldc(DisasContext *ctx, arg_ldst *a)
 {
-    MemOp mop = MO_TEUL | MO_ALIGN_16 | a->size;
+    MemOp mop = MO_TE | MO_ALIGN | a->size;
     TCGv_reg zero, dest, ofs;
     TCGv_tl addr;
 
@@ -2958,8 +2958,21 @@ static bool trans_ldc(DisasContext *ctx, arg_ldst *a)
 
     form_gva(ctx, &addr, &ofs, a->b, a->x, a->scale ? a->size : 0,
              a->disp, a->sp, a->m, ctx->mmu_idx == MMU_PHYS_IDX);
+
+    /*
+     * For hppa1.1, LDCW is undefined unless aligned mod 16.
+     * However actual hardware succeeds with aligned mod 4.
+     * Detect this case and log a GUEST_ERROR.
+     *
+     * TODO: HPPA64 relaxes the over-alignment requirement
+     * with the ,co completer.
+     */
+    gen_helper_ldc_check(addr);
+
     zero = tcg_const_reg(0);
     tcg_gen_atomic_xchg_reg(dest, addr, zero, ctx->mmu_idx, mop);
+    tcg_temp_free(zero);
+
     if (a->m) {
         save_gpr(ctx, a->b, ofs);
     }
Signature · 1 line
2.20.1
#18 lore raw

[PULL v3 06/11] seabios-hppa: update to latest version

In reply to Richard Henderson

From: Sven Schnelle <[email protected]>

Helge Deller (13):
Add PDC_MEM_MAP and ENTRY_INIT_SRCH_FRST for OSF/MkLinux
Return non-existant BTLB for PDC_BLOCK_TLB
Add serial, parallel and LAN port support of LASI chip
Implement ENTRY_IO_BBLOCK_IN IODC function
Do not print \r on parisc SeaBIOS
Fix serial ports and add PDC_MODEL functions for special instructions enablement Implement SeaBIOS returning additional addresses. Fixes HP-UX boot. Fix mod_pgs (number of pages) for graphic cards
Merge pull request #3 from svenschnelle/sti
Merge pull request #4 from svenschnelle/parisc-qemu-4.1.0
parisc: Implement PDC rendenzvous
parisc: Improve soft power button emulation
parisc: Fix line wrapping in STI console code

Sven Schnelle (7):
parisc: fix PDC info for graphics adapter
parisc: add missing header guard to hppa.h
parisc: add LASI PS/2 emulation.
parisc: Add STI support
parisc: wire up graphics console
parisc: Add support for setting STI screen resolution
parisc: support LASI RTC register

Required for STI and LASI support. Also adds a few Bugfixes.

Message-Id: <[email protected]>

---
 pc-bios/hppa-firmware.img | Bin 783724 -> 766136 bytes
 roms/seabios-hppa         |   2 +-
 2 files changed, 1 insertion(+), 1 deletion(-)
pc-bios/hppa-firmware.img binary +0 -0
index c79e1e923cf6e1e06f28e43e87fce48707971611..82d98b13536cdc9830edfec5caa33bc5b09ca912 100644
GIT binary patch
roms/seabios-hppa +1 -1
index 0f4fe84658..1630ac7d65 160000
@@ -1 +1 @@
-Subproject commit 0f4fe84658165e96ce35870fd19fc634e182e77b
+Subproject commit 1630ac7d65c4a09218cc677f1fa56cd5b3140447
Signature · 1 line
2.20.1
#19 lore raw
In reply to Richard Henderson
Quoted 18 lines · Richard Henderson

Version 3 fixes printf formatting errors, and some indentation errors, in artist.c, lasips2.c, and i82596.c.

r~

The following changes since commit ba2ed84fe6a78f64b2da441750fc6e925d94106a:

Merge remote-tracking branch 'remotes/palmer/tags/riscv-for-master-5.0-sf1' into staging (2020-01-24 12:34:04 +0000)

are available in the Git repository at:

https://github.com/rth7680/qemu.git tags/pull-pa-20200124

for you to fetch changes up to 5b33eecce852658121b1190fee466f01a69e1910:

Quoted 7 lines

target/hppa: Allow, but diagnose, LDCW aligned only mod 4 (2020-01-24 14:35:58 -0800)

----------------------------------------------------------------
Improve LASI emulation
Add Artist graphics
Fix main memory allocation
Improve LDCW emulation wrt real hw

This fails to compile -- I suspect your pullreq needs updating to account for something in Paolo's most recent applied pull:

/home/petmay01/qemu-for-merges/hw/net/lasi_i82596.c: In function 'lasi_82596_class_init':
/home/petmay01/qemu-for-merges/hw/net/lasi_i82596.c:171:7: error:
'DeviceClass {aka struct DeviceClass}' has no member named 'props'

     dc->props = lasi_82596_properties;
       ^

(and similar in hw/display/artist.c)

Looks like these should now be

     device_class_set_props(dc, lasi_82596_properties);

thanks
-- PMM