Skip to contentSkip to conversation

Implement network booting directly into the s390-ccw BIOS

RFC PATCH Superseded Thomas Huth 4 participants 22 messages Jul 7, 2017 – Jul 10, 2017

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover Implement network booting directly into the s390-ccw BIOS

It's already possible to do a network boot of an s390x guest with an external netboot image based on a Linux installation, but it would be much more convenient if the s390-ccw firmware supported network booting right out of the box, without the need to assemble such an external image first.

This patch series now introduces a s390-netboot.img that can be used for network booting via DHCP and TFTP by re-using the networking stack from the SLOF firmware (see https://github.com/aik/SLOF/ for details), and adds a driver for virtio-net-ccw devices.

The code can only be built if the roms/SLOF/ submodule has been checked out, and you apply the following pending SLOF patch on top of it:

https://patchwork.ozlabs.org/patch/785384/

Then it should be possible to download a combined kernel + initrd image via TFTP, e.g. by starting QEMU with:

qemu-system-s390x ... -device virtio-net,netdev=n1,bootindex=1 \
-netdev user,id=n1,tftp=/path/to/tftp,bootfile=kernel.img

Note that this version does not support downloading via config files (i.e. pxelinux config files or .INS config files) yet. This will be added later.

v2:
- Put the network boot loader into a separate s390-netboot.img binary instead of linking it directly into the s390-ccw firmware.
- Use the SLOF sources from the roms/SLOF/ submodule instead of copying them into the pc-bios/s390-ccw folder
- Removed the .INS config file loading code for now - only support combined kernel + initrd images in this initial implementation.

Thomas Huth (8):
pc-bios/s390-ccw: Move libc functions to separate header pc-bios/s390-ccw: Move ebc2asc to sclp.c
pc-bios/s390-ccw: Move virtio-block related functions into a separate
file
pc-bios/s390-ccw: Add a write() function for stdio roms/SLOF: Update submodule to latest status pc-bios/s390-ccw: Add core files for the network bootloading program pc-bios/s390-ccw: Add virtio-net driver code pc-bios/s390-ccw: Link libnet into the netboot image and do the TFTP
load

 pc-bios/s390-ccw/Makefile      |  13 +-
 pc-bios/s390-ccw/bootmap.c     |   1 +
 pc-bios/s390-ccw/libc.h        |  43 +++++
 pc-bios/s390-ccw/main.c        |  14 +-
 pc-bios/s390-ccw/netboot.mak   |  51 ++++++
 pc-bios/s390-ccw/netmain.c     | 365 +++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/s390-ccw.h    |  31 +---
 pc-bios/s390-ccw/sclp.c        |  37 +++--
 pc-bios/s390-ccw/virtio-blk.c  | 296 +++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/virtio-net.c  | 130 +++++++++++++++
 pc-bios/s390-ccw/virtio-scsi.c |   1 +
 pc-bios/s390-ccw/virtio.c      | 283 ++------------------------------
 pc-bios/s390-ccw/virtio.h      |  17 +-
 roms/SLOF                      |   2 +-
 14 files changed, 955 insertions(+), 329 deletions(-)
 create mode 100644 pc-bios/s390-ccw/libc.h
 create mode 100644 pc-bios/s390-ccw/netboot.mak
 create mode 100644 pc-bios/s390-ccw/netmain.c
 create mode 100644 pc-bios/s390-ccw/virtio-blk.c
 create mode 100644 pc-bios/s390-ccw/virtio-net.c
Signature · 1 line
1.8.3.1
#1 lore raw

RFC PATCH v2 1/8 pc-bios/s390-ccw: Move libc functions to separate header

The upcoming netboot code will use the libc from SLOF. To be able to still use s390-ccw.h there, the libc related functions in this header have to be moved to a different location.
And while we're at it, remove the duplicate memcpy() function from sclp.c.

---
 pc-bios/s390-ccw/bootmap.c     |  1 +
 pc-bios/s390-ccw/libc.h        | 43 ++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/main.c        |  1 +
 pc-bios/s390-ccw/s390-ccw.h    | 29 ----------------------------
 pc-bios/s390-ccw/sclp.c        | 10 ++--------
 pc-bios/s390-ccw/virtio-scsi.c |  1 +
 pc-bios/s390-ccw/virtio.c      |  1 +
 7 files changed, 49 insertions(+), 37 deletions(-)
 create mode 100644 pc-bios/s390-ccw/libc.h
pc-bios/s390-ccw/bootmap.c +1 -0
index 523fa78..458d3da 100644
@@ -8,6 +8,7 @@
  * directory.
  */
 
+#include "libc.h"
 #include "s390-ccw.h"
 #include "bootmap.h"
 #include "virtio.h"
pc-bios/s390-ccw/libc.h new file +43 -0

43 lines not on this page · raw message

pc-bios/s390-ccw/main.c +1 -0
index 1cacc1b..40cba8d 100644
@@ -8,6 +8,7 @@
  * directory.
  */
 
+#include "libc.h"
 #include "s390-ccw.h"
 #include "virtio.h"
 
pc-bios/s390-ccw/s390-ccw.h +0 -29
index 2089274..43e2d42 100644
@@ -18,12 +18,6 @@ typedef unsigned short     u16;
 typedef unsigned int       u32;
 typedef unsigned long long u64;
 typedef unsigned long      ulong;
-typedef long               size_t;
-typedef int                bool;
-typedef unsigned char      uint8_t;
-typedef unsigned short     uint16_t;
-typedef unsigned int       uint32_t;
-typedef unsigned long long uint64_t;
 typedef unsigned char      __u8;
 typedef unsigned short     __u16;
 typedef unsigned int       __u32;
@@ -88,18 +82,6 @@ ulong get_second(void);
 /* bootmap.c */
 void zipl_load(void);
 
-static inline void *memset(void *s, int c, size_t n)
-{
-    int i;
-    unsigned char *p = s;
-
-    for (i = 0; i < n; i++) {
-        p[i] = c;
-    }
-
-    return s;
-}
-
 static inline void fill_hex(char *out, unsigned char val)
 {
     const char hex[] = "0123456789abcdef";
@@ -169,17 +151,6 @@ static inline void sleep(unsigned int seconds)
     }
 }
 
-static inline void *memcpy(void *s1, const void *s2, size_t n)
-{
-    uint8_t *p1 = s1;
-    const uint8_t *p2 = s2;
-
-    while (n--) {
-        p1[n] = p2[n];
-    }
-    return s1;
-}
-
 static inline void IPL_assert(bool term, const char *message)
 {
     if (!term) {
pc-bios/s390-ccw/sclp.c +2 -8
index a1639ba..aa1c862 100644
@@ -8,6 +8,7 @@
  * directory.
  */
 
+#include "libc.h"
 #include "s390-ccw.h"
 #include "sclp.h"
 
@@ -59,13 +60,6 @@ static int _strlen(const char *str)
     return i;
 }
 
-static void _memcpy(char *dest, const char *src, int len)
-{
-    int i;
-    for (i = 0; i < len; i++)
-        dest[i] = src[i];
-}
-
 void sclp_print(const char *str)
 {
     int len = _strlen(str);
@@ -76,7 +70,7 @@ void sclp_print(const char *str)
     sccb->ebh.length = sizeof(EventBufferHeader) + len;
     sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
     sccb->ebh.flags = 0;
-    _memcpy(sccb->data, str, len);
+    memcpy(sccb->data, str, len);
 
     sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
 }
pc-bios/s390-ccw/virtio-scsi.c +1 -0
index f61ecf0..c92f5d3 100644
@@ -9,6 +9,7 @@
  * directory.
  */
 
+#include "libc.h"
 #include "s390-ccw.h"
 #include "virtio.h"
 #include "scsi.h"
pc-bios/s390-ccw/virtio.c +1 -0
index 6ee93d5..8768331 100644
@@ -8,6 +8,7 @@
  * directory.
  */
 
+#include "libc.h"
 #include "s390-ccw.h"
 #include "virtio.h"
 #include "virtio-scsi.h"
Signature · 1 line
1.8.3.1
#6 lore raw

RFC PATCH v2 2/8 pc-bios/s390-ccw: Move ebc2asc to sclp.c

In reply to Thomas Huth

We will later need this array in a file that we will link to the netboot code, too. Since there is some ebcdic convertion done in sclp_get_loadparm_ascii(), the sclp.c file seems to be a good candidate.

---
 pc-bios/s390-ccw/main.c | 11 -----------
 pc-bios/s390-ccw/sclp.c | 11 +++++++++++
 2 files changed, 11 insertions(+), 11 deletions(-)
pc-bios/s390-ccw/main.c +0 -11
index 40cba8d..0580eac 100644
@@ -17,17 +17,6 @@ static SubChannelId blk_schid = { .one = 1 };
 IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE)));
 static char loadparm[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 
-const unsigned char ebc2asc[256] =
-      /* 0123456789abcdef0123456789abcdef */
-        "................................" /* 1F */
-        "................................" /* 3F */
-        " ...........<(+|&.........!$*);." /* 5F first.chr.here.is.real.space */
-        "-/.........,%_>?.........`:#@'=\""/* 7F */
-        ".abcdefghi.......jklmnopqr......" /* 9F */
-        "..stuvwxyz......................" /* BF */
-        ".ABCDEFGHI.......JKLMNOPQR......" /* DF */
-        "..STUVWXYZ......0123456789......";/* FF */
-
 /*
  * Priniciples of Operations (SA22-7832-09) chapter 17 requires that
  * a subsystem-identification is at 184-187 and bytes 188-191 are zero
pc-bios/s390-ccw/sclp.c +11 -0
index aa1c862..2ee204a 100644
@@ -14,6 +14,17 @@
 
 static char _sccb[PAGE_SIZE] __attribute__((__aligned__(4096)));
 
+const unsigned char ebc2asc[256] =
+      /* 0123456789abcdef0123456789abcdef */
+        "................................" /* 1F */
+        "................................" /* 3F */
+        " ...........<(+|&.........!$*);." /* 5F first.chr.here.is.real.space */
+        "-/.........,%_>?.........`:#@'=\""/* 7F */
+        ".abcdefghi.......jklmnopqr......" /* 9F */
+        "..stuvwxyz......................" /* BF */
+        ".ABCDEFGHI.......JKLMNOPQR......" /* DF */
+        "..STUVWXYZ......0123456789......";/* FF */
+
 /* Perform service call. Return 0 on success, non-zero otherwise. */
 static int sclp_service_call(unsigned int command, void *sccb)
 {
Signature · 1 line
1.8.3.1
#9 lore raw

RFC PATCH v2 3/8 pc-bios/s390-ccw: Move virtio-block related functions into a separate file

In reply to Thomas Huth

The netboot code is going to link against the code from virtio.c, too, so we've got to move the virtio-block related code out of the way.

---
 pc-bios/s390-ccw/Makefile     |   2 +-
 pc-bios/s390-ccw/main.c       |   2 +-
 pc-bios/s390-ccw/s390-ccw.h   |   2 +-
 pc-bios/s390-ccw/virtio-blk.c | 296 ++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/virtio.c     | 273 +-------------------------------------
 pc-bios/s390-ccw/virtio.h     |   4 +
 6 files changed, 307 insertions(+), 272 deletions(-)
 create mode 100644 pc-bios/s390-ccw/virtio-blk.c
pc-bios/s390-ccw/Makefile +1 -1
index fb88c13..4abe7d3 100644
@@ -9,7 +9,7 @@ $(call set-vpath, $(SRC_PATH)/pc-bios/s390-ccw)
 
 .PHONY : all clean build-all
 
-OBJECTS = start.o main.o bootmap.o sclp.o virtio.o virtio-scsi.o
+OBJECTS = start.o main.o bootmap.o sclp.o virtio.o virtio-scsi.o virtio-blk.o
 QEMU_CFLAGS := $(filter -W%, $(QEMU_CFLAGS))
 QEMU_CFLAGS += -ffreestanding -fno-delete-null-pointer-checks -msoft-float
 QEMU_CFLAGS += -march=z900 -fPIE -fno-strict-aliasing
pc-bios/s390-ccw/main.c +1 -1
index 0580eac..401e9db 100644
@@ -144,7 +144,7 @@ static void virtio_setup(void)
         sclp_print("Network boot device detected\n");
         vdev->netboot_start_addr = iplb.ccw.netboot_start_addr;
     } else {
-        virtio_setup_device(blk_schid);
+        virtio_blk_setup_device(blk_schid);
 
         IPL_assert(virtio_ipl_disk_is_valid(), "No valid IPL device detected");
     }
pc-bios/s390-ccw/s390-ccw.h +1 -1
index 43e2d42..6fdc858 100644
@@ -74,7 +74,7 @@ void sclp_get_loadparm_ascii(char *loadparm);
 unsigned long virtio_load_direct(ulong rec_list1, ulong rec_list2,
                                  ulong subchan_id, void *load_addr);
 bool virtio_is_supported(SubChannelId schid);
-void virtio_setup_device(SubChannelId schid);
+void virtio_blk_setup_device(SubChannelId schid);
 int virtio_read(ulong sector, void *load_addr);
 int enable_mss_facility(void);
 ulong get_second(void);
pc-bios/s390-ccw/virtio-blk.c new file +296 -0

296 lines not on this page · raw message

pc-bios/s390-ccw/virtio.c +4 -269

303 lines not on this page · raw message

pc-bios/s390-ccw/virtio.h +4 -0
index 1eaf865..d743919 100644
@@ -291,6 +291,10 @@ struct VirtioCmd {
 };
 typedef struct VirtioCmd VirtioCmd;
 
+int drain_irqs(SubChannelId schid);
+void vring_send_buf(VRing *vr, void *p, int len, int flags);
+int vring_wait_reply(void);
 int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd);
+void virtio_setup_ccw(VDev *vdev);
 
 #endif /* VIRTIO_H */
Signature · 1 line
1.8.3.1
#13 lore raw

RFC PATCH v2 4/8 pc-bios/s390-ccw: Add a write() function for stdio

In reply to Thomas Huth

The stdio functions from the SLOF libc need a write() function for printing text to stdout/stderr. Let's implement this function by refactoring the code from sclp_print().

---
 pc-bios/s390-ccw/sclp.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
pc-bios/s390-ccw/sclp.c +14 -2
index 2ee204a..f6466db 100644
@@ -12,6 +12,8 @@
 #include "s390-ccw.h"
 #include "sclp.h"
 
+long write(int fd, const void *buf, size_t len);
+
 static char _sccb[PAGE_SIZE] __attribute__((__aligned__(4096)));
 
 const unsigned char ebc2asc[256] =
@@ -71,11 +73,14 @@ static int _strlen(const char *str)
     return i;
 }
 
-void sclp_print(const char *str)
+long write(int fd, const void *str, size_t len)
 {
-    int len = _strlen(str);
     WriteEventData *sccb = (void *)_sccb;
 
+    if (fd != 1 && fd != 2) {
+        return -EIO;
+    }
+
     sccb->h.length = sizeof(WriteEventData) + len;
     sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
     sccb->ebh.length = sizeof(EventBufferHeader) + len;
@@ -84,6 +89,13 @@ void sclp_print(const char *str)
     memcpy(sccb->data, str, len);
 
     sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
+
+    return len;
+}
+
+void sclp_print(const char *str)
+{
+    write(1, str, _strlen(str));
 }
 
 void sclp_get_loadparm_ascii(char *loadparm)
Signature · 1 line
1.8.3.1
#15 lore raw

RFC PATCH v2 5/8 roms/SLOF: Update submodule to latest status

In reply to Thomas Huth

We need the latest fixes for building the libc and libnet of SLOF for the s390-ccw network bootloader firmware.

---
 roms/SLOF | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
roms/SLOF +1 -1
index 66d250e..64215f4 160000
@@ -1 +1 @@
-Subproject commit 66d250ef0fd06bb88b7399b9563b5008201f2d63
+Subproject commit 64215f41aafd182e51947dc5d544e5ebaab744ca
Signature · 1 line
1.8.3.1
#16 lore raw

RFC PATCH v2 6/8 pc-bios/s390-ccw: Add core files for the network bootloading program

In reply to Thomas Huth

This is just a preparation for the next steps: Add a makefile and a stripped down copy of pc-bios/s390-ccw/main.c as a basis for the network bootloader program, linked against the libc from SLOF already (which we will need for SLOF's libnet). The networking code is not included yet.

---
 pc-bios/s390-ccw/Makefile    |  11 +++-
 pc-bios/s390-ccw/netboot.mak |  41 +++++++++++++
 pc-bios/s390-ccw/netmain.c   | 140 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 190 insertions(+), 2 deletions(-)
 create mode 100644 pc-bios/s390-ccw/netboot.mak
 create mode 100644 pc-bios/s390-ccw/netmain.c
pc-bios/s390-ccw/Makefile +9 -2
index 4abe7d3..4e11026 100644
@@ -16,7 +16,7 @@ QEMU_CFLAGS += -march=z900 -fPIE -fno-strict-aliasing
 QEMU_CFLAGS += $(call cc-option, $(QEMU_CFLAGS), -fno-stack-protector)
 LDFLAGS += -Wl,-pie -nostdlib
 
-build-all: s390-ccw.img
+build-all: s390-ccw.img s390-netboot.img
 
 s390-ccw.elf: $(OBJECTS)
 	$(call quiet-command,$(CC) $(LDFLAGS) -o $@ $(OBJECTS),"BUILD","$(TARGET_DIR)$@")
@@ -28,5 +28,12 @@ s390-ccw.img: s390-ccw.elf
 
 $(OBJECTS): Makefile
 
+ifneq ($(wildcard $(SRC_PATH)/roms/SLOF/lib/libnet),)
+include $(SRC_PATH)/pc-bios/s390-ccw/netboot.mak
+else
+s390-netboot.img:
+	@echo "s390-netboot.img not built since roms/SLOF/ is not available."
+endif
+
 clean:
-	rm -f *.o *.d *.img *.elf *~
+	$(RM) *.o *.d *.img *.elf *~ *.a
pc-bios/s390-ccw/netboot.mak new file +41 -0

41 lines not on this page · raw message

pc-bios/s390-ccw/netmain.c new file +140 -0

140 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#17 lore raw

RFC PATCH v2 7/8 pc-bios/s390-ccw: Add virtio-net driver code

In reply to Thomas Huth

The driver provides the recv() and send() functions which will be required by SLOF's libnet code for receiving and sending packets.

---
 pc-bios/s390-ccw/netboot.mak  |   2 +-
 pc-bios/s390-ccw/virtio-net.c | 130 ++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/virtio.c     |   9 ++-
 pc-bios/s390-ccw/virtio.h     |  13 ++++-
 4 files changed, 149 insertions(+), 5 deletions(-)
 create mode 100644 pc-bios/s390-ccw/virtio-net.c
pc-bios/s390-ccw/netboot.mak +1 -1
index 1eaa0d8..dd9119a 100644
@@ -1,7 +1,7 @@
 
 SLOF_DIR := $(SRC_PATH)/roms/SLOF
 
-NETOBJS := start.o sclp.o virtio.o netmain.o sbrk.o libc.a
+NETOBJS := start.o sclp.o virtio.o virtio-net.o netmain.o sbrk.o libc.a
 
 LIBC_INC := -I$(SLOF_DIR)/lib/libc/include
 LIBNET_INC := -I$(SLOF_DIR)/lib/libnet
pc-bios/s390-ccw/virtio-net.c new file +130 -0

130 lines not on this page · raw message

pc-bios/s390-ccw/virtio.c +7 -2
index 09ab291..729d926 100644
@@ -149,7 +149,7 @@ static void vring_init(VRing *vr, VqInfo *info)
     debug_print_addr("init vr", vr);
 }
 
-static bool vring_notify(VRing *vr)
+bool vring_notify(VRing *vr)
 {
     vr->cookie = virtio_notify(vr->schid, vr->id, vr->cookie);
     return vr->cookie >= 0;
@@ -188,7 +188,7 @@ ulong get_second(void)
     return (get_clock() >> 12) / 1000000;
 }
 
-static int vr_poll(VRing *vr)
+int vr_poll(VRing *vr)
 {
     if (vr->used->idx == vr->used_idx) {
         vring_notify(vr);
@@ -261,6 +261,11 @@ void virtio_setup_ccw(VDev *vdev)
     run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0);
 
     switch (vdev->senseid.cu_model) {
+    case VIRTIO_ID_NET:
+        vdev->nr_vqs = 3;
+        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;
pc-bios/s390-ccw/virtio.h +11 -2
index d743919..d6af022 100644
@@ -11,8 +11,6 @@
 #ifndef VIRTIO_H
 #define VIRTIO_H
 
-#include "s390-ccw.h"
-
 /* Status byte for guest to report progress, and synchronize features. */
 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
 #define VIRTIO_CONFIG_S_ACKNOWLEDGE     1
@@ -254,6 +252,12 @@ struct ScsiDevice {
 };
 typedef struct ScsiDevice ScsiDevice;
 
+struct VirtioNetConfig {
+    uint8_t mac[6];
+    uint16_t status;
+};
+typedef struct VirtioNetConfig VirtioNetConfig;
+
 struct VDev {
     int nr_vqs;
     VRing *vrings;
@@ -266,6 +270,7 @@ struct VDev {
     union {
         VirtioBlkConfig blk;
         VirtioScsiConfig scsi;
+        VirtioNetConfig net;
     } config;
     ScsiDevice *scsi_device;
     bool is_cdrom;
@@ -291,10 +296,14 @@ struct VirtioCmd {
 };
 typedef struct VirtioCmd VirtioCmd;
 
+bool vring_notify(VRing *vr);
 int drain_irqs(SubChannelId schid);
 void vring_send_buf(VRing *vr, void *p, int len, int flags);
+int vr_poll(VRing *vr);
 int vring_wait_reply(void);
 int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd);
 void virtio_setup_ccw(VDev *vdev);
 
+int virtio_net_init(void *mac_addr);
+
 #endif /* VIRTIO_H */
Signature · 1 line
1.8.3.1
#21 lore raw

RFC PATCH v2 8/8 pc-bios/s390-ccw: Link libnet into the netboot image and do the TFTP load

In reply to Thomas Huth

Most of the code has been taken from SLOF's netload.c file. Now we can finally load an image via TFTP and execute the downloaded kernel.

---
 pc-bios/s390-ccw/netboot.mak |  12 ++-
 pc-bios/s390-ccw/netmain.c   | 225 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 236 insertions(+), 1 deletion(-)
pc-bios/s390-ccw/netboot.mak +11 -1
index dd9119a..8c54b05 100644
@@ -1,7 +1,7 @@
 
 SLOF_DIR := $(SRC_PATH)/roms/SLOF
 
-NETOBJS := start.o sclp.o virtio.o virtio-net.o netmain.o sbrk.o libc.a
+NETOBJS := start.o sclp.o virtio.o virtio-net.o netmain.o sbrk.o libnet.a libc.a
 
 LIBC_INC := -I$(SLOF_DIR)/lib/libc/include
 LIBNET_INC := -I$(SLOF_DIR)/lib/libnet
@@ -39,3 +39,13 @@ libc.a: $(LIBCOBJS)
 
 sbrk.o: $(SLOF_DIR)/slof/sbrk.c
 	$(call quiet-command,$(CC) $(QEMU_CFLAGS) $(LIBC_INC) -c -o $@ $<,"CC","$(TARGET_DIR)$@")
+
+LIBNETOBJS := args.o dhcp.o dns.o icmpv6.o ipv6.o tcp.o udp.o bootp.o \
+	      dhcpv6.o ethernet.o ipv4.o ndp.o tftp.o
+LIBNETCFLAGS :=  $(QEMU_CFLAGS) $(LIBC_INC) $(LIBNET_INC)
+
+%.o : $(SLOF_DIR)/lib/libnet/%.c
+	$(call quiet-command,$(CC) $(LIBNETCFLAGS) -c -o $@ $<,"CC","$(TARGET_DIR)$@")
+
+libnet.a: $(LIBNETOBJS)
+	$(call quiet-command,$(AR) -rc $@ $^,"AR","$(TARGET_DIR)$@")
pc-bios/s390-ccw/netmain.c +225 -0

264 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#18 lore raw

Re: [Qemu-devel] [RFC PATCH v2 7/8] pc-bios/s390-ccw: Add virtio-net driver code

In reply to Thomas Huth
Quoted 3 lines · On Fri, 7 Jul 2017 12:27:03 +0200 Thomas Huth <[email protected]> wrote:

The driver provides the recv() and send() functions which will be required by SLOF's libnet code for receiving and sending packets.

No thorough review yet, only some things I noticed.

Quoted 59 lines
---
 pc-bios/s390-ccw/netboot.mak  |   2 +-
 pc-bios/s390-ccw/virtio-net.c | 130 ++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/virtio.c     |   9 ++-
 pc-bios/s390-ccw/virtio.h     |  13 ++++-
 4 files changed, 149 insertions(+), 5 deletions(-)
 create mode 100644 pc-bios/s390-ccw/virtio-net.c
pc-bios/s390-ccw/virtio-net.c new file +42 -0
new file mode 100644
index 0000000..290b018
@@ -0,0 +1,130 @@
+/*
+ * Virtio-net driver for the s390-ccw firmware
+ *
+ * Copyright 2017 Thomas Huth, Red Hat Inc.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ */
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <ethernet.h>
+#include "s390-ccw.h"
+#include "virtio.h"
+
+#ifndef DEBUG_VIRTIO_NET
+#define DEBUG_VIRTIO_NET 0
+#endif
+
+#define VQ_RX 0         /* Receive queue */
+#define VQ_TX 1         /* Transmit queue */
+
+struct VirtioNetHdr {
+    uint8_t flags;
+    uint8_t gso_type;
+    uint16_t hdr_len;
+    uint16_t gso_size;
+    uint16_t csum_start;
+    uint16_t csum_offset;
+    /*uint16_t num_buffers;*/ /* Only with VIRTIO_NET_F_MRG_RXBUF or VIRTIO1 */
+};
+typedef struct VirtioNetHdr VirtioNetHdr;
+
+static uint16_t rx_last_idx;  /* Last index in receive queue "used" ring */
+

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

Quoted 10 lines
pc-bios/s390-ccw/virtio-net.c new file +10 -0
+int virtio_net_init(void *mac_addr)
+{
+    VDev *vdev = virtio_get_device();
+    VRing *rxvq = &vdev->vrings[VQ_RX];
+    void *buf;
+    int i;
+
+    virtio_setup_ccw(vdev);
+
+    memcpy(mac_addr, vdev->config.net.mac, ETH_ALEN);

The mac address in the config space is only valid if the mac feature flag has been negotiated (although the field always exists).

Quoted 19 lines
pc-bios/s390-ccw/virtio-net.c new file +19 -0
+
+    for (i = 0; i < 64; i++) {
+        buf = malloc(ETH_MTU_SIZE + sizeof(VirtioNetHdr));
+        IPL_assert(buf != NULL, "Can not allocate memory for receive buffers");
+        vring_send_buf(rxvq, buf, ETH_MTU_SIZE + sizeof(VirtioNetHdr),
+                       VRING_DESC_F_WRITE);
+    }
+    vring_notify(rxvq);
+
+    return 0;
+}
+
+int send(int fd, const void *buf, int len, int flags)
+{
+    VirtioNetHdr tx_hdr;
+    VDev *vdev = virtio_get_device();
+    VRing *txvq = &vdev->vrings[VQ_TX];
+
+    /* Set up header - we do not use anything special, so simply clear it */

pc-bios/s390-ccw/virtio-net.c line 81

Quoted 10 lines
pc-bios/s390-ccw/virtio-net.c new file +10 -0
+    memset(&tx_hdr, 0, sizeof(tx_hdr));
+
+    vring_send_buf(txvq, &tx_hdr, sizeof(tx_hdr), VRING_DESC_F_NEXT);
+    vring_send_buf(txvq, (void *)buf, len, VRING_HIDDEN_IS_CHAIN);
+    while (!vr_poll(txvq)) {
+        yield();
+    }
+    if (drain_irqs(txvq->schid)) {
+        puts("send: drain irqs failed");
+        return -1;

Does the caller try again later? drain_irqs() errors may be transient.

Quoted 72 lines
pc-bios/s390-ccw/virtio-net.c new file +49 -0
+    }
+
+    return len;
+}
+
+int recv(int fd, void *buf, int maxlen, int flags)
+{
+    VDev *vdev = virtio_get_device();
+    VRing *rxvq = &vdev->vrings[VQ_RX];
+    int len, id;
+    uint8_t *pkt;
+
+    if (rx_last_idx == rxvq->used->idx) {
+        return 0;
+    }
+
+    len = rxvq->used->ring[rx_last_idx % rxvq->num].len - sizeof(VirtioNetHdr);
+    if (len > maxlen) {
+        puts("virtio-net: Receive buffer too small");
+        len = maxlen;
+    }
+    id = rxvq->used->ring[rx_last_idx % rxvq->num].id % rxvq->num;
+    pkt = (uint8_t *)(rxvq->desc[id].addr + sizeof(VirtioNetHdr));
+
+#if DEBUG_VIRTIO_NET   /* Dump packet */
+    int i;
+    printf("\nbuf %p: len=%i\n", (void *)rxvq->desc[id].addr, len);
+    for (i = 0; i < 64; i++) {
+        printf(" %02x", pkt[i]);
+        if ((i % 16) == 15) {
+            printf("\n");
+        }
+    }
+    printf("\n");
+#endif
+
+    /* Copy data to destination buffer */
+    memcpy(buf, pkt, len);
+
+    /* Mark buffer as available to the host again */
+    rxvq->avail->ring[rxvq->avail->idx % rxvq->num] = id;
+    rxvq->avail->idx = rxvq->avail->idx + 1;
+    vring_notify(rxvq);
+
+    /* Move index to next entry */
+    rx_last_idx = rx_last_idx + 1;
+
+    return len;
+}
pc-bios/s390-ccw/virtio.c +2 -2
index 09ab291..729d926 100644
@@ -149,7 +149,7 @@ static void vring_init(VRing *vr, VqInfo *info)
     debug_print_addr("init vr", vr);
 }
 
-static bool vring_notify(VRing *vr)
+bool vring_notify(VRing *vr)
 {
     vr->cookie = virtio_notify(vr->schid, vr->id, vr->cookie);
     return vr->cookie >= 0;
@@ -188,7 +188,7 @@ ulong get_second(void)
     return (get_clock() >> 12) / 1000000;
 }
 
-static int vr_poll(VRing *vr)
+int vr_poll(VRing *vr)
 {
     if (vr->used->idx == vr->used_idx) {
         vring_notify(vr);

pc-bios/s390-ccw/virtio.c void virtio_setup_ccw(VDev *vdev) · line 265

Quoted 5 lines
pc-bios/s390-ccw/virtio.c +2 -0
@@ -261,6 +261,11 @@ void virtio_setup_ccw(VDev *vdev)
     run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0);
 
     switch (vdev->senseid.cu_model) {
+    case VIRTIO_ID_NET:
+        vdev->nr_vqs = 3;

I think this should be 2, as you don't negotiate for the control vq.

Quoted 20 lines
pc-bios/s390-ccw/virtio.c +3 -0
+        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;
pc-bios/s390-ccw/virtio.h +0 -2
index d743919..d6af022 100644
@@ -11,8 +11,6 @@
 #ifndef VIRTIO_H
 #define VIRTIO_H
 
-#include "s390-ccw.h"
-
 /* Status byte for guest to report progress, and synchronize features. */
 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
 #define VIRTIO_CONFIG_S_ACKNOWLEDGE     1

pc-bios/s390-ccw/virtio.h struct ScsiDevice { · line 257

Quoted 6 lines
pc-bios/s390-ccw/virtio.h +3 -0
@@ -254,6 +252,12 @@ struct ScsiDevice {
 };
 typedef struct ScsiDevice ScsiDevice;
 
+struct VirtioNetConfig {
+    uint8_t mac[6];
+    uint16_t status;

I don't think status exists if you don't negotiate the respective feature bit.

Quoted 29 lines
pc-bios/s390-ccw/virtio.h +8 -0
+};
+typedef struct VirtioNetConfig VirtioNetConfig;
+
 struct VDev {
     int nr_vqs;
     VRing *vrings;
@@ -266,6 +270,7 @@ struct VDev {
     union {
         VirtioBlkConfig blk;
         VirtioScsiConfig scsi;
+        VirtioNetConfig net;
     } config;
     ScsiDevice *scsi_device;
     bool is_cdrom;
@@ -291,10 +296,14 @@ struct VirtioCmd {
 };
 typedef struct VirtioCmd VirtioCmd;
 
+bool vring_notify(VRing *vr);
 int drain_irqs(SubChannelId schid);
 void vring_send_buf(VRing *vr, void *p, int len, int flags);
+int vr_poll(VRing *vr);
 int vring_wait_reply(void);
 int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd);
 void virtio_setup_ccw(VDev *vdev);
 
+int virtio_net_init(void *mac_addr);
+
 #endif /* VIRTIO_H */
Acked-by on v2 2/8
#7 lore raw

Re: [Qemu-devel] [RFC PATCH v2 2/8] pc-bios/s390-ccw: Move ebc2asc to sclp.c

In reply to Thomas Huth
Quoted 4 lines · On Fri, 7 Jul 2017 12:26:58 +0200 Thomas Huth <[email protected]> wrote:

We will later need this array in a file that we will link to the netboot code, too. Since there is some ebcdic convertion done in sclp_get_loadparm_ascii(), the sclp.c file seems to be a good candidate.

Interacting with sclp is probably the only place we'll need to deal with ebcdic in the ccw bios, so this seems sensible.

Quoted 6 lines
---
 pc-bios/s390-ccw/main.c | 11 -----------
 pc-bios/s390-ccw/sclp.c | 11 +++++++++++
 2 files changed, 11 insertions(+), 11 deletions(-)
#10 lore raw

Re: [Qemu-devel] [RFC PATCH v2 3/8] pc-bios/s390-ccw: Move virtio-block related functions into a separate file

In reply to Thomas Huth
Quoted 21 lines · On Fri, 7 Jul 2017 12:26:59 +0200 Thomas Huth <[email protected]> wrote:

The netboot code is going to link against the code from virtio.c, too, so we've got to move the virtio-block related code out of the way.

---
 pc-bios/s390-ccw/Makefile     |   2 +-
 pc-bios/s390-ccw/main.c       |   2 +-
 pc-bios/s390-ccw/s390-ccw.h   |   2 +-
 pc-bios/s390-ccw/virtio-blk.c | 296 ++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/virtio.c     | 273 +-------------------------------------
 pc-bios/s390-ccw/virtio.h     |   4 +
 6 files changed, 307 insertions(+), 272 deletions(-)
 create mode 100644 pc-bios/s390-ccw/virtio-blk.c
pc-bios/s390-ccw/virtio-blk.c new file +0 -0
new file mode 100644
index 0000000..6cb77bc

pc-bios/s390-ccw/virtio-blk.c line 4

Quoted 4 lines
pc-bios/s390-ccw/virtio-blk.c new file +4 -0
@@ -0,0 +1,296 @@
+/*
+ * Virtio driver bits
+ *
+ * Copyright (c) 2013 Alexander Graf <[email protected]>

The original code carries the same copyright notice, but there's a lot of IBM code in there. Just saying.

Quoted 43 lines
pc-bios/s390-ccw/virtio-blk.c new file +43 -0
+ *
+ * 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 "libc.h"
+#include "s390-ccw.h"
+#include "virtio.h"
+#include "virtio-scsi.h"
+
+static int virtio_blk_read_many(VDev *vdev,
+                                ulong sector, void *load_addr, int sec_num)
+{
+    VirtioBlkOuthdr out_hdr;
+    u8 status;
+    VRing *vr = &vdev->vrings[vdev->cmd_vr_idx];
+
+    /* Tell the host we want to read */
+    out_hdr.type = VIRTIO_BLK_T_IN;
+    out_hdr.ioprio = 99;
+    out_hdr.sector = virtio_sector_adjust(sector);
+
+    vring_send_buf(vr, &out_hdr, sizeof(out_hdr), VRING_DESC_F_NEXT);
+
+    /* This is where we want to receive data */
+    vring_send_buf(vr, load_addr, virtio_get_block_size() * sec_num,
+                   VRING_DESC_F_WRITE | VRING_HIDDEN_IS_CHAIN |
+                   VRING_DESC_F_NEXT);
+
+    /* status field */
+    vring_send_buf(vr, &status, sizeof(u8),
+                   VRING_DESC_F_WRITE | VRING_HIDDEN_IS_CHAIN);
+
+    /* Now we can tell the host to read */
+    vring_wait_reply();
+
+    if (drain_irqs(vr->schid)) {
+        /* Well, whatever status is supposed to contain... */
+        status = 1;
+    }
+    return status;
+}

pc-bios/s390-ccw/virtio-blk.c line 57

Quoted 10 lines
pc-bios/s390-ccw/virtio-blk.c new file +10 -0
+
+int virtio_read_many(ulong sector, void *load_addr, int sec_num)
+{
+    VDev *vdev = virtio_get_device();
+
+    switch (vdev->senseid.cu_model) {
+    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);

This is scsi, not blk. Should virtio_read_many() stay in virtio.c?

Quoted 4 lines
pc-bios/s390-ccw/virtio-blk.c new file +4 -0
+    }
+    panic("\n! No readable IPL device !\n");
+    return -1;
+}

(...)

pc-bios/s390-ccw/virtio-blk.c line 127

Quoted 13 lines
pc-bios/s390-ccw/virtio-blk.c new file +13 -0
+void virtio_assume_scsi(void)
+{
+    VDev *vdev = virtio_get_device();
+
+    switch (vdev->senseid.cu_model) {
+    case VIRTIO_ID_BLOCK:
+        vdev->guessed_disk_nature = VIRTIO_GDN_SCSI;
+        vdev->config.blk.blk_size = VIRTIO_SCSI_BLOCK_SIZE;
+        vdev->config.blk.physical_block_exp = 0;
+        vdev->blk_factor = 1;
+        break;
+    case VIRTIO_ID_SCSI:
+        vdev->scsi_block_size = VIRTIO_SCSI_BLOCK_SIZE;

More scsi. Maybe the file just needs a different name :)

pc-bios/s390-ccw/virtio-blk.c line 130

Quoted 3 lines
pc-bios/s390-ccw/virtio-blk.c new file +3 -0
+        break;
+    }
+}

General ack on splitting out this code.

Acked-by on v2 4/8
#14 lore raw

Re: [Qemu-devel] [RFC PATCH v2 4/8] pc-bios/s390-ccw: Add a write() function for stdio

In reply to Thomas Huth
Quoted 8 lines · On Fri, 7 Jul 2017 12:27:00 +0200 Thomas Huth <[email protected]> wrote:

The stdio functions from the SLOF libc need a write() function for printing text to stdout/stderr. Let's implement this function by refactoring the code from sclp_print().

---
 pc-bios/s390-ccw/sclp.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
#11 lore raw

Re: [Qemu-devel] [RFC PATCH v2 3/8] pc-bios/s390-ccw: Move virtio-block related functions into a separate file

In reply to Cornelia Huck
Quoted 18 lines · Cornelia Huck
Quoted 14 lines · On Fri, 7 Jul 2017 12:26:59 +0200 Thomas Huth <[email protected]> wrote:

The netboot code is going to link against the code from virtio.c, too, so we've got to move the virtio-block related code out of the way.

---
 pc-bios/s390-ccw/Makefile     |   2 +-
 pc-bios/s390-ccw/main.c       |   2 +-
 pc-bios/s390-ccw/s390-ccw.h   |   2 +-
 pc-bios/s390-ccw/virtio-blk.c | 296 ++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/virtio.c     | 273 +-------------------------------------
 pc-bios/s390-ccw/virtio.h     |   4 +
 6 files changed, 307 insertions(+), 272 deletions(-)
 create mode 100644 pc-bios/s390-ccw/virtio-blk.c
Quoted 13 lines

pc-bios/s390-ccw/virtio-blk.c line 4

Quoted 10 lines
pc-bios/s390-ccw/virtio-blk.c new file +4 -0
new file mode 100644
index 0000000..6cb77bc
@@ -0,0 +1,296 @@
+/*
+ * Virtio driver bits
+ *
+ * Copyright (c) 2013 Alexander Graf <[email protected]>

The original code carries the same copyright notice, but there's a lot of IBM code in there. Just saying.

Then the IBM folks should maybe add their (c) statement to that file first? Otherwise, I assume they simply do not care ;-)

[...]

Quoted 11 lines

pc-bios/s390-ccw/virtio-blk.c line 57

Quoted 9 lines
pc-bios/s390-ccw/virtio-blk.c new file +9 -0
+int virtio_read_many(ulong sector, void *load_addr, int sec_num)
+{
+    VDev *vdev = virtio_get_device();
+
+    switch (vdev->senseid.cu_model) {
+    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);

This is scsi, not blk. Should virtio_read_many() stay in virtio.c?

No, that does not work since virtio_scsi_read_many() is defined in virtio-scsi.c which is not linked to the netboot code.

Quoted 5 lines
Quoted 4 lines
pc-bios/s390-ccw/virtio-blk.c new file +4 -0
+    }
+    panic("\n! No readable IPL device !\n");
+    return -1;
+}

(...)

Quoted 15 lines

pc-bios/s390-ccw/virtio-blk.c line 127

Quoted 13 lines
pc-bios/s390-ccw/virtio-blk.c new file +13 -0
+void virtio_assume_scsi(void)
+{
+    VDev *vdev = virtio_get_device();
+
+    switch (vdev->senseid.cu_model) {
+    case VIRTIO_ID_BLOCK:
+        vdev->guessed_disk_nature = VIRTIO_GDN_SCSI;
+        vdev->config.blk.blk_size = VIRTIO_SCSI_BLOCK_SIZE;
+        vdev->config.blk.physical_block_exp = 0;
+        vdev->blk_factor = 1;
+        break;
+    case VIRTIO_ID_SCSI:
+        vdev->scsi_block_size = VIRTIO_SCSI_BLOCK_SIZE;

More scsi. Maybe the file just needs a different name :)

Do you have a better suggestion?

Thomas

#12 lore raw

Re: [Qemu-devel] [RFC PATCH v2 3/8] pc-bios/s390-ccw: Move virtio-block related functions into a separate file

Quoted 20 lines · On Fri, 7 Jul 2017 15:04:55 +0200 Thomas Huth <[email protected]> wrote:
Quoted 18 lines · Cornelia Huck

pc-bios/s390-ccw/virtio-blk.c line 127

Quoted 13 lines · On Fri, 7 Jul 2017 12:26:59 +0200 Thomas Huth <[email protected]> wrote:
pc-bios/s390-ccw/virtio-blk.c new file +13 -0
+void virtio_assume_scsi(void)
+{
+    VDev *vdev = virtio_get_device();
+
+    switch (vdev->senseid.cu_model) {
+    case VIRTIO_ID_BLOCK:
+        vdev->guessed_disk_nature = VIRTIO_GDN_SCSI;
+        vdev->config.blk.blk_size = VIRTIO_SCSI_BLOCK_SIZE;
+        vdev->config.blk.physical_block_exp = 0;
+        vdev->blk_factor = 1;
+        break;
+    case VIRTIO_ID_SCSI:
+        vdev->scsi_block_size = VIRTIO_SCSI_BLOCK_SIZE;  

More scsi. Maybe the file just needs a different name :)

Quoted 1 line

Do you have a better suggestion?

virtio-blockdev.c ?

Reviewed-by on v2 1/8
#2 lore raw

Re: [Qemu-devel] [RFC PATCH v2 1/8] pc-bios/s390-ccw: Move libc functions to separate header

In reply to Thomas Huth
Quoted 7 lines · Thomas Huth

The upcoming netboot code will use the libc from SLOF. To be able to still use s390-ccw.h there, the libc related functions in this header have to be moved to a different location.
And while we're at it, remove the duplicate memcpy() function from sclp.c.

one suggestion below, but

for change and no change

[...]

pc-bios/s390-ccw/libc.h line 41

Quoted 10 lines
pc-bios/s390-ccw/libc.h new file +10 -0
+static inline void *memcpy(void *s1, const void *s2, size_t n)
+{
+    uint8_t *p1 = s1;
+    const uint8_t *p2 = s2;
+
+    while (n--) {
+        p1[n] = p2[n];
+    }
+    return s1;
+}

Not that it matters, and no idea if moving forward like in the for loop below might allow gcc to generate better code, but a for loop like below will be the same direction as the MVC instruction. Can you double check if this generates better code?

Quoted 7 lines
pc-bios/s390-ccw/sclp.c +0 -7
-static void _memcpy(char *dest, const char *src, int len)
-{
-   int i;
-     for (i = 0; i < len; i++)
-        dest[i] = src[i];
-}
- 
Reviewed-by on v2 1/8
#5 lore raw

Re: [Qemu-devel] [RFC PATCH v2 1/8] pc-bios/s390-ccw: Move libc functions to separate header

In reply to Thomas Huth
Quoted 198 lines · Thomas Huth

The upcoming netboot code will use the libc from SLOF. To be able to still use s390-ccw.h there, the libc related functions in this header have to be moved to a different location.
And while we're at it, remove the duplicate memcpy() function from sclp.c.

---
 pc-bios/s390-ccw/bootmap.c     |  1 +
 pc-bios/s390-ccw/libc.h        | 43 ++++++++++++++++++++++++++++++++++++++++++
 pc-bios/s390-ccw/main.c        |  1 +
 pc-bios/s390-ccw/s390-ccw.h    | 29 ----------------------------
 pc-bios/s390-ccw/sclp.c        | 10 ++--------
 pc-bios/s390-ccw/virtio-scsi.c |  1 +
 pc-bios/s390-ccw/virtio.c      |  1 +
 7 files changed, 49 insertions(+), 37 deletions(-)
 create mode 100644 pc-bios/s390-ccw/libc.h
pc-bios/s390-ccw/bootmap.c +1 -0
index 523fa78..458d3da 100644
@@ -8,6 +8,7 @@
  * directory.
  */
 
+#include "libc.h"
 #include "s390-ccw.h"
 #include "bootmap.h"
 #include "virtio.h"
pc-bios/s390-ccw/libc.h new file +43 -0
new file mode 100644
index 0000000..9076bb7
@@ -0,0 +1,43 @@
+/*
+ * libc-style definitions and functions
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ */
+
+#ifndef S390_CCW_LIBC_H
+#define S390_CCW_LIBC_H
+
+typedef long               size_t;
+typedef int                bool;
+typedef unsigned char      uint8_t;
+typedef unsigned short     uint16_t;
+typedef unsigned int       uint32_t;
+typedef unsigned long long uint64_t;
+
+static inline void *memset(void *s, int c, size_t n)
+{
+    int i;
+    unsigned char *p = s;
+
+    for (i = 0; i < n; i++) {
+        p[i] = c;
+    }
+
+    return s;
+}
+
+static inline void *memcpy(void *s1, const void *s2, size_t n)
+{
+    uint8_t *p1 = s1;
+    const uint8_t *p2 = s2;
+
+    while (n--) {
+        p1[n] = p2[n];
+    }
+    return s1;
+}
+
+#endif
pc-bios/s390-ccw/main.c +1 -0
index 1cacc1b..40cba8d 100644
@@ -8,6 +8,7 @@
  * directory.
  */
 
+#include "libc.h"
 #include "s390-ccw.h"
 #include "virtio.h"
 
pc-bios/s390-ccw/s390-ccw.h +0 -29
index 2089274..43e2d42 100644
@@ -18,12 +18,6 @@ typedef unsigned short     u16;
 typedef unsigned int       u32;
 typedef unsigned long long u64;
 typedef unsigned long      ulong;
-typedef long               size_t;
-typedef int                bool;
-typedef unsigned char      uint8_t;
-typedef unsigned short     uint16_t;
-typedef unsigned int       uint32_t;
-typedef unsigned long long uint64_t;
 typedef unsigned char      __u8;
 typedef unsigned short     __u16;
 typedef unsigned int       __u32;
@@ -88,18 +82,6 @@ ulong get_second(void);
 /* bootmap.c */
 void zipl_load(void);
 
-static inline void *memset(void *s, int c, size_t n)
-{
-    int i;
-    unsigned char *p = s;
-
-    for (i = 0; i < n; i++) {
-        p[i] = c;
-    }
-
-    return s;
-}
-
 static inline void fill_hex(char *out, unsigned char val)
 {
     const char hex[] = "0123456789abcdef";
@@ -169,17 +151,6 @@ static inline void sleep(unsigned int seconds)
     }
 }
 
-static inline void *memcpy(void *s1, const void *s2, size_t n)
-{
-    uint8_t *p1 = s1;
-    const uint8_t *p2 = s2;
-
-    while (n--) {
-        p1[n] = p2[n];
-    }
-    return s1;
-}
-
 static inline void IPL_assert(bool term, const char *message)
 {
     if (!term) {
pc-bios/s390-ccw/sclp.c +2 -8
index a1639ba..aa1c862 100644
@@ -8,6 +8,7 @@
  * directory.
  */
 
+#include "libc.h"
 #include "s390-ccw.h"
 #include "sclp.h"
 
@@ -59,13 +60,6 @@ static int _strlen(const char *str)
     return i;
 }
 
-static void _memcpy(char *dest, const char *src, int len)
-{
-    int i;
-    for (i = 0; i < len; i++)
-        dest[i] = src[i];
-}
-
 void sclp_print(const char *str)
 {
     int len = _strlen(str);
@@ -76,7 +70,7 @@ void sclp_print(const char *str)
     sccb->ebh.length = sizeof(EventBufferHeader) + len;
     sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
     sccb->ebh.flags = 0;
-    _memcpy(sccb->data, str, len);
+    memcpy(sccb->data, str, len);
 
     sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
 }
pc-bios/s390-ccw/virtio-scsi.c +1 -0
index f61ecf0..c92f5d3 100644
@@ -9,6 +9,7 @@
  * directory.
  */
 
+#include "libc.h"
 #include "s390-ccw.h"
 #include "virtio.h"
 #include "scsi.h"
pc-bios/s390-ccw/virtio.c +0 -0
index 6ee93d5..8768331 100644

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

Quoted 7 lines
pc-bios/s390-ccw/virtio.c +1 -0
@@ -8,6 +8,7 @@
  * directory.
  */
 
+#include "libc.h"
 #include "s390-ccw.h"
 #include "virtio.h"
 #include "virtio-scsi.h"
#8 lore raw

Re: [Qemu-devel] [RFC PATCH v2 2/8] pc-bios/s390-ccw: Move ebc2asc to sclp.c

In reply to Thomas Huth
Quoted 4 lines · Thomas Huth

We will later need this array in a file that we will link to the netboot code, too. Since there is some ebcdic convertion done in sclp_get_loadparm_ascii(), the sclp.c file seems to be a good candidate.

s/convertion/conversion

Quoted 52 lines
---
 pc-bios/s390-ccw/main.c | 11 -----------
 pc-bios/s390-ccw/sclp.c | 11 +++++++++++
 2 files changed, 11 insertions(+), 11 deletions(-)
pc-bios/s390-ccw/main.c +0 -11
index 40cba8d..0580eac 100644
@@ -17,17 +17,6 @@ static SubChannelId blk_schid = { .one = 1 };
 IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE)));
 static char loadparm[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 
-const unsigned char ebc2asc[256] =
-      /* 0123456789abcdef0123456789abcdef */
-        "................................" /* 1F */
-        "................................" /* 3F */
-        " ...........<(+|&.........!$*);." /* 5F first.chr.here.is.real.space */
-        "-/.........,%_>?.........`:#@'=\""/* 7F */
-        ".abcdefghi.......jklmnopqr......" /* 9F */
-        "..stuvwxyz......................" /* BF */
-        ".ABCDEFGHI.......JKLMNOPQR......" /* DF */
-        "..STUVWXYZ......0123456789......";/* FF */
-
 /*
  * Priniciples of Operations (SA22-7832-09) chapter 17 requires that
  * a subsystem-identification is at 184-187 and bytes 188-191 are zero
pc-bios/s390-ccw/sclp.c +11 -0
index aa1c862..2ee204a 100644
@@ -14,6 +14,17 @@
 
 static char _sccb[PAGE_SIZE] __attribute__((__aligned__(4096)));
 
+const unsigned char ebc2asc[256] =
+      /* 0123456789abcdef0123456789abcdef */
+        "................................" /* 1F */
+        "................................" /* 3F */
+        " ...........<(+|&.........!$*);." /* 5F first.chr.here.is.real.space */
+        "-/.........,%_>?.........`:#@'=\""/* 7F */
+        ".abcdefghi.......jklmnopqr......" /* 9F */
+        "..stuvwxyz......................" /* BF */
+        ".ABCDEFGHI.......JKLMNOPQR......" /* DF */
+        "..STUVWXYZ......0123456789......";/* FF */
+
 /* Perform service call. Return 0 on success, non-zero otherwise. */
 static int sclp_service_call(unsigned int command, void *sccb)
 {
#3 lore raw

Re: [Qemu-devel] [RFC PATCH v2 1/8] pc-bios/s390-ccw: Move libc functions to separate header

In reply to Christian Borntraeger
Quoted 16 lines · Christian Borntraeger
Quoted 7 lines · Thomas Huth

The upcoming netboot code will use the libc from SLOF. To be able to still use s390-ccw.h there, the libc related functions in this header have to be moved to a different location.
And while we're at it, remove the duplicate memcpy() function from sclp.c.

one suggestion below, but

for change and no change

[...]

Quoted 15 lines

pc-bios/s390-ccw/libc.h line 41

Quoted 10 lines
pc-bios/s390-ccw/libc.h new file +10 -0
+static inline void *memcpy(void *s1, const void *s2, size_t n)
+{
+    uint8_t *p1 = s1;
+    const uint8_t *p2 = s2;
+
+    while (n--) {
+        p1[n] = p2[n];
+    }
+    return s1;
+}

Not that it matters, and no idea if moving forward like in the for loop below might allow gcc to generate better code, but a for loop like below will be the same direction as the MVC instruction. Can you double check if this generates better code?

At least with my compiler version (old 4.8.1), the code looks pretty similar. Backward loop:

 1d8:   a7 19 04 d1             lghi    %r1,1233
 1dc:   e3 40 50 00 00 04       lg      %r4,0(%r5)
 1e2:   e3 30 70 00 00 04       lg      %r3,0(%r7)
 1e8:   a7 29 04 d2             lghi    %r2,1234
 1ec:   43 01 30 00             ic      %r0,0(%r1,%r3)
 1f0:   a7 1b ff ff             aghi    %r1,-1
 1f4:   42 01 40 01             stc     %r0,1(%r1,%r4)
 1f8:   a7 27 ff fa             brctg   %r2,1ec <main+0x1ec>

Forward loop:

 238:   a7 19 00 00             lghi    %r1,0
 23c:   e3 40 50 00 00 04       lg      %r4,0(%r5)
 242:   e3 30 70 00 00 04       lg      %r3,0(%r7)
 248:   a7 29 04 d2             lghi    %r2,1234
 24c:   43 01 30 00             ic      %r0,0(%r1,%r3)
 250:   42 01 40 00             stc     %r0,0(%r1,%r4)
 254:   a7 1b 00 01             aghi    %r1,1
 258:   a7 27 ff fa             brctg   %r2,24c <main+0x24c>

But I can switch to the for-loop version in my patch anyway, if you prefer that.

Thomas

#4 lore raw

Re: [Qemu-devel] [RFC PATCH v2 1/8] pc-bios/s390-ccw: Move libc functions to separate header

Quoted 56 lines · Thomas Huth
Quoted 16 lines · Christian Borntraeger
Quoted 7 lines · Thomas Huth

The upcoming netboot code will use the libc from SLOF. To be able to still use s390-ccw.h there, the libc related functions in this header have to be moved to a different location.
And while we're at it, remove the duplicate memcpy() function from sclp.c.

one suggestion below, but

for change and no change

[...]

Quoted 15 lines

pc-bios/s390-ccw/libc.h line 41

Quoted 10 lines
pc-bios/s390-ccw/libc.h new file +10 -0
+static inline void *memcpy(void *s1, const void *s2, size_t n)
+{
+    uint8_t *p1 = s1;
+    const uint8_t *p2 = s2;
+
+    while (n--) {
+        p1[n] = p2[n];
+    }
+    return s1;
+}

Not that it matters, and no idea if moving forward like in the for loop below might allow gcc to generate better code, but a for loop like below will be the same direction as the MVC instruction. Can you double check if this generates better code?

At least with my compiler version (old 4.8.1), the code looks pretty similar. Backward loop:

 1d8:   a7 19 04 d1             lghi    %r1,1233
 1dc:   e3 40 50 00 00 04       lg      %r4,0(%r5)
 1e2:   e3 30 70 00 00 04       lg      %r3,0(%r7)
 1e8:   a7 29 04 d2             lghi    %r2,1234
 1ec:   43 01 30 00             ic      %r0,0(%r1,%r3)
 1f0:   a7 1b ff ff             aghi    %r1,-1
 1f4:   42 01 40 01             stc     %r0,1(%r1,%r4)
 1f8:   a7 27 ff fa             brctg   %r2,1ec <main+0x1ec>

Forward loop:

 238:   a7 19 00 00             lghi    %r1,0
 23c:   e3 40 50 00 00 04       lg      %r4,0(%r5)
 242:   e3 30 70 00 00 04       lg      %r3,0(%r7)
 248:   a7 29 04 d2             lghi    %r2,1234
 24c:   43 01 30 00             ic      %r0,0(%r1,%r3)
 250:   42 01 40 00             stc     %r0,0(%r1,%r4)
 254:   a7 1b 00 01             aghi    %r1,1
 258:   a7 27 ff fa             brctg   %r2,24c <main+0x24c>
Quoted 2 lines

But I can switch to the for-loop version in my patch anyway, if you prefer that.

FWIW, if I define memcpy() via __builtin_memcpy() instead, I get a MVC in the disassembly... should we use maybe that macro instead?

Thomas

#19 lore raw

Re: [Qemu-devel] [RFC PATCH v2 7/8] pc-bios/s390-ccw: Add virtio-net driver code

In reply to Cornelia Huck
Quoted 6 lines · Cornelia Huck
Quoted 3 lines · On Fri, 7 Jul 2017 12:27:03 +0200 Thomas Huth <[email protected]> wrote:

The driver provides the recv() and send() functions which will be required by SLOF's libnet code for receiving and sending packets.

[...]

Quoted 5 lines
Quoted 5 lines
pc-bios/s390-ccw/virtio-net.c new file +5 -0
+int virtio_net_init(void *mac_addr)
+{
+    VDev *vdev = virtio_get_device();
+    VRing *rxvq = &vdev->vrings[VQ_RX];
+    void *buf;
Quoted 8 lines

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

Quoted 5 lines
pc-bios/s390-ccw/virtio-net.c new file +5 -0
+    int i;
+
+    virtio_setup_ccw(vdev);
+
+    memcpy(mac_addr, vdev->config.net.mac, ETH_ALEN);

The mac address in the config space is only valid if the mac feature flag has been negotiated (although the field always exists).

QEMU seems to always set it ... but yes, let's better negotiate VIRTIO_NET_F_MAC here.

Quoted 23 lines
Quoted 23 lines
pc-bios/s390-ccw/virtio-net.c new file +23 -0
+
+    for (i = 0; i < 64; i++) {
+        buf = malloc(ETH_MTU_SIZE + sizeof(VirtioNetHdr));
+        IPL_assert(buf != NULL, "Can not allocate memory for receive buffers");
+        vring_send_buf(rxvq, buf, ETH_MTU_SIZE + sizeof(VirtioNetHdr),
+                       VRING_DESC_F_WRITE);
+    }
+    vring_notify(rxvq);
+
+    return 0;
+}
+
+int send(int fd, const void *buf, int len, int flags)
+{
+    VirtioNetHdr tx_hdr;
+    VDev *vdev = virtio_get_device();
+    VRing *txvq = &vdev->vrings[VQ_TX];
+
+    /* Set up header - we do not use anything special, so simply clear it */
+    memset(&tx_hdr, 0, sizeof(tx_hdr));
+
+    vring_send_buf(txvq, &tx_hdr, sizeof(tx_hdr), VRING_DESC_F_NEXT);
+    vring_send_buf(txvq, (void *)buf, len, VRING_HIDDEN_IS_CHAIN);
Quoted 8 lines

pc-bios/s390-ccw/virtio-net.c line 81

Quoted 6 lines
pc-bios/s390-ccw/virtio-net.c new file +6 -0
+    while (!vr_poll(txvq)) {
+        yield();
+    }
+    if (drain_irqs(txvq->schid)) {
+        puts("send: drain irqs failed");
+        return -1;

Does the caller try again later? drain_irqs() errors may be transient.

Yes, the TFTP code retries to send packets after a certain amount of time (since the packets could also be lost in the network somewhere).

Quoted 25 lines
Quoted 25 lines
pc-bios/s390-ccw/virtio.c +2 -2
index 09ab291..729d926 100644
@@ -149,7 +149,7 @@ static void vring_init(VRing *vr, VqInfo *info)
     debug_print_addr("init vr", vr);
 }
 
-static bool vring_notify(VRing *vr)
+bool vring_notify(VRing *vr)
 {
     vr->cookie = virtio_notify(vr->schid, vr->id, vr->cookie);
     return vr->cookie >= 0;
@@ -188,7 +188,7 @@ ulong get_second(void)
     return (get_clock() >> 12) / 1000000;
 }
 
-static int vr_poll(VRing *vr)
+int vr_poll(VRing *vr)
 {
     if (vr->used->idx == vr->used_idx) {
         vring_notify(vr);
@@ -261,6 +261,11 @@ void virtio_setup_ccw(VDev *vdev)
     run_ccw(vdev, CCW_CMD_VDEV_RESET, NULL, 0);
 
Quoted 5 lines

pc-bios/s390-ccw/virtio.c void virtio_setup_ccw(VDev *vdev) · line 265

Quoted 3 lines
pc-bios/s390-ccw/virtio.c +2 -0
     switch (vdev->senseid.cu_model) {
+    case VIRTIO_ID_NET:
+        vdev->nr_vqs = 3;

I think this should be 2, as you don't negotiate for the control vq.

OK.

Quoted 23 lines
Quoted 23 lines
pc-bios/s390-ccw/virtio.c +3 -0
+        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;
pc-bios/s390-ccw/virtio.h +0 -2
index d743919..d6af022 100644
@@ -11,8 +11,6 @@
 #ifndef VIRTIO_H
 #define VIRTIO_H
 
-#include "s390-ccw.h"
-
 /* Status byte for guest to report progress, and synchronize features. */
 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
 #define VIRTIO_CONFIG_S_ACKNOWLEDGE     1
@@ -254,6 +252,12 @@ struct ScsiDevice {
 };
 typedef struct ScsiDevice ScsiDevice;
 
Quoted 6 lines

pc-bios/s390-ccw/virtio.h struct ScsiDevice { · line 257

Quoted 3 lines
pc-bios/s390-ccw/virtio.h +3 -0
+struct VirtioNetConfig {
+    uint8_t mac[6];
+    uint16_t status;

I don't think status exists if you don't negotiate the respective feature bit.

OK.

Quoted 26 lines
Quoted 26 lines
pc-bios/s390-ccw/virtio.h +6 -0
+};
+typedef struct VirtioNetConfig VirtioNetConfig;
+
 struct VDev {
     int nr_vqs;
     VRing *vrings;
@@ -266,6 +270,7 @@ struct VDev {
     union {
         VirtioBlkConfig blk;
         VirtioScsiConfig scsi;
+        VirtioNetConfig net;
     } config;
     ScsiDevice *scsi_device;
     bool is_cdrom;
@@ -291,10 +296,14 @@ struct VirtioCmd {
 };
 typedef struct VirtioCmd VirtioCmd;
 
+bool vring_notify(VRing *vr);
 int drain_irqs(SubChannelId schid);
 void vring_send_buf(VRing *vr, void *p, int len, int flags);
+int vr_poll(VRing *vr);
 int vring_wait_reply(void);
 int virtio_run(VDev *vdev, int vqid, VirtioCmd *cmd);
 void virtio_setup_ccw(VDev *vdev);
 
Quoted 4 lines

pc-bios/s390-ccw/virtio.h struct VirtioCmd { · line 309

Quoted 3 lines
pc-bios/s390-ccw/virtio.h +2 -0
+int virtio_net_init(void *mac_addr);
+
 #endif /* VIRTIO_H */

Thanks for the review!

Thomas

#20 lore raw

Re: [Qemu-devel] [RFC PATCH v2 7/8] pc-bios/s390-ccw: Add virtio-net driver code

Quoted 22 lines · On Mon, 10 Jul 2017 13:23:07 +0200 Thomas Huth <[email protected]> wrote:
Quoted 6 lines · Cornelia Huck
Quoted 3 lines · On Fri, 7 Jul 2017 12:27:03 +0200 Thomas Huth <[email protected]> wrote:

The driver provides the recv() and send() functions which will be required by SLOF's libnet code for receiving and sending packets.

[...]

Quoted 5 lines
Quoted 5 lines
pc-bios/s390-ccw/virtio-net.c new file +5 -0
+int virtio_net_init(void *mac_addr)
+{
+    VDev *vdev = virtio_get_device();
+    VRing *rxvq = &vdev->vrings[VQ_RX];
+    void *buf;
Quoted 8 lines

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

Quoted 5 lines
pc-bios/s390-ccw/virtio-net.c new file +5 -0
+    int i;
+
+    virtio_setup_ccw(vdev);
+
+    memcpy(mac_addr, vdev->config.net.mac, ETH_ALEN);  

The mac address in the config space is only valid if the mac feature flag has been negotiated (although the field always exists).

Quoted 2 lines

QEMU seems to always set it ... but yes, let's better negotiate VIRTIO_NET_F_MAC here.

Ah, feature negotiation in the bios, nice :)

Quoted 33 lines
Quoted 24 lines
Quoted 24 lines
pc-bios/s390-ccw/virtio-net.c new file +23 -0
+
+    for (i = 0; i < 64; i++) {
+        buf = malloc(ETH_MTU_SIZE + sizeof(VirtioNetHdr));
+        IPL_assert(buf != NULL, "Can not allocate memory for receive buffers");
+        vring_send_buf(rxvq, buf, ETH_MTU_SIZE + sizeof(VirtioNetHdr),
+                       VRING_DESC_F_WRITE);
+    }
+    vring_notify(rxvq);
+
+    return 0;
+}
+
+int send(int fd, const void *buf, int len, int flags)
+{
+    VirtioNetHdr tx_hdr;
+    VDev *vdev = virtio_get_device();
+    VRing *txvq = &vdev->vrings[VQ_TX];
+
+    /* Set up header - we do not use anything special, so simply clear it */
+    memset(&tx_hdr, 0, sizeof(tx_hdr));
+
+    vring_send_buf(txvq, &tx_hdr, sizeof(tx_hdr), VRING_DESC_F_NEXT);
+    vring_send_buf(txvq, (void *)buf, len, VRING_HIDDEN_IS_CHAIN);
Quoted 8 lines

pc-bios/s390-ccw/virtio-net.c line 81

Quoted 6 lines
pc-bios/s390-ccw/virtio-net.c new file +6 -0
+    while (!vr_poll(txvq)) {
+        yield();
+    }
+    if (drain_irqs(txvq->schid)) {
+        puts("send: drain irqs failed");
+        return -1;  

Does the caller try again later? drain_irqs() errors may be transient.

Quoted 2 lines

Yes, the TFTP code retries to send packets after a certain amount of time (since the packets could also be lost in the network somewhere).

Sounds good. (Not that I expect many errors in drain_irqs()...)