[RFC PATCH 03/10] hw/sd: Move proto_name to SDProto structure

Philippe Mathieu-Daudé posted 10 patches 4 years, 5 months ago
[RFC PATCH 03/10] hw/sd: Move proto_name to SDProto structure
Posted by Philippe Mathieu-Daudé 4 years, 5 months ago
Introduce a new structure to hold the bus protocol specific
fields: SDProto. The first field is the protocol name.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 hw/sd/sd.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index d71ec81c22a..a1cc8ab0be8 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -88,6 +88,10 @@ enum SDCardStates {
     sd_disconnect_state,
 };
 
+typedef struct SDProto {
+    const char *name;
+} SDProto;
+
 struct SDState {
     DeviceState parent_obj;
 
@@ -112,6 +116,7 @@ struct SDState {
 
     /* Runtime changeables */
 
+    const SDProto *proto;   /* Bus protocol */
     uint32_t mode;    /* current card mode, one of SDCardModes */
     int32_t state;    /* current card state, one of SDCardStates */
     uint32_t vhs;
@@ -138,7 +143,6 @@ struct SDState {
     qemu_irq readonly_cb;
     qemu_irq inserted_cb;
     QEMUTimer *ocr_power_timer;
-    const char *proto_name;
     bool enable;
     uint8_t dat_lines;
     bool cmd_line;
@@ -951,8 +955,8 @@ static bool address_in_range(SDState *sd, const char *desc,
 
 static sd_rsp_type_t sd_invalid_state_for_cmd(SDState *sd, SDRequest req)
 {
-    qemu_log_mask(LOG_GUEST_ERROR, "SD: CMD%i in a wrong state: %s\n",
-                  req.cmd, sd_state_name(sd->state));
+    qemu_log_mask(LOG_GUEST_ERROR, "%s: CMD%i in a wrong state: %s\n",
+                  sd->proto->name, req.cmd, sd_state_name(sd->state));
 
     return sd_illegal;
 }
@@ -966,7 +970,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
      * However there is no ACMD55, so we want to trace this particular case.
      */
     if (req.cmd != 55 || sd->expecting_acmd) {
-        trace_sdcard_normal_command(sd->proto_name,
+        trace_sdcard_normal_command(sd->proto->name,
                                     sd_cmd_name(req.cmd), req.cmd,
                                     req.arg, sd_state_name(sd->state));
     }
@@ -1526,7 +1530,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
 static sd_rsp_type_t sd_app_command(SDState *sd,
                                     SDRequest req)
 {
-    trace_sdcard_app_command(sd->proto_name, sd_acmd_name(req.cmd),
+    trace_sdcard_app_command(sd->proto->name, sd_acmd_name(req.cmd),
                              req.cmd, req.arg, sd_state_name(sd->state));
     sd->card_status |= APP_CMD;
     switch (req.cmd) {
@@ -1820,7 +1824,7 @@ void sd_write_byte(SDState *sd, uint8_t value)
     if (sd->card_status & (ADDRESS_ERROR | WP_VIOLATION))
         return;
 
-    trace_sdcard_write_data(sd->proto_name,
+    trace_sdcard_write_data(sd->proto->name,
                             sd_acmd_name(sd->current_cmd),
                             sd->current_cmd, value);
     switch (sd->current_cmd) {
@@ -1976,7 +1980,7 @@ uint8_t sd_read_byte(SDState *sd)
 
     io_len = (sd->ocr & (1 << 30)) ? 512 : sd->blk_len;
 
-    trace_sdcard_read_data(sd->proto_name,
+    trace_sdcard_read_data(sd->proto->name,
                            sd_acmd_name(sd->current_cmd),
                            sd->current_cmd, io_len);
     switch (sd->current_cmd) {
@@ -2095,6 +2099,14 @@ void sd_enable(SDState *sd, bool enable)
     sd->enable = enable;
 }
 
+static const SDProto sd_proto_spi = {
+    .name = "SPI",
+};
+
+static const SDProto sd_proto_sd = {
+    .name = "SD",
+};
+
 static void sd_instance_init(Object *obj)
 {
     SDState *sd = SD_CARD(obj);
@@ -2115,7 +2127,7 @@ static void sd_realize(DeviceState *dev, Error **errp)
     SDState *sd = SD_CARD(dev);
     int ret;
 
-    sd->proto_name = sd->spi ? "SPI" : "SD";
+    sd->proto = sd->spi ? &sd_proto_spi : &sd_proto_sd;
 
     switch (sd->spec_version) {
     case SD_PHY_SPECv1_10_VERS
-- 
2.31.1

Re: [RFC PATCH 03/10] hw/sd: Move proto_name to SDProto structure
Posted by Bin Meng 4 years, 5 months ago
On Thu, Jun 24, 2021 at 10:24 PM Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
>
> Introduce a new structure to hold the bus protocol specific
> fields: SDProto. The first field is the protocol name.
>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>  hw/sd/sd.c | 28 ++++++++++++++++++++--------
>  1 file changed, 20 insertions(+), 8 deletions(-)
>

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

Re: [RFC PATCH 03/10] hw/sd: Move proto_name to SDProto structure
Posted by Cédric Le Goater 4 years, 5 months ago
On 6/24/21 4:22 PM, Philippe Mathieu-Daudé wrote:
> Introduce a new structure to hold the bus protocol specific
> fields: SDProto. The first field is the protocol name.
> 
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>  hw/sd/sd.c | 28 ++++++++++++++++++++--------
>  1 file changed, 20 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
> index d71ec81c22a..a1cc8ab0be8 100644
> --- a/hw/sd/sd.c
> +++ b/hw/sd/sd.c
> @@ -88,6 +88,10 @@ enum SDCardStates {
>      sd_disconnect_state,
>  };
>  
> +typedef struct SDProto {
> +    const char *name;
> +} SDProto;


Why not use an Object class ? 

C.


> +
>  struct SDState {
>      DeviceState parent_obj;
>  
> @@ -112,6 +116,7 @@ struct SDState {
>  
>      /* Runtime changeables */
>  
> +    const SDProto *proto;   /* Bus protocol */
>      uint32_t mode;    /* current card mode, one of SDCardModes */
>      int32_t state;    /* current card state, one of SDCardStates */
>      uint32_t vhs;
> @@ -138,7 +143,6 @@ struct SDState {
>      qemu_irq readonly_cb;
>      qemu_irq inserted_cb;
>      QEMUTimer *ocr_power_timer;
> -    const char *proto_name;
>      bool enable;
>      uint8_t dat_lines;
>      bool cmd_line;
> @@ -951,8 +955,8 @@ static bool address_in_range(SDState *sd, const char *desc,
>  
>  static sd_rsp_type_t sd_invalid_state_for_cmd(SDState *sd, SDRequest req)
>  {
> -    qemu_log_mask(LOG_GUEST_ERROR, "SD: CMD%i in a wrong state: %s\n",
> -                  req.cmd, sd_state_name(sd->state));
> +    qemu_log_mask(LOG_GUEST_ERROR, "%s: CMD%i in a wrong state: %s\n",
> +                  sd->proto->name, req.cmd, sd_state_name(sd->state));
>  
>      return sd_illegal;
>  }
> @@ -966,7 +970,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
>       * However there is no ACMD55, so we want to trace this particular case.
>       */
>      if (req.cmd != 55 || sd->expecting_acmd) {
> -        trace_sdcard_normal_command(sd->proto_name,
> +        trace_sdcard_normal_command(sd->proto->name,
>                                      sd_cmd_name(req.cmd), req.cmd,
>                                      req.arg, sd_state_name(sd->state));
>      }
> @@ -1526,7 +1530,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
>  static sd_rsp_type_t sd_app_command(SDState *sd,
>                                      SDRequest req)
>  {
> -    trace_sdcard_app_command(sd->proto_name, sd_acmd_name(req.cmd),
> +    trace_sdcard_app_command(sd->proto->name, sd_acmd_name(req.cmd),
>                               req.cmd, req.arg, sd_state_name(sd->state));
>      sd->card_status |= APP_CMD;
>      switch (req.cmd) {
> @@ -1820,7 +1824,7 @@ void sd_write_byte(SDState *sd, uint8_t value)
>      if (sd->card_status & (ADDRESS_ERROR | WP_VIOLATION))
>          return;
>  
> -    trace_sdcard_write_data(sd->proto_name,
> +    trace_sdcard_write_data(sd->proto->name,
>                              sd_acmd_name(sd->current_cmd),
>                              sd->current_cmd, value);
>      switch (sd->current_cmd) {
> @@ -1976,7 +1980,7 @@ uint8_t sd_read_byte(SDState *sd)
>  
>      io_len = (sd->ocr & (1 << 30)) ? 512 : sd->blk_len;
>  
> -    trace_sdcard_read_data(sd->proto_name,
> +    trace_sdcard_read_data(sd->proto->name,
>                             sd_acmd_name(sd->current_cmd),
>                             sd->current_cmd, io_len);
>      switch (sd->current_cmd) {
> @@ -2095,6 +2099,14 @@ void sd_enable(SDState *sd, bool enable)
>      sd->enable = enable;
>  }
>  
> +static const SDProto sd_proto_spi = {
> +    .name = "SPI",
> +};
> +
> +static const SDProto sd_proto_sd = {
> +    .name = "SD",
> +};
> +
>  static void sd_instance_init(Object *obj)
>  {
>      SDState *sd = SD_CARD(obj);
> @@ -2115,7 +2127,7 @@ static void sd_realize(DeviceState *dev, Error **errp)
>      SDState *sd = SD_CARD(dev);
>      int ret;
>  
> -    sd->proto_name = sd->spi ? "SPI" : "SD";
> +    sd->proto = sd->spi ? &sd_proto_spi : &sd_proto_sd;
>  
>      switch (sd->spec_version) {
>      case SD_PHY_SPECv1_10_VERS
>