Skip to contentSkip to conversation

hw/sd/ssi-sd: Return noise (dummy byte) when no card connected

PATCH RESEND In review Philippe Mathieu-Daudé 3 participants 10 messages Aug 12, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover hw/sd/ssi-sd: Return noise (dummy byte) when no card connected

Trivial fix for the issue reported by Guenter here:
https://lore.kernel.org/qemu-devel/[email protected]/

- Return dummy byte when no card is connected
- Add a test
- Document ssi_transfer()

Since v1:
- Reworded patch #1 description (Alex)
- Removed '_' test method prefix (Alex)

Philippe Mathieu-Daudé (3):
hw/sd/ssi-sd: Return noise (dummy byte) when no card connected tests/functional: Test SPI-SD adapter without SD card connected hw/ssi: Document ssi_transfer() method

 include/hw/ssi/ssi.h                      | 14 ++++++++++++++
 hw/sd/ssi-sd.c                            |  4 ++++
 tests/functional/test_riscv64_sifive_u.py | 22 +++++++++++++++++-----
 3 files changed, 35 insertions(+), 5 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH RESEND v2 1/3 hw/sd/ssi-sd: Return noise (dummy byte) when no card connected

Commit 1585ab9f1ba ("hw/sd/sdcard: Fill SPI response bits in card code") exposed a bug in the SPI adapter: if no SD card is plugged, we are returning "there is a card with an error". This is wrong, we shouldn't return any particular packet response, but the noise shifted on the MISO line. Return the dummy byte, otherwise we get:

qemu-system-riscv64: ../hw/sd/ssi-sd.c:160: ssi_sd_transfer: Assertion `s->arglen > 0' failed.

Fixes 775616c3ae8 ("Partial SD card SPI mode support")
---
 hw/sd/ssi-sd.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/sd/ssi-sd.c +4 -0
index 594dead19ee..3aacbd03871 100644
@@ -89,6 +89,10 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)
     SDRequest request;
     uint8_t longresp[5];
 
+    if (!sdbus_get_inserted(&s->sdbus)) {
+        return SSI_DUMMY;
+    }
+
     /*
      * Special case: allow CMD12 (STOP TRANSMISSION) while reading data.
      *
Signature · 1 line
2.49.0
Reviewed-by on v2 1/3
#2 lore raw

Hi Phil,

Quoted 22 lines · Philippe Mathieu-Daudé

Commit 1585ab9f1ba ("hw/sd/sdcard: Fill SPI response bits in card code") exposed a bug in the SPI adapter: if no SD card is plugged, we are returning "there is a card with an error". This is wrong, we shouldn't return any particular packet response, but the noise shifted on the MISO line. Return the dummy byte, otherwise we get:

   qemu-system-riscv64: ../hw/sd/ssi-sd.c:160: ssi_sd_transfer: Assertion `s->arglen > 0' failed.
Fixes 775616c3ae8 ("Partial SD card SPI mode support")
---
  hw/sd/ssi-sd.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/sd/ssi-sd.c +0 -0
index 594dead19ee..3aacbd03871 100644

hw/sd/ssi-sd.c static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val) · line 98

Quoted 10 lines
hw/sd/ssi-sd.c +4 -0
@@ -89,6 +89,10 @@ static uint32_t ssi_sd_transfer(SSIPeripheral *dev, uint32_t val)
      SDRequest request;
      uint8_t longresp[5];
 
+    if (!sdbus_get_inserted(&s->sdbus)) {
+        return SSI_DUMMY;
+    }
+
      /*
       * Special case: allow CMD12 (STOP TRANSMISSION) while reading data.
       *

Cheers,
Gustavo

#3 lore raw

PATCH RESEND v2 2/3 tests/functional: Test SPI-SD adapter without SD card connected

In reply to Philippe Mathieu-Daudé

SPI-SD adapter should be usable, even without any SD card wired. Refactor test_riscv64_sifive_u_mmc_spi() to make it more generic and add another test, inspired by this report:
https://lore.kernel.org/qemu-devel/[email protected]/

---
 tests/functional/test_riscv64_sifive_u.py | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
tests/functional/test_riscv64_sifive_u.py +17 -5
index dc4cb8a4a96..ca4c3ba6e12 100755
@@ -27,25 +27,37 @@ class SifiveU(LinuxKernelTest):
          'rootfs.ext2.gz'),
         'b6ed95610310b7956f9bf20c4c9c0c05fea647900df441da9dfe767d24e8b28b')
 
-    def test_riscv64_sifive_u_mmc_spi(self):
+    def do_test_riscv64_sifive_u_mmc_spi(self, connect_card):
         self.set_machine('sifive_u')
         kernel_path = self.ASSET_KERNEL.fetch()
         rootfs_path = self.uncompress(self.ASSET_ROOTFS)
 
         self.vm.set_console()
         kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
-                               'root=/dev/mmcblk0 rootwait '
                                'earlycon=sbi console=ttySIF0 '
-                               'panic=-1 noreboot')
+                               'root=/dev/mmcblk0 ')
         self.vm.add_args('-kernel', kernel_path,
-                         '-drive', f'file={rootfs_path},if=sd,format=raw',
                          '-append', kernel_command_line,
                          '-no-reboot')
+        if connect_card:
+            self.vm.add_args('-drive', f'file={rootfs_path},if=sd,format=raw')
+            kernel_command_line += 'panic=-1 noreboot rootwait '
+            pattern = 'Boot successful.'
+        else:
+            kernel_command_line += 'panic=0 noreboot '
+            pattern = 'Cannot open root device "mmcblk0" or unknown-block(0,0)'
+
         self.vm.launch()
-        self.wait_for_console_pattern('Boot successful.')
+        self.wait_for_console_pattern(pattern)
 
         os.remove(rootfs_path)
 
+    def test_riscv64_sifive_u_nommc_spi(self):
+        self.do_test_riscv64_sifive_u_mmc_spi(False)
+
+    def test_riscv64_sifive_u_mmc_spi(self):
+        self.do_test_riscv64_sifive_u_mmc_spi(True)
+
 
 if __name__ == '__main__':
     LinuxKernelTest.main()
Signature · 1 line
2.49.0
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH-for-10.1 RESEND RESEND v2 2/3] tests/functional: Test SPI-SD adapter without SD card connected

Hi Phil,

Quoted 28 lines · Philippe Mathieu-Daudé

SPI-SD adapter should be usable, even without any SD card wired. Refactor test_riscv64_sifive_u_mmc_spi() to make it more generic and add another test, inspired by this report:
https://lore.kernel.org/qemu-devel/[email protected]/

---
  tests/functional/test_riscv64_sifive_u.py | 22 +++++++++++++++++-----
  1 file changed, 17 insertions(+), 5 deletions(-)
tests/functional/test_riscv64_sifive_u.py +1 -2
index dc4cb8a4a96..ca4c3ba6e12 100755
@@ -27,25 +27,37 @@ class SifiveU(LinuxKernelTest):
           'rootfs.ext2.gz'),
          'b6ed95610310b7956f9bf20c4c9c0c05fea647900df441da9dfe767d24e8b28b')
 
-    def test_riscv64_sifive_u_mmc_spi(self):
+    def do_test_riscv64_sifive_u_mmc_spi(self, connect_card):
          self.set_machine('sifive_u')
          kernel_path = self.ASSET_KERNEL.fetch()
          rootfs_path = self.uncompress(self.ASSET_ROOTFS)
 
          self.vm.set_console()
          kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
-                               'root=/dev/mmcblk0 rootwait '

tests/functional/test_riscv64_sifive_u.py class SifiveU(LinuxKernelTest): · line 44

Quoted 10 lines
tests/functional/test_riscv64_sifive_u.py +4 -2
                                 'earlycon=sbi console=ttySIF0 '
-                               'panic=-1 noreboot')
+                               'root=/dev/mmcblk0 ')
          self.vm.add_args('-kernel', kernel_path,
-                         '-drive', f'file={rootfs_path},if=sd,format=raw',
                           '-append', kernel_command_line,
                           '-no-reboot')
+        if connect_card:
+            self.vm.add_args('-drive', f'file={rootfs_path},if=sd,format=raw')
+            kernel_command_line += 'panic=-1 noreboot rootwait '

I would put kernel_command_line before add_args just to follow the order above, but really it's a nit and for sure not worth a v3, but mentioning it just in case there will be a v3 and you want to change it.

Quoted 10 lines
tests/functional/test_riscv64_sifive_u.py +6 -1
+            pattern = 'Boot successful.'
+        else:
+            kernel_command_line += 'panic=0 noreboot '
+            pattern = 'Cannot open root device "mmcblk0" or unknown-block(0,0)'
+
          self.vm.launch()
-        self.wait_for_console_pattern('Boot successful.')
+        self.wait_for_console_pattern(pattern)
 
          os.remove(rootfs_path)

tests/functional/test_riscv64_sifive_u.py class SifiveU(LinuxKernelTest): · line 63

Quoted 10 lines
tests/functional/test_riscv64_sifive_u.py +6 -0
 
+    def test_riscv64_sifive_u_nommc_spi(self):
+        self.do_test_riscv64_sifive_u_mmc_spi(False)
+
+    def test_riscv64_sifive_u_mmc_spi(self):
+        self.do_test_riscv64_sifive_u_mmc_spi(True)
+
 
  if __name__ == '__main__':
      LinuxKernelTest.main()

Cheers,
Gustavo

Reviewed-by Tested-by on v2 2/3
#5 lore raw

Re: [PATCH-for-10.1 RESEND RESEND v2 2/3] tests/functional: Test SPI-SD adapter without SD card connected

In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

SPI-SD adapter should be usable, even without any SD card wired. Refactor test_riscv64_sifive_u_mmc_spi() to make it more generic and add another test, inspired by this report:
https://lore.kernel.org/qemu-devel/[email protected]/

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#6 lore raw

PATCH RESEND v2 3/3 hw/ssi: Document ssi_transfer() method

In reply to Philippe Mathieu-Daudé

A SPI transaction consists of shifting bit in sync with the CLK line, writing on the MOSI (output) line / and reading MISO (input) line.

---
 include/hw/ssi/ssi.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/hw/ssi/ssi.h +14 -0
index 3cdcbd53904..2ad8033d8f5 100644
@@ -38,6 +38,7 @@ struct SSIPeripheralClass {
 
     /* if you have standard or no CS behaviour, just override transfer.
      * This is called when the device cs is active (true by default).
+     * See ssi_transfer().
      */
     uint32_t (*transfer)(SSIPeripheral *dev, uint32_t val);
     /* called when the CS line changes. Optional, devices only need to implement
@@ -52,6 +53,7 @@ struct SSIPeripheralClass {
      * of the CS behaviour at the device level. transfer, set_cs, and
      * cs_polarity are unused if this is overwritten. Transfer_raw will
      * always be called for the device for every txrx access to the parent bus
+     * See ssi_transfer().
      */
     uint32_t (*transfer_raw)(SSIPeripheral *dev, uint32_t val);
 };
@@ -110,6 +112,18 @@ bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp);
 /* Master interface.  */
 SSIBus *ssi_create_bus(DeviceState *parent, const char *name);
 
+/**
+ * Transfer a word on a SSI bus
+ * @bus: SSI bus
+ * @val: word to transmit
+ *
+ * At the same time, read a word and write the @val one on the SSI bus.
+ *
+ * SSI words might vary between 8 and 32 bits. The same number of bits
+ * written is received.
+ *
+ * Return: word value received
+ */
 uint32_t ssi_transfer(SSIBus *bus, uint32_t val);
 
 DeviceState *ssi_get_cs(SSIBus *bus, uint8_t cs_index);
Signature · 1 line
2.49.0
Reviewed-by on v2 3/3
#7 lore raw

Re: [PATCH-for-10.1 RESEND RESEND v2 3/3] hw/ssi: Document ssi_transfer() method

Hi Phil,

Quoted 38 lines · Philippe Mathieu-Daudé

A SPI transaction consists of shifting bit in sync with the CLK line, writing on the MOSI (output) line / and reading MISO (input) line.

---
  include/hw/ssi/ssi.h | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
include/hw/ssi/ssi.h +7 -0
index 3cdcbd53904..2ad8033d8f5 100644
@@ -38,6 +38,7 @@ struct SSIPeripheralClass {
 
      /* if you have standard or no CS behaviour, just override transfer.
       * This is called when the device cs is active (true by default).
+     * See ssi_transfer().
       */
      uint32_t (*transfer)(SSIPeripheral *dev, uint32_t val);
      /* called when the CS line changes. Optional, devices only need to implement
@@ -52,6 +53,7 @@ struct SSIPeripheralClass {
       * of the CS behaviour at the device level. transfer, set_cs, and
       * cs_polarity are unused if this is overwritten. Transfer_raw will
       * always be called for the device for every txrx access to the parent bus
+     * See ssi_transfer().
       */
      uint32_t (*transfer_raw)(SSIPeripheral *dev, uint32_t val);
  };
@@ -110,6 +112,18 @@ bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp);
  /* Master interface.  */
  SSIBus *ssi_create_bus(DeviceState *parent, const char *name);
 
+/**
+ * Transfer a word on a SSI bus
+ * @bus: SSI bus
+ * @val: word to transmit
+ *

include/hw/ssi/ssi.h bool ssi_realize_and_unref(DeviceState *dev, SSIBus *bus, Error **errp); · line 129

Quoted 10 lines
include/hw/ssi/ssi.h +7 -0
+ * At the same time, read a word and write the @val one on the SSI bus.
+ *
+ * SSI words might vary between 8 and 32 bits. The same number of bits
+ * written is received.
+ *
+ * Return: word value received
+ */
  uint32_t ssi_transfer(SSIBus *bus, uint32_t val);
 
  DeviceState *ssi_get_cs(SSIBus *bus, uint8_t cs_index);

Cheers,
Gustavo

Reviewed-by on v2 3/3
#8 lore raw

Re: [PATCH-for-10.1 RESEND RESEND v2 3/3] hw/ssi: Document ssi_transfer() method

In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

A SPI transaction consists of shifting bit in sync with the CLK line, writing on the MOSI (output) line / and reading MISO (input) line.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#9 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 4 lines · Philippe Mathieu-Daudé

Philippe Mathieu-Daudé (3):
hw/sd/ssi-sd: Return noise (dummy byte) when no card connected tests/functional: Test SPI-SD adapter without SD card connected hw/ssi: Document ssi_transfer() method

Series queued, thanks!