[PATCH v42 11/98] hw/sd/sdcard: Simplify sd_inactive_state handling

Philippe Mathieu-Daudé posted 98 patches 2 months, 2 weeks ago
Maintainers: "Philippe Mathieu-Daudé" <philmd@linaro.org>, Bin Meng <bmeng.cn@gmail.com>, Tyrone Ting <kfting@nuvoton.com>, Hao Wu <wuhaotsh@google.com>, Thomas Huth <thuth@redhat.com>, Laurent Vivier <lvivier@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
There is a newer version of this series
[PATCH v42 11/98] hw/sd/sdcard: Simplify sd_inactive_state handling
Posted by Philippe Mathieu-Daudé 2 months, 2 weeks ago
Card entering sd_inactive_state powers off, and won't respond
anymore. Handle that once when entering sd_do_command().

Remove condition always true in sd_cmd_GO_IDLE_STATE().

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Tested-by: Cédric Le Goater <clg@redhat.com>
---
 hw/sd/sd.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index fbdfafa3a6..7533a78cf6 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -1081,10 +1081,8 @@ static sd_rsp_type_t sd_cmd_unimplemented(SDState *sd, SDRequest req)
 /* CMD0 */
 static sd_rsp_type_t sd_cmd_GO_IDLE_STATE(SDState *sd, SDRequest req)
 {
-    if (sd->state != sd_inactive_state) {
-        sd->state = sd_idle_state;
-        sd_reset(DEVICE(sd));
-    }
+    sd->state = sd_idle_state;
+    sd_reset(DEVICE(sd));
 
     return sd_is_spi(sd) ? sd_r1 : sd_r0;
 }
@@ -1579,7 +1577,6 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
         switch (sd->state) {
         case sd_ready_state:
         case sd_identification_state:
-        case sd_inactive_state:
             return sd_illegal;
         case sd_idle_state:
             if (rca) {
@@ -1800,6 +1797,11 @@ int sd_do_command(SDState *sd, SDRequest *req,
         return 0;
     }
 
+    if (sd->state == sd_inactive_state) {
+        rtype = sd_illegal;
+        goto send_response;
+    }
+
     if (sd_req_crc_validate(req)) {
         sd->card_status |= COM_CRC_ERROR;
         rtype = sd_illegal;
-- 
2.41.0


Re: [PATCH v42 11/98] hw/sd/sdcard: Simplify sd_inactive_state handling
Posted by Cédric Le Goater 2 months, 2 weeks ago
On 6/28/24 9:00 AM, Philippe Mathieu-Daudé wrote:
> Card entering sd_inactive_state powers off, and won't respond
> anymore. Handle that once when entering sd_do_command().
> 
> Remove condition always true in sd_cmd_GO_IDLE_STATE().
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Tested-by: Cédric Le Goater <clg@redhat.com>


Reviewed-by: Cédric Le Goater <clg@redhat.com>

Thanks,

C.


> ---
>   hw/sd/sd.c | 12 +++++++-----
>   1 file changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
> index fbdfafa3a6..7533a78cf6 100644
> --- a/hw/sd/sd.c
> +++ b/hw/sd/sd.c
> @@ -1081,10 +1081,8 @@ static sd_rsp_type_t sd_cmd_unimplemented(SDState *sd, SDRequest req)
>   /* CMD0 */
>   static sd_rsp_type_t sd_cmd_GO_IDLE_STATE(SDState *sd, SDRequest req)
>   {
> -    if (sd->state != sd_inactive_state) {
> -        sd->state = sd_idle_state;
> -        sd_reset(DEVICE(sd));
> -    }
> +    sd->state = sd_idle_state;
> +    sd_reset(DEVICE(sd));
>   
>       return sd_is_spi(sd) ? sd_r1 : sd_r0;
>   }
> @@ -1579,7 +1577,6 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
>           switch (sd->state) {
>           case sd_ready_state:
>           case sd_identification_state:
> -        case sd_inactive_state:
>               return sd_illegal;
>           case sd_idle_state:
>               if (rca) {
> @@ -1800,6 +1797,11 @@ int sd_do_command(SDState *sd, SDRequest *req,
>           return 0;
>       }
>   
> +    if (sd->state == sd_inactive_state) {
> +        rtype = sd_illegal;
> +        goto send_response;
> +    }
> +
>       if (sd_req_crc_validate(req)) {
>           sd->card_status |= COM_CRC_ERROR;
>           rtype = sd_illegal;