qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: Ben Dooks <ben.dooks@codethink.co.uk>,
	qemu-block@nongnu.org, bmeng.cn@gmail.com
Cc: qemu-devel@nongnu.org, Guenter Roeck <linux@roeck-us.net>
Subject: Re: [PATCH] hw/sd/sdcard: fix spi_cmd_SEND_CSD/CID state check
Date: Tue, 29 Jul 2025 15:51:33 +0200	[thread overview]
Message-ID: <893a7943-e5ac-4c42-b30f-1dfa262b923e@linaro.org> (raw)
In-Reply-To: <20250724105807.697915-1-ben.dooks@codethink.co.uk>

Hi Ben,

On 24/7/25 12:58, Ben Dooks wrote:
> 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)")
> Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
> ---
>   hw/sd/sd.c | 6 +++---
>   1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
> index 49fc79cf8a..e6c1ba7c5d 100644
> --- a/hw/sd/sd.c
> +++ b/hw/sd/sd.c
> @@ -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.


>           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.

>           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.

>           return sd_invalid_state_for_cmd(sd, req);
>       }
>   
Regards,
Phil.


  reply	other threads:[~2025-07-29 13:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-24 10:58 [PATCH] hw/sd/sdcard: fix spi_cmd_SEND_CSD/CID state check Ben Dooks
2025-07-29 13:51 ` Philippe Mathieu-Daudé [this message]
2025-07-29 14:06   ` Guenter Roeck
2025-07-29 15:06   ` Ben Dooks
2025-07-29 16:35     ` Guenter Roeck
2025-07-31 21:00       ` Philippe Mathieu-Daudé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=893a7943-e5ac-4c42-b30f-1dfa262b923e@linaro.org \
    --to=philmd@linaro.org \
    --cc=ben.dooks@codethink.co.uk \
    --cc=bmeng.cn@gmail.com \
    --cc=linux@roeck-us.net \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).