Skip to contentSkip to conversation

s390x: Add support for virtio-blk-pci IPL device

PATCH Superseded [email protected] 5 participants 36 messages Dec 10, 2025 – Jan 9

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover s390x: Add support for virtio-blk-pci IPL device

From: Jared Rossi <[email protected]>

This patch series introduces an IPLB subtype to support PCI devices, which may be built if a device has been assigned a boot index and is identified as a PCI device with a corresponding s390 PCI Bus device.

Boot support is only added for virtio-blk-pci at this time and is limited to devices with an assigned bootindex.

A "loadparm" property is added to PCI boot devices on s390x.

A simple test to check basic functionality is added to the cdrom-tests in qtest.

Changes v1 -> v2:
- Store boot device type/bus as fields of VDev to reduce global variables
- Move existing CLP definitions for easier use in BIOS
- Define a constant for the configuration BAR rather than using a magic 15
- Read BARs from configuration space rather than assuming BAR 4 is always used
- Rework read/write logic to avoid potential buffer overflows
- Create wrappers to do byte swapping as part of common read/writes
- Add "loadparm" property to PCI boot devices on s390x

Jared Rossi (10):
pc-bios/s390-ccw: Fix misattributed function prototypes pc-bios/s390-ccw: Store boot device type and bus separately pc-bios/s390-ccw: Split virtio-ccw and generic virtio include/hw/s390x: Move CLP definitions for easier BIOS access pc-bios/s390-ccw: Introduce CLP Architecture pc-bios/s390-ccw: Introduce PCI device pc-bios/s390-ccw: Add support for virtio-blk-pci IPL
s390x: Build IPLB for virtio-pci devices
hw: Add "loadparm" property to PCI devices for booting on s390x tests/qtest: Add s390x PCI boot test to cdrom-test.c

 hw/s390x/ipl.h                            |   8 +-
 include/hw/pci/pci_device.h               |   3 +
 include/hw/s390x/ipl/qipl.h               |  17 +
 include/hw/s390x/{ => ipl}/s390-pci-clp.h |   0
 include/hw/s390x/s390-pci-bus.h           |   4 +-
 pc-bios/s390-ccw/clp.h                    |  24 ++
 pc-bios/s390-ccw/iplb.h                   |   4 -
 pc-bios/s390-ccw/pci.h                    |  88 ++++++
 pc-bios/s390-ccw/s390-ccw.h               |   7 -
 pc-bios/s390-ccw/virtio-ccw.h             |  24 ++
 pc-bios/s390-ccw/virtio-pci.h             |  79 +++++
 pc-bios/s390-ccw/virtio-scsi.h            |   2 +-
 pc-bios/s390-ccw/virtio.h                 |  15 +-
 hw/pci/pci.c                              |  39 +++
 hw/s390x/ipl.c                            |  63 +++-
 hw/s390x/s390-pci-bus.c                   |   2 +-
 hw/s390x/s390-pci-vfio.c                  |   2 +-
 pc-bios/s390-ccw/bootmap.c                |   2 +-
 pc-bios/s390-ccw/clp.c                    |  96 ++++++
 pc-bios/s390-ccw/main.c                   |  80 ++++-
 pc-bios/s390-ccw/netmain.c                |   2 +-
 pc-bios/s390-ccw/pci.c                    | 331 ++++++++++++++++++++
 pc-bios/s390-ccw/virtio-blkdev.c          |  59 ++--
 pc-bios/s390-ccw/virtio-ccw.c             | 242 +++++++++++++++
 pc-bios/s390-ccw/virtio-net.c             |   5 +-
 pc-bios/s390-ccw/virtio-pci.c             | 360 ++++++++++++++++++++++
 pc-bios/s390-ccw/virtio-scsi.c            |   6 +-
 pc-bios/s390-ccw/virtio.c                 | 239 +++-----------
 tests/qtest/cdrom-test.c                  |   7 +
 pc-bios/s390-ccw/Makefile                 |   3 +-
 30 files changed, 1552 insertions(+), 261 deletions(-)
 rename include/hw/s390x/{ => ipl}/s390-pci-clp.h (100%)
 create mode 100644 pc-bios/s390-ccw/clp.h
 create mode 100644 pc-bios/s390-ccw/pci.h
 create mode 100644 pc-bios/s390-ccw/virtio-ccw.h
 create mode 100644 pc-bios/s390-ccw/virtio-pci.h
 create mode 100644 pc-bios/s390-ccw/clp.c
 create mode 100644 pc-bios/s390-ccw/pci.c
 create mode 100644 pc-bios/s390-ccw/virtio-ccw.c
 create mode 100644 pc-bios/s390-ccw/virtio-pci.c
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 1/10 pc-bios/s390-ccw: Fix misattributed function prototypes

From: Jared Rossi <[email protected]>

The virtio-blkdev functions are incorrectly listed in s390-ccw.h as belonging to virtio.c. Additionally, virtio_load_direct() has an unused subchan_id argument.

Remove the unused argument and move the prototypes to virtio.h so that they are independent from the CCW bus.

---
 pc-bios/s390-ccw/s390-ccw.h      | 4 ----
 pc-bios/s390-ccw/virtio.h        | 7 +++++++
 pc-bios/s390-ccw/bootmap.c       | 2 +-
 pc-bios/s390-ccw/virtio-blkdev.c | 2 +-
 4 files changed, 9 insertions(+), 6 deletions(-)
pc-bios/s390-ccw/s390-ccw.h +0 -4
index b1dc35cded..47ea66bd4d 100644
@@ -67,11 +67,7 @@ void sclp_get_loadparm_ascii(char *loadparm);
 int sclp_read(char *str, size_t count);
 
 /* virtio.c */
-unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2,
-                                 unsigned long subchan_id, void *load_addr);
 bool virtio_is_supported(SubChannelId schid);
-int virtio_blk_setup_device(SubChannelId schid);
-int virtio_read(unsigned long sector, void *load_addr);
 
 /* bootmap.c */
 void zipl_load(void);
pc-bios/s390-ccw/virtio.h +7 -0
index 5c5e808a50..597bd42358 100644
@@ -277,7 +277,14 @@ int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd);
 int virtio_reset(VDev *vdev);
 int virtio_setup_ccw(VDev *vdev);
 
+/* virtio-net.c */
 int virtio_net_init(void *mac_addr);
 void virtio_net_deinit(void);
 
+/* virtio-blkdev.c */
+int virtio_blk_setup_device(SubChannelId schid);
+int virtio_read(unsigned long sector, void *load_addr);
+unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2,
+                                 void *load_addr);
+
 #endif /* VIRTIO_H */
pc-bios/s390-ccw/bootmap.c +1 -1
index 0f8baa0198..420ee32eff 100644
@@ -662,7 +662,7 @@ static int zipl_load_segment(ComponentEntry *entry)
                  */
                 break;
             }
-            address = virtio_load_direct(cur_desc[0], cur_desc[1], 0,
+            address = virtio_load_direct(cur_desc[0], cur_desc[1],
                                          (void *)address);
             if (!address) {
                 puts("zIPL load segment failed");
pc-bios/s390-ccw/virtio-blkdev.c +1 -1
index 7b2d1e20f4..4b819dd80f 100644
@@ -64,7 +64,7 @@ int virtio_read_many(unsigned long sector, void *load_addr, int sec_num)
 }
 
 unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2,
-                                 unsigned long subchan_id, void *load_addr)
+                                 void *load_addr)
 {
     u8 status;
     int sec = rec_list1;
Signature · 1 line
2.49.0
Reviewed-by on v2 1/10
#2 lore raw

Re: [PATCH 01/10] pc-bios/s390-ccw: Fix misattributed function prototypes

Quoted 8 lines · On Wed, 2025-12-10 at 15:54 -0500, [email protected] wrote:

From: Jared Rossi <[email protected]>

The virtio-blkdev functions are incorrectly listed in s390-ccw.h as belonging to virtio.c. Additionally, virtio_load_direct() has an unused subchan_id argument.

Remove the unused argument and move the prototypes to virtio.h so that they are independent from the CCW bus.

Quoted 8 lines
---
 pc-bios/s390-ccw/s390-ccw.h      | 4 ----
 pc-bios/s390-ccw/virtio.h        | 7 +++++++
 pc-bios/s390-ccw/bootmap.c       | 2 +-
 pc-bios/s390-ccw/virtio-blkdev.c | 2 +-
 4 files changed, 9 insertions(+), 6 deletions(-)
Quoted 62 lines
pc-bios/s390-ccw/s390-ccw.h +0 -4
index b1dc35cded..47ea66bd4d 100644
@@ -67,11 +67,7 @@ void sclp_get_loadparm_ascii(char *loadparm);
 int sclp_read(char *str, size_t count);
 
 /* virtio.c */
-unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2,
-                                 unsigned long subchan_id, void *load_addr);
 bool virtio_is_supported(SubChannelId schid);
-int virtio_blk_setup_device(SubChannelId schid);
-int virtio_read(unsigned long sector, void *load_addr);
 
 /* bootmap.c */
 void zipl_load(void);
pc-bios/s390-ccw/virtio.h +7 -0
index 5c5e808a50..597bd42358 100644
@@ -277,7 +277,14 @@ int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd);
 int virtio_reset(VDev *vdev);
 int virtio_setup_ccw(VDev *vdev);
 
+/* virtio-net.c */
 int virtio_net_init(void *mac_addr);
 void virtio_net_deinit(void);
 
+/* virtio-blkdev.c */
+int virtio_blk_setup_device(SubChannelId schid);
+int virtio_read(unsigned long sector, void *load_addr);
+unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2,
+                                 void *load_addr);
+
 #endif /* VIRTIO_H */
pc-bios/s390-ccw/bootmap.c +1 -1
index 0f8baa0198..420ee32eff 100644
@@ -662,7 +662,7 @@ static int zipl_load_segment(ComponentEntry *entry)
                  */
                 break;
             }
-            address = virtio_load_direct(cur_desc[0], cur_desc[1], 0,
+            address = virtio_load_direct(cur_desc[0], cur_desc[1],
                                          (void *)address);
             if (!address) {
                 puts("zIPL load segment failed");
pc-bios/s390-ccw/virtio-blkdev.c +1 -1
index 7b2d1e20f4..4b819dd80f 100644
@@ -64,7 +64,7 @@ int virtio_read_many(unsigned long sector, void *load_addr, int sec_num)
 }
 
 unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2,
-                                 unsigned long subchan_id, void *load_addr)
+                                 void *load_addr)
 {
     u8 status;
     int sec = rec_list1;
#3 lore raw

PATCH v2 2/10 pc-bios/s390-ccw: Store boot device type and bus separately

In reply to [email protected]

From: Jared Rossi <[email protected]>

Store both the device type (e.g. block) and device bus (e.g. CCW) to determine IPL format rather than assume all devices can be identified by CCW specific sense data.

---
 hw/s390x/ipl.h                   |  5 ----
 include/hw/s390x/ipl/qipl.h      |  7 ++++++
 pc-bios/s390-ccw/iplb.h          |  4 ----
 pc-bios/s390-ccw/virtio.h        |  2 ++
 pc-bios/s390-ccw/main.c          | 10 +++++---
 pc-bios/s390-ccw/virtio-blkdev.c | 39 +++++++++++++++++++-------------
 pc-bios/s390-ccw/virtio.c        | 13 +++++++----
 7 files changed, 48 insertions(+), 32 deletions(-)
hw/s390x/ipl.h +0 -5
index 505cded490..aec2244321 100644
@@ -103,11 +103,6 @@ QEMU_BUILD_BUG_MSG(offsetof(S390IPLState, iplb) & 3, "alignment of iplb wrong");
 #define DIAG308_PV_STORE            9
 #define DIAG308_PV_START            10
 
-#define S390_IPL_TYPE_FCP 0x00
-#define S390_IPL_TYPE_CCW 0x02
-#define S390_IPL_TYPE_PV 0x05
-#define S390_IPL_TYPE_QEMU_SCSI 0xff
-
 #define S390_IPLB_HEADER_LEN 8
 #define S390_IPLB_MIN_PV_LEN 148
 #define S390_IPLB_MIN_CCW_LEN 200
include/hw/s390x/ipl/qipl.h +7 -0
index 6824391111..8199b839f0 100644
@@ -20,6 +20,13 @@
 #define LOADPARM_LEN    8
 #define NO_LOADPARM "\0\0\0\0\0\0\0\0"
 
+#define S390_IPL_TYPE_FCP 0x00
+#define S390_IPL_TYPE_CCW 0x02
+#define S390_IPL_TYPE_PV 0x05
+#define S390_IPL_TYPE_QEMU_SCSI 0xff
+
+#define QEMU_DEFAULT_IPL S390_IPL_TYPE_CCW
+
 /*
  * The QEMU IPL Parameters will be stored at absolute address
  * 204 (0xcc) which means it is 32-bit word aligned but not
pc-bios/s390-ccw/iplb.h +0 -4
index 08f259ff31..926e8eed5d 100644
@@ -23,10 +23,6 @@ extern QemuIplParameters qipl;
 extern IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE)));
 extern bool have_iplb;
 
-#define S390_IPL_TYPE_FCP 0x00
-#define S390_IPL_TYPE_CCW 0x02
-#define S390_IPL_TYPE_QEMU_SCSI 0xff
-
 static inline bool manage_iplb(IplParameterBlock *iplb, bool store)
 {
     register unsigned long addr asm("0") = (unsigned long) iplb;
pc-bios/s390-ccw/virtio.h +2 -0
index 597bd42358..d557a4a90e 100644
@@ -239,6 +239,8 @@ struct VDev {
     VirtioGDN guessed_disk_nature;
     SubChannelId schid;
     SenseId senseid;
+    VirtioDevType dev_type;
+    int ipl_type;
     union {
         VirtioBlkConfig blk;
         VirtioScsiConfig scsi;
pc-bios/s390-ccw/main.c +7 -3
index 76bf743900..fef192c934 100644
@@ -162,7 +162,7 @@ static void menu_setup(void)
         return;
     }
 
-    switch (iplb.pbt) {
+    switch (virtio_get_device()->ipl_type) {
     case S390_IPL_TYPE_CCW:
     case S390_IPL_TYPE_QEMU_SCSI:
         menu_set_parms(qipl.qipl_flags & BOOT_MENU_FLAG_MASK,
@@ -190,6 +190,7 @@ static void css_setup(void)
 static void boot_setup(void)
 {
     char lpmsg[] = "LOADPARM=[________]\n";
+    VDev *vdev = virtio_get_device();
 
     if (have_iplb && memcmp(iplb.loadparm, NO_LOADPARM, LOADPARM_LEN) != 0) {
         ebcdic_to_ascii((char *) iplb.loadparm, loadparm_str, LOADPARM_LEN);
@@ -198,7 +199,10 @@ static void boot_setup(void)
     }
 
     if (have_iplb) {
+        vdev->ipl_type = iplb.pbt;
         menu_setup();
+    } else {
+        vdev->ipl_type = QEMU_DEFAULT_IPL;
     }
 
     memcpy(lpmsg + 10, loadparm_str, 8);
@@ -216,7 +220,7 @@ static bool find_boot_device(void)
     VDev *vdev = virtio_get_device();
     bool found = false;
 
-    switch (iplb.pbt) {
+    switch (vdev->ipl_type) {
     case S390_IPL_TYPE_CCW:
         vdev->scsi_device_selected = false;
         debug_print_int("device no. ", iplb.ccw.devno);
@@ -245,7 +249,7 @@ static int virtio_setup(void)
     vdev->is_cdrom = false;
     int ret;
 
-    switch (vdev->senseid.cu_model) {
+    switch (vdev->dev_type) {
     case VIRTIO_ID_NET:
         puts("Network boot device detected");
         return 0;
pc-bios/s390-ccw/virtio-blkdev.c +23 -16
index 4b819dd80f..f40a9407c2 100644
@@ -53,14 +53,14 @@ int virtio_read_many(unsigned long sector, void *load_addr, int sec_num)
 {
     VDev *vdev = virtio_get_device();
 
-    switch (vdev->senseid.cu_model) {
+    switch (vdev->dev_type) {
     case VIRTIO_ID_BLOCK:
         return virtio_blk_read_many(vdev, sector, load_addr, sec_num);
     case VIRTIO_ID_SCSI:
         return virtio_scsi_read_many(vdev, sector, load_addr, sec_num);
+    default:
+        return -1;
     }
-
-    return -1;
 }
 
 unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2,
@@ -119,7 +119,7 @@ void virtio_assume_iso9660(void)
 {
     VDev *vdev = virtio_get_device();
 
-    switch (vdev->senseid.cu_model) {
+    switch (vdev->dev_type) {
     case VIRTIO_ID_BLOCK:
         vdev->guessed_disk_nature = VIRTIO_GDN_SCSI;
         vdev->config.blk.blk_size = VIRTIO_ISO_BLOCK_SIZE;
@@ -129,6 +129,8 @@ void virtio_assume_iso9660(void)
     case VIRTIO_ID_SCSI:
         vdev->scsi_block_size = VIRTIO_ISO_BLOCK_SIZE;
         break;
+    default:
+        return;
     }
 }
 
@@ -139,13 +141,15 @@ void virtio_assume_eckd(void)
     vdev->guessed_disk_nature = VIRTIO_GDN_DASD;
     vdev->blk_factor = 1;
     vdev->config.blk.physical_block_exp = 0;
-    switch (vdev->senseid.cu_model) {
+    switch (vdev->dev_type) {
     case VIRTIO_ID_BLOCK:
         vdev->config.blk.blk_size = VIRTIO_DASD_DEFAULT_BLOCK_SIZE;
         break;
     case VIRTIO_ID_SCSI:
         vdev->config.blk.blk_size = vdev->scsi_block_size;
         break;
+    default:
+        return;
     }
     vdev->config.blk.geometry.heads = 15;
     vdev->config.blk.geometry.sectors =
@@ -162,50 +166,52 @@ bool virtio_ipl_disk_is_valid(void)
         return true;
     }
 
-    return (vdev->senseid.cu_model == VIRTIO_ID_BLOCK ||
-            vdev->senseid.cu_model == VIRTIO_ID_SCSI) &&
-           blksize >= 512 && blksize <= 4096;
+    return (vdev->dev_type == VIRTIO_ID_BLOCK || vdev->dev_type == VIRTIO_ID_SCSI)
+           && blksize >= 512 && blksize <= 4096;
 }
 
 int virtio_get_block_size(void)
 {
     VDev *vdev = virtio_get_device();
 
-    switch (vdev->senseid.cu_model) {
+    switch (vdev->dev_type) {
     case VIRTIO_ID_BLOCK:
         return vdev->config.blk.blk_size;
     case VIRTIO_ID_SCSI:
         return vdev->scsi_block_size;
+    default:
+        return 0;
     }
-    return 0;
 }
 
 uint8_t virtio_get_heads(void)
 {
     VDev *vdev = virtio_get_device();
 
-    switch (vdev->senseid.cu_model) {
+    switch (vdev->dev_type) {
     case VIRTIO_ID_BLOCK:
         return vdev->config.blk.geometry.heads;
     case VIRTIO_ID_SCSI:
         return vdev->guessed_disk_nature == VIRTIO_GDN_DASD
                ? vdev->config.blk.geometry.heads : 255;
+    default:
+        return 0;
     }
-    return 0;
 }
 
 uint8_t virtio_get_sectors(void)
 {
     VDev *vdev = virtio_get_device();
 
-    switch (vdev->senseid.cu_model) {
+    switch (vdev->dev_type) {
     case VIRTIO_ID_BLOCK:
         return vdev->config.blk.geometry.sectors;
     case VIRTIO_ID_SCSI:
         return vdev->guessed_disk_nature == VIRTIO_GDN_DASD
                ? vdev->config.blk.geometry.sectors : 63;
+    default:
+        return 0;
     }
-    return 0;
 }
 
 uint64_t virtio_get_blocks(void)
@@ -213,13 +219,14 @@ uint64_t virtio_get_blocks(void)
     VDev *vdev = virtio_get_device();
     const uint64_t factor = virtio_get_block_size() / VIRTIO_SECTOR_SIZE;
 
-    switch (vdev->senseid.cu_model) {
+    switch (vdev->dev_type) {
     case VIRTIO_ID_BLOCK:
         return vdev->config.blk.capacity / factor;
     case VIRTIO_ID_SCSI:
         return vdev->scsi_last_block / factor;
+    default:
+        return 0;
     }
-    return 0;
 }
 
 int virtio_blk_setup_device(SubChannelId schid)
pc-bios/s390-ccw/virtio.c +9 -4
index cd6c99c7e3..0f4f201038 100644
@@ -41,7 +41,7 @@ VDev *virtio_get_device(void)
 
 VirtioDevType virtio_get_device_type(void)
 {
-    return vdev.senseid.cu_model;
+    return vdev.dev_type;
 }
 
 /* virtio spec v1.0 para 4.3.3.2 */
@@ -103,7 +103,7 @@ static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
         ccw.flags |= CCW_FLAG_SLI;
     }
 
-    return do_cio(vdev->schid, vdev->senseid.cu_type, ptr2u32(&ccw), CCW_FMT1);
+    return do_cio(vdev->schid, vdev->dev_type, ptr2u32(&ccw), CCW_FMT1);
 }
 
 static void vring_init(VRing *vr, VqInfo *info)
@@ -248,7 +248,7 @@ int virtio_setup_ccw(VDev *vdev)
         return -EIO;
     }
 
-    switch (vdev->senseid.cu_model) {
+    switch (vdev->dev_type) {
     case VIRTIO_ID_NET:
         vdev->nr_vqs = 2;
         vdev->cmd_vr_idx = 0;
@@ -347,12 +347,17 @@ bool virtio_is_supported(SubChannelId schid)
                 true)) {
         return false;
     }
+
+    vdev.dev_type = vdev.senseid.cu_model;
+
     if (vdev.senseid.cu_type == 0x3832) {
-        switch (vdev.senseid.cu_model) {
+        switch (vdev.dev_type) {
         case VIRTIO_ID_BLOCK:
         case VIRTIO_ID_SCSI:
         case VIRTIO_ID_NET:
             return true;
+        default:
+            return false;
         }
     }
     return false;
Signature · 1 line
2.49.0
Reviewed-by on v2 2/10
#4 lore raw

Re: [PATCH 02/10] pc-bios/s390-ccw: Store boot device type and bus separately

Quoted 7 lines · On Wed, 2025-12-10 at 15:54 -0500, [email protected] wrote:

From: Jared Rossi <[email protected]>

Store both the device type (e.g. block) and device bus (e.g. CCW) to determine IPL format rather than assume all devices can be identified by CCW specific sense data.

Quoted 9 lines
---
 hw/s390x/ipl.h                   |  5 ----
 include/hw/s390x/ipl/qipl.h      |  7 ++++++
 pc-bios/s390-ccw/iplb.h          |  4 ----
 pc-bios/s390-ccw/virtio.h        |  2 ++
 pc-bios/s390-ccw/main.c          | 10 +++++---
 pc-bios/s390-ccw/virtio-blkdev.c | 39 +++++++++++++++++++-------------
 pc-bios/s390-ccw/virtio.c        | 13 +++++++----
 7 files changed, 48 insertions(+), 32 deletions(-)

I might've suggested that this be broken up even further, to distinguish between the vdev->ipl_type (nee iplb.pbt) and the vdev->dev_type (nee vdev->senseid.cu_model) changes, and maybe even the consolidation of the S390_IPL_TYPE_* defines, to make it more obvious that things are just moving around without any actual change. But, this is honestly fine.

#5 lore raw

Re: [PATCH 02/10] pc-bios/s390-ccw: Store boot device type and bus separately

In reply to [email protected]
Quoted 8 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Store both the device type (e.g. block) and device bus (e.g. CCW) to determine IPL format rather than assume all devices can be identified by CCW specific sense data.

---

...

Quoted 5 lines
include/hw/s390x/ipl/qipl.h +0 -0
index 6824391111..8199b839f0 100644

include/hw/s390x/ipl/qipl.h line 28

Quoted 9 lines
include/hw/s390x/ipl/qipl.h +6 -0
@@ -20,6 +20,13 @@
  #define LOADPARM_LEN    8
  #define NO_LOADPARM "\0\0\0\0\0\0\0\0"
 
+#define S390_IPL_TYPE_FCP 0x00
+#define S390_IPL_TYPE_CCW 0x02
+#define S390_IPL_TYPE_PV 0x05
+#define S390_IPL_TYPE_QEMU_SCSI 0xff
+
+#define QEMU_DEFAULT_IPL S390_IPL_TYPE_CCW

Would it make sense to turn this into an enum as well?

Quoted 3 lines
include/hw/s390x/ipl/qipl.h +0 -0
  /*
   * The QEMU IPL Parameters will be stored at absolute address
   * 204 (0xcc) which means it is 32-bit word aligned but not

...

Quoted 4 lines
pc-bios/s390-ccw/virtio-blkdev.c +0 -0
index 4b819dd80f..f40a9407c2 100644

...

Quoted 13 lines
pc-bios/s390-ccw/virtio-blkdev.c +2 -0
@@ -129,6 +129,8 @@ void virtio_assume_iso9660(void)
      case VIRTIO_ID_SCSI:
          vdev->scsi_block_size = VIRTIO_ISO_BLOCK_SIZE;
          break;
+    default:
+        return;
      }
  }
 
@@ -139,13 +141,15 @@ void virtio_assume_eckd(void)
      vdev->guessed_disk_nature = VIRTIO_GDN_DASD;
      vdev->blk_factor = 1;
      vdev->config.blk.physical_block_exp = 0;

pc-bios/s390-ccw/virtio-blkdev.c void virtio_assume_eckd(void) · line 152

Quoted 10 lines
pc-bios/s390-ccw/virtio-blkdev.c +3 -1
-    switch (vdev->senseid.cu_model) {
+    switch (vdev->dev_type) {
      case VIRTIO_ID_BLOCK:
          vdev->config.blk.blk_size = VIRTIO_DASD_DEFAULT_BLOCK_SIZE;
          break;
      case VIRTIO_ID_SCSI:
          vdev->config.blk.blk_size = vdev->scsi_block_size;
          break;
+    default:
+        return;

This looks like errors could silently be ignored here. Maybe rather panic() here instead of returning? Or use "break" to maintain the old behavior?

pc-bios/s390-ccw/virtio-blkdev.c void virtio_assume_eckd(void) · line 155

Quoted 3 lines
pc-bios/s390-ccw/virtio-blkdev.c +0 -0
      }
      vdev->config.blk.geometry.heads = 15;
      vdev->config.blk.geometry.sectors =

Thomas

#6 lore raw

PATCH v2 3/10 pc-bios/s390-ccw: Split virtio-ccw and generic virtio

In reply to [email protected]

From: Jared Rossi <[email protected]>

Separate the CCW specific virtio routines and create generic wrappers for easier reuse of existing virtio functions with non-CCW devices.

---
 pc-bios/s390-ccw/s390-ccw.h      |   3 -
 pc-bios/s390-ccw/virtio-ccw.h    |  24 +++
 pc-bios/s390-ccw/virtio-scsi.h   |   2 +-
 pc-bios/s390-ccw/virtio.h        |   7 +-
 pc-bios/s390-ccw/main.c          |  10 +-
 pc-bios/s390-ccw/netmain.c       |   2 +-
 pc-bios/s390-ccw/virtio-blkdev.c |  15 +-
 pc-bios/s390-ccw/virtio-ccw.c    | 242 +++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/virtio-net.c    |   5 +-
 pc-bios/s390-ccw/virtio-scsi.c   |   6 +-
 pc-bios/s390-ccw/virtio.c        | 237 +++++-------------------------
 pc-bios/s390-ccw/Makefile        |   3 +-
 12 files changed, 334 insertions(+), 222 deletions(-)
 create mode 100644 pc-bios/s390-ccw/virtio-ccw.h
 create mode 100644 pc-bios/s390-ccw/virtio-ccw.c
pc-bios/s390-ccw/s390-ccw.h +0 -3
index 47ea66bd4d..ccd68ff0a4 100644
@@ -66,9 +66,6 @@ void sclp_setup(void);
 void sclp_get_loadparm_ascii(char *loadparm);
 int sclp_read(char *str, size_t count);
 
-/* virtio.c */
-bool virtio_is_supported(SubChannelId schid);
-
 /* bootmap.c */
 void zipl_load(void);
 
pc-bios/s390-ccw/virtio-ccw.h new file +24 -0
new file mode 100644
index 0000000000..cdf6a55dc8
@@ -0,0 +1,24 @@
+/*
+ * Virtio definitions for CCW devices
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef VIRTIO_CCW_H
+#define VIRTIO_CCW_H
+
+/* main.c */
+extern SubChannelId blk_schid;
+
+/* virtio-ccw.c */
+int drain_irqs_ccw(SubChannelId schid);
+bool virtio_ccw_is_supported(SubChannelId schid);
+int virtio_ccw_run(VDev *vdev, int vqid, VirtioCmd *cmd);
+long virtio_ccw_notify(SubChannelId schid, int vq_idx, long cookie);
+int virtio_ccw_setup(VDev *vdev);
+int virtio_ccw_reset(VDev *vdev);
+
+#endif
pc-bios/s390-ccw/virtio-scsi.h +1 -1
index c5612e16a2..7a37f8b45a 100644
@@ -69,6 +69,6 @@ static inline bool virtio_scsi_response_ok(const VirtioScsiCmdResp *r)
 
 int virtio_scsi_read_many(VDev *vdev,
                           unsigned long sector, void *load_addr, int sec_num);
-int virtio_scsi_setup_device(SubChannelId schid);
+int virtio_scsi_setup_device(void);
 
 #endif /* VIRTIO_SCSI_H */
pc-bios/s390-ccw/virtio.h +5 -2
index d557a4a90e..e747891a2c 100644
@@ -109,6 +109,7 @@ struct VRing {
 };
 typedef struct VRing VRing;
 
+char *virtio_get_ring_area(void);
 
 /***********************************************
  *               Virtio block                  *
@@ -270,8 +271,10 @@ struct VirtioCmd {
 };
 typedef struct VirtioCmd VirtioCmd;
 
+void vring_init(VRing *vr, VqInfo *info);
+bool virtio_is_supported(VDev *vdev);
 bool vring_notify(VRing *vr);
-int drain_irqs(SubChannelId schid);
+int drain_irqs(VRing *vr);
 void vring_send_buf(VRing *vr, void *p, int len, int flags);
 int vr_poll(VRing *vr);
 int vring_wait_reply(void);
@@ -284,7 +287,7 @@ int virtio_net_init(void *mac_addr);
 void virtio_net_deinit(void);
 
 /* virtio-blkdev.c */
-int virtio_blk_setup_device(SubChannelId schid);
+int virtio_blk_setup_device(void);
 int virtio_read(unsigned long sector, void *load_addr);
 unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2,
                                  void *load_addr);
pc-bios/s390-ccw/main.c +7 -3
index fef192c934..e82d60bbb7 100644
@@ -72,6 +72,7 @@ static int is_dev_possibly_bootable(int dev_no, int sch_no)
     Schib schib;
     int r;
 
+    VDev *vdev = virtio_get_device();
     blk_schid.sch_no = sch_no;
     r = stsch_err(blk_schid, &schib);
     if (r == 3 || r == -EIO) {
@@ -91,7 +92,8 @@ static int is_dev_possibly_bootable(int dev_no, int sch_no)
      * Note: we always have to run virtio_is_supported() here to make
      * sure that the vdev.senseid data gets pre-initialized correctly
      */
-    is_virtio = virtio_is_supported(blk_schid);
+    vdev->schid = blk_schid;
+    is_virtio = virtio_is_supported(vdev);
 
     /* No specific devno given, just return whether the device is possibly bootable */
     if (dev_no < 0) {
@@ -254,10 +256,12 @@ static int virtio_setup(void)
         puts("Network boot device detected");
         return 0;
     case VIRTIO_ID_BLOCK:
-        ret = virtio_blk_setup_device(blk_schid);
+        vdev->schid = blk_schid;
+        ret = virtio_blk_setup_device();
         break;
     case VIRTIO_ID_SCSI:
-        ret = virtio_scsi_setup_device(blk_schid);
+        vdev->schid = blk_schid;
+        ret = virtio_scsi_setup_device();
         break;
     default:
         puts("\n! No IPL device available !\n");
pc-bios/s390-ccw/netmain.c +1 -1
index a9521dff41..651cedf6ef 100644
@@ -500,7 +500,7 @@ static bool find_net_dev(Schib *schib, int dev_no)
             continue;
         }
         enable_subchannel(net_schid);
-        if (!virtio_is_supported(net_schid)) {
+        if (!virtio_is_supported(virtio_get_device())) {
             continue;
         }
         if (virtio_get_device_type() != VIRTIO_ID_NET) {
pc-bios/s390-ccw/virtio-blkdev.c +10 -5
index f40a9407c2..87ab9a9513 100644
@@ -12,6 +12,7 @@
 #include "s390-ccw.h"
 #include "virtio.h"
 #include "virtio-scsi.h"
+#include "virtio-ccw.h"
 
 #define VIRTIO_BLK_F_GEOMETRY   (1 << 4)
 #define VIRTIO_BLK_F_BLK_SIZE   (1 << 6)
@@ -42,7 +43,7 @@ static int virtio_blk_read_many(VDev *vdev, unsigned long sector, void *load_add
     /* Now we can tell the host to read */
     vring_wait_reply();
 
-    if (drain_irqs(vr->schid)) {
+    if (drain_irqs(vr)) {
         /* Well, whatever status is supposed to contain... */
         status = 1;
     }
@@ -229,15 +230,19 @@ uint64_t virtio_get_blocks(void)
     }
 }
 
-int virtio_blk_setup_device(SubChannelId schid)
+int virtio_blk_setup_device(void)
 {
     VDev *vdev = virtio_get_device();
 
     vdev->guest_features[0] = VIRTIO_BLK_F_GEOMETRY | VIRTIO_BLK_F_BLK_SIZE;
-    vdev->schid = schid;
-    virtio_setup_ccw(vdev);
 
     puts("Using virtio-blk.");
 
-    return 0;
+    switch (virtio_get_device()->ipl_type) {
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return virtio_ccw_setup(vdev);
+    }
+
+    return 1;
 }
pc-bios/s390-ccw/virtio-ccw.c new file +242 -0

242 lines not on this page · raw message

pc-bios/s390-ccw/virtio-net.c +3 -2
index 301445bf97..604f1cf003 100644
@@ -19,6 +19,7 @@
 #include <ethernet.h>
 #include "s390-ccw.h"
 #include "virtio.h"
+#include "virtio-ccw.h"
 #include "s390-time.h"
 #include "helper.h"
 
@@ -54,7 +55,7 @@ int virtio_net_init(void *mac_addr)
     rx_last_idx = 0;
 
     vdev->guest_features[0] = VIRTIO_NET_F_MAC_BIT;
-    virtio_setup_ccw(vdev);
+    virtio_ccw_setup(vdev);
 
     if (!(vdev->guest_features[0] & VIRTIO_NET_F_MAC_BIT)) {
         puts("virtio-net device does not support the MAC address feature");
@@ -88,7 +89,7 @@ int send(int fd, const void *buf, int len, int flags)
     while (!vr_poll(txvq)) {
         yield();
     }
-    if (drain_irqs(txvq->schid)) {
+    if (drain_irqs(txvq)) {
         puts("send: drain irqs failed");
         return -1;
     }
pc-bios/s390-ccw/virtio-scsi.c +3 -3
index 71db75ce7b..6ab0f755f2 100644
@@ -15,6 +15,7 @@
 #include "virtio.h"
 #include "scsi.h"
 #include "virtio-scsi.h"
+#include "virtio-ccw.h"
 #include "s390-time.h"
 #include "helper.h"
 
@@ -476,12 +477,11 @@ static int virtio_scsi_setup(VDev *vdev)
     return 0;
 }
 
-int virtio_scsi_setup_device(SubChannelId schid)
+int virtio_scsi_setup_device(void)
 {
     VDev *vdev = virtio_get_device();
 
-    vdev->schid = schid;
-    virtio_setup_ccw(vdev);
+    virtio_ccw_setup(vdev);
 
     if (vdev->config.scsi.sense_size != VIRTIO_SCSI_SENSE_SIZE) {
         puts("Config: sense size mismatch");
pc-bios/s390-ccw/virtio.c +36 -201

289 lines not on this page · raw message

pc-bios/s390-ccw/Makefile +2 -1
index a0f24c94a8..259cff09db 100644
@@ -34,7 +34,8 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d
 .PHONY : all clean build-all distclean
 
 OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \
-	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o
+	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \
+	  virtio-ccw.o
 
 SLOF_DIR := $(SRC_PATH)/../../roms/SLOF
 
Signature · 1 line
2.49.0
#7 lore raw

Re: [PATCH 03/10] pc-bios/s390-ccw: Split virtio-ccw and generic virtio

Quoted 7 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Separate the CCW specific virtio routines and create generic wrappers for easier reuse of existing virtio functions with non-CCW devices.

---

...

Quoted 54 lines
pc-bios/s390-ccw/virtio-ccw.c new file +48 -0
new file mode 100644
index 0000000000..e121826625
@@ -0,0 +1,242 @@
+/*
+ * Virtio functionality for CCW devices
+ *
+ * Copyright (c) 2013 Alexander Graf <[email protected]>
+ * Copyright 2025 IBM Corp. Author(s): Jared Rossi <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or (at
+ * your option) any later version. See the COPYING file in the top-level
+ * directory.
+ */
+
+#include <string.h>
+#include "s390-ccw.h"
+#include "cio.h"
+#include "virtio.h"
+#include "virtio-ccw.h"
+#include "virtio-scsi.h"
+#include "bswap.h"
+#include "helper.h"
+#include "s390-time.h"
+
+/* virtio spec v1.0 para 4.3.3.2 */
+static long kvm_hypercall(unsigned long nr, unsigned long param1,
+                          unsigned long param2, unsigned long param3)
+{
+    register unsigned long r_nr asm("1") = nr;
+    register unsigned long r_param1 asm("2") = param1;
+    register unsigned long r_param2 asm("3") = param2;
+    register unsigned long r_param3 asm("4") = param3;
+    register long retval asm("2");
+
+    asm volatile ("diag %%r2,%%r4,0x500"
+                  : "=d" (retval)
+                  : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
+                  : "memory", "cc");
+
+    return retval;
+}
+
+static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
+{
+    Ccw1 ccw = {};
+
+    ccw.cmd_code = cmd;
+    ccw.cda = (long)ptr;
+    ccw.count = len;
+
+    if (sli) {

pc-bios/s390-ccw/virtio-ccw.c line 58

Quoted 10 lines
pc-bios/s390-ccw/virtio-ccw.c new file +10 -0
+        ccw.flags |= CCW_FLAG_SLI;
+    }
+
+    return do_cio(vdev->schid, vdev->senseid.cu_type, ptr2u32(&ccw), CCW_FMT1);
+}
+
+bool virtio_ccw_is_supported(SubChannelId schid)
+{
+    VDev *vdev = virtio_get_device();
+    vdev->schid = schid;

At the calling site (virtio_ccw_setup), you're doing:

if (!virtio_ccw_is_supported(vdev->schid)) ...

i.e. vdev->schid is already set up, and you already have a pointer to vdev there. So I think it would make more sense to replace the "schid" parameter of this function here to a "vdev" pointer, and drop the call to virtio_get_device() here, and not to re-assign vdev->schid here.

Quoted 27 lines
pc-bios/s390-ccw/virtio-ccw.c new file +27 -0
+    memset(&vdev->senseid, 0, sizeof(vdev->senseid));
+
+    /*
+     * Run sense id command.
+     * The size of the senseid data differs between devices (notably,
+     * between virtio devices and dasds), so specify the largest possible
+     * size and suppress the incorrect length indication for smaller sizes.
+     */
+    if (run_ccw(vdev, CCW_CMD_SENSE_ID, &vdev->senseid, sizeof(vdev->senseid),
+                true)) {
+        return false;
+    }
+
+    vdev->dev_type = vdev->senseid.cu_model;
+
+    if (vdev->senseid.cu_type == 0x3832) {
+        switch (vdev->dev_type) {
+        case VIRTIO_ID_BLOCK:
+        case VIRTIO_ID_SCSI:
+        case VIRTIO_ID_NET:
+            return true;
+        default:
+            return false;
+        }
+    }
+    return false;
+}

...

Quoted 68 lines
pc-bios/s390-ccw/virtio-ccw.c new file +68 -0
+int virtio_ccw_setup(VDev *vdev)
+{
+    int i, cfg_size = 0;
+    uint8_t status;
+    struct VirtioFeatureDesc {
+        uint32_t features;
+        uint8_t index;
+    } __attribute__((packed)) feats;
+
+    if (!virtio_ccw_is_supported(vdev->schid)) {
+        puts("Virtio unsupported for this device ID");
+        return -ENODEV;
+    }
+    /* device ID has been established now */
+
+    vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
+    vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
+
+    virtio_reset(vdev);
+
+    status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
+        puts("Could not write ACKNOWLEDGE status to host");
+        return -EIO;
+    }
+
+    switch (vdev->dev_type) {
+    case VIRTIO_ID_NET:
+        vdev->nr_vqs = 2;
+        vdev->cmd_vr_idx = 0;
+        cfg_size = sizeof(vdev->config.net);
+        break;
+    case VIRTIO_ID_BLOCK:
+        vdev->nr_vqs = 1;
+        vdev->cmd_vr_idx = 0;
+        cfg_size = sizeof(vdev->config.blk);
+        break;
+    case VIRTIO_ID_SCSI:
+        vdev->nr_vqs = 3;
+        vdev->cmd_vr_idx = VR_REQUEST;
+        cfg_size = sizeof(vdev->config.scsi);
+        break;
+    default:
+        puts("Unsupported virtio device");
+        return -ENODEV;
+    }
+
+    status |= VIRTIO_CONFIG_S_DRIVER;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
+        puts("Could not write DRIVER status to host");
+        return -EIO;
+    }
+
+    /* Feature negotiation */
+    for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
+        feats.features = 0;
+        feats.index = i;
+        if (run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false)) {
+            puts("Could not get features bits");
+            return -EIO;
+        }
+
+        vdev->guest_features[i] &= bswap32(feats.features);
+        feats.features = bswap32(vdev->guest_features[i]);
+        if (run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false)) {
+            puts("Could not set features bits");
+            return -EIO;
+        }

pc-bios/s390-ccw/virtio-ccw.c line 211

Quoted 10 lines
pc-bios/s390-ccw/virtio-ccw.c new file +10 -0
+    }
+
+    if (run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false)) {
+        puts("Could not get virtio device configuration");
+        return -EIO;
+    }
+
+    for (i = 0; i < vdev->nr_vqs; i++) {
+        VqInfo info = {
+            .queue = (unsigned long long) virtio_get_ring_area() + (i * VIRTIO_RING_SIZE),

Would it make sense to add a "ring_num" parameter to virtio_get_ring_area(), so that you could call virtio_get_ring_area(i) here instead?

virtio_get_ring_area() would then look like this:

char *virtio_get_ring_area(int ring_num)

{
	return ring_area + ring_num * VIRTIO_RING_SIZE;
}

?

Quoted 31 lines
pc-bios/s390-ccw/virtio-ccw.c new file +31 -0
+            .align = KVM_S390_VIRTIO_RING_ALIGN,
+            .index = i,
+            .num = 0,
+        };
+        VqConfig config = {
+            .index = i,
+            .num = 0,
+        };
+
+        if (run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config),
+                false)) {
+            puts("Could not get virtio device VQ config");
+            return -EIO;
+        }
+        info.num = config.num;
+        vring_init(&vdev->vrings[i], &info);
+        vdev->vrings[i].schid = vdev->schid;
+        if (run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false)) {
+            puts("Cannot set VQ info");
+            return -EIO;
+        }
+    }
+
+    status |= VIRTIO_CONFIG_S_DRIVER_OK;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
+        puts("Could not write DRIVER_OK status to host");
+        return -EIO;
+    }
+
+    return 0;
+}

...

pc-bios/s390-ccw/virtio.c line 5

Quoted 9 lines
pc-bios/s390-ccw/virtio.c +1 -0
index 0f4f201038..0488b3a07e 100644
@@ -2,6 +2,7 @@
   * Virtio driver bits
   *
   * Copyright (c) 2013 Alexander Graf <[email protected]>
+ * Copyright 2025 IBM Corp. Author(s): Jared Rossi <[email protected]>

I assume you wanted to put Authors on a separate line?

Thomas

#8 lore raw

Re: [PATCH 03/10] pc-bios/s390-ccw: Split virtio-ccw and generic virtio

Quoted 81 lines · Thomas Huth
Quoted 7 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Separate the CCW specific virtio routines and create generic wrappers for easier reuse of existing virtio functions with non-CCW devices.

---

...

Quoted 63 lines
pc-bios/s390-ccw/virtio-ccw.c new file +5 -0
new file mode 100644
index 0000000000..e121826625
@@ -0,0 +1,242 @@
+/*
+ * Virtio functionality for CCW devices
+ *
+ * Copyright (c) 2013 Alexander Graf <[email protected]>
+ * Copyright 2025 IBM Corp. Author(s): Jared Rossi 

<[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or (at
+ * your option) any later version. See the COPYING file in the top-level
+ * directory.
+ */

pc-bios/s390-ccw/virtio-ccw.c new file +24 -0
+
+#include <string.h>
+#include "s390-ccw.h"
+#include "cio.h"
+#include "virtio.h"
+#include "virtio-ccw.h"
+#include "virtio-scsi.h"
+#include "bswap.h"
+#include "helper.h"
+#include "s390-time.h"
+
+/* virtio spec v1.0 para 4.3.3.2 */
+static long kvm_hypercall(unsigned long nr, unsigned long param1,
+                          unsigned long param2, unsigned long param3)
+{
+    register unsigned long r_nr asm("1") = nr;
+    register unsigned long r_param1 asm("2") = param1;
+    register unsigned long r_param2 asm("3") = param2;
+    register unsigned long r_param3 asm("4") = param3;
+    register long retval asm("2");
+
+    asm volatile ("diag %%r2,%%r4,0x500"
+                  : "=d" (retval)
+                  : "d" (r_nr), "0" (r_param1), "r"(r_param2), 

"d"(r_param3)
+                  : "memory", "cc");

pc-bios/s390-ccw/virtio-ccw.c new file +17 -0
+
+    return retval;
+}
+
+static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
+{
+    Ccw1 ccw = {};
+
+    ccw.cmd_code = cmd;
+    ccw.cda = (long)ptr;
+    ccw.count = len;
+
+    if (sli) {
+        ccw.flags |= CCW_FLAG_SLI;
+    }
+
+    return do_cio(vdev->schid, vdev->senseid.cu_type, ptr2u32(&ccw), 

CCW_FMT1);

pc-bios/s390-ccw/virtio-ccw.c line 58

Quoted 6 lines
pc-bios/s390-ccw/virtio-ccw.c new file +6 -0
+}
+
+bool virtio_ccw_is_supported(SubChannelId schid)
+{
+    VDev *vdev = virtio_get_device();
+    vdev->schid = schid;

At the calling site (virtio_ccw_setup), you're doing:

Quoted 3 lines

 if (!virtio_ccw_is_supported(vdev->schid)) ...

i.e. vdev->schid is already set up, and you already have a pointer to vdev there. So I think it would make more sense to replace the "schid" parameter of this function here to a "vdev" pointer, and drop the call to virtio_get_device() here, and not to re-assign vdev->schid here.

I will work on cleaning up the vdev->schid assignments and accesses throughout.  Eric pointed out several areas that it seems to be redundant also.

Quoted 120 lines
Quoted 31 lines

+    memset(&vdev->senseid, 0, sizeof(vdev->senseid));
+
+    /*
+     * Run sense id command.
+     * The size of the senseid data differs between devices (notably, +     * between virtio devices and dasds), so specify the largest possible
+     * size and suppress the incorrect length indication for smaller sizes.
+     */
+    if (run_ccw(vdev, CCW_CMD_SENSE_ID, &vdev->senseid, sizeof(vdev->senseid),
+                true)) {
+        return false;
+    }

pc-bios/s390-ccw/virtio-ccw.c new file +15 -0
+
+    vdev->dev_type = vdev->senseid.cu_model;
+
+    if (vdev->senseid.cu_type == 0x3832) {
+        switch (vdev->dev_type) {
+        case VIRTIO_ID_BLOCK:
+        case VIRTIO_ID_SCSI:
+        case VIRTIO_ID_NET:
+            return true;
+        default:
+            return false;
+        }
+    }
+    return false;
+}

...

Quoted 77 lines
pc-bios/s390-ccw/virtio-ccw.c new file +16 -0
+int virtio_ccw_setup(VDev *vdev)
+{
+    int i, cfg_size = 0;
+    uint8_t status;
+    struct VirtioFeatureDesc {
+        uint32_t features;
+        uint8_t index;
+    } __attribute__((packed)) feats;
+
+    if (!virtio_ccw_is_supported(vdev->schid)) {
+        puts("Virtio unsupported for this device ID");
+        return -ENODEV;
+    }
+    /* device ID has been established now */
+
+    vdev->config.blk.blk_size = 0; /* mark "illegal" - setup 

started... */
+    vdev->guessed_disk_nature = VIRTIO_GDN_NONE;

pc-bios/s390-ccw/virtio-ccw.c new file +5 -0
+
+    virtio_reset(vdev);
+
+    status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), 

false)) {
+        puts("Could not write ACKNOWLEDGE status to host"); +        return -EIO;
+    }

pc-bios/s390-ccw/virtio-ccw.c new file +24 -0
+
+    switch (vdev->dev_type) {
+    case VIRTIO_ID_NET:
+        vdev->nr_vqs = 2;
+        vdev->cmd_vr_idx = 0;
+        cfg_size = sizeof(vdev->config.net);
+        break;
+    case VIRTIO_ID_BLOCK:
+        vdev->nr_vqs = 1;
+        vdev->cmd_vr_idx = 0;
+        cfg_size = sizeof(vdev->config.blk);
+        break;
+    case VIRTIO_ID_SCSI:
+        vdev->nr_vqs = 3;
+        vdev->cmd_vr_idx = VR_REQUEST;
+        cfg_size = sizeof(vdev->config.scsi);
+        break;
+    default:
+        puts("Unsupported virtio device");
+        return -ENODEV;
+    }
+
+    status |= VIRTIO_CONFIG_S_DRIVER;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), 

false)) {
+        puts("Could not write DRIVER status to host");
+        return -EIO;
+    }
+
+    /* Feature negotiation */
+    for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) { +        feats.features = 0;
+        feats.index = i;
+        if (run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false)) {
+            puts("Could not get features bits");
+            return -EIO;
+        }
+
+        vdev->guest_features[i] &= bswap32(feats.features); +        feats.features = bswap32(vdev->guest_features[i]); +        if (run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false)) {
+            puts("Could not set features bits");
+            return -EIO;
+        }
+    }
+
+    if (run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false)) {

Quoted 8 lines

+        puts("Could not get virtio device configuration"); +        return -EIO;
+    }
+
+    for (i = 0; i < vdev->nr_vqs; i++) {
+        VqInfo info = {
+            .queue = (unsigned long long) virtio_get_ring_area() + (i * VIRTIO_RING_SIZE),

Would it make sense to add a "ring_num" parameter to virtio_get_ring_area(), so that you could call virtio_get_ring_area(i) here instead?

Quoted 8 lines

virtio_get_ring_area() would then look like this:

char *virtio_get_ring_area(int ring_num)
{
    return ring_area + ring_num * VIRTIO_RING_SIZE; }

?

That makes sense to me.  I'll change it.

Quoted 36 lines
Quoted 35 lines

+            .align = KVM_S390_VIRTIO_RING_ALIGN,
+            .index = i,
+            .num = 0,
+        };
+        VqConfig config = {
+            .index = i,
+            .num = 0,
+        };
+
+        if (run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config),
+                false)) {
+            puts("Could not get virtio device VQ config"); +            return -EIO;
+        }
+        info.num = config.num;
+        vring_init(&vdev->vrings[i], &info);
+        vdev->vrings[i].schid = vdev->schid;
+        if (run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false)) {
+            puts("Cannot set VQ info");
+            return -EIO;
+        }
+    }
+
+    status |= VIRTIO_CONFIG_S_DRIVER_OK;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
+        puts("Could not write DRIVER_OK status to host"); +        return -EIO;
+    }

pc-bios/s390-ccw/virtio-ccw.c new file +3 -0
+
+    return 0;
+}

...

Quoted 12 lines
Quoted 10 lines
pc-bios/s390-ccw/virtio.c +1 -0
index 0f4f201038..0488b3a07e 100644
@@ -2,6 +2,7 @@
    * Virtio driver bits
    *
    * Copyright (c) 2013 Alexander Graf <[email protected]>
+ * Copyright 2025 IBM Corp. Author(s): Jared Rossi 

<[email protected]>

I assume you wanted to put Authors on a separate line?

I don't quite understand what you are asking about here.  Or maybe I don't understand how to attribute the authors in this case?

Much of this was just copy/pasted to the new file, so I included the existing byline too.  Should I format things differently?

Thanks,
Jared Rossi

#9 lore raw

Re: [PATCH 03/10] pc-bios/s390-ccw: Split virtio-ccw and generic virtio

Quoted 5 lines · Jared Rossi
Quoted 2 lines · Thomas Huth
Quoted 1 line · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

...

Quoted 12 lines
Quoted 11 lines

pc-bios/s390-ccw/virtio.c line 5

Quoted 9 lines
pc-bios/s390-ccw/virtio.c +1 -0
index 0f4f201038..0488b3a07e 100644
@@ -2,6 +2,7 @@
    * Virtio driver bits
    *
    * Copyright (c) 2013 Alexander Graf <[email protected]>
+ * Copyright 2025 IBM Corp. Author(s): Jared Rossi <[email protected]>

I assume you wanted to put Authors on a separate line?

Quoted 3 lines

I don't quite understand what you are asking about here.  Or maybe I don't understand how to attribute the authors in this case?

Much of this was just copy/pasted to the new file, so I included the existing byline too.  Should I format things differently?

No, you don't have to change it, I was just surprised to see "Copyright" and "Author(s):" in the same line. People normally put it on separate lines, see e.g. cio.c, menu.c or virtio-scsi.c in the pc-ios/s390-ccw/ directory.

Thomas

#10 lore raw

Re: [PATCH 03/10] pc-bios/s390-ccw: Split virtio-ccw and generic virtio

Quoted 22 lines · Thomas Huth
Quoted 5 lines · Jared Rossi
Quoted 2 lines · Thomas Huth
Quoted 1 line · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

...

Quoted 15 lines
Quoted 11 lines

pc-bios/s390-ccw/virtio.c line 5

Quoted 9 lines
pc-bios/s390-ccw/virtio.c +1 -0
index 0f4f201038..0488b3a07e 100644
@@ -2,6 +2,7 @@
    * Virtio driver bits
    *
    * Copyright (c) 2013 Alexander Graf <[email protected]>
+ * Copyright 2025 IBM Corp. Author(s): Jared Rossi <[email protected]>

I assume you wanted to put Authors on a separate line?

I don't quite understand what you are asking about here.  Or maybe I don't understand how to attribute the authors in this case?

Quoted 6 lines
Quoted 2 lines

Much of this was just copy/pasted to the new file, so I included the existing byline too.  Should I format things differently?

No, you don't have to change it, I was just surprised to see "Copyright" and "Author(s):" in the same line. People normally put it on separate lines, see e.g. cio.c, menu.c or virtio-scsi.c in the pc-ios/s390-ccw/ directory.

The reason for the usual separation is that they have different effects.

Any text that is part of a "Copyright" line is legally associated with, and their presence is protected by the license terms.

Lists of authors have no legal effects, they're merely credits, and are not protected by the license.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#11 lore raw

Re: [PATCH 03/10] pc-bios/s390-ccw: Split virtio-ccw and generic virtio

In reply to [email protected]
Quoted 117 lines · On Wed, 2025-12-10 at 15:54 -0500, [email protected] wrote:

From: Jared Rossi <[email protected]>

Separate the CCW specific virtio routines and create generic wrappers for easier reuse of existing virtio functions with non-CCW devices.

---
 pc-bios/s390-ccw/s390-ccw.h      |   3 -
 pc-bios/s390-ccw/virtio-ccw.h    |  24 +++
 pc-bios/s390-ccw/virtio-scsi.h   |   2 +-
 pc-bios/s390-ccw/virtio.h        |   7 +-
 pc-bios/s390-ccw/main.c          |  10 +-
 pc-bios/s390-ccw/netmain.c       |   2 +-
 pc-bios/s390-ccw/virtio-blkdev.c |  15 +-
 pc-bios/s390-ccw/virtio-ccw.c    | 242 +++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/virtio-net.c    |   5 +-
 pc-bios/s390-ccw/virtio-scsi.c   |   6 +-
 pc-bios/s390-ccw/virtio.c        | 237 +++++-------------------------
 pc-bios/s390-ccw/Makefile        |   3 +-
 12 files changed, 334 insertions(+), 222 deletions(-)
 create mode 100644 pc-bios/s390-ccw/virtio-ccw.h
 create mode 100644 pc-bios/s390-ccw/virtio-ccw.c
pc-bios/s390-ccw/s390-ccw.h +0 -3
index 47ea66bd4d..ccd68ff0a4 100644
@@ -66,9 +66,6 @@ void sclp_setup(void);
 void sclp_get_loadparm_ascii(char *loadparm);
 int sclp_read(char *str, size_t count);
 
-/* virtio.c */
-bool virtio_is_supported(SubChannelId schid);
-
 /* bootmap.c */
 void zipl_load(void);
 
pc-bios/s390-ccw/virtio-ccw.h new file +24 -0
new file mode 100644
index 0000000000..cdf6a55dc8
@@ -0,0 +1,24 @@
+/*
+ * Virtio definitions for CCW devices
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef VIRTIO_CCW_H
+#define VIRTIO_CCW_H
+
+/* main.c */
+extern SubChannelId blk_schid;
+
+/* virtio-ccw.c */
+int drain_irqs_ccw(SubChannelId schid);
+bool virtio_ccw_is_supported(SubChannelId schid);
+int virtio_ccw_run(VDev *vdev, int vqid, VirtioCmd *cmd);
+long virtio_ccw_notify(SubChannelId schid, int vq_idx, long cookie);
+int virtio_ccw_setup(VDev *vdev);
+int virtio_ccw_reset(VDev *vdev);
+
+#endif
pc-bios/s390-ccw/virtio-scsi.h +1 -1
index c5612e16a2..7a37f8b45a 100644
@@ -69,6 +69,6 @@ static inline bool virtio_scsi_response_ok(const VirtioScsiCmdResp *r)
 
 int virtio_scsi_read_many(VDev *vdev,
                           unsigned long sector, void *load_addr, int sec_num);
-int virtio_scsi_setup_device(SubChannelId schid);
+int virtio_scsi_setup_device(void);
 
 #endif /* VIRTIO_SCSI_H */
pc-bios/s390-ccw/virtio.h +5 -2
index d557a4a90e..e747891a2c 100644
@@ -109,6 +109,7 @@ struct VRing {
 };
 typedef struct VRing VRing;
 
+char *virtio_get_ring_area(void);
 
 /***********************************************
  *               Virtio block                  *
@@ -270,8 +271,10 @@ struct VirtioCmd {
 };
 typedef struct VirtioCmd VirtioCmd;
 
+void vring_init(VRing *vr, VqInfo *info);
+bool virtio_is_supported(VDev *vdev);
 bool vring_notify(VRing *vr);
-int drain_irqs(SubChannelId schid);
+int drain_irqs(VRing *vr);
 void vring_send_buf(VRing *vr, void *p, int len, int flags);
 int vr_poll(VRing *vr);
 int vring_wait_reply(void);
@@ -284,7 +287,7 @@ int virtio_net_init(void *mac_addr);
 void virtio_net_deinit(void);
 
 /* virtio-blkdev.c */
-int virtio_blk_setup_device(SubChannelId schid);
+int virtio_blk_setup_device(void);
 int virtio_read(unsigned long sector, void *load_addr);
 unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2,
                                  void *load_addr);
pc-bios/s390-ccw/main.c +0 -0
index fef192c934..e82d60bbb7 100644

pc-bios/s390-ccw/main.c static int is_dev_possibly_bootable(int dev_no, int sch_no) · line 75

Quoted 4 lines
pc-bios/s390-ccw/main.c +1 -0
@@ -72,6 +72,7 @@ static int is_dev_possibly_bootable(int dev_no, int sch_no)
     Schib schib;
     int r;
 
+    VDev *vdev = virtio_get_device();

Newline would need to be after this, but I'm not sure you need the vdev definition here.

pc-bios/s390-ccw/main.c static int is_dev_possibly_bootable(int dev_no, int sch_no) · line 95

Quoted 9 lines
pc-bios/s390-ccw/main.c +1 -1
     blk_schid.sch_no = sch_no;
     r = stsch_err(blk_schid, &schib);
     if (r == 3 || r == -EIO) {
@@ -91,7 +92,8 @@ static int is_dev_possibly_bootable(int dev_no, int sch_no)
      * Note: we always have to run virtio_is_supported() here to make
      * sure that the vdev.senseid data gets pre-initialized correctly
      */
-    is_virtio = virtio_is_supported(blk_schid);
+    vdev->schid = blk_schid;

You save this in four different places (here, [2a], [2b], and [3])... But maybe only need one? In the ccw path ([3]?) ?

pc-bios/s390-ccw/main.c static int virtio_setup(void) · line 259

Quoted 10 lines
pc-bios/s390-ccw/main.c +2 -1
+    is_virtio = virtio_is_supported(vdev);
 
     /* No specific devno given, just return whether the device is possibly bootable */
     if (dev_no < 0) {
@@ -254,10 +256,12 @@ static int virtio_setup(void)
         puts("Network boot device detected");
         return 0;
     case VIRTIO_ID_BLOCK:
-        ret = virtio_blk_setup_device(blk_schid);
+        vdev->schid = blk_schid;

[2a]

pc-bios/s390-ccw/main.c static int virtio_setup(void) · line 263

Quoted 5 lines
pc-bios/s390-ccw/main.c +2 -1
+        ret = virtio_blk_setup_device();
         break;
     case VIRTIO_ID_SCSI:
-        ret = virtio_scsi_setup_device(blk_schid);
+        vdev->schid = blk_schid;

[2b]

Quoted 46 lines
pc-bios/s390-ccw/main.c +1 -0
+        ret = virtio_scsi_setup_device();
         break;
     default:
         puts("\n! No IPL device available !\n");
pc-bios/s390-ccw/netmain.c +1 -1
index a9521dff41..651cedf6ef 100644
@@ -500,7 +500,7 @@ static bool find_net_dev(Schib *schib, int dev_no)
             continue;
         }
         enable_subchannel(net_schid);
-        if (!virtio_is_supported(net_schid)) {
+        if (!virtio_is_supported(virtio_get_device())) {
             continue;
         }
         if (virtio_get_device_type() != VIRTIO_ID_NET) {
pc-bios/s390-ccw/virtio-blkdev.c +2 -1
index f40a9407c2..87ab9a9513 100644
@@ -12,6 +12,7 @@
 #include "s390-ccw.h"
 #include "virtio.h"
 #include "virtio-scsi.h"
+#include "virtio-ccw.h"
 
 #define VIRTIO_BLK_F_GEOMETRY   (1 << 4)
 #define VIRTIO_BLK_F_BLK_SIZE   (1 << 6)
@@ -42,7 +43,7 @@ static int virtio_blk_read_many(VDev *vdev, unsigned long sector, void *load_add
     /* Now we can tell the host to read */
     vring_wait_reply();
 
-    if (drain_irqs(vr->schid)) {
+    if (drain_irqs(vr)) {
 
         /* Well, whatever status is supposed to contain... */
         status = 1;
     }
pc-bios/s390-ccw/virtio-blkdev.c +1 -1
@@ -229,15 +230,19 @@ uint64_t virtio_get_blocks(void)
     }
 }
 
-int virtio_blk_setup_device(SubChannelId schid)
+int virtio_blk_setup_device(void)
 {

pc-bios/s390-ccw/virtio-blkdev.c uint64_t virtio_get_blocks(void) · line 241

Quoted 10 lines
pc-bios/s390-ccw/virtio-blkdev.c +1 -3
     VDev *vdev = virtio_get_device();
 
     vdev->guest_features[0] = VIRTIO_BLK_F_GEOMETRY | VIRTIO_BLK_F_BLK_SIZE;
-    vdev->schid = schid;
-    virtio_setup_ccw(vdev);
 
     puts("Using virtio-blk.");
 
-    return 0;
+    switch (virtio_get_device()->ipl_type) {

vdev->ipl_type

pc-bios/s390-ccw/virtio-blkdev.c uint64_t virtio_get_blocks(void) · line 244

Quoted 3 lines
pc-bios/s390-ccw/virtio-blkdev.c +3 -0
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return virtio_ccw_setup(vdev);

default:

    return 1;

...instead of...

pc-bios/s390-ccw/virtio-blkdev.c uint64_t virtio_get_blocks(void) · line 247

Quoted 3 lines
pc-bios/s390-ccw/virtio-blkdev.c +3 -0
+    }
+
+    return 1;

...this?

Quoted 49 lines
pc-bios/s390-ccw/virtio-blkdev.c +0 -0
 }
pc-bios/s390-ccw/virtio-ccw.c new file +42 -0
new file mode 100644
index 0000000000..e121826625
@@ -0,0 +1,242 @@
+/*
+ * Virtio functionality for CCW devices
+ *
+ * Copyright (c) 2013 Alexander Graf <[email protected]>
+ * Copyright 2025 IBM Corp. Author(s): Jared Rossi <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or (at
+ * your option) any later version. See the COPYING file in the top-level
+ * directory.
+ */
+
+#include <string.h>
+#include "s390-ccw.h"
+#include "cio.h"
+#include "virtio.h"
+#include "virtio-ccw.h"
+#include "virtio-scsi.h"
+#include "bswap.h"
+#include "helper.h"
+#include "s390-time.h"
+
+/* virtio spec v1.0 para 4.3.3.2 */
+static long kvm_hypercall(unsigned long nr, unsigned long param1,
+                          unsigned long param2, unsigned long param3)
+{
+    register unsigned long r_nr asm("1") = nr;
+    register unsigned long r_param1 asm("2") = param1;
+    register unsigned long r_param2 asm("3") = param2;
+    register unsigned long r_param3 asm("4") = param3;
+    register long retval asm("2");
+
+    asm volatile ("diag %%r2,%%r4,0x500"
+                  : "=d" (retval)
+                  : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
+                  : "memory", "cc");
+
+    return retval;
+}
+
+static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
+{
+    Ccw1 ccw = {};

pc-bios/s390-ccw/virtio-ccw.c line 52

Quoted 10 lines
pc-bios/s390-ccw/virtio-ccw.c new file +10 -0
+
+    ccw.cmd_code = cmd;
+    ccw.cda = (long)ptr;
+    ccw.count = len;
+
+    if (sli) {
+        ccw.flags |= CCW_FLAG_SLI;
+    }
+
+    return do_cio(vdev->schid, vdev->senseid.cu_type, ptr2u32(&ccw), CCW_FMT1);

The previous patch changed vdev->senseid.cu_type to vdev->dev_type, and here you're switching it back. Probably because dev_type is assigned from cu_model, which... Wait does that mean the previous patch would break IPL? (Sorry if I missed that yesterday.)

pc-bios/s390-ccw/virtio-ccw.c line 58

Quoted 6 lines
pc-bios/s390-ccw/virtio-ccw.c new file +6 -0
+}
+
+bool virtio_ccw_is_supported(SubChannelId schid)
+{
+    VDev *vdev = virtio_get_device();
+    vdev->schid = schid;

[3]

Quoted 303 lines
pc-bios/s390-ccw/virtio-ccw.c new file +184 -0
+    memset(&vdev->senseid, 0, sizeof(vdev->senseid));
+
+    /*
+     * Run sense id command.
+     * The size of the senseid data differs between devices (notably,
+     * between virtio devices and dasds), so specify the largest possible
+     * size and suppress the incorrect length indication for smaller sizes.
+     */
+    if (run_ccw(vdev, CCW_CMD_SENSE_ID, &vdev->senseid, sizeof(vdev->senseid),
+                true)) {
+        return false;
+    }
+
+    vdev->dev_type = vdev->senseid.cu_model;
+
+    if (vdev->senseid.cu_type == 0x3832) {
+        switch (vdev->dev_type) {
+        case VIRTIO_ID_BLOCK:
+        case VIRTIO_ID_SCSI:
+        case VIRTIO_ID_NET:
+            return true;
+        default:
+            return false;
+        }
+    }
+    return false;
+}
+
+int drain_irqs_ccw(SubChannelId schid)
+{
+    Irb irb = {};
+    int r = 0;
+
+    while (1) {
+        /* FIXME: make use of TPI, for that enable subchannel and isc */
+        if (tsch(schid, &irb)) {
+            /* Might want to differentiate error codes later on. */
+            if (irb.scsw.cstat) {
+                r = -EIO;
+            } else if (irb.scsw.dstat != 0xc) {
+                r = -EIO;
+            }
+            return r;
+        }
+    }
+}
+
+long virtio_ccw_notify(SubChannelId schid, int vq_idx, long cookie)
+{
+    return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid,
+                         vq_idx, cookie);
+}
+
+int virtio_ccw_run(VDev *vdev, int vqid, VirtioCmd *cmd)
+{
+    VRing *vr = &vdev->vrings[vqid];
+    int i = 0;
+
+    do {
+        vring_send_buf(vr, cmd[i].data, cmd[i].size,
+                       cmd[i].flags | (i ? VRING_HIDDEN_IS_CHAIN : 0));
+    } while (cmd[i++].flags & VRING_DESC_F_NEXT);
+
+    vring_wait_reply();
+    if (drain_irqs(vr)) {
+        return -1;
+    }
+    return 0;
+}
+
+int virtio_ccw_reset(VDev *vdev)
+{
+    return run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
+}
+
+int virtio_ccw_setup(VDev *vdev)
+{
+    int i, cfg_size = 0;
+    uint8_t status;
+    struct VirtioFeatureDesc {
+        uint32_t features;
+        uint8_t index;
+    } __attribute__((packed)) feats;
+
+    if (!virtio_ccw_is_supported(vdev->schid)) {
+        puts("Virtio unsupported for this device ID");
+        return -ENODEV;
+    }
+    /* device ID has been established now */
+
+    vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
+    vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
+
+    virtio_reset(vdev);
+
+    status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
+        puts("Could not write ACKNOWLEDGE status to host");
+        return -EIO;
+    }
+
+    switch (vdev->dev_type) {
+    case VIRTIO_ID_NET:
+        vdev->nr_vqs = 2;
+        vdev->cmd_vr_idx = 0;
+        cfg_size = sizeof(vdev->config.net);
+        break;
+    case VIRTIO_ID_BLOCK:
+        vdev->nr_vqs = 1;
+        vdev->cmd_vr_idx = 0;
+        cfg_size = sizeof(vdev->config.blk);
+        break;
+    case VIRTIO_ID_SCSI:
+        vdev->nr_vqs = 3;
+        vdev->cmd_vr_idx = VR_REQUEST;
+        cfg_size = sizeof(vdev->config.scsi);
+        break;
+    default:
+        puts("Unsupported virtio device");
+        return -ENODEV;
+    }
+
+    status |= VIRTIO_CONFIG_S_DRIVER;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
+        puts("Could not write DRIVER status to host");
+        return -EIO;
+    }
+
+    /* Feature negotiation */
+    for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
+        feats.features = 0;
+        feats.index = i;
+        if (run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false)) {
+            puts("Could not get features bits");
+            return -EIO;
+        }
+
+        vdev->guest_features[i] &= bswap32(feats.features);
+        feats.features = bswap32(vdev->guest_features[i]);
+        if (run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false)) {
+            puts("Could not set features bits");
+            return -EIO;
+        }
+    }
+
+    if (run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false)) {
+        puts("Could not get virtio device configuration");
+        return -EIO;
+    }
+
+    for (i = 0; i < vdev->nr_vqs; i++) {
+        VqInfo info = {
+            .queue = (unsigned long long) virtio_get_ring_area() + (i * VIRTIO_RING_SIZE),
+            .align = KVM_S390_VIRTIO_RING_ALIGN,
+            .index = i,
+            .num = 0,
+        };
+        VqConfig config = {
+            .index = i,
+            .num = 0,
+        };
+
+        if (run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config),
+                false)) {
+            puts("Could not get virtio device VQ config");
+            return -EIO;
+        }
+        info.num = config.num;
+        vring_init(&vdev->vrings[i], &info);
+        vdev->vrings[i].schid = vdev->schid;
+        if (run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false)) {
+            puts("Cannot set VQ info");
+            return -EIO;
+        }
+    }
+
+    status |= VIRTIO_CONFIG_S_DRIVER_OK;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
+        puts("Could not write DRIVER_OK status to host");
+        return -EIO;
+    }
+
+    return 0;
+}
pc-bios/s390-ccw/virtio-net.c +3 -2
index 301445bf97..604f1cf003 100644
@@ -19,6 +19,7 @@
 #include <ethernet.h>
 #include "s390-ccw.h"
 #include "virtio.h"
+#include "virtio-ccw.h"
 #include "s390-time.h"
 #include "helper.h"
 
@@ -54,7 +55,7 @@ int virtio_net_init(void *mac_addr)
     rx_last_idx = 0;
 
     vdev->guest_features[0] = VIRTIO_NET_F_MAC_BIT;
-    virtio_setup_ccw(vdev);
+    virtio_ccw_setup(vdev);
 
     if (!(vdev->guest_features[0] & VIRTIO_NET_F_MAC_BIT)) {
         puts("virtio-net device does not support the MAC address feature");
@@ -88,7 +89,7 @@ int send(int fd, const void *buf, int len, int flags)
     while (!vr_poll(txvq)) {
         yield();
     }
-    if (drain_irqs(txvq->schid)) {
+    if (drain_irqs(txvq)) {
         puts("send: drain irqs failed");
         return -1;
     }
pc-bios/s390-ccw/virtio-scsi.c +3 -3
index 71db75ce7b..6ab0f755f2 100644
@@ -15,6 +15,7 @@
 #include "virtio.h"
 #include "scsi.h"
 #include "virtio-scsi.h"
+#include "virtio-ccw.h"
 #include "s390-time.h"
 #include "helper.h"
 
@@ -476,12 +477,11 @@ static int virtio_scsi_setup(VDev *vdev)
     return 0;
 }
 
-int virtio_scsi_setup_device(SubChannelId schid)
+int virtio_scsi_setup_device(void)
 {
     VDev *vdev = virtio_get_device();
 
-    vdev->schid = schid;
-    virtio_setup_ccw(vdev);
+    virtio_ccw_setup(vdev);
 
     if (vdev->config.scsi.sense_size != VIRTIO_SCSI_SENSE_SIZE) {
         puts("Config: sense size mismatch");
pc-bios/s390-ccw/virtio.c +5 -27
index 0f4f201038..0488b3a07e 100644
@@ -2,6 +2,7 @@
  * Virtio driver bits
  *
  * Copyright (c) 2013 Alexander Graf <[email protected]>
+ * Copyright 2025 IBM Corp. Author(s): Jared Rossi <[email protected]>
  *
  * This work is licensed under the terms of the GNU GPL, version 2 or (at
  * your option) any later version. See the COPYING file in the top-level
@@ -13,6 +14,7 @@
 #include "cio.h"
 #include "virtio.h"
 #include "virtio-scsi.h"
+#include "virtio-ccw.h"
 #include "bswap.h"
 #include "helper.h"
 #include "s390-time.h"
@@ -44,69 +46,38 @@ VirtioDevType virtio_get_device_type(void)
     return vdev.dev_type;
 }
 
-/* virtio spec v1.0 para 4.3.3.2 */
-static long kvm_hypercall(unsigned long nr, unsigned long param1,
-                          unsigned long param2, unsigned long param3)
+char *virtio_get_ring_area(void)
 {
-    register unsigned long r_nr asm("1") = nr;
-    register unsigned long r_param1 asm("2") = param1;
-    register unsigned long r_param2 asm("3") = param2;
-    register unsigned long r_param3 asm("4") = param3;
-    register long retval asm("2");
-
-    asm volatile ("diag %%r2,%%r4,0x500"
-                  : "=d" (retval)
-                  : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
-                  : "memory", "cc");
-
-    return retval;
-}
-
-static long virtio_notify(SubChannelId schid, int vq_idx, long cookie)
-{
-    return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid,
-                         vq_idx, cookie);
+    return ring_area;
 }
 
 /***********************************************
  *             Virtio functions                *
  ***********************************************/
 
-int drain_irqs(SubChannelId schid)
+int drain_irqs(VRing *vr)
 {
-    Irb irb = {};
-    int r = 0;
-
-    while (1) {
-        /* FIXME: make use of TPI, for that enable subchannel and isc */

pc-bios/s390-ccw/virtio.c VirtioDevType virtio_get_device_type(void) · line 60

Quoted 10 lines
pc-bios/s390-ccw/virtio.c +1 -9
-        if (tsch(schid, &irb)) {
-            /* Might want to differentiate error codes later on. */
-            if (irb.scsw.cstat) {
-                r = -EIO;
-            } else if (irb.scsw.dstat != 0xc) {
-                r = -EIO;
-            }
-            return r;
-        }
+    switch (vdev.ipl_type) {

Wait, we're just accessing the global vdev directly here?

pc-bios/s390-ccw/virtio.c VirtioDevType virtio_get_device_type(void) · line 63

Quoted 3 lines
pc-bios/s390-ccw/virtio.c +3 -0
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return drain_irqs_ccw(vr->schid);

In that case, what's the point of vr being passed in? Isn't vr->schid == vdev->schid?

This makes me think that vr->schid is not needed, and it can just be pulled from the vdev.

Quoted 237 lines
pc-bios/s390-ccw/virtio.c +27 -165
+    default:
+        return 0;
     }
 }
 
-static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
+int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
 {
-    Ccw1 ccw = {};
-
-    ccw.cmd_code = cmd;
-    ccw.cda = (long)ptr;
-    ccw.count = len;
-
-    if (sli) {
-        ccw.flags |= CCW_FLAG_SLI;
+    switch (vdev->ipl_type) {
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return virtio_ccw_run(vdev, vqid, cmd);
+    default:
+        return -1;
     }
-
-    return do_cio(vdev->schid, vdev->dev_type, ptr2u32(&ccw), CCW_FMT1);
 }
 
-static void vring_init(VRing *vr, VqInfo *info)
+void vring_init(VRing *vr, VqInfo *info)
 {
     void *p = (void *) info->queue;
 
@@ -134,7 +105,12 @@ static void vring_init(VRing *vr, VqInfo *info)
 
 bool vring_notify(VRing *vr)
 {
-    vr->cookie = virtio_notify(vr->schid, vr->id, vr->cookie);
+    switch (vdev.ipl_type) {
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        vr->cookie = virtio_ccw_notify(vr->schid, vr->id, vr->cookie);
+    }
+
     return vr->cookie >= 0;
 }
 
@@ -200,165 +176,24 @@ int vring_wait_reply(void)
     return 1;
 }
 
-int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
-{
-    VRing *vr = &vdev->vrings[vqid];
-    int i = 0;
-
-    do {
-        vring_send_buf(vr, cmd[i].data, cmd[i].size,
-                       cmd[i].flags | (i ? VRING_HIDDEN_IS_CHAIN : 0));
-    } while (cmd[i++].flags & VRING_DESC_F_NEXT);
-
-    vring_wait_reply();
-    if (drain_irqs(vr->schid)) {
-        return -1;
-    }
-    return 0;
-}
-
 int virtio_reset(VDev *vdev)
 {
-    return run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
-}
-
-int virtio_setup_ccw(VDev *vdev)
-{
-    int i, cfg_size = 0;
-    uint8_t status;
-    struct VirtioFeatureDesc {
-        uint32_t features;
-        uint8_t index;
-    } __attribute__((packed)) feats;
-
-    if (!virtio_is_supported(vdev->schid)) {
-        puts("Virtio unsupported for this device ID");
-        return -ENODEV;
-    }
-    /* device ID has been established now */
-
-    vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
-    vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
-
-    virtio_reset(vdev);
-
-    status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
-    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
-        puts("Could not write ACKNOWLEDGE status to host");
-        return -EIO;
-    }
-
-    switch (vdev->dev_type) {
-    case VIRTIO_ID_NET:
-        vdev->nr_vqs = 2;
-        vdev->cmd_vr_idx = 0;
-        cfg_size = sizeof(vdev->config.net);
-        break;
-    case VIRTIO_ID_BLOCK:
-        vdev->nr_vqs = 1;
-        vdev->cmd_vr_idx = 0;
-        cfg_size = sizeof(vdev->config.blk);
-        break;
-    case VIRTIO_ID_SCSI:
-        vdev->nr_vqs = 3;
-        vdev->cmd_vr_idx = VR_REQUEST;
-        cfg_size = sizeof(vdev->config.scsi);
-        break;
+    switch (vdev->ipl_type) {
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return virtio_ccw_reset(vdev);
     default:
-        puts("Unsupported virtio device");
-        return -ENODEV;
-    }
-
-    status |= VIRTIO_CONFIG_S_DRIVER;
-    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
-        puts("Could not write DRIVER status to host");
-        return -EIO;
-    }
-
-    /* Feature negotiation */
-    for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
-        feats.features = 0;
-        feats.index = i;
-        if (run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false)) {
-            puts("Could not get features bits");
-            return -EIO;
-        }
-
-        vdev->guest_features[i] &= bswap32(feats.features);
-        feats.features = bswap32(vdev->guest_features[i]);
-        if (run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false)) {
-            puts("Could not set features bits");
-            return -EIO;
-        }
-    }
-
-    if (run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false)) {
-        puts("Could not get virtio device configuration");
-        return -EIO;
-    }
-
-    for (i = 0; i < vdev->nr_vqs; i++) {
-        VqInfo info = {
-            .queue = (unsigned long long) ring_area + (i * VIRTIO_RING_SIZE),
-            .align = KVM_S390_VIRTIO_RING_ALIGN,
-            .index = i,
-            .num = 0,
-        };
-        VqConfig config = {
-            .index = i,
-            .num = 0,
-        };
-
-        if (run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config),
-                false)) {
-            puts("Could not get virtio device VQ config");
-            return -EIO;
-        }
-        info.num = config.num;
-        vring_init(&vdev->vrings[i], &info);
-        vdev->vrings[i].schid = vdev->schid;
-        if (run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false)) {
-            puts("Cannot set VQ info");
-            return -EIO;
-        }
-    }
-
-    status |= VIRTIO_CONFIG_S_DRIVER_OK;
-    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
-        puts("Could not write DRIVER_OK status to host");
-        return -EIO;
+        return -1;
     }
-
-    return 0;
 }
 
-bool virtio_is_supported(SubChannelId schid)
+bool virtio_is_supported(VDev *vdev)
 {
-    vdev.schid = schid;
-    memset(&vdev.senseid, 0, sizeof(vdev.senseid));
-
-    /*
-     * Run sense id command.
-     * The size of the senseid data differs between devices (notably,
-     * between virtio devices and dasds), so specify the largest possible
-     * size and suppress the incorrect length indication for smaller sizes.
-     */
-    if (run_ccw(&vdev, CCW_CMD_SENSE_ID, &vdev.senseid, sizeof(vdev.senseid),
-                true)) {
+    switch (vdev->ipl_type) {
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return virtio_ccw_is_supported(vdev->schid);
+    default:
         return false;
     }
-
-    vdev.dev_type = vdev.senseid.cu_model;
-
-    if (vdev.senseid.cu_type == 0x3832) {
-        switch (vdev.dev_type) {
-        case VIRTIO_ID_BLOCK:
-        case VIRTIO_ID_SCSI:
-        case VIRTIO_ID_NET:
-            return true;
-        default:
-            return false;
-        }
-    }
-    return false;
 }
pc-bios/s390-ccw/Makefile +2 -1
index a0f24c94a8..259cff09db 100644
@@ -34,7 +34,8 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d
 .PHONY : all clean build-all distclean
 
 OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \
-	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o
+	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \
+	  virtio-ccw.o
 
 SLOF_DIR := $(SRC_PATH)/../../roms/SLOF
 
#12 lore raw

Re: [PATCH 03/10] pc-bios/s390-ccw: Split virtio-ccw and generic virtio

Quoted 121 lines · Eric Farman
Quoted 120 lines · On Wed, 2025-12-10 at 15:54 -0500, [email protected] wrote:

From: Jared Rossi <[email protected]>

Separate the CCW specific virtio routines and create generic wrappers for easier reuse of existing virtio functions with non-CCW devices.

---
  pc-bios/s390-ccw/s390-ccw.h      |   3 -
  pc-bios/s390-ccw/virtio-ccw.h    |  24 +++
  pc-bios/s390-ccw/virtio-scsi.h   |   2 +-
  pc-bios/s390-ccw/virtio.h        |   7 +-
  pc-bios/s390-ccw/main.c          |  10 +-
  pc-bios/s390-ccw/netmain.c       |   2 +-
  pc-bios/s390-ccw/virtio-blkdev.c |  15 +-
  pc-bios/s390-ccw/virtio-ccw.c    | 242 +++++++++++++++++++++++++++++++
  pc-bios/s390-ccw/virtio-net.c    |   5 +-
  pc-bios/s390-ccw/virtio-scsi.c   |   6 +-
  pc-bios/s390-ccw/virtio.c        | 237 +++++-------------------------
  pc-bios/s390-ccw/Makefile        |   3 +-
  12 files changed, 334 insertions(+), 222 deletions(-)

create mode 100644 pc-bios/s390-ccw/virtio-ccw.h create mode 100644 pc-bios/s390-ccw/virtio-ccw.c

pc-bios/s390-ccw/s390-ccw.h +0 -3
index 47ea66bd4d..ccd68ff0a4 100644
@@ -66,9 +66,6 @@ void sclp_setup(void);
  void sclp_get_loadparm_ascii(char *loadparm);
  int sclp_read(char *str, size_t count);
 
-/* virtio.c */
-bool virtio_is_supported(SubChannelId schid);
-
  /* bootmap.c */
  void zipl_load(void);
 
pc-bios/s390-ccw/virtio-ccw.h new file +24 -0
new file mode 100644
index 0000000000..cdf6a55dc8
@@ -0,0 +1,24 @@
+/*
+ * Virtio definitions for CCW devices
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef VIRTIO_CCW_H
+#define VIRTIO_CCW_H
+
+/* main.c */
+extern SubChannelId blk_schid;
+
+/* virtio-ccw.c */
+int drain_irqs_ccw(SubChannelId schid);
+bool virtio_ccw_is_supported(SubChannelId schid);
+int virtio_ccw_run(VDev *vdev, int vqid, VirtioCmd *cmd);
+long virtio_ccw_notify(SubChannelId schid, int vq_idx, long cookie);
+int virtio_ccw_setup(VDev *vdev);
+int virtio_ccw_reset(VDev *vdev);
+
+#endif
pc-bios/s390-ccw/virtio-scsi.h +1 -1
index c5612e16a2..7a37f8b45a 100644
@@ -69,6 +69,6 @@ static inline bool virtio_scsi_response_ok(const VirtioScsiCmdResp *r)
 
  int virtio_scsi_read_many(VDev *vdev,
                            unsigned long sector, void *load_addr, int sec_num);
-int virtio_scsi_setup_device(SubChannelId schid);
+int virtio_scsi_setup_device(void);
 
  #endif /* VIRTIO_SCSI_H */
pc-bios/s390-ccw/virtio.h +5 -2
index d557a4a90e..e747891a2c 100644
@@ -109,6 +109,7 @@ struct VRing {
  };
  typedef struct VRing VRing;
 
+char *virtio_get_ring_area(void);
 
  /***********************************************
   *               Virtio block                  *
@@ -270,8 +271,10 @@ struct VirtioCmd {
  };
  typedef struct VirtioCmd VirtioCmd;
 
+void vring_init(VRing *vr, VqInfo *info);
+bool virtio_is_supported(VDev *vdev);
  bool vring_notify(VRing *vr);
-int drain_irqs(SubChannelId schid);
+int drain_irqs(VRing *vr);
  void vring_send_buf(VRing *vr, void *p, int len, int flags);
  int vr_poll(VRing *vr);
  int vring_wait_reply(void);
@@ -284,7 +287,7 @@ int virtio_net_init(void *mac_addr);
  void virtio_net_deinit(void);
 
  /* virtio-blkdev.c */
-int virtio_blk_setup_device(SubChannelId schid);
+int virtio_blk_setup_device(void);
  int virtio_read(unsigned long sector, void *load_addr);
  unsigned long virtio_load_direct(unsigned long rec_list1, unsigned long rec_list2,
                                   void *load_addr);
pc-bios/s390-ccw/main.c +0 -0
index fef192c934..e82d60bbb7 100644
@@ -72,6 +72,7 @@ static int is_dev_possibly_bootable(int dev_no, int sch_no)
      Schib schib;
      int r;
 
Quoted 2 lines

pc-bios/s390-ccw/main.c static int is_dev_possibly_bootable(int dev_no, int sch_no) · line 75

Quoted 1 line
pc-bios/s390-ccw/main.c +1 -0
+    VDev *vdev = virtio_get_device();

Newline would need to be after this, but I'm not sure you need the vdev definition here.

At a minimum I'll fix the formatting.  It might be unneeded if the schid assignment/access is simplified as you suggest, so I'll see if I can clean that up too.

Quoted 12 lines

pc-bios/s390-ccw/main.c static int is_dev_possibly_bootable(int dev_no, int sch_no) · line 95

Quoted 10 lines
pc-bios/s390-ccw/main.c +1 -1
      blk_schid.sch_no = sch_no;
      r = stsch_err(blk_schid, &schib);
      if (r == 3 || r == -EIO) {
@@ -91,7 +92,8 @@ static int is_dev_possibly_bootable(int dev_no, int sch_no)
       * Note: we always have to run virtio_is_supported() here to make
       * sure that the vdev.senseid data gets pre-initialized correctly
       */
-    is_virtio = virtio_is_supported(blk_schid);
+    vdev->schid = blk_schid;

You save this in four different places (here, [2a], [2b], and [3])... But maybe only need one? In the ccw path ([3]?) ?

Yes, it shouldn't need to be saved in all these places.  I'll consolidate it.

Quoted 72 lines

pc-bios/s390-ccw/main.c static int virtio_setup(void) · line 259

Quoted 11 lines
pc-bios/s390-ccw/main.c +2 -1
+    is_virtio = virtio_is_supported(vdev);
 
      /* No specific devno given, just return whether the device is possibly bootable */
      if (dev_no < 0) {
@@ -254,10 +256,12 @@ static int virtio_setup(void)
          puts("Network boot device detected");
          return 0;
      case VIRTIO_ID_BLOCK:
-        ret = virtio_blk_setup_device(blk_schid);
+        vdev->schid = blk_schid;

[2a]

pc-bios/s390-ccw/main.c static int virtio_setup(void) · line 263

Quoted 5 lines
pc-bios/s390-ccw/main.c +2 -1
+        ret = virtio_blk_setup_device();
          break;
      case VIRTIO_ID_SCSI:
-        ret = virtio_scsi_setup_device(blk_schid);
+        vdev->schid = blk_schid;

[2b]

Quoted 52 lines
pc-bios/s390-ccw/main.c +1 -0
+        ret = virtio_scsi_setup_device();
          break;
      default:
          puts("\n! No IPL device available !\n");
pc-bios/s390-ccw/netmain.c +1 -1
index a9521dff41..651cedf6ef 100644
@@ -500,7 +500,7 @@ static bool find_net_dev(Schib *schib, int dev_no)
              continue;
          }
          enable_subchannel(net_schid);
-        if (!virtio_is_supported(net_schid)) {
+        if (!virtio_is_supported(virtio_get_device())) {
              continue;
          }
          if (virtio_get_device_type() != VIRTIO_ID_NET) {
pc-bios/s390-ccw/virtio-blkdev.c +2 -1
index f40a9407c2..87ab9a9513 100644
@@ -12,6 +12,7 @@
  #include "s390-ccw.h"
  #include "virtio.h"
  #include "virtio-scsi.h"
+#include "virtio-ccw.h"
 
  #define VIRTIO_BLK_F_GEOMETRY   (1 << 4)
  #define VIRTIO_BLK_F_BLK_SIZE   (1 << 6)
@@ -42,7 +43,7 @@ static int virtio_blk_read_many(VDev *vdev, unsigned long sector, void *load_add
      /* Now we can tell the host to read */
      vring_wait_reply();
 
-    if (drain_irqs(vr->schid)) {
+    if (drain_irqs(vr)) {
 
          /* Well, whatever status is supposed to contain... */
          status = 1;
      }
pc-bios/s390-ccw/virtio-blkdev.c +1 -3
@@ -229,15 +230,19 @@ uint64_t virtio_get_blocks(void)
      }
  }
 
-int virtio_blk_setup_device(SubChannelId schid)
+int virtio_blk_setup_device(void)
  {
      VDev *vdev = virtio_get_device();
 
      vdev->guest_features[0] = VIRTIO_BLK_F_GEOMETRY | VIRTIO_BLK_F_BLK_SIZE;
-    vdev->schid = schid;
-    virtio_setup_ccw(vdev);
 
Quoted 5 lines

pc-bios/s390-ccw/virtio-blkdev.c uint64_t virtio_get_blocks(void) · line 241

Quoted 4 lines
pc-bios/s390-ccw/virtio-blkdev.c +1 -1
      puts("Using virtio-blk.");
 
-    return 0;
+    switch (virtio_get_device()->ipl_type) {

vdev->ipl_type

Good catch, will fix.

Quoted 5 lines

pc-bios/s390-ccw/virtio-blkdev.c uint64_t virtio_get_blocks(void) · line 244

Quoted 4 lines
pc-bios/s390-ccw/virtio-blkdev.c +3 -0
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return virtio_ccw_setup(vdev);

default:

Quoted 8 lines
     return 1;

...instead of...

pc-bios/s390-ccw/virtio-blkdev.c uint64_t virtio_get_blocks(void) · line 247

Quoted 3 lines
pc-bios/s390-ccw/virtio-blkdev.c +3 -0
+    }
+
+    return 1;

...this?

Makes sense.

Quoted 55 lines
Quoted 55 lines
pc-bios/s390-ccw/virtio-blkdev.c +0 -0
  }
pc-bios/s390-ccw/virtio-ccw.c new file +47 -0
new file mode 100644
index 0000000000..e121826625
@@ -0,0 +1,242 @@
+/*
+ * Virtio functionality for CCW devices
+ *
+ * Copyright (c) 2013 Alexander Graf <[email protected]>
+ * Copyright 2025 IBM Corp. Author(s): Jared Rossi <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or (at
+ * your option) any later version. See the COPYING file in the top-level
+ * directory.
+ */
+
+#include <string.h>
+#include "s390-ccw.h"
+#include "cio.h"
+#include "virtio.h"
+#include "virtio-ccw.h"
+#include "virtio-scsi.h"
+#include "bswap.h"
+#include "helper.h"
+#include "s390-time.h"
+
+/* virtio spec v1.0 para 4.3.3.2 */
+static long kvm_hypercall(unsigned long nr, unsigned long param1,
+                          unsigned long param2, unsigned long param3)
+{
+    register unsigned long r_nr asm("1") = nr;
+    register unsigned long r_param1 asm("2") = param1;
+    register unsigned long r_param2 asm("3") = param2;
+    register unsigned long r_param3 asm("4") = param3;
+    register long retval asm("2");
+
+    asm volatile ("diag %%r2,%%r4,0x500"
+                  : "=d" (retval)
+                  : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
+                  : "memory", "cc");
+
+    return retval;
+}
+
+static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
+{
+    Ccw1 ccw = {};
+
+    ccw.cmd_code = cmd;
+    ccw.cda = (long)ptr;
+    ccw.count = len;
+
Quoted 8 lines

pc-bios/s390-ccw/virtio-ccw.c line 52

Quoted 5 lines
pc-bios/s390-ccw/virtio-ccw.c new file +5 -0
+    if (sli) {
+        ccw.flags |= CCW_FLAG_SLI;
+    }
+
+    return do_cio(vdev->schid, vdev->senseid.cu_type, ptr2u32(&ccw), CCW_FMT1);

The previous patch changed vdev->senseid.cu_type to vdev->dev_type, and here you're switching it back. Probably because dev_type is assigned from cu_model, which... Wait does that mean the previous patch would break IPL? (Sorry if I missed that yesterday.)

The previous patch didn't break IPL when I ran my smoke test on it, but now I'm second guessing myself...

I believe using vdev->senseid.cu_type here is correct, so the other patch might need to be fixed.  I'll look over it.

Quoted 324 lines

pc-bios/s390-ccw/virtio-ccw.c line 58

Quoted 7 lines
pc-bios/s390-ccw/virtio-ccw.c new file +6 -0
+}
+
+bool virtio_ccw_is_supported(SubChannelId schid)
+{
+    VDev *vdev = virtio_get_device();
+    vdev->schid = schid;

[3]

Quoted 303 lines
pc-bios/s390-ccw/virtio-ccw.c new file +184 -0
+    memset(&vdev->senseid, 0, sizeof(vdev->senseid));
+
+    /*
+     * Run sense id command.
+     * The size of the senseid data differs between devices (notably,
+     * between virtio devices and dasds), so specify the largest possible
+     * size and suppress the incorrect length indication for smaller sizes.
+     */
+    if (run_ccw(vdev, CCW_CMD_SENSE_ID, &vdev->senseid, sizeof(vdev->senseid),
+                true)) {
+        return false;
+    }
+
+    vdev->dev_type = vdev->senseid.cu_model;
+
+    if (vdev->senseid.cu_type == 0x3832) {
+        switch (vdev->dev_type) {
+        case VIRTIO_ID_BLOCK:
+        case VIRTIO_ID_SCSI:
+        case VIRTIO_ID_NET:
+            return true;
+        default:
+            return false;
+        }
+    }
+    return false;
+}
+
+int drain_irqs_ccw(SubChannelId schid)
+{
+    Irb irb = {};
+    int r = 0;
+
+    while (1) {
+        /* FIXME: make use of TPI, for that enable subchannel and isc */
+        if (tsch(schid, &irb)) {
+            /* Might want to differentiate error codes later on. */
+            if (irb.scsw.cstat) {
+                r = -EIO;
+            } else if (irb.scsw.dstat != 0xc) {
+                r = -EIO;
+            }
+            return r;
+        }
+    }
+}
+
+long virtio_ccw_notify(SubChannelId schid, int vq_idx, long cookie)
+{
+    return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid,
+                         vq_idx, cookie);
+}
+
+int virtio_ccw_run(VDev *vdev, int vqid, VirtioCmd *cmd)
+{
+    VRing *vr = &vdev->vrings[vqid];
+    int i = 0;
+
+    do {
+        vring_send_buf(vr, cmd[i].data, cmd[i].size,
+                       cmd[i].flags | (i ? VRING_HIDDEN_IS_CHAIN : 0));
+    } while (cmd[i++].flags & VRING_DESC_F_NEXT);
+
+    vring_wait_reply();
+    if (drain_irqs(vr)) {
+        return -1;
+    }
+    return 0;
+}
+
+int virtio_ccw_reset(VDev *vdev)
+{
+    return run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
+}
+
+int virtio_ccw_setup(VDev *vdev)
+{
+    int i, cfg_size = 0;
+    uint8_t status;
+    struct VirtioFeatureDesc {
+        uint32_t features;
+        uint8_t index;
+    } __attribute__((packed)) feats;
+
+    if (!virtio_ccw_is_supported(vdev->schid)) {
+        puts("Virtio unsupported for this device ID");
+        return -ENODEV;
+    }
+    /* device ID has been established now */
+
+    vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
+    vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
+
+    virtio_reset(vdev);
+
+    status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
+        puts("Could not write ACKNOWLEDGE status to host");
+        return -EIO;
+    }
+
+    switch (vdev->dev_type) {
+    case VIRTIO_ID_NET:
+        vdev->nr_vqs = 2;
+        vdev->cmd_vr_idx = 0;
+        cfg_size = sizeof(vdev->config.net);
+        break;
+    case VIRTIO_ID_BLOCK:
+        vdev->nr_vqs = 1;
+        vdev->cmd_vr_idx = 0;
+        cfg_size = sizeof(vdev->config.blk);
+        break;
+    case VIRTIO_ID_SCSI:
+        vdev->nr_vqs = 3;
+        vdev->cmd_vr_idx = VR_REQUEST;
+        cfg_size = sizeof(vdev->config.scsi);
+        break;
+    default:
+        puts("Unsupported virtio device");
+        return -ENODEV;
+    }
+
+    status |= VIRTIO_CONFIG_S_DRIVER;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
+        puts("Could not write DRIVER status to host");
+        return -EIO;
+    }
+
+    /* Feature negotiation */
+    for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
+        feats.features = 0;
+        feats.index = i;
+        if (run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false)) {
+            puts("Could not get features bits");
+            return -EIO;
+        }
+
+        vdev->guest_features[i] &= bswap32(feats.features);
+        feats.features = bswap32(vdev->guest_features[i]);
+        if (run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false)) {
+            puts("Could not set features bits");
+            return -EIO;
+        }
+    }
+
+    if (run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false)) {
+        puts("Could not get virtio device configuration");
+        return -EIO;
+    }
+
+    for (i = 0; i < vdev->nr_vqs; i++) {
+        VqInfo info = {
+            .queue = (unsigned long long) virtio_get_ring_area() + (i * VIRTIO_RING_SIZE),
+            .align = KVM_S390_VIRTIO_RING_ALIGN,
+            .index = i,
+            .num = 0,
+        };
+        VqConfig config = {
+            .index = i,
+            .num = 0,
+        };
+
+        if (run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config),
+                false)) {
+            puts("Could not get virtio device VQ config");
+            return -EIO;
+        }
+        info.num = config.num;
+        vring_init(&vdev->vrings[i], &info);
+        vdev->vrings[i].schid = vdev->schid;
+        if (run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false)) {
+            puts("Cannot set VQ info");
+            return -EIO;
+        }
+    }
+
+    status |= VIRTIO_CONFIG_S_DRIVER_OK;
+    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
+        puts("Could not write DRIVER_OK status to host");
+        return -EIO;
+    }
+
+    return 0;
+}
pc-bios/s390-ccw/virtio-net.c +3 -2
index 301445bf97..604f1cf003 100644
@@ -19,6 +19,7 @@
  #include <ethernet.h>
  #include "s390-ccw.h"
  #include "virtio.h"
+#include "virtio-ccw.h"
  #include "s390-time.h"
  #include "helper.h"
 
@@ -54,7 +55,7 @@ int virtio_net_init(void *mac_addr)
      rx_last_idx = 0;
 
      vdev->guest_features[0] = VIRTIO_NET_F_MAC_BIT;
-    virtio_setup_ccw(vdev);
+    virtio_ccw_setup(vdev);
 
      if (!(vdev->guest_features[0] & VIRTIO_NET_F_MAC_BIT)) {
          puts("virtio-net device does not support the MAC address feature");
@@ -88,7 +89,7 @@ int send(int fd, const void *buf, int len, int flags)
      while (!vr_poll(txvq)) {
          yield();
      }
-    if (drain_irqs(txvq->schid)) {
+    if (drain_irqs(txvq)) {
          puts("send: drain irqs failed");
          return -1;
      }
pc-bios/s390-ccw/virtio-scsi.c +3 -3
index 71db75ce7b..6ab0f755f2 100644
@@ -15,6 +15,7 @@
  #include "virtio.h"
  #include "scsi.h"
  #include "virtio-scsi.h"
+#include "virtio-ccw.h"
  #include "s390-time.h"
  #include "helper.h"
 
@@ -476,12 +477,11 @@ static int virtio_scsi_setup(VDev *vdev)
      return 0;
  }
 
-int virtio_scsi_setup_device(SubChannelId schid)
+int virtio_scsi_setup_device(void)
  {
      VDev *vdev = virtio_get_device();
 
-    vdev->schid = schid;
-    virtio_setup_ccw(vdev);
+    virtio_ccw_setup(vdev);
 
      if (vdev->config.scsi.sense_size != VIRTIO_SCSI_SENSE_SIZE) {
          puts("Config: sense size mismatch");
pc-bios/s390-ccw/virtio.c +5 -27
index 0f4f201038..0488b3a07e 100644
@@ -2,6 +2,7 @@
   * Virtio driver bits
   *
   * Copyright (c) 2013 Alexander Graf <[email protected]>
+ * Copyright 2025 IBM Corp. Author(s): Jared Rossi <[email protected]>
   *
   * This work is licensed under the terms of the GNU GPL, version 2 or (at
   * your option) any later version. See the COPYING file in the top-level
@@ -13,6 +14,7 @@
  #include "cio.h"
  #include "virtio.h"
  #include "virtio-scsi.h"
+#include "virtio-ccw.h"
  #include "bswap.h"
  #include "helper.h"
  #include "s390-time.h"
@@ -44,69 +46,38 @@ VirtioDevType virtio_get_device_type(void)
      return vdev.dev_type;
  }
 
-/* virtio spec v1.0 para 4.3.3.2 */
-static long kvm_hypercall(unsigned long nr, unsigned long param1,
-                          unsigned long param2, unsigned long param3)
+char *virtio_get_ring_area(void)
  {
-    register unsigned long r_nr asm("1") = nr;
-    register unsigned long r_param1 asm("2") = param1;
-    register unsigned long r_param2 asm("3") = param2;
-    register unsigned long r_param3 asm("4") = param3;
-    register long retval asm("2");
-
-    asm volatile ("diag %%r2,%%r4,0x500"
-                  : "=d" (retval)
-                  : "d" (r_nr), "0" (r_param1), "r"(r_param2), "d"(r_param3)
-                  : "memory", "cc");
-
-    return retval;
-}
-
-static long virtio_notify(SubChannelId schid, int vq_idx, long cookie)
-{
-    return kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32 *)&schid,
-                         vq_idx, cookie);
+    return ring_area;
  }
 
  /***********************************************
   *             Virtio functions                *
   ***********************************************/
 
-int drain_irqs(SubChannelId schid)
+int drain_irqs(VRing *vr)
  {
-    Irb irb = {};
-    int r = 0;
-
-    while (1) {
-        /* FIXME: make use of TPI, for that enable subchannel and isc */

pc-bios/s390-ccw/virtio.c VirtioDevType virtio_get_device_type(void) · line 60

Quoted 10 lines
pc-bios/s390-ccw/virtio.c +1 -9
-        if (tsch(schid, &irb)) {
-            /* Might want to differentiate error codes later on. */
-            if (irb.scsw.cstat) {
-                r = -EIO;
-            } else if (irb.scsw.dstat != 0xc) {
-                r = -EIO;
-            }
-            return r;
-        }
+    switch (vdev.ipl_type) {

Wait, we're just accessing the global vdev directly here?

Quoted 6 lines

pc-bios/s390-ccw/virtio.c VirtioDevType virtio_get_device_type(void) · line 63

Quoted 3 lines
pc-bios/s390-ccw/virtio.c +3 -0
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return drain_irqs_ccw(vr->schid);

In that case, what's the point of vr being passed in? Isn't vr->schid == vdev->schid?

This makes me think that vr->schid is not needed, and it can just be pulled from the vdev.

Hmm... I suspect you are correct the more I think about it.

Quoted 238 lines
Quoted 238 lines
pc-bios/s390-ccw/virtio.c +27 -165
+    default:
+        return 0;
      }
  }
 
-static int run_ccw(VDev *vdev, int cmd, void *ptr, int len, bool sli)
+int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
  {
-    Ccw1 ccw = {};
-
-    ccw.cmd_code = cmd;
-    ccw.cda = (long)ptr;
-    ccw.count = len;
-
-    if (sli) {
-        ccw.flags |= CCW_FLAG_SLI;
+    switch (vdev->ipl_type) {
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return virtio_ccw_run(vdev, vqid, cmd);
+    default:
+        return -1;
      }
-
-    return do_cio(vdev->schid, vdev->dev_type, ptr2u32(&ccw), CCW_FMT1);
  }
 
-static void vring_init(VRing *vr, VqInfo *info)
+void vring_init(VRing *vr, VqInfo *info)
  {
      void *p = (void *) info->queue;
 
@@ -134,7 +105,12 @@ static void vring_init(VRing *vr, VqInfo *info)
 
  bool vring_notify(VRing *vr)
  {
-    vr->cookie = virtio_notify(vr->schid, vr->id, vr->cookie);
+    switch (vdev.ipl_type) {
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        vr->cookie = virtio_ccw_notify(vr->schid, vr->id, vr->cookie);
+    }
+
      return vr->cookie >= 0;
  }
 
@@ -200,165 +176,24 @@ int vring_wait_reply(void)
      return 1;
  }
 
-int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd)
-{
-    VRing *vr = &vdev->vrings[vqid];
-    int i = 0;
-
-    do {
-        vring_send_buf(vr, cmd[i].data, cmd[i].size,
-                       cmd[i].flags | (i ? VRING_HIDDEN_IS_CHAIN : 0));
-    } while (cmd[i++].flags & VRING_DESC_F_NEXT);
-
-    vring_wait_reply();
-    if (drain_irqs(vr->schid)) {
-        return -1;
-    }
-    return 0;
-}
-
  int virtio_reset(VDev *vdev)
  {
-    return run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0, false);
-}
-
-int virtio_setup_ccw(VDev *vdev)
-{
-    int i, cfg_size = 0;
-    uint8_t status;
-    struct VirtioFeatureDesc {
-        uint32_t features;
-        uint8_t index;
-    } __attribute__((packed)) feats;
-
-    if (!virtio_is_supported(vdev->schid)) {
-        puts("Virtio unsupported for this device ID");
-        return -ENODEV;
-    }
-    /* device ID has been established now */
-
-    vdev->config.blk.blk_size = 0; /* mark "illegal" - setup started... */
-    vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
-
-    virtio_reset(vdev);
-
-    status = VIRTIO_CONFIG_S_ACKNOWLEDGE;
-    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
-        puts("Could not write ACKNOWLEDGE status to host");
-        return -EIO;
-    }
-
-    switch (vdev->dev_type) {
-    case VIRTIO_ID_NET:
-        vdev->nr_vqs = 2;
-        vdev->cmd_vr_idx = 0;
-        cfg_size = sizeof(vdev->config.net);
-        break;
-    case VIRTIO_ID_BLOCK:
-        vdev->nr_vqs = 1;
-        vdev->cmd_vr_idx = 0;
-        cfg_size = sizeof(vdev->config.blk);
-        break;
-    case VIRTIO_ID_SCSI:
-        vdev->nr_vqs = 3;
-        vdev->cmd_vr_idx = VR_REQUEST;
-        cfg_size = sizeof(vdev->config.scsi);
-        break;
+    switch (vdev->ipl_type) {
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return virtio_ccw_reset(vdev);
      default:
-        puts("Unsupported virtio device");
-        return -ENODEV;
-    }
-
-    status |= VIRTIO_CONFIG_S_DRIVER;
-    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
-        puts("Could not write DRIVER status to host");
-        return -EIO;
-    }
-
-    /* Feature negotiation */
-    for (i = 0; i < ARRAY_SIZE(vdev->guest_features); i++) {
-        feats.features = 0;
-        feats.index = i;
-        if (run_ccw(vdev, CCW_CMD_READ_FEAT, &feats, sizeof(feats), false)) {
-            puts("Could not get features bits");
-            return -EIO;
-        }
-
-        vdev->guest_features[i] &= bswap32(feats.features);
-        feats.features = bswap32(vdev->guest_features[i]);
-        if (run_ccw(vdev, CCW_CMD_WRITE_FEAT, &feats, sizeof(feats), false)) {
-            puts("Could not set features bits");
-            return -EIO;
-        }
-    }
-
-    if (run_ccw(vdev, CCW_CMD_READ_CONF, &vdev->config, cfg_size, false)) {
-        puts("Could not get virtio device configuration");
-        return -EIO;
-    }
-
-    for (i = 0; i < vdev->nr_vqs; i++) {
-        VqInfo info = {
-            .queue = (unsigned long long) ring_area + (i * VIRTIO_RING_SIZE),
-            .align = KVM_S390_VIRTIO_RING_ALIGN,
-            .index = i,
-            .num = 0,
-        };
-        VqConfig config = {
-            .index = i,
-            .num = 0,
-        };
-
-        if (run_ccw(vdev, CCW_CMD_READ_VQ_CONF, &config, sizeof(config),
-                false)) {
-            puts("Could not get virtio device VQ config");
-            return -EIO;
-        }
-        info.num = config.num;
-        vring_init(&vdev->vrings[i], &info);
-        vdev->vrings[i].schid = vdev->schid;
-        if (run_ccw(vdev, CCW_CMD_SET_VQ, &info, sizeof(info), false)) {
-            puts("Cannot set VQ info");
-            return -EIO;
-        }
-    }
-
-    status |= VIRTIO_CONFIG_S_DRIVER_OK;
-    if (run_ccw(vdev, CCW_CMD_WRITE_STATUS, &status, sizeof(status), false)) {
-        puts("Could not write DRIVER_OK status to host");
-        return -EIO;
+        return -1;
      }
-
-    return 0;
  }
 
-bool virtio_is_supported(SubChannelId schid)
+bool virtio_is_supported(VDev *vdev)
  {
-    vdev.schid = schid;
-    memset(&vdev.senseid, 0, sizeof(vdev.senseid));
-
-    /*
-     * Run sense id command.
-     * The size of the senseid data differs between devices (notably,
-     * between virtio devices and dasds), so specify the largest possible
-     * size and suppress the incorrect length indication for smaller sizes.
-     */
-    if (run_ccw(&vdev, CCW_CMD_SENSE_ID, &vdev.senseid, sizeof(vdev.senseid),
-                true)) {
+    switch (vdev->ipl_type) {
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        return virtio_ccw_is_supported(vdev->schid);
+    default:
          return false;
      }
-
-    vdev.dev_type = vdev.senseid.cu_model;
-
-    if (vdev.senseid.cu_type == 0x3832) {
-        switch (vdev.dev_type) {
-        case VIRTIO_ID_BLOCK:
-        case VIRTIO_ID_SCSI:
-        case VIRTIO_ID_NET:
-            return true;
-        default:
-            return false;
-        }
-    }
-    return false;
  }
pc-bios/s390-ccw/Makefile +2 -1
index a0f24c94a8..259cff09db 100644
@@ -34,7 +34,8 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d
  .PHONY : all clean build-all distclean
 
  OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \
-	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o
+	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \
+	  virtio-ccw.o
 
  SLOF_DIR := $(SRC_PATH)/../../roms/SLOF
 
#13 lore raw

PATCH v2 4/10 include/hw/s390x: Move CLP definitions for easier BIOS access

In reply to [email protected]

From: Jared Rossi <[email protected]>

Move the s390-pci-clp definitions into the "ipl" sub-directory, which is visible to the s390-bios. This allows the bios to reuse the architected definitions and prevents code duplication.

---
 include/hw/s390x/{ => ipl}/s390-pci-clp.h | 0
 include/hw/s390x/s390-pci-bus.h           | 2 +-
 hw/s390x/s390-pci-vfio.c                  | 2 +-
 3 files changed, 2 insertions(+), 2 deletions(-)
 rename include/hw/s390x/{ => ipl}/s390-pci-clp.h (100%)
include/hw/s390x/ipl/s390-pci-clp.h renamed from include/hw/s390x/s390-pci-clp.h +0 -0
similarity index 100%
rename from include/hw/s390x/s390-pci-clp.h
rename to include/hw/s390x/ipl/s390-pci-clp.h
include/hw/s390x/s390-pci-bus.h +1 -1
index 04944d4fed..f643e13057 100644
@@ -19,7 +19,7 @@
 #include "hw/s390x/sclp.h"
 #include "hw/s390x/s390_flic.h"
 #include "hw/s390x/css.h"
-#include "hw/s390x/s390-pci-clp.h"
+#include "hw/s390x/ipl/s390-pci-clp.h"
 #include "qom/object.h"
 
 #define TYPE_S390_PCI_HOST_BRIDGE "s390-pcihost"
hw/s390x/s390-pci-vfio.c +1 -1
index 9e31029d7a..8ce44dbecc 100644
@@ -17,7 +17,7 @@
 
 #include "trace.h"
 #include "hw/s390x/s390-pci-bus.h"
-#include "hw/s390x/s390-pci-clp.h"
+#include "hw/s390x/ipl/s390-pci-clp.h"
 #include "hw/s390x/s390-pci-vfio.h"
 #include "hw/vfio/pci.h"
 #include "hw/vfio/vfio-container-legacy.h"
Signature · 1 line
2.49.0
Reviewed-by on v2 4/10
#14 lore raw

Re: [PATCH 04/10] include/hw/s390x: Move CLP definitions for easier BIOS access

Quoted 6 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Move the s390-pci-clp definitions into the "ipl" sub-directory, which is visible to the s390-bios. This allows the bios to reuse the architected definitions and prevents code duplication.

Quoted 7 lines
---
  include/hw/s390x/{ => ipl}/s390-pci-clp.h | 0
  include/hw/s390x/s390-pci-bus.h           | 2 +-
  hw/s390x/s390-pci-vfio.c                  | 2 +-
  3 files changed, 2 insertions(+), 2 deletions(-)

rename include/hw/s390x/{ => ipl}/s390-pci-clp.h (100%)

#15 lore raw

PATCH v2 5/10 pc-bios/s390-ccw: Introduce CLP Architecture

In reply to [email protected]

From: Jared Rossi <[email protected]>

Call Logical Processor (CLP) Architecture is used for managing PCI functions on s390x. Define and include the structures and routines needed to interact with PCI devices during IPL.

---
 pc-bios/s390-ccw/clp.h    | 24 ++++++++++
 pc-bios/s390-ccw/clp.c    | 96 +++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/Makefile |  2 +-
 3 files changed, 121 insertions(+), 1 deletion(-)
 create mode 100644 pc-bios/s390-ccw/clp.h
 create mode 100644 pc-bios/s390-ccw/clp.c
pc-bios/s390-ccw/clp.h new file +24 -0
new file mode 100644
index 0000000000..1ac2f8c177
@@ -0,0 +1,24 @@
+/*
+ * Call Logical Processor (CLP) architecture definitions
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef CLP_H
+#define CLP_H
+
+#ifndef QEMU_PACKED
+#define QEMU_PACKED __attribute__((packed))
+#endif
+
+#include <stdint.h>
+#include <s390-pci-clp.h>
+
+int clp_pci(void *data);
+int find_pci_function(uint32_t fid, ClpFhListEntry *entry);
+int enable_pci_function(uint32_t *fhandle);
+
+#endif
pc-bios/s390-ccw/clp.c new file +96 -0

96 lines not on this page · raw message

pc-bios/s390-ccw/Makefile +1 -1
index 259cff09db..9c29548f84 100644
@@ -35,7 +35,7 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d
 
 OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \
 	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \
-	  virtio-ccw.o
+	  virtio-ccw.o clp.o
 
 SLOF_DIR := $(SRC_PATH)/../../roms/SLOF
 
Signature · 1 line
2.49.0
#16 lore raw

Re: [PATCH 05/10] pc-bios/s390-ccw: Introduce CLP Architecture

Quoted 86 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Call Logical Processor (CLP) Architecture is used for managing PCI functions on s390x. Define and include the structures and routines needed to interact with PCI devices during IPL.

---
  pc-bios/s390-ccw/clp.h    | 24 ++++++++++
  pc-bios/s390-ccw/clp.c    | 96 +++++++++++++++++++++++++++++++++++++++
  pc-bios/s390-ccw/Makefile |  2 +-
  3 files changed, 121 insertions(+), 1 deletion(-)

create mode 100644 pc-bios/s390-ccw/clp.h
create mode 100644 pc-bios/s390-ccw/clp.c

pc-bios/s390-ccw/clp.h new file +24 -0
new file mode 100644
index 0000000000..1ac2f8c177
@@ -0,0 +1,24 @@
+/*
+ * Call Logical Processor (CLP) architecture definitions
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef CLP_H
+#define CLP_H
+
+#ifndef QEMU_PACKED
+#define QEMU_PACKED __attribute__((packed))
+#endif
+
+#include <stdint.h>
+#include <s390-pci-clp.h>
+
+int clp_pci(void *data);
+int find_pci_function(uint32_t fid, ClpFhListEntry *entry);
+int enable_pci_function(uint32_t *fhandle);
+
+#endif
pc-bios/s390-ccw/clp.c new file +35 -0
new file mode 100644
index 0000000000..6dc72c685c
@@ -0,0 +1,96 @@
+/*
+ * Call Logical Processor (CLP) architecture
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "clp.h"
+#include <stdio.h>
+#include <string.h>
+
+int clp_pci(void *data)
+{
+    struct { uint8_t _[2048]; } *req = data;
+    int cc = 3;
+
+    asm volatile (
+        "     .insn   rrf,0xb9a00000,0,%[req],0,2\n"
+        "     ipm     %[cc]\n"
+        "     srl     %[cc],28\n"
+        : [cc] "+d" (cc), "+m" (*req)
+        : [req] "a" (req)
+        : "cc");
+    return cc;
+}
+
+/*
+ * Get the PCI function entry for a given function ID
+ * Return 0 on success, 1 if the FID is not found, or a negative RC on error
+ */
+int find_pci_function(uint32_t fid, ClpFhListEntry *entry)
+{
+    int count = 0;

pc-bios/s390-ccw/clp.c line 45

Quoted 10 lines
pc-bios/s390-ccw/clp.c new file +10 -0
+    int limit = PCI_MAX_FUNCTIONS;
+    ClpReqRspListPci rrb;
+
+    rrb.request.hdr.len = 32;
+    rrb.request.hdr.cmd = 0x02;
+    rrb.request.resume_token = 0;
+    rrb.response.hdr.len = sizeof(ClpRspListPci);
+
+    do {
+        if (clp_pci(&rrb) || (rrb.response.hdr.rsp != 0x0010)) {

You could drop the parentheses around rrb.response.hdr.rsp != 0x0010 here.

Quoted 12 lines
pc-bios/s390-ccw/clp.c new file +12 -0
+            puts("Failed to list PCI functions");
+            return -1;
+        }
+
+        /* Resume token set when max enteries are returned */
+        if (rrb.response.resume_token) {
+            count = CLP_FH_LIST_NR_ENTRIES;
+            rrb.request.resume_token = rrb.response.resume_token;
+        } else {
+            count = (rrb.response.hdr.len - 32) / sizeof(ClpFhListEntry);
+        }
+

pc-bios/s390-ccw/clp.c line 67

Quoted 10 lines
pc-bios/s390-ccw/clp.c new file +10 -0
+        limit -= count;
+
+        for (int i = 0; i < count; i++) {
+            if (rrb.response.fh_list[i].fid == fid) {
+                memcpy(entry, &rrb.response.fh_list[i], sizeof(ClpFhListEntry));
+                return 0;
+            }
+        }
+
+    } while (rrb.request.resume_token && limit);

Maybe check for "limit > 0", just in case... ?

Quoted 11 lines
pc-bios/s390-ccw/clp.c new file +11 -0
+    puts("No function entry found for FID!");
+
+    return 1;
+}
+
+/*
+ * Enable the PCI function associated with a given handle
+ * Return 0 on success or a negative RC on error
+ */
+int enable_pci_function(uint32_t *fhandle)
+{

pc-bios/s390-ccw/clp.c line 89

Quoted 10 lines
pc-bios/s390-ccw/clp.c new file +10 -0
+    ClpReqRspSetPci rrb;
+
+    rrb.request.hdr.len = 32;
+    rrb.request.hdr.cmd = 0x05;
+    rrb.request.fh = *fhandle;
+    rrb.request.oc = 0;
+    rrb.request.ndas = 1;
+    rrb.response.hdr.len = 32;
+
+    if (clp_pci(&rrb) || (rrb.response.hdr.rsp != 0x0010)) {

Drop parentheses, please.

Quoted 12 lines
pc-bios/s390-ccw/clp.c new file +7 -0
+        puts("Failed to enable PCI function");
+        return -1;
+    }
+
+    *fhandle = rrb.response.fh;
+    return 0;
+}
pc-bios/s390-ccw/Makefile +0 -0
index 259cff09db..9c29548f84 100644

pc-bios/s390-ccw/Makefile QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d · line 41

Quoted 8 lines
pc-bios/s390-ccw/Makefile +1 -1
@@ -35,7 +35,7 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d
 
  OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \
  	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \
-	  virtio-ccw.o
+	  virtio-ccw.o clp.o
 
  SLOF_DIR := $(SRC_PATH)/../../roms/SLOF
 

Thomas

#17 lore raw

Re: [PATCH 05/10] pc-bios/s390-ccw: Introduce CLP Architecture

Hi Thomas,

All of the comments below raise good points, I'll fix them in the next version.

Thanks for your reviews.

Regards,
Jared Rossi

Quoted 172 lines · Thomas Huth
Quoted 87 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Call Logical Processor (CLP) Architecture is used for managing PCI functions on s390x. Define and include the structures and routines needed to interact with PCI devices during IPL.

---
  pc-bios/s390-ccw/clp.h    | 24 ++++++++++ pc-bios/s390-ccw/clp.c    | 96 +++++++++++++++++++++++++++++++++++++++ pc-bios/s390-ccw/Makefile |  2 +-

  3 files changed, 121 insertions(+), 1 deletion(-)

  create mode 100644 pc-bios/s390-ccw/clp.h
  create mode 100644 pc-bios/s390-ccw/clp.c

pc-bios/s390-ccw/clp.h new file +24 -0
new file mode 100644
index 0000000000..1ac2f8c177
@@ -0,0 +1,24 @@
+/*
+ * Call Logical Processor (CLP) architecture definitions
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef CLP_H
+#define CLP_H
+
+#ifndef QEMU_PACKED
+#define QEMU_PACKED __attribute__((packed))
+#endif
+
+#include <stdint.h>
+#include <s390-pci-clp.h>
+
+int clp_pci(void *data);
+int find_pci_function(uint32_t fid, ClpFhListEntry *entry);
+int enable_pci_function(uint32_t *fhandle);
+
+#endif
pc-bios/s390-ccw/clp.c new file +31 -0
new file mode 100644
index 0000000000..6dc72c685c
@@ -0,0 +1,96 @@
+/*
+ * Call Logical Processor (CLP) architecture
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "clp.h"
+#include <stdio.h>
+#include <string.h>
+
+int clp_pci(void *data)
+{
+    struct { uint8_t _[2048]; } *req = data;
+    int cc = 3;
+
+    asm volatile (
+        "     .insn   rrf,0xb9a00000,0,%[req],0,2\n"
+        "     ipm     %[cc]\n"
+        "     srl     %[cc],28\n"
+        : [cc] "+d" (cc), "+m" (*req)
+        : [req] "a" (req)
+        : "cc");
+    return cc;
+}
+
+/*
+ * Get the PCI function entry for a given function ID
+ * Return 0 on success, 1 if the FID is not found, or a negative RC 

on error
+ */

pc-bios/s390-ccw/clp.c new file +3 -0
+int find_pci_function(uint32_t fid, ClpFhListEntry *entry)
+{
+    int count = 0;

pc-bios/s390-ccw/clp.c line 45

Quoted 10 lines
pc-bios/s390-ccw/clp.c new file +10 -0
+    int limit = PCI_MAX_FUNCTIONS;
+    ClpReqRspListPci rrb;
+
+    rrb.request.hdr.len = 32;
+    rrb.request.hdr.cmd = 0x02;
+    rrb.request.resume_token = 0;
+    rrb.response.hdr.len = sizeof(ClpRspListPci);
+
+    do {
+        if (clp_pci(&rrb) || (rrb.response.hdr.rsp != 0x0010)) {

You could drop the parentheses around rrb.response.hdr.rsp != 0x0010 here.

Quoted 18 lines

+            puts("Failed to list PCI functions");
+            return -1;
+        }
+
+        /* Resume token set when max enteries are returned */ +        if (rrb.response.resume_token) {
+            count = CLP_FH_LIST_NR_ENTRIES;
+            rrb.request.resume_token = rrb.response.resume_token; +        } else {
+            count = (rrb.response.hdr.len - 32) / sizeof(ClpFhListEntry);
+        }

pc-bios/s390-ccw/clp.c new file +6 -0
+
+        limit -= count;
+
+        for (int i = 0; i < count; i++) {
+            if (rrb.response.fh_list[i].fid == fid) {
+                memcpy(entry, &rrb.response.fh_list[i], 
Quoted 6 lines

sizeof(ClpFhListEntry));
+                return 0;
+            }
+        }
+
+    } while (rrb.request.resume_token && limit);

Maybe check for "limit > 0", just in case... ?

Quoted 11 lines

+    puts("No function entry found for FID!");

pc-bios/s390-ccw/clp.c new file +10 -0
+
+    return 1;
+}
+
+/*
+ * Enable the PCI function associated with a given handle
+ * Return 0 on success or a negative RC on error
+ */
+int enable_pci_function(uint32_t *fhandle)
+{

pc-bios/s390-ccw/clp.c line 89

Quoted 10 lines
pc-bios/s390-ccw/clp.c new file +10 -0
+    ClpReqRspSetPci rrb;
+
+    rrb.request.hdr.len = 32;
+    rrb.request.hdr.cmd = 0x05;
+    rrb.request.fh = *fhandle;
+    rrb.request.oc = 0;
+    rrb.request.ndas = 1;
+    rrb.response.hdr.len = 32;
+
+    if (clp_pci(&rrb) || (rrb.response.hdr.rsp != 0x0010)) {

Drop parentheses, please.

Quoted 16 lines

+        puts("Failed to enable PCI function");
+        return -1;
+    }

pc-bios/s390-ccw/clp.c new file +4 -0
+
+    *fhandle = rrb.response.fh;
+    return 0;
+}
pc-bios/s390-ccw/Makefile +1 -1
index 259cff09db..9c29548f84 100644
@@ -35,7 +35,7 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d
     OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \
         virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \
-      virtio-ccw.o
+      virtio-ccw.o clp.o
Quoted 1 line

    SLOF_DIR := $(SRC_PATH)/../../roms/SLOF

 Thomas

#18 lore raw

PATCH v2 6/10 pc-bios/s390-ccw: Introduce PCI device

In reply to [email protected]

From: Jared Rossi <[email protected]>

Define selected s390x PCI instructions and extend IPLB to allow PCI devices.

---
 pc-bios/s390-ccw/pci.h    |  88 ++++++++++
 pc-bios/s390-ccw/pci.c    | 331 ++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/Makefile |   2 +-
 3 files changed, 420 insertions(+), 1 deletion(-)
 create mode 100644 pc-bios/s390-ccw/pci.h
 create mode 100644 pc-bios/s390-ccw/pci.c
pc-bios/s390-ccw/pci.h new file +88 -0
new file mode 100644
index 0000000000..847cf3f194
@@ -0,0 +1,88 @@
+/*
+ * s390x PCI definitions
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef PCI_H
+#define PCI_H
+
+#include <stdint.h>
+#include <stdbool.h>
+#include "clp.h"
+
+#define ZPCI_CREATE_REQ(handle, space, len)                    \
+    ((uint64_t) handle << 32 | space << 16 | len)
+
+union register_pair {
+    unsigned __int128 pair;
+    struct {
+        unsigned long even;
+        unsigned long odd;
+    };
+};
+
+#define PCIFIB_FC_ENABLED      0x80
+#define PCIFIB_FC_ERROR        0x40
+#define PCIFIB_FC_BLOCKED      0x20
+#define PCIFIB_FC_DMAREG       0x10
+
+#define PCIST_DISABLED         0x0
+#define PCIST_ENABLED          0x1
+
+#define PCI_CFGBAR             0xF  /* Base Address Register for config space */
+#define PCI_CAPABILITY_LIST    0x34 /* Offset of first capability list entry */
+
+struct PciFib {
+    uint32_t reserved0[2];
+    uint8_t fcflags;
+    uint8_t reserved1[3];
+    uint32_t reserved2;
+    uint64_t pba;
+    uint64_t pal;
+    uint64_t iota;
+    uint16_t isc:4;
+    uint16_t noi:12;
+    uint8_t reserved3:2;
+    uint8_t aibvo:6;
+    uint8_t s:1;
+    uint8_t reserved4:1;
+    uint8_t aisbo:6;
+    uint32_t reserved5;
+    uint64_t aibv;
+    uint64_t aisb;
+    uint64_t fmba;
+    uint32_t reserved6[2];
+};
+typedef struct PciFib PciFib;
+
+struct PciDevice {
+    uint16_t device_id;
+    uint16_t vendor_id;
+    uint32_t fid;
+    uint32_t fhandle;
+    uint8_t status;
+    PciFib fib;
+};
+typedef struct PciDevice PciDevice;
+
+int pci_write_flex(uint32_t fhandle, uint64_t offset, uint8_t pcias, void *data, int len);
+int pci_write_byte(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint8_t data);
+int pci_bswap16_write(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint16_t data);
+int pci_bswap32_write(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint32_t data);
+int pci_bswap64_write(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint64_t data);
+
+int pci_read_flex(uint32_t fhandle, uint64_t offset, uint8_t pcias, void *buf, int len);
+int pci_read_bswap64(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint64_t *buf);
+int pci_read_bswap32(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint32_t *buf);
+int pci_read_bswap16(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint16_t *buf);
+int pci_read_byte(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint8_t *buf);
+
+int pci_dev_enable(PciDevice *pcidev);
+int get_fib(PciFib *fib, uint32_t fhandle);
+int set_fib(PciFib *fib, uint32_t fhandle, uint8_t dma_as, uint8_t opcontrol);
+
+#endif
pc-bios/s390-ccw/pci.c new file +331 -0

331 lines not on this page · raw message

pc-bios/s390-ccw/Makefile +1 -1
index 9c29548f84..a62fc9d766 100644
@@ -35,7 +35,7 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d
 
 OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \
 	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \
-	  virtio-ccw.o clp.o
+	  virtio-ccw.o clp.o pci.o
 
 SLOF_DIR := $(SRC_PATH)/../../roms/SLOF
 
Signature · 1 line
2.49.0
#19 lore raw

Re: [PATCH 06/10] pc-bios/s390-ccw: Introduce PCI device

Quoted 12 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Define selected s390x PCI instructions and extend IPLB to allow PCI devices.

---
  pc-bios/s390-ccw/pci.h    |  88 ++++++++++
  pc-bios/s390-ccw/pci.c    | 331 ++++++++++++++++++++++++++++++++++++++
  pc-bios/s390-ccw/Makefile |   2 +-
  3 files changed, 420 insertions(+), 1 deletion(-)

create mode 100644 pc-bios/s390-ccw/pci.h
create mode 100644 pc-bios/s390-ccw/pci.c

...

Quoted 107 lines
pc-bios/s390-ccw/pci.c new file +101 -0
new file mode 100644
index 0000000000..c149789386
@@ -0,0 +1,331 @@
+/*
+ * s390x PCI funcionality
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "clp.h"
+#include "pci.h"
+#include "bswap.h"
+#include <stdio.h>
+#include <stdbool.h>
+
+/* PCI load */
+static inline int pcilg(uint64_t *data, uint64_t req, uint64_t offset, uint8_t *status)
+{
+    union register_pair req_off = {.even = req, .odd = offset};
+    int cc = -1;
+    uint64_t __data;
+
+    asm volatile (
+        "     .insn   rre,0xb9d20000,%[data],%[req_off]\n"
+        "     ipm     %[cc]\n"
+        "     srl     %[cc],28\n"
+        : [cc] "+d" (cc), [data] "=d" (__data),
+          [req_off] "+d" (req_off.pair) :: "cc");
+    *status = req_off.even >> 24 & 0xff;
+    *data = __data;
+    return cc;
+}
+
+/* PCI store */
+int pcistg(uint64_t data, uint64_t req, uint64_t offset, uint8_t *status)
+{
+    union register_pair req_off = {.even = req, .odd = offset};
+    int cc = -1;
+
+    asm volatile (
+        "     .insn   rre,0xb9d00000,%[data],%[req_off]\n"
+        "     ipm     %[cc]\n"
+        "     srl     %[cc],28\n"
+        : [cc] "+d" (cc), [req_off] "+d" (req_off.pair)
+        : [data] "d" (data)
+        : "cc");
+    *status = req_off.even >> 24 & 0xff;
+    return cc;
+}
+
+/* store PCI function controls */
+int stpcifc(uint64_t req, PciFib *fib, uint8_t *status)
+{
+    uint8_t cc;
+
+    asm volatile (
+        "     .insn   rxy,0xe300000000d4,%[req],%[fib]\n"
+        "     ipm     %[cc]\n"
+        "     srl     %[cc],28\n"
+        : [cc] "=d" (cc), [req] "+d" (req), [fib] "+Q" (*fib)
+        : : "cc");
+    *status = req >> 24 & 0xff;
+    return cc;
+}
+
+/* modify PCI function controls */
+int mpcifc(uint64_t req, PciFib *fib, uint8_t *status)
+{
+    uint8_t cc;
+
+    asm volatile (
+        "     .insn   rxy,0xe300000000d0,%[req],%[fib]\n"
+        "     ipm     %[cc]\n"
+        "     srl     %[cc],28\n"
+        : [cc] "=d" (cc), [req] "+d" (req), [fib] "+Q" (*fib)
+        : : "cc");
+    *status = req >> 24 & 0xff;
+    return cc;
+}
+
+static int pci_write(uint32_t fhandle, uint64_t offset, uint8_t pcias,
+                     uint64_t data, uint8_t len)
+{
+
+    uint64_t req = ZPCI_CREATE_REQ(fhandle, pcias, len);
+    uint8_t status;
+    int rc;
+
+    /* writes must be non-zero powers of 2 with a maximum of 8 bytes per read */
+    switch (len) {
+    case 1:
+    case 2:
+    case 4:
+    case 8:
+        rc = pcistg(data, req, offset, &status);
+        break;
+    default:
+        rc = -1;
+    }
+
+    /* Error condition detected */

pc-bios/s390-ccw/pci.c line 111

Quoted 10 lines
pc-bios/s390-ccw/pci.c new file +10 -0
+    if (rc == 1) {
+        printf("PCI store failed with status condition %d\n", status);
+        return -1;
+    }
+
+    return rc ? -1 : 0;
+}
+
+/* Write an arbitrary length of data without byte swapping */
+int pci_write_flex(uint32_t fh, uint64_t offset, uint8_t pcias, void *data, int len)

You don't seem to use the pci_write_flex() functions in any later patches ... could it be dropped?

Quoted 122 lines
pc-bios/s390-ccw/pci.c new file +122 -0
+{
+    uint8_t writelen, tmp;
+    int rc;
+    int remaining = len;
+
+    /* write bytes in powers of 2, up to a maximum of 8 bytes per read */
+    while (remaining) {
+        if (remaining > 7) {
+            writelen = 8;
+        } else {
+            writelen = 1;
+            while (true) {
+                tmp = writelen * 2;
+                if (tmp > remaining) {
+                    break;
+                }
+
+                writelen = tmp;
+            }
+        }
+
+        /* Access next data based on write size */
+        switch (writelen) {
+        case 1:
+            rc = pci_write(fh, offset, pcias, ((uint8_t *)data)[0], 1);
+            break;
+        case 2:
+            rc = pci_write(fh, offset, pcias, ((uint16_t *)data)[0], 2);
+            break;
+        case 4:
+            rc = pci_write(fh, offset, pcias, ((uint32_t *)data)[0], 4);
+            break;
+        case 8:
+            rc = pci_write(fh, offset, pcias, ((uint64_t *)data)[0], 8);
+            break;
+        default:
+            rc = -1;
+        }
+
+        if (rc) {
+            return -1;
+        }
+
+        remaining -= writelen;
+        data += writelen;
+        offset += writelen;
+    }
+
+    return 0;
+}
+
+int pci_write_byte(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint8_t data)
+{
+    return pci_write(fhandle, offset, pcias, (uint64_t)data, 1);
+}
+
+/* Wrappers to byte swap common data sizes then write */
+int pci_bswap16_write(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint16_t data)
+{
+    uint64_t le_data = bswap16(data);
+    return pci_write(fhandle, offset, pcias, le_data, 2);
+}
+
+int pci_bswap32_write(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint32_t data)
+{
+    uint64_t le_data = bswap32(data);
+    return pci_write(fhandle, offset, pcias, le_data, 4);
+}
+
+int pci_bswap64_write(uint32_t fhandle, uint64_t offset, uint8_t pcias, uint64_t data)
+{
+    uint64_t le_data = bswap64(data);
+    return pci_write(fhandle, offset, pcias, le_data, 8);
+}
+
+static int pci_read(uint32_t fh, uint64_t offset, uint8_t pcias, void *buf, uint8_t len)
+{
+    uint64_t req, data;
+    uint8_t status;
+    int rc;
+
+    req = ZPCI_CREATE_REQ(fh, pcias, len);
+    rc = pcilg(&data, req, offset, &status);
+
+    /* Error condition detected */
+    if (rc == 1) {
+        printf("PCI load failed with status condition %d\n", status);
+        return -1;
+    }
+
+    switch (len) {
+    case 1:
+        *(uint8_t *)buf = data;
+        break;
+    case 2:
+        *(uint16_t *)buf = data;
+        break;
+    case 4:
+        *(uint32_t *)buf = data;
+        break;
+    case 8:
+        *(uint64_t *)buf = data;
+        break;
+    default:
+        return -1;
+    }
+
+    return rc ? -1 : 0;
+}
+
+/* Read to an arbitrary length buffer without byte swapping */
+int pci_read_flex(uint32_t fh, uint64_t offset, uint8_t pcias, void *buf, int len)
+{
+    uint8_t readlen, tmp;
+    int rc;
+    int remaining = len;
+
+    /* Read bytes in powers of 2, up to a maximum of 8 bytes per read */
+    while (remaining) {
+        if (remaining > 7) {
+            readlen = 8;
+        } else {

pc-bios/s390-ccw/pci.c line 243

Quoted 10 lines
pc-bios/s390-ccw/pci.c new file +10 -0
+            readlen = 1;
+            while (true) {
+                tmp = readlen * 2;
+                if (tmp > remaining) {
+                    break;
+                }
+
+                readlen = tmp;
+            }
+        }

This looks cumbersome ... maybe rather something like this (untested):

	for (int i = 3; i >= 0; i--) {
		readlen = 1 << i;
		if (remaining >= readlen)
			break;
	}

?

pc-bios/s390-ccw/pci.c line 256

Quoted 12 lines
pc-bios/s390-ccw/pci.c new file +12 -0
+        rc = pci_read(fh, offset, pcias, buf, readlen);
+        if (rc) {
+            return -1;
+        }
+
+        remaining -= readlen;
+        buf += readlen;
+        offset += readlen;
+    }
+
+    return 0;
+}

Thomas

#20 lore raw

PATCH v2 7/10 pc-bios/s390-ccw: Add support for virtio-blk-pci IPL

In reply to [email protected]

From: Jared Rossi <[email protected]>

Enable virt-queue PCI configuration and add routines for virtio-blk-pci devices.

---
 include/hw/s390x/ipl/qipl.h      |  10 +
 pc-bios/s390-ccw/virtio-pci.h    |  79 +++++++
 pc-bios/s390-ccw/virtio.h        |   1 +
 pc-bios/s390-ccw/main.c          |  60 +++++-
 pc-bios/s390-ccw/virtio-blkdev.c |   3 +
 pc-bios/s390-ccw/virtio-pci.c    | 360 +++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/virtio.c        |   5 +
 pc-bios/s390-ccw/Makefile        |   2 +-
 8 files changed, 517 insertions(+), 3 deletions(-)
 create mode 100644 pc-bios/s390-ccw/virtio-pci.h
 create mode 100644 pc-bios/s390-ccw/virtio-pci.c
include/hw/s390x/ipl/qipl.h +10 -0
index 8199b839f0..5c7779a1c3 100644
@@ -22,6 +22,7 @@
 
 #define S390_IPL_TYPE_FCP 0x00
 #define S390_IPL_TYPE_CCW 0x02
+#define S390_IPL_TYPE_PCI 0x04
 #define S390_IPL_TYPE_PV 0x05
 #define S390_IPL_TYPE_QEMU_SCSI 0xff
 
@@ -105,6 +106,14 @@ struct IplBlockQemuScsi {
 } QEMU_PACKED;
 typedef struct IplBlockQemuScsi IplBlockQemuScsi;
 
+struct IplBlockPci {
+    uint32_t reserved0[80];
+    uint8_t  opt;
+    uint8_t  reserved1[3];
+    uint32_t fid;
+} QEMU_PACKED;
+typedef struct IplBlockPci IplBlockPci;
+
 union IplParameterBlock {
     struct {
         uint32_t len;
@@ -120,6 +129,7 @@ union IplParameterBlock {
             IplBlockFcp fcp;
             IPLBlockPV pv;
             IplBlockQemuScsi scsi;
+            IplBlockPci pci;
         };
     } QEMU_PACKED;
     struct {
pc-bios/s390-ccw/virtio-pci.h new file +79 -0

79 lines not on this page · raw message

pc-bios/s390-ccw/virtio.h +1 -0
index e747891a2c..cc68c2f8a9 100644
@@ -256,6 +256,7 @@ struct VDev {
     uint8_t scsi_dev_heads;
     bool scsi_device_selected;
     ScsiDevice selected_scsi_device;
+    uint32_t pci_fh;
     uint32_t max_transfer;
     uint32_t guest_features[2];
 };
pc-bios/s390-ccw/main.c +58 -2

91 lines not on this page · raw message

pc-bios/s390-ccw/virtio-blkdev.c +3 -0
index 87ab9a9513..0542b4feee 100644
@@ -13,6 +13,7 @@
 #include "virtio.h"
 #include "virtio-scsi.h"
 #include "virtio-ccw.h"
+#include "virtio-pci.h"
 
 #define VIRTIO_BLK_F_GEOMETRY   (1 << 4)
 #define VIRTIO_BLK_F_BLK_SIZE   (1 << 6)
@@ -242,6 +243,8 @@ int virtio_blk_setup_device(void)
     case S390_IPL_TYPE_QEMU_SCSI:
     case S390_IPL_TYPE_CCW:
         return virtio_ccw_setup(vdev);
+    case S390_IPL_TYPE_PCI:
+        return virtio_pci_setup(vdev);
     }
 
     return 1;
pc-bios/s390-ccw/virtio-pci.c new file +360 -0

360 lines not on this page · raw message

pc-bios/s390-ccw/virtio.c +5 -0
index 0488b3a07e..50afb11f6a 100644
@@ -15,6 +15,7 @@
 #include "virtio.h"
 #include "virtio-scsi.h"
 #include "virtio-ccw.h"
+#include "virtio-pci.h"
 #include "bswap.h"
 #include "helper.h"
 #include "s390-time.h"
@@ -109,6 +110,8 @@ bool vring_notify(VRing *vr)
     case S390_IPL_TYPE_QEMU_SCSI:
     case S390_IPL_TYPE_CCW:
         vr->cookie = virtio_ccw_notify(vr->schid, vr->id, vr->cookie);
+    case S390_IPL_TYPE_PCI:
+        vr->cookie = virtio_pci_notify(virtio_get_device()->pci_fh, vr->id);
     }
 
     return vr->cookie >= 0;
@@ -182,6 +185,8 @@ int virtio_reset(VDev *vdev)
     case S390_IPL_TYPE_QEMU_SCSI:
     case S390_IPL_TYPE_CCW:
         return virtio_ccw_reset(vdev);
+    case S390_IPL_TYPE_PCI:
+        return virtio_pci_reset(vdev);
     default:
         return -1;
     }
pc-bios/s390-ccw/Makefile +1 -1
index a62fc9d766..3e5dfb64d5 100644
@@ -35,7 +35,7 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d
 
 OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \
 	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \
-	  virtio-ccw.o clp.o pci.o
+	  virtio-ccw.o clp.o pci.o virtio-pci.o
 
 SLOF_DIR := $(SRC_PATH)/../../roms/SLOF
 
Signature · 1 line
2.49.0
#21 lore raw

Re: [PATCH 07/10] pc-bios/s390-ccw: Add support for virtio-blk-pci IPL

Quoted 31 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Enable virt-queue PCI configuration and add routines for virtio-blk-pci devices.

---
  include/hw/s390x/ipl/qipl.h      |  10 +
  pc-bios/s390-ccw/virtio-pci.h    |  79 +++++++
  pc-bios/s390-ccw/virtio.h        |   1 +
  pc-bios/s390-ccw/main.c          |  60 +++++-
  pc-bios/s390-ccw/virtio-blkdev.c |   3 +
  pc-bios/s390-ccw/virtio-pci.c    | 360 +++++++++++++++++++++++++++++++
  pc-bios/s390-ccw/virtio.c        |   5 +
  pc-bios/s390-ccw/Makefile        |   2 +-
  8 files changed, 517 insertions(+), 3 deletions(-)

create mode 100644 pc-bios/s390-ccw/virtio-pci.h create mode 100644 pc-bios/s390-ccw/virtio-pci.c

include/hw/s390x/ipl/qipl.h +1 -0
index 8199b839f0..5c7779a1c3 100644
@@ -22,6 +22,7 @@
 
  #define S390_IPL_TYPE_FCP 0x00
  #define S390_IPL_TYPE_CCW 0x02
+#define S390_IPL_TYPE_PCI 0x04
  #define S390_IPL_TYPE_PV 0x05
  #define S390_IPL_TYPE_QEMU_SCSI 0xff
 

include/hw/s390x/ipl/qipl.h struct IplBlockQemuScsi { · line 115

Quoted 10 lines
include/hw/s390x/ipl/qipl.h +7 -0
@@ -105,6 +106,14 @@ struct IplBlockQemuScsi {
  } QEMU_PACKED;
  typedef struct IplBlockQemuScsi IplBlockQemuScsi;
 
+struct IplBlockPci {
+    uint32_t reserved0[80];
+    uint8_t  opt;
+    uint8_t  reserved1[3];
+    uint32_t fid;
+} QEMU_PACKED;
+typedef struct IplBlockPci IplBlockPci;

Is this supposed to have the same positions for "fid" and "opt" as in the ipl_pb0_nvme structure in the kernel (arch/s390/include/uapi/asm/ipl.h)?

Quoted 11 lines
include/hw/s390x/ipl/qipl.h +1 -0
  union IplParameterBlock {
      struct {
          uint32_t len;
@@ -120,6 +129,7 @@ union IplParameterBlock {
              IplBlockFcp fcp;
              IPLBlockPV pv;
              IplBlockQemuScsi scsi;
+            IplBlockPci pci;
          };
      } QEMU_PACKED;
      struct {

...

Quoted 122 lines
pc-bios/s390-ccw/virtio-pci.c new file +116 -0
new file mode 100644
index 0000000000..838231d86c
@@ -0,0 +1,360 @@
+/*
+ * Functionality for virtio-pci
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "clp.h"
+#include "pci.h"
+#include "helper.h"
+#include "s390-ccw.h"
+#include "virtio.h"
+#include "bswap.h"
+#include "virtio-pci.h"
+#include "s390-time.h"
+#include <stdio.h>
+
+/* Variable offsets used for reads/writes to modern memory regions */
+VirtioPciCap c_cap; /* Common capabilities  */
+VirtioPciCap d_cap; /* Device capabilities  */
+VirtioPciCap n_cap; /* Notify capabilities  */
+uint32_t notify_mult;
+uint16_t q_notify_offset;
+
+static int virtio_pci_set_status(VDev *vdev, uint8_t status)
+{
+    int rc = pci_write_byte(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_STATUS,
+                             c_cap.bar, status);
+    if (rc) {
+        puts("Failed to write virtio-pci status");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+static int virtio_pci_get_status(VDev *vdev, uint8_t *status)
+{
+    int rc = pci_read_byte(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_STATUS,
+                           c_cap.bar, status);
+    if (rc) {
+        puts("Failed to read virtio-pci status");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+/*
+ * Find the position of the capability config within PCI configuration
+ * space for a given cfg type.  Return the position if found, otherwise 0.
+ */
+static uint8_t find_cap_pos(uint32_t fhandle, uint8_t cfg_type)
+{
+    uint8_t next, cfg;
+    int rc;
+
+    rc = pci_read_byte(fhandle, PCI_CAPABILITY_LIST, PCI_CFGBAR, &next);
+    rc |= pci_read_byte(fhandle, next + 3, PCI_CFGBAR, &cfg);
+
+    while (!rc && (cfg != cfg_type) && next) {
+        rc = pci_read_byte(fhandle, next + 1, PCI_CFGBAR, &next);
+        rc |= pci_read_byte(fhandle, next + 3, PCI_CFGBAR, &cfg);
+    }
+
+    return rc ? 0 : next;
+}
+
+static int virtio_pci_get_hfeatures(VDev *vdev, uint64_t *features)
+{
+    uint32_t feat0, feat1;
+    int rc;
+
+    rc = pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_DFSELECT,
+                           c_cap.bar, 0);
+
+    rc |= pci_read_bswap32(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_DF,
+                           c_cap.bar, &feat0);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_DFSELECT,
+                            c_cap.bar, 1);
+
+    rc |= pci_read_bswap32(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_DF,
+                               c_cap.bar, &feat1);
+
+    if (rc) {
+        puts("Failed to get PCI feature bits");
+        return -EIO;
+    }
+
+    *features = (uint64_t) feat1 << 32;
+    *features |= (uint64_t) feat0;
+
+    return 0;
+}
+
+static int virtio_pci_set_gfeatures(VDev *vdev)
+{
+    int rc;
+
+    rc = pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GFSELECT,
+                           c_cap.bar, 0);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GF,
+                            c_cap.bar, vdev->guest_features[1]);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GFSELECT,
+                            c_cap.bar, 1);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GF,
+                                c_cap.bar, vdev->guest_features[0]);
+
+    if (rc) {
+        puts("Failed to set PCI feature bits");

pc-bios/s390-ccw/virtio-pci.c line 126

Quoted 10 lines
pc-bios/s390-ccw/virtio-pci.c new file +10 -0
+        return -EIO;
+    }
+
+    return 0;
+}
+
+static int virtio_pci_get_blk_config(VDev *vdev)
+{
+    return pci_read_flex(vdev->pci_fh, d_cap.off, d_cap.bar, &vdev->config.blk,
+                         sizeof(VirtioBlkConfig));

I'm a little bit surprised that there does not seem to be any endianess swapping for the config.blk data anywhere here ... isn't that config space data supposed to be in little endian?

... oh, wait, you're not negotiating VIRTIO_F_VERSION_1, are you? ... so the config space is still in big endian for legacy virtio? ... hmm, I guess it's ok for now, but in the long run, I think we should rather use VERSION_1 instead.

Quoted 118 lines
pc-bios/s390-ccw/virtio-pci.c new file +118 -0
+}
+
+/* virtio spec v1.3 section 4.1.2.1 */
+void virtio_pci_id2type(VDev *vdev, uint16_t device_id)
+{
+    switch (device_id) {
+    case 0x1001:
+        vdev->dev_type = VIRTIO_ID_BLOCK;
+        break;
+    case 0x1000: /* Other valid but currently unsupported virtio device types */
+    case 0x1004:
+    default:
+        vdev->dev_type = 0;
+    }
+}
+
+/*
+ * Read PCI configuration space to find the offset of the Common, Device, and
+ * Notification memory regions within the modern memory space.
+ * Returns 0 if success, 1 if a capability could not be located, or a
+ * negative RC if the configuration read failed.
+ */
+static int virtio_pci_read_pci_cap_config(VDev *vdev)
+{
+    uint8_t pos;
+    int rc;
+
+    /* Common capabilities */
+    pos = find_cap_pos(vdev->pci_fh, VPCI_CAP_COMMON_CFG);
+    if (!pos) {
+        puts("Failed to locate PCI common configuration");
+        return 1;
+    }
+
+    rc = pci_read_byte(vdev->pci_fh, pos + VPCI_CAP_BAR, PCI_CFGBAR, &c_cap.bar);
+    if (rc || pci_read_bswap32(vdev->pci_fh, pos + VPCI_CAP_OFFSET, PCI_CFGBAR,
+                               &c_cap.off)) {
+        puts("Failed to read PCI common configuration");
+        return -EIO;
+    }
+
+    /* Device capabilities */
+    pos = find_cap_pos(vdev->pci_fh, VPCI_CAP_DEVICE_CFG);
+    if (!pos) {
+        puts("Failed to locate PCI device configuration");
+        return 1;
+    }
+
+    rc = pci_read_byte(vdev->pci_fh, pos + VPCI_CAP_BAR, PCI_CFGBAR, &d_cap.bar);
+    if (rc || pci_read_bswap32(vdev->pci_fh, pos + VPCI_CAP_OFFSET, PCI_CFGBAR,
+                               &d_cap.off)) {
+        puts("Failed to read PCI device configuration");
+        return -EIO;
+    }
+
+    /* Notification capabilities */
+    pos = find_cap_pos(vdev->pci_fh, VPCI_CAP_NOTIFY_CFG);
+    if (!pos) {
+        puts("Failed to locate PCI notification configuration");
+        return 1;
+    }
+
+    rc = pci_read_byte(vdev->pci_fh, pos + VPCI_CAP_BAR, PCI_CFGBAR, &n_cap.bar);
+    if (rc || pci_read_bswap32(vdev->pci_fh, pos + VPCI_CAP_OFFSET, PCI_CFGBAR,
+                               &n_cap.off)) {
+        puts("Failed to read PCI notification configuration");
+        return -EIO;
+    }
+
+    rc = pci_read_bswap32(vdev->pci_fh, pos + VPCI_N_CAP_MULT, PCI_CFGBAR, &notify_mult);
+    if (rc || pci_read_bswap16(vdev->pci_fh, d_cap.off + VPCI_C_OFFSET_Q_NOFF,
+                               d_cap.bar, &q_notify_offset)) {
+        puts("Failed to read notification queue configuration");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+int virtio_pci_reset(VDev *vdev)
+{
+    int rc;
+    uint8_t status = VPCI_S_RESET;
+
+    rc = virtio_pci_set_status(vdev, status);
+    rc |= virtio_pci_get_status(vdev, &status);
+
+    if (rc || status) {
+        puts("Failed to reset virtio-pci device");
+        return 1;
+    }
+
+    return 0;
+}
+
+int virtio_pci_set_selected_vq(VDev *vdev, uint16_t queue_num)
+{
+    return pci_bswap16_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_Q_SELECT,
+                             c_cap.bar, queue_num);
+}
+
+int virtio_pci_set_queue_size(VDev *vdev, uint16_t queue_size)
+{
+    return pci_bswap16_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_Q_SIZE,
+                             c_cap.bar, queue_size);
+}
+
+static int virtio_pci_set_queue_enable(VDev *vdev, uint16_t enabled)
+{
+    return pci_bswap16_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_Q_ENABLE,
+                             c_cap.bar, enabled);
+}
+
+static int set_pci_vq_addr(VDev *vdev, uint64_t config_off, void *addr)
+{
+    return pci_bswap64_write(vdev->pci_fh, c_cap.off + config_off, c_cap.bar,
+                             (uint64_t) addr);
+}

pc-bios/s390-ccw/virtio-pci.c line 254

Quoted 10 lines
pc-bios/s390-ccw/virtio-pci.c new file +10 -0
+
+int virtio_pci_setup(VDev *vdev)
+{
+    VRing *vr;
+    int rc;
+    uint64_t pci_features;
+    uint8_t status;
+    int i = 0;
+
+    vdev->config.blk.blk_size = 0;

For really getting blk_size, I think you should negotiate VIRTIO_BLK_F_BLK_SIZE ?

Also, I assume the generic part of this function should rather be agnostic of virtio-block specific settings instead, so it can be used for virtio-net-pci and virtio-scsi-pci later? So the above line should likely rather go into the "case VIRTIO_ID_BLOCK" part below?

Quoted 78 lines
pc-bios/s390-ccw/virtio-pci.c new file +78 -0
+    vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
+    vdev->cmd_vr_idx = 0;
+
+    if (virtio_reset(vdev)) {
+        return -EIO;
+    }
+
+    status = VPCI_S_ACKNOWLEDGE;
+    rc = virtio_pci_set_status(vdev, status);
+    if (rc) {
+        puts("Virtio-pci device Failed to ACKNOWLEDGE");
+        return -EIO;
+    }
+
+    rc = virtio_pci_read_pci_cap_config(vdev);
+    if (rc) {
+        printf("Invalid PCI capabilities");
+        return -EIO;
+    }
+
+    switch (vdev->dev_type) {
+    case VIRTIO_ID_BLOCK:
+        vdev->nr_vqs = 1;
+        vdev->cmd_vr_idx = 0;
+        virtio_pci_get_blk_config(vdev);
+        break;
+    default:
+        puts("Unsupported virtio device");
+        return -ENODEV;
+    }
+
+    status |= VPCI_S_DRIVER;
+    rc = virtio_pci_set_status(vdev, status);
+    if (rc) {
+        puts("Set status failed");
+        return -EIO;
+    }
+
+    /* Feature negotiation */
+    rc = virtio_pci_get_hfeatures(vdev, &pci_features);
+    if (rc || virtio_pci_set_gfeatures(vdev)) {
+        return -EIO;
+    }
+
+    /* Configure virt-queues for pci */
+    for (i = 0; i < vdev->nr_vqs; i++) {
+        VqInfo info = {
+            .queue = (unsigned long long) virtio_get_ring_area() + (i * VIRTIO_RING_SIZE),
+            .align = KVM_S390_VIRTIO_RING_ALIGN,
+            .index = i,
+            .num = 0,
+        };
+
+        vr = &vdev->vrings[i];
+
+        if (pci_read_flex(vdev->pci_fh, VPCI_C_COMMON_NUMQ, c_cap.bar, &info.num, 2)) {
+            return -EIO;
+        }
+
+        vring_init(vr, &info);
+
+        if (virtio_pci_set_selected_vq(vdev, vr->id)) {
+            puts("Failed to set selected virt-queue");
+            return -EIO;
+        }
+
+        if (virtio_pci_set_queue_size(vdev, VIRTIO_RING_SIZE)) {
+            puts("Failed to set virt-queue size");
+            return -EIO;
+        }
+
+        rc = set_pci_vq_addr(vdev, VPCI_C_OFFSET_Q_DESCLO, vr->desc);
+        rc |= set_pci_vq_addr(vdev, VPCI_C_OFFSET_Q_AVAILLO, vr->avail);
+        rc |= set_pci_vq_addr(vdev, VPCI_C_OFFSET_Q_USEDLO, vr->used);
+        if (rc) {
+            puts("Failed to configure virt-queue address");
+            return -EIO;
+        }

pc-bios/s390-ccw/virtio-pci.c line 342

Quoted 10 lines
pc-bios/s390-ccw/virtio-pci.c new file +10 -0
+
+        if (virtio_pci_set_queue_enable(vdev, true)) {
+            puts("Failed to set virt-queue enabled");
+            return -EIO;
+        }
+    }
+
+    status |= VPCI_S_FEATURES_OK | VPCI_S_DRIVER_OK;
+    return virtio_pci_set_status(vdev, status);
+}

Thomas

#22 lore raw

Re: [PATCH 07/10] pc-bios/s390-ccw: Add support for virtio-blk-pci IPL

Quoted 7 lines · Thomas Huth
Quoted 6 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Enable virt-queue PCI configuration and add routines for virtio-blk-pci devices.

---

...

Quoted 28 lines
Quoted 28 lines
pc-bios/s390-ccw/virtio-pci.c new file +11 -0
+static int virtio_pci_set_gfeatures(VDev *vdev)
+{
+    int rc;
+
+    rc = pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GFSELECT,
+                           c_cap.bar, 0);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GF,
+                            c_cap.bar, vdev->guest_features[1]);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_GFSELECT,
+                            c_cap.bar, 1);

pc-bios/s390-ccw/virtio-pci.c new file +15 -0
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GF,
+                                c_cap.bar, vdev->guest_features[0]);
+
+    if (rc) {
+        puts("Failed to set PCI feature bits");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+static int virtio_pci_get_blk_config(VDev *vdev)
+{
+    return pci_read_flex(vdev->pci_fh, d_cap.off, d_cap.bar, &vdev- 
Quoted 6 lines
Quoted 2 lines
Quoted 1 line

config.blk,

+                         sizeof(VirtioBlkConfig));

I'm a little bit surprised that there does not seem to be any endianess swapping for the config.blk data anywhere here ... isn't that config space data supposed to be in little endian?

... oh, wait, you're not negotiating VIRTIO_F_VERSION_1, are you? ... so the config space is still in big endian for legacy virtio? ... hmm, I guess it's ok for now, but in the long run, I think we should rather use VERSION_1 instead.

Thinking about this twice, could you please have a try to use VIRTIO_F_VERSION_1 right from the start? I already heard from some people that they'd rather want to get rid of legacy virtio in QEMU (e.g. for the universal binary project that includes multiple targets in a single QEMU binary), so I think it would be safer to immediately go with VERSION_1 here.

Thanks,
Thomas

#23 lore raw

Re: [PATCH 07/10] pc-bios/s390-ccw: Add support for virtio-blk-pci IPL

Quoted 43 lines · Thomas Huth
Quoted 7 lines · Thomas Huth
Quoted 6 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Enable virt-queue PCI configuration and add routines for virtio-blk-pci devices.

---

...

Quoted 34 lines
Quoted 33 lines
pc-bios/s390-ccw/virtio-pci.c new file +5 -0
+static int virtio_pci_set_gfeatures(VDev *vdev)
+{
+    int rc;
+
+    rc = pci_bswap32_write(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_GFSELECT,
+                           c_cap.bar, 0);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GF,
+                            c_cap.bar, vdev->guest_features[1]); +
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GFSELECT,
+                            c_cap.bar, 1);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GF,
+                                c_cap.bar, vdev->guest_features[0]);

pc-bios/s390-ccw/virtio-pci.c new file +12 -0
+
+    if (rc) {
+        puts("Failed to set PCI feature bits");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+static int virtio_pci_get_blk_config(VDev *vdev)
+{
+    return pci_read_flex(vdev->pci_fh, d_cap.off, d_cap.bar, &vdev- 
Quoted 1 line

config.blk,

+                         sizeof(VirtioBlkConfig));

Quoted 8 lines
Quoted 3 lines

I'm a little bit surprised that there does not seem to be any endianess swapping for the config.blk data anywhere here ... isn't that config space data supposed to be in little endian?

... oh, wait, you're not negotiating VIRTIO_F_VERSION_1, are you? ... so the config space is still in big endian for legacy virtio? ... hmm, I guess it's ok for now, but in the long run, I think we should rather use VERSION_1 instead.

Thinking about this twice, could you please have a try to use VIRTIO_F_VERSION_1 right from the start? I already heard from some people that they'd rather want to get rid of legacy virtio in QEMU (e.g. for the universal binary project that includes multiple targets in a single QEMU binary), so I think it would be safer to immediately go with VERSION_1 here.

 Thanks,
  Thomas

Hi Thomas,

I touched on this in reply to your other message, but yes, I agree that it is better to do all of these negotiations in the most compatible way right from the start.  I will fix the feature negotiations and try to avoid using anything legacy.

Thanks again for your reviews,
Jared Rossi

#24 lore raw

Re: [PATCH 07/10] pc-bios/s390-ccw: Add support for virtio-blk-pci IPL

In reply to Thomas Huth
Quoted 33 lines · Thomas Huth
Quoted 32 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Enable virt-queue PCI configuration and add routines for virtio-blk-pci devices.

---
  include/hw/s390x/ipl/qipl.h      |  10 + pc-bios/s390-ccw/virtio-pci.h    |  79 +++++++ pc-bios/s390-ccw/virtio.h        |   1 + pc-bios/s390-ccw/main.c          |  60 +++++- pc-bios/s390-ccw/virtio-blkdev.c |   3 + pc-bios/s390-ccw/virtio-pci.c    | 360 +++++++++++++++++++++++++++++++ pc-bios/s390-ccw/virtio.c        |   5 + pc-bios/s390-ccw/Makefile        |   2 +-

  8 files changed, 517 insertions(+), 3 deletions(-)

  create mode 100644 pc-bios/s390-ccw/virtio-pci.h create mode 100644 pc-bios/s390-ccw/virtio-pci.c

include/hw/s390x/ipl/qipl.h +1 -0
index 8199b839f0..5c7779a1c3 100644
@@ -22,6 +22,7 @@
     #define S390_IPL_TYPE_FCP 0x00
   #define S390_IPL_TYPE_CCW 0x02
+#define S390_IPL_TYPE_PCI 0x04
   #define S390_IPL_TYPE_PV 0x05
   #define S390_IPL_TYPE_QEMU_SCSI 0xff
   @@ -105,6 +106,14 @@ struct IplBlockQemuScsi {
   } QEMU_PACKED;

  typedef struct IplBlockQemuScsi IplBlockQemuScsi; +struct IplBlockPci {

Quoted 8 lines

include/hw/s390x/ipl/qipl.h struct IplBlockQemuScsi { · line 115

Quoted 6 lines

+    uint32_t reserved0[80];
+    uint8_t  opt;
+    uint8_t  reserved1[3];
+    uint32_t fid;

include/hw/s390x/ipl/qipl.h +2 -0
+} QEMU_PACKED;
+typedef struct IplBlockPci IplBlockPci;

Is this supposed to have the same positions for "fid" and "opt" as in the ipl_pb0_nvme structure in the kernel (arch/s390/include/uapi/asm/ipl.h)?

Yes, I think.  That was my intention anyway. Are you suggesting it shouldn't?

Quoted 157 lines
Quoted 12 lines

  union IplParameterBlock {
      struct {
          uint32_t len;

include/hw/s390x/ipl/qipl.h +1 -0
@@ -120,6 +129,7 @@ union IplParameterBlock {
               IplBlockFcp fcp;
               IPLBlockPV pv;
               IplBlockQemuScsi scsi;
+            IplBlockPci pci;
           };
       } QEMU_PACKED;
       struct {

...

Quoted 139 lines
pc-bios/s390-ccw/virtio-pci.c new file +29 -0
new file mode 100644
index 0000000000..838231d86c
@@ -0,0 +1,360 @@
+/*
+ * Functionality for virtio-pci
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "clp.h"
+#include "pci.h"
+#include "helper.h"
+#include "s390-ccw.h"
+#include "virtio.h"
+#include "bswap.h"
+#include "virtio-pci.h"
+#include "s390-time.h"
+#include <stdio.h>
+
+/* Variable offsets used for reads/writes to modern memory regions */
+VirtioPciCap c_cap; /* Common capabilities  */
+VirtioPciCap d_cap; /* Device capabilities  */
+VirtioPciCap n_cap; /* Notify capabilities  */
+uint32_t notify_mult;
+uint16_t q_notify_offset;
+
+static int virtio_pci_set_status(VDev *vdev, uint8_t status)
+{
+    int rc = pci_write_byte(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_STATUS,
+                             c_cap.bar, status); +    if (rc) {
+        puts("Failed to write virtio-pci status"); +        return -EIO;
+    }

pc-bios/s390-ccw/virtio-pci.c new file +7 -0
+
+    return 0;
+}
+
+static int virtio_pci_get_status(VDev *vdev, uint8_t *status)
+{
+    int rc = pci_read_byte(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_STATUS,
+                           c_cap.bar, status); +    if (rc) {
+        puts("Failed to read virtio-pci status"); +        return -EIO;
+    }

pc-bios/s390-ccw/virtio-pci.c new file +7 -0
+
+    return 0;
+}
+
+/*
+ * Find the position of the capability config within PCI configuration
+ * space for a given cfg type.  Return the position if found, 

otherwise 0.
+ */

pc-bios/s390-ccw/virtio-pci.c new file +6 -0
+static uint8_t find_cap_pos(uint32_t fhandle, uint8_t cfg_type)
+{
+    uint8_t next, cfg;
+    int rc;
+
+    rc = pci_read_byte(fhandle, PCI_CAPABILITY_LIST, PCI_CFGBAR, 

&next);
+    rc |= pci_read_byte(fhandle, next + 3, PCI_CFGBAR, &cfg);

pc-bios/s390-ccw/virtio-pci.c new file +15 -0
+
+    while (!rc && (cfg != cfg_type) && next) {
+        rc = pci_read_byte(fhandle, next + 1, PCI_CFGBAR, &next);
+        rc |= pci_read_byte(fhandle, next + 3, PCI_CFGBAR, &cfg);
+    }
+
+    return rc ? 0 : next;
+}
+
+static int virtio_pci_get_hfeatures(VDev *vdev, uint64_t *features)
+{
+    uint32_t feat0, feat1;
+    int rc;
+
+    rc = pci_bswap32_write(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_DFSELECT,
+                           c_cap.bar, 0);

pc-bios/s390-ccw/virtio-pci.c new file +5 -0
+
+    rc |= pci_read_bswap32(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_DF,
+                           c_cap.bar, &feat0);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_DFSELECT,
+                            c_cap.bar, 1);

pc-bios/s390-ccw/virtio-pci.c new file +20 -0
+
+    rc |= pci_read_bswap32(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_DF,
+                               c_cap.bar, &feat1);
+
+    if (rc) {
+        puts("Failed to get PCI feature bits");
+        return -EIO;
+    }
+
+    *features = (uint64_t) feat1 << 32;
+    *features |= (uint64_t) feat0;
+
+    return 0;
+}
+
+static int virtio_pci_set_gfeatures(VDev *vdev)
+{
+    int rc;
+
+    rc = pci_bswap32_write(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_GFSELECT,
+                           c_cap.bar, 0);

pc-bios/s390-ccw/virtio-pci.c new file +5 -0
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GF,
+                            c_cap.bar, vdev->guest_features[1]);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_GFSELECT,
+                            c_cap.bar, 1);

pc-bios/s390-ccw/virtio-pci.c new file +15 -0
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GF,
+                                c_cap.bar, vdev->guest_features[0]);
+
+    if (rc) {
+        puts("Failed to set PCI feature bits");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+static int virtio_pci_get_blk_config(VDev *vdev)
+{
+    return pci_read_flex(vdev->pci_fh, d_cap.off, d_cap.bar, 
Quoted 2 lines

&vdev->config.blk,
+                         sizeof(VirtioBlkConfig));

I'm a little bit surprised that there does not seem to be any endianess swapping for the config.blk data anywhere here ... isn't that config space data supposed to be in little endian?

Quoted 1 line

... oh, wait, you're not negotiating VIRTIO_F_VERSION_1, are you? ... so the config space is still in big endian for legacy virtio? ... hmm, I guess it's ok for now, but in the long run, I think we should rather use VERSION_1 instead.

I will figure out how to fix the negotiations, both for this and VIRTIO_BLK_F_BLK_SIZE as you mention below.

Quoted 146 lines
Quoted 133 lines
pc-bios/s390-ccw/virtio-pci.c new file +10 -0
+}
+
+/* virtio spec v1.3 section 4.1.2.1 */
+void virtio_pci_id2type(VDev *vdev, uint16_t device_id)
+{
+    switch (device_id) {
+    case 0x1001:
+        vdev->dev_type = VIRTIO_ID_BLOCK;
+        break;
+    case 0x1000: /* Other valid but currently unsupported virtio 

device types */
+    case 0x1004:
+    default:
+        vdev->dev_type = 0;
+    }

pc-bios/s390-ccw/virtio-pci.c new file +4 -0
+}
+
+/*
+ * Read PCI configuration space to find the offset of the Common, 

Device, and
+ * Notification memory regions within the modern memory space.
+ * Returns 0 if success, 1 if a capability could not be located, or a
+ * negative RC if the configuration read failed.
+ */

pc-bios/s390-ccw/virtio-pci.c new file +13 -0
+static int virtio_pci_read_pci_cap_config(VDev *vdev)
+{
+    uint8_t pos;
+    int rc;
+
+    /* Common capabilities */
+    pos = find_cap_pos(vdev->pci_fh, VPCI_CAP_COMMON_CFG);
+    if (!pos) {
+        puts("Failed to locate PCI common configuration");
+        return 1;
+    }
+
+    rc = pci_read_byte(vdev->pci_fh, pos + VPCI_CAP_BAR, PCI_CFGBAR, 

&c_cap.bar);
+    if (rc || pci_read_bswap32(vdev->pci_fh, pos + VPCI_CAP_OFFSET, PCI_CFGBAR,
+                               &c_cap.off)) {
+        puts("Failed to read PCI common configuration"); +        return -EIO;
+    }

pc-bios/s390-ccw/virtio-pci.c new file +9 -0
+
+    /* Device capabilities */
+    pos = find_cap_pos(vdev->pci_fh, VPCI_CAP_DEVICE_CFG);
+    if (!pos) {
+        puts("Failed to locate PCI device configuration");
+        return 1;
+    }
+
+    rc = pci_read_byte(vdev->pci_fh, pos + VPCI_CAP_BAR, PCI_CFGBAR, 

&d_cap.bar);
+    if (rc || pci_read_bswap32(vdev->pci_fh, pos + VPCI_CAP_OFFSET, PCI_CFGBAR,
+                               &d_cap.off)) {
+        puts("Failed to read PCI device configuration"); +        return -EIO;
+    }

pc-bios/s390-ccw/virtio-pci.c new file +9 -0
+
+    /* Notification capabilities */
+    pos = find_cap_pos(vdev->pci_fh, VPCI_CAP_NOTIFY_CFG);
+    if (!pos) {
+        puts("Failed to locate PCI notification configuration");
+        return 1;
+    }
+
+    rc = pci_read_byte(vdev->pci_fh, pos + VPCI_CAP_BAR, PCI_CFGBAR, 

&n_cap.bar);
+    if (rc || pci_read_bswap32(vdev->pci_fh, pos + VPCI_CAP_OFFSET, PCI_CFGBAR,
+                               &n_cap.off)) {
+        puts("Failed to read PCI notification configuration"); +        return -EIO;
+    }
+
+    rc = pci_read_bswap32(vdev->pci_fh, pos + VPCI_N_CAP_MULT, PCI_CFGBAR, &notify_mult);
+    if (rc || pci_read_bswap16(vdev->pci_fh, d_cap.off + VPCI_C_OFFSET_Q_NOFF,
+                               d_cap.bar, &q_notify_offset)) { +        puts("Failed to read notification queue configuration"); +        return -EIO;
+    }

pc-bios/s390-ccw/virtio-pci.c new file +23 -0
+
+    return 0;
+}
+
+int virtio_pci_reset(VDev *vdev)
+{
+    int rc;
+    uint8_t status = VPCI_S_RESET;
+
+    rc = virtio_pci_set_status(vdev, status);
+    rc |= virtio_pci_get_status(vdev, &status);
+
+    if (rc || status) {
+        puts("Failed to reset virtio-pci device");
+        return 1;
+    }
+
+    return 0;
+}
+
+int virtio_pci_set_selected_vq(VDev *vdev, uint16_t queue_num)
+{
+    return pci_bswap16_write(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_Q_SELECT,
+                             c_cap.bar, queue_num);

pc-bios/s390-ccw/virtio-pci.c new file +5 -0
+}
+
+int virtio_pci_set_queue_size(VDev *vdev, uint16_t queue_size)
+{
+    return pci_bswap16_write(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_Q_SIZE,
+                             c_cap.bar, queue_size);

pc-bios/s390-ccw/virtio-pci.c new file +5 -0
+}
+
+static int virtio_pci_set_queue_enable(VDev *vdev, uint16_t enabled)
+{
+    return pci_bswap16_write(vdev->pci_fh, c_cap.off + 

VPCI_C_OFFSET_Q_ENABLE,
+                             c_cap.bar, enabled);

pc-bios/s390-ccw/virtio-pci.c new file +5 -0
+}
+
+static int set_pci_vq_addr(VDev *vdev, uint64_t config_off, void *addr)
+{
+    return pci_bswap64_write(vdev->pci_fh, c_cap.off + config_off, 

c_cap.bar,
+                             (uint64_t) addr);

pc-bios/s390-ccw/virtio-pci.c new file +1 -0
+}

pc-bios/s390-ccw/virtio-pci.c line 254

Quoted 10 lines
pc-bios/s390-ccw/virtio-pci.c new file +10 -0
+
+int virtio_pci_setup(VDev *vdev)
+{
+    VRing *vr;
+    int rc;
+    uint64_t pci_features;
+    uint8_t status;
+    int i = 0;
+
+    vdev->config.blk.blk_size = 0;

For really getting blk_size, I think you should negotiate VIRTIO_BLK_F_BLK_SIZE ?

Quoted 1 line

Also, I assume the generic part of this function should rather be agnostic of virtio-block specific settings instead, so it can be used for virtio-net-pci and virtio-scsi-pci later? So the above line should likely rather go into the "case VIRTIO_ID_BLOCK" part below?

In the existing virtio_ccw_setup code, config.blk.blk_size is initialized to 0 for all device types like this, which is why I did it that way here too.

Conceptually, I agree that it makes more sense to do it only for actual block devices that care about it.  I'll check if there is any reason why it was done this way in the CCW code to begin with, but probably it can be moved it into the VIRTIO_ID_BLOCK case.

Quoted 96 lines
Quoted 82 lines

+    vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
+    vdev->cmd_vr_idx = 0;

pc-bios/s390-ccw/virtio-pci.c new file +46 -0
+
+    if (virtio_reset(vdev)) {
+        return -EIO;
+    }
+
+    status = VPCI_S_ACKNOWLEDGE;
+    rc = virtio_pci_set_status(vdev, status);
+    if (rc) {
+        puts("Virtio-pci device Failed to ACKNOWLEDGE");
+        return -EIO;
+    }
+
+    rc = virtio_pci_read_pci_cap_config(vdev);
+    if (rc) {
+        printf("Invalid PCI capabilities");
+        return -EIO;
+    }
+
+    switch (vdev->dev_type) {
+    case VIRTIO_ID_BLOCK:
+        vdev->nr_vqs = 1;
+        vdev->cmd_vr_idx = 0;
+        virtio_pci_get_blk_config(vdev);
+        break;
+    default:
+        puts("Unsupported virtio device");
+        return -ENODEV;
+    }
+
+    status |= VPCI_S_DRIVER;
+    rc = virtio_pci_set_status(vdev, status);
+    if (rc) {
+        puts("Set status failed");
+        return -EIO;
+    }
+
+    /* Feature negotiation */
+    rc = virtio_pci_get_hfeatures(vdev, &pci_features);
+    if (rc || virtio_pci_set_gfeatures(vdev)) {
+        return -EIO;
+    }
+
+    /* Configure virt-queues for pci */
+    for (i = 0; i < vdev->nr_vqs; i++) {
+        VqInfo info = {
+            .queue = (unsigned long long) virtio_get_ring_area() + 

(i * VIRTIO_RING_SIZE),
+            .align = KVM_S390_VIRTIO_RING_ALIGN,
+            .index = i,
+            .num = 0,
+        };

pc-bios/s390-ccw/virtio-pci.c new file +4 -0
+
+        vr = &vdev->vrings[i];
+
+        if (pci_read_flex(vdev->pci_fh, VPCI_C_COMMON_NUMQ, 

c_cap.bar, &info.num, 2)) {
+            return -EIO;
+        }

pc-bios/s390-ccw/virtio-pci.c new file +15 -0
+
+        vring_init(vr, &info);
+
+        if (virtio_pci_set_selected_vq(vdev, vr->id)) {
+            puts("Failed to set selected virt-queue");
+            return -EIO;
+        }
+
+        if (virtio_pci_set_queue_size(vdev, VIRTIO_RING_SIZE)) {
+            puts("Failed to set virt-queue size");
+            return -EIO;
+        }
+
+        rc = set_pci_vq_addr(vdev, VPCI_C_OFFSET_Q_DESCLO, vr->desc);
+        rc |= set_pci_vq_addr(vdev, VPCI_C_OFFSET_Q_AVAILLO, 

vr->avail);
+        rc |= set_pci_vq_addr(vdev, VPCI_C_OFFSET_Q_USEDLO, vr->used); +        if (rc) {
+            puts("Failed to configure virt-queue address"); +            return -EIO;
+        }

pc-bios/s390-ccw/virtio-pci.c line 342

Quoted 10 lines
pc-bios/s390-ccw/virtio-pci.c new file +10 -0
+
+        if (virtio_pci_set_queue_enable(vdev, true)) {
+            puts("Failed to set virt-queue enabled");
+            return -EIO;
+        }
+    }
+
+    status |= VPCI_S_FEATURES_OK | VPCI_S_DRIVER_OK;
+    return virtio_pci_set_status(vdev, status);
+}

 Thomas

#25 lore raw

Re: [PATCH 07/10] pc-bios/s390-ccw: Add support for virtio-blk-pci IPL

Quoted 45 lines · Jared Rossi
Quoted 33 lines · Thomas Huth
Quoted 32 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Enable virt-queue PCI configuration and add routines for virtio-blk-pci devices.

---
  include/hw/s390x/ipl/qipl.h      |  10 + pc-bios/s390-ccw/virtio-pci.h    |  79 +++++++ pc-bios/s390-ccw/virtio.h        |   1 + pc-bios/s390-ccw/main.c          |  60 +++++- pc-bios/s390-ccw/virtio-blkdev.c |   3 + pc-bios/s390-ccw/virtio-pci.c    | 360 +++++++++++++++++++++++++++++++ pc-bios/s390-ccw/virtio.c        |   5 + pc-bios/s390-ccw/Makefile        |   2 +-

  8 files changed, 517 insertions(+), 3 deletions(-)

  create mode 100644 pc-bios/s390-ccw/virtio-pci.h create mode 100644 pc-bios/s390-ccw/virtio-pci.c

include/hw/s390x/ipl/qipl.h +1 -0
index 8199b839f0..5c7779a1c3 100644
@@ -22,6 +22,7 @@
     #define S390_IPL_TYPE_FCP 0x00
   #define S390_IPL_TYPE_CCW 0x02
+#define S390_IPL_TYPE_PCI 0x04
   #define S390_IPL_TYPE_PV 0x05
   #define S390_IPL_TYPE_QEMU_SCSI 0xff
   @@ -105,6 +106,14 @@ struct IplBlockQemuScsi {
   } QEMU_PACKED;

  typedef struct IplBlockQemuScsi IplBlockQemuScsi; +struct IplBlockPci {

Quoted 8 lines

include/hw/s390x/ipl/qipl.h struct IplBlockQemuScsi { · line 115

Quoted 6 lines

+    uint32_t reserved0[80];
+    uint8_t  opt;
+    uint8_t  reserved1[3];
+    uint32_t fid;

include/hw/s390x/ipl/qipl.h +2 -0
+} QEMU_PACKED;
+typedef struct IplBlockPci IplBlockPci;

Is this supposed to have the same positions for "fid" and "opt" as in the ipl_pb0_nvme structure in the kernel (arch/s390/include/uapi/asm/ipl.h)?

Quoted 1 line

Yes, I think.  That was my intention anyway. Are you suggesting it shouldn't?

Having the same positions sound like a good idea. But if I'm counting right, it currently does not match:

In the code above, there are 80 * 4 = 320 bytes between the end of the loadparm[] array and the "opt" field.

In the kernel, there is the reserved2 field with 304 bytes between the end of the loadparm[] array and the "opt" field.

So either I'm counting wrong, or your reserved0 field should be decreased in size?

Thomas

#26 lore raw

Re: [PATCH 07/10] pc-bios/s390-ccw: Add support for virtio-blk-pci IPL

Quoted 51 lines · Thomas Huth
Quoted 46 lines · Jared Rossi
Quoted 34 lines · Thomas Huth
Quoted 33 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Enable virt-queue PCI configuration and add routines for virtio-blk-pci devices.

---
  include/hw/s390x/ipl/qipl.h      |  10 +
  pc-bios/s390-ccw/virtio-pci.h    |  79 +++++++
  pc-bios/s390-ccw/virtio.h        |   1 +
  pc-bios/s390-ccw/main.c          |  60 +++++-
  pc-bios/s390-ccw/virtio-blkdev.c |   3 +
  pc-bios/s390-ccw/virtio-pci.c    | 360
+++++++++++++++++++++++++++++++
  pc-bios/s390-ccw/virtio.c        |   5 +
  pc-bios/s390-ccw/Makefile        |   2 +-

  8 files changed, 517 insertions(+), 3 deletions(-)

  create mode 100644 pc-bios/s390-ccw/virtio-pci.h create mode 100644 pc-bios/s390-ccw/virtio-pci.c

include/hw/s390x/ipl/qipl.h +1 -0
index 8199b839f0..5c7779a1c3 100644
@@ -22,6 +22,7 @@
     #define S390_IPL_TYPE_FCP 0x00
   #define S390_IPL_TYPE_CCW 0x02
+#define S390_IPL_TYPE_PCI 0x04
   #define S390_IPL_TYPE_PV 0x05
   #define S390_IPL_TYPE_QEMU_SCSI 0xff
   @@ -105,6 +106,14 @@ struct IplBlockQemuScsi {
   } QEMU_PACKED;

  typedef struct IplBlockQemuScsi IplBlockQemuScsi; +struct IplBlockPci {

Quoted 8 lines

include/hw/s390x/ipl/qipl.h struct IplBlockQemuScsi { · line 115

Quoted 6 lines

+    uint32_t reserved0[80];
+    uint8_t  opt;
+    uint8_t  reserved1[3];
+    uint32_t fid;

include/hw/s390x/ipl/qipl.h +2 -0
+} QEMU_PACKED;
+typedef struct IplBlockPci IplBlockPci;

Is this supposed to have the same positions for "fid" and "opt" as in the ipl_pb0_nvme structure in the kernel (arch/s390/include/uapi/asm/ipl.h)?

Quoted 1 line

Yes, I think.  That was my intention anyway. Are you suggesting it shouldn't?

Having the same positions sound like a good idea. But if I'm counting right, it currently does not match:

Quoted 8 lines

In the code above, there are 80 * 4 = 320 bytes between the end of the loadparm[] array and the "opt" field.

In the kernel, there is the reserved2 field with 304 bytes between the end of the loadparm[] array and the "opt" field.

So either I'm counting wrong, or your reserved0 field should be decreased in size?

 Thomas

Ah, now I see.  Your are correct.  I will fix it.  Thanks for catching that!

Regards,
Jared Rossi

#27 lore raw

Re: [PATCH 07/10] pc-bios/s390-ccw: Add support for virtio-blk-pci IPL

In reply to [email protected]
Quoted 465 lines · On 12/10/25 3:54 PM, [email protected] wrote:

From: Jared Rossi <[email protected]>

Enable virt-queue PCI configuration and add routines for virtio-blk-pci devices.

---
 include/hw/s390x/ipl/qipl.h      |  10 +
 pc-bios/s390-ccw/virtio-pci.h    |  79 +++++++
 pc-bios/s390-ccw/virtio.h        |   1 +
 pc-bios/s390-ccw/main.c          |  60 +++++-
 pc-bios/s390-ccw/virtio-blkdev.c |   3 +
 pc-bios/s390-ccw/virtio-pci.c    | 360 +++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/virtio.c        |   5 +
 pc-bios/s390-ccw/Makefile        |   2 +-
 8 files changed, 517 insertions(+), 3 deletions(-)
 create mode 100644 pc-bios/s390-ccw/virtio-pci.h
 create mode 100644 pc-bios/s390-ccw/virtio-pci.c
include/hw/s390x/ipl/qipl.h +10 -0
index 8199b839f0..5c7779a1c3 100644
@@ -22,6 +22,7 @@
 
 #define S390_IPL_TYPE_FCP 0x00
 #define S390_IPL_TYPE_CCW 0x02
+#define S390_IPL_TYPE_PCI 0x04
 #define S390_IPL_TYPE_PV 0x05
 #define S390_IPL_TYPE_QEMU_SCSI 0xff
 
@@ -105,6 +106,14 @@ struct IplBlockQemuScsi {
 } QEMU_PACKED;
 typedef struct IplBlockQemuScsi IplBlockQemuScsi;
 
+struct IplBlockPci {
+    uint32_t reserved0[80];
+    uint8_t  opt;
+    uint8_t  reserved1[3];
+    uint32_t fid;
+} QEMU_PACKED;
+typedef struct IplBlockPci IplBlockPci;
+
 union IplParameterBlock {
     struct {
         uint32_t len;
@@ -120,6 +129,7 @@ union IplParameterBlock {
             IplBlockFcp fcp;
             IPLBlockPV pv;
             IplBlockQemuScsi scsi;
+            IplBlockPci pci;
         };
     } QEMU_PACKED;
     struct {
pc-bios/s390-ccw/virtio-pci.h new file +79 -0
new file mode 100644
index 0000000000..b203f37aea
@@ -0,0 +1,79 @@
+/*
+ * Definitions for virtio-pci
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef VIRTIO_PCI_H
+#define VIRTIO_PCI_H
+
+/* Common configuration */
+#define VPCI_CAP_COMMON_CFG          1
+/* Notifications */
+#define VPCI_CAP_NOTIFY_CFG          2
+/* ISR access */
+#define VPCI_CAP_ISR_CFG             3
+/* Device specific configuration */
+#define VPCI_CAP_DEVICE_CFG          4
+/* PCI configuration access */
+#define VPCI_CAP_PCI_CFG             5
+/* Additional shared memory capability */
+#define VPCI_CAP_SHARED_MEMORY_CFG   8
+/* PCI vendor data configuration */
+#define VPCI_CAP_VENDOR_CFG          9
+
+/* Offsets within capability header */
+#define VPCI_CAP_VNDR        0
+#define VPCI_CAP_NEXT        1
+#define VPCI_CAP_LEN         2
+#define VPCI_CAP_CFG_TYPE    3
+#define VPCI_CAP_BAR         4
+#define VPCI_CAP_OFFSET      8
+#define VPCI_CAP_LENGTH      12
+
+#define VPCI_N_CAP_MULT 16 /* Notify multiplier, VPCI_CAP_NOTIFY_CFG only */
+
+/* Common Area Offsets for virtio-pci queue */
+#define VPCI_C_OFFSET_DFSELECT      0
+#define VPCI_C_OFFSET_DF            4
+#define VPCI_C_OFFSET_GFSELECT      8
+#define VPCI_C_OFFSET_GF            12
+#define VPCI_C_COMMON_NUMQ          18
+#define VPCI_C_OFFSET_STATUS        20
+#define VPCI_C_OFFSET_Q_SELECT      22
+#define VPCI_C_OFFSET_Q_SIZE        24
+#define VPCI_C_OFFSET_Q_ENABLE      28
+#define VPCI_C_OFFSET_Q_NOFF        30
+#define VPCI_C_OFFSET_Q_DESCLO      32
+#define VPCI_C_OFFSET_Q_DESCHI      36
+#define VPCI_C_OFFSET_Q_AVAILLO     40
+#define VPCI_C_OFFSET_Q_AVAILHI     44
+#define VPCI_C_OFFSET_Q_USEDLO      48
+#define VPCI_C_OFFSET_Q_USEDHI      52
+
+#define VPCI_S_RESET            0
+#define VPCI_S_ACKNOWLEDGE      1
+#define VPCI_S_DRIVER           2
+#define VPCI_S_DRIVER_OK        4
+#define VPCI_S_FEATURES_OK      8
+
+#define VIRTIO_F_VERSION_1      (1 << (32 - 32)) /* Feature bit 32 */
+
+#define VIRT_Q_SIZE 16
+
+struct VirtioPciCap {
+    uint8_t bar;     /* Which PCIAS it's in */
+    uint32_t off;    /* Offset within bar */
+};
+typedef struct VirtioPciCap  VirtioPciCap;
+
+long virtio_pci_notify(uint32_t fhandle, int vq_id);
+int virtio_pci_setup(VDev *vdev);
+int virtio_pci_setup_device(void);
+int virtio_pci_reset(VDev *vdev);
+void virtio_pci_id2type(VDev *vdev, uint16_t device_id);
+
+#endif
pc-bios/s390-ccw/virtio.h +1 -0
index e747891a2c..cc68c2f8a9 100644
@@ -256,6 +256,7 @@ struct VDev {
     uint8_t scsi_dev_heads;
     bool scsi_device_selected;
     ScsiDevice selected_scsi_device;
+    uint32_t pci_fh;
     uint32_t max_transfer;
     uint32_t guest_features[2];
 };
pc-bios/s390-ccw/main.c +58 -2
index e82d60bbb7..562eeede63 100644
@@ -15,7 +15,9 @@
 #include "s390-arch.h"
 #include "s390-ccw.h"
 #include "cio.h"
+#include "clp.h"
 #include "virtio.h"
+#include "virtio-pci.h"
 #include "virtio-scsi.h"
 #include "dasd-ipl.h"
 
@@ -151,6 +153,21 @@ static bool find_subch(int dev_no)
     return false;
 }
 
+static bool find_fid(uint32_t fid)
+{
+    ClpFhListEntry entry;
+    VDev *vdev = virtio_get_device();
+
+    if (find_pci_function(fid, &entry)) {
+        return false;
+    }
+
+    vdev->pci_fh = entry.fh;
+    virtio_pci_id2type(vdev, entry.device_id);
+
+    return vdev->dev_type != 0;
+}
+
 static void menu_setup(void)
 {
     if (memcmp(loadparm_str, LOADPARM_PROMPT, LOADPARM_LEN) == 0) {
@@ -238,6 +255,9 @@ static bool find_boot_device(void)
         blk_schid.ssid = iplb.scsi.ssid & 0x3;
         found = find_subch(iplb.scsi.devno);
         break;
+     case S390_IPL_TYPE_PCI:
+        found = find_fid(iplb.pci.fid);
+        break;
     default:
         puts("Unsupported IPLB");
     }
@@ -276,7 +296,7 @@ static int virtio_setup(void)
     return ret;
 }
 
-static void ipl_boot_device(void)
+static void ipl_ccw_device(void)
 {
     switch (cutype) {
     case CU_TYPE_DASD_3990:
@@ -289,7 +309,43 @@ static void ipl_boot_device(void)
         }
         break;
     default:
-        printf("Attempting to boot from unexpected device type 0x%X\n", cutype);
+        printf("Cannot boot CCW device with cu type 0x%X\n", cutype);
+    }
+}
+
+static void ipl_pci_device(void)
+{
+    VDev *vdev = virtio_get_device();
+    vdev->is_cdrom = false;
+    vdev->scsi_device_selected = false;
+
+    if (virtio_pci_setup_device()) {
+        return;
+    }
+
+    switch (vdev->dev_type) {
+    case VIRTIO_ID_BLOCK:
+        if (virtio_setup() == 0) {
+            zipl_load();
+        }
+        break;
+    default:
+        printf("Cannot boot PCI device type 0x%X\n", vdev->dev_type);
+    }
+}
+
+static void ipl_boot_device(void)
+{
+    switch (virtio_get_device()->ipl_type) {
+    case S390_IPL_TYPE_QEMU_SCSI:
+    case S390_IPL_TYPE_CCW:
+        ipl_ccw_device();
+        break;
+    case S390_IPL_TYPE_PCI:
+        ipl_pci_device();
+        break;
+    default:
+        puts("Unrecognized IPL type!");
     }
 }
 
pc-bios/s390-ccw/virtio-blkdev.c +3 -0
index 87ab9a9513..0542b4feee 100644
@@ -13,6 +13,7 @@
 #include "virtio.h"
 #include "virtio-scsi.h"
 #include "virtio-ccw.h"
+#include "virtio-pci.h"
 
 #define VIRTIO_BLK_F_GEOMETRY   (1 << 4)
 #define VIRTIO_BLK_F_BLK_SIZE   (1 << 6)
@@ -242,6 +243,8 @@ int virtio_blk_setup_device(void)
     case S390_IPL_TYPE_QEMU_SCSI:
     case S390_IPL_TYPE_CCW:
         return virtio_ccw_setup(vdev);
+    case S390_IPL_TYPE_PCI:
+        return virtio_pci_setup(vdev);
     }
 
     return 1;
pc-bios/s390-ccw/virtio-pci.c new file +188 -0
new file mode 100644
index 0000000000..838231d86c
@@ -0,0 +1,360 @@
+/*
+ * Functionality for virtio-pci
+ *
+ * Copyright 2025 IBM Corp.
+ * Author(s): Jared Rossi <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "clp.h"
+#include "pci.h"
+#include "helper.h"
+#include "s390-ccw.h"
+#include "virtio.h"
+#include "bswap.h"
+#include "virtio-pci.h"
+#include "s390-time.h"
+#include <stdio.h>
+
+/* Variable offsets used for reads/writes to modern memory regions */
+VirtioPciCap c_cap; /* Common capabilities  */
+VirtioPciCap d_cap; /* Device capabilities  */
+VirtioPciCap n_cap; /* Notify capabilities  */
+uint32_t notify_mult;
+uint16_t q_notify_offset;
+
+static int virtio_pci_set_status(VDev *vdev, uint8_t status)
+{
+    int rc = pci_write_byte(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_STATUS,
+                             c_cap.bar, status);
+    if (rc) {
+        puts("Failed to write virtio-pci status");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+static int virtio_pci_get_status(VDev *vdev, uint8_t *status)
+{
+    int rc = pci_read_byte(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_STATUS,
+                           c_cap.bar, status);
+    if (rc) {
+        puts("Failed to read virtio-pci status");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+/*
+ * Find the position of the capability config within PCI configuration
+ * space for a given cfg type.  Return the position if found, otherwise 0.
+ */
+static uint8_t find_cap_pos(uint32_t fhandle, uint8_t cfg_type)
+{
+    uint8_t next, cfg;
+    int rc;
+
+    rc = pci_read_byte(fhandle, PCI_CAPABILITY_LIST, PCI_CFGBAR, &next);
+    rc |= pci_read_byte(fhandle, next + 3, PCI_CFGBAR, &cfg);
+
+    while (!rc && (cfg != cfg_type) && next) {
+        rc = pci_read_byte(fhandle, next + 1, PCI_CFGBAR, &next);
+        rc |= pci_read_byte(fhandle, next + 3, PCI_CFGBAR, &cfg);
+    }
+
+    return rc ? 0 : next;
+}
+
+static int virtio_pci_get_hfeatures(VDev *vdev, uint64_t *features)
+{
+    uint32_t feat0, feat1;
+    int rc;
+
+    rc = pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_DFSELECT,
+                           c_cap.bar, 0);
+
+    rc |= pci_read_bswap32(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_DF,
+                           c_cap.bar, &feat0);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_DFSELECT,
+                            c_cap.bar, 1);
+
+    rc |= pci_read_bswap32(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_DF,
+                               c_cap.bar, &feat1);
+
+    if (rc) {
+        puts("Failed to get PCI feature bits");
+        return -EIO;
+    }
+
+    *features = (uint64_t) feat1 << 32;
+    *features |= (uint64_t) feat0;
+
+    return 0;
+}
+
+static int virtio_pci_set_gfeatures(VDev *vdev)
+{
+    int rc;
+
+    rc = pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GFSELECT,
+                           c_cap.bar, 0);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GF,
+                            c_cap.bar, vdev->guest_features[1]);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GFSELECT,
+                            c_cap.bar, 1);
+
+    rc |= pci_bswap32_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_GF,
+                                c_cap.bar, vdev->guest_features[0]);
+
+    if (rc) {
+        puts("Failed to set PCI feature bits");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+static int virtio_pci_get_blk_config(VDev *vdev)
+{
+    return pci_read_flex(vdev->pci_fh, d_cap.off, d_cap.bar, &vdev->config.blk,
+                         sizeof(VirtioBlkConfig));
+}
+
+/* virtio spec v1.3 section 4.1.2.1 */
+void virtio_pci_id2type(VDev *vdev, uint16_t device_id)
+{
+    switch (device_id) {
+    case 0x1001:
+        vdev->dev_type = VIRTIO_ID_BLOCK;
+        break;
+    case 0x1000: /* Other valid but currently unsupported virtio device types */
+    case 0x1004:
+    default:
+        vdev->dev_type = 0;
+    }
+}
+
+/*
+ * Read PCI configuration space to find the offset of the Common, Device, and
+ * Notification memory regions within the modern memory space.
+ * Returns 0 if success, 1 if a capability could not be located, or a
+ * negative RC if the configuration read failed.
+ */
+static int virtio_pci_read_pci_cap_config(VDev *vdev)
+{
+    uint8_t pos;
+    int rc;
+
+    /* Common capabilities */
+    pos = find_cap_pos(vdev->pci_fh, VPCI_CAP_COMMON_CFG);
+    if (!pos) {
+        puts("Failed to locate PCI common configuration");
+        return 1;
+    }
+
+    rc = pci_read_byte(vdev->pci_fh, pos + VPCI_CAP_BAR, PCI_CFGBAR, &c_cap.bar);
+    if (rc || pci_read_bswap32(vdev->pci_fh, pos + VPCI_CAP_OFFSET, PCI_CFGBAR,
+                               &c_cap.off)) {
+        puts("Failed to read PCI common configuration");
+        return -EIO;
+    }
+
+    /* Device capabilities */
+    pos = find_cap_pos(vdev->pci_fh, VPCI_CAP_DEVICE_CFG);
+    if (!pos) {
+        puts("Failed to locate PCI device configuration");
+        return 1;
+    }
+
+    rc = pci_read_byte(vdev->pci_fh, pos + VPCI_CAP_BAR, PCI_CFGBAR, &d_cap.bar);
+    if (rc || pci_read_bswap32(vdev->pci_fh, pos + VPCI_CAP_OFFSET, PCI_CFGBAR,
+                               &d_cap.off)) {
+        puts("Failed to read PCI device configuration");
+        return -EIO;
+    }
+
+    /* Notification capabilities */
+    pos = find_cap_pos(vdev->pci_fh, VPCI_CAP_NOTIFY_CFG);
+    if (!pos) {
+        puts("Failed to locate PCI notification configuration");
+        return 1;
+    }
+

pc-bios/s390-ccw/virtio-pci.c line 198

Quoted 10 lines
pc-bios/s390-ccw/virtio-pci.c new file +10 -0
+    rc = pci_read_byte(vdev->pci_fh, pos + VPCI_CAP_BAR, PCI_CFGBAR, &n_cap.bar);
+    if (rc || pci_read_bswap32(vdev->pci_fh, pos + VPCI_CAP_OFFSET, PCI_CFGBAR,
+                               &n_cap.off)) {
+        puts("Failed to read PCI notification configuration");
+        return -EIO;
+    }
+
+    rc = pci_read_bswap32(vdev->pci_fh, pos + VPCI_N_CAP_MULT, PCI_CFGBAR, &notify_mult);
+    if (rc || pci_read_bswap16(vdev->pci_fh, d_cap.off + VPCI_C_OFFSET_Q_NOFF,
+                               d_cap.bar, &q_notify_offset)) {

Hi Jared, I think this came up in the previous version -- should q_notify_offset be taken from the common configuration structure?

Quoted 205 lines
pc-bios/s390-ccw/virtio-pci.c new file +162 -0
+        puts("Failed to read notification queue configuration");
+        return -EIO;
+    }
+
+    return 0;
+}
+
+int virtio_pci_reset(VDev *vdev)
+{
+    int rc;
+    uint8_t status = VPCI_S_RESET;
+
+    rc = virtio_pci_set_status(vdev, status);
+    rc |= virtio_pci_get_status(vdev, &status);
+
+    if (rc || status) {
+        puts("Failed to reset virtio-pci device");
+        return 1;
+    }
+
+    return 0;
+}
+
+int virtio_pci_set_selected_vq(VDev *vdev, uint16_t queue_num)
+{
+    return pci_bswap16_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_Q_SELECT,
+                             c_cap.bar, queue_num);
+}
+
+int virtio_pci_set_queue_size(VDev *vdev, uint16_t queue_size)
+{
+    return pci_bswap16_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_Q_SIZE,
+                             c_cap.bar, queue_size);
+}
+
+static int virtio_pci_set_queue_enable(VDev *vdev, uint16_t enabled)
+{
+    return pci_bswap16_write(vdev->pci_fh, c_cap.off + VPCI_C_OFFSET_Q_ENABLE,
+                             c_cap.bar, enabled);
+}
+
+static int set_pci_vq_addr(VDev *vdev, uint64_t config_off, void *addr)
+{
+    return pci_bswap64_write(vdev->pci_fh, c_cap.off + config_off, c_cap.bar,
+                             (uint64_t) addr);
+}
+
+int virtio_pci_setup(VDev *vdev)
+{
+    VRing *vr;
+    int rc;
+    uint64_t pci_features;
+    uint8_t status;
+    int i = 0;
+
+    vdev->config.blk.blk_size = 0;
+    vdev->guessed_disk_nature = VIRTIO_GDN_NONE;
+    vdev->cmd_vr_idx = 0;
+
+    if (virtio_reset(vdev)) {
+        return -EIO;
+    }
+
+    status = VPCI_S_ACKNOWLEDGE;
+    rc = virtio_pci_set_status(vdev, status);
+    if (rc) {
+        puts("Virtio-pci device Failed to ACKNOWLEDGE");
+        return -EIO;
+    }
+
+    rc = virtio_pci_read_pci_cap_config(vdev);
+    if (rc) {
+        printf("Invalid PCI capabilities");
+        return -EIO;
+    }
+
+    switch (vdev->dev_type) {
+    case VIRTIO_ID_BLOCK:
+        vdev->nr_vqs = 1;
+        vdev->cmd_vr_idx = 0;
+        virtio_pci_get_blk_config(vdev);
+        break;
+    default:
+        puts("Unsupported virtio device");
+        return -ENODEV;
+    }
+
+    status |= VPCI_S_DRIVER;
+    rc = virtio_pci_set_status(vdev, status);
+    if (rc) {
+        puts("Set status failed");
+        return -EIO;
+    }
+
+    /* Feature negotiation */
+    rc = virtio_pci_get_hfeatures(vdev, &pci_features);
+    if (rc || virtio_pci_set_gfeatures(vdev)) {
+        return -EIO;
+    }
+
+    /* Configure virt-queues for pci */
+    for (i = 0; i < vdev->nr_vqs; i++) {
+        VqInfo info = {
+            .queue = (unsigned long long) virtio_get_ring_area() + (i * VIRTIO_RING_SIZE),
+            .align = KVM_S390_VIRTIO_RING_ALIGN,
+            .index = i,
+            .num = 0,
+        };
+
+        vr = &vdev->vrings[i];
+
+        if (pci_read_flex(vdev->pci_fh, VPCI_C_COMMON_NUMQ, c_cap.bar, &info.num, 2)) {
+            return -EIO;
+        }
+
+        vring_init(vr, &info);
+
+        if (virtio_pci_set_selected_vq(vdev, vr->id)) {
+            puts("Failed to set selected virt-queue");
+            return -EIO;
+        }
+
+        if (virtio_pci_set_queue_size(vdev, VIRTIO_RING_SIZE)) {
+            puts("Failed to set virt-queue size");
+            return -EIO;
+        }
+
+        rc = set_pci_vq_addr(vdev, VPCI_C_OFFSET_Q_DESCLO, vr->desc);
+        rc |= set_pci_vq_addr(vdev, VPCI_C_OFFSET_Q_AVAILLO, vr->avail);
+        rc |= set_pci_vq_addr(vdev, VPCI_C_OFFSET_Q_USEDLO, vr->used);
+        if (rc) {
+            puts("Failed to configure virt-queue address");
+            return -EIO;
+        }
+
+        if (virtio_pci_set_queue_enable(vdev, true)) {
+            puts("Failed to set virt-queue enabled");
+            return -EIO;
+        }
+    }
+
+    status |= VPCI_S_FEATURES_OK | VPCI_S_DRIVER_OK;
+    return virtio_pci_set_status(vdev, status);
+}
+
+int virtio_pci_setup_device(void)
+{
+    VDev *vdev = virtio_get_device();
+
+    if (enable_pci_function(&vdev->pci_fh)) {
+        puts("Failed to enable PCI function");
+        return -ENODEV;
+    }
+
+    return 0;
+}
+
+long virtio_pci_notify(uint32_t fhandle, int vq_id)
+{
+    uint32_t offset = n_cap.off + notify_mult * q_notify_offset;
+    return pci_bswap16_write(fhandle, offset, n_cap.bar, (uint16_t) vq_id);
+}
pc-bios/s390-ccw/virtio.c +5 -0
index 0488b3a07e..50afb11f6a 100644
@@ -15,6 +15,7 @@
 #include "virtio.h"
 #include "virtio-scsi.h"
 #include "virtio-ccw.h"
+#include "virtio-pci.h"
 #include "bswap.h"
 #include "helper.h"
 #include "s390-time.h"
@@ -109,6 +110,8 @@ bool vring_notify(VRing *vr)
     case S390_IPL_TYPE_QEMU_SCSI:
     case S390_IPL_TYPE_CCW:
         vr->cookie = virtio_ccw_notify(vr->schid, vr->id, vr->cookie);
+    case S390_IPL_TYPE_PCI:
+        vr->cookie = virtio_pci_notify(virtio_get_device()->pci_fh, vr->id);
     }
 
     return vr->cookie >= 0;
@@ -182,6 +185,8 @@ int virtio_reset(VDev *vdev)
     case S390_IPL_TYPE_QEMU_SCSI:
     case S390_IPL_TYPE_CCW:
         return virtio_ccw_reset(vdev);
+    case S390_IPL_TYPE_PCI:
+        return virtio_pci_reset(vdev);
     default:
         return -1;
     }
pc-bios/s390-ccw/Makefile +1 -1
index a62fc9d766..3e5dfb64d5 100644
@@ -35,7 +35,7 @@ QEMU_DGFLAGS = -MMD -MP -MT $@ -MF $(@D)/$(*F).d
 
 OBJECTS = start.o main.o bootmap.o jump2ipl.o sclp.o menu.o netmain.o \
 	  virtio.o virtio-net.o virtio-scsi.o virtio-blkdev.o cio.o dasd-ipl.o \
-	  virtio-ccw.o clp.o pci.o
+	  virtio-ccw.o clp.o pci.o virtio-pci.o
 
 SLOF_DIR := $(SRC_PATH)/../../roms/SLOF
 
#28 lore raw

Re: [PATCH 07/10] pc-bios/s390-ccw: Add support for virtio-blk-pci IPL

Quoted 8 lines · Zhuoying Cai

pc-bios/s390-ccw/virtio-pci.c line 198

Quoted 4 lines · On 12/10/25 3:54 PM, [email protected] wrote:
pc-bios/s390-ccw/virtio-pci.c new file +4 -0
+
+    rc = pci_read_bswap32(vdev->pci_fh, pos + VPCI_N_CAP_MULT, PCI_CFGBAR, &notify_mult);
+    if (rc || pci_read_bswap16(vdev->pci_fh, d_cap.off + VPCI_C_OFFSET_Q_NOFF,
+                               d_cap.bar, &q_notify_offset)) {

Hi Jared, I think this came up in the previous version -- should q_notify_offset be taken from the common configuration structure?

Ah, you are correct.  I've missed fixing it.  My bad...  Thanks.

Regards,
Jared Rossi

#29 lore raw

PATCH v2 8/10 s390x: Build IPLB for virtio-pci devices

In reply to [email protected]

From: Jared Rossi <[email protected]>

Search for a corresponding S390PCIBusDevice and build an IPLB if a device has been indexed for boot but does not identify as a CCW device,

PCI devices are not yet included in boot probing (they must have a boot index).

---
 hw/s390x/ipl.h                  |  3 ++
 include/hw/s390x/s390-pci-bus.h |  2 ++
 hw/s390x/ipl.c                  | 56 ++++++++++++++++++++++++++++++---
 hw/s390x/s390-pci-bus.c         |  2 +-
 4 files changed, 57 insertions(+), 6 deletions(-)
hw/s390x/ipl.h +3 -0
index aec2244321..5396d4ed91 100644
@@ -107,6 +107,7 @@ QEMU_BUILD_BUG_MSG(offsetof(S390IPLState, iplb) & 3, "alignment of iplb wrong");
 #define S390_IPLB_MIN_PV_LEN 148
 #define S390_IPLB_MIN_CCW_LEN 200
 #define S390_IPLB_MIN_FCP_LEN 384
+#define S390_IPLB_MIN_PCI_LEN 376
 #define S390_IPLB_MIN_QEMU_SCSI_LEN 200
 
 static inline bool iplb_valid_len(IplParameterBlock *iplb)
@@ -179,6 +180,8 @@ static inline bool iplb_valid(IplParameterBlock *iplb)
         return len >= S390_IPLB_MIN_FCP_LEN;
     case S390_IPL_TYPE_CCW:
         return len >= S390_IPLB_MIN_CCW_LEN;
+    case S390_IPL_TYPE_PCI:
+        return len >= S390_IPLB_MIN_PCI_LEN;
     case S390_IPL_TYPE_QEMU_SCSI:
     default:
         return false;
include/hw/s390x/s390-pci-bus.h +2 -0
index f643e13057..6cbe26a0b8 100644
@@ -402,6 +402,8 @@ S390PCIBusDevice *s390_pci_find_dev_by_fh(S390pciState *s, uint32_t fh);
 S390PCIBusDevice *s390_pci_find_dev_by_fid(S390pciState *s, uint32_t fid);
 S390PCIBusDevice *s390_pci_find_dev_by_target(S390pciState *s,
                                               const char *target);
+S390PCIBusDevice *s390_pci_find_dev_by_pci(S390pciState *s,
+                                               PCIDevice *pci_dev);
 S390PCIBusDevice *s390_pci_find_next_avail_dev(S390pciState *s,
                                                S390PCIBusDevice *pbdev);
 void s390_pci_ism_reset(void);
hw/s390x/ipl.c +51 -5

103 lines not on this page · raw message

hw/s390x/s390-pci-bus.c +1 -1
index 52820894fa..615974851b 100644
@@ -249,7 +249,7 @@ S390PCIBusDevice *s390_pci_find_dev_by_target(S390pciState *s,
     return NULL;
 }
 
-static S390PCIBusDevice *s390_pci_find_dev_by_pci(S390pciState *s,
+S390PCIBusDevice *s390_pci_find_dev_by_pci(S390pciState *s,
                                                   PCIDevice *pci_dev)
 {
     S390PCIBusDevice *pbdev;
Signature · 1 line
2.49.0
#30 lore raw

Re: [PATCH 08/10] s390x: Build IPLB for virtio-pci devices

Quoted 41 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

Search for a corresponding S390PCIBusDevice and build an IPLB if a device has been indexed for boot but does not identify as a CCW device,

PCI devices are not yet included in boot probing (they must have a boot index).

---
  hw/s390x/ipl.h                  |  3 ++
  include/hw/s390x/s390-pci-bus.h |  2 ++
  hw/s390x/ipl.c                  | 56 ++++++++++++++++++++++++++++++---
  hw/s390x/s390-pci-bus.c         |  2 +-
  4 files changed, 57 insertions(+), 6 deletions(-)
hw/s390x/ipl.h +3 -0
index aec2244321..5396d4ed91 100644
@@ -107,6 +107,7 @@ QEMU_BUILD_BUG_MSG(offsetof(S390IPLState, iplb) & 3, "alignment of iplb wrong");
  #define S390_IPLB_MIN_PV_LEN 148
  #define S390_IPLB_MIN_CCW_LEN 200
  #define S390_IPLB_MIN_FCP_LEN 384
+#define S390_IPLB_MIN_PCI_LEN 376
  #define S390_IPLB_MIN_QEMU_SCSI_LEN 200
 
  static inline bool iplb_valid_len(IplParameterBlock *iplb)
@@ -179,6 +180,8 @@ static inline bool iplb_valid(IplParameterBlock *iplb)
          return len >= S390_IPLB_MIN_FCP_LEN;
      case S390_IPL_TYPE_CCW:
          return len >= S390_IPLB_MIN_CCW_LEN;
+    case S390_IPL_TYPE_PCI:
+        return len >= S390_IPLB_MIN_PCI_LEN;
      case S390_IPL_TYPE_QEMU_SCSI:
      default:
          return false;
include/hw/s390x/s390-pci-bus.h +0 -0
index f643e13057..6cbe26a0b8 100644

include/hw/s390x/s390-pci-bus.h S390PCIBusDevice *s390_pci_find_dev_by_fh(S390pciState *s, uint32_t fh); · line 406

Quoted 5 lines
include/hw/s390x/s390-pci-bus.h +2 -0
@@ -402,6 +402,8 @@ S390PCIBusDevice *s390_pci_find_dev_by_fh(S390pciState *s, uint32_t fh);
  S390PCIBusDevice *s390_pci_find_dev_by_fid(S390pciState *s, uint32_t fid);
  S390PCIBusDevice *s390_pci_find_dev_by_target(S390pciState *s,
                                                const char *target);
+S390PCIBusDevice *s390_pci_find_dev_by_pci(S390pciState *s,
+                                               PCIDevice *pci_dev);

Please indent according to the "(" in the previous line.

Quoted 50 lines
include/hw/s390x/s390-pci-bus.h +0 -0
  S390PCIBusDevice *s390_pci_find_next_avail_dev(S390pciState *s,
                                                 S390PCIBusDevice *pbdev);
  void s390_pci_ism_reset(void);
hw/s390x/ipl.c +10 -2
index 3843d2a850..cf423ac764 100644
@@ -22,12 +22,14 @@
  #include "hw/loader.h"
  #include "hw/qdev-properties.h"
  #include "hw/boards.h"
+#include "hw/s390x/s390-pci-bus.h"
  #include "hw/s390x/virtio-ccw.h"
  #include "hw/s390x/vfio-ccw.h"
  #include "hw/s390x/css.h"
  #include "hw/s390x/ebcdic.h"
  #include "hw/scsi/scsi.h"
  #include "hw/virtio/virtio-net.h"
+#include "hw/virtio/virtio-pci.h"
  #include "ipl.h"
  #include "qemu/error-report.h"
  #include "qemu/config-file.h"
@@ -339,7 +341,8 @@ static void s390_ipl_set_boot_menu(S390IPLState *ipl)
      ipl->qipl.boot_menu_timeout = cpu_to_be32(splash_time);
  }
 
-#define CCW_DEVTYPE_NONE        0x00
+#define S390_DEVTYPE_NONE       0x00
+
  #define CCW_DEVTYPE_VIRTIO      0x01
  #define CCW_DEVTYPE_VIRTIO_NET  0x02
  #define CCW_DEVTYPE_SCSI        0x03
@@ -348,7 +351,7 @@ static void s390_ipl_set_boot_menu(S390IPLState *ipl)
  static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype)
  {
      CcwDevice *ccw_dev = NULL;
-    int tmp_dt = CCW_DEVTYPE_NONE;
+    int tmp_dt = S390_DEVTYPE_NONE;
 
      if (dev_st) {
          VirtIONet *virtio_net_dev = (VirtIONet *)
@@ -395,6 +398,31 @@ static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype)
      return ccw_dev;
  }
 
+#define PCI_DEVTYPE_VIRTIO       0x05
+
+static S390PCIBusDevice *s390_get_pci_device(DeviceState *dev_st, int *devtype)
+{
+    S390PCIBusDevice *pbdev = NULL;

hw/s390x/ipl.c static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype) · line 415

Quoted 10 lines
hw/s390x/ipl.c +10 -0
+    int tmp_dt = S390_DEVTYPE_NONE;
+
+    if (dev_st) {
+        PCIDevice *pci_dev = (PCIDevice *)
+            object_dynamic_cast(OBJECT(qdev_get_parent_bus(dev_st)->parent),
+                                                           TYPE_PCI_DEVICE);
+        if (pci_dev) {
+            pbdev = s390_pci_find_dev_by_pci(s390_get_phb(), pci_dev);
+            if (pbdev) {
+                tmp_dt = PCI_DEVTYPE_VIRTIO;

Hmm, so this code assumes that all PCI devices are virtio devices? ... I guess that might cause trouble in case someone tries "-device nvme,bootindex=1" ...? I think you might want to do the object_dynamic_cast with TYPE_VIRTIO_PCI instead.

hw/s390x/ipl.c static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype) · line 424

Quoted 9 lines
hw/s390x/ipl.c +9 -0
+            }
+        }
+    }
+    if (devtype) {
+        *devtype = tmp_dt;
+    }
+
+    return pbdev;
+}

Thomas

#31 lore raw

PATCH v2 9/10 hw: Add "loadparm" property to PCI devices for booting on s390x

In reply to [email protected]

From: Jared Rossi <[email protected]>

The loadparm is required on s390x to pass the information to the boot loader such as which kernel should be started or whether the boot menu should be shown.

Because PCI devices do not naturally allocate space for this, the property is added on an architecture specific basis.

---
 include/hw/pci/pci_device.h |  3 +++
 hw/pci/pci.c                | 39 +++++++++++++++++++++++++++++++++++++
 hw/s390x/ipl.c              | 11 +++++++++--
 3 files changed, 51 insertions(+), 2 deletions(-)
include/hw/pci/pci_device.h +3 -0
index 88ccea5011..5cac6e1688 100644
@@ -62,6 +62,9 @@ struct PCIDevice {
     bool partially_hotplugged;
     bool enabled;
 
+    /* only for s390x */
+    char *loadparm;
+
     /* PCI config space */
     uint8_t *config;
 
hw/pci/pci.c +39 -0
index b1eba348e0..1ea0d7c54c 100644
@@ -36,6 +36,7 @@
 #include "migration/qemu-file-types.h"
 #include "migration/vmstate.h"
 #include "net/net.h"
+#include "system/arch_init.h"
 #include "system/numa.h"
 #include "system/runstate.h"
 #include "system/system.h"
@@ -2825,6 +2826,43 @@ int pci_qdev_find_device(const char *id, PCIDevice **pdev)
     return rc;
 }
 
+static char *pci_qdev_property_get_loadparm(Object *obj, Error **errp)
+{
+    return g_strdup(PCI_DEVICE(obj)->loadparm);
+}
+
+static void pci_qdev_property_set_loadparm(Object *obj, const char *value,
+                                       Error **errp)
+{
+    void *lp_str;
+
+    if (object_property_get_int(obj, "bootindex", NULL) < 0) {
+        error_setg(errp, "'loadparm' is only valid for boot devices");
+        return;
+    }
+
+    lp_str = g_malloc0(strlen(value) + 1);
+    if (!qdev_prop_sanitize_s390x_loadparm(lp_str, value, errp)) {
+        g_free(lp_str);
+        return;
+    }
+    PCI_DEVICE(obj)->loadparm = lp_str;
+}
+
+static void pci_qdev_property_add_specifics(DeviceClass *dc)
+{
+    ObjectClass *oc = OBJECT_CLASS(dc);
+
+    /* The loadparm property is only supported on s390x */
+    if (qemu_arch_available(QEMU_ARCH_S390X)) {
+        object_class_property_add_str(oc, "loadparm",
+                                      pci_qdev_property_get_loadparm,
+                                      pci_qdev_property_set_loadparm);
+        object_class_property_set_description(oc, "loadparm",
+                                              "load parameter (s390x only)");
+    }
+}
+
 MemoryRegion *pci_address_space(PCIDevice *dev)
 {
     return pci_get_bus(dev)->address_space_mem;
@@ -2843,6 +2881,7 @@ static void pci_device_class_init(ObjectClass *klass, const void *data)
     k->unrealize = pci_qdev_unrealize;
     k->bus_type = TYPE_PCI_BUS;
     device_class_set_props(k, pci_props);
+    pci_qdev_property_add_specifics(k);
     object_class_property_set_description(
         klass, "x-max-bounce-buffer-size",
         "Maximum buffer size allocated for bounce buffers used for mapped "
hw/s390x/ipl.c +9 -2
index cf423ac764..57f5a7a204 100644
@@ -460,6 +460,7 @@ static bool s390_build_iplb(DeviceState *dev_st, IplParameterBlock *iplb)
     int devtype;
     uint8_t *lp;
     g_autofree void *scsi_lp = NULL;
+    g_autofree void *pci_lp = NULL;
 
     ccw_dev = s390_get_ccw_device(dev_st, &devtype);
     if (ccw_dev) {
@@ -512,6 +513,14 @@ static bool s390_build_iplb(DeviceState *dev_st, IplParameterBlock *iplb)
 
     pbdev = s390_get_pci_device(dev_st, &devtype);
     if (pbdev) {
+        pci_lp = object_property_get_str(OBJECT(pbdev->pdev), "loadparm", NULL);
+        if (pci_lp && strlen(pci_lp) > 0) {
+            lp = pci_lp;
+        } else {
+            /* Use machine loadparm as a place holder if PCI LP is unset */
+            lp = S390_CCW_MACHINE(qdev_get_machine())->loadparm;
+        }
+
         switch (devtype) {
         case PCI_DEVTYPE_VIRTIO:
             iplb->len = S390_IPLB_MIN_PCI_LEN;
@@ -522,8 +531,6 @@ static bool s390_build_iplb(DeviceState *dev_st, IplParameterBlock *iplb)
             return false;
         }
 
-        /* Per-device loadparm not yet supported for non-ccw IPL */
-        lp = S390_CCW_MACHINE(qdev_get_machine())->loadparm;
         s390_ipl_convert_loadparm((char *)lp, iplb->loadparm);
         iplb->flags |= DIAG308_FLAGS_LP_VALID;
 
Signature · 1 line
2.49.0
#32 lore raw

Re: [PATCH 09/10] hw: Add "loadparm" property to PCI devices for booting on s390x

Quoted 71 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

The loadparm is required on s390x to pass the information to the boot loader such as which kernel should be started or whether the boot menu should be shown.

Because PCI devices do not naturally allocate space for this, the property is added on an architecture specific basis.

---
  include/hw/pci/pci_device.h |  3 +++
  hw/pci/pci.c                | 39 +++++++++++++++++++++++++++++++++++++
  hw/s390x/ipl.c              | 11 +++++++++--
  3 files changed, 51 insertions(+), 2 deletions(-)
include/hw/pci/pci_device.h +3 -0
index 88ccea5011..5cac6e1688 100644
@@ -62,6 +62,9 @@ struct PCIDevice {
      bool partially_hotplugged;
      bool enabled;
 
+    /* only for s390x */
+    char *loadparm;
+
      /* PCI config space */
      uint8_t *config;
 
hw/pci/pci.c +27 -0
index b1eba348e0..1ea0d7c54c 100644
@@ -36,6 +36,7 @@
  #include "migration/qemu-file-types.h"
  #include "migration/vmstate.h"
  #include "net/net.h"
+#include "system/arch_init.h"
  #include "system/numa.h"
  #include "system/runstate.h"
  #include "system/system.h"
@@ -2825,6 +2826,43 @@ int pci_qdev_find_device(const char *id, PCIDevice **pdev)
      return rc;
  }
 
+static char *pci_qdev_property_get_loadparm(Object *obj, Error **errp)
+{
+    return g_strdup(PCI_DEVICE(obj)->loadparm);
+}
+
+static void pci_qdev_property_set_loadparm(Object *obj, const char *value,
+                                       Error **errp)
+{
+    void *lp_str;
+
+    if (object_property_get_int(obj, "bootindex", NULL) < 0) {
+        error_setg(errp, "'loadparm' is only valid for boot devices");
+        return;
+    }
+
+    lp_str = g_malloc0(strlen(value) + 1);
+    if (!qdev_prop_sanitize_s390x_loadparm(lp_str, value, errp)) {
+        g_free(lp_str);
+        return;
+    }
+    PCI_DEVICE(obj)->loadparm = lp_str;
+}
+
+static void pci_qdev_property_add_specifics(DeviceClass *dc)
+{
+    ObjectClass *oc = OBJECT_CLASS(dc);

hw/pci/pci.c int pci_qdev_find_device(const char *id, PCIDevice **pdev) · line 2864

Quoted 10 lines
hw/pci/pci.c +10 -0
+
+    /* The loadparm property is only supported on s390x */
+    if (qemu_arch_available(QEMU_ARCH_S390X)) {
+        object_class_property_add_str(oc, "loadparm",
+                                      pci_qdev_property_get_loadparm,
+                                      pci_qdev_property_set_loadparm);
+        object_class_property_set_description(oc, "loadparm",
+                                              "load parameter (s390x only)");
+    }
+}

Adding this unconditionally to each and every PCI device is a little bit ugly ... could we please limit this to virtio-blk-pci devices for now? (or maybe check if there is a bootindex property, and only add it for devices with a bootindex property?)

Thomas

#33 lore raw

Re: [PATCH 09/10] hw: Add "loadparm" property to PCI devices for booting on s390x

Quoted 86 lines · Thomas Huth
Quoted 71 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

The loadparm is required on s390x to pass the information to the boot loader such as which kernel should be started or whether the boot menu should be shown.

Because PCI devices do not naturally allocate space for this, the property is added on an architecture specific basis.

---
  include/hw/pci/pci_device.h |  3 +++
  hw/pci/pci.c                | 39 +++++++++++++++++++++++++++++++++++++ hw/s390x/ipl.c              | 11 +++++++++--

  3 files changed, 51 insertions(+), 2 deletions(-)
include/hw/pci/pci_device.h +0 -0
index 88ccea5011..5cac6e1688 100644
@@ -62,6 +62,9 @@ struct PCIDevice {
       bool partially_hotplugged;
       bool enabled;

  +    /* only for s390x */
+    char *loadparm;
+
      /* PCI config space */
      uint8_t *config;
  diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index b1eba348e0..1ea0d7c54c 100644

hw/pci/pci.c +27 -0
@@ -36,6 +36,7 @@
   #include "migration/qemu-file-types.h"
   #include "migration/vmstate.h"
   #include "net/net.h"
+#include "system/arch_init.h"
   #include "system/numa.h"
   #include "system/runstate.h"
   #include "system/system.h"
@@ -2825,6 +2826,43 @@ int pci_qdev_find_device(const char *id, 
 PCIDevice **pdev)
       return rc;
   }
   +static char *pci_qdev_property_get_loadparm(Object *obj, Error **errp)
+{
+    return g_strdup(PCI_DEVICE(obj)->loadparm);
+}
+
+static void pci_qdev_property_set_loadparm(Object *obj, const char 
 *value,
+                                       Error **errp)
+{
+    void *lp_str;
+
+    if (object_property_get_int(obj, "bootindex", NULL) < 0) {
+        error_setg(errp, "'loadparm' is only valid for boot devices");
+        return;
+    }
+
+    lp_str = g_malloc0(strlen(value) + 1);
+    if (!qdev_prop_sanitize_s390x_loadparm(lp_str, value, errp)) {
+        g_free(lp_str);
+        return;
+    }
+    PCI_DEVICE(obj)->loadparm = lp_str;
+}
+
+static void pci_qdev_property_add_specifics(DeviceClass *dc)
+{
+    ObjectClass *oc = OBJECT_CLASS(dc);
+

hw/pci/pci.c int pci_qdev_find_device(const char *id, · line 2866

Quoted 10 lines
hw/pci/pci.c +9 -0
+    /* The loadparm property is only supported on s390x */
+    if (qemu_arch_available(QEMU_ARCH_S390X)) {
+        object_class_property_add_str(oc, "loadparm",
+ pci_qdev_property_get_loadparm,
+ pci_qdev_property_set_loadparm);
+        object_class_property_set_description(oc, "loadparm",
+                                              "load parameter (s390x 
 only)");
+    }
+}

Adding this unconditionally to each and every PCI device is a little bit ugly ... could we please limit this to virtio-blk-pci devices for now? (or maybe check if there is a bootindex property, and only add it for devices with a bootindex property?)

Quoted 2 lines

 Thomas

Maybe I'm missing some nuance, but won't the check in pci_qdev_property_set_loadparm() already enforce that this is only added to boot devices?

Hmm... on second though, I guess the problem is that a loadparm property will still be added (but not necessarily set) for all PCI devices?  I agree that is not good.  I will limit what qualifies for the property to be added in the first place.

Thanks,
Jared Rossi

#34 lore raw

Re: [PATCH 09/10] hw: Add "loadparm" property to PCI devices for booting on s390x

Quoted 88 lines · Jared Rossi
Quoted 85 lines · Thomas Huth
Quoted 70 lines · On 10/12/2025 21.54, [email protected] wrote:

From: Jared Rossi <[email protected]>

The loadparm is required on s390x to pass the information to the boot loader such as which kernel should be started or whether the boot menu should be shown.

Because PCI devices do not naturally allocate space for this, the property is added on an architecture specific basis.

---
  include/hw/pci/pci_device.h |  3 +++
  hw/pci/pci.c                | 39 +++++++++++++++++++++++++++++++++++++ hw/s390x/ipl.c              | 11 +++++++++--

  3 files changed, 51 insertions(+), 2 deletions(-)
include/hw/pci/pci_device.h +0 -0
index 88ccea5011..5cac6e1688 100644
@@ -62,6 +62,9 @@ struct PCIDevice {
       bool partially_hotplugged;
       bool enabled;

  +    /* only for s390x */
+    char *loadparm;
+
      /* PCI config space */
      uint8_t *config;
  diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index b1eba348e0..1ea0d7c54c 100644

hw/pci/pci.c +27 -0
@@ -36,6 +36,7 @@
   #include "migration/qemu-file-types.h"
   #include "migration/vmstate.h"
   #include "net/net.h"
+#include "system/arch_init.h"
   #include "system/numa.h"
   #include "system/runstate.h"
   #include "system/system.h"
@@ -2825,6 +2826,43 @@ int pci_qdev_find_device(const char *id, PCIDevice 
 **pdev)
       return rc;
   }
   +static char *pci_qdev_property_get_loadparm(Object *obj, Error **errp)
+{
+    return g_strdup(PCI_DEVICE(obj)->loadparm);
+}
+
+static void pci_qdev_property_set_loadparm(Object *obj, const char *value,
+                                       Error **errp)
+{
+    void *lp_str;
+
+    if (object_property_get_int(obj, "bootindex", NULL) < 0) {
+        error_setg(errp, "'loadparm' is only valid for boot devices");
+        return;
+    }
+
+    lp_str = g_malloc0(strlen(value) + 1);
+    if (!qdev_prop_sanitize_s390x_loadparm(lp_str, value, errp)) {
+        g_free(lp_str);
+        return;
+    }
+    PCI_DEVICE(obj)->loadparm = lp_str;
+}
+
+static void pci_qdev_property_add_specifics(DeviceClass *dc)
+{
+    ObjectClass *oc = OBJECT_CLASS(dc);
+

hw/pci/pci.c int pci_qdev_find_device(const char *id, PCIDevice · line 2865

Quoted 10 lines
hw/pci/pci.c +9 -0
+    /* The loadparm property is only supported on s390x */
+    if (qemu_arch_available(QEMU_ARCH_S390X)) {
+        object_class_property_add_str(oc, "loadparm",
+ pci_qdev_property_get_loadparm,
+ pci_qdev_property_set_loadparm);
+        object_class_property_set_description(oc, "loadparm",
+                                              "load parameter (s390x 
 only)");
+    }
+}

Adding this unconditionally to each and every PCI device is a little bit ugly ... could we please limit this to virtio-blk-pci devices for now? (or maybe check if there is a bootindex property, and only add it for devices with a bootindex property?)

Quoted 6 lines
Quoted 2 lines

 Thomas

Maybe I'm missing some nuance, but won't the check in pci_qdev_property_set_loadparm() already enforce that this is only added to boot devices?

Hmm... on second though, I guess the problem is that a loadparm property will still be added (but not necessarily set) for all PCI devices?  I agree that is not good.  I will limit what qualifies for the property to be added in the first place.

Yes, the problem is that this shows up in the user interface, e.g. if you run QEMU with "-device qemu-xhci,help" to show the properties of the qemu-xhci device. Having a "loadparm" property there could cause quite some confusion for the users.

Thomas

#35 lore raw

PATCH v2 10/10 tests/qtest: Add s390x PCI boot test to cdrom-test.c

In reply to [email protected]

From: Jared Rossi <[email protected]>

Add a rudimentary test for s390x IPL to verify that a guest may boot using virtio-blk-pci device.

---
 tests/qtest/cdrom-test.c | 7 +++++++
 1 file changed, 7 insertions(+)
tests/qtest/cdrom-test.c +7 -0
index 56e2d283a9..a65854d2bc 100644
@@ -246,6 +246,13 @@ static void add_s390x_tests(void)
                             "-drive if=none,id=d2,media=cdrom,file=",
                             test_cdboot);
     }
+    if (qtest_has_device("virtio-blk-pci")) {
+        qtest_add_data_func("cdrom/boot/pci-bus-with-bootindex",
+                            "-device virtio-scsi -device virtio-serial "
+                            "-device virtio-blk-pci,drive=d1,bootindex=1 "
+                            "-drive if=none,id=d1,media=cdrom,file=",
+                            test_cdboot);
+    }
 }
 
 int main(int argc, char **argv)
Signature · 1 line
2.49.0