Skip to contentSkip to conversation

hw/sd/sdcard: fix spi_cmd_SEND_CSD/CID state check

PATCH NAK Ben Dooks 3 participants 6 messages Jul 24, 2025 – Jul 31, 2025

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

PATCH 1/1 hw/sd/sdcard: fix spi_cmd_SEND_CSD/CID state check

The addition of specific handlers for mmc-spi for SEND_CSD and SEND_CID has broken at least Linux and possibly also u-boot's mmc-spi code.

It looks like when adding the code, it is checking for these commands to not be in sd_standby_state but the check looks to have been accidentally reversed (see below)

     if (sd->state != sd_standby_state) {
         return sd_invalid_state_for_cmd(sd, req);
     }

Linux shows the following:

[    0.293983] Waiting for root device /dev/mmcblk0...
[    1.363071] mmc0: error -38 whilst initialising SD card
[    2.418566] mmc0: error -38 whilst initialising SD card
Fixes da954d0e32444f122a4 ("hw/sd/sdcard: Add spi_cmd_SEND_CSD/CID handlers (CMD9 & CMD10)")
---
 hw/sd/sd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/sd.c +3 -3
index 49fc79cf8a..e6c1ba7c5d 100644
@@ -1317,7 +1317,7 @@ static sd_rsp_type_t sd_cmd_SEND_IF_COND(SDState *sd, SDRequest req)
 /* CMD9 */
 static sd_rsp_type_t spi_cmd_SEND_CSD(SDState *sd, SDRequest req)
 {
-    if (sd->state != sd_standby_state) {
+    if (sd->state == sd_standby_state) {
         return sd_invalid_state_for_cmd(sd, req);
     }
     return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd, req),
@@ -1336,7 +1336,7 @@ static sd_rsp_type_t sd_cmd_SEND_CSD(SDState *sd, SDRequest req)
 /* CMD10 */
 static sd_rsp_type_t spi_cmd_SEND_CID(SDState *sd, SDRequest req)
 {
-    if (sd->state != sd_standby_state) {
+    if (sd->state == sd_standby_state) {
         return sd_invalid_state_for_cmd(sd, req);
     }
     return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd, req),
@@ -1345,7 +1345,7 @@ static sd_rsp_type_t spi_cmd_SEND_CID(SDState *sd, SDRequest req)
 
 static sd_rsp_type_t sd_cmd_SEND_CID(SDState *sd, SDRequest req)
 {
-    if (sd->state != sd_standby_state) {
+    if (sd->state == sd_standby_state) {
         return sd_invalid_state_for_cmd(sd, req);
     }
 
Signature · 1 line
2.37.2.352.g3c44437643
#1 lore raw

Hi Ben,

Quoted 29 lines · Ben Dooks

The addition of specific handlers for mmc-spi for SEND_CSD and SEND_CID has broken at least Linux and possibly also u-boot's mmc-spi code.

It looks like when adding the code, it is checking for these commands to not be in sd_standby_state but the check looks to have been accidentally reversed (see below)

      if (sd->state != sd_standby_state) {
          return sd_invalid_state_for_cmd(sd, req);
      }

Linux shows the following:

[    0.293983] Waiting for root device /dev/mmcblk0...
[    1.363071] mmc0: error -38 whilst initialising SD card
[    2.418566] mmc0: error -38 whilst initialising SD card
Fixes da954d0e32444f122a4 ("hw/sd/sdcard: Add spi_cmd_SEND_CSD/CID handlers (CMD9 & CMD10)")
---
  hw/sd/sd.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/sd.c +0 -0
index 49fc79cf8a..e6c1ba7c5d 100644

hw/sd/sd.c static sd_rsp_type_t sd_cmd_SEND_IF_COND(SDState *sd, SDRequest req) · line 1320

Quoted 5 lines
hw/sd/sd.c +1 -1
@@ -1317,7 +1317,7 @@ static sd_rsp_type_t sd_cmd_SEND_IF_COND(SDState *sd, SDRequest req)
  /* CMD9 */
  static sd_rsp_type_t spi_cmd_SEND_CSD(SDState *sd, SDRequest req)
  {
-    if (sd->state != sd_standby_state) {
+    if (sd->state == sd_standby_state) {

This happens to work in your case by luck.

Since we switch to sd_sendingdata_state in sd_cmd_to_sendingdata(), we need to check for sd_transfer_state -- the spec is wrong here! --.

Btw sd_cmd_to_sendingdata() already checks for that.

hw/sd/sd.c static sd_rsp_type_t sd_cmd_SEND_CSD(SDState *sd, SDRequest req) · line 1339

Quoted 9 lines
hw/sd/sd.c +1 -1
          return sd_invalid_state_for_cmd(sd, req);
      }
      return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd, req),
@@ -1336,7 +1336,7 @@ static sd_rsp_type_t sd_cmd_SEND_CSD(SDState *sd, SDRequest req)
  /* CMD10 */
  static sd_rsp_type_t spi_cmd_SEND_CID(SDState *sd, SDRequest req)
  {
-    if (sd->state != sd_standby_state) {
+    if (sd->state == sd_standby_state) {

Ditto.

hw/sd/sd.c static sd_rsp_type_t spi_cmd_SEND_CID(SDState *sd, SDRequest req) · line 1348

Quoted 9 lines
hw/sd/sd.c +1 -1
          return sd_invalid_state_for_cmd(sd, req);
      }
      return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd, req),
@@ -1345,7 +1345,7 @@ static sd_rsp_type_t spi_cmd_SEND_CID(SDState *sd, SDRequest req)
 
  static sd_rsp_type_t sd_cmd_SEND_CID(SDState *sd, SDRequest req)
  {
-    if (sd->state != sd_standby_state) {
+    if (sd->state == sd_standby_state) {

Nack, you just broke SD card implementation. Have a look at the "4.8 Card State Transition Table" in the spec.

hw/sd/sd.c static sd_rsp_type_t spi_cmd_SEND_CID(SDState *sd, SDRequest req) · line 1351

Quoted 3 lines
hw/sd/sd.c +0 -0
          return sd_invalid_state_for_cmd(sd, req);
      }
 

Regards,
Phil.

#2 lore raw
Quoted 40 lines · Philippe Mathieu-Daudé

Hi Ben,

Quoted 29 lines · Ben Dooks

The addition of specific handlers for mmc-spi for SEND_CSD and SEND_CID has broken at least Linux and possibly also u-boot's mmc-spi code.

It looks like when adding the code, it is checking for these commands to not be in sd_standby_state but the check looks to have been accidentally reversed (see below)

      if (sd->state != sd_standby_state) {
          return sd_invalid_state_for_cmd(sd, req);
      }

Linux shows the following:

[    0.293983] Waiting for root device /dev/mmcblk0...
[    1.363071] mmc0: error -38 whilst initialising SD card
[    2.418566] mmc0: error -38 whilst initialising SD card
Fixes da954d0e32444f122a4 ("hw/sd/sdcard: Add spi_cmd_SEND_CSD/CID handlers (CMD9 & CMD10)")
---
  hw/sd/sd.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/sd.c +0 -0
index 49fc79cf8a..e6c1ba7c5d 100644

hw/sd/sd.c static sd_rsp_type_t sd_cmd_SEND_IF_COND(SDState *sd, SDRequest req) · line 1320

Quoted 5 lines
hw/sd/sd.c +1 -1
@@ -1317,7 +1317,7 @@ static sd_rsp_type_t sd_cmd_SEND_IF_COND(SDState *sd, SDRequest req)
  /* CMD9 */
  static sd_rsp_type_t spi_cmd_SEND_CSD(SDState *sd, SDRequest req)
  {
-    if (sd->state != sd_standby_state) {
+    if (sd->state == sd_standby_state) {

This happens to work in your case by luck.

Quoted 5 lines

Since we switch to sd_sendingdata_state in sd_cmd_to_sendingdata(), we need to check for sd_transfer_state -- the spec is wrong here! --.

Btw sd_cmd_to_sendingdata() already checks for that.

In my fix (the one I am carrying downstream) I have

- if (sd->state != sd_standby_state) {
+ if (sd->state != sd_transfer_state) {

in spi_cmd_SEND_CSD() and spi_cmd_SEND_CID(), together with

- return sd_r2_s;
+ return sd_r1;

in sd_cmd_SEND_STATUS().

Guenter

author
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 75 lines · Philippe Mathieu-Daudé

Hi Ben,

Quoted 29 lines · Ben Dooks

The addition of specific handlers for mmc-spi for SEND_CSD and SEND_CID has broken at least Linux and possibly also u-boot's mmc-spi code.

It looks like when adding the code, it is checking for these commands to not be in sd_standby_state but the check looks to have been accidentally reversed (see below)

      if (sd->state != sd_standby_state) {
          return sd_invalid_state_for_cmd(sd, req);
      }

Linux shows the following:

[    0.293983] Waiting for root device /dev/mmcblk0...
[    1.363071] mmc0: error -38 whilst initialising SD card
[    2.418566] mmc0: error -38 whilst initialising SD card
Fixes da954d0e32444f122a4 ("hw/sd/sdcard: Add spi_cmd_SEND_CSD/CID handlers (CMD9 & CMD10)")
---
  hw/sd/sd.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/sd.c +0 -0
index 49fc79cf8a..e6c1ba7c5d 100644

hw/sd/sd.c static sd_rsp_type_t sd_cmd_SEND_IF_COND(SDState · line 1321

Quoted 6 lines
hw/sd/sd.c +1 -1
@@ -1317,7 +1317,7 @@ static sd_rsp_type_t sd_cmd_SEND_IF_COND(SDState 
 *sd, SDRequest req)
  /* CMD9 */
  static sd_rsp_type_t spi_cmd_SEND_CSD(SDState *sd, SDRequest req)
  {
-    if (sd->state != sd_standby_state) {
+    if (sd->state == sd_standby_state) {

This happens to work in your case by luck.

Since we switch to sd_sendingdata_state in sd_cmd_to_sendingdata(), we need to check for sd_transfer_state -- the spec is wrong here! --.

Btw sd_cmd_to_sendingdata() already checks for that.

hw/sd/sd.c static sd_rsp_type_t sd_cmd_SEND_CSD(SDState · line 1340

Quoted 11 lines
hw/sd/sd.c +0 -0
          return sd_invalid_state_for_cmd(sd, req);
      }

return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd,
req),

hw/sd/sd.c +1 -1
@@ -1336,7 +1336,7 @@ static sd_rsp_type_t sd_cmd_SEND_CSD(SDState 
 *sd, SDRequest req)
  /* CMD10 */
  static sd_rsp_type_t spi_cmd_SEND_CID(SDState *sd, SDRequest req)
  {
-    if (sd->state != sd_standby_state) {
+    if (sd->state == sd_standby_state) {

Ditto.

hw/sd/sd.c static sd_rsp_type_t spi_cmd_SEND_CID(SDState · line 1348

Quoted 10 lines
hw/sd/sd.c +0 -0
          return sd_invalid_state_for_cmd(sd, req);
      }

return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd,
req),

hw/sd/sd.c +1 -1
@@ -1345,7 +1345,7 @@ static sd_rsp_type_t spi_cmd_SEND_CID(SDState 
 *sd, SDRequest req)
    static sd_rsp_type_t sd_cmd_SEND_CID(SDState *sd, SDRequest req)
  {
-    if (sd->state != sd_standby_state) {
+    if (sd->state == sd_standby_state) {

Nack, you just broke SD card implementation. Have a look at the "4.8 Card State Transition Table" in the spec.

Quoted 5 lines

hw/sd/sd.c static sd_rsp_type_t spi_cmd_SEND_CID(SDState *sd, SDRequest req) · line 1351

Quoted 3 lines
hw/sd/sd.c +0 -0
          return sd_invalid_state_for_cmd(sd, req);
      }
 

Regards,
Phil.

ok, so what is the correct fix? the sd-spi has been broken for some time.

out of office at the moment so may not be able to test anything until 5th August

#4 lore raw
Quoted 31 lines · Ben Dooks
Quoted 28 lines · Philippe Mathieu-Daudé

Hi Ben,

Quoted 25 lines · Ben Dooks

The addition of specific handlers for mmc-spi for SEND_CSD and SEND_CID has broken at least Linux and possibly also u-boot's mmc-spi code.

It looks like when adding the code, it is checking for these commands to not be in sd_standby_state but the check looks to have been accidentally reversed (see below)

      if (sd->state != sd_standby_state) {
          return sd_invalid_state_for_cmd(sd, req);
      }

Linux shows the following:

[    0.293983] Waiting for root device /dev/mmcblk0...
[    1.363071] mmc0: error -38 whilst initialising SD card
[    2.418566] mmc0: error -38 whilst initialising SD card
Fixes da954d0e32444f122a4 ("hw/sd/sdcard: Add spi_cmd_SEND_CSD/CID

handlers (CMD9 & CMD10)")

---
  hw/sd/sd.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)

...

Quoted 3 lines

ok, so what is the correct fix? the sd-spi has been broken for some time.

FWIW, I use the patch below on top of upstream qemu. I have no idea if it is correct, but it fixes the problem for me.

Guenter

---
From 87a2004005eb47758c524b54dd3fbc68a00e317f Mon Sep 17 00:00:00 2001
From: Guenter Roeck <[email protected]>
Date: Thu, 24 Oct 2024 12:16:44 -0700
Subject: [PATCH] sd: Fix boot failures seen on sifive_u

sifive_u fails to boot from SD. This patch fixes the problem.

---
 hw/sd/sd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/sd.c +3 -3
index c275fdda2d..f5c44a4a86 100644
@@ -1520,7 +1520,7 @@ static sd_rsp_type_t emmc_cmd_SEND_EXT_CSD(SDState *sd, SDRequest req)
 /* CMD9 */
 static sd_rsp_type_t spi_cmd_SEND_CSD(SDState *sd, SDRequest req)
 {
-    if (sd->state != sd_standby_state) {
+    if (sd->state != sd_transfer_state) {
         return sd_invalid_state_for_cmd(sd, req);
     }
     return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd, req),
@@ -1539,7 +1539,7 @@ static sd_rsp_type_t sd_cmd_SEND_CSD(SDState *sd, SDRequest req)
 /* CMD10 */
 static sd_rsp_type_t spi_cmd_SEND_CID(SDState *sd, SDRequest req)
 {
-    if (sd->state != sd_standby_state) {
+    if (sd->state != sd_transfer_state) {
         return sd_invalid_state_for_cmd(sd, req);
     }
     return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd, req),
@@ -1592,7 +1592,7 @@ static sd_rsp_type_t sd_cmd_SEND_STATUS(SDState *sd, SDRequest req)
     }
 
     if (sd_is_spi(sd)) {
-        return sd_r2_s;
+        return sd_r1;
     }
 
     return sd_req_rca_same(sd, req) ? sd_r1 : sd_r0;
Signature · 1 line
2.45.2
#5 lore raw
Quoted 32 lines · Guenter Roeck
Quoted 24 lines · Ben Dooks
Quoted 21 lines · Philippe Mathieu-Daudé

Hi Ben,

Quoted 18 lines · Ben Dooks

The addition of specific handlers for mmc-spi for SEND_CSD and SEND_CID has broken at least Linux and possibly also u-boot's mmc-spi code.

It looks like when adding the code, it is checking for these commands to not be in sd_standby_state but the check looks to have been accidentally reversed (see below)

       if (sd->state != sd_standby_state) {
           return sd_invalid_state_for_cmd(sd, req);
       }

Linux shows the following:

[    0.293983] Waiting for root device /dev/mmcblk0...
[    1.363071] mmc0: error -38 whilst initialising SD card
[    2.418566] mmc0: error -38 whilst initialising SD card
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Fixes da954d0e32444f122a4 ("hw/sd/sdcard: Add spi_cmd_SEND_CSD/CID

handlers (CMD9 & CMD10)")

---
   hw/sd/sd.c | 6 +++---
   1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 3 lines

...

Quoted 2 lines

ok, so what is the correct fix? the sd-spi has been broken for some time.

I couldn't figure yet the problem with SEND_CSD and SEND_CID *not* being in sd_standby_state, but I figured what was missing with R2 return in SEND_STATUS. I'll post the series ASAP.

About "broken for some time", I was only testing the SPI mode with the Gumstix machines, but we removed their support in commit a2ccff4d2bc ("hw/arm: Remove 'connex' and 'verdex' machines").
For older SPI testing, see:
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 49 lines

FWIW, I use the patch below on top of upstream qemu. I have no idea if it is correct, but it fixes the problem for me.

Guenter

---
From 87a2004005eb47758c524b54dd3fbc68a00e317f Mon Sep 17 00:00:00 2001
From: Guenter Roeck <[email protected]>
Date: Thu, 24 Oct 2024 12:16:44 -0700
Subject: [PATCH] sd: Fix boot failures seen on sifive_u

sifive_u fails to boot from SD. This patch fixes the problem.

---
  hw/sd/sd.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/sd.c +3 -3
index c275fdda2d..f5c44a4a86 100644
@@ -1520,7 +1520,7 @@ static sd_rsp_type_t emmc_cmd_SEND_EXT_CSD(SDState *sd, SDRequest req)
  /* CMD9 */
  static sd_rsp_type_t spi_cmd_SEND_CSD(SDState *sd, SDRequest req)
  {
-    if (sd->state != sd_standby_state) {
+    if (sd->state != sd_transfer_state) {
          return sd_invalid_state_for_cmd(sd, req);
      }
      return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd, req),
@@ -1539,7 +1539,7 @@ static sd_rsp_type_t sd_cmd_SEND_CSD(SDState *sd, SDRequest req)
  /* CMD10 */
  static sd_rsp_type_t spi_cmd_SEND_CID(SDState *sd, SDRequest req)
  {
-    if (sd->state != sd_standby_state) {
+    if (sd->state != sd_transfer_state) {
          return sd_invalid_state_for_cmd(sd, req);
      }
      return sd_cmd_to_sendingdata(sd, req, sd_req_get_address(sd, req),
@@ -1592,7 +1592,7 @@ static sd_rsp_type_t sd_cmd_SEND_STATUS(SDState *sd, SDRequest req)
      }
 
      if (sd_is_spi(sd)) {
-        return sd_r2_s;
+        return sd_r1;
      }
 
      return sd_req_rca_same(sd, req) ? sd_r1 : sd_r0;